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