- :: [Id] -- foreign exported functions
- -> Module -- module name
- -> [Module] -- import names
- -> ([CostCentre], -- cost centre info
- [CostCentre],
- [CostCentreStack])
- -> AbstractC
-mkModuleInit fe_binders mod imps cost_centre_info
- = let
- register_fes =
- map (\f -> CMacroStmt REGISTER_FOREIGN_EXPORT [f]) fe_labels
-
- fe_labels =
- map (\f -> CLbl (mkClosureLabel (idName f)) PtrRep) fe_binders
-
- (cc_decls, cc_regs) = mkCostCentreStuff cost_centre_info
-
- mk_import_register imp =
- CMacroStmt REGISTER_IMPORT [
- CLbl (mkModuleInitLabel imp) AddrRep
- ]
-
- register_imports = map mk_import_register imps
- in
- mkAbstractCs [
- cc_decls,
- CModuleInitBlock (mkModuleInitLabel mod)
- (mkAbstractCs (register_fes ++
- cc_regs :
- register_imports))
- ]
+ :: String -- the "way"
+ -> CollectedCCs -- cost centre info
+ -> Module
+ -> Maybe String -- Just m ==> we have flag: -main-is Foo.baz
+ -> ForeignStubs
+ -> [Module]
+ -> Code
+mkModuleInit way cost_centre_info this_mod mb_main_mod foreign_stubs imported_mods
+ = do {
+
+ -- Allocate the static boolean that records if this
+ -- module has been registered already
+ ; emitData Data [CmmDataLabel moduleRegdLabel,
+ CmmStaticLit zeroCLit]
+
+ ; emitSimpleProc real_init_lbl $ do
+ { -- The return-code pops the work stack by
+ -- incrementing Sp, and then jumpd to the popped item
+ ret_blk <- forkLabelledCode $ stmtsC
+ [ CmmAssign spReg (cmmRegOffW spReg 1)
+ , CmmJump (CmmLoad (cmmRegOffW spReg (-1)) wordRep) [] ]
+
+ ; init_blk <- forkLabelledCode $ do
+ { mod_init_code; stmtC (CmmBranch ret_blk) }
+
+ ; stmtC (CmmCondBranch (cmmNeWord (CmmLit zeroCLit) mod_reg_val)
+ ret_blk)
+ ; stmtC (CmmBranch init_blk)
+ }
+
+
+ -- Make the "plain" procedure jump to the "real" init procedure
+ ; emitSimpleProc plain_init_lbl jump_to_init
+
+ -- When compiling the module in which the 'main' function lives,
+ -- (that is, Module.moduleName this_mod == main_mod_name)
+ -- we inject an extra stg_init procedure for stg_init_ZCMain, for the
+ -- RTS to invoke. We must consult the -main-is flag in case the
+ -- user specified a different function to Main.main
+ ; whenC (Module.moduleName this_mod == main_mod_name)
+ (emitSimpleProc plain_main_init_lbl jump_to_init)
+ }
+ where
+ plain_init_lbl = mkPlainModuleInitLabel this_mod
+ real_init_lbl = mkModuleInitLabel this_mod way
+ plain_main_init_lbl = mkPlainModuleInitLabel rOOT_MAIN
+
+ jump_to_init = stmtC (CmmJump (mkLblExpr real_init_lbl) [])
+
+ mod_reg_val = CmmLoad (mkLblExpr moduleRegdLabel) wordRep
+
+ main_mod_name = case mb_main_mod of
+ Just mod_name -> mkModuleName mod_name
+ Nothing -> mAIN_Name
+
+ -- Main refers to GHC.TopHandler.runIO, so make sure we call the
+ -- init function for GHC.TopHandler.
+ extra_imported_mods
+ | Module.moduleName this_mod == main_mod_name = [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
+ ; registerForeignExports foreign_stubs
+ ; initCostCentres cost_centre_info
+ ; mapCs (registerModuleImport way) (imported_mods++extra_imported_mods)
+ }
+
+
+-----------------------
+registerModuleImport :: String -> Module -> Code
+registerModuleImport 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 mod way)) ]
+
+-----------------------
+registerForeignExports :: ForeignStubs -> Code
+registerForeignExports NoStubs
+ = nopC
+registerForeignExports (ForeignStubs _ _ _ fe_bndrs)
+ = mapM_ mk_export_register fe_bndrs
+ where
+ mk_export_register bndr
+ = emitRtsCall SLIT("getStablePtr")
+ [ (CmmLit (CmmLabel (mkClosureLabel (idName bndr))), PtrHint) ]