module ProfInit (profilingInitCode) where
import GhcPrelude
import CLabel
import CostCentre
import DynFlags
import Outputable
import Module
profilingInitCode :: Module -> CollectedCCs -> SDoc
profilingInitCode :: Module -> CollectedCCs -> SDoc
profilingInitCode Module
this_mod ([CostCentre]
local_CCs, [CostCentreStack]
singleton_CCSs)
= (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \DynFlags
dflags ->
if Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SccProfilingOn DynFlags
dflags)
then SDoc
empty
else [SDoc] -> SDoc
vcat
([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ (CostCentre -> SDoc) -> [CostCentre] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map CostCentre -> SDoc
emit_cc_decl [CostCentre]
local_CCs
[SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ (CostCentreStack -> SDoc) -> [CostCentreStack] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map CostCentreStack -> SDoc
emit_ccs_decl [CostCentreStack]
singleton_CCSs
[SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ [[CostCentre] -> SDoc
emit_cc_list [CostCentre]
local_CCs]
[SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ [[CostCentreStack] -> SDoc
emit_ccs_list [CostCentreStack]
singleton_CCSs]
[SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ [ String -> SDoc
text String
"static void prof_init_" SDoc -> SDoc -> SDoc
<> Module -> SDoc
forall a. Outputable a => a -> SDoc
ppr Module
this_mod
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"(void) __attribute__((constructor));"
, String -> SDoc
text String
"static void prof_init_" SDoc -> SDoc -> SDoc
<> Module -> SDoc
forall a. Outputable a => a -> SDoc
ppr Module
this_mod SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"(void)"
, SDoc -> SDoc
braces ([SDoc] -> SDoc
vcat
[ String -> SDoc
text String
"registerCcList" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens SDoc
local_cc_list_label SDoc -> SDoc -> SDoc
<> SDoc
semi
, String -> SDoc
text String
"registerCcsList" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens SDoc
singleton_cc_list_label SDoc -> SDoc -> SDoc
<> SDoc
semi
])
]
where
emit_cc_decl :: CostCentre -> SDoc
emit_cc_decl CostCentre
cc =
String -> SDoc
text String
"extern CostCentre" SDoc -> SDoc -> SDoc
<+> SDoc
cc_lbl SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"[];"
where cc_lbl :: SDoc
cc_lbl = CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CostCentre -> CLabel
mkCCLabel CostCentre
cc)
local_cc_list_label :: SDoc
local_cc_list_label = String -> SDoc
text String
"local_cc_" SDoc -> SDoc -> SDoc
<> Module -> SDoc
forall a. Outputable a => a -> SDoc
ppr Module
this_mod
emit_cc_list :: [CostCentre] -> SDoc
emit_cc_list [CostCentre]
ccs =
String -> SDoc
text String
"static CostCentre *" SDoc -> SDoc -> SDoc
<> SDoc
local_cc_list_label SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"[] ="
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
braces ([SDoc] -> SDoc
vcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ [ CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CostCentre -> CLabel
mkCCLabel CostCentre
cc) SDoc -> SDoc -> SDoc
<> SDoc
comma
| CostCentre
cc <- [CostCentre]
ccs
] [SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ [String -> SDoc
text String
"NULL"])
SDoc -> SDoc -> SDoc
<> SDoc
semi
emit_ccs_decl :: CostCentreStack -> SDoc
emit_ccs_decl CostCentreStack
ccs =
String -> SDoc
text String
"extern CostCentreStack" SDoc -> SDoc -> SDoc
<+> SDoc
ccs_lbl SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"[];"
where ccs_lbl :: SDoc
ccs_lbl = CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CostCentreStack -> CLabel
mkCCSLabel CostCentreStack
ccs)
singleton_cc_list_label :: SDoc
singleton_cc_list_label = String -> SDoc
text String
"singleton_cc_" SDoc -> SDoc -> SDoc
<> Module -> SDoc
forall a. Outputable a => a -> SDoc
ppr Module
this_mod
emit_ccs_list :: [CostCentreStack] -> SDoc
emit_ccs_list [CostCentreStack]
ccs =
String -> SDoc
text String
"static CostCentreStack *" SDoc -> SDoc -> SDoc
<> SDoc
singleton_cc_list_label SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"[] ="
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
braces ([SDoc] -> SDoc
vcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ [ CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CostCentreStack -> CLabel
mkCCSLabel CostCentreStack
cc) SDoc -> SDoc -> SDoc
<> SDoc
comma
| CostCentreStack
cc <- [CostCentreStack]
ccs
] [SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ [String -> SDoc
text String
"NULL"])
SDoc -> SDoc -> SDoc
<> SDoc
semi