-{-# OPTIONS -Wall -fno-warn-name-shadowing #-}
+
-- This module is pure representation and should be imported only by
-- clients that need to manipulate representation and know what
module ZipCfgCmmRep
( CmmZ, CmmTopZ, CmmGraph, CmmBlock, CmmAGraph, Middle(..), Last(..), Convention(..)
+ , ValueDirection(..)
)
where
import MkZipCfg
import Maybes
-import Outputable hiding (empty)
-import qualified Outputable as PP
+import Outputable
import Prelude hiding (zip, unzip, last)
type CmmGraph = LGraph Middle Last
| MidUnsafeCall -- An "unsafe" foreign call;
CmmCallTarget -- just a fat machine instructoin
- CmmFormals -- zero or more results
+ CmmFormals -- zero or more results
CmmActuals -- zero or more arguments
| CopyIn -- Move parameters or results from conventional locations to registers
-- Note [CopyIn invariant]
Convention
- CmmFormals
+ CmmFormals -- eventually [CmmKind] will be used only for foreign
+ -- calls and will migrate into 'Convention' (helping to
+ -- drain "the swamp")
C_SRT -- Static things kept alive by this block
- | CopyOut Convention CmmFormals
+ | CopyOut Convention CmmActuals
data Last
= LastReturn CmmActuals -- Return from a function,
-- The parameters are unused at present.
| LastCall { -- A call (native or safe foreign)
- cml_target :: CmmCallTarget,
- cml_actual :: CmmActuals, -- Zero or more arguments
+ cml_target :: CmmExpr, -- never a CmmPrim to a CallishMachOp!
cml_next :: Maybe BlockId } -- BlockId of continuation, if call returns
| LastCondBranch { -- conditional branch
-- Undefined outside range, and when there's a Nothing
data Convention
- = Argument CCallConv -- Used for function formal params
- | Result CCallConv -- Used for function results
-
- | Local -- Used for control transfers within a (pre-CPS) procedure
+ = ConventionStandard CCallConv ValueDirection
+ | ConventionPrivate
+ -- Used for control transfers within a (pre-CPS) procedure
-- All jump sites known, never pushed on the stack (hence no SRT)
-- You can choose whatever calling convention
-- you please (provided you make sure
-- all the call sites agree)!
deriving Eq
--- ^ In a complete LGraph for a procedure, the [[Exit]] node should not
--- appear, but it is useful in a subgraph (e.g., replacement for a node).
+data ValueDirection = Arguments | Results
+ -- Arguments go with procedure definitions, jumps, and arguments to calls
+ -- Results go with returns and with results of calls.
+ deriving Eq
{-
Note [CopyIn invariant]
branchNodeTarget _ = panic "asked for target of non-branch"
cmmSuccs :: Last -> [BlockId]
-cmmSuccs (LastReturn {}) = []
-cmmSuccs (LastJump {}) = []
-cmmSuccs (LastBranch id _) = [id]
-cmmSuccs (LastCall _ _ (Just id)) = [id]
-cmmSuccs (LastCall _ _ Nothing) = []
-cmmSuccs (LastCondBranch _ t f) = [f, t] -- meets layout constraint
-cmmSuccs (LastSwitch _ edges) = catMaybes edges
+cmmSuccs (LastReturn {}) = []
+cmmSuccs (LastJump {}) = []
+cmmSuccs (LastBranch id _) = [id]
+cmmSuccs (LastCall _ (Just id)) = [id]
+cmmSuccs (LastCall _ Nothing) = []
+cmmSuccs (LastCondBranch _ t f) = [f, t] -- meets layout constraint
+cmmSuccs (LastSwitch _ edges) = catMaybes edges
fold_cmm_succs :: (BlockId -> a -> a) -> Last -> a -> a
fold_cmm_succs _f (LastReturn {}) z = z
fold_cmm_succs _f (LastJump {}) z = z
fold_cmm_succs f (LastBranch id _) z = f id z
-fold_cmm_succs f (LastCall _ _ (Just id)) z = f id z
-fold_cmm_succs _f (LastCall _ _ Nothing) z = z
+fold_cmm_succs f (LastCall _ (Just id)) z = f id z
+fold_cmm_succs _f (LastCall _ Nothing) z = z
fold_cmm_succs f (LastCondBranch _ te fe) z = f te (f fe z)
fold_cmm_succs f (LastSwitch _ edges) z = foldl (flip f) z $ catMaybes edges
instance DF.DebugNodes Middle Last
instance Outputable CmmGraph where
- ppr = pprCmmGraphAsRep
+ ppr = pprLgraph
-pprCmmGraphAsRep :: CmmGraph -> SDoc
-pprCmmGraphAsRep g = vcat (map ppr_block blocks)
- where blocks = postorder_dfs g
- ppr_block (Block id tail) = hang (ppr id <> colon) 4 (ppr tail)
+debugPpr :: Bool
+debugPpr = debugIsOn
pprMiddle :: Middle -> SDoc
pprMiddle stmt = (case stmt of
ptext SLIT("foreign") <+> doubleQuotes(ppr conv) <+> ptext SLIT("...")
CopyOut conv args ->
- if null args then PP.empty
+ if null args then empty
else ptext SLIT("CopyOut") <+> doubleQuotes(ppr conv) <+>
parens (commafy (map pprHinted args))
-- ToDo ppr volatile
MidUnsafeCall (CmmCallee fn cconv) results args ->
hcat [ if null results
- then PP.empty
+ then empty
else parens (commafy $ map ppr results) <>
ptext SLIT(" = "),
ptext SLIT("call"), space,
pprMiddle (MidUnsafeCall (CmmCallee (CmmLit lbl) CCallConv) results args)
where
lbl = CmmLabel (mkForeignLabel (mkFastString (show op)) Nothing False)
- ) <+> text "//" <+>
- case stmt of
- MidNop {} -> text "MidNop"
- CopyIn {} -> text "CopyIn"
- CopyOut {} -> text "CopyOut"
- MidComment {} -> text "MidComment"
- MidAssign {} -> text "MidAssign"
- MidStore {} -> text "MidStore"
- MidUnsafeCall {} -> text "MidUnsafeCall"
+ ) <>
+ if debugPpr then empty
+ else text " //" <+>
+ case stmt of
+ MidNop {} -> text "MidNop"
+ CopyIn {} -> text "CopyIn"
+ CopyOut {} -> text "CopyOut"
+ MidComment {} -> text "MidComment"
+ MidAssign {} -> text "MidAssign"
+ MidStore {} -> text "MidStore"
+ MidUnsafeCall {} -> text "MidUnsafeCall"
pprHinted :: Outputable a => (a, MachHint) -> SDoc
, parens ( commafy $ map pprHinted results )
, semi ]
LastSwitch arg ids -> ppr $ CmmSwitch arg ids
- LastCall tgt params k -> genCall tgt params k
- ) <+> text "//" <+>
- case stmt of
- LastBranch {} -> text "LastBranch"
- LastCondBranch {} -> text "LastCondBranch"
- LastJump {} -> text "LastJump"
- LastReturn {} -> text "LastReturn"
- LastSwitch {} -> text "LastSwitch"
- LastCall {} -> text "LastCall"
-
-
-genCall :: CmmCallTarget -> CmmActuals -> Maybe BlockId -> SDoc
-genCall (CmmCallee fn cconv) args k =
+ LastCall tgt k -> genBareCall tgt k
+ ) <>
+ if debugPpr then empty
+ else text " //" <+>
+ case stmt of
+ LastBranch {} -> text "LastBranch"
+ LastCondBranch {} -> text "LastCondBranch"
+ LastJump {} -> text "LastJump"
+ LastReturn {} -> text "LastReturn"
+ LastSwitch {} -> text "LastSwitch"
+ LastCall {} -> text "LastCall"
+
+genBareCall :: CmmExpr -> Maybe BlockId -> SDoc
+genBareCall fn k =
hcat [ ptext SLIT("foreign"), space
- , doubleQuotes(ppr cconv), space
- , target fn, parens ( commafy $ map pprHinted args ), space
+ , doubleQuotes(ptext SLIT("<convention from CopyOut>")), space
+ , target fn, parens ( ptext SLIT("<parameters from CopyOut>") ), space
, case k of Nothing -> ptext SLIT("never returns")
Just k -> ptext SLIT("returns to") <+> ppr k
, semi ]
target t@(CmmLit _) = ppr t
target fn' = parens (ppr fn')
-genCall (CmmPrim op) args k =
- hcat [ text "%", text (show op), parens ( commafy $ map pprHinted args ),
- ptext SLIT("returns to"), space, ppr k,
- semi ]
-
genBranchWithArgs :: (Outputable id, Outputable arg) => id -> [arg] -> SDoc
genBranchWithArgs ident [] = ptext SLIT("goto") <+> ppr ident <> semi
genBranchWithArgs ident args = ptext SLIT("goto") <+> ppr ident <+>
]
pprConvention :: Convention -> SDoc
-pprConvention (Argument c) = ppr c
-pprConvention (Result c) = ppr c
-pprConvention Local = text "<local>"
+pprConvention (ConventionStandard c _) = ppr c
+pprConvention (ConventionPrivate {} ) = text "<private-convention>"
commafy :: [SDoc] -> SDoc
commafy xs = hsep $ punctuate comma xs