Checkpoint. More refactoring.
[tinycc/k1w1.git] / tccpp.c
blob6a15ab347b083fe240311c3629181aa8a0b22011
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
20 #include "tcc.h"
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_spc(void);
43 static void macro_subst(TokenString *tok_str, Sym **nested_list,
44 const int *macro_str, struct macro_level **can_read_stream);
47 /* allocate a new token */
48 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
50 TokenSym *ts, **ptable;
51 int i;
53 if (tok_ident >= SYM_FIRST_ANOM)
54 error("memory full");
56 /* expand token table if needed */
57 i = tok_ident - TOK_IDENT;
58 if ((i % TOK_ALLOC_INCR) == 0) {
59 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
60 if (!ptable)
61 error("memory full");
62 table_ident = ptable;
65 ts = tcc_malloc(sizeof(TokenSym) + len);
66 table_ident[i] = ts;
67 ts->tok = tok_ident++;
68 ts->sym_define = NULL;
69 ts->sym_label = NULL;
70 ts->sym_struct = NULL;
71 ts->sym_identifier = NULL;
72 ts->len = len;
73 ts->hash_next = NULL;
74 memcpy(ts->str, str, len);
75 ts->str[len] = '\0';
76 *pts = ts;
77 return ts;
80 #define TOK_HASH_INIT 1
81 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
83 /* find a token and add it if not found */
84 TokenSym *tok_alloc(const char *str, int len)
86 TokenSym *ts, **pts;
87 int i;
88 unsigned int h;
90 h = TOK_HASH_INIT;
91 for(i=0;i<len;i++)
92 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
93 h &= (TOK_HASH_SIZE - 1);
95 pts = &hash_ident[h];
96 for(;;) {
97 ts = *pts;
98 if (!ts)
99 break;
100 if (ts->len == len && !memcmp(ts->str, str, len))
101 return ts;
102 pts = &(ts->hash_next);
104 return tok_alloc_new(pts, str, len);
107 /* XXX: buffer overflow */
108 /* XXX: float tokens */
109 char *get_tok_str(int v, CValue *cv)
111 static char buf[STRING_MAX_SIZE + 1];
112 static CString cstr_buf;
113 CString *cstr;
114 char *p;
115 int i, len;
117 /* NOTE: to go faster, we give a fixed buffer for small strings */
118 cstr_reset(&cstr_buf);
119 cstr_buf.data = buf;
120 cstr_buf.size_allocated = sizeof(buf);
121 p = buf;
123 switch(v) {
124 case TOK_CINT:
125 case TOK_CUINT:
126 /* XXX: not quite exact, but only useful for testing */
127 sprintf(p, "%u", cv->ui);
128 break;
129 case TOK_CLLONG:
130 case TOK_CULLONG:
131 /* XXX: not quite exact, but only useful for testing */
132 #ifdef _WIN32
133 sprintf(p, "%u", (unsigned)cv->ull);
134 #else
135 sprintf(p, "%Lu", cv->ull);
136 #endif
137 break;
138 case TOK_LCHAR:
139 cstr_ccat(&cstr_buf, 'L');
140 case TOK_CCHAR:
141 cstr_ccat(&cstr_buf, '\'');
142 add_char(&cstr_buf, cv->i);
143 cstr_ccat(&cstr_buf, '\'');
144 cstr_ccat(&cstr_buf, '\0');
145 break;
146 case TOK_PPNUM:
147 cstr = cv->cstr;
148 len = cstr->size - 1;
149 for(i=0;i<len;i++)
150 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
151 cstr_ccat(&cstr_buf, '\0');
152 break;
153 case TOK_LSTR:
154 cstr_ccat(&cstr_buf, 'L');
155 case TOK_STR:
156 cstr = cv->cstr;
157 cstr_ccat(&cstr_buf, '\"');
158 if (v == TOK_STR) {
159 len = cstr->size - 1;
160 for(i=0;i<len;i++)
161 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
162 } else {
163 len = (cstr->size / sizeof(nwchar_t)) - 1;
164 for(i=0;i<len;i++)
165 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
167 cstr_ccat(&cstr_buf, '\"');
168 cstr_ccat(&cstr_buf, '\0');
169 break;
170 case TOK_LT:
171 v = '<';
172 goto addv;
173 case TOK_GT:
174 v = '>';
175 goto addv;
176 case TOK_DOTS:
177 return strcpy(p, "...");
178 case TOK_A_SHL:
179 return strcpy(p, "<<=");
180 case TOK_A_SAR:
181 return strcpy(p, ">>=");
182 default:
183 if (v < TOK_IDENT) {
184 /* search in two bytes table */
185 const unsigned char *q = tok_two_chars;
186 while (*q) {
187 if (q[2] == v) {
188 *p++ = q[0];
189 *p++ = q[1];
190 *p = '\0';
191 return buf;
193 q += 3;
195 addv:
196 *p++ = v;
197 *p = '\0';
198 } else if (v < tok_ident) {
199 return table_ident[v - TOK_IDENT]->str;
200 } else if (v >= SYM_FIRST_ANOM) {
201 /* special name for anonymous symbol */
202 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
203 } else {
204 /* should never happen */
205 return NULL;
207 break;
209 return cstr_buf.data;
212 /* fill input buffer and peek next char */
213 static int tcc_peekc_slow(BufferedFile *bf)
215 int len;
216 /* only tries to read if really end of buffer */
217 if (bf->buf_ptr >= bf->buf_end) {
218 if (bf->fd != -1) {
219 #if defined(PARSE_DEBUG)
220 len = 8;
221 #else
222 len = IO_BUF_SIZE;
223 #endif
224 len = read(bf->fd, bf->buffer, len);
225 if (len < 0)
226 len = 0;
227 } else {
228 len = 0;
230 total_bytes += len;
231 bf->buf_ptr = bf->buffer;
232 bf->buf_end = bf->buffer + len;
233 *bf->buf_end = CH_EOB;
235 if (bf->buf_ptr < bf->buf_end) {
236 return bf->buf_ptr[0];
237 } else {
238 bf->buf_ptr = bf->buf_end;
239 return CH_EOF;
243 /* return the current character, handling end of block if necessary
244 (but not stray) */
245 int handle_eob(void)
247 return tcc_peekc_slow(file);
250 /* handle '\[\r]\n' */
251 static int handle_stray_noerror(void)
253 while (ch == '\\') {
254 inp();
255 if (ch == '\n') {
256 file->line_num++;
257 inp();
258 } else if (ch == '\r') {
259 inp();
260 if (ch != '\n')
261 goto fail;
262 file->line_num++;
263 inp();
264 } else {
265 fail:
266 return 1;
269 return 0;
272 static void handle_stray(void)
274 if (handle_stray_noerror())
275 error("stray '\\' in program");
278 /* skip the stray and handle the \\n case. Output an error if
279 incorrect char after the stray */
280 static int handle_stray1(uint8_t *p)
282 int c;
284 if (p >= file->buf_end) {
285 file->buf_ptr = p;
286 c = handle_eob();
287 p = file->buf_ptr;
288 if (c == '\\')
289 goto parse_stray;
290 } else {
291 parse_stray:
292 file->buf_ptr = p;
293 ch = *p;
294 handle_stray();
295 p = file->buf_ptr;
296 c = *p;
298 return c;
301 /* handle just the EOB case, but not stray */
302 #define PEEKC_EOB(c, p)\
304 p++;\
305 c = *p;\
306 if (c == '\\') {\
307 file->buf_ptr = p;\
308 c = handle_eob();\
309 p = file->buf_ptr;\
313 /* handle the complicated stray case */
314 #define PEEKC(c, p)\
316 p++;\
317 c = *p;\
318 if (c == '\\') {\
319 c = handle_stray1(p);\
320 p = file->buf_ptr;\
324 /* input with '\[\r]\n' handling. Note that this function cannot
325 handle other characters after '\', so you cannot call it inside
326 strings or comments */
327 void minp(void)
329 inp();
330 if (ch == '\\')
331 handle_stray();
335 /* single line C++ comments */
336 static uint8_t *parse_line_comment(uint8_t *p)
338 int c;
340 p++;
341 for(;;) {
342 c = *p;
343 redo:
344 if (c == '\n' || c == CH_EOF) {
345 break;
346 } else if (c == '\\') {
347 file->buf_ptr = p;
348 c = handle_eob();
349 p = file->buf_ptr;
350 if (c == '\\') {
351 PEEKC_EOB(c, p);
352 if (c == '\n') {
353 file->line_num++;
354 PEEKC_EOB(c, p);
355 } else if (c == '\r') {
356 PEEKC_EOB(c, p);
357 if (c == '\n') {
358 file->line_num++;
359 PEEKC_EOB(c, p);
362 } else {
363 goto redo;
365 } else {
366 p++;
369 return p;
372 /* C comments */
373 uint8_t *parse_comment(uint8_t *p)
375 int c;
377 p++;
378 for(;;) {
379 /* fast skip loop */
380 for(;;) {
381 c = *p;
382 if (c == '\n' || c == '*' || c == '\\')
383 break;
384 p++;
385 c = *p;
386 if (c == '\n' || c == '*' || c == '\\')
387 break;
388 p++;
390 /* now we can handle all the cases */
391 if (c == '\n') {
392 file->line_num++;
393 p++;
394 } else if (c == '*') {
395 p++;
396 for(;;) {
397 c = *p;
398 if (c == '*') {
399 p++;
400 } else if (c == '/') {
401 goto end_of_comment;
402 } else if (c == '\\') {
403 file->buf_ptr = p;
404 c = handle_eob();
405 p = file->buf_ptr;
406 if (c == '\\') {
407 /* skip '\[\r]\n', otherwise just skip the stray */
408 while (c == '\\') {
409 PEEKC_EOB(c, p);
410 if (c == '\n') {
411 file->line_num++;
412 PEEKC_EOB(c, p);
413 } else if (c == '\r') {
414 PEEKC_EOB(c, p);
415 if (c == '\n') {
416 file->line_num++;
417 PEEKC_EOB(c, p);
419 } else {
420 goto after_star;
424 } else {
425 break;
428 after_star: ;
429 } else {
430 /* stray, eob or eof */
431 file->buf_ptr = p;
432 c = handle_eob();
433 p = file->buf_ptr;
434 if (c == CH_EOF) {
435 error("unexpected end of file in comment");
436 } else if (c == '\\') {
437 p++;
441 end_of_comment:
442 p++;
443 return p;
446 #define cinp minp
448 static inline void skip_spaces(void)
450 while (is_space(ch))
451 cinp();
454 static inline int check_space(int t, int *spc)
456 if (is_space(t)) {
457 if (*spc)
458 return 1;
459 *spc = 1;
460 } else
461 *spc = 0;
462 return 0;
465 /* parse a string without interpreting escapes */
466 static uint8_t *parse_pp_string(uint8_t *p,
467 int sep, CString *str)
469 int c;
470 p++;
471 for(;;) {
472 c = *p;
473 if (c == sep) {
474 break;
475 } else if (c == '\\') {
476 file->buf_ptr = p;
477 c = handle_eob();
478 p = file->buf_ptr;
479 if (c == CH_EOF) {
480 unterminated_string:
481 /* XXX: indicate line number of start of string */
482 error("missing terminating %c character", sep);
483 } else if (c == '\\') {
484 /* escape : just skip \[\r]\n */
485 PEEKC_EOB(c, p);
486 if (c == '\n') {
487 file->line_num++;
488 p++;
489 } else if (c == '\r') {
490 PEEKC_EOB(c, p);
491 if (c != '\n')
492 expect("'\n' after '\r'");
493 file->line_num++;
494 p++;
495 } else if (c == CH_EOF) {
496 goto unterminated_string;
497 } else {
498 if (str) {
499 cstr_ccat(str, '\\');
500 cstr_ccat(str, c);
502 p++;
505 } else if (c == '\n') {
506 file->line_num++;
507 goto add_char;
508 } else if (c == '\r') {
509 PEEKC_EOB(c, p);
510 if (c != '\n') {
511 if (str)
512 cstr_ccat(str, '\r');
513 } else {
514 file->line_num++;
515 goto add_char;
517 } else {
518 add_char:
519 if (str)
520 cstr_ccat(str, c);
521 p++;
524 p++;
525 return p;
528 /* skip block of text until #else, #elif or #endif. skip also pairs of
529 #if/#endif */
530 void preprocess_skip(void)
532 int a, start_of_line, c, in_warn_or_error;
533 uint8_t *p;
535 p = file->buf_ptr;
536 a = 0;
537 redo_start:
538 start_of_line = 1;
539 in_warn_or_error = 0;
540 for(;;) {
541 redo_no_start:
542 c = *p;
543 switch(c) {
544 case ' ':
545 case '\t':
546 case '\f':
547 case '\v':
548 case '\r':
549 p++;
550 goto redo_no_start;
551 case '\n':
552 file->line_num++;
553 p++;
554 goto redo_start;
555 case '\\':
556 file->buf_ptr = p;
557 c = handle_eob();
558 if (c == CH_EOF) {
559 expect("#endif");
560 } else if (c == '\\') {
561 ch = file->buf_ptr[0];
562 handle_stray_noerror();
564 p = file->buf_ptr;
565 goto redo_no_start;
566 /* skip strings */
567 case '\"':
568 case '\'':
569 if (in_warn_or_error)
570 goto _default;
571 p = parse_pp_string(p, c, NULL);
572 break;
573 /* skip comments */
574 case '/':
575 if (in_warn_or_error)
576 goto _default;
577 file->buf_ptr = p;
578 ch = *p;
579 minp();
580 p = file->buf_ptr;
581 if (ch == '*') {
582 p = parse_comment(p);
583 } else if (ch == '/') {
584 p = parse_line_comment(p);
586 break;
587 case '#':
588 p++;
589 if (start_of_line) {
590 file->buf_ptr = p;
591 next_nomacro();
592 p = file->buf_ptr;
593 if (a == 0 &&
594 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
595 goto the_end;
596 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
597 a++;
598 else if (tok == TOK_ENDIF)
599 a--;
600 else if( tok == TOK_ERROR || tok == TOK_WARNING)
601 in_warn_or_error = 1;
603 break;
604 _default:
605 default:
606 p++;
607 break;
609 start_of_line = 0;
611 the_end: ;
612 file->buf_ptr = p;
615 /* ParseState handling */
617 /* XXX: currently, no include file info is stored. Thus, we cannot display
618 accurate messages if the function or data definition spans multiple
619 files */
621 /* save current parse state in 's' */
622 void save_parse_state(ParseState *s)
624 s->line_num = file->line_num;
625 s->macro_ptr = macro_ptr;
626 s->tok = tok;
627 s->tokc = tokc;
630 /* restore parse state from 's' */
631 void restore_parse_state(ParseState *s)
633 file->line_num = s->line_num;
634 macro_ptr = s->macro_ptr;
635 tok = s->tok;
636 tokc = s->tokc;
640 void tok_str_free(int *str)
642 tcc_free(str);
645 static int *tok_str_realloc(TokenString *s)
647 int *str, len;
649 if (s->allocated_len == 0) {
650 len = 8;
651 } else {
652 len = s->allocated_len * 2;
654 str = tcc_realloc(s->str, len * sizeof(int));
655 if (!str)
656 error("memory full");
657 s->allocated_len = len;
658 s->str = str;
659 return str;
662 void tok_str_add(TokenString *s, int t)
664 int len, *str;
666 len = s->len;
667 str = s->str;
668 if (len >= s->allocated_len)
669 str = tok_str_realloc(s);
670 str[len++] = t;
671 s->len = len;
674 static void tok_str_add2(TokenString *s, int t, CValue *cv)
676 int len, *str;
678 len = s->len;
679 str = s->str;
681 /* allocate space for worst case */
682 if (len + TOK_MAX_SIZE > s->allocated_len)
683 str = tok_str_realloc(s);
684 str[len++] = t;
685 switch(t) {
686 case TOK_CINT:
687 case TOK_CUINT:
688 case TOK_CCHAR:
689 case TOK_LCHAR:
690 case TOK_CFLOAT:
691 case TOK_LINENUM:
692 str[len++] = cv->tab[0];
693 break;
694 case TOK_PPNUM:
695 case TOK_STR:
696 case TOK_LSTR:
698 int nb_words;
699 CString *cstr;
701 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
702 while ((len + nb_words) > s->allocated_len)
703 str = tok_str_realloc(s);
704 cstr = (CString *)(str + len);
705 cstr->data = NULL;
706 cstr->size = cv->cstr->size;
707 cstr->data_allocated = NULL;
708 cstr->size_allocated = cstr->size;
709 memcpy((char *)cstr + sizeof(CString),
710 cv->cstr->data, cstr->size);
711 len += nb_words;
713 break;
714 case TOK_CDOUBLE:
715 case TOK_CLLONG:
716 case TOK_CULLONG:
717 #if LDOUBLE_SIZE == 8
718 case TOK_CLDOUBLE:
719 #endif
720 str[len++] = cv->tab[0];
721 str[len++] = cv->tab[1];
722 break;
723 #if LDOUBLE_SIZE == 12
724 case TOK_CLDOUBLE:
725 str[len++] = cv->tab[0];
726 str[len++] = cv->tab[1];
727 str[len++] = cv->tab[2];
728 #elif LDOUBLE_SIZE == 16
729 case TOK_CLDOUBLE:
730 str[len++] = cv->tab[0];
731 str[len++] = cv->tab[1];
732 str[len++] = cv->tab[2];
733 str[len++] = cv->tab[3];
734 #elif LDOUBLE_SIZE != 8
735 #error add long double size support
736 #endif
737 break;
738 default:
739 break;
741 s->len = len;
744 /* token string handling */
746 /* add the current parse token in token string 's' */
747 void tok_str_add_tok(TokenString *s)
749 CValue cval;
751 /* save line number info */
752 if (file->line_num != s->last_line_num) {
753 s->last_line_num = file->line_num;
754 cval.i = s->last_line_num;
755 tok_str_add2(s, TOK_LINENUM, &cval);
757 tok_str_add2(s, tok, &tokc);
760 #if LDOUBLE_SIZE == 16
761 #define LDOUBLE_GET(p, cv) \
762 cv.tab[0] = p[0]; \
763 cv.tab[1] = p[1]; \
764 cv.tab[2] = p[2]; \
765 cv.tab[3] = p[3];
766 #elif LDOUBLE_SIZE == 12
767 #define LDOUBLE_GET(p, cv) \
768 cv.tab[0] = p[0]; \
769 cv.tab[1] = p[1]; \
770 cv.tab[2] = p[2];
771 #elif LDOUBLE_SIZE == 8
772 #define LDOUBLE_GET(p, cv) \
773 cv.tab[0] = p[0]; \
774 cv.tab[1] = p[1];
775 #else
776 #error add long double size support
777 #endif
780 /* get a token from an integer array and increment pointer
781 accordingly. we code it as a macro to avoid pointer aliasing. */
782 #define TOK_GET(t, p, cv) \
784 t = *p++; \
785 switch(t) { \
786 case TOK_CINT: \
787 case TOK_CUINT: \
788 case TOK_CCHAR: \
789 case TOK_LCHAR: \
790 case TOK_CFLOAT: \
791 case TOK_LINENUM: \
792 cv.tab[0] = *p++; \
793 break; \
794 case TOK_STR: \
795 case TOK_LSTR: \
796 case TOK_PPNUM: \
797 cv.cstr = (CString *)p; \
798 cv.cstr->data = (char *)p + sizeof(CString);\
799 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
800 break; \
801 case TOK_CDOUBLE: \
802 case TOK_CLLONG: \
803 case TOK_CULLONG: \
804 cv.tab[0] = p[0]; \
805 cv.tab[1] = p[1]; \
806 p += 2; \
807 break; \
808 case TOK_CLDOUBLE: \
809 LDOUBLE_GET(p, cv); \
810 p += LDOUBLE_SIZE / 4; \
811 break; \
812 default: \
813 break; \
817 /* undefined a define symbol. Its name is just set to zero */
818 void define_undef(Sym *s)
820 int v;
821 v = s->v;
822 if (v >= TOK_IDENT && v < tok_ident)
823 table_ident[v - TOK_IDENT]->sym_define = NULL;
824 s->v = 0;
827 static inline Sym *define_find(int v)
829 v -= TOK_IDENT;
830 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
831 return NULL;
832 return table_ident[v]->sym_define;
835 /* free define stack until top reaches 'b' */
836 void free_defines(Sym *b)
838 Sym *top, *top1;
839 int v;
841 top = define_stack;
842 while (top != b) {
843 top1 = top->prev;
844 /* do not free args or predefined defines */
845 if (top->d)
846 tok_str_free(top->d);
847 v = top->v;
848 if (v >= TOK_IDENT && v < tok_ident)
849 table_ident[v - TOK_IDENT]->sym_define = NULL;
850 sym_free(top);
851 top = top1;
853 define_stack = b;
856 /* label lookup */
857 Sym *label_find(int v)
859 v -= TOK_IDENT;
860 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
861 return NULL;
862 return table_ident[v]->sym_label;
865 Sym *label_push(Sym **ptop, int v, int flags)
867 Sym *s, **ps;
868 s = sym_push2(ptop, v, 0, 0);
869 s->r = flags;
870 ps = &table_ident[v - TOK_IDENT]->sym_label;
871 if (ptop == &global_label_stack) {
872 /* modify the top most local identifier, so that
873 sym_identifier will point to 's' when popped */
874 while (*ps != NULL)
875 ps = &(*ps)->prev_tok;
877 s->prev_tok = *ps;
878 *ps = s;
879 return s;
882 /* pop labels until element last is reached. Look if any labels are
883 undefined. Define symbols if '&&label' was used. */
884 void label_pop(Sym **ptop, Sym *slast)
886 Sym *s, *s1;
887 for(s = *ptop; s != slast; s = s1) {
888 s1 = s->prev;
889 if (s->r == LABEL_DECLARED) {
890 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
891 } else if (s->r == LABEL_FORWARD) {
892 error("label '%s' used but not defined",
893 get_tok_str(s->v, NULL));
894 } else {
895 if (s->c) {
896 /* define corresponding symbol. A size of
897 1 is put. */
898 put_extern_sym(s, cur_text_section, s->jnext, 1);
901 /* remove label */
902 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
903 sym_free(s);
905 *ptop = slast;
908 /* eval an expression for #if/#elif */
909 static int expr_preprocess(void)
911 int c, t;
912 TokenString str;
914 tok_str_new(&str);
915 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
916 next(); /* do macro subst */
917 if (tok == TOK_DEFINED) {
918 next_nomacro();
919 t = tok;
920 if (t == '(')
921 next_nomacro();
922 c = define_find(tok) != 0;
923 if (t == '(')
924 next_nomacro();
925 tok = TOK_CINT;
926 tokc.i = c;
927 } else if (tok >= TOK_IDENT) {
928 /* if undefined macro */
929 tok = TOK_CINT;
930 tokc.i = 0;
932 tok_str_add_tok(&str);
934 tok_str_add(&str, -1); /* simulate end of file */
935 tok_str_add(&str, 0);
936 /* now evaluate C constant expression */
937 macro_ptr = str.str;
938 next();
939 c = expr_const();
940 macro_ptr = NULL;
941 tok_str_free(str.str);
942 return c != 0;
945 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
946 static void tok_print(int *str)
948 int t;
949 CValue cval;
951 printf("<");
952 while (1) {
953 TOK_GET(t, str, cval);
954 if (!t)
955 break;
956 printf("%s", get_tok_str(t, &cval));
958 printf(">\n");
960 #endif
962 /* parse after #define */
963 void parse_define(void)
965 Sym *s, *first, **ps;
966 int v, t, varg, is_vaargs, spc;
967 TokenString str;
969 v = tok;
970 if (v < TOK_IDENT)
971 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
972 /* XXX: should check if same macro (ANSI) */
973 first = NULL;
974 t = MACRO_OBJ;
975 /* '(' must be just after macro definition for MACRO_FUNC */
976 next_nomacro_spc();
977 if (tok == '(') {
978 next_nomacro();
979 ps = &first;
980 while (tok != ')') {
981 varg = tok;
982 next_nomacro();
983 is_vaargs = 0;
984 if (varg == TOK_DOTS) {
985 varg = TOK___VA_ARGS__;
986 is_vaargs = 1;
987 } else if (tok == TOK_DOTS && gnu_ext) {
988 is_vaargs = 1;
989 next_nomacro();
991 if (varg < TOK_IDENT)
992 error("badly punctuated parameter list");
993 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
994 *ps = s;
995 ps = &s->next;
996 if (tok != ',')
997 break;
998 next_nomacro();
1000 if (tok == ')')
1001 next_nomacro_spc();
1002 t = MACRO_FUNC;
1004 tok_str_new(&str);
1005 spc = 2;
1006 /* EOF testing necessary for '-D' handling */
1007 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1008 /* remove spaces around ## and after '#' */
1009 if (TOK_TWOSHARPS == tok) {
1010 if (1 == spc)
1011 --str.len;
1012 spc = 2;
1013 } else if ('#' == tok) {
1014 spc = 2;
1015 } else if (check_space(tok, &spc)) {
1016 goto skip;
1018 tok_str_add2(&str, tok, &tokc);
1019 skip:
1020 next_nomacro_spc();
1022 if (spc == 1)
1023 --str.len; /* remove trailing space */
1024 tok_str_add(&str, 0);
1025 #ifdef PP_DEBUG
1026 printf("define %s %d: ", get_tok_str(v, NULL), t);
1027 tok_print(str.str);
1028 #endif
1029 define_push(v, t, str.str, first);
1032 static inline int hash_cached_include(int type, const char *filename)
1034 const unsigned char *s;
1035 unsigned int h;
1037 h = TOK_HASH_INIT;
1038 h = TOK_HASH_FUNC(h, type);
1039 s = filename;
1040 while (*s) {
1041 h = TOK_HASH_FUNC(h, *s);
1042 s++;
1044 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1045 return h;
1048 /* XXX: use a token or a hash table to accelerate matching ? */
1049 static CachedInclude *search_cached_include(TCCState *s1,
1050 int type, const char *filename)
1052 CachedInclude *e;
1053 int i, h;
1054 h = hash_cached_include(type, filename);
1055 i = s1->cached_includes_hash[h];
1056 for(;;) {
1057 if (i == 0)
1058 break;
1059 e = s1->cached_includes[i - 1];
1060 if (e->type == type && !PATHCMP(e->filename, filename))
1061 return e;
1062 i = e->hash_next;
1064 return NULL;
1067 static inline void add_cached_include(TCCState *s1, int type,
1068 const char *filename, int ifndef_macro)
1070 CachedInclude *e;
1071 int h;
1073 if (search_cached_include(s1, type, filename))
1074 return;
1075 #ifdef INC_DEBUG
1076 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1077 #endif
1078 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1079 if (!e)
1080 return;
1081 e->type = type;
1082 strcpy(e->filename, filename);
1083 e->ifndef_macro = ifndef_macro;
1084 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1085 /* add in hash table */
1086 h = hash_cached_include(type, filename);
1087 e->hash_next = s1->cached_includes_hash[h];
1088 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1091 static void pragma_parse(TCCState *s1)
1093 int val;
1095 next();
1096 if (tok == TOK_pack) {
1098 This may be:
1099 #pragma pack(1) // set
1100 #pragma pack() // reset to default
1101 #pragma pack(push,1) // push & set
1102 #pragma pack(pop) // restore previous
1104 next();
1105 skip('(');
1106 if (tok == TOK_ASM_pop) {
1107 next();
1108 if (s1->pack_stack_ptr <= s1->pack_stack) {
1109 stk_error:
1110 error("out of pack stack");
1112 s1->pack_stack_ptr--;
1113 } else {
1114 val = 0;
1115 if (tok != ')') {
1116 if (tok == TOK_ASM_push) {
1117 next();
1118 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1119 goto stk_error;
1120 s1->pack_stack_ptr++;
1121 skip(',');
1123 if (tok != TOK_CINT) {
1124 pack_error:
1125 error("invalid pack pragma");
1127 val = tokc.i;
1128 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1129 goto pack_error;
1130 next();
1132 *s1->pack_stack_ptr = val;
1133 skip(')');
1138 /* is_bof is true if first non space token at beginning of file */
1139 static void preprocess(int is_bof)
1141 TCCState *s1 = tcc_state;
1142 int i, c, n, saved_parse_flags;
1143 char buf[1024], *q;
1144 Sym *s;
1146 saved_parse_flags = parse_flags;
1147 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1148 PARSE_FLAG_LINEFEED;
1149 next_nomacro();
1150 redo:
1151 switch(tok) {
1152 case TOK_DEFINE:
1153 next_nomacro();
1154 parse_define();
1155 break;
1156 case TOK_UNDEF:
1157 next_nomacro();
1158 s = define_find(tok);
1159 /* undefine symbol by putting an invalid name */
1160 if (s)
1161 define_undef(s);
1162 break;
1163 case TOK_INCLUDE:
1164 case TOK_INCLUDE_NEXT:
1165 ch = file->buf_ptr[0];
1166 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1167 skip_spaces();
1168 if (ch == '<') {
1169 c = '>';
1170 goto read_name;
1171 } else if (ch == '\"') {
1172 c = ch;
1173 read_name:
1174 inp();
1175 q = buf;
1176 while (ch != c && ch != '\n' && ch != CH_EOF) {
1177 if ((q - buf) < sizeof(buf) - 1)
1178 *q++ = ch;
1179 if (ch == '\\') {
1180 if (handle_stray_noerror() == 0)
1181 --q;
1182 } else
1183 inp();
1185 *q = '\0';
1186 minp();
1187 #if 0
1188 /* eat all spaces and comments after include */
1189 /* XXX: slightly incorrect */
1190 while (ch1 != '\n' && ch1 != CH_EOF)
1191 inp();
1192 #endif
1193 } else {
1194 /* computed #include : either we have only strings or
1195 we have anything enclosed in '<>' */
1196 next();
1197 buf[0] = '\0';
1198 if (tok == TOK_STR) {
1199 while (tok != TOK_LINEFEED) {
1200 if (tok != TOK_STR) {
1201 include_syntax:
1202 error("'#include' expects \"FILENAME\" or <FILENAME>");
1204 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1205 next();
1207 c = '\"';
1208 } else {
1209 int len;
1210 while (tok != TOK_LINEFEED) {
1211 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1212 next();
1214 len = strlen(buf);
1215 /* check syntax and remove '<>' */
1216 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1217 goto include_syntax;
1218 memmove(buf, buf + 1, len - 2);
1219 buf[len - 2] = '\0';
1220 c = '>';
1224 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1225 error("#include recursion too deep");
1227 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1228 for (i = -2; i < n; ++i) {
1229 char buf1[sizeof file->filename];
1230 BufferedFile *f;
1231 CachedInclude *e;
1232 const char *path;
1233 int size;
1235 if (i == -2) {
1236 /* check absolute include path */
1237 if (!IS_ABSPATH(buf))
1238 continue;
1239 buf1[0] = 0;
1241 } else if (i == -1) {
1242 /* search in current dir if "header.h" */
1243 if (c != '\"')
1244 continue;
1245 size = tcc_basename(file->filename) - file->filename;
1246 memcpy(buf1, file->filename, size);
1247 buf1[size] = '\0';
1249 } else {
1250 /* search in all the include paths */
1251 if (i < s1->nb_include_paths)
1252 path = s1->include_paths[i];
1253 else
1254 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1255 pstrcpy(buf1, sizeof(buf1), path);
1256 pstrcat(buf1, sizeof(buf1), "/");
1259 pstrcat(buf1, sizeof(buf1), buf);
1261 e = search_cached_include(s1, c, buf1);
1262 if (e && define_find(e->ifndef_macro)) {
1263 /* no need to parse the include because the 'ifndef macro'
1264 is defined */
1265 #ifdef INC_DEBUG
1266 printf("%s: skipping %s\n", file->filename, buf);
1267 #endif
1268 f = NULL;
1269 } else {
1270 f = tcc_open(s1, buf1);
1271 if (!f)
1272 continue;
1275 if (tok == TOK_INCLUDE_NEXT) {
1276 tok = TOK_INCLUDE;
1277 if (f)
1278 tcc_close(f);
1279 continue;
1282 if (!f)
1283 goto include_done;
1285 #ifdef INC_DEBUG
1286 printf("%s: including %s\n", file->filename, buf1);
1287 #endif
1289 /* XXX: fix current line init */
1290 /* push current file in stack */
1291 *s1->include_stack_ptr++ = file;
1292 f->inc_type = c;
1293 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
1294 file = f;
1295 /* add include file debug info */
1296 if (tcc_state->do_debug) {
1297 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1299 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1300 ch = file->buf_ptr[0];
1301 goto the_end;
1303 error("include file '%s' not found", buf);
1304 include_done:
1305 break;
1306 case TOK_IFNDEF:
1307 c = 1;
1308 goto do_ifdef;
1309 case TOK_IF:
1310 c = expr_preprocess();
1311 goto do_if;
1312 case TOK_IFDEF:
1313 c = 0;
1314 do_ifdef:
1315 next_nomacro();
1316 if (tok < TOK_IDENT)
1317 error("invalid argument for '#if%sdef'", c ? "n" : "");
1318 if (is_bof) {
1319 if (c) {
1320 #ifdef INC_DEBUG
1321 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1322 #endif
1323 file->ifndef_macro = tok;
1326 c = (define_find(tok) != 0) ^ c;
1327 do_if:
1328 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1329 error("memory full");
1330 *s1->ifdef_stack_ptr++ = c;
1331 goto test_skip;
1332 case TOK_ELSE:
1333 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1334 error("#else without matching #if");
1335 if (s1->ifdef_stack_ptr[-1] & 2)
1336 error("#else after #else");
1337 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1338 goto test_else;
1339 case TOK_ELIF:
1340 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1341 error("#elif without matching #if");
1342 c = s1->ifdef_stack_ptr[-1];
1343 if (c > 1)
1344 error("#elif after #else");
1345 /* last #if/#elif expression was true: we skip */
1346 if (c == 1)
1347 goto skip;
1348 c = expr_preprocess();
1349 s1->ifdef_stack_ptr[-1] = c;
1350 test_else:
1351 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1352 file->ifndef_macro = 0;
1353 test_skip:
1354 if (!(c & 1)) {
1355 skip:
1356 preprocess_skip();
1357 is_bof = 0;
1358 goto redo;
1360 break;
1361 case TOK_ENDIF:
1362 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1363 error("#endif without matching #if");
1364 s1->ifdef_stack_ptr--;
1365 /* '#ifndef macro' was at the start of file. Now we check if
1366 an '#endif' is exactly at the end of file */
1367 if (file->ifndef_macro &&
1368 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1369 file->ifndef_macro_saved = file->ifndef_macro;
1370 /* need to set to zero to avoid false matches if another
1371 #ifndef at middle of file */
1372 file->ifndef_macro = 0;
1373 while (tok != TOK_LINEFEED)
1374 next_nomacro();
1375 tok_flags |= TOK_FLAG_ENDIF;
1376 goto the_end;
1378 break;
1379 case TOK_LINE:
1380 next();
1381 if (tok != TOK_CINT)
1382 error("#line");
1383 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1384 next();
1385 if (tok != TOK_LINEFEED) {
1386 if (tok != TOK_STR)
1387 error("#line");
1388 pstrcpy(file->filename, sizeof(file->filename),
1389 (char *)tokc.cstr->data);
1391 break;
1392 case TOK_ERROR:
1393 case TOK_WARNING:
1394 c = tok;
1395 ch = file->buf_ptr[0];
1396 skip_spaces();
1397 q = buf;
1398 while (ch != '\n' && ch != CH_EOF) {
1399 if ((q - buf) < sizeof(buf) - 1)
1400 *q++ = ch;
1401 if (ch == '\\') {
1402 if (handle_stray_noerror() == 0)
1403 --q;
1404 } else
1405 inp();
1407 *q = '\0';
1408 if (c == TOK_ERROR)
1409 error("#error %s", buf);
1410 else
1411 warning("#warning %s", buf);
1412 break;
1413 case TOK_PRAGMA:
1414 pragma_parse(s1);
1415 break;
1416 default:
1417 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
1418 /* '!' is ignored to allow C scripts. numbers are ignored
1419 to emulate cpp behaviour */
1420 } else {
1421 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1422 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1424 break;
1426 /* ignore other preprocess commands or #! for C scripts */
1427 while (tok != TOK_LINEFEED)
1428 next_nomacro();
1429 the_end:
1430 parse_flags = saved_parse_flags;
1433 /* evaluate escape codes in a string. */
1434 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1436 int c, n;
1437 const uint8_t *p;
1439 p = buf;
1440 for(;;) {
1441 c = *p;
1442 if (c == '\0')
1443 break;
1444 if (c == '\\') {
1445 p++;
1446 /* escape */
1447 c = *p;
1448 switch(c) {
1449 case '0': case '1': case '2': case '3':
1450 case '4': case '5': case '6': case '7':
1451 /* at most three octal digits */
1452 n = c - '0';
1453 p++;
1454 c = *p;
1455 if (isoct(c)) {
1456 n = n * 8 + c - '0';
1457 p++;
1458 c = *p;
1459 if (isoct(c)) {
1460 n = n * 8 + c - '0';
1461 p++;
1464 c = n;
1465 goto add_char_nonext;
1466 case 'x':
1467 case 'u':
1468 case 'U':
1469 p++;
1470 n = 0;
1471 for(;;) {
1472 c = *p;
1473 if (c >= 'a' && c <= 'f')
1474 c = c - 'a' + 10;
1475 else if (c >= 'A' && c <= 'F')
1476 c = c - 'A' + 10;
1477 else if (isnum(c))
1478 c = c - '0';
1479 else
1480 break;
1481 n = n * 16 + c;
1482 p++;
1484 c = n;
1485 goto add_char_nonext;
1486 case 'a':
1487 c = '\a';
1488 break;
1489 case 'b':
1490 c = '\b';
1491 break;
1492 case 'f':
1493 c = '\f';
1494 break;
1495 case 'n':
1496 c = '\n';
1497 break;
1498 case 'r':
1499 c = '\r';
1500 break;
1501 case 't':
1502 c = '\t';
1503 break;
1504 case 'v':
1505 c = '\v';
1506 break;
1507 case 'e':
1508 if (!gnu_ext)
1509 goto invalid_escape;
1510 c = 27;
1511 break;
1512 case '\'':
1513 case '\"':
1514 case '\\':
1515 case '?':
1516 break;
1517 default:
1518 invalid_escape:
1519 if (c >= '!' && c <= '~')
1520 warning("unknown escape sequence: \'\\%c\'", c);
1521 else
1522 warning("unknown escape sequence: \'\\x%x\'", c);
1523 break;
1526 p++;
1527 add_char_nonext:
1528 if (!is_long)
1529 cstr_ccat(outstr, c);
1530 else
1531 cstr_wccat(outstr, c);
1533 /* add a trailing '\0' */
1534 if (!is_long)
1535 cstr_ccat(outstr, '\0');
1536 else
1537 cstr_wccat(outstr, '\0');
1540 /* we use 64 bit numbers */
1541 #define BN_SIZE 2
1543 /* bn = (bn << shift) | or_val */
1544 void bn_lshift(unsigned int *bn, int shift, int or_val)
1546 int i;
1547 unsigned int v;
1548 for(i=0;i<BN_SIZE;i++) {
1549 v = bn[i];
1550 bn[i] = (v << shift) | or_val;
1551 or_val = v >> (32 - shift);
1555 void bn_zero(unsigned int *bn)
1557 int i;
1558 for(i=0;i<BN_SIZE;i++) {
1559 bn[i] = 0;
1563 /* parse number in null terminated string 'p' and return it in the
1564 current token */
1565 void parse_number(const char *p)
1567 int b, t, shift, frac_bits, s, exp_val, ch;
1568 char *q;
1569 unsigned int bn[BN_SIZE];
1570 double d;
1572 /* number */
1573 q = token_buf;
1574 ch = *p++;
1575 t = ch;
1576 ch = *p++;
1577 *q++ = t;
1578 b = 10;
1579 if (t == '.') {
1580 goto float_frac_parse;
1581 } else if (t == '0') {
1582 if (ch == 'x' || ch == 'X') {
1583 q--;
1584 ch = *p++;
1585 b = 16;
1586 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1587 q--;
1588 ch = *p++;
1589 b = 2;
1592 /* parse all digits. cannot check octal numbers at this stage
1593 because of floating point constants */
1594 while (1) {
1595 if (ch >= 'a' && ch <= 'f')
1596 t = ch - 'a' + 10;
1597 else if (ch >= 'A' && ch <= 'F')
1598 t = ch - 'A' + 10;
1599 else if (isnum(ch))
1600 t = ch - '0';
1601 else
1602 break;
1603 if (t >= b)
1604 break;
1605 if (q >= token_buf + STRING_MAX_SIZE) {
1606 num_too_long:
1607 error("number too long");
1609 *q++ = ch;
1610 ch = *p++;
1612 if (ch == '.' ||
1613 ((ch == 'e' || ch == 'E') && b == 10) ||
1614 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1615 if (b != 10) {
1616 /* NOTE: strtox should support that for hexa numbers, but
1617 non ISOC99 libcs do not support it, so we prefer to do
1618 it by hand */
1619 /* hexadecimal or binary floats */
1620 /* XXX: handle overflows */
1621 *q = '\0';
1622 if (b == 16)
1623 shift = 4;
1624 else
1625 shift = 2;
1626 bn_zero(bn);
1627 q = token_buf;
1628 while (1) {
1629 t = *q++;
1630 if (t == '\0') {
1631 break;
1632 } else if (t >= 'a') {
1633 t = t - 'a' + 10;
1634 } else if (t >= 'A') {
1635 t = t - 'A' + 10;
1636 } else {
1637 t = t - '0';
1639 bn_lshift(bn, shift, t);
1641 frac_bits = 0;
1642 if (ch == '.') {
1643 ch = *p++;
1644 while (1) {
1645 t = ch;
1646 if (t >= 'a' && t <= 'f') {
1647 t = t - 'a' + 10;
1648 } else if (t >= 'A' && t <= 'F') {
1649 t = t - 'A' + 10;
1650 } else if (t >= '0' && t <= '9') {
1651 t = t - '0';
1652 } else {
1653 break;
1655 if (t >= b)
1656 error("invalid digit");
1657 bn_lshift(bn, shift, t);
1658 frac_bits += shift;
1659 ch = *p++;
1662 if (ch != 'p' && ch != 'P')
1663 expect("exponent");
1664 ch = *p++;
1665 s = 1;
1666 exp_val = 0;
1667 if (ch == '+') {
1668 ch = *p++;
1669 } else if (ch == '-') {
1670 s = -1;
1671 ch = *p++;
1673 if (ch < '0' || ch > '9')
1674 expect("exponent digits");
1675 while (ch >= '0' && ch <= '9') {
1676 exp_val = exp_val * 10 + ch - '0';
1677 ch = *p++;
1679 exp_val = exp_val * s;
1681 /* now we can generate the number */
1682 /* XXX: should patch directly float number */
1683 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1684 d = ldexp(d, exp_val - frac_bits);
1685 t = toup(ch);
1686 if (t == 'F') {
1687 ch = *p++;
1688 tok = TOK_CFLOAT;
1689 /* float : should handle overflow */
1690 tokc.f = (float)d;
1691 } else if (t == 'L') {
1692 ch = *p++;
1693 #ifdef TCC_TARGET_PE
1694 tok = TOK_CDOUBLE;
1695 tokc.d = d;
1696 #else
1697 tok = TOK_CLDOUBLE;
1698 /* XXX: not large enough */
1699 tokc.ld = (long double)d;
1700 #endif
1701 } else {
1702 tok = TOK_CDOUBLE;
1703 tokc.d = d;
1705 } else {
1706 /* decimal floats */
1707 if (ch == '.') {
1708 if (q >= token_buf + STRING_MAX_SIZE)
1709 goto num_too_long;
1710 *q++ = ch;
1711 ch = *p++;
1712 float_frac_parse:
1713 while (ch >= '0' && ch <= '9') {
1714 if (q >= token_buf + STRING_MAX_SIZE)
1715 goto num_too_long;
1716 *q++ = ch;
1717 ch = *p++;
1720 if (ch == 'e' || ch == 'E') {
1721 if (q >= token_buf + STRING_MAX_SIZE)
1722 goto num_too_long;
1723 *q++ = ch;
1724 ch = *p++;
1725 if (ch == '-' || ch == '+') {
1726 if (q >= token_buf + STRING_MAX_SIZE)
1727 goto num_too_long;
1728 *q++ = ch;
1729 ch = *p++;
1731 if (ch < '0' || ch > '9')
1732 expect("exponent digits");
1733 while (ch >= '0' && ch <= '9') {
1734 if (q >= token_buf + STRING_MAX_SIZE)
1735 goto num_too_long;
1736 *q++ = ch;
1737 ch = *p++;
1740 *q = '\0';
1741 t = toup(ch);
1742 errno = 0;
1743 if (t == 'F') {
1744 ch = *p++;
1745 tok = TOK_CFLOAT;
1746 tokc.f = strtof(token_buf, NULL);
1747 } else if (t == 'L') {
1748 ch = *p++;
1749 #ifdef TCC_TARGET_PE
1750 tok = TOK_CDOUBLE;
1751 tokc.d = strtod(token_buf, NULL);
1752 #else
1753 tok = TOK_CLDOUBLE;
1754 tokc.ld = strtold(token_buf, NULL);
1755 #endif
1756 } else {
1757 tok = TOK_CDOUBLE;
1758 tokc.d = strtod(token_buf, NULL);
1761 } else {
1762 unsigned long long n, n1;
1763 int lcount, ucount;
1765 /* integer number */
1766 *q = '\0';
1767 q = token_buf;
1768 if (b == 10 && *q == '0') {
1769 b = 8;
1770 q++;
1772 n = 0;
1773 while(1) {
1774 t = *q++;
1775 /* no need for checks except for base 10 / 8 errors */
1776 if (t == '\0') {
1777 break;
1778 } else if (t >= 'a') {
1779 t = t - 'a' + 10;
1780 } else if (t >= 'A') {
1781 t = t - 'A' + 10;
1782 } else {
1783 t = t - '0';
1784 if (t >= b)
1785 error("invalid digit");
1787 n1 = n;
1788 n = n * b + t;
1789 /* detect overflow */
1790 /* XXX: this test is not reliable */
1791 if (n < n1)
1792 error("integer constant overflow");
1795 /* XXX: not exactly ANSI compliant */
1796 if ((n & 0xffffffff00000000LL) != 0) {
1797 if ((n >> 63) != 0)
1798 tok = TOK_CULLONG;
1799 else
1800 tok = TOK_CLLONG;
1801 } else if (n > 0x7fffffff) {
1802 tok = TOK_CUINT;
1803 } else {
1804 tok = TOK_CINT;
1806 lcount = 0;
1807 ucount = 0;
1808 for(;;) {
1809 t = toup(ch);
1810 if (t == 'L') {
1811 if (lcount >= 2)
1812 error("three 'l's in integer constant");
1813 lcount++;
1814 if (lcount == 2) {
1815 if (tok == TOK_CINT)
1816 tok = TOK_CLLONG;
1817 else if (tok == TOK_CUINT)
1818 tok = TOK_CULLONG;
1820 ch = *p++;
1821 } else if (t == 'U') {
1822 if (ucount >= 1)
1823 error("two 'u's in integer constant");
1824 ucount++;
1825 if (tok == TOK_CINT)
1826 tok = TOK_CUINT;
1827 else if (tok == TOK_CLLONG)
1828 tok = TOK_CULLONG;
1829 ch = *p++;
1830 } else {
1831 break;
1834 if (tok == TOK_CINT || tok == TOK_CUINT)
1835 tokc.ui = n;
1836 else
1837 tokc.ull = n;
1839 if (ch)
1840 error("invalid number\n");
1844 #define PARSE2(c1, tok1, c2, tok2) \
1845 case c1: \
1846 PEEKC(c, p); \
1847 if (c == c2) { \
1848 p++; \
1849 tok = tok2; \
1850 } else { \
1851 tok = tok1; \
1853 break;
1855 /* return next token without macro substitution */
1856 static inline void next_nomacro1(void)
1858 int t, c, is_long;
1859 TokenSym *ts;
1860 uint8_t *p, *p1;
1861 unsigned int h;
1863 p = file->buf_ptr;
1864 redo_no_start:
1865 c = *p;
1866 switch(c) {
1867 case ' ':
1868 case '\t':
1869 tok = c;
1870 p++;
1871 goto keep_tok_flags;
1872 case '\f':
1873 case '\v':
1874 case '\r':
1875 p++;
1876 goto redo_no_start;
1877 case '\\':
1878 /* first look if it is in fact an end of buffer */
1879 if (p >= file->buf_end) {
1880 file->buf_ptr = p;
1881 handle_eob();
1882 p = file->buf_ptr;
1883 if (p >= file->buf_end)
1884 goto parse_eof;
1885 else
1886 goto redo_no_start;
1887 } else {
1888 file->buf_ptr = p;
1889 ch = *p;
1890 handle_stray();
1891 p = file->buf_ptr;
1892 goto redo_no_start;
1894 parse_eof:
1896 TCCState *s1 = tcc_state;
1897 if ((parse_flags & PARSE_FLAG_LINEFEED)
1898 && !(tok_flags & TOK_FLAG_EOF)) {
1899 tok_flags |= TOK_FLAG_EOF;
1900 tok = TOK_LINEFEED;
1901 goto keep_tok_flags;
1902 } else if (s1->include_stack_ptr == s1->include_stack ||
1903 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
1904 /* no include left : end of file. */
1905 tok = TOK_EOF;
1906 } else {
1907 tok_flags &= ~TOK_FLAG_EOF;
1908 /* pop include file */
1910 /* test if previous '#endif' was after a #ifdef at
1911 start of file */
1912 if (tok_flags & TOK_FLAG_ENDIF) {
1913 #ifdef INC_DEBUG
1914 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
1915 #endif
1916 add_cached_include(s1, file->inc_type, file->inc_filename,
1917 file->ifndef_macro_saved);
1920 /* add end of include file debug info */
1921 if (tcc_state->do_debug) {
1922 put_stabd(N_EINCL, 0, 0);
1924 /* pop include stack */
1925 tcc_close(file);
1926 s1->include_stack_ptr--;
1927 file = *s1->include_stack_ptr;
1928 p = file->buf_ptr;
1929 goto redo_no_start;
1932 break;
1934 case '\n':
1935 file->line_num++;
1936 tok_flags |= TOK_FLAG_BOL;
1937 p++;
1938 maybe_newline:
1939 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
1940 goto redo_no_start;
1941 tok = TOK_LINEFEED;
1942 goto keep_tok_flags;
1944 case '#':
1945 /* XXX: simplify */
1946 PEEKC(c, p);
1947 if ((tok_flags & TOK_FLAG_BOL) &&
1948 (parse_flags & PARSE_FLAG_PREPROCESS)) {
1949 file->buf_ptr = p;
1950 preprocess(tok_flags & TOK_FLAG_BOF);
1951 p = file->buf_ptr;
1952 goto maybe_newline;
1953 } else {
1954 if (c == '#') {
1955 p++;
1956 tok = TOK_TWOSHARPS;
1957 } else {
1958 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
1959 p = parse_line_comment(p - 1);
1960 goto redo_no_start;
1961 } else {
1962 tok = '#';
1966 break;
1968 case 'a': case 'b': case 'c': case 'd':
1969 case 'e': case 'f': case 'g': case 'h':
1970 case 'i': case 'j': case 'k': case 'l':
1971 case 'm': case 'n': case 'o': case 'p':
1972 case 'q': case 'r': case 's': case 't':
1973 case 'u': case 'v': case 'w': case 'x':
1974 case 'y': case 'z':
1975 case 'A': case 'B': case 'C': case 'D':
1976 case 'E': case 'F': case 'G': case 'H':
1977 case 'I': case 'J': case 'K':
1978 case 'M': case 'N': case 'O': case 'P':
1979 case 'Q': case 'R': case 'S': case 'T':
1980 case 'U': case 'V': case 'W': case 'X':
1981 case 'Y': case 'Z':
1982 case '_':
1983 parse_ident_fast:
1984 p1 = p;
1985 h = TOK_HASH_INIT;
1986 h = TOK_HASH_FUNC(h, c);
1987 p++;
1988 for(;;) {
1989 c = *p;
1990 if (!isidnum_table[c-CH_EOF])
1991 break;
1992 h = TOK_HASH_FUNC(h, c);
1993 p++;
1995 if (c != '\\') {
1996 TokenSym **pts;
1997 int len;
1999 /* fast case : no stray found, so we have the full token
2000 and we have already hashed it */
2001 len = p - p1;
2002 h &= (TOK_HASH_SIZE - 1);
2003 pts = &hash_ident[h];
2004 for(;;) {
2005 ts = *pts;
2006 if (!ts)
2007 break;
2008 if (ts->len == len && !memcmp(ts->str, p1, len))
2009 goto token_found;
2010 pts = &(ts->hash_next);
2012 ts = tok_alloc_new(pts, p1, len);
2013 token_found: ;
2014 } else {
2015 /* slower case */
2016 cstr_reset(&tokcstr);
2018 while (p1 < p) {
2019 cstr_ccat(&tokcstr, *p1);
2020 p1++;
2022 p--;
2023 PEEKC(c, p);
2024 parse_ident_slow:
2025 while (isidnum_table[c-CH_EOF]) {
2026 cstr_ccat(&tokcstr, c);
2027 PEEKC(c, p);
2029 ts = tok_alloc(tokcstr.data, tokcstr.size);
2031 tok = ts->tok;
2032 break;
2033 case 'L':
2034 t = p[1];
2035 if (t != '\\' && t != '\'' && t != '\"') {
2036 /* fast case */
2037 goto parse_ident_fast;
2038 } else {
2039 PEEKC(c, p);
2040 if (c == '\'' || c == '\"') {
2041 is_long = 1;
2042 goto str_const;
2043 } else {
2044 cstr_reset(&tokcstr);
2045 cstr_ccat(&tokcstr, 'L');
2046 goto parse_ident_slow;
2049 break;
2050 case '0': case '1': case '2': case '3':
2051 case '4': case '5': case '6': case '7':
2052 case '8': case '9':
2054 cstr_reset(&tokcstr);
2055 /* after the first digit, accept digits, alpha, '.' or sign if
2056 prefixed by 'eEpP' */
2057 parse_num:
2058 for(;;) {
2059 t = c;
2060 cstr_ccat(&tokcstr, c);
2061 PEEKC(c, p);
2062 if (!(isnum(c) || isid(c) || c == '.' ||
2063 ((c == '+' || c == '-') &&
2064 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2065 break;
2067 /* We add a trailing '\0' to ease parsing */
2068 cstr_ccat(&tokcstr, '\0');
2069 tokc.cstr = &tokcstr;
2070 tok = TOK_PPNUM;
2071 break;
2072 case '.':
2073 /* special dot handling because it can also start a number */
2074 PEEKC(c, p);
2075 if (isnum(c)) {
2076 cstr_reset(&tokcstr);
2077 cstr_ccat(&tokcstr, '.');
2078 goto parse_num;
2079 } else if (c == '.') {
2080 PEEKC(c, p);
2081 if (c != '.')
2082 expect("'.'");
2083 PEEKC(c, p);
2084 tok = TOK_DOTS;
2085 } else {
2086 tok = '.';
2088 break;
2089 case '\'':
2090 case '\"':
2091 is_long = 0;
2092 str_const:
2094 CString str;
2095 int sep;
2097 sep = c;
2099 /* parse the string */
2100 cstr_new(&str);
2101 p = parse_pp_string(p, sep, &str);
2102 cstr_ccat(&str, '\0');
2104 /* eval the escape (should be done as TOK_PPNUM) */
2105 cstr_reset(&tokcstr);
2106 parse_escape_string(&tokcstr, str.data, is_long);
2107 cstr_free(&str);
2109 if (sep == '\'') {
2110 int char_size;
2111 /* XXX: make it portable */
2112 if (!is_long)
2113 char_size = 1;
2114 else
2115 char_size = sizeof(nwchar_t);
2116 if (tokcstr.size <= char_size)
2117 error("empty character constant");
2118 if (tokcstr.size > 2 * char_size)
2119 warning("multi-character character constant");
2120 if (!is_long) {
2121 tokc.i = *(int8_t *)tokcstr.data;
2122 tok = TOK_CCHAR;
2123 } else {
2124 tokc.i = *(nwchar_t *)tokcstr.data;
2125 tok = TOK_LCHAR;
2127 } else {
2128 tokc.cstr = &tokcstr;
2129 if (!is_long)
2130 tok = TOK_STR;
2131 else
2132 tok = TOK_LSTR;
2135 break;
2137 case '<':
2138 PEEKC(c, p);
2139 if (c == '=') {
2140 p++;
2141 tok = TOK_LE;
2142 } else if (c == '<') {
2143 PEEKC(c, p);
2144 if (c == '=') {
2145 p++;
2146 tok = TOK_A_SHL;
2147 } else {
2148 tok = TOK_SHL;
2150 } else {
2151 tok = TOK_LT;
2153 break;
2155 case '>':
2156 PEEKC(c, p);
2157 if (c == '=') {
2158 p++;
2159 tok = TOK_GE;
2160 } else if (c == '>') {
2161 PEEKC(c, p);
2162 if (c == '=') {
2163 p++;
2164 tok = TOK_A_SAR;
2165 } else {
2166 tok = TOK_SAR;
2168 } else {
2169 tok = TOK_GT;
2171 break;
2173 case '&':
2174 PEEKC(c, p);
2175 if (c == '&') {
2176 p++;
2177 tok = TOK_LAND;
2178 } else if (c == '=') {
2179 p++;
2180 tok = TOK_A_AND;
2181 } else {
2182 tok = '&';
2184 break;
2186 case '|':
2187 PEEKC(c, p);
2188 if (c == '|') {
2189 p++;
2190 tok = TOK_LOR;
2191 } else if (c == '=') {
2192 p++;
2193 tok = TOK_A_OR;
2194 } else {
2195 tok = '|';
2197 break;
2199 case '+':
2200 PEEKC(c, p);
2201 if (c == '+') {
2202 p++;
2203 tok = TOK_INC;
2204 } else if (c == '=') {
2205 p++;
2206 tok = TOK_A_ADD;
2207 } else {
2208 tok = '+';
2210 break;
2212 case '-':
2213 PEEKC(c, p);
2214 if (c == '-') {
2215 p++;
2216 tok = TOK_DEC;
2217 } else if (c == '=') {
2218 p++;
2219 tok = TOK_A_SUB;
2220 } else if (c == '>') {
2221 p++;
2222 tok = TOK_ARROW;
2223 } else {
2224 tok = '-';
2226 break;
2228 PARSE2('!', '!', '=', TOK_NE)
2229 PARSE2('=', '=', '=', TOK_EQ)
2230 PARSE2('*', '*', '=', TOK_A_MUL)
2231 PARSE2('%', '%', '=', TOK_A_MOD)
2232 PARSE2('^', '^', '=', TOK_A_XOR)
2234 /* comments or operator */
2235 case '/':
2236 PEEKC(c, p);
2237 if (c == '*') {
2238 p = parse_comment(p);
2239 goto redo_no_start;
2240 } else if (c == '/') {
2241 p = parse_line_comment(p);
2242 goto redo_no_start;
2243 } else if (c == '=') {
2244 p++;
2245 tok = TOK_A_DIV;
2246 } else {
2247 tok = '/';
2249 break;
2251 /* simple tokens */
2252 case '(':
2253 case ')':
2254 case '[':
2255 case ']':
2256 case '{':
2257 case '}':
2258 case ',':
2259 case ';':
2260 case ':':
2261 case '?':
2262 case '~':
2263 case '$': /* only used in assembler */
2264 case '@': /* dito */
2265 tok = c;
2266 p++;
2267 break;
2268 default:
2269 error("unrecognized character \\x%02x", c);
2270 break;
2272 tok_flags = 0;
2273 keep_tok_flags:
2274 file->buf_ptr = p;
2275 #if defined(PARSE_DEBUG)
2276 printf("token = %s\n", get_tok_str(tok, &tokc));
2277 #endif
2280 /* return next token without macro substitution. Can read input from
2281 macro_ptr buffer */
2282 static void next_nomacro_spc(void)
2284 if (macro_ptr) {
2285 redo:
2286 tok = *macro_ptr;
2287 if (tok) {
2288 TOK_GET(tok, macro_ptr, tokc);
2289 if (tok == TOK_LINENUM) {
2290 file->line_num = tokc.i;
2291 goto redo;
2294 } else {
2295 next_nomacro1();
2299 void next_nomacro(void)
2301 do {
2302 next_nomacro_spc();
2303 } while (is_space(tok));
2306 /* substitute args in macro_str and return allocated string */
2307 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
2309 int *st, last_tok, t, spc;
2310 Sym *s;
2311 CValue cval;
2312 TokenString str;
2313 CString cstr;
2315 tok_str_new(&str);
2316 last_tok = 0;
2317 while(1) {
2318 TOK_GET(t, macro_str, cval);
2319 if (!t)
2320 break;
2321 if (t == '#') {
2322 /* stringize */
2323 TOK_GET(t, macro_str, cval);
2324 if (!t)
2325 break;
2326 s = sym_find2(args, t);
2327 if (s) {
2328 cstr_new(&cstr);
2329 st = s->d;
2330 spc = 0;
2331 while (*st) {
2332 TOK_GET(t, st, cval);
2333 if (!check_space(t, &spc))
2334 cstr_cat(&cstr, get_tok_str(t, &cval));
2336 cstr.size -= spc;
2337 cstr_ccat(&cstr, '\0');
2338 #ifdef PP_DEBUG
2339 printf("stringize: %s\n", (char *)cstr.data);
2340 #endif
2341 /* add string */
2342 cval.cstr = &cstr;
2343 tok_str_add2(&str, TOK_STR, &cval);
2344 cstr_free(&cstr);
2345 } else {
2346 tok_str_add2(&str, t, &cval);
2348 } else if (t >= TOK_IDENT) {
2349 s = sym_find2(args, t);
2350 if (s) {
2351 st = s->d;
2352 /* if '##' is present before or after, no arg substitution */
2353 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2354 /* special case for var arg macros : ## eats the
2355 ',' if empty VA_ARGS variable. */
2356 /* XXX: test of the ',' is not 100%
2357 reliable. should fix it to avoid security
2358 problems */
2359 if (gnu_ext && s->type.t &&
2360 last_tok == TOK_TWOSHARPS &&
2361 str.len >= 2 && str.str[str.len - 2] == ',') {
2362 if (*st == 0) {
2363 /* suppress ',' '##' */
2364 str.len -= 2;
2365 } else {
2366 /* suppress '##' and add variable */
2367 str.len--;
2368 goto add_var;
2370 } else {
2371 int t1;
2372 add_var:
2373 for(;;) {
2374 TOK_GET(t1, st, cval);
2375 if (!t1)
2376 break;
2377 tok_str_add2(&str, t1, &cval);
2380 } else {
2381 /* NOTE: the stream cannot be read when macro
2382 substituing an argument */
2383 macro_subst(&str, nested_list, st, NULL);
2385 } else {
2386 tok_str_add(&str, t);
2388 } else {
2389 tok_str_add2(&str, t, &cval);
2391 last_tok = t;
2393 tok_str_add(&str, 0);
2394 return str.str;
2397 static char const ab_month_name[12][4] =
2399 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2400 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2403 /* do macro substitution of current token with macro 's' and add
2404 result to (tok_str,tok_len). 'nested_list' is the list of all
2405 macros we got inside to avoid recursing. Return non zero if no
2406 substitution needs to be done */
2407 static int macro_subst_tok(TokenString *tok_str,
2408 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2410 Sym *args, *sa, *sa1;
2411 int mstr_allocated, parlevel, *mstr, t, t1, *p, spc;
2412 TokenString str;
2413 char *cstrval;
2414 CValue cval;
2415 CString cstr;
2416 char buf[32];
2418 /* if symbol is a macro, prepare substitution */
2419 /* special macros */
2420 if (tok == TOK___LINE__) {
2421 snprintf(buf, sizeof(buf), "%d", file->line_num);
2422 cstrval = buf;
2423 t1 = TOK_PPNUM;
2424 goto add_cstr1;
2425 } else if (tok == TOK___FILE__) {
2426 cstrval = file->filename;
2427 goto add_cstr;
2428 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2429 time_t ti;
2430 struct tm *tm;
2432 time(&ti);
2433 tm = localtime(&ti);
2434 if (tok == TOK___DATE__) {
2435 snprintf(buf, sizeof(buf), "%s %2d %d",
2436 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2437 } else {
2438 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2439 tm->tm_hour, tm->tm_min, tm->tm_sec);
2441 cstrval = buf;
2442 add_cstr:
2443 t1 = TOK_STR;
2444 add_cstr1:
2445 cstr_new(&cstr);
2446 cstr_cat(&cstr, cstrval);
2447 cstr_ccat(&cstr, '\0');
2448 cval.cstr = &cstr;
2449 tok_str_add2(tok_str, t1, &cval);
2450 cstr_free(&cstr);
2451 } else {
2452 mstr = s->d;
2453 mstr_allocated = 0;
2454 if (s->type.t == MACRO_FUNC) {
2455 /* NOTE: we do not use next_nomacro to avoid eating the
2456 next token. XXX: find better solution */
2457 redo:
2458 if (macro_ptr) {
2459 p = macro_ptr;
2460 while (is_space(t = *p) || TOK_LINEFEED == t)
2461 ++p;
2462 if (t == 0 && can_read_stream) {
2463 /* end of macro stream: we must look at the token
2464 after in the file */
2465 struct macro_level *ml = *can_read_stream;
2466 macro_ptr = NULL;
2467 if (ml)
2469 macro_ptr = ml->p;
2470 ml->p = NULL;
2471 *can_read_stream = ml -> prev;
2473 goto redo;
2475 } else {
2476 /* XXX: incorrect with comments */
2477 ch = file->buf_ptr[0];
2478 while (is_space(ch) || ch == '\n')
2479 cinp();
2480 t = ch;
2482 if (t != '(') /* no macro subst */
2483 return -1;
2485 /* argument macro */
2486 next_nomacro();
2487 next_nomacro();
2488 args = NULL;
2489 sa = s->next;
2490 /* NOTE: empty args are allowed, except if no args */
2491 for(;;) {
2492 /* handle '()' case */
2493 if (!args && !sa && tok == ')')
2494 break;
2495 if (!sa)
2496 error("macro '%s' used with too many args",
2497 get_tok_str(s->v, 0));
2498 tok_str_new(&str);
2499 parlevel = spc = 0;
2500 /* NOTE: non zero sa->t indicates VA_ARGS */
2501 while ((parlevel > 0 ||
2502 (tok != ')' &&
2503 (tok != ',' || sa->type.t))) &&
2504 tok != -1) {
2505 if (tok == '(')
2506 parlevel++;
2507 else if (tok == ')')
2508 parlevel--;
2509 if (tok == TOK_LINEFEED)
2510 tok = ' ';
2511 if (!check_space(tok, &spc))
2512 tok_str_add2(&str, tok, &tokc);
2513 next_nomacro_spc();
2515 str.len -= spc;
2516 tok_str_add(&str, 0);
2517 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2518 sa1->d = str.str;
2519 sa = sa->next;
2520 if (tok == ')') {
2521 /* special case for gcc var args: add an empty
2522 var arg argument if it is omitted */
2523 if (sa && sa->type.t && gnu_ext)
2524 continue;
2525 else
2526 break;
2528 if (tok != ',')
2529 expect(",");
2530 next_nomacro();
2532 if (sa) {
2533 error("macro '%s' used with too few args",
2534 get_tok_str(s->v, 0));
2537 /* now subst each arg */
2538 mstr = macro_arg_subst(nested_list, mstr, args);
2539 /* free memory */
2540 sa = args;
2541 while (sa) {
2542 sa1 = sa->prev;
2543 tok_str_free(sa->d);
2544 sym_free(sa);
2545 sa = sa1;
2547 mstr_allocated = 1;
2549 sym_push2(nested_list, s->v, 0, 0);
2550 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2551 /* pop nested defined symbol */
2552 sa1 = *nested_list;
2553 *nested_list = sa1->prev;
2554 sym_free(sa1);
2555 if (mstr_allocated)
2556 tok_str_free(mstr);
2558 return 0;
2561 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2562 return the resulting string (which must be freed). */
2563 static inline int *macro_twosharps(const int *macro_str)
2565 const int *ptr;
2566 int t;
2567 CValue cval;
2568 TokenString macro_str1;
2569 CString cstr;
2570 char *p;
2571 int n;
2573 /* we search the first '##' */
2574 for(ptr = macro_str;;) {
2575 TOK_GET(t, ptr, cval);
2576 if (t == TOK_TWOSHARPS)
2577 break;
2578 /* nothing more to do if end of string */
2579 if (t == 0)
2580 return NULL;
2583 /* we saw '##', so we need more processing to handle it */
2584 tok_str_new(&macro_str1);
2585 for(ptr = macro_str;;) {
2586 TOK_GET(tok, ptr, tokc);
2587 if (tok == 0)
2588 break;
2589 if (tok == TOK_TWOSHARPS)
2590 continue;
2591 while (*ptr == TOK_TWOSHARPS) {
2592 t = *++ptr;
2593 if (t && t != TOK_TWOSHARPS) {
2594 TOK_GET(t, ptr, cval);
2596 /* We concatenate the two tokens */
2597 cstr_new(&cstr);
2598 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2599 n = cstr.size;
2600 cstr_cat(&cstr, get_tok_str(t, &cval));
2601 cstr_ccat(&cstr, '\0');
2603 p = file->buf_ptr;
2604 file->buf_ptr = cstr.data;
2605 for (;;) {
2606 next_nomacro1();
2607 if (0 == *file->buf_ptr)
2608 break;
2609 tok_str_add2(&macro_str1, tok, &tokc);
2611 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2612 n, cstr.data, (char*)cstr.data + n);
2614 file->buf_ptr = p;
2615 cstr_reset(&cstr);
2618 tok_str_add2(&macro_str1, tok, &tokc);
2620 tok_str_add(&macro_str1, 0);
2621 return macro_str1.str;
2625 /* do macro substitution of macro_str and add result to
2626 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2627 inside to avoid recursing. */
2628 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2629 const int *macro_str, struct macro_level ** can_read_stream)
2631 Sym *s;
2632 int *macro_str1;
2633 const int *ptr;
2634 int t, ret, spc;
2635 CValue cval;
2636 struct macro_level ml;
2638 /* first scan for '##' operator handling */
2639 ptr = macro_str;
2640 macro_str1 = macro_twosharps(ptr);
2641 if (macro_str1)
2642 ptr = macro_str1;
2643 spc = 0;
2644 while (1) {
2645 /* NOTE: ptr == NULL can only happen if tokens are read from
2646 file stream due to a macro function call */
2647 if (ptr == NULL)
2648 break;
2649 TOK_GET(t, ptr, cval);
2650 if (t == 0)
2651 break;
2652 s = define_find(t);
2653 if (s != NULL) {
2654 /* if nested substitution, do nothing */
2655 if (sym_find2(*nested_list, t))
2656 goto no_subst;
2657 ml.p = macro_ptr;
2658 if (can_read_stream)
2659 ml.prev = *can_read_stream, *can_read_stream = &ml;
2660 macro_ptr = (int *)ptr;
2661 tok = t;
2662 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
2663 ptr = (int *)macro_ptr;
2664 macro_ptr = ml.p;
2665 if (can_read_stream && *can_read_stream == &ml)
2666 *can_read_stream = ml.prev;
2667 if (ret != 0)
2668 goto no_subst;
2669 } else {
2670 no_subst:
2671 if (!check_space(t, &spc))
2672 tok_str_add2(tok_str, t, &cval);
2675 if (macro_str1)
2676 tok_str_free(macro_str1);
2679 /* return next token with macro substitution */
2680 void next(void)
2682 Sym *nested_list, *s;
2683 TokenString str;
2684 struct macro_level *ml;
2686 redo:
2687 if (parse_flags & PARSE_FLAG_SPACES)
2688 next_nomacro_spc();
2689 else
2690 next_nomacro();
2691 if (!macro_ptr) {
2692 /* if not reading from macro substituted string, then try
2693 to substitute macros */
2694 if (tok >= TOK_IDENT &&
2695 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2696 s = define_find(tok);
2697 if (s) {
2698 /* we have a macro: we try to substitute */
2699 tok_str_new(&str);
2700 nested_list = NULL;
2701 ml = NULL;
2702 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
2703 /* substitution done, NOTE: maybe empty */
2704 tok_str_add(&str, 0);
2705 macro_ptr = str.str;
2706 macro_ptr_allocated = str.str;
2707 goto redo;
2711 } else {
2712 if (tok == 0) {
2713 /* end of macro or end of unget buffer */
2714 if (unget_buffer_enabled) {
2715 macro_ptr = unget_saved_macro_ptr;
2716 unget_buffer_enabled = 0;
2717 } else {
2718 /* end of macro string: free it */
2719 tok_str_free(macro_ptr_allocated);
2720 macro_ptr_allocated = NULL;
2721 macro_ptr = NULL;
2723 goto redo;
2727 /* convert preprocessor tokens into C tokens */
2728 if (tok == TOK_PPNUM &&
2729 (parse_flags & PARSE_FLAG_TOK_NUM)) {
2730 parse_number((char *)tokc.cstr->data);
2734 /* better than nothing, but needs extension to handle '-E' option
2735 correctly too */
2736 void preprocess_init(TCCState *s1)
2738 s1->include_stack_ptr = s1->include_stack;
2739 /* XXX: move that before to avoid having to initialize
2740 file->ifdef_stack_ptr ? */
2741 s1->ifdef_stack_ptr = s1->ifdef_stack;
2742 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2744 /* XXX: not ANSI compliant: bound checking says error */
2745 vtop = vstack - 1;
2746 s1->pack_stack[0] = 0;
2747 s1->pack_stack_ptr = s1->pack_stack;
2750 void preprocess_new()
2752 int i, c;
2753 const char *p, *r;
2754 TokenSym *ts;
2756 /* init isid table */
2757 for(i=CH_EOF;i<256;i++)
2758 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
2760 /* add all tokens */
2761 table_ident = NULL;
2762 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
2764 tok_ident = TOK_IDENT;
2765 p = tcc_keywords;
2766 while (*p) {
2767 r = p;
2768 for(;;) {
2769 c = *r++;
2770 if (c == '\0')
2771 break;
2773 ts = tok_alloc(p, r - p - 1);
2774 p = r;
2778 /* Preprocess the current file */
2779 int tcc_preprocess(TCCState *s1)
2781 Sym *define_start;
2782 BufferedFile *file_ref, **iptr, **iptr_new;
2783 int token_seen, line_ref, d;
2784 const char *s;
2786 preprocess_init(s1);
2787 define_start = define_stack;
2788 ch = file->buf_ptr[0];
2789 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
2790 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
2791 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
2792 token_seen = 0;
2793 line_ref = 0;
2794 file_ref = NULL;
2796 iptr = s1->include_stack_ptr;
2797 for (;;) {
2798 next();
2799 if (tok == TOK_EOF) {
2800 break;
2801 } else if (file != file_ref) {
2802 goto print_line;
2803 } else if (tok == TOK_LINEFEED) {
2804 if (!token_seen)
2805 continue;
2806 ++line_ref;
2807 token_seen = 0;
2808 } else if (!token_seen) {
2809 d = file->line_num - line_ref;
2810 if (file != file_ref || d < 0 || d >= 8) {
2811 print_line:
2812 iptr_new = s1->include_stack_ptr;
2813 s = iptr_new > iptr ? " 1"
2814 : iptr_new < iptr ? " 2"
2815 : iptr_new > s1->include_stack ? " 3"
2816 : ""
2818 iptr = iptr_new;
2819 fprintf(s1->outfile, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
2820 } else {
2821 while (d)
2822 fputs("\n", s1->outfile), --d;
2824 line_ref = (file_ref = file)->line_num;
2825 token_seen = tok != TOK_LINEFEED;
2826 if (!token_seen)
2827 continue;
2829 fputs(get_tok_str(tok, &tokc), s1->outfile);
2831 free_defines(define_start);
2832 return 0;
2835 /* defines handling */
2836 void define_push(int v, int macro_type, int *str, Sym *first_arg)
2838 Sym *s;
2840 s = sym_push2(&define_stack, v, macro_type, 0);
2841 s->d = str;
2842 s->next = first_arg;
2843 table_ident[v - TOK_IDENT]->sym_define = s;