PprCmmZ.hs 3.85 KB
Newer Older
1

Ian Lynagh's avatar
Ian Lynagh committed
2
module PprCmmZ
3
    ( pprCmmGraphLikeCmm
4 5 6
    )
where

7
import BlockId
8
import Cmm
9
import PprCmm
10
import Outputable
11
import qualified ZipCfgCmmRep as G
12 13 14
import qualified ZipCfg as Z
import CmmZipUtil

15
import Maybe
16 17 18
import FastString

----------------------------------------------------------------
19 20 21 22
-- | The purpose of this function is to print a Cmm zipper graph "as if it were"
-- a Cmm program.  The objective is dodgy, so it's unsurprising parts of the
-- code are dodgy as well.

23 24
pprCmmGraphLikeCmm :: G.CmmGraph -> SDoc
pprCmmGraphLikeCmm g = vcat (swallow blocks)
25 26 27
    where blocks = Z.postorder_dfs g
          swallow :: [G.CmmBlock] -> [SDoc]
          swallow [] = []
28
          swallow (Z.Block id t : rest) = tail id [] Nothing t rest
29 30
          tail id prev' out (Z.ZTail m t) rest = tail id (mid m : prev') out t rest
          tail id prev' out (Z.ZLast (Z.LastOther l)) rest = last id prev' out l rest
31
          tail id prev' _   (Z.ZLast Z.LastExit)      rest = exit id prev' rest
32
          mid m = ppr m
33
          block' id prev'
34
              | id == Z.lg_entry g, entry_has_no_pred =
35 36
                            vcat (text "<entry>" : reverse prev')
              | otherwise = hang (ppr id <> colon) 4 (vcat (reverse prev'))
37
          last id prev' out l n =
38 39
              let endblock stmt = block' id (stmt : prev') : swallow n in
              case l of
40
                G.LastBranch tgt ->
41
                    case n of
42
                      Z.Block id' t : bs
43
                          | tgt == id', unique_pred id' 
44
                          -> tail id prev' out t bs  -- optimize out redundant labels
45 46 47 48
                      _ -> endblock (ppr $ CmmBranch tgt)
                l@(G.LastCondBranch expr tid fid) ->
                  let ft id = text "// fall through to " <> ppr id in
                  case n of
49
                    Z.Block id' t : bs
50 51 52 53 54
                      | id' == fid, isNothing out ->
                          tail id (ft fid : ppr (CmmCondBranch expr tid) : prev') Nothing t bs
                      | id' == tid, Just e' <- maybeInvertCmmExpr expr, isNothing out->
                          tail id (ft tid : ppr (CmmCondBranch e'   fid) : prev') Nothing t bs
                    _ -> endblock $ with_out out l
55 56
                l@(G.LastSwitch {})      -> endblock $ with_out out l
                l@(G.LastCall _ _ _ _ _) -> endblock $ with_out out l
57
          exit id prev' n = -- highly irregular (assertion violation?)
58
              let endblock stmt = block' id (stmt : prev') : swallow n in
59
              endblock (text "// <exit>")
60
          preds = zipPreds g
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
61
          entry_has_no_pred = case lookupBlockEnv preds (Z.lg_entry g) of
62
                                Nothing -> True
63
                                Just s -> isEmptyBlockSet s
64 65 66
          single_preds =
              let add b single =
                    let id = Z.blockId b
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
67
                    in  case lookupBlockEnv preds id of
68
                          Nothing -> single
69
                          Just s -> if sizeBlockSet s == 1 then
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
70
                                        extendBlockSet single id
71
                                    else single
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
72 73
              in  Z.fold_blocks add emptyBlockSet g
          unique_pred id = elemBlockSet id single_preds
74

75
with_out :: Maybe (G.Convention, CmmActuals) -> G.Last -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
76
with_out Nothing l = ptext (sLit "??no-arguments??") <+> ppr l
77
with_out (Just (conv, args)) l = last l
78
    where last (G.LastCall e k _ _ _) =
Ian Lynagh's avatar
Ian Lynagh committed
79
              hcat [ptext (sLit "... = foreign "),
80 81
                    doubleQuotes(ppr conv), space,
                    ppr_target e, parens ( commafy $ map ppr args ),
Ian Lynagh's avatar
Ian Lynagh committed
82
                    ptext (sLit " \"safe\""),
83
                    text " returns to " <+> ppr k,
84
                    semi ]
85
          last l = ppr l
86 87 88
          ppr_target (CmmLit lit) = pprLit lit
          ppr_target fn'          = parens (ppr fn')
          commafy xs = hsep $ punctuate comma xs