History

fixed bound checking for structures - fixed ?: type handling - fixed '--' parsing

bellard committed 24 years ago

Commit 405c8811 parent f1418e81 file · +103 −26Browse files

Unified · Split

tcc.c+103−26
@@ -271,9 +271,11 @@ int nb_library_paths; · 270 unmodified lines
271 anon_sym: anonymous symbol index271 anon_sym: anonymous symbol index
272*/272*/
273int rsym, anon_sym,273int rsym, anon_sym,
274 prog, ind, loc, const_wanted;274 prog, ind, loc;
275int global_expr; /* true if compound literals must be allocated275/* expression generation modifiers */
276 globally (used during initializers parsing */276int const_wanted; /* true if constant wanted */
277int global_expr; /* true if compound literals must be allocated
278 globally (used during initializers parsing */
277int func_vt, func_vc; /* current function return type (used by279int func_vt, func_vc; /* current function return type (used by
278 return instruction) */280 return instruction) */
279int last_line_num, last_ind, func_ind; /* debug last line number and pc */281int 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 /* if bound checking is activated, we change some function923 /* if bound checking is activated, we change some function
922 names by adding the "__bound" prefix */924 names by adding the "__bound" prefix */
923 switch(sym->v) {925 switch(sym->v) {
926#if 0
927 /* XXX: we rely only on malloc hooks */
924 case TOK_malloc: 928 case TOK_malloc:
925 case TOK_free: 929 case TOK_free:
926 case TOK_realloc: 930 case TOK_realloc:
927 case TOK_memalign: 931 case TOK_memalign:
928 case TOK_calloc: 932 case TOK_calloc:
933#endif
929 case TOK_memcpy: 934 case TOK_memcpy:
930 case TOK_memmove:935 case TOK_memmove:
931 case TOK_memset:936 case TOK_memset:
@@ -1901,7 +1906,7 @@ void preprocess(void) · 969 unmodified lines
1901 }1906 }
1902 1907
1903 if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)1908 if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)
1904 error("memory full");1909 error("#include recursion too deep");
1905 if (c == '\"') {1910 if (c == '\"') {
1906 /* first search in current dir if "header.h" */1911 /* first search in current dir if "header.h" */
1907 size = 0;1912 size = 0;
@@ -2857,12 +2862,18 @@ void swap(int *p, int *q) · 949 unmodified lines
2857 2862
2858void vsetc(int t, int r, CValue *vc)2863void vsetc(int t, int r, CValue *vc)
2859{2864{
2865 int v;
2866
2860 if (vtop >= vstack + VSTACK_SIZE)2867 if (vtop >= vstack + VSTACK_SIZE)
2861 error("memory full");2868 error("memory full");
2862 /* cannot let cpu flags if other instruction are generated */2869 /* cannot let cpu flags if other instruction are generated. Also
2863 /* XXX: VT_JMP test too ? */2870 avoid leaving VT_JMP anywhere except on the top of the stack
2864 if ((vtop->r & VT_VALMASK) == VT_CMP)2871 because it would complicate the code generator. */
2865 gv(RC_INT);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 vtop++;2877 vtop++;
2867 vtop->t = t;2878 vtop->t = t;
2868 vtop->r = r;2879 vtop->r = r;
@@ -3251,8 +3262,13 @@ int gv(int rc) · 382 unmodified lines
3251/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */3262/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
3252void gv2(int rc1, int rc2)3263void gv2(int rc1, int rc2)
3253{3264{
3254 /* generate more generic register first */3265 int v;
3255 if (rc1 <= rc2) {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 vswap();3272 vswap();
3257 gv(rc1);3273 gv(rc1);
3258 vswap();3274 vswap();
@@ -5239,8 +5255,12 @@ void unary(void) · 1980 unmodified lines
5239 /* change type to field type, and set to lvalue */5255 /* change type to field type, and set to lvalue */
5240 vtop->t = s->t;5256 vtop->t = s->t;
5241 /* an array is never an lvalue */5257 /* an array is never an lvalue */
5242 if (!(vtop->t & VT_ARRAY))5258 if (!(vtop->t & VT_ARRAY)) {
5243 vtop->r |= lvalue_type(vtop->t);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 next();5264 next();
5245 } else if (tok == '[') {5265 } else if (tok == '[') {
5246 next();5266 next();
@@ -5468,21 +5488,23 @@ void eor(void) · 221 unmodified lines
5468/* XXX: better constant handling */5488/* XXX: better constant handling */
5469void expr_eq(void)5489void 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 if (const_wanted) {5494 if (const_wanted) {
5495 int c1, c;
5474 sum(10);5496 sum(10);
5475 if (tok == '?') {5497 if (tok == '?') {
5476 c = vtop->c.i;5498 c = vtop->c.i;
5477 vpop();5499 vpop();
5478 next();5500 next();
5479 gexpr();5501 gexpr();
5480 t = vtop->c.i;5502 c1 = vtop->c.i;
5481 vpop();5503 vpop();
5482 skip(':');5504 skip(':');
5483 expr_eq();5505 expr_eq();
5484 if (c)5506 if (c)
5485 vtop->c.i = t;5507 vtop->c.i = c1;
5486 }5508 }
5487 } else {5509 } else {
5488 eor();5510 eor();
@@ -5490,23 +5512,76 @@ void expr_eq(void) · 1 unmodified line
5490 next();5512 next();
5491 save_regs(1); /* we need to save all registers here except5513 save_regs(1); /* we need to save all registers here except
5492 at the top because it is a branch point */5514 at the top because it is a branch point */
5493 t = gtst(1, 0);5515 tt = gtst(1, 0);
5494 gexpr();5516 gexpr();
5495 /* XXX: long long handling ? */5517 t1 = vtop->t;
5496 rc = RC_INT;5518 bt1 = t1 & VT_BTYPE;
5497 if (is_float(vtop->t))5519 sv = *vtop; /* save value to handle it later */
5498 rc = RC_FLOAT;
5499 r1 = gv(rc);
5500 vtop--; /* no vpop so that FP stack is not flushed */5520 vtop--; /* no vpop so that FP stack is not flushed */
5501 skip(':');5521 skip(':');
5502 u = gjmp(0);5522 u = gjmp(0);
5503 5523
5504 gsym(t);5524 gsym(tt);
5505 expr_eq();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 r2 = gv(rc);5573 r2 = gv(rc);
5507 move_reg(r1, r2);5574 /* this is horrible, but we must also convert first
5508 vtop->r = r1;5575 operand */
5576 tt = gjmp(0);
5509 gsym(u);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
7197void help(void)7272void 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 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"7275 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
7201 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"7276 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
7202 " [--] infile1 [infile2... --] [infile_args...]\n"7277 " [--] infile1 [infile2... --] [infile_args...]\n"
@@ -7232,7 +7307,7 @@ int main(int argc, char **argv) · 29 unmodified lines
7232 int optind, output_type, multiple_files, i, reloc_output;7307 int optind, output_type, multiple_files, i, reloc_output;
7233 TCCState *s;7308 TCCState *s;
7234 char **files;7309 char **files;
7235 int nb_files, nb_libraries, nb_objfiles;7310 int nb_files, nb_libraries, nb_objfiles, dminus;
7236 char objfilename[1024];7311 char objfilename[1024];
7237 7312
7238 s = tcc_new();7313 s = tcc_new();
@@ -7241,6 +7316,7 @@ int main(int argc, char **argv) · 2 unmodified lines
7241 optind = 1;7316 optind = 1;
7242 outfile = NULL;7317 outfile = NULL;
7243 multiple_files = 0;7318 multiple_files = 0;
7319 dminus = 0;
7244 files = NULL;7320 files = NULL;
7245 nb_files = 0;7321 nb_files = 0;
7246 nb_libraries = 0;7322 nb_libraries = 0;
@@ -7263,10 +7339,11 @@ int main(int argc, char **argv) · 16 unmodified lines
7263 }7339 }
7264 } else if (r[1] == '-') {7340 } else if (r[1] == '-') {
7265 /* '--' enables multiple files input and also ends several file input */7341 /* '--' enables multiple files input and also ends several file input */
7266 if (multiple_files) {7342 if (dminus && multiple_files) {
7267 optind--; /* argv[0] will be '--' */7343 optind--; /* argv[0] will be '--' */
7268 break;7344 break;
7269 }7345 }
7346 dminus = 1;
7270 multiple_files = 1;7347 multiple_files = 1;
7271 } else if (r[1] == 'h' || r[1] == '?') {7348 } else if (r[1] == 'h' || r[1] == '?') {
7272 show_help:7349 show_help:

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