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