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 ()
26 import PrelForeign hiding ( makeForeignObj )
28 import qualified PrelForeign as PF ( makeForeignObj )
29 import PrelBase ( Int(..), Double(..), Float(..), Char(..) )
30 import PrelGHC ( indexCharOffForeignObj#, indexIntOffForeignObj#,
31 indexAddrOffForeignObj#, indexFloatOffForeignObj#,
32 indexDoubleOffForeignObj#
34 import PrelAddr ( Addr(..), Word(..) )
35 import PrelWeak ( addForeignFinalizer )
38 indexWord8OffForeignObj
39 , indexWord16OffForeignObj
40 , indexWord32OffForeignObj
41 , indexWord64OffForeignObj
42 , readWord8OffForeignObj
43 , readWord16OffForeignObj
44 , readWord32OffForeignObj
45 , readWord64OffForeignObj
46 , writeWord8OffForeignObj
47 , writeWord16OffForeignObj
48 , writeWord32OffForeignObj
49 , writeWord64OffForeignObj
54 indexInt8OffForeignObj
55 , indexInt16OffForeignObj
56 , indexInt32OffForeignObj
57 , indexInt64OffForeignObj
58 , readInt8OffForeignObj
59 , readInt16OffForeignObj
60 , readInt32OffForeignObj
61 , readInt64OffForeignObj
62 , writeInt8OffForeignObj
63 , writeInt16OffForeignObj
64 , writeInt32OffForeignObj
65 , writeInt64OffForeignObj
67 import PrelIOBase ( IO(..) )
71 foreignObjToAddr :: ForeignObj -> IO Addr
72 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
76 makeForeignObj :: Addr -> Addr -> IO ForeignObj
77 makeForeignObj obj finalizer = do
78 fobj <- PF.makeForeignObj obj
79 addForeignFinalizer fobj (app0 finalizer fobj)
82 mkForeignObj :: Addr -> IO ForeignObj
83 mkForeignObj = PF.makeForeignObj
85 foreign import dynamic unsafe app0 :: Addr -> (ForeignObj -> IO ())
91 indexCharOffForeignObj :: ForeignObj -> Int -> Char
92 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
94 indexIntOffForeignObj :: ForeignObj -> Int -> Int
95 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
97 indexAddrOffForeignObj :: ForeignObj -> Int -> Addr
98 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
100 indexFloatOffForeignObj :: ForeignObj -> Int -> Float
101 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
103 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
104 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
106 -- read value out of mutable memory
107 readCharOffForeignObj :: ForeignObj -> Int -> IO Char
108 readCharOffForeignObj fo i = _casm_ `` %r=(StgChar)(((StgChar*)%0)[(StgInt)%1]); '' fo i
110 readIntOffForeignObj :: ForeignObj -> Int -> IO Int
111 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
113 readWordOffForeignObj :: ForeignObj -> Int -> IO Word
114 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
116 readAddrOffForeignObj :: ForeignObj -> Int -> IO Addr
117 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
119 readFloatOffForeignObj :: ForeignObj -> Int -> IO Float
120 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
122 readDoubleOffForeignObj :: ForeignObj -> Int -> IO Double
123 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
127 writeCharOffForeignObj :: ForeignObj -> Int -> Char -> IO ()
128 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
130 writeIntOffForeignObj :: ForeignObj -> Int -> Int -> IO ()
131 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
133 writeWordOffForeignObj :: ForeignObj -> Int -> Word -> IO ()
134 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
136 writeAddrOffForeignObj :: ForeignObj -> Int -> Addr -> IO ()
137 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
139 writeFloatOffForeignObj :: ForeignObj -> Int -> Float -> IO ()
140 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
142 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
143 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e