[project @ 1998-12-02 13:17:09 by simonm]
[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
25 import PrelBase    ( Int(..), Double(..), Float(..), Char(..) )
26 import PrelGHC     ( indexCharOffForeignObj#, indexIntOffForeignObj#, 
27                      indexAddrOffForeignObj#, indexFloatOffForeignObj#, 
28                      indexDoubleOffForeignObj#
29                    )
30 import PrelAddr    ( Addr(..), Word(..) )
31 import PrelWeak    ( addForeignFinaliser )
32 import Word 
33    ( 
34      indexWord8OffForeignObj
35    , indexWord16OffForeignObj
36    , indexWord32OffForeignObj
37    , indexWord64OffForeignObj
38    , readWord8OffForeignObj
39    , readWord16OffForeignObj
40    , readWord32OffForeignObj
41    , readWord64OffForeignObj
42    , writeWord8OffForeignObj
43    , writeWord16OffForeignObj
44    , writeWord32OffForeignObj
45    , writeWord64OffForeignObj
46    )
47
48 import Int
49    ( 
50      indexInt8OffForeignObj
51    , indexInt16OffForeignObj
52    , indexInt32OffForeignObj
53    , indexInt64OffForeignObj
54    , readInt8OffForeignObj
55    , readInt16OffForeignObj
56    , readInt32OffForeignObj
57    , readInt64OffForeignObj
58    , writeInt8OffForeignObj
59    , writeInt16OffForeignObj
60    , writeInt32OffForeignObj
61    , writeInt64OffForeignObj
62    )
63 import PrelIOBase ( IO(..) )
64 \end{code}
65
66 \begin{code}
67 foreignObjToAddr :: ForeignObj -> IO Addr
68 foreignObjToAddr fo = _casm_ `` %r=(StgAddr)%0; '' fo
69 \end{code}
70
71
72 \begin{code}
73 indexCharOffForeignObj   :: ForeignObj -> Int -> Char
74 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
75
76 indexIntOffForeignObj    :: ForeignObj -> Int -> Int
77 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
78
79 indexAddrOffForeignObj   :: ForeignObj -> Int -> Addr
80 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
81
82 indexFloatOffForeignObj  :: ForeignObj -> Int -> Float
83 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
84
85 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
86 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
87
88 -- read value out of mutable memory
89 readCharOffForeignObj    :: ForeignObj -> Int -> IO Char
90 readCharOffForeignObj fo i = _casm_ `` %r=(StgChar)(((StgChar*)%0)[(StgInt)%1]); '' fo i
91
92 readIntOffForeignObj     :: ForeignObj -> Int -> IO Int
93 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
94
95 readWordOffForeignObj     :: ForeignObj -> Int -> IO Word
96 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
97
98 readAddrOffForeignObj    :: ForeignObj -> Int -> IO Addr
99 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
100
101 readFloatOffForeignObj   :: ForeignObj -> Int -> IO Float
102 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
103
104 readDoubleOffForeignObj  :: ForeignObj -> Int -> IO Double
105 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
106 \end{code}
107
108 \begin{code}
109 writeCharOffForeignObj   :: ForeignObj -> Int -> Char   -> IO ()
110 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
111
112 writeIntOffForeignObj    :: ForeignObj -> Int -> Int    -> IO ()
113 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
114
115 writeWordOffForeignObj    :: ForeignObj -> Int -> Word  -> IO ()
116 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
117
118 writeAddrOffForeignObj   :: ForeignObj -> Int -> Addr   -> IO ()
119 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
120
121 writeFloatOffForeignObj  :: ForeignObj -> Int -> Float  -> IO ()
122 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
123
124 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
125 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e
126
127 \end{code}