module GHC.Cmm.Ppr.Decl
( pprCmms, pprCmmGroup, pprSection, pprStatic
)
where
import GHC.Prelude
import GHC.Platform
import GHC.Cmm.Ppr.Expr
import GHC.Cmm
import GHC.Utils.Outputable
import GHC.Data.FastString
import Data.List (intersperse)
import qualified Data.ByteString as BS
pprCmms :: (OutputableP Platform info, OutputableP Platform g)
=> Platform -> [GenCmmGroup RawCmmStatics info g] -> SDoc
pprCmms platform cmms = pprCode CStyle (vcat (intersperse separator $ map (pdoc platform) cmms))
where
separator = space $$ text "-------------------" $$ space
instance (OutputableP Platform d, OutputableP Platform info, OutputableP Platform i)
=> OutputableP Platform (GenCmmDecl d info i) where
pdoc = pprTop
instance OutputableP Platform (GenCmmStatics a) where
pdoc = pprStatics
instance OutputableP Platform CmmStatic where
pdoc = pprStatic
instance OutputableP Platform CmmInfoTable where
pdoc = pprInfoTable
pprCmmGroup :: (OutputableP Platform d, OutputableP Platform info, OutputableP Platform g)
=> Platform -> GenCmmGroup d info g -> SDoc
pprCmmGroup platform tops
= vcat $ intersperse blankLine $ map (pprTop platform) tops
pprTop :: (OutputableP Platform d, OutputableP Platform info, OutputableP Platform i)
=> Platform -> GenCmmDecl d info i -> SDoc
pprTop platform (CmmProc info lbl live graph)
= vcat [ pdoc platform lbl <> lparen <> rparen <+> lbrace <+> text "// " <+> ppr live
, nest 8 $ lbrace <+> pdoc platform info $$ rbrace
, nest 4 $ pdoc platform graph
, rbrace ]
pprTop platform (CmmData section ds) =
(hang (pprSection platform section <+> lbrace) 4 (pdoc platform ds))
$$ rbrace
pprInfoTable :: Platform -> CmmInfoTable -> SDoc
pprInfoTable platform (CmmInfoTable { cit_lbl = lbl, cit_rep = rep
, cit_prof = prof_info
, cit_srt = srt })
= vcat [ text "label: " <> pdoc platform lbl
, text "rep: " <> ppr rep
, case prof_info of
NoProfilingInfo -> empty
ProfilingInfo ct cd ->
vcat [ text "type: " <> text (show (BS.unpack ct))
, text "desc: " <> text (show (BS.unpack cd)) ]
, text "srt: " <> pdoc platform srt ]
instance Outputable ForeignHint where
ppr NoHint = empty
ppr SignedHint = quotes(text "signed")
ppr AddrHint = (text "PtrHint")
pprStatics :: Platform -> GenCmmStatics a -> SDoc
pprStatics platform (CmmStatics lbl itbl ccs payload) =
pdoc platform lbl <> colon <+> pdoc platform itbl <+> ppr ccs <+> pdoc platform payload
pprStatics platform (CmmStaticsRaw lbl ds) = vcat ((pdoc platform lbl <> colon) : map (pprStatic platform) ds)
pprStatic :: Platform -> CmmStatic -> SDoc
pprStatic platform s = case s of
CmmStaticLit lit -> nest 4 $ text "const" <+> pprLit platform lit <> semi
CmmUninitialised i -> nest 4 $ text "I8" <> brackets (int i)
CmmString s' -> nest 4 $ text "I8[]" <+> text (show s')
CmmFileEmbed path -> nest 4 $ text "incbin " <+> text (show path)
pprSection :: Platform -> Section -> SDoc
pprSection platform (Section t suffix) =
section <+> doubleQuotes (pprSectionType t <+> char '.' <+> pdoc platform suffix)
where
section = text "section"
pprSectionType :: SectionType -> SDoc
pprSectionType s = doubleQuotes (ptext t)
where
t = case s of
Text -> sLit "text"
Data -> sLit "data"
ReadOnlyData -> sLit "readonly"
ReadOnlyData16 -> sLit "readonly16"
RelocatableReadOnlyData
-> sLit "relreadonly"
UninitialisedData -> sLit "uninitialised"
CString -> sLit "cstring"
OtherSection s' -> sLit s'