2 % (c) The AQUA Project, Glasgow University, 1994-1998
5 \section[Foreign]{Module @Foreign@}
10 ForeignObj -- abstract, instance of: Eq
11 , makeForeignObj -- :: Addr{-obj-} -> Addr{-finaliser-} -> IO ForeignObj
12 , mkForeignObj -- :: Addr -> IO ForeignObj
13 , writeForeignObj -- :: ForeignObj -> Addr{-new obj-} -> IO ()
14 , addForeignFinalizer -- :: ForeignObj -> IO () -> IO ()
16 -- the coercion from a foreign obj to an addr is unsafe,
17 -- and should not be used unless absolutely necessary.
18 , foreignObjToAddr -- :: ForeignObj -> IO Addr
20 , StablePtr {-a-} -- abstract.
21 , makeStablePtr -- :: a -> IO (StablePtr a)
22 , deRefStablePtr -- :: StablePtr a -> IO a
23 , freeStablePtr -- :: StablePtr a -> IO ()
25 , indexCharOffForeignObj -- :: ForeignObj -> Int -> Char
27 , indexIntOffForeignObj -- :: ForeignObj -> Int -> Int
28 , indexInt8OffForeignObj -- :: ForeignObj -> Int -> Int8
29 , indexInt16OffForeignObj -- :: ForeignObj -> Int -> Int16
30 , indexInt32OffForeignObj -- :: ForeignObj -> Int -> Int32
31 , indexInt64OffForeignObj -- :: ForeignObj -> Int -> Int64
33 , indexWord8OffForeignObj -- :: ForeignObj -> Int -> Word
34 , indexWord8OffForeignObj -- :: ForeignObj -> Int -> Word8
35 , indexWord16OffForeignObj -- :: ForeignObj -> Int -> Word16
36 , indexWord32OffForeignObj -- :: ForeignObj -> Int -> Word32
37 , indexWord64OffForeignObj -- :: ForeignObj -> Int -> Word64
39 , indexAddrOffForeignObj -- :: ForeignObj -> Int -> Addr
40 , indexFloatOffForeignObj -- :: ForeignObj -> Int -> Float
41 , indexDoubleOffForeignObj -- :: ForeignObj -> Int -> Double
43 , readCharOffForeignObj -- :: ForeignObj -> Int -> IO Char
44 , readIntOffForeignObj -- :: ForeignObj -> Int -> IO Int
45 , readInt8OffForeignObj -- :: ForeignObj -> Int -> IO Int8
46 , readInt16OffForeignObj -- :: ForeignObj -> Int -> IO Int16
47 , readInt32OffForeignObj -- :: ForeignObj -> Int -> IO Int32
48 , readInt64OffForeignObj -- :: ForeignObj -> Int -> IO Int64
50 , readWordOffForeignObj -- :: ForeignObj -> Int -> IO Word
51 , readWord8OffForeignObj -- :: ForeignObj -> Int -> IO Word8
52 , readWord16OffForeignObj -- :: ForeignObj -> Int -> IO Word16
53 , readWord32OffForeignObj -- :: ForeignObj -> Int -> IO Word32
54 , readWord64OffForeignObj -- :: ForeignObj -> Int -> IO Word64
56 , readAddrOffForeignObj -- :: ForeignObj -> Int -> IO Addr
57 , readFloatOffForeignObj -- :: ForeignObj -> Int -> IO Float
58 , readDoubleOffForeignObj -- :: ForeignObj -> Int -> IO Double
60 , writeCharOffForeignObj -- :: ForeignObj -> Int -> Char -> IO ()
61 , writeIntOffForeignObj -- :: ForeignObj -> Int -> Int -> IO ()
62 , writeInt8OffForeignObj -- :: ForeignObj -> Int -> Int8 -> IO ()
63 , writeInt16OffForeignObj -- :: ForeignObj -> Int -> Int16 -> IO ()
64 , writeInt32OffForeignObj -- :: ForeignObj -> Int -> Int32 -> IO ()
65 , writeInt64OffForeignObj -- :: ForeignObj -> Int -> Int64 -> IO ()
67 , writeWordOffForeignObj -- :: ForeignObj -> Int -> Word -> IO ()
68 , writeWord8OffForeignObj -- :: ForeignObj -> Int -> Word8 -> IO ()
69 , writeWord16OffForeignObj -- :: ForeignObj -> Int -> Word16 -> IO ()
70 , writeWord32OffForeignObj -- :: ForeignObj -> Int -> Word32 -> IO ()
71 , writeWord64OffForeignObj -- :: ForeignObj -> Int -> Word64 -> IO ()
73 , writeAddrOffForeignObj -- :: ForeignObj -> Int -> Addr -> IO ()
74 , writeFloatOffForeignObj -- :: ForeignObj -> Int -> Float -> IO ()
75 , writeDoubleOffForeignObj -- :: ForeignObj -> Int -> Double -> IO ()
79 import PrelForeign hiding ( makeForeignObj )
81 import qualified PrelForeign as PF ( makeForeignObj )
82 import PrelBase ( Int(..), Double(..), Float(..), Char(..) )
83 import PrelGHC ( indexCharOffForeignObj#, indexIntOffForeignObj#,
84 indexAddrOffForeignObj#, indexFloatOffForeignObj#,
85 indexDoubleOffForeignObj#, indexWordOffForeignObj#
87 import PrelAddr ( Addr(..), Word(..) )
88 import PrelWeak ( addForeignFinalizer )
91 indexWord8OffForeignObj
92 , indexWord16OffForeignObj
93 , indexWord32OffForeignObj
94 , indexWord64OffForeignObj
95 , readWord8OffForeignObj
96 , readWord16OffForeignObj
97 , readWord32OffForeignObj
98 , readWord64OffForeignObj
99 , writeWord8OffForeignObj
100 , writeWord16OffForeignObj
101 , writeWord32OffForeignObj
102 , writeWord64OffForeignObj
107 indexInt8OffForeignObj
108 , indexInt16OffForeignObj
109 , indexInt32OffForeignObj
110 , indexInt64OffForeignObj
111 , readInt8OffForeignObj
112 , readInt16OffForeignObj
113 , readInt32OffForeignObj
114 , readInt64OffForeignObj
115 , writeInt8OffForeignObj
116 , writeInt16OffForeignObj
117 , writeInt32OffForeignObj
118 , writeInt64OffForeignObj
120 import PrelIOBase ( IO(..) )
124 foreignObjToAddr :: ForeignObj -> IO Addr
125 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
129 makeForeignObj :: Addr -> Addr -> IO ForeignObj
130 makeForeignObj obj finalizer = do
131 fobj <- PF.makeForeignObj obj
132 addForeignFinalizer fobj (app0 finalizer fobj)
135 mkForeignObj :: Addr -> IO ForeignObj
136 mkForeignObj = PF.makeForeignObj
138 foreign import dynamic unsafe app0 :: Addr -> (ForeignObj -> IO ())
144 indexCharOffForeignObj :: ForeignObj -> Int -> Char
145 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
147 indexIntOffForeignObj :: ForeignObj -> Int -> Int
148 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
150 indexWordOffForeignObj :: ForeignObj -> Int -> Word
151 indexWordOffForeignObj (ForeignObj fo#) (I# i#) = W# (indexWordOffForeignObj# fo# i#)
153 indexAddrOffForeignObj :: ForeignObj -> Int -> Addr
154 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
156 indexFloatOffForeignObj :: ForeignObj -> Int -> Float
157 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
159 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
160 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
162 -- read value out of mutable memory
163 readCharOffForeignObj :: ForeignObj -> Int -> IO Char
164 readCharOffForeignObj fo i = _casm_ `` %r=(StgChar)(((StgChar*)%0)[(StgInt)%1]); '' fo i
166 readIntOffForeignObj :: ForeignObj -> Int -> IO Int
167 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
169 readWordOffForeignObj :: ForeignObj -> Int -> IO Word
170 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
172 readAddrOffForeignObj :: ForeignObj -> Int -> IO Addr
173 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
175 readFloatOffForeignObj :: ForeignObj -> Int -> IO Float
176 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
178 readDoubleOffForeignObj :: ForeignObj -> Int -> IO Double
179 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
183 writeCharOffForeignObj :: ForeignObj -> Int -> Char -> IO ()
184 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
186 writeIntOffForeignObj :: ForeignObj -> Int -> Int -> IO ()
187 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
189 writeWordOffForeignObj :: ForeignObj -> Int -> Word -> IO ()
190 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
192 writeAddrOffForeignObj :: ForeignObj -> Int -> Addr -> IO ()
193 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
195 writeFloatOffForeignObj :: ForeignObj -> Int -> Float -> IO ()
196 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
198 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
199 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e