[project @ 2003-09-16 13:03:37 by simonmar]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Literal.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
3 %
4 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
5
6 \begin{code}
7 module Literal
8         ( Literal(..)           -- Exported to ParseIface
9         , mkMachInt, mkMachWord
10         , mkMachInt64, mkMachWord64
11         , litSize
12         , litIsDupable, litIsTrivial
13         , literalType, literalPrimRep
14         , hashLiteral
15
16         , inIntRange, inWordRange, tARGET_MAX_INT, inCharRange
17         , isZeroLit,
18
19         , word2IntLit, int2WordLit
20         , narrow8IntLit, narrow16IntLit, narrow32IntLit
21         , narrow8WordLit, narrow16WordLit, narrow32WordLit
22         , char2IntLit, int2CharLit
23         , float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit
24         , nullAddrLit, float2DoubleLit, double2FloatLit
25         ) where
26
27 #include "HsVersions.h"
28
29 import TysPrim          ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
30                           intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
31                         )
32 import PrimRep          ( PrimRep(..) )
33 import TcType           ( Type, tcCmpType )
34 import Type             ( typePrimRep )
35 import PprType          ( pprParendType )
36 import CStrings         ( pprFSInCStyle )
37
38 import Outputable
39 import FastTypes
40 import FastString
41 import Binary
42 import Util             ( thenCmp )
43
44 import Ratio            ( numerator )
45 import FastString       ( uniqueOfFS, lengthFS )
46 import DATA_INT         ( Int8,  Int16,  Int32 )
47 import DATA_WORD        ( Word8, Word16, Word32 )
48 import Char             ( ord, chr )
49 \end{code}
50
51
52
53 %************************************************************************
54 %*                                                                      *
55 \subsection{Sizes}
56 %*                                                                      *
57 %************************************************************************
58
59 If we're compiling with GHC (and we're not cross-compiling), then we
60 know that minBound and maxBound :: Int are the right values for the
61 target architecture.  Otherwise, we assume -2^31 and 2^31-1
62 respectively (which will be wrong on a 64-bit machine).
63
64 \begin{code}
65 tARGET_MIN_INT, tARGET_MAX_INT, tARGET_MAX_WORD :: Integer
66 #if __GLASGOW_HASKELL__
67 tARGET_MIN_INT  = toInteger (minBound :: Int)
68 tARGET_MAX_INT  = toInteger (maxBound :: Int)
69 #else
70 tARGET_MIN_INT = -2147483648
71 tARGET_MAX_INT =  2147483647
72 #endif
73 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
74
75 tARGET_MAX_CHAR :: Int
76 tARGET_MAX_CHAR = 0x10ffff
77 \end{code}
78  
79
80 %************************************************************************
81 %*                                                                      *
82 \subsection{Literals}
83 %*                                                                      *
84 %************************************************************************
85
86 So-called @Literals@ are {\em either}:
87 \begin{itemize}
88 \item
89 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
90 which is presumed to be surrounded by appropriate constructors
91 (@mKINT@, etc.), so that the overall thing makes sense.
92 \item
93 An Integer, Rational, or String literal whose representation we are
94 {\em uncommitted} about; i.e., the surrounding with constructors,
95 function applications, etc., etc., has not yet been done.
96 \end{itemize}
97
98 \begin{code}
99 data Literal
100   =     ------------------
101         -- First the primitive guys
102     MachChar    Int             -- Char#        At least 31 bits
103   | MachStr     FastString
104
105   | MachNullAddr                -- the NULL pointer, the only pointer value
106                                 -- that can be represented as a Literal.
107
108   | MachInt     Integer         -- Int#         At least WORD_SIZE_IN_BITS bits
109   | MachInt64   Integer         -- Int64#       At least 64 bits
110   | MachWord    Integer         -- Word#        At least WORD_SIZE_IN_BITS bits
111   | MachWord64  Integer         -- Word64#      At least 64 bits
112
113   | MachFloat   Rational
114   | MachDouble  Rational
115
116         -- MachLabel is used (only) for the literal derived from a 
117         -- "foreign label" declaration.
118         -- string argument is the name of a symbol.  This literal
119         -- refers to the *address* of the label.
120   | MachLabel   FastString              -- always an Addr#
121                 (Maybe Int)             -- the size (in bytes) of the arguments
122                                         -- the label expects. Only applicable with
123                                         -- 'stdcall' labels.
124                                         -- Just x => "@<x>" will be appended to label
125                                         --           name when emitting asm.
126 \end{code}
127
128 Binary instance
129
130 \begin{code}
131 instance Binary Literal where
132     put_ bh (MachChar aa)     = do putByte bh 0; put_ bh aa
133     put_ bh (MachStr ab)      = do putByte bh 1; put_ bh ab
134     put_ bh (MachNullAddr)    = do putByte bh 2
135     put_ bh (MachInt ad)      = do putByte bh 3; put_ bh ad
136     put_ bh (MachInt64 ae)    = do putByte bh 4; put_ bh ae
137     put_ bh (MachWord af)     = do putByte bh 5; put_ bh af
138     put_ bh (MachWord64 ag)   = do putByte bh 6; put_ bh ag
139     put_ bh (MachFloat ah)    = do putByte bh 7; put_ bh ah
140     put_ bh (MachDouble ai)   = do putByte bh 8; put_ bh ai
141     put_ bh (MachLabel aj mb) = do putByte bh 9; put_ bh aj ; put_ bh mb
142     get bh = do
143             h <- getByte bh
144             case h of
145               0 -> do
146                     aa <- get bh
147                     return (MachChar aa)
148               1 -> do
149                     ab <- get bh
150                     return (MachStr ab)
151               2 -> do
152                     return (MachNullAddr)
153               3 -> do
154                     ad <- get bh
155                     return (MachInt ad)
156               4 -> do
157                     ae <- get bh
158                     return (MachInt64 ae)
159               5 -> do
160                     af <- get bh
161                     return (MachWord af)
162               6 -> do
163                     ag <- get bh
164                     return (MachWord64 ag)
165               7 -> do
166                     ah <- get bh
167                     return (MachFloat ah)
168               8 -> do
169                     ai <- get bh
170                     return (MachDouble ai)
171               9 -> do
172                     aj <- get bh
173                     mb <- get bh
174                     return (MachLabel aj mb)
175 \end{code}
176
177 \begin{code}
178 instance Outputable Literal where
179     ppr lit = pprLit lit
180
181 instance Show Literal where
182     showsPrec p lit = showsPrecSDoc p (ppr lit)
183
184 instance Eq Literal where
185     a == b = case (a `compare` b) of { EQ -> True;   _ -> False }
186     a /= b = case (a `compare` b) of { EQ -> False;  _ -> True  }
187
188 instance Ord Literal where
189     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
190     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
191     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
192     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
193     compare a b = cmpLit a b
194 \end{code}
195
196
197         Construction
198         ~~~~~~~~~~~~
199 \begin{code}
200 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
201
202 mkMachInt  x   = -- ASSERT2( inIntRange x,  integer x ) 
203                  -- Not true: you can write out of range Int# literals
204                  -- For example, one can write (intToWord# 0xffff0000) to
205                  -- get a particular Word bit-pattern, and there's no other
206                  -- convenient way to write such literals, which is why we allow it.
207                  MachInt x
208 mkMachWord x   = -- ASSERT2( inWordRange x, integer x ) 
209                  MachWord x
210 mkMachInt64  x = MachInt64 x
211 mkMachWord64 x = MachWord64 x
212
213 inIntRange, inWordRange :: Integer -> Bool
214 inIntRange  x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
215 inWordRange x = x >= 0              && x <= tARGET_MAX_WORD
216
217 inCharRange :: Int -> Bool
218 inCharRange c =  c >= 0 && c <= tARGET_MAX_CHAR
219
220 isZeroLit :: Literal -> Bool
221 isZeroLit (MachInt    0) = True
222 isZeroLit (MachInt64  0) = True
223 isZeroLit (MachWord   0) = True
224 isZeroLit (MachWord64 0) = True
225 isZeroLit (MachFloat  0) = True
226 isZeroLit (MachDouble 0) = True
227 isZeroLit other          = False
228 \end{code}
229
230         Coercions
231         ~~~~~~~~~
232 \begin{code}
233 word2IntLit, int2WordLit,
234   narrow8IntLit, narrow16IntLit, narrow32IntLit,
235   narrow8WordLit, narrow16WordLit, narrow32WordLit,
236   char2IntLit, int2CharLit,
237   float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
238   float2DoubleLit, double2FloatLit
239   :: Literal -> Literal
240
241 word2IntLit (MachWord w) 
242   | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
243   | otherwise          = MachInt w
244
245 int2WordLit (MachInt i)
246   | i < 0     = MachWord (1 + tARGET_MAX_WORD + i)      -- (-1)  --->  tARGET_MAX_WORD
247   | otherwise = MachWord i
248
249 narrow8IntLit    (MachInt  i) = MachInt  (toInteger (fromInteger i :: Int8))
250 narrow16IntLit   (MachInt  i) = MachInt  (toInteger (fromInteger i :: Int16))
251 narrow32IntLit   (MachInt  i) = MachInt  (toInteger (fromInteger i :: Int32))
252 narrow8WordLit   (MachWord w) = MachWord (toInteger (fromInteger w :: Word8))
253 narrow16WordLit  (MachWord w) = MachWord (toInteger (fromInteger w :: Word16))
254 narrow32WordLit  (MachWord w) = MachWord (toInteger (fromInteger w :: Word32))
255
256 char2IntLit (MachChar c) = MachInt  (toInteger c)
257 int2CharLit (MachInt  i) = MachChar (fromInteger i)
258
259 float2IntLit (MachFloat f) = MachInt   (truncate    f)
260 int2FloatLit (MachInt   i) = MachFloat (fromInteger i)
261
262 double2IntLit (MachDouble f) = MachInt    (truncate    f)
263 int2DoubleLit (MachInt   i) = MachDouble (fromInteger i)
264
265 float2DoubleLit (MachFloat  f) = MachDouble f
266 double2FloatLit (MachDouble d) = MachFloat  d
267
268 nullAddrLit :: Literal
269 nullAddrLit = MachNullAddr
270 \end{code}
271
272         Predicates
273         ~~~~~~~~~~
274 \begin{code}
275 litIsTrivial :: Literal -> Bool
276 -- True if there is absolutely no penalty to duplicating the literal
277 --      c.f. CoreUtils.exprIsTrivial
278 -- False principally of strings
279 litIsTrivial (MachStr _) = False
280 litIsTrivial other       = True
281
282 litIsDupable :: Literal -> Bool
283 -- True if code space does not go bad if we duplicate this literal
284 --      c.f. CoreUtils.exprIsDupable
285 -- Currently we treat it just like litIsTrivial
286 litIsDupable (MachStr _) = False
287 litIsDupable other       = True
288
289 litSize :: Literal -> Int
290 -- Used by CoreUnfold.sizeExpr
291 litSize (MachStr str) = 1 + (lengthFS str `div` 4)
292         -- Every literal has size at least 1, otherwise
293         --      f "x" 
294         -- might be too small
295 litSize _other        = 1
296 \end{code}
297
298         Types
299         ~~~~~
300 \begin{code}
301 literalType :: Literal -> Type
302 literalType (MachChar _)          = charPrimTy
303 literalType (MachStr  _)          = addrPrimTy
304 literalType (MachNullAddr)        = addrPrimTy
305 literalType (MachInt  _)          = intPrimTy
306 literalType (MachWord  _)         = wordPrimTy
307 literalType (MachInt64  _)        = int64PrimTy
308 literalType (MachWord64  _)       = word64PrimTy
309 literalType (MachFloat _)         = floatPrimTy
310 literalType (MachDouble _)        = doublePrimTy
311 literalType (MachLabel _ _)       = addrPrimTy
312 \end{code}
313
314 \begin{code}
315 literalPrimRep :: Literal -> PrimRep
316
317 literalPrimRep (MachChar _)       = CharRep
318 literalPrimRep (MachStr _)        = AddrRep  -- specifically: "char *"
319 literalPrimRep (MachNullAddr)     = AddrRep
320 literalPrimRep (MachInt _)        = IntRep
321 literalPrimRep (MachWord _)       = WordRep
322 literalPrimRep (MachInt64 _)      = Int64Rep
323 literalPrimRep (MachWord64 _)     = Word64Rep
324 literalPrimRep (MachFloat _)      = FloatRep
325 literalPrimRep (MachDouble _)     = DoubleRep
326 literalPrimRep (MachLabel _ _)    = AddrRep
327 \end{code}
328
329
330         Comparison
331         ~~~~~~~~~~
332 \begin{code}
333 cmpLit (MachChar      a)   (MachChar       b)   = a `compare` b
334 cmpLit (MachStr       a)   (MachStr        b)   = a `compare` b
335 cmpLit (MachNullAddr)      (MachNullAddr)       = EQ
336 cmpLit (MachInt       a)   (MachInt        b)   = a `compare` b
337 cmpLit (MachWord      a)   (MachWord       b)   = a `compare` b
338 cmpLit (MachInt64     a)   (MachInt64      b)   = a `compare` b
339 cmpLit (MachWord64    a)   (MachWord64     b)   = a `compare` b
340 cmpLit (MachFloat     a)   (MachFloat      b)   = a `compare` b
341 cmpLit (MachDouble    a)   (MachDouble     b)   = a `compare` b
342 cmpLit (MachLabel     a _) (MachLabel      b _) = a `compare` b
343 cmpLit lit1                lit2                 | litTag lit1 <# litTag lit2 = LT
344                                                 | otherwise                    = GT
345
346 litTag (MachChar      _)   = _ILIT(1)
347 litTag (MachStr       _)   = _ILIT(2)
348 litTag (MachNullAddr)      = _ILIT(3)
349 litTag (MachInt       _)   = _ILIT(4)
350 litTag (MachWord      _)   = _ILIT(5)
351 litTag (MachInt64     _)   = _ILIT(6)
352 litTag (MachWord64    _)   = _ILIT(7)
353 litTag (MachFloat     _)   = _ILIT(8)
354 litTag (MachDouble    _)   = _ILIT(9)
355 litTag (MachLabel   _ _)   = _ILIT(10)
356 \end{code}
357
358         Printing
359         ~~~~~~~~
360 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
361   exceptions: MachFloat gets an initial keyword prefix.
362
363 \begin{code}
364 pprLit lit
365   = getPprStyle $ \ sty ->
366     let
367       code_style  = codeStyle  sty
368     in
369     case lit of
370       MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show ch)]
371                   | otherwise  -> pprHsChar ch
372
373       MachStr s | code_style -> pprFSInCStyle s
374                 | otherwise  -> pprHsString s
375       -- Warning: printing MachStr in code_style assumes it contains
376       -- only characters '\0'..'\xFF'!
377
378       MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
379                                 -- Avoid a problem whereby gcc interprets
380                                 -- the constant minInt as unsigned.
381                 | otherwise -> pprIntVal i
382
383       MachInt64 i | code_style -> pprIntVal i           -- Same problem with gcc???
384                   | otherwise -> ptext SLIT("__int64") <+> integer i
385
386       MachWord w | code_style -> pprHexVal w
387                  | otherwise  -> ptext SLIT("__word") <+> integer w
388
389       MachWord64 w | code_style -> pprHexVal w
390                    | otherwise  -> ptext SLIT("__word64") <+> integer w
391
392       MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> code_rational f
393                   | otherwise  -> ptext SLIT("__float") <+> rational f
394
395       MachDouble d | code_style -> code_rational d
396                    | otherwise  -> rational d
397
398       MachNullAddr | code_style -> ptext SLIT("(void*)0")
399                    | otherwise  -> ptext SLIT("__NULL")
400
401       MachLabel l mb
402          | code_style -> ptext SLIT("(&") <> ftext l <> char ')'
403          | otherwise  -> ptext SLIT("__label") <+> 
404              case mb of
405                Nothing -> pprHsString l
406                Just x  -> doubleQuotes (text (unpackFS l ++ '@':show x))
407
408 -- negative floating literals in code style need parentheses to avoid
409 -- interacting with surrounding syntax.
410 code_rational d | d < 0     = parens (rational d)
411                 | otherwise = rational d
412
413 pprIntVal :: Integer -> SDoc
414 -- Print negative integers with parens to be sure it's unambiguous
415 pprIntVal i | i < 0     = parens (integer i)
416             | otherwise = integer i
417                 
418 pprHexVal :: Integer -> SDoc
419 -- Print in C hex format: 0x13fa 
420 pprHexVal 0 = ptext SLIT("0x0")
421 pprHexVal w = ptext SLIT("0x") <> go w
422             where
423               go 0 = empty
424               go w = go quot <> dig
425                    where
426                      (quot,rem) = w `quotRem` 16
427                      dig | rem < 10  = char (chr (fromInteger rem + ord '0'))
428                          | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
429 \end{code}
430
431
432 %************************************************************************
433 %*                                                                      *
434 \subsection{Hashing}
435 %*                                                                      *
436 %************************************************************************
437
438 Hash values should be zero or a positive integer.  No negatives please.
439 (They mess up the UniqFM for some reason.)
440
441 \begin{code}
442 hashLiteral :: Literal -> Int
443 hashLiteral (MachChar c)        = c + 1000      -- Keep it out of range of common ints
444 hashLiteral (MachStr s)         = hashFS s
445 hashLiteral (MachNullAddr)      = 0
446 hashLiteral (MachInt i)         = hashInteger i
447 hashLiteral (MachInt64 i)       = hashInteger i
448 hashLiteral (MachWord i)        = hashInteger i
449 hashLiteral (MachWord64 i)      = hashInteger i
450 hashLiteral (MachFloat r)       = hashRational r
451 hashLiteral (MachDouble r)      = hashRational r
452 hashLiteral (MachLabel s _)     = hashFS s
453
454 hashRational :: Rational -> Int
455 hashRational r = hashInteger (numerator r)
456
457 hashInteger :: Integer -> Int
458 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
459                 -- The 1+ is to avoid zero, which is a Bad Number
460                 -- since we use * to combine hash values
461
462 hashFS :: FastString -> Int
463 hashFS s = iBox (uniqueOfFS s)
464 \end{code}