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,
26 mkModuleInitLabel, labelDynamic )
28 import PprAbsC ( dumpRealC )
29 import AbsCUtils ( mkAbstractCs, flattenAbsC )
30 import CgBindery ( CgIdInfo, addBindC, addBindsC )
31 import CgClosure ( cgTopRhsClosure )
32 import CgCon ( cgTopRhsCon )
33 import CgConTbls ( genStaticConBits )
34 import ClosureInfo ( mkClosureLFInfo )
35 import CmdLineOpts ( opt_SccProfilingOn, opt_EnsureSplittableC,
38 import CostCentre ( CostCentre, CostCentreStack )
39 import Id ( Id, idName )
40 import Module ( Module, moduleString, moduleName,
42 import PrimRep ( getPrimRepSize, PrimRep(..) )
44 import TyCon ( TyCon, isDataTyCon )
45 import Class ( Class, classTyCon )
46 import BasicTypes ( TopLevelFlag(..) )
47 import UniqSupply ( mkSplitUniqSupply )
48 import ErrUtils ( dumpIfSet )
50 import Panic ( assertPanic )
56 codeGen :: Module -- Module name
57 -> [Module] -- Import names
58 -> ([CostCentre], -- Local cost-centres needing declaring/registering
59 [CostCentre], -- "extern" cost-centres needing declaring
60 [CostCentreStack]) -- Pre-defined "singleton" cost centre stacks
61 -> [Id] -- foreign-exported binders
62 -> [TyCon] -> [Class] -- Local tycons and classes
63 -> [(StgBinding,[Id])] -- Bindings to convert, with SRTs
64 -> IO AbstractC -- Output
66 codeGen mod_name imported_modules cost_centre_info fe_binders
67 tycons classes stg_binds
68 = mkSplitUniqSupply 'f' >>= \ fl_uniqs -> -- absC flattener
70 datatype_stuff = genStaticConBits cinfo data_tycons
71 code_stuff = initC cinfo (cgTopBindings maybe_split stg_binds)
72 init_stuff = mkModuleInit fe_binders mod_name imported_modules
75 abstractC = mkAbstractCs [ maybe_split,
79 -- Put datatype_stuff after code_stuff, because the
80 -- datatype closure table (for enumeration types)
81 -- to (say) PrelBase_True_closure, which is defined in code_stuff
83 flat_abstractC = flattenAbsC fl_uniqs abstractC
85 dumpIfSet opt_D_dump_absC "Abstract C" (dumpRealC abstractC) >>
89 data_tycons = filter isDataTyCon (tycons ++ map classTyCon classes)
90 -- Generate info tables for the data constrs arising
91 -- from class decls as well
93 maybe_split = if opt_EnsureSplittableC
96 cinfo = MkCompInfo mod_name
99 %************************************************************************
101 \subsection[codegen-init]{Module initialisation code}
103 %************************************************************************
107 :: [Id] -- foreign exported functions
108 -> Module -- module name
109 -> [Module] -- import names
110 -> ([CostCentre], -- cost centre info
114 mkModuleInit fe_binders mod imps cost_centre_info
117 map (\f -> CMacroStmt REGISTER_FOREIGN_EXPORT [f]) fe_labels
120 map (\f -> CLbl (mkClosureLabel (idName f)) PtrRep) fe_binders
122 (cc_decls, cc_regs) = mkCostCentreStuff cost_centre_info
124 mk_import_register imp =
125 CMacroStmt REGISTER_IMPORT [
126 CLbl (mkModuleInitLabel imp) AddrRep
129 register_imports = map mk_import_register imps
133 CModuleInitBlock (mkModuleInitLabel mod)
134 (mkAbstractCs (register_fes ++
140 Cost-centre profiling: Besides the usual stuff, we must produce
141 declarations for the cost-centres defined in this module;
143 (The local cost-centres involved in this are passed into the
147 mkCostCentreStuff (local_CCs, extern_CCs, singleton_CCSs)
148 | not opt_SccProfilingOn = (AbsCNop, AbsCNop)
151 map (CCostCentreDecl True) local_CCs ++
152 map (CCostCentreDecl False) extern_CCs ++
153 map CCostCentreStackDecl singleton_CCSs),
154 mkAbstractCs (mkCcRegister local_CCs singleton_CCSs)
157 mkCcRegister ccs cc_stacks
159 register_ccs = mkAbstractCs (map mk_register ccs)
160 register_cc_stacks = mkAbstractCs (map mk_register_ccs cc_stacks)
162 [ register_ccs, register_cc_stacks ]
165 = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
168 = CCallProfCCMacro SLIT("REGISTER_CCS") [mkCCostCentreStack ccs]
171 %************************************************************************
173 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
175 %************************************************************************
177 @cgTopBindings@ is only used for top-level bindings, since they need
178 to be allocated statically (not in the heap) and need to be labelled.
179 No unboxed bindings can happen at top level.
181 In the code below, the static bindings are accumulated in the
182 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
183 This is so that we can write the top level processing in a compositional
184 style, with the increasing static environment being plumbed as a state
188 cgTopBindings :: AbstractC -> [(StgBinding,[Id])] -> Code
190 cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
192 cgTopBinding :: AbstractC -> (StgBinding,[Id]) -> Code
194 cgTopBinding split ((StgNonRec name rhs), srt)
196 absC (mkSRT srt_label srt) `thenC`
197 setSRTLabel srt_label (
198 cgTopRhs name rhs `thenFC` \ (name, info) ->
202 srt_label = mkSRTLabel (idName name)
204 cgTopBinding split ((StgRec pairs@((name,rhs):_)), srt)
206 absC (mkSRT srt_label srt) `thenC`
207 setSRTLabel srt_label (
208 fixC (\ new_binds -> addBindsC new_binds `thenC`
209 mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
210 ) `thenFC` \ new_binds ->
214 srt_label = mkSRTLabel (idName name)
216 mkSRT :: CLabel -> [Id] -> AbstractC
217 mkSRT lbl [] = AbsCNop
218 mkSRT lbl ids = CSRT lbl (map (mkClosureLabel . idName) ids)
220 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
221 -- to enclose the listFCs in cgTopBinding, but that tickled the
222 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
224 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
225 -- the Id is passed along for setting up a binding...
227 cgTopRhs bndr (StgRhsCon cc con args)
228 = forkStatics (cgTopRhsCon bndr con args)
230 cgTopRhs bndr (StgRhsClosure cc bi srt fvs upd_flag args body)
231 = ASSERT(null fvs) -- There should be no free variables
232 getSRTLabel `thenFC` \srt_label ->
234 mkClosureLFInfo bndr TopLevel [{-no fvs-}] upd_flag args srt_label srt
236 forkStatics (cgTopRhsClosure bndr cc bi args body lf_info)