fix for large stack allocations
[ghc-hetmet.git] / compiler / utils / Serialized.hs
1 --
2 -- (c) The University of Glasgow 2002-2006
3 --
4 -- Serialized values
5
6 {-# LANGUAGE ScopedTypeVariables #-}
7 module Serialized (
8     -- * Main Serialized data type
9     Serialized,
10     seqSerialized,
11     
12     -- * Going into and out of 'Serialized'
13     toSerialized, fromSerialized,
14     
15     -- * Handy serialization functions
16     serializeWithData, deserializeWithData,
17   ) where
18
19 import Binary
20 import Outputable
21 import FastString
22 import Util
23
24 import Data.Bits
25 import Data.Word        ( Word8 )
26
27 import Data.Data
28
29
30 -- | Represents a serialized value of a particular type. Attempts can be made to deserialize it at certain types
31 data Serialized = Serialized TypeRep [Word8]
32
33 instance Outputable Serialized where
34     ppr (Serialized the_type bytes) = int (length bytes) <+> ptext (sLit "of type") <+> text (show the_type)
35
36 instance Binary Serialized where
37     put_ bh (Serialized the_type bytes) = do
38         put_ bh the_type
39         put_ bh bytes
40     get bh = do
41         the_type <- get bh
42         bytes <- get bh
43         return (Serialized the_type bytes)
44
45 -- | Put a Typeable value that we are able to actually turn into bytes into a 'Serialized' value ready for deserialization later
46 toSerialized :: Typeable a => (a -> [Word8]) -> a -> Serialized
47 toSerialized serialize what = Serialized (typeOf what) (serialize what)
48
49 -- | If the 'Serialized' value contains something of the given type, then use the specified deserializer to return @Just@ that.
50 -- Otherwise return @Nothing@.
51 fromSerialized :: forall a. Typeable a => ([Word8] -> a) -> Serialized -> Maybe a
52 fromSerialized deserialize (Serialized the_type bytes)
53   | the_type == typeOf (undefined :: a) = Just (deserialize bytes)
54   | otherwise                           = Nothing
55
56 -- | Force the contents of the Serialized value so weknow it doesn't contain any bottoms
57 seqSerialized :: Serialized -> ()
58 seqSerialized (Serialized the_type bytes) = the_type `seq` bytes `seqList` ()
59
60
61 -- | Use a 'Data' instance to implement a serialization scheme dual to that of 'deserializeWithData'
62 serializeWithData :: Data a => a -> [Word8]
63 serializeWithData what = serializeWithData' what []
64
65 serializeWithData' :: Data a => a -> [Word8] -> [Word8]
66 serializeWithData' what = fst $ gfoldl (\(before, a_to_b) a -> (before . serializeWithData' a, a_to_b a))
67                                        (\x -> (serializeConstr (constrRep (toConstr what)), x))
68                                        what
69
70 -- | Use a 'Data' instance to implement a deserialization scheme dual to that of 'serializeWithData'
71 deserializeWithData :: Data a => [Word8] -> a
72 deserializeWithData = snd . deserializeWithData'
73
74 deserializeWithData' :: forall a. Data a => [Word8] -> ([Word8], a)
75 deserializeWithData' bytes = deserializeConstr bytes $ \constr_rep bytes ->
76                              gunfold (\(bytes, b_to_r) -> let (bytes', b) = deserializeWithData' bytes in (bytes', b_to_r b))
77                                      (\x -> (bytes, x))
78                                      (repConstr (dataTypeOf (undefined :: a)) constr_rep)
79
80
81 serializeConstr :: ConstrRep -> [Word8] -> [Word8]
82 serializeConstr (AlgConstr ix)   = serializeWord8 1 . serializeInt ix
83 serializeConstr (IntConstr i)    = serializeWord8 2 . serializeInteger i
84 serializeConstr (FloatConstr r)  = serializeWord8 3 . serializeRational r
85 #if __GLASGOW_HASKELL__ < 611
86 serializeConstr (StringConstr s) = serializeWord8 4 . serializeString s
87 #else
88 serializeConstr (CharConstr c)   = serializeWord8 4 . serializeChar c
89 #endif
90
91
92 deserializeConstr :: [Word8] -> (ConstrRep -> [Word8] -> a) -> a
93 deserializeConstr bytes k = deserializeWord8 bytes $ \constr_ix bytes ->
94                             case constr_ix of
95                                 1 -> deserializeInt      bytes $ \ix -> k (AlgConstr ix)
96                                 2 -> deserializeInteger  bytes $ \i  -> k (IntConstr i)
97                                 3 -> deserializeRational bytes $ \r  -> k (FloatConstr r)
98 #if __GLASGOW_HASKELL__ >= 611
99                                 4 -> deserializeChar     bytes $ \c  -> k (CharConstr c)
100 #else
101                                 4 -> deserializeString   bytes $ \s  -> k (StringConstr s)
102 #endif
103                                 x -> error $ "deserializeConstr: unrecognised serialized constructor type " ++ show x ++ " in context " ++ show bytes
104
105
106 serializeFixedWidthNum :: forall a. (Num a, Integral a, Bits a) => a -> [Word8] -> [Word8]
107 serializeFixedWidthNum what = go (bitSize what) what
108   where
109     go :: Int -> a -> [Word8] -> [Word8]
110     go size current rest
111       | size <= 0 = rest
112       | otherwise = fromIntegral (current .&. 255) : go (size - 8) (current `shiftR` 8) rest
113
114 deserializeFixedWidthNum :: forall a b. (Num a, Integral a, Bits a) => [Word8] -> (a -> [Word8] -> b) -> b
115 deserializeFixedWidthNum bytes k = go (bitSize (undefined :: a)) bytes k
116   where
117     go :: Int -> [Word8] -> (a -> [Word8] -> b) -> b
118     go size bytes k
119       | size <= 0 = k 0 bytes
120       | otherwise = case bytes of
121                         (byte:bytes) -> go (size - 8) bytes (\x -> k ((x `shiftL` 8) .|. fromIntegral byte))
122                         []           -> error "deserializeFixedWidthNum: unexpected end of stream"
123
124
125 serializeEnum :: (Enum a) => a -> [Word8] -> [Word8]
126 serializeEnum = serializeInt . fromEnum
127
128 deserializeEnum :: Enum a => [Word8] -> (a -> [Word8] -> b) -> b
129 deserializeEnum bytes k = deserializeInt bytes (k . toEnum)
130
131
132 serializeWord8 :: Word8 -> [Word8] -> [Word8]
133 serializeWord8 x = (x:)
134
135 deserializeWord8 :: [Word8] -> (Word8 -> [Word8] -> a) -> a
136 deserializeWord8 (byte:bytes) k = k byte bytes
137 deserializeWord8 []           _ = error "deserializeWord8: unexpected end of stream"
138
139
140 serializeInt :: Int -> [Word8] -> [Word8]
141 serializeInt = serializeFixedWidthNum
142
143 deserializeInt :: [Word8] -> (Int -> [Word8] -> a) -> a
144 deserializeInt = deserializeFixedWidthNum
145
146
147 serializeRational :: (Real a) => a -> [Word8] -> [Word8]
148 serializeRational = serializeString . show . toRational
149
150 deserializeRational :: (Fractional a) => [Word8] -> (a -> [Word8] -> b) -> b
151 deserializeRational bytes k = deserializeString bytes (k . fromRational . read)
152
153
154 serializeInteger :: Integer -> [Word8] -> [Word8]
155 serializeInteger = serializeString . show
156
157 deserializeInteger :: [Word8] -> (Integer -> [Word8] -> a) -> a
158 deserializeInteger bytes k = deserializeString bytes (k . read)
159
160
161 #if __GLASGOW_HASKELL__ >= 611
162 serializeChar :: Char -> [Word8] -> [Word8]
163 serializeChar = serializeString . show
164
165 deserializeChar :: [Word8] -> (Char -> [Word8] -> a) -> a
166 deserializeChar bytes k = deserializeString bytes (k . read)
167 #endif
168
169
170 serializeString :: String -> [Word8] -> [Word8]
171 serializeString = serializeList serializeEnum
172
173 deserializeString :: [Word8] -> (String -> [Word8] -> a) -> a
174 deserializeString = deserializeList deserializeEnum
175
176
177 serializeList :: (a -> [Word8] -> [Word8]) -> [a] -> [Word8] -> [Word8]
178 serializeList serialize_element xs = serializeInt (length xs) . foldr (.) id (map serialize_element xs)
179
180 deserializeList :: forall a b. (forall c. [Word8] -> (a -> [Word8] -> c) -> c)
181                 -> [Word8] -> ([a] -> [Word8] -> b) -> b
182 deserializeList deserialize_element bytes k = deserializeInt bytes $ \len bytes -> go len bytes k
183   where
184     go :: Int -> [Word8] -> ([a] -> [Word8] -> b) -> b
185     go len bytes k
186       | len <= 0  = k [] bytes
187       | otherwise = deserialize_element bytes (\elt bytes -> go (len - 1) bytes (k . (elt:)))
188