Unified · Split
| @@ -501,15 +501,7 @@ enum { · 500 unmodified lines | ||
| 501 | 501 | TOK_UNDEF, |
| 502 | 502 | TOK_ERROR, |
| 503 | 503 | TOK_LINE, |
| 504 | − TOK___LINE__, | |
| 505 | − TOK___FILE__, | |
| 506 | − TOK___DATE__, | |
| 507 | − TOK___TIME__, | |
| 508 | − TOK___VA_ARGS__, | |
| 509 | − | |
| 510 | − /* special identifiers */ | |
| 511 | − TOK___FUNC__, | |
| 512 | − TOK_MAIN, | |
| 504 | + | |
| 513 | 505 | #define DEF(id, str) id, |
| 514 | 506 | #include "tcctok.h" |
| 515 | 507 | #undef DEF |
| @@ -524,8 +516,6 @@ char *tcc_keywords = · 8 unmodified lines | ||
| 524 | 516 | /* the following are not keywords. They are included to ease parsing */ |
| 525 | 517 | "define\0include\0ifdef\0ifndef\0elif\0endif\0" |
| 526 | 518 | "defined\0undef\0error\0line\0" |
| 527 | −"__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0" | |
| 528 | −"__func__\0main\0" | |
| 529 | 519 | /* builtin functions */ |
| 530 | 520 | #define DEF(id, str) str "\0" |
| 531 | 521 | #include "tcctok.h" |
| @@ -1470,22 +1460,38 @@ static inline void inp(void) · 938 unmodified lines | ||
| 1470 | 1460 | // printf("ch1=%c 0x%x\n", ch1, ch1); |
| 1471 | 1461 | } |
| 1472 | 1462 | |
| 1473 | −/* input with '\\n' handling */ | |
| 1463 | +/* handle '\\n' and '\\r\n' */ | |
| 1464 | +static void handle_stray(void) | |
| 1465 | +{ | |
| 1466 | + do { | |
| 1467 | + if (ch1 == '\n') { | |
| 1468 | + inp(); | |
| 1469 | + } else if (ch1 == '\r') { | |
| 1470 | + inp(); | |
| 1471 | + if (ch1 != '\n') | |
| 1472 | + error("invalid character after '\\'"); | |
| 1473 | + inp(); | |
| 1474 | + } else { | |
| 1475 | + break; | |
| 1476 | + } | |
| 1477 | + ch = ch1; | |
| 1478 | + inp(); | |
| 1479 | + } while (ch == '\\'); | |
| 1480 | +} | |
| 1481 | + | |
| 1482 | +/* input with '\\n' handling. Also supports '\\r\n' for horrible MSDOS | |
| 1483 | + case */ | |
| 1474 | 1484 | static inline void minp(void) |
| 1475 | 1485 | { |
| 1476 | − redo: | |
| 1477 | 1486 | ch = ch1; |
| 1478 | 1487 | inp(); |
| 1479 | − if (ch == '\\' && ch1 == '\n') { | |
| 1480 | − inp(); | |
| 1481 | − goto redo; | |
| 1482 | − } | |
| 1483 | − //printf("ch=%c 0x%x\n", ch, ch); | |
| 1488 | + if (ch == '\\') | |
| 1489 | + handle_stray(); | |
| 1484 | 1490 | } |
| 1485 | 1491 | |
| 1486 | 1492 | |
| 1487 | 1493 | /* same as minp, but also skip comments */ |
| 1488 | −void cinp(void) | |
| 1494 | +static void cinp(void) | |
| 1489 | 1495 | { |
| 1490 | 1496 | int c; |
| 1491 | 1497 | |
| @@ -1494,14 +1500,13 @@ void cinp(void) · 2 unmodified lines | ||
| 1494 | 1500 | if (ch1 == '/') { |
| 1495 | 1501 | /* single line C++ comments */ |
| 1496 | 1502 | inp(); |
| 1497 | − while (ch1 != '\n' && ch1 != -1) | |
| 1503 | + while (ch1 != '\n' && ch1 != CH_EOF) | |
| 1498 | 1504 | inp(); |
| 1499 | − inp(); | |
| 1500 | 1505 | ch = ' '; /* return space */ |
| 1501 | 1506 | } else if (ch1 == '*') { |
| 1502 | 1507 | /* C comments */ |
| 1503 | 1508 | inp(); |
| 1504 | − while (ch1 != -1) { | |
| 1509 | + while (ch1 != CH_EOF) { | |
| 1505 | 1510 | c = ch1; |
| 1506 | 1511 | inp(); |
| 1507 | 1512 | if (c == '*' && ch1 == '/') { |
| @@ -1518,9 +1523,15 @@ void cinp(void) · 10 unmodified lines | ||
| 1518 | 1523 | } |
| 1519 | 1524 | } |
| 1520 | 1525 | |
| 1521 | −void skip_spaces(void) | |
| 1526 | +/* space exlcuding newline */ | |
| 1527 | +static inline int is_space(int ch) | |
| 1522 | 1528 | { |
| 1523 | − while (ch == ' ' || ch == '\t') | |
| 1529 | + return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r'; | |
| 1530 | +} | |
| 1531 | + | |
| 1532 | +static inline void skip_spaces(void) | |
| 1533 | +{ | |
| 1534 | + while (is_space(ch)) | |
| 1524 | 1535 | cinp(); |
| 1525 | 1536 | } |
| 1526 | 1537 | |
| @@ -1532,7 +1543,7 @@ void preprocess_skip(void) · 5 unmodified lines | ||
| 1532 | 1543 | a = 0; |
| 1533 | 1544 | while (1) { |
| 1534 | 1545 | while (ch != '\n') { |
| 1535 | − if (ch == -1) | |
| 1546 | + if (ch == CH_EOF) | |
| 1536 | 1547 | expect("#endif"); |
| 1537 | 1548 | cinp(); |
| 1538 | 1549 | } |
| @@ -1798,7 +1809,7 @@ void parse_define(void) · 259 unmodified lines | ||
| 1798 | 1809 | tok_str_new(&str); |
| 1799 | 1810 | while (1) { |
| 1800 | 1811 | skip_spaces(); |
| 1801 | − if (ch == '\n' || ch == -1) | |
| 1812 | + if (ch == '\n' || ch == CH_EOF) | |
| 1802 | 1813 | break; |
| 1803 | 1814 | next_nomacro(); |
| 1804 | 1815 | tok_str_add2(&str, tok, &tokc); |
| @@ -2067,6 +2078,9 @@ static int getq(void) · 262 unmodified lines | ||
| 2067 | 2078 | error("invalid escaped char"); |
| 2068 | 2079 | minp(); |
| 2069 | 2080 | } |
| 2081 | + } else if (c == '\r' && ch == '\n') { | |
| 2082 | + minp(); | |
| 2083 | + c = '\n'; | |
| 2070 | 2084 | } |
| 2071 | 2085 | return c; |
| 2072 | 2086 | } |
| @@ -2384,15 +2398,14 @@ void next_nomacro1(void) · 311 unmodified lines | ||
| 2384 | 2398 | return; |
| 2385 | 2399 | } |
| 2386 | 2400 | cinp(); |
| 2387 | − while (ch == ' ' || ch == '\t') | |
| 2388 | − cinp(); | |
| 2401 | + skip_spaces(); | |
| 2389 | 2402 | if (ch == '#') { |
| 2390 | 2403 | /* preprocessor command if # at start of line after |
| 2391 | 2404 | spaces */ |
| 2392 | 2405 | preprocess(); |
| 2393 | 2406 | } |
| 2394 | 2407 | } |
| 2395 | − if (ch != ' ' && ch != '\t' && ch != '\f') | |
| 2408 | + if (!is_space(ch)) | |
| 2396 | 2409 | break; |
| 2397 | 2410 | cinp(); |
| 2398 | 2411 | } |
| @@ -2660,113 +2673,119 @@ void macro_subst(TokenString *tok_str, · 261 unmodified lines | ||
| 2660 | 2673 | next_nomacro(); |
| 2661 | 2674 | if (tok == 0) |
| 2662 | 2675 | break; |
| 2663 | − /* special macros */ | |
| 2664 | − if (tok == TOK___LINE__) { | |
| 2665 | − cval.i = file->line_num; | |
| 2666 | − tok_str_add2(tok_str, TOK_CINT, &cval); | |
| 2667 | − } else if (tok == TOK___FILE__) { | |
| 2668 | − cstrval = file->filename; | |
| 2669 | − goto add_cstr; | |
| 2670 | − tok_str_add2(tok_str, TOK_STR, &cval); | |
| 2671 | − } else if (tok == TOK___DATE__) { | |
| 2672 | − cstrval = "Jan 1 1970"; | |
| 2673 | − goto add_cstr; | |
| 2674 | − } else if (tok == TOK___TIME__) { | |
| 2675 | − cstrval = "00:00:00"; | |
| 2676 | − add_cstr: | |
| 2677 | − cstr_new(&cstr); | |
| 2678 | − cstr_cat(&cstr, cstrval); | |
| 2679 | − cstr_ccat(&cstr, '\0'); | |
| 2680 | − cval.cstr = &cstr; | |
| 2681 | − tok_str_add2(tok_str, TOK_STR, &cval); | |
| 2682 | − cstr_free(&cstr); | |
| 2683 | − } else if ((s = sym_find1(&define_stack, tok)) != NULL) { | |
| 2676 | + if ((s = sym_find1(&define_stack, tok)) != NULL) { | |
| 2684 | 2677 | /* if symbol is a macro, prepare substitution */ |
| 2685 | 2678 | /* if nested substitution, do nothing */ |
| 2686 | 2679 | if (sym_find2(*nested_list, tok)) |
| 2687 | 2680 | goto no_subst; |
| 2688 | − mstr = (int *)s->c; | |
| 2689 | − mstr_allocated = 0; | |
| 2690 | − if (s->t == MACRO_FUNC) { | |
| 2691 | − /* NOTE: we do not use next_nomacro to avoid eating the | |
| 2692 | − next token. XXX: find better solution */ | |
| 2693 | − if (macro_ptr) { | |
| 2694 | − t = *macro_ptr; | |
| 2695 | − } else { | |
| 2696 | − while (ch == ' ' || ch == '\t' || ch == '\n') | |
| 2697 | − cinp(); | |
| 2698 | − t = ch; | |
| 2699 | − } | |
| 2700 | − if (t != '(') /* no macro subst */ | |
| 2701 | − goto no_subst; | |
| 2681 | + | |
| 2682 | + /* special macros */ | |
| 2683 | + if (tok == TOK___LINE__) { | |
| 2684 | + cval.i = file->line_num; | |
| 2685 | + tok_str_add2(tok_str, TOK_CINT, &cval); | |
| 2686 | + } else if (tok == TOK___FILE__) { | |
| 2687 | + cstrval = file->filename; | |
| 2688 | + goto add_cstr; | |
| 2689 | + tok_str_add2(tok_str, TOK_STR, &cval); | |
| 2690 | + } else if (tok == TOK___DATE__) { | |
| 2691 | + cstrval = "Jan 1 2002"; | |
| 2692 | + goto add_cstr; | |
| 2693 | + } else if (tok == TOK___TIME__) { | |
| 2694 | + cstrval = "00:00:00"; | |
| 2695 | + goto add_cstr; | |
| 2696 | + } else if (tok == TOK___FUNCTION__) { | |
| 2697 | + cstrval = funcname; | |
| 2698 | + add_cstr: | |
| 2699 | + cstr_new(&cstr); | |
| 2700 | + cstr_cat(&cstr, cstrval); | |
| 2701 | + cstr_ccat(&cstr, '\0'); | |
| 2702 | + cval.cstr = &cstr; | |
| 2703 | + tok_str_add2(tok_str, TOK_STR, &cval); | |
| 2704 | + cstr_free(&cstr); | |
| 2705 | + } else { | |
| 2706 | + mstr = (int *)s->c; | |
| 2707 | + mstr_allocated = 0; | |
| 2708 | + if (s->t == MACRO_FUNC) { | |
| 2709 | + /* NOTE: we do not use next_nomacro to avoid eating the | |
| 2710 | + next token. XXX: find better solution */ | |
| 2711 | + if (macro_ptr) { | |
| 2712 | + t = *macro_ptr; | |
| 2713 | + } else { | |
| 2714 | + while (is_space(ch) || ch == '\n') | |
| 2715 | + cinp(); | |
| 2716 | + t = ch; | |
| 2717 | + } | |
| 2718 | + if (t != '(') /* no macro subst */ | |
| 2719 | + goto no_subst; | |
| 2702 | 2720 | |
| 2703 | 2721 | /* argument macro */ |
| 2704 | − next_nomacro(); | |
| 2705 | − next_nomacro(); | |
| 2706 | − args = NULL; | |
| 2707 | − sa = s->next; | |
| 2708 | − /* NOTE: empty args are allowed, except if no args */ | |
| 2709 | − for(;;) { | |
| 2710 | − /* handle '()' case */ | |
| 2711 | − if (!args && tok == ')') | |
| 2712 | − break; | |
| 2713 | − if (!sa) | |
| 2714 | − error("macro '%s' used with too many args", | |
| 2715 | − get_tok_str(s->v, 0)); | |
| 2716 | − tok_str_new(&str); | |
| 2717 | − parlevel = 0; | |
| 2718 | − /* NOTE: non zero sa->t indicates VA_ARGS */ | |
| 2719 | − while ((parlevel > 0 || | |
| 2720 | − (tok != ')' && | |
| 2721 | − (tok != ',' || sa->t))) && | |
| 2722 | − tok != -1) { | |
| 2723 | − if (tok == '(') | |
| 2724 | − parlevel++; | |
| 2725 | − else if (tok == ')') | |
| 2726 | − parlevel--; | |
| 2727 | − tok_str_add2(&str, tok, &tokc); | |
| 2722 | + next_nomacro(); | |
| 2723 | + next_nomacro(); | |
| 2724 | + args = NULL; | |
| 2725 | + sa = s->next; | |
| 2726 | + /* NOTE: empty args are allowed, except if no args */ | |
| 2727 | + for(;;) { | |
| 2728 | + /* handle '()' case */ | |
| 2729 | + if (!args && tok == ')') | |
| 2730 | + break; | |
| 2731 | + if (!sa) | |
| 2732 | + error("macro '%s' used with too many args", | |
| 2733 | + get_tok_str(s->v, 0)); | |
| 2734 | + tok_str_new(&str); | |
| 2735 | + parlevel = 0; | |
| 2736 | + /* NOTE: non zero sa->t indicates VA_ARGS */ | |
| 2737 | + while ((parlevel > 0 || | |
| 2738 | + (tok != ')' && | |
| 2739 | + (tok != ',' || sa->t))) && | |
| 2740 | + tok != -1) { | |
| 2741 | + if (tok == '(') | |
| 2742 | + parlevel++; | |
| 2743 | + else if (tok == ')') | |
| 2744 | + parlevel--; | |
| 2745 | + tok_str_add2(&str, tok, &tokc); | |
| 2746 | + next_nomacro(); | |
| 2747 | + } | |
| 2748 | + tok_str_add(&str, 0); | |
| 2749 | + sym_push2(&args, sa->v & ~SYM_FIELD, sa->t, (int)str.str); | |
| 2750 | + sa = sa->next; | |
| 2751 | + if (tok == ')') { | |
| 2752 | + /* special case for gcc var args: add an empty | |
| 2753 | + var arg argument if it is omitted */ | |
| 2754 | + if (sa && sa->t && gnu_ext) | |
| 2755 | + continue; | |
| 2756 | + else | |
| 2757 | + break; | |
| 2758 | + } | |
| 2759 | + if (tok != ',') | |
| 2760 | + expect(","); | |
| 2728 | 2761 | next_nomacro(); |
| 2729 | 2762 | } |
| 2730 | − tok_str_add(&str, 0); | |
| 2731 | − sym_push2(&args, sa->v & ~SYM_FIELD, sa->t, (int)str.str); | |
| 2732 | − sa = sa->next; | |
| 2733 | − if (tok == ')') { | |
| 2734 | − /* special case for gcc var args: add an empty | |
| 2735 | − var arg argument if it is omitted */ | |
| 2736 | − if (sa && sa->t && gnu_ext) | |
| 2737 | − continue; | |
| 2738 | − else | |
| 2739 | − break; | |
| 2763 | + if (sa) { | |
| 2764 | + error("macro '%s' used with too few args", | |
| 2765 | + get_tok_str(s->v, 0)); | |
| 2740 | 2766 | } |
| 2741 | − if (tok != ',') | |
| 2742 | − expect(","); | |
| 2743 | − next_nomacro(); | |
| 2744 | − } | |
| 2745 | − if (sa) { | |
| 2746 | − error("macro '%s' used with too few args", | |
| 2747 | − get_tok_str(s->v, 0)); | |
| 2748 | − } | |
| 2749 | 2767 | |
| 2750 | − /* now subst each arg */ | |
| 2751 | − mstr = macro_arg_subst(nested_list, mstr, args); | |
| 2752 | − /* free memory */ | |
| 2753 | − sa = args; | |
| 2754 | − while (sa) { | |
| 2755 | − sa1 = sa->prev; | |
| 2756 | − tok_str_free((int *)sa->c); | |
| 2757 | − tcc_free(sa); | |
| 2758 | − sa = sa1; | |
| 2768 | + /* now subst each arg */ | |
| 2769 | + mstr = macro_arg_subst(nested_list, mstr, args); | |
| 2770 | + /* free memory */ | |
| 2771 | + sa = args; | |
| 2772 | + while (sa) { | |
| 2773 | + sa1 = sa->prev; | |
| 2774 | + tok_str_free((int *)sa->c); | |
| 2775 | + tcc_free(sa); | |
| 2776 | + sa = sa1; | |
| 2777 | + } | |
| 2778 | + mstr_allocated = 1; | |
| 2759 | 2779 | } |
| 2760 | − mstr_allocated = 1; | |
| 2780 | + sym_push2(nested_list, s->v, 0, 0); | |
| 2781 | + macro_subst(tok_str, nested_list, mstr); | |
| 2782 | + /* pop nested defined symbol */ | |
| 2783 | + sa1 = *nested_list; | |
| 2784 | + *nested_list = sa1->prev; | |
| 2785 | + tcc_free(sa1); | |
| 2786 | + if (mstr_allocated) | |
| 2787 | + tok_str_free(mstr); | |
| 2761 | 2788 | } |
| 2762 | − sym_push2(nested_list, s->v, 0, 0); | |
| 2763 | − macro_subst(tok_str, nested_list, mstr); | |
| 2764 | − /* pop nested defined symbol */ | |
| 2765 | − sa1 = *nested_list; | |
| 2766 | − *nested_list = sa1->prev; | |
| 2767 | − tcc_free(sa1); | |
| 2768 | − if (mstr_allocated) | |
| 2769 | − tok_str_free(mstr); | |
| 2770 | 2789 | } else { |
| 2771 | 2790 | no_subst: |
| 2772 | 2791 | /* no need to add if reading input stream */ |
| @@ -2880,7 +2899,7 @@ void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long size) · 107 unmodified lines | ||
| 2880 | 2899 | vsetc(t, VT_CONST | VT_SYM, &cval); |
| 2881 | 2900 | } |
| 2882 | 2901 | |
| 2883 | −/* push a reference to symbol v */ | |
| 2902 | +/* push a reference to global symbol v */ | |
| 2884 | 2903 | void vpush_sym(int t, int v) |
| 2885 | 2904 | { |
| 2886 | 2905 | Sym *sym; |
| @@ -6524,7 +6543,7 @@ void decl(int l) · 3637 unmodified lines | ||
| 6524 | 6543 | /* compile the C file opened in 'file'. Return non zero if errors. */ |
| 6525 | 6544 | static int tcc_compile(TCCState *s) |
| 6526 | 6545 | { |
| 6527 | − Sym *define_start; | |
| 6546 | + Sym *define_start, *sym; | |
| 6528 | 6547 | char buf[512]; |
| 6529 | 6548 | int p, section_sym; |
| 6530 | 6549 | |
| @@ -6560,7 +6579,8 @@ static int tcc_compile(TCCState *s) · 29 unmodified lines | ||
| 6560 | 6579 | char_pointer_type = mk_pointer(VT_BYTE); |
| 6561 | 6580 | /* define an old type function 'int func()' */ |
| 6562 | 6581 | p = anon_sym++; |
| 6563 | − sym_push1(&global_stack, p, 0, FUNC_OLD); | |
| 6582 | + sym = sym_push1(&global_stack, p, 0, FUNC_OLD); | |
| 6583 | + sym->r = FUNC_CDECL; | |
| 6564 | 6584 | func_old_type = VT_FUNC | (p << VT_STRUCT_SHIFT); |
| 6565 | 6585 | |
| 6566 | 6586 | define_start = define_stack.top; |
| @@ -6894,10 +6914,21 @@ TCCState *tcc_new(void) · 327 unmodified lines | ||
| 6894 | 6914 | p = r; |
| 6895 | 6915 | } |
| 6896 | 6916 | |
| 6917 | + /* we add dummy defines for some special macros to speed up tests | |
| 6918 | + and to have working defined() */ | |
| 6919 | + sym_push1(&define_stack, TOK___LINE__, MACRO_OBJ, 0); | |
| 6920 | + sym_push1(&define_stack, TOK___FILE__, MACRO_OBJ, 0); | |
| 6921 | + sym_push1(&define_stack, TOK___DATE__, MACRO_OBJ, 0); | |
| 6922 | + sym_push1(&define_stack, TOK___TIME__, MACRO_OBJ, 0); | |
| 6923 | + sym_push1(&define_stack, TOK___FUNCTION__, MACRO_OBJ, 0); | |
| 6924 | + | |
| 6897 | 6925 | /* standard defines */ |
| 6898 | 6926 | tcc_define_symbol(s, "__STDC__", NULL); |
| 6899 | 6927 | #if defined(TCC_TARGET_I386) |
| 6900 | 6928 | tcc_define_symbol(s, "__i386__", NULL); |
| 6929 | +#endif | |
| 6930 | +#if defined(linux) | |
| 6931 | + tcc_define_symbol(s, "linux", NULL); | |
| 6901 | 6932 | #endif |
| 6902 | 6933 | /* tiny C specific defines */ |
| 6903 | 6934 | tcc_define_symbol(s, "__TINYC__", NULL); |