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,
13 DataCon, PrimOp, -- For completeness
16 Literal(..), -- Exported to ParseIface
17 mkMachInt, mkMachWord,
18 mkMachInt_safe, mkMachInt64, mkMachWord64,
19 mkStrLit, -- ToDo: rm (not used anywhere)
20 isNoRepLit, isLitLitLit,
21 literalType, literalPrimRep
24 #include "HsVersions.h"
26 import TysPrim ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
27 intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
29 import PrimOp ( PrimOp, primOpType, primOpIsCheap )
30 import PrimRep ( PrimRep(..) )
31 import DataCon ( DataCon, dataConType, dataConTyCon, isNullaryDataCon )
32 import TyCon ( isNewTyCon )
33 import Type ( Type, typePrimRep )
34 import PprType ( pprParendType )
35 import CStrings ( stringToC, charToC, charToEasyHaskell )
38 import Util ( thenCmp )
42 %************************************************************************
44 \subsection{The main data type}
46 %************************************************************************
53 | DEFAULT -- Used in case clauses
56 -- The Ord is needed for the FiniteMap used in the lookForConstructor
57 -- in SimplEnv. If you declared that lookForConstructor *ignores*
58 -- constructor-applications with LitArg args, then you could get
61 instance Outputable Con where
62 ppr (DataCon dc) = ppr dc
63 ppr (Literal lit) = ppr lit
64 ppr (PrimOp op) = ppr op
65 ppr DEFAULT = ptext SLIT("__DEFAULT")
67 instance Show Con where
68 showsPrec p con = showsPrecSDoc p (ppr con)
70 conType :: Con -> Type
71 conType (DataCon dc) = dataConType dc
72 conType (Literal lit) = literalType lit
73 conType (PrimOp op) = primOpType op
75 conPrimRep :: Con -> PrimRep -- Only data valued constants
76 conPrimRep (DataCon dc) = ASSERT( isNullaryDataCon dc) PtrRep
77 conPrimRep (Literal lit) = literalPrimRep lit
79 conOkForApp, conOkForAlt :: Con -> Bool
81 -- OK for appliation site
82 conOkForApp (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
83 conOkForApp (Literal _) = True
84 conOkForApp (PrimOp op) = True
85 conOkForApp DEFAULT = False
87 -- OK for case alternative pattern
88 conOkForAlt (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
89 conOkForAlt (Literal lit) = not (isNoRepLit lit)
90 conOkForAlt (PrimOp _) = False
91 conOkForAlt DEFAULT = True
93 -- isWHNFCon is false for PrimOps, which contain work
94 -- Ditto for newtype constructors, which can occur in the output
95 -- of the desugarer, but which will be inlined right away thereafter
96 isWHNFCon (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
97 isWHNFCon (Literal _) = True
98 isWHNFCon (PrimOp _) = False
100 isDataCon (DataCon dc) = True
101 isDataCon other = False
103 -- conIsTrivial is true for constants we are unconditionally happy to duplicate
104 -- cf CoreUtils.exprIsTrivial
105 conIsTrivial (Literal lit) = not (isNoRepLit lit)
106 conIsTrivial (PrimOp _) = False
107 conIsTrivial con = True
109 -- conIsCheap is true for constants whose applications we are willing
110 -- to duplicate in exchange for some modest gain. cf CoreUtils.exprIsCheap
111 conIsCheap (Literal lit) = not (isNoRepLit lit)
112 conIsCheap (DataCon con) = True
113 conIsCheap (PrimOp op) = primOpIsCheap op
117 %************************************************************************
119 \subsection{Literals}
121 %************************************************************************
123 So-called @Literals@ are {\em either}:
126 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
127 which is presumed to be surrounded by appropriate constructors
128 (@mKINT@, etc.), so that the overall thing makes sense.
130 An Integer, Rational, or String literal whose representation we are
131 {\em uncommitted} about; i.e., the surrounding with constructors,
132 function applications, etc., etc., has not yet been done.
138 -- First the primitive guys
140 | MachStr FAST_STRING
142 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
144 | MachInt Integer -- For the numeric types, these are
145 Bool -- True <=> signed (Int#); False <=> unsigned (Word#)
147 | MachInt64 Integer -- guaranteed 64-bit versions of the above.
148 Bool -- True <=> signed (Int#); False <=> unsigned (Word#)
152 | MachDouble Rational
154 | MachLitLit FAST_STRING Type -- Type might be Add# or Int# etc
158 | NoRepStr FAST_STRING Type -- This Type is always String
159 | NoRepInteger Integer Type -- This Type is always Integer
160 | NoRepRational Rational Type -- This Type is always Rational
161 -- We keep these Types in the literal because Rational isn't
162 -- (currently) wired in, so we can't conjure up its type out of
163 -- thin air. Integer is, so the type here is really redundant.
168 instance Outputable Literal where
171 instance Show Literal where
172 showsPrec p lit = showsPrecSDoc p (ppr lit)
174 instance Eq Literal where
175 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
176 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
178 instance Ord Literal where
179 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
180 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
181 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
182 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
183 compare a b = cmpLit a b
190 mkMachInt, mkMachWord :: Integer -> Literal
192 mkMachInt x = MachInt x True{-signed-}
193 mkMachWord x = MachInt x False{-unsigned-}
195 -- check if the int is within range
196 mkMachInt_safe :: Integer -> Literal
199 pprPanic "mkMachInt_safe"
200 (hsep [text "ERROR: Int ", text (show i), text "out of range",
201 brackets (int minInt <+> text ".." <+> int maxInt)])
202 | otherwise = MachInt i True{-signed-}
205 -- i < fromInt minBound ||
208 mkMachInt64 x = MachInt64 x True{-signed-}
209 mkMachWord64 x = MachInt64 x False{-unsigned-}
211 mkStrLit :: String -> Type -> Literal
212 mkStrLit s ty = NoRepStr (_PK_ s) ty
219 isNoRepLit (NoRepStr _ _) = True -- these are not primitive typed!
220 isNoRepLit (NoRepInteger _ _) = True
221 isNoRepLit (NoRepRational _ _) = True
224 isLitLitLit (MachLitLit _ _) = True
225 isLitLitLit _ = False
231 literalType :: Literal -> Type
232 literalType (MachChar _) = charPrimTy
233 literalType (MachStr _) = addrPrimTy
234 literalType (MachAddr _) = addrPrimTy
235 literalType (MachInt _ signed) = if signed then intPrimTy else wordPrimTy
236 literalType (MachInt64 _ signed) = if signed then int64PrimTy else word64PrimTy
237 literalType (MachFloat _) = floatPrimTy
238 literalType (MachDouble _) = doublePrimTy
239 literalType (MachLitLit _ ty) = ty
240 literalType (NoRepInteger _ ty) = ty
241 literalType (NoRepRational _ ty) = ty
242 literalType (NoRepStr _ ty) = ty
246 literalPrimRep :: Literal -> PrimRep
248 literalPrimRep (MachChar _) = CharRep
249 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
250 literalPrimRep (MachAddr _) = AddrRep
251 literalPrimRep (MachInt _ signed) = if signed then IntRep else WordRep
252 literalPrimRep (MachInt64 _ signed) = if signed then Int64Rep else Word64Rep
253 literalPrimRep (MachFloat _) = FloatRep
254 literalPrimRep (MachDouble _) = DoubleRep
255 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
257 literalPrimRep (NoRepInteger _ _) = panic "literalPrimRep:NoRepInteger"
258 literalPrimRep (NoRepRational _ _) = panic "literalPrimRep:NoRepRational"
259 literalPrimRep (NoRepStr _ _) = panic "literalPrimRep:NoRepString"
267 cmpLit (MachChar a) (MachChar b) = a `compare` b
268 cmpLit (MachStr a) (MachStr b) = a `compare` b
269 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
270 cmpLit (MachInt a b) (MachInt c d) = (a `compare` c) `thenCmp` (b `compare` d)
271 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
272 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
273 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
274 cmpLit (NoRepStr a _) (NoRepStr b _) = a `compare` b
275 cmpLit (NoRepInteger a _) (NoRepInteger b _) = a `compare` b
276 cmpLit (NoRepRational a _) (NoRepRational b _) = a `compare` b
277 cmpLit lit1 lit2 | litTag lit1 _LT_ litTag lit2 = LT
280 litTag (MachChar _) = ILIT(1)
281 litTag (MachStr _) = ILIT(2)
282 litTag (MachAddr _) = ILIT(3)
283 litTag (MachInt _ _) = ILIT(4)
284 litTag (MachFloat _) = ILIT(5)
285 litTag (MachDouble _) = ILIT(6)
286 litTag (MachLitLit _ _) = ILIT(7)
287 litTag (NoRepStr _ _) = ILIT(8)
288 litTag (NoRepInteger _ _) = ILIT(9)
289 litTag (NoRepRational _ _) = ILIT(10)
294 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
295 exceptions: MachFloat and MachAddr get an initial keyword prefix
297 * NoRep things get an initial keyword prefix (e.g. _integer_ 3)
301 = getPprStyle $ \ sty ->
303 code_style = codeStyle sty
306 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), char '\'',
307 text (charToC ch), char '\'']
308 | ifaceStyle sty -> char '\'' <> text (charToEasyHaskell ch) <> char '\''
309 | otherwise -> text ['\'', ch, '\'']
311 MachStr s | code_style -> doubleQuotes (text (stringToC (_UNPK_ s)))
312 | otherwise -> pprFSAsString s
315 NoRepStr s ty | code_style -> pprPanic "NoRep in code style" (ppr lit)
316 | otherwise -> ptext SLIT("__string") <+> pprFSAsString s
318 MachInt i signed | code_style && out_of_range
319 -> pprPanic "" (hsep [text "ERROR: Int ", text (show i),
321 brackets (ppr range_min <+> text ".."
323 | otherwise -> integer i
326 range_min = if signed then minInt else 0
328 out_of_range = not (i >= toInteger range_min && i <= toInteger range_max)
330 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
331 | otherwise -> ptext SLIT("__float") <+> rational f
333 MachDouble d -> rational d
335 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
336 | otherwise -> ptext SLIT("__addr") <+> integer p
338 NoRepInteger i _ | code_style -> pprPanic "NoRep in code style" (ppr lit)
339 | otherwise -> ptext SLIT("__integer") <+> integer i
341 NoRepRational r _ | code_style -> pprPanic "NoRep in code style" (ppr lit)
342 | otherwise -> hsep [ptext SLIT("__rational"), integer (numerator r),
343 integer (denominator r)]
345 MachLitLit s ty | code_style -> ptext s
346 | otherwise -> parens (hsep [ptext SLIT("__litlit"),