2 % (c) The AQUA Project, Glasgow University, 1994-1998
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 )
26 --import qualified PrelForeign as PF ( makeForeignObj )
27 import PrelBase ( Int(..), Double(..), Float(..), Char(..) )
28 import PrelGHC ( indexCharOffForeignObj#, indexIntOffForeignObj#,
29 indexAddrOffForeignObj#, indexFloatOffForeignObj#,
30 indexDoubleOffForeignObj#
32 import PrelAddr ( Addr(..), Word(..) )
33 import PrelWeak ( addForeignFinaliser )
36 indexWord8OffForeignObj
37 , indexWord16OffForeignObj
38 , indexWord32OffForeignObj
39 , indexWord64OffForeignObj
40 , readWord8OffForeignObj
41 , readWord16OffForeignObj
42 , readWord32OffForeignObj
43 , readWord64OffForeignObj
44 , writeWord8OffForeignObj
45 , writeWord16OffForeignObj
46 , writeWord32OffForeignObj
47 , writeWord64OffForeignObj
52 indexInt8OffForeignObj
53 , indexInt16OffForeignObj
54 , indexInt32OffForeignObj
55 , indexInt64OffForeignObj
56 , readInt8OffForeignObj
57 , readInt16OffForeignObj
58 , readInt32OffForeignObj
59 , readInt64OffForeignObj
60 , writeInt8OffForeignObj
61 , writeInt16OffForeignObj
62 , writeInt32OffForeignObj
63 , writeInt64OffForeignObj
65 import PrelIOBase ( IO(..) )
69 foreignObjToAddr :: ForeignObj -> IO Addr
70 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
74 makeForeignObj :: Addr -> Addr -> IO ForeignObj
75 makeForeignObj obj finaliser = do
76 fobj <- PF.makeForeignObj obj
77 addForeignFinaliser fobj (app0 finaliser)
80 foreign import dynamic unsafe app0 :: Addr -> IO ()
86 indexCharOffForeignObj :: ForeignObj -> Int -> Char
87 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
89 indexIntOffForeignObj :: ForeignObj -> Int -> Int
90 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
92 indexAddrOffForeignObj :: ForeignObj -> Int -> Addr
93 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
95 indexFloatOffForeignObj :: ForeignObj -> Int -> Float
96 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
98 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
99 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
101 -- read value out of mutable memory
102 readCharOffForeignObj :: ForeignObj -> Int -> IO Char
103 readCharOffForeignObj fo i = _casm_ `` %r=(StgChar)(((StgChar*)%0)[(StgInt)%1]); '' fo i
105 readIntOffForeignObj :: ForeignObj -> Int -> IO Int
106 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
108 readWordOffForeignObj :: ForeignObj -> Int -> IO Word
109 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
111 readAddrOffForeignObj :: ForeignObj -> Int -> IO Addr
112 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
114 readFloatOffForeignObj :: ForeignObj -> Int -> IO Float
115 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
117 readDoubleOffForeignObj :: ForeignObj -> Int -> IO Double
118 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
122 writeCharOffForeignObj :: ForeignObj -> Int -> Char -> IO ()
123 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
125 writeIntOffForeignObj :: ForeignObj -> Int -> Int -> IO ()
126 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
128 writeWordOffForeignObj :: ForeignObj -> Int -> Word -> IO ()
129 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
131 writeAddrOffForeignObj :: ForeignObj -> Int -> Addr -> IO ()
132 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
134 writeFloatOffForeignObj :: ForeignObj -> Int -> Float -> IO ()
135 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
137 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
138 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e