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