Unified · Split
| @@ -271,9 +271,11 @@ int nb_library_paths; · 270 unmodified lines | ||
| 271 | 271 | anon_sym: anonymous symbol index |
| 272 | 272 | */ |
| 273 | 273 | int rsym, anon_sym, |
| 274 | − prog, ind, loc, const_wanted; | |
| 275 | −int global_expr; /* true if compound literals must be allocated | |
| 276 | − globally (used during initializers parsing */ | |
| 274 | + prog, ind, loc; | |
| 275 | +/* expression generation modifiers */ | |
| 276 | +int const_wanted; /* true if constant wanted */ | |
| 277 | +int global_expr; /* true if compound literals must be allocated | |
| 278 | + globally (used during initializers parsing */ | |
| 277 | 279 | int func_vt, func_vc; /* current function return type (used by |
| 278 | 280 | return instruction) */ |
| 279 | 281 | int last_line_num, last_ind, func_ind; /* debug last line number and pc */ |
| @@ -921,11 +923,14 @@ static void put_extern_sym(Sym *sym, Section *section, · 641 unmodified lines | ||
| 921 | 923 | /* if bound checking is activated, we change some function |
| 922 | 924 | names by adding the "__bound" prefix */ |
| 923 | 925 | switch(sym->v) { |
| 926 | +#if 0 | |
| 927 | + /* XXX: we rely only on malloc hooks */ | |
| 924 | 928 | case TOK_malloc: |
| 925 | 929 | case TOK_free: |
| 926 | 930 | case TOK_realloc: |
| 927 | 931 | case TOK_memalign: |
| 928 | 932 | case TOK_calloc: |
| 933 | +#endif | |
| 929 | 934 | case TOK_memcpy: |
| 930 | 935 | case TOK_memmove: |
| 931 | 936 | case TOK_memset: |
| @@ -1901,7 +1906,7 @@ void preprocess(void) · 969 unmodified lines | ||
| 1901 | 1906 | } |
| 1902 | 1907 | |
| 1903 | 1908 | if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE) |
| 1904 | − error("memory full"); | |
| 1909 | + error("#include recursion too deep"); | |
| 1905 | 1910 | if (c == '\"') { |
| 1906 | 1911 | /* first search in current dir if "header.h" */ |
| 1907 | 1912 | size = 0; |
| @@ -2857,12 +2862,18 @@ void swap(int *p, int *q) · 949 unmodified lines | ||
| 2857 | 2862 | |
| 2858 | 2863 | void vsetc(int t, int r, CValue *vc) |
| 2859 | 2864 | { |
| 2865 | + int v; | |
| 2866 | + | |
| 2860 | 2867 | if (vtop >= vstack + VSTACK_SIZE) |
| 2861 | 2868 | error("memory full"); |
| 2862 | − /* cannot let cpu flags if other instruction are generated */ | |
| 2863 | − /* XXX: VT_JMP test too ? */ | |
| 2864 | − if ((vtop->r & VT_VALMASK) == VT_CMP) | |
| 2865 | − gv(RC_INT); | |
| 2869 | + /* cannot let cpu flags if other instruction are generated. Also | |
| 2870 | + avoid leaving VT_JMP anywhere except on the top of the stack | |
| 2871 | + because it would complicate the code generator. */ | |
| 2872 | + if (vtop >= vstack) { | |
| 2873 | + v = vtop->r & VT_VALMASK; | |
| 2874 | + if (v == VT_CMP || (v & ~1) == VT_JMP) | |
| 2875 | + gv(RC_INT); | |
| 2876 | + } | |
| 2866 | 2877 | vtop++; |
| 2867 | 2878 | vtop->t = t; |
| 2868 | 2879 | vtop->r = r; |
| @@ -3251,8 +3262,13 @@ int gv(int rc) · 382 unmodified lines | ||
| 3251 | 3262 | /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */ |
| 3252 | 3263 | void gv2(int rc1, int rc2) |
| 3253 | 3264 | { |
| 3254 | − /* generate more generic register first */ | |
| 3255 | − if (rc1 <= rc2) { | |
| 3265 | + int v; | |
| 3266 | + | |
| 3267 | + /* generate more generic register first. But VT_JMP or VT_CMP | |
| 3268 | + values must be generated first in all cases to avoid possible | |
| 3269 | + reload errors */ | |
| 3270 | + v = vtop[0].r & VT_VALMASK; | |
| 3271 | + if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) { | |
| 3256 | 3272 | vswap(); |
| 3257 | 3273 | gv(rc1); |
| 3258 | 3274 | vswap(); |
| @@ -5239,8 +5255,12 @@ void unary(void) · 1980 unmodified lines | ||
| 5239 | 5255 | /* change type to field type, and set to lvalue */ |
| 5240 | 5256 | vtop->t = s->t; |
| 5241 | 5257 | /* an array is never an lvalue */ |
| 5242 | − if (!(vtop->t & VT_ARRAY)) | |
| 5258 | + if (!(vtop->t & VT_ARRAY)) { | |
| 5243 | 5259 | vtop->r |= lvalue_type(vtop->t); |
| 5260 | + /* if bound checking, the referenced pointer must be checked */ | |
| 5261 | + if (do_bounds_check) | |
| 5262 | + vtop->r |= VT_MUSTBOUND; | |
| 5263 | + } | |
| 5244 | 5264 | next(); |
| 5245 | 5265 | } else if (tok == '[') { |
| 5246 | 5266 | next(); |
| @@ -5468,21 +5488,23 @@ void eor(void) · 221 unmodified lines | ||
| 5468 | 5488 | /* XXX: better constant handling */ |
| 5469 | 5489 | void expr_eq(void) |
| 5470 | 5490 | { |
| 5471 | − int t, u, c, r1, r2, rc; | |
| 5491 | + int tt, u, r1, r2, rc, t1, t2, t, bt1, bt2; | |
| 5492 | + SValue sv; | |
| 5472 | 5493 | |
| 5473 | 5494 | if (const_wanted) { |
| 5495 | + int c1, c; | |
| 5474 | 5496 | sum(10); |
| 5475 | 5497 | if (tok == '?') { |
| 5476 | 5498 | c = vtop->c.i; |
| 5477 | 5499 | vpop(); |
| 5478 | 5500 | next(); |
| 5479 | 5501 | gexpr(); |
| 5480 | − t = vtop->c.i; | |
| 5502 | + c1 = vtop->c.i; | |
| 5481 | 5503 | vpop(); |
| 5482 | 5504 | skip(':'); |
| 5483 | 5505 | expr_eq(); |
| 5484 | 5506 | if (c) |
| 5485 | − vtop->c.i = t; | |
| 5507 | + vtop->c.i = c1; | |
| 5486 | 5508 | } |
| 5487 | 5509 | } else { |
| 5488 | 5510 | eor(); |
| @@ -5490,23 +5512,76 @@ void expr_eq(void) · 1 unmodified line | ||
| 5490 | 5512 | next(); |
| 5491 | 5513 | save_regs(1); /* we need to save all registers here except |
| 5492 | 5514 | at the top because it is a branch point */ |
| 5493 | − t = gtst(1, 0); | |
| 5515 | + tt = gtst(1, 0); | |
| 5494 | 5516 | gexpr(); |
| 5495 | − /* XXX: long long handling ? */ | |
| 5496 | − rc = RC_INT; | |
| 5497 | − if (is_float(vtop->t)) | |
| 5498 | − rc = RC_FLOAT; | |
| 5499 | − r1 = gv(rc); | |
| 5517 | + t1 = vtop->t; | |
| 5518 | + bt1 = t1 & VT_BTYPE; | |
| 5519 | + sv = *vtop; /* save value to handle it later */ | |
| 5500 | 5520 | vtop--; /* no vpop so that FP stack is not flushed */ |
| 5501 | 5521 | skip(':'); |
| 5502 | 5522 | u = gjmp(0); |
| 5503 | 5523 | |
| 5504 | − gsym(t); | |
| 5524 | + gsym(tt); | |
| 5505 | 5525 | expr_eq(); |
| 5526 | + t2 = vtop->t; | |
| 5527 | + | |
| 5528 | + bt2 = t2 & VT_BTYPE; | |
| 5529 | + /* cast operands to correct type according to ISOC rules */ | |
| 5530 | + if (is_float(bt1) || is_float(bt2)) { | |
| 5531 | + if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) { | |
| 5532 | + t = VT_LDOUBLE; | |
| 5533 | + } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) { | |
| 5534 | + t = VT_DOUBLE; | |
| 5535 | + } else { | |
| 5536 | + t = VT_FLOAT; | |
| 5537 | + } | |
| 5538 | + } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) { | |
| 5539 | + /* cast to biggest op */ | |
| 5540 | + t = VT_LLONG; | |
| 5541 | + /* convert to unsigned if it does not fit in a long long */ | |
| 5542 | + if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) || | |
| 5543 | + (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED)) | |
| 5544 | + t |= VT_UNSIGNED; | |
| 5545 | + } else if (bt1 == VT_PTR || bt2 == VT_PTR) { | |
| 5546 | + /* XXX: test pointer compatibility */ | |
| 5547 | + t = t1; | |
| 5548 | + } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) { | |
| 5549 | + /* XXX: test structure compatibility */ | |
| 5550 | + t = t1; | |
| 5551 | + } else if (bt1 == VT_VOID || bt2 == VT_VOID) { | |
| 5552 | + /* NOTE: as an extension, we accept void on only one side */ | |
| 5553 | + t = VT_VOID; | |
| 5554 | + } else { | |
| 5555 | + /* integer operations */ | |
| 5556 | + t = VT_INT; | |
| 5557 | + /* convert to unsigned if it does not fit in an integer */ | |
| 5558 | + if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) || | |
| 5559 | + (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) | |
| 5560 | + t |= VT_UNSIGNED; | |
| 5561 | + } | |
| 5562 | + | |
| 5563 | + /* now we convert second operand */ | |
| 5564 | + gen_cast(t); | |
| 5565 | + rc = RC_INT; | |
| 5566 | + if (is_float(t)) { | |
| 5567 | + rc = RC_FLOAT; | |
| 5568 | + } else if ((t & VT_BTYPE) == VT_LLONG) { | |
| 5569 | + /* for long longs, we use fixed registers to avoid having | |
| 5570 | + to handle a complicated move */ | |
| 5571 | + rc = RC_IRET; | |
| 5572 | + } | |
| 5506 | 5573 | r2 = gv(rc); |
| 5507 | − move_reg(r1, r2); | |
| 5508 | − vtop->r = r1; | |
| 5574 | + /* this is horrible, but we must also convert first | |
| 5575 | + operand */ | |
| 5576 | + tt = gjmp(0); | |
| 5509 | 5577 | gsym(u); |
| 5578 | + /* put again first value and cast it */ | |
| 5579 | + *vtop = sv; | |
| 5580 | + gen_cast(t); | |
| 5581 | + r1 = gv(rc); | |
| 5582 | + move_reg(r2, r1); | |
| 5583 | + vtop->r = r2; | |
| 5584 | + gsym(tt); | |
| 5510 | 5585 | } |
| 5511 | 5586 | } |
| 5512 | 5587 | } |
| @@ -7196,7 +7271,7 @@ int tcc_set_output_type(TCCState *s, int output_type) · 1683 unmodified lines | ||
| 7196 | 7271 | |
| 7197 | 7272 | void help(void) |
| 7198 | 7273 | { |
| 7199 | − printf("tcc version 0.9.11 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" | |
| 7274 | + printf("tcc version 0.9.12 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" | |
| 7200 | 7275 | "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" |
| 7201 | 7276 | " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n" |
| 7202 | 7277 | " [--] infile1 [infile2... --] [infile_args...]\n" |
| @@ -7232,7 +7307,7 @@ int main(int argc, char **argv) · 29 unmodified lines | ||
| 7232 | 7307 | int optind, output_type, multiple_files, i, reloc_output; |
| 7233 | 7308 | TCCState *s; |
| 7234 | 7309 | char **files; |
| 7235 | − int nb_files, nb_libraries, nb_objfiles; | |
| 7310 | + int nb_files, nb_libraries, nb_objfiles, dminus; | |
| 7236 | 7311 | char objfilename[1024]; |
| 7237 | 7312 | |
| 7238 | 7313 | s = tcc_new(); |
| @@ -7241,6 +7316,7 @@ int main(int argc, char **argv) · 2 unmodified lines | ||
| 7241 | 7316 | optind = 1; |
| 7242 | 7317 | outfile = NULL; |
| 7243 | 7318 | multiple_files = 0; |
| 7319 | + dminus = 0; | |
| 7244 | 7320 | files = NULL; |
| 7245 | 7321 | nb_files = 0; |
| 7246 | 7322 | nb_libraries = 0; |
| @@ -7263,10 +7339,11 @@ int main(int argc, char **argv) · 16 unmodified lines | ||
| 7263 | 7339 | } |
| 7264 | 7340 | } else if (r[1] == '-') { |
| 7265 | 7341 | /* '--' enables multiple files input and also ends several file input */ |
| 7266 | − if (multiple_files) { | |
| 7342 | + if (dminus && multiple_files) { | |
| 7267 | 7343 | optind--; /* argv[0] will be '--' */ |
| 7268 | 7344 | break; |
| 7269 | 7345 | } |
| 7346 | + dminus = 1; | |
| 7270 | 7347 | multiple_files = 1; |
| 7271 | 7348 | } else if (r[1] == 'h' || r[1] == '?') { |
| 7272 | 7349 | show_help: |