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 CStrings ( modnameToC )
38 import FiniteMap ( FiniteMap )
39 import Id ( Id, idName )
40 import Name ( Module )
41 import PrimRep ( getPrimRepSize, PrimRep(..) )
43 import TyCon ( TyCon )
44 import BasicTypes ( TopLevelFlag(..) )
49 codeGen :: FAST_STRING -- 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 ]
97 grp_name = case opt_SccGroup of
99 Nothing -> mod_name -- default: module name
102 mkCcRegister ccs cc_stacks import_names
104 register_ccs = mkAbstractCs (map mk_register ccs)
106 = foldr (mkAbsCStmts . mk_import_register) AbsCNop import_names
107 register_cc_stacks = mkAbstractCs (map mk_register_ccs cc_stacks)
110 CCallProfCCMacro SLIT("START_REGISTER_CCS")
111 [ CLitLit (modnameToC (SLIT("_reg") _APPEND_ mod_name)) AddrRep],
115 CCallProfCCMacro SLIT("END_REGISTER_CCS") []
119 = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
122 = CCallProfCCMacro SLIT("REGISTER_CCS") [mkCCostCentreStack ccs]
124 mk_import_register import_name
125 = CCallProfCCMacro SLIT("REGISTER_IMPORT")
126 [CLitLit (modnameToC (SLIT("_reg") _APPEND_ import_name)) AddrRep]
129 %************************************************************************
131 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
133 %************************************************************************
135 @cgTopBindings@ is only used for top-level bindings, since they need
136 to be allocated statically (not in the heap) and need to be labelled.
137 No unboxed bindings can happen at top level.
139 In the code below, the static bindings are accumulated in the
140 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
141 This is so that we can write the top level processing in a compositional
142 style, with the increasing static environment being plumbed as a state
146 cgTopBindings :: AbstractC -> [(StgBinding,[Id])] -> Code
148 cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
150 cgTopBinding :: AbstractC -> (StgBinding,[Id]) -> Code
152 cgTopBinding split ((StgNonRec name rhs), srt)
154 absC (mkSRT srt_label srt) `thenC`
155 setSRTLabel srt_label (
156 cgTopRhs name rhs `thenFC` \ (name, info) ->
160 srt_label = mkSRTLabel (idName name)
162 cgTopBinding split ((StgRec pairs@((name,rhs):_)), srt)
164 absC (mkSRT srt_label srt) `thenC`
165 setSRTLabel srt_label (
166 fixC (\ new_binds -> addBindsC new_binds `thenC`
167 mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
168 ) `thenFC` \ new_binds ->
172 srt_label = mkSRTLabel (idName name)
174 mkSRT :: CLabel -> [Id] -> AbstractC
175 mkSRT lbl [] = AbsCNop
176 mkSRT lbl ids = CSRT lbl (map (mkClosureLabel . idName) ids)
178 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
179 -- to enclose the listFCs in cgTopBinding, but that tickled the
180 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
182 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
183 -- the Id is passed along for setting up a binding...
185 cgTopRhs bndr (StgRhsCon cc con args)
186 = forkStatics (cgTopRhsCon bndr con args (all zero_size args))
188 zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
190 cgTopRhs bndr (StgRhsClosure cc bi srt fvs upd_flag args body)
191 = ASSERT(null fvs) -- There should be no free variables
192 forkStatics (cgTopRhsClosure bndr cc bi srt args body lf_info)
194 lf_info = mkClosureLFInfo bndr TopLevel [{-no fvs-}] upd_flag args