Rune - Further Object abstraction work
[rune.git] / libruntime / rt_main.c
1 /*
2  * MAIN.C
3  */
4
5 #include "defs.h"
6
7 typedef struct {
8     int     ac;
9 #if LONG_BIT == 64
10     int     unused01;
11 #endif
12     char    **av;
13 } args_t;
14
15 typedef struct {
16     int     rcode;
17 } rval_t;
18
19 void    RUNEENTRYABI rune_entry_main(args_t *args, rval_t *rval);
20
21 static void startRuneProgram(void *data);
22
23 int
24 main(int ac, char **av)
25 {
26     args_t  args;
27     rval_t  rval;
28     void   *ary[2];
29     int     fd;
30
31     /*
32      * Rune binary execution
33      */
34     RuneMode = RUNEMODE_BINARY;
35
36     /*
37      * We want a core dump for any dpanic during the execution of a Rune
38      * binary.
39      */
40     if (DebugOpt == 0)
41         DebugOpt = -1;
42
43     /*
44      * Make sure that descriptors 0, 1, and 2 exist.
45      */
46     do {
47         fd = open("/dev/null", O_RDWR);
48     } while (fd >= 0 && fd < 2);
49     if (fd > 2)
50         close(fd);
51
52     /*
53      * General initialization - the runtime only uses libsupport.
54      */
55     LibRuneSupportInit();
56
57     /*
58      * Arguments to main
59      */
60     args.av = av;
61     args.ac = ac;
62     rval.rcode = 0;
63
64     ary[0] = &args;
65     ary[1] = &rval;
66     threadEnvironmentStart(1);
67     startRuneProgram(ary);
68
69     /* exit (even if there are threads present) */
70
71     return rval.rcode;
72 }
73
74 /* __attribute((__section__("runectors"))) void *X; */
75
76 /*
77  * CTORS are added in their own section and must be executed in forward
78  * order.
79  */
80 extern void **start_runectors;
81 extern void **stop_runectors;
82 typedef void RUNEENTRYABI(*ctorfunc_t) (void *dummy);
83
84 static
85 void
86 startRuneProgram(void *data)
87 {
88     void  **ary;
89
90     for (ary = start_runectors; ary < stop_runectors; ++ary) {
91         ((ctorfunc_t) *ary) (NULL);
92     }
93
94     ary = data;
95     rune_entry_main(ary[0], ary[1]);
96     /* rune_entry_main(&args, &rval); */
97     fflush(stdout);
98 #if 0
99     fprintf(stderr, "MAIN EXITED, waiting for threads\n");
100     threadWaitTerminate();
101 #endif
102 }