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