[project @ 1999-10-10 18:38:52 by sof]
[ghc-hetmet.git] / ghc / lib / exts / Foreign.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1998
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        , mkForeignObj        -- :: Addr -> IO ForeignObj
13        , writeForeignObj     -- :: ForeignObj  -> Addr{-new obj-}   -> IO ()
14        , addForeignFinalizer -- :: ForeignObj -> IO () -> IO ()
15
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
19        
20        , StablePtr {-a-}     -- abstract.
21        , makeStablePtr       -- :: a -> IO (StablePtr a)
22        , deRefStablePtr      -- :: StablePtr a -> IO a
23        , freeStablePtr       -- :: StablePtr a -> IO ()
24        
25        , indexCharOffForeignObj   -- :: ForeignObj -> Int -> Char
26
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
32
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
38
39        , indexAddrOffForeignObj   -- :: ForeignObj -> Int -> Addr
40        , indexFloatOffForeignObj  -- :: ForeignObj -> Int -> Float
41        , indexDoubleOffForeignObj -- :: ForeignObj -> Int -> Double
42        
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
49
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
55
56        , readAddrOffForeignObj    -- :: ForeignObj -> Int -> IO Addr
57        , readFloatOffForeignObj   -- :: ForeignObj -> Int -> IO Float
58        , readDoubleOffForeignObj  -- :: ForeignObj -> Int -> IO Double
59        
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 ()
66
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 ()
72
73        , writeAddrOffForeignObj   -- :: ForeignObj -> Int -> Addr   -> IO ()
74        , writeFloatOffForeignObj  -- :: ForeignObj -> Int -> Float  -> IO ()
75        , writeDoubleOffForeignObj -- :: ForeignObj -> Int -> Double -> IO ()
76
77        ) where
78
79 import PrelForeign hiding ( makeForeignObj )
80 import PrelStable
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#
86                    )
87 import PrelAddr    ( Addr(..), Word(..) )
88 import PrelWeak    ( addForeignFinalizer )
89 import Word 
90    ( 
91      indexWord8OffForeignObj
92    , indexWord16OffForeignObj
93    , indexWord32OffForeignObj
94    , indexWord64OffForeignObj
95    , readWord8OffForeignObj
96    , readWord16OffForeignObj
97    , readWord32OffForeignObj
98    , readWord64OffForeignObj
99    , writeWord8OffForeignObj
100    , writeWord16OffForeignObj
101    , writeWord32OffForeignObj
102    , writeWord64OffForeignObj
103    )
104
105 import Int
106    ( 
107      indexInt8OffForeignObj
108    , indexInt16OffForeignObj
109    , indexInt32OffForeignObj
110    , indexInt64OffForeignObj
111    , readInt8OffForeignObj
112    , readInt16OffForeignObj
113    , readInt32OffForeignObj
114    , readInt64OffForeignObj
115    , writeInt8OffForeignObj
116    , writeInt16OffForeignObj
117    , writeInt32OffForeignObj
118    , writeInt64OffForeignObj
119    )
120 import PrelIOBase ( IO(..) )
121 \end{code}
122
123 \begin{code}
124 foreignObjToAddr :: ForeignObj -> IO Addr
125 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
126 \end{code}
127
128 \begin{code}
129 makeForeignObj :: Addr -> Addr -> IO ForeignObj
130 makeForeignObj obj finalizer = do
131    fobj <- PF.makeForeignObj obj
132    addForeignFinalizer fobj (app0 finalizer fobj)
133    return fobj
134
135 mkForeignObj :: Addr -> IO ForeignObj
136 mkForeignObj = PF.makeForeignObj
137
138 foreign import dynamic unsafe app0 :: Addr -> (ForeignObj -> IO ())
139 \end{code}
140
141
142
143 \begin{code}
144 indexCharOffForeignObj   :: ForeignObj -> Int -> Char
145 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
146
147 indexIntOffForeignObj    :: ForeignObj -> Int -> Int
148 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
149
150 indexWordOffForeignObj    :: ForeignObj -> Int -> Word
151 indexWordOffForeignObj (ForeignObj fo#) (I# i#) = W# (indexWordOffForeignObj# fo# i#)
152
153 indexAddrOffForeignObj   :: ForeignObj -> Int -> Addr
154 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
155
156 indexFloatOffForeignObj  :: ForeignObj -> Int -> Float
157 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
158
159 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
160 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
161
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
165
166 readIntOffForeignObj     :: ForeignObj -> Int -> IO Int
167 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
168
169 readWordOffForeignObj     :: ForeignObj -> Int -> IO Word
170 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
171
172 readAddrOffForeignObj    :: ForeignObj -> Int -> IO Addr
173 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
174
175 readFloatOffForeignObj   :: ForeignObj -> Int -> IO Float
176 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
177
178 readDoubleOffForeignObj  :: ForeignObj -> Int -> IO Double
179 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
180 \end{code}
181
182 \begin{code}
183 writeCharOffForeignObj   :: ForeignObj -> Int -> Char   -> IO ()
184 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
185
186 writeIntOffForeignObj    :: ForeignObj -> Int -> Int    -> IO ()
187 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
188
189 writeWordOffForeignObj    :: ForeignObj -> Int -> Word  -> IO ()
190 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
191
192 writeAddrOffForeignObj   :: ForeignObj -> Int -> Addr   -> IO ()
193 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
194
195 writeFloatOffForeignObj  :: ForeignObj -> Int -> Float  -> IO ()
196 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
197
198 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
199 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e
200
201 \end{code}