Fixed compilation error in i386-asm.c
[tinycc/kirr.git] / tccpp.c
blobecdaf172e0504e171cf96974653366e2b8849c41
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 static const char tcc_keywords[] =
23 #define DEF(id, str) str "\0"
24 #include "tcctok.h"
25 #undef DEF
28 /* WARNING: the content of this string encodes token numbers */
29 static const unsigned char tok_two_chars[] =
30 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
31 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
33 /* true if isid(c) || isnum(c) */
34 static unsigned char isidnum_table[256-CH_EOF];
37 struct macro_level {
38 struct macro_level *prev;
39 int *p;
42 static void next_nomacro(void);
43 static void next_nomacro_spc(void);
44 static void macro_subst(TokenString *tok_str, Sym **nested_list,
45 const int *macro_str, struct macro_level **can_read_stream);
48 /* allocate a new token */
49 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
51 TokenSym *ts, **ptable;
52 int i;
54 if (tok_ident >= SYM_FIRST_ANOM)
55 error("memory full");
57 /* expand token table if needed */
58 i = tok_ident - TOK_IDENT;
59 if ((i % TOK_ALLOC_INCR) == 0) {
60 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
61 if (!ptable)
62 error("memory full");
63 table_ident = ptable;
66 ts = tcc_malloc(sizeof(TokenSym) + len);
67 table_ident[i] = ts;
68 ts->tok = tok_ident++;
69 ts->sym_define = NULL;
70 ts->sym_label = NULL;
71 ts->sym_struct = NULL;
72 ts->sym_identifier = NULL;
73 ts->len = len;
74 ts->hash_next = NULL;
75 memcpy(ts->str, str, len);
76 ts->str[len] = '\0';
77 *pts = ts;
78 return ts;
81 #define TOK_HASH_INIT 1
82 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
84 /* find a token and add it if not found */
85 static TokenSym *tok_alloc(const char *str, int len)
87 TokenSym *ts, **pts;
88 int i;
89 unsigned int h;
91 h = TOK_HASH_INIT;
92 for(i=0;i<len;i++)
93 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
94 h &= (TOK_HASH_SIZE - 1);
96 pts = &hash_ident[h];
97 for(;;) {
98 ts = *pts;
99 if (!ts)
100 break;
101 if (ts->len == len && !memcmp(ts->str, str, len))
102 return ts;
103 pts = &(ts->hash_next);
105 return tok_alloc_new(pts, str, len);
108 /* XXX: buffer overflow */
109 /* XXX: float tokens */
110 char *get_tok_str(int v, CValue *cv)
112 static char buf[STRING_MAX_SIZE + 1];
113 static CString cstr_buf;
114 CString *cstr;
115 char *p;
116 int i, len;
118 /* NOTE: to go faster, we give a fixed buffer for small strings */
119 cstr_reset(&cstr_buf);
120 cstr_buf.data = buf;
121 cstr_buf.size_allocated = sizeof(buf);
122 p = buf;
124 switch(v) {
125 case TOK_CINT:
126 case TOK_CUINT:
127 /* XXX: not quite exact, but only useful for testing */
128 sprintf(p, "%u", cv->ui);
129 break;
130 case TOK_CLLONG:
131 case TOK_CULLONG:
132 /* XXX: not quite exact, but only useful for testing */
133 #ifdef _WIN32
134 sprintf(p, "%u", (unsigned)cv->ull);
135 #else
136 sprintf(p, "%Lu", cv->ull);
137 #endif
138 break;
139 case TOK_LCHAR:
140 cstr_ccat(&cstr_buf, 'L');
141 case TOK_CCHAR:
142 cstr_ccat(&cstr_buf, '\'');
143 add_char(&cstr_buf, cv->i);
144 cstr_ccat(&cstr_buf, '\'');
145 cstr_ccat(&cstr_buf, '\0');
146 break;
147 case TOK_PPNUM:
148 cstr = cv->cstr;
149 len = cstr->size - 1;
150 for(i=0;i<len;i++)
151 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
152 cstr_ccat(&cstr_buf, '\0');
153 break;
154 case TOK_LSTR:
155 cstr_ccat(&cstr_buf, 'L');
156 case TOK_STR:
157 cstr = cv->cstr;
158 cstr_ccat(&cstr_buf, '\"');
159 if (v == TOK_STR) {
160 len = cstr->size - 1;
161 for(i=0;i<len;i++)
162 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
163 } else {
164 len = (cstr->size / sizeof(nwchar_t)) - 1;
165 for(i=0;i<len;i++)
166 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
168 cstr_ccat(&cstr_buf, '\"');
169 cstr_ccat(&cstr_buf, '\0');
170 break;
171 case TOK_LT:
172 v = '<';
173 goto addv;
174 case TOK_GT:
175 v = '>';
176 goto addv;
177 case TOK_DOTS:
178 return strcpy(p, "...");
179 case TOK_A_SHL:
180 return strcpy(p, "<<=");
181 case TOK_A_SAR:
182 return strcpy(p, ">>=");
183 default:
184 if (v < TOK_IDENT) {
185 /* search in two bytes table */
186 const unsigned char *q = tok_two_chars;
187 while (*q) {
188 if (q[2] == v) {
189 *p++ = q[0];
190 *p++ = q[1];
191 *p = '\0';
192 return buf;
194 q += 3;
196 addv:
197 *p++ = v;
198 *p = '\0';
199 } else if (v < tok_ident) {
200 return table_ident[v - TOK_IDENT]->str;
201 } else if (v >= SYM_FIRST_ANOM) {
202 /* special name for anonymous symbol */
203 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
204 } else {
205 /* should never happen */
206 return NULL;
208 break;
210 return cstr_buf.data;
213 /* fill input buffer and peek next char */
214 static int tcc_peekc_slow(BufferedFile *bf)
216 int len;
217 /* only tries to read if really end of buffer */
218 if (bf->buf_ptr >= bf->buf_end) {
219 if (bf->fd != -1) {
220 #if defined(PARSE_DEBUG)
221 len = 8;
222 #else
223 len = IO_BUF_SIZE;
224 #endif
225 len = read(bf->fd, bf->buffer, len);
226 if (len < 0)
227 len = 0;
228 } else {
229 len = 0;
231 total_bytes += len;
232 bf->buf_ptr = bf->buffer;
233 bf->buf_end = bf->buffer + len;
234 *bf->buf_end = CH_EOB;
236 if (bf->buf_ptr < bf->buf_end) {
237 return bf->buf_ptr[0];
238 } else {
239 bf->buf_ptr = bf->buf_end;
240 return CH_EOF;
244 /* return the current character, handling end of block if necessary
245 (but not stray) */
246 static int handle_eob(void)
248 return tcc_peekc_slow(file);
251 /* read next char from current input file and handle end of input buffer */
252 static inline void inp(void)
254 ch = *(++(file->buf_ptr));
255 /* end of buffer/file handling */
256 if (ch == CH_EOB)
257 ch = handle_eob();
260 /* handle '\[\r]\n' */
261 static int handle_stray_noerror(void)
263 while (ch == '\\') {
264 inp();
265 if (ch == '\n') {
266 file->line_num++;
267 inp();
268 } else if (ch == '\r') {
269 inp();
270 if (ch != '\n')
271 goto fail;
272 file->line_num++;
273 inp();
274 } else {
275 fail:
276 return 1;
279 return 0;
282 static void handle_stray(void)
284 if (handle_stray_noerror())
285 error("stray '\\' in program");
288 /* skip the stray and handle the \\n case. Output an error if
289 incorrect char after the stray */
290 static int handle_stray1(uint8_t *p)
292 int c;
294 if (p >= file->buf_end) {
295 file->buf_ptr = p;
296 c = handle_eob();
297 p = file->buf_ptr;
298 if (c == '\\')
299 goto parse_stray;
300 } else {
301 parse_stray:
302 file->buf_ptr = p;
303 ch = *p;
304 handle_stray();
305 p = file->buf_ptr;
306 c = *p;
308 return c;
311 /* handle just the EOB case, but not stray */
312 #define PEEKC_EOB(c, p)\
314 p++;\
315 c = *p;\
316 if (c == '\\') {\
317 file->buf_ptr = p;\
318 c = handle_eob();\
319 p = file->buf_ptr;\
323 /* handle the complicated stray case */
324 #define PEEKC(c, p)\
326 p++;\
327 c = *p;\
328 if (c == '\\') {\
329 c = handle_stray1(p);\
330 p = file->buf_ptr;\
334 /* input with '\[\r]\n' handling. Note that this function cannot
335 handle other characters after '\', so you cannot call it inside
336 strings or comments */
337 static void minp(void)
339 inp();
340 if (ch == '\\')
341 handle_stray();
345 /* single line C++ comments */
346 static uint8_t *parse_line_comment(uint8_t *p)
348 int c;
350 p++;
351 for(;;) {
352 c = *p;
353 redo:
354 if (c == '\n' || c == CH_EOF) {
355 break;
356 } else if (c == '\\') {
357 file->buf_ptr = p;
358 c = handle_eob();
359 p = file->buf_ptr;
360 if (c == '\\') {
361 PEEKC_EOB(c, p);
362 if (c == '\n') {
363 file->line_num++;
364 PEEKC_EOB(c, p);
365 } else if (c == '\r') {
366 PEEKC_EOB(c, p);
367 if (c == '\n') {
368 file->line_num++;
369 PEEKC_EOB(c, p);
372 } else {
373 goto redo;
375 } else {
376 p++;
379 return p;
382 /* C comments */
383 static uint8_t *parse_comment(uint8_t *p)
385 int c;
387 p++;
388 for(;;) {
389 /* fast skip loop */
390 for(;;) {
391 c = *p;
392 if (c == '\n' || c == '*' || c == '\\')
393 break;
394 p++;
395 c = *p;
396 if (c == '\n' || c == '*' || c == '\\')
397 break;
398 p++;
400 /* now we can handle all the cases */
401 if (c == '\n') {
402 file->line_num++;
403 p++;
404 } else if (c == '*') {
405 p++;
406 for(;;) {
407 c = *p;
408 if (c == '*') {
409 p++;
410 } else if (c == '/') {
411 goto end_of_comment;
412 } else if (c == '\\') {
413 file->buf_ptr = p;
414 c = handle_eob();
415 p = file->buf_ptr;
416 if (c == '\\') {
417 /* skip '\[\r]\n', otherwise just skip the stray */
418 while (c == '\\') {
419 PEEKC_EOB(c, p);
420 if (c == '\n') {
421 file->line_num++;
422 PEEKC_EOB(c, p);
423 } else if (c == '\r') {
424 PEEKC_EOB(c, p);
425 if (c == '\n') {
426 file->line_num++;
427 PEEKC_EOB(c, p);
429 } else {
430 goto after_star;
434 } else {
435 break;
438 after_star: ;
439 } else {
440 /* stray, eob or eof */
441 file->buf_ptr = p;
442 c = handle_eob();
443 p = file->buf_ptr;
444 if (c == CH_EOF) {
445 error("unexpected end of file in comment");
446 } else if (c == '\\') {
447 p++;
451 end_of_comment:
452 p++;
453 return p;
456 #define cinp minp
458 static inline void skip_spaces(void)
460 while (is_space(ch))
461 cinp();
464 static inline int check_space(int t, int *spc)
466 if (is_space(t)) {
467 if (*spc)
468 return 1;
469 *spc = 1;
470 } else
471 *spc = 0;
472 return 0;
475 /* parse a string without interpreting escapes */
476 static uint8_t *parse_pp_string(uint8_t *p,
477 int sep, CString *str)
479 int c;
480 p++;
481 for(;;) {
482 c = *p;
483 if (c == sep) {
484 break;
485 } else if (c == '\\') {
486 file->buf_ptr = p;
487 c = handle_eob();
488 p = file->buf_ptr;
489 if (c == CH_EOF) {
490 unterminated_string:
491 /* XXX: indicate line number of start of string */
492 error("missing terminating %c character", sep);
493 } else if (c == '\\') {
494 /* escape : just skip \[\r]\n */
495 PEEKC_EOB(c, p);
496 if (c == '\n') {
497 file->line_num++;
498 p++;
499 } else if (c == '\r') {
500 PEEKC_EOB(c, p);
501 if (c != '\n')
502 expect("'\n' after '\r'");
503 file->line_num++;
504 p++;
505 } else if (c == CH_EOF) {
506 goto unterminated_string;
507 } else {
508 if (str) {
509 cstr_ccat(str, '\\');
510 cstr_ccat(str, c);
512 p++;
515 } else if (c == '\n') {
516 file->line_num++;
517 goto add_char;
518 } else if (c == '\r') {
519 PEEKC_EOB(c, p);
520 if (c != '\n') {
521 if (str)
522 cstr_ccat(str, '\r');
523 } else {
524 file->line_num++;
525 goto add_char;
527 } else {
528 add_char:
529 if (str)
530 cstr_ccat(str, c);
531 p++;
534 p++;
535 return p;
538 /* skip block of text until #else, #elif or #endif. skip also pairs of
539 #if/#endif */
540 void preprocess_skip(void)
542 int a, start_of_line, c, in_warn_or_error;
543 uint8_t *p;
545 p = file->buf_ptr;
546 a = 0;
547 redo_start:
548 start_of_line = 1;
549 in_warn_or_error = 0;
550 for(;;) {
551 redo_no_start:
552 c = *p;
553 switch(c) {
554 case ' ':
555 case '\t':
556 case '\f':
557 case '\v':
558 case '\r':
559 p++;
560 goto redo_no_start;
561 case '\n':
562 file->line_num++;
563 p++;
564 goto redo_start;
565 case '\\':
566 file->buf_ptr = p;
567 c = handle_eob();
568 if (c == CH_EOF) {
569 expect("#endif");
570 } else if (c == '\\') {
571 ch = file->buf_ptr[0];
572 handle_stray_noerror();
574 p = file->buf_ptr;
575 goto redo_no_start;
576 /* skip strings */
577 case '\"':
578 case '\'':
579 if (in_warn_or_error)
580 goto _default;
581 p = parse_pp_string(p, c, NULL);
582 break;
583 /* skip comments */
584 case '/':
585 if (in_warn_or_error)
586 goto _default;
587 file->buf_ptr = p;
588 ch = *p;
589 minp();
590 p = file->buf_ptr;
591 if (ch == '*') {
592 p = parse_comment(p);
593 } else if (ch == '/') {
594 p = parse_line_comment(p);
596 break;
597 case '#':
598 p++;
599 if (start_of_line) {
600 file->buf_ptr = p;
601 next_nomacro();
602 p = file->buf_ptr;
603 if (a == 0 &&
604 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
605 goto the_end;
606 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
607 a++;
608 else if (tok == TOK_ENDIF)
609 a--;
610 else if( tok == TOK_ERROR || tok == TOK_WARNING)
611 in_warn_or_error = 1;
613 break;
614 _default:
615 default:
616 p++;
617 break;
619 start_of_line = 0;
621 the_end: ;
622 file->buf_ptr = p;
625 /* ParseState handling */
627 /* XXX: currently, no include file info is stored. Thus, we cannot display
628 accurate messages if the function or data definition spans multiple
629 files */
631 /* save current parse state in 's' */
632 void save_parse_state(ParseState *s)
634 s->line_num = file->line_num;
635 s->macro_ptr = macro_ptr;
636 s->tok = tok;
637 s->tokc = tokc;
640 /* restore parse state from 's' */
641 void restore_parse_state(ParseState *s)
643 file->line_num = s->line_num;
644 macro_ptr = s->macro_ptr;
645 tok = s->tok;
646 tokc = s->tokc;
649 /* return the number of additional 'ints' necessary to store the
650 token */
651 static inline int tok_ext_size(int t)
653 switch(t) {
654 /* 4 bytes */
655 case TOK_CINT:
656 case TOK_CUINT:
657 case TOK_CCHAR:
658 case TOK_LCHAR:
659 case TOK_CFLOAT:
660 case TOK_LINENUM:
661 return 1;
662 case TOK_STR:
663 case TOK_LSTR:
664 case TOK_PPNUM:
665 error("unsupported token");
666 return 1;
667 case TOK_CDOUBLE:
668 case TOK_CLLONG:
669 case TOK_CULLONG:
670 return 2;
671 case TOK_CLDOUBLE:
672 return LDOUBLE_SIZE / 4;
673 default:
674 return 0;
678 /* token string handling */
680 static inline void tok_str_new(TokenString *s)
682 s->str = NULL;
683 s->len = 0;
684 s->allocated_len = 0;
685 s->last_line_num = -1;
688 static void tok_str_free(int *str)
690 tcc_free(str);
693 static int *tok_str_realloc(TokenString *s)
695 int *str, len;
697 if (s->allocated_len == 0) {
698 len = 8;
699 } else {
700 len = s->allocated_len * 2;
702 str = tcc_realloc(s->str, len * sizeof(int));
703 if (!str)
704 error("memory full");
705 s->allocated_len = len;
706 s->str = str;
707 return str;
710 static void tok_str_add(TokenString *s, int t)
712 int len, *str;
714 len = s->len;
715 str = s->str;
716 if (len >= s->allocated_len)
717 str = tok_str_realloc(s);
718 str[len++] = t;
719 s->len = len;
722 static void tok_str_add2(TokenString *s, int t, CValue *cv)
724 int len, *str;
726 len = s->len;
727 str = s->str;
729 /* allocate space for worst case */
730 if (len + TOK_MAX_SIZE > s->allocated_len)
731 str = tok_str_realloc(s);
732 str[len++] = t;
733 switch(t) {
734 case TOK_CINT:
735 case TOK_CUINT:
736 case TOK_CCHAR:
737 case TOK_LCHAR:
738 case TOK_CFLOAT:
739 case TOK_LINENUM:
740 str[len++] = cv->tab[0];
741 break;
742 case TOK_PPNUM:
743 case TOK_STR:
744 case TOK_LSTR:
746 int nb_words;
747 CString *cstr;
749 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
750 while ((len + nb_words) > s->allocated_len)
751 str = tok_str_realloc(s);
752 cstr = (CString *)(str + len);
753 cstr->data = NULL;
754 cstr->size = cv->cstr->size;
755 cstr->data_allocated = NULL;
756 cstr->size_allocated = cstr->size;
757 memcpy((char *)cstr + sizeof(CString),
758 cv->cstr->data, cstr->size);
759 len += nb_words;
761 break;
762 case TOK_CDOUBLE:
763 case TOK_CLLONG:
764 case TOK_CULLONG:
765 #if LDOUBLE_SIZE == 8
766 case TOK_CLDOUBLE:
767 #endif
768 str[len++] = cv->tab[0];
769 str[len++] = cv->tab[1];
770 break;
771 #if LDOUBLE_SIZE == 12
772 case TOK_CLDOUBLE:
773 str[len++] = cv->tab[0];
774 str[len++] = cv->tab[1];
775 str[len++] = cv->tab[2];
776 #elif LDOUBLE_SIZE == 16
777 case TOK_CLDOUBLE:
778 str[len++] = cv->tab[0];
779 str[len++] = cv->tab[1];
780 str[len++] = cv->tab[2];
781 str[len++] = cv->tab[3];
782 #elif LDOUBLE_SIZE != 8
783 #error add long double size support
784 #endif
785 break;
786 default:
787 break;
789 s->len = len;
792 /* add the current parse token in token string 's' */
793 static void tok_str_add_tok(TokenString *s)
795 CValue cval;
797 /* save line number info */
798 if (file->line_num != s->last_line_num) {
799 s->last_line_num = file->line_num;
800 cval.i = s->last_line_num;
801 tok_str_add2(s, TOK_LINENUM, &cval);
803 tok_str_add2(s, tok, &tokc);
806 #if LDOUBLE_SIZE == 16
807 #define LDOUBLE_GET(p, cv) \
808 cv.tab[0] = p[0]; \
809 cv.tab[1] = p[1]; \
810 cv.tab[2] = p[2]; \
811 cv.tab[3] = p[3];
812 #elif LDOUBLE_SIZE == 12
813 #define LDOUBLE_GET(p, cv) \
814 cv.tab[0] = p[0]; \
815 cv.tab[1] = p[1]; \
816 cv.tab[2] = p[2];
817 #elif LDOUBLE_SIZE == 8
818 #define LDOUBLE_GET(p, cv) \
819 cv.tab[0] = p[0]; \
820 cv.tab[1] = p[1];
821 #else
822 #error add long double size support
823 #endif
826 /* get a token from an integer array and increment pointer
827 accordingly. we code it as a macro to avoid pointer aliasing. */
828 #define TOK_GET(t, p, cv) \
830 t = *p++; \
831 switch(t) { \
832 case TOK_CINT: \
833 case TOK_CUINT: \
834 case TOK_CCHAR: \
835 case TOK_LCHAR: \
836 case TOK_CFLOAT: \
837 case TOK_LINENUM: \
838 cv.tab[0] = *p++; \
839 break; \
840 case TOK_STR: \
841 case TOK_LSTR: \
842 case TOK_PPNUM: \
843 cv.cstr = (CString *)p; \
844 cv.cstr->data = (char *)p + sizeof(CString);\
845 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
846 break; \
847 case TOK_CDOUBLE: \
848 case TOK_CLLONG: \
849 case TOK_CULLONG: \
850 cv.tab[0] = p[0]; \
851 cv.tab[1] = p[1]; \
852 p += 2; \
853 break; \
854 case TOK_CLDOUBLE: \
855 LDOUBLE_GET(p, cv); \
856 p += LDOUBLE_SIZE / 4; \
857 break; \
858 default: \
859 break; \
863 /* defines handling */
864 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
866 Sym *s;
868 s = sym_push2(&define_stack, v, macro_type, 0);
869 s->d = str;
870 s->next = first_arg;
871 table_ident[v - TOK_IDENT]->sym_define = s;
874 /* undefined a define symbol. Its name is just set to zero */
875 static void define_undef(Sym *s)
877 int v;
878 v = s->v;
879 if (v >= TOK_IDENT && v < tok_ident)
880 table_ident[v - TOK_IDENT]->sym_define = NULL;
881 s->v = 0;
884 static inline Sym *define_find(int v)
886 v -= TOK_IDENT;
887 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
888 return NULL;
889 return table_ident[v]->sym_define;
892 /* free define stack until top reaches 'b' */
893 static void free_defines(Sym *b)
895 Sym *top, *top1;
896 int v;
898 top = define_stack;
899 while (top != b) {
900 top1 = top->prev;
901 /* do not free args or predefined defines */
902 if (top->d)
903 tok_str_free(top->d);
904 v = top->v;
905 if (v >= TOK_IDENT && v < tok_ident)
906 table_ident[v - TOK_IDENT]->sym_define = NULL;
907 sym_free(top);
908 top = top1;
910 define_stack = b;
913 /* label lookup */
914 static Sym *label_find(int v)
916 v -= TOK_IDENT;
917 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
918 return NULL;
919 return table_ident[v]->sym_label;
922 static Sym *label_push(Sym **ptop, int v, int flags)
924 Sym *s, **ps;
925 s = sym_push2(ptop, v, 0, 0);
926 s->r = flags;
927 ps = &table_ident[v - TOK_IDENT]->sym_label;
928 if (ptop == &global_label_stack) {
929 /* modify the top most local identifier, so that
930 sym_identifier will point to 's' when popped */
931 while (*ps != NULL)
932 ps = &(*ps)->prev_tok;
934 s->prev_tok = *ps;
935 *ps = s;
936 return s;
939 /* pop labels until element last is reached. Look if any labels are
940 undefined. Define symbols if '&&label' was used. */
941 static void label_pop(Sym **ptop, Sym *slast)
943 Sym *s, *s1;
944 for(s = *ptop; s != slast; s = s1) {
945 s1 = s->prev;
946 if (s->r == LABEL_DECLARED) {
947 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
948 } else if (s->r == LABEL_FORWARD) {
949 error("label '%s' used but not defined",
950 get_tok_str(s->v, NULL));
951 } else {
952 if (s->c) {
953 /* define corresponding symbol. A size of
954 1 is put. */
955 put_extern_sym(s, cur_text_section, s->jnext, 1);
958 /* remove label */
959 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
960 sym_free(s);
962 *ptop = slast;
965 /* eval an expression for #if/#elif */
966 static int expr_preprocess(void)
968 int c, t;
969 TokenString str;
971 tok_str_new(&str);
972 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
973 next(); /* do macro subst */
974 if (tok == TOK_DEFINED) {
975 next_nomacro();
976 t = tok;
977 if (t == '(')
978 next_nomacro();
979 c = define_find(tok) != 0;
980 if (t == '(')
981 next_nomacro();
982 tok = TOK_CINT;
983 tokc.i = c;
984 } else if (tok >= TOK_IDENT) {
985 /* if undefined macro */
986 tok = TOK_CINT;
987 tokc.i = 0;
989 tok_str_add_tok(&str);
991 tok_str_add(&str, -1); /* simulate end of file */
992 tok_str_add(&str, 0);
993 /* now evaluate C constant expression */
994 macro_ptr = str.str;
995 next();
996 c = expr_const();
997 macro_ptr = NULL;
998 tok_str_free(str.str);
999 return c != 0;
1002 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1003 static void tok_print(int *str)
1005 int t;
1006 CValue cval;
1008 printf("<");
1009 while (1) {
1010 TOK_GET(t, str, cval);
1011 if (!t)
1012 break;
1013 printf("%s", get_tok_str(t, &cval));
1015 printf(">\n");
1017 #endif
1019 /* parse after #define */
1020 static void parse_define(void)
1022 Sym *s, *first, **ps;
1023 int v, t, varg, is_vaargs, spc;
1024 TokenString str;
1026 v = tok;
1027 if (v < TOK_IDENT)
1028 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1029 /* XXX: should check if same macro (ANSI) */
1030 first = NULL;
1031 t = MACRO_OBJ;
1032 /* '(' must be just after macro definition for MACRO_FUNC */
1033 next_nomacro_spc();
1034 if (tok == '(') {
1035 next_nomacro();
1036 ps = &first;
1037 while (tok != ')') {
1038 varg = tok;
1039 next_nomacro();
1040 is_vaargs = 0;
1041 if (varg == TOK_DOTS) {
1042 varg = TOK___VA_ARGS__;
1043 is_vaargs = 1;
1044 } else if (tok == TOK_DOTS && gnu_ext) {
1045 is_vaargs = 1;
1046 next_nomacro();
1048 if (varg < TOK_IDENT)
1049 error("badly punctuated parameter list");
1050 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1051 *ps = s;
1052 ps = &s->next;
1053 if (tok != ',')
1054 break;
1055 next_nomacro();
1057 if (tok == ')')
1058 next_nomacro_spc();
1059 t = MACRO_FUNC;
1061 tok_str_new(&str);
1062 spc = 2;
1063 /* EOF testing necessary for '-D' handling */
1064 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1065 /* remove spaces around ## and after '#' */
1066 if (TOK_TWOSHARPS == tok) {
1067 if (1 == spc)
1068 --str.len;
1069 spc = 2;
1070 } else if ('#' == tok) {
1071 spc = 2;
1072 } else if (check_space(tok, &spc)) {
1073 goto skip;
1075 tok_str_add2(&str, tok, &tokc);
1076 skip:
1077 next_nomacro_spc();
1079 if (spc == 1)
1080 --str.len; /* remove trailing space */
1081 tok_str_add(&str, 0);
1082 #ifdef PP_DEBUG
1083 printf("define %s %d: ", get_tok_str(v, NULL), t);
1084 tok_print(str.str);
1085 #endif
1086 define_push(v, t, str.str, first);
1089 static inline int hash_cached_include(int type, const char *filename)
1091 const unsigned char *s;
1092 unsigned int h;
1094 h = TOK_HASH_INIT;
1095 h = TOK_HASH_FUNC(h, type);
1096 s = filename;
1097 while (*s) {
1098 h = TOK_HASH_FUNC(h, *s);
1099 s++;
1101 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1102 return h;
1105 /* XXX: use a token or a hash table to accelerate matching ? */
1106 static CachedInclude *search_cached_include(TCCState *s1,
1107 int type, const char *filename)
1109 CachedInclude *e;
1110 int i, h;
1111 h = hash_cached_include(type, filename);
1112 i = s1->cached_includes_hash[h];
1113 for(;;) {
1114 if (i == 0)
1115 break;
1116 e = s1->cached_includes[i - 1];
1117 if (e->type == type && !PATHCMP(e->filename, filename))
1118 return e;
1119 i = e->hash_next;
1121 return NULL;
1124 static inline void add_cached_include(TCCState *s1, int type,
1125 const char *filename, int ifndef_macro)
1127 CachedInclude *e;
1128 int h;
1130 if (search_cached_include(s1, type, filename))
1131 return;
1132 #ifdef INC_DEBUG
1133 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1134 #endif
1135 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1136 if (!e)
1137 return;
1138 e->type = type;
1139 strcpy(e->filename, filename);
1140 e->ifndef_macro = ifndef_macro;
1141 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1142 /* add in hash table */
1143 h = hash_cached_include(type, filename);
1144 e->hash_next = s1->cached_includes_hash[h];
1145 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1148 static void pragma_parse(TCCState *s1)
1150 int val;
1152 next();
1153 if (tok == TOK_pack) {
1155 This may be:
1156 #pragma pack(1) // set
1157 #pragma pack() // reset to default
1158 #pragma pack(push,1) // push & set
1159 #pragma pack(pop) // restore previous
1161 next();
1162 skip('(');
1163 if (tok == TOK_ASM_pop) {
1164 next();
1165 if (s1->pack_stack_ptr <= s1->pack_stack) {
1166 stk_error:
1167 error("out of pack stack");
1169 s1->pack_stack_ptr--;
1170 } else {
1171 val = 0;
1172 if (tok != ')') {
1173 if (tok == TOK_ASM_push) {
1174 next();
1175 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1176 goto stk_error;
1177 s1->pack_stack_ptr++;
1178 skip(',');
1180 if (tok != TOK_CINT) {
1181 pack_error:
1182 error("invalid pack pragma");
1184 val = tokc.i;
1185 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1186 goto pack_error;
1187 next();
1189 *s1->pack_stack_ptr = val;
1190 skip(')');
1195 /* is_bof is true if first non space token at beginning of file */
1196 static void preprocess(int is_bof)
1198 TCCState *s1 = tcc_state;
1199 int i, c, n, saved_parse_flags;
1200 char buf[1024], *q;
1201 Sym *s;
1203 saved_parse_flags = parse_flags;
1204 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1205 PARSE_FLAG_LINEFEED;
1206 next_nomacro();
1207 redo:
1208 switch(tok) {
1209 case TOK_DEFINE:
1210 next_nomacro();
1211 parse_define();
1212 break;
1213 case TOK_UNDEF:
1214 next_nomacro();
1215 s = define_find(tok);
1216 /* undefine symbol by putting an invalid name */
1217 if (s)
1218 define_undef(s);
1219 break;
1220 case TOK_INCLUDE:
1221 case TOK_INCLUDE_NEXT:
1222 ch = file->buf_ptr[0];
1223 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1224 skip_spaces();
1225 if (ch == '<') {
1226 c = '>';
1227 goto read_name;
1228 } else if (ch == '\"') {
1229 c = ch;
1230 read_name:
1231 inp();
1232 q = buf;
1233 while (ch != c && ch != '\n' && ch != CH_EOF) {
1234 if ((q - buf) < sizeof(buf) - 1)
1235 *q++ = ch;
1236 if (ch == '\\') {
1237 if (handle_stray_noerror() == 0)
1238 --q;
1239 } else
1240 inp();
1242 *q = '\0';
1243 minp();
1244 #if 0
1245 /* eat all spaces and comments after include */
1246 /* XXX: slightly incorrect */
1247 while (ch1 != '\n' && ch1 != CH_EOF)
1248 inp();
1249 #endif
1250 } else {
1251 /* computed #include : either we have only strings or
1252 we have anything enclosed in '<>' */
1253 next();
1254 buf[0] = '\0';
1255 if (tok == TOK_STR) {
1256 while (tok != TOK_LINEFEED) {
1257 if (tok != TOK_STR) {
1258 include_syntax:
1259 error("'#include' expects \"FILENAME\" or <FILENAME>");
1261 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1262 next();
1264 c = '\"';
1265 } else {
1266 int len;
1267 while (tok != TOK_LINEFEED) {
1268 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1269 next();
1271 len = strlen(buf);
1272 /* check syntax and remove '<>' */
1273 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1274 goto include_syntax;
1275 memmove(buf, buf + 1, len - 2);
1276 buf[len - 2] = '\0';
1277 c = '>';
1281 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1282 error("#include recursion too deep");
1284 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1285 for (i = -2; i < n; ++i) {
1286 char buf1[sizeof file->filename];
1287 BufferedFile *f;
1288 CachedInclude *e;
1289 const char *path;
1290 int size;
1292 if (i == -2) {
1293 /* check absolute include path */
1294 if (!IS_ABSPATH(buf))
1295 continue;
1296 buf1[0] = 0;
1298 } else if (i == -1) {
1299 /* search in current dir if "header.h" */
1300 if (c != '\"')
1301 continue;
1302 size = tcc_basename(file->filename) - file->filename;
1303 memcpy(buf1, file->filename, size);
1304 buf1[size] = '\0';
1306 } else {
1307 /* search in all the include paths */
1308 if (i < s1->nb_include_paths)
1309 path = s1->include_paths[i];
1310 else
1311 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1312 pstrcpy(buf1, sizeof(buf1), path);
1313 pstrcat(buf1, sizeof(buf1), "/");
1316 pstrcat(buf1, sizeof(buf1), buf);
1318 e = search_cached_include(s1, c, buf1);
1319 if (e && define_find(e->ifndef_macro)) {
1320 /* no need to parse the include because the 'ifndef macro'
1321 is defined */
1322 #ifdef INC_DEBUG
1323 printf("%s: skipping %s\n", file->filename, buf);
1324 #endif
1325 f = NULL;
1326 } else {
1327 f = tcc_open(s1, buf1);
1328 if (!f)
1329 continue;
1332 if (tok == TOK_INCLUDE_NEXT) {
1333 tok = TOK_INCLUDE;
1334 if (f)
1335 tcc_close(f);
1336 continue;
1339 if (!f)
1340 goto include_done;
1342 #ifdef INC_DEBUG
1343 printf("%s: including %s\n", file->filename, buf1);
1344 #endif
1346 /* XXX: fix current line init */
1347 /* push current file in stack */
1348 *s1->include_stack_ptr++ = file;
1349 f->inc_type = c;
1350 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
1351 file = f;
1352 /* add include file debug info */
1353 if (tcc_state->do_debug) {
1354 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1356 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1357 ch = file->buf_ptr[0];
1358 goto the_end;
1360 error("include file '%s' not found", buf);
1361 include_done:
1362 break;
1363 case TOK_IFNDEF:
1364 c = 1;
1365 goto do_ifdef;
1366 case TOK_IF:
1367 c = expr_preprocess();
1368 goto do_if;
1369 case TOK_IFDEF:
1370 c = 0;
1371 do_ifdef:
1372 next_nomacro();
1373 if (tok < TOK_IDENT)
1374 error("invalid argument for '#if%sdef'", c ? "n" : "");
1375 if (is_bof) {
1376 if (c) {
1377 #ifdef INC_DEBUG
1378 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1379 #endif
1380 file->ifndef_macro = tok;
1383 c = (define_find(tok) != 0) ^ c;
1384 do_if:
1385 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1386 error("memory full");
1387 *s1->ifdef_stack_ptr++ = c;
1388 goto test_skip;
1389 case TOK_ELSE:
1390 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1391 error("#else without matching #if");
1392 if (s1->ifdef_stack_ptr[-1] & 2)
1393 error("#else after #else");
1394 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1395 goto test_else;
1396 case TOK_ELIF:
1397 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1398 error("#elif without matching #if");
1399 c = s1->ifdef_stack_ptr[-1];
1400 if (c > 1)
1401 error("#elif after #else");
1402 /* last #if/#elif expression was true: we skip */
1403 if (c == 1)
1404 goto skip;
1405 c = expr_preprocess();
1406 s1->ifdef_stack_ptr[-1] = c;
1407 test_else:
1408 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1409 file->ifndef_macro = 0;
1410 test_skip:
1411 if (!(c & 1)) {
1412 skip:
1413 preprocess_skip();
1414 is_bof = 0;
1415 goto redo;
1417 break;
1418 case TOK_ENDIF:
1419 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1420 error("#endif without matching #if");
1421 s1->ifdef_stack_ptr--;
1422 /* '#ifndef macro' was at the start of file. Now we check if
1423 an '#endif' is exactly at the end of file */
1424 if (file->ifndef_macro &&
1425 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1426 file->ifndef_macro_saved = file->ifndef_macro;
1427 /* need to set to zero to avoid false matches if another
1428 #ifndef at middle of file */
1429 file->ifndef_macro = 0;
1430 while (tok != TOK_LINEFEED)
1431 next_nomacro();
1432 tok_flags |= TOK_FLAG_ENDIF;
1433 goto the_end;
1435 break;
1436 case TOK_LINE:
1437 next();
1438 if (tok != TOK_CINT)
1439 error("#line");
1440 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1441 next();
1442 if (tok != TOK_LINEFEED) {
1443 if (tok != TOK_STR)
1444 error("#line");
1445 pstrcpy(file->filename, sizeof(file->filename),
1446 (char *)tokc.cstr->data);
1448 break;
1449 case TOK_ERROR:
1450 case TOK_WARNING:
1451 c = tok;
1452 ch = file->buf_ptr[0];
1453 skip_spaces();
1454 q = buf;
1455 while (ch != '\n' && ch != CH_EOF) {
1456 if ((q - buf) < sizeof(buf) - 1)
1457 *q++ = ch;
1458 if (ch == '\\') {
1459 if (handle_stray_noerror() == 0)
1460 --q;
1461 } else
1462 inp();
1464 *q = '\0';
1465 if (c == TOK_ERROR)
1466 error("#error %s", buf);
1467 else
1468 warning("#warning %s", buf);
1469 break;
1470 case TOK_PRAGMA:
1471 pragma_parse(s1);
1472 break;
1473 default:
1474 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
1475 /* '!' is ignored to allow C scripts. numbers are ignored
1476 to emulate cpp behaviour */
1477 } else {
1478 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1479 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1481 break;
1483 /* ignore other preprocess commands or #! for C scripts */
1484 while (tok != TOK_LINEFEED)
1485 next_nomacro();
1486 the_end:
1487 parse_flags = saved_parse_flags;
1490 /* evaluate escape codes in a string. */
1491 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1493 int c, n;
1494 const uint8_t *p;
1496 p = buf;
1497 for(;;) {
1498 c = *p;
1499 if (c == '\0')
1500 break;
1501 if (c == '\\') {
1502 p++;
1503 /* escape */
1504 c = *p;
1505 switch(c) {
1506 case '0': case '1': case '2': case '3':
1507 case '4': case '5': case '6': case '7':
1508 /* at most three octal digits */
1509 n = c - '0';
1510 p++;
1511 c = *p;
1512 if (isoct(c)) {
1513 n = n * 8 + c - '0';
1514 p++;
1515 c = *p;
1516 if (isoct(c)) {
1517 n = n * 8 + c - '0';
1518 p++;
1521 c = n;
1522 goto add_char_nonext;
1523 case 'x':
1524 case 'u':
1525 case 'U':
1526 p++;
1527 n = 0;
1528 for(;;) {
1529 c = *p;
1530 if (c >= 'a' && c <= 'f')
1531 c = c - 'a' + 10;
1532 else if (c >= 'A' && c <= 'F')
1533 c = c - 'A' + 10;
1534 else if (isnum(c))
1535 c = c - '0';
1536 else
1537 break;
1538 n = n * 16 + c;
1539 p++;
1541 c = n;
1542 goto add_char_nonext;
1543 case 'a':
1544 c = '\a';
1545 break;
1546 case 'b':
1547 c = '\b';
1548 break;
1549 case 'f':
1550 c = '\f';
1551 break;
1552 case 'n':
1553 c = '\n';
1554 break;
1555 case 'r':
1556 c = '\r';
1557 break;
1558 case 't':
1559 c = '\t';
1560 break;
1561 case 'v':
1562 c = '\v';
1563 break;
1564 case 'e':
1565 if (!gnu_ext)
1566 goto invalid_escape;
1567 c = 27;
1568 break;
1569 case '\'':
1570 case '\"':
1571 case '\\':
1572 case '?':
1573 break;
1574 default:
1575 invalid_escape:
1576 if (c >= '!' && c <= '~')
1577 warning("unknown escape sequence: \'\\%c\'", c);
1578 else
1579 warning("unknown escape sequence: \'\\x%x\'", c);
1580 break;
1583 p++;
1584 add_char_nonext:
1585 if (!is_long)
1586 cstr_ccat(outstr, c);
1587 else
1588 cstr_wccat(outstr, c);
1590 /* add a trailing '\0' */
1591 if (!is_long)
1592 cstr_ccat(outstr, '\0');
1593 else
1594 cstr_wccat(outstr, '\0');
1597 /* we use 64 bit numbers */
1598 #define BN_SIZE 2
1600 /* bn = (bn << shift) | or_val */
1601 void bn_lshift(unsigned int *bn, int shift, int or_val)
1603 int i;
1604 unsigned int v;
1605 for(i=0;i<BN_SIZE;i++) {
1606 v = bn[i];
1607 bn[i] = (v << shift) | or_val;
1608 or_val = v >> (32 - shift);
1612 void bn_zero(unsigned int *bn)
1614 int i;
1615 for(i=0;i<BN_SIZE;i++) {
1616 bn[i] = 0;
1620 /* parse number in null terminated string 'p' and return it in the
1621 current token */
1622 void parse_number(const char *p)
1624 int b, t, shift, frac_bits, s, exp_val, ch;
1625 char *q;
1626 unsigned int bn[BN_SIZE];
1627 double d;
1629 /* number */
1630 q = token_buf;
1631 ch = *p++;
1632 t = ch;
1633 ch = *p++;
1634 *q++ = t;
1635 b = 10;
1636 if (t == '.') {
1637 goto float_frac_parse;
1638 } else if (t == '0') {
1639 if (ch == 'x' || ch == 'X') {
1640 q--;
1641 ch = *p++;
1642 b = 16;
1643 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1644 q--;
1645 ch = *p++;
1646 b = 2;
1649 /* parse all digits. cannot check octal numbers at this stage
1650 because of floating point constants */
1651 while (1) {
1652 if (ch >= 'a' && ch <= 'f')
1653 t = ch - 'a' + 10;
1654 else if (ch >= 'A' && ch <= 'F')
1655 t = ch - 'A' + 10;
1656 else if (isnum(ch))
1657 t = ch - '0';
1658 else
1659 break;
1660 if (t >= b)
1661 break;
1662 if (q >= token_buf + STRING_MAX_SIZE) {
1663 num_too_long:
1664 error("number too long");
1666 *q++ = ch;
1667 ch = *p++;
1669 if (ch == '.' ||
1670 ((ch == 'e' || ch == 'E') && b == 10) ||
1671 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1672 if (b != 10) {
1673 /* NOTE: strtox should support that for hexa numbers, but
1674 non ISOC99 libcs do not support it, so we prefer to do
1675 it by hand */
1676 /* hexadecimal or binary floats */
1677 /* XXX: handle overflows */
1678 *q = '\0';
1679 if (b == 16)
1680 shift = 4;
1681 else
1682 shift = 2;
1683 bn_zero(bn);
1684 q = token_buf;
1685 while (1) {
1686 t = *q++;
1687 if (t == '\0') {
1688 break;
1689 } else if (t >= 'a') {
1690 t = t - 'a' + 10;
1691 } else if (t >= 'A') {
1692 t = t - 'A' + 10;
1693 } else {
1694 t = t - '0';
1696 bn_lshift(bn, shift, t);
1698 frac_bits = 0;
1699 if (ch == '.') {
1700 ch = *p++;
1701 while (1) {
1702 t = ch;
1703 if (t >= 'a' && t <= 'f') {
1704 t = t - 'a' + 10;
1705 } else if (t >= 'A' && t <= 'F') {
1706 t = t - 'A' + 10;
1707 } else if (t >= '0' && t <= '9') {
1708 t = t - '0';
1709 } else {
1710 break;
1712 if (t >= b)
1713 error("invalid digit");
1714 bn_lshift(bn, shift, t);
1715 frac_bits += shift;
1716 ch = *p++;
1719 if (ch != 'p' && ch != 'P')
1720 expect("exponent");
1721 ch = *p++;
1722 s = 1;
1723 exp_val = 0;
1724 if (ch == '+') {
1725 ch = *p++;
1726 } else if (ch == '-') {
1727 s = -1;
1728 ch = *p++;
1730 if (ch < '0' || ch > '9')
1731 expect("exponent digits");
1732 while (ch >= '0' && ch <= '9') {
1733 exp_val = exp_val * 10 + ch - '0';
1734 ch = *p++;
1736 exp_val = exp_val * s;
1738 /* now we can generate the number */
1739 /* XXX: should patch directly float number */
1740 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1741 d = ldexp(d, exp_val - frac_bits);
1742 t = toup(ch);
1743 if (t == 'F') {
1744 ch = *p++;
1745 tok = TOK_CFLOAT;
1746 /* float : should handle overflow */
1747 tokc.f = (float)d;
1748 } else if (t == 'L') {
1749 ch = *p++;
1750 #ifdef TCC_TARGET_PE
1751 tok = TOK_CDOUBLE;
1752 tokc.d = d;
1753 #else
1754 tok = TOK_CLDOUBLE;
1755 /* XXX: not large enough */
1756 tokc.ld = (long double)d;
1757 #endif
1758 } else {
1759 tok = TOK_CDOUBLE;
1760 tokc.d = d;
1762 } else {
1763 /* decimal floats */
1764 if (ch == '.') {
1765 if (q >= token_buf + STRING_MAX_SIZE)
1766 goto num_too_long;
1767 *q++ = ch;
1768 ch = *p++;
1769 float_frac_parse:
1770 while (ch >= '0' && ch <= '9') {
1771 if (q >= token_buf + STRING_MAX_SIZE)
1772 goto num_too_long;
1773 *q++ = ch;
1774 ch = *p++;
1777 if (ch == 'e' || ch == 'E') {
1778 if (q >= token_buf + STRING_MAX_SIZE)
1779 goto num_too_long;
1780 *q++ = ch;
1781 ch = *p++;
1782 if (ch == '-' || ch == '+') {
1783 if (q >= token_buf + STRING_MAX_SIZE)
1784 goto num_too_long;
1785 *q++ = ch;
1786 ch = *p++;
1788 if (ch < '0' || ch > '9')
1789 expect("exponent digits");
1790 while (ch >= '0' && ch <= '9') {
1791 if (q >= token_buf + STRING_MAX_SIZE)
1792 goto num_too_long;
1793 *q++ = ch;
1794 ch = *p++;
1797 *q = '\0';
1798 t = toup(ch);
1799 errno = 0;
1800 if (t == 'F') {
1801 ch = *p++;
1802 tok = TOK_CFLOAT;
1803 tokc.f = strtof(token_buf, NULL);
1804 } else if (t == 'L') {
1805 ch = *p++;
1806 #ifdef TCC_TARGET_PE
1807 tok = TOK_CDOUBLE;
1808 tokc.d = strtod(token_buf, NULL);
1809 #else
1810 tok = TOK_CLDOUBLE;
1811 tokc.ld = strtold(token_buf, NULL);
1812 #endif
1813 } else {
1814 tok = TOK_CDOUBLE;
1815 tokc.d = strtod(token_buf, NULL);
1818 } else {
1819 unsigned long long n, n1;
1820 int lcount, ucount;
1822 /* integer number */
1823 *q = '\0';
1824 q = token_buf;
1825 if (b == 10 && *q == '0') {
1826 b = 8;
1827 q++;
1829 n = 0;
1830 while(1) {
1831 t = *q++;
1832 /* no need for checks except for base 10 / 8 errors */
1833 if (t == '\0') {
1834 break;
1835 } else if (t >= 'a') {
1836 t = t - 'a' + 10;
1837 } else if (t >= 'A') {
1838 t = t - 'A' + 10;
1839 } else {
1840 t = t - '0';
1841 if (t >= b)
1842 error("invalid digit");
1844 n1 = n;
1845 n = n * b + t;
1846 /* detect overflow */
1847 /* XXX: this test is not reliable */
1848 if (n < n1)
1849 error("integer constant overflow");
1852 /* XXX: not exactly ANSI compliant */
1853 if ((n & 0xffffffff00000000LL) != 0) {
1854 if ((n >> 63) != 0)
1855 tok = TOK_CULLONG;
1856 else
1857 tok = TOK_CLLONG;
1858 } else if (n > 0x7fffffff) {
1859 tok = TOK_CUINT;
1860 } else {
1861 tok = TOK_CINT;
1863 lcount = 0;
1864 ucount = 0;
1865 for(;;) {
1866 t = toup(ch);
1867 if (t == 'L') {
1868 if (lcount >= 2)
1869 error("three 'l's in integer constant");
1870 lcount++;
1871 if (lcount == 2) {
1872 if (tok == TOK_CINT)
1873 tok = TOK_CLLONG;
1874 else if (tok == TOK_CUINT)
1875 tok = TOK_CULLONG;
1877 ch = *p++;
1878 } else if (t == 'U') {
1879 if (ucount >= 1)
1880 error("two 'u's in integer constant");
1881 ucount++;
1882 if (tok == TOK_CINT)
1883 tok = TOK_CUINT;
1884 else if (tok == TOK_CLLONG)
1885 tok = TOK_CULLONG;
1886 ch = *p++;
1887 } else {
1888 break;
1891 if (tok == TOK_CINT || tok == TOK_CUINT)
1892 tokc.ui = n;
1893 else
1894 tokc.ull = n;
1896 if (ch)
1897 error("invalid number\n");
1901 #define PARSE2(c1, tok1, c2, tok2) \
1902 case c1: \
1903 PEEKC(c, p); \
1904 if (c == c2) { \
1905 p++; \
1906 tok = tok2; \
1907 } else { \
1908 tok = tok1; \
1910 break;
1912 /* return next token without macro substitution */
1913 static inline void next_nomacro1(void)
1915 int t, c, is_long;
1916 TokenSym *ts;
1917 uint8_t *p, *p1;
1918 unsigned int h;
1920 p = file->buf_ptr;
1921 redo_no_start:
1922 c = *p;
1923 switch(c) {
1924 case ' ':
1925 case '\t':
1926 tok = c;
1927 p++;
1928 goto keep_tok_flags;
1929 case '\f':
1930 case '\v':
1931 case '\r':
1932 p++;
1933 goto redo_no_start;
1934 case '\\':
1935 /* first look if it is in fact an end of buffer */
1936 if (p >= file->buf_end) {
1937 file->buf_ptr = p;
1938 handle_eob();
1939 p = file->buf_ptr;
1940 if (p >= file->buf_end)
1941 goto parse_eof;
1942 else
1943 goto redo_no_start;
1944 } else {
1945 file->buf_ptr = p;
1946 ch = *p;
1947 handle_stray();
1948 p = file->buf_ptr;
1949 goto redo_no_start;
1951 parse_eof:
1953 TCCState *s1 = tcc_state;
1954 if ((parse_flags & PARSE_FLAG_LINEFEED)
1955 && !(tok_flags & TOK_FLAG_EOF)) {
1956 tok_flags |= TOK_FLAG_EOF;
1957 tok = TOK_LINEFEED;
1958 goto keep_tok_flags;
1959 } else if (s1->include_stack_ptr == s1->include_stack ||
1960 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
1961 /* no include left : end of file. */
1962 tok = TOK_EOF;
1963 } else {
1964 tok_flags &= ~TOK_FLAG_EOF;
1965 /* pop include file */
1967 /* test if previous '#endif' was after a #ifdef at
1968 start of file */
1969 if (tok_flags & TOK_FLAG_ENDIF) {
1970 #ifdef INC_DEBUG
1971 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
1972 #endif
1973 add_cached_include(s1, file->inc_type, file->inc_filename,
1974 file->ifndef_macro_saved);
1977 /* add end of include file debug info */
1978 if (tcc_state->do_debug) {
1979 put_stabd(N_EINCL, 0, 0);
1981 /* pop include stack */
1982 tcc_close(file);
1983 s1->include_stack_ptr--;
1984 file = *s1->include_stack_ptr;
1985 p = file->buf_ptr;
1986 goto redo_no_start;
1989 break;
1991 case '\n':
1992 file->line_num++;
1993 tok_flags |= TOK_FLAG_BOL;
1994 p++;
1995 maybe_newline:
1996 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
1997 goto redo_no_start;
1998 tok = TOK_LINEFEED;
1999 goto keep_tok_flags;
2001 case '#':
2002 /* XXX: simplify */
2003 PEEKC(c, p);
2004 if ((tok_flags & TOK_FLAG_BOL) &&
2005 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2006 file->buf_ptr = p;
2007 preprocess(tok_flags & TOK_FLAG_BOF);
2008 p = file->buf_ptr;
2009 goto maybe_newline;
2010 } else {
2011 if (c == '#') {
2012 p++;
2013 tok = TOK_TWOSHARPS;
2014 } else {
2015 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2016 p = parse_line_comment(p - 1);
2017 goto redo_no_start;
2018 } else {
2019 tok = '#';
2023 break;
2025 case 'a': case 'b': case 'c': case 'd':
2026 case 'e': case 'f': case 'g': case 'h':
2027 case 'i': case 'j': case 'k': case 'l':
2028 case 'm': case 'n': case 'o': case 'p':
2029 case 'q': case 'r': case 's': case 't':
2030 case 'u': case 'v': case 'w': case 'x':
2031 case 'y': case 'z':
2032 case 'A': case 'B': case 'C': case 'D':
2033 case 'E': case 'F': case 'G': case 'H':
2034 case 'I': case 'J': case 'K':
2035 case 'M': case 'N': case 'O': case 'P':
2036 case 'Q': case 'R': case 'S': case 'T':
2037 case 'U': case 'V': case 'W': case 'X':
2038 case 'Y': case 'Z':
2039 case '_':
2040 parse_ident_fast:
2041 p1 = p;
2042 h = TOK_HASH_INIT;
2043 h = TOK_HASH_FUNC(h, c);
2044 p++;
2045 for(;;) {
2046 c = *p;
2047 if (!isidnum_table[c-CH_EOF])
2048 break;
2049 h = TOK_HASH_FUNC(h, c);
2050 p++;
2052 if (c != '\\') {
2053 TokenSym **pts;
2054 int len;
2056 /* fast case : no stray found, so we have the full token
2057 and we have already hashed it */
2058 len = p - p1;
2059 h &= (TOK_HASH_SIZE - 1);
2060 pts = &hash_ident[h];
2061 for(;;) {
2062 ts = *pts;
2063 if (!ts)
2064 break;
2065 if (ts->len == len && !memcmp(ts->str, p1, len))
2066 goto token_found;
2067 pts = &(ts->hash_next);
2069 ts = tok_alloc_new(pts, p1, len);
2070 token_found: ;
2071 } else {
2072 /* slower case */
2073 cstr_reset(&tokcstr);
2075 while (p1 < p) {
2076 cstr_ccat(&tokcstr, *p1);
2077 p1++;
2079 p--;
2080 PEEKC(c, p);
2081 parse_ident_slow:
2082 while (isidnum_table[c-CH_EOF]) {
2083 cstr_ccat(&tokcstr, c);
2084 PEEKC(c, p);
2086 ts = tok_alloc(tokcstr.data, tokcstr.size);
2088 tok = ts->tok;
2089 break;
2090 case 'L':
2091 t = p[1];
2092 if (t != '\\' && t != '\'' && t != '\"') {
2093 /* fast case */
2094 goto parse_ident_fast;
2095 } else {
2096 PEEKC(c, p);
2097 if (c == '\'' || c == '\"') {
2098 is_long = 1;
2099 goto str_const;
2100 } else {
2101 cstr_reset(&tokcstr);
2102 cstr_ccat(&tokcstr, 'L');
2103 goto parse_ident_slow;
2106 break;
2107 case '0': case '1': case '2': case '3':
2108 case '4': case '5': case '6': case '7':
2109 case '8': case '9':
2111 cstr_reset(&tokcstr);
2112 /* after the first digit, accept digits, alpha, '.' or sign if
2113 prefixed by 'eEpP' */
2114 parse_num:
2115 for(;;) {
2116 t = c;
2117 cstr_ccat(&tokcstr, c);
2118 PEEKC(c, p);
2119 if (!(isnum(c) || isid(c) || c == '.' ||
2120 ((c == '+' || c == '-') &&
2121 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2122 break;
2124 /* We add a trailing '\0' to ease parsing */
2125 cstr_ccat(&tokcstr, '\0');
2126 tokc.cstr = &tokcstr;
2127 tok = TOK_PPNUM;
2128 break;
2129 case '.':
2130 /* special dot handling because it can also start a number */
2131 PEEKC(c, p);
2132 if (isnum(c)) {
2133 cstr_reset(&tokcstr);
2134 cstr_ccat(&tokcstr, '.');
2135 goto parse_num;
2136 } else if (c == '.') {
2137 PEEKC(c, p);
2138 if (c != '.')
2139 expect("'.'");
2140 PEEKC(c, p);
2141 tok = TOK_DOTS;
2142 } else {
2143 tok = '.';
2145 break;
2146 case '\'':
2147 case '\"':
2148 is_long = 0;
2149 str_const:
2151 CString str;
2152 int sep;
2154 sep = c;
2156 /* parse the string */
2157 cstr_new(&str);
2158 p = parse_pp_string(p, sep, &str);
2159 cstr_ccat(&str, '\0');
2161 /* eval the escape (should be done as TOK_PPNUM) */
2162 cstr_reset(&tokcstr);
2163 parse_escape_string(&tokcstr, str.data, is_long);
2164 cstr_free(&str);
2166 if (sep == '\'') {
2167 int char_size;
2168 /* XXX: make it portable */
2169 if (!is_long)
2170 char_size = 1;
2171 else
2172 char_size = sizeof(nwchar_t);
2173 if (tokcstr.size <= char_size)
2174 error("empty character constant");
2175 if (tokcstr.size > 2 * char_size)
2176 warning("multi-character character constant");
2177 if (!is_long) {
2178 tokc.i = *(int8_t *)tokcstr.data;
2179 tok = TOK_CCHAR;
2180 } else {
2181 tokc.i = *(nwchar_t *)tokcstr.data;
2182 tok = TOK_LCHAR;
2184 } else {
2185 tokc.cstr = &tokcstr;
2186 if (!is_long)
2187 tok = TOK_STR;
2188 else
2189 tok = TOK_LSTR;
2192 break;
2194 case '<':
2195 PEEKC(c, p);
2196 if (c == '=') {
2197 p++;
2198 tok = TOK_LE;
2199 } else if (c == '<') {
2200 PEEKC(c, p);
2201 if (c == '=') {
2202 p++;
2203 tok = TOK_A_SHL;
2204 } else {
2205 tok = TOK_SHL;
2207 } else {
2208 tok = TOK_LT;
2210 break;
2212 case '>':
2213 PEEKC(c, p);
2214 if (c == '=') {
2215 p++;
2216 tok = TOK_GE;
2217 } else if (c == '>') {
2218 PEEKC(c, p);
2219 if (c == '=') {
2220 p++;
2221 tok = TOK_A_SAR;
2222 } else {
2223 tok = TOK_SAR;
2225 } else {
2226 tok = TOK_GT;
2228 break;
2230 case '&':
2231 PEEKC(c, p);
2232 if (c == '&') {
2233 p++;
2234 tok = TOK_LAND;
2235 } else if (c == '=') {
2236 p++;
2237 tok = TOK_A_AND;
2238 } else {
2239 tok = '&';
2241 break;
2243 case '|':
2244 PEEKC(c, p);
2245 if (c == '|') {
2246 p++;
2247 tok = TOK_LOR;
2248 } else if (c == '=') {
2249 p++;
2250 tok = TOK_A_OR;
2251 } else {
2252 tok = '|';
2254 break;
2256 case '+':
2257 PEEKC(c, p);
2258 if (c == '+') {
2259 p++;
2260 tok = TOK_INC;
2261 } else if (c == '=') {
2262 p++;
2263 tok = TOK_A_ADD;
2264 } else {
2265 tok = '+';
2267 break;
2269 case '-':
2270 PEEKC(c, p);
2271 if (c == '-') {
2272 p++;
2273 tok = TOK_DEC;
2274 } else if (c == '=') {
2275 p++;
2276 tok = TOK_A_SUB;
2277 } else if (c == '>') {
2278 p++;
2279 tok = TOK_ARROW;
2280 } else {
2281 tok = '-';
2283 break;
2285 PARSE2('!', '!', '=', TOK_NE)
2286 PARSE2('=', '=', '=', TOK_EQ)
2287 PARSE2('*', '*', '=', TOK_A_MUL)
2288 PARSE2('%', '%', '=', TOK_A_MOD)
2289 PARSE2('^', '^', '=', TOK_A_XOR)
2291 /* comments or operator */
2292 case '/':
2293 PEEKC(c, p);
2294 if (c == '*') {
2295 p = parse_comment(p);
2296 goto redo_no_start;
2297 } else if (c == '/') {
2298 p = parse_line_comment(p);
2299 goto redo_no_start;
2300 } else if (c == '=') {
2301 p++;
2302 tok = TOK_A_DIV;
2303 } else {
2304 tok = '/';
2306 break;
2308 /* simple tokens */
2309 case '(':
2310 case ')':
2311 case '[':
2312 case ']':
2313 case '{':
2314 case '}':
2315 case ',':
2316 case ';':
2317 case ':':
2318 case '?':
2319 case '~':
2320 case '$': /* only used in assembler */
2321 case '@': /* dito */
2322 tok = c;
2323 p++;
2324 break;
2325 default:
2326 error("unrecognized character \\x%02x", c);
2327 break;
2329 tok_flags = 0;
2330 keep_tok_flags:
2331 file->buf_ptr = p;
2332 #if defined(PARSE_DEBUG)
2333 printf("token = %s\n", get_tok_str(tok, &tokc));
2334 #endif
2337 /* return next token without macro substitution. Can read input from
2338 macro_ptr buffer */
2339 static void next_nomacro_spc(void)
2341 if (macro_ptr) {
2342 redo:
2343 tok = *macro_ptr;
2344 if (tok) {
2345 TOK_GET(tok, macro_ptr, tokc);
2346 if (tok == TOK_LINENUM) {
2347 file->line_num = tokc.i;
2348 goto redo;
2351 } else {
2352 next_nomacro1();
2356 static void next_nomacro(void)
2358 do {
2359 next_nomacro_spc();
2360 } while (is_space(tok));
2363 /* substitute args in macro_str and return allocated string */
2364 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
2366 int *st, last_tok, t, spc;
2367 Sym *s;
2368 CValue cval;
2369 TokenString str;
2370 CString cstr;
2372 tok_str_new(&str);
2373 last_tok = 0;
2374 while(1) {
2375 TOK_GET(t, macro_str, cval);
2376 if (!t)
2377 break;
2378 if (t == '#') {
2379 /* stringize */
2380 TOK_GET(t, macro_str, cval);
2381 if (!t)
2382 break;
2383 s = sym_find2(args, t);
2384 if (s) {
2385 cstr_new(&cstr);
2386 st = s->d;
2387 spc = 0;
2388 while (*st) {
2389 TOK_GET(t, st, cval);
2390 if (!check_space(t, &spc))
2391 cstr_cat(&cstr, get_tok_str(t, &cval));
2393 cstr.size -= spc;
2394 cstr_ccat(&cstr, '\0');
2395 #ifdef PP_DEBUG
2396 printf("stringize: %s\n", (char *)cstr.data);
2397 #endif
2398 /* add string */
2399 cval.cstr = &cstr;
2400 tok_str_add2(&str, TOK_STR, &cval);
2401 cstr_free(&cstr);
2402 } else {
2403 tok_str_add2(&str, t, &cval);
2405 } else if (t >= TOK_IDENT) {
2406 s = sym_find2(args, t);
2407 if (s) {
2408 st = s->d;
2409 /* if '##' is present before or after, no arg substitution */
2410 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2411 /* special case for var arg macros : ## eats the
2412 ',' if empty VA_ARGS variable. */
2413 /* XXX: test of the ',' is not 100%
2414 reliable. should fix it to avoid security
2415 problems */
2416 if (gnu_ext && s->type.t &&
2417 last_tok == TOK_TWOSHARPS &&
2418 str.len >= 2 && str.str[str.len - 2] == ',') {
2419 if (*st == 0) {
2420 /* suppress ',' '##' */
2421 str.len -= 2;
2422 } else {
2423 /* suppress '##' and add variable */
2424 str.len--;
2425 goto add_var;
2427 } else {
2428 int t1;
2429 add_var:
2430 for(;;) {
2431 TOK_GET(t1, st, cval);
2432 if (!t1)
2433 break;
2434 tok_str_add2(&str, t1, &cval);
2437 } else {
2438 /* NOTE: the stream cannot be read when macro
2439 substituing an argument */
2440 macro_subst(&str, nested_list, st, NULL);
2442 } else {
2443 tok_str_add(&str, t);
2445 } else {
2446 tok_str_add2(&str, t, &cval);
2448 last_tok = t;
2450 tok_str_add(&str, 0);
2451 return str.str;
2454 static char const ab_month_name[12][4] =
2456 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2457 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2460 /* do macro substitution of current token with macro 's' and add
2461 result to (tok_str,tok_len). 'nested_list' is the list of all
2462 macros we got inside to avoid recursing. Return non zero if no
2463 substitution needs to be done */
2464 static int macro_subst_tok(TokenString *tok_str,
2465 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2467 Sym *args, *sa, *sa1;
2468 int mstr_allocated, parlevel, *mstr, t, t1, *p, spc;
2469 TokenString str;
2470 char *cstrval;
2471 CValue cval;
2472 CString cstr;
2473 char buf[32];
2475 /* if symbol is a macro, prepare substitution */
2476 /* special macros */
2477 if (tok == TOK___LINE__) {
2478 snprintf(buf, sizeof(buf), "%d", file->line_num);
2479 cstrval = buf;
2480 t1 = TOK_PPNUM;
2481 goto add_cstr1;
2482 } else if (tok == TOK___FILE__) {
2483 cstrval = file->filename;
2484 goto add_cstr;
2485 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2486 time_t ti;
2487 struct tm *tm;
2489 time(&ti);
2490 tm = localtime(&ti);
2491 if (tok == TOK___DATE__) {
2492 snprintf(buf, sizeof(buf), "%s %2d %d",
2493 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2494 } else {
2495 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2496 tm->tm_hour, tm->tm_min, tm->tm_sec);
2498 cstrval = buf;
2499 add_cstr:
2500 t1 = TOK_STR;
2501 add_cstr1:
2502 cstr_new(&cstr);
2503 cstr_cat(&cstr, cstrval);
2504 cstr_ccat(&cstr, '\0');
2505 cval.cstr = &cstr;
2506 tok_str_add2(tok_str, t1, &cval);
2507 cstr_free(&cstr);
2508 } else {
2509 mstr = s->d;
2510 mstr_allocated = 0;
2511 if (s->type.t == MACRO_FUNC) {
2512 /* NOTE: we do not use next_nomacro to avoid eating the
2513 next token. XXX: find better solution */
2514 redo:
2515 if (macro_ptr) {
2516 p = macro_ptr;
2517 while (is_space(t = *p) || TOK_LINEFEED == t)
2518 ++p;
2519 if (t == 0 && can_read_stream) {
2520 /* end of macro stream: we must look at the token
2521 after in the file */
2522 struct macro_level *ml = *can_read_stream;
2523 macro_ptr = NULL;
2524 if (ml)
2526 macro_ptr = ml->p;
2527 ml->p = NULL;
2528 *can_read_stream = ml -> prev;
2530 goto redo;
2532 } else {
2533 /* XXX: incorrect with comments */
2534 ch = file->buf_ptr[0];
2535 while (is_space(ch) || ch == '\n')
2536 cinp();
2537 t = ch;
2539 if (t != '(') /* no macro subst */
2540 return -1;
2542 /* argument macro */
2543 next_nomacro();
2544 next_nomacro();
2545 args = NULL;
2546 sa = s->next;
2547 /* NOTE: empty args are allowed, except if no args */
2548 for(;;) {
2549 /* handle '()' case */
2550 if (!args && !sa && tok == ')')
2551 break;
2552 if (!sa)
2553 error("macro '%s' used with too many args",
2554 get_tok_str(s->v, 0));
2555 tok_str_new(&str);
2556 parlevel = spc = 0;
2557 /* NOTE: non zero sa->t indicates VA_ARGS */
2558 while ((parlevel > 0 ||
2559 (tok != ')' &&
2560 (tok != ',' || sa->type.t))) &&
2561 tok != -1) {
2562 if (tok == '(')
2563 parlevel++;
2564 else if (tok == ')')
2565 parlevel--;
2566 if (tok == TOK_LINEFEED)
2567 tok = ' ';
2568 if (!check_space(tok, &spc))
2569 tok_str_add2(&str, tok, &tokc);
2570 next_nomacro_spc();
2572 str.len -= spc;
2573 tok_str_add(&str, 0);
2574 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2575 sa1->d = str.str;
2576 sa = sa->next;
2577 if (tok == ')') {
2578 /* special case for gcc var args: add an empty
2579 var arg argument if it is omitted */
2580 if (sa && sa->type.t && gnu_ext)
2581 continue;
2582 else
2583 break;
2585 if (tok != ',')
2586 expect(",");
2587 next_nomacro();
2589 if (sa) {
2590 error("macro '%s' used with too few args",
2591 get_tok_str(s->v, 0));
2594 /* now subst each arg */
2595 mstr = macro_arg_subst(nested_list, mstr, args);
2596 /* free memory */
2597 sa = args;
2598 while (sa) {
2599 sa1 = sa->prev;
2600 tok_str_free(sa->d);
2601 sym_free(sa);
2602 sa = sa1;
2604 mstr_allocated = 1;
2606 sym_push2(nested_list, s->v, 0, 0);
2607 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2608 /* pop nested defined symbol */
2609 sa1 = *nested_list;
2610 *nested_list = sa1->prev;
2611 sym_free(sa1);
2612 if (mstr_allocated)
2613 tok_str_free(mstr);
2615 return 0;
2618 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2619 return the resulting string (which must be freed). */
2620 static inline int *macro_twosharps(const int *macro_str)
2622 const int *ptr;
2623 int t;
2624 CValue cval;
2625 TokenString macro_str1;
2626 CString cstr;
2627 char *p;
2628 int n;
2630 /* we search the first '##' */
2631 for(ptr = macro_str;;) {
2632 TOK_GET(t, ptr, cval);
2633 if (t == TOK_TWOSHARPS)
2634 break;
2635 /* nothing more to do if end of string */
2636 if (t == 0)
2637 return NULL;
2640 /* we saw '##', so we need more processing to handle it */
2641 tok_str_new(&macro_str1);
2642 for(ptr = macro_str;;) {
2643 TOK_GET(tok, ptr, tokc);
2644 if (tok == 0)
2645 break;
2646 if (tok == TOK_TWOSHARPS)
2647 continue;
2648 while (*ptr == TOK_TWOSHARPS) {
2649 t = *++ptr;
2650 if (t && t != TOK_TWOSHARPS) {
2651 TOK_GET(t, ptr, cval);
2653 /* We concatenate the two tokens */
2654 cstr_new(&cstr);
2655 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2656 n = cstr.size;
2657 cstr_cat(&cstr, get_tok_str(t, &cval));
2658 cstr_ccat(&cstr, '\0');
2660 p = file->buf_ptr;
2661 file->buf_ptr = cstr.data;
2662 for (;;) {
2663 next_nomacro1();
2664 if (0 == *file->buf_ptr)
2665 break;
2666 tok_str_add2(&macro_str1, tok, &tokc);
2668 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2669 n, cstr.data, (char*)cstr.data + n);
2671 file->buf_ptr = p;
2672 cstr_reset(&cstr);
2675 tok_str_add2(&macro_str1, tok, &tokc);
2677 tok_str_add(&macro_str1, 0);
2678 return macro_str1.str;
2682 /* do macro substitution of macro_str and add result to
2683 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2684 inside to avoid recursing. */
2685 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2686 const int *macro_str, struct macro_level ** can_read_stream)
2688 Sym *s;
2689 int *macro_str1;
2690 const int *ptr;
2691 int t, ret, spc;
2692 CValue cval;
2693 struct macro_level ml;
2695 /* first scan for '##' operator handling */
2696 ptr = macro_str;
2697 macro_str1 = macro_twosharps(ptr);
2698 if (macro_str1)
2699 ptr = macro_str1;
2700 spc = 0;
2701 while (1) {
2702 /* NOTE: ptr == NULL can only happen if tokens are read from
2703 file stream due to a macro function call */
2704 if (ptr == NULL)
2705 break;
2706 TOK_GET(t, ptr, cval);
2707 if (t == 0)
2708 break;
2709 s = define_find(t);
2710 if (s != NULL) {
2711 /* if nested substitution, do nothing */
2712 if (sym_find2(*nested_list, t))
2713 goto no_subst;
2714 ml.p = macro_ptr;
2715 if (can_read_stream)
2716 ml.prev = *can_read_stream, *can_read_stream = &ml;
2717 macro_ptr = (int *)ptr;
2718 tok = t;
2719 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
2720 ptr = (int *)macro_ptr;
2721 macro_ptr = ml.p;
2722 if (can_read_stream && *can_read_stream == &ml)
2723 *can_read_stream = ml.prev;
2724 if (ret != 0)
2725 goto no_subst;
2726 } else {
2727 no_subst:
2728 if (!check_space(t, &spc))
2729 tok_str_add2(tok_str, t, &cval);
2732 if (macro_str1)
2733 tok_str_free(macro_str1);
2736 /* return next token with macro substitution */
2737 static void next(void)
2739 Sym *nested_list, *s;
2740 TokenString str;
2741 struct macro_level *ml;
2743 redo:
2744 if (parse_flags & PARSE_FLAG_SPACES)
2745 next_nomacro_spc();
2746 else
2747 next_nomacro();
2748 if (!macro_ptr) {
2749 /* if not reading from macro substituted string, then try
2750 to substitute macros */
2751 if (tok >= TOK_IDENT &&
2752 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2753 s = define_find(tok);
2754 if (s) {
2755 /* we have a macro: we try to substitute */
2756 tok_str_new(&str);
2757 nested_list = NULL;
2758 ml = NULL;
2759 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
2760 /* substitution done, NOTE: maybe empty */
2761 tok_str_add(&str, 0);
2762 macro_ptr = str.str;
2763 macro_ptr_allocated = str.str;
2764 goto redo;
2768 } else {
2769 if (tok == 0) {
2770 /* end of macro or end of unget buffer */
2771 if (unget_buffer_enabled) {
2772 macro_ptr = unget_saved_macro_ptr;
2773 unget_buffer_enabled = 0;
2774 } else {
2775 /* end of macro string: free it */
2776 tok_str_free(macro_ptr_allocated);
2777 macro_ptr_allocated = NULL;
2778 macro_ptr = NULL;
2780 goto redo;
2784 /* convert preprocessor tokens into C tokens */
2785 if (tok == TOK_PPNUM &&
2786 (parse_flags & PARSE_FLAG_TOK_NUM)) {
2787 parse_number((char *)tokc.cstr->data);
2791 /* push back current token and set current token to 'last_tok'. Only
2792 identifier case handled for labels. */
2793 static inline void unget_tok(int last_tok)
2795 int i, n;
2796 int *q;
2797 unget_saved_macro_ptr = macro_ptr;
2798 unget_buffer_enabled = 1;
2799 q = unget_saved_buffer;
2800 macro_ptr = q;
2801 *q++ = tok;
2802 n = tok_ext_size(tok) - 1;
2803 for(i=0;i<n;i++)
2804 *q++ = tokc.tab[i];
2805 *q = 0; /* end of token string */
2806 tok = last_tok;
2810 /* better than nothing, but needs extension to handle '-E' option
2811 correctly too */
2812 static void preprocess_init(TCCState *s1)
2814 s1->include_stack_ptr = s1->include_stack;
2815 /* XXX: move that before to avoid having to initialize
2816 file->ifdef_stack_ptr ? */
2817 s1->ifdef_stack_ptr = s1->ifdef_stack;
2818 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2820 /* XXX: not ANSI compliant: bound checking says error */
2821 vtop = vstack - 1;
2822 s1->pack_stack[0] = 0;
2823 s1->pack_stack_ptr = s1->pack_stack;
2826 void preprocess_new()
2828 int i, c;
2829 const char *p, *r;
2830 TokenSym *ts;
2832 /* init isid table */
2833 for(i=CH_EOF;i<256;i++)
2834 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
2836 /* add all tokens */
2837 table_ident = NULL;
2838 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
2840 tok_ident = TOK_IDENT;
2841 p = tcc_keywords;
2842 while (*p) {
2843 r = p;
2844 for(;;) {
2845 c = *r++;
2846 if (c == '\0')
2847 break;
2849 ts = tok_alloc(p, r - p - 1);
2850 p = r;
2854 /* Preprocess the current file */
2855 static int tcc_preprocess(TCCState *s1)
2857 Sym *define_start;
2858 BufferedFile *file_ref, **iptr, **iptr_new;
2859 int token_seen, line_ref, d;
2860 const char *s;
2862 preprocess_init(s1);
2863 define_start = define_stack;
2864 ch = file->buf_ptr[0];
2865 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
2866 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
2867 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
2868 token_seen = 0;
2869 line_ref = 0;
2870 file_ref = NULL;
2872 iptr = s1->include_stack_ptr;
2873 for (;;) {
2874 next();
2875 if (tok == TOK_EOF) {
2876 break;
2877 } else if (file != file_ref) {
2878 goto print_line;
2879 } else if (tok == TOK_LINEFEED) {
2880 if (!token_seen)
2881 continue;
2882 ++line_ref;
2883 token_seen = 0;
2884 } else if (!token_seen) {
2885 d = file->line_num - line_ref;
2886 if (file != file_ref || d < 0 || d >= 8) {
2887 print_line:
2888 iptr_new = s1->include_stack_ptr;
2889 s = iptr_new > iptr ? " 1"
2890 : iptr_new < iptr ? " 2"
2891 : iptr_new > s1->include_stack ? " 3"
2892 : ""
2894 iptr = iptr_new;
2895 fprintf(s1->outfile, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
2896 } else {
2897 while (d)
2898 fputs("\n", s1->outfile), --d;
2900 line_ref = (file_ref = file)->line_num;
2901 token_seen = tok != TOK_LINEFEED;
2902 if (!token_seen)
2903 continue;
2905 fputs(get_tok_str(tok, &tokc), s1->outfile);
2907 free_defines(define_start);
2908 return 0;