[project @ 2001-03-23 16:36:20 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelStorable.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelStorable.lhs,v 1.4 2001/03/13 21:21:27 qrczak Exp $
3 %
4 % (c) The FFI task force, 2000
5 %
6
7 A class for primitive marshaling
8
9 \begin{code}
10 #include "MachDeps.h"
11
12 module PrelStorable
13         ( Storable(
14              sizeOf,         -- :: a -> Int
15              alignment,      -- :: a -> Int
16              peekElemOff,    -- :: Ptr a -> Int      -> IO a
17              pokeElemOff,    -- :: Ptr a -> Int -> a -> IO ()
18              peekByteOff,    -- :: Ptr b -> Int      -> IO a
19              pokeByteOff,    -- :: Ptr b -> Int -> a -> IO ()
20              peek,           -- :: Ptr a             -> IO a
21              poke,           -- :: Ptr a        -> a -> IO ()
22              destruct)       -- :: Ptr a             -> IO ()
23         ) where
24 \end{code}
25
26 \begin{code}
27 import Char             ( chr, ord )
28 import Monad            ( liftM )
29
30 #ifdef __GLASGOW_HASKELL__
31 import PrelStable       ( StablePtr )
32 import PrelInt
33 import PrelWord
34 import PrelCTypes
35 import PrelCTypesISO
36 import PrelStable
37 import PrelPtr
38 import PrelFloat
39 import PrelIOBase
40 import PrelBase
41 #endif
42 \end{code}
43
44 Primitive marshaling
45
46 Minimal complete definition: sizeOf, alignment, and one definition
47 in each of the peek/poke families.
48
49 \begin{code}
50 class Storable a where
51
52    -- sizeOf/alignment *never* use their first argument
53    sizeOf      :: a -> Int
54    alignment   :: a -> Int
55
56    -- replacement for read-/write???OffAddr
57    peekElemOff :: Ptr a -> Int      -> IO a
58    pokeElemOff :: Ptr a -> Int -> a -> IO ()
59
60    -- the same with *byte* offsets
61    peekByteOff :: Ptr b -> Int      -> IO a
62    pokeByteOff :: Ptr b -> Int -> a -> IO ()
63
64    -- ... and with no offsets at all
65    peek        :: Ptr a      -> IO a
66    poke        :: Ptr a -> a -> IO ()
67
68    -- free memory associated with the object
69    -- (except the object pointer itself)
70    destruct    :: Ptr a -> IO ()
71
72    -- circular default instances
73    peekElemOff = peekElemOff_ undefined
74       where peekElemOff_ :: a -> Ptr a -> Int -> IO a
75             peekElemOff_ undef ptr off = peekByteOff ptr (off * sizeOf undef)
76    pokeElemOff ptr off val = pokeByteOff ptr (off * sizeOf val) val
77
78    peekByteOff ptr off = peek (ptr `plusPtr` off)
79    pokeByteOff ptr off = poke (ptr `plusPtr` off)
80
81    peek ptr = peekElemOff ptr 0
82    poke ptr = pokeElemOff ptr 0
83
84    destruct _ = return ()
85 \end{code}
86
87 System-dependent, but rather obvious instances
88
89 \begin{code}
90 instance Storable Bool where
91    sizeOf _          = sizeOf (undefined::CInt)
92    alignment _       = alignment (undefined::CInt)
93    peekElemOff p i   = liftM (/= (0::CInt)) $ peekElemOff (castPtr p) i
94    pokeElemOff p i x = pokeElemOff (castPtr p) i (if x then 1 else 0::CInt)
95
96 instance Storable (FunPtr a) where
97    sizeOf          (FunPtr x) = sizeOf x
98    alignment       (FunPtr x) = alignment x
99    peekElemOff p i            = liftM FunPtr $ peekElemOff (castPtr p) i
100    pokeElemOff p i (FunPtr x) = pokeElemOff (castPtr p) i x
101
102 #define STORABLE(T,size,align,read,write)       \
103 instance Storable (T) where {                   \
104     sizeOf    _ = size;                         \
105     alignment _ = align;                        \
106     peekElemOff = read;                         \
107     pokeElemOff = write }
108
109 STORABLE(Char,SIZEOF_INT32,ALIGNMENT_INT32,
110          readWideCharOffPtr,writeWideCharOffPtr)
111
112 STORABLE(Int,SIZEOF_LONG,ALIGNMENT_LONG,
113          readIntOffPtr,writeIntOffPtr)
114
115 STORABLE(Word,SIZEOF_LONG,ALIGNMENT_LONG,
116          readWordOffPtr,writeWordOffPtr)
117
118 STORABLE((Ptr a),SIZEOF_VOID_P,ALIGNMENT_VOID_P,
119          readPtrOffPtr,writePtrOffPtr)
120
121 STORABLE((StablePtr a),SIZEOF_VOID_P,ALIGNMENT_VOID_P,
122          readStablePtrOffPtr,writeStablePtrOffPtr)
123
124 STORABLE(Float,SIZEOF_FLOAT,ALIGNMENT_FLOAT,
125          readFloatOffPtr,writeFloatOffPtr)
126
127 STORABLE(Double,SIZEOF_DOUBLE,ALIGNMENT_DOUBLE,
128          readDoubleOffPtr,writeDoubleOffPtr)
129
130 STORABLE(Word8,SIZEOF_WORD8,ALIGNMENT_WORD8,
131          readWord8OffPtr,writeWord8OffPtr)
132
133 STORABLE(Word16,SIZEOF_WORD16,ALIGNMENT_WORD16,
134          readWord16OffPtr,writeWord16OffPtr)
135
136 STORABLE(Word32,SIZEOF_WORD32,ALIGNMENT_WORD32,
137          readWord32OffPtr,writeWord32OffPtr)
138
139 STORABLE(Word64,SIZEOF_WORD64,ALIGNMENT_WORD64,
140          readWord64OffPtr,writeWord64OffPtr)
141
142 STORABLE(Int8,SIZEOF_INT8,ALIGNMENT_INT8,
143          readInt8OffPtr,writeInt8OffPtr)
144
145 STORABLE(Int16,SIZEOF_INT16,ALIGNMENT_INT16,
146          readInt16OffPtr,writeInt16OffPtr)
147
148 STORABLE(Int32,SIZEOF_INT32,ALIGNMENT_INT32,
149          readInt32OffPtr,writeInt32OffPtr)
150
151 STORABLE(Int64,SIZEOF_INT64,ALIGNMENT_INT64,
152          readInt64OffPtr,writeInt64OffPtr)
153
154 #define NSTORABLE(T) \
155 instance Storable T where { \
156    sizeOf    (T x)       = sizeOf x ; \
157    alignment (T x)       = alignment x ; \
158    peekElemOff a i       = liftM T (peekElemOff (castPtr a) i) ; \
159    pokeElemOff a i (T x) = pokeElemOff (castPtr a) i x }
160
161 NSTORABLE(CChar)
162 NSTORABLE(CSChar)
163 NSTORABLE(CUChar)
164 NSTORABLE(CShort)
165 NSTORABLE(CUShort)
166 NSTORABLE(CInt)
167 NSTORABLE(CUInt)
168 NSTORABLE(CLong)
169 NSTORABLE(CULong)
170 NSTORABLE(CLLong)
171 NSTORABLE(CULLong)
172 NSTORABLE(CFloat)
173 NSTORABLE(CDouble)
174 NSTORABLE(CLDouble)
175 NSTORABLE(CPtrdiff)
176 NSTORABLE(CSize)
177 NSTORABLE(CWchar)
178 NSTORABLE(CSigAtomic)
179 NSTORABLE(CClock)
180 NSTORABLE(CTime)
181 \end{code}
182
183 Helper functions
184
185 \begin{code}
186 #ifdef __GLASGOW_HASKELL__
187
188 readWideCharOffPtr  :: Ptr Char          -> Int -> IO Char
189 readIntOffPtr       :: Ptr Int           -> Int -> IO Int
190 readWordOffPtr      :: Ptr Word          -> Int -> IO Word
191 readPtrOffPtr       :: Ptr (Ptr a)       -> Int -> IO (Ptr a)
192 readFloatOffPtr     :: Ptr Float         -> Int -> IO Float
193 readDoubleOffPtr    :: Ptr Double        -> Int -> IO Double
194 readStablePtrOffPtr :: Ptr (StablePtr a) -> Int -> IO (StablePtr a)
195 readInt8OffPtr      :: Ptr Int8          -> Int -> IO Int8
196 readInt16OffPtr     :: Ptr Int16         -> Int -> IO Int16
197 readInt32OffPtr     :: Ptr Int32         -> Int -> IO Int32
198 readInt64OffPtr     :: Ptr Int64         -> Int -> IO Int64
199 readWord8OffPtr     :: Ptr Word8         -> Int -> IO Word8
200 readWord16OffPtr    :: Ptr Word16        -> Int -> IO Word16
201 readWord32OffPtr    :: Ptr Word32        -> Int -> IO Word32
202 readWord64OffPtr    :: Ptr Word64        -> Int -> IO Word64
203
204 readWideCharOffPtr (Ptr a) (I# i)
205   = IO $ \s -> case readWideCharOffAddr# a i s  of (# s2, x #) -> (# s2, C# x #)
206 readIntOffPtr (Ptr a) (I# i)
207   = IO $ \s -> case readIntOffAddr# a i s       of (# s2, x #) -> (# s2, I# x #)
208 readWordOffPtr (Ptr a) (I# i)
209   = IO $ \s -> case readWordOffAddr# a i s      of (# s2, x #) -> (# s2, W# x #)
210 readPtrOffPtr (Ptr a) (I# i)
211   = IO $ \s -> case readAddrOffAddr# a i s      of (# s2, x #) -> (# s2, Ptr x #)
212 readFloatOffPtr (Ptr a) (I# i)
213   = IO $ \s -> case readFloatOffAddr# a i s     of (# s2, x #) -> (# s2, F# x #)
214 readDoubleOffPtr (Ptr a) (I# i)
215   = IO $ \s -> case readDoubleOffAddr# a i s    of (# s2, x #) -> (# s2, D# x #)
216 readStablePtrOffPtr (Ptr a) (I# i)
217   = IO $ \s -> case readStablePtrOffAddr# a i s of (# s2, x #) -> (# s2, StablePtr x #)
218 readInt8OffPtr (Ptr a) (I# i)
219   = IO $ \s -> case readInt8OffAddr# a i s      of (# s2, x #) -> (# s2, I8# x #)
220 readInt16OffPtr (Ptr a) (I# i)
221   = IO $ \s -> case readInt16OffAddr# a i s     of (# s2, x #) -> (# s2, I16# x #)
222 readInt32OffPtr (Ptr a) (I# i)
223   = IO $ \s -> case readInt32OffAddr# a i s     of (# s2, x #) -> (# s2, I32# x #)
224 readInt64OffPtr (Ptr a) (I# i)
225   = IO $ \s -> case readInt64OffAddr# a i s     of (# s2, x #) -> (# s2, I64# x #)
226 readWord8OffPtr (Ptr a) (I# i)
227   = IO $ \s -> case readWord8OffAddr# a i s     of (# s2, x #) -> (# s2, W8# x #)
228 readWord16OffPtr (Ptr a) (I# i)
229   = IO $ \s -> case readWord16OffAddr# a i s    of (# s2, x #) -> (# s2, W16# x #)
230 readWord32OffPtr (Ptr a) (I# i)
231   = IO $ \s -> case readWord32OffAddr# a i s    of (# s2, x #) -> (# s2, W32# x #)
232 readWord64OffPtr (Ptr a) (I# i)
233   = IO $ \s -> case readWord64OffAddr# a i s    of (# s2, x #) -> (# s2, W64# x #)
234
235 writeWideCharOffPtr  :: Ptr Char          -> Int -> Char        -> IO ()
236 writeIntOffPtr       :: Ptr Int           -> Int -> Int         -> IO ()
237 writeWordOffPtr      :: Ptr Word          -> Int -> Word        -> IO ()
238 writePtrOffPtr       :: Ptr (Ptr a)       -> Int -> Ptr a       -> IO ()
239 writeFloatOffPtr     :: Ptr Float         -> Int -> Float       -> IO ()
240 writeDoubleOffPtr    :: Ptr Double        -> Int -> Double      -> IO ()
241 writeStablePtrOffPtr :: Ptr (StablePtr a) -> Int -> StablePtr a -> IO ()
242 writeInt8OffPtr      :: Ptr Int8          -> Int -> Int8        -> IO ()
243 writeInt16OffPtr     :: Ptr Int16         -> Int -> Int16       -> IO ()
244 writeInt32OffPtr     :: Ptr Int32         -> Int -> Int32       -> IO ()
245 writeInt64OffPtr     :: Ptr Int64         -> Int -> Int64       -> IO ()
246 writeWord8OffPtr     :: Ptr Word8         -> Int -> Word8       -> IO ()
247 writeWord16OffPtr    :: Ptr Word16        -> Int -> Word16      -> IO ()
248 writeWord32OffPtr    :: Ptr Word32        -> Int -> Word32      -> IO ()
249 writeWord64OffPtr    :: Ptr Word64        -> Int -> Word64      -> IO ()
250
251 writeWideCharOffPtr (Ptr a) (I# i) (C# x)
252   = IO $ \s -> case writeWideCharOffAddr# a i x s  of s2 -> (# s2, () #)
253 writeIntOffPtr (Ptr a) (I# i) (I# x)
254   = IO $ \s -> case writeIntOffAddr# a i x s       of s2 -> (# s2, () #)
255 writeWordOffPtr (Ptr a) (I# i) (W# x)
256   = IO $ \s -> case writeWordOffAddr# a i x s      of s2 -> (# s2, () #)
257 writePtrOffPtr (Ptr a) (I# i) (Ptr x)
258   = IO $ \s -> case writeAddrOffAddr# a i x s      of s2 -> (# s2, () #)
259 writeFloatOffPtr (Ptr a) (I# i) (F# x)
260   = IO $ \s -> case writeFloatOffAddr# a i x s     of s2 -> (# s2, () #)
261 writeDoubleOffPtr (Ptr a) (I# i) (D# x)
262   = IO $ \s -> case writeDoubleOffAddr# a i x s    of s2 -> (# s2, () #)
263 writeStablePtrOffPtr (Ptr a) (I# i) (StablePtr x)
264   = IO $ \s -> case writeStablePtrOffAddr# a i x s of s2 -> (# s2 , () #)
265 writeInt8OffPtr (Ptr a) (I# i) (I8# x)
266   = IO $ \s -> case writeInt8OffAddr# a i x s      of s2 -> (# s2, () #)
267 writeInt16OffPtr (Ptr a) (I# i) (I16# x)
268   = IO $ \s -> case writeInt16OffAddr# a i x s     of s2 -> (# s2, () #)
269 writeInt32OffPtr (Ptr a) (I# i) (I32# x)
270   = IO $ \s -> case writeInt32OffAddr# a i x s     of s2 -> (# s2, () #)
271 writeInt64OffPtr (Ptr a) (I# i) (I64# x)
272   = IO $ \s -> case writeInt64OffAddr# a i x s     of s2 -> (# s2, () #)
273 writeWord8OffPtr (Ptr a) (I# i) (W8# x)
274   = IO $ \s -> case writeWord8OffAddr# a i x s     of s2 -> (# s2, () #)
275 writeWord16OffPtr (Ptr a) (I# i) (W16# x)
276   = IO $ \s -> case writeWord16OffAddr# a i x s    of s2 -> (# s2, () #)
277 writeWord32OffPtr (Ptr a) (I# i) (W32# x)
278   = IO $ \s -> case writeWord32OffAddr# a i x s    of s2 -> (# s2, () #)
279 writeWord64OffPtr (Ptr a) (I# i) (W64# x)
280   = IO $ \s -> case writeWord64OffAddr# a i x s    of s2 -> (# s2, () #)
281
282 #endif /* __GLASGOW_HASKELL__ */
283 \end{code}