History

added linux define - added __FUNCTION - fixed spaces parsing - defined special macros

bellard committed 24 years ago

Commit 41391fb1 parent 5e85ad21 file · +158 −127Browse files

Unified · Split

tcc.c+158−127
@@ -501,15 +501,7 @@ enum { · 500 unmodified lines
501501 TOK_UNDEF,
502502 TOK_ERROR,
503503 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+
513505 #define DEF(id, str) id,
514506 #include "tcctok.h"
515507 #undef DEF
@@ -524,8 +516,6 @@ char *tcc_keywords = · 8 unmodified lines
524516 /* the following are not keywords. They are included to ease parsing */
525517 "define\0include\0ifdef\0ifndef\0elif\0endif\0"
526518 "defined\0undef\0error\0line\0"
527−"__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0"
528−"__func__\0main\0"
529519 /* builtin functions */
530520 #define DEF(id, str) str "\0"
531521 #include "tcctok.h"
@@ -1470,22 +1460,38 @@ static inline void inp(void) · 938 unmodified lines
14701460 // printf("ch1=%c 0x%x\n", ch1, ch1);
14711461 }
14721462
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 */
14741484 static inline void minp(void)
14751485 {
1476− redo:
14771486 ch = ch1;
14781487 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();
14841490 }
14851491
14861492
14871493 /* same as minp, but also skip comments */
1488−void cinp(void)
1494+static void cinp(void)
14891495 {
14901496 int c;
14911497
@@ -1494,14 +1500,13 @@ void cinp(void) · 2 unmodified lines
14941500 if (ch1 == '/') {
14951501 /* single line C++ comments */
14961502 inp();
1497− while (ch1 != '\n' && ch1 != -1)
1503+ while (ch1 != '\n' && ch1 != CH_EOF)
14981504 inp();
1499− inp();
15001505 ch = ' '; /* return space */
15011506 } else if (ch1 == '*') {
15021507 /* C comments */
15031508 inp();
1504− while (ch1 != -1) {
1509+ while (ch1 != CH_EOF) {
15051510 c = ch1;
15061511 inp();
15071512 if (c == '*' && ch1 == '/') {
@@ -1518,9 +1523,15 @@ void cinp(void) · 10 unmodified lines
15181523 }
15191524 }
15201525
1521−void skip_spaces(void)
1526+/* space exlcuding newline */
1527+static inline int is_space(int ch)
15221528 {
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))
15241535 cinp();
15251536 }
15261537
@@ -1532,7 +1543,7 @@ void preprocess_skip(void) · 5 unmodified lines
15321543 a = 0;
15331544 while (1) {
15341545 while (ch != '\n') {
1535− if (ch == -1)
1546+ if (ch == CH_EOF)
15361547 expect("#endif");
15371548 cinp();
15381549 }
@@ -1798,7 +1809,7 @@ void parse_define(void) · 259 unmodified lines
17981809 tok_str_new(&str);
17991810 while (1) {
18001811 skip_spaces();
1801− if (ch == '\n' || ch == -1)
1812+ if (ch == '\n' || ch == CH_EOF)
18021813 break;
18031814 next_nomacro();
18041815 tok_str_add2(&str, tok, &tokc);
@@ -2067,6 +2078,9 @@ static int getq(void) · 262 unmodified lines
20672078 error("invalid escaped char");
20682079 minp();
20692080 }
2081+ } else if (c == '\r' && ch == '\n') {
2082+ minp();
2083+ c = '\n';
20702084 }
20712085 return c;
20722086 }
@@ -2384,15 +2398,14 @@ void next_nomacro1(void) · 311 unmodified lines
23842398 return;
23852399 }
23862400 cinp();
2387− while (ch == ' ' || ch == '\t')
2388− cinp();
2401+ skip_spaces();
23892402 if (ch == '#') {
23902403 /* preprocessor command if # at start of line after
23912404 spaces */
23922405 preprocess();
23932406 }
23942407 }
2395− if (ch != ' ' && ch != '\t' && ch != '\f')
2408+ if (!is_space(ch))
23962409 break;
23972410 cinp();
23982411 }
@@ -2660,113 +2673,119 @@ void macro_subst(TokenString *tok_str, · 261 unmodified lines
26602673 next_nomacro();
26612674 if (tok == 0)
26622675 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) {
26842677 /* if symbol is a macro, prepare substitution */
26852678 /* if nested substitution, do nothing */
26862679 if (sym_find2(*nested_list, tok))
26872680 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;
27022720
27032721 /* 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(",");
27282761 next_nomacro();
27292762 }
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));
27402766 }
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− }
27492767
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;
27592779 }
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);
27612788 }
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);
27702789 } else {
27712790 no_subst:
27722791 /* 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
28802899 vsetc(t, VT_CONST | VT_SYM, &cval);
28812900 }
28822901
2883−/* push a reference to symbol v */
2902+/* push a reference to global symbol v */
28842903 void vpush_sym(int t, int v)
28852904 {
28862905 Sym *sym;
@@ -6524,7 +6543,7 @@ void decl(int l) · 3637 unmodified lines
65246543 /* compile the C file opened in 'file'. Return non zero if errors. */
65256544 static int tcc_compile(TCCState *s)
65266545 {
6527− Sym *define_start;
6546+ Sym *define_start, *sym;
65286547 char buf[512];
65296548 int p, section_sym;
65306549
@@ -6560,7 +6579,8 @@ static int tcc_compile(TCCState *s) · 29 unmodified lines
65606579 char_pointer_type = mk_pointer(VT_BYTE);
65616580 /* define an old type function 'int func()' */
65626581 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;
65646584 func_old_type = VT_FUNC | (p << VT_STRUCT_SHIFT);
65656585
65666586 define_start = define_stack.top;
@@ -6894,10 +6914,21 @@ TCCState *tcc_new(void) · 327 unmodified lines
68946914 p = r;
68956915 }
68966916
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+
68976925 /* standard defines */
68986926 tcc_define_symbol(s, "__STDC__", NULL);
68996927 #if defined(TCC_TARGET_I386)
69006928 tcc_define_symbol(s, "__i386__", NULL);
6929+#endif
6930+#if defined(linux)
6931+ tcc_define_symbol(s, "linux", NULL);
69016932 #endif
69026933 /* tiny C specific defines */
69036934 tcc_define_symbol(s, "__TINYC__", NULL);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t