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 -- MachLabel is used (only) for the literal derived from a
103 -- "foreign label" declaration.
104 -- string argument is the name of a symbol. This literal
105 -- refers to the *address* of the label.
106 | MachLabel FAST_STRING -- always an Addr#
108 -- lit-lits only work for via-C compilation, hence they
109 -- are deprecated. The string is emitted verbatim into
110 -- the C file, and can therefore be any C expression,
111 -- macro call, #defined constant etc.
112 | MachLitLit FAST_STRING Type -- Type might be Addr# or Int# etc
116 instance Outputable Literal where
119 instance Show Literal where
120 showsPrec p lit = showsPrecSDoc p (ppr lit)
122 instance Eq Literal where
123 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
124 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
126 instance Ord Literal where
127 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
128 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
129 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
130 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
131 compare a b = cmpLit a b
138 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
140 mkMachInt x = ASSERT2( inIntRange x, integer x ) MachInt x
141 mkMachWord x = ASSERT2( inWordRange x, integer x ) MachWord x
142 mkMachInt64 x = MachInt64 x -- Assertions?
143 mkMachWord64 x = MachWord64 x -- Ditto?
145 inIntRange, inWordRange :: Integer -> Bool
146 inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
147 inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
153 word2IntLit, int2WordLit, char2IntLit, int2CharLit,
154 float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
155 addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit :: Literal -> Literal
157 word2IntLit (MachWord w)
158 | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
159 | otherwise = MachInt w
161 int2WordLit (MachInt i)
162 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
163 | otherwise = MachWord i
165 char2IntLit (MachChar c) = MachInt (toInteger c)
166 int2CharLit (MachInt i) = MachChar (fromInteger i)
168 float2IntLit (MachFloat f) = MachInt (truncate f)
169 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
171 double2IntLit (MachFloat f) = MachInt (truncate f)
172 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
174 addr2IntLit (MachAddr a) = MachInt a
175 int2AddrLit (MachInt i) = MachAddr i
177 float2DoubleLit (MachFloat f) = MachDouble f
178 double2FloatLit (MachDouble d) = MachFloat d
184 isLitLitLit (MachLitLit _ _) = True
185 isLitLitLit _ = False
187 maybeLitLit (MachLitLit s t) = Just (s,t)
188 maybeLitLit _ = Nothing
190 litIsDupable :: Literal -> Bool
191 -- True if code space does not go bad if we duplicate this literal
192 -- False principally of strings
193 litIsDupable (MachStr _) = False
194 litIsDupable other = True
196 litSize :: Literal -> Int
197 -- used by CoreUnfold.sizeExpr
198 litSize (MachStr str) = lengthFS str `div` 4
205 literalType :: Literal -> Type
206 literalType (MachChar _) = charPrimTy
207 literalType (MachStr _) = addrPrimTy
208 literalType (MachAddr _) = addrPrimTy
209 literalType (MachInt _) = intPrimTy
210 literalType (MachWord _) = wordPrimTy
211 literalType (MachInt64 _) = int64PrimTy
212 literalType (MachWord64 _) = word64PrimTy
213 literalType (MachFloat _) = floatPrimTy
214 literalType (MachDouble _) = doublePrimTy
215 literalType (MachLabel _) = addrPrimTy
216 literalType (MachLitLit _ ty) = ty
220 literalPrimRep :: Literal -> PrimRep
222 literalPrimRep (MachChar _) = CharRep
223 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
224 literalPrimRep (MachAddr _) = AddrRep
225 literalPrimRep (MachInt _) = IntRep
226 literalPrimRep (MachWord _) = WordRep
227 literalPrimRep (MachInt64 _) = Int64Rep
228 literalPrimRep (MachWord64 _) = Word64Rep
229 literalPrimRep (MachFloat _) = FloatRep
230 literalPrimRep (MachDouble _) = DoubleRep
231 literalPrimRep (MachLabel _) = AddrRep
232 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
239 cmpLit (MachChar a) (MachChar b) = a `compare` b
240 cmpLit (MachStr a) (MachStr b) = a `compare` b
241 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
242 cmpLit (MachInt a) (MachInt b) = a `compare` b
243 cmpLit (MachWord a) (MachWord b) = a `compare` b
244 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
245 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
246 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
247 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
248 cmpLit (MachLabel a) (MachLabel b) = a `compare` b
249 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
250 cmpLit lit1 lit2 | litTag lit1 <# litTag lit2 = LT
253 litTag (MachChar _) = _ILIT(1)
254 litTag (MachStr _) = _ILIT(2)
255 litTag (MachAddr _) = _ILIT(3)
256 litTag (MachInt _) = _ILIT(4)
257 litTag (MachWord _) = _ILIT(5)
258 litTag (MachInt64 _) = _ILIT(6)
259 litTag (MachWord64 _) = _ILIT(7)
260 litTag (MachFloat _) = _ILIT(8)
261 litTag (MachDouble _) = _ILIT(9)
262 litTag (MachLabel _) = _ILIT(10)
263 litTag (MachLitLit _ _) = _ILIT(11)
268 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
269 exceptions: MachFloat and MachAddr get an initial keyword prefix
273 = getPprStyle $ \ sty ->
275 code_style = codeStyle sty
276 iface_style = ifaceStyle sty
279 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show ch)]
280 | otherwise -> pprHsChar ch
282 MachStr s | code_style -> pprFSInCStyle s
283 | otherwise -> pprHsString s
284 -- Warning: printing MachStr in code_style assumes it contains
285 -- only characters '\0'..'\xFF'!
287 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
288 -- Avoid a problem whereby gcc interprets
289 -- the constant minInt as unsigned.
290 | otherwise -> pprIntVal i
292 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
293 | otherwise -> ptext SLIT("__int64") <+> integer i
295 MachWord w | code_style -> pprHexVal w
296 | otherwise -> ptext SLIT("__word") <+> integer w
298 MachWord64 w | code_style -> pprHexVal w
299 | otherwise -> ptext SLIT("__word64") <+> integer w
301 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
302 | otherwise -> ptext SLIT("__float") <+> rational f
304 MachDouble d | iface_style && d < 0 -> parens (rational d)
305 | otherwise -> rational d
307 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
308 | otherwise -> ptext SLIT("__addr") <+> integer p
310 MachLabel l | code_style -> ptext SLIT("(&") <> ptext l <> char ')'
311 | otherwise -> ptext SLIT("__label") <+> pprHsString l
313 MachLitLit s ty | code_style -> ptext s
314 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
318 pprIntVal :: Integer -> SDoc
319 -- Print negative integers with parens to be sure it's unambiguous
320 pprIntVal i | i < 0 = parens (integer i)
321 | otherwise = integer i
323 pprHexVal :: Integer -> SDoc
324 -- Print in C hex format: 0x13fa
325 pprHexVal 0 = ptext SLIT("0x0")
326 pprHexVal w = ptext SLIT("0x") <> go w
329 go w = go quot <> dig
331 (quot,rem) = w `quotRem` 16
332 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
333 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
337 %************************************************************************
341 %************************************************************************
343 Hash values should be zero or a positive integer. No negatives please.
344 (They mess up the UniqFM for some reason.)
347 hashLiteral :: Literal -> Int
348 hashLiteral (MachChar c) = c + 1000 -- Keep it out of range of common ints
349 hashLiteral (MachStr s) = hashFS s
350 hashLiteral (MachAddr i) = hashInteger i
351 hashLiteral (MachInt i) = hashInteger i
352 hashLiteral (MachInt64 i) = hashInteger i
353 hashLiteral (MachWord i) = hashInteger i
354 hashLiteral (MachWord64 i) = hashInteger i
355 hashLiteral (MachFloat r) = hashRational r
356 hashLiteral (MachDouble r) = hashRational r
357 hashLiteral (MachLabel s) = hashFS s
358 hashLiteral (MachLitLit s _) = hashFS s
360 hashRational :: Rational -> Int
361 hashRational r = hashInteger (numerator r)
363 hashInteger :: Integer -> Int
364 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
365 -- The 1+ is to avoid zero, which is a Bad Number
366 -- since we use * to combine hash values
368 hashFS :: FAST_STRING -> Int
369 hashFS s = iBox (uniqueOfFS s)