0359fe2bd223f8271191b20b136fb25631a36649
[ghc-hetmet.git] / compiler / cmm / PprCmmZ.hs
1
2 module PprCmmZ
3     ( pprCmmGraphLikeCmm
4     )
5 where
6
7 import Cmm
8 import CmmExpr
9 import ForeignCall
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 UniqSet
18 import FastString
19
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.
24
25 pprCmmGraphLikeCmm :: G.CmmGraph -> SDoc
26 pprCmmGraphLikeCmm g = vcat (swallow blocks)
27     where blocks = Z.postorder_dfs g
28           swallow :: [G.CmmBlock] -> [SDoc]
29           swallow [] = []
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"
33               else
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)"
40           mid m = ppr m
41           block' id prev'
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
47               case l of
48                 G.LastBranch tgt ->
49                     case n of
50                       Z.Block id' t : bs
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
56                   case n of
57                     Z.Block id' t : bs
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,
70                      id' == k ->
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
76                             else
77                                 endblock (ppcall)
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)
83                              delayed =
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) $$
94                                                          text "// <exit>")
95           preds = zipPreds g
96           entry_has_no_pred = case Z.lookupBlockEnv preds (Z.lg_entry g) of
97                                 Nothing -> True
98                                 Just s -> isEmptyUniqSet s
99           single_preds =
100               let add b single =
101                     let id = Z.blockId b
102                     in  case Z.lookupBlockEnv preds id of
103                           Nothing -> single
104                           Just s -> if sizeUniqSet s == 1 then
105                                         Z.extendBlockSet single id
106                                     else single
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
111
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")
121                               Just _ -> empty,
122                     semi ]
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