[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Const.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
3 %
4 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
5
6 \begin{code}
7 module Const (
8         Con(..),
9         conType, conPrimRep,
10         conOkForApp, conOkForAlt, isWHNFCon, isDataCon,
11         conIsTrivial, conIsCheap,
12
13         DataCon, PrimOp,        -- For completeness
14
15         -- Defined here
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
22     ) where
23
24 #include "HsVersions.h"
25
26 import TysPrim          ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
27                           intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
28                         )
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 )
36
37 import Outputable
38 import Util             ( thenCmp )
39 \end{code}
40
41
42 %************************************************************************
43 %*                                                                      *
44 \subsection{The main data type}
45 %*                                                                      *
46 %************************************************************************
47
48 \begin{code}
49 data Con
50   = DataCon  DataCon
51   | Literal  Literal
52   | PrimOp   PrimOp
53   | DEFAULT                     -- Used in case clauses
54   deriving (Eq, Ord)
55
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
59 -- rid of this Ord.
60
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")
66
67 instance Show Con where
68   showsPrec p con = showsPrecSDoc p (ppr con)
69
70 conType :: Con -> Type
71 conType (DataCon dc)  = dataConType dc
72 conType (Literal lit) = literalType lit
73 conType (PrimOp op)   = primOpType op
74
75 conPrimRep :: Con -> PrimRep    -- Only data valued constants
76 conPrimRep (DataCon dc)  = ASSERT( isNullaryDataCon dc) PtrRep
77 conPrimRep (Literal lit) = literalPrimRep lit
78
79 conOkForApp, conOkForAlt :: Con -> Bool
80
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
86
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
92
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
99
100 isDataCon (DataCon dc) = True
101 isDataCon other        = False
102
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
108
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
114 \end{code}
115
116
117 %************************************************************************
118 %*                                                                      *
119 \subsection{Literals}
120 %*                                                                      *
121 %************************************************************************
122
123 So-called @Literals@ are {\em either}:
124 \begin{itemize}
125 \item
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.
129 \item
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.
133 \end{itemize}
134
135 \begin{code}
136 data Literal
137   =     ------------------
138         -- First the primitive guys
139     MachChar    Char
140   | MachStr     FAST_STRING
141
142   | MachAddr    Integer -- Whatever this machine thinks is a "pointer"
143
144   | MachInt     Integer -- For the numeric types, these are
145                 Bool    -- True <=> signed (Int#); False <=> unsigned (Word#)
146
147   | MachInt64   Integer -- guaranteed 64-bit versions of the above.
148                 Bool    -- True <=> signed (Int#); False <=> unsigned (Word#)
149
150
151   | MachFloat   Rational
152   | MachDouble  Rational
153
154   | MachLitLit  FAST_STRING Type        -- Type might be Add# or Int# etc
155
156         ------------------
157         -- The no-rep guys
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.
164 \end{code}
165
166
167 \begin{code}
168 instance Outputable Literal where
169     ppr lit = pprLit lit
170
171 instance Show Literal where
172     showsPrec p lit = showsPrecSDoc p (ppr lit)
173
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  }
177
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
184 \end{code}
185
186
187         Construction
188         ~~~~~~~~~~~~
189 \begin{code}
190 mkMachInt, mkMachWord :: Integer -> Literal
191
192 mkMachInt  x = MachInt x True{-signed-}
193 mkMachWord x = MachInt x False{-unsigned-}
194
195 -- check if the int is within range
196 mkMachInt_safe :: Integer -> Literal
197 mkMachInt_safe i
198  | out_of_range = 
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-}
203  where
204   out_of_range =
205 --    i < fromInt minBound ||
206     i > fromInt maxInt
207
208 mkMachInt64  x = MachInt64 x True{-signed-}
209 mkMachWord64 x = MachInt64 x False{-unsigned-}
210
211 mkStrLit :: String -> Type -> Literal
212 mkStrLit s ty = NoRepStr (_PK_ s) ty
213 \end{code}
214
215
216         Predicates
217         ~~~~~~~~~~
218 \begin{code}
219 isNoRepLit (NoRepStr _ _)       = True -- these are not primitive typed!
220 isNoRepLit (NoRepInteger  _ _)  = True
221 isNoRepLit (NoRepRational _ _)  = True
222 isNoRepLit _                    = False
223
224 isLitLitLit (MachLitLit _ _) = True
225 isLitLitLit _                = False
226 \end{code}
227
228         Types
229         ~~~~~
230 \begin{code}
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
243 \end{code}
244
245 \begin{code}
246 literalPrimRep :: Literal -> PrimRep
247
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
256 #ifdef DEBUG
257 literalPrimRep (NoRepInteger  _ _) = panic "literalPrimRep:NoRepInteger"
258 literalPrimRep (NoRepRational _ _) = panic "literalPrimRep:NoRepRational"
259 literalPrimRep (NoRepStr _ _)      = panic "literalPrimRep:NoRepString"
260 #endif
261 \end{code}
262
263
264         Comparison
265         ~~~~~~~~~~
266 \begin{code}
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
278                                                 | otherwise                    = GT
279
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)
290 \end{code}
291
292         Printing
293         ~~~~~~~~
294 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
295   exceptions: MachFloat and MachAddr get an initial keyword prefix
296
297 * NoRep things get an initial keyword prefix (e.g. _integer_ 3)
298
299 \begin{code}
300 pprLit lit
301   = getPprStyle $ \ sty ->
302     let
303       code_style = codeStyle sty
304     in
305     case lit of
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, '\'']
310
311       MachStr s | code_style -> doubleQuotes (text (stringToC (_UNPK_ s)))
312                 | otherwise  -> pprFSAsString s
313
314
315       NoRepStr s ty | code_style -> pprPanic "NoRep in code style" (ppr lit)
316                     | otherwise  -> ptext SLIT("__string") <+> pprFSAsString s
317
318       MachInt i signed | code_style && out_of_range 
319                        -> pprPanic "" (hsep [text "ERROR: Int ", text (show i), 
320                                              text "out of range",
321                                              brackets (ppr range_min <+> text ".." 
322                                                         <+> ppr range_max)])
323                        | otherwise -> integer i
324
325                        where
326                         range_min = if signed then minInt else 0
327                         range_max = maxInt
328                         out_of_range = not (i >= toInteger range_min && i <= toInteger range_max)
329
330       MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
331                   | otherwise  -> ptext SLIT("__float") <+> rational f
332
333       MachDouble d -> rational d
334
335       MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
336                  | otherwise  -> ptext SLIT("__addr") <+> integer p
337
338       NoRepInteger i _ | code_style -> pprPanic "NoRep in code style" (ppr lit)
339                        | otherwise  -> ptext SLIT("__integer") <+> integer i
340
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)]
344
345       MachLitLit s ty | code_style -> ptext s
346                       | otherwise  -> parens (hsep [ptext SLIT("__litlit"), 
347                                                     pprFSAsString s,
348                                                     pprParendType ty])
349 \end{code}