2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
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"
23 -- and to make the interface self-sufficient...
24 UniqFM, AbstractC, StgBinding, Id, FiniteMap
32 import CLabelInfo ( modnameToC )
33 import CgClosure ( cgTopRhsClosure )
34 import CgCon ( cgTopRhsCon )
35 import CgConTbls ( genStaticConBits, TCE(..), UniqFM )
36 import ClosureInfo ( LambdaFormInfo, mkClosureLFInfo )
38 import FiniteMap ( FiniteMap )
39 import Maybes ( Maybe(..) )
40 import Pretty -- debugging only
41 import PrimKind ( getKindSize )
46 codeGen :: FAST_STRING -- module name
47 -> ([CostCentre], -- local cost-centres needing declaring/registering
48 [CostCentre]) -- "extern" cost-centres needing declaring
49 -> [FAST_STRING] -- import names
50 -> (GlobalSwitch -> SwitchResult)
51 -- global switch lookup function
52 -> [TyCon] -- tycons with data constructors to convert
53 -> FiniteMap TyCon [[Maybe UniType]]
54 -- tycon specialisation info
55 -> PlainStgProgram -- bindings to convert
56 -> AbstractC -- output
58 codeGen mod_name (local_CCs, extern_CCs) import_names sw_lookup_fn gen_tycons tycon_specs stg_pgm
60 switch_is_on = switchIsOn sw_lookup_fn
61 int_switch_set = intSwitchSet sw_lookup_fn
62 doing_profiling = switch_is_on SccProfilingOn
63 compiling_prelude = switch_is_on CompilingPrelude
64 splitting = switch_is_on (EnsureSplittableC (panic "codeGen:esc"))
66 cinfo = MkCompInfo switch_is_on int_switch_set mod_name
70 pprTrace "codeGen:" (ppCat [
71 (case (switch_is_on StgDoLetNoEscapes) of
72 False -> ppStr "False?"
75 (case (int_switch_set ReturnInRegsThreshold) of
76 Nothing -> ppStr "Nothing!"
77 Just n -> ppCat [ppStr "Just", ppInt n]
79 (case (int_switch_set UnfoldingUseThreshold) of
80 Nothing -> ppStr "Nothing!"
81 Just n -> ppCat [ppStr "Just", ppInt n]
83 (case (int_switch_set UnfoldingCreationThreshold) of
84 Nothing -> ppStr "Nothing!"
85 Just n -> ppCat [ppStr "Just", ppInt n]
89 if not doing_profiling then
91 genStaticConBits cinfo gen_tycons tycon_specs,
92 initC cinfo (cgTopBindings splitting stg_pgm) ]
94 else -- yes, cost-centre profiling:
95 -- Besides the usual stuff, we must produce:
97 -- * Declarations for the cost-centres defined in this module;
98 -- * Code to participate in "registering" all the cost-centres
99 -- in the program (done at startup time when the pgm is run).
101 -- (The local cost-centres involved in this are passed
102 -- into the code-generator, as are the imported-modules' names.)
104 -- Note: we don't register/etc if compiling Prelude bits.
109 else mkAbstractCs [mkAbstractCs (map (CCostCentreDecl True) local_CCs),
110 mkAbstractCs (map (CCostCentreDecl False) extern_CCs),
111 mkCcRegister local_CCs import_names],
113 genStaticConBits cinfo gen_tycons tycon_specs,
114 initC cinfo (cgTopBindings splitting stg_pgm) ]
117 grp_name = case (stringSwitchSet sw_lookup_fn SccGroup) of
119 Nothing -> mod_name -- default: module name
122 mkCcRegister ccs import_names
124 register_ccs = mkAbstractCs (map mk_register ccs)
125 register_imports = mkAbstractCs (map mk_import_register import_names)
128 CCallProfCCMacro SLIT("START_REGISTER_CCS") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ mod_name)) AddrKind],
131 CCallProfCCMacro SLIT("END_REGISTER_CCS") []
135 = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
137 mk_import_register import_name
138 = CCallProfCCMacro SLIT("REGISTER_IMPORT") [CLitLit (modnameToC (SLIT("_reg") _APPEND_ import_name)) AddrKind]
141 %************************************************************************
143 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
145 %************************************************************************
147 @cgTopBindings@ is only used for top-level bindings, since they need
148 to be allocated statically (not in the heap) and need to be labelled.
149 No unboxed bindings can happen at top level.
151 In the code below, the static bindings are accumulated in the
152 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
153 This is so that we can write the top level processing in a compositional
154 style, with the increasing static environment being plumbed as a state
158 cgTopBindings :: Bool -> PlainStgProgram -> Code
160 cgTopBindings splitting bindings = mapCs (cgTopBinding splitting) bindings
162 cgTopBinding :: Bool -> PlainStgBinding -> Code
164 cgTopBinding splitting (StgNonRec name rhs)
165 = absC maybe_split `thenC`
166 cgTopRhs name rhs `thenFC` \ (name, info) ->
169 maybe_split = if splitting then CSplitMarker else AbsCNop
171 cgTopBinding splitting (StgRec pairs)
172 = absC maybe_split `thenC`
173 fixC (\ new_binds -> addBindsC new_binds `thenC`
174 mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs
175 ) `thenFC` \ new_binds ->
178 maybe_split = if splitting then CSplitMarker else AbsCNop
180 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
181 -- to enclose the listFCs in cgTopBinding, but that tickled the
182 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
184 cgTopRhs :: Id -> PlainStgRhs -> FCode (Id, CgIdInfo)
185 -- the Id is passed along for setting up a binding...
187 cgTopRhs name (StgRhsCon cc con args)
188 = forkStatics (cgTopRhsCon name con args (all zero_size args))
190 zero_size atom = getKindSize (getAtomKind atom) == 0
192 cgTopRhs name (StgRhsClosure cc bi fvs upd_flag args body)
193 = ASSERT(null fvs) -- There should be no free variables
194 forkStatics (cgTopRhsClosure name cc bi args body lf_info)
196 lf_info = mkClosureLFInfo True{-top level-} [{-no fvs-}] upd_flag args body