History

Identifiers can start and/or contain '.' in *.S

modified version of the old one which don't allow '.' in #define Identifiers. This allow correctly preprocess the following code in *.S #define SRC(y...) \ 9999: y; \ .section __ex_table, "a"; \ .long 9999b, 6001f ; \ // .previous SRC(1: movw (%esi), %bx) 6001: A test included.

seyko committed 10 years ago

Commit d3e85e81 parent 21665f48 files · +115 −73Browse files

Unified · Split

8 files
  1. tcc.h +3 −1
  2. tccasm.c +46 −41
  3. tccpp.c +9 −3
  4. tcctok.h +31 −26
  5. tests/Makefile +5 −1
  6. tests/pp/12.S +8 −0
  7. tests/pp/12.expect +2 −0
  8. tests/pp/Makefile +11 −1
tcc.h+3−1
@@ -957,7 +957,9 @@ struct TCCState { · 956 unmodified lines
957957
958958 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
959959 #define TOK_ASM_int TOK_INT
960−#define TOK_ASM_weak TOK_WEAK1
960+#define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
961+#define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
962+#define TOK_ASMDIR_LAST TOK_ASMDIR_section
961963
962964 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
963965 /* only used for i386 asm opcodes definitions */
tccasm.c+46−41
@@ -331,24 +331,23 @@ static void asm_parse_directive(TCCState *s1) · 330 unmodified lines
331331 uint8_t *ptr;
332332
333333 /* assembler directive */
334− next();
335334 sec = cur_text_section;
336335 switch(tok) {
337− case TOK_ASM_align:
338− case TOK_ASM_p2align:
339− case TOK_ASM_skip:
340− case TOK_ASM_space:
336+ case TOK_ASMDIR_align:
337+ case TOK_ASMDIR_p2align:
338+ case TOK_ASMDIR_skip:
339+ case TOK_ASMDIR_space:
341340 tok1 = tok;
342341 next();
343342 n = asm_int_expr(s1);
344− if (tok1 == TOK_ASM_p2align)
343+ if (tok1 == TOK_ASMDIR_p2align)
345344 {
346345 if (n < 0 || n > 30)
347346 tcc_error("invalid p2align, must be between 0 and 30");
348347 n = 1 << n;
349− tok1 = TOK_ASM_align;
348+ tok1 = TOK_ASMDIR_align;
350349 }
351− if (tok1 == TOK_ASM_align) {
350+ if (tok1 == TOK_ASMDIR_align) {
352351 if (n < 0 || (n & (n-1)) != 0)
353352 tcc_error("alignment must be a positive power of two");
354353 offset = (ind + n - 1) & -n;
@@ -372,7 +371,7 @@ static void asm_parse_directive(TCCState *s1) · 17 unmodified lines
372371 }
373372 ind += size;
374373 break;
375− case TOK_ASM_quad:
374+ case TOK_ASMDIR_quad:
376375 next();
377376 for(;;) {
378377 uint64_t vl;
@@ -399,15 +398,15 @@ static void asm_parse_directive(TCCState *s1) · 20 unmodified lines
399398 next();
400399 }
401400 break;
402− case TOK_ASM_byte:
401+ case TOK_ASMDIR_byte:
403402 size = 1;
404403 goto asm_data;
405− case TOK_ASM_word:
406− case TOK_SHORT:
404+ case TOK_ASMDIR_word:
405+ case TOK_ASMDIR_short:
407406 size = 2;
408407 goto asm_data;
409− case TOK_LONG:
410− case TOK_INT:
408+ case TOK_ASMDIR_long:
409+ case TOK_ASMDIR_int:
411410 size = 4;
412411 asm_data:
413412 next();
@@ -433,7 +432,7 @@ static void asm_parse_directive(TCCState *s1) · 19 unmodified lines
433432 next();
434433 }
435434 break;
436− case TOK_ASM_fill:
435+ case TOK_ASMDIR_fill:
437436 {
438437 int repeat, size, val, i, j;
439438 uint8_t repeat_buf[8];
@@ -475,7 +474,7 @@ static void asm_parse_directive(TCCState *s1) · 35 unmodified lines
475474 }
476475 }
477476 break;
478− case TOK_ASM_org:
477+ case TOK_ASMDIR_org:
479478 {
480479 unsigned long n;
481480 next();
@@ -488,10 +487,10 @@ static void asm_parse_directive(TCCState *s1) · 6 unmodified lines
488487 goto zero_pad;
489488 }
490489 break;
491− case TOK_ASM_globl:
492− case TOK_ASM_global:
493− case TOK_ASM_weak:
494− case TOK_ASM_hidden:
490+ case TOK_ASMDIR_globl:
491+ case TOK_ASMDIR_global:
492+ case TOK_ASMDIR_weak:
493+ case TOK_ASMDIR_hidden:
495494 tok1 = tok;
496495 do {
497496 Sym *sym;
@@ -502,18 +501,18 @@ static void asm_parse_directive(TCCState *s1) · 4 unmodified lines
502501 sym = label_push(&s1->asm_labels, tok, 0);
503502 sym->type.t = VT_VOID;
504503 }
505− if (tok1 != TOK_ASM_hidden)
504+ if (tok1 != TOK_ASMDIR_hidden)
506505 sym->type.t &= ~VT_STATIC;
507− if (tok1 == TOK_ASM_weak)
506+ if (tok1 == TOK_ASMDIR_weak)
508507 sym->type.t |= VT_WEAK;
509− else if (tok1 == TOK_ASM_hidden)
508+ else if (tok1 == TOK_ASMDIR_hidden)
510509 sym->type.t |= STV_HIDDEN << VT_VIS_SHIFT;
511510 next();
512511 } while (tok == ',');
513512 break;
514− case TOK_ASM_string:
515− case TOK_ASM_ascii:
516− case TOK_ASM_asciz:
513+ case TOK_ASMDIR_string:
514+ case TOK_ASMDIR_ascii:
515+ case TOK_ASMDIR_asciz:
517516 {
518517 const uint8_t *p;
519518 int i, size, t;
@@ -525,7 +524,7 @@ static void asm_parse_directive(TCCState *s1) · 5 unmodified lines
525524 expect("string constant");
526525 p = tokc.str.data;
527526 size = tokc.str.size;
528− if (t == TOK_ASM_ascii && size > 0)
527+ if (t == TOK_ASMDIR_ascii && size > 0)
529528 size--;
530529 for(i = 0; i < size; i++)
531530 g(p[i]);
@@ -538,9 +537,9 @@ static void asm_parse_directive(TCCState *s1) · 6 unmodified lines
538537 }
539538 }
540539 break;
541− case TOK_ASM_text:
542− case TOK_ASM_data:
543− case TOK_ASM_bss:
540+ case TOK_ASMDIR_text:
541+ case TOK_ASMDIR_data:
542+ case TOK_ASMDIR_bss:
544543 {
545544 char sname[64];
546545 tok1 = tok;
@@ -557,7 +556,7 @@ static void asm_parse_directive(TCCState *s1) · 10 unmodified lines
557556 use_section(s1, sname);
558557 }
559558 break;
560− case TOK_ASM_file:
559+ case TOK_ASMDIR_file:
561560 {
562561 char filename[512];
563562
@@ -575,7 +574,7 @@ static void asm_parse_directive(TCCState *s1) · 11 unmodified lines
575574 next();
576575 }
577576 break;
578− case TOK_ASM_ident:
577+ case TOK_ASMDIR_ident:
579578 {
580579 char ident[256];
581580
@@ -593,7 +592,7 @@ static void asm_parse_directive(TCCState *s1) · 11 unmodified lines
593592 next();
594593 }
595594 break;
596− case TOK_ASM_size:
595+ case TOK_ASMDIR_size:
597596 {
598597 Sym *sym;
599598
@@ -614,7 +613,7 @@ static void asm_parse_directive(TCCState *s1) · 14 unmodified lines
614613 }
615614 }
616615 break;
617− case TOK_ASM_type:
616+ case TOK_ASMDIR_type:
618617 {
619618 Sym *sym;
620619 const char *newtype;
@@ -632,7 +631,7 @@ static void asm_parse_directive(TCCState *s1) · 11 unmodified lines
632631 newtype = tokc.str.data;
633632 } else {
634633 if (tok == '@' || tok == '%')
635− skip(tok);
634+ next();
636635 newtype = get_tok_str(tok, NULL);
637636 }
638637
@@ -646,7 +645,7 @@ static void asm_parse_directive(TCCState *s1) · 7 unmodified lines
646645 next();
647646 }
648647 break;
649− case TOK_SECTION1:
648+ case TOK_ASMDIR_section:
650649 {
651650 char sname[256];
652651
@@ -666,12 +665,18 @@ static void asm_parse_directive(TCCState *s1) · 13 unmodified lines
666665 if (tok != TOK_STR)
667666 expect("string constant");
668667 next();
668+ if (tok == ',') {
669+ next();
670+ if (tok == '@' || tok == '%')
671+ next();
672+ next();
673+ }
669674 }
670675 last_text_section = cur_text_section;
671676 use_section(s1, sname);
672677 }
673678 break;
674− case TOK_ASM_previous:
679+ case TOK_ASMDIR_previous:
675680 {
676681 Section *sec;
677682 next();
@@ -683,13 +688,13 @@ static void asm_parse_directive(TCCState *s1) · 5 unmodified lines
683688 }
684689 break;
685690 #ifdef TCC_TARGET_I386
686− case TOK_ASM_code16:
691+ case TOK_ASMDIR_code16:
687692 {
688693 next();
689694 s1->seg_size = 16;
690695 }
691696 break;
692− case TOK_ASM_code32:
697+ case TOK_ASMDIR_code32:
693698 {
694699 next();
695700 s1->seg_size = 32;
@@ -698,7 +703,7 @@ static void asm_parse_directive(TCCState *s1) · 2 unmodified lines
698703 #endif
699704 #ifdef TCC_TARGET_X86_64
700705 /* added for compatibility with GAS */
701− case TOK_ASM_code64:
706+ case TOK_ASMDIR_code64:
702707 next();
703708 break;
704709 #endif
@@ -763,7 +768,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess) · 58 unmodified lines
763768 /* horrible gas comment */
764769 while (tok != TOK_LINEFEED)
765770 next();
766− } else if (tok == '.') {
771+ } else if (tok >= TOK_ASMDIR_FIRST && tok <= TOK_ASMDIR_LAST) {
767772 asm_parse_directive(s1);
768773 } else if (tok == TOK_PPNUM) {
769774 const char *p;
tccpp.c+9−3
@@ -1328,6 +1328,8 @@ ST_FUNC void parse_define(void) · 1327 unmodified lines
13281328 parse_flags |= PARSE_FLAG_SPACES;
13291329 next_nomacro_spc();
13301330 if (tok == '(') {
1331+ /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1332+ parse_flags &= ~PARSE_FLAG_ASM_FILE;
13311333 next_nomacro();
13321334 ps = &first;
13331335 if (tok != ')') for (;;) {
@@ -1355,6 +1357,7 @@ ST_FUNC void parse_define(void) · 21 unmodified lines
13551357 }
13561358 next_nomacro_spc();
13571359 t = MACRO_FUNC;
1360+ parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
13581361 }
13591362 tok_str_new(&str);
13601363 spc = 2;
@@ -2460,7 +2463,8 @@ maybe_newline: · 1099 unmodified lines
24602463 p1 = p;
24612464 h = TOK_HASH_INIT;
24622465 h = TOK_HASH_FUNC(h, c);
2463− while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2466+ while (c = *++p, (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2467+ || (c == '.' && (parse_flags & PARSE_FLAG_ASM_FILE)))
24642468 h = TOK_HASH_FUNC(h, c);
24652469 if (c != '\\') {
24662470 TokenSym **pts;
@@ -2492,7 +2496,9 @@ maybe_newline: · 25 unmodified lines
24922496 p--;
24932497 PEEKC(c, p);
24942498 parse_ident_slow:
2495− while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) {
2499+ while ((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2500+ || (c == '.' && (parse_flags & PARSE_FLAG_ASM_FILE)))
2501+ {
24962502 cstr_ccat(&tokcstr, c);
24972503 PEEKC(c, p);
24982504 }
@@ -2551,7 +2557,7 @@ maybe_newline: · 52 unmodified lines
25512557 cstr_reset(&tokcstr);
25522558 cstr_ccat(&tokcstr, '.');
25532559 goto parse_num;
2554− } else if ((isidnum_table['.' - CH_EOF] & IS_ID) != 0) { /* asm mode */
2560+ } else if (parse_flags & PARSE_FLAG_ASM_FILE) {
25552561 *--p = c = '.';
25562562 goto parse_ident_fast;
25572563 } else if (c == '.') {
tcctok.h+31−26
@@ -301,35 +301,40 @@ · 300 unmodified lines
301301 #endif
302302
303303 /* Tiny Assembler */
304− DEF_ASM(byte)
305− DEF_ASM(word)
306− DEF_ASM(align)
307− DEF_ASM(p2align)
308− DEF_ASM(skip)
309− DEF_ASM(space)
310− DEF_ASM(string)
311− DEF_ASM(asciz)
312− DEF_ASM(ascii)
313− DEF_ASM(file)
314− DEF_ASM(globl)
315− DEF_ASM(global)
316− DEF_ASM(hidden)
317− DEF_ASM(ident)
318− DEF_ASM(size)
319− DEF_ASM(type)
320− DEF_ASM(text)
321− DEF_ASM(data)
322− DEF_ASM(bss)
323− DEF_ASM(previous)
324− DEF_ASM(fill)
325− DEF_ASM(org)
326− DEF_ASM(quad)
304+ DEF_ASMDIR(byte) /* must be first directive */
305+ DEF_ASMDIR(word)
306+ DEF_ASMDIR(align)
307+ DEF_ASMDIR(p2align)
308+ DEF_ASMDIR(skip)
309+ DEF_ASMDIR(space)
310+ DEF_ASMDIR(string)
311+ DEF_ASMDIR(asciz)
312+ DEF_ASMDIR(ascii)
313+ DEF_ASMDIR(file)
314+ DEF_ASMDIR(globl)
315+ DEF_ASMDIR(global)
316+ DEF_ASMDIR(weak)
317+ DEF_ASMDIR(hidden)
318+ DEF_ASMDIR(ident)
319+ DEF_ASMDIR(size)
320+ DEF_ASMDIR(type)
321+ DEF_ASMDIR(text)
322+ DEF_ASMDIR(data)
323+ DEF_ASMDIR(bss)
324+ DEF_ASMDIR(previous)
325+ DEF_ASMDIR(fill)
326+ DEF_ASMDIR(org)
327+ DEF_ASMDIR(quad)
327328 #if defined(TCC_TARGET_I386)
328− DEF_ASM(code16)
329− DEF_ASM(code32)
329+ DEF_ASMDIR(code16)
330+ DEF_ASMDIR(code32)
330331 #elif defined(TCC_TARGET_X86_64)
331− DEF_ASM(code64)
332+ DEF_ASMDIR(code64)
332333 #endif
334+ DEF_ASMDIR(short)
335+ DEF_ASMDIR(long)
336+ DEF_ASMDIR(int)
337+ DEF_ASMDIR(section) /* must be last directive */
333338
334339 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
335340 #include "i386-tok.h"
tests/Makefile+5−1
@@ -73,7 +73,11 @@ DISAS = objdump -d · 72 unmodified lines
7373
7474 # libtcc test
7575 ifdef LIBTCC1
76− LIBTCC1:=$(TOP)/$(LIBTCC1)
76+ ifdef CONFIG_WIN32
77+ LIBTCC1:=$(TOP)/win32/libtcc/libtcc.a
78+ else
79+ LIBTCC1:=$(TOP)/$(LIBTCC1)
80+ endif
7781 endif
7882
7983 all test : $(TESTS)
tests/pp/12.S+8−0
@@ -0,0 +1,8 @@
1+#define SRC(y...) \
2+ 9999: y; \
3+ .section __ex_table, "a"; \
4+ .long 9999b, 6001f ; \
5+ // .previous
6+
7+ SRC(1: movw (%esi), %bx)
8+6001:
tests/pp/12.expect+2−0
@@ -0,0 +1,2 @@
1+ 9999: 1: movw (%esi), %bx; .section __ex_table, "a"; .long 9999b, 6001f ;
2+6001:
tests/pp/Makefile+11−1
@@ -7,6 +7,7 @@ include $(TOP)/Makefile · 6 unmodified lines
77
88 TCC = $(TOP)/tcc
99 TESTS = $(patsubst %.c,%.test,$(wildcard *.c))
10+TESTS += $(patsubst %.S,%.test,$(wildcard *.S))
1011
1112 ifdef CONFIG_WIN32
1213 TCC = $(top_srcdir)/win32/tcc
@@ -20,10 +21,19 @@ all test : $(TESTS) · 7 unmodified lines
2021 diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \
2122 && rm -f $*.output
2223
24+%.test: %.S %.expect
25+ @echo PPTest $* ...
26+ @$(TCC) -E -P $< >$*.output 2>&1 ; \
27+ diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \
28+ && rm -f $*.output
29+
2330 # automatically generate .expect files with gcc:
24−%.expect :
31+%.expect: %.c
2532 gcc -E -P $*.c >$*.expect 2>&1
2633
34+%.expect: %.S
35+ gcc -E -P $*.S >$*.expect 2>&1
36+
2737 # tell make not to delete
2838 .PRECIOUS: %.expect
2939

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