Merge branch 'master' of c:/code/HEAD-git/. into ghc-generics
[ghc.git] / compiler / profiling / ProfInit.hs
1 -- -----------------------------------------------------------------------------
2 --
3 -- (c) The University of Glasgow, 2011
4 --
5 -- Generate code to initialise cost centres
6 --
7 -- -----------------------------------------------------------------------------
8
9 module ProfInit (profilingInitCode) where
10
11 import CLabel
12 import CostCentre
13 import Outputable
14 import StaticFlags
15 import FastString
16 import Module
17
18 -- -----------------------------------------------------------------------------
19 -- Initialising cost centres
20
21 -- We must produce declarations for the cost-centres defined in this
22 -- module;
23
24 profilingInitCode :: Module -> CollectedCCs -> SDoc
25 profilingInitCode this_mod (local_CCs, ___extern_CCs, singleton_CCSs)
26 | not opt_SccProfilingOn = empty
27 | otherwise
28 = vcat
29 [ text "static void prof_init_" <> ppr this_mod
30 <> text "(void) __attribute__((constructor));"
31 , text "static void prof_init_" <> ppr this_mod <> text "(void)"
32 , braces (vcat (
33 map emitRegisterCC local_CCs ++
34 map emitRegisterCCS singleton_CCSs
35 ))
36 ]
37 where
38 emitRegisterCC cc =
39 ptext (sLit "extern CostCentre ") <> cc_lbl <> ptext (sLit "[];") $$
40 ptext (sLit "REGISTER_CC(") <> cc_lbl <> char ')' <> semi
41 where cc_lbl = ppr (mkCCLabel cc)
42 emitRegisterCCS ccs =
43 ptext (sLit "extern CostCentreStack ") <> ccs_lbl <> ptext (sLit "[];") $$
44 ptext (sLit "REGISTER_CCS(") <> ccs_lbl <> char ')' <> semi
45 where ccs_lbl = ppr (mkCCSLabel ccs)