[project @ 1998-08-14 13:02:08 by sof]
[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-} -> Addr{-finaliser-} -> IO ForeignObj
12        , writeForeignObj -- :: ForeignObj  -> Addr{-new obj-}   -> IO ()
13        
14        , StablePtr {-a-} -- abstract.
15        , makeStablePtr   -- :: a -> IO (StablePtr a)
16        , deRefStablePtr  -- :: StablePtr a -> IO a
17        , freeStablePtr   -- :: StablePtr a -> IO ()
18
19        , indexCharOffForeignObj
20        , indexAddrOffForeignObj
21        , indexIntOffForeignObj
22        , indexFloatOffForeignObj
23        , indexDoubleOffForeignObj
24        , readCharOffForeignObj
25        , readAddrOffForeignObj
26        , readIntOffForeignObj
27        , readFloatOffForeignObj
28        , readDoubleOffForeignObj
29        , writeCharOffForeignObj
30        , writeAddrOffForeignObj
31        , writeIntOffForeignObj
32        , writeFloatOffForeignObj
33        , writeDoubleOffForeignObj
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        , indexInt8OffForeignObj
49        , indexInt16OffForeignObj
50        , indexInt32OffForeignObj
51        , indexInt64OffForeignObj
52        , readInt8OffForeignObj
53        , readInt16OffForeignObj
54        , readInt32OffForeignObj
55        , readInt64OffForeignObj
56        , writeInt8OffForeignObj
57        , writeInt16OffForeignObj
58        , writeInt32OffForeignObj
59        , writeInt64OffForeignObj
60
61        ) where
62
63 import PrelForeign
64 import PrelBase    ( Int(..), Double(..), Float(..), Char(..) )
65 import PrelGHC     ( indexCharOffForeignObj#, indexIntOffForeignObj#, 
66                      indexAddrOffForeignObj#, indexFloatOffForeignObj#, 
67                      indexDoubleOffForeignObj#
68                    )
69 import PrelAddr    ( Addr(..) )
70 import Word 
71    ( 
72      indexWord8OffForeignObj
73    , indexWord16OffForeignObj
74    , indexWord32OffForeignObj
75    , indexWord64OffForeignObj
76    , readWord8OffForeignObj
77    , readWord16OffForeignObj
78    , readWord32OffForeignObj
79    , readWord64OffForeignObj
80    , writeWord8OffForeignObj
81    , writeWord16OffForeignObj
82    , writeWord32OffForeignObj
83    , writeWord64OffForeignObj
84    )
85
86 import Int
87    ( 
88      indexInt8OffForeignObj
89    , indexInt16OffForeignObj
90    , indexInt32OffForeignObj
91    , indexInt64OffForeignObj
92    , readInt8OffForeignObj
93    , readInt16OffForeignObj
94    , readInt32OffForeignObj
95    , readInt64OffForeignObj
96    , writeInt8OffForeignObj
97    , writeInt16OffForeignObj
98    , writeInt32OffForeignObj
99    , writeInt64OffForeignObj
100    )
101 import PrelIOBase ( IO(..), IOResult(..) )
102 \end{code}
103
104 \begin{code}
105 indexCharOffForeignObj   :: ForeignObj -> Int -> Char
106 indexCharOffForeignObj (ForeignObj fo#) (I# i#) = C# (indexCharOffForeignObj# fo# i#)
107
108 indexIntOffForeignObj    :: ForeignObj -> Int -> Int
109 indexIntOffForeignObj (ForeignObj fo#) (I# i#) = I# (indexIntOffForeignObj# fo# i#)
110
111 indexAddrOffForeignObj   :: ForeignObj -> Int -> Addr
112 indexAddrOffForeignObj (ForeignObj fo#) (I# i#) = A# (indexAddrOffForeignObj# fo# i#)
113
114 indexFloatOffForeignObj  :: ForeignObj -> Int -> Float
115 indexFloatOffForeignObj (ForeignObj fo#) (I# i#) = F# (indexFloatOffForeignObj# fo# i#)
116
117 indexDoubleOffForeignObj :: ForeignObj -> Int -> Double
118 indexDoubleOffForeignObj (ForeignObj fo#) (I# i#) = D# (indexDoubleOffForeignObj# fo# i#)
119
120 -- read value out of mutable memory
121 readCharOffForeignObj    :: ForeignObj -> Int -> IO Char
122 readCharOffForeignObj fo i = _casm_ `` %r=(StgChar)(((StgChar*)%0)[(StgInt)%1]); '' fo i
123
124 readIntOffForeignObj     :: ForeignObj -> Int -> IO Int
125 readIntOffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
126
127 readWordOffForeignObj     :: ForeignObj -> Int -> IO Word
128 readWordOffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
129
130 readAddrOffForeignObj    :: ForeignObj -> Int -> IO Addr
131 readAddrOffForeignObj fo i = _casm_ `` %r=(StgAddr)(((StgAddr*)%0)[(StgInt)%1]); '' fo i
132
133 readFloatOffForeignObj   :: ForeignObj -> Int -> IO Float
134 readFloatOffForeignObj fo i = _casm_ `` %r=(StgFloat)(((StgFloat*)%0)[(StgInt)%1]); '' fo i
135
136 readDoubleOffForeignObj  :: ForeignObj -> Int -> IO Double
137 readDoubleOffForeignObj fo i = _casm_ `` %r=(StgDouble)(((StgDouble*)%0)[(StgInt)%1]); '' fo i
138 \end{code}
139
140 \begin{code}
141 writeCharOffForeignObj   :: ForeignObj -> Int -> Char   -> IO ()
142 writeCharOffForeignObj fo i e = _casm_ `` (((StgChar*)%0)[(StgInt)%1])=(StgChar)%2; '' fo i e
143
144 writeIntOffForeignObj    :: ForeignObj -> Int -> Int    -> IO ()
145 writeIntOffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
146
147 writeWordOffForeignObj    :: ForeignObj -> Int -> Word  -> IO ()
148 writeWordOffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
149
150 writeAddrOffForeignObj   :: ForeignObj -> Int -> Addr   -> IO ()
151 writeAddrOffForeignObj fo i e = _casm_ `` (((StgAddr*)%0)[(StgInt)%1])=(StgAddr)%2; ''fo i e
152
153 writeFloatOffForeignObj  :: ForeignObj -> Int -> Float  -> IO ()
154 writeFloatOffForeignObj fo i e = _casm_ `` (((StgFloat*)%0)[(StgInt)%1])=(StgFloat)%2; '' fo i e
155
156 writeDoubleOffForeignObj :: ForeignObj -> Int -> Double -> IO ()
157 writeDoubleOffForeignObj fo i e = _casm_ `` (((StgDouble*)%0)[(StgInt)%1])=(StgDouble)%2; '' fo i e
158
159 \end{code}