2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
8 ( Literal(..) -- Exported to ParseIface
9 , mkMachInt, mkMachWord
10 , mkMachInt64, mkMachWord64
11 , isLitLitLit, maybeLitLit, litSize, litIsDupable,
12 , literalType, literalPrimRep
15 , inIntRange, inWordRange, tARGET_MAX_INT
17 , word2IntLit, int2WordLit, char2IntLit, int2CharLit
18 , float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit
19 , addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit
22 #include "HsVersions.h"
24 import TysPrim ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
25 intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
27 import PrimRep ( PrimRep(..) )
28 import Type ( Type, typePrimRep )
29 import PprType ( pprParendType )
30 import CStrings ( pprFSInCStyle )
34 import Util ( thenCmp )
36 import Ratio ( numerator )
37 import FastString ( uniqueOfFS, lengthFS )
38 import Char ( ord, chr )
43 %************************************************************************
47 %************************************************************************
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).
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)
60 tARGET_MIN_INT = -2147483648
61 tARGET_MAX_INT = 2147483647
63 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
67 %************************************************************************
71 %************************************************************************
73 So-called @Literals@ are {\em either}:
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.
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.
88 -- First the primitive guys
89 MachChar Int -- Char# At least 31 bits
92 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
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
100 | MachDouble Rational
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#
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
114 instance Outputable Literal where
117 instance Show Literal where
118 showsPrec p lit = showsPrecSDoc p (ppr lit)
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 }
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
136 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
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?
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
151 word2IntLit, int2WordLit, char2IntLit, int2CharLit,
152 float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
153 addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit :: Literal -> Literal
155 word2IntLit (MachWord w)
156 | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
157 | otherwise = MachInt w
159 int2WordLit (MachInt i)
160 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
161 | otherwise = MachWord i
163 char2IntLit (MachChar c) = MachInt (toInteger c)
164 int2CharLit (MachInt i) = MachChar (fromInteger i)
166 float2IntLit (MachFloat f) = MachInt (truncate f)
167 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
169 double2IntLit (MachFloat f) = MachInt (truncate f)
170 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
172 addr2IntLit (MachAddr a) = MachInt a
173 int2AddrLit (MachInt i) = MachAddr i
175 float2DoubleLit (MachFloat f) = MachDouble f
176 double2FloatLit (MachDouble d) = MachFloat d
182 isLitLitLit (MachLitLit _ _) = True
183 isLitLitLit _ = False
185 maybeLitLit (MachLitLit s t) = Just (s,t)
186 maybeLitLit _ = Nothing
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
194 litSize :: Literal -> Int
195 -- used by CoreUnfold.sizeExpr
196 litSize (MachStr str) = lengthFS str `div` 4
203 literalType :: Literal -> Type
204 literalType (MachChar _) = charPrimTy
205 literalType (MachStr _) = addrPrimTy
206 literalType (MachAddr _) = addrPrimTy
207 literalType (MachInt _) = intPrimTy
208 literalType (MachWord _) = wordPrimTy
209 literalType (MachInt64 _) = int64PrimTy
210 literalType (MachWord64 _) = word64PrimTy
211 literalType (MachFloat _) = floatPrimTy
212 literalType (MachDouble _) = doublePrimTy
213 literalType (MachLabel _) = addrPrimTy
214 literalType (MachLitLit _ ty) = ty
218 literalPrimRep :: Literal -> PrimRep
220 literalPrimRep (MachChar _) = CharRep
221 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
222 literalPrimRep (MachAddr _) = AddrRep
223 literalPrimRep (MachInt _) = IntRep
224 literalPrimRep (MachWord _) = WordRep
225 literalPrimRep (MachInt64 _) = Int64Rep
226 literalPrimRep (MachWord64 _) = Word64Rep
227 literalPrimRep (MachFloat _) = FloatRep
228 literalPrimRep (MachDouble _) = DoubleRep
229 literalPrimRep (MachLabel _) = AddrRep
230 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
237 cmpLit (MachChar a) (MachChar b) = a `compare` b
238 cmpLit (MachStr a) (MachStr b) = a `compare` b
239 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
240 cmpLit (MachInt a) (MachInt b) = a `compare` b
241 cmpLit (MachWord a) (MachWord b) = a `compare` b
242 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
243 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
244 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
245 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
246 cmpLit (MachLabel a) (MachLabel b) = a `compare` b
247 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
248 cmpLit lit1 lit2 | litTag lit1 <# litTag lit2 = LT
251 litTag (MachChar _) = _ILIT(1)
252 litTag (MachStr _) = _ILIT(2)
253 litTag (MachAddr _) = _ILIT(3)
254 litTag (MachInt _) = _ILIT(4)
255 litTag (MachWord _) = _ILIT(5)
256 litTag (MachInt64 _) = _ILIT(6)
257 litTag (MachWord64 _) = _ILIT(7)
258 litTag (MachFloat _) = _ILIT(8)
259 litTag (MachDouble _) = _ILIT(9)
260 litTag (MachLabel _) = _ILIT(10)
261 litTag (MachLitLit _ _) = _ILIT(11)
266 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
267 exceptions: MachFloat and MachAddr get an initial keyword prefix
271 = getPprStyle $ \ sty ->
273 code_style = codeStyle sty
274 iface_style = ifaceStyle sty
277 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show ch)]
278 | otherwise -> pprHsChar ch
280 MachStr s | code_style -> pprFSInCStyle s
281 | otherwise -> pprHsString s
282 -- Warning: printing MachStr in code_style assumes it contains
283 -- only characters '\0'..'\xFF'!
285 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
286 -- Avoid a problem whereby gcc interprets
287 -- the constant minInt as unsigned.
288 | otherwise -> pprIntVal i
290 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
291 | otherwise -> ptext SLIT("__int64") <+> integer i
293 MachWord w | code_style -> pprHexVal w
294 | otherwise -> ptext SLIT("__word") <+> integer w
296 MachWord64 w | code_style -> pprHexVal w
297 | otherwise -> ptext SLIT("__word64") <+> integer w
299 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
300 | otherwise -> ptext SLIT("__float") <+> rational f
302 MachDouble d | iface_style && d < 0 -> parens (rational d)
303 | otherwise -> rational d
305 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
306 | otherwise -> ptext SLIT("__addr") <+> integer p
308 MachLabel l | code_style -> ptext SLIT("(&") <> ptext l <> char ')'
309 | otherwise -> ptext SLIT("__label") <+> pprHsString l
311 MachLitLit s ty | code_style -> ptext s
312 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
316 pprIntVal :: Integer -> SDoc
317 -- Print negative integers with parens to be sure it's unambiguous
318 pprIntVal i | i < 0 = parens (integer i)
319 | otherwise = integer i
321 pprHexVal :: Integer -> SDoc
322 -- Print in C hex format: 0x13fa
323 pprHexVal 0 = ptext SLIT("0x0")
324 pprHexVal w = ptext SLIT("0x") <> go w
327 go w = go quot <> dig
329 (quot,rem) = w `quotRem` 16
330 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
331 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
335 %************************************************************************
339 %************************************************************************
341 Hash values should be zero or a positive integer. No negatives please.
342 (They mess up the UniqFM for some reason.)
345 hashLiteral :: Literal -> Int
346 hashLiteral (MachChar c) = c + 1000 -- Keep it out of range of common ints
347 hashLiteral (MachStr s) = hashFS s
348 hashLiteral (MachAddr i) = hashInteger i
349 hashLiteral (MachInt i) = hashInteger i
350 hashLiteral (MachInt64 i) = hashInteger i
351 hashLiteral (MachWord i) = hashInteger i
352 hashLiteral (MachWord64 i) = hashInteger i
353 hashLiteral (MachFloat r) = hashRational r
354 hashLiteral (MachDouble r) = hashRational r
355 hashLiteral (MachLabel s) = hashFS s
356 hashLiteral (MachLitLit s _) = hashFS s
358 hashRational :: Rational -> Int
359 hashRational r = hashInteger (numerator r)
361 hashInteger :: Integer -> Int
362 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
363 -- The 1+ is to avoid zero, which is a Bad Number
364 -- since we use * to combine hash values
366 hashFS :: FAST_STRING -> Int
367 hashFS s = iBox (uniqueOfFS s)