2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
10 ForeignObj -- abstract, instance of: Eq
11 , makeForeignObj -- :: Addr{-obj-} -> IO ForeignObj
12 , writeForeignObj -- :: ForeignObj -> Addr{-new obj-} -> IO ()
13 , addForeignFinaliser -- :: ForeignObj -> IO () -> IO ()
14 , foreignObjToAddr -- :: ForeignObj -> IO Addr
15 -- the coercion from a foreign obj. to an addr. is unsafe,
16 -- and should not be used unless absolutely necessary.
18 , StablePtr {-a-} -- abstract.
19 , makeStablePtr -- :: a -> IO (StablePtr a)
20 , deRefStablePtr -- :: StablePtr a -> IO a
21 , freeStablePtr -- :: StablePtr a -> IO ()
24 import PrelForeign --hiding ( makeForeignObj )
25 --import qualified PrelForeign as PF ( makeForeignObj )
26 import PrelBase ( Int(..), Double(..), Float(..), Char(..) )
27 import PrelGHC ( indexCharOffForeignObj#, indexIntOffForeignObj#,
28 indexAddrOffForeignObj#, indexFloatOffForeignObj#,
29 indexDoubleOffForeignObj#
31 import PrelAddr ( Addr(..), Word(..) )
32 import PrelWeak ( addForeignFinaliser )
35 indexWord8OffForeignObj
36 , indexWord16OffForeignObj
37 , indexWord32OffForeignObj
38 , indexWord64OffForeignObj
39 , readWord8OffForeignObj
40 , readWord16OffForeignObj
41 , readWord32OffForeignObj
42 , readWord64OffForeignObj
43 , writeWord8OffForeignObj
44 , writeWord16OffForeignObj
45 , writeWord32OffForeignObj
46 , writeWord64OffForeignObj
51 indexInt8OffForeignObj
52 , indexInt16OffForeignObj
53 , indexInt32OffForeignObj
54 , indexInt64OffForeignObj
55 , readInt8OffForeignObj
56 , readInt16OffForeignObj
57 , readInt32OffForeignObj
58 , readInt64OffForeignObj
59 , writeInt8OffForeignObj
60 , writeInt16OffForeignObj
61 , writeInt32OffForeignObj
62 , writeInt64OffForeignObj
64 import PrelIOBase ( IO(..) )
68 foreignObjToAddr :: ForeignObj -> IO Addr
69 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
73 makeForeignObj :: Addr -> Addr -> IO ForeignObj
74 makeForeignObj obj finaliser = do
75 fobj <- PF.makeForeignObj obj
76 addForeignFinaliser fobj (app0 finaliser)
79 foreign import dynamic unsafe app0 :: Addr -> IO ()
85 indexCharOffForeignObj :: ForeignObj -> Int -> Char
86 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
88 indexIntOffForeignObj :: ForeignObj -> Int -> Int
89 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
91 indexAddrOffForeignObj :: ForeignObj -> Int -> Addr
92 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
94 indexFloatOffForeignObj :: ForeignObj -> Int -> Float
95 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
97 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
98 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
100 -- read value out of mutable memory
101 readCharOffForeignObj :: ForeignObj -> Int -> IO Char
102 readCharOffForeignObj fo i = _casm_ `` %r=(StgChar)(((StgChar*)%0)[(StgInt)%1]); '' fo i
104 readIntOffForeignObj :: ForeignObj -> Int -> IO Int
105 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
107 readWordOffForeignObj :: ForeignObj -> Int -> IO Word
108 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
110 readAddrOffForeignObj :: ForeignObj -> Int -> IO Addr
111 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
113 readFloatOffForeignObj :: ForeignObj -> Int -> IO Float
114 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
116 readDoubleOffForeignObj :: ForeignObj -> Int -> IO Double
117 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
121 writeCharOffForeignObj :: ForeignObj -> Int -> Char -> IO ()
122 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
124 writeIntOffForeignObj :: ForeignObj -> Int -> Int -> IO ()
125 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
127 writeWordOffForeignObj :: ForeignObj -> Int -> Word -> IO ()
128 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
130 writeAddrOffForeignObj :: ForeignObj -> Int -> Addr -> IO ()
131 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
133 writeFloatOffForeignObj :: ForeignObj -> Int -> Float -> IO ()
134 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
136 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
137 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e