+ stat_endInit();
+}
+
+/* -----------------------------------------------------------------------------
+ Per-module initialisation
+
+ This process traverses all the compiled modules in the program
+ starting with "Main", and performing per-module initialisation for
+ each one.
+
+ So far, two things happen at initialisation time:
+
+ - we register stable names for each foreign-exported function
+ in that module. This prevents foreign-exported entities, and
+ things they depend on, from being garbage collected.
+
+ - we supply a unique integer to each statically declared cost
+ centre and cost centre stack in the program.
+
+ The code generator inserts a small function "__stginit_<module>" in each
+ module and calls the registration functions in each of the modules it
+ imports. So, if we call "__stginit_PrelMain", each reachable module in the
+ program will be registered (because PrelMain.mainIO calls Main.main).
+
+ The init* functions are compiled in the same way as STG code,
+ i.e. without normal C call/return conventions. Hence we must use
+ StgRun to call this stuff.
+ -------------------------------------------------------------------------- */
+
+/* The init functions use an explicit stack...
+ */
+#define INIT_STACK_SIZE (BLOCK_SIZE * 4)
+F_ *init_stack = NULL;
+nat init_sp = 0;
+
+static void
+initModules ( void (*init_root)(void) )
+{
+#ifdef SMP
+ Capability cap;
+#else
+#define cap MainRegTable
+#endif
+
+ init_sp = 0;
+ init_stack = (F_ *)allocate(INIT_STACK_SIZE / sizeof(W_));
+ init_stack[init_sp++] = (F_)stg_init_ret;
+ init_stack[init_sp++] = (F_)__stginit_Prelude;
+ if (init_root != NULL) {
+ init_stack[init_sp++] = (F_)init_root;
+ }
+
+ cap.rSp = (P_)(init_stack + init_sp);
+ StgRun((StgFunPtr)stg_init, &cap);
+}
+
+/* -----------------------------------------------------------------------------
+ * Shutting down the RTS - two ways of doing this, one which
+ * calls exit(), one that doesn't.
+ *
+ * (shutdownHaskellAndExit() is called by System.exitWith).
+ * -----------------------------------------------------------------------------
+ */
+void
+shutdownHaskellAndExit(int n)
+{
+ OnExitHook();
+ shutdownHaskell();
+#if defined(PAR)
+ /* really exit (stg_exit() would call shutdownParallelSystem() again) */
+ exit(n);
+#else
+ stg_exit(n);
+#endif