+/* -----------------------------------------------------------------------------
+ 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_BLOCKS 4
+F_ *init_stack = NULL;
+nat init_sp = 0;
+
+static void
+initModules ( void (*init_root)(void) )
+{
+ bdescr *bd;
+#ifdef SMP
+ Capability cap;
+#else
+#define cap MainCapability
+#endif
+
+ init_sp = 0;
+ bd = allocGroup(INIT_STACK_BLOCKS);
+ init_stack = (F_ *)bd->start;
+ 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.r.rSp = (P_)(init_stack + init_sp);
+ StgRun((StgFunPtr)stg_init, &cap.r);
+
+ freeGroup(bd);
+}
+
+/* -----------------------------------------------------------------------------