12 import qualified ZipCfgCmmRep as G
13 import qualified ZipCfg as Z
20 ----------------------------------------------------------------
21 -- | The purpose of this function is to print a Cmm zipper graph "as if it were"
22 -- a Cmm program. The objective is dodgy, so it's unsurprising parts of the
23 -- code are dodgy as well.
25 pprCmmGraphLikeCmm :: G.CmmGraph -> SDoc
26 pprCmmGraphLikeCmm g = vcat (swallow blocks)
27 where blocks = Z.postorder_dfs g
28 swallow :: [G.CmmBlock] -> [SDoc]
30 swallow (Z.Block id off t : rest) = tail (id, off) [] Nothing t rest
31 tail id prev' out (Z.ZTail m t) rest = tail id (mid m : prev') out t rest
32 tail id prev' out (Z.ZLast (Z.LastOther l)) rest = last id prev' out l rest
33 tail id prev' _ (Z.ZLast Z.LastExit) rest = exit id prev' rest
35 block' (id, off) prev'
36 | id == Z.lg_entry g, entry_has_no_pred =
37 vcat (text "<entry>" <> parens (ppr off) : reverse prev')
38 | otherwise = hang (ppr id <> parens (ppr off) <> colon) 4 (vcat (reverse prev'))
39 last id prev' out l n =
40 let endblock stmt = block' id (stmt : prev') : swallow n in
45 | tgt == id', unique_pred id'
46 -> tail id prev' out t bs -- optimize out redundant labels
47 _ -> endblock (ppr $ CmmBranch tgt)
48 l@(G.LastCondBranch expr tid fid) ->
49 let ft id = text "// fall through to " <> ppr id in
52 | id' == fid, isNothing out ->
53 tail id (ft fid : ppr (CmmCondBranch expr tid) : prev') Nothing t bs
54 | id' == tid, Just e' <- maybeInvertCmmExpr expr, isNothing out->
55 tail id (ft tid : ppr (CmmCondBranch e' fid) : prev') Nothing t bs
56 _ -> endblock $ with_out out l
57 l@(G.LastJump {}) -> endblock $ with_out out l
58 l@(G.LastReturn {}) -> endblock $ with_out out l
59 l@(G.LastSwitch {}) -> endblock $ with_out out l
60 l@(G.LastCall _ _ _)-> endblock $ with_out out l
61 exit id prev' n = -- highly irregular (assertion violation?)
62 let endblock stmt = block' id (stmt : prev') : swallow n in
63 endblock (text "// <exit>")
65 entry_has_no_pred = case lookupBlockEnv preds (Z.lg_entry g) of
67 Just s -> isEmptyUniqSet s
71 in case lookupBlockEnv preds id of
73 Just s -> if sizeUniqSet s == 1 then
74 extendBlockSet single id
76 in Z.fold_blocks add emptyBlockSet g
77 unique_pred id = elemBlockSet id single_preds
79 with_out :: Maybe (G.Convention, CmmActuals) -> G.Last -> SDoc
80 with_out Nothing l = ptext (sLit "??no-arguments??") <+> ppr l
81 with_out (Just (conv, args)) l = last l
82 where last (G.LastCall e k _) =
83 hcat [ptext (sLit "... = foreign "),
84 doubleQuotes(ppr conv), space,
85 ppr_target e, parens ( commafy $ map ppr args ),
86 ptext (sLit " \"safe\""),
87 case k of Nothing -> ptext (sLit " never returns")
90 last (G.LastReturn _) = ppr (CmmReturn $ noHints args)
91 last (G.LastJump e _) = ppr (CmmJump e $ noHints args)
93 ppr_target (CmmLit lit) = pprLit lit
94 ppr_target fn' = parens (ppr fn')
95 commafy xs = hsep $ punctuate comma xs
97 -- Anything that uses this is bogus!
98 noHints :: [a] -> [CmmHinted a]
99 noHints = map (\v -> CmmHinted v NoHint)