[project @ 1999-06-01 16:40:41 by simonmar]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Const.lhs
index d254cbe..d1e1a5d 100644 (file)
@@ -8,7 +8,8 @@ module Const (
        Con(..),
        conType, conPrimRep,
        conOkForApp, conOkForAlt, isWHNFCon, isDataCon,
-       conIsTrivial, conIsCheap,
+       conIsTrivial, conIsCheap, conIsDupable, conStrictness, 
+       conOkForSpeculation,
 
        DataCon, PrimOp,        -- For completeness
 
@@ -26,16 +27,20 @@ module Const (
 import TysPrim         ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
                          intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
                        )
-import PrimOp          ( PrimOp, primOpType, primOpIsCheap )
+import PrimOp          ( PrimOp, primOpType, primOpIsDupable,
+                         primOpIsCheap, primOpStrictness, primOpOkForSpeculation )
 import PrimRep         ( PrimRep(..) )
-import DataCon         ( DataCon, dataConType, dataConTyCon, isNullaryDataCon )
+import DataCon         ( DataCon, dataConType, dataConTyCon, isNullaryDataCon, dataConRepStrictness )
 import TyCon           ( isNewTyCon )
 import Type            ( Type, typePrimRep )
 import PprType         ( pprParendType )
+import Demand          ( Demand )
 import CStrings                ( stringToC, charToC, charToEasyHaskell )
 
 import Outputable
 import Util            ( thenCmp )
+
+import Ratio           ( numerator, denominator )
 \end{code}
 
 
@@ -72,6 +77,11 @@ conType (DataCon dc)  = dataConType dc
 conType (Literal lit) = literalType lit
 conType (PrimOp op)   = primOpType op
 
+conStrictness :: Con -> ([Demand], Bool)
+conStrictness (DataCon dc)  = (dataConRepStrictness dc, False)
+conStrictness (PrimOp op)   = primOpStrictness op
+conStrictness (Literal lit) = ([], False)
+
 conPrimRep :: Con -> PrimRep   -- Only data valued constants
 conPrimRep (DataCon dc)  = ASSERT( isNullaryDataCon dc) PtrRep
 conPrimRep (Literal lit) = literalPrimRep lit
@@ -111,6 +121,18 @@ conIsTrivial con      = True
 conIsCheap (Literal lit) = not (isNoRepLit lit)
 conIsCheap (DataCon con) = True
 conIsCheap (PrimOp op)   = primOpIsCheap op
+
+-- conIsDupable is true for constants whose applications we are willing
+-- to duplicate in different case branches; i.e no issue about loss of
+-- work, just space
+conIsDupable (Literal lit) = not (isNoRepLit lit)
+conIsDupable (DataCon con) = True
+conIsDupable (PrimOp op)   = primOpIsDupable op
+
+-- Similarly conOkForSpeculation
+conOkForSpeculation (Literal lit) = True
+conOkForSpeculation (DataCon con) = True
+conOkForSpeculation (PrimOp op)   = primOpOkForSpeculation op
 \end{code}
 
 
@@ -320,6 +342,10 @@ pprLit lit
                                             text "out of range",
                                             brackets (ppr range_min <+> text ".." 
                                                        <+> ppr range_max)])
+                       -- in interface files, parenthesize raw negative ints.
+                       -- this avoids problems like {-1} being interpreted
+                       -- as a comment starter.
+                      | ifaceStyle sty && i < 0 -> parens (integer i)
                       | otherwise -> integer i
 
                       where
@@ -330,7 +356,8 @@ pprLit lit
       MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
                   | otherwise  -> ptext SLIT("__float") <+> rational f
 
-      MachDouble d -> rational d
+      MachDouble d | ifaceStyle sty && d < 0 -> parens (rational d)
+                  | otherwise -> rational d
 
       MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
                 | otherwise  -> ptext SLIT("__addr") <+> integer p