History

correct version of "Identifiers can start and/or contain"

A problem was in TOK_ASMDIR_text: - sprintf(sname, ".%s", get_tok_str(tok1, NULL)); + sprintf(sname, "%s", get_tok_str(tok1, NULL)); When tok1 is '.text', then sname is '..text'

seyko committed 10 years ago

Commit 6a49afb1 parent 989b5ee4 files · +94 −72Browse files

Unified · Split

4 files
  1. tcc.h +3 −1
  2. tccasm.c +48 −43
  3. tccpp.c +12 −2
  4. tcctok.h +31 −26
tcc.h+3−1
@@ -974,7 +974,9 @@ struct TCCState { · 973 unmodified lines
974974
975975 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
976976 #define TOK_ASM_int TOK_INT
977−#define TOK_ASM_weak TOK_WEAK1
977+#define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
978+#define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
979+#define TOK_ASMDIR_LAST TOK_ASMDIR_section
978980
979981 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
980982 /* only used for i386 asm opcodes definitions */
tccasm.c+48−43
@@ -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;
@@ -551,13 +550,13 @@ static void asm_parse_directive(TCCState *s1) · 4 unmodified lines
551550 next();
552551 }
553552 if (n)
554− sprintf(sname, ".%s%d", get_tok_str(tok1, NULL), n);
553+ sprintf(sname, "%s%d", get_tok_str(tok1, NULL), n);
555554 else
556− sprintf(sname, ".%s", get_tok_str(tok1, NULL));
555+ sprintf(sname, "%s", get_tok_str(tok1, NULL));
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+12−2
@@ -1328,6 +1328,9 @@ 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;
1333+ isidnum_table['.' - CH_EOF] = 0;
13311334 next_nomacro();
13321335 ps = &first;
13331336 if (tok != ')') for (;;) {
@@ -1355,6 +1358,9 @@ ST_FUNC void parse_define(void) · 21 unmodified lines
13551358 }
13561359 next_nomacro_spc();
13571360 t = MACRO_FUNC;
1361+ parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
1362+ isidnum_table['.' - CH_EOF] =
1363+ (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
13581364 }
13591365 tok_str_new(&str);
13601366 spc = 2;
@@ -2508,7 +2514,8 @@ maybe_newline: · 1147 unmodified lines
25082514 p--;
25092515 PEEKC(c, p);
25102516 parse_ident_slow:
2511− while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) {
2517+ while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2518+ {
25122519 cstr_ccat(&tokcstr, c);
25132520 PEEKC(c, p);
25142521 }
@@ -2567,7 +2574,7 @@ maybe_newline: · 52 unmodified lines
25672574 cstr_reset(&tokcstr);
25682575 cstr_ccat(&tokcstr, '.');
25692576 goto parse_num;
2570− } else if ((isidnum_table['.' - CH_EOF] & IS_ID) != 0) { /* asm mode */
2577+ } else if (parse_flags & PARSE_FLAG_ASM_FILE) {
25712578 *--p = c = '.';
25722579 goto parse_ident_fast;
25732580 } else if (c == '.') {
@@ -3364,6 +3371,9 @@ ST_FUNC void preprocess_init(TCCState *s1) · 790 unmodified lines
33643371
33653372 isidnum_table['$' - CH_EOF] =
33663373 tcc_state->dollars_in_identifiers ? IS_ID : 0;
3374+
3375+ isidnum_table['.' - CH_EOF] =
3376+ (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
33673377 }
33683378
33693379 ST_FUNC void preprocess_new(void)
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"

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