2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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 #include "HsVersions.h"
20 module CodeGen ( codeGen ) where
28 import AbsCUtils ( mkAbstractCs, mkAbsCStmts )
29 import Bag ( foldBag )
30 import CgBindery ( CgIdInfo )
31 import CgClosure ( cgTopRhsClosure )
32 import CgCon ( cgTopRhsCon )
33 import CgConTbls ( genStaticConBits )
34 import ClosureInfo ( mkClosureLFInfo )
35 import CmdLineOpts ( opt_SccProfilingOn, opt_CompilingGhcInternals,
36 opt_EnsureSplittableC, opt_SccGroup
38 import CostCentre ( CostCentre )
39 import CStrings ( modnameToC )
40 import FiniteMap ( FiniteMap )
41 import Id ( SYN_IE(Id) )
42 import Maybes ( maybeToBool )
43 import Name ( SYN_IE(Module) )
44 import PrimRep ( getPrimRepSize, PrimRep(..) )
45 import Type ( SYN_IE(Type) )
46 import TyCon ( TyCon )
47 import Util ( panic, assertPanic )
51 codeGen :: FAST_STRING -- module name
52 -> ([CostCentre], -- local cost-centres needing declaring/registering
53 [CostCentre]) -- "extern" cost-centres needing declaring
54 -> [Module] -- import names
55 -> [TyCon] -- tycons with data constructors to convert
56 -> FiniteMap TyCon [(Bool, [Maybe Type])]
57 -- tycon specialisation info
58 -> [StgBinding] -- bindings to convert
59 -> AbstractC -- output
61 codeGen mod_name (local_CCs, extern_CCs) import_names gen_tycons tycon_specs stg_pgm
63 doing_profiling = opt_SccProfilingOn
64 compiling_prelude = opt_CompilingGhcInternals
65 maybe_split = if opt_EnsureSplittableC then CSplitMarker else AbsCNop
66 cinfo = MkCompInfo mod_name
68 if not doing_profiling then
70 genStaticConBits cinfo gen_tycons tycon_specs,
71 initC cinfo (cgTopBindings maybe_split stg_pgm) ]
73 else -- yes, 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.)
83 -- Note: we don't register/etc if compiling Prelude bits.
88 else mkAbstractCs [mkAbstractCs (map (CCostCentreDecl True) local_CCs),
89 mkAbstractCs (map (CCostCentreDecl False) extern_CCs),
90 mkCcRegister local_CCs import_names],
92 genStaticConBits cinfo gen_tycons tycon_specs,
93 initC cinfo (cgTopBindings maybe_split stg_pgm) ]
96 grp_name = case opt_SccGroup of
98 Nothing -> mod_name -- default: module name
101 mkCcRegister ccs import_names
103 register_ccs = mkAbstractCs (map mk_register ccs)
105 = foldr (mkAbsCStmts . mk_import_register) AbsCNop import_names
108 CCallProfCCMacro SLIT("START_REGISTER_CCS") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ mod_name)) AddrRep],
111 CCallProfCCMacro SLIT("END_REGISTER_CCS") []
115 = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
117 mk_import_register import_name
118 = CCallProfCCMacro SLIT("REGISTER_IMPORT") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ import_name)) AddrRep]
121 %************************************************************************
123 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
125 %************************************************************************
127 @cgTopBindings@ is only used for top-level bindings, since they need
128 to be allocated statically (not in the heap) and need to be labelled.
129 No unboxed bindings can happen at top level.
131 In the code below, the static bindings are accumulated in the
132 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
133 This is so that we can write the top level processing in a compositional
134 style, with the increasing static environment being plumbed as a state
138 cgTopBindings :: AbstractC -> [StgBinding] -> Code
140 cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
142 cgTopBinding :: AbstractC -> StgBinding -> Code
144 cgTopBinding split (StgNonRec name rhs)
146 cgTopRhs name rhs `thenFC` \ (name, info) ->
149 cgTopBinding split (StgRec pairs)
151 fixC (\ new_binds -> addBindsC new_binds `thenC`
152 mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
153 ) `thenFC` \ new_binds ->
156 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
157 -- to enclose the listFCs in cgTopBinding, but that tickled the
158 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
160 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
161 -- the Id is passed along for setting up a binding...
163 cgTopRhs name (StgRhsCon cc con args)
164 = forkStatics (cgTopRhsCon name con args (all zero_size args))
166 zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
168 cgTopRhs name (StgRhsClosure cc bi fvs upd_flag args body)
169 = ASSERT(null fvs) -- There should be no free variables
170 forkStatics (cgTopRhsClosure name cc bi args body lf_info)
172 lf_info = mkClosureLFInfo True{-top level-} [{-no fvs-}] upd_flag args