History

added computed gotos - better runtime error handling

bellard committed 24 years ago

Commit 804d0c81 parent fde824b1 file · +179 −65Browse files

Unified · Split

tcc.c+179−65
@@ -190,7 +190,7 @@ typedef struct AttributeDef { · 189 unmodified lines
190#define MACRO_OBJ 0 /* object like macro */190#define MACRO_OBJ 0 /* object like macro */
191#define MACRO_FUNC 1 /* function like macro */191#define MACRO_FUNC 1 /* function like macro */
192 192
193/* field 'Sym.t' for labels */193/* field 'Sym.r' for labels */
194#define LABEL_FORWARD 1 /* label is forward defined */194#define LABEL_FORWARD 1 /* label is forward defined */
195 195
196/* type_decl() types */196/* type_decl() types */
@@ -305,6 +305,10 @@ int gnu_ext = 1; · 108 unmodified lines
305/* use Tiny C extensions */305/* use Tiny C extensions */
306int tcc_ext = 1;306int tcc_ext = 1;
307 307
308/* max number of callers shown if error */
309static int num_callers = 6;
310static const char **rt_bound_error_msg;
311
308/* XXX: suppress that ASAP */312/* XXX: suppress that ASAP */
309static struct TCCState *tcc_state;313static struct TCCState *tcc_state;
310 314
@@ -572,7 +576,7 @@ int parse_btype(int *type_ptr, AttributeDef *ad); · 261 unmodified lines
572int type_decl(AttributeDef *ad, int *v, int t, int td);576int type_decl(AttributeDef *ad, int *v, int t, int td);
573 577
574void error(const char *fmt, ...);578void error(const char *fmt, ...);
575void rt_error(unsigned long pc, const char *fmt, ...);579void rt_error(struct ucontext *uc, const char *fmt, ...);
576void vpushi(int v);580void vpushi(int v);
577void vset(int t, int r, int v);581void vset(int t, int r, int v);
578void type_to_str(char *buf, int buf_size, 582void type_to_str(char *buf, int buf_size,
@@ -910,6 +914,7 @@ static void put_extern_sym(Sym *sym, Section *section, · 331 unmodified lines
910 name = get_tok_str(sym->v, NULL);914 name = get_tok_str(sym->v, NULL);
911#ifdef CONFIG_TCC_BCHECK915#ifdef CONFIG_TCC_BCHECK
912 if (do_bounds_check) {916 if (do_bounds_check) {
917 /* XXX: avoid doing that for statics ? */
913 /* if bound checking is activated, we change some function918 /* if bound checking is activated, we change some function
914 names by adding the "__bound" prefix */919 names by adding the "__bound" prefix */
915 switch(sym->v) {920 switch(sym->v) {
@@ -5475,8 +5480,7 @@ static void unary(void) · 4559 unmodified lines
5475 test_lvalue();5480 test_lvalue();
5476 vtop->t = mk_pointer(vtop->t);5481 vtop->t = mk_pointer(vtop->t);
5477 gaddrof();5482 gaddrof();
5478 } else5483 } else if (t == '!') {
5479 if (t == '!') {
5480 unary();5484 unary();
5481 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)5485 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
5482 vtop->c.i = !vtop->c.i;5486 vtop->c.i = !vtop->c.i;
@@ -5484,21 +5488,18 @@ static void unary(void) · 1 unmodified line
5484 vtop->c.i = vtop->c.i ^ 1;5488 vtop->c.i = vtop->c.i ^ 1;
5485 else5489 else
5486 vset(VT_INT, VT_JMP, gtst(1, 0));5490 vset(VT_INT, VT_JMP, gtst(1, 0));
5487 } else5491 } else if (t == '~') {
5488 if (t == '~') {
5489 unary();5492 unary();
5490 vpushi(-1);5493 vpushi(-1);
5491 gen_op('^');5494 gen_op('^');
5492 } else 5495 } else if (t == '+') {
5493 if (t == '+') {
5494 /* in order to force cast, we add zero */5496 /* in order to force cast, we add zero */
5495 unary();5497 unary();
5496 if ((vtop->t & VT_BTYPE) == VT_PTR)5498 if ((vtop->t & VT_BTYPE) == VT_PTR)
5497 error("pointer not accepted for unary plus");5499 error("pointer not accepted for unary plus");
5498 vpushi(0);5500 vpushi(0);
5499 gen_op('+');5501 gen_op('+');
5500 } else 5502 } else if (t == TOK_SIZEOF || t == TOK_ALIGNOF) {
5501 if (t == TOK_SIZEOF || t == TOK_ALIGNOF) {
5502 if (tok == '(') {5503 if (tok == '(') {
5503 ft = parse_expr_type();5504 ft = parse_expr_type();
5504 } else {5505 } else {
@@ -5509,16 +5510,28 @@ static void unary(void) · 4 unmodified lines
5509 vpushi(size);5510 vpushi(size);
5510 else5511 else
5511 vpushi(align);5512 vpushi(align);
5512 } else5513 } else if (t == TOK_INC || t == TOK_DEC) {
5513 if (t == TOK_INC || t == TOK_DEC) {
5514 unary();5514 unary();
5515 inc(0, t);5515 inc(0, t);
5516 } else if (t == '-') {5516 } else if (t == '-') {
5517 vpushi(0);5517 vpushi(0);
5518 unary();5518 unary();
5519 gen_op('-');5519 gen_op('-');
5520 } else 5520 } else if (t == TOK_LAND && gnu_ext) {
5521 {5521 /* allow to take the address of a label */
5522 if (tok < TOK_UIDENT)
5523 expect("label identifier");
5524 s = sym_find1(&label_stack, tok);
5525 if (!s) {
5526 s = sym_push1(&label_stack, tok, 0, 0);
5527 s->r = LABEL_FORWARD;
5528 }
5529 if (!s->t)
5530 s->t = mk_pointer(VT_VOID) | VT_STATIC;
5531 vset(s->t, VT_CONST | VT_SYM, 0);
5532 vtop->sym = s;
5533 next();
5534 } else {
5522 if (t < TOK_UIDENT)5535 if (t < TOK_UIDENT)
5523 expect("identifier");5536 expect("identifier");
5524 s = sym_find(t);5537 s = sym_find(t);
@@ -6200,16 +6213,29 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 675 unmodified lines
6200 } else6213 } else
6201 if (tok == TOK_GOTO) {6214 if (tok == TOK_GOTO) {
6202 next();6215 next();
6203 s = sym_find1(&label_stack, tok);6216 if (tok == '*' && gnu_ext) {
6204 /* put forward definition if needed */6217 /* computed goto */
6205 if (!s)6218 next();
6206 s = sym_push1(&label_stack, tok, LABEL_FORWARD, 0);6219 gexpr();
6207 /* label already defined */6220 if ((vtop->t & VT_BTYPE) != VT_PTR)
6208 if (s->t & LABEL_FORWARD) 6221 expect("pointer");
6209 s->c = gjmp(s->c);6222 ggoto();
6210 else6223 } else if (tok >= TOK_UIDENT) {
6211 gjmp_addr(s->c);6224 s = sym_find1(&label_stack, tok);
6212 next();6225 /* put forward definition if needed */
6226 if (!s) {
6227 s = sym_push1(&label_stack, tok, 0, 0);
6228 s->r = LABEL_FORWARD;
6229 }
6230 /* label already defined */
6231 if (s->r & LABEL_FORWARD)
6232 s->next = (void *)gjmp((long)s->next);
6233 else
6234 gjmp_addr((long)s->next);
6235 next();
6236 } else {
6237 expect("label identifier");
6238 }
6213 skip(';');6239 skip(';');
6214 } else {6240 } else {
6215 b = is_label();6241 b = is_label();
@@ -6217,14 +6243,14 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 1 unmodified line
6217 /* label case */6243 /* label case */
6218 s = sym_find1(&label_stack, b);6244 s = sym_find1(&label_stack, b);
6219 if (s) {6245 if (s) {
6220 if (!(s->t & LABEL_FORWARD))6246 if (!(s->r & LABEL_FORWARD))
6221 error("multiple defined label");6247 error("multiple defined label");
6222 gsym(s->c);6248 gsym((long)s->next);
6223 s->c = ind;
6224 s->t = 0;
6225 } else {6249 } else {
6226 sym_push1(&label_stack, b, 0, ind);6250 s = sym_push1(&label_stack, b, 0, 0);
6227 }6251 }
6252 s->next = (void *)ind;
6253 s->r = 0;
6228 /* we accept this, but it is a mistake */6254 /* we accept this, but it is a mistake */
6229 if (tok == '}') 6255 if (tok == '}')
6230 warning("deprecated use of label at end of compound statement");6256 warning("deprecated use of label at end of compound statement");
@@ -6997,6 +7023,22 @@ static void decl(int l) · 766 unmodified lines
6997 gsym(rsym);7023 gsym(rsym);
6998 gfunc_epilog();7024 gfunc_epilog();
6999 cur_text_section->data_offset = ind;7025 cur_text_section->data_offset = ind;
7026 /* look if any labels are undefined. Define symbols if
7027 '&&label' was used. */
7028 {
7029 Sym *s;
7030 for(s = label_stack.top; s != NULL; s = s->prev) {
7031 if (s->r & LABEL_FORWARD) {
7032 error("label '%s' used but not defined",
7033 get_tok_str(s->v, NULL));
7034 }
7035 if (s->c) {
7036 /* define corresponding symbol. A size of
7037 1 is put. */
7038 put_extern_sym(s, cur_text_section, (long)s->next, 1);
7039 }
7040 }
7041 }
7000 sym_pop(&label_stack, NULL); /* reset label stack */7042 sym_pop(&label_stack, NULL); /* reset label stack */
7001 sym_pop(&local_stack, NULL); /* reset local stack */7043 sym_pop(&local_stack, NULL); /* reset local stack */
7002 /* end of function */7044 /* end of function */
@@ -7234,6 +7276,8 @@ static void rt_printline(unsigned long wanted_pc) · 231 unmodified lines
7234 int incl_index, len, last_line_num, i;7276 int incl_index, len, last_line_num, i;
7235 const char *str, *p;7277 const char *str, *p;
7236 7278
7279 fprintf(stderr, "0x%08lx:", wanted_pc);
7280
7237 func_name[0] = '\0';7281 func_name[0] = '\0';
7238 func_addr = 0;7282 func_addr = 0;
7239 incl_index = 0;7283 incl_index = 0;
@@ -7247,6 +7291,10 @@ static void rt_printline(unsigned long wanted_pc) · 7 unmodified lines
7247 /* function start or end */7291 /* function start or end */
7248 case N_FUN:7292 case N_FUN:
7249 if (sym->n_strx == 0) {7293 if (sym->n_strx == 0) {
7294 /* we test if between last line and end of function */
7295 pc = sym->n_value + func_addr;
7296 if (wanted_pc >= last_pc && wanted_pc < pc)
7297 goto found;
7250 func_name[0] = '\0';7298 func_name[0] = '\0';
7251 func_addr = 0;7299 func_addr = 0;
7252 } else {7300 } else {
@@ -7300,31 +7348,98 @@ static void rt_printline(unsigned long wanted_pc) · 47 unmodified lines
7300 }7348 }
7301 sym++;7349 sym++;
7302 }7350 }
7303 /* did not find line number info: */7351
7304 fprintf(stderr, "(no debug info, pc=0x%08lx): ", wanted_pc);7352 /* second pass: we try symtab symbols (no line number info) */
7353 incl_index = 0;
7354 {
7355 Elf32_Sym *sym, *sym_end;
7356 int type;
7357
7358 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
7359 for(sym = (Elf32_Sym *)symtab_section->data + 1;
7360 sym < sym_end;
7361 sym++) {
7362 type = ELF32_ST_TYPE(sym->st_info);
7363 if (type == STT_FUNC) {
7364 if (wanted_pc >= sym->st_value &&
7365 wanted_pc < sym->st_value + sym->st_size) {
7366 pstrcpy(last_func_name, sizeof(last_func_name),
7367 strtab_section->data + sym->st_name);
7368 goto found;
7369 }
7370 }
7371 }
7372 }
7373 /* did not find any info: */
7374 fprintf(stderr, " ???\n");
7305 return;7375 return;
7306 found:7376 found:
7307 for(i = 0; i < incl_index - 1; i++)7377 if (last_func_name[0] != '\0') {
7308 fprintf(stderr, "In file included from %s\n", 7378 fprintf(stderr, " %s()", last_func_name);
7309 incl_files[i]);7379 }
7310 if (incl_index > 0) {7380 if (incl_index > 0) {
7311 fprintf(stderr, "%s:%d: ", 7381 fprintf(stderr, " (%s:%d",
7312 incl_files[incl_index - 1], last_line_num);7382 incl_files[incl_index - 1], last_line_num);
7383 for(i = incl_index - 2; i >= 0; i--)
7384 fprintf(stderr, ", included from %s", incl_files[i]);
7385 fprintf(stderr, ")");
7313 }7386 }
7314 if (last_func_name[0] != '\0') {7387 fprintf(stderr, "\n");
7315 fprintf(stderr, "in function '%s()': ", last_func_name);7388}
7389
7390#ifdef __i386__
7391
7392#ifndef EIP
7393#define EIP 14
7394#define EBP 6
7395#endif
7396
7397/* return the PC at frame level 'level'. Return non zero if not found */
7398static int rt_get_caller_pc(unsigned long *paddr,
7399 struct ucontext *uc, int level)
7400{
7401 unsigned long fp;
7402 int i;
7403
7404 if (level == 0) {
7405 *paddr = uc->uc_mcontext.gregs[EIP];
7406 return 0;
7407 } else {
7408 fp = uc->uc_mcontext.gregs[EBP];
7409 for(i=1;i<level;i++) {
7410 /* XXX: check address validity with program info */
7411 if (fp <= 0x1000 || fp >= 0xc0000000)
7412 return -1;
7413 fp = ((unsigned long *)fp)[0];
7414 }
7415 *paddr = ((unsigned long *)fp)[1];
7416 return 0;
7316 }7417 }
7317}7418}
7419#else
7420#error add corresponding function
7421#endif
7318 7422
7319/* emit a run time error at position 'pc' */7423/* emit a run time error at position 'pc' */
7320void rt_error(unsigned long pc, const char *fmt, ...)7424void rt_error(struct ucontext *uc, const char *fmt, ...)
7321{7425{
7322 va_list ap;7426 va_list ap;
7323 va_start(ap, fmt);7427 unsigned long pc;
7428 int i;
7324 7429
7325 rt_printline(pc);7430 va_start(ap, fmt);
7431 fprintf(stderr, "Runtime error: ");
7326 vfprintf(stderr, fmt, ap);7432 vfprintf(stderr, fmt, ap);
7327 fprintf(stderr, "\n");7433 fprintf(stderr, "\n");
7434 for(i=0;i<num_callers;i++) {
7435 if (rt_get_caller_pc(&pc, uc, i) < 0)
7436 break;
7437 if (i == 0)
7438 fprintf(stderr, "at ");
7439 else
7440 fprintf(stderr, "by ");
7441 rt_printline(pc);
7442 }
7328 exit(255);7443 exit(255);
7329 va_end(ap);7444 va_end(ap);
7330}7445}
@@ -7334,38 +7449,34 @@ void rt_error(unsigned long pc, const char *fmt, ...) · 3 unmodified lines
7334static void sig_error(int signum, siginfo_t *siginf, void *puc)7449static void sig_error(int signum, siginfo_t *siginf, void *puc)
7335{7450{
7336 struct ucontext *uc = puc;7451 struct ucontext *uc = puc;
7337 unsigned long pc;
7338
7339#ifdef __i386__
7340 pc = uc->uc_mcontext.gregs[14];
7341#else
7342#error please put the right sigcontext field
7343#endif
7344 7452
7345 switch(signum) {7453 switch(signum) {
7346 case SIGFPE:7454 case SIGFPE:
7347 switch(siginf->si_code) {7455 switch(siginf->si_code) {
7348 case FPE_INTDIV:7456 case FPE_INTDIV:
7349 case FPE_FLTDIV:7457 case FPE_FLTDIV:
7350 rt_error(pc, "division by zero");7458 rt_error(uc, "division by zero");
7351 break;7459 break;
7352 default:7460 default:
7353 rt_error(pc, "floating point exception");7461 rt_error(uc, "floating point exception");
7354 break;7462 break;
7355 }7463 }
7356 break;7464 break;
7357 case SIGBUS:7465 case SIGBUS:
7358 case SIGSEGV:7466 case SIGSEGV:
7359 rt_error(pc, "dereferencing invalid pointer");7467 if (rt_bound_error_msg && *rt_bound_error_msg)
7468 rt_error(uc, *rt_bound_error_msg);
7469 else
7470 rt_error(uc, "dereferencing invalid pointer");
7360 break;7471 break;
7361 case SIGILL:7472 case SIGILL:
7362 rt_error(pc, "illegal instruction");7473 rt_error(uc, "illegal instruction");
7363 break;7474 break;
7364 case SIGABRT:7475 case SIGABRT:
7365 rt_error(pc, "abort() called");7476 rt_error(uc, "abort() called");
7366 break;7477 break;
7367 default:7478 default:
7368 rt_error(pc, "caught signal %d", signum);7479 rt_error(uc, "caught signal %d", signum);
7369 break;7480 break;
7370 }7481 }
7371 exit(255);7482 exit(255);
@@ -7440,11 +7551,9 @@ int tcc_run(TCCState *s1, int argc, char **argv) · 68 unmodified lines
7440#ifdef CONFIG_TCC_BCHECK7551#ifdef CONFIG_TCC_BCHECK
7441 if (do_bounds_check) {7552 if (do_bounds_check) {
7442 void (*bound_init)(void);7553 void (*bound_init)(void);
7443 void **bound_error_func;
7444 7554
7445 /* set error function */7555 /* set error function */
7446 bound_error_func = (void **)tcc_get_symbol(s1, "__bound_error_func");7556 rt_bound_error_msg = (void *)tcc_get_symbol(s1, "__bound_error_msg");
7447 *bound_error_func = rt_error;
7448 7557
7449 /* XXX: use .init section so that it also work in binary ? */7558 /* XXX: use .init section so that it also work in binary ? */
7450 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");7559 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
@@ -7782,9 +7891,9 @@ int tcc_set_output_type(TCCState *s, int output_type) · 331 unmodified lines
7782 7891
7783void help(void)7892void help(void)
7784{7893{
7785 printf("tcc version 0.9.12 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" 7894 printf("tcc version 0.9.13 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
7786 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"7895 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
7787 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"7896 " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
7788 " [--] infile1 [infile2... --] [infile_args...]\n"7897 " [--] infile1 [infile2... --] [infile_args...]\n"
7789 "\n"7898 "\n"
7790 "General options:\n"7899 "General options:\n"
@@ -7798,17 +7907,18 @@ void help(void) · 7 unmodified lines
7798 " -Idir add include path 'dir'\n"7907 " -Idir add include path 'dir'\n"
7799 " -Dsym[=val] define 'sym' with value 'val'\n"7908 " -Dsym[=val] define 'sym' with value 'val'\n"
7800 " -Usym undefine 'sym'\n"7909 " -Usym undefine 'sym'\n"
7801 "C compiler options:\n"
7802 " -g generate runtime debug info\n"
7803#ifdef CONFIG_TCC_BCHECK
7804 " -b compile with built-in memory and bounds checker (implies -g)\n"
7805#endif
7806 "Linker options:\n"7910 "Linker options:\n"
7807 " -Ldir add library path 'dir'\n"7911 " -Ldir add library path 'dir'\n"
7808 " -llib link with dynamic or static library 'lib'\n"7912 " -llib link with dynamic or static library 'lib'\n"
7809 " -shared generate a shared library\n"7913 " -shared generate a shared library\n"
7810 " -static static linking\n"7914 " -static static linking\n"
7811 " -r relocatable output\n"7915 " -r relocatable output\n"
7916 "Debugger options:\n"
7917 " -g generate runtime debug info\n"
7918#ifdef CONFIG_TCC_BCHECK
7919 " -b compile with built-in memory and bounds checker (implies -g)\n"
7920#endif
7921 " -bt N show N callers in stack traces\n"
7812 );7922 );
7813}7923}
7814 7924
@@ -7884,7 +7994,9 @@ int main(int argc, char **argv) · 69 unmodified lines
7884 nb_libraries++;7994 nb_libraries++;
7885 } else if (!strcmp(r + 1, "bench")) {7995 } else if (!strcmp(r + 1, "bench")) {
7886 do_bench = 1;7996 do_bench = 1;
7887 } else 7997 } else if (!strcmp(r + 1, "bt")) {
7998 num_callers = atoi(argv[optind++]);
7999 } else
7888#ifdef CONFIG_TCC_BCHECK8000#ifdef CONFIG_TCC_BCHECK
7889 if (r[1] == 'b') {8001 if (r[1] == 'b') {
7890 do_bounds_check = 1;8002 do_bounds_check = 1;
@@ -7982,7 +8094,9 @@ int main(int argc, char **argv) · 91 unmodified lines
7982 ret = tcc_run(s, argc - optind, argv + optind);8094 ret = tcc_run(s, argc - optind, argv + optind);
7983 }8095 }
7984 the_end:8096 the_end:
7985 tcc_delete(s);8097 /* XXX: cannot do it with bound checking because of the malloc hooks */
8098 if (!do_bounds_check)
8099 tcc_delete(s);
7986 8100
7987#ifdef MEM_DEBUG8101#ifdef MEM_DEBUG
7988 if (do_bench) {8102 if (do_bench) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t