[project @ 2001-04-26 12:16:57 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         , isLitLitLit, maybeLitLit, litSize, litIsDupable,
12         , literalType, literalPrimRep
13         , hashLiteral
14
15         , inIntRange, inWordRange, tARGET_MAX_INT, inCharRange
16
17         , word2IntLit, int2WordLit, char2IntLit, int2CharLit
18         , float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit
19         , addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit
20         ) where
21
22 #include "HsVersions.h"
23
24 import TysPrim          ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
25                           intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
26                         )
27 import PrimRep          ( PrimRep(..) )
28 import Type             ( Type, typePrimRep )
29 import PprType          ( pprParendType )
30 import CStrings         ( pprFSInCStyle )
31
32 import Outputable
33 import FastTypes
34 import Util             ( thenCmp )
35
36 import Ratio            ( numerator )
37 import FastString       ( uniqueOfFS, lengthFS )
38 import Char             ( ord, chr )
39 \end{code}
40
41
42
43 %************************************************************************
44 %*                                                                      *
45 \subsection{Sizes}
46 %*                                                                      *
47 %************************************************************************
48
49 If we're compiling with GHC (and we're not cross-compiling), then we
50 know that minBound and maxBound :: Int are the right values for the
51 target architecture.  Otherwise, we assume -2^31 and 2^31-1
52 respectively (which will be wrong on a 64-bit machine).
53
54 \begin{code}
55 tARGET_MIN_INT, tARGET_MAX_INT, tARGET_MAX_WORD :: Integer
56 #if __GLASGOW_HASKELL__
57 tARGET_MIN_INT  = toInteger (minBound :: Int)
58 tARGET_MAX_INT  = toInteger (maxBound :: Int)
59 #else
60 tARGET_MIN_INT = -2147483648
61 tARGET_MAX_INT =  2147483647
62 #endif
63 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
64
65 tARGET_MAX_CHAR :: Int
66 tARGET_MAX_CHAR = 0x10ffff
67 \end{code}
68  
69
70 %************************************************************************
71 %*                                                                      *
72 \subsection{Literals}
73 %*                                                                      *
74 %************************************************************************
75
76 So-called @Literals@ are {\em either}:
77 \begin{itemize}
78 \item
79 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
80 which is presumed to be surrounded by appropriate constructors
81 (@mKINT@, etc.), so that the overall thing makes sense.
82 \item
83 An Integer, Rational, or String literal whose representation we are
84 {\em uncommitted} about; i.e., the surrounding with constructors,
85 function applications, etc., etc., has not yet been done.
86 \end{itemize}
87
88 \begin{code}
89 data Literal
90   =     ------------------
91         -- First the primitive guys
92     MachChar    Int             -- Char#        At least 31 bits
93   | MachStr     FAST_STRING
94
95   | MachAddr    Integer -- Whatever this machine thinks is a "pointer"
96
97   | MachInt     Integer         -- Int#         At least 32 bits
98   | MachInt64   Integer         -- Int64#       At least 64 bits
99   | MachWord    Integer         -- Word#        At least 32 bits
100   | MachWord64  Integer         -- Word64#      At least 64 bits
101
102   | MachFloat   Rational
103   | MachDouble  Rational
104
105         -- MachLabel is used (only) for the literal derived from a 
106         -- "foreign label" declaration.
107         -- string argument is the name of a symbol.  This literal
108         -- refers to the *address* of the label.
109   | MachLabel   FAST_STRING             -- always an Addr#
110
111         -- lit-lits only work for via-C compilation, hence they
112         -- are deprecated.  The string is emitted verbatim into
113         -- the C file, and can therefore be any C expression,
114         -- macro call, #defined constant etc.
115   | MachLitLit  FAST_STRING Type        -- Type might be Addr# or Int# etc
116 \end{code}
117
118 \begin{code}
119 instance Outputable Literal where
120     ppr lit = pprLit lit
121
122 instance Show Literal where
123     showsPrec p lit = showsPrecSDoc p (ppr lit)
124
125 instance Eq Literal where
126     a == b = case (a `compare` b) of { EQ -> True;   _ -> False }
127     a /= b = case (a `compare` b) of { EQ -> False;  _ -> True  }
128
129 instance Ord Literal where
130     a <= b = case (a `compare` b) of { LT -> True;  EQ -> True;  GT -> False }
131     a <  b = case (a `compare` b) of { LT -> True;  EQ -> False; GT -> False }
132     a >= b = case (a `compare` b) of { LT -> False; EQ -> True;  GT -> True  }
133     a >  b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True  }
134     compare a b = cmpLit a b
135 \end{code}
136
137
138         Construction
139         ~~~~~~~~~~~~
140 \begin{code}
141 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
142
143 mkMachInt  x   = ASSERT2( inIntRange x,  integer x ) MachInt x
144 mkMachWord x   = ASSERT2( inWordRange x, integer x ) MachWord x
145 mkMachInt64  x = MachInt64 x    -- Assertions?
146 mkMachWord64 x = MachWord64 x   -- Ditto?
147
148 inIntRange, inWordRange :: Integer -> Bool
149 inIntRange  x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
150 inWordRange x = x >= 0              && x <= tARGET_MAX_WORD
151
152 inCharRange :: Int -> Bool
153 inCharRange c =  c >= 0 && c <= tARGET_MAX_CHAR
154 \end{code}
155
156         Coercions
157         ~~~~~~~~~
158 \begin{code}
159 word2IntLit, int2WordLit, char2IntLit, int2CharLit,
160  float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
161  addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit :: Literal -> Literal
162
163 word2IntLit (MachWord w) 
164   | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
165   | otherwise          = MachInt w
166
167 int2WordLit (MachInt i)
168   | i < 0     = MachWord (1 + tARGET_MAX_WORD + i)      -- (-1)  --->  tARGET_MAX_WORD
169   | otherwise = MachWord i
170
171 char2IntLit (MachChar c) = MachInt  (toInteger c)
172 int2CharLit (MachInt  i) = MachChar (fromInteger i)
173
174 float2IntLit (MachFloat f) = MachInt   (truncate    f)
175 int2FloatLit (MachInt   i) = MachFloat (fromInteger i)
176
177 double2IntLit (MachFloat f) = MachInt    (truncate    f)
178 int2DoubleLit (MachInt   i) = MachDouble (fromInteger i)
179
180 addr2IntLit (MachAddr a) = MachInt  a
181 int2AddrLit (MachInt  i) = MachAddr i
182
183 float2DoubleLit (MachFloat  f) = MachDouble f
184 double2FloatLit (MachDouble d) = MachFloat  d
185 \end{code}
186
187         Predicates
188         ~~~~~~~~~~
189 \begin{code}
190 isLitLitLit (MachLitLit _ _) = True
191 isLitLitLit _                = False
192
193 maybeLitLit (MachLitLit s t) = Just (s,t)
194 maybeLitLit _                = Nothing
195
196 litIsDupable :: Literal -> Bool
197         -- True if code space does not go bad if we duplicate this literal
198         -- False principally of strings
199 litIsDupable (MachStr _) = False
200 litIsDupable other       = True
201
202 litSize :: Literal -> Int
203         -- used by CoreUnfold.sizeExpr
204 litSize (MachStr str) = lengthFS str `div` 4
205 litSize _other        = 1
206 \end{code}
207
208         Types
209         ~~~~~
210 \begin{code}
211 literalType :: Literal -> Type
212 literalType (MachChar _)          = charPrimTy
213 literalType (MachStr  _)          = addrPrimTy
214 literalType (MachAddr _)          = addrPrimTy
215 literalType (MachInt  _)          = intPrimTy
216 literalType (MachWord  _)         = wordPrimTy
217 literalType (MachInt64  _)        = int64PrimTy
218 literalType (MachWord64  _)       = word64PrimTy
219 literalType (MachFloat _)         = floatPrimTy
220 literalType (MachDouble _)        = doublePrimTy
221 literalType (MachLabel _)         = addrPrimTy
222 literalType (MachLitLit _ ty)     = ty
223 \end{code}
224
225 \begin{code}
226 literalPrimRep :: Literal -> PrimRep
227
228 literalPrimRep (MachChar _)       = CharRep
229 literalPrimRep (MachStr _)        = AddrRep  -- specifically: "char *"
230 literalPrimRep (MachAddr  _)      = AddrRep
231 literalPrimRep (MachInt _)        = IntRep
232 literalPrimRep (MachWord _)       = WordRep
233 literalPrimRep (MachInt64 _)      = Int64Rep
234 literalPrimRep (MachWord64 _)     = Word64Rep
235 literalPrimRep (MachFloat _)      = FloatRep
236 literalPrimRep (MachDouble _)     = DoubleRep
237 literalPrimRep (MachLabel _)      = AddrRep
238 literalPrimRep (MachLitLit _ ty)  = typePrimRep ty
239 \end{code}
240
241
242         Comparison
243         ~~~~~~~~~~
244 \begin{code}
245 cmpLit (MachChar      a)   (MachChar       b)   = a `compare` b
246 cmpLit (MachStr       a)   (MachStr        b)   = a `compare` b
247 cmpLit (MachAddr      a)   (MachAddr       b)   = a `compare` b
248 cmpLit (MachInt       a)   (MachInt        b)   = a `compare` b
249 cmpLit (MachWord      a)   (MachWord       b)   = a `compare` b
250 cmpLit (MachInt64     a)   (MachInt64      b)   = a `compare` b
251 cmpLit (MachWord64    a)   (MachWord64     b)   = a `compare` b
252 cmpLit (MachFloat     a)   (MachFloat      b)   = a `compare` b
253 cmpLit (MachDouble    a)   (MachDouble     b)   = a `compare` b
254 cmpLit (MachLabel     a)   (MachLabel      b)   = a `compare` b
255 cmpLit (MachLitLit    a b) (MachLitLit    c d)  = (a `compare` c) `thenCmp` (b `compare` d)
256 cmpLit lit1                lit2                 | litTag lit1 <# litTag lit2 = LT
257                                                 | otherwise                    = GT
258
259 litTag (MachChar      _)   = _ILIT(1)
260 litTag (MachStr       _)   = _ILIT(2)
261 litTag (MachAddr      _)   = _ILIT(3)
262 litTag (MachInt       _)   = _ILIT(4)
263 litTag (MachWord      _)   = _ILIT(5)
264 litTag (MachInt64     _)   = _ILIT(6)
265 litTag (MachWord64    _)   = _ILIT(7)
266 litTag (MachFloat     _)   = _ILIT(8)
267 litTag (MachDouble    _)   = _ILIT(9)
268 litTag (MachLabel     _)   = _ILIT(10)
269 litTag (MachLitLit    _ _) = _ILIT(11)
270 \end{code}
271
272         Printing
273         ~~~~~~~~
274 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
275   exceptions: MachFloat and MachAddr get an initial keyword prefix
276
277 \begin{code}
278 pprLit lit
279   = getPprStyle $ \ sty ->
280     let
281       code_style  = codeStyle  sty
282       iface_style = ifaceStyle sty
283     in
284     case lit of
285       MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show ch)]
286                   | otherwise  -> pprHsChar ch
287
288       MachStr s | code_style -> pprFSInCStyle s
289                 | otherwise  -> pprHsString s
290       -- Warning: printing MachStr in code_style assumes it contains
291       -- only characters '\0'..'\xFF'!
292
293       MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
294                                 -- Avoid a problem whereby gcc interprets
295                                 -- the constant minInt as unsigned.
296                 | otherwise -> pprIntVal i
297
298       MachInt64 i | code_style -> pprIntVal i           -- Same problem with gcc???
299                   | otherwise -> ptext SLIT("__int64") <+> integer i
300
301       MachWord w | code_style -> pprHexVal w
302                  | otherwise  -> ptext SLIT("__word") <+> integer w
303
304       MachWord64 w | code_style -> pprHexVal w
305                    | otherwise  -> ptext SLIT("__word64") <+> integer w
306
307       MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
308                   | otherwise  -> ptext SLIT("__float") <+> rational f
309
310       MachDouble d | iface_style && d < 0 -> parens (rational d)
311                    | otherwise            -> rational d
312
313       MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
314                  | otherwise  -> ptext SLIT("__addr") <+> integer p
315
316       MachLabel l | code_style -> ptext SLIT("(&") <> ptext l <> char ')'
317                   | otherwise  -> ptext SLIT("__label") <+> pprHsString l
318
319       MachLitLit s ty | code_style  -> ptext s
320                       | otherwise   -> parens (hsep [ptext SLIT("__litlit"), 
321                                                      pprHsString s,
322                                                      pprParendType ty])
323
324 pprIntVal :: Integer -> SDoc
325 -- Print negative integers with parens to be sure it's unambiguous
326 pprIntVal i | i < 0     = parens (integer i)
327             | otherwise = integer i
328                 
329 pprHexVal :: Integer -> SDoc
330 -- Print in C hex format: 0x13fa 
331 pprHexVal 0 = ptext SLIT("0x0")
332 pprHexVal w = ptext SLIT("0x") <> go w
333             where
334               go 0 = empty
335               go w = go quot <> dig
336                    where
337                      (quot,rem) = w `quotRem` 16
338                      dig | rem < 10  = char (chr (fromInteger rem + ord '0'))
339                          | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
340 \end{code}
341
342
343 %************************************************************************
344 %*                                                                      *
345 \subsection{Hashing}
346 %*                                                                      *
347 %************************************************************************
348
349 Hash values should be zero or a positive integer.  No negatives please.
350 (They mess up the UniqFM for some reason.)
351
352 \begin{code}
353 hashLiteral :: Literal -> Int
354 hashLiteral (MachChar c)        = c + 1000      -- Keep it out of range of common ints
355 hashLiteral (MachStr s)         = hashFS s
356 hashLiteral (MachAddr i)        = hashInteger i
357 hashLiteral (MachInt i)         = hashInteger i
358 hashLiteral (MachInt64 i)       = hashInteger i
359 hashLiteral (MachWord i)        = hashInteger i
360 hashLiteral (MachWord64 i)      = hashInteger i
361 hashLiteral (MachFloat r)       = hashRational r
362 hashLiteral (MachDouble r)      = hashRational r
363 hashLiteral (MachLabel s)       = hashFS s
364 hashLiteral (MachLitLit s _)    = hashFS s
365
366 hashRational :: Rational -> Int
367 hashRational r = hashInteger (numerator r)
368
369 hashInteger :: Integer -> Int
370 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
371                 -- The 1+ is to avoid zero, which is a Bad Number
372                 -- since we use * to combine hash values
373
374 hashFS :: FAST_STRING -> Int
375 hashFS s = iBox (uniqueOfFS s)
376 \end{code}