Unified · Split
| @@ -17,12 +17,17 @@ · 16 unmodified lines | |||
| 17 | * along with this program; if not, write to the Free Software | 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 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 | 22 | #define INCLUDE_PATH "/usr/include" |
| 23 | #define DATA_SIZE 2000 | 23 | |
| 24 | #define SYM_TABLE_SIZE 10000 | 24 | #define TEXT_SIZE 50000 |
| 25 | #define VAR_TABLE_SIZE 4096 | 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 | /* symbol management */ | 32 | /* symbol management */ |
| 28 | typedef struct Sym { | 33 | typedef struct Sym { |
| @@ -36,68 +41,62 @@ typedef struct Sym { · 7 unmodified lines | |||
| 36 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ | 41 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ |
| 37 | #define SYM_FIELD 0x20000000 /* struct/union field symbol space */ | 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 | /* loc : local variable index | 50 | /* loc : local variable index |
| 40 | glo : global variable index | 51 | glo : global variable index |
| 41 | parm : parameter variable index | ||
| 42 | ind : output code ptr | 52 | ind : output code ptr |
| 43 | rsym: return symbol | 53 | rsym: return symbol |
| 44 | prog: output code | 54 | prog: output code |
| 45 | astk: arg position stack | 55 | anon_sym: anonymous symbol index |
| 46 | */ | 56 | */ |
| 47 | void *file; | 57 | FILE *file; |
| 48 | int tok, tok1, rsym, | 58 | int tok, tok1, tokc, rsym, anon_sym, |
| 49 | prog, ind, loc, glo, vt, | 59 | prog, ind, loc, glo, vt, *vstack, *vstack_ptr, |
| 50 | vc, *macro_stack, *macro_stack_ptr, line_num; | 60 | vc, line_num; |
| 51 | char *idtable, *idptr, *filename; | 61 | char *idtable, *idptr, *filename; |
| 52 | Sym *define_stack, *global_stack, *local_stack, *label_stack; | 62 | Sym *define_stack, *global_stack, *local_stack, *label_stack; |
| 53 | 63 | ||
| 54 | /* The current value can be: */ | 64 | char *macro_stack[MACRO_STACK_SIZE], **macro_stack_ptr, *macro_ptr; |
| 55 | #define VT_CONST 0x0002 /* constant in vc */ | 65 | IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr; |
| 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 */ | ||
| 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 | #define VT_LVALN -17 /* ~VT_LVAL */ | 77 | #define VT_LVALN -17 /* ~VT_LVAL */ |
| 65 | 78 | #define VT_FORWARD 0x0020 /* value is forward reference | |
| 66 | /* | 79 | (only used for functions) */ |
| 67 | * | 80 | |
| 68 | * VT_FUNC indicates a function. The return type is the stored type. A | 81 | #define VT_VOID 0x00040 |
| 69 | * function pointer is stored as a 'char' pointer. | 82 | #define VT_BYTE 0x00080 /* byte type */ |
| 70 | * | 83 | #define VT_PTR 0x00100 /* pointer increment */ |
| 71 | * If VT_PTRMASK is non nul, then it indicates the number of pointer | 84 | #define VT_UNSIGNED 0x00200 /* unsigned type */ |
| 72 | * iterations to reach the basic type. | 85 | #define VT_ARRAY 0x00400 /* array type (only used in parsing) */ |
| 73 | * | 86 | #define VT_ENUM 0x00800 /* enum definition */ |
| 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 */ | ||
| 86 | #define VT_FUNC 0x01000 /* function type */ | 87 | #define VT_FUNC 0x01000 /* function type */ |
| 87 | #define VT_UNSIGNED 0x02000 /* unsigned type */ | 88 | #define VT_STRUCT 0x002000 /* struct/union definition */ |
| 88 | #define VT_ARRAY 0x04000 /* array type (only used in parsing) */ | 89 | #define VT_TYPEDEF 0x004000 /* typedef definition */ |
| 89 | #define VT_TYPE 0xffffff01 /* type mask */ | ||
| 90 | #define VT_TYPEN 0x000000fe /* ~VT_TYPE */ | ||
| 91 | #define VT_FUNCN -4097 /* ~VT_FUNC */ | ||
| 92 | |||
| 93 | #define VT_EXTERN 0x008000 /* extern definition */ | 90 | #define VT_EXTERN 0x008000 /* extern definition */ |
| 94 | #define VT_STATIC 0x010000 /* static variable */ | 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 | /* Special infos */ | 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 | /* token values */ | 101 | /* token values */ |
| 103 | #define TOK_INT 256 | 102 | #define TOK_INT 256 |
| @@ -126,15 +125,19 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 22 unmodified lines | |||
| 126 | #define TOK_LONG 277 | 125 | #define TOK_LONG 277 |
| 127 | #define TOK_REGISTER 278 | 126 | #define TOK_REGISTER 278 |
| 128 | #define TOK_SIGNED 279 | 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_FLOAT 282 /* unsupported */ |
| 131 | #define TOK_DOUBLE 281 /* unsupported */ | 132 | #define TOK_DOUBLE 283 /* unsupported */ |
| 132 | 133 | ||
| 133 | #define TOK_STRUCT 282 | 134 | #define TOK_STRUCT 284 |
| 134 | #define TOK_UNION 283 | 135 | #define TOK_UNION 285 |
| 135 | #define TOK_TYPEDEF 284 | 136 | #define TOK_TYPEDEF 286 |
| 136 | #define TOK_DEFAULT 285 | 137 | #define TOK_DEFAULT 287 |
| 137 | #define TOK_ENUM 286 | 138 | #define TOK_ENUM 288 |
| 139 | #define TOK_SIZEOF 289 | ||
| 140 | #define TOK_INCLUDE 290 | ||
| 138 | 141 | ||
| 139 | #define TOK_EQ 0x94 /* warning: depend on asm code */ | 142 | #define TOK_EQ 0x94 /* warning: depend on asm code */ |
| 140 | #define TOK_NE 0x95 /* warning: depend on asm code */ | 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 | #define TOK_LE 0x9e /* warning: depend on asm code */ | 146 | #define TOK_LE 0x9e /* warning: depend on asm code */ |
| 144 | #define TOK_GT 0x9f /* warning: depend on asm code */ | 147 | #define TOK_GT 0x9f /* warning: depend on asm code */ |
| 145 | 148 | ||
| 146 | #define TOK_LAND 0xa0 | 149 | #define TOK_LAND 0xa0 |
| 147 | #define TOK_LOR 0xa1 | 150 | #define TOK_LOR 0xa1 |
| 148 | 151 | ||
| 149 | #define TOK_DEC 0xa2 | 152 | #define TOK_DEC 0xa2 |
| 150 | #define TOK_MID 0xa3 /* inc/dec, to void constant */ | 153 | #define TOK_MID 0xa3 /* inc/dec, to void constant */ |
| 151 | #define TOK_INC 0xa4 | 154 | #define TOK_INC 0xa4 |
| 152 | #define TOK_ARROW 0xa7 | 155 | #define TOK_ARROW 0xa7 |
| 153 | 156 | #define TOK_DOTS 0xa8 /* three dots */ | |
| 154 | #define TOK_SHL 0x01 | 157 | #define TOK_SHR 0xa9 /* unsigned shift right */ |
| 155 | #define TOK_SHR 0x02 | 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 | /* assignement operators : normal operator or 0x80 */ | 166 | /* assignement operators : normal operator or 0x80 */ |
| 158 | #define TOK_A_MOD 0xa5 | 167 | #define TOK_A_MOD 0xa5 |
| @@ -164,30 +173,16 @@ Sym *define_stack, *global_stack, *local_stack, *label_stack; · 5 unmodified lines | |||
| 164 | #define TOK_A_XOR 0xde | 173 | #define TOK_A_XOR 0xde |
| 165 | #define TOK_A_OR 0xfc | 174 | #define TOK_A_OR 0xfc |
| 166 | #define TOK_A_SHL 0x81 | 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 | void sum(); | 178 | void sum(); |
| 173 | void next(); | 179 | void next(); |
| 174 | void expr_eq(); | 180 | void expr_eq(); |
| 175 | void expr(); | 181 | void expr(); |
| 176 | void decl(); | 182 | void decl(); |
| 177 | #endif | 183 | int gv(); |
| 178 | 184 | void move_reg(); | |
| 179 | 185 | void save_reg(); | |
| 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 | } | ||
| 191 | 186 | ||
| 192 | int isid(c) | 187 | int isid(c) |
| 193 | { | 188 | { |
| @@ -297,7 +292,7 @@ Sym *sym_find(int v) · 103 unmodified lines | |||
| 297 | /* push a given symbol on the symbol stack */ | 292 | /* push a given symbol on the symbol stack */ |
| 298 | Sym *sym_push(int v, int t, int c) | 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 | if (local_stack) | 296 | if (local_stack) |
| 302 | return sym_push1(&local_stack, v, t, c); | 297 | return sym_push1(&local_stack, v, t, c); |
| 303 | else | 298 | else |
| @@ -312,17 +307,214 @@ void sym_pop(Sym **ps, Sym *b) · 8 unmodified lines | |||
| 312 | s = *ps; | 307 | s = *ps; |
| 313 | while(s != b) { | 308 | while(s != b) { |
| 314 | ss = s->prev; | 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 | free(s); | 311 | free(s); |
| 317 | s = ss; | 312 | s = ss; |
| 318 | } | 313 | } |
| 319 | *ps = b; | 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 | void next() | 515 | void next() |
| 324 | { | 516 | { |
| 325 | int c, v; | 517 | int v, b; |
| 326 | char *q, *p; | 518 | char *q, *p; |
| 327 | Sym *s; | 519 | Sym *s; |
| 328 | 520 | ||
| @@ -332,67 +524,30 @@ void next() · 3 unmodified lines | |||
| 332 | tok1 = 0; | 524 | tok1 = 0; |
| 333 | return; | 525 | return; |
| 334 | } | 526 | } |
| 335 | 527 | redo: | |
| 528 | /* skip spaces */ | ||
| 336 | while(1) { | 529 | while(1) { |
| 337 | c = inp(); | 530 | while (ch == '\n') { |
| 338 | #ifndef TINY | 531 | cinp(); |
| 339 | if (c == '/') { | 532 | while (ch == ' ' || ch == 9) |
| 340 | /* comments */ | 533 | cinp(); |
| 341 | c = inp(); | 534 | if (ch == '#') { |
| 342 | if (c == '/') { | 535 | /* preprocessor command if # at start of line after |
| 343 | /* single line comments */ | 536 | spaces */ |
| 344 | while (c != '\n') | 537 | preprocess(); |
| 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)); | ||
| 373 | } | 538 | } |
| 374 | /* ignore preprocessor or shell */ | ||
| 375 | while (c != '\n') | ||
| 376 | c = inp(); | ||
| 377 | } | 539 | } |
| 378 | if (c == '\n') { | 540 | if (ch != ' ' && ch != 9) |
| 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) | ||
| 386 | break; | 541 | break; |
| 542 | cinp(); | ||
| 387 | } | 543 | } |
| 388 | if (isid(c)) { | 544 | if (isid(ch)) { |
| 389 | q = idptr; | 545 | q = idptr; |
| 390 | while(isid(c) | isnum(c)) { | 546 | while(isid(ch) | isnum(ch)) { |
| 391 | *q++ = c; | 547 | *q++ = ch; |
| 392 | c = inp(); | 548 | cinp(); |
| 393 | } | 549 | } |
| 394 | *q++ = '\0'; | 550 | *q++ = '\0'; |
| 395 | ungetc(c, file); | ||
| 396 | p = idtable; | 551 | p = idtable; |
| 397 | tok = 256; | 552 | tok = 256; |
| 398 | while (p < idptr) { | 553 | while (p < idptr) { |
| @@ -401,48 +556,86 @@ void next() · 2 unmodified lines | |||
| 401 | while (*p++); | 556 | while (*p++); |
| 402 | tok++; | 557 | tok++; |
| 403 | } | 558 | } |
| 559 | // printf("id=%s\n", idptr); | ||
| 404 | /* if not found, add symbol */ | 560 | /* if not found, add symbol */ |
| 405 | if (p == idptr) | 561 | if (p == idptr) |
| 406 | idptr = q; | 562 | idptr = q; |
| 407 | /* eval defines */ | 563 | /* if symbol is a define, prepare substitution */ |
| 408 | if (s = sym_find1(define_stack, tok)) { | 564 | if (s = sym_find1(define_stack, tok)) { |
| 409 | *macro_stack_ptr++ = ftell(file); | 565 | if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE) |
| 410 | fseek(file, s->c, 0); | 566 | error("too many nested macros"); |
| 411 | next(); | 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 | } else { | 586 | } else { |
| 414 | #ifdef TINY | 587 | #ifdef TINY |
| 415 | q = "<=\236>=\235!=\225++\244--\242==\224"; | 588 | q = "<=\236>=\235!=\225++\244--\242==\224"; |
| 416 | #else | 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 | #endif | 591 | #endif |
| 419 | /* two chars */ | 592 | /* two chars */ |
| 420 | v = inp(); | 593 | tok = ch; |
| 594 | cinp(); | ||
| 421 | while (*q) { | 595 | while (*q) { |
| 422 | if (*q == c & q[1] == v) { | 596 | if (*q == tok & q[1] == ch) { |
| 597 | cinp(); | ||
| 423 | tok = q[2] & 0xff; | 598 | tok = q[2] & 0xff; |
| 424 | if (tok == TOK_SHL | tok == TOK_SHR) { | 599 | /* three chars tests */ |
| 425 | v = inp(); | 600 | if (tok == TOK_SHL | tok == TOK_SAR) { |
| 426 | if (v == '=') | 601 | if (ch == '=') { |
| 427 | tok = tok | 0x80; | 602 | tok = tok | 0x80; |
| 428 | else | 603 | cinp(); |
| 429 | ungetc(v, file); | 604 | } |
| 605 | } else if (tok == TOK_DOTS) { | ||
| 606 | if (ch != '.') | ||
| 607 | error("parse error"); | ||
| 608 | cinp(); | ||
| 430 | } | 609 | } |
| 431 | return; | 610 | return; |
| 432 | } | 611 | } |
| 433 | q = q + 3; | 612 | q = q + 3; |
| 434 | } | 613 | } |
| 435 | ungetc(v, file); | ||
| 436 | /* single char substitutions */ | 614 | /* single char substitutions */ |
| 437 | if (c == '<') | 615 | if (tok == '<') |
| 438 | tok = TOK_LT; | 616 | tok = TOK_LT; |
| 439 | else if (c == '>') | 617 | else if (tok == '>') |
| 440 | tok = TOK_GT; | 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 | void g(c) | 639 | void g(c) |
| 447 | { | 640 | { |
| 448 | *(char *)ind++ = c; | 641 | *(char *)ind++ = c; |
| @@ -486,195 +679,520 @@ int oad(c, s) · 37 unmodified lines | |||
| 486 | return s; | 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 | /* XXX: generate correct pointer for forward references to functions */ | 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 | 686 | int v, t; |
| 500 | int t; | 687 | |
| 501 | #endif | 688 | v = ft & VT_VALMASK; |
| 502 | if (vt & VT_LVAL) { | 689 | if (ft & VT_LVAL) { |
| 503 | if ((vt & VT_TYPE) == VT_BYTE) | 690 | if (v == VT_LLOCAL) { |
| 504 | o(0xbe0f); /* movsbl x, %eax */ | 691 | load(r, VT_LOCAL | VT_LVAL, fc); |
| 505 | else | 692 | v = r; |
| 506 | o(0x8b); /* movl x,%eax */ | 693 | } |
| 507 | if (vt & VT_CONST) | 694 | if ((ft & VT_TYPE) == VT_BYTE) |
| 508 | oad(0x05, vc); | 695 | o(0xbe0f); /* movsbl */ |
| 509 | else if (vt & VT_LOCAL) | ||
| 510 | oad(0x85, vc); | ||
| 511 | else | 696 | else |
| 512 | g(0x00); | 697 | o(0x8b); /* movl */ |
| 513 | } else { | 698 | if (v == VT_CONST) { |
| 514 | if (vt & VT_CONST) { | 699 | oad(0x05 + r * 8, fc); /* 0xXX, r */ |
| 515 | oad(0xb8, vc); /* mov $xx, %eax */ | 700 | } else if (v == VT_LOCAL) { |
| 516 | } else if (vt & VT_LOCAL) { | 701 | oad(0x85 + r * 8, fc); /* xx(%ebp), r */ |
| 517 | oad(0x858d, vc); /* lea xxx(%ebp), %eax */ | 702 | } else { |
| 518 | } else if (vt & VT_CMP) { | 703 | g(0x00 + r * 8 + v); /* (v), r */ |
| 519 | oad(0xb8, 0); /* mov $0, %eax */ | ||
| 520 | o(0x0f); /* setxx %al */ | ||
| 521 | o(vc); | ||
| 522 | o(0xc0); | ||
| 523 | } | 704 | } |
| 524 | #ifndef TINY | 705 | } else { |
| 525 | else if (vt & VT_JMP) { | 706 | if (v == VT_CONST) { |
| 526 | t = vt & 1; | 707 | oad(0xb8 + r, fc); /* mov $xx, r */ |
| 527 | oad(0xb8, t); /* mov $1, %eax */ | 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 | oad(0xe9, 5); /* jmp after */ | 719 | oad(0xe9, 5); /* jmp after */ |
| 529 | gsym(vc); | 720 | gsym(fc); |
| 530 | oad(0xb8, t ^ 1); /* mov $0, %eax */ | 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 | /* generate a test. set 'inv' to invert test */ | 754 | /* generate a test. set 'inv' to invert test */ |
| 538 | /* XXX: handle constant */ | ||
| 539 | int gtst(inv, t) | 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 | /* fast case : can jump directly since flags are set */ | 760 | /* fast case : can jump directly since flags are set */ |
| 543 | g(0x0f); | 761 | g(0x0f); |
| 544 | t = psym((vc - 16) ^ inv, t); | 762 | t = psym((vc - 16) ^ inv, t); |
| 545 | } else | 763 | } else if (v == VT_JMP || v == VT_JMPI) { |
| 546 | #ifndef TINY | ||
| 547 | if (vt & VT_JMP) { | ||
| 548 | /* && or || optimization */ | 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 | t = vc; | 771 | t = vc; |
| 551 | else { | 772 | } else { |
| 552 | t = psym(0xe9, t); | 773 | t = gjmp(t); |
| 553 | gsym(vc); | 774 | gsym(vc); |
| 554 | } | 775 | } |
| 555 | } else | 776 | } else if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) { |
| 556 | if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) { | ||
| 557 | /* constant jmp optimization */ | 777 | /* constant jmp optimization */ |
| 558 | if ((vc != 0) != inv) | 778 | if ((vc != 0) != inv) |
| 559 | t = psym(0xe9, t); | 779 | t = gjmp(t); |
| 560 | } else | 780 | } else { |
| 561 | #endif | 781 | v = gv(); |
| 562 | { | 782 | o(0x85); |
| 563 | gv(); | 783 | o(0xc0 + v * 9); |
| 564 | o(0xc085); /* test %eax, %eax */ | ||
| 565 | g(0x0f); | 784 | g(0x0f); |
| 566 | t = psym(0x85 ^ inv, t); | 785 | t = psym(0x85 ^ inv, t); |
| 567 | } | 786 | } |
| 568 | return t; | 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 | /* return type size. Put alignment at 'a' */ | 1114 | /* return type size. Put alignment at 'a' */ |
| 572 | int type_size(int t, int *a) | 1115 | int type_size(int t, int *a) |
| 573 | { | 1116 | { |
| 574 | Sym *s; | 1117 | Sym *s; |
| 575 | 1118 | ||
| 576 | /* int, enum or pointer */ | 1119 | /* int, enum or pointer */ |
| 577 | if ((t & VT_PTRMASK) >= VT_PTRINC | | 1120 | if (t & VT_STRUCT) { |
| 578 | (t & VT_TYPE) == 0 | | ||
| 579 | (t & VT_ENUM)) { | ||
| 580 | *a = 4; | ||
| 581 | return 4; | ||
| 582 | } else if (t & VT_STRUCT) { | ||
| 583 | /* struct/union */ | 1121 | /* struct/union */ |
| 584 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); | 1122 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 585 | *a = 4; /* XXX: cannot store it yet. Doing that is safe */ | 1123 | *a = 4; /* XXX: cannot store it yet. Doing that is safe */ |
| 586 | return s->c; | 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 | } else { | 1133 | } else { |
| 588 | *a = 1; | 1134 | *a = 1; |
| 589 | return 1; | 1135 | return 1; |
| 590 | } | 1136 | } |
| 591 | } | 1137 | } |
| 592 | 1138 | ||
| 593 | /* return the number size in bytes of a given type */ | 1139 | /* return the pointed type of t */ |
| 594 | int incr_value(t) | 1140 | int pointed_type(int t) |
| 595 | { | 1141 | { |
| 596 | int a; | 1142 | Sym *s; |
| 597 | 1143 | s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); | |
| 598 | if ((t & VT_PTRMASK) >= VT_PTRINC) | 1144 | return s->t | (t & VT_TYPEN); |
| 599 | return type_size(t - VT_PTRINC, &a); | ||
| 600 | else | ||
| 601 | return 1; | ||
| 602 | } | 1145 | } |
| 603 | 1146 | ||
| 604 | #define POST_ADD 0x1000 | 1147 | int mk_pointer(int t) |
| 605 | #define PRE_ADD 0 | ||
| 606 | |||
| 607 | /* a defines POST/PRE add. c is the token ++ or -- */ | ||
| 608 | void inc(a, c) | ||
| 609 | { | 1148 | { |
| 610 | test_lvalue(); | 1149 | int p; |
| 611 | vt = vt & VT_LVALN; | 1150 | p = anon_sym++; |
| 612 | gv(); | 1151 | sym_push(p, t, -1); |
| 613 | o(0x018bc189); /* movl %eax, %ecx ; mov (%ecx), %eax */ | 1152 | return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & VT_TYPEN); |
| 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) */ | ||
| 617 | } | 1153 | } |
| 618 | 1154 | ||
| 619 | /* XXX: handle ptr sub and 'int + ptr' case (only 'ptr + int' handled) */ | 1155 | /* store value in lvalue pushed on stack */ |
| 620 | /* XXX: handle constant propagation (need to track live eax) */ | 1156 | void vstore() |
| 621 | /* XXX: handle unsigned propagation */ | ||
| 622 | void gen_op(op, l) | ||
| 623 | { | 1157 | { |
| 624 | int t; | 1158 | int ft, fc, r, t; |
| 625 | gv(); | 1159 | |
| 626 | t = vt; | 1160 | r = gv(); /* generate value */ |
| 627 | o(0x50); /* push %eax */ | 1161 | vpush(); |
| 628 | next(); | 1162 | ft = vstack_ptr[-4]; |
| 629 | if (l == -1) | 1163 | fc = vstack_ptr[-3]; |
| 630 | expr(); | 1164 | if ((ft & VT_VALMASK) == VT_LLOCAL) { |
| 631 | else if (l == -2) | 1165 | t = get_reg(); |
| 632 | expr_eq(); | 1166 | load(t, VT_LOCAL | VT_LVAL, fc); |
| 633 | else | 1167 | ft = (ft & ~VT_VALMASK) | t; |
| 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); | ||
| 662 | } | 1168 | } |
| 663 | #endif | 1169 | store(r, ft, fc); |
| 664 | else if (op == '/' | op == '%') { | 1170 | vstack_ptr -= 4; |
| 665 | o(0x91); /* xchg %ecx, %eax */ | 1171 | } |
| 666 | if (t & VT_UNSIGNED) { | 1172 | |
| 667 | o(0xd231); /* xor %edx, %edx */ | 1173 | /* post defines POST/PRE add. c is the token ++ or -- */ |
| 668 | o(0xf1f7); /* div %ecx, %eax */ | 1174 | void inc(post, c) |
| 669 | } else { | 1175 | { |
| 670 | o(0xf9f799); /* cltd, idiv %ecx, %eax */ | 1176 | int r, r1; |
| 671 | } | 1177 | test_lvalue(); |
| 672 | if (op == '%') | 1178 | if (post) |
| 673 | o(0x92); /* xchg %edx, %eax */ | 1179 | vpush(); /* room for returned value */ |
| 674 | } else { | 1180 | vpush(); /* save lvalue */ |
| 675 | o(0xc139); /* cmp %eax,%ecx */ | 1181 | r = gv(); |
| 676 | vset(VT_CMP, op); | 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 | int expr_const() | 1198 | int expr_const() |
| @@ -685,8 +1203,6 @@ int expr_const() · 4 unmodified lines | |||
| 685 | return vc; | 1203 | return vc; |
| 686 | } | 1204 | } |
| 687 | 1205 | ||
| 688 | #ifndef TINY | ||
| 689 | |||
| 690 | /* enum/struct/union declaration */ | 1206 | /* enum/struct/union declaration */ |
| 691 | int struct_decl(u) | 1207 | int struct_decl(u) |
| 692 | { | 1208 | { |
| @@ -695,7 +1211,6 @@ int struct_decl(u) · 2 unmodified lines | |||
| 695 | 1211 | ||
| 696 | a = tok; /* save decl type */ | 1212 | a = tok; /* save decl type */ |
| 697 | next(); | 1213 | next(); |
| 698 | v = 0; | ||
| 699 | if (tok != '{') { | 1214 | if (tok != '{') { |
| 700 | v = tok; | 1215 | v = tok; |
| 701 | next(); | 1216 | next(); |
| @@ -707,6 +1222,8 @@ int struct_decl(u) · 5 unmodified lines | |||
| 707 | u = u | (v << VT_STRUCT_SHIFT); | 1222 | u = u | (v << VT_STRUCT_SHIFT); |
| 708 | return u; | 1223 | return u; |
| 709 | } | 1224 | } |
| 1225 | } else { | ||
| 1226 | v = anon_sym++; | ||
| 710 | } | 1227 | } |
| 711 | s = sym_push(v | SYM_STRUCT, a, 0); | 1228 | s = sym_push(v | SYM_STRUCT, a, 0); |
| 712 | /* put struct/union/enum name in type */ | 1229 | /* put struct/union/enum name in type */ |
| @@ -733,7 +1250,7 @@ int struct_decl(u) · 20 unmodified lines | |||
| 733 | } else { | 1250 | } else { |
| 734 | b = ist(); | 1251 | b = ist(); |
| 735 | while (1) { | 1252 | while (1) { |
| 736 | t = typ(&v, b, &size); | 1253 | t = typ(&v, b); |
| 737 | if (t & (VT_FUNC | VT_TYPEDEF)) | 1254 | if (t & (VT_FUNC | VT_TYPEDEF)) |
| 738 | error("invalid type"); | 1255 | error("invalid type"); |
| 739 | /* XXX: align & correct type size */ | 1256 | /* XXX: align & correct type size */ |
| @@ -768,7 +1285,6 @@ int struct_decl(u) · 28 unmodified lines | |||
| 768 | } | 1285 | } |
| 769 | return u; | 1286 | return u; |
| 770 | } | 1287 | } |
| 771 | #endif | ||
| 772 | 1288 | ||
| 773 | /* return 0 if no type declaration. otherwise, return the basic type | 1289 | /* return 0 if no type declaration. otherwise, return the basic type |
| 774 | and skip it. | 1290 | and skip it. |
| @@ -783,16 +1299,18 @@ int ist() · 8 unmodified lines | |||
| 783 | while(1) { | 1299 | while(1) { |
| 784 | #ifndef TINY | 1300 | #ifndef TINY |
| 785 | if (tok == TOK_ENUM) { | 1301 | if (tok == TOK_ENUM) { |
| 786 | t = struct_decl(VT_ENUM); | 1302 | t |= struct_decl(VT_ENUM); |
| 787 | } else if (tok == TOK_STRUCT || tok == TOK_UNION) { | 1303 | } else if (tok == TOK_STRUCT || tok == TOK_UNION) { |
| 788 | t = struct_decl(VT_STRUCT); | 1304 | t |= struct_decl(VT_STRUCT); |
| 789 | } else | 1305 | } else |
| 790 | #endif | 1306 | #endif |
| 791 | { | 1307 | { |
| 792 | if (tok == TOK_CHAR | tok == TOK_VOID) { | 1308 | if (tok == TOK_CHAR) { |
| 793 | t |= VT_BYTE; | 1309 | t |= VT_BYTE; |
| 1310 | } else if (tok == TOK_VOID) { | ||
| 1311 | t |= VT_VOID; | ||
| 794 | } else if (tok == TOK_INT | | 1312 | } else if (tok == TOK_INT | |
| 795 | (tok >= TOK_CONST & tok <= TOK_SIGNED)) { | 1313 | (tok >= TOK_CONST & tok <= TOK_INLINE)) { |
| 796 | /* ignored types */ | 1314 | /* ignored types */ |
| 797 | } else if (tok == TOK_FLOAT & tok == TOK_DOUBLE) { | 1315 | } else if (tok == TOK_FLOAT & tok == TOK_DOUBLE) { |
| 798 | error("floats not supported"); | 1316 | error("floats not supported"); |
| @@ -817,23 +1335,97 @@ int ist() · 18 unmodified lines | |||
| 817 | return t; | 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 | /* Read a type declaration (except basic type), and return the | 1412 | /* Read a type declaration (except basic type), and return the |
| 821 | type. If v is true, then also put variable name in 'vc' */ | 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 | t = t & -3; /* suppress the ored '2' */ | 1418 | t = t & -3; /* suppress the ored '2' */ |
| 827 | while (tok == '*') { | 1419 | while (tok == '*') { |
| 828 | next(); | 1420 | next(); |
| 829 | t = t + VT_PTRINC; | 1421 | t = mk_pointer(t); |
| 830 | } | 1422 | } |
| 831 | 1423 | ||
| 832 | /* recursive type */ | 1424 | /* recursive type */ |
| 833 | /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */ | 1425 | /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */ |
| 834 | if (tok == '(') { | 1426 | if (tok == '(') { |
| 835 | next(); | 1427 | next(); |
| 836 | u = typ(v, 0, 0); | 1428 | u = typ(v, 0); |
| 837 | skip(')'); | 1429 | skip(')'); |
| 838 | } else { | 1430 | } else { |
| 839 | u = 0; | 1431 | u = 0; |
| @@ -843,51 +1435,7 @@ int typ(int *v, int t, int *array_size_ptr) · 3 unmodified lines | |||
| 843 | next(); | 1435 | next(); |
| 844 | } | 1436 | } |
| 845 | } | 1437 | } |
| 846 | while(1) { | 1438 | return post_type(u, t); |
| 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; | ||
| 891 | } | 1439 | } |
| 892 | 1440 | ||
| 893 | /* define a new external reference to a function 'v' of type 'u' */ | 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 | return s; | 1459 | return s; |
| 912 | } | 1460 | } |
| 913 | 1461 | ||
| 914 | /* read a number in base b */ | 1462 | /* read a character for string or char constant and eval escape codes */ |
| 915 | int getn(c, b) | 1463 | int getq() |
| 916 | { | 1464 | { |
| 917 | int n, t; | 1465 | int c; |
| 918 | n = 0; | 1466 | |
| 919 | #ifndef TINY | 1467 | c = ch; |
| 920 | while (1) { | 1468 | minp(); |
| 921 | if (c >= 'a') | 1469 | if (c == '\\') { |
| 922 | t = c - 'a' + 10; | 1470 | if (isnum(ch)) { |
| 923 | else if (c >= 'A') | 1471 | return getn(8); |
| 924 | t = c - 'A' + 10; | 1472 | } else { |
| 925 | else | 1473 | if (ch == 'n') |
| 926 | t = c - '0'; | 1474 | c = '\n'; |
| 927 | if (t < 0 | t >= b) | 1475 | else if (ch == 'r') |
| 928 | break; | 1476 | c = '\r'; |
| 929 | n = n * b + t; | 1477 | else if (ch == 't') |
| 930 | c = inp(); | 1478 | c = '\t'; |
| 931 | } | 1479 | else |
| 932 | #else | 1480 | c = ch; |
| 933 | while (isnum(c)) { | 1481 | minp(); |
| 934 | n = n * b + c - '0'; | 1482 | } |
| 935 | c = inp(); | ||
| 936 | } | 1483 | } |
| 937 | #endif | 1484 | return c; |
| 938 | ungetc(c, file); | ||
| 939 | return n; | ||
| 940 | } | 1485 | } |
| 941 | 1486 | ||
| 942 | int getq(n) | 1487 | void indir() |
| 943 | { | 1488 | { |
| 944 | if (n == '\\') { | 1489 | if (vt & VT_LVAL) |
| 945 | n = inp(); | 1490 | gv(); |
| 946 | if (n == 'n') | 1491 | if (!(vt & VT_PTR)) |
| 947 | n = '\n'; | 1492 | expect("pointer"); |
| 948 | #ifndef TINY | 1493 | vt = pointed_type(vt); |
| 949 | else if (n == 'r') | 1494 | if (!(vt & VT_ARRAY)) /* an array is never an lvalue */ |
| 950 | n = '\r'; | 1495 | vt |= VT_LVAL; |
| 951 | else if (n == 't') | ||
| 952 | n = '\t'; | ||
| 953 | #endif | ||
| 954 | else if (isnum(n)) | ||
| 955 | n = getn(n, 8); | ||
| 956 | } | ||
| 957 | return n; | ||
| 958 | } | 1496 | } |
| 959 | 1497 | ||
| 960 | void unary() | 1498 | void unary() |
| 961 | { | 1499 | { |
| 962 | int n, t, ft, fc, p; | 1500 | int n, t, ft, fc, p, r; |
| 963 | Sym *s; | 1501 | Sym *s; |
| 964 | 1502 | ||
| 965 | if (isnum(tok)) { | 1503 | if (tok == TOK_NUM) { |
| 966 | /* number */ | 1504 | vset(VT_CONST, tokc); |
| 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 | ||
| 981 | next(); | 1505 | next(); |
| 982 | } else | 1506 | } else |
| 983 | #ifndef TINY | ||
| 984 | if (tok == '\'') { | 1507 | if (tok == '\'') { |
| 985 | vset(VT_CONST, getq(inp())); | 1508 | vset(VT_CONST, getq()); |
| 986 | next(); /* skip char */ | 1509 | next(); /* skip char */ |
| 987 | skip('\''); | 1510 | skip('\''); |
| 988 | } else | 1511 | } else if (tok == '\"') { |
| 989 | #endif | 1512 | /* generate (char *) type */ |
| 990 | if (tok == '\"') { | 1513 | vset(VT_CONST | mk_pointer(VT_TYPE), glo); |
| 991 | vset(VT_CONST | VT_PTRINC | VT_BYTE, glo); | ||
| 992 | while (tok == '\"') { | 1514 | while (tok == '\"') { |
| 993 | while((n = inp()) != 34) { | 1515 | while (ch != '\"') { |
| 994 | *(char *)glo++ = getq(n); | 1516 | if (ch == -1) |
| 1517 | error("unterminated string"); | ||
| 1518 | *(char *)glo++ = getq(); | ||
| 995 | } | 1519 | } |
| 1520 | minp(); | ||
| 996 | next(); | 1521 | next(); |
| 997 | } | 1522 | } |
| 998 | *(char *)glo++ = 0; | 1523 | *(char *)glo++ = 0; |
| @@ -1002,7 +1527,7 @@ void unary() · 3 unmodified lines | |||
| 1002 | if (t == '(') { | 1527 | if (t == '(') { |
| 1003 | /* cast ? */ | 1528 | /* cast ? */ |
| 1004 | if (t = ist()) { | 1529 | if (t = ist()) { |
| 1005 | ft = typ(0, t, 0); | 1530 | ft = typ(0, t); |
| 1006 | skip(')'); | 1531 | skip(')'); |
| 1007 | unary(); | 1532 | unary(); |
| 1008 | vt = (vt & VT_TYPEN) | ft; | 1533 | vt = (vt & VT_TYPEN) | ft; |
| @@ -1012,50 +1537,50 @@ void unary() · 3 unmodified lines | |||
| 1012 | } | 1537 | } |
| 1013 | } else if (t == '*') { | 1538 | } else if (t == '*') { |
| 1014 | unary(); | 1539 | unary(); |
| 1015 | if (vt & VT_LVAL) | 1540 | indir(); |
| 1016 | gv(); | ||
| 1017 | #ifndef TINY | ||
| 1018 | if (!(vt & VT_PTRMASK)) | ||
| 1019 | expect("pointer"); | ||
| 1020 | #endif | ||
| 1021 | vt = (vt - VT_PTRINC) | VT_LVAL; | ||
| 1022 | } else if (t == '&') { | 1541 | } else if (t == '&') { |
| 1023 | unary(); | 1542 | unary(); |
| 1024 | test_lvalue(); | 1543 | test_lvalue(); |
| 1025 | vt = (vt & VT_LVALN) + VT_PTRINC; | 1544 | vt = mk_pointer(vt & VT_LVALN); |
| 1026 | } else | 1545 | } else |
| 1027 | #ifndef TINY | ||
| 1028 | if (t == '!') { | 1546 | if (t == '!') { |
| 1029 | unary(); | 1547 | unary(); |
| 1030 | if (vt & VT_CMP) | 1548 | if ((vt & VT_VALMASK) == VT_CMP) |
| 1031 | vc = vc ^ 1; | 1549 | vc = vc ^ 1; |
| 1032 | else | 1550 | else |
| 1033 | vset(VT_JMP, gtst(1, 0)); | 1551 | vset(VT_JMP, gtst(1, 0)); |
| 1034 | } else | 1552 | } else |
| 1035 | if (t == '~') { | 1553 | if (t == '~') { |
| 1036 | unary(); | 1554 | unary(); |
| 1037 | if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) | 1555 | vpush(); |
| 1038 | vc = ~vc; | 1556 | vset(VT_CONST, -1); |
| 1039 | else { | 1557 | gen_op('^'); |
| 1040 | gv(); | ||
| 1041 | o(0xd0f7); | ||
| 1042 | } | ||
| 1043 | } else | 1558 | } else |
| 1044 | if (t == '+') { | 1559 | if (t == '+') { |
| 1045 | unary(); | 1560 | unary(); |
| 1046 | } else | 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 | if (t == TOK_INC | t == TOK_DEC) { | 1576 | if (t == TOK_INC | t == TOK_DEC) { |
| 1049 | unary(); | 1577 | unary(); |
| 1050 | inc(PRE_ADD, t); | 1578 | inc(0, t); |
| 1051 | } else if (t == '-') { | 1579 | } else if (t == '-') { |
| 1580 | vset(VT_CONST, 0); | ||
| 1581 | vpush(); | ||
| 1052 | unary(); | 1582 | unary(); |
| 1053 | if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) | 1583 | gen_op('-'); |
| 1054 | vc = -vc; | ||
| 1055 | else { | ||
| 1056 | gv(); | ||
| 1057 | o(0xd8f7); /* neg %eax */ | ||
| 1058 | } | ||
| 1059 | } else | 1584 | } else |
| 1060 | { | 1585 | { |
| 1061 | s = sym_find(t); | 1586 | s = sym_find(t); |
| @@ -1076,17 +1601,17 @@ void unary() · 14 unmodified lines | |||
| 1076 | /* post operations */ | 1601 | /* post operations */ |
| 1077 | while (1) { | 1602 | while (1) { |
| 1078 | if (tok == TOK_INC | tok == TOK_DEC) { | 1603 | if (tok == TOK_INC | tok == TOK_DEC) { |
| 1079 | inc(POST_ADD, tok); | 1604 | inc(1, tok); |
| 1080 | next(); | 1605 | next(); |
| 1081 | } else if (tok == '.' | tok == TOK_ARROW) { | 1606 | } else if (tok == '.' | tok == TOK_ARROW) { |
| 1082 | /* field */ | 1607 | /* field */ |
| 1083 | if (tok == '.') { | 1608 | if (tok == TOK_ARROW) |
| 1084 | test_lvalue(); | 1609 | indir(); |
| 1085 | vt = (vt & VT_LVALN) + VT_PTRINC; | 1610 | test_lvalue(); |
| 1086 | } | 1611 | vt &= VT_LVALN; |
| 1087 | next(); | 1612 | next(); |
| 1088 | /* expect pointer on structure */ | 1613 | /* expect pointer on structure */ |
| 1089 | if (!(vt & VT_STRUCT) || (vt & VT_PTRMASK) == 0) | 1614 | if (!(vt & VT_STRUCT)) |
| 1090 | expect("struct or union"); | 1615 | expect("struct or union"); |
| 1091 | s = sym_find(((unsigned)vt >> VT_STRUCT_SHIFT) | SYM_STRUCT); | 1616 | s = sym_find(((unsigned)vt >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 1092 | /* find field */ | 1617 | /* find field */ |
| @@ -1098,40 +1623,39 @@ void unary() · 5 unmodified lines | |||
| 1098 | if (!s) | 1623 | if (!s) |
| 1099 | error("field not found"); | 1624 | error("field not found"); |
| 1100 | /* add field offset to pointer */ | 1625 | /* add field offset to pointer */ |
| 1101 | gv(); | 1626 | vt = vt & VT_TYPEN; /* change type to int */ |
| 1102 | if (s->c) | 1627 | vpush(); |
| 1103 | oad(0x05, s->c); | 1628 | vset(VT_CONST, s->c); |
| 1629 | gen_op('+'); | ||
| 1104 | /* change type to field type, and set to lvalue */ | 1630 | /* change type to field type, and set to lvalue */ |
| 1105 | vt = (vt & VT_TYPEN) | VT_LVAL | s->t; | 1631 | vt = (vt & VT_TYPEN) | VT_LVAL | s->t; |
| 1106 | next(); | 1632 | next(); |
| 1107 | } else if (tok == '[') { | 1633 | } else if (tok == '[') { |
| 1108 | #ifndef TINY | 1634 | next(); |
| 1109 | if (!(vt & VT_PTRMASK)) | 1635 | vpush(); |
| 1110 | expect("pointer"); | 1636 | expr(); |
| 1111 | #endif | 1637 | gen_op('+'); |
| 1112 | gen_op('+', -1); | 1638 | indir(); |
| 1113 | /* dereference pointer */ | ||
| 1114 | vt = (vt - VT_PTRINC) | VT_LVAL; | ||
| 1115 | skip(']'); | 1639 | skip(']'); |
| 1116 | } else if (tok == '(') { | 1640 | } else if (tok == '(') { |
| 1117 | /* function call */ | 1641 | /* function call */ |
| 1642 | save_regs(); /* save used temporary registers */ | ||
| 1118 | /* lvalue is implied */ | 1643 | /* lvalue is implied */ |
| 1119 | vt = vt & VT_LVALN; | 1644 | vt = vt & VT_LVALN; |
| 1120 | if ((vt & VT_CONST) == 0) { | 1645 | if ((vt & VT_VALMASK) != VT_CONST) { |
| 1121 | /* evaluate function address */ | 1646 | /* evaluate function address */ |
| 1122 | gv(); | 1647 | r = gv(); |
| 1123 | o(0x50); /* push %eax */ | 1648 | o(0x50 + r); /* push r */ |
| 1124 | } | 1649 | } |
| 1125 | ft = vt; | 1650 | ft = vt; |
| 1126 | fc = vc; | 1651 | fc = vc; |
| 1127 | |||
| 1128 | next(); | 1652 | next(); |
| 1129 | t = 0; | 1653 | t = 0; |
| 1130 | while (tok != ')') { | 1654 | while (tok != ')') { |
| 1131 | t = t + 4; | 1655 | t = t + 4; |
| 1132 | expr_eq(); | 1656 | expr_eq(); |
| 1133 | gv(); | 1657 | r = gv(); |
| 1134 | o(0x50); /* push %eax */ | 1658 | o(0x50 + r); /* push r */ |
| 1135 | if (tok == ',') | 1659 | if (tok == ',') |
| 1136 | next(); | 1660 | next(); |
| 1137 | } | 1661 | } |
| @@ -1148,19 +1672,19 @@ void unary() · 10 unmodified lines | |||
| 1148 | n = n + 4; | 1672 | n = n + 4; |
| 1149 | p = p - 4; | 1673 | p = p - 4; |
| 1150 | } | 1674 | } |
| 1151 | if (ft & VT_CONST) { | 1675 | if ((ft & VT_VALMASK) == VT_CONST) { |
| 1152 | /* forward reference */ | 1676 | /* forward reference */ |
| 1153 | if (ft & VT_FORWARD) { | 1677 | if (ft & VT_FORWARD) { |
| 1154 | *(int *)fc = psym(0xe8, *(int *)fc); | 1678 | *(int *)fc = psym(0xe8, *(int *)fc); |
| 1155 | } else | 1679 | } else |
| 1156 | oad(0xe8, fc - ind - 5); | 1680 | oad(0xe8, fc - ind - 5); |
| 1157 | /* return value is variable, and take type from function proto */ | 1681 | /* return value is %eax, and take type from function proto */ |
| 1158 | vt = VT_VAR | (ft & VT_TYPE & VT_FUNCN); | 1682 | vt = 0 | (ft & VT_TYPE & VT_FUNCN); |
| 1159 | } else { | 1683 | } else { |
| 1160 | oad(0x2494ff, t); /* call *xxx(%esp) */ | 1684 | oad(0x2494ff, t); /* call *xxx(%esp) */ |
| 1161 | t = t + 4; | 1685 | t = t + 4; |
| 1162 | /* return value is variable, int */ | 1686 | /* return value is %eax, integer */ |
| 1163 | vt = VT_VAR; | 1687 | vt = 0; |
| 1164 | } | 1688 | } |
| 1165 | if (t) | 1689 | if (t) |
| 1166 | oad(0xc481, t); | 1690 | oad(0xc481, t); |
| @@ -1172,60 +1696,52 @@ void unary() · 5 unmodified lines | |||
| 1172 | 1696 | ||
| 1173 | void uneq() | 1697 | void uneq() |
| 1174 | { | 1698 | { |
| 1175 | int ft, fc, b; | 1699 | int t; |
| 1176 | 1700 | ||
| 1177 | unary(); | 1701 | unary(); |
| 1178 | if (tok == '=' | | 1702 | if (tok == '=' | |
| 1179 | (tok >= TOK_A_MOD & TOK_A_DIV) | | 1703 | (tok >= TOK_A_MOD & tok <= TOK_A_DIV) | |
| 1180 | tok == TOK_A_XOR | tok == TOK_A_OR | | 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 | test_lvalue(); | 1706 | test_lvalue(); |
| 1183 | fc = vc; | 1707 | vpush(); |
| 1184 | ft = vt; | 1708 | t = tok; |
| 1185 | b = (vt & VT_TYPE) == VT_BYTE; | 1709 | next(); |
| 1186 | if (ft & VT_VAR) | 1710 | if (t == '=') { |
| 1187 | o(0x50); /* push %eax */ | ||
| 1188 | if (tok == '=') { | ||
| 1189 | next(); | ||
| 1190 | expr_eq(); | 1711 | expr_eq(); |
| 1191 | #ifndef TINY | 1712 | /* XXX: be more precise */ |
| 1192 | if ((vt & VT_PTRMASK) != (ft & VT_PTRMASK)) | 1713 | if ((vt & VT_PTR) != (vstack_ptr[-2] & VT_PTR)) |
| 1193 | warning("incompatible type"); | 1714 | warning("incompatible type"); |
| 1194 | #endif | 1715 | } else { |
| 1195 | gv(); /* generate value */ | 1716 | vpush(); |
| 1196 | } else | 1717 | expr_eq(); |
| 1197 | gen_op(tok & 0x7f, -2); /* XXX: incorrect, must call expr_eq */ | 1718 | gen_op(t & 0x7f); |
| 1198 | 1719 | } | |
| 1199 | if (ft & VT_VAR) { | 1720 | vstore(); |
| 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 */ | ||
| 1206 | } | 1721 | } |
| 1207 | } | 1722 | } |
| 1208 | 1723 | ||
| 1209 | void sum(l) | 1724 | void sum(l) |
| 1210 | { | 1725 | { |
| 1211 | #ifndef TINY | 1726 | int ft, fc, t; |
| 1212 | int t; | 1727 | |
| 1213 | #endif | ||
| 1214 | if (l == 0) | 1728 | if (l == 0) |
| 1215 | uneq(); | 1729 | uneq(); |
| 1216 | else { | 1730 | else { |
| 1217 | sum(--l); | 1731 | sum(--l); |
| 1218 | while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) | | 1732 | while ((l == 0 & (tok == '*' | tok == '/' | tok == '%')) | |
| 1219 | (l == 1 & (tok == '+' | tok == '-')) | | 1733 | (l == 1 & (tok == '+' | tok == '-')) | |
| 1220 | #ifndef TINY | 1734 | (l == 2 & (tok == TOK_SHL | tok == TOK_SAR)) | |
| 1221 | (l == 2 & (tok == TOK_SHL | tok == TOK_SHR)) | | ||
| 1222 | #endif | ||
| 1223 | (l == 3 & (tok >= TOK_LT & tok <= TOK_GT)) | | 1735 | (l == 3 & (tok >= TOK_LT & tok <= TOK_GT)) | |
| 1224 | (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) | | 1736 | (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) | |
| 1225 | (l == 5 & tok == '&') | | 1737 | (l == 5 & tok == '&') | |
| 1226 | (l == 6 & tok == '^') | | 1738 | (l == 6 & tok == '^') | |
| 1227 | (l == 7 & tok == '|')) { | 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 | if (tok != TOK_LAND) { | 1762 | if (tok != TOK_LAND) { |
| 1247 | if (t) { | 1763 | if (t) { |
| 1248 | t = gtst(1, t); | 1764 | t = gtst(1, t); |
| 1249 | vset(VT_JMP | 1, t); | 1765 | vset(VT_JMPI, t); |
| 1250 | } | 1766 | } |
| 1251 | break; | 1767 | break; |
| 1252 | } | 1768 | } |
| @@ -1287,7 +1803,7 @@ void expr_eq() · 34 unmodified lines | |||
| 1287 | expr(); | 1803 | expr(); |
| 1288 | gv(); | 1804 | gv(); |
| 1289 | skip(':'); | 1805 | skip(':'); |
| 1290 | u = psym(0xe9, 0); | 1806 | u = gjmp(0); |
| 1291 | gsym(t); | 1807 | gsym(t); |
| 1292 | expr_eq(); | 1808 | expr_eq(); |
| 1293 | gv(); | 1809 | gv(); |
| @@ -1307,7 +1823,7 @@ void expr() · 13 unmodified lines | |||
| 1307 | 1823 | ||
| 1308 | #endif | 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 | int a, b, c, d; | 1828 | int a, b, c, d; |
| 1313 | Sym *s; | 1829 | Sym *s; |
| @@ -1319,13 +1835,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 5 unmodified lines | |||
| 1319 | expr(); | 1835 | expr(); |
| 1320 | skip(')'); | 1836 | skip(')'); |
| 1321 | a = gtst(1, 0); | 1837 | a = gtst(1, 0); |
| 1322 | block(bsym, csym, case_sym, def_sym); | 1838 | block(bsym, csym, case_sym, def_sym, case_reg); |
| 1323 | c = tok; | 1839 | c = tok; |
| 1324 | if (c == TOK_ELSE) { | 1840 | if (c == TOK_ELSE) { |
| 1325 | next(); | 1841 | next(); |
| 1326 | d = psym(0xe9, 0); /* jmp */ | 1842 | d = gjmp(0); |
| 1327 | gsym(a); | 1843 | gsym(a); |
| 1328 | block(bsym, csym, case_sym, def_sym); | 1844 | block(bsym, csym, case_sym, def_sym, case_reg); |
| 1329 | gsym(d); /* patch else jmp */ | 1845 | gsym(d); /* patch else jmp */ |
| 1330 | } else | 1846 | } else |
| 1331 | gsym(a); | 1847 | gsym(a); |
| @@ -1337,7 +1853,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 5 unmodified lines | |||
| 1337 | skip(')'); | 1853 | skip(')'); |
| 1338 | a = gtst(1, 0); | 1854 | a = gtst(1, 0); |
| 1339 | b = 0; | 1855 | b = 0; |
| 1340 | block(&a, &b, case_sym, def_sym); | 1856 | block(&a, &b, case_sym, def_sym, case_reg); |
| 1341 | oad(0xe9, d - ind - 5); /* jmp */ | 1857 | oad(0xe9, d - ind - 5); /* jmp */ |
| 1342 | gsym(a); | 1858 | gsym(a); |
| 1343 | gsym_addr(b, d); | 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 | s = local_stack; | 1863 | s = local_stack; |
| 1348 | decl(VT_LOCAL); | 1864 | decl(VT_LOCAL); |
| 1349 | while (tok != '}') | 1865 | while (tok != '}') |
| 1350 | block(bsym, csym, case_sym, def_sym); | 1866 | block(bsym, csym, case_sym, def_sym, case_reg); |
| 1351 | /* pop locally defined symbols */ | 1867 | /* pop locally defined symbols */ |
| 1352 | sym_pop(&local_stack, s); | 1868 | sym_pop(&local_stack, s); |
| 1353 | next(); | 1869 | next(); |
| @@ -1355,22 +1871,22 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 1 unmodified line | |||
| 1355 | next(); | 1871 | next(); |
| 1356 | if (tok != ';') { | 1872 | if (tok != ';') { |
| 1357 | expr(); | 1873 | expr(); |
| 1358 | gv(); | 1874 | move_reg(0, gv()); |
| 1359 | } | 1875 | } |
| 1360 | skip(';'); | 1876 | skip(';'); |
| 1361 | rsym = psym(0xe9, rsym); /* jmp */ | 1877 | rsym = gjmp(rsym); /* jmp */ |
| 1362 | } else if (tok == TOK_BREAK) { | 1878 | } else if (tok == TOK_BREAK) { |
| 1363 | /* compute jump */ | 1879 | /* compute jump */ |
| 1364 | if (!bsym) | 1880 | if (!bsym) |
| 1365 | error("cannot break"); | 1881 | error("cannot break"); |
| 1366 | *bsym = psym(0xe9, *bsym); | 1882 | *bsym = gjmp(*bsym); |
| 1367 | next(); | 1883 | next(); |
| 1368 | skip(';'); | 1884 | skip(';'); |
| 1369 | } else if (tok == TOK_CONTINUE) { | 1885 | } else if (tok == TOK_CONTINUE) { |
| 1370 | /* compute jump */ | 1886 | /* compute jump */ |
| 1371 | if (!csym) | 1887 | if (!csym) |
| 1372 | error("cannot continue"); | 1888 | error("cannot continue"); |
| 1373 | *csym = psym(0xe9, *csym); | 1889 | *csym = gjmp(*csym); |
| 1374 | next(); | 1890 | next(); |
| 1375 | skip(';'); | 1891 | skip(';'); |
| 1376 | } else | 1892 | } else |
| @@ -1392,14 +1908,14 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 15 unmodified lines | |||
| 1392 | } | 1908 | } |
| 1393 | skip(';'); | 1909 | skip(';'); |
| 1394 | if (tok != ')') { | 1910 | if (tok != ')') { |
| 1395 | e = psym(0xe9, 0); | 1911 | e = gjmp(0); |
| 1396 | c = ind; | 1912 | c = ind; |
| 1397 | expr(); | 1913 | expr(); |
| 1398 | oad(0xe9, d - ind - 5); /* jmp */ | 1914 | oad(0xe9, d - ind - 5); /* jmp */ |
| 1399 | gsym(e); | 1915 | gsym(e); |
| 1400 | } | 1916 | } |
| 1401 | skip(')'); | 1917 | skip(')'); |
| 1402 | block(&a, &b, case_sym, def_sym); | 1918 | block(&a, &b, case_sym, def_sym, case_reg); |
| 1403 | oad(0xe9, c - ind - 5); /* jmp */ | 1919 | oad(0xe9, c - ind - 5); /* jmp */ |
| 1404 | gsym(a); | 1920 | gsym(a); |
| 1405 | gsym_addr(b, c); | 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 | a = 0; | 1925 | a = 0; |
| 1410 | b = 0; | 1926 | b = 0; |
| 1411 | d = ind; | 1927 | d = ind; |
| 1412 | block(&a, &b, case_sym, def_sym); | 1928 | block(&a, &b, case_sym, def_sym, case_reg); |
| 1413 | skip(TOK_WHILE); | 1929 | skip(TOK_WHILE); |
| 1414 | skip('('); | 1930 | skip('('); |
| 1415 | gsym(b); | 1931 | gsym(b); |
| @@ -1423,12 +1939,12 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 7 unmodified lines | |||
| 1423 | next(); | 1939 | next(); |
| 1424 | skip('('); | 1940 | skip('('); |
| 1425 | expr(); | 1941 | expr(); |
| 1426 | gv(); | 1942 | case_reg = gv(); |
| 1427 | skip(')'); | 1943 | skip(')'); |
| 1428 | a = 0; | 1944 | a = 0; |
| 1429 | b = 0; | 1945 | b = 0; |
| 1430 | c = 0; | 1946 | c = 0; |
| 1431 | block(&a, csym, &b, &c); | 1947 | block(&a, csym, &b, &c, case_reg); |
| 1432 | /* if no default, jmp after switch */ | 1948 | /* if no default, jmp after switch */ |
| 1433 | if (c == 0) | 1949 | if (c == 0) |
| 1434 | c = ind; | 1950 | c = ind; |
| @@ -1443,10 +1959,13 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 8 unmodified lines | |||
| 1443 | if (!case_sym) | 1959 | if (!case_sym) |
| 1444 | expect("switch"); | 1960 | expect("switch"); |
| 1445 | gsym(*case_sym); | 1961 | gsym(*case_sym); |
| 1446 | oad(0x3d, a); /* cmp $xxx, %eax */ | 1962 | vset(case_reg, 0); |
| 1447 | *case_sym = psym(0x850f, 0); /* jne xxx */ | 1963 | vpush(); |
| 1964 | vset(VT_CONST, a); | ||
| 1965 | gen_op(TOK_EQ); | ||
| 1966 | *case_sym = gtst(1, 0); | ||
| 1448 | skip(':'); | 1967 | skip(':'); |
| 1449 | block(bsym, csym, case_sym, def_sym); | 1968 | block(bsym, csym, case_sym, def_sym, case_reg); |
| 1450 | } else | 1969 | } else |
| 1451 | if (tok == TOK_DEFAULT) { | 1970 | if (tok == TOK_DEFAULT) { |
| 1452 | next(); | 1971 | next(); |
| @@ -1456,7 +1975,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines | |||
| 1456 | if (*def_sym) | 1975 | if (*def_sym) |
| 1457 | error("too many 'default'"); | 1976 | error("too many 'default'"); |
| 1458 | *def_sym = ind; | 1977 | *def_sym = ind; |
| 1459 | block(bsym, csym, case_sym, def_sym); | 1978 | block(bsym, csym, case_sym, def_sym, case_reg); |
| 1460 | } else | 1979 | } else |
| 1461 | if (tok == TOK_GOTO) { | 1980 | if (tok == TOK_GOTO) { |
| 1462 | next(); | 1981 | next(); |
| @@ -1466,7 +1985,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 3 unmodified lines | |||
| 1466 | s = sym_push1(&label_stack, tok, VT_FORWARD, 0); | 1985 | s = sym_push1(&label_stack, tok, VT_FORWARD, 0); |
| 1467 | /* label already defined */ | 1986 | /* label already defined */ |
| 1468 | if (s->t & VT_FORWARD) | 1987 | if (s->t & VT_FORWARD) |
| 1469 | s->c = psym(0xe9, s->c); /* jmp xxx */ | 1988 | s->c = gjmp(s->c); /* jmp xxx */ |
| 1470 | else | 1989 | else |
| 1471 | oad(0xe9, s->c - ind - 5); /* jmp xxx */ | 1990 | oad(0xe9, s->c - ind - 5); /* jmp xxx */ |
| 1472 | next(); | 1991 | next(); |
| @@ -1489,7 +2008,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym) · 16 unmodified lines | |||
| 1489 | } else { | 2008 | } else { |
| 1490 | sym_push1(&label_stack, b, 0, ind); | 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 | } else { | 2012 | } else { |
| 1494 | /* expression case: go backward of one token */ | 2013 | /* expression case: go backward of one token */ |
| 1495 | /* XXX: currently incorrect if number/string/char */ | 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 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ | 2025 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 1507 | void decl(l) | 2026 | void decl(l) |
| 1508 | { | 2027 | { |
| 1509 | int *a, t, b, s, align, v, u, n; | 2028 | int *a, t, b, size, align, v, u, n; |
| 1510 | Sym *sym, *slocal; | 2029 | Sym *sym; |
| 1511 | 2030 | ||
| 1512 | while (b = ist()) { | 2031 | while (b = ist()) { |
| 1513 | if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') { | 2032 | if ((b & (VT_ENUM | VT_STRUCT)) && tok == ';') { |
| @@ -1516,9 +2035,7 @@ void decl(l) · 2 unmodified lines | |||
| 1516 | continue; | 2035 | continue; |
| 1517 | } | 2036 | } |
| 1518 | while (1) { /* iterate thru each declaration */ | 2037 | while (1) { /* iterate thru each declaration */ |
| 1519 | s = 1; | 2038 | t = typ(&v, b); |
| 1520 | slocal = local_stack; /* save local stack position, to restore it */ | ||
| 1521 | t = typ(&v, b, &s); | ||
| 1522 | if (tok == '{') { | 2039 | if (tok == '{') { |
| 1523 | /* patch forward references */ | 2040 | /* patch forward references */ |
| 1524 | if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) { | 2041 | if ((sym = sym_find(v)) && (sym->t & VT_FORWARD)) { |
| @@ -1533,7 +2050,7 @@ void decl(l) · 8 unmodified lines | |||
| 1533 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ | 2050 | o(0xe58955); /* push %ebp, mov %esp, %ebp */ |
| 1534 | a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */ | 2051 | a = (int *)oad(0xec81, 0); /* sub $xxx, %esp */ |
| 1535 | rsym = 0; | 2052 | rsym = 0; |
| 1536 | block(0, 0, 0, 0); | 2053 | block(0, 0, 0, 0, 0); |
| 1537 | gsym(rsym); | 2054 | gsym(rsym); |
| 1538 | o(0xc3c9); /* leave, ret */ | 2055 | o(0xc3c9); /* leave, ret */ |
| 1539 | *a = (-loc + 3) & -4; /* align local size to word & | 2056 | *a = (-loc + 3) & -4; /* align local size to word & |
| @@ -1542,13 +2059,12 @@ void decl(l) · 2 unmodified lines | |||
| 1542 | sym_pop(&local_stack, 0); /* reset local stack */ | 2059 | sym_pop(&local_stack, 0); /* reset local stack */ |
| 1543 | break; | 2060 | break; |
| 1544 | } else { | 2061 | } else { |
| 1545 | /* reset local stack (needed because of dummy function | 2062 | if (b & VT_TYPEDEF) { |
| 1546 | parameters */ | ||
| 1547 | sym_pop(&local_stack, slocal); | ||
| 1548 | if (t & VT_TYPEDEF) { | ||
| 1549 | /* save typedefed type */ | 2063 | /* save typedefed type */ |
| 1550 | sym_push(v, t, 0); | 2064 | sym_push(v, t | VT_TYPEDEF, 0); |
| 1551 | } else if (t & VT_FUNC) { | 2065 | } else if (t & VT_FUNC) { |
| 2066 | /* XXX: incorrect to flush, but needed while | ||
| 2067 | waiting for function prototypes */ | ||
| 1552 | /* external function definition */ | 2068 | /* external function definition */ |
| 1553 | external_func(v, t); | 2069 | external_func(v, t); |
| 1554 | } else { | 2070 | } else { |
| @@ -1567,19 +2083,18 @@ void decl(l) · 12 unmodified lines | |||
| 1567 | if (t & VT_STATIC) | 2083 | if (t & VT_STATIC) |
| 1568 | u = VT_CONST; | 2084 | u = VT_CONST; |
| 1569 | u |= t; | 2085 | u |= t; |
| 1570 | if (t & VT_ARRAY) | 2086 | size = type_size(t, &align); |
| 1571 | t -= VT_PTRINC; | 2087 | if (size < 0) |
| 1572 | align = type_size(t, &align); | 2088 | error("invalid size"); |
| 1573 | s *= align; | 2089 | if ((u & VT_VALMASK) == VT_LOCAL) { |
| 1574 | if (u & VT_LOCAL) { | ||
| 1575 | /* allocate space down on the stack */ | 2090 | /* allocate space down on the stack */ |
| 1576 | loc = (loc - s) & -align; | 2091 | loc = (loc - size) & -align; |
| 1577 | sym_push(v, u, loc); | 2092 | sym_push(v, u, loc); |
| 1578 | } else { | 2093 | } else { |
| 1579 | /* allocate space up in the data space */ | 2094 | /* allocate space up in the data space */ |
| 1580 | glo = (glo + align - 1) & -align; | 2095 | glo = (glo + align - 1) & -align; |
| 1581 | sym_push(v, u, glo); | 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 | Sym *s; | 2113 | Sym *s; |
| 1599 | int (*t)(); | 2114 | int (*t)(); |
| 1600 | if (c < 2) { | 2115 | if (c < 2) { |
| 1601 | printf("usage: tc src\n"); | 2116 | printf("usage: tcc source ...\n"); |
| 1602 | return 1; | 2117 | return 1; |
| 1603 | } | 2118 | } |
| 1604 | v++; | 2119 | v++; |
| 1605 | filename = *v; | 2120 | filename = *v; |
| 2121 | line_num = 1; | ||
| 1606 | file = fopen(filename, "r"); | 2122 | file = fopen(filename, "r"); |
| 1607 | #ifndef TINY | ||
| 1608 | if (!file) { | 2123 | if (!file) { |
| 1609 | perror(filename); | 2124 | perror(filename); |
| 1610 | exit(1); | 2125 | exit(1); |
| 1611 | } | 2126 | } |
| 1612 | #endif | 2127 | include_stack_ptr = include_stack; |
| 1613 | 2128 | ||
| 1614 | idtable = malloc(SYM_TABLE_SIZE); | 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 | memcpy(idtable, | 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); | 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); |
| 1622 | idptr = idtable + 192; | 2132 | idptr = idtable + 219; |
| 1623 | #endif | 2133 | |
| 1624 | glo = malloc(DATA_SIZE); | 2134 | glo = malloc(DATA_SIZE); |
| 2135 | memset((void *)glo, 0, DATA_SIZE); | ||
| 1625 | prog = malloc(TEXT_SIZE); | 2136 | prog = malloc(TEXT_SIZE); |
| 1626 | macro_stack = malloc(256); | 2137 | vstack = malloc(256); |
| 2138 | vstack_ptr = vstack; | ||
| 1627 | macro_stack_ptr = macro_stack; | 2139 | macro_stack_ptr = macro_stack; |
| 2140 | anon_sym = 1 << (31 - VT_STRUCT_SHIFT); | ||
| 1628 | ind = prog; | 2141 | ind = prog; |
| 1629 | line_num = 1; | 2142 | inp(); |
| 2143 | ch = '\n'; /* needed to parse correctly first preprocessor command */ | ||
| 1630 | next(); | 2144 | next(); |
| 1631 | decl(VT_CONST); | 2145 | decl(VT_CONST); |
| 1632 | #ifdef TEST | 2146 | #ifdef TEST |