History

fixed switch - preparation for new types

bellard committed 24 years ago

Commit cb693e51 parent 9b3c96c1 file · +84 −35Browse files

Unified · Split

tcc.c+84−35
@@ -443,6 +443,10 @@ TokenSym *tok_alloc(char *str, int len) · 442 unmodified lines
443443 return ts;
444444 pts = &(ts->hash_next);
445445 }
446+
447+ if (tok_ident >= SYM_FIRST_ANOM)
448+ error("memory full");
449+
446450 /* expand token table if needed */
447451 i = tok_ident - TOK_IDENT;
448452 if ((i % TOK_ALLOC_INCR) == 0) {
@@ -451,8 +455,9 @@ TokenSym *tok_alloc(char *str, int len) · 2 unmodified lines
451455 error("memory full");
452456 table_ident = ptable;
453457 }
458+
454459 ts = malloc(sizeof(TokenSym) + len);
455− if (!ts || tok_ident >= SYM_FIRST_ANOM)
460+ if (!ts)
456461 error("memory full");
457462 table_ident[i] = ts;
458463 ts->tok = tok_ident++;
@@ -1468,7 +1473,7 @@ void macro_subst(int **tok_str, int *tok_len, · 1009 unmodified lines
14681473 }
14691474
14701475 /* return next token with macro substitution */
1471−void next()
1476+void next(void)
14721477 {
14731478 int len, *ptr;
14741479 Sym *nested_list;
@@ -2413,46 +2418,87 @@ int struct_decl(int u) · 938 unmodified lines
24132418 */
24142419 int ist(void)
24152420 {
2416− int t;
2421+ int t, u;
24172422 Sym *s;
24182423
24192424 t = 0;
24202425 while(1) {
2421− if (tok == TOK_ENUM) {
2422− t |= struct_decl(VT_ENUM);
2423− } else if (tok == TOK_STRUCT || tok == TOK_UNION) {
2424− t |= struct_decl(VT_STRUCT);
2425− } else {
2426− if (tok == TOK_CHAR) {
2427− t |= VT_BYTE;
2428− } else if (tok == TOK_VOID) {
2429− t |= VT_VOID;
2430− } else if (tok == TOK_SHORT) {
2431− t |= VT_SHORT;
2432− } else if (tok == TOK_INT |
2433− (tok >= TOK_CONST & tok <= TOK_INLINE)) {
2434− /* ignored types */
2435− } else if (tok == TOK_FLOAT || tok == TOK_DOUBLE) {
2436− /* We allow that to compile standard headers */
2437− // warning("floats not supported");
2438− } else if (tok == TOK_EXTERN) {
2439− t |= VT_EXTERN;
2440− } else if (tok == TOK_STATIC) {
2441− t |= VT_STATIC;
2442− } else if (tok == TOK_UNSIGNED) {
2443− t |= VT_UNSIGNED;
2444− } else if (tok == TOK_TYPEDEF) {
2445− t |= VT_TYPEDEF;
2446− } else {
2447− s = sym_find(tok);
2448− if (!s || !(s->t & VT_TYPEDEF))
2449− break;
2450− t |= (s->t & ~VT_TYPEDEF);
2451− }
2426+ switch(tok) {
2427+ /* basic types */
2428+ case TOK_CHAR:
2429+ u = VT_BYTE;
2430+ basic_type:
2431+ next();
2432+ basic_type1:
2433+ if ((t & VT_BTYPE) != 0)
2434+ error("too many basic types %x", t);
2435+ t |= u;
2436+ break;
2437+ case TOK_VOID:
2438+ u = VT_VOID;
2439+ goto basic_type;
2440+ case TOK_SHORT:
2441+ u = VT_SHORT;
2442+ goto basic_type;
2443+ case TOK_INT:
2444+ next();
2445+ break;
2446+ case TOK_LONG:
2447+ /* XXX: add long type */
2448+ u = VT_INT;
2449+ goto basic_type;
2450+ case TOK_FLOAT:
2451+ case TOK_DOUBLE:
2452+ /* XXX: add float types */
2453+ u = VT_INT;
2454+ goto basic_type;
2455+ case TOK_ENUM:
2456+ u = struct_decl(VT_ENUM);
2457+ goto basic_type1;
2458+ case TOK_STRUCT:
2459+ case TOK_UNION:
2460+ u = struct_decl(VT_STRUCT);
2461+ goto basic_type1;
2462+
2463+ /* type modifiers */
2464+ case TOK_CONST:
2465+ case TOK_VOLATILE:
2466+ case TOK_REGISTER:
2467+ case TOK_SIGNED:
2468+ case TOK_AUTO:
2469+ case TOK_INLINE:
2470+ case TOK_RESTRICT:
2471+ next();
2472+ break;
2473+ case TOK_UNSIGNED:
2474+ t |= VT_UNSIGNED;
2475+ next();
2476+ break;
2477+
2478+ /* storage */
2479+ case TOK_EXTERN:
2480+ t |= VT_EXTERN;
2481+ next();
2482+ break;
2483+ case TOK_STATIC:
2484+ t |= VT_STATIC;
2485+ next();
2486+ break;
2487+ case TOK_TYPEDEF:
2488+ t |= VT_TYPEDEF;
2489+ next();
2490+ break;
2491+ default:
2492+ s = sym_find(tok);
2493+ if (!s || !(s->t & VT_TYPEDEF))
2494+ goto the_end;
2495+ t |= (s->t & ~VT_TYPEDEF);
24522496 next();
2497+ break;
24532498 }
24542499 t |= 2;
24552500 }
2501+the_end:
24562502 return t;
24572503 }
24582504
@@ -3268,7 +3314,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 809 unmodified lines
32683314 case_reg = gv();
32693315 skip(')');
32703316 a = 0;
3271− b = 0;
3317+ b = gjmp(0); /* jump to first case */
32723318 c = 0;
32733319 block(&a, csym, &b, &c, case_reg);
32743320 /* if no default, jmp after switch */
@@ -3284,12 +3330,15 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 9 unmodified lines
32843330 a = expr_const();
32853331 if (!case_sym)
32863332 expect("switch");
3333+ /* since a case is like a label, we must skip it with a jmp */
3334+ b = gjmp(0);
32873335 gsym(*case_sym);
32883336 vset(case_reg, 0);
32893337 vpush();
32903338 vset(VT_CONST, a);
32913339 gen_op(TOK_EQ);
32923340 *case_sym = gtst(1, 0);
3341+ gsym(b);
32933342 skip(':');
32943343 block(bsym, csym, case_sym, def_sym, case_reg);
32953344 } else

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