History

tccasm: Don't ignore # in preprocessor directives

Our preprocessor throws away # line-comments in asm mode. It did so also inside preprocessor directives, thereby removing stringification. Parse defines in non-asm mode (but retain '.' as identifier character inside macro definitions).

Michael Matz committed 10 years ago

Commit dd57a341 parent e7ef0874 files · +50 −26Browse files

Unified · Split

4 files
  1. tcc.h +6 −0
  2. tccasm.c +2 −0
  3. tccpp.c +24 −21
  4. tests/asmtest.S +18 −5
tcc.h+6−0
@@ -1175,10 +1175,16 @@ ST_DATA TokenSym **table_ident; · 1174 unmodified lines
11751175 #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
11761176 #define PARSE_FLAG_TOK_STR 0x0040 /* return parsed strings instead of TOK_PPSTR */
11771177
1178+/* isidnum_table flags: */
1179+#define IS_SPC 1
1180+#define IS_ID 2
1181+#define IS_NUM 4
1182+
11781183 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
11791184 ST_FUNC const char *get_tok_str(int v, CValue *cv);
11801185 ST_FUNC void begin_macro(TokenString *str, int alloc);
11811186 ST_FUNC void end_macro(void);
1187+ST_FUNC void set_idnum(int c, int val);
11821188 ST_FUNC void save_parse_state(ParseState *s);
11831189 ST_FUNC void restore_parse_state(ParseState *s);
11841190 ST_INLN void tok_str_new(TokenString *s);
tccasm.c+2−0
@@ -916,6 +916,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 915 unmodified lines
916916 ch = file->buf_ptr[0];
917917 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
918918 parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR;
919+ set_idnum('.', IS_ID);
919920 if (do_preprocess)
920921 parse_flags |= PARSE_FLAG_PREPROCESS;
921922 next();
@@ -1036,6 +1037,7 @@ static void tcc_assemble_inline(TCCState *s1, char *str, int len) · 114 unmodified lines
10361037 tcc_close();
10371038
10381039 parse_flags = saved_parse_flags;
1040+ set_idnum('.', (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
10391041 macro_ptr = saved_macro_ptr;
10401042 }
10411043
tccpp.c+24−21
@@ -53,11 +53,6 @@ static struct TinyAlloc *toksym_alloc; · 52 unmodified lines
5353 static struct TinyAlloc *tokstr_alloc;
5454 static struct TinyAlloc *cstr_alloc;
5555
56−/* isidnum_table flags: */
57−#define IS_SPC 1
58−#define IS_ID 2
59−#define IS_NUM 4
60−
6156 static TokenString *macro_stack;
6257
6358 static const char tcc_keywords[] =
@@ -819,6 +814,11 @@ ST_FUNC uint8_t *parse_comment(uint8_t *p) · 755 unmodified lines
819814 return p;
820815 }
821816
817+ST_FUNC void set_idnum(int c, int val)
818+{
819+ isidnum_table[c - CH_EOF] = val;
820+}
821+
822822 #define cinp minp
823823
824824 static inline void skip_spaces(void)
@@ -1453,13 +1453,15 @@ ST_FUNC void parse_define(void) · 628 unmodified lines
14531453 /* XXX: should check if same macro (ANSI) */
14541454 first = NULL;
14551455 t = MACRO_OBJ;
1456+ /* We have to parse the whole define as if not in asm mode, in particular
1457+ no line comment with '#' must be ignored. Also for function
1458+ macros the argument list must be parsed without '.' being an ID
1459+ character. */
1460+ parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES);
14561461 /* '(' must be just after macro definition for MACRO_FUNC */
1457− parse_flags |= PARSE_FLAG_SPACES;
14581462 next_nomacro_spc();
14591463 if (tok == '(') {
1460− /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1461− parse_flags &= ~PARSE_FLAG_ASM_FILE;
1462− isidnum_table['.' - CH_EOF] = 0;
1464+ set_idnum('.', 0);
14631465 next_nomacro();
14641466 ps = &first;
14651467 if (tok != ')') for (;;) {
@@ -1487,14 +1489,17 @@ ST_FUNC void parse_define(void) · 21 unmodified lines
14871489 }
14881490 next_nomacro_spc();
14891491 t = MACRO_FUNC;
1490− parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
1491− isidnum_table['.' - CH_EOF] =
1492− (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
14931492 }
14941493
14951494 tokstr_buf.len = 0;
14961495 spc = 2;
14971496 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1497+ /* The body of a macro definition should be parsed such that identifiers
1498+ are parsed like the file mode determines (i.e. with '.' being an
1499+ ID character in asm mode). But '#' should be retained instead of
1500+ regarded as line comment leader, so still don't set ASM_FILE
1501+ in parse_flags. */
1502+ set_idnum('.', (saved_parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
14981503 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
14991504 /* remove spaces around ## and after '#' */
15001505 if (TOK_TWOSHARPS == tok) {
@@ -2673,7 +2678,7 @@ maybe_newline: · 1172 unmodified lines
26732678 if (isnum(c)) {
26742679 t = '.';
26752680 goto parse_num;
2676− } else if ((parse_flags & PARSE_FLAG_ASM_FILE)
2681+ } else if ((isidnum_table['.' - CH_EOF] & IS_ID)
26772682 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
26782683 *--p = c = '.';
26792684 goto parse_ident_fast;
@@ -3470,10 +3475,8 @@ ST_FUNC void preprocess_start(TCCState *s1) · 790 unmodified lines
34703475 s1->pack_stack[0] = 0;
34713476 s1->pack_stack_ptr = s1->pack_stack;
34723477
3473− isidnum_table['$' - CH_EOF] =
3474− s1->dollars_in_identifiers ? IS_ID : 0;
3475− isidnum_table['.' - CH_EOF] =
3476− (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
3478+ set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
3479+ set_idnum('.', (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
34773480 buf = tcc_malloc(3 + strlen(file->filename));
34783481 sprintf(buf, "\"%s\"", file->filename);
34793482 tcc_undefine_symbol(s1, "__BASE_FILE__");
@@ -3505,14 +3508,14 @@ ST_FUNC void tccpp_new(TCCState *s) · 25 unmodified lines
35053508
35063509 /* init isid table */
35073510 for(i = CH_EOF; i<128; i++)
3508− isidnum_table[i - CH_EOF]
3509− = is_space(i) ? IS_SPC
3511+ set_idnum(i,
3512+ is_space(i) ? IS_SPC
35103513 : isid(i) ? IS_ID
35113514 : isnum(i) ? IS_NUM
3512− : 0;
3515+ : 0);
35133516
35143517 for(i = 128; i<256; i++)
3515− isidnum_table[i - CH_EOF] = IS_ID;
3518+ set_idnum(i, IS_ID);
35163519
35173520 /* init allocators */
35183521 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
tests/asmtest.S+18−5
@@ -16,10 +16,17 @@ · 15 unmodified lines
1616 .skip 3
1717 .skip 15, 0x90
1818 .string "hello\0world"
19−
19+/* Macro expansion should work like with C, the #n shouldn't be parsed
20+ as asm line comment */
21+#define __stringify(n) #n
22+#define stringify(n) __stringify(n)
23+ .skip 8,0x90
24+ .asciz stringify(BLA)
25+ .skip 8,0x90
26+
27+# 28 "asmtest.S" # a line directive (and a line comment)
28+ movl %eax, %ebx # some more asm comment
2029 /* some label tests */
21−
22− movl %eax, %ebx
2330 L1:
2431 movl %eax, %ebx
2532 mov 0x10000, %eax
@@ -572,8 +579,8 @@ int $0x10 · 546 unmodified lines
572579 pusha
573580 popa
574581 #endif
575− clc
576− cld
582+ clc # another comment
583+ cld # a comment with embedded ' tick
577584 cli
578585 clts
579586 cmc
@@ -676,8 +683,10 @@ int $0x10 · 96 unmodified lines
676683 lar %eax,%dx
677684 lar %ax,%edx
678685 lar %eax,%edx
686+#ifdef __x86_64__
679687 lar %ax,%rdx
680688 lar %eax,%rdx
689+#endif
681690 emms
682691 movd %edx, %mm3
683692 movd 0x1000, %mm2
@@ -820,7 +829,11 @@ nop · 136 unmodified lines
820829 .long 145 + 2b
821830 .word 164, 0
822831 .org 2b+32
832+#ifdef __x86_64__
823833 .quad 1b
834+#else
835+ .long 1b
836+#endif
824837 .popsection
825838 3: mov %eax,%ecx
826839 4:

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