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 )
41 import Ratio (numerator, denominator)
46 %************************************************************************
48 \subsection{The main data type}
50 %************************************************************************
57 | DEFAULT -- Used in case clauses
60 -- The Ord is needed for the FiniteMap used in the lookForConstructor
61 -- in SimplEnv. If you declared that lookForConstructor *ignores*
62 -- constructor-applications with LitArg args, then you could get
65 instance Outputable Con where
66 ppr (DataCon dc) = ppr dc
67 ppr (Literal lit) = ppr lit
68 ppr (PrimOp op) = ppr op
69 ppr DEFAULT = ptext SLIT("__DEFAULT")
71 instance Show Con where
72 showsPrec p con = showsPrecSDoc p (ppr con)
74 conType :: Con -> Type
75 conType (DataCon dc) = dataConType dc
76 conType (Literal lit) = literalType lit
77 conType (PrimOp op) = primOpType op
79 conPrimRep :: Con -> PrimRep -- Only data valued constants
80 conPrimRep (DataCon dc) = ASSERT( isNullaryDataCon dc) PtrRep
81 conPrimRep (Literal lit) = literalPrimRep lit
83 conOkForApp, conOkForAlt :: Con -> Bool
85 -- OK for appliation site
86 conOkForApp (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
87 conOkForApp (Literal _) = True
88 conOkForApp (PrimOp op) = True
89 conOkForApp DEFAULT = False
91 -- OK for case alternative pattern
92 conOkForAlt (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
93 conOkForAlt (Literal lit) = not (isNoRepLit lit)
94 conOkForAlt (PrimOp _) = False
95 conOkForAlt DEFAULT = True
97 -- isWHNFCon is false for PrimOps, which contain work
98 -- Ditto for newtype constructors, which can occur in the output
99 -- of the desugarer, but which will be inlined right away thereafter
100 isWHNFCon (DataCon dc) = not (isNewTyCon (dataConTyCon dc))
101 isWHNFCon (Literal _) = True
102 isWHNFCon (PrimOp _) = False
104 isDataCon (DataCon dc) = True
105 isDataCon other = False
107 -- conIsTrivial is true for constants we are unconditionally happy to duplicate
108 -- cf CoreUtils.exprIsTrivial
109 conIsTrivial (Literal lit) = not (isNoRepLit lit)
110 conIsTrivial (PrimOp _) = False
111 conIsTrivial con = True
113 -- conIsCheap is true for constants whose applications we are willing
114 -- to duplicate in exchange for some modest gain. cf CoreUtils.exprIsCheap
115 conIsCheap (Literal lit) = not (isNoRepLit lit)
116 conIsCheap (DataCon con) = True
117 conIsCheap (PrimOp op) = primOpIsCheap op
121 %************************************************************************
123 \subsection{Literals}
125 %************************************************************************
127 So-called @Literals@ are {\em either}:
130 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
131 which is presumed to be surrounded by appropriate constructors
132 (@mKINT@, etc.), so that the overall thing makes sense.
134 An Integer, Rational, or String literal whose representation we are
135 {\em uncommitted} about; i.e., the surrounding with constructors,
136 function applications, etc., etc., has not yet been done.
142 -- First the primitive guys
144 | MachStr FAST_STRING
146 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
148 | MachInt Integer -- For the numeric types, these are
149 Bool -- True <=> signed (Int#); False <=> unsigned (Word#)
151 | MachInt64 Integer -- guaranteed 64-bit versions of the above.
152 Bool -- True <=> signed (Int#); False <=> unsigned (Word#)
156 | MachDouble Rational
158 | MachLitLit FAST_STRING Type -- Type might be Add# or Int# etc
162 | NoRepStr FAST_STRING Type -- This Type is always String
163 | NoRepInteger Integer Type -- This Type is always Integer
164 | NoRepRational Rational Type -- This Type is always Rational
165 -- We keep these Types in the literal because Rational isn't
166 -- (currently) wired in, so we can't conjure up its type out of
167 -- thin air. Integer is, so the type here is really redundant.
172 instance Outputable Literal where
175 instance Show Literal where
176 showsPrec p lit = showsPrecSDoc p (ppr lit)
178 instance Eq Literal where
179 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
180 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
182 instance Ord Literal where
183 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
184 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
185 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
186 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
187 compare a b = cmpLit a b
194 mkMachInt, mkMachWord :: Integer -> Literal
196 mkMachInt x = MachInt x True{-signed-}
197 mkMachWord x = MachInt x False{-unsigned-}
199 -- check if the int is within range
200 mkMachInt_safe :: Integer -> Literal
203 pprPanic "mkMachInt_safe"
204 (hsep [text "ERROR: Int ", text (show i), text "out of range",
205 brackets (int minInt <+> text ".." <+> int maxInt)])
206 | otherwise = MachInt i True{-signed-}
209 -- i < fromInt minBound ||
212 mkMachInt64 x = MachInt64 x True{-signed-}
213 mkMachWord64 x = MachInt64 x False{-unsigned-}
215 mkStrLit :: String -> Type -> Literal
216 mkStrLit s ty = NoRepStr (_PK_ s) ty
223 isNoRepLit (NoRepStr _ _) = True -- these are not primitive typed!
224 isNoRepLit (NoRepInteger _ _) = True
225 isNoRepLit (NoRepRational _ _) = True
228 isLitLitLit (MachLitLit _ _) = True
229 isLitLitLit _ = False
235 literalType :: Literal -> Type
236 literalType (MachChar _) = charPrimTy
237 literalType (MachStr _) = addrPrimTy
238 literalType (MachAddr _) = addrPrimTy
239 literalType (MachInt _ signed) = if signed then intPrimTy else wordPrimTy
240 literalType (MachInt64 _ signed) = if signed then int64PrimTy else word64PrimTy
241 literalType (MachFloat _) = floatPrimTy
242 literalType (MachDouble _) = doublePrimTy
243 literalType (MachLitLit _ ty) = ty
244 literalType (NoRepInteger _ ty) = ty
245 literalType (NoRepRational _ ty) = ty
246 literalType (NoRepStr _ ty) = ty
250 literalPrimRep :: Literal -> PrimRep
252 literalPrimRep (MachChar _) = CharRep
253 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
254 literalPrimRep (MachAddr _) = AddrRep
255 literalPrimRep (MachInt _ signed) = if signed then IntRep else WordRep
256 literalPrimRep (MachInt64 _ signed) = if signed then Int64Rep else Word64Rep
257 literalPrimRep (MachFloat _) = FloatRep
258 literalPrimRep (MachDouble _) = DoubleRep
259 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
261 literalPrimRep (NoRepInteger _ _) = panic "literalPrimRep:NoRepInteger"
262 literalPrimRep (NoRepRational _ _) = panic "literalPrimRep:NoRepRational"
263 literalPrimRep (NoRepStr _ _) = panic "literalPrimRep:NoRepString"
271 cmpLit (MachChar a) (MachChar b) = a `compare` b
272 cmpLit (MachStr a) (MachStr b) = a `compare` b
273 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
274 cmpLit (MachInt a b) (MachInt c d) = (a `compare` c) `thenCmp` (b `compare` d)
275 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
276 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
277 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
278 cmpLit (NoRepStr a _) (NoRepStr b _) = a `compare` b
279 cmpLit (NoRepInteger a _) (NoRepInteger b _) = a `compare` b
280 cmpLit (NoRepRational a _) (NoRepRational b _) = a `compare` b
281 cmpLit lit1 lit2 | litTag lit1 _LT_ litTag lit2 = LT
284 litTag (MachChar _) = ILIT(1)
285 litTag (MachStr _) = ILIT(2)
286 litTag (MachAddr _) = ILIT(3)
287 litTag (MachInt _ _) = ILIT(4)
288 litTag (MachFloat _) = ILIT(5)
289 litTag (MachDouble _) = ILIT(6)
290 litTag (MachLitLit _ _) = ILIT(7)
291 litTag (NoRepStr _ _) = ILIT(8)
292 litTag (NoRepInteger _ _) = ILIT(9)
293 litTag (NoRepRational _ _) = ILIT(10)
298 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
299 exceptions: MachFloat and MachAddr get an initial keyword prefix
301 * NoRep things get an initial keyword prefix (e.g. _integer_ 3)
305 = getPprStyle $ \ sty ->
307 code_style = codeStyle sty
310 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), char '\'',
311 text (charToC ch), char '\'']
312 | ifaceStyle sty -> char '\'' <> text (charToEasyHaskell ch) <> char '\''
313 | otherwise -> text ['\'', ch, '\'']
315 MachStr s | code_style -> doubleQuotes (text (stringToC (_UNPK_ s)))
316 | otherwise -> pprFSAsString s
319 NoRepStr s ty | code_style -> pprPanic "NoRep in code style" (ppr lit)
320 | otherwise -> ptext SLIT("__string") <+> pprFSAsString s
322 MachInt i signed | code_style && out_of_range
323 -> pprPanic "" (hsep [text "ERROR: Int ", text (show i),
325 brackets (ppr range_min <+> text ".."
327 | otherwise -> integer i
330 range_min = if signed then minInt else 0
332 out_of_range = not (i >= toInteger range_min && i <= toInteger range_max)
334 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
335 | otherwise -> ptext SLIT("__float") <+> rational f
337 MachDouble d -> rational d
339 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
340 | otherwise -> ptext SLIT("__addr") <+> integer p
342 NoRepInteger i _ | code_style -> pprPanic "NoRep in code style" (ppr lit)
343 | otherwise -> ptext SLIT("__integer") <+> integer i
345 NoRepRational r _ | code_style -> pprPanic "NoRep in code style" (ppr lit)
346 | otherwise -> hsep [ptext SLIT("__rational"), integer (numerator r),
347 integer (denominator r)]
349 MachLitLit s ty | code_style -> ptext s
350 | otherwise -> parens (hsep [ptext SLIT("__litlit"),