tccpp: avoid double free with macro_ptr_allocated (after errors)
[tinycc/k1w1.git] / tccpp.c
blobd03adc0f97bdd0bd6fd7cf5be4c888ce4291cb69
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_skip;
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_skip:
1408 if (!(c & 1)) {
1409 skip:
1410 preprocess_skip();
1411 is_bof = 0;
1412 goto redo;
1414 break;
1415 case TOK_ENDIF:
1416 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1417 error("#endif without matching #if");
1418 s1->ifdef_stack_ptr--;
1419 /* '#ifndef macro' was at the start of file. Now we check if
1420 an '#endif' is exactly at the end of file */
1421 if (file->ifndef_macro &&
1422 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1423 file->ifndef_macro_saved = file->ifndef_macro;
1424 /* need to set to zero to avoid false matches if another
1425 #ifndef at middle of file */
1426 file->ifndef_macro = 0;
1427 while (tok != TOK_LINEFEED)
1428 next_nomacro();
1429 tok_flags |= TOK_FLAG_ENDIF;
1430 goto the_end;
1432 break;
1433 case TOK_LINE:
1434 next();
1435 if (tok != TOK_CINT)
1436 error("#line");
1437 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1438 next();
1439 if (tok != TOK_LINEFEED) {
1440 if (tok != TOK_STR)
1441 error("#line");
1442 pstrcpy(file->filename, sizeof(file->filename),
1443 (char *)tokc.cstr->data);
1445 break;
1446 case TOK_ERROR:
1447 case TOK_WARNING:
1448 c = tok;
1449 ch = file->buf_ptr[0];
1450 skip_spaces();
1451 q = buf;
1452 while (ch != '\n' && ch != CH_EOF) {
1453 if ((q - buf) < sizeof(buf) - 1)
1454 *q++ = ch;
1455 if (ch == '\\') {
1456 if (handle_stray_noerror() == 0)
1457 --q;
1458 } else
1459 inp();
1461 *q = '\0';
1462 if (c == TOK_ERROR)
1463 error("#error %s", buf);
1464 else
1465 warning("#warning %s", buf);
1466 break;
1467 case TOK_PRAGMA:
1468 pragma_parse(s1);
1469 break;
1470 default:
1471 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
1472 /* '!' is ignored to allow C scripts. numbers are ignored
1473 to emulate cpp behaviour */
1474 } else {
1475 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1476 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1478 break;
1480 /* ignore other preprocess commands or #! for C scripts */
1481 while (tok != TOK_LINEFEED)
1482 next_nomacro();
1483 the_end:
1484 parse_flags = saved_parse_flags;
1487 /* evaluate escape codes in a string. */
1488 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1490 int c, n;
1491 const uint8_t *p;
1493 p = buf;
1494 for(;;) {
1495 c = *p;
1496 if (c == '\0')
1497 break;
1498 if (c == '\\') {
1499 p++;
1500 /* escape */
1501 c = *p;
1502 switch(c) {
1503 case '0': case '1': case '2': case '3':
1504 case '4': case '5': case '6': case '7':
1505 /* at most three octal digits */
1506 n = c - '0';
1507 p++;
1508 c = *p;
1509 if (isoct(c)) {
1510 n = n * 8 + c - '0';
1511 p++;
1512 c = *p;
1513 if (isoct(c)) {
1514 n = n * 8 + c - '0';
1515 p++;
1518 c = n;
1519 goto add_char_nonext;
1520 case 'x':
1521 case 'u':
1522 case 'U':
1523 p++;
1524 n = 0;
1525 for(;;) {
1526 c = *p;
1527 if (c >= 'a' && c <= 'f')
1528 c = c - 'a' + 10;
1529 else if (c >= 'A' && c <= 'F')
1530 c = c - 'A' + 10;
1531 else if (isnum(c))
1532 c = c - '0';
1533 else
1534 break;
1535 n = n * 16 + c;
1536 p++;
1538 c = n;
1539 goto add_char_nonext;
1540 case 'a':
1541 c = '\a';
1542 break;
1543 case 'b':
1544 c = '\b';
1545 break;
1546 case 'f':
1547 c = '\f';
1548 break;
1549 case 'n':
1550 c = '\n';
1551 break;
1552 case 'r':
1553 c = '\r';
1554 break;
1555 case 't':
1556 c = '\t';
1557 break;
1558 case 'v':
1559 c = '\v';
1560 break;
1561 case 'e':
1562 if (!gnu_ext)
1563 goto invalid_escape;
1564 c = 27;
1565 break;
1566 case '\'':
1567 case '\"':
1568 case '\\':
1569 case '?':
1570 break;
1571 default:
1572 invalid_escape:
1573 if (c >= '!' && c <= '~')
1574 warning("unknown escape sequence: \'\\%c\'", c);
1575 else
1576 warning("unknown escape sequence: \'\\x%x\'", c);
1577 break;
1580 p++;
1581 add_char_nonext:
1582 if (!is_long)
1583 cstr_ccat(outstr, c);
1584 else
1585 cstr_wccat(outstr, c);
1587 /* add a trailing '\0' */
1588 if (!is_long)
1589 cstr_ccat(outstr, '\0');
1590 else
1591 cstr_wccat(outstr, '\0');
1594 /* we use 64 bit numbers */
1595 #define BN_SIZE 2
1597 /* bn = (bn << shift) | or_val */
1598 void bn_lshift(unsigned int *bn, int shift, int or_val)
1600 int i;
1601 unsigned int v;
1602 for(i=0;i<BN_SIZE;i++) {
1603 v = bn[i];
1604 bn[i] = (v << shift) | or_val;
1605 or_val = v >> (32 - shift);
1609 void bn_zero(unsigned int *bn)
1611 int i;
1612 for(i=0;i<BN_SIZE;i++) {
1613 bn[i] = 0;
1617 /* parse number in null terminated string 'p' and return it in the
1618 current token */
1619 void parse_number(const char *p)
1621 int b, t, shift, frac_bits, s, exp_val, ch;
1622 char *q;
1623 unsigned int bn[BN_SIZE];
1624 double d;
1626 /* number */
1627 q = token_buf;
1628 ch = *p++;
1629 t = ch;
1630 ch = *p++;
1631 *q++ = t;
1632 b = 10;
1633 if (t == '.') {
1634 goto float_frac_parse;
1635 } else if (t == '0') {
1636 if (ch == 'x' || ch == 'X') {
1637 q--;
1638 ch = *p++;
1639 b = 16;
1640 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1641 q--;
1642 ch = *p++;
1643 b = 2;
1646 /* parse all digits. cannot check octal numbers at this stage
1647 because of floating point constants */
1648 while (1) {
1649 if (ch >= 'a' && ch <= 'f')
1650 t = ch - 'a' + 10;
1651 else if (ch >= 'A' && ch <= 'F')
1652 t = ch - 'A' + 10;
1653 else if (isnum(ch))
1654 t = ch - '0';
1655 else
1656 break;
1657 if (t >= b)
1658 break;
1659 if (q >= token_buf + STRING_MAX_SIZE) {
1660 num_too_long:
1661 error("number too long");
1663 *q++ = ch;
1664 ch = *p++;
1666 if (ch == '.' ||
1667 ((ch == 'e' || ch == 'E') && b == 10) ||
1668 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1669 if (b != 10) {
1670 /* NOTE: strtox should support that for hexa numbers, but
1671 non ISOC99 libcs do not support it, so we prefer to do
1672 it by hand */
1673 /* hexadecimal or binary floats */
1674 /* XXX: handle overflows */
1675 *q = '\0';
1676 if (b == 16)
1677 shift = 4;
1678 else
1679 shift = 2;
1680 bn_zero(bn);
1681 q = token_buf;
1682 while (1) {
1683 t = *q++;
1684 if (t == '\0') {
1685 break;
1686 } else if (t >= 'a') {
1687 t = t - 'a' + 10;
1688 } else if (t >= 'A') {
1689 t = t - 'A' + 10;
1690 } else {
1691 t = t - '0';
1693 bn_lshift(bn, shift, t);
1695 frac_bits = 0;
1696 if (ch == '.') {
1697 ch = *p++;
1698 while (1) {
1699 t = ch;
1700 if (t >= 'a' && t <= 'f') {
1701 t = t - 'a' + 10;
1702 } else if (t >= 'A' && t <= 'F') {
1703 t = t - 'A' + 10;
1704 } else if (t >= '0' && t <= '9') {
1705 t = t - '0';
1706 } else {
1707 break;
1709 if (t >= b)
1710 error("invalid digit");
1711 bn_lshift(bn, shift, t);
1712 frac_bits += shift;
1713 ch = *p++;
1716 if (ch != 'p' && ch != 'P')
1717 expect("exponent");
1718 ch = *p++;
1719 s = 1;
1720 exp_val = 0;
1721 if (ch == '+') {
1722 ch = *p++;
1723 } else if (ch == '-') {
1724 s = -1;
1725 ch = *p++;
1727 if (ch < '0' || ch > '9')
1728 expect("exponent digits");
1729 while (ch >= '0' && ch <= '9') {
1730 exp_val = exp_val * 10 + ch - '0';
1731 ch = *p++;
1733 exp_val = exp_val * s;
1735 /* now we can generate the number */
1736 /* XXX: should patch directly float number */
1737 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1738 d = ldexp(d, exp_val - frac_bits);
1739 t = toup(ch);
1740 if (t == 'F') {
1741 ch = *p++;
1742 tok = TOK_CFLOAT;
1743 /* float : should handle overflow */
1744 tokc.f = (float)d;
1745 } else if (t == 'L') {
1746 ch = *p++;
1747 #ifdef TCC_TARGET_PE
1748 tok = TOK_CDOUBLE;
1749 tokc.d = d;
1750 #else
1751 tok = TOK_CLDOUBLE;
1752 /* XXX: not large enough */
1753 tokc.ld = (long double)d;
1754 #endif
1755 } else {
1756 tok = TOK_CDOUBLE;
1757 tokc.d = d;
1759 } else {
1760 /* decimal floats */
1761 if (ch == '.') {
1762 if (q >= token_buf + STRING_MAX_SIZE)
1763 goto num_too_long;
1764 *q++ = ch;
1765 ch = *p++;
1766 float_frac_parse:
1767 while (ch >= '0' && ch <= '9') {
1768 if (q >= token_buf + STRING_MAX_SIZE)
1769 goto num_too_long;
1770 *q++ = ch;
1771 ch = *p++;
1774 if (ch == 'e' || ch == 'E') {
1775 if (q >= token_buf + STRING_MAX_SIZE)
1776 goto num_too_long;
1777 *q++ = ch;
1778 ch = *p++;
1779 if (ch == '-' || ch == '+') {
1780 if (q >= token_buf + STRING_MAX_SIZE)
1781 goto num_too_long;
1782 *q++ = ch;
1783 ch = *p++;
1785 if (ch < '0' || ch > '9')
1786 expect("exponent digits");
1787 while (ch >= '0' && ch <= '9') {
1788 if (q >= token_buf + STRING_MAX_SIZE)
1789 goto num_too_long;
1790 *q++ = ch;
1791 ch = *p++;
1794 *q = '\0';
1795 t = toup(ch);
1796 errno = 0;
1797 if (t == 'F') {
1798 ch = *p++;
1799 tok = TOK_CFLOAT;
1800 tokc.f = strtof(token_buf, NULL);
1801 } else if (t == 'L') {
1802 ch = *p++;
1803 #ifdef TCC_TARGET_PE
1804 tok = TOK_CDOUBLE;
1805 tokc.d = strtod(token_buf, NULL);
1806 #else
1807 tok = TOK_CLDOUBLE;
1808 tokc.ld = strtold(token_buf, NULL);
1809 #endif
1810 } else {
1811 tok = TOK_CDOUBLE;
1812 tokc.d = strtod(token_buf, NULL);
1815 } else {
1816 unsigned long long n, n1;
1817 int lcount, ucount;
1819 /* integer number */
1820 *q = '\0';
1821 q = token_buf;
1822 if (b == 10 && *q == '0') {
1823 b = 8;
1824 q++;
1826 n = 0;
1827 while(1) {
1828 t = *q++;
1829 /* no need for checks except for base 10 / 8 errors */
1830 if (t == '\0') {
1831 break;
1832 } else if (t >= 'a') {
1833 t = t - 'a' + 10;
1834 } else if (t >= 'A') {
1835 t = t - 'A' + 10;
1836 } else {
1837 t = t - '0';
1838 if (t >= b)
1839 error("invalid digit");
1841 n1 = n;
1842 n = n * b + t;
1843 /* detect overflow */
1844 /* XXX: this test is not reliable */
1845 if (n < n1)
1846 error("integer constant overflow");
1849 /* XXX: not exactly ANSI compliant */
1850 if ((n & 0xffffffff00000000LL) != 0) {
1851 if ((n >> 63) != 0)
1852 tok = TOK_CULLONG;
1853 else
1854 tok = TOK_CLLONG;
1855 } else if (n > 0x7fffffff) {
1856 tok = TOK_CUINT;
1857 } else {
1858 tok = TOK_CINT;
1860 lcount = 0;
1861 ucount = 0;
1862 for(;;) {
1863 t = toup(ch);
1864 if (t == 'L') {
1865 if (lcount >= 2)
1866 error("three 'l's in integer constant");
1867 lcount++;
1868 if (lcount == 2) {
1869 if (tok == TOK_CINT)
1870 tok = TOK_CLLONG;
1871 else if (tok == TOK_CUINT)
1872 tok = TOK_CULLONG;
1874 ch = *p++;
1875 } else if (t == 'U') {
1876 if (ucount >= 1)
1877 error("two 'u's in integer constant");
1878 ucount++;
1879 if (tok == TOK_CINT)
1880 tok = TOK_CUINT;
1881 else if (tok == TOK_CLLONG)
1882 tok = TOK_CULLONG;
1883 ch = *p++;
1884 } else {
1885 break;
1888 if (tok == TOK_CINT || tok == TOK_CUINT)
1889 tokc.ui = n;
1890 else
1891 tokc.ull = n;
1893 if (ch)
1894 error("invalid number\n");
1898 #define PARSE2(c1, tok1, c2, tok2) \
1899 case c1: \
1900 PEEKC(c, p); \
1901 if (c == c2) { \
1902 p++; \
1903 tok = tok2; \
1904 } else { \
1905 tok = tok1; \
1907 break;
1909 /* return next token without macro substitution */
1910 static inline void next_nomacro1(void)
1912 int t, c, is_long;
1913 TokenSym *ts;
1914 uint8_t *p, *p1;
1915 unsigned int h;
1917 p = file->buf_ptr;
1918 redo_no_start:
1919 c = *p;
1920 switch(c) {
1921 case ' ':
1922 case '\t':
1923 tok = c;
1924 p++;
1925 goto keep_tok_flags;
1926 case '\f':
1927 case '\v':
1928 case '\r':
1929 p++;
1930 goto redo_no_start;
1931 case '\\':
1932 /* first look if it is in fact an end of buffer */
1933 if (p >= file->buf_end) {
1934 file->buf_ptr = p;
1935 handle_eob();
1936 p = file->buf_ptr;
1937 if (p >= file->buf_end)
1938 goto parse_eof;
1939 else
1940 goto redo_no_start;
1941 } else {
1942 file->buf_ptr = p;
1943 ch = *p;
1944 handle_stray();
1945 p = file->buf_ptr;
1946 goto redo_no_start;
1948 parse_eof:
1950 TCCState *s1 = tcc_state;
1951 if ((parse_flags & PARSE_FLAG_LINEFEED)
1952 && !(tok_flags & TOK_FLAG_EOF)) {
1953 tok_flags |= TOK_FLAG_EOF;
1954 tok = TOK_LINEFEED;
1955 goto keep_tok_flags;
1956 } else if (s1->include_stack_ptr == s1->include_stack ||
1957 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
1958 /* no include left : end of file. */
1959 tok = TOK_EOF;
1960 } else {
1961 tok_flags &= ~TOK_FLAG_EOF;
1962 /* pop include file */
1964 /* test if previous '#endif' was after a #ifdef at
1965 start of file */
1966 if (tok_flags & TOK_FLAG_ENDIF) {
1967 #ifdef INC_DEBUG
1968 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
1969 #endif
1970 add_cached_include(s1, file->inc_type, file->inc_filename,
1971 file->ifndef_macro_saved);
1974 /* add end of include file debug info */
1975 if (tcc_state->do_debug) {
1976 put_stabd(N_EINCL, 0, 0);
1978 /* pop include stack */
1979 tcc_close(file);
1980 s1->include_stack_ptr--;
1981 file = *s1->include_stack_ptr;
1982 p = file->buf_ptr;
1983 goto redo_no_start;
1986 break;
1988 case '\n':
1989 file->line_num++;
1990 tok_flags |= TOK_FLAG_BOL;
1991 p++;
1992 maybe_newline:
1993 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
1994 goto redo_no_start;
1995 tok = TOK_LINEFEED;
1996 goto keep_tok_flags;
1998 case '#':
1999 /* XXX: simplify */
2000 PEEKC(c, p);
2001 if ((tok_flags & TOK_FLAG_BOL) &&
2002 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2003 file->buf_ptr = p;
2004 preprocess(tok_flags & TOK_FLAG_BOF);
2005 p = file->buf_ptr;
2006 goto maybe_newline;
2007 } else {
2008 if (c == '#') {
2009 p++;
2010 tok = TOK_TWOSHARPS;
2011 } else {
2012 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2013 p = parse_line_comment(p - 1);
2014 goto redo_no_start;
2015 } else {
2016 tok = '#';
2020 break;
2022 case 'a': case 'b': case 'c': case 'd':
2023 case 'e': case 'f': case 'g': case 'h':
2024 case 'i': case 'j': case 'k': case 'l':
2025 case 'm': case 'n': case 'o': case 'p':
2026 case 'q': case 'r': case 's': case 't':
2027 case 'u': case 'v': case 'w': case 'x':
2028 case 'y': case 'z':
2029 case 'A': case 'B': case 'C': case 'D':
2030 case 'E': case 'F': case 'G': case 'H':
2031 case 'I': case 'J': case 'K':
2032 case 'M': case 'N': case 'O': case 'P':
2033 case 'Q': case 'R': case 'S': case 'T':
2034 case 'U': case 'V': case 'W': case 'X':
2035 case 'Y': case 'Z':
2036 case '_':
2037 parse_ident_fast:
2038 p1 = p;
2039 h = TOK_HASH_INIT;
2040 h = TOK_HASH_FUNC(h, c);
2041 p++;
2042 for(;;) {
2043 c = *p;
2044 if (!isidnum_table[c-CH_EOF])
2045 break;
2046 h = TOK_HASH_FUNC(h, c);
2047 p++;
2049 if (c != '\\') {
2050 TokenSym **pts;
2051 int len;
2053 /* fast case : no stray found, so we have the full token
2054 and we have already hashed it */
2055 len = p - p1;
2056 h &= (TOK_HASH_SIZE - 1);
2057 pts = &hash_ident[h];
2058 for(;;) {
2059 ts = *pts;
2060 if (!ts)
2061 break;
2062 if (ts->len == len && !memcmp(ts->str, p1, len))
2063 goto token_found;
2064 pts = &(ts->hash_next);
2066 ts = tok_alloc_new(pts, p1, len);
2067 token_found: ;
2068 } else {
2069 /* slower case */
2070 cstr_reset(&tokcstr);
2072 while (p1 < p) {
2073 cstr_ccat(&tokcstr, *p1);
2074 p1++;
2076 p--;
2077 PEEKC(c, p);
2078 parse_ident_slow:
2079 while (isidnum_table[c-CH_EOF]) {
2080 cstr_ccat(&tokcstr, c);
2081 PEEKC(c, p);
2083 ts = tok_alloc(tokcstr.data, tokcstr.size);
2085 tok = ts->tok;
2086 break;
2087 case 'L':
2088 t = p[1];
2089 if (t != '\\' && t != '\'' && t != '\"') {
2090 /* fast case */
2091 goto parse_ident_fast;
2092 } else {
2093 PEEKC(c, p);
2094 if (c == '\'' || c == '\"') {
2095 is_long = 1;
2096 goto str_const;
2097 } else {
2098 cstr_reset(&tokcstr);
2099 cstr_ccat(&tokcstr, 'L');
2100 goto parse_ident_slow;
2103 break;
2104 case '0': case '1': case '2': case '3':
2105 case '4': case '5': case '6': case '7':
2106 case '8': case '9':
2108 cstr_reset(&tokcstr);
2109 /* after the first digit, accept digits, alpha, '.' or sign if
2110 prefixed by 'eEpP' */
2111 parse_num:
2112 for(;;) {
2113 t = c;
2114 cstr_ccat(&tokcstr, c);
2115 PEEKC(c, p);
2116 if (!(isnum(c) || isid(c) || c == '.' ||
2117 ((c == '+' || c == '-') &&
2118 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2119 break;
2121 /* We add a trailing '\0' to ease parsing */
2122 cstr_ccat(&tokcstr, '\0');
2123 tokc.cstr = &tokcstr;
2124 tok = TOK_PPNUM;
2125 break;
2126 case '.':
2127 /* special dot handling because it can also start a number */
2128 PEEKC(c, p);
2129 if (isnum(c)) {
2130 cstr_reset(&tokcstr);
2131 cstr_ccat(&tokcstr, '.');
2132 goto parse_num;
2133 } else if (c == '.') {
2134 PEEKC(c, p);
2135 if (c != '.')
2136 expect("'.'");
2137 PEEKC(c, p);
2138 tok = TOK_DOTS;
2139 } else {
2140 tok = '.';
2142 break;
2143 case '\'':
2144 case '\"':
2145 is_long = 0;
2146 str_const:
2148 CString str;
2149 int sep;
2151 sep = c;
2153 /* parse the string */
2154 cstr_new(&str);
2155 p = parse_pp_string(p, sep, &str);
2156 cstr_ccat(&str, '\0');
2158 /* eval the escape (should be done as TOK_PPNUM) */
2159 cstr_reset(&tokcstr);
2160 parse_escape_string(&tokcstr, str.data, is_long);
2161 cstr_free(&str);
2163 if (sep == '\'') {
2164 int char_size;
2165 /* XXX: make it portable */
2166 if (!is_long)
2167 char_size = 1;
2168 else
2169 char_size = sizeof(nwchar_t);
2170 if (tokcstr.size <= char_size)
2171 error("empty character constant");
2172 if (tokcstr.size > 2 * char_size)
2173 warning("multi-character character constant");
2174 if (!is_long) {
2175 tokc.i = *(int8_t *)tokcstr.data;
2176 tok = TOK_CCHAR;
2177 } else {
2178 tokc.i = *(nwchar_t *)tokcstr.data;
2179 tok = TOK_LCHAR;
2181 } else {
2182 tokc.cstr = &tokcstr;
2183 if (!is_long)
2184 tok = TOK_STR;
2185 else
2186 tok = TOK_LSTR;
2189 break;
2191 case '<':
2192 PEEKC(c, p);
2193 if (c == '=') {
2194 p++;
2195 tok = TOK_LE;
2196 } else if (c == '<') {
2197 PEEKC(c, p);
2198 if (c == '=') {
2199 p++;
2200 tok = TOK_A_SHL;
2201 } else {
2202 tok = TOK_SHL;
2204 } else {
2205 tok = TOK_LT;
2207 break;
2209 case '>':
2210 PEEKC(c, p);
2211 if (c == '=') {
2212 p++;
2213 tok = TOK_GE;
2214 } else if (c == '>') {
2215 PEEKC(c, p);
2216 if (c == '=') {
2217 p++;
2218 tok = TOK_A_SAR;
2219 } else {
2220 tok = TOK_SAR;
2222 } else {
2223 tok = TOK_GT;
2225 break;
2227 case '&':
2228 PEEKC(c, p);
2229 if (c == '&') {
2230 p++;
2231 tok = TOK_LAND;
2232 } else if (c == '=') {
2233 p++;
2234 tok = TOK_A_AND;
2235 } else {
2236 tok = '&';
2238 break;
2240 case '|':
2241 PEEKC(c, p);
2242 if (c == '|') {
2243 p++;
2244 tok = TOK_LOR;
2245 } else if (c == '=') {
2246 p++;
2247 tok = TOK_A_OR;
2248 } else {
2249 tok = '|';
2251 break;
2253 case '+':
2254 PEEKC(c, p);
2255 if (c == '+') {
2256 p++;
2257 tok = TOK_INC;
2258 } else if (c == '=') {
2259 p++;
2260 tok = TOK_A_ADD;
2261 } else {
2262 tok = '+';
2264 break;
2266 case '-':
2267 PEEKC(c, p);
2268 if (c == '-') {
2269 p++;
2270 tok = TOK_DEC;
2271 } else if (c == '=') {
2272 p++;
2273 tok = TOK_A_SUB;
2274 } else if (c == '>') {
2275 p++;
2276 tok = TOK_ARROW;
2277 } else {
2278 tok = '-';
2280 break;
2282 PARSE2('!', '!', '=', TOK_NE)
2283 PARSE2('=', '=', '=', TOK_EQ)
2284 PARSE2('*', '*', '=', TOK_A_MUL)
2285 PARSE2('%', '%', '=', TOK_A_MOD)
2286 PARSE2('^', '^', '=', TOK_A_XOR)
2288 /* comments or operator */
2289 case '/':
2290 PEEKC(c, p);
2291 if (c == '*') {
2292 p = parse_comment(p);
2293 goto redo_no_start;
2294 } else if (c == '/') {
2295 p = parse_line_comment(p);
2296 goto redo_no_start;
2297 } else if (c == '=') {
2298 p++;
2299 tok = TOK_A_DIV;
2300 } else {
2301 tok = '/';
2303 break;
2305 /* simple tokens */
2306 case '(':
2307 case ')':
2308 case '[':
2309 case ']':
2310 case '{':
2311 case '}':
2312 case ',':
2313 case ';':
2314 case ':':
2315 case '?':
2316 case '~':
2317 case '$': /* only used in assembler */
2318 case '@': /* dito */
2319 tok = c;
2320 p++;
2321 break;
2322 default:
2323 error("unrecognized character \\x%02x", c);
2324 break;
2326 tok_flags = 0;
2327 keep_tok_flags:
2328 file->buf_ptr = p;
2329 #if defined(PARSE_DEBUG)
2330 printf("token = %s\n", get_tok_str(tok, &tokc));
2331 #endif
2334 /* return next token without macro substitution. Can read input from
2335 macro_ptr buffer */
2336 static void next_nomacro_spc(void)
2338 if (macro_ptr) {
2339 redo:
2340 tok = *macro_ptr;
2341 if (tok) {
2342 TOK_GET(tok, macro_ptr, tokc);
2343 if (tok == TOK_LINENUM) {
2344 file->line_num = tokc.i;
2345 goto redo;
2348 } else {
2349 next_nomacro1();
2353 static void next_nomacro(void)
2355 do {
2356 next_nomacro_spc();
2357 } while (is_space(tok));
2360 /* substitute args in macro_str and return allocated string */
2361 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
2363 int *st, last_tok, t, spc;
2364 Sym *s;
2365 CValue cval;
2366 TokenString str;
2367 CString cstr;
2369 tok_str_new(&str);
2370 last_tok = 0;
2371 while(1) {
2372 TOK_GET(t, macro_str, cval);
2373 if (!t)
2374 break;
2375 if (t == '#') {
2376 /* stringize */
2377 TOK_GET(t, macro_str, cval);
2378 if (!t)
2379 break;
2380 s = sym_find2(args, t);
2381 if (s) {
2382 cstr_new(&cstr);
2383 st = s->d;
2384 spc = 0;
2385 while (*st) {
2386 TOK_GET(t, st, cval);
2387 if (!check_space(t, &spc))
2388 cstr_cat(&cstr, get_tok_str(t, &cval));
2390 cstr.size -= spc;
2391 cstr_ccat(&cstr, '\0');
2392 #ifdef PP_DEBUG
2393 printf("stringize: %s\n", (char *)cstr.data);
2394 #endif
2395 /* add string */
2396 cval.cstr = &cstr;
2397 tok_str_add2(&str, TOK_STR, &cval);
2398 cstr_free(&cstr);
2399 } else {
2400 tok_str_add2(&str, t, &cval);
2402 } else if (t >= TOK_IDENT) {
2403 s = sym_find2(args, t);
2404 if (s) {
2405 st = s->d;
2406 /* if '##' is present before or after, no arg substitution */
2407 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2408 /* special case for var arg macros : ## eats the
2409 ',' if empty VA_ARGS variable. */
2410 /* XXX: test of the ',' is not 100%
2411 reliable. should fix it to avoid security
2412 problems */
2413 if (gnu_ext && s->type.t &&
2414 last_tok == TOK_TWOSHARPS &&
2415 str.len >= 2 && str.str[str.len - 2] == ',') {
2416 if (*st == 0) {
2417 /* suppress ',' '##' */
2418 str.len -= 2;
2419 } else {
2420 /* suppress '##' and add variable */
2421 str.len--;
2422 goto add_var;
2424 } else {
2425 int t1;
2426 add_var:
2427 for(;;) {
2428 TOK_GET(t1, st, cval);
2429 if (!t1)
2430 break;
2431 tok_str_add2(&str, t1, &cval);
2434 } else {
2435 /* NOTE: the stream cannot be read when macro
2436 substituing an argument */
2437 macro_subst(&str, nested_list, st, NULL);
2439 } else {
2440 tok_str_add(&str, t);
2442 } else {
2443 tok_str_add2(&str, t, &cval);
2445 last_tok = t;
2447 tok_str_add(&str, 0);
2448 return str.str;
2451 static char const ab_month_name[12][4] =
2453 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2454 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2457 /* do macro substitution of current token with macro 's' and add
2458 result to (tok_str,tok_len). 'nested_list' is the list of all
2459 macros we got inside to avoid recursing. Return non zero if no
2460 substitution needs to be done */
2461 static int macro_subst_tok(TokenString *tok_str,
2462 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2464 Sym *args, *sa, *sa1;
2465 int mstr_allocated, parlevel, *mstr, t, t1, *p, spc;
2466 TokenString str;
2467 char *cstrval;
2468 CValue cval;
2469 CString cstr;
2470 char buf[32];
2472 /* if symbol is a macro, prepare substitution */
2473 /* special macros */
2474 if (tok == TOK___LINE__) {
2475 snprintf(buf, sizeof(buf), "%d", file->line_num);
2476 cstrval = buf;
2477 t1 = TOK_PPNUM;
2478 goto add_cstr1;
2479 } else if (tok == TOK___FILE__) {
2480 cstrval = file->filename;
2481 goto add_cstr;
2482 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2483 time_t ti;
2484 struct tm *tm;
2486 time(&ti);
2487 tm = localtime(&ti);
2488 if (tok == TOK___DATE__) {
2489 snprintf(buf, sizeof(buf), "%s %2d %d",
2490 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2491 } else {
2492 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2493 tm->tm_hour, tm->tm_min, tm->tm_sec);
2495 cstrval = buf;
2496 add_cstr:
2497 t1 = TOK_STR;
2498 add_cstr1:
2499 cstr_new(&cstr);
2500 cstr_cat(&cstr, cstrval);
2501 cstr_ccat(&cstr, '\0');
2502 cval.cstr = &cstr;
2503 tok_str_add2(tok_str, t1, &cval);
2504 cstr_free(&cstr);
2505 } else {
2506 mstr = s->d;
2507 mstr_allocated = 0;
2508 if (s->type.t == MACRO_FUNC) {
2509 /* NOTE: we do not use next_nomacro to avoid eating the
2510 next token. XXX: find better solution */
2511 redo:
2512 if (macro_ptr) {
2513 p = macro_ptr;
2514 while (is_space(t = *p) || TOK_LINEFEED == t)
2515 ++p;
2516 if (t == 0 && can_read_stream) {
2517 /* end of macro stream: we must look at the token
2518 after in the file */
2519 struct macro_level *ml = *can_read_stream;
2520 macro_ptr = NULL;
2521 if (ml)
2523 macro_ptr = ml->p;
2524 ml->p = NULL;
2525 *can_read_stream = ml -> prev;
2527 goto redo;
2529 } else {
2530 /* XXX: incorrect with comments */
2531 ch = file->buf_ptr[0];
2532 while (is_space(ch) || ch == '\n')
2533 cinp();
2534 t = ch;
2536 if (t != '(') /* no macro subst */
2537 return -1;
2539 /* argument macro */
2540 next_nomacro();
2541 next_nomacro();
2542 args = NULL;
2543 sa = s->next;
2544 /* NOTE: empty args are allowed, except if no args */
2545 for(;;) {
2546 /* handle '()' case */
2547 if (!args && !sa && tok == ')')
2548 break;
2549 if (!sa)
2550 error("macro '%s' used with too many args",
2551 get_tok_str(s->v, 0));
2552 tok_str_new(&str);
2553 parlevel = spc = 0;
2554 /* NOTE: non zero sa->t indicates VA_ARGS */
2555 while ((parlevel > 0 ||
2556 (tok != ')' &&
2557 (tok != ',' || sa->type.t))) &&
2558 tok != -1) {
2559 if (tok == '(')
2560 parlevel++;
2561 else if (tok == ')')
2562 parlevel--;
2563 if (tok == TOK_LINEFEED)
2564 tok = ' ';
2565 if (!check_space(tok, &spc))
2566 tok_str_add2(&str, tok, &tokc);
2567 next_nomacro_spc();
2569 str.len -= spc;
2570 tok_str_add(&str, 0);
2571 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2572 sa1->d = str.str;
2573 sa = sa->next;
2574 if (tok == ')') {
2575 /* special case for gcc var args: add an empty
2576 var arg argument if it is omitted */
2577 if (sa && sa->type.t && gnu_ext)
2578 continue;
2579 else
2580 break;
2582 if (tok != ',')
2583 expect(",");
2584 next_nomacro();
2586 if (sa) {
2587 error("macro '%s' used with too few args",
2588 get_tok_str(s->v, 0));
2591 /* now subst each arg */
2592 mstr = macro_arg_subst(nested_list, mstr, args);
2593 /* free memory */
2594 sa = args;
2595 while (sa) {
2596 sa1 = sa->prev;
2597 tok_str_free(sa->d);
2598 sym_free(sa);
2599 sa = sa1;
2601 mstr_allocated = 1;
2603 sym_push2(nested_list, s->v, 0, 0);
2604 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2605 /* pop nested defined symbol */
2606 sa1 = *nested_list;
2607 *nested_list = sa1->prev;
2608 sym_free(sa1);
2609 if (mstr_allocated)
2610 tok_str_free(mstr);
2612 return 0;
2615 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2616 return the resulting string (which must be freed). */
2617 static inline int *macro_twosharps(const int *macro_str)
2619 const int *ptr;
2620 int t;
2621 CValue cval;
2622 TokenString macro_str1;
2623 CString cstr;
2624 char *p;
2625 int n;
2627 /* we search the first '##' */
2628 for(ptr = macro_str;;) {
2629 TOK_GET(t, ptr, cval);
2630 if (t == TOK_TWOSHARPS)
2631 break;
2632 /* nothing more to do if end of string */
2633 if (t == 0)
2634 return NULL;
2637 /* we saw '##', so we need more processing to handle it */
2638 tok_str_new(&macro_str1);
2639 for(ptr = macro_str;;) {
2640 TOK_GET(tok, ptr, tokc);
2641 if (tok == 0)
2642 break;
2643 if (tok == TOK_TWOSHARPS)
2644 continue;
2645 while (*ptr == TOK_TWOSHARPS) {
2646 t = *++ptr;
2647 if (t && t != TOK_TWOSHARPS) {
2648 TOK_GET(t, ptr, cval);
2650 /* We concatenate the two tokens */
2651 cstr_new(&cstr);
2652 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2653 n = cstr.size;
2654 cstr_cat(&cstr, get_tok_str(t, &cval));
2655 cstr_ccat(&cstr, '\0');
2657 p = file->buf_ptr;
2658 file->buf_ptr = cstr.data;
2659 for (;;) {
2660 next_nomacro1();
2661 if (0 == *file->buf_ptr)
2662 break;
2663 tok_str_add2(&macro_str1, tok, &tokc);
2665 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2666 n, cstr.data, (char*)cstr.data + n);
2668 file->buf_ptr = p;
2669 cstr_reset(&cstr);
2672 tok_str_add2(&macro_str1, tok, &tokc);
2674 tok_str_add(&macro_str1, 0);
2675 return macro_str1.str;
2679 /* do macro substitution of macro_str and add result to
2680 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2681 inside to avoid recursing. */
2682 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2683 const int *macro_str, struct macro_level ** can_read_stream)
2685 Sym *s;
2686 int *macro_str1;
2687 const int *ptr;
2688 int t, ret, spc;
2689 CValue cval;
2690 struct macro_level ml;
2692 /* first scan for '##' operator handling */
2693 ptr = macro_str;
2694 macro_str1 = macro_twosharps(ptr);
2695 if (macro_str1)
2696 ptr = macro_str1;
2697 spc = 0;
2698 while (1) {
2699 /* NOTE: ptr == NULL can only happen if tokens are read from
2700 file stream due to a macro function call */
2701 if (ptr == NULL)
2702 break;
2703 TOK_GET(t, ptr, cval);
2704 if (t == 0)
2705 break;
2706 s = define_find(t);
2707 if (s != NULL) {
2708 /* if nested substitution, do nothing */
2709 if (sym_find2(*nested_list, t))
2710 goto no_subst;
2711 ml.p = macro_ptr;
2712 if (can_read_stream)
2713 ml.prev = *can_read_stream, *can_read_stream = &ml;
2714 macro_ptr = (int *)ptr;
2715 tok = t;
2716 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
2717 ptr = (int *)macro_ptr;
2718 macro_ptr = ml.p;
2719 if (can_read_stream && *can_read_stream == &ml)
2720 *can_read_stream = ml.prev;
2721 if (ret != 0)
2722 goto no_subst;
2723 } else {
2724 no_subst:
2725 if (!check_space(t, &spc))
2726 tok_str_add2(tok_str, t, &cval);
2729 if (macro_str1)
2730 tok_str_free(macro_str1);
2733 /* return next token with macro substitution */
2734 static void next(void)
2736 Sym *nested_list, *s;
2737 TokenString str;
2738 struct macro_level *ml;
2740 redo:
2741 if (parse_flags & PARSE_FLAG_SPACES)
2742 next_nomacro_spc();
2743 else
2744 next_nomacro();
2745 if (!macro_ptr) {
2746 /* if not reading from macro substituted string, then try
2747 to substitute macros */
2748 if (tok >= TOK_IDENT &&
2749 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2750 s = define_find(tok);
2751 if (s) {
2752 /* we have a macro: we try to substitute */
2753 tok_str_new(&str);
2754 nested_list = NULL;
2755 ml = NULL;
2756 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
2757 /* substitution done, NOTE: maybe empty */
2758 tok_str_add(&str, 0);
2759 macro_ptr = str.str;
2760 macro_ptr_allocated = str.str;
2761 goto redo;
2765 } else {
2766 if (tok == 0) {
2767 /* end of macro or end of unget buffer */
2768 if (unget_buffer_enabled) {
2769 macro_ptr = unget_saved_macro_ptr;
2770 unget_buffer_enabled = 0;
2771 } else {
2772 /* end of macro string: free it */
2773 tok_str_free(macro_ptr_allocated);
2774 macro_ptr_allocated = NULL;
2775 macro_ptr = NULL;
2777 goto redo;
2781 /* convert preprocessor tokens into C tokens */
2782 if (tok == TOK_PPNUM &&
2783 (parse_flags & PARSE_FLAG_TOK_NUM)) {
2784 parse_number((char *)tokc.cstr->data);
2788 /* push back current token and set current token to 'last_tok'. Only
2789 identifier case handled for labels. */
2790 static inline void unget_tok(int last_tok)
2792 int i, n;
2793 int *q;
2794 unget_saved_macro_ptr = macro_ptr;
2795 unget_buffer_enabled = 1;
2796 q = unget_saved_buffer;
2797 macro_ptr = q;
2798 *q++ = tok;
2799 n = tok_ext_size(tok) - 1;
2800 for(i=0;i<n;i++)
2801 *q++ = tokc.tab[i];
2802 *q = 0; /* end of token string */
2803 tok = last_tok;
2807 /* better than nothing, but needs extension to handle '-E' option
2808 correctly too */
2809 static void preprocess_init(TCCState *s1)
2811 s1->include_stack_ptr = s1->include_stack;
2812 /* XXX: move that before to avoid having to initialize
2813 file->ifdef_stack_ptr ? */
2814 s1->ifdef_stack_ptr = s1->ifdef_stack;
2815 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2817 /* XXX: not ANSI compliant: bound checking says error */
2818 vtop = vstack - 1;
2819 s1->pack_stack[0] = 0;
2820 s1->pack_stack_ptr = s1->pack_stack;
2823 void preprocess_new()
2825 int i, c;
2826 const char *p, *r;
2827 TokenSym *ts;
2829 /* init isid table */
2830 for(i=CH_EOF;i<256;i++)
2831 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
2833 /* add all tokens */
2834 table_ident = NULL;
2835 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
2837 tok_ident = TOK_IDENT;
2838 p = tcc_keywords;
2839 while (*p) {
2840 r = p;
2841 for(;;) {
2842 c = *r++;
2843 if (c == '\0')
2844 break;
2846 ts = tok_alloc(p, r - p - 1);
2847 p = r;
2851 /* Preprocess the current file */
2852 static int tcc_preprocess(TCCState *s1)
2854 Sym *define_start;
2855 BufferedFile *file_ref, **iptr, **iptr_new;
2856 int token_seen, line_ref, d;
2857 const char *s;
2859 preprocess_init(s1);
2860 define_start = define_stack;
2861 ch = file->buf_ptr[0];
2862 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
2863 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
2864 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
2865 token_seen = 0;
2866 line_ref = 0;
2867 file_ref = NULL;
2869 iptr = s1->include_stack_ptr;
2870 for (;;) {
2871 next();
2872 if (tok == TOK_EOF) {
2873 break;
2874 } else if (file != file_ref) {
2875 goto print_line;
2876 } else if (tok == TOK_LINEFEED) {
2877 if (!token_seen)
2878 continue;
2879 ++line_ref;
2880 token_seen = 0;
2881 } else if (!token_seen) {
2882 d = file->line_num - line_ref;
2883 if (file != file_ref || d < 0 || d >= 8) {
2884 print_line:
2885 iptr_new = s1->include_stack_ptr;
2886 s = iptr_new > iptr ? " 1"
2887 : iptr_new < iptr ? " 2"
2888 : iptr_new > s1->include_stack ? " 3"
2889 : ""
2891 iptr = iptr_new;
2892 fprintf(s1->outfile, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
2893 } else {
2894 while (d)
2895 fputs("\n", s1->outfile), --d;
2897 line_ref = (file_ref = file)->line_num;
2898 token_seen = tok != TOK_LINEFEED;
2899 if (!token_seen)
2900 continue;
2902 fputs(get_tok_str(tok, &tokc), s1->outfile);
2904 free_defines(define_start);
2905 return 0;