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, 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
65 tARGET_MAX_CHAR :: Int
66 tARGET_MAX_CHAR = 0x10ffff
70 %************************************************************************
74 %************************************************************************
76 So-called @Literals@ are {\em either}:
79 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
80 which is presumed to be surrounded by appropriate constructors
81 (@mKINT@, etc.), so that the overall thing makes sense.
83 An Integer, Rational, or String literal whose representation we are
84 {\em uncommitted} about; i.e., the surrounding with constructors,
85 function applications, etc., etc., has not yet been done.
91 -- First the primitive guys
92 MachChar Int -- Char# At least 31 bits
95 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
97 | MachInt Integer -- Int# At least 32 bits
98 | MachInt64 Integer -- Int64# At least 64 bits
99 | MachWord Integer -- Word# At least 32 bits
100 | MachWord64 Integer -- Word64# At least 64 bits
103 | MachDouble Rational
105 -- MachLabel is used (only) for the literal derived from a
106 -- "foreign label" declaration.
107 -- string argument is the name of a symbol. This literal
108 -- refers to the *address* of the label.
109 | MachLabel FAST_STRING -- always an Addr#
111 -- lit-lits only work for via-C compilation, hence they
112 -- are deprecated. The string is emitted verbatim into
113 -- the C file, and can therefore be any C expression,
114 -- macro call, #defined constant etc.
115 | MachLitLit FAST_STRING Type -- Type might be Addr# or Int# etc
119 instance Outputable Literal where
122 instance Show Literal where
123 showsPrec p lit = showsPrecSDoc p (ppr lit)
125 instance Eq Literal where
126 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
127 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
129 instance Ord Literal where
130 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
131 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
132 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
133 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
134 compare a b = cmpLit a b
141 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
143 mkMachInt x = ASSERT2( inIntRange x, integer x ) MachInt x
144 mkMachWord x = ASSERT2( inWordRange x, integer x ) MachWord x
145 mkMachInt64 x = MachInt64 x -- Assertions?
146 mkMachWord64 x = MachWord64 x -- Ditto?
148 inIntRange, inWordRange :: Integer -> Bool
149 inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
150 inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
152 inCharRange :: Int -> Bool
153 inCharRange c = c >= 0 && c <= tARGET_MAX_CHAR
159 word2IntLit, int2WordLit, char2IntLit, int2CharLit,
160 float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
161 addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit :: Literal -> Literal
163 word2IntLit (MachWord w)
164 | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
165 | otherwise = MachInt w
167 int2WordLit (MachInt i)
168 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
169 | otherwise = MachWord i
171 char2IntLit (MachChar c) = MachInt (toInteger c)
172 int2CharLit (MachInt i) = MachChar (fromInteger i)
174 float2IntLit (MachFloat f) = MachInt (truncate f)
175 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
177 double2IntLit (MachFloat f) = MachInt (truncate f)
178 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
180 addr2IntLit (MachAddr a) = MachInt a
181 int2AddrLit (MachInt i) = MachAddr i
183 float2DoubleLit (MachFloat f) = MachDouble f
184 double2FloatLit (MachDouble d) = MachFloat d
190 isLitLitLit (MachLitLit _ _) = True
191 isLitLitLit _ = False
193 maybeLitLit (MachLitLit s t) = Just (s,t)
194 maybeLitLit _ = Nothing
196 litIsDupable :: Literal -> Bool
197 -- True if code space does not go bad if we duplicate this literal
198 -- False principally of strings
199 litIsDupable (MachStr _) = False
200 litIsDupable other = True
202 litSize :: Literal -> Int
203 -- used by CoreUnfold.sizeExpr
204 litSize (MachStr str) = lengthFS str `div` 4
211 literalType :: Literal -> Type
212 literalType (MachChar _) = charPrimTy
213 literalType (MachStr _) = addrPrimTy
214 literalType (MachAddr _) = addrPrimTy
215 literalType (MachInt _) = intPrimTy
216 literalType (MachWord _) = wordPrimTy
217 literalType (MachInt64 _) = int64PrimTy
218 literalType (MachWord64 _) = word64PrimTy
219 literalType (MachFloat _) = floatPrimTy
220 literalType (MachDouble _) = doublePrimTy
221 literalType (MachLabel _) = addrPrimTy
222 literalType (MachLitLit _ ty) = ty
226 literalPrimRep :: Literal -> PrimRep
228 literalPrimRep (MachChar _) = CharRep
229 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
230 literalPrimRep (MachAddr _) = AddrRep
231 literalPrimRep (MachInt _) = IntRep
232 literalPrimRep (MachWord _) = WordRep
233 literalPrimRep (MachInt64 _) = Int64Rep
234 literalPrimRep (MachWord64 _) = Word64Rep
235 literalPrimRep (MachFloat _) = FloatRep
236 literalPrimRep (MachDouble _) = DoubleRep
237 literalPrimRep (MachLabel _) = AddrRep
238 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
245 cmpLit (MachChar a) (MachChar b) = a `compare` b
246 cmpLit (MachStr a) (MachStr b) = a `compare` b
247 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
248 cmpLit (MachInt a) (MachInt b) = a `compare` b
249 cmpLit (MachWord a) (MachWord b) = a `compare` b
250 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
251 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
252 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
253 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
254 cmpLit (MachLabel a) (MachLabel b) = a `compare` b
255 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
256 cmpLit lit1 lit2 | litTag lit1 <# litTag lit2 = LT
259 litTag (MachChar _) = _ILIT(1)
260 litTag (MachStr _) = _ILIT(2)
261 litTag (MachAddr _) = _ILIT(3)
262 litTag (MachInt _) = _ILIT(4)
263 litTag (MachWord _) = _ILIT(5)
264 litTag (MachInt64 _) = _ILIT(6)
265 litTag (MachWord64 _) = _ILIT(7)
266 litTag (MachFloat _) = _ILIT(8)
267 litTag (MachDouble _) = _ILIT(9)
268 litTag (MachLabel _) = _ILIT(10)
269 litTag (MachLitLit _ _) = _ILIT(11)
274 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
275 exceptions: MachFloat and MachAddr get an initial keyword prefix
279 = getPprStyle $ \ sty ->
281 code_style = codeStyle sty
282 iface_style = ifaceStyle sty
285 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show ch)]
286 | otherwise -> pprHsChar ch
288 MachStr s | code_style -> pprFSInCStyle s
289 | otherwise -> pprHsString s
290 -- Warning: printing MachStr in code_style assumes it contains
291 -- only characters '\0'..'\xFF'!
293 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
294 -- Avoid a problem whereby gcc interprets
295 -- the constant minInt as unsigned.
296 | otherwise -> pprIntVal i
298 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
299 | otherwise -> ptext SLIT("__int64") <+> integer i
301 MachWord w | code_style -> pprHexVal w
302 | otherwise -> ptext SLIT("__word") <+> integer w
304 MachWord64 w | code_style -> pprHexVal w
305 | otherwise -> ptext SLIT("__word64") <+> integer w
307 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
308 | otherwise -> ptext SLIT("__float") <+> rational f
310 MachDouble d | iface_style && d < 0 -> parens (rational d)
311 | otherwise -> rational d
313 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
314 | otherwise -> ptext SLIT("__addr") <+> integer p
316 MachLabel l | code_style -> ptext SLIT("(&") <> ptext l <> char ')'
317 | otherwise -> ptext SLIT("__label") <+> pprHsString l
319 MachLitLit s ty | code_style -> ptext s
320 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
324 pprIntVal :: Integer -> SDoc
325 -- Print negative integers with parens to be sure it's unambiguous
326 pprIntVal i | i < 0 = parens (integer i)
327 | otherwise = integer i
329 pprHexVal :: Integer -> SDoc
330 -- Print in C hex format: 0x13fa
331 pprHexVal 0 = ptext SLIT("0x0")
332 pprHexVal w = ptext SLIT("0x") <> go w
335 go w = go quot <> dig
337 (quot,rem) = w `quotRem` 16
338 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
339 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
343 %************************************************************************
347 %************************************************************************
349 Hash values should be zero or a positive integer. No negatives please.
350 (They mess up the UniqFM for some reason.)
353 hashLiteral :: Literal -> Int
354 hashLiteral (MachChar c) = c + 1000 -- Keep it out of range of common ints
355 hashLiteral (MachStr s) = hashFS s
356 hashLiteral (MachAddr i) = hashInteger i
357 hashLiteral (MachInt i) = hashInteger i
358 hashLiteral (MachInt64 i) = hashInteger i
359 hashLiteral (MachWord i) = hashInteger i
360 hashLiteral (MachWord64 i) = hashInteger i
361 hashLiteral (MachFloat r) = hashRational r
362 hashLiteral (MachDouble r) = hashRational r
363 hashLiteral (MachLabel s) = hashFS s
364 hashLiteral (MachLitLit s _) = hashFS s
366 hashRational :: Rational -> Int
367 hashRational r = hashInteger (numerator r)
369 hashInteger :: Integer -> Int
370 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
371 -- The 1+ is to avoid zero, which is a Bad Number
372 -- since we use * to combine hash values
374 hashFS :: FAST_STRING -> Int
375 hashFS s = iBox (uniqueOfFS s)