Unified · Split
| @@ -17,12 +17,17 @@ · 16 unmodified lines | ||
| 17 | 17 | * along with this program; if not, write to the Free Software |
| 18 | 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | 19 | */ |
| 20 | −#include <stdio.h> | |
| 20 | +#include "tcclib.h" | |
| 21 | 21 | |
| 22 | −#define TEXT_SIZE 20000 | |
| 23 | −#define DATA_SIZE 2000 | |
| 24 | −#define SYM_TABLE_SIZE 10000 | |
| 25 | −#define VAR_TABLE_SIZE 4096 | |
| 22 | +#define INCLUDE_PATH "/usr/include" | |
| 23 | + | |
| 24 | +#define TEXT_SIZE 50000 | |
| 25 | +#define DATA_SIZE 50000 | |
| 26 | +#define SYM_TABLE_SIZE 10000 | |
| 27 | +#define MACRO_STACK_SIZE 32 | |
| 28 | +#define INCLUDE_STACK_SIZE 32 | |
| 29 | + | |
| 30 | +#define NB_REGS 3 | |
| 26 | 31 | |
| 27 | 32 | /* symbol management */ |
| 28 | 33 | typedef struct Sym { |
| @@ -36,68 +41,62 @@ typedef struct Sym { · 7 unmodified lines | ||
| 36 | 41 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ |
| 37 | 42 | #define SYM_FIELD 0x20000000 /* struct/union field symbol space */ |
| 38 | 43 | |
| 44 | +typedef struct { | |
| 45 | + FILE *file; /* stdio file */ | |
| 46 | + char *filename; | |
| 47 | + int line_num; | |
| 48 | +} IncludeFile; | |
| 49 | + | |
| 39 | 50 | /* loc : local variable index |
| 40 | 51 | glo : global variable index |
| 41 | − parm : parameter variable index | |
| 42 | 52 | ind : output code ptr |
| 43 | 53 | rsym: return symbol |
| 44 | 54 | prog: output code |
| 45 | − astk: arg position stack | |
| 55 | + anon_sym: anonymous symbol index | |
| 46 | 56 | */ |
| 47 | −void *file; | |
| 48 | −int tok, tok1, rsym, | |
| 49 | − prog, ind, loc, glo, vt, | |
| 50 | − vc, *macro_stack, *macro_stack_ptr, line_num; | |
| 57 | +FILE *file; | |
| 58 | +int tok, tok1, tokc, rsym, anon_sym, | |
| 59 | + prog, ind, loc, glo, vt, *vstack, *vstack_ptr, | |
| 60 | + vc, line_num; | |
| 51 | 61 | char *idtable, *idptr, *filename; |
| 52 | 62 | Sym *define_stack, *global_stack, *local_stack, *label_stack; |
| 53 | 63 | |
| 54 | −/* The current value can be: */ | |
| 55 | −#define VT_CONST 0x0002 /* constant in vc */ | |
| 56 | −#define VT_VAR 0x0004 /* value is in eax */ | |
| 57 | −#define VT_LOCAL 0x0008 /* offset on stack */ | |
| 58 | − | |
| 59 | −#define VT_LVAL 0x0010 /* const or var is an lvalue */ | |
| 60 | −#define VT_CMP 0x0020 /* the value is stored in processor flags (in vc) */ | |
| 61 | −#define VT_FORWARD 0x0040 /* value is forward reference (only used for functions) */ | |
| 62 | −#define VT_JMP 0x0080 /* value is the consequence of jmp. bit 0 is set if inv */ | |
| 64 | +char *macro_stack[MACRO_STACK_SIZE], **macro_stack_ptr, *macro_ptr; | |
| 65 | +IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr; | |
| 63 | 66 | |
| 67 | +/* The current value can be: */ | |
| 68 | +#define VT_VALMASK 0x000f | |
| 69 | +#define VT_CONST 0x000a /* constant in vc | |
| 70 | + (must be first non register value) */ | |
| 71 | +#define VT_LLOCAL 0x000b /* lvalue, offset on stack */ | |
| 72 | +#define VT_LOCAL 0x000c /* offset on stack */ | |
| 73 | +#define VT_CMP 0x000d /* the value is stored in processor flags (in vc) */ | |
| 74 | +#define VT_JMP 0x000e /* value is the consequence of jmp true */ | |
| 75 | +#define VT_JMPI 0x000f /* value is the consequence of jmp false */ | |
| 76 | +#define VT_LVAL 0x0010 /* var is an lvalue */ | |
| 64 | 77 | #define VT_LVALN -17 /* ~VT_LVAL */ |
| 65 | − | |
| 66 | −/* | |
| 67 | − * | |
| 68 | − * VT_FUNC indicates a function. The return type is the stored type. A | |
| 69 | − * function pointer is stored as a 'char' pointer. | |
| 70 | − * | |
| 71 | − * If VT_PTRMASK is non nul, then it indicates the number of pointer | |
| 72 | − * iterations to reach the basic type. | |
| 73 | − * | |
| 74 | − * Basic types: | |
| 75 | − * | |
| 76 | − * VT_BYTE indicate a char | |
| 77 | − * VT_UNSIGNED indicates unsigned type | |
| 78 | − * | |
| 79 | − * otherwise integer type is assumed. | |
| 80 | − * | |
| 81 | − */ | |
| 82 | − | |
| 83 | −#define VT_BYTE 0x00001 /* byte type, HARDCODED VALUE */ | |
| 84 | −#define VT_PTRMASK 0x00f00 /* pointer mask */ | |
| 85 | −#define VT_PTRINC 0x00100 /* pointer increment */ | |
| 78 | +#define VT_FORWARD 0x0020 /* value is forward reference | |
| 79 | + (only used for functions) */ | |
| 80 | + | |
| 81 | +#define VT_VOID 0x00040 | |
| 82 | +#define VT_BYTE 0x00080 /* byte type */ | |
| 83 | +#define VT_PTR 0x00100 /* pointer increment */ | |
| 84 | +#define VT_UNSIGNED 0x00200 /* unsigned type */ | |
| 85 | +#define VT_ARRAY 0x00400 /* array type (only used in parsing) */ | |
| 86 | +#define VT_ENUM 0x00800 /* enum definition */ | |
| 86 | 87 | #define VT_FUNC 0x01000 /* function type */ |
| 87 | −#define VT_UNSIGNED 0x02000 /* unsigned type */ | |
| 88 | −#define VT_ARRAY 0x04000 /* array type (only used in parsing) */ | |
| 89 | −#define VT_TYPE 0xffffff01 /* type mask */ | |
| 90 | −#define VT_TYPEN 0x000000fe /* ~VT_TYPE */ | |
| 91 | −#define VT_FUNCN -4097 /* ~VT_FUNC */ | |
| 92 | − | |
| 88 | +#define VT_STRUCT 0x002000 /* struct/union definition */ | |
| 89 | +#define VT_TYPEDEF 0x004000 /* typedef definition */ | |
| 93 | 90 | #define VT_EXTERN 0x008000 /* extern definition */ |
| 94 | 91 | #define VT_STATIC 0x010000 /* static variable */ |
| 92 | +#define VT_STRUCT_SHIFT 17 /* structure/enum name shift (12 bits lefts) */ | |
| 93 | + | |
| 94 | +#define VT_TYPE 0xffffffc0 /* type mask */ | |
| 95 | +#define VT_TYPEN 0x0000003f /* ~VT_TYPE */ | |
| 96 | +#define VT_FUNCN -4097 /* ~VT_FUNC */ | |
| 97 | + | |
| 95 | 98 | |
| 96 | 99 | /* Special infos */ |
| 97 | −#define VT_ENUM 0x020000 /* enum definition */ | |
| 98 | −#define VT_STRUCT 0x040000 /* struct/union definition */ | |
| 99 | −#define VT_TYPEDEF 0x080000 /* typedef definition */ | |
| 100 | −#define VT_STRUCT_SHIFT 20 /* structure/enum name shift (12 bits lefts) */ | |
| 101 | 100 | |
| 102 | 101 | /* token values */ |
| 103 | 102 | #define TOK_INT 256 |
| @@ -126,15 +125,19 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 22 unmodified lines | ||
| 126 | 125 | #define TOK_LONG 277 |
| 127 | 126 | #define TOK_REGISTER 278 |
| 128 | 127 | #define TOK_SIGNED 279 |
| 128 | +#define TOK_AUTO 280 | |
| 129 | +#define TOK_INLINE 281 | |
| 129 | 130 | |
| 130 | −#define TOK_FLOAT 280 /* unsupported */ | |
| 131 | −#define TOK_DOUBLE 281 /* unsupported */ | |
| 131 | +#define TOK_FLOAT 282 /* unsupported */ | |
| 132 | +#define TOK_DOUBLE 283 /* unsupported */ | |
| 132 | 133 | |
| 133 | −#define TOK_STRUCT 282 | |
| 134 | −#define TOK_UNION 283 | |
| 135 | −#define TOK_TYPEDEF 284 | |
| 136 | −#define TOK_DEFAULT 285 | |
| 137 | −#define TOK_ENUM 286 | |
| 134 | +#define TOK_STRUCT 284 | |
| 135 | +#define TOK_UNION 285 | |
| 136 | +#define TOK_TYPEDEF 286 | |
| 137 | +#define TOK_DEFAULT 287 | |
| 138 | +#define TOK_ENUM 288 | |
| 139 | +#define TOK_SIZEOF 289 | |
| 140 | +#define TOK_INCLUDE 290 | |
| 138 | 141 | |
| 139 | 142 | #define TOK_EQ 0x94 /* warning: depend on asm code */ |
| 140 | 143 | #define TOK_NE 0x95 /* warning: depend on asm code */ |
| @@ -143,16 +146,22 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 2 unmodified lines | ||
| 143 | 146 | #define TOK_LE 0x9e /* warning: depend on asm code */ |
| 144 | 147 | #define TOK_GT 0x9f /* warning: depend on asm code */ |
| 145 | 148 | |
| 146 | −#define TOK_LAND 0xa0 | |
| 147 | −#define TOK_LOR 0xa1 | |
| 149 | +#define TOK_LAND 0xa0 | |
| 150 | +#define TOK_LOR 0xa1 | |
| 148 | 151 | |
| 149 | 152 | #define TOK_DEC 0xa2 |
| 150 | 153 | #define TOK_MID 0xa3 /* inc/dec, to void constant */ |
| 151 | 154 | #define TOK_INC 0xa4 |
| 152 | 155 | #define TOK_ARROW 0xa7 |
| 153 | − | |
| 154 | −#define TOK_SHL 0x01 | |
| 155 | −#define TOK_SHR 0x02 | |
| 156 | +#define TOK_DOTS 0xa8 /* three dots */ | |
| 157 | +#define TOK_SHR 0xa9 /* unsigned shift right */ | |
| 158 | +#define TOK_UDIV 0xb0 /* unsigned division */ | |
| 159 | +#define TOK_UMOD 0xb1 /* unsigned modulo */ | |
| 160 | +#define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */ | |
| 161 | +#define TOK_NUM 0xb3 /* number in tokc */ | |
| 162 | + | |
| 163 | +#define TOK_SHL 0x01 /* shift left */ | |
| 164 | +#define TOK_SAR 0x02 /* signed shift right */ | |
| 156 | 165 | |
| 157 | 166 | /* assignement operators : normal operator or 0x80 */ |
| 158 | 167 | #define TOK_A_MOD 0xa5 |
| @@ -164,30 +173,16 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 5 unmodified lines | ||
| 164 | 173 | #define TOK_A_XOR 0xde |
| 165 | 174 | #define TOK_A_OR 0xfc |
| 166 | 175 | #define TOK_A_SHL 0x81 |
| 167 | −#define TOK_A_SHR 0x82 | |
| 176 | +#define TOK_A_SAR 0x82 | |
| 168 | 177 | |
| 169 | −#ifdef TINY | |
| 170 | −#define expr_eq() expr() | |
| 171 | −#else | |
| 172 | 178 | void sum(); |
| 173 | 179 | void next(); |
| 174 | 180 | void expr_eq(); |
| 175 | 181 | void expr(); |
| 176 | 182 | void decl(); |
| 177 | −#endif | |
| 178 | − | |
| 179 | − | |
| 180 | −int inp() | |
| 181 | −{ | |
| 182 | −#if 0 | |
| 183 | − int c; | |
| 184 | − c = fgetc(file); | |
| 185 | − printf("c=%c\n", c); | |
| 186 | − return c; | |
| 187 | −#else | |
| 188 | − return fgetc(file); | |
| 189 | −#endif | |
| 190 | −} | |
| 183 | +int gv(); | |
| 184 | +void move_reg(); | |
| 185 | +void save_reg(); | |
| 191 | 186 | |
| 192 | 187 | int isid(c) |
| 193 | 188 | { |
| @@ -297,7 +292,7 @@ Sym *sym_find(int v) · 103 unmodified lines | ||
| 297 | 292 | /* push a given symbol on the symbol stack */ |
| 298 | 293 | Sym *sym_push(int v, int t, int c) |
| 299 | 294 | { |
| 300 | − // printf("sym_push: %s type=%x\n", get_tok_str(v), t); | |
| 295 | + // printf("sym_push: %x %s type=%x\n", v, get_tok_str(v), t); | |
| 301 | 296 | if (local_stack) |
| 302 | 297 | return sym_push1(&local_stack, v, t, c); |
| 303 | 298 | else |
| @@ -312,17 +307,214 @@ void sym_pop(Sym **ps, Sym *b) · 8 unmodified lines | ||
| 312 | 307 | s = *ps; |
| 313 | 308 | while(s != b) { |
| 314 | 309 | ss = s->prev; |
| 315 | − // printf("sym_pop: %s type=%x\n", get_tok_str(s->v), s->t); | |
| 310 | + // printf("sym_pop: %x %s type=%x\n", s->v, get_tok_str(s->v), s->t); | |
| 316 | 311 | free(s); |
| 317 | 312 | s = ss; |
| 318 | 313 | } |
| 319 | 314 | *ps = b; |
| 320 | 315 | } |
| 321 | 316 | |
| 317 | +int ch, ch1; | |
| 318 | + | |
| 319 | +/* read next char from current input file */ | |
| 320 | +void inp() | |
| 321 | +{ | |
| 322 | + int c; | |
| 323 | + | |
| 324 | + redo: | |
| 325 | + ch1 = fgetc(file); | |
| 326 | + if (ch1 == -1) { | |
| 327 | + if (include_stack_ptr == include_stack) | |
| 328 | + return; | |
| 329 | + /* pop include stack */ | |
| 330 | + fclose(file); | |
| 331 | + free(filename); | |
| 332 | + include_stack_ptr--; | |
| 333 | + file = include_stack_ptr->file; | |
| 334 | + filename = include_stack_ptr->filename; | |
| 335 | + line_num = include_stack_ptr->line_num; | |
| 336 | + goto redo; | |
| 337 | + } | |
| 338 | + if (ch1 == '\n') | |
| 339 | + line_num++; | |
| 340 | + // printf("ch1=%c\n", ch1); | |
| 341 | +} | |
| 342 | + | |
| 343 | +/* input with '\\n' handling and macro subtitution if in macro state */ | |
| 344 | +void minp() | |
| 345 | +{ | |
| 346 | + if (macro_ptr != 0) { | |
| 347 | + ch = *macro_ptr++; | |
| 348 | + /* end of macro ? */ | |
| 349 | + if (ch == '\0') { | |
| 350 | + macro_ptr = *--macro_stack_ptr; | |
| 351 | + ch = (int)*--macro_stack_ptr; | |
| 352 | + } | |
| 353 | + } else { | |
| 354 | + redo: | |
| 355 | + ch = ch1; | |
| 356 | + inp(); | |
| 357 | + if (ch == '\\' && ch1 == '\n') { | |
| 358 | + inp(); | |
| 359 | + goto redo; | |
| 360 | + } | |
| 361 | + } | |
| 362 | +} | |
| 363 | + | |
| 364 | +/* same as minp, but also skip comments */ | |
| 365 | +void cinp() | |
| 366 | +{ | |
| 367 | + int c; | |
| 368 | + | |
| 369 | + if (ch1 == '/') { | |
| 370 | + inp(); | |
| 371 | + if (ch1 == '/') { | |
| 372 | + /* single line C++ comments */ | |
| 373 | + inp(); | |
| 374 | + while (ch1 != '\n' && ch1 != -1) | |
| 375 | + inp(); | |
| 376 | + inp(); | |
| 377 | + ch = ' '; /* return space */ | |
| 378 | + } else if (ch1 == '*') { | |
| 379 | + /* C comments */ | |
| 380 | + inp(); | |
| 381 | + while (ch1 != -1) { | |
| 382 | + c = ch1; | |
| 383 | + inp(); | |
| 384 | + if (c == '*' && ch1 == '/') { | |
| 385 | + inp(); | |
| 386 | + ch = ' '; /* return space */ | |
| 387 | + break; | |
| 388 | + } | |
| 389 | + } | |
| 390 | + } else { | |
| 391 | + ch = '/'; | |
| 392 | + } | |
| 393 | + } else { | |
| 394 | + minp(); | |
| 395 | + } | |
| 396 | +} | |
| 397 | + | |
| 398 | +void skip_spaces() | |
| 399 | +{ | |
| 400 | + while (ch == ' ' || ch == '\t') | |
| 401 | + cinp(); | |
| 402 | +} | |
| 403 | + | |
| 404 | +void preprocess() | |
| 405 | +{ | |
| 406 | + char *str; | |
| 407 | + int size, n, c; | |
| 408 | + char buf[1024], *q, *p; | |
| 409 | + char buf1[1024]; | |
| 410 | + FILE *f; | |
| 411 | + | |
| 412 | + cinp(); | |
| 413 | + next(); /* XXX: should pass parameter to avoid macro subst */ | |
| 414 | + if (tok == TOK_DEFINE) { | |
| 415 | + next(); /* XXX: should pass parameter to avoid macro subst */ | |
| 416 | + skip_spaces(); | |
| 417 | + /* now 'tok' is the macro symbol */ | |
| 418 | + str = NULL; | |
| 419 | + size = 0; | |
| 420 | + n = 0; | |
| 421 | + while (1) { | |
| 422 | + if ((n + 1) >= size) { | |
| 423 | + size += 128; | |
| 424 | + str = realloc(str, size); | |
| 425 | + if (!str) | |
| 426 | + error("memory full"); | |
| 427 | + } | |
| 428 | + if (ch == -1 || ch == '\n') { | |
| 429 | + str[n++] = ' '; /* a space is inserted after each macro */ | |
| 430 | + str[n++] = '\0'; | |
| 431 | + break; | |
| 432 | + } | |
| 433 | + str[n++] = ch; | |
| 434 | + cinp(); | |
| 435 | + } | |
| 436 | + sym_push1(&define_stack, tok, 0, (int)str); | |
| 437 | + } else if (tok == TOK_INCLUDE) { | |
| 438 | + skip_spaces(); | |
| 439 | + if (ch == '<') { | |
| 440 | + c = '>'; | |
| 441 | + goto read_name; | |
| 442 | + } else if (ch == '\"') { | |
| 443 | + c = ch; | |
| 444 | + read_name: | |
| 445 | + minp(); | |
| 446 | + q = buf; | |
| 447 | + while (ch != c && ch != '\n' && ch != -1) { | |
| 448 | + if ((q - buf) < sizeof(buf) - 1) | |
| 449 | + *q++ = ch; | |
| 450 | + minp(); | |
| 451 | + } | |
| 452 | + *q = '\0'; | |
| 453 | + if (include_stack_ptr >= include_stack + INCLUDE_STACK_SIZE) | |
| 454 | + error("memory full"); | |
| 455 | + if (c == '\"') { | |
| 456 | + /* first search in current dir if "header.h" */ | |
| 457 | + /* XXX: buffer overflow */ | |
| 458 | + size = 0; | |
| 459 | + p = strrchr(filename, '/'); | |
| 460 | + if (p) | |
| 461 | + size = p + 1 - filename; | |
| 462 | + memcpy(buf1, filename, size); | |
| 463 | + buf1[size] = '\0'; | |
| 464 | + strcat(buf1, buf); | |
| 465 | + f = fopen(buf1, "r"); | |
| 466 | + if (f) | |
| 467 | + goto found; | |
| 468 | + } | |
| 469 | + /* now search in standard include path */ | |
| 470 | + strcpy(buf1, INCLUDE_PATH); | |
| 471 | + strcat(buf1, "/"); | |
| 472 | + strcat(buf1, buf); | |
| 473 | + f = fopen(buf1, "r"); | |
| 474 | + if (!f) | |
| 475 | + error("include file not found"); | |
| 476 | + found: | |
| 477 | + /* push current file in stack */ | |
| 478 | + include_stack_ptr->file = file; | |
| 479 | + include_stack_ptr->filename = filename; | |
| 480 | + include_stack_ptr->line_num = line_num; | |
| 481 | + include_stack_ptr++; | |
| 482 | + file = f; | |
| 483 | + filename = strdup(buf1); | |
| 484 | + line_num = 1; | |
| 485 | + } | |
| 486 | + | |
| 487 | + } | |
| 488 | + /* ignore other preprocess commands or #! for C scripts */ | |
| 489 | + while (ch != '\n' && ch != -1) | |
| 490 | + cinp(); | |
| 491 | +} | |
| 492 | + | |
| 493 | +/* read a number in base b */ | |
| 494 | +int getn(b) | |
| 495 | +{ | |
| 496 | + int n, t; | |
| 497 | + n = 0; | |
| 498 | + while (1) { | |
| 499 | + if (ch >= 'a' & ch <= 'f') | |
| 500 | + t = ch - 'a' + 10; | |
| 501 | + else if (ch >= 'A' & ch <= 'F') | |
| 502 | + t = ch - 'A' + 10; | |
| 503 | + else if (isnum(ch)) | |
| 504 | + t = ch - '0'; | |
| 505 | + else | |
| 506 | + break; | |
| 507 | + if (t < 0 | t >= b) | |
| 508 | + break; | |
| 509 | + n = n * b + t; | |
| 510 | + cinp(); | |
| 511 | + } | |
| 512 | + return n; | |
| 513 | +} | |
| 322 | 514 | |
| 323 | 515 | void next() |
| 324 | 516 | { |
| 325 | − int c, v; | |
| 517 | + int v, b; | |
| 326 | 518 | char *q, *p; |
| 327 | 519 | Sym *s; |
| 328 | 520 | |
| @@ -332,67 +524,30 @@ void next() · 3 unmodified lines | ||
| 332 | 524 | tok1 = 0; |
| 333 | 525 | return; |
| 334 | 526 | } |
| 335 | − | |
| 527 | + redo: | |
| 528 | + /* skip spaces */ | |
| 336 | 529 | while(1) { |
| 337 | − c = inp(); | |
| 338 | −#ifndef TINY | |
| 339 | − if (c == '/') { | |
| 340 | − /* comments */ | |
| 341 | − c = inp(); | |
| 342 | − if (c == '/') { | |
| 343 | − /* single line comments */ | |
| 344 | − while (c != '\n') | |
| 345 | − c = inp(); | |
| 346 | − } else if (c == '*') { | |
| 347 | − /* comments */ | |
| 348 | − while ((c = inp()) >= 0) { | |
| 349 | − if (c == '*') { | |
| 350 | − c = inp(); | |
| 351 | − if (c == '/') { | |
| 352 | − c = ' '; | |
| 353 | − break; | |
| 354 | − } else if (c == '*') | |
| 355 | − ungetc(c, file); | |
| 356 | − } else if (c == '\n') | |
| 357 | − line_num++; | |
| 358 | − } | |
| 359 | − } else { | |
| 360 | − ungetc(c, file); | |
| 361 | − c = '/'; | |
| 362 | − break; | |
| 363 | − } | |
| 364 | − } else | |
| 365 | −#endif | |
| 366 | − if (c == 35) { | |
| 367 | − /* preprocessor: we handle only define */ | |
| 368 | − next(); | |
| 369 | − if (tok == TOK_DEFINE) { | |
| 370 | − next(); | |
| 371 | − /* now tok is the macro symbol */ | |
| 372 | − sym_push1(&define_stack, tok, 0, ftell(file)); | |
| 530 | + while (ch == '\n') { | |
| 531 | + cinp(); | |
| 532 | + while (ch == ' ' || ch == 9) | |
| 533 | + cinp(); | |
| 534 | + if (ch == '#') { | |
| 535 | + /* preprocessor command if # at start of line after | |
| 536 | + spaces */ | |
| 537 | + preprocess(); | |
| 373 | 538 | } |
| 374 | − /* ignore preprocessor or shell */ | |
| 375 | − while (c != '\n') | |
| 376 | − c = inp(); | |
| 377 | 539 | } |
| 378 | − if (c == '\n') { | |
| 379 | − /* end of line : check if we are in macro state. if so, | |
| 380 | − pop new file position */ | |
| 381 | − if (macro_stack_ptr > macro_stack) | |
| 382 | − fseek(file, *--macro_stack_ptr, 0); | |
| 383 | − else | |
| 384 | − line_num++; | |
| 385 | − } else if (c != ' ' & c != 9) | |
| 540 | + if (ch != ' ' && ch != 9) | |
| 386 | 541 | break; |
| 542 | + cinp(); | |
| 387 | 543 | } |
| 388 | − if (isid(c)) { | |
| 544 | + if (isid(ch)) { | |
| 389 | 545 | q = idptr; |
| 390 | − while(isid(c) | isnum(c)) { | |
| 391 | − *q++ = c; | |
| 392 | − c = inp(); | |
| 546 | + while(isid(ch) | isnum(ch)) { | |
| 547 | + *q++ = ch; | |
| 548 | + cinp(); | |
| 393 | 549 | } |
| 394 | 550 | *q++ = '\0'; |
| 395 | − ungetc(c, file); | |
| 396 | 551 | p = idtable; |
| 397 | 552 | tok = 256; |
| 398 | 553 | while (p < idptr) { |
| @@ -401,48 +556,86 @@ void next() · 2 unmodified lines | ||
| 401 | 556 | while (*p++); |
| 402 | 557 | tok++; |
| 403 | 558 | } |
| 559 | + // printf("id=%s\n", idptr); | |
| 404 | 560 | /* if not found, add symbol */ |
| 405 | 561 | if (p == idptr) |
| 406 | 562 | idptr = q; |
| 407 | − /* eval defines */ | |
| 563 | + /* if symbol is a define, prepare substitution */ | |
| 408 | 564 | if (s = sym_find1(define_stack, tok)) { |
| 409 | − *macro_stack_ptr++ = ftell(file); | |
| 410 | − fseek(file, s->c, 0); | |
| 411 | − next(); | |
| 565 | + if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE) | |
| 566 | + error("too many nested macros"); | |
| 567 | + *macro_stack_ptr++ = (char *)ch; | |
| 568 | + *macro_stack_ptr++ = macro_ptr; | |
| 569 | + macro_ptr = (char *)s->c; | |
| 570 | + cinp(); | |
| 571 | + goto redo; | |
| 412 | 572 | } |
| 573 | + } else if (isnum(ch)) { | |
| 574 | + /* number */ | |
| 575 | + b = 10; | |
| 576 | + if (ch == '0') { | |
| 577 | + cinp(); | |
| 578 | + b = 8; | |
| 579 | + if (ch == 'x') { | |
| 580 | + cinp(); | |
| 581 | + b = 16; | |
| 582 | + } | |
| 583 | + } | |
| 584 | + tokc = getn(b); | |
| 585 | + tok = TOK_NUM; | |
| 413 | 586 | } else { |
| 414 | 587 | #ifdef TINY |
| 415 | 588 | q = "<=\236>=\235!=\225++\244--\242==\224"; |
| 416 | 589 | #else |
| 417 | − q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247"; | |
| 590 | + q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250"; | |
| 418 | 591 | #endif |
| 419 | 592 | /* two chars */ |
| 420 | − v = inp(); | |
| 593 | + tok = ch; | |
| 594 | + cinp(); | |
| 421 | 595 | while (*q) { |
| 422 | − if (*q == c & q[1] == v) { | |
| 596 | + if (*q == tok & q[1] == ch) { | |
| 597 | + cinp(); | |
| 423 | 598 | tok = q[2] & 0xff; |
| 424 | − if (tok == TOK_SHL | tok == TOK_SHR) { | |
| 425 | − v = inp(); | |
| 426 | − if (v == '=') | |
| 599 | + /* three chars tests */ | |
| 600 | + if (tok == TOK_SHL | tok == TOK_SAR) { | |
| 601 | + if (ch == '=') { | |
| 427 | 602 | tok = tok | 0x80; |
| 428 | − else | |
| 429 | − ungetc(v, file); | |
| 603 | + cinp(); | |
| 604 | + } | |
| 605 | + } else if (tok == TOK_DOTS) { | |
| 606 | + if (ch != '.') | |
| 607 | + error("parse error"); | |
| 608 | + cinp(); | |
| 430 | 609 | } |
| 431 | 610 | return; |
| 432 | 611 | } |
| 433 | 612 | q = q + 3; |
| 434 | 613 | } |
| 435 | − ungetc(v, file); | |
| 436 | 614 | /* single char substitutions */ |
| 437 | − if (c == '<') | |
| 615 | + if (tok == '<') | |
| 438 | 616 | tok = TOK_LT; |
| 439 | − else if (c == '>') | |
| 617 | + else if (tok == '>') | |
| 440 | 618 | tok = TOK_GT; |
| 441 | − else | |
| 442 | − tok = c; | |
| 443 | 619 | } |
| 444 | 620 | } |
| 445 | 621 | |
| 622 | +void swap(int *p, int *q) | |
| 623 | +{ | |
| 624 | + int t; | |
| 625 | + t = *p; | |
| 626 | + *p = *q; | |
| 627 | + *q = t; | |
| 628 | +} | |
| 629 | + | |
| 630 | +void vset(t, v) | |
| 631 | +{ | |
| 632 | + vt = t; | |
| 633 | + vc = v; | |
| 634 | +} | |
| 635 | + | |
| 636 | +/******************************************************/ | |
| 637 | +/* X86 code generator */ | |
| 638 | + | |
| 446 | 639 | void g(c) |
| 447 | 640 | { |
| 448 | 641 | *(char *)ind++ = c; |
| @@ -486,195 +679,520 @@ int oad(c, s) · 37 unmodified lines | ||
| 486 | 679 | return s; |
| 487 | 680 | } |
| 488 | 681 | |
| 489 | −void vset(t, v) | |
| 490 | −{ | |
| 491 | − vt = t; | |
| 492 | − vc = v; | |
| 493 | −} | |
| 494 | − | |
| 495 | −/* generate a value in eax from vt and vc */ | |
| 496 | 682 | /* XXX: generate correct pointer for forward references to functions */ |
| 497 | −void gv() | |
| 683 | +/* r = (ft, fc) */ | |
| 684 | +void load(r, ft, fc) | |
| 498 | 685 | { |
| 499 | −#ifndef TINY | |
| 500 | − int t; | |
| 501 | −#endif | |
| 502 | − if (vt & VT_LVAL) { | |
| 503 | − if ((vt & VT_TYPE) == VT_BYTE) | |
| 504 | − o(0xbe0f); /* movsbl x, %eax */ | |
| 505 | − else | |
| 506 | − o(0x8b); /* movl x,%eax */ | |
| 507 | − if (vt & VT_CONST) | |
| 508 | − oad(0x05, vc); | |
| 509 | − else if (vt & VT_LOCAL) | |
| 510 | − oad(0x85, vc); | |
| 686 | + int v, t; | |
| 687 | + | |
| 688 | + v = ft & VT_VALMASK; | |
| 689 | + if (ft & VT_LVAL) { | |
| 690 | + if (v == VT_LLOCAL) { | |
| 691 | + load(r, VT_LOCAL | VT_LVAL, fc); | |
| 692 | + v = r; | |
| 693 | + } | |
| 694 | + if ((ft & VT_TYPE) == VT_BYTE) | |
| 695 | + o(0xbe0f); /* movsbl */ | |
| 511 | 696 | else |
| 512 | − g(0x00); | |
| 513 | − } else { | |
| 514 | − if (vt & VT_CONST) { | |
| 515 | − oad(0xb8, vc); /* mov $xx, %eax */ | |
| 516 | − } else if (vt & VT_LOCAL) { | |
| 517 | − oad(0x858d, vc); /* lea xxx(%ebp), %eax */ | |
| 518 | − } else if (vt & VT_CMP) { | |
| 519 | − oad(0xb8, 0); /* mov $0, %eax */ | |
| 520 | − o(0x0f); /* setxx %al */ | |
| 521 | − o(vc); | |
| 522 | − o(0xc0); | |
| 697 | + o(0x8b); /* movl */ | |
| 698 | + if (v == VT_CONST) { | |
| 699 | + oad(0x05 + r * 8, fc); /* 0xXX, r */ | |
| 700 | + } else if (v == VT_LOCAL) { | |
| 701 | + oad(0x85 + r * 8, fc); /* xx(%ebp), r */ | |
| 702 | + } else { | |
| 703 | + g(0x00 + r * 8 + v); /* (v), r */ | |
| 523 | 704 | } |
| 524 | −#ifndef TINY | |
| 525 | − else if (vt & VT_JMP) { | |
| 526 | − t = vt & 1; | |
| 527 | − oad(0xb8, t); /* mov $1, %eax */ | |
| 705 | + } else { | |
| 706 | + if (v == VT_CONST) { | |
| 707 | + oad(0xb8 + r, fc); /* mov $xx, r */ | |
| 708 | + } else if (v == VT_LOCAL) { | |
| 709 | + o(0x8d); | |
| 710 | + oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */ | |
| 711 | + } else if (v == VT_CMP) { | |
| 712 | + oad(0xb8 + r, 0); /* mov $0, r */ | |
| 713 | + o(0x0f); /* setxx %br */ | |
| 714 | + o(fc); | |
| 715 | + o(0xc0 + r); | |
| 716 | + } else if (v == VT_JMP || v == VT_JMPI) { | |
| 717 | + t = v & 1; | |
| 718 | + oad(0xb8 + r, t); /* mov $1, r */ | |
| 528 | 719 | oad(0xe9, 5); /* jmp after */ |
| 529 | − gsym(vc); | |
| 530 | − oad(0xb8, t ^ 1); /* mov $0, %eax */ | |
| 720 | + gsym(fc); | |
| 721 | + oad(0xb8 + r, t ^ 1); /* mov $0, r */ | |
| 722 | + } else if (v != r) { | |
| 723 | + o(0x89); | |
| 724 | + o(0xc0 + r + v * 8); /* mov v, r */ | |
| 531 | 725 | } |
| 532 | −#endif | |
| 533 | 726 | } |
| 534 | − vt = (vt & VT_TYPE) | VT_VAR; | |
| 727 | +} | |
| 728 | + | |
| 729 | +/* (ft, fc) = r */ | |
| 730 | +/* WARNING: r must not be allocated on the stack */ | |
| 731 | +void store(r, ft, fc) | |
| 732 | +{ | |
| 733 | + int fr, b; | |
| 734 | + | |
| 735 | + fr = ft & VT_VALMASK; | |
| 736 | + b = (ft & VT_TYPE) == VT_BYTE; | |
| 737 | + o(0x89 - b); | |
| 738 | + if (fr == VT_CONST) { | |
| 739 | + oad(0x05 + r * 8, fc); /* mov r,xxx */ | |
| 740 | + } else if (fr == VT_LOCAL) { | |
| 741 | + oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */ | |
| 742 | + } else if (ft & VT_LVAL) { | |
| 743 | + g(fr + r * 8); /* mov r, (fr) */ | |
| 744 | + } else if (fr != r) { | |
| 745 | + o(0xc0 + fr + r * 8); /* mov r, fr */ | |
| 746 | + } | |
| 747 | +} | |
| 748 | + | |
| 749 | +int gjmp(t) | |
| 750 | +{ | |
| 751 | + return psym(0xe9, t); | |
| 535 | 752 | } |
| 536 | 753 | |
| 537 | 754 | /* generate a test. set 'inv' to invert test */ |
| 538 | −/* XXX: handle constant */ | |
| 539 | 755 | int gtst(inv, t) |
| 540 | 756 | { |
| 541 | − if (vt & VT_CMP) { | |
| 757 | + int v, *p; | |
| 758 | + v = vt & VT_VALMASK; | |
| 759 | + if (v == VT_CMP) { | |
| 542 | 760 | /* fast case : can jump directly since flags are set */ |
| 543 | 761 | g(0x0f); |
| 544 | 762 | t = psym((vc - 16) ^ inv, t); |
| 545 | − } else | |
| 546 | −#ifndef TINY | |
| 547 | − if (vt & VT_JMP) { | |
| 763 | + } else if (v == VT_JMP || v == VT_JMPI) { | |
| 548 | 764 | /* && or || optimization */ |
| 549 | − if ((vt & 1) == inv) | |
| 765 | + if ((v & 1) == inv) { | |
| 766 | + /* insert vc jump list in t */ | |
| 767 | + p = &vc; | |
| 768 | + while (*p != 0) | |
| 769 | + p = (int *)*p; | |
| 770 | + *p = t; | |
| 550 | 771 | t = vc; |
| 551 | − else { | |
| 552 | − t = psym(0xe9, t); | |
| 772 | + } else { | |
| 773 | + t = gjmp(t); | |
| 553 | 774 | gsym(vc); |
| 554 | 775 | } |
| 555 | − } else | |
| 556 | − if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) { | |
| 776 | + } else if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) { | |
| 557 | 777 | /* constant jmp optimization */ |
| 558 | 778 | if ((vc != 0) != inv) |
| 559 | − t = psym(0xe9, t); | |
| 560 | − } else | |
| 561 | −#endif | |
| 562 | − { | |
| 563 | − gv(); | |
| 564 | − o(0xc085); /* test %eax, %eax */ | |
| 779 | + t = gjmp(t); | |
| 780 | + } else { | |
| 781 | + v = gv(); | |
| 782 | + o(0x85); | |
| 783 | + o(0xc0 + v * 9); | |
| 565 | 784 | g(0x0f); |
| 566 | 785 | t = psym(0x85 ^ inv, t); |
| 567 | 786 | } |
| 568 | 787 | return t; |
| 569 | 788 | } |
| 570 | 789 | |
| 790 | +/* generate a binary operation 'v = r op fr' instruction and modifies | |
| 791 | + (vt,vc) if needed */ | |
| 792 | +void gen_op1(op, r, fr) | |
| 793 | +{ | |
| 794 | + int t; | |
| 795 | + if (op == '+') { | |
| 796 | + o(0x01); | |
| 797 | + o(0xc0 + r + fr * 8); | |
| 798 | + } else if (op == '-') { | |
| 799 | + o(0x29); | |
| 800 | + o(0xc0 + r + fr * 8); | |
| 801 | + } else if (op == '&') { | |
| 802 | + o(0x21); | |
| 803 | + o(0xc0 + r + fr * 8); | |
| 804 | + } else if (op == '^') { | |
| 805 | + o(0x31); | |
| 806 | + o(0xc0 + r + fr * 8); | |
| 807 | + } else if (op == '|') { | |
| 808 | + o(0x09); | |
| 809 | + o(0xc0 + r + fr * 8); | |
| 810 | + } else if (op == '*') { | |
| 811 | + o(0xaf0f); /* imul fr, r */ | |
| 812 | + o(0xc0 + fr + r * 8); | |
| 813 | + } else if (op == TOK_SHL | op == TOK_SHR | op == TOK_SAR) { | |
| 814 | + /* op2 is %ecx */ | |
| 815 | + if (fr != 1) { | |
| 816 | + if (r == 1) { | |
| 817 | + r = fr; | |
| 818 | + fr = 1; | |
| 819 | + o(0x87); /* xchg r, %ecx */ | |
| 820 | + o(0xc1 + r * 8); | |
| 821 | + } else | |
| 822 | + move_reg(1, fr); | |
| 823 | + } | |
| 824 | + o(0xd3); /* shl/shr/sar %cl, r */ | |
| 825 | + if (op == TOK_SHL) | |
| 826 | + o(0xe0 + r); | |
| 827 | + else if (op == TOK_SHR) | |
| 828 | + o(0xe8 + r); | |
| 829 | + else | |
| 830 | + o(0xf8 + r); | |
| 831 | + vt = (vt & VT_TYPE) | r; | |
| 832 | + } else if (op == '/' | op == TOK_UDIV | op == TOK_PDIV | | |
| 833 | + op == '%' | op == TOK_UMOD) { | |
| 834 | + save_reg(2); /* save edx */ | |
| 835 | + t = save_reg_forced(fr); /* save fr and get op2 location */ | |
| 836 | + move_reg(0, r); /* op1 is %eax */ | |
| 837 | + if (op == TOK_UDIV | op == TOK_UMOD) { | |
| 838 | + o(0xf7d231); /* xor %edx, %edx, div t(%ebp), %eax */ | |
| 839 | + oad(0xb5, t); | |
| 840 | + } else { | |
| 841 | + o(0xf799); /* cltd, idiv t(%ebp), %eax */ | |
| 842 | + oad(0xbd, t); | |
| 843 | + } | |
| 844 | + if (op == '%' | op == TOK_UMOD) | |
| 845 | + r = 2; | |
| 846 | + else | |
| 847 | + r = 0; | |
| 848 | + vt = (vt & VT_TYPE) | r; | |
| 849 | + } else { | |
| 850 | + o(0x39); | |
| 851 | + o(0xc0 + r + fr * 8); /* cmp fr, r */ | |
| 852 | + vset(VT_CMP, op); | |
| 853 | + } | |
| 854 | +} | |
| 855 | + | |
| 856 | +/* end of X86 code generator */ | |
| 857 | +/*************************************************************/ | |
| 858 | + | |
| 859 | +int save_reg_forced(r) | |
| 860 | +{ | |
| 861 | + int i, l, *p, t; | |
| 862 | + /* store register */ | |
| 863 | + loc = (loc - 4) & -3; | |
| 864 | + store(r, VT_LOCAL, loc); | |
| 865 | + l = loc; | |
| 866 | + | |
| 867 | + /* modify all stack values */ | |
| 868 | + for(p=vstack;p<vstack_ptr;p+=2) { | |
| 869 | + i = p[0] & VT_VALMASK; | |
| 870 | + if (i == r) { | |
| 871 | + if (p[0] & VT_LVAL) | |
| 872 | + t = VT_LLOCAL; | |
| 873 | + else | |
| 874 | + t = VT_LOCAL; | |
| 875 | + p[0] = (p[0] & VT_TYPE) | VT_LVAL | t; | |
| 876 | + p[1] = l; | |
| 877 | + } | |
| 878 | + } | |
| 879 | + return l; | |
| 880 | +} | |
| 881 | + | |
| 882 | +/* save r to memory. and mark it as being free */ | |
| 883 | +void save_reg(r) | |
| 884 | +{ | |
| 885 | + int i, *p; | |
| 886 | + | |
| 887 | + /* modify all stack values */ | |
| 888 | + for(p=vstack;p<vstack_ptr;p+=2) { | |
| 889 | + i = p[0] & VT_VALMASK; | |
| 890 | + if (i == r) { | |
| 891 | + save_reg_forced(r); | |
| 892 | + break; | |
| 893 | + } | |
| 894 | + } | |
| 895 | +} | |
| 896 | + | |
| 897 | +/* find a free register. If none, save one register */ | |
| 898 | +int get_reg() | |
| 899 | +{ | |
| 900 | + int r, i, *p; | |
| 901 | + | |
| 902 | + /* find a free register */ | |
| 903 | + for(r=0;r<NB_REGS;r++) { | |
| 904 | + for(p=vstack;p<vstack_ptr;p+=2) { | |
| 905 | + i = p[0] & VT_VALMASK; | |
| 906 | + if (i == r) | |
| 907 | + goto notfound; | |
| 908 | + } | |
| 909 | + return r; | |
| 910 | + notfound: ; | |
| 911 | + } | |
| 912 | + | |
| 913 | + /* no register left : free the first one on the stack (very | |
| 914 | + important to start from the bottom to ensure that we don't | |
| 915 | + spill registers used in gen_op()) */ | |
| 916 | + for(p=vstack;p<vstack_ptr;p+=2) { | |
| 917 | + r = p[0] & VT_VALMASK; | |
| 918 | + if (r < VT_CONST) { | |
| 919 | + save_reg(r); | |
| 920 | + break; | |
| 921 | + } | |
| 922 | + } | |
| 923 | + return r; | |
| 924 | +} | |
| 925 | + | |
| 926 | +void save_regs() | |
| 927 | +{ | |
| 928 | + int r, *p; | |
| 929 | + for(p=vstack;p<vstack_ptr;p+=2) { | |
| 930 | + r = p[0] & VT_VALMASK; | |
| 931 | + if (r < VT_CONST) { | |
| 932 | + save_reg(r); | |
| 933 | + } | |
| 934 | + } | |
| 935 | +} | |
| 936 | + | |
| 937 | +/* move register 's' to 'r', and flush previous value of r to memory | |
| 938 | + if needed */ | |
| 939 | +void move_reg(r, s) | |
| 940 | +{ | |
| 941 | + if (r != s) { | |
| 942 | + save_reg(r); | |
| 943 | + load(r, s, 0); | |
| 944 | + } | |
| 945 | +} | |
| 946 | + | |
| 947 | +/* convert a stack entry in register */ | |
| 948 | +int gvp(int *p) | |
| 949 | +{ | |
| 950 | + int r; | |
| 951 | + r = p[0] & VT_VALMASK; | |
| 952 | + if (r >= VT_CONST || (p[0] & VT_LVAL)) | |
| 953 | + r = get_reg(); | |
| 954 | + load(r, p[0], p[1]); | |
| 955 | + p[0] = (p[0] & VT_TYPE) | r; | |
| 956 | + return r; | |
| 957 | +} | |
| 958 | + | |
| 959 | +void vpush() | |
| 960 | +{ | |
| 961 | + *vstack_ptr++ = vt; | |
| 962 | + *vstack_ptr++ = vc; | |
| 963 | + /* cannot let cpu flags if other instruction are generated */ | |
| 964 | + if ((vt & VT_VALMASK) == VT_CMP) | |
| 965 | + gvp(vstack_ptr - 2); | |
| 966 | +} | |
| 967 | + | |
| 968 | +void vpop(int *ft, int *fc) | |
| 969 | +{ | |
| 970 | + *fc = *--vstack_ptr; | |
| 971 | + *ft = *--vstack_ptr; | |
| 972 | +} | |
| 973 | + | |
| 974 | +/* generate a value in a register from vt and vc */ | |
| 975 | +int gv() | |
| 976 | +{ | |
| 977 | + int r; | |
| 978 | + vpush(); | |
| 979 | + r = gvp(vstack_ptr - 2); | |
| 980 | + vpop(&vt, &vc); | |
| 981 | + return r; | |
| 982 | +} | |
| 983 | + | |
| 984 | +/* handle constant optimizations and various machine independant opt */ | |
| 985 | +void gen_opc(op) | |
| 986 | +{ | |
| 987 | + int fr, ft, fc, r, c1, c2, n; | |
| 988 | + | |
| 989 | + vpop(&ft, &fc); | |
| 990 | + vpop(&vt, &vc); | |
| 991 | + c1 = (vt & (VT_VALMASK | VT_LVAL)) == VT_CONST; | |
| 992 | + c2 = (ft & (VT_VALMASK | VT_LVAL)) == VT_CONST; | |
| 993 | + if (c1 && c2) { | |
| 994 | + switch(op) { | |
| 995 | + case '+': vc += fc; break; | |
| 996 | + case '-': vc -= fc; break; | |
| 997 | + case '&': vc &= fc; break; | |
| 998 | + case '^': vc ^= fc; break; | |
| 999 | + case '|': vc |= fc; break; | |
| 1000 | + case '*': vc *= fc; break; | |
| 1001 | + case TOK_PDIV: | |
| 1002 | + case '/': vc /= fc; break; /* XXX: zero case ? */ | |
| 1003 | + case '%': vc %= fc; break; /* XXX: zero case ? */ | |
| 1004 | + case TOK_UDIV: vc = (unsigned)vc / fc; break; /* XXX: zero case ? */ | |
| 1005 | + case TOK_UMOD: vc = (unsigned)vc % fc; break; /* XXX: zero case ? */ | |
| 1006 | + case TOK_SHL: vc <<= fc; break; | |
| 1007 | + case TOK_SHR: vc = (unsigned)vc >> fc; break; | |
| 1008 | + case TOK_SAR: vc >>= fc; break; | |
| 1009 | + default: | |
| 1010 | + goto general_case; | |
| 1011 | + } | |
| 1012 | + } else { | |
| 1013 | + /* if commutative ops, put c2 as constant */ | |
| 1014 | + if (c1 && (op == '+' || op == '&' || op == '^' || | |
| 1015 | + op == '|' || op == '*')) { | |
| 1016 | + swap(&vt, &ft); | |
| 1017 | + swap(&vc, &fc); | |
| 1018 | + swap(&c1, &c2); | |
| 1019 | + } | |
| 1020 | + if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV || | |
| 1021 | + op == TOK_PDIV) && | |
| 1022 | + fc == 1) || | |
| 1023 | + ((op == '+' || op == '-' || op == '|' || op == '^' || | |
| 1024 | + op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) && | |
| 1025 | + fc == 0) || | |
| 1026 | + (op == '&' && | |
| 1027 | + fc == -1))) { | |
| 1028 | + } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) { | |
| 1029 | + /* try to use shifts instead of muls or divs */ | |
| 1030 | + if (fc > 0 && (fc & (fc - 1)) == 0) { | |
| 1031 | + n = -1; | |
| 1032 | + while (fc) { | |
| 1033 | + fc >>= 1; | |
| 1034 | + n++; | |
| 1035 | + } | |
| 1036 | + fc = n; | |
| 1037 | + if (op == '*') | |
| 1038 | + op = TOK_SHL; | |
| 1039 | + else if (op == TOK_PDIV) | |
| 1040 | + op = TOK_SAR; | |
| 1041 | + else | |
| 1042 | + op = TOK_SHR; | |
| 1043 | + } | |
| 1044 | + goto general_case; | |
| 1045 | + } else { | |
| 1046 | + general_case: | |
| 1047 | + vpush(); | |
| 1048 | + vt = ft; | |
| 1049 | + vc = fc; | |
| 1050 | + vpush(); | |
| 1051 | + r = gvp(vstack_ptr - 4); | |
| 1052 | + fr = gvp(vstack_ptr - 2); | |
| 1053 | + vpop(&ft, &fc); | |
| 1054 | + vpop(&vt, &vc); | |
| 1055 | + /* call low level op generator */ | |
| 1056 | + gen_op1(op, r, fr); | |
| 1057 | + } | |
| 1058 | + } | |
| 1059 | +} | |
| 1060 | + | |
| 1061 | +int pointed_size(t) | |
| 1062 | +{ | |
| 1063 | + return type_size(pointed_type(t), &t); | |
| 1064 | +} | |
| 1065 | + | |
| 1066 | +/* generic gen_op: handles types problems */ | |
| 1067 | +void gen_op(op) | |
| 1068 | +{ | |
| 1069 | + int u, t1, t2; | |
| 1070 | + | |
| 1071 | + vpush(); | |
| 1072 | + t1 = vstack_ptr[-4]; | |
| 1073 | + t2 = vstack_ptr[-2]; | |
| 1074 | + if (op == '+' | op == '-') { | |
| 1075 | + if ((t1 & VT_PTR) && (t2 & VT_PTR)) { | |
| 1076 | + if (op != '-') | |
| 1077 | + error("invalid type"); | |
| 1078 | + /* XXX: check that types are compatible */ | |
| 1079 | + u = pointed_size(t1); | |
| 1080 | + gen_opc(op); | |
| 1081 | + vpush(); | |
| 1082 | + vstack_ptr[-2] &= ~VT_TYPE; /* set to integer */ | |
| 1083 | + vset(VT_CONST, u); | |
| 1084 | + gen_op(TOK_PDIV); | |
| 1085 | + } else if ((t1 | t2) & VT_PTR) { | |
| 1086 | + if (t2 & VT_PTR) { | |
| 1087 | + swap(vstack_ptr - 4, vstack_ptr - 2); | |
| 1088 | + swap(vstack_ptr - 3, vstack_ptr - 1); | |
| 1089 | + swap(&t1, &t2); | |
| 1090 | + } | |
| 1091 | + /* stack-4 contains pointer, stack-2 value to add */ | |
| 1092 | + vset(VT_CONST, pointed_size(vstack_ptr[-4])); | |
| 1093 | + gen_op('*'); | |
| 1094 | + vpush(); | |
| 1095 | + gen_opc(op); | |
| 1096 | + /* put again type if gen_opc() swaped operands */ | |
| 1097 | + vt = (vt & VT_TYPEN) | (t1 & VT_TYPE); | |
| 1098 | + } else { | |
| 1099 | + gen_opc(op); | |
| 1100 | + } | |
| 1101 | + } else { | |
| 1102 | + if ((t1 | t2) & VT_UNSIGNED) { | |
| 1103 | + if (op == TOK_SAR) | |
| 1104 | + op = TOK_SHR; | |
| 1105 | + else if (op == '/') | |
| 1106 | + op = TOK_UDIV; | |
| 1107 | + else if (op == '%') | |
| 1108 | + op = TOK_UMOD; | |
| 1109 | + } | |
| 1110 | + gen_opc(op); | |
| 1111 | + } | |
| 1112 | +} | |
| 1113 | + | |
| 571 | 1114 | /* return type size. Put alignment at 'a' */ |
| 572 | 1115 | int type_size(int t, int *a) |
| 573 | 1116 | { |
| 574 | 1117 | Sym *s; |
| 575 | 1118 | |
| 576 | 1119 | /* int, enum or pointer */ |
| 577 | − if ((t & VT_PTRMASK) >= VT_PTRINC | | |
| 578 | − (t & VT_TYPE) == 0 | | |
| 579 | − (t & VT_ENUM)) { | |
| 580 | − *a = 4; | |
| 581 | − return 4; | |
| 582 | − } else if (t & VT_STRUCT) { | |
| 1120 | + if (t & VT_STRUCT) { | |
| 583 | 1121 | /* struct/union */ |
| 584 | 1122 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 585 | 1123 | *a = 4; /* XXX: cannot store it yet. Doing that is safe */ |
| 586 | 1124 | return s->c; |
| 1125 | + } else if (t & VT_ARRAY) { | |
| 1126 | + s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); | |
| 1127 | + return type_size(s->t, a) * s->c; | |
| 1128 | + } else if ((t & VT_PTR) | | |
| 1129 | + (t & VT_TYPE) == 0 | | |
| 1130 | + (t & VT_ENUM)) { | |
| 1131 | + *a = 4; | |
| 1132 | + return 4; | |
| 587 | 1133 | } else { |
| 588 | 1134 | *a = 1; |
| 589 | 1135 | return 1; |
| 590 | 1136 | } |
| 591 | 1137 | } |
| 592 | 1138 | |
| 593 | −/* return the number size in bytes of a given type */ | |
| 594 | −int incr_value(t) | |
| 1139 | +/* return the pointed type of t */ | |
| 1140 | +int pointed_type(int t) | |
| 595 | 1141 | { |
| 596 | − int a; | |
| 597 | − | |
| 598 | − if ((t & VT_PTRMASK) >= VT_PTRINC) | |
| 599 | − return type_size(t - VT_PTRINC, &a); | |
| 600 | − else | |
| 601 | − return 1; | |
| 1142 | + Sym *s; | |
| 1143 | + s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); | |
| 1144 | + return s->t | (t & VT_TYPEN); | |
| 602 | 1145 | } |
| 603 | 1146 | |
| 604 | −#define POST_ADD 0x1000 | |
| 605 | −#define PRE_ADD 0 | |
| 606 | − | |
| 607 | −/* a defines POST/PRE add. c is the token ++ or -- */ | |
| 608 | −void inc(a, c) | |
| 1147 | +int mk_pointer(int t) | |
| 609 | 1148 | { |
| 610 | − test_lvalue(); | |
| 611 | − vt = vt & VT_LVALN; | |
| 612 | − gv(); | |
| 613 | − o(0x018bc189); /* movl %eax, %ecx ; mov (%ecx), %eax */ | |
| 614 | − o(0x408d | a); /* leal x(%eax), %eax/%edx */ | |
| 615 | − g((c - TOK_MID) * incr_value(vt)); | |
| 616 | − o(0x0189 | a); /* mov %eax/%edx, (%ecx) */ | |
| 1149 | + int p; | |
| 1150 | + p = anon_sym++; | |
| 1151 | + sym_push(p, t, -1); | |
| 1152 | + return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & VT_TYPEN); | |
| 617 | 1153 | } |
| 618 | 1154 | |
| 619 | −/* XXX: handle ptr sub and 'int + ptr' case (only 'ptr + int' handled) */ | |
| 620 | −/* XXX: handle constant propagation (need to track live eax) */ | |
| 621 | −/* XXX: handle unsigned propagation */ | |
| 622 | −void gen_op(op, l) | |
| 1155 | +/* store value in lvalue pushed on stack */ | |
| 1156 | +void vstore() | |
| 623 | 1157 | { |
| 624 | − int t; | |
| 625 | − gv(); | |
| 626 | − t = vt; | |
| 627 | − o(0x50); /* push %eax */ | |
| 628 | − next(); | |
| 629 | − if (l == -1) | |
| 630 | − expr(); | |
| 631 | − else if (l == -2) | |
| 632 | − expr_eq(); | |
| 633 | − else | |
| 634 | − sum(l); | |
| 635 | − gv(); | |
| 636 | − o(0x59); /* pop %ecx */ | |
| 637 | − if (op == '+' | op == '-') { | |
| 638 | − /* XXX: incorrect for short (futur!) */ | |
| 639 | − if (incr_value(t) == 4) | |
| 640 | − o(0x02e0c1); /* shl $2, %eax */ | |
| 641 | − if (op == '-') | |
| 642 | − o(0xd8f7); /* neg %eax */ | |
| 643 | − o(0xc801); /* add %ecx, %eax */ | |
| 644 | − vt = t; | |
| 645 | − } else if (op == '&') | |
| 646 | − o(0xc821); | |
| 647 | − else if (op == '^') | |
| 648 | − o(0xc831); | |
| 649 | − else if (op == '|') | |
| 650 | − o(0xc809); | |
| 651 | − else if (op == '*') | |
| 652 | − o(0xc1af0f); /* imul %ecx, %eax */ | |
| 653 | −#ifndef TINY | |
| 654 | − else if (op == TOK_SHL | op == TOK_SHR) { | |
| 655 | − o(0xd391); /* xchg %ecx, %eax, shl/shr/sar %cl, %eax */ | |
| 656 | − if (op == TOK_SHL) | |
| 657 | − o(0xe0); | |
| 658 | − else if (t & VT_UNSIGNED) | |
| 659 | − o(0xe8); | |
| 660 | − else | |
| 661 | − o(0xf8); | |
| 1158 | + int ft, fc, r, t; | |
| 1159 | + | |
| 1160 | + r = gv(); /* generate value */ | |
| 1161 | + vpush(); | |
| 1162 | + ft = vstack_ptr[-4]; | |
| 1163 | + fc = vstack_ptr[-3]; | |
| 1164 | + if ((ft & VT_VALMASK) == VT_LLOCAL) { | |
| 1165 | + t = get_reg(); | |
| 1166 | + load(t, VT_LOCAL | VT_LVAL, fc); | |
| 1167 | + ft = (ft & ~VT_VALMASK) | t; | |
| 662 | 1168 | } |
| 663 | −#endif | |
| 664 | − else if (op == '/' | op == '%') { | |
| 665 | − o(0x91); /* xchg %ecx, %eax */ | |
| 666 | − if (t & VT_UNSIGNED) { | |
| 667 | − o(0xd231); /* xor %edx, %edx */ | |
| 668 | − o(0xf1f7); /* div %ecx, %eax */ | |
| 669 | − } else { | |
| 670 | − o(0xf9f799); /* cltd, idiv %ecx, %eax */ | |
| 671 | − } | |
| 672 | − if (op == '%') | |
| 673 | − o(0x92); /* xchg %edx, %eax */ | |
| 674 | − } else { | |
| 675 | − o(0xc139); /* cmp %eax,%ecx */ | |
| 676 | − vset(VT_CMP, op); | |
| 1169 | + store(r, ft, fc); | |
| 1170 | + vstack_ptr -= 4; | |
| 1171 | +} | |
| 1172 | + | |
| 1173 | +/* post defines POST/PRE add. c is the token ++ or -- */ | |
| 1174 | +void inc(post, c) | |
| 1175 | +{ | |
| 1176 | + int r, r1; | |
| 1177 | + test_lvalue(); | |
| 1178 | + if (post) | |
| 1179 | + vpush(); /* room for returned value */ | |
| 1180 | + vpush(); /* save lvalue */ | |
| 1181 | + r = gv(); | |
| 1182 | + vpush(); /* save value */ | |
| 1183 | + if (post) { | |
| 1184 | + /* duplicate value */ | |
| 1185 | + r1 = get_reg(); | |
| 1186 | + load(r1, r, 0); /* move r to r1 */ | |
| 1187 | + vstack_ptr[-6] = (vt & VT_TYPE) | r1; | |
| 1188 | + vstack_ptr[-5] = 0; | |
| 677 | 1189 | } |
| 1190 | + /* add constant */ | |
| 1191 | + vset(VT_CONST, c - TOK_MID); | |
| 1192 | + gen_op('+'); | |
| 1193 | + vstore(); /* store value */ | |
| 1194 | + if (post) | |
| 1195 | + vpop(&vt, &vc); | |
| 678 | 1196 | } |
| 679 | 1197 | |
| 680 | 1198 | int expr_const() |
| @@ -685,8 +1203,6 @@ int expr_const() · 4 unmodified lines | ||
| 685 | 1203 | return vc; |
| 686 | 1204 | } |
| 687 | 1205 | |
| 688 | −#ifndef TINY | |
| 689 | − | |
| 690 | 1206 | /* enum/struct/union declaration */ |
| 691 | 1207 | int struct_decl(u) |
| 692 | 1208 | { |
| @@ -695,7 +1211,6 @@ int struct_decl(u) · 2 unmodified lines | ||
| 695 | 1211 | |
| 696 | 1212 | a = tok; /* save decl type */ |
| 697 | 1213 | next(); |
| 698 | − v = 0; | |
| 699 | 1214 | if (tok != '{') { |
| 700 | 1215 | v = tok; |
| 701 | 1216 | next(); |
| @@ -707,6 +1222,8 @@ int struct_decl(u) · 5 unmodified lines | ||
| 707 | 1222 | u = u | (v << VT_STRUCT_SHIFT); |
| 708 | 1223 | return u; |
| 709 | 1224 | } |
| 1225 | + } else { | |
| 1226 | + v = anon_sym++; | |
| 710 | 1227 | } |
| 711 | 1228 | s = sym_push(v | SYM_STRUCT, a, 0); |
| 712 | 1229 | /* put struct/union/enum name in type */ |
| @@ -733,7 +1250,7 @@ int struct_decl(u) · 20 unmodified lines | ||
| 733 | 1250 | } else { |
| 734 | 1251 | b = ist(); |
| 735 | 1252 | while (1) { |
| 736 | − t = typ(&v, b, &size); | |
| 1253 | + t = typ(&v, b); | |
| 737 | 1254 | if (t & (VT_FUNC | VT_TYPEDEF)) |
| 738 | 1255 | error("invalid type"); |
| 739 | 1256 | /* XXX: align & correct type size */ |
| @@ -768,7 +1285,6 @@ int struct_decl(u) · 28 unmodified lines | ||
| 768 | 1285 | } |
| 769 | 1286 | return u; |
| 770 | 1287 | } |
| 771 | −#endif | |
| 772 | 1288 | |
| 773 | 1289 | /* return 0 if no type declaration. otherwise, return the basic type |
| 774 | 1290 | and skip it. |
| @@ -783,16 +1299,18 @@ int ist() · 8 unmodified lines | ||
| 783 | 1299 | while(1) { |
| 784 | 1300 | #ifndef TINY |
| 785 | 1301 | if (tok == TOK_ENUM) { |
| 786 | − t = struct_decl(VT_ENUM); | |
| 1302 | + t |= struct_decl(VT_ENUM); | |
| 787 | 1303 | } else if (tok == TOK_STRUCT || tok == TOK_UNION) { |
| 788 | − t = struct_decl(VT_STRUCT); | |
| 1304 | + t |= struct_decl(VT_STRUCT); | |
| 789 | 1305 | } else |
| 790 | 1306 | #endif |
| 791 | 1307 | { |
| 792 | − if (tok == TOK_CHAR | tok == TOK_VOID) { | |
| 1308 | + if (tok == TOK_CHAR) { | |
| 793 | 1309 | t |= VT_BYTE; |
| 1310 | + } else if (tok == TOK_VOID) { | |
| 1311 | + t |= VT_VOID; | |
| 794 | 1312 | } else if (tok == TOK_INT | |
| 795 | − (tok >= TOK_CONST & tok <= TOK_SIGNED)) { | |
| 1313 | + (tok >= TOK_CONST & tok <= TOK_INLINE)) { | |
| 796 | 1314 | /* ignored types */ |
| 797 | 1315 | } else if (tok == TOK_FLOAT & tok == TOK_DOUBLE) { |
| 798 | 1316 | error("floats not supported"); |
| @@ -817,23 +1335,97 @@ int ist() · 18 unmodified lines | ||
| 817 | 1335 | return t; |
| 818 | 1336 | } |
| 819 | 1337 | |
| 1338 | +int post_type(u, t) | |
| 1339 | +{ | |
| 1340 | + int p, n, pt, l; | |
| 1341 | + | |
| 1342 | + if (tok == '(') { | |
| 1343 | + /* function declaration */ | |
| 1344 | + next(); | |
| 1345 | + /* push a dummy symbol to force local symbol stack usage */ | |
| 1346 | + sym_push1(&local_stack, 0, 0, 0); | |
| 1347 | + p = 4; | |
| 1348 | + l = 0; | |
| 1349 | + while (tok != ')') { | |
| 1350 | + /* read param name and compute offset */ | |
| 1351 | + if (l != 2) { | |
| 1352 | + if (!(pt = ist())) { | |
| 1353 | + if (l) { | |
| 1354 | + error("invalid type"); | |
| 1355 | + } else { | |
| 1356 | + l = 2; | |
| 1357 | + goto old_proto; | |
| 1358 | + } | |
| 1359 | + } | |
| 1360 | + if (pt & VT_VOID && tok == ')') | |
| 1361 | + break; | |
| 1362 | + l = 1; | |
| 1363 | + pt = typ(&n, pt); /* XXX: should accept | |
| 1364 | + both arg/non arg if v == 0 */ | |
| 1365 | + } else { | |
| 1366 | + old_proto: | |
| 1367 | + n = tok; | |
| 1368 | + pt = 0; /* int type */ | |
| 1369 | + next(); | |
| 1370 | + } | |
| 1371 | + /* array must be transformed to pointer according to ANSI C */ | |
| 1372 | + pt &= ~VT_ARRAY; | |
| 1373 | + p = p + 4; | |
| 1374 | + sym_push(n, VT_LOCAL | VT_LVAL | pt, p); | |
| 1375 | + if (tok == ',') { | |
| 1376 | + next(); | |
| 1377 | + if (l == 1 && tok == TOK_DOTS) { | |
| 1378 | + next(); | |
| 1379 | + break; | |
| 1380 | + } | |
| 1381 | + } | |
| 1382 | + } | |
| 1383 | + skip(')'); | |
| 1384 | + t = post_type(0, t); /* XXX: may be incorrect */ | |
| 1385 | + /* transform function pointer to 'char *' */ | |
| 1386 | + if (u) | |
| 1387 | + t = u + VT_BYTE; | |
| 1388 | + else | |
| 1389 | + t = t | VT_FUNC; | |
| 1390 | + } else if (tok == '[') { | |
| 1391 | + /* array definition */ | |
| 1392 | + next(); | |
| 1393 | + n = -1; | |
| 1394 | + if (tok != ']') { | |
| 1395 | + n = expr_const(); | |
| 1396 | + if (n < 0) | |
| 1397 | + error("invalid array size"); | |
| 1398 | + } | |
| 1399 | + skip(']'); | |
| 1400 | + /* parse next post type */ | |
| 1401 | + t = post_type(u, t); | |
| 1402 | + | |
| 1403 | + /* we push a anonymous symbol which will contain the array | |
| 1404 | + element type */ | |
| 1405 | + p = anon_sym++; | |
| 1406 | + sym_push(p, t, n); | |
| 1407 | + t = VT_ARRAY | VT_PTR | (p << VT_STRUCT_SHIFT); | |
| 1408 | + } | |
| 1409 | + return t; | |
| 1410 | +} | |
| 1411 | + | |
| 820 | 1412 | /* Read a type declaration (except basic type), and return the |
| 821 | 1413 | type. If v is true, then also put variable name in 'vc' */ |
| 822 | −int typ(int *v, int t, int *array_size_ptr) | |
| 1414 | +int typ(int *v, int t) | |
| 823 | 1415 | { |
| 824 | − int u, p, n; | |
| 1416 | + int u; | |
| 825 | 1417 | |
| 826 | 1418 | t = t & -3; /* suppress the ored '2' */ |
| 827 | 1419 | while (tok == '*') { |
| 828 | 1420 | next(); |
| 829 | − t = t + VT_PTRINC; | |
| 830 | − } | |
| 1421 | + t = mk_pointer(t); | |
| 1422 | + } | |
| 831 | 1423 | |
| 832 | 1424 | /* recursive type */ |
| 833 | 1425 | /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */ |
| 834 | 1426 | if (tok == '(') { |
| 835 | 1427 | next(); |
| 836 | − u = typ(v, 0, 0); | |
| 1428 | + u = typ(v, 0); | |
| 837 | 1429 | skip(')'); |
| 838 | 1430 | } else { |
| 839 | 1431 | u = 0; |
| @@ -843,51 +1435,7 @@ int typ(int *v, int t, int *array_size_ptr) · 3 unmodified lines | ||
| 843 | 1435 | next(); |
| 844 | 1436 | } |
| 845 | 1437 | } |
| 846 | − while(1) { | |
| 847 | − if (tok == '(') { | |
| 848 | − /* function declaration */ | |
| 849 | − next(); | |
| 850 | − /* push a dummy symbol to force local symbol stack usage */ | |
| 851 | − sym_push1(&local_stack, 0, 0, 0); | |
| 852 | − p = 4; | |
| 853 | − while (tok != ')') { | |
| 854 | − /* read param name and compute offset */ | |
| 855 | − if (t = ist()) | |
| 856 | − t = typ(&n, t, 0); /* XXX: should accept both arg/non arg if v == 0 */ | |
| 857 | − else { | |
| 858 | − n = tok; | |
| 859 | − t = 0; | |
| 860 | − next(); | |
| 861 | − } | |
| 862 | − p = p + 4; | |
| 863 | − sym_push(n, VT_LOCAL | VT_LVAL | t, p); | |
| 864 | − if (tok == ',') | |
| 865 | − next(); | |
| 866 | − } | |
| 867 | − next(); /* skip ')' */ | |
| 868 | − if (u) | |
| 869 | − t = u + VT_BYTE; | |
| 870 | − else | |
| 871 | − t = t | VT_FUNC; | |
| 872 | − } else if (tok == '[') { | |
| 873 | − /* array definition */ | |
| 874 | − if (t & VT_ARRAY) | |
| 875 | − error("multi dimension arrays not supported"); | |
| 876 | − next(); | |
| 877 | − n = 0; | |
| 878 | − if (tok != ']') { | |
| 879 | − n = expr_const(); | |
| 880 | − if (array_size_ptr) | |
| 881 | − *array_size_ptr = n; | |
| 882 | − } | |
| 883 | − if (n <= 0 & array_size_ptr != 0) | |
| 884 | − error("invalid array size"); | |
| 885 | − skip(']'); | |
| 886 | − t = (t + VT_PTRINC) | VT_ARRAY; | |
| 887 | − } else | |
| 888 | − break; | |
| 889 | − } | |
| 890 | − return t; | |
| 1438 | + return post_type(u, t); | |
| 891 | 1439 | } |
| 892 | 1440 | |
| 893 | 1441 | /* define a new external reference to a function 'v' of type 'u' */ |
| @@ -911,88 +1459,65 @@ Sym *external_func(v, u) · 17 unmodified lines | ||
| 911 | 1459 | return s; |
| 912 | 1460 | } |
| 913 | 1461 | |
| 914 | −/* read a number in base b */ | |
| 915 | −int getn(c, b) | |
| 1462 | +/* read a character for string or char constant and eval escape codes */ | |
| 1463 | +int getq() | |
| 916 | 1464 | { |
| 917 | − int n, t; | |
| 918 | − n = 0; | |
| 919 | −#ifndef TINY | |
| 920 | − while (1) { | |
| 921 | − if (c >= 'a') | |
| 922 | − t = c - 'a' + 10; | |
| 923 | − else if (c >= 'A') | |
| 924 | − t = c - 'A' + 10; | |
| 925 | − else | |
| 926 | − t = c - '0'; | |
| 927 | − if (t < 0 | t >= b) | |
| 928 | − break; | |
| 929 | − n = n * b + t; | |
| 930 | − c = inp(); | |
| 931 | − } | |
| 932 | −#else | |
| 933 | − while (isnum(c)) { | |
| 934 | − n = n * b + c - '0'; | |
| 935 | − c = inp(); | |
| 1465 | + int c; | |
| 1466 | + | |
| 1467 | + c = ch; | |
| 1468 | + minp(); | |
| 1469 | + if (c == '\\') { | |
| 1470 | + if (isnum(ch)) { | |
| 1471 | + return getn(8); | |
| 1472 | + } else { | |
| 1473 | + if (ch == 'n') | |
| 1474 | + c = '\n'; | |
| 1475 | + else if (ch == 'r') | |
| 1476 | + c = '\r'; | |
| 1477 | + else if (ch == 't') | |
| 1478 | + c = '\t'; | |
| 1479 | + else | |
| 1480 | + c = ch; | |
| 1481 | + minp(); | |
| 1482 | + } | |
| 936 | 1483 | } |
| 937 | −#endif | |
| 938 | − ungetc(c, file); | |
| 939 | − return n; | |
| 1484 | + return c; | |
| 940 | 1485 | } |
| 941 | 1486 | |
| 942 | −int getq(n) | |
| 1487 | +void indir() | |
| 943 | 1488 | { |
| 944 | − if (n == '\\') { | |
| 945 | − n = inp(); | |
| 946 | − if (n == 'n') | |
| 947 | − n = '\n'; | |
| 948 | −#ifndef TINY | |
| 949 | − else if (n == 'r') | |
| 950 | − n = '\r'; | |
| 951 | − else if (n == 't') | |
| 952 | − n = '\t'; | |
| 953 | −#endif | |
| 954 | − else if (isnum(n)) | |
| 955 | − n = getn(n, 8); | |
| 956 | − } | |
| 957 | − return n; | |
| 1489 | + if (vt & VT_LVAL) | |
| 1490 | + gv(); | |
| 1491 | + if (!(vt & VT_PTR)) | |
| 1492 | + expect("pointer"); | |
| 1493 | + vt = pointed_type(vt); | |
| 1494 | + if (!(vt & VT_ARRAY)) /* an array is never an lvalue */ | |
| 1495 | + vt |= VT_LVAL; | |
| 958 | 1496 | } |
| 959 | 1497 | |
| 960 | 1498 | void unary() |
| 961 | 1499 | { |
| 962 | − int n, t, ft, fc, p; | |
| 1500 | + int n, t, ft, fc, p, r; | |
| 963 | 1501 | Sym *s; |
| 964 | 1502 | |
| 965 | − if (isnum(tok)) { | |
| 966 | − /* number */ | |
| 967 | −#ifndef TINY | |
| 968 | − t = 10; | |
| 969 | − if (tok == '0') { | |
| 970 | − t = 8; | |
| 971 | − tok = inp(); | |
| 972 | − if (tok == 'x') { | |
| 973 | − t = 16; | |
| 974 | − tok = inp(); | |
| 975 | − } | |
| 976 | − } | |
| 977 | − vset(VT_CONST, getn(tok, t)); | |
| 978 | −#else | |
| 979 | − vset(VT_CONST, getn(tok, 10)); | |
| 980 | −#endif | |
| 1503 | + if (tok == TOK_NUM) { | |
| 1504 | + vset(VT_CONST, tokc); | |
| 981 | 1505 | next(); |
| 982 | 1506 | } else |
| 983 | −#ifndef TINY | |
| 984 | 1507 | if (tok == '\'') { |
| 985 | − vset(VT_CONST, getq(inp())); | |
| 1508 | + vset(VT_CONST, getq()); | |
| 986 | 1509 | next(); /* skip char */ |
| 987 | 1510 | skip('\''); |
| 988 | − } else | |
| 989 | −#endif | |
| 990 | − if (tok == '\"') { | |
| 991 | − vset(VT_CONST | VT_PTRINC | VT_BYTE, glo); | |
| 1511 | + } else if (tok == '\"') { | |
| 1512 | + /* generate (char *) type */ | |
| 1513 | + vset(VT_CONST | mk_pointer(VT_TYPE), glo); | |
| 992 | 1514 | while (tok == '\"') { |
| 993 | − while((n = inp()) != 34) { | |
| 994 | − *(char *)glo++ = getq(n); | |
| 1515 | + while (ch != '\"') { | |
| 1516 | + if (ch == -1) | |
| 1517 | + error("unterminated string"); | |
| 1518 | + *(char *)glo++ = getq(); | |
| 995 | 1519 | } |
| 1520 | + minp(); | |
| 996 | 1521 | next(); |
| 997 | 1522 | } |
| 998 | 1523 | *(char *)glo++ = 0; |
| @@ -1002,7 +1527,7 @@ void unary() · 3 unmodified lines | ||
| 1002 | 1527 | if (t == '(') { |
| 1003 | 1528 | /* cast ? */ |
| 1004 | 1529 | if (t = ist()) { |
| 1005 | − ft = typ(0, t, 0); | |
| 1530 | + ft = typ(0, t); | |
| 1006 | 1531 | skip(')'); |
| 1007 | 1532 | unary(); |
| 1008 | 1533 | vt = (vt & VT_TYPEN) | ft; |
| @@ -1012,50 +1537,50 @@ void unary() · 3 unmodified lines | ||
| 1012 | 1537 | } |
| 1013 | 1538 | } else if (t == '*') { |
| 1014 | 1539 | unary(); |
| 1015 | − if (vt & VT_LVAL) | |
| 1016 | − gv(); | |
| 1017 | −#ifndef TINY | |
| 1018 | − if (!(vt & VT_PTRMASK)) | |
| 1019 | − expect("pointer"); | |
| 1020 | −#endif | |
| 1021 | − vt = (vt - VT_PTRINC) | VT_LVAL; | |
| 1540 | + indir(); | |
| 1022 | 1541 | } else if (t == '&') { |
| 1023 | 1542 | unary(); |
| 1024 | 1543 | test_lvalue(); |
| 1025 | − vt = (vt & VT_LVALN) + VT_PTRINC; | |
| 1544 | + vt = mk_pointer(vt & VT_LVALN); | |
| 1026 | 1545 | } else |
| 1027 | −#ifndef TINY | |
| 1028 | 1546 | if (t == '!') { |
| 1029 | 1547 | unary(); |
| 1030 | − if (vt & VT_CMP) | |
| 1548 | + if ((vt & VT_VALMASK) == VT_CMP) | |
| 1031 | 1549 | vc = vc ^ 1; |
| 1032 | 1550 | else |
| 1033 | 1551 | vset(VT_JMP, gtst(1, 0)); |
| 1034 | 1552 | } else |
| 1035 | 1553 | if (t == '~') { |
| 1036 | 1554 | unary(); |
| 1037 | − if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) | |
| 1038 | − vc = ~vc; | |
| 1039 | − else { | |
| 1040 | − gv(); | |
| 1041 | − o(0xd0f7); | |
| 1042 | − } | |
| 1555 | + vpush(); | |
| 1556 | + vset(VT_CONST, -1); | |
| 1557 | + gen_op('^'); | |
| 1043 | 1558 | } else |
| 1044 | 1559 | if (t == '+') { |
| 1045 | 1560 | unary(); |
| 1046 | 1561 | } else |
| 1047 | −#endif | |
| 1562 | + if (t == TOK_SIZEOF) { | |
| 1563 | + /* XXX: some code can be generated */ | |
| 1564 | + if (tok == '(') { | |
| 1565 | + next(); | |
| 1566 | + if (t = ist()) | |
| 1567 | + vt = typ(0, t); | |
| 1568 | + else | |
| 1569 | + expr(); | |
| 1570 | + skip(')'); | |
| 1571 | + } else { | |
| 1572 | + unary(); | |
| 1573 | + } | |
| 1574 | + vset(VT_CONST, type_size(vt, &t)); | |
| 1575 | + } else | |
| 1048 | 1576 | if (t == TOK_INC | t == TOK_DEC) { |
| 1049 | 1577 | unary(); |
| 1050 | − inc(PRE_ADD, t); | |
| 1578 | + inc(0, t); | |
| 1051 | 1579 | } else if (t == '-') { |
| 1580 | + vset(VT_CONST, 0); | |
| 1581 | + vpush(); | |
| 1052 | 1582 | unary(); |
| 1053 | − if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) | |
| 1054 | − vc = -vc; | |
| 1055 | − else { | |
| 1056 | − gv(); | |
| 1057 | − o(0xd8f7); /* neg %eax */ | |
| 1058 | − } | |
| 1583 | + gen_op('-'); | |
| 1059 | 1584 | } else |
| 1060 | 1585 | { |
| 1061 | 1586 | s = sym_find(t); |
| @@ -1076,17 +1601,17 @@ void unary() · 14 unmodified lines | ||
| 1076 | 1601 | /* post operations */ |
| 1077 | 1602 | while (1) { |
| 1078 | 1603 | if (tok == TOK_INC | tok == TOK_DEC) { |
| 1079 | − inc(POST_ADD, tok); | |
| 1604 | + inc(1, tok); | |
| 1080 | 1605 | next(); |
| 1081 | 1606 | } else if (tok == '.' | tok == TOK_ARROW) { |
| 1082 | 1607 | /* field */ |
| 1083 | − if (tok == '.') { | |
| 1084 | − test_lvalue(); | |
| 1085 | − vt = (vt & VT_LVALN) + VT_PTRINC; | |
| 1086 | − } | |
| 1608 | + if (tok == TOK_ARROW) | |
| 1609 | + indir(); | |
| 1610 | + test_lvalue(); | |
| 1611 | + vt &= VT_LVALN; | |
| 1087 | 1612 | next(); |
| 1088 | 1613 | /* expect pointer on structure */ |
| 1089 | − if (!(vt & VT_STRUCT) || (vt & VT_PTRMASK) == 0) | |
| 1614 | + if (!(vt & VT_STRUCT)) | |
| 1090 | 1615 | expect("struct or union"); |
| 1091 | 1616 | s = sym_find(((unsigned)vt >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 1092 | 1617 | /* find field */ |
| @@ -1098,40 +1623,39 @@ void unary() · 5 unmodified lines | ||
| 1098 | 1623 | if (!s) |
| 1099 | 1624 | error("field not found"); |
| 1100 | 1625 | /* add field offset to pointer */ |
| 1101 | − gv(); | |
| 1102 | − if (s->c) | |
| 1103 | − oad(0x05, s->c); | |
| 1626 | + vt = vt & VT_TYPEN; /* change type to int */ | |
| 1627 | + vpush(); | |
| 1628 | + vset(VT_CONST, s->c); | |
| 1629 | + gen_op('+'); | |
| 1104 | 1630 | /* change type to field type, and set to lvalue */ |
| 1105 | 1631 | vt = (vt & VT_TYPEN) | VT_LVAL | s->t; |
| 1106 | 1632 | next(); |
| 1107 | 1633 | } else if (tok == '[') { |
| 1108 | −#ifndef TINY | |
| 1109 | − if (!(vt & VT_PTRMASK)) | |
| 1110 | − expect("pointer"); | |
| 1111 | −#endif | |
| 1112 | − gen_op('+', -1); | |
| 1113 | − /* dereference pointer */ | |
| 1114 | − vt = (vt - VT_PTRINC) | VT_LVAL; | |
| 1634 | + next(); | |
| 1635 | + vpush(); | |
| 1636 | + expr(); | |
| 1637 | + gen_op('+'); | |
| 1638 | + indir(); | |
| 1115 | 1639 | skip(']'); |
| 1116 | 1640 | } else if (tok == '(') { |
| 1117 | 1641 | /* function call */ |
| 1642 | + save_regs(); /* save used temporary registers */ | |
| 1118 | 1643 | /* lvalue is implied */ |
| 1119 | 1644 | vt = vt & VT_LVALN; |
| 1120 | − if ((vt & VT_CONST) == 0) { | |
| 1645 | + if ((vt & VT_VALMASK) != VT_CONST) { | |
| 1121 | 1646 | /* evaluate function address */ |
| 1122 | − gv(); | |
| 1123 | − o(0x50); /* push %eax */ | |
| 1647 | + r = gv(); | |
| 1648 | + o(0x50 + r); /* push r */ | |
| 1124 | 1649 | } |
| 1125 | 1650 | ft = vt; |
| 1126 | 1651 | fc = vc; |
| 1127 | − | |
| 1128 | 1652 | next(); |
| 1129 | 1653 | t = 0; |
| 1130 | 1654 | while (tok != ')') { |
| 1131 | 1655 | t = t + 4; |
| 1132 | 1656 | expr_eq(); |
| 1133 | − gv(); | |
| 1134 | − o(0x50); /* push %eax */ | |
| 1657 | + r = gv(); | |
| 1658 | + o(0x50 + r); /* push r */ | |
| 1135 | 1659 | if (tok == ',') |
| 1136 | 1660 | next(); |
| 1137 | 1661 | } |
| @@ -1148,19 +1672,19 @@ void unary() · 10 unmodified lines | ||
| 1148 | 1672 | n = n + 4; |
| 1149 | 1673 | p = p - 4; |
| 1150 | 1674 | } |
| 1151 | − if (ft & VT_CONST) { | |
| 1675 | + if ((ft & VT_VALMASK) == VT_CONST) { | |
| 1152 | 1676 | /* forward reference */ |
| 1153 | 1677 | if (ft & VT_FORWARD) { |
| 1154 | 1678 | *(int *)fc = psym(0xe8, *(int *)fc); |
| 1155 | 1679 | } else |
| 1156 | 1680 | oad(0xe8, fc - ind - 5); |
| 1157 | − /* return value is variable, and take type from function proto */ | |
| 1158 | − vt = VT_VAR | (ft & VT_TYPE & VT_FUNCN); | |
| 1681 | + /* return value is %eax, and take type from function proto */ | |
| 1682 | + vt = 0 | (ft & VT_TYPE & VT_FUNCN); | |
| 1159 | 1683 | } else { |
| 1160 | 1684 | oad(0x2494ff, t); /* call *xxx(%esp) */ |
| 1161 | 1685 | t = t + 4; |
| 1162 | − /* return value is variable, int */ | |
| 1163 | − vt = VT_VAR; | |
| 1686 | + /* return value is %eax, integer */ | |
| 1687 | + vt = 0; | |
| 1164 | 1688 | } |
| 1165 | 1689 | if (t) |
| 1166 | 1690 | oad(0xc481, t); |
| @@ -1172,60 +1696,52 @@ void unary() · 5 unmodified lines | ||
| 1172 | 1696 | |
| 1173 | 1697 | void uneq() |
| 1174 | 1698 | { |
| 1175 | − int ft, fc, b; | |
| 1699 | + int t; | |
| 1176 | 1700 | |
| 1177 | 1701 | unary(); |
| 1178 | 1702 | if (tok == '=' | |
| 1179 | − (tok >= TOK_A_MOD & TOK_A_DIV) | | |
| 1703 | + (tok >= TOK_A_MOD & tok <= TOK_A_DIV) | | |
| 1180 | 1704 | tok == TOK_A_XOR | tok == TOK_A_OR | |
| 1181 | − tok == TOK_A_SHL | tok == TOK_A_SHR) { | |
| 1705 | + tok == TOK_A_SHL | tok == TOK_A_SAR) { | |
| 1182 | 1706 | test_lvalue(); |
| 1183 | − fc = vc; | |
| 1184 | − ft = vt; | |
| 1185 | − b = (vt & VT_TYPE) == VT_BYTE; | |
| 1186 | − if (ft & VT_VAR) | |
| 1187 | − o(0x50); /* push %eax */ | |
| 1188 | − if (tok == '=') { | |
| 1189 | − next(); | |
| 1707 | + vpush(); | |
| 1708 | + t = tok; | |
| 1709 | + next(); | |
| 1710 | + if (t == '=') { | |
| 1190 | 1711 | expr_eq(); |
| 1191 | −#ifndef TINY | |
| 1192 | − if ((vt & VT_PTRMASK) != (ft & VT_PTRMASK)) | |
| 1712 | + /* XXX: be more precise */ | |
| 1713 | + if ((vt & VT_PTR) != (vstack_ptr[-2] & VT_PTR)) | |
| 1193 | 1714 | warning("incompatible type"); |
| 1194 | −#endif | |
| 1195 | − gv(); /* generate value */ | |
| 1196 | − } else | |
| 1197 | − gen_op(tok & 0x7f, -2); /* XXX: incorrect, must call expr_eq */ | |
| 1198 | − | |
| 1199 | − if (ft & VT_VAR) { | |
| 1200 | − o(0x59); /* pop %ecx */ | |
| 1201 | − o(0x0189 - b); /* mov %eax/%al, (%ecx) */ | |
| 1202 | − } else if (ft & VT_LOCAL) | |
| 1203 | − oad(0x8589 - b, fc); /* mov %eax/%al,xxx(%ebp) */ | |
| 1204 | − else | |
| 1205 | − oad(0xa3 - b, fc); /* mov %eax/%al,xxx */ | |
| 1715 | + } else { | |
| 1716 | + vpush(); | |
| 1717 | + expr_eq(); | |
| 1718 | + gen_op(t & 0x7f); | |
| 1719 | + } | |
| 1720 | + vstore(); | |
| 1206 | 1721 | } |
| 1207 | 1722 | } |
| 1208 | 1723 | |
| 1209 | 1724 | void sum(l) |
| 1210 | 1725 | { |
| 1211 | −#ifndef TINY | |
| 1212 | − int t; | |
| 1213 | −#endif | |
| 1726 | + int ft, fc, t; | |
| 1727 | + | |
| 1214 | 1728 | if (l == 0) |
| 1215 | 1729 | uneq(); |
| 1216 | 1730 | else { |
| 1217 | 1731 | sum(--l); |
| 1218 | 1732 | while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) | |
| 1219 | 1733 | (l == 1 & (tok == '+' | tok == '-')) | |
| 1220 | −#ifndef TINY | |
| 1221 | − (l == 2 & (tok == TOK_SHL | tok == TOK_SHR)) | | |
| 1222 | −#endif | |
| 1734 | + (l == 2 & (tok == TOK_SHL | tok == TOK_SAR)) | | |
| 1223 | 1735 | (l == 3 & (tok >= TOK_LT & tok <= TOK_GT)) | |
| 1224 | 1736 | (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) | |
| 1225 | 1737 | (l == 5 & tok == '&') | |
| 1226 | 1738 | (l == 6 & tok == '^') | |
| 1227 | 1739 | (l == 7 & tok == '|')) { |
| 1228 | − gen_op(tok, l); | |
| 1740 | + vpush(); | |
| 1741 | + t = tok; | |
| 1742 | + next(); | |
| 1743 | + sum(l); | |
| 1744 | + gen_op(t); | |
| 1229 | 1745 | } |
| 1230 | 1746 | } |
| 1231 | 1747 | } |
| @@ -1246,7 +1762,7 @@ void eand() · 14 unmodified lines | ||
| 1246 | 1762 | if (tok != TOK_LAND) { |
| 1247 | 1763 | if (t) { |
| 1248 | 1764 | t = gtst(1, t); |
| 1249 | − vset(VT_JMP | 1, t); | |
| 1765 | + vset(VT_JMPI, t); | |
| 1250 | 1766 | } |
| 1251 | 1767 | break; |
| 1252 | 1768 | } |
| @@ -1287,7 +1803,7 @@ void expr_eq() · 34 unmodified lines | ||
| 1287 | 1803 | expr(); |
| 1288 | 1804 | gv(); |
| 1289 | 1805 | skip(':'); |
| 1290 | − u = psym(0xe9, 0); | |
| 1806 | + u = gjmp(0); | |
| 1291 | 1807 | gsym(t); |
| 1292 | 1808 | expr_eq(); |
| 1293 | 1809 | gv(); |
| @@ -1307,7 +1823,7 @@ void expr() · 13 unmodified lines | ||
| 1307 | 1823 | |
| 1308 | 1824 | #endif |
| 1309 | 1825 | |
| 1310 | −void block(int *bsym, int *csym, int *case_sym, int *def_sym) | |
| 1826 | +void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) | |
| 1311 | 1827 | { |
| 1312 | 1828 | int a, b, c, d; |
| 1313 | 1829 | Sym *s; |
| @@ -1319,13 +1835,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 5 unmodified lines | ||
| 1319 | 1835 | expr(); |
| 1320 | 1836 | skip(')'); |
| 1321 | 1837 | a = gtst(1, 0); |
| 1322 | − block(bsym, csym, case_sym, def_sym); | |
| 1838 | + block(bsym, csym, case_sym, def_sym, case_reg); | |
| 1323 | 1839 | c = tok; |
| 1324 | 1840 | if (c == TOK_ELSE) { |
| 1325 | 1841 | next(); |
| 1326 | − d = psym(0xe9, 0); /* jmp */ | |
| 1842 | + d = gjmp(0); | |
| 1327 | 1843 | gsym(a); |
| 1328 | − block(bsym, csym, case_sym, def_sym); | |
| 1844 | + block(bsym, csym, case_sym, def_sym, case_reg); | |
| 1329 | 1845 | gsym(d); /* patch else jmp */ |
| 1330 | 1846 | } else |
| 1331 | 1847 | gsym(a); |
| @@ -1337,7 +1853,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 5 unmodified lines | ||
| 1337 | 1853 | skip(')'); |
| 1338 | 1854 | a = gtst(1, 0); |
| 1339 | 1855 | b = 0; |
| 1340 | − block(&a, &b, case_sym, def_sym); | |
| 1856 | + block(&a, &b, case_sym, def_sym, case_reg); | |
| 1341 | 1857 | oad(0xe9, d - ind - 5); /* jmp */ |
| 1342 | 1858 | gsym(a); |
| 1343 | 1859 | gsym_addr(b, d); |
| @@ -1347,7 +1863,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines | ||
| 1347 | 1863 | s = local_stack; |
| 1348 | 1864 | decl(VT_LOCAL); |
| 1349 | 1865 | while (tok != '}') |
| 1350 | − block(bsym, csym, case_sym, def_sym); | |
| 1866 | + block(bsym, csym, case_sym, def_sym, case_reg); | |
| 1351 | 1867 | /* pop locally defined symbols */ |
| 1352 | 1868 | sym_pop(&local_stack, s); |
| 1353 | 1869 | next(); |
| @@ -1355,22 +1871,22 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 1 unmodified line | ||
| 1355 | 1871 | next(); |
| 1356 | 1872 | if (tok != ';') { |
| 1357 | 1873 | expr(); |
| 1358 | − gv(); | |
| 1874 | + move_reg(0, gv()); | |
| 1359 | 1875 | } |
| 1360 | 1876 | skip(';'); |
| 1361 | − rsym = psym(0xe9, rsym); /* jmp */ | |
| 1877 | + rsym = gjmp(rsym); /* jmp */ | |
| 1362 | 1878 | } else if (tok == TOK_BREAK) { |
| 1363 | 1879 | /* compute jump */ |
| 1364 | 1880 | if (!bsym) |
| 1365 | 1881 | error("cannot break"); |
| 1366 | − *bsym = psym(0xe9, *bsym); | |
| 1882 | + *bsym = gjmp(*bsym); | |
| 1367 | 1883 | next(); |
| 1368 | 1884 | skip(';'); |
| 1369 | 1885 | } else if (tok == TOK_CONTINUE) { |
| 1370 | 1886 | /* compute jump */ |
| 1371 | 1887 | if (!csym) |
| 1372 | 1888 | error("cannot continue"); |
| 1373 | − *csym = psym(0xe9, *csym); | |
| 1889 | + *csym = gjmp(*csym); | |
| 1374 | 1890 | next(); |
| 1375 | 1891 | skip(';'); |
| 1376 | 1892 | } else |
| @@ -1392,14 +1908,14 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 15 unmodified lines | ||
| 1392 | 1908 | } |
| 1393 | 1909 | skip(';'); |
| 1394 | 1910 | if (tok != ')') { |
| 1395 | − e = psym(0xe9, 0); | |
| 1911 | + e = gjmp(0); | |
| 1396 | 1912 | c = ind; |
| 1397 | 1913 | expr(); |
| 1398 | 1914 | oad(0xe9, d - ind - 5); /* jmp */ |
| 1399 | 1915 | gsym(e); |
| 1400 | 1916 | } |
| 1401 | 1917 | skip(')'); |
| 1402 | − block(&a, &b, case_sym, def_sym); | |
| 1918 | + block(&a, &b, case_sym, def_sym, case_reg); | |
| 1403 | 1919 | oad(0xe9, c - ind - 5); /* jmp */ |
| 1404 | 1920 | gsym(a); |
| 1405 | 1921 | gsym_addr(b, c); |
| @@ -1409,7 +1925,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines | ||
| 1409 | 1925 | a = 0; |
| 1410 | 1926 | b = 0; |
| 1411 | 1927 | d = ind; |
| 1412 | − block(&a, &b, case_sym, def_sym); | |
| 1928 | + block(&a, &b, case_sym, def_sym, case_reg); | |
| 1413 | 1929 | skip(TOK_WHILE); |
| 1414 | 1930 | skip('('); |
| 1415 | 1931 | gsym(b); |
| @@ -1423,12 +1939,12 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 7 unmodified lines | ||
| 1423 | 1939 | next(); |
| 1424 | 1940 | skip('('); |
| 1425 | 1941 | expr(); |
| 1426 | − gv(); | |
| 1942 | + case_reg = gv(); | |
| 1427 | 1943 | skip(')'); |
| 1428 | 1944 | a = 0; |
| 1429 | 1945 | b = 0; |
| 1430 | 1946 | c = 0; |
| 1431 | − block(&a, csym, &b, &c); | |
| 1947 | + block(&a, csym, &b, &c, case_reg); | |
| 1432 | 1948 | /* if no default, jmp after switch */ |
| 1433 | 1949 | if (c == 0) |
| 1434 | 1950 | c = ind; |
| @@ -1443,10 +1959,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 8 unmodified lines | ||
| 1443 | 1959 | if (!case_sym) |
| 1444 | 1960 | expect("switch"); |
| 1445 | 1961 | gsym(*case_sym); |
| 1446 | − oad(0x3d, a); /* cmp $xxx, %eax */ | |
| 1447 | − *case_sym = psym(0x850f, 0); /* jne xxx */ | |
| 1962 | + vset(case_reg, 0); | |
| 1963 | + vpush(); | |
| 1964 | + vset(VT_CONST, a); | |
| 1965 | + gen_op(TOK_EQ); | |
| 1966 | + *case_sym = gtst(1, 0); | |
| 1448 | 1967 | skip(':'); |
| 1449 | − block(bsym, csym, case_sym, def_sym); | |
| 1968 | + block(bsym, csym, case_sym, def_sym, case_reg); | |
| 1450 | 1969 | } else |
| 1451 | 1970 | if (tok == TOK_DEFAULT) { |
| 1452 | 1971 | next(); |
| @@ -1456,7 +1975,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines | ||
| 1456 | 1975 | if (*def_sym) |
| 1457 | 1976 | error("too many 'default'"); |
| 1458 | 1977 | *def_sym = ind; |
| 1459 | − block(bsym, csym, case_sym, def_sym); | |
| 1978 | + block(bsym, csym, case_sym, def_sym, case_reg); | |
| 1460 | 1979 | } else |
| 1461 | 1980 | if (tok == TOK_GOTO) { |
| 1462 | 1981 | next(); |
| @@ -1466,7 +1985,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines | ||
| 1466 | 1985 | s = sym_push1(&label_stack, tok, VT_FORWARD, 0); |
| 1467 | 1986 | /* label already defined */ |
| 1468 | 1987 | if (s->t & VT_FORWARD) |
| 1469 | − s->c = psym(0xe9, s->c); /* jmp xxx */ | |
| 1988 | + s->c = gjmp(s->c); /* jmp xxx */ | |
| 1470 | 1989 | else |
| 1471 | 1990 | oad(0xe9, s->c - ind - 5); /* jmp xxx */ |
| 1472 | 1991 | next(); |
| @@ -1489,7 +2008,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 16 unmodified lines | ||
| 1489 | 2008 | } else { |
| 1490 | 2009 | sym_push1(&label_stack, b, 0, ind); |
| 1491 | 2010 | } |
| 1492 | − block(bsym, csym, case_sym, def_sym); | |
| 2011 | + block(bsym, csym, case_sym, def_sym, case_reg); | |
| 1493 | 2012 | } else { |
| 1494 | 2013 | /* expression case: go backward of one token */ |
| 1495 | 2014 | /* XXX: currently incorrect if number/string/char */ |
| @@ -1506,8 +2025,8 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 10 unmodified lines | ||
| 1506 | 2025 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 1507 | 2026 | void decl(l) |
| 1508 | 2027 | { |
| 1509 | − int *a, t, b, s, align, v, u, n; | |
| 1510 | − Sym *sym, *slocal; | |
| 2028 | + int *a, t, b, size, align, v, u, n; | |
| 2029 | + Sym *sym; | |
| 1511 | 2030 | |
| 1512 | 2031 | while (b = ist()) { |
| 1513 | 2032 | if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') { |
| @@ -1516,9 +2035,7 @@ void decl(l) · 2 unmodified lines | ||
| 1516 | 2035 | continue; |
| 1517 | 2036 | } |
| 1518 | 2037 | while (1) { /* iterate thru each declaration */ |
| 1519 | − s = 1; | |
| 1520 | − slocal = local_stack; /* save local stack position, to restore it */ | |
| 1521 | − t = typ(&v, b, &s); | |
| 2038 | + t = typ(&v, b); | |
| 1522 | 2039 | if (tok == '{') { |
| 1523 | 2040 | /* patch forward references */ |
| 1524 | 2041 | if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) { |
| @@ -1533,7 +2050,7 @@ void decl(l) · 8 unmodified lines | ||
| 1533 | 2050 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 1534 | 2051 | a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */ |
| 1535 | 2052 | rsym = 0; |
| 1536 | − block(0, 0, 0, 0); | |
| 2053 | + block(0, 0, 0, 0, 0); | |
| 1537 | 2054 | gsym(rsym); |
| 1538 | 2055 | o(0xc3c9); /* leave, ret */ |
| 1539 | 2056 | *a = (-loc + 3) & -4; /* align local size to word & |
| @@ -1542,13 +2059,12 @@ void decl(l) · 2 unmodified lines | ||
| 1542 | 2059 | sym_pop(&local_stack, 0); /* reset local stack */ |
| 1543 | 2060 | break; |
| 1544 | 2061 | } else { |
| 1545 | − /* reset local stack (needed because of dummy function | |
| 1546 | − parameters */ | |
| 1547 | − sym_pop(&local_stack, slocal); | |
| 1548 | − if (t & VT_TYPEDEF) { | |
| 2062 | + if (b & VT_TYPEDEF) { | |
| 1549 | 2063 | /* save typedefed type */ |
| 1550 | − sym_push(v, t, 0); | |
| 2064 | + sym_push(v, t | VT_TYPEDEF, 0); | |
| 1551 | 2065 | } else if (t & VT_FUNC) { |
| 2066 | + /* XXX: incorrect to flush, but needed while | |
| 2067 | + waiting for function prototypes */ | |
| 1552 | 2068 | /* external function definition */ |
| 1553 | 2069 | external_func(v, t); |
| 1554 | 2070 | } else { |
| @@ -1567,19 +2083,18 @@ void decl(l) · 12 unmodified lines | ||
| 1567 | 2083 | if (t & VT_STATIC) |
| 1568 | 2084 | u = VT_CONST; |
| 1569 | 2085 | u |= t; |
| 1570 | − if (t & VT_ARRAY) | |
| 1571 | − t -= VT_PTRINC; | |
| 1572 | − align = type_size(t, &align); | |
| 1573 | − s *= align; | |
| 1574 | − if (u & VT_LOCAL) { | |
| 2086 | + size = type_size(t, &align); | |
| 2087 | + if (size < 0) | |
| 2088 | + error("invalid size"); | |
| 2089 | + if ((u & VT_VALMASK) == VT_LOCAL) { | |
| 1575 | 2090 | /* allocate space down on the stack */ |
| 1576 | − loc = (loc - s) & -align; | |
| 2091 | + loc = (loc - size) & -align; | |
| 1577 | 2092 | sym_push(v, u, loc); |
| 1578 | 2093 | } else { |
| 1579 | 2094 | /* allocate space up in the data space */ |
| 1580 | 2095 | glo = (glo + align - 1) & -align; |
| 1581 | 2096 | sym_push(v, u, glo); |
| 1582 | − glo += s; | |
| 2097 | + glo += size; | |
| 1583 | 2098 | } |
| 1584 | 2099 | } |
| 1585 | 2100 | } |
| @@ -1598,35 +2113,34 @@ int main(int c, char **v) · 12 unmodified lines | ||
| 1598 | 2113 | Sym *s; |
| 1599 | 2114 | int (*t)(); |
| 1600 | 2115 | if (c < 2) { |
| 1601 | − printf("usage: tc src\n"); | |
| 2116 | + printf("usage: tcc source ...\n"); | |
| 1602 | 2117 | return 1; |
| 1603 | 2118 | } |
| 1604 | 2119 | v++; |
| 1605 | 2120 | filename = *v; |
| 2121 | + line_num = 1; | |
| 1606 | 2122 | file = fopen(filename, "r"); |
| 1607 | −#ifndef TINY | |
| 1608 | 2123 | if (!file) { |
| 1609 | 2124 | perror(filename); |
| 1610 | 2125 | exit(1); |
| 1611 | 2126 | } |
| 1612 | −#endif | |
| 1613 | − | |
| 2127 | + include_stack_ptr = include_stack; | |
| 2128 | + | |
| 1614 | 2129 | idtable = malloc(SYM_TABLE_SIZE); |
| 1615 | −#ifdef TINY | |
| 1616 | − memcpy(idtable, | |
| 1617 | − "int\0void\0char\0if\0else\0while\0break\0return\0define\0main", 53); | |
| 1618 | − idptr = idtable + 53; | |
| 1619 | −#else | |
| 1620 | 2130 | memcpy(idtable, |
| 1621 | − "int\0void\0char\0if\0else\0while\0break\0return\0define\0main\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0float\0double\0struct\0union\0typedef\0default\0enum", 192); | |
| 1622 | − idptr = idtable + 192; | |
| 1623 | −#endif | |
| 2131 | + "int\0void\0char\0if\0else\0while\0break\0return\0define\0main\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0float\0double\0struct\0union\0typedef\0default\0enum\0sizeof\0include", 219); | |
| 2132 | + idptr = idtable + 219; | |
| 2133 | + | |
| 1624 | 2134 | glo = malloc(DATA_SIZE); |
| 2135 | + memset((void *)glo, 0, DATA_SIZE); | |
| 1625 | 2136 | prog = malloc(TEXT_SIZE); |
| 1626 | − macro_stack = malloc(256); | |
| 2137 | + vstack = malloc(256); | |
| 2138 | + vstack_ptr = vstack; | |
| 1627 | 2139 | macro_stack_ptr = macro_stack; |
| 2140 | + anon_sym = 1 << (31 - VT_STRUCT_SHIFT); | |
| 1628 | 2141 | ind = prog; |
| 1629 | − line_num = 1; | |
| 2142 | + inp(); | |
| 2143 | + ch = '\n'; /* needed to parse correctly first preprocessor command */ | |
| 1630 | 2144 | next(); |
| 1631 | 2145 | decl(VT_CONST); |
| 1632 | 2146 | #ifdef TEST |