Merging in the new codegen branch
[ghc-hetmet.git] / compiler / cmm / PprCmmZ.hs
1
2 module PprCmmZ
3     ( pprCmmGraphLikeCmm
4     )
5 where
6
7 import BlockId
8 import Cmm
9 import CmmExpr
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 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
34           mid m = ppr m
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
41               case l of
42                 G.LastBranch tgt ->
43                     case n of
44                       Z.Block id' _ t : bs
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
50                   case n of
51                     Z.Block id' _ t : bs
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>")
64           preds = zipPreds g
65           entry_has_no_pred = case lookupBlockEnv preds (Z.lg_entry g) of
66                                 Nothing -> True
67                                 Just s -> isEmptyUniqSet s
68           single_preds =
69               let add b single =
70                     let id = Z.blockId b
71                     in  case lookupBlockEnv preds id of
72                           Nothing -> single
73                           Just s -> if sizeUniqSet s == 1 then
74                                         extendBlockSet single id
75                                     else single
76               in  Z.fold_blocks add emptyBlockSet g
77           unique_pred id = elemBlockSet id single_preds
78
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")
88                               Just _ -> empty,
89                     semi ]
90           last (G.LastReturn _) = ppr (CmmReturn $ noHints args)
91           last (G.LastJump e _) = ppr (CmmJump e $ noHints args)
92           last l = ppr l
93           ppr_target (CmmLit lit) = pprLit lit
94           ppr_target fn'          = parens (ppr fn')
95           commafy xs = hsep $ punctuate comma xs
96
97 -- Anything that uses this is bogus!
98 noHints :: [a] -> [CmmHinted a]
99 noHints = map (\v -> CmmHinted v NoHint)