12 import qualified ZipCfgCmmRep as G
13 import qualified ZipCfg as Z
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.
24 pprCmmGraphLikeCmm :: G.CmmGraph -> SDoc
25 pprCmmGraphLikeCmm g = vcat (swallow blocks)
26 where blocks = Z.postorder_dfs g
27 swallow :: [G.CmmBlock] -> [SDoc]
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
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
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
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>")
62 entry_has_no_pred = case lookupBlockEnv preds (Z.lg_entry g) of
64 Just s -> isEmptyBlockSet s
68 in case lookupBlockEnv preds id of
70 Just s -> if sizeBlockSet s == 1 then
71 extendBlockSet single id
73 in Z.fold_blocks add emptyBlockSet g
74 unique_pred id = elemBlockSet id single_preds
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,
87 ppr_target (CmmLit lit) = pprLit lit
88 ppr_target fn' = parens (ppr fn')
89 commafy xs = hsep $ punctuate comma xs