Unified · Split
| @@ -17,20 +17,30 @@ · 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 "tcclib.h" | |
| 20 | +#include <tcclib.h> | |
| 21 | 21 | |
| 22 | −#define INCLUDE_PATH "/usr/include" | |
| 22 | +//#define DEBUG | |
| 23 | +/* preprocessor debug */ | |
| 24 | +//#define PP_DEBUG | |
| 23 | 25 | |
| 24 | 26 | #define TEXT_SIZE 50000 |
| 25 | 27 | #define DATA_SIZE 50000 |
| 26 | −#define SYM_TABLE_SIZE 10000 | |
| 27 | −#define MACRO_STACK_SIZE 32 | |
| 28 | 28 | #define INCLUDE_STACK_SIZE 32 |
| 29 | 29 | #define IFDEF_STACK_SIZE 64 |
| 30 | 30 | #define VSTACK_SIZE 64 |
| 31 | +#define STRING_MAX_SIZE 1024 | |
| 32 | +#define INCLUDE_PATHS_MAX 32 | |
| 31 | 33 | |
| 32 | 34 | #define NB_REGS 3 |
| 33 | 35 | |
| 36 | +/* token symbol management */ | |
| 37 | +typedef struct TokenSym { | |
| 38 | + struct TokenSym *next; | |
| 39 | + int tok; /* token number */ | |
| 40 | + int len; | |
| 41 | + char str[1]; | |
| 42 | +} TokenSym; | |
| 43 | + | |
| 34 | 44 | /* symbol management */ |
| 35 | 45 | typedef struct Sym { |
| 36 | 46 | int v; /* symbol token */ |
| @@ -47,6 +57,10 @@ typedef struct Sym { · 10 unmodified lines | ||
| 47 | 57 | #define FUNC_OLD 2 /* old function prototype */ |
| 48 | 58 | #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */ |
| 49 | 59 | |
| 60 | +/* field t for macros */ | |
| 61 | +#define MACRO_OBJ 0 /* object like macro */ | |
| 62 | +#define MACRO_FUNC 1 /* function like macro */ | |
| 63 | + | |
| 50 | 64 | typedef struct { |
| 51 | 65 | FILE *file; |
| 52 | 66 | char *filename; |
| @@ -62,14 +76,18 @@ typedef struct { · 9 unmodified lines | ||
| 62 | 76 | */ |
| 63 | 77 | FILE *file; |
| 64 | 78 | int tok, tok1, tokc, rsym, anon_sym, |
| 65 | − prog, ind, loc, glo, vt, vc, line_num; | |
| 66 | −char *idtable, *idptr, *filename; | |
| 79 | + prog, ind, loc, glo, vt, vc, const_wanted, line_num; | |
| 80 | +TokenSym *first_ident; | |
| 81 | +char token_buf[STRING_MAX_SIZE + 1]; | |
| 82 | +char *filename; | |
| 67 | 83 | Sym *define_stack, *global_stack, *local_stack, *label_stack; |
| 68 | 84 | |
| 69 | 85 | int vstack[VSTACK_SIZE], *vstack_ptr; |
| 70 | −char *macro_stack[MACRO_STACK_SIZE], **macro_stack_ptr, *macro_ptr; | |
| 86 | +int *macro_ptr, *macro_ptr_allocated; | |
| 71 | 87 | IncludeFile include_stack[INCLUDE_STACK_SIZE], *include_stack_ptr; |
| 72 | 88 | int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; |
| 89 | +char *include_paths[INCLUDE_PATHS_MAX]; | |
| 90 | +int nb_include_paths; | |
| 73 | 91 | |
| 74 | 92 | /* The current value can be: */ |
| 75 | 93 | #define VT_VALMASK 0x000f |
| @@ -132,7 +150,11 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 56 unmodified lines | ||
| 132 | 150 | #define TOK_UMOD 0xb1 /* unsigned modulo */ |
| 133 | 151 | #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */ |
| 134 | 152 | #define TOK_NUM 0xb3 /* number in tokc */ |
| 153 | +#define TOK_CCHAR 0xb4 /* char constant in tokc */ | |
| 154 | +#define TOK_STR 0xb5 /* pointer to string in tokc */ | |
| 135 | 155 | |
| 156 | +#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */ | |
| 157 | + | |
| 136 | 158 | #define TOK_SHL 0x01 /* shift left */ |
| 137 | 159 | #define TOK_SAR 0x02 /* signed shift right */ |
| 138 | 160 | |
| @@ -148,8 +170,11 @@ int ifdef_stack[IFDEF_STACK_SIZE], *ifdef_stack_ptr; · 9 unmodified lines | ||
| 148 | 170 | #define TOK_A_SHL 0x81 |
| 149 | 171 | #define TOK_A_SAR 0x82 |
| 150 | 172 | |
| 173 | +/* all identificators and strings have token above that */ | |
| 174 | +#define TOK_IDENT 256 | |
| 175 | + | |
| 151 | 176 | enum { |
| 152 | − TOK_INT = 256, | |
| 177 | + TOK_INT = TOK_IDENT, | |
| 153 | 178 | TOK_VOID, |
| 154 | 179 | TOK_CHAR, |
| 155 | 180 | TOK_IF, |
| @@ -180,6 +205,7 @@ enum { · 24 unmodified lines | ||
| 180 | 205 | TOK_FLOAT, |
| 181 | 206 | TOK_DOUBLE, |
| 182 | 207 | |
| 208 | + TOK_SHORT, | |
| 183 | 209 | TOK_STRUCT, |
| 184 | 210 | TOK_UNION, |
| 185 | 211 | TOK_TYPEDEF, |
| @@ -195,6 +221,7 @@ enum { · 9 unmodified lines | ||
| 195 | 221 | TOK_ELIF, |
| 196 | 222 | TOK_ENDIF, |
| 197 | 223 | TOK_DEFINED, |
| 224 | + TOK_UNDEF, | |
| 198 | 225 | |
| 199 | 226 | /* special identifiers */ |
| 200 | 227 | TOK_MAIN, |
| @@ -202,6 +229,7 @@ enum { · 1 unmodified line | ||
| 202 | 229 | |
| 203 | 230 | void sum(); |
| 204 | 231 | void next(); |
| 232 | +void next_nomacro(); | |
| 205 | 233 | int expr_const(); |
| 206 | 234 | void expr_eq(); |
| 207 | 235 | void expr(); |
| @@ -209,6 +237,14 @@ void decl(); · 1 unmodified line | ||
| 209 | 237 | int gv(); |
| 210 | 238 | void move_reg(); |
| 211 | 239 | void save_reg(); |
| 240 | +void macro_subst(int **tok_str, int *tok_len, | |
| 241 | + Sym **nested_list, int *macro_str); | |
| 242 | +int save_reg_forced(int r); | |
| 243 | +int type_size(int t, int *a); | |
| 244 | +int pointed_type(int t); | |
| 245 | +int pointed_size(int t); | |
| 246 | +int ist(void); | |
| 247 | +int typ(int *v, int t); | |
| 212 | 248 | |
| 213 | 249 | int isid(c) |
| 214 | 250 | { |
| @@ -222,8 +258,6 @@ int isnum(c) · 7 unmodified lines | ||
| 222 | 258 | return c >= '0' & c <= '9'; |
| 223 | 259 | } |
| 224 | 260 | |
| 225 | −#ifndef TINY | |
| 226 | − | |
| 227 | 261 | void printline() |
| 228 | 262 | { |
| 229 | 263 | IncludeFile *f; |
| @@ -269,26 +303,98 @@ void test_lvalue() · 39 unmodified lines | ||
| 269 | 303 | expect("lvalue"); |
| 270 | 304 | } |
| 271 | 305 | |
| 272 | −#else | |
| 306 | +TokenSym *tok_alloc(char *str, int len) | |
| 307 | +{ | |
| 308 | + TokenSym *ts, **pts; | |
| 309 | + int t; | |
| 310 | + | |
| 311 | + t = TOK_IDENT; | |
| 312 | + pts = &first_ident; | |
| 313 | + while (1) { | |
| 314 | + ts = *pts; | |
| 315 | + if (!ts) | |
| 316 | + break; | |
| 317 | + if (ts->len == len && !memcmp(ts->str, str, len)) | |
| 318 | + return ts; | |
| 319 | + t++; | |
| 320 | + pts = &(ts->next); | |
| 321 | + } | |
| 322 | + ts = malloc(sizeof(TokenSym) + len); | |
| 323 | + if (!ts) | |
| 324 | + error("memory full"); | |
| 325 | + ts->tok = t; | |
| 326 | + ts->len = len; | |
| 327 | + ts->next = NULL; | |
| 328 | + memcpy(ts->str, str, len + 1); | |
| 273 | 329 | |
| 274 | −#define skip(c) next() | |
| 275 | −#define test_lvalue() | |
| 330 | + *pts = ts; | |
| 331 | + return ts; | |
| 332 | +} | |
| 276 | 333 | |
| 277 | −#endif | |
| 334 | +void add_char(char **pp, int c) | |
| 335 | +{ | |
| 336 | + char *p; | |
| 337 | + p = *pp; | |
| 338 | + if (c == '\'' || c == '\"' || c == '\\') { | |
| 339 | + /* XXX: could be more precise if char or string */ | |
| 340 | + *p++ = '\\'; | |
| 341 | + } | |
| 342 | + if (c >= 32 && c <= 126) { | |
| 343 | + *p++ = c; | |
| 344 | + } else { | |
| 345 | + *p++ = '\\'; | |
| 346 | + if (c == '\n') { | |
| 347 | + *p++ = 'n'; | |
| 348 | + } else { | |
| 349 | + *p++ = '0' + ((c >> 6) & 7); | |
| 350 | + *p++ = '0' + ((c >> 3) & 7); | |
| 351 | + *p++ = '0' + (c & 7); | |
| 352 | + } | |
| 353 | + } | |
| 354 | + *pp = p; | |
| 355 | +} | |
| 278 | 356 | |
| 279 | −char *get_tok_str(int v) | |
| 357 | +/* XXX: buffer overflow */ | |
| 358 | +char *get_tok_str(int v, int c) | |
| 280 | 359 | { |
| 281 | − int t; | |
| 360 | + static char buf[STRING_MAX_SIZE + 1]; | |
| 361 | + TokenSym *ts; | |
| 282 | 362 | char *p; |
| 283 | − p = idtable; | |
| 284 | − t = 256; | |
| 285 | − while (t != v) { | |
| 286 | − if (p >= idptr) | |
| 287 | − return 0; | |
| 288 | − while (*p++); | |
| 289 | − t++; | |
| 363 | + int i; | |
| 364 | + | |
| 365 | + if (v == TOK_NUM) { | |
| 366 | + sprintf(buf, "%d", c); | |
| 367 | + return buf; | |
| 368 | + } else if (v == TOK_CCHAR) { | |
| 369 | + p = buf; | |
| 370 | + *p++ = '\''; | |
| 371 | + add_char(&p, c); | |
| 372 | + *p++ = '\''; | |
| 373 | + *p = '\0'; | |
| 374 | + return buf; | |
| 375 | + } else if (v == TOK_STR) { | |
| 376 | + ts = (TokenSym *)c; | |
| 377 | + p = buf; | |
| 378 | + *p++ = '\"'; | |
| 379 | + for(i=0;i<ts->len;i++) | |
| 380 | + add_char(&p, ts->str[i]); | |
| 381 | + *p++ = '\"'; | |
| 382 | + *p = '\0'; | |
| 383 | + return buf; | |
| 384 | + } else if (v < TOK_IDENT) { | |
| 385 | + p = buf; | |
| 386 | + *p++ = v; | |
| 387 | + *p = '\0'; | |
| 388 | + return buf; | |
| 389 | + } else { | |
| 390 | + ts = first_ident; | |
| 391 | + while (ts != NULL) { | |
| 392 | + if (ts->tok == v) | |
| 393 | + return ts->str; | |
| 394 | + ts = ts->next; | |
| 395 | + } | |
| 396 | + return NULL; | |
| 290 | 397 | } |
| 291 | − return p; | |
| 292 | 398 | } |
| 293 | 399 | |
| 294 | 400 | /* find a symbol and return its associated structure. 's' is the top |
| @@ -312,7 +418,7 @@ Sym *sym_push1(Sym **ps, int v, int t, int c) · 17 unmodified lines | ||
| 312 | 418 | s->v = v; |
| 313 | 419 | s->t = t; |
| 314 | 420 | s->c = c; |
| 315 | − s->next = 0; | |
| 421 | + s->next = NULL; | |
| 316 | 422 | s->prev = *ps; |
| 317 | 423 | *ps = s; |
| 318 | 424 | return s; |
| @@ -331,7 +437,6 @@ Sym *sym_find(int v) · 12 unmodified lines | ||
| 331 | 437 | /* push a given symbol on the symbol stack */ |
| 332 | 438 | Sym *sym_push(int v, int t, int c) |
| 333 | 439 | { |
| 334 | − // printf("sym_push: %x %s type=%x\n", v, get_tok_str(v), t); | |
| 335 | 440 | if (local_stack) |
| 336 | 441 | return sym_push1(&local_stack, v, t, c); |
| 337 | 442 | else |
| @@ -346,7 +451,6 @@ void sym_pop(Sym **ps, Sym *b) · 8 unmodified lines | ||
| 346 | 451 | s = *ps; |
| 347 | 452 | while(s != b) { |
| 348 | 453 | ss = s->prev; |
| 349 | − // printf("sym_pop: %x %s type=%x\n", s->v, get_tok_str(s->v), s->t); | |
| 350 | 454 | free(s); |
| 351 | 455 | s = ss; |
| 352 | 456 | } |
| @@ -358,8 +462,6 @@ int ch, ch1; · 5 unmodified lines | ||
| 358 | 462 | /* read next char from current input file */ |
| 359 | 463 | void inp() |
| 360 | 464 | { |
| 361 | − int c; | |
| 362 | − | |
| 363 | 465 | redo: |
| 364 | 466 | ch1 = fgetc(file); |
| 365 | 467 | if (ch1 == -1) { |
| @@ -379,26 +481,17 @@ void inp() · 13 unmodified lines | ||
| 379 | 481 | // printf("ch1=%c 0x%x\n", ch1, ch1); |
| 380 | 482 | } |
| 381 | 483 | |
| 382 | −/* input with '\\n' handling and macro subtitution if in macro state */ | |
| 484 | +/* input with '\\n' handling */ | |
| 383 | 485 | void minp() |
| 384 | 486 | { |
| 385 | − if (macro_ptr != 0) { | |
| 386 | − ch = *macro_ptr++; | |
| 387 | − /* end of macro ? */ | |
| 388 | − if (ch == '\0') { | |
| 389 | − macro_ptr = *--macro_stack_ptr; | |
| 390 | − ch = (int)*--macro_stack_ptr; | |
| 391 | − } | |
| 392 | − } else { | |
| 393 | 487 | redo: |
| 394 | − ch = ch1; | |
| 488 | + ch = ch1; | |
| 489 | + inp(); | |
| 490 | + if (ch == '\\' && ch1 == '\n') { | |
| 395 | 491 | inp(); |
| 396 | − if (ch == '\\' && ch1 == '\n') { | |
| 397 | − inp(); | |
| 398 | − goto redo; | |
| 399 | − } | |
| 492 | + goto redo; | |
| 400 | 493 | } |
| 401 | − // printf("ch=%c 0x%x\n", ch, ch); | |
| 494 | + //printf("ch=%c 0x%x\n", ch, ch); | |
| 402 | 495 | } |
| 403 | 496 | |
| 404 | 497 | /* same as minp, but also skip comments */ |
| @@ -458,7 +551,7 @@ void preprocess_skip() · 53 unmodified lines | ||
| 458 | 551 | skip_spaces(); |
| 459 | 552 | if (ch == '#') { |
| 460 | 553 | cinp(); |
| 461 | − next(); | |
| 554 | + next_nomacro(); | |
| 462 | 555 | if (a == 0 && |
| 463 | 556 | (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF)) |
| 464 | 557 | break; |
| @@ -497,53 +590,145 @@ char *get_str(c) · 32 unmodified lines | ||
| 497 | 590 | return str; |
| 498 | 591 | } |
| 499 | 592 | |
| 500 | −/* return next token without macro substitution */ | |
| 501 | −void next_nomacro() | |
| 593 | +void tok_add(int **tok_str, int *tok_len, int t) | |
| 502 | 594 | { |
| 503 | − Sym *s; | |
| 504 | − /* hack to avoid replacing id by defined value */ | |
| 505 | − s = define_stack; | |
| 506 | − define_stack = 0; | |
| 507 | − next(); | |
| 508 | − define_stack = s; | |
| 595 | + int len, *str; | |
| 596 | + len = *tok_len; | |
| 597 | + str = *tok_str; | |
| 598 | + if ((len & 63) == 0) { | |
| 599 | + str = realloc(str, (len + 64) * sizeof(int)); | |
| 600 | + if (!str) | |
| 601 | + return; | |
| 602 | + *tok_str = str; | |
| 603 | + } | |
| 604 | + str[len++] = t; | |
| 605 | + *tok_len = len; | |
| 509 | 606 | } |
| 510 | 607 | |
| 511 | −/* XXX: not correct yet (need to ensure it is constant) */ | |
| 512 | −int expr_preprocess() | |
| 608 | +void tok_add2(int **tok_str, int *tok_len, int t, int c) | |
| 513 | 609 | { |
| 514 | − int c; | |
| 610 | + tok_add(tok_str, tok_len, t); | |
| 611 | + if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) | |
| 612 | + tok_add(tok_str, tok_len, c); | |
| 613 | +} | |
| 515 | 614 | |
| 516 | − if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE) | |
| 517 | − error("too many nested macros"); | |
| 518 | − *macro_stack_ptr++ = (char *)'\n'; | |
| 519 | − *macro_stack_ptr++ = macro_ptr; | |
| 520 | − macro_ptr = get_str(';'); | |
| 521 | − cinp(); | |
| 615 | +/* eval an expression for #if/#elif */ | |
| 616 | +int expr_preprocess() | |
| 617 | +{ | |
| 618 | + int *str, len, c, t; | |
| 619 | + int *saved_macro_ptr; | |
| 620 | + | |
| 621 | + str = NULL; | |
| 622 | + len = 0; | |
| 623 | + while (1) { | |
| 624 | + skip_spaces(); | |
| 625 | + if (ch == '\n') | |
| 626 | + break; | |
| 627 | + next(); /* do macro subst */ | |
| 628 | + if (tok == TOK_DEFINED) { | |
| 629 | + next_nomacro(); | |
| 630 | + t = tok; | |
| 631 | + if (t == '(') | |
| 632 | + next_nomacro(); | |
| 633 | + c = sym_find1(define_stack, tok) != 0; | |
| 634 | + if (t == '(') | |
| 635 | + next_nomacro(); | |
| 636 | + tok = TOK_NUM; | |
| 637 | + tokc = c; | |
| 638 | + } else if (tok >= TOK_IDENT) { | |
| 639 | + /* if undefined macro */ | |
| 640 | + tok = TOK_NUM; | |
| 641 | + tokc = 0; | |
| 642 | + } | |
| 643 | + tok_add2(&str, &len, tok, tokc); | |
| 644 | + } | |
| 645 | + tok_add(&str, &len, -1); /* simulate end of file */ | |
| 646 | + tok_add(&str, &len, 0); | |
| 647 | + /* now evaluate C constant expression */ | |
| 648 | + saved_macro_ptr = macro_ptr; | |
| 649 | + macro_ptr = str; | |
| 522 | 650 | next(); |
| 523 | 651 | c = expr_const(); |
| 524 | − ch = '\n'; | |
| 652 | + macro_ptr = saved_macro_ptr; | |
| 653 | + free(str); | |
| 525 | 654 | return c != 0; |
| 526 | 655 | } |
| 527 | 656 | |
| 657 | +#ifdef DEBUG | |
| 658 | +void tok_print(int *str) | |
| 659 | +{ | |
| 660 | + int t, c; | |
| 661 | + | |
| 662 | + while (1) { | |
| 663 | + t = *str++; | |
| 664 | + if (!t) | |
| 665 | + break; | |
| 666 | + c = 0; | |
| 667 | + if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) | |
| 668 | + c = *str++; | |
| 669 | + printf(" %s", get_tok_str(t, c)); | |
| 670 | + } | |
| 671 | + printf("\n"); | |
| 672 | +} | |
| 673 | +#endif | |
| 674 | + | |
| 528 | 675 | void preprocess() |
| 529 | 676 | { |
| 530 | − char *str; | |
| 531 | − int size, n, c, v; | |
| 677 | + int size, i, c, v, t, *str, len; | |
| 532 | 678 | char buf[1024], *q, *p; |
| 533 | 679 | char buf1[1024]; |
| 534 | 680 | FILE *f; |
| 681 | + Sym **ps, *first, *s; | |
| 535 | 682 | |
| 536 | 683 | cinp(); |
| 537 | 684 | next_nomacro(); |
| 538 | 685 | redo: |
| 539 | 686 | if (tok == TOK_DEFINE) { |
| 540 | − next_nomacro(); /* XXX: should pass parameter to avoid macro subst */ | |
| 541 | − skip_spaces(); | |
| 542 | − /* now 'tok' is the macro symbol */ | |
| 543 | − /* a space is inserted after each macro */ | |
| 544 | − str = get_str(' '); | |
| 545 | − // printf("define %s '%s'\n", get_tok_str(tok), str); | |
| 546 | − sym_push1(&define_stack, tok, 0, (int)str); | |
| 687 | + next_nomacro(); | |
| 688 | + v = tok; | |
| 689 | + /* XXX: should check if same macro (ANSI) */ | |
| 690 | + if (sym_find1(define_stack, v)) | |
| 691 | + warning("macro redefinition"); | |
| 692 | + first = NULL; | |
| 693 | + t = MACRO_OBJ; | |
| 694 | + /* '(' must be just after macro definition for MACRO_FUNC */ | |
| 695 | + if (ch == '(') { | |
| 696 | + next_nomacro(); | |
| 697 | + next_nomacro(); | |
| 698 | + ps = &first; | |
| 699 | + while (tok != ')') { | |
| 700 | + s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0); | |
| 701 | + *ps = s; | |
| 702 | + ps = &s->next; | |
| 703 | + next_nomacro(); | |
| 704 | + if (tok != ',') | |
| 705 | + break; | |
| 706 | + next_nomacro(); | |
| 707 | + } | |
| 708 | + t = MACRO_FUNC; | |
| 709 | + } | |
| 710 | + str = NULL; | |
| 711 | + len = 0; | |
| 712 | + while (1) { | |
| 713 | + skip_spaces(); | |
| 714 | + if (ch == '\n' || ch == -1) | |
| 715 | + break; | |
| 716 | + next_nomacro(); | |
| 717 | + tok_add2(&str, &len, tok, tokc); | |
| 718 | + } | |
| 719 | + tok_add(&str, &len, 0); | |
| 720 | +#ifdef PP_DEBUG | |
| 721 | + printf("define %s %d: ", get_tok_str(v, 0), t); | |
| 722 | + tok_print(str); | |
| 723 | +#endif | |
| 724 | + s = sym_push1(&define_stack, v, t, (int)str); | |
| 725 | + s->next = first; | |
| 726 | + } else if (tok == TOK_UNDEF) { | |
| 727 | + next_nomacro(); | |
| 728 | + s = sym_find1(define_stack, tok); | |
| 729 | + /* undefine symbol by putting an invalid name */ | |
| 730 | + if (s) | |
| 731 | + s->v = 0; | |
| 547 | 732 | } else if (tok == TOK_INCLUDE) { |
| 548 | 733 | skip_spaces(); |
| 549 | 734 | if (ch == '<') { |
| @@ -577,12 +762,16 @@ void preprocess() · 27 unmodified lines | ||
| 577 | 762 | goto found; |
| 578 | 763 | } |
| 579 | 764 | /* now search in standard include path */ |
| 580 | − strcpy(buf1, INCLUDE_PATH); | |
| 581 | − strcat(buf1, "/"); | |
| 582 | − strcat(buf1, buf); | |
| 583 | − f = fopen(buf1, "r"); | |
| 584 | − if (!f) | |
| 585 | − error("include file not found"); | |
| 765 | + for(i=nb_include_paths - 1;i>=0;i--) { | |
| 766 | + strcpy(buf1, include_paths[i]); | |
| 767 | + strcat(buf1, "/"); | |
| 768 | + strcat(buf1, buf); | |
| 769 | + f = fopen(buf1, "r"); | |
| 770 | + if (f) | |
| 771 | + goto found; | |
| 772 | + } | |
| 773 | + error("include file not found"); | |
| 774 | + f = NULL; | |
| 586 | 775 | found: |
| 587 | 776 | /* push current file in stack */ |
| 588 | 777 | /* XXX: fix current line init */ |
| @@ -659,19 +848,46 @@ int getn(b) · 70 unmodified lines | ||
| 659 | 848 | return n; |
| 660 | 849 | } |
| 661 | 850 | |
| 662 | −void next() | |
| 851 | +/* read a character for string or char constant and eval escape codes */ | |
| 852 | +int getq() | |
| 663 | 853 | { |
| 664 | − int v, b; | |
| 665 | − char *q, *p; | |
| 666 | − Sym *s; | |
| 854 | + int c; | |
| 667 | 855 | |
| 668 | − /* special 'ungettok' case for label parsing */ | |
| 669 | − if (tok1) { | |
| 670 | − tok = tok1; | |
| 671 | − tok1 = 0; | |
| 672 | − return; | |
| 856 | + c = ch; | |
| 857 | + minp(); | |
| 858 | + if (c == '\\') { | |
| 859 | + if (isnum(ch)) { | |
| 860 | + return getn(8); | |
| 861 | + } else { | |
| 862 | + if (ch == 'a') | |
| 863 | + c = '\a'; | |
| 864 | + else if (ch == 'b') | |
| 865 | + c = '\b'; | |
| 866 | + else if (ch == 'f') | |
| 867 | + c = '\f'; | |
| 868 | + else if (ch == 'n') | |
| 869 | + c = '\n'; | |
| 870 | + else if (ch == 'r') | |
| 871 | + c = '\r'; | |
| 872 | + else if (ch == 't') | |
| 873 | + c = '\t'; | |
| 874 | + else if (ch == 'v') | |
| 875 | + c = '\v'; | |
| 876 | + else | |
| 877 | + c = ch; | |
| 878 | + minp(); | |
| 879 | + } | |
| 673 | 880 | } |
| 674 | − redo: | |
| 881 | + return c; | |
| 882 | +} | |
| 883 | + | |
| 884 | +/* return next token without macro substitution */ | |
| 885 | +void next_nomacro1() | |
| 886 | +{ | |
| 887 | + int b; | |
| 888 | + char *q; | |
| 889 | + TokenSym *ts; | |
| 890 | + | |
| 675 | 891 | /* skip spaces */ |
| 676 | 892 | while(1) { |
| 677 | 893 | while (ch == '\n') { |
| @@ -684,58 +900,61 @@ void next() · 6 unmodified lines | ||
| 684 | 900 | preprocess(); |
| 685 | 901 | } |
| 686 | 902 | } |
| 687 | − if (ch != ' ' && ch != 9) | |
| 903 | + if (ch != ' ' && ch != '\t' && ch != '\f') | |
| 688 | 904 | break; |
| 689 | 905 | cinp(); |
| 690 | 906 | } |
| 691 | 907 | if (isid(ch)) { |
| 692 | − q = idptr; | |
| 693 | − while(isid(ch) | isnum(ch)) { | |
| 908 | + q = token_buf; | |
| 909 | + while (isid(ch) | isnum(ch)) { | |
| 910 | + if (q >= token_buf + STRING_MAX_SIZE) | |
| 911 | + error("ident too long"); | |
| 694 | 912 | *q++ = ch; |
| 695 | 913 | cinp(); |
| 696 | 914 | } |
| 697 | − *q++ = '\0'; | |
| 698 | − p = idtable; | |
| 699 | − tok = 256; | |
| 700 | − while (p < idptr) { | |
| 701 | − if (strcmp(p, idptr) == 0) | |
| 702 | − break; | |
| 703 | − while (*p++); | |
| 704 | − tok++; | |
| 705 | − } | |
| 706 | − // printf("id=%s\n", idptr); | |
| 707 | − /* if not found, add symbol */ | |
| 708 | − if (p == idptr) | |
| 709 | − idptr = q; | |
| 710 | − /* if symbol is a define, prepare substitution */ | |
| 711 | − if (s = sym_find1(define_stack, tok)) { | |
| 712 | − if ((macro_stack_ptr - macro_stack) >= MACRO_STACK_SIZE) | |
| 713 | − error("too many nested macros"); | |
| 714 | − *macro_stack_ptr++ = (char *)ch; | |
| 715 | − *macro_stack_ptr++ = macro_ptr; | |
| 716 | − macro_ptr = (char *)s->c; | |
| 717 | − cinp(); | |
| 718 | − goto redo; | |
| 719 | − } | |
| 915 | + *q = '\0'; | |
| 916 | + ts = tok_alloc(token_buf, q - token_buf); | |
| 917 | + tok = ts->tok; | |
| 720 | 918 | } else if (isnum(ch)) { |
| 721 | 919 | /* number */ |
| 722 | 920 | b = 10; |
| 723 | 921 | if (ch == '0') { |
| 724 | 922 | cinp(); |
| 725 | 923 | b = 8; |
| 726 | − if (ch == 'x') { | |
| 924 | + if (ch == 'x' || ch == 'X') { | |
| 727 | 925 | cinp(); |
| 728 | 926 | b = 16; |
| 729 | 927 | } |
| 730 | 928 | } |
| 731 | 929 | tokc = getn(b); |
| 930 | + /* XXX: add unsigned constant support (ANSI) */ | |
| 931 | + while (ch == 'L' || ch == 'l' || ch == 'U' || ch == 'u') | |
| 932 | + cinp(); | |
| 732 | 933 | tok = TOK_NUM; |
| 934 | + } else if (ch == '\'') { | |
| 935 | + minp(); | |
| 936 | + tokc = getq(); | |
| 937 | + tok = TOK_CCHAR; | |
| 938 | + if (ch != '\'') | |
| 939 | + expect("\'"); | |
| 940 | + minp(); | |
| 941 | + } else if (ch == '\"') { | |
| 942 | + minp(); | |
| 943 | + q = token_buf; | |
| 944 | + while (ch != '\"') { | |
| 945 | + b = getq(); | |
| 946 | + if (ch == -1) | |
| 947 | + error("unterminated string"); | |
| 948 | + if (q >= token_buf + STRING_MAX_SIZE) | |
| 949 | + error("string too long"); | |
| 950 | + *q++ = b; | |
| 951 | + } | |
| 952 | + *q = '\0'; | |
| 953 | + tokc = (int)tok_alloc(token_buf, q - token_buf); | |
| 954 | + tok = TOK_STR; | |
| 955 | + minp(); | |
| 733 | 956 | } else { |
| 734 | −#ifdef TINY | |
| 735 | − q = "<=\236>=\235!=\225++\244--\242==\224"; | |
| 736 | −#else | |
| 737 | − q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250"; | |
| 738 | −#endif | |
| 957 | + q = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\247..\250##\266"; | |
| 739 | 958 | /* two chars */ |
| 740 | 959 | tok = ch; |
| 741 | 960 | cinp(); |
| @@ -764,7 +983,278 @@ void next() · 22 unmodified lines | ||
| 764 | 983 | else if (tok == '>') |
| 765 | 984 | tok = TOK_GT; |
| 766 | 985 | } |
| 767 | − // printf("tok=%x\n", tok); | |
| 986 | +} | |
| 987 | + | |
| 988 | +/* return next token without macro substitution. Can read input from | |
| 989 | + macro_ptr buffer */ | |
| 990 | +void next_nomacro() | |
| 991 | +{ | |
| 992 | + if (macro_ptr) { | |
| 993 | + tok = *macro_ptr; | |
| 994 | + if (tok) { | |
| 995 | + macro_ptr++; | |
| 996 | + if (tok == TOK_NUM || tok == TOK_CCHAR || tok == TOK_STR) | |
| 997 | + tokc = *macro_ptr++; | |
| 998 | + } | |
| 999 | + } else { | |
| 1000 | + next_nomacro1(); | |
| 1001 | + } | |
| 1002 | +} | |
| 1003 | + | |
| 1004 | +/* substitute args in macro_str and return allocated string */ | |
| 1005 | +int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) | |
| 1006 | +{ | |
| 1007 | + int *st, last_tok, t, c, notfirst, *str, len; | |
| 1008 | + Sym *s; | |
| 1009 | + TokenSym *ts; | |
| 1010 | + | |
| 1011 | + str = NULL; | |
| 1012 | + len = 0; | |
| 1013 | + last_tok = 0; | |
| 1014 | + while(1) { | |
| 1015 | + t = *macro_str++; | |
| 1016 | + if (!t) | |
| 1017 | + break; | |
| 1018 | + if (t == '#') { | |
| 1019 | + /* stringize */ | |
| 1020 | + t = *macro_str++; | |
| 1021 | + if (!t) | |
| 1022 | + break; | |
| 1023 | + s = sym_find1(args, t); | |
| 1024 | + if (s) { | |
| 1025 | + token_buf[0] = '\0'; | |
| 1026 | + st = (int *)s->c; | |
| 1027 | + /* XXX: buffer overflow */ | |
| 1028 | + notfirst = 0; | |
| 1029 | + while (*st) { | |
| 1030 | + if (notfirst) | |
| 1031 | + strcat(token_buf, " "); | |
| 1032 | + t = *st++; | |
| 1033 | + c = 0; | |
| 1034 | + if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) | |
| 1035 | + c = *st++; | |
| 1036 | + strcat(token_buf, get_tok_str(t, c)); | |
| 1037 | + notfirst = 1; | |
| 1038 | + } | |
| 1039 | +#ifdef PP_DEBUG | |
| 1040 | + printf("stringize: %s\n", token_buf); | |
| 1041 | +#endif | |
| 1042 | + /* add string */ | |
| 1043 | + ts = tok_alloc(token_buf, strlen(token_buf)); | |
| 1044 | + tok_add2(&str, &len, TOK_STR, (int)ts); | |
| 1045 | + } else { | |
| 1046 | + tok_add(&str, &len, t); | |
| 1047 | + } | |
| 1048 | + } else if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) { | |
| 1049 | + tok_add2(&str, &len, t, *macro_ptr++); | |
| 1050 | + } else { | |
| 1051 | + s = sym_find1(args, t); | |
| 1052 | + if (s) { | |
| 1053 | + st = (int *)s->c; | |
| 1054 | + /* if '##' is present before or after , no arg substitution */ | |
| 1055 | + if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) { | |
| 1056 | + while (*st) | |
| 1057 | + tok_add(&str, &len, *st++); | |
| 1058 | + } else { | |
| 1059 | + macro_subst(&str, &len, nested_list, st); | |
| 1060 | + } | |
| 1061 | + } else { | |
| 1062 | + tok_add(&str, &len, t); | |
| 1063 | + } | |
| 1064 | + } | |
| 1065 | + last_tok = t; | |
| 1066 | + } | |
| 1067 | + tok_add(&str, &len, 0); | |
| 1068 | + return str; | |
| 1069 | +} | |
| 1070 | + | |
| 1071 | +/* handle the '##' operator */ | |
| 1072 | +int *macro_twosharps(int *macro_str) | |
| 1073 | +{ | |
| 1074 | + TokenSym *ts; | |
| 1075 | + int *macro_str1, macro_str1_len, *macro_ptr1; | |
| 1076 | + int t, c; | |
| 1077 | + char *p; | |
| 1078 | + | |
| 1079 | + macro_str1 = NULL; | |
| 1080 | + macro_str1_len = 0; | |
| 1081 | + tok = 0; | |
| 1082 | + while (1) { | |
| 1083 | + next_nomacro(); | |
| 1084 | + if (tok == 0) | |
| 1085 | + break; | |
| 1086 | + if (*macro_ptr == TOK_TWOSHARPS) { | |
| 1087 | + macro_ptr++; | |
| 1088 | + macro_ptr1 = macro_ptr; | |
| 1089 | + t = *macro_ptr; | |
| 1090 | + if (t) { | |
| 1091 | + macro_ptr++; | |
| 1092 | + c = 0; | |
| 1093 | + if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) | |
| 1094 | + c = *macro_ptr++; | |
| 1095 | + /* XXX: we handle only most common cases: | |
| 1096 | + ident + ident or ident + number */ | |
| 1097 | + if (tok >= TOK_IDENT && | |
| 1098 | + (t >= TOK_IDENT || t == TOK_NUM)) { | |
| 1099 | + /* XXX: buffer overflow */ | |
| 1100 | + p = get_tok_str(tok, tokc); | |
| 1101 | + strcpy(token_buf, p); | |
| 1102 | + p = get_tok_str(t, c); | |
| 1103 | + strcat(token_buf, p); | |
| 1104 | + ts = tok_alloc(token_buf, strlen(token_buf)); | |
| 1105 | + tok_add2(¯o_str1, ¯o_str1_len, ts->tok, 0); | |
| 1106 | + } else { | |
| 1107 | + /* cannot merge tokens: skip '##' */ | |
| 1108 | + macro_ptr = macro_ptr1; | |
| 1109 | + } | |
| 1110 | + } | |
| 1111 | + } else { | |
| 1112 | + tok_add2(¯o_str1, ¯o_str1_len, tok, tokc); | |
| 1113 | + } | |
| 1114 | + } | |
| 1115 | + tok_add(¯o_str1, ¯o_str1_len, 0); | |
| 1116 | + return macro_str1; | |
| 1117 | +} | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | +/* do macro substitution of macro_str and add result to | |
| 1122 | + (tok_str,tok_len). If macro_str is NULL, then input stream token is | |
| 1123 | + substituted. 'nested_list' is the list of all macros we got inside | |
| 1124 | + to avoid recursing. */ | |
| 1125 | +void macro_subst(int **tok_str, int *tok_len, | |
| 1126 | + Sym **nested_list, int *macro_str) | |
| 1127 | +{ | |
| 1128 | + Sym *s, *args, *sa; | |
| 1129 | + int *str, parlevel, len, *mstr, t, *saved_macro_ptr; | |
| 1130 | + int mstr_allocated, *macro_str1; | |
| 1131 | + | |
| 1132 | + saved_macro_ptr = macro_ptr; | |
| 1133 | + macro_ptr = macro_str; | |
| 1134 | + macro_str1 = NULL; | |
| 1135 | + if (macro_str) { | |
| 1136 | + /* first scan for '##' operator handling */ | |
| 1137 | + macro_str1 = macro_twosharps(macro_str); | |
| 1138 | + macro_ptr = macro_str1; | |
| 1139 | + } | |
| 1140 | + | |
| 1141 | + while (1) { | |
| 1142 | + next_nomacro(); | |
| 1143 | + if (tok == 0) | |
| 1144 | + break; | |
| 1145 | + /* if symbol is a macro, prepare substitution */ | |
| 1146 | + s = sym_find1(define_stack, tok); | |
| 1147 | + if (s) { | |
| 1148 | + /* if nested substitution, do nothing */ | |
| 1149 | + if (sym_find1(*nested_list, tok)) | |
| 1150 | + goto no_subst; | |
| 1151 | + mstr = (int *)s->c; | |
| 1152 | + mstr_allocated = 0; | |
| 1153 | + if (s->t == MACRO_FUNC) { | |
| 1154 | + /* NOTE: we do not use next_nomacro to avoid eating the | |
| 1155 | + next token. XXX: find better solution */ | |
| 1156 | + if (macro_ptr) { | |
| 1157 | + t = *macro_ptr; | |
| 1158 | + } else { | |
| 1159 | + while (ch == ' ' || ch == '\t' || ch == '\n') | |
| 1160 | + cinp(); | |
| 1161 | + t = ch; | |
| 1162 | + } | |
| 1163 | + if (t != '(') /* no macro subst */ | |
| 1164 | + goto no_subst; | |
| 1165 | + | |
| 1166 | + /* argument macro */ | |
| 1167 | + next_nomacro(); | |
| 1168 | + next_nomacro(); | |
| 1169 | + args = NULL; | |
| 1170 | + sa = s->next; | |
| 1171 | + while (tok != ')' && sa) { | |
| 1172 | + len = 0; | |
| 1173 | + str = NULL; | |
| 1174 | + parlevel = 0; | |
| 1175 | + while ((parlevel > 0 || (tok != ')' && tok != ',')) && | |
| 1176 | + tok != -1) { | |
| 1177 | + if (tok == '(') | |
| 1178 | + parlevel++; | |
| 1179 | + else if (tok == ')') | |
| 1180 | + parlevel--; | |
| 1181 | + tok_add2(&str, &len, tok, tokc); | |
| 1182 | + next_nomacro(); | |
| 1183 | + } | |
| 1184 | + tok_add(&str, &len, 0); | |
| 1185 | + sym_push1(&args, sa->v & ~SYM_FIELD, 0, (int)str); | |
| 1186 | + if (tok != ',') | |
| 1187 | + break; | |
| 1188 | + next_nomacro(); | |
| 1189 | + sa = sa->next; | |
| 1190 | + } | |
| 1191 | + if (tok != ')') | |
| 1192 | + expect(")"); | |
| 1193 | + /* now subst each arg */ | |
| 1194 | + mstr = macro_arg_subst(nested_list, mstr, args); | |
| 1195 | + sym_pop(&args, NULL); | |
| 1196 | + mstr_allocated = 1; | |
| 1197 | + } | |
| 1198 | + sym_push1(nested_list, s->v, 0, 0); | |
| 1199 | + macro_subst(tok_str, tok_len, nested_list, mstr); | |
| 1200 | + sym_pop(nested_list, (*nested_list)->prev); | |
| 1201 | + if (mstr_allocated) | |
| 1202 | + free(mstr); | |
| 1203 | + /* only replace one macro while parsing input stream */ | |
| 1204 | + if (!macro_str) | |
| 1205 | + return; | |
| 1206 | + } else { | |
| 1207 | + no_subst: | |
| 1208 | + /* no need to add if reading input stream */ | |
| 1209 | + if (!macro_str) | |
| 1210 | + return; | |
| 1211 | + tok_add2(tok_str, tok_len, tok, tokc); | |
| 1212 | + } | |
| 1213 | + } | |
| 1214 | + macro_ptr = saved_macro_ptr; | |
| 1215 | + if (macro_str1) | |
| 1216 | + free(macro_str1); | |
| 1217 | +} | |
| 1218 | + | |
| 1219 | +/* return next token with macro substitution */ | |
| 1220 | +void next() | |
| 1221 | +{ | |
| 1222 | + int len, *ptr; | |
| 1223 | + Sym *nested_list; | |
| 1224 | + | |
| 1225 | + /* special 'ungettok' case for label parsing */ | |
| 1226 | + if (tok1) { | |
| 1227 | + tok = tok1; | |
| 1228 | + tok1 = 0; | |
| 1229 | + } else { | |
| 1230 | + redo: | |
| 1231 | + if (!macro_ptr) { | |
| 1232 | + /* if not reading from macro substuted string, then try to substitute */ | |
| 1233 | + len = 0; | |
| 1234 | + ptr = NULL; | |
| 1235 | + nested_list = NULL; | |
| 1236 | + macro_subst(&ptr, &len, &nested_list, NULL); | |
| 1237 | + if (ptr) { | |
| 1238 | + tok_add(&ptr, &len, 0); | |
| 1239 | + macro_ptr = ptr; | |
| 1240 | + macro_ptr_allocated = ptr; | |
| 1241 | + goto redo; | |
| 1242 | + } | |
| 1243 | + if (tok == 0) | |
| 1244 | + goto redo; | |
| 1245 | + } else { | |
| 1246 | + next_nomacro(); | |
| 1247 | + if (tok == 0) { | |
| 1248 | + /* end of macro string: free it */ | |
| 1249 | + free(macro_ptr_allocated); | |
| 1250 | + macro_ptr = NULL; | |
| 1251 | + goto redo; | |
| 1252 | + } | |
| 1253 | + } | |
| 1254 | + } | |
| 1255 | +#ifdef DEBUG | |
| 1256 | + printf("token = %s\n", get_tok_str(tok, tokc)); | |
| 1257 | +#endif | |
| 768 | 1258 | } |
| 769 | 1259 | |
| 770 | 1260 | void swap(int *p, int *q) |
| @@ -1004,7 +1494,7 @@ void gen_op1(op, r, fr) · 233 unmodified lines | ||
| 1004 | 1494 | /* end of X86 code generator */ |
| 1005 | 1495 | /*************************************************************/ |
| 1006 | 1496 | |
| 1007 | −int save_reg_forced(r) | |
| 1497 | +int save_reg_forced(int r) | |
| 1008 | 1498 | { |
| 1009 | 1499 | int i, l, *p, t; |
| 1010 | 1500 | /* store register */ |
| @@ -1156,6 +1646,20 @@ void gen_opc(op) · 145 unmodified lines | ||
| 1156 | 1646 | case TOK_SHL: vc <<= fc; break; |
| 1157 | 1647 | case TOK_SHR: vc = (unsigned)vc >> fc; break; |
| 1158 | 1648 | case TOK_SAR: vc >>= fc; break; |
| 1649 | + /* tests */ | |
| 1650 | + case TOK_ULT: vc = (unsigned)vc < (unsigned)fc; break; | |
| 1651 | + case TOK_UGE: vc = (unsigned)vc >= (unsigned)fc; break; | |
| 1652 | + case TOK_EQ: vc = vc == fc; break; | |
| 1653 | + case TOK_NE: vc = vc != fc; break; | |
| 1654 | + case TOK_ULE: vc = (unsigned)vc <= (unsigned)fc; break; | |
| 1655 | + case TOK_UGT: vc = (unsigned)vc > (unsigned)fc; break; | |
| 1656 | + case TOK_LT: vc = vc < fc; break; | |
| 1657 | + case TOK_GE: vc = vc >= fc; break; | |
| 1658 | + case TOK_LE: vc = vc <= fc; break; | |
| 1659 | + case TOK_GT: vc = vc > fc; break; | |
| 1660 | + /* logical */ | |
| 1661 | + case TOK_LAND: vc = vc && fc; break; | |
| 1662 | + case TOK_LOR: vc = vc || fc; break; | |
| 1159 | 1663 | default: |
| 1160 | 1664 | goto general_case; |
| 1161 | 1665 | } |
| @@ -1208,13 +1712,13 @@ void gen_opc(op) · 46 unmodified lines | ||
| 1208 | 1712 | } |
| 1209 | 1713 | } |
| 1210 | 1714 | |
| 1211 | −int pointed_size(t) | |
| 1715 | +int pointed_size(int t) | |
| 1212 | 1716 | { |
| 1213 | 1717 | return type_size(pointed_type(t), &t); |
| 1214 | 1718 | } |
| 1215 | 1719 | |
| 1216 | 1720 | /* generic gen_op: handles types problems */ |
| 1217 | −void gen_op(op) | |
| 1721 | +void gen_op(int op) | |
| 1218 | 1722 | { |
| 1219 | 1723 | int u, t1, t2; |
| 1220 | 1724 | |
| @@ -1354,14 +1858,6 @@ void inc(post, c) · 133 unmodified lines | ||
| 1354 | 1858 | vpop(&vt, &vc); |
| 1355 | 1859 | } |
| 1356 | 1860 | |
| 1357 | −int expr_const() | |
| 1358 | −{ | |
| 1359 | − expr_eq(); | |
| 1360 | − if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST) | |
| 1361 | − expect("constant"); | |
| 1362 | − return vc; | |
| 1363 | −} | |
| 1364 | − | |
| 1365 | 1861 | /* enum/struct/union declaration */ |
| 1366 | 1862 | int struct_decl(u) |
| 1367 | 1863 | { |
| @@ -1393,7 +1889,7 @@ int struct_decl(u) · 25 unmodified lines | ||
| 1393 | 1889 | /* cannot be empty */ |
| 1394 | 1890 | c = 0; |
| 1395 | 1891 | maxalign = 0; |
| 1396 | − slast = 0; | |
| 1892 | + slast = NULL; | |
| 1397 | 1893 | while (1) { |
| 1398 | 1894 | if (a == TOK_ENUM) { |
| 1399 | 1895 | v = tok; |
| @@ -1449,30 +1945,27 @@ int struct_decl(u) · 49 unmodified lines | ||
| 1449 | 1945 | and skip it. |
| 1450 | 1946 | XXX: A '2' is ored to ensure non zero return if int type. |
| 1451 | 1947 | */ |
| 1452 | −int ist() | |
| 1948 | +int ist(void) | |
| 1453 | 1949 | { |
| 1454 | − int t, n, v; | |
| 1950 | + int t; | |
| 1455 | 1951 | Sym *s; |
| 1456 | 1952 | |
| 1457 | 1953 | t = 0; |
| 1458 | 1954 | while(1) { |
| 1459 | −#ifndef TINY | |
| 1460 | 1955 | if (tok == TOK_ENUM) { |
| 1461 | 1956 | t |= struct_decl(VT_ENUM); |
| 1462 | 1957 | } else if (tok == TOK_STRUCT || tok == TOK_UNION) { |
| 1463 | 1958 | t |= struct_decl(VT_STRUCT); |
| 1464 | − } else | |
| 1465 | −#endif | |
| 1466 | − { | |
| 1959 | + } else { | |
| 1467 | 1960 | if (tok == TOK_CHAR) { |
| 1468 | 1961 | t |= VT_BYTE; |
| 1469 | 1962 | } else if (tok == TOK_VOID) { |
| 1470 | 1963 | t |= VT_VOID; |
| 1471 | − } else if (tok == TOK_INT | | |
| 1964 | + } else if (tok == TOK_INT | tok == TOK_SHORT | | |
| 1472 | 1965 | (tok >= TOK_CONST & tok <= TOK_INLINE)) { |
| 1473 | 1966 | /* ignored types */ |
| 1474 | − } else if (tok == TOK_FLOAT & tok == TOK_DOUBLE) { | |
| 1475 | − error("floats not supported"); | |
| 1967 | + } else if (tok == TOK_FLOAT || tok == TOK_DOUBLE) { | |
| 1968 | + warning("floats not supported"); | |
| 1476 | 1969 | } else if (tok == TOK_EXTERN) { |
| 1477 | 1970 | t |= VT_EXTERN; |
| 1478 | 1971 | } else if (tok == TOK_STATIC) { |
| @@ -1618,11 +2111,11 @@ int typ(int *v, int t) · 139 unmodified lines | ||
| 1618 | 2111 | /* define a new external reference to a function 'v' of type 'u' */ |
| 1619 | 2112 | Sym *external_func(v, u) |
| 1620 | 2113 | { |
| 1621 | − int t, n, p; | |
| 2114 | + int n; | |
| 1622 | 2115 | Sym *s; |
| 1623 | 2116 | s = sym_find(v); |
| 1624 | 2117 | if (!s) { |
| 1625 | − n = dlsym(0, get_tok_str(v)); | |
| 2118 | + n = (int)dlsym(0, get_tok_str(v, 0)); | |
| 1626 | 2119 | if (n == 0) { |
| 1627 | 2120 | /* used to generate symbol list */ |
| 1628 | 2121 | s = sym_push1(&global_stack, |
| @@ -1636,31 +2129,6 @@ Sym *external_func(v, u) · 7 unmodified lines | ||
| 1636 | 2129 | return s; |
| 1637 | 2130 | } |
| 1638 | 2131 | |
| 1639 | −/* read a character for string or char constant and eval escape codes */ | |
| 1640 | −int getq() | |
| 1641 | −{ | |
| 1642 | − int c; | |
| 1643 | − | |
| 1644 | − c = ch; | |
| 1645 | − minp(); | |
| 1646 | − if (c == '\\') { | |
| 1647 | − if (isnum(ch)) { | |
| 1648 | − return getn(8); | |
| 1649 | − } else { | |
| 1650 | − if (ch == 'n') | |
| 1651 | − c = '\n'; | |
| 1652 | − else if (ch == 'r') | |
| 1653 | − c = '\r'; | |
| 1654 | − else if (ch == 't') | |
| 1655 | − c = '\t'; | |
| 1656 | − else | |
| 1657 | − c = ch; | |
| 1658 | − minp(); | |
| 1659 | − } | |
| 1660 | − } | |
| 1661 | − return c; | |
| 1662 | −} | |
| 1663 | − | |
| 1664 | 2132 | void indir() |
| 1665 | 2133 | { |
| 1666 | 2134 | if (vt & VT_LVAL) |
| @@ -1677,37 +2145,20 @@ void unary() · 10 unmodified lines | ||
| 1677 | 2145 | int n, t, ft, fc, p, r; |
| 1678 | 2146 | Sym *s; |
| 1679 | 2147 | |
| 1680 | − if (tok == TOK_NUM) { | |
| 2148 | + if (tok == TOK_NUM || tok == TOK_CCHAR) { | |
| 1681 | 2149 | vset(VT_CONST, tokc); |
| 1682 | 2150 | next(); |
| 1683 | − } else | |
| 1684 | − if (tok == '\'') { | |
| 1685 | − vset(VT_CONST, getq()); | |
| 1686 | − next(); /* skip char */ | |
| 1687 | − skip('\''); | |
| 1688 | − } else if (tok == '\"') { | |
| 2151 | + } else if (tok == TOK_STR) { | |
| 2152 | + TokenSym *ts; | |
| 1689 | 2153 | /* generate (char *) type */ |
| 1690 | 2154 | vset(VT_CONST | mk_pointer(VT_TYPE), glo); |
| 1691 | − while (tok == '\"') { | |
| 1692 | − while (ch != '\"') { | |
| 1693 | − if (ch == -1) | |
| 1694 | − error("unterminated string"); | |
| 1695 | − *(char *)glo++ = getq(); | |
| 1696 | − } | |
| 1697 | − minp(); | |
| 2155 | + while (tok == TOK_STR) { | |
| 2156 | + ts = (TokenSym *)tokc; | |
| 2157 | + memcpy((void *)glo, ts->str, ts->len); | |
| 2158 | + glo += ts->len; | |
| 1698 | 2159 | next(); |
| 1699 | 2160 | } |
| 1700 | 2161 | *(char *)glo++ = 0; |
| 1701 | − } else if (tok == TOK_DEFINED) { | |
| 1702 | − /* XXX: should only be used in preprocess expr parsing */ | |
| 1703 | − next_nomacro(); | |
| 1704 | − t = tok; | |
| 1705 | − if (t == '(') | |
| 1706 | − next_nomacro(); | |
| 1707 | − vset(VT_CONST, sym_find1(define_stack, tok) != 0); | |
| 1708 | − next(); | |
| 1709 | − if (t == '(') | |
| 1710 | − skip(')'); | |
| 1711 | 2162 | } else { |
| 1712 | 2163 | t = tok; |
| 1713 | 2164 | next(); |
| @@ -1732,11 +2183,13 @@ void unary() · 18 unmodified lines | ||
| 1732 | 2183 | } else |
| 1733 | 2184 | if (t == '!') { |
| 1734 | 2185 | unary(); |
| 1735 | − if ((vt & VT_VALMASK) == VT_CMP) | |
| 2186 | + if ((vt & (VT_CONST | VT_LVAL)) == VT_CONST) | |
| 2187 | + vc = !vc; | |
| 2188 | + else if ((vt & VT_VALMASK) == VT_CMP) | |
| 1736 | 2189 | vc = vc ^ 1; |
| 1737 | 2190 | else |
| 1738 | 2191 | vset(VT_JMP, gtst(1, 0)); |
| 1739 | − } else | |
| 2192 | + } else | |
| 1740 | 2193 | if (t == '~') { |
| 1741 | 2194 | unary(); |
| 1742 | 2195 | vpush(); |
| @@ -1818,7 +2271,10 @@ void unary() · 75 unmodified lines | ||
| 1818 | 2271 | vset(VT_CONST, s->c); |
| 1819 | 2272 | gen_op('+'); |
| 1820 | 2273 | /* change type to field type, and set to lvalue */ |
| 1821 | − vt = (vt & VT_TYPEN) | VT_LVAL | s->t; | |
| 2274 | + vt = (vt & VT_TYPEN) | s->t; | |
| 2275 | + /* an array is never an lvalue */ | |
| 2276 | + if (!(vt & VT_ARRAY)) | |
| 2277 | + vt |= VT_LVAL; | |
| 1822 | 2278 | next(); |
| 1823 | 2279 | } else if (tok == '[') { |
| 1824 | 2280 | next(); |
| @@ -1912,7 +2368,7 @@ void uneq() · 87 unmodified lines | ||
| 1912 | 2368 | |
| 1913 | 2369 | void sum(l) |
| 1914 | 2370 | { |
| 1915 | − int ft, fc, t; | |
| 2371 | + int t; | |
| 1916 | 2372 | |
| 1917 | 2373 | if (l == 0) |
| 1918 | 2374 | uneq(); |
| @@ -1926,7 +2382,9 @@ void sum(l) · 7 unmodified lines | ||
| 1926 | 2382 | (l == 4 & (tok == TOK_EQ | tok == TOK_NE)) | |
| 1927 | 2383 | (l == 5 & tok == '&') | |
| 1928 | 2384 | (l == 6 & tok == '^') | |
| 1929 | − (l == 7 & tok == '|')) { | |
| 2385 | + (l == 7 & tok == '|') | | |
| 2386 | + (l == 8 & tok == TOK_LAND) | | |
| 2387 | + (l == 9 & tok == TOK_LOR)) { | |
| 1930 | 2388 | vpush(); |
| 1931 | 2389 | t = tok; |
| 1932 | 2390 | next(); |
| @@ -1936,12 +2394,7 @@ void sum(l) · 3 unmodified lines | ||
| 1936 | 2394 | } |
| 1937 | 2395 | } |
| 1938 | 2396 | |
| 1939 | −#ifdef TINY | |
| 1940 | −void expr() | |
| 1941 | −{ | |
| 1942 | − sum(8); | |
| 1943 | −} | |
| 1944 | −#else | |
| 2397 | +/* only used if non constant */ | |
| 1945 | 2398 | void eand() |
| 1946 | 2399 | { |
| 1947 | 2400 | int t; |
| @@ -1964,7 +2417,7 @@ void eand() · 16 unmodified lines | ||
| 1964 | 2417 | |
| 1965 | 2418 | void eor() |
| 1966 | 2419 | { |
| 1967 | − int t, u; | |
| 2420 | + int t; | |
| 1968 | 2421 | |
| 1969 | 2422 | eand(); |
| 1970 | 2423 | t = 0; |
| @@ -1982,22 +2435,37 @@ void eor() · 11 unmodified lines | ||
| 1982 | 2435 | } |
| 1983 | 2436 | } |
| 1984 | 2437 | |
| 2438 | +/* XXX: better constant handling */ | |
| 1985 | 2439 | void expr_eq() |
| 1986 | 2440 | { |
| 1987 | − int t, u; | |
| 1988 | − | |
| 1989 | − eor(); | |
| 1990 | − if (tok == '?') { | |
| 1991 | − next(); | |
| 1992 | − t = gtst(1, 0); | |
| 1993 | − expr(); | |
| 1994 | − gv(); | |
| 1995 | − skip(':'); | |
| 1996 | − u = gjmp(0); | |
| 1997 | − gsym(t); | |
| 1998 | − expr_eq(); | |
| 1999 | − gv(); | |
| 2000 | − gsym(u); | |
| 2441 | + int t, u, c; | |
| 2442 | + | |
| 2443 | + if (const_wanted) { | |
| 2444 | + sum(10); | |
| 2445 | + if (tok == '?') { | |
| 2446 | + c = vc; | |
| 2447 | + next(); | |
| 2448 | + expr(); | |
| 2449 | + t = vc; | |
| 2450 | + skip(':'); | |
| 2451 | + expr_eq(); | |
| 2452 | + if (c) | |
| 2453 | + vc = t; | |
| 2454 | + } | |
| 2455 | + } else { | |
| 2456 | + eor(); | |
| 2457 | + if (tok == '?') { | |
| 2458 | + next(); | |
| 2459 | + t = gtst(1, 0); | |
| 2460 | + expr(); | |
| 2461 | + gv(); | |
| 2462 | + skip(':'); | |
| 2463 | + u = gjmp(0); | |
| 2464 | + gsym(t); | |
| 2465 | + expr_eq(); | |
| 2466 | + gv(); | |
| 2467 | + gsym(u); | |
| 2468 | + } | |
| 2001 | 2469 | } |
| 2002 | 2470 | } |
| 2003 | 2471 | |
| @@ -2011,7 +2479,17 @@ void expr() · 7 unmodified lines | ||
| 2011 | 2479 | } |
| 2012 | 2480 | } |
| 2013 | 2481 | |
| 2014 | −#endif | |
| 2482 | +int expr_const() | |
| 2483 | +{ | |
| 2484 | + int a; | |
| 2485 | + a = const_wanted; | |
| 2486 | + const_wanted = 1; | |
| 2487 | + expr_eq(); | |
| 2488 | + if ((vt & (VT_CONST | VT_LVAL)) != VT_CONST) | |
| 2489 | + expect("constant"); | |
| 2490 | + const_wanted = a; | |
| 2491 | + return vc; | |
| 2492 | +} | |
| 2015 | 2493 | |
| 2016 | 2494 | void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) |
| 2017 | 2495 | { |
| @@ -2079,9 +2557,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 61 unmodified lines | ||
| 2079 | 2557 | *csym = gjmp(*csym); |
| 2080 | 2558 | next(); |
| 2081 | 2559 | skip(';'); |
| 2082 | − } else | |
| 2083 | −#ifndef TINY | |
| 2084 | − if (tok == TOK_FOR) { | |
| 2560 | + } else if (tok == TOK_FOR) { | |
| 2085 | 2561 | int e; |
| 2086 | 2562 | next(); |
| 2087 | 2563 | skip('('); |
| @@ -2180,9 +2656,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 92 unmodified lines | ||
| 2180 | 2656 | oad(0xe9, s->c - ind - 5); /* jmp xxx */ |
| 2181 | 2657 | next(); |
| 2182 | 2658 | skip(';'); |
| 2183 | − } else | |
| 2184 | −#endif | |
| 2185 | − { | |
| 2659 | + } else { | |
| 2186 | 2660 | b = tok; |
| 2187 | 2661 | next(); |
| 2188 | 2662 | if (tok == ':') { |
| @@ -2272,7 +2746,7 @@ void decl(l) · 83 unmodified lines | ||
| 2272 | 2746 | if (t & VT_EXTERN) { |
| 2273 | 2747 | /* external variable */ |
| 2274 | 2748 | /* XXX: factorize with external function def */ |
| 2275 | − n = dlsym(NULL, get_tok_str(v)); | |
| 2749 | + n = (int)dlsym(NULL, get_tok_str(v, 0)); | |
| 2276 | 2750 | if (!n) |
| 2277 | 2751 | error("unknown external variable"); |
| 2278 | 2752 | sym_push(v, VT_CONST | t, n); |
| @@ -2306,16 +2780,47 @@ void decl(l) · 27 unmodified lines | ||
| 2306 | 2780 | } |
| 2307 | 2781 | } |
| 2308 | 2782 | |
| 2309 | −int main(int c, char **v) | |
| 2783 | +int main(int argc, char **argv) | |
| 2310 | 2784 | { |
| 2311 | 2785 | Sym *s; |
| 2312 | 2786 | int (*t)(); |
| 2313 | − if (c < 2) { | |
| 2314 | − printf("usage: tcc source ...\n"); | |
| 2315 | − return 1; | |
| 2787 | + char *p, *r; | |
| 2788 | + int optind; | |
| 2789 | + | |
| 2790 | + include_paths[0] = "/usr/include"; | |
| 2791 | + include_paths[1] = "/usr/lib/tcc"; | |
| 2792 | + include_paths[2] = "/usr/local/lib/tcc"; | |
| 2793 | + nb_include_paths = 3; | |
| 2794 | + | |
| 2795 | + /* add all tokens */ | |
| 2796 | + p = "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0float\0double\0short\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0undef\0main\0"; | |
| 2797 | + while (*p) { | |
| 2798 | + r = p; | |
| 2799 | + while (*r++); | |
| 2800 | + tok_alloc(p, r - p - 1); | |
| 2801 | + p = r; | |
| 2316 | 2802 | } |
| 2317 | − v++; | |
| 2318 | − filename = *v; | |
| 2803 | + | |
| 2804 | + optind = 1; | |
| 2805 | + while (1) { | |
| 2806 | + if (optind >= argc) { | |
| 2807 | + printf("usage: tcc [-Idir] infile [infile_arg...]\n"); | |
| 2808 | + return 1; | |
| 2809 | + } | |
| 2810 | + r = argv[optind]; | |
| 2811 | + if (r[0] != '-') | |
| 2812 | + break; | |
| 2813 | + if (r[1] == 'I') { | |
| 2814 | + if (nb_include_paths >= INCLUDE_PATHS_MAX) | |
| 2815 | + error("too many include paths"); | |
| 2816 | + include_paths[nb_include_paths++] = r + 2; | |
| 2817 | + } else { | |
| 2818 | + error("invalid option"); | |
| 2819 | + } | |
| 2820 | + optind++; | |
| 2821 | + } | |
| 2822 | + | |
| 2823 | + filename = argv[optind]; | |
| 2319 | 2824 | line_num = 1; |
| 2320 | 2825 | file = fopen(filename, "r"); |
| 2321 | 2826 | if (!file) { |
| @@ -2325,16 +2830,10 @@ int main(int c, char **v) · 3 unmodified lines | ||
| 2325 | 2830 | include_stack_ptr = include_stack; |
| 2326 | 2831 | ifdef_stack_ptr = ifdef_stack; |
| 2327 | 2832 | |
| 2328 | − idtable = malloc(SYM_TABLE_SIZE); | |
| 2329 | − memcpy(idtable, | |
| 2330 | − "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0float\0double\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0main", 251); | |
| 2331 | − idptr = idtable + 251; | |
| 2332 | − | |
| 2333 | − glo = malloc(DATA_SIZE); | |
| 2833 | + glo = (int)malloc(DATA_SIZE); | |
| 2334 | 2834 | memset((void *)glo, 0, DATA_SIZE); |
| 2335 | − prog = malloc(TEXT_SIZE); | |
| 2835 | + prog = (int)malloc(TEXT_SIZE); | |
| 2336 | 2836 | vstack_ptr = vstack; |
| 2337 | − macro_stack_ptr = macro_stack; | |
| 2338 | 2837 | anon_sym = 1 << (31 - VT_STRUCT_SHIFT); |
| 2339 | 2838 | ind = prog; |
| 2340 | 2839 | inp(); |
| @@ -2356,6 +2855,6 @@ int main(int c, char **v) · 15 unmodified lines | ||
| 2356 | 2855 | if (!s) |
| 2357 | 2856 | error("main() not defined"); |
| 2358 | 2857 | t = s->c; |
| 2359 | − return (*t)(c - 1, v); | |
| 2858 | + return (*t)(argc - optind, argv + optind); | |
| 2360 | 2859 | #endif |
| 2361 | 2860 | } |