- mkCcRegister ccs cc_stacks import_names
- = let
- register_ccs = mkAbstractCs (map mk_register ccs)
- register_imports
- = foldr (mkAbsCStmts . mk_import_register) AbsCNop import_names
- register_cc_stacks = mkAbstractCs (map mk_register_ccs cc_stacks)
- in
- [
- CCallProfCCMacro SLIT("START_REGISTER_CCS")
- [ CLitLit (_PK_ ("_reg" ++ moduleString mod_name)) AddrRep],
- register_ccs,
- register_cc_stacks,
- register_imports,
- CCallProfCCMacro SLIT("END_REGISTER_CCS") []
- ]
- where
- mk_register cc
- = CCallProfCCMacro SLIT("REGISTER_CC") [mkCCostCentre cc]
-
- mk_register_ccs ccs
- = CCallProfCCMacro SLIT("REGISTER_CCS") [mkCCostCentreStack ccs]
-
- mk_import_register import_name
- = CCallProfCCMacro SLIT("REGISTER_IMPORT")
- [CLitLit (_PK_ ("_reg" ++ moduleString import_name)) AddrRep]
+ plain_init_lbl = mkPlainModuleInitLabel hmods this_mod
+ real_init_lbl = mkModuleInitLabel hmods this_mod way
+ plain_main_init_lbl = mkPlainModuleInitLabel hmods rOOT_MAIN
+
+ jump_to_init = stmtC (CmmJump (mkLblExpr real_init_lbl) [])
+
+ mod_reg_val = CmmLoad (mkLblExpr moduleRegdLabel) wordRep
+
+ -- Main refers to GHC.TopHandler.runIO, so make sure we call the
+ -- init function for GHC.TopHandler.
+ extra_imported_mods
+ | this_mod == main_mod = [pREL_TOP_HANDLER]
+ | otherwise = []
+
+ mod_init_code = do
+ { -- Set mod_reg to 1 to record that we've been here
+ stmtC (CmmStore (mkLblExpr moduleRegdLabel) (CmmLit (mkIntCLit 1)))
+
+ -- Now do local stuff
+ ; initCostCentres cost_centre_info
+ ; mapCs (registerModuleImport hmods way)
+ (imported_mods++extra_imported_mods)
+ }
+
+ -- The return-code pops the work stack by
+ -- incrementing Sp, and then jumpd to the popped item
+ ret_code = stmtsC [ CmmAssign spReg (cmmRegOffW spReg 1)
+ , CmmJump (CmmLoad (cmmRegOffW spReg (-1)) wordRep) [] ]
+
+-----------------------
+registerModuleImport :: HomeModules -> String -> Module -> Code
+registerModuleImport hmods way mod
+ | mod == gHC_PRIM
+ = nopC
+ | otherwise -- Push the init procedure onto the work stack
+ = stmtsC [ CmmAssign spReg (cmmRegOffW spReg (-1))
+ , CmmStore (CmmReg spReg) (mkLblExpr (mkModuleInitLabel hmods mod way)) ]
+\end{code}
+
+
+
+Cost-centre profiling: Besides the usual stuff, we must produce
+declarations for the cost-centres defined in this module;
+
+(The local cost-centres involved in this are passed into the
+code-generator.)
+
+\begin{code}
+initCostCentres :: CollectedCCs -> Code
+-- Emit the declarations, and return code to register them
+initCostCentres (local_CCs, ___extern_CCs, singleton_CCSs)
+ | not opt_SccProfilingOn = nopC
+ | otherwise
+ = do { mapM_ emitCostCentreDecl local_CCs
+ ; mapM_ emitCostCentreStackDecl singleton_CCSs
+ ; mapM_ emitRegisterCC local_CCs
+ ; mapM_ emitRegisterCCS singleton_CCSs
+ }