2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 This module says how things get going at the top level.
10 @codeGen@ is the interface to the outside world. The \tr{cgTop*}
11 functions drive the mangling of top-level bindings.
14 module CodeGen ( codeGen ) where
16 #include "HsVersions.h"
18 -- Kludge (??) so that CgExpr is reached via at least one non-SOURCE
19 -- import. Before, that wasn't the case, and CM therefore didn't
20 -- bother to compile it.
21 import CgExpr ( {-NOTHING!-} ) -- DO NOT DELETE THIS IMPORT
53 -> CollectedCCs -- (Local/global) cost-centres needing declaring/registering.
54 -> [(StgBinding,[(Id,[Id])])] -- Bindings to convert, with SRTs
58 -- N.B. returning '[Cmm]' and not 'Cmm' here makes it
59 -- possible for object splitting to split up the
62 codeGen dflags this_mod data_tycons cost_centre_info stg_binds hpc_info
64 { showPass dflags "CodeGen"
67 -- ; mapM_ (\x -> seq x (return ())) data_tycons
69 ; code_stuff <- initC dflags this_mod $ do
70 { cmm_binds <- mapM (getCmm . cgTopBinding dflags) stg_binds
71 ; cmm_tycons <- mapM cgTyCon data_tycons
72 ; cmm_init <- getCmm (mkModuleInit dflags cost_centre_info
74 ; return (cmm_init : cmm_binds ++ concat cmm_tycons)
76 -- Put datatype_stuff after code_stuff, because the
77 -- datatype closure table (for enumeration types) to
78 -- (say) PrelBase_True_closure, which is defined in
81 -- Note [codegen-split-init] the cmm_init block must
82 -- come FIRST. This is because when -split-objs is on
83 -- we need to combine this block with its
84 -- initialisation routines; see Note
85 -- [pipeline-split-init].
87 ; dumpIfSet_dyn dflags Opt_D_dump_cmm "Cmm" (pprCmms code_stuff)
93 -> CollectedCCs -- cost centre info
98 mkModuleInit dflags cost_centre_info this_mod hpc_info
99 = do { -- Allocate the static boolean that records if this
101 hpcTable this_mod hpc_info
103 ; whenC (opt_SccProfilingOn) $ do
104 initCostCentres cost_centre_info
106 -- For backwards compatibility: user code may refer to this
107 -- label for calling hs_add_root().
108 ; emitData Data $ [ CmmDataLabel (mkPlainModuleInitLabel this_mod) ]
110 ; whenC (this_mod == mainModIs dflags) $
111 emitSimpleProc (mkPlainModuleInitLabel rOOT_MAIN) $ return ()
117 Cost-centre profiling: Besides the usual stuff, we must produce
118 declarations for the cost-centres defined in this module;
120 (The local cost-centres involved in this are passed into the
124 initCostCentres :: CollectedCCs -> Code
125 -- Emit the declarations, and return code to register them
126 initCostCentres (local_CCs, ___extern_CCs, singleton_CCSs)
127 | not opt_SccProfilingOn = nopC
129 = do { mapM_ emitCostCentreDecl local_CCs
130 ; mapM_ emitCostCentreStackDecl singleton_CCSs
134 %************************************************************************
136 \subsection[codegen-top-bindings]{Converting top-level STG bindings}
138 %************************************************************************
140 @cgTopBinding@ is only used for top-level bindings, since they need
141 to be allocated statically (not in the heap) and need to be labelled.
142 No unboxed bindings can happen at top level.
144 In the code below, the static bindings are accumulated in the
145 @MkCgState@, and transferred into the ``statics'' slot by @forkStatics@.
146 This is so that we can write the top level processing in a compositional
147 style, with the increasing static environment being plumbed as a state
151 cgTopBinding :: DynFlags -> (StgBinding,[(Id,[Id])]) -> Code
152 cgTopBinding dflags (StgNonRec id rhs, srts)
153 = do { id' <- maybeExternaliseId dflags id
154 ; mapM_ (mkSRT [id']) srts
155 ; (id,info) <- cgTopRhs id' rhs
156 ; addBindC id info -- Add the *un-externalised* Id to the envt,
157 -- so we find it when we look up occurrences
160 cgTopBinding dflags (StgRec pairs, srts)
161 = do { let (bndrs, rhss) = unzip pairs
162 ; bndrs' <- mapFCs (maybeExternaliseId dflags) bndrs
163 ; let pairs' = zip bndrs' rhss
164 ; mapM_ (mkSRT bndrs') srts
165 ; _new_binds <- fixC (\ new_binds -> do
166 { addBindsC new_binds
167 ; mapFCs ( \ (b,e) -> cgTopRhs b e ) pairs' })
170 mkSRT :: [Id] -> (Id,[Id]) -> Code
171 mkSRT _ (_,[]) = nopC
173 = do { ids <- mapFCs remap ids
175 ; emitRODataLits "CodeGen.mkSRT" (mkSRTLabel (idName id) (idCafInfo id))
176 (map (\id -> CmmLabel $ mkClosureLabel (idName id) (idCafInfo id)) ids)
179 -- Sigh, better map all the ids against the environment in
180 -- case they've been externalised (see maybeExternaliseId below).
181 remap id = case filter (==id) these of
182 (id':_) -> returnFC id'
183 [] -> do { info <- getCgIdInfo id; return (cgIdInfoId info) }
185 -- Urgh! I tried moving the forkStatics call from the rhss of cgTopRhs
186 -- to enclose the listFCs in cgTopBinding, but that tickled the
187 -- statics "error" call in initC. I DON'T UNDERSTAND WHY!
189 cgTopRhs :: Id -> StgRhs -> FCode (Id, CgIdInfo)
190 -- The Id is passed along for setting up a binding...
191 -- It's already been externalised if necessary
193 cgTopRhs bndr (StgRhsCon _cc con args)
194 = forkStatics (cgTopRhsCon bndr con args)
196 cgTopRhs bndr (StgRhsClosure cc bi fvs upd_flag srt args body)
197 = ASSERT(null fvs) -- There should be no free variables
198 setSRTLabel (mkSRTLabel (idName bndr) (idCafInfo bndr)) $
200 forkStatics (cgTopRhsClosure bndr cc bi upd_flag args body)
204 %************************************************************************
206 \subsection{Stuff to support splitting}
208 %************************************************************************
210 If we're splitting the object, we need to externalise all the top-level names
211 (and then make sure we only use the externalised one in any C label we use
212 which refers to this name).
215 maybeExternaliseId :: DynFlags -> Id -> FCode Id
216 maybeExternaliseId dflags id
217 | dopt Opt_SplitObjs dflags, -- Externalise the name for -split-objs
218 isInternalName name = do { mod <- getModuleName
219 ; returnFC (setIdName id (externalise mod)) }
220 | otherwise = returnFC id
222 externalise mod = mkExternalName uniq mod new_occ loc
224 uniq = nameUnique name
225 new_occ = mkLocalOcc uniq (nameOccName name)
226 loc = nameSrcSpan name
227 -- We want to conjure up a name that can't clash with any
228 -- existing name. So we generate
230 -- where 243 is the unique.