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 CgClosure ( cgTopRhsClosure )
31 import CgCon ( cgTopRhsCon )
32 import CgConTbls ( genStaticConBits )
33 import ClosureInfo ( mkClosureLFInfo )
34 import CmdLineOpts ( opt_SccProfilingOn, opt_CompilingPrelude,
35 opt_EnsureSplittableC, opt_SccGroup
37 import CStrings ( modnameToC )
38 import Maybes ( maybeToBool )
39 import PrimRep ( getPrimRepSize, PrimRep(..) )
40 import Util ( panic, assertPanic )
44 codeGen :: FAST_STRING -- module name
45 -> ([CostCentre], -- local cost-centres needing declaring/registering
46 [CostCentre]) -- "extern" cost-centres needing declaring
47 -> [Module] -- import names
48 -> [TyCon] -- tycons with data constructors to convert
49 -> FiniteMap TyCon [(Bool, [Maybe Type])]
50 -- tycon specialisation info
51 -> [StgBinding] -- bindings to convert
52 -> AbstractC -- output
54 codeGen mod_name (local_CCs, extern_CCs) import_names gen_tycons tycon_specs stg_pgm
56 doing_profiling = opt_SccProfilingOn
57 compiling_prelude = opt_CompilingPrelude
58 maybe_split = if maybeToBool (opt_EnsureSplittableC)
62 cinfo = MkCompInfo mod_name
64 if not doing_profiling then
66 genStaticConBits cinfo gen_tycons tycon_specs,
67 initC cinfo (cgTopBindings maybe_split stg_pgm) ]
69 else -- yes, cost-centre profiling:
70 -- Besides the usual stuff, we must produce:
72 -- * Declarations for the cost-centres defined in this module;
73 -- * Code to participate in "registering" all the cost-centres
74 -- in the program (done at startup time when the pgm is run).
76 -- (The local cost-centres involved in this are passed
77 -- into the code-generator, as are the imported-modules' names.)
79 -- Note: we don't register/etc if compiling Prelude bits.
84 else mkAbstractCs [mkAbstractCs (map (CCostCentreDecl True) local_CCs),
85 mkAbstractCs (map (CCostCentreDecl False) extern_CCs),
86 mkCcRegister local_CCs import_names],
88 genStaticConBits cinfo gen_tycons tycon_specs,
89 initC cinfo (cgTopBindings maybe_split stg_pgm) ]
92 grp_name = case opt_SccGroup of
94 Nothing -> mod_name -- default: module name
97 mkCcRegister ccs import_names
99 register_ccs = mkAbstractCs (map mk_register ccs)
101 = foldr (mkAbsCStmts . mk_import_register) AbsCNop import_names
104 CCallProfCCMacro SLIT("START_REGISTER_CCS") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ mod_name)) AddrRep],
107 CCallProfCCMacro SLIT("END_REGISTER_CCS") []
111 = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
113 mk_import_register import_name
114 = CCallProfCCMacro SLIT("REGISTER_IMPORT") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ import_name)) AddrRep]
117 %************************************************************************
119 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
121 %************************************************************************
123 @cgTopBindings@ is only used for top-level bindings, since they need
124 to be allocated statically (not in the heap) and need to be labelled.
125 No unboxed bindings can happen at top level.
127 In the code below, the static bindings are accumulated in the
128 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
129 This is so that we can write the top level processing in a compositional
130 style, with the increasing static environment being plumbed as a state
134 cgTopBindings :: AbstractC -> [StgBinding] -> Code
136 cgTopBindings split bindings = mapCs (cgTopBinding split) bindings
138 cgTopBinding :: AbstractC -> StgBinding -> Code
140 cgTopBinding split (StgNonRec name rhs)
142 cgTopRhs name rhs `thenFC` \ (name, info) ->
145 cgTopBinding split (StgRec pairs)
147 fixC (\ new_binds -> addBindsC new_binds `thenC`
148 mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
149 ) `thenFC` \ new_binds ->
152 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
153 -- to enclose the listFCs in cgTopBinding, but that tickled the
154 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
156 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
157 -- the Id is passed along for setting up a binding...
159 cgTopRhs name (StgRhsCon cc con args)
160 = forkStatics (cgTopRhsCon name con args (all zero_size args))
162 zero_size atom = getPrimRepSize (getArgPrimRep atom) == 0
164 cgTopRhs name (StgRhsClosure cc bi fvs upd_flag args body)
165 = ASSERT(null fvs) -- There should be no free variables
166 forkStatics (cgTopRhsClosure name cc bi args body lf_info)
168 lf_info = mkClosureLFInfo True{-top level-} [{-no fvs-}] upd_flag args body