Introduce G structure and thread-local global g.
[gcc.git] / libgo / runtime / go-main.c
1 /* go-main.c -- the main function for a Go program.
2
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
6
7 #include "config.h"
8
9 #include <stdlib.h>
10 #include <time.h>
11
12 #ifdef HAVE_FPU_CONTROL_H
13 #include <fpu_control.h>
14 #endif
15
16 #include "go-alloc.h"
17 #include "array.h"
18 #include "go-signal.h"
19 #include "go-string.h"
20
21 #include "runtime.h"
22 #include "arch.h"
23 #include "malloc.h"
24
25 #undef int
26 #undef char
27 #undef unsigned
28
29 /* The main function for a Go program. This records the command line
30 parameters, calls the real main function, and returns a zero status
31 if the real main function returns. */
32
33 extern char **environ;
34
35 extern struct __go_open_array Args asm ("libgo_os.os.Args");
36
37 extern struct __go_open_array Envs asm ("libgo_os.os.Envs");
38
39 /* These functions are created for the main package. */
40 extern void __go_init_main (void);
41 extern void real_main (void) asm ("main.main");
42
43 /* The main function. */
44
45 int
46 main (int argc, char **argv)
47 {
48 int i;
49 struct __go_string *values;
50
51 m = &runtime_m0;
52 g = &runtime_g0;
53 m->curg = g;
54 g->m = m;
55 runtime_mallocinit ();
56 runtime_cpuprofinit ();
57 __go_gc_goroutine_init (&argc);
58
59 Args.__count = argc;
60 Args.__capacity = argc;
61 values = __go_alloc (argc * sizeof (struct __go_string));
62 for (i = 0; i < argc; ++i)
63 {
64 values[i].__data = (unsigned char *) argv[i];
65 values[i].__length = __builtin_strlen (argv[i]);
66 }
67 Args.__values = values;
68
69 for (i = 0; environ[i] != NULL; ++i)
70 ;
71 Envs.__count = i;
72 Envs.__capacity = i;
73 values = __go_alloc (i * sizeof (struct __go_string));
74 for (i = 0; environ[i] != NULL; ++i)
75 {
76 values[i].__data = (unsigned char *) environ[i];
77 values[i].__length = __builtin_strlen (environ[i]);
78 }
79 Envs.__values = values;
80
81 __initsig ();
82
83 #if defined(HAVE_SRANDOM)
84 srandom ((unsigned int) time (NULL));
85 #else
86 srand ((unsigned int) time (NULL));
87 #endif
88 __go_init_main ();
89
90 __go_enable_gc ();
91
92 real_main ();
93
94 return 0;
95 }