13 import qualified ZipCfgCmmRep as G
14 import qualified ZipCfg as Z
21 ----------------------------------------------------------------
22 -- | The purpose of this function is to print a Cmm zipper graph "as if it were"
23 -- a Cmm program. The objective is dodgy, so it's unsurprising parts of the
24 -- code are dodgy as well.
26 pprCmmGraphLikeCmm :: G.CmmGraph -> SDoc
27 pprCmmGraphLikeCmm g = vcat (swallow blocks)
28 where blocks = Z.postorder_dfs g
29 swallow :: [G.CmmBlock] -> [SDoc]
31 swallow (Z.Block id t : rest) = tail id [] Nothing t rest
32 tail id prev' out (Z.ZTail (G.CopyOut conv args) t) rest =
33 if isJust out then panic "multiple CopyOut nodes in one basic block"
35 tail id (prev') (Just (conv, args)) t rest
36 tail id prev' out (Z.ZTail m t) rest = tail id (mid m : prev') out t rest
37 tail id prev' out (Z.ZLast Z.LastExit) rest = exit id prev' out rest
38 tail id prev' out (Z.ZLast (Z.LastOther l)) rest = last id prev' out l rest
39 mid (G.CopyIn _ [] _) = text "// proc point (no parameters)"
40 mid m@(G.CopyIn {}) = ppr m <+> text "(proc point)"
43 | id == Z.lg_entry g, entry_has_no_pred =
44 vcat (text "<entry>" : reverse prev')
45 | otherwise = hang (ppr id <> colon) 4 (vcat (reverse prev'))
46 last id prev' out l n =
47 let endblock stmt = block' id (stmt : prev') : swallow n in
52 | tgt == id', unique_pred id'
53 -> tail id prev' out t bs -- optimize out redundant labels
54 _ -> endblock (ppr $ CmmBranch tgt)
55 l@(G.LastCondBranch expr tid fid) ->
56 let ft id = text "// fall through to " <> ppr id in
59 | id' == fid, isNothing out ->
60 tail id (ft fid : ppr (CmmCondBranch expr tid) : prev') Nothing t bs
61 | id' == tid, Just e' <- maybeInvertCmmExpr expr, isNothing out->
62 tail id (ft tid : ppr (CmmCondBranch e' fid) : prev') Nothing t bs
63 _ -> endblock $ with_out out l
64 l@(G.LastJump {}) -> endblock $ with_out out l
65 l@(G.LastReturn {}) -> endblock $ with_out out l
66 l@(G.LastSwitch {}) -> endblock $ with_out out l
67 l@(G.LastCall _ Nothing) -> endblock $ with_out out l
68 l@(G.LastCall tgt (Just k))
69 | Z.Block id' (Z.ZTail (G.CopyIn _ ress srt) t) : bs <- n,
70 Just (conv, args) <- out,
72 let call = CmmCall tgt' ress args (CmmSafe srt) CmmMayReturn
73 tgt' = CmmCallee tgt (cconv_of_conv conv)
74 ppcall = ppr call <+> parens (text "ret to" <+> ppr k)
75 in if unique_pred k then
76 tail id (ppcall : prev') Nothing t bs
79 | Z.Block id' t : bs <- n, id' == k, unique_pred k,
80 Just (conv, args) <- out,
81 Just (ress, srt) <- findCopyIn t ->
82 let call = CmmCall tgt' ress args (CmmSafe srt) CmmMayReturn
83 tgt' = CmmCallee tgt (cconv_of_conv conv)
85 ptext (sLit "// delayed CopyIn follows previous call")
86 in tail id (delayed : ppr call : prev') Nothing t bs
87 | otherwise -> endblock $ with_out out l
88 findCopyIn (Z.ZTail (G.CopyIn _ ress srt) _) = Just (ress, srt)
89 findCopyIn (Z.ZTail _ t) = findCopyIn t
90 findCopyIn (Z.ZLast _) = Nothing
91 exit id prev' out n = -- highly irregular (assertion violation?)
92 let endblock stmt = block' id (stmt : prev') : swallow n in
93 case out of Nothing -> endblock (text "// <exit>")
94 Just (conv, args) -> endblock (ppr (G.CopyOut conv args) $$
97 entry_has_no_pred = case lookupBlockEnv preds (Z.lg_entry g) of
99 Just s -> isEmptyUniqSet s
103 in case lookupBlockEnv preds id of
105 Just s -> if sizeUniqSet s == 1 then
106 extendBlockSet single id
108 in Z.fold_blocks add emptyBlockSet g
109 unique_pred id = elemBlockSet id single_preds
110 cconv_of_conv (G.ConventionStandard conv _) = conv
111 cconv_of_conv (G.ConventionPrivate {}) = CmmCallConv -- XXX totally bogus
113 with_out :: Maybe (G.Convention, CmmActuals) -> G.Last -> SDoc
114 with_out Nothing l = ptext (sLit "??no-arguments??") <+> ppr l
115 with_out (Just (conv, args)) l = last l
116 where last (G.LastCall e k) =
117 hcat [ptext (sLit "... = foreign "),
118 doubleQuotes(ppr conv), space,
119 ppr_target e, parens ( commafy $ map ppr args ),
120 ptext (sLit " \"safe\""),
121 case k of Nothing -> ptext (sLit " never returns")
124 last (G.LastReturn) = ppr (CmmReturn args)
125 last (G.LastJump e) = ppr (CmmJump e args)
126 last l = ppr (G.CopyOut conv args) $$ ppr l
127 ppr_target (CmmLit lit) = pprLit lit
128 ppr_target fn' = parens (ppr fn')
129 commafy xs = hsep $ punctuate comma xs