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