1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2000
5 * Main function for a standalone Haskell program.
7 * ---------------------------------------------------------------------------*/
9 #define COMPILING_RTS_MAIN
11 #include "PosixSource.h"
20 #if defined(mingw32_HOST_OS)
21 #include "win32/seh_excn.h"
26 # include "Printer.h" /* for printing */
33 extern void __stginit_ZCMain(void);
35 /* Annoying global vars for passing parameters to real_main() below
36 * This is to get around problem with Windows SEH, see hs_main(). */
38 static char **progargv;
39 static void (*progmain_init)(void); /* This will be __stginit_ZCMain */
40 static StgClosure *progmain_closure; /* This will be ZCMain_main_closure */
42 /* Hack: we assume that we're building a batch-mode system unless
45 #ifndef INTERPRETER /* Hack */
46 static void real_main(void)
49 SchedulerStatus status;
50 /* all GranSim/GUM init is done in startupHaskell; sets IAmMainThread! */
52 startupHaskell(progargc,progargv,progmain_init);
54 /* kick off the computation by creating the main thread with a pointer
55 to mainIO_closure representing the computation of the overall program;
56 then enter the scheduler with this thread and off we go;
58 the same for GranSim (we have only one instance of this code)
60 in a parallel setup, where we have many instances of this code
61 running on different PEs, we should do this only for the main PE
62 (IAmMainThread is set in startupHaskell)
65 /* ToDo: want to start with a larger stack size */
67 Capability *cap = rts_lock();
68 cap = rts_evalLazyIO(cap,progmain_closure, NULL);
69 status = rts_getSchedStatus(cap);
70 taskTimeStamp(myTask());
74 /* check the status of the entire Haskell computation */
77 errorBelch("main thread exited (uncaught exception)");
78 exit_status = EXIT_KILLED;
81 errorBelch("interrupted");
82 exit_status = EXIT_INTERRUPTED;
85 exit_status = EXIT_HEAPOVERFLOW;
88 exit_status = EXIT_SUCCESS;
91 barf("main thread completed with invalid status");
93 shutdownHaskellAndExit(exit_status);
96 /* The rts entry point from a compiled program using a Haskell main function.
97 * This gets called from a tiny main function which gets linked into each
98 * compiled Haskell program that uses a Haskell main function.
100 * We expect the caller to pass __stginit_ZCMain for main_init and
101 * ZCMain_main_closure for main_closure. The reason we cannot refer to
102 * these symbols directly is because we're inside the rts and we do not know
103 * for sure that we'll be using a Haskell main function.
105 int hs_main(int argc, char *argv[], void (*main_init)(void), StgClosure *main_closure)
107 /* We do this dance with argc and argv as otherwise the SEH exception
108 stuff (the BEGIN/END CATCH below) on Windows gets confused */
111 progmain_init = main_init;
112 progmain_closure = main_closure;
114 #if defined(mingw32_HOST_OS)
118 #if defined(mingw32_HOST_OS)
121 return 0; /* not reached, but keeps gcc -Wall happy */
123 # endif /* BATCH_MODE */