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
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 Name ( hashName )
28 import PrimRep ( PrimRep(..) )
29 import TyCon ( isNewTyCon )
30 import Type ( Type, typePrimRep )
31 import PprType ( pprParendType )
32 import Demand ( Demand )
33 import CStrings ( charToC, charToEasyHaskell, pprFSInCStyle )
36 import Util ( thenCmp )
38 import Ratio ( numerator, denominator )
39 import FastString ( uniqueOfFS )
40 import Char ( ord, chr )
45 %************************************************************************
49 %************************************************************************
51 If we're compiling with GHC (and we're not cross-compiling), then we
52 know that minBound and maxBound :: Int are the right values for the
53 target architecture. Otherwise, we assume -2^31 and 2^31-1
54 respectively (which will be wrong on a 64-bit machine).
57 tARGET_MIN_INT, tARGET_MAX_INT, tARGET_MAX_WORD :: Integer
58 #if __GLASGOW_HASKELL__
59 tARGET_MIN_INT = toInteger (minBound :: Int)
60 tARGET_MAX_INT = toInteger (maxBound :: Int)
62 tARGET_MIN_INT = -2147483648
63 tARGET_MAX_INT = 2147483647
65 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
69 %************************************************************************
73 %************************************************************************
75 So-called @Literals@ are {\em either}:
78 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
79 which is presumed to be surrounded by appropriate constructors
80 (@mKINT@, etc.), so that the overall thing makes sense.
82 An Integer, Rational, or String literal whose representation we are
83 {\em uncommitted} about; i.e., the surrounding with constructors,
84 function applications, etc., etc., has not yet been done.
90 -- First the primitive guys
94 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
96 | MachInt Integer -- Int# At least 32 bits
97 | MachInt64 Integer -- Int64# At least 64 bits
98 | MachWord Integer -- Word# At least 32 bits
99 | MachWord64 Integer -- Word64# At least 64 bits
102 | MachDouble Rational
104 | MachLitLit FAST_STRING Type -- Type might be Add# or Int# etc
108 instance Outputable Literal where
111 instance Show Literal where
112 showsPrec p lit = showsPrecSDoc p (ppr lit)
114 instance Eq Literal where
115 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
116 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
118 instance Ord Literal where
119 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
120 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
121 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
122 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
123 compare a b = cmpLit a b
130 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
132 mkMachInt x = ASSERT2( inIntRange x, integer x ) MachInt x
133 mkMachWord x = ASSERT2( inWordRange x, integer x ) MachWord x
134 mkMachInt64 x = MachInt64 x -- Assertions?
135 mkMachWord64 x = MachWord64 x -- Ditto?
137 inIntRange, inWordRange :: Integer -> Bool
138 inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
139 inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
145 word2IntLit, int2WordLit, char2IntLit, int2CharLit,
146 float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
147 addr2IntLit, int2AddrLit, float2DoubleLit, double2FloatLit :: Literal -> Literal
149 word2IntLit (MachWord w)
150 | w > tARGET_MAX_INT = MachInt ((-1) + tARGET_MAX_WORD - w)
151 | otherwise = MachInt w
153 int2WordLit (MachInt i)
154 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
155 | otherwise = MachWord i
157 char2IntLit (MachChar c) = MachInt (toInteger (ord c))
158 int2CharLit (MachInt i) = MachChar (chr (fromInteger i))
160 float2IntLit (MachFloat f) = MachInt (truncate f)
161 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
163 double2IntLit (MachFloat f) = MachInt (truncate f)
164 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
166 addr2IntLit (MachAddr a) = MachInt a
167 int2AddrLit (MachInt i) = MachAddr i
169 float2DoubleLit (MachFloat f) = MachDouble f
170 double2FloatLit (MachDouble d) = MachFloat d
176 isLitLitLit (MachLitLit _ _) = True
177 isLitLitLit _ = False
179 maybeLitLit (MachLitLit s t) = Just (s,t)
180 maybeLitLit _ = Nothing
186 literalType :: Literal -> Type
187 literalType (MachChar _) = charPrimTy
188 literalType (MachStr _) = addrPrimTy
189 literalType (MachAddr _) = addrPrimTy
190 literalType (MachInt _) = intPrimTy
191 literalType (MachWord _) = wordPrimTy
192 literalType (MachInt64 _) = int64PrimTy
193 literalType (MachWord64 _) = word64PrimTy
194 literalType (MachFloat _) = floatPrimTy
195 literalType (MachDouble _) = doublePrimTy
196 literalType (MachLitLit _ ty) = ty
200 literalPrimRep :: Literal -> PrimRep
202 literalPrimRep (MachChar _) = CharRep
203 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
204 literalPrimRep (MachAddr _) = AddrRep
205 literalPrimRep (MachInt _) = IntRep
206 literalPrimRep (MachWord _) = WordRep
207 literalPrimRep (MachInt64 _) = Int64Rep
208 literalPrimRep (MachWord64 _) = Word64Rep
209 literalPrimRep (MachFloat _) = FloatRep
210 literalPrimRep (MachDouble _) = DoubleRep
211 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
218 cmpLit (MachChar a) (MachChar b) = a `compare` b
219 cmpLit (MachStr a) (MachStr b) = a `compare` b
220 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
221 cmpLit (MachInt a) (MachInt b) = a `compare` b
222 cmpLit (MachWord a) (MachWord b) = a `compare` b
223 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
224 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
225 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
226 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
227 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
228 cmpLit lit1 lit2 | litTag lit1 _LT_ litTag lit2 = LT
231 litTag (MachChar _) = ILIT(1)
232 litTag (MachStr _) = ILIT(2)
233 litTag (MachAddr _) = ILIT(3)
234 litTag (MachInt _) = ILIT(4)
235 litTag (MachWord _) = ILIT(5)
236 litTag (MachInt64 _) = ILIT(6)
237 litTag (MachWord64 _) = ILIT(7)
238 litTag (MachFloat _) = ILIT(8)
239 litTag (MachDouble _) = ILIT(9)
240 litTag (MachLitLit _ _) = ILIT(10)
245 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
246 exceptions: MachFloat and MachAddr get an initial keyword prefix
250 = getPprStyle $ \ sty ->
252 code_style = codeStyle sty
253 iface_style = ifaceStyle sty
256 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), char '\'',
257 text (charToC ch), char '\'']
258 | iface_style -> char '\'' <> text (charToEasyHaskell ch) <> char '\''
259 | otherwise -> text ['\'', ch, '\'']
261 MachStr s | code_style -> pprFSInCStyle s
262 | otherwise -> pprFSAsString s
264 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
265 -- Avoid a problem whereby gcc interprets
266 -- the constant minInt as unsigned.
267 | otherwise -> pprIntVal i
269 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
270 | otherwise -> ptext SLIT("__int64") <+> integer i
272 MachWord w | code_style -> pprHexVal w
273 | otherwise -> ptext SLIT("__word") <+> integer w
275 MachWord64 w | code_style -> pprHexVal w
276 | otherwise -> ptext SLIT("__word64") <+> integer w
278 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
279 | otherwise -> ptext SLIT("__float") <+> rational f
281 MachDouble d | iface_style && d < 0 -> parens (rational d)
282 | otherwise -> rational d
284 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
285 | otherwise -> ptext SLIT("__addr") <+> integer p
287 MachLitLit s ty | code_style -> ptext s
288 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
292 pprIntVal :: Integer -> SDoc
293 -- Print negative integers with parens to be sure it's unambiguous
294 pprIntVal i | i < 0 = parens (integer i)
295 | otherwise = integer i
297 pprHexVal :: Integer -> SDoc
298 -- Print in C hex format: 0x13fa
299 pprHexVal 0 = ptext SLIT("0x0")
300 pprHexVal w = ptext SLIT("0x") <> go w
303 go w = go quot <> dig
305 (quot,rem) = w `quotRem` 16
306 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
307 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
311 %************************************************************************
315 %************************************************************************
317 Hash values should be zero or a positive integer. No negatives please.
318 (They mess up the UniqFM for some reason.)
321 hashLiteral :: Literal -> Int
322 hashLiteral (MachChar c) = ord c + 1000 -- Keep it out of range of common ints
323 hashLiteral (MachStr s) = hashFS s
324 hashLiteral (MachAddr i) = hashInteger i
325 hashLiteral (MachInt i) = hashInteger i
326 hashLiteral (MachInt64 i) = hashInteger i
327 hashLiteral (MachWord i) = hashInteger i
328 hashLiteral (MachWord64 i) = hashInteger i
329 hashLiteral (MachFloat r) = hashRational r
330 hashLiteral (MachDouble r) = hashRational r
331 hashLiteral (MachLitLit s _) = hashFS s
333 hashRational :: Rational -> Int
334 hashRational r = hashInteger (numerator r)
336 hashInteger :: Integer -> Int
337 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
338 -- The 1+ is to avoid zero, which is a Bad Number
339 -- since we use * to combine hash values
341 hashFS :: FAST_STRING -> Int
342 hashFS s = IBOX( uniqueOfFS s )