[project @ 1999-07-05 15:30:25 by simonpj]
[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        ) where
25
26 import PrelForeign hiding ( makeForeignObj )
27 import PrelStable
28 import qualified PrelForeign as PF ( makeForeignObj )
29 import PrelBase    ( Int(..), Double(..), Float(..), Char(..) )
30 import PrelGHC     ( indexCharOffForeignObj#, indexIntOffForeignObj#, 
31                      indexAddrOffForeignObj#, indexFloatOffForeignObj#, 
32                      indexDoubleOffForeignObj#
33                    )
34 import PrelAddr    ( Addr(..), Word(..) )
35 import PrelWeak    ( addForeignFinalizer )
36 import Word 
37    ( 
38      indexWord8OffForeignObj
39    , indexWord16OffForeignObj
40    , indexWord32OffForeignObj
41    , indexWord64OffForeignObj
42    , readWord8OffForeignObj
43    , readWord16OffForeignObj
44    , readWord32OffForeignObj
45    , readWord64OffForeignObj
46    , writeWord8OffForeignObj
47    , writeWord16OffForeignObj
48    , writeWord32OffForeignObj
49    , writeWord64OffForeignObj
50    )
51
52 import Int
53    ( 
54      indexInt8OffForeignObj
55    , indexInt16OffForeignObj
56    , indexInt32OffForeignObj
57    , indexInt64OffForeignObj
58    , readInt8OffForeignObj
59    , readInt16OffForeignObj
60    , readInt32OffForeignObj
61    , readInt64OffForeignObj
62    , writeInt8OffForeignObj
63    , writeInt16OffForeignObj
64    , writeInt32OffForeignObj
65    , writeInt64OffForeignObj
66    )
67 import PrelIOBase ( IO(..) )
68 \end{code}
69
70 \begin{code}
71 foreignObjToAddr :: ForeignObj -> IO Addr
72 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
73 \end{code}
74
75 \begin{code}
76 makeForeignObj :: Addr -> Addr -> IO ForeignObj
77 makeForeignObj obj finalizer = do
78    fobj <- PF.makeForeignObj obj
79    addForeignFinalizer fobj (app0 finalizer fobj)
80    return fobj
81
82 mkForeignObj :: Addr -> IO ForeignObj
83 mkForeignObj = PF.makeForeignObj
84
85 foreign import dynamic unsafe app0 :: Addr -> (ForeignObj -> IO ())
86 \end{code}
87
88
89
90 \begin{code}
91 indexCharOffForeignObj   :: ForeignObj -> Int -> Char
92 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
93
94 indexIntOffForeignObj    :: ForeignObj -> Int -> Int
95 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
96
97 indexAddrOffForeignObj   :: ForeignObj -> Int -> Addr
98 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
99
100 indexFloatOffForeignObj  :: ForeignObj -> Int -> Float
101 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
102
103 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
104 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
105
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
109
110 readIntOffForeignObj     :: ForeignObj -> Int -> IO Int
111 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
112
113 readWordOffForeignObj     :: ForeignObj -> Int -> IO Word
114 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
115
116 readAddrOffForeignObj    :: ForeignObj -> Int -> IO Addr
117 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
118
119 readFloatOffForeignObj   :: ForeignObj -> Int -> IO Float
120 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
121
122 readDoubleOffForeignObj  :: ForeignObj -> Int -> IO Double
123 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
124 \end{code}
125
126 \begin{code}
127 writeCharOffForeignObj   :: ForeignObj -> Int -> Char   -> IO ()
128 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
129
130 writeIntOffForeignObj    :: ForeignObj -> Int -> Int    -> IO ()
131 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
132
133 writeWordOffForeignObj    :: ForeignObj -> Int -> Word  -> IO ()
134 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
135
136 writeAddrOffForeignObj   :: ForeignObj -> Int -> Addr   -> IO ()
137 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
138
139 writeFloatOffForeignObj  :: ForeignObj -> Int -> Float  -> IO ()
140 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
141
142 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
143 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e
144
145 \end{code}