2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CodeGen]{@CodeGen@: main module of the code generator}
6 This module says how things get going at the top level.
8 @codeGen@ is the interface to the outside world. The \tr{cgTop*}
9 functions drive the mangling of top-level bindings.
11 %************************************************************************
13 \subsection[codeGen-outside-interface]{The code generator's offering to the world}
15 %************************************************************************
18 module CodeGen ( codeGen ) where
20 #include "HsVersions.h"
25 import CLabel ( CLabel, mkSRTLabel, mkClosureLabel )
27 import AbsCUtils ( mkAbstractCs, mkAbsCStmts )
28 import CgBindery ( CgIdInfo )
29 import CgClosure ( cgTopRhsClosure )
30 import CgCon ( cgTopRhsCon )
31 import CgConTbls ( genStaticConBits )
32 import ClosureInfo ( mkClosureLFInfo )
33 import CmdLineOpts ( opt_SccProfilingOn, opt_EnsureSplittableC,
36 import CostCentre ( CostCentre, CostCentreStack )
37 import FiniteMap ( FiniteMap )
38 import Id ( Id, idName )
39 import Name ( Module, moduleString )
40 import PrimRep ( getPrimRepSize, PrimRep(..) )
42 import TyCon ( TyCon )
43 import BasicTypes ( TopLevelFlag(..) )
45 import Panic ( assertPanic )
49 codeGen :: Module -- module name
50 -> ([CostCentre], -- local cost-centres needing declaring/registering
51 [CostCentre], -- "extern" cost-centres needing declaring
52 [CostCentreStack]) -- pre-defined "singleton" cost centre stacks
53 -> [Module] -- import names
54 -> [TyCon] -- tycons with data constructors to convert
55 -> FiniteMap TyCon [(Bool, [Maybe Type])]
56 -- tycon specialisation info
57 -> [(StgBinding,[Id])] -- bindings to convert, with SRTs
58 -> AbstractC -- output
60 codeGen mod_name (local_CCs, extern_CCs, singleton_CCSs)
61 import_names gen_tycons tycon_specs stg_pgm
63 maybe_split = if opt_EnsureSplittableC
66 cinfo = MkCompInfo mod_name
69 module_code = mkAbstractCs [
70 genStaticConBits cinfo gen_tycons tycon_specs,
71 initC cinfo (cgTopBindings maybe_split stg_pgm) ]
73 -- Cost-centre profiling:
74 -- Besides the usual stuff, we must produce:
76 -- * Declarations for the cost-centres defined in this module;
77 -- * Code to participate in "registering" all the cost-centres
78 -- in the program (done at startup time when the pgm is run).
80 -- (The local cost-centres involved in this are passed
81 -- into the code-generator, as are the imported-modules' names.)
85 | not opt_SccProfilingOn = AbsCNop
86 | otherwise = mkAbstractCs (
87 map (CCostCentreDecl True) local_CCs ++
88 map (CCostCentreDecl False) extern_CCs ++
89 map CCostCentreStackDecl singleton_CCSs ++
90 mkCcRegister local_CCs singleton_CCSs import_names
93 mkAbstractCs [ cost_centre_stuff, module_code ]
96 mkCcRegister ccs cc_stacks import_names
98 register_ccs = mkAbstractCs (map mk_register ccs)
100 = foldr (mkAbsCStmts . mk_import_register) AbsCNop import_names
101 register_cc_stacks = mkAbstractCs (map mk_register_ccs cc_stacks)
104 CCallProfCCMacro SLIT("START_REGISTER_CCS")
105 [ CLitLit (_PK_ ("_reg" ++ moduleString mod_name)) AddrRep],
109 CCallProfCCMacro SLIT("END_REGISTER_CCS") []
113 = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
116 = CCallProfCCMacro SLIT("REGISTER_CCS") [mkCCostCentreStack ccs]
118 mk_import_register import_name
119 = CCallProfCCMacro SLIT("REGISTER_IMPORT")
120 [CLitLit (_PK_ ("_reg" ++ moduleString import_name)) AddrRep]
123 %************************************************************************
125 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
127 %************************************************************************
129 @cgTopBindings@ is only used for top-level bindings, since they need
130 to be allocated statically (not in the heap) and need to be labelled.
131 No unboxed bindings can happen at top level.
133 In the code below, the static bindings are accumulated in the
134 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
135 This is so that we can write the top level processing in a compositional
136 style, with the increasing static environment being plumbed as a state
140 cgTopBindings :: AbstractC -> [(StgBinding,[Id])] -> Code
142 cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
144 cgTopBinding :: AbstractC -> (StgBinding,[Id]) -> Code
146 cgTopBinding split ((StgNonRec name rhs), srt)
148 absC (mkSRT srt_label srt) `thenC`
149 setSRTLabel srt_label (
150 cgTopRhs name rhs `thenFC` \ (name, info) ->
154 srt_label = mkSRTLabel (idName name)
156 cgTopBinding split ((StgRec pairs@((name,rhs):_)), srt)
158 absC (mkSRT srt_label srt) `thenC`
159 setSRTLabel srt_label (
160 fixC (\ new_binds -> addBindsC new_binds `thenC`
161 mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
162 ) `thenFC` \ new_binds ->
166 srt_label = mkSRTLabel (idName name)
168 mkSRT :: CLabel -> [Id] -> AbstractC
169 mkSRT lbl [] = AbsCNop
170 mkSRT lbl ids = CSRT lbl (map (mkClosureLabel . idName) ids)
172 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
173 -- to enclose the listFCs in cgTopBinding, but that tickled the
174 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
176 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
177 -- the Id is passed along for setting up a binding...
179 cgTopRhs bndr (StgRhsCon cc con args)
180 = forkStatics (cgTopRhsCon bndr con args (all zero_size args))
182 zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
184 cgTopRhs bndr (StgRhsClosure cc bi srt fvs upd_flag args body)
185 = ASSERT(null fvs) -- There should be no free variables
186 forkStatics (cgTopRhsClosure bndr cc bi srt args body lf_info)
188 lf_info = mkClosureLFInfo bndr TopLevel [{-no fvs-}] upd_flag args