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, inCharRange
17 , word2IntLit, int2WordLit
18 , intToInt8Lit, intToInt16Lit, intToInt32Lit
19 , wordToWord8Lit, wordToWord16Lit, wordToWord32Lit
20 , char2IntLit, int2CharLit
21 , float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit
22 , addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit
25 #include "HsVersions.h"
27 import TysPrim ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
28 intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
30 import PrimRep ( PrimRep(..) )
31 import TcType ( Type, tcCmpType )
32 import Type ( typePrimRep )
33 import PprType ( pprParendType )
34 import CStrings ( pprFSInCStyle )
38 import Util ( thenCmp )
40 import Ratio ( numerator )
41 import FastString ( uniqueOfFS, lengthFS )
42 import Int ( Int8, Int16, Int32 )
43 import Word ( Word8, Word16, Word32 )
44 import Char ( ord, chr )
49 %************************************************************************
53 %************************************************************************
55 If we're compiling with GHC (and we're not cross-compiling), then we
56 know that minBound and maxBound :: Int are the right values for the
57 target architecture. Otherwise, we assume -2^31 and 2^31-1
58 respectively (which will be wrong on a 64-bit machine).
61 tARGET_MIN_INT, tARGET_MAX_INT, tARGET_MAX_WORD :: Integer
62 #if __GLASGOW_HASKELL__
63 tARGET_MIN_INT = toInteger (minBound :: Int)
64 tARGET_MAX_INT = toInteger (maxBound :: Int)
66 tARGET_MIN_INT = -2147483648
67 tARGET_MAX_INT = 2147483647
69 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
71 tARGET_MAX_CHAR :: Int
72 tARGET_MAX_CHAR = 0x10ffff
76 %************************************************************************
80 %************************************************************************
82 So-called @Literals@ are {\em either}:
85 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
86 which is presumed to be surrounded by appropriate constructors
87 (@mKINT@, etc.), so that the overall thing makes sense.
89 An Integer, Rational, or String literal whose representation we are
90 {\em uncommitted} about; i.e., the surrounding with constructors,
91 function applications, etc., etc., has not yet been done.
97 -- First the primitive guys
98 MachChar Int -- Char# At least 31 bits
101 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
103 | MachInt Integer -- Int# At least 32 bits
104 | MachInt64 Integer -- Int64# At least 64 bits
105 | MachWord Integer -- Word# At least 32 bits
106 | MachWord64 Integer -- Word64# At least 64 bits
109 | MachDouble Rational
111 -- MachLabel is used (only) for the literal derived from a
112 -- "foreign label" declaration.
113 -- string argument is the name of a symbol. This literal
114 -- refers to the *address* of the label.
115 | MachLabel FAST_STRING -- always an Addr#
117 -- lit-lits only work for via-C compilation, hence they
118 -- are deprecated. The string is emitted verbatim into
119 -- the C file, and can therefore be any C expression,
120 -- macro call, #defined constant etc.
121 | MachLitLit FAST_STRING Type -- Type might be Addr# or Int# etc
125 instance Outputable Literal where
128 instance Show Literal where
129 showsPrec p lit = showsPrecSDoc p (ppr lit)
131 instance Eq Literal where
132 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
133 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
135 instance Ord Literal where
136 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
137 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
138 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
139 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
140 compare a b = cmpLit a b
147 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
149 mkMachInt x = ASSERT2( inIntRange x, integer x ) MachInt x
150 mkMachWord x = ASSERT2( inWordRange x, integer x ) MachWord x
151 mkMachInt64 x = MachInt64 x -- Assertions?
152 mkMachWord64 x = MachWord64 x -- Ditto?
154 inIntRange, inWordRange :: Integer -> Bool
155 inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
156 inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
158 inCharRange :: Int -> Bool
159 inCharRange c = c >= 0 && c <= tARGET_MAX_CHAR
165 word2IntLit, int2WordLit,
166 intToInt8Lit, intToInt16Lit, intToInt32Lit,
167 wordToWord8Lit, wordToWord16Lit, wordToWord32Lit,
168 char2IntLit, int2CharLit,
169 float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
170 addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit
171 :: Literal -> Literal
173 word2IntLit (MachWord w)
174 | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
175 | otherwise = MachInt w
177 int2WordLit (MachInt i)
178 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
179 | otherwise = MachWord i
181 intToInt8Lit (MachInt i) = MachInt (toInteger (fromInteger i :: Int8))
182 intToInt16Lit (MachInt i) = MachInt (toInteger (fromInteger i :: Int16))
183 intToInt32Lit (MachInt i) = MachInt (toInteger (fromInteger i :: Int32))
184 wordToWord8Lit (MachWord w) = MachWord (toInteger (fromInteger w :: Word8))
185 wordToWord16Lit (MachWord w) = MachWord (toInteger (fromInteger w :: Word16))
186 wordToWord32Lit (MachWord w) = MachWord (toInteger (fromInteger w :: Word32))
188 char2IntLit (MachChar c) = MachInt (toInteger c)
189 int2CharLit (MachInt i) = MachChar (fromInteger i)
191 float2IntLit (MachFloat f) = MachInt (truncate f)
192 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
194 double2IntLit (MachFloat f) = MachInt (truncate f)
195 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
197 addr2IntLit (MachAddr a) = MachInt a
198 int2AddrLit (MachInt i) = MachAddr i
200 float2DoubleLit (MachFloat f) = MachDouble f
201 double2FloatLit (MachDouble d) = MachFloat d
207 isLitLitLit (MachLitLit _ _) = True
208 isLitLitLit _ = False
210 maybeLitLit (MachLitLit s t) = Just (s,t)
211 maybeLitLit _ = Nothing
213 litIsDupable :: Literal -> Bool
214 -- True if code space does not go bad if we duplicate this literal
215 -- False principally of strings
216 litIsDupable (MachStr _) = False
217 litIsDupable other = True
219 litSize :: Literal -> Int
220 -- used by CoreUnfold.sizeExpr
221 litSize (MachStr str) = lengthFS str `div` 4
228 literalType :: Literal -> Type
229 literalType (MachChar _) = charPrimTy
230 literalType (MachStr _) = addrPrimTy
231 literalType (MachAddr _) = addrPrimTy
232 literalType (MachInt _) = intPrimTy
233 literalType (MachWord _) = wordPrimTy
234 literalType (MachInt64 _) = int64PrimTy
235 literalType (MachWord64 _) = word64PrimTy
236 literalType (MachFloat _) = floatPrimTy
237 literalType (MachDouble _) = doublePrimTy
238 literalType (MachLabel _) = addrPrimTy
239 literalType (MachLitLit _ ty) = ty
243 literalPrimRep :: Literal -> PrimRep
245 literalPrimRep (MachChar _) = CharRep
246 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
247 literalPrimRep (MachAddr _) = AddrRep
248 literalPrimRep (MachInt _) = IntRep
249 literalPrimRep (MachWord _) = WordRep
250 literalPrimRep (MachInt64 _) = Int64Rep
251 literalPrimRep (MachWord64 _) = Word64Rep
252 literalPrimRep (MachFloat _) = FloatRep
253 literalPrimRep (MachDouble _) = DoubleRep
254 literalPrimRep (MachLabel _) = AddrRep
255 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
262 cmpLit (MachChar a) (MachChar b) = a `compare` b
263 cmpLit (MachStr a) (MachStr b) = a `compare` b
264 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
265 cmpLit (MachInt a) (MachInt b) = a `compare` b
266 cmpLit (MachWord a) (MachWord b) = a `compare` b
267 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
268 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
269 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
270 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
271 cmpLit (MachLabel a) (MachLabel b) = a `compare` b
272 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `tcCmpType` d)
273 cmpLit lit1 lit2 | litTag lit1 <# litTag lit2 = LT
276 litTag (MachChar _) = _ILIT(1)
277 litTag (MachStr _) = _ILIT(2)
278 litTag (MachAddr _) = _ILIT(3)
279 litTag (MachInt _) = _ILIT(4)
280 litTag (MachWord _) = _ILIT(5)
281 litTag (MachInt64 _) = _ILIT(6)
282 litTag (MachWord64 _) = _ILIT(7)
283 litTag (MachFloat _) = _ILIT(8)
284 litTag (MachDouble _) = _ILIT(9)
285 litTag (MachLabel _) = _ILIT(10)
286 litTag (MachLitLit _ _) = _ILIT(11)
291 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
292 exceptions: MachFloat and MachAddr get an initial keyword prefix
296 = getPprStyle $ \ sty ->
298 code_style = codeStyle sty
299 iface_style = ifaceStyle sty
302 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show ch)]
303 | otherwise -> pprHsChar ch
305 MachStr s | code_style -> pprFSInCStyle s
306 | otherwise -> pprHsString s
307 -- Warning: printing MachStr in code_style assumes it contains
308 -- only characters '\0'..'\xFF'!
310 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
311 -- Avoid a problem whereby gcc interprets
312 -- the constant minInt as unsigned.
313 | otherwise -> pprIntVal i
315 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
316 | otherwise -> ptext SLIT("__int64") <+> integer i
318 MachWord w | code_style -> pprHexVal w
319 | otherwise -> ptext SLIT("__word") <+> integer w
321 MachWord64 w | code_style -> pprHexVal w
322 | otherwise -> ptext SLIT("__word64") <+> integer w
324 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
325 | otherwise -> ptext SLIT("__float") <+> rational f
327 MachDouble d | iface_style && d < 0 -> parens (rational d)
328 | otherwise -> rational d
330 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
331 | otherwise -> ptext SLIT("__addr") <+> integer p
333 MachLabel l | code_style -> ptext SLIT("(&") <> ptext l <> char ')'
334 | otherwise -> ptext SLIT("__label") <+> pprHsString l
336 MachLitLit s ty | code_style -> ptext s
337 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
341 pprIntVal :: Integer -> SDoc
342 -- Print negative integers with parens to be sure it's unambiguous
343 pprIntVal i | i < 0 = parens (integer i)
344 | otherwise = integer i
346 pprHexVal :: Integer -> SDoc
347 -- Print in C hex format: 0x13fa
348 pprHexVal 0 = ptext SLIT("0x0")
349 pprHexVal w = ptext SLIT("0x") <> go w
352 go w = go quot <> dig
354 (quot,rem) = w `quotRem` 16
355 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
356 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
360 %************************************************************************
364 %************************************************************************
366 Hash values should be zero or a positive integer. No negatives please.
367 (They mess up the UniqFM for some reason.)
370 hashLiteral :: Literal -> Int
371 hashLiteral (MachChar c) = c + 1000 -- Keep it out of range of common ints
372 hashLiteral (MachStr s) = hashFS s
373 hashLiteral (MachAddr i) = hashInteger i
374 hashLiteral (MachInt i) = hashInteger i
375 hashLiteral (MachInt64 i) = hashInteger i
376 hashLiteral (MachWord i) = hashInteger i
377 hashLiteral (MachWord64 i) = hashInteger i
378 hashLiteral (MachFloat r) = hashRational r
379 hashLiteral (MachDouble r) = hashRational r
380 hashLiteral (MachLabel s) = hashFS s
381 hashLiteral (MachLitLit s _) = hashFS s
383 hashRational :: Rational -> Int
384 hashRational r = hashInteger (numerator r)
386 hashInteger :: Integer -> Int
387 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
388 -- The 1+ is to avoid zero, which is a Bad Number
389 -- since we use * to combine hash values
391 hashFS :: FAST_STRING -> Int
392 hashFS s = iBox (uniqueOfFS s)