[project @ 1997-09-04 19:56:48 by sof]
[ghc-hetmet.git] / ghc / compiler / stgSyn / StgSyn.lhs
index bac7e8a..7a7a65f 100644 (file)
@@ -30,7 +30,7 @@ module StgSyn (
        SYN_IE(StgBinding), SYN_IE(StgExpr), SYN_IE(StgRhs),
        SYN_IE(StgCaseAlts), SYN_IE(StgCaseDefault),
 
-       pprPlainStgBinding,
+       pprStgBinding, pprStgBindings,
        getArgPrimRep,
        isLitLitArg,
        stgArity,
@@ -39,18 +39,19 @@ module StgSyn (
 
 IMP_Ubiq(){-uitous-}
 
-import CostCentre      ( showCostCentre )
-import Id              ( externallyVisibleId, idPrimRep, GenId{-instance NamedThing-} )
+import CostCentre      ( showCostCentre, CostCentre )
+import Id              ( idPrimRep, SYN_IE(DataCon), 
+                         GenId{-instance NamedThing-}, SYN_IE(Id) )
 import Literal         ( literalPrimRep, isLitLitLit, Literal{-instance Outputable-} )
-import Name            ( isSymLexeme )
-import Outputable      ( ifPprDebug, interppSP, interpp'SP,
+import Outputable      ( PprStyle(..), userStyle,
+                         ifPprDebug, interppSP, interpp'SP,
                          Outputable(..){-instance * Bool-}
                        )
-import PprStyle                ( PprStyle(..) )
 import PprType         ( GenType{-instance Outputable-} )
 import Pretty          -- all of it
 import PrimOp          ( PrimOp{-instance Outputable-} )
-import Unique          ( pprUnique )
+import Type             ( SYN_IE(Type) )
+import Unique          ( pprUnique, Unique )
 import UniqSet         ( isEmptyUniqSet, uniqSetToList, SYN_IE(UniqSet) )
 import Util            ( panic )
 \end{code}
@@ -70,7 +71,7 @@ with respect to binder and occurrence information (just as in
 data GenStgBinding bndr occ
   = StgNonRec  bndr (GenStgRhs bndr occ)
   | StgRec     [(bndr, GenStgRhs bndr occ)]
-  | StgCoerceBinding bndr occ
+  | StgCoerceBinding bndr occ                  -- UNUSED?
 \end{code}
 
 %************************************************************************
@@ -83,10 +84,12 @@ data GenStgBinding bndr occ
 data GenStgArg occ
   = StgVarArg  occ
   | StgLitArg  Literal
+  | StgConArg   DataCon                -- A nullary data constructor
 \end{code}
 
 \begin{code}
 getArgPrimRep (StgVarArg  local) = idPrimRep local
+getArgPrimRep (StgConArg  con)  = idPrimRep con
 getArgPrimRep (StgLitArg  lit)  = literalPrimRep lit
 
 isLitLitArg (StgLitArg x) = isLitLitLit x
@@ -461,7 +464,7 @@ data UpdateFlag = ReEntrant | Updatable | SingleEntry
 
 instance Outputable UpdateFlag where
     ppr sty u
-      = ppChar (case u of { ReEntrant -> 'r';  Updatable -> 'u';  SingleEntry -> 's' })
+      = char (case u of { ReEntrant -> 'r';  Updatable -> 'u';  SingleEntry -> 's' })
 \end{code}
 
 %************************************************************************
@@ -478,24 +481,11 @@ latest/greatest pragma info.
 \begin{code}
 collectFinalStgBinders
        :: [StgBinding] -- input program
-       -> [Id]         -- final externally-visible top-level Ids
+       -> [Id]
 
-collectFinalStgBinders binds
-  = ex [] binds
-  where
-    ex es [] = es
-
-    ex es ((StgNonRec b _) : binds)
-      = if not (externallyVisibleId b) then
-           ex es binds
-       else
-           ex (b:es) binds
-
-    ex es ((StgRec []) : binds) = ex es binds
-
-    ex es ((StgRec ((b, rhs) : pairs)) : binds)
-      = ex es (StgNonRec b rhs : (StgRec pairs : binds))
-           -- OK, a total hack; laziness rules
+collectFinalStgBinders [] = []
+collectFinalStgBinders (StgNonRec b _ : binds) = b : collectFinalStgBinders binds
+collectFinalStgBinders (StgRec bs     : binds) = map fst bs ++ collectFinalStgBinders binds
 \end{code}
 
 %************************************************************************
@@ -508,27 +498,30 @@ Robin Popplestone asked for semi-colon separators on STG binds; here's
 hoping he likes terminators instead...  Ditto for case alternatives.
 
 \begin{code}
-pprStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee) =>
-               PprStyle -> GenStgBinding bndr bdee -> Pretty
+pprGenStgBinding :: (Outputable bndr, Outputable bdee, Ord bdee) =>
+               PprStyle -> GenStgBinding bndr bdee -> Doc
 
-pprStgBinding sty (StgNonRec bndr rhs)
-  = ppHang (ppCat [ppr sty bndr, ppEquals])
-        4 (ppBeside (ppr sty rhs) ppSemi)
+pprGenStgBinding sty (StgNonRec bndr rhs)
+  = hang (hsep [ppr sty bndr, equals])
+        4 ((<>) (ppr sty rhs) semi)
 
-pprStgBinding sty (StgCoerceBinding bndr occ)
-  = ppHang (ppCat [ppr sty bndr, ppEquals, ppStr "{-Coerce-}"])
-        4 (ppBeside (ppr sty occ) ppSemi)
+pprGenStgBinding sty (StgCoerceBinding bndr occ)
+  = hang (hsep [ppr sty bndr, equals, ptext SLIT("{-Coerce-}")])
+        4 ((<>) (ppr sty occ) semi)
 
-pprStgBinding sty (StgRec pairs)
-  = ppAboves ((ifPprDebug sty (ppStr "{- StgRec -}")) :
-             (map (ppr_bind sty) pairs))
+pprGenStgBinding sty (StgRec pairs)
+  = vcat ((ifPprDebug sty (ptext SLIT("{- StgRec (begin) -}"))) :
+             (map (ppr_bind sty) pairs) ++ [(ifPprDebug sty (ptext SLIT("{- StgRec (end) -}")))])
   where
     ppr_bind sty (bndr, expr)
-      = ppHang (ppCat [ppr sty bndr, ppEquals])
-            4 (ppBeside (ppr sty expr) ppSemi)
+      = hang (hsep [ppr sty bndr, equals])
+            4 ((<>) (ppr sty expr) semi)
+
+pprStgBinding  :: PprStyle -> StgBinding   -> Doc
+pprStgBinding sty  bind  = pprGenStgBinding sty bind
 
-pprPlainStgBinding :: PprStyle -> StgBinding -> Pretty
-pprPlainStgBinding sty b = pprStgBinding sty b
+pprStgBindings :: PprStyle -> [StgBinding] -> Doc
+pprStgBindings sty binds = vcat (map (pprGenStgBinding sty) binds)
 \end{code}
 
 \begin{code}
@@ -537,7 +530,7 @@ instance (Outputable bdee) => Outputable (GenStgArg bdee) where
 
 instance (Outputable bndr, Outputable bdee, Ord bdee)
                => Outputable (GenStgBinding bndr bdee) where
-    ppr = pprStgBinding
+    ppr = pprGenStgBinding
 
 instance (Outputable bndr, Outputable bdee, Ord bdee)
                => Outputable (GenStgExpr bndr bdee) where
@@ -549,33 +542,34 @@ instance (Outputable bndr, Outputable bdee, Ord bdee)
 \end{code}
 
 \begin{code}
-pprStgArg :: (Outputable bdee) => PprStyle -> GenStgArg bdee -> Pretty
+pprStgArg :: (Outputable bdee) => PprStyle -> GenStgArg bdee -> Doc
 
 pprStgArg sty (StgVarArg var) = ppr sty var
+pprStgArg sty (StgConArg con) = ppr sty con
 pprStgArg sty (StgLitArg lit) = ppr sty lit
 \end{code}
 
 \begin{code}
 pprStgExpr :: (Outputable bndr, Outputable bdee, Ord bdee) =>
-               PprStyle -> GenStgExpr bndr bdee -> Pretty
+               PprStyle -> GenStgExpr bndr bdee -> Doc
 -- special case
 pprStgExpr sty (StgApp func [] lvs)
-  = ppBeside (ppr sty func) (pprStgLVs sty lvs)
+  = (<>) (ppr sty func) (pprStgLVs sty lvs)
 
 -- general case
 pprStgExpr sty (StgApp func args lvs)
-  = ppHang (ppBeside (ppr sty func) (pprStgLVs sty lvs))
-        4 (ppSep (map (ppr sty) args))
+  = hang ((<>) (ppr sty func) (pprStgLVs sty lvs))
+        4 (sep (map (ppr sty) args))
 \end{code}
 
 \begin{code}
 pprStgExpr sty (StgCon con args lvs)
-  = ppBesides [ ppBeside (ppr sty con) (pprStgLVs sty lvs),
-               ppStr "! [", interppSP sty args, ppStr "]" ]
+  = hcat [ (<>) (ppr sty con) (pprStgLVs sty lvs),
+               ptext SLIT("! ["), interppSP sty args, char ']' ]
 
 pprStgExpr sty (StgPrim op args lvs)
-  = ppBesides [ ppr sty op, ppChar '#', pprStgLVs sty lvs,
-               ppStr " [", interppSP sty args, ppStr "]" ]
+  = hcat [ ppr sty op, char '#', pprStgLVs sty lvs,
+               ptext SLIT(" ["), interppSP sty args, char ']' ]
 \end{code}
 
 \begin{code}
@@ -589,136 +583,131 @@ pprStgExpr sty (StgPrim op args lvs)
 
 pprStgExpr sty (StgLet (StgNonRec bndr (StgRhsClosure cc bi free_vars upd_flag args rhs))
                        expr@(StgLet _ _))
-  = ppAbove
-      (ppHang (ppBesides [ppStr "let { ", ppr sty bndr, ppStr " = ",
-                         ppStr (showCostCentre sty True{-as string-} cc),
+  = ($$)
+      (hang (hcat [ptext SLIT("let { "), ppr sty bndr, ptext SLIT(" = "),
+                         text (showCostCentre sty True{-as string-} cc),
                          pp_binder_info sty bi,
-                         ppStr " [", ifPprDebug sty (interppSP sty free_vars), ppStr "] \\",
-                         ppr sty upd_flag, ppStr " [",
-                         interppSP sty args, ppStr "]"])
-           8 (ppSep [ppCat [ppr sty rhs, ppStr "} in"]]))
+                         ptext SLIT(" ["), ifPprDebug sty (interppSP sty free_vars), ptext SLIT("] \\"),
+                         ppr sty upd_flag, ptext SLIT(" ["),
+                         interppSP sty args, char ']'])
+           8 (sep [hsep [ppr sty rhs, ptext SLIT("} in")]]))
       (ppr sty expr)
 
 -- special case: let ... in let ...
 
 pprStgExpr sty (StgLet bind expr@(StgLet _ _))
-  = ppAbove
-      (ppSep [ppHang (ppStr "let {") 2 (ppCat [pprStgBinding sty bind, ppStr "} in"])])
+  = ($$)
+      (sep [hang (ptext SLIT("let {")) 2 (hsep [pprGenStgBinding sty bind, ptext SLIT("} in")])])
       (ppr sty expr)
 
 -- general case
 pprStgExpr sty (StgLet bind expr)
-  = ppSep [ppHang (ppStr "let {") 2 (pprStgBinding sty bind),
-          ppHang (ppStr "} in ") 2 (ppr sty expr)]
+  = sep [hang (ptext SLIT("let {")) 2 (pprGenStgBinding sty bind),
+          hang (ptext SLIT("} in ")) 2 (ppr sty expr)]
 
 pprStgExpr sty (StgLetNoEscape lvs_whole lvs_rhss bind expr)
-  = ppSep [ppHang (ppStr "let-no-escape {")
-               2 (pprStgBinding sty bind),
-          ppHang (ppBeside (ppStr "} in ")
+  = sep [hang (ptext SLIT("let-no-escape {"))
+               2 (pprGenStgBinding sty bind),
+          hang ((<>) (ptext SLIT("} in "))
                   (ifPprDebug sty (
-                   ppNest 4 (
-                     ppBesides [ppStr  "-- lvs: [", interppSP sty (uniqSetToList lvs_whole),
-                            ppStr "]; rhs lvs: [", interppSP sty (uniqSetToList lvs_rhss),
-                            ppStr "]"]))))
+                   nest 4 (
+                     hcat [ptext  SLIT("-- lvs: ["), interppSP sty (uniqSetToList lvs_whole),
+                            ptext SLIT("]; rhs lvs: ["), interppSP sty (uniqSetToList lvs_rhss),
+                            char ']']))))
                2 (ppr sty expr)]
 \end{code}
 
 \begin{code}
 pprStgExpr sty (StgSCC ty cc expr)
-  = ppSep [ ppCat [ppStr "_scc_", ppStr (showCostCentre sty True{-as string-} cc)],
+  = sep [ hsep [ptext SLIT("_scc_"), text (showCostCentre sty True{-as string-} cc)],
            pprStgExpr sty expr ]
 \end{code}
 
 \begin{code}
 pprStgExpr sty (StgCase expr lvs_whole lvs_rhss uniq alts)
-  = ppSep [ppSep [ppStr "case",
-          ppNest 4 (ppCat [pprStgExpr sty expr,
-            ifPprDebug sty (ppBeside (ppStr "::") (pp_ty alts))]),
-          ppStr "of {"],
+  = sep [sep [ptext SLIT("case"),
+          nest 4 (hsep [pprStgExpr sty expr,
+            ifPprDebug sty ((<>) (ptext SLIT("::")) (pp_ty alts))]),
+          ptext SLIT("of {")],
           ifPprDebug sty (
-          ppNest 4 (
-            ppBesides [ppStr  "-- lvs: [", interppSP sty (uniqSetToList lvs_whole),
-                   ppStr "]; rhs lvs: [", interppSP sty (uniqSetToList lvs_rhss),
-                   ppStr "]; uniq: ", pprUnique uniq])),
-          ppNest 2 (ppr_alts sty alts),
-          ppStr "}"]
+          nest 4 (
+            hcat [ptext  SLIT("-- lvs: ["), interppSP sty (uniqSetToList lvs_whole),
+                   ptext SLIT("]; rhs lvs: ["), interppSP sty (uniqSetToList lvs_rhss),
+                   ptext SLIT("]; uniq: "), pprUnique uniq])),
+          nest 2 (ppr_alts sty alts),
+          char '}']
   where
+    ppr_default sty StgNoDefault = empty
+    ppr_default sty (StgBindDefault bndr used expr)
+      = hang (hsep [pp_binder, ptext SLIT("->")]) 4 (ppr sty expr)
+      where
+       pp_binder = if used then ppr sty bndr else char '_'
+
     pp_ty (StgAlgAlts  ty _ _) = ppr sty ty
     pp_ty (StgPrimAlts ty _ _) = ppr sty ty
 
     ppr_alts sty (StgAlgAlts ty alts deflt)
-      = ppAboves [ ppAboves (map (ppr_bxd_alt sty) alts),
+      = vcat [ vcat (map (ppr_bxd_alt sty) alts),
                   ppr_default sty deflt ]
       where
        ppr_bxd_alt sty (con, params, use_mask, expr)
-         = ppHang (ppCat [ppr_con sty con, interppSP sty params, ppStr "->"])
-                  4 (ppBeside (ppr sty expr) ppSemi)
-         where
-           ppr_con sty con
-             = if isSymLexeme con
-               then ppBesides [ppLparen, ppr sty con, ppRparen]
-               else ppr sty con
+         = hang (hsep [ppr sty con, interppSP sty params, ptext SLIT("->")])
+                  4 ((<>) (ppr sty expr) semi)
 
     ppr_alts sty (StgPrimAlts ty alts deflt)
-      = ppAboves [ ppAboves (map (ppr_ubxd_alt sty) alts),
+      = vcat [ vcat (map (ppr_ubxd_alt sty) alts),
                   ppr_default sty deflt ]
       where
        ppr_ubxd_alt sty (lit, expr)
-         = ppHang (ppCat [ppr sty lit, ppStr "->"])
-                4 (ppBeside (ppr sty expr) ppSemi)
-
-    ppr_default sty StgNoDefault = ppNil
-    ppr_default sty (StgBindDefault bndr used expr)
-      = ppHang (ppCat [pp_binder, ppStr "->"]) 4 (ppr sty expr)
-      where
-       pp_binder = if used then ppr sty bndr else ppChar '_'
+         = hang (hsep [ppr sty lit, ptext SLIT("->")])
+                4 ((<>) (ppr sty expr) semi)
 \end{code}
 
 \begin{code}
--- pprStgLVs :: PprStyle -> GenStgLiveVars occ -> Pretty
+-- pprStgLVs :: PprStyle -> GenStgLiveVars occ -> Doc
 
-pprStgLVs PprForUser lvs = ppNil
+pprStgLVs sty lvs | userStyle sty = empty
 
 pprStgLVs sty lvs
   = if isEmptyUniqSet lvs then
-       ppNil
+       empty
     else
-       ppBesides [ppStr "{-lvs:", interpp'SP sty (uniqSetToList lvs), ppStr "-}"]
+       hcat [text "{-lvs:", interpp'SP sty (uniqSetToList lvs), text "-}"]
 \end{code}
 
 \begin{code}
 pprStgRhs :: (Outputable bndr, Outputable bdee, Ord bdee) =>
-               PprStyle -> GenStgRhs bndr bdee -> Pretty
+               PprStyle -> GenStgRhs bndr bdee -> Doc
 
 -- special case
 pprStgRhs sty (StgRhsClosure cc bi [free_var] upd_flag [{-no args-}] (StgApp func [] lvs))
-  = ppBesides [ ppStr (showCostCentre sty True{-as String-} cc),
+  = hcat [ text (showCostCentre sty True{-as String-} cc),
                pp_binder_info sty bi,
-               ppStr " [", ifPprDebug sty (ppr sty free_var),
-           ppStr "] \\", ppr sty upd_flag, ppStr " [] ", ppr sty func ]
+               ptext SLIT(" ["), ifPprDebug sty (ppr sty free_var),
+           ptext SLIT("] \\"), ppr sty upd_flag, ptext SLIT(" [] "), ppr sty func ]
 -- general case
 pprStgRhs sty (StgRhsClosure cc bi free_vars upd_flag args body)
-  = ppHang (ppBesides [ ppStr (showCostCentre sty True{-as String-} cc),
+  = hang (hcat [ text (showCostCentre sty True{-as String-} cc),
                pp_binder_info sty bi,
-               ppStr " [", ifPprDebug sty (interppSP sty free_vars),
-               ppStr "] \\", ppr sty upd_flag, ppStr " [", interppSP sty args, ppStr "]"])
+               ptext SLIT(" ["), ifPprDebug sty (interppSP sty free_vars),
+               ptext SLIT("] \\"), ppr sty upd_flag, ptext SLIT(" ["), interppSP sty args, char ']'])
         4 (ppr sty body)
 
 pprStgRhs sty (StgRhsCon cc con args)
-  = ppBesides [ ppStr (showCostCentre sty True{-as String-} cc),
-               ppSP, ppr sty con, ppStr "! [", interppSP sty args, ppStr "]" ]
+  = hcat [ text (showCostCentre sty True{-as String-} cc),
+               space, ppr sty con, ptext SLIT("! ["), interppSP sty args, char ']' ]
 
 --------------
-pp_binder_info PprForUser _ = ppNil
+pp_binder_info sty _ | userStyle sty = empty
 
-pp_binder_info sty NoStgBinderInfo = ppNil
+pp_binder_info sty NoStgBinderInfo = empty
 
 -- cases so boring that we print nothing
-pp_binder_info sty (StgBinderInfo True b c d e) = ppNil
+pp_binder_info sty (StgBinderInfo True b c d e) = empty
 
 -- general case
 pp_binder_info sty (StgBinderInfo a b c d e)
-  = ppBesides [ppChar '(', ppInterleave ppComma (map pp_bool [a,b,c,d,e]), ppChar ')']
+  = parens (hsep (punctuate comma (map pp_bool [a,b,c,d,e])))
   where
     pp_bool x = ppr (panic "pp_bool") x
 \end{code}