eaf8ef3775423d023814600fe98150d1c01c6ccc
[ghc-hetmet.git] / ghc / lib / exts / Foreign.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4
5 \section[Foreign]{Module @Foreign@}
6
7 \begin{code}
8 module Foreign 
9        ( 
10          ForeignObj       -- abstract, instance of: Eq
11        , makeForeignObj   -- :: Addr{-obj-} -> Addr{-finaliser-} -> IO ForeignObj
12        , writeForeignObj  -- :: ForeignObj  -> Addr{-new obj-}   -> IO ()
13        , foreignObjToAddr -- :: ForeignObj  -> IO Addr
14            -- the coercion from a foreign obj. to an addr. is unsafe,
15            -- and should not be used unless absolutely necessary.
16        
17        , StablePtr {-a-} -- abstract.
18        , makeStablePtr   -- :: a -> IO (StablePtr a)
19        , deRefStablePtr  -- :: StablePtr a -> IO a
20        , freeStablePtr   -- :: StablePtr a -> IO ()
21
22        , indexCharOffForeignObj
23        , indexAddrOffForeignObj
24        , indexIntOffForeignObj
25        , indexFloatOffForeignObj
26        , indexDoubleOffForeignObj
27        , readCharOffForeignObj
28        , readAddrOffForeignObj
29        , readIntOffForeignObj
30        , readFloatOffForeignObj
31        , readDoubleOffForeignObj
32        , writeCharOffForeignObj
33        , writeAddrOffForeignObj
34        , writeIntOffForeignObj
35        , writeFloatOffForeignObj
36        , writeDoubleOffForeignObj
37         
38        , indexWord8OffForeignObj
39        , indexWord16OffForeignObj
40        , indexWord32OffForeignObj
41        , indexWord64OffForeignObj
42        , readWord8OffForeignObj
43        , readWord16OffForeignObj
44        , readWord32OffForeignObj
45        , readWord64OffForeignObj
46        , writeWord8OffForeignObj
47        , writeWord16OffForeignObj
48        , writeWord32OffForeignObj
49        , writeWord64OffForeignObj
50
51        , indexInt8OffForeignObj
52        , indexInt16OffForeignObj
53        , indexInt32OffForeignObj
54        , indexInt64OffForeignObj
55        , readInt8OffForeignObj
56        , readInt16OffForeignObj
57        , readInt32OffForeignObj
58        , readInt64OffForeignObj
59        , writeInt8OffForeignObj
60        , writeInt16OffForeignObj
61        , writeInt32OffForeignObj
62        , writeInt64OffForeignObj
63
64        ) where
65
66 import PrelForeign
67 import PrelBase    ( Int(..), Double(..), Float(..), Char(..) )
68 import PrelGHC     ( indexCharOffForeignObj#, indexIntOffForeignObj#, 
69                      indexAddrOffForeignObj#, indexFloatOffForeignObj#, 
70                      indexDoubleOffForeignObj#
71                    )
72 import PrelAddr    ( Addr(..) )
73 import PrelCCall   ( Word(..) )
74 import Word 
75    ( 
76      indexWord8OffForeignObj
77    , indexWord16OffForeignObj
78    , indexWord32OffForeignObj
79    , indexWord64OffForeignObj
80    , readWord8OffForeignObj
81    , readWord16OffForeignObj
82    , readWord32OffForeignObj
83    , readWord64OffForeignObj
84    , writeWord8OffForeignObj
85    , writeWord16OffForeignObj
86    , writeWord32OffForeignObj
87    , writeWord64OffForeignObj
88    )
89
90 import Int
91    ( 
92      indexInt8OffForeignObj
93    , indexInt16OffForeignObj
94    , indexInt32OffForeignObj
95    , indexInt64OffForeignObj
96    , readInt8OffForeignObj
97    , readInt16OffForeignObj
98    , readInt32OffForeignObj
99    , readInt64OffForeignObj
100    , writeInt8OffForeignObj
101    , writeInt16OffForeignObj
102    , writeInt32OffForeignObj
103    , writeInt64OffForeignObj
104    )
105 import PrelIOBase ( IO(..), IOResult(..) )
106 \end{code}
107
108 \begin{code}
109 foreignObjToAddr :: ForeignObj -> IO Addr
110 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
111 \end{code}
112
113
114 \begin{code}
115 indexCharOffForeignObj   :: ForeignObj -> Int -> Char
116 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
117
118 indexIntOffForeignObj    :: ForeignObj -> Int -> Int
119 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
120
121 indexAddrOffForeignObj   :: ForeignObj -> Int -> Addr
122 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
123
124 indexFloatOffForeignObj  :: ForeignObj -> Int -> Float
125 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
126
127 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
128 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
129
130 -- read value out of mutable memory
131 readCharOffForeignObj    :: ForeignObj -> Int -> IO Char
132 readCharOffForeignObj fo i = _casm_ `` %r=(StgChar)(((StgChar*)%0)[(StgInt)%1]); '' fo i
133
134 readIntOffForeignObj     :: ForeignObj -> Int -> IO Int
135 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
136
137 readWordOffForeignObj     :: ForeignObj -> Int -> IO Word
138 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
139
140 readAddrOffForeignObj    :: ForeignObj -> Int -> IO Addr
141 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
142
143 readFloatOffForeignObj   :: ForeignObj -> Int -> IO Float
144 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
145
146 readDoubleOffForeignObj  :: ForeignObj -> Int -> IO Double
147 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
148 \end{code}
149
150 \begin{code}
151 writeCharOffForeignObj   :: ForeignObj -> Int -> Char   -> IO ()
152 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
153
154 writeIntOffForeignObj    :: ForeignObj -> Int -> Int    -> IO ()
155 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
156
157 writeWordOffForeignObj    :: ForeignObj -> Int -> Word  -> IO ()
158 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
159
160 writeAddrOffForeignObj   :: ForeignObj -> Int -> Addr   -> IO ()
161 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
162
163 writeFloatOffForeignObj  :: ForeignObj -> Int -> Float  -> IO ()
164 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
165
166 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
167 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e
168
169 \end{code}