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 t : rest) = tail id [] Nothing t rest
31 tail id prev' out (Z.ZTail (G.CopyOut conv args) t) rest =
32 if isJust out then panic "multiple CopyOut nodes in one basic block"
34 tail id (prev') (Just (conv, args)) t rest
35 tail id prev' out (Z.ZTail m t) rest = tail id (mid m : prev') out t rest
36 tail id prev' out (Z.ZLast Z.LastExit) rest = exit id prev' out rest
37 tail id prev' out (Z.ZLast (Z.LastOther l)) rest = last id prev' out l rest
38 mid (G.CopyIn _ [] _) = text "// proc point (no parameters)"
39 mid m@(G.CopyIn {}) = ppr m <+> text "(proc point)"
42 | id == Z.lg_entry g, entry_has_no_pred =
43 vcat (text "<entry>" : reverse prev')
44 | otherwise = hang (ppr id <> colon) 4 (vcat (reverse prev'))
45 last id prev' out l n =
46 let endblock stmt = block' id (stmt : prev') : swallow n in
51 | tgt == id', unique_pred id'
52 -> tail id prev' out t bs -- optimize out redundant labels
53 _ -> endblock (ppr $ CmmBranch tgt)
54 l@(G.LastCondBranch expr tid fid) ->
55 let ft id = text "// fall through to " <> ppr id in
58 | id' == fid, isNothing out ->
59 tail id (ft fid : ppr (CmmCondBranch expr tid) : prev') Nothing t bs
60 | id' == tid, Just e' <- maybeInvertCmmExpr expr, isNothing out->
61 tail id (ft tid : ppr (CmmCondBranch e' fid) : prev') Nothing t bs
62 _ -> endblock $ with_out out l
63 l@(G.LastJump {}) -> endblock $ with_out out l
64 l@(G.LastReturn {}) -> endblock $ with_out out l
65 l@(G.LastSwitch {}) -> endblock $ with_out out l
66 l@(G.LastCall _ Nothing) -> endblock $ with_out out l
67 l@(G.LastCall tgt (Just k))
68 | Z.Block id' (Z.ZTail (G.CopyIn _ ress srt) t) : bs <- n,
69 Just (conv, args) <- out,
71 let call = CmmCall tgt' ress args (CmmSafe srt) CmmMayReturn
72 tgt' = CmmCallee tgt (cconv_of_conv conv)
73 ppcall = ppr call <+> parens (text "ret to" <+> ppr k)
74 in if unique_pred k then
75 tail id (ppcall : prev') Nothing t bs
78 | Z.Block id' t : bs <- n, id' == k, unique_pred k,
79 Just (conv, args) <- out,
80 Just (ress, srt) <- findCopyIn t ->
81 let call = CmmCall tgt' ress args (CmmSafe srt) CmmMayReturn
82 tgt' = CmmCallee tgt (cconv_of_conv conv)
84 ptext (sLit "// delayed CopyIn follows previous call")
85 in tail id (delayed : ppr call : prev') Nothing t bs
86 | otherwise -> endblock $ with_out out l
87 findCopyIn (Z.ZTail (G.CopyIn _ ress srt) _) = Just (ress, srt)
88 findCopyIn (Z.ZTail _ t) = findCopyIn t
89 findCopyIn (Z.ZLast _) = Nothing
90 exit id prev' out n = -- highly irregular (assertion violation?)
91 let endblock stmt = block' id (stmt : prev') : swallow n in
92 case out of Nothing -> endblock (text "// <exit>")
93 Just (conv, args) -> endblock (ppr (G.CopyOut conv args) $$
96 entry_has_no_pred = case Z.lookupBlockEnv preds (Z.lg_entry g) of
98 Just s -> isEmptyUniqSet s
102 in case Z.lookupBlockEnv preds id of
104 Just s -> if sizeUniqSet s == 1 then
105 Z.extendBlockSet single id
107 in Z.fold_blocks add Z.emptyBlockSet g
108 unique_pred id = Z.elemBlockSet id single_preds
109 cconv_of_conv (G.ConventionStandard conv _) = conv
110 cconv_of_conv (G.ConventionPrivate {}) = CmmCallConv -- XXX totally bogus
112 with_out :: Maybe (G.Convention, CmmActuals) -> G.Last -> SDoc
113 with_out Nothing l = ptext (sLit "??no-arguments??") <+> ppr l
114 with_out (Just (conv, args)) l = last l
115 where last (G.LastCall e k) =
116 hcat [ptext (sLit "... = foreign "),
117 doubleQuotes(ppr conv), space,
118 ppr_target e, parens ( commafy $ map ppr args ),
119 ptext (sLit " \"safe\""),
120 case k of Nothing -> ptext (sLit " never returns")
123 last (G.LastReturn) = ppr (CmmReturn args)
124 last (G.LastJump e) = ppr (CmmJump e args)
125 last l = ppr (G.CopyOut conv args) $$ ppr l
126 ppr_target (CmmLit lit) = pprLit lit
127 ppr_target fn' = parens (ppr fn')
128 commafy xs = hsep $ punctuate comma xs