clib: prefix runtime errors with "Runtime error"
[nit.git] / clib / nit_main.c
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2 *
3 * Copyright 2006-2009 Jean Privat <jean@pryen.org>
4 *
5 * This file is free software, which comes along with NIT. This software is
6 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 * is kept unaltered, and a notification of the changes is added.
10 * You are allowed to redistribute it and sell it, alone or is a part of
11 * another product.
12 */
13
14 #include "nit_common.h"
15 #include <signal.h>
16 #include <stdarg.h>
17 #include "gc.h"
18
19 bigint object_id_counter = 1000000;
20 enum gc_option { large, gc_opt_malloc, boehm, nitgc } gc_option;
21
22 #ifdef WITH_LIBGC
23 #define GC_DEBUG
24 #include <gc/gc.h>
25 #endif
26
27 void *raw_alloc(size_t s0)
28 {
29 switch (gc_option) {
30 case nitgc: return malloc(s0);
31 case gc_opt_malloc: return malloc(s0);
32 default: return alloc(s0);
33 }
34 }
35
36 void register_static_object(val_t *o)
37 {
38 switch (gc_option) {
39 case nitgc: GC_add_static_object(o); break;
40 default: break;
41 }
42 return;
43 }
44
45 void *large_alloc(size_t s0)
46 {
47 static char * alloc_pos = NULL;
48 static size_t alloc_size = 0;
49 void * res;
50 size_t s = ((s0+3)/4)*4;
51 if(alloc_size < s) {
52 alloc_size = s + 1024*1024;
53 alloc_pos = (char *)calloc(alloc_size, 1);
54 }
55 res = alloc_pos;
56 alloc_size -= s;
57 alloc_pos += s;
58 return res;
59 }
60
61 void * alloc(size_t s0)
62 {
63 switch (gc_option) {
64 #ifdef WITH_LIBGC
65 case boehm: return GC_MALLOC(s0);
66 #endif
67 case nitgc: return Nit_gc_malloc(s0);
68 case gc_opt_malloc: return calloc(1, s0);
69 case large:
70 default: return large_alloc(s0);
71 }
72 }
73
74 int glob_argc;
75 char **glob_argv;
76 val_t G_sys;
77
78 void exithandler(int s) {
79 fprintf(stderr, "Recieved signal %d\n", s);
80 nit_exit(1);
81 }
82 void initialize_gc_option(void) {
83 /* GC default */
84 char *def;
85 gc_option = nitgc;
86 def = "nitgc";
87
88 /* Process GC runtime selection */
89 if (getenv("NIT_GC_OPTION") != NULL) {
90 char *opt=getenv("NIT_GC_OPTION");
91 if (strcmp(opt, "boehm")==0) {
92 #ifdef WITH_LIBGC
93 gc_option = boehm;
94 #else
95 fprintf(stderr, "Compiled without Boehm GC support. Using default '%s'.\n", def);
96 #endif
97 } else if (strcmp(opt, "nitgc")==0) {
98 gc_option = nitgc;
99 } else if (strcmp(opt, "malloc")==0) {
100 gc_option = gc_opt_malloc;
101 } else if (strcmp(opt, "large")==0) {
102 gc_option = large;
103 } else if (strcmp(opt, "help")==0) {
104 fprintf(stderr, "NIT_GC_OPTION accepts 'nitgc'"
105 #ifdef WITH_LIBGC
106 ", 'boehm'"
107 #endif
108 ", 'large', 'malloc'. Default is '%s'.\n", def);
109 exit(1);
110 } else {
111 fprintf(stderr, "Invalid GC option in NIT_GC_OPTION environment variable. Using default '%s'.\n", def);
112 }
113 }
114
115 /* Initialize GC (if needed) */
116 switch(gc_option) {
117 #ifdef WITH_LIBGC
118 case boehm: GC_INIT(); break;
119 #endif
120 case nitgc: Nit_gc_init(); break;
121 default: break; /* Nothing */
122 }
123
124 /* Initialize global references list */
125 nitni_global_ref_list_init();
126 }
127 void prepare_signals(void) {
128 initialize_gc_option();
129
130 signal(SIGINT,exithandler);
131 signal(SIGABRT,exithandler);
132 signal(SIGSEGV,exithandler);
133 signal(SIGILL, exithandler);
134 signal(SIGFPE, exithandler);
135 signal(SIGTERM,exithandler);
136 signal(SIGBUS, exithandler);
137 }
138 struct stack_frame_t *stack_frame_head = NULL;
139 void nit_exit(int i) {
140 char *opt=getenv("NIT_NO_STACK");
141 if (opt == NULL || strcmp(opt, "0")==0) {
142 fprintf(stderr, ",---- Stack trace -- - - -\n");
143 while(stack_frame_head != NULL) {
144 fprintf(stderr, "| %s (%s:%d)\n", stack_frame_head->meth, stack_frame_head->file, stack_frame_head->line);
145 if (stack_frame_head == stack_frame_head->prev) break;
146 stack_frame_head = stack_frame_head->prev;
147 }
148 fprintf(stderr, "`------------------- - - -\n");
149 }
150 exit(i);
151 }
152
153 void nit_abort(const char* s, const char* msg, const char* loc, int line) {
154 fprintf(stderr, "Runtime error: ");
155 fprintf(stderr, s, msg);
156 fprintf(stderr, " (%s", loc);
157 if (line != 0) fprintf(stderr, ":%d", line);
158 fprintf(stderr, ")\n");
159 nit_exit(1);
160 }
161
162 /* Register reference to Nit object with the latest extern method called. */
163 void nitni_local_ref_add( struct nitni_ref *ref ) {
164 struct nitni_ref_array_link **link_p;
165 struct nitni_ref_array_link * link = NULL;
166
167 /* find usable link or link to create */
168 link_p = &( stack_frame_head->nitni_local_ref_head );
169 while ( (*link_p) != NULL &&
170 (*link_p)->count >= NITNI_REF_ARRAY_LINK_SIZE ) {
171 link_p = &((*link_p)->next);
172 }
173
174 /* create link if needed */
175 if ( *link_p == NULL ) {
176 link = malloc( sizeof(struct nitni_ref_array_link) );
177 link->count = 0;
178 link->next = NULL;
179
180 (*link_p) = link;
181 } else {
182 link = *link_p;
183 }
184
185 /* add to link */
186 link->reg[ link->count ] = ref;
187 link->count ++;
188 }
189
190 /* Clean all references associated to the current (but returning) extern method. */
191 void nitni_local_ref_clean( void ) {
192 struct nitni_ref_array_link * link,
193 * last_link;
194 int i;
195
196 link = stack_frame_head->nitni_local_ref_head;
197 while ( link != NULL )
198 {
199 for ( i = 0; i < link->count; i ++ ) {
200 if ( link->reg[i]->count == 0 ) { /* not registered globally */
201 free( link->reg[ i ] );
202 }
203 }
204
205 last_link = link;
206 if ( link->count == NITNI_REF_ARRAY_LINK_SIZE )
207 link = link->next;
208 else
209 link = NULL;
210
211 free(last_link);
212 }
213
214 stack_frame_head->nitni_local_ref_head = NULL;
215 }
216
217 struct nitni_global_ref_list_t *nitni_global_ref_list;
218 void nitni_global_ref_list_init() {
219 nitni_global_ref_list = (struct nitni_global_ref_list_t*)malloc(sizeof(struct nitni_global_ref_list_t));
220 nitni_global_ref_list->head = NULL;
221 nitni_global_ref_list->tail = NULL;
222 }
223
224 void nitni_global_ref_add( struct nitni_ref *ref ) {
225 if ( nitni_global_ref_list->head == NULL ) {
226 nitni_global_ref_list->head = ref;
227 nitni_global_ref_list->tail = ref;
228
229 ref->prev = NULL;
230 } else {
231 nitni_global_ref_list->tail->next = ref;
232 ref->prev = nitni_global_ref_list->tail;
233 }
234
235 ref->next = NULL;
236 }
237
238 void nitni_global_ref_remove( struct nitni_ref *ref ) {
239 if ( ref->prev == NULL ) {
240 nitni_global_ref_list->head = ref->next;
241 } else {
242 ref->prev->next = ref->next;
243 }
244
245 if ( ref->next == NULL ) {
246 nitni_global_ref_list->tail = ref->prev;
247 } else {
248 ref->next->prev = ref->prev;
249 }
250 }
251
252 extern void nitni_global_ref_incr( struct nitni_ref *ref ) {
253 if ( ref->count == 0 ) /* not registered */
254 {
255 /* add to list */
256 nitni_global_ref_add( ref );
257 }
258
259 ref->count ++;
260 }
261
262 extern void nitni_global_ref_decr( struct nitni_ref *ref ) {
263 if ( ref->count == 1 ) /* was last reference */
264 {
265 /* remove from list */
266 nitni_global_ref_remove( ref );
267 }
268
269 ref->count --;
270 }