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"
22 -- Kludge (??) so that CgExpr is reached via at least one non-SOURCE
23 -- import. Before, that wasn't the case, and CM therefore didn't
24 -- bother to compile it.
25 import CgExpr ( {-NOTHING!-} ) -- DO NOT DELETE THIS IMPORT
27 import DriverState ( v_Build_tag )
31 import PrelNames ( gHC_PRIM )
32 import CLabel ( CLabel, mkSRTLabel, mkClosureLabel,
33 mkPlainModuleInitLabel, mkModuleInitLabel )
34 import PprAbsC ( dumpRealC )
35 import AbsCUtils ( mkAbstractCs, flattenAbsC )
36 import CgBindery ( CgIdInfo, addBindC, addBindsC, getCAddrModeAndInfo )
37 import CgClosure ( cgTopRhsClosure )
38 import CgCon ( cgTopRhsCon )
39 import CgConTbls ( genStaticConBits )
40 import ClosureInfo ( mkClosureLFInfo )
41 import CmdLineOpts ( DynFlags, DynFlag(..),
42 opt_SccProfilingOn, opt_EnsureSplittableC )
43 import HscTypes ( ModGuts(..), ModGuts, ForeignStubs(..),
45 import CostCentre ( CollectedCCs )
46 import Id ( Id, idName, setIdName )
47 import Name ( nameSrcLoc, nameOccName, nameUnique, isInternalName, mkExternalName )
48 import OccName ( mkLocalOcc )
49 import PrimRep ( PrimRep(..) )
50 import TyCon ( isDataTyCon )
51 import BasicTypes ( TopLevelFlag(..) )
52 import UniqSupply ( mkSplitUniqSupply )
53 import ErrUtils ( dumpIfSet_dyn, showPass )
54 import Panic ( assertPanic )
60 import DATA_IOREF ( readIORef )
66 -> CollectedCCs -- (Local/global) cost-centres needing declaring/registering.
67 -> [(StgBinding,[Id])] -- Bindings to convert, with SRTs
68 -> IO AbstractC -- Output
71 mod_impl@(ModGuts { mg_module = mod_name, mg_types = type_env })
72 cost_centre_info stg_binds
74 showPass dflags "CodeGen"
75 fl_uniqs <- mkSplitUniqSupply 'f'
76 way <- readIORef v_Build_tag
79 tycons = typeEnvTyCons type_env
80 data_tycons = filter isDataTyCon tycons
81 cinfo = MkCompInfo mod_name
83 datatype_stuff = genStaticConBits cinfo data_tycons
84 code_stuff = initC cinfo (mapCs cgTopBinding stg_binds)
85 init_stuff = mkModuleInit way cost_centre_info mod_impl
87 abstractC = mkAbstractCs [ maybeSplitCode,
91 -- Put datatype_stuff after code_stuff, because the
92 -- datatype closure table (for enumeration types) to
93 -- (say) PrelBase_True_closure, which is defined in
96 dumpIfSet_dyn dflags Opt_D_dump_absC "Abstract C" (dumpRealC abstractC)
98 return $! flattenAbsC fl_uniqs abstractC
101 %************************************************************************
103 \subsection[codegen-init]{Module initialisation code}
105 %************************************************************************
109 :: String -- the "way"
110 -> CollectedCCs -- cost centre info
113 mkModuleInit way cost_centre_info
114 (ModGuts { mg_module = mod,
115 mg_foreign = for_stubs,
116 mg_dir_imps = imported_modules })
118 (cc_decls, cc_regs) = mkCostCentreStuff cost_centre_info
120 register_foreign_exports
123 ForeignStubs _ _ _ fe_bndrs -> map mk_export_register fe_bndrs
125 mk_export_register bndr
126 = CMacroStmt REGISTER_FOREIGN_EXPORT [lbl]
128 lbl = CLbl (mkClosureLabel (idName bndr)) PtrRep
129 -- we don't want/need to init GHC.Prim, so filter it out
131 mk_import_register mod
132 | mod == gHC_PRIM = AbsCNop
133 | otherwise = CMacroStmt REGISTER_IMPORT [
134 CLbl (mkModuleInitLabel mod way) AddrRep
137 register_mod_imports = map mk_import_register imported_modules
141 CModuleInitBlock (mkPlainModuleInitLabel mod)
142 (mkModuleInitLabel mod way)
143 (mkAbstractCs (register_foreign_exports ++
145 register_mod_imports))
149 Cost-centre profiling: Besides the usual stuff, we must produce
150 declarations for the cost-centres defined in this module;
152 (The local cost-centres involved in this are passed into the
156 mkCostCentreStuff (local_CCs, extern_CCs, singleton_CCSs)
157 | not opt_SccProfilingOn = (AbsCNop, AbsCNop)
160 map (CCostCentreDecl True) local_CCs ++
161 map (CCostCentreDecl False) extern_CCs ++
162 map CCostCentreStackDecl singleton_CCSs),
163 mkAbstractCs (mkCcRegister local_CCs singleton_CCSs)
166 mkCcRegister ccs cc_stacks
168 register_ccs = mkAbstractCs (map mk_register ccs)
169 register_cc_stacks = mkAbstractCs (map mk_register_ccs cc_stacks)
171 [ register_ccs, register_cc_stacks ]
174 = CCallProfCCMacro FSLIT("REGISTER_CC") [mkCCostCentre cc]
177 = CCallProfCCMacro FSLIT("REGISTER_CCS") [mkCCostCentreStack ccs]
180 %************************************************************************
182 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
184 %************************************************************************
186 @cgTopBinding@ is only used for top-level bindings, since they need
187 to be allocated statically (not in the heap) and need to be labelled.
188 No unboxed bindings can happen at top level.
190 In the code below, the static bindings are accumulated in the
191 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
192 This is so that we can write the top level processing in a compositional
193 style, with the increasing static environment being plumbed as a state
197 cgTopBinding :: (StgBinding,[Id]) -> Code
198 cgTopBinding (StgNonRec srt_info id rhs, srt)
199 = absC maybeSplitCode `thenC`
200 maybeExternaliseId id `thenFC` \ id' ->
202 srt_label = mkSRTLabel (idName id')
204 mkSRT srt_label srt [] `thenC`
205 setSRTLabel srt_label (
206 cgTopRhs id' rhs srt_info `thenFC` \ (id, info) ->
207 addBindC id info -- Add the un-externalised Id to the envt, so we
208 -- find it when we look up occurrences
211 cgTopBinding (StgRec srt_info pairs, srt)
212 = absC maybeSplitCode `thenC`
214 (bndrs, rhss) = unzip pairs
216 mapFCs maybeExternaliseId bndrs `thenFC` \ bndrs'@(id:_) ->
218 srt_label = mkSRTLabel (idName id)
219 pairs' = zip bndrs' rhss
221 mkSRT srt_label srt bndrs' `thenC`
222 setSRTLabel srt_label (
224 addBindsC new_binds `thenC`
225 mapFCs ( \ (b,e) -> cgTopRhs b e srt_info ) pairs'
226 ) `thenFC` \ new_binds -> nopC
229 mkSRT :: CLabel -> [Id] -> [Id] -> Code
230 mkSRT lbl [] these = nopC
232 = mapFCs remap ids `thenFC` \ ids ->
233 absC (CSRT lbl (map (mkClosureLabel . idName) ids))
235 -- sigh, better map all the ids against the environment in case they've
236 -- been externalised (see maybeExternaliseId below).
237 remap id = case filter (==id) these of
238 [] -> getCAddrModeAndInfo id
239 `thenFC` \ (id, _, _) -> returnFC id
240 (id':_) -> returnFC id'
242 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
243 -- to enclose the listFCs in cgTopBinding, but that tickled the
244 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
246 cgTopRhs :: Id -> StgRhs -> SRT -> FCode (Id, CgIdInfo)
247 -- The Id is passed along for setting up a binding...
248 -- It's already been externalised if necessary
250 cgTopRhs bndr (StgRhsCon cc con args) srt
251 = forkStatics (cgTopRhsCon bndr con args srt)
253 cgTopRhs bndr (StgRhsClosure cc bi fvs upd_flag args body) srt
254 = ASSERT(null fvs) -- There should be no free variables
256 lf_info = mkClosureLFInfo bndr TopLevel [{-no fvs-}] upd_flag args
258 forkStatics (cgTopRhsClosure bndr cc bi srt args body lf_info)
262 %************************************************************************
264 \subsection{Stuff to support splitting}
266 %************************************************************************
268 If we're splitting the object, we need to externalise all the top-level names
269 (and then make sure we only use the externalised one in any C label we use
270 which refers to this name).
273 maybeExternaliseId :: Id -> FCode Id
274 maybeExternaliseId id
275 | opt_EnsureSplittableC, -- Externalise the name for -split-objs
277 = moduleName `thenFC` \ mod ->
278 returnFC (setIdName id (mkExternalName uniq mod new_occ (nameSrcLoc name)))
283 uniq = nameUnique name
284 new_occ = mkLocalOcc uniq (nameOccName name)
285 -- We want to conjure up a name that can't clash with any
286 -- existing name. So we generate
288 -- where 243 is the unique.
291 | opt_EnsureSplittableC = CSplitMarker
292 | otherwise = AbsCNop