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
271271 anon_sym: anonymous symbol index
272272 */
273273 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 */
277279 int func_vt, func_vc; /* current function return type (used by
278280 return instruction) */
279281 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
921923 /* if bound checking is activated, we change some function
922924 names by adding the "__bound" prefix */
923925 switch(sym->v) {
926+#if 0
927+ /* XXX: we rely only on malloc hooks */
924928 case TOK_malloc:
925929 case TOK_free:
926930 case TOK_realloc:
927931 case TOK_memalign:
928932 case TOK_calloc:
933+#endif
929934 case TOK_memcpy:
930935 case TOK_memmove:
931936 case TOK_memset:
@@ -1901,7 +1906,7 @@ void preprocess(void) · 969 unmodified lines
19011906 }
19021907
19031908 if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE)
1904− error("memory full");
1909+ error("#include recursion too deep");
19051910 if (c == '\"') {
19061911 /* first search in current dir if "header.h" */
19071912 size = 0;
@@ -2857,12 +2862,18 @@ void swap(int *p, int *q) · 949 unmodified lines
28572862
28582863 void vsetc(int t, int r, CValue *vc)
28592864 {
2865+ int v;
2866+
28602867 if (vtop >= vstack + VSTACK_SIZE)
28612868 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+ }
28662877 vtop++;
28672878 vtop->t = t;
28682879 vtop->r = r;
@@ -3251,8 +3262,13 @@ int gv(int rc) · 382 unmodified lines
32513262 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
32523263 void gv2(int rc1, int rc2)
32533264 {
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) {
32563272 vswap();
32573273 gv(rc1);
32583274 vswap();
@@ -5239,8 +5255,12 @@ void unary(void) · 1980 unmodified lines
52395255 /* change type to field type, and set to lvalue */
52405256 vtop->t = s->t;
52415257 /* an array is never an lvalue */
5242− if (!(vtop->t & VT_ARRAY))
5258+ if (!(vtop->t & VT_ARRAY)) {
52435259 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+ }
52445264 next();
52455265 } else if (tok == '[') {
52465266 next();
@@ -5468,21 +5488,23 @@ void eor(void) · 221 unmodified lines
54685488 /* XXX: better constant handling */
54695489 void expr_eq(void)
54705490 {
5471− int t, u, c, r1, r2, rc;
5491+ int tt, u, r1, r2, rc, t1, t2, t, bt1, bt2;
5492+ SValue sv;
54725493
54735494 if (const_wanted) {
5495+ int c1, c;
54745496 sum(10);
54755497 if (tok == '?') {
54765498 c = vtop->c.i;
54775499 vpop();
54785500 next();
54795501 gexpr();
5480− t = vtop->c.i;
5502+ c1 = vtop->c.i;
54815503 vpop();
54825504 skip(':');
54835505 expr_eq();
54845506 if (c)
5485− vtop->c.i = t;
5507+ vtop->c.i = c1;
54865508 }
54875509 } else {
54885510 eor();
@@ -5490,23 +5512,76 @@ void expr_eq(void) · 1 unmodified line
54905512 next();
54915513 save_regs(1); /* we need to save all registers here except
54925514 at the top because it is a branch point */
5493− t = gtst(1, 0);
5515+ tt = gtst(1, 0);
54945516 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 */
55005520 vtop--; /* no vpop so that FP stack is not flushed */
55015521 skip(':');
55025522 u = gjmp(0);
55035523
5504− gsym(t);
5524+ gsym(tt);
55055525 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+ }
55065573 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);
55095577 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);
55105585 }
55115586 }
55125587 }
@@ -7196,7 +7271,7 @@ int tcc_set_output_type(TCCState *s, int output_type) · 1683 unmodified lines
71967271
71977272 void help(void)
71987273 {
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"
72007275 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
72017276 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
72027277 " [--] infile1 [infile2... --] [infile_args...]\n"
@@ -7232,7 +7307,7 @@ int main(int argc, char **argv) · 29 unmodified lines
72327307 int optind, output_type, multiple_files, i, reloc_output;
72337308 TCCState *s;
72347309 char **files;
7235− int nb_files, nb_libraries, nb_objfiles;
7310+ int nb_files, nb_libraries, nb_objfiles, dminus;
72367311 char objfilename[1024];
72377312
72387313 s = tcc_new();
@@ -7241,6 +7316,7 @@ int main(int argc, char **argv) · 2 unmodified lines
72417316 optind = 1;
72427317 outfile = NULL;
72437318 multiple_files = 0;
7319+ dminus = 0;
72447320 files = NULL;
72457321 nb_files = 0;
72467322 nb_libraries = 0;
@@ -7263,10 +7339,11 @@ int main(int argc, char **argv) · 16 unmodified lines
72637339 }
72647340 } else if (r[1] == '-') {
72657341 /* '--' enables multiple files input and also ends several file input */
7266− if (multiple_files) {
7342+ if (dminus && multiple_files) {
72677343 optind--; /* argv[0] will be '--' */
72687344 break;
72697345 }
7346+ dminus = 1;
72707347 multiple_files = 1;
72717348 } else if (r[1] == 'h' || r[1] == '?') {
72727349 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