Big collection of patches for the new codegen branch.
[ghc-hetmet.git] / compiler / cmm / PprCmmZ.hs
1
2 module PprCmmZ
3     ( pprCmmGraphLikeCmm
4     )
5 where
6
7 import BlockId
8 import Cmm
9 import CmmExpr
10 import PprCmm
11 import Outputable
12 import qualified ZipCfgCmmRep as G
13 import qualified ZipCfg as Z
14 import CmmZipUtil
15
16 import Maybe
17 import FastString
18
19 ----------------------------------------------------------------
20 -- | The purpose of this function is to print a Cmm zipper graph "as if it were"
21 -- a Cmm program.  The objective is dodgy, so it's unsurprising parts of the
22 -- code are dodgy as well.
23
24 pprCmmGraphLikeCmm :: G.CmmGraph -> SDoc
25 pprCmmGraphLikeCmm g = vcat (swallow blocks)
26     where blocks = Z.postorder_dfs g
27           swallow :: [G.CmmBlock] -> [SDoc]
28           swallow [] = []
29           swallow (Z.Block id off t : rest) = tail (id, off) [] Nothing t rest
30           tail id prev' out (Z.ZTail m t) rest = tail id (mid m : prev') out t rest
31           tail id prev' out (Z.ZLast (Z.LastOther l)) rest = last id prev' out l rest
32           tail id prev' _   (Z.ZLast Z.LastExit)      rest = exit id prev' rest
33           mid m = ppr m
34           block' (id, off) prev'
35               | id == Z.lg_entry g, entry_has_no_pred =
36                             vcat (text "<entry>" <> parens (ppr off) : reverse prev')
37               | otherwise = hang (ppr id <> parens (ppr off) <> colon) 4 (vcat (reverse prev'))
38           last id prev' out l n =
39               let endblock stmt = block' id (stmt : prev') : swallow n in
40               case l of
41                 G.LastBranch tgt ->
42                     case n of
43                       Z.Block id' _ t : bs
44                           | tgt == id', unique_pred id' 
45                           -> tail id prev' out t bs  -- optimize out redundant labels
46                       _ -> endblock (ppr $ CmmBranch tgt)
47                 l@(G.LastCondBranch expr tid fid) ->
48                   let ft id = text "// fall through to " <> ppr id in
49                   case n of
50                     Z.Block id' _ t : bs
51                       | id' == fid, isNothing out ->
52                           tail id (ft fid : ppr (CmmCondBranch expr tid) : prev') Nothing t bs
53                       | id' == tid, Just e' <- maybeInvertCmmExpr expr, isNothing out->
54                           tail id (ft tid : ppr (CmmCondBranch e'   fid) : prev') Nothing t bs
55                     _ -> endblock $ with_out out l
56                 l@(G.LastSwitch {})   -> endblock $ with_out out l
57                 l@(G.LastCall _ _ _ _)-> endblock $ with_out out l
58           exit id prev' n = -- highly irregular (assertion violation?)
59               let endblock stmt = block' id (stmt : prev') : swallow n in
60               endblock (text "// <exit>")
61           preds = zipPreds g
62           entry_has_no_pred = case lookupBlockEnv preds (Z.lg_entry g) of
63                                 Nothing -> True
64                                 Just s -> isEmptyBlockSet s
65           single_preds =
66               let add b single =
67                     let id = Z.blockId b
68                     in  case lookupBlockEnv preds id of
69                           Nothing -> single
70                           Just s -> if sizeBlockSet s == 1 then
71                                         extendBlockSet single id
72                                     else single
73               in  Z.fold_blocks add emptyBlockSet g
74           unique_pred id = elemBlockSet id single_preds
75
76 with_out :: Maybe (G.Convention, CmmActuals) -> G.Last -> SDoc
77 with_out Nothing l = ptext (sLit "??no-arguments??") <+> ppr l
78 with_out (Just (conv, args)) l = last l
79     where last (G.LastCall e k _ _) =
80               hcat [ptext (sLit "... = foreign "),
81                     doubleQuotes(ppr conv), space,
82                     ppr_target e, parens ( commafy $ map ppr args ),
83                     ptext (sLit " \"safe\""),
84                     text " returns to " <+> ppr k,
85                     semi ]
86           last l = ppr l
87           ppr_target (CmmLit lit) = pprLit lit
88           ppr_target fn'          = parens (ppr fn')
89           commafy xs = hsep $ punctuate comma xs