2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
10 conOkForApp, conOkForAlt, isWHNFCon, isDataCon,
11 conIsTrivial, conIsCheap, conIsDupable, conStrictness,
12 conOkForSpeculation, hashCon,
14 DataCon, PrimOp, -- For completeness
17 Literal(..), -- Exported to ParseIface
18 mkMachInt, mkMachWord,
19 mkMachInt_safe, mkMachInt64, mkMachWord64,
20 mkStrLit, -- ToDo: rm (not used anywhere)
21 isNoRepLit, isLitLitLit,
22 literalType, literalPrimRep
25 #include "HsVersions.h"
27 import TysPrim ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
28 intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
30 import Name ( hashName )
31 import PrimOp ( PrimOp, primOpType, primOpIsDupable, primOpTag,
32 primOpIsCheap, primOpStrictness, primOpOkForSpeculation )
33 import PrimRep ( PrimRep(..) )
34 import DataCon ( DataCon, dataConName, dataConType, dataConTyCon, isNullaryDataCon, dataConRepStrictness )
35 import TyCon ( isNewTyCon )
36 import Type ( Type, typePrimRep )
37 import PprType ( pprParendType )
38 import Demand ( Demand )
39 import CStrings ( stringToC, charToC, charToEasyHaskell )
42 import Util ( thenCmp )
44 import Ratio ( numerator, denominator )
45 import FastString ( uniqueOfFS )
50 %************************************************************************
52 \subsection{The main data type}
54 %************************************************************************
61 | DEFAULT -- Used in case clauses
64 -- The Ord is needed for the FiniteMap used in the lookForConstructor
65 -- in SimplEnv. If you declared that lookForConstructor *ignores*
66 -- constructor-applications with LitArg args, then you could get
69 instance Outputable Con where
70 ppr (DataCon dc) = ppr dc
71 ppr (Literal lit) = ppr lit
72 ppr (PrimOp op) = ppr op
73 ppr DEFAULT = ptext SLIT("__DEFAULT")
75 instance Show Con where
76 showsPrec p con = showsPrecSDoc p (ppr con)
78 conType :: Con -> Type
79 conType (DataCon dc) = dataConType dc
80 conType (Literal lit) = literalType lit
81 conType (PrimOp op) = primOpType op
83 conStrictness :: Con -> ([Demand], Bool)
84 conStrictness (DataCon dc) = (dataConRepStrictness dc, False)
85 conStrictness (PrimOp op) = primOpStrictness op
86 conStrictness (Literal lit) = ([], False)
88 conPrimRep :: Con -> PrimRep -- Only data valued constants
89 conPrimRep (DataCon dc) = ASSERT( isNullaryDataCon dc) PtrRep
90 conPrimRep (Literal lit) = literalPrimRep lit
92 conOkForApp, conOkForAlt :: Con -> Bool
94 -- OK for appliation site
95 conOkForApp (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
96 conOkForApp (Literal _) = True
97 conOkForApp (PrimOp op) = True
98 conOkForApp DEFAULT = False
100 -- OK for case alternative pattern
101 conOkForAlt (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
102 conOkForAlt (Literal lit) = not (isNoRepLit lit)
103 conOkForAlt (PrimOp _) = False
104 conOkForAlt DEFAULT = True
106 -- isWHNFCon is false for PrimOps, which contain work
107 -- Ditto for newtype constructors, which can occur in the output
108 -- of the desugarer, but which will be inlined right away thereafter
109 isWHNFCon (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
110 isWHNFCon (Literal _) = True
111 isWHNFCon (PrimOp _) = False
113 isDataCon (DataCon dc) = True
114 isDataCon other = False
116 -- conIsTrivial is true for constants we are unconditionally happy to duplicate
117 -- cf CoreUtils.exprIsTrivial
118 conIsTrivial (Literal lit) = not (isNoRepLit lit)
119 conIsTrivial (PrimOp _) = False
120 conIsTrivial con = True
122 -- conIsCheap is true for constants whose applications we are willing
123 -- to duplicate in exchange for some modest gain. cf CoreUtils.exprIsCheap
124 conIsCheap (Literal lit) = not (isNoRepLit lit)
125 conIsCheap (DataCon con) = True
126 conIsCheap (PrimOp op) = primOpIsCheap op
128 -- conIsDupable is true for constants whose applications we are willing
129 -- to duplicate in different case branches; i.e no issue about loss of
131 conIsDupable (Literal lit) = not (isNoRepLit lit)
132 conIsDupable (DataCon con) = True
133 conIsDupable (PrimOp op) = primOpIsDupable op
135 -- Similarly conOkForSpeculation
136 conOkForSpeculation (Literal lit) = True
137 conOkForSpeculation (DataCon con) = True
138 conOkForSpeculation (PrimOp op) = primOpOkForSpeculation op
142 %************************************************************************
144 \subsection{Literals}
146 %************************************************************************
148 So-called @Literals@ are {\em either}:
151 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
152 which is presumed to be surrounded by appropriate constructors
153 (@mKINT@, etc.), so that the overall thing makes sense.
155 An Integer, Rational, or String literal whose representation we are
156 {\em uncommitted} about; i.e., the surrounding with constructors,
157 function applications, etc., etc., has not yet been done.
163 -- First the primitive guys
165 | MachStr FAST_STRING
167 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
169 | MachInt Integer -- For the numeric types, these are
170 Bool -- True <=> signed (Int#); False <=> unsigned (Word#)
172 | MachInt64 Integer -- guaranteed 64-bit versions of the above.
173 Bool -- True <=> signed (Int#); False <=> unsigned (Word#)
177 | MachDouble Rational
179 | MachLitLit FAST_STRING Type -- Type might be Add# or Int# etc
183 | NoRepStr FAST_STRING Type -- This Type is always String
184 | NoRepInteger Integer Type -- This Type is always Integer
185 | NoRepRational Rational Type -- This Type is always Rational
186 -- We keep these Types in the literal because Rational isn't
187 -- (currently) wired in, so we can't conjure up its type out of
188 -- thin air. Integer is, so the type here is really redundant.
192 instance Outputable Literal where
195 instance Show Literal where
196 showsPrec p lit = showsPrecSDoc p (ppr lit)
198 instance Eq Literal where
199 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
200 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
202 instance Ord Literal where
203 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
204 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
205 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
206 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
207 compare a b = cmpLit a b
214 mkMachInt, mkMachWord :: Integer -> Literal
216 mkMachInt x = MachInt x True{-signed-}
217 mkMachWord x = MachInt x False{-unsigned-}
219 -- check if the int is within range
220 mkMachInt_safe :: Integer -> Literal
223 pprPanic "mkMachInt_safe"
224 (hsep [text "ERROR: Int ", text (show i), text "out of range",
225 brackets (int minInt <+> text ".." <+> int maxInt)])
226 | otherwise = MachInt i True{-signed-}
229 -- i < fromInt minBound ||
232 mkMachInt64 x = MachInt64 x True{-signed-}
233 mkMachWord64 x = MachInt64 x False{-unsigned-}
235 mkStrLit :: String -> Type -> Literal
236 mkStrLit s ty = NoRepStr (_PK_ s) ty
243 isNoRepLit (NoRepStr _ _) = True -- these are not primitive typed!
244 isNoRepLit (NoRepInteger _ _) = True
245 isNoRepLit (NoRepRational _ _) = True
248 isLitLitLit (MachLitLit _ _) = True
249 isLitLitLit _ = False
255 literalType :: Literal -> Type
256 literalType (MachChar _) = charPrimTy
257 literalType (MachStr _) = addrPrimTy
258 literalType (MachAddr _) = addrPrimTy
259 literalType (MachInt _ signed) = if signed then intPrimTy else wordPrimTy
260 literalType (MachInt64 _ signed) = if signed then int64PrimTy else word64PrimTy
261 literalType (MachFloat _) = floatPrimTy
262 literalType (MachDouble _) = doublePrimTy
263 literalType (MachLitLit _ ty) = ty
264 literalType (NoRepInteger _ ty) = ty
265 literalType (NoRepRational _ ty) = ty
266 literalType (NoRepStr _ ty) = ty
270 literalPrimRep :: Literal -> PrimRep
272 literalPrimRep (MachChar _) = CharRep
273 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
274 literalPrimRep (MachAddr _) = AddrRep
275 literalPrimRep (MachInt _ signed) = if signed then IntRep else WordRep
276 literalPrimRep (MachInt64 _ signed) = if signed then Int64Rep else Word64Rep
277 literalPrimRep (MachFloat _) = FloatRep
278 literalPrimRep (MachDouble _) = DoubleRep
279 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
281 literalPrimRep (NoRepInteger _ _) = panic "literalPrimRep:NoRepInteger"
282 literalPrimRep (NoRepRational _ _) = panic "literalPrimRep:NoRepRational"
283 literalPrimRep (NoRepStr _ _) = panic "literalPrimRep:NoRepString"
291 cmpLit (MachChar a) (MachChar b) = a `compare` b
292 cmpLit (MachStr a) (MachStr b) = a `compare` b
293 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
294 cmpLit (MachInt a b) (MachInt c d) = (a `compare` c) `thenCmp` (b `compare` d)
295 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
296 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
297 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
298 cmpLit (NoRepStr a _) (NoRepStr b _) = a `compare` b
299 cmpLit (NoRepInteger a _) (NoRepInteger b _) = a `compare` b
300 cmpLit (NoRepRational a _) (NoRepRational b _) = a `compare` b
301 cmpLit lit1 lit2 | litTag lit1 _LT_ litTag lit2 = LT
304 litTag (MachChar _) = ILIT(1)
305 litTag (MachStr _) = ILIT(2)
306 litTag (MachAddr _) = ILIT(3)
307 litTag (MachInt _ _) = ILIT(4)
308 litTag (MachFloat _) = ILIT(5)
309 litTag (MachDouble _) = ILIT(6)
310 litTag (MachLitLit _ _) = ILIT(7)
311 litTag (NoRepStr _ _) = ILIT(8)
312 litTag (NoRepInteger _ _) = ILIT(9)
313 litTag (NoRepRational _ _) = ILIT(10)
318 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
319 exceptions: MachFloat and MachAddr get an initial keyword prefix
321 * NoRep things get an initial keyword prefix (e.g. _integer_ 3)
325 = getPprStyle $ \ sty ->
327 code_style = codeStyle sty
330 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), char '\'',
331 text (charToC ch), char '\'']
332 | ifaceStyle sty -> char '\'' <> text (charToEasyHaskell ch) <> char '\''
333 | otherwise -> text ['\'', ch, '\'']
335 MachStr s | code_style -> doubleQuotes (text (stringToC (_UNPK_ s)))
336 | otherwise -> pprFSAsString s
339 NoRepStr s ty | code_style -> pprPanic "NoRep in code style" (ppr lit)
340 | otherwise -> ptext SLIT("__string") <+> pprFSAsString s
342 MachInt i signed | code_style && out_of_range
343 -> pprPanic "" (hsep [text "ERROR: Int ", text (show i),
345 brackets (ppr range_min <+> text ".."
347 -- in interface files, parenthesize raw negative ints.
348 -- this avoids problems like {-1} being interpreted
349 -- as a comment starter.
350 | ifaceStyle sty && i < 0 -> parens (integer i)
351 | otherwise -> integer i
354 range_min = if signed then minInt else 0
356 out_of_range = not (i >= toInteger range_min && i <= toInteger range_max)
358 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
359 | otherwise -> ptext SLIT("__float") <+> rational f
361 MachDouble d | ifaceStyle sty && d < 0 -> parens (rational d)
362 | otherwise -> rational d
364 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
365 | otherwise -> ptext SLIT("__addr") <+> integer p
367 NoRepInteger i _ | code_style -> pprPanic "NoRep in code style" (ppr lit)
368 | otherwise -> ptext SLIT("__integer") <+> integer i
370 NoRepRational r _ | code_style -> pprPanic "NoRep in code style" (ppr lit)
371 | otherwise -> hsep [ptext SLIT("__rational"), integer (numerator r),
372 integer (denominator r)]
374 MachLitLit s ty | code_style -> ptext s
375 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
381 %************************************************************************
385 %************************************************************************
387 Hash values should be zero or a positive integer. No negatives please.
388 (They mess up the UniqFM for some reason.)
391 hashCon :: Con -> Int
392 hashCon (DataCon dc) = hashName (dataConName dc)
393 hashCon (PrimOp op) = primOpTag op + 500 -- Keep it out of range of common ints
394 hashCon (Literal lit) = hashLiteral lit
395 hashCon other = pprTrace "hashCon" (ppr other) 0
397 hashLiteral :: Literal -> Int
398 hashLiteral (MachChar c) = ord c + 1000 -- Keep it out of range of common ints
399 hashLiteral (MachStr s) = hashFS s
400 hashLiteral (MachAddr i) = hashInteger i
401 hashLiteral (MachInt i _) = hashInteger i
402 hashLiteral (MachInt64 i _) = hashInteger i
403 hashLiteral (MachFloat r) = hashRational r
404 hashLiteral (MachDouble r) = hashRational r
405 hashLiteral (MachLitLit s _) = hashFS s
406 hashLiteral (NoRepStr s _) = hashFS s
407 hashLiteral (NoRepInteger i _) = hashInteger i
408 hashLiteral (NoRepRational r _) = hashRational r
410 hashRational :: Rational -> Int
411 hashRational r = hashInteger (numerator r)
413 hashInteger :: Integer -> Int
414 hashInteger i = abs (fromInteger (i `rem` 10000))
416 hashFS :: FAST_STRING -> Int
417 hashFS s = IBOX( uniqueOfFS s )