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
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 ( charToC, charToEasyHaskell, pprFSInCStyle )
33 import Util ( thenCmp )
35 import Ratio ( numerator, denominator )
36 import FastString ( uniqueOfFS )
37 import Char ( ord, chr )
42 %************************************************************************
46 %************************************************************************
48 If we're compiling with GHC (and we're not cross-compiling), then we
49 know that minBound and maxBound :: Int are the right values for the
50 target architecture. Otherwise, we assume -2^31 and 2^31-1
51 respectively (which will be wrong on a 64-bit machine).
54 tARGET_MIN_INT, tARGET_MAX_INT, tARGET_MAX_WORD :: Integer
55 #if __GLASGOW_HASKELL__
56 tARGET_MIN_INT = toInteger (minBound :: Int)
57 tARGET_MAX_INT = toInteger (maxBound :: Int)
59 tARGET_MIN_INT = -2147483648
60 tARGET_MAX_INT = 2147483647
62 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
66 %************************************************************************
70 %************************************************************************
72 So-called @Literals@ are {\em either}:
75 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
76 which is presumed to be surrounded by appropriate constructors
77 (@mKINT@, etc.), so that the overall thing makes sense.
79 An Integer, Rational, or String literal whose representation we are
80 {\em uncommitted} about; i.e., the surrounding with constructors,
81 function applications, etc., etc., has not yet been done.
87 -- First the primitive guys
91 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
93 | MachInt Integer -- Int# At least 32 bits
94 | MachInt64 Integer -- Int64# At least 64 bits
95 | MachWord Integer -- Word# At least 32 bits
96 | MachWord64 Integer -- Word64# At least 64 bits
101 -- string argument is the name of a symbol. This literal
102 -- refers to the *address* of the label.
103 | MachLabel FAST_STRING -- always an Addr#
105 -- lit-lits only work for via-C compilation, hence they
106 -- are deprecated. The string is emitted verbatim into
107 -- the C file, and can therefore be any C expression,
108 -- macro call, #defined constant etc.
109 | MachLitLit FAST_STRING Type -- Type might be Addr# or Int# etc
113 instance Outputable Literal where
116 instance Show Literal where
117 showsPrec p lit = showsPrecSDoc p (ppr lit)
119 instance Eq Literal where
120 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
121 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
123 instance Ord Literal where
124 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
125 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
126 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
127 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
128 compare a b = cmpLit a b
135 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
137 mkMachInt x = ASSERT2( inIntRange x, integer x ) MachInt x
138 mkMachWord x = ASSERT2( inWordRange x, integer x ) MachWord x
139 mkMachInt64 x = MachInt64 x -- Assertions?
140 mkMachWord64 x = MachWord64 x -- Ditto?
142 inIntRange, inWordRange :: Integer -> Bool
143 inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
144 inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
150 word2IntLit, int2WordLit, char2IntLit, int2CharLit,
151 float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
152 addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit :: Literal -> Literal
154 word2IntLit (MachWord w)
155 | w > tARGET_MAX_INT = MachInt ((-1) + tARGET_MAX_WORD - w)
156 | otherwise = MachInt w
158 int2WordLit (MachInt i)
159 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
160 | otherwise = MachWord i
162 char2IntLit (MachChar c) = MachInt (toInteger (ord c))
163 int2CharLit (MachInt i) = MachChar (chr (fromInteger i))
165 float2IntLit (MachFloat f) = MachInt (truncate f)
166 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
168 double2IntLit (MachFloat f) = MachInt (truncate f)
169 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
171 addr2IntLit (MachAddr a) = MachInt a
172 int2AddrLit (MachInt i) = MachAddr i
174 float2DoubleLit (MachFloat f) = MachDouble f
175 double2FloatLit (MachDouble d) = MachFloat d
181 isLitLitLit (MachLitLit _ _) = True
182 isLitLitLit _ = False
184 maybeLitLit (MachLitLit s t) = Just (s,t)
185 maybeLitLit _ = Nothing
191 literalType :: Literal -> Type
192 literalType (MachChar _) = charPrimTy
193 literalType (MachStr _) = addrPrimTy
194 literalType (MachAddr _) = addrPrimTy
195 literalType (MachInt _) = intPrimTy
196 literalType (MachWord _) = wordPrimTy
197 literalType (MachInt64 _) = int64PrimTy
198 literalType (MachWord64 _) = word64PrimTy
199 literalType (MachFloat _) = floatPrimTy
200 literalType (MachDouble _) = doublePrimTy
201 literalType (MachLabel _) = addrPrimTy
202 literalType (MachLitLit _ ty) = ty
206 literalPrimRep :: Literal -> PrimRep
208 literalPrimRep (MachChar _) = CharRep
209 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
210 literalPrimRep (MachAddr _) = AddrRep
211 literalPrimRep (MachInt _) = IntRep
212 literalPrimRep (MachWord _) = WordRep
213 literalPrimRep (MachInt64 _) = Int64Rep
214 literalPrimRep (MachWord64 _) = Word64Rep
215 literalPrimRep (MachFloat _) = FloatRep
216 literalPrimRep (MachDouble _) = DoubleRep
217 literalPrimRep (MachLabel _) = AddrRep
218 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
225 cmpLit (MachChar a) (MachChar b) = a `compare` b
226 cmpLit (MachStr a) (MachStr b) = a `compare` b
227 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
228 cmpLit (MachInt a) (MachInt b) = a `compare` b
229 cmpLit (MachWord a) (MachWord b) = a `compare` b
230 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
231 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
232 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
233 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
234 cmpLit (MachLabel a) (MachLabel b) = a `compare` b
235 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
236 cmpLit lit1 lit2 | litTag lit1 _LT_ litTag lit2 = LT
239 litTag (MachChar _) = ILIT(1)
240 litTag (MachStr _) = ILIT(2)
241 litTag (MachAddr _) = ILIT(3)
242 litTag (MachInt _) = ILIT(4)
243 litTag (MachWord _) = ILIT(5)
244 litTag (MachInt64 _) = ILIT(6)
245 litTag (MachWord64 _) = ILIT(7)
246 litTag (MachFloat _) = ILIT(8)
247 litTag (MachDouble _) = ILIT(9)
248 litTag (MachLabel _) = ILIT(10)
249 litTag (MachLitLit _ _) = ILIT(11)
254 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
255 exceptions: MachFloat and MachAddr get an initial keyword prefix
259 = getPprStyle $ \ sty ->
261 code_style = codeStyle sty
262 iface_style = ifaceStyle sty
265 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), char '\'',
266 text (charToC ch), char '\'']
267 | iface_style -> char '\'' <> text (charToEasyHaskell ch) <> char '\''
268 | otherwise -> text ['\'', ch, '\'']
270 MachStr s | code_style -> pprFSInCStyle s
271 | otherwise -> pprFSAsString s
273 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
274 -- Avoid a problem whereby gcc interprets
275 -- the constant minInt as unsigned.
276 | otherwise -> pprIntVal i
278 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
279 | otherwise -> ptext SLIT("__int64") <+> integer i
281 MachWord w | code_style -> pprHexVal w
282 | otherwise -> ptext SLIT("__word") <+> integer w
284 MachWord64 w | code_style -> pprHexVal w
285 | otherwise -> ptext SLIT("__word64") <+> integer w
287 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
288 | otherwise -> ptext SLIT("__float") <+> rational f
290 MachDouble d | iface_style && d < 0 -> parens (rational d)
291 | otherwise -> rational d
293 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
294 | otherwise -> ptext SLIT("__addr") <+> integer p
296 MachLabel l | code_style -> ptext SLIT("(&") <> ptext l <> char ')'
297 | otherwise -> ptext SLIT("__label") <+> pprFSAsString l
299 MachLitLit s ty | code_style -> ptext s
300 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
304 pprIntVal :: Integer -> SDoc
305 -- Print negative integers with parens to be sure it's unambiguous
306 pprIntVal i | i < 0 = parens (integer i)
307 | otherwise = integer i
309 pprHexVal :: Integer -> SDoc
310 -- Print in C hex format: 0x13fa
311 pprHexVal 0 = ptext SLIT("0x0")
312 pprHexVal w = ptext SLIT("0x") <> go w
315 go w = go quot <> dig
317 (quot,rem) = w `quotRem` 16
318 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
319 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
323 %************************************************************************
327 %************************************************************************
329 Hash values should be zero or a positive integer. No negatives please.
330 (They mess up the UniqFM for some reason.)
333 hashLiteral :: Literal -> Int
334 hashLiteral (MachChar c) = ord c + 1000 -- Keep it out of range of common ints
335 hashLiteral (MachStr s) = hashFS s
336 hashLiteral (MachAddr i) = hashInteger i
337 hashLiteral (MachInt i) = hashInteger i
338 hashLiteral (MachInt64 i) = hashInteger i
339 hashLiteral (MachWord i) = hashInteger i
340 hashLiteral (MachWord64 i) = hashInteger i
341 hashLiteral (MachFloat r) = hashRational r
342 hashLiteral (MachDouble r) = hashRational r
343 hashLiteral (MachLitLit s _) = hashFS s
345 hashRational :: Rational -> Int
346 hashRational r = hashInteger (numerator r)
348 hashInteger :: Integer -> Int
349 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
350 -- The 1+ is to avoid zero, which is a Bad Number
351 -- since we use * to combine hash values
353 hashFS :: FAST_STRING -> Int
354 hashFS s = IBOX( uniqueOfFS s )