-mkCostCentreStuff mod_name import_names (local_CCs, extern_CCs, singleton_CCSs)
- | not opt_SccProfilingOn = AbsCNop
- | otherwise = mkAbstractCs (
- map (CCostCentreDecl True) local_CCs ++
- map (CCostCentreDecl False) extern_CCs ++
- map CCostCentreStackDecl singleton_CCSs ++
- mkCcRegister local_CCs singleton_CCSs import_names
- )
+mkModuleInit
+ :: String -- the "way"
+ -> CollectedCCs -- cost centre info
+ -> Module
+ -> Maybe String -- Just m ==> we have flag: -main-is Foo.baz
+ -> ForeignStubs
+ -> [Module]
+ -> AbstractC
+mkModuleInit way cost_centre_info this_mod mb_main_mod foreign_stubs imported_mods
+ = let
+ (cc_decls, cc_regs) = mkCostCentreStuff cost_centre_info
+
+ register_foreign_exports
+ = case foreign_stubs of
+ NoStubs -> []
+ ForeignStubs _ _ _ fe_bndrs -> map mk_export_register fe_bndrs
+
+ mk_export_register bndr
+ = CMacroStmt REGISTER_FOREIGN_EXPORT [lbl]
+ where
+ lbl = CLbl (mkClosureLabel (idName bndr)) PtrRep
+ -- we don't want/need to init GHC.Prim, so filter it out
+
+ mk_import_register mod
+ | mod == gHC_PRIM = AbsCNop
+ | otherwise = CMacroStmt REGISTER_IMPORT [
+ CLbl (mkModuleInitLabel mod way) AddrRep
+ ]
+
+ register_mod_imports = map mk_import_register imported_mods
+
+ -- When compiling the module in which the 'main' function lives,
+ -- we inject an extra stg_init procedure for stg_init_zdMain, for the
+ -- RTS to invoke. We must consult the -main-is flag in case the
+ -- user specified a different function to Main.main
+ main_mod_name = case mb_main_mod of
+ Just mod_name -> mkModuleName mod_name
+ Nothing -> mAIN_Name
+ main_init_block
+ | Module.moduleName this_mod /= main_mod_name
+ = AbsCNop -- The normal case
+ | otherwise -- this_mod contains the main function
+ = CCodeBlock (mkPlainModuleInitLabel dOLLAR_MAIN)
+ (CJump (CLbl (mkPlainModuleInitLabel this_mod) CodePtrRep))
+
+ in
+ mkAbstractCs [
+ cc_decls,
+ CModuleInitBlock (mkPlainModuleInitLabel this_mod)
+ (mkModuleInitLabel this_mod way)
+ (mkAbstractCs (register_foreign_exports ++
+ cc_regs :
+ register_mod_imports)),
+ main_init_block
+ ]
+\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.)