Added missing file.
[tinycc/k1w1.git] / tccpp.c
blobeb8a621858d18d2b4a63ab9df3b5bc52e291d3f4
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 /* free define stack until top reaches 'b' */
828 void free_defines(Sym *b)
830 Sym *top, *top1;
831 int v;
833 top = define_stack;
834 while (top != b) {
835 top1 = top->prev;
836 /* do not free args or predefined defines */
837 if (top->d)
838 tok_str_free(top->d);
839 v = top->v;
840 if (v >= TOK_IDENT && v < tok_ident)
841 table_ident[v - TOK_IDENT]->sym_define = NULL;
842 sym_free(top);
843 top = top1;
845 define_stack = b;
848 /* label lookup */
849 Sym *label_find(int v)
851 v -= TOK_IDENT;
852 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
853 return NULL;
854 return table_ident[v]->sym_label;
857 Sym *label_push(Sym **ptop, int v, int flags)
859 Sym *s, **ps;
860 s = sym_push2(ptop, v, 0, 0);
861 s->r = flags;
862 ps = &table_ident[v - TOK_IDENT]->sym_label;
863 if (ptop == &global_label_stack) {
864 /* modify the top most local identifier, so that
865 sym_identifier will point to 's' when popped */
866 while (*ps != NULL)
867 ps = &(*ps)->prev_tok;
869 s->prev_tok = *ps;
870 *ps = s;
871 return s;
874 /* pop labels until element last is reached. Look if any labels are
875 undefined. Define symbols if '&&label' was used. */
876 void label_pop(Sym **ptop, Sym *slast)
878 Sym *s, *s1;
879 for(s = *ptop; s != slast; s = s1) {
880 s1 = s->prev;
881 if (s->r == LABEL_DECLARED) {
882 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
883 } else if (s->r == LABEL_FORWARD) {
884 error("label '%s' used but not defined",
885 get_tok_str(s->v, NULL));
886 } else {
887 if (s->c) {
888 /* define corresponding symbol. A size of
889 1 is put. */
890 put_extern_sym(s, cur_text_section, s->jnext, 1);
893 /* remove label */
894 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
895 sym_free(s);
897 *ptop = slast;
900 /* eval an expression for #if/#elif */
901 static int expr_preprocess(void)
903 int c, t;
904 TokenString str;
906 tok_str_new(&str);
907 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
908 next(); /* do macro subst */
909 if (tok == TOK_DEFINED) {
910 next_nomacro();
911 t = tok;
912 if (t == '(')
913 next_nomacro();
914 c = define_find(tok) != 0;
915 if (t == '(')
916 next_nomacro();
917 tok = TOK_CINT;
918 tokc.i = c;
919 } else if (tok >= TOK_IDENT) {
920 /* if undefined macro */
921 tok = TOK_CINT;
922 tokc.i = 0;
924 tok_str_add_tok(&str);
926 tok_str_add(&str, -1); /* simulate end of file */
927 tok_str_add(&str, 0);
928 /* now evaluate C constant expression */
929 macro_ptr = str.str;
930 next();
931 c = expr_const();
932 macro_ptr = NULL;
933 tok_str_free(str.str);
934 return c != 0;
937 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
938 static void tok_print(int *str)
940 int t;
941 CValue cval;
943 printf("<");
944 while (1) {
945 TOK_GET(t, str, cval);
946 if (!t)
947 break;
948 printf("%s", get_tok_str(t, &cval));
950 printf(">\n");
952 #endif
954 /* parse after #define */
955 void parse_define(void)
957 Sym *s, *first, **ps;
958 int v, t, varg, is_vaargs, spc;
959 TokenString str;
961 v = tok;
962 if (v < TOK_IDENT)
963 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
964 /* XXX: should check if same macro (ANSI) */
965 first = NULL;
966 t = MACRO_OBJ;
967 /* '(' must be just after macro definition for MACRO_FUNC */
968 next_nomacro_spc();
969 if (tok == '(') {
970 next_nomacro();
971 ps = &first;
972 while (tok != ')') {
973 varg = tok;
974 next_nomacro();
975 is_vaargs = 0;
976 if (varg == TOK_DOTS) {
977 varg = TOK___VA_ARGS__;
978 is_vaargs = 1;
979 } else if (tok == TOK_DOTS && gnu_ext) {
980 is_vaargs = 1;
981 next_nomacro();
983 if (varg < TOK_IDENT)
984 error("badly punctuated parameter list");
985 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
986 *ps = s;
987 ps = &s->next;
988 if (tok != ',')
989 break;
990 next_nomacro();
992 if (tok == ')')
993 next_nomacro_spc();
994 t = MACRO_FUNC;
996 tok_str_new(&str);
997 spc = 2;
998 /* EOF testing necessary for '-D' handling */
999 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1000 /* remove spaces around ## and after '#' */
1001 if (TOK_TWOSHARPS == tok) {
1002 if (1 == spc)
1003 --str.len;
1004 spc = 2;
1005 } else if ('#' == tok) {
1006 spc = 2;
1007 } else if (check_space(tok, &spc)) {
1008 goto skip;
1010 tok_str_add2(&str, tok, &tokc);
1011 skip:
1012 next_nomacro_spc();
1014 if (spc == 1)
1015 --str.len; /* remove trailing space */
1016 tok_str_add(&str, 0);
1017 #ifdef PP_DEBUG
1018 printf("define %s %d: ", get_tok_str(v, NULL), t);
1019 tok_print(str.str);
1020 #endif
1021 define_push(v, t, str.str, first);
1024 static inline int hash_cached_include(int type, const char *filename)
1026 const unsigned char *s;
1027 unsigned int h;
1029 h = TOK_HASH_INIT;
1030 h = TOK_HASH_FUNC(h, type);
1031 s = filename;
1032 while (*s) {
1033 h = TOK_HASH_FUNC(h, *s);
1034 s++;
1036 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1037 return h;
1040 /* XXX: use a token or a hash table to accelerate matching ? */
1041 static CachedInclude *search_cached_include(TCCState *s1,
1042 int type, const char *filename)
1044 CachedInclude *e;
1045 int i, h;
1046 h = hash_cached_include(type, filename);
1047 i = s1->cached_includes_hash[h];
1048 for(;;) {
1049 if (i == 0)
1050 break;
1051 e = s1->cached_includes[i - 1];
1052 if (e->type == type && !PATHCMP(e->filename, filename))
1053 return e;
1054 i = e->hash_next;
1056 return NULL;
1059 static inline void add_cached_include(TCCState *s1, int type,
1060 const char *filename, int ifndef_macro)
1062 CachedInclude *e;
1063 int h;
1065 if (search_cached_include(s1, type, filename))
1066 return;
1067 #ifdef INC_DEBUG
1068 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1069 #endif
1070 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1071 if (!e)
1072 return;
1073 e->type = type;
1074 strcpy(e->filename, filename);
1075 e->ifndef_macro = ifndef_macro;
1076 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1077 /* add in hash table */
1078 h = hash_cached_include(type, filename);
1079 e->hash_next = s1->cached_includes_hash[h];
1080 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1083 static void pragma_parse(TCCState *s1)
1085 int val;
1087 next();
1088 if (tok == TOK_pack) {
1090 This may be:
1091 #pragma pack(1) // set
1092 #pragma pack() // reset to default
1093 #pragma pack(push,1) // push & set
1094 #pragma pack(pop) // restore previous
1096 next();
1097 skip('(');
1098 if (tok == TOK_ASM_pop) {
1099 next();
1100 if (s1->pack_stack_ptr <= s1->pack_stack) {
1101 stk_error:
1102 error("out of pack stack");
1104 s1->pack_stack_ptr--;
1105 } else {
1106 val = 0;
1107 if (tok != ')') {
1108 if (tok == TOK_ASM_push) {
1109 next();
1110 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1111 goto stk_error;
1112 s1->pack_stack_ptr++;
1113 skip(',');
1115 if (tok != TOK_CINT) {
1116 pack_error:
1117 error("invalid pack pragma");
1119 val = tokc.i;
1120 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1121 goto pack_error;
1122 next();
1124 *s1->pack_stack_ptr = val;
1125 skip(')');
1130 /* is_bof is true if first non space token at beginning of file */
1131 static void preprocess(int is_bof)
1133 TCCState *s1 = tcc_state;
1134 int i, c, n, saved_parse_flags;
1135 char buf[1024], *q;
1136 Sym *s;
1138 saved_parse_flags = parse_flags;
1139 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1140 PARSE_FLAG_LINEFEED;
1141 next_nomacro();
1142 redo:
1143 switch(tok) {
1144 case TOK_DEFINE:
1145 next_nomacro();
1146 parse_define();
1147 break;
1148 case TOK_UNDEF:
1149 next_nomacro();
1150 s = define_find(tok);
1151 /* undefine symbol by putting an invalid name */
1152 if (s)
1153 define_undef(s);
1154 break;
1155 case TOK_INCLUDE:
1156 case TOK_INCLUDE_NEXT:
1157 ch = file->buf_ptr[0];
1158 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1159 skip_spaces();
1160 if (ch == '<') {
1161 c = '>';
1162 goto read_name;
1163 } else if (ch == '\"') {
1164 c = ch;
1165 read_name:
1166 inp();
1167 q = buf;
1168 while (ch != c && ch != '\n' && ch != CH_EOF) {
1169 if ((q - buf) < sizeof(buf) - 1)
1170 *q++ = ch;
1171 if (ch == '\\') {
1172 if (handle_stray_noerror() == 0)
1173 --q;
1174 } else
1175 inp();
1177 *q = '\0';
1178 minp();
1179 #if 0
1180 /* eat all spaces and comments after include */
1181 /* XXX: slightly incorrect */
1182 while (ch1 != '\n' && ch1 != CH_EOF)
1183 inp();
1184 #endif
1185 } else {
1186 /* computed #include : either we have only strings or
1187 we have anything enclosed in '<>' */
1188 next();
1189 buf[0] = '\0';
1190 if (tok == TOK_STR) {
1191 while (tok != TOK_LINEFEED) {
1192 if (tok != TOK_STR) {
1193 include_syntax:
1194 error("'#include' expects \"FILENAME\" or <FILENAME>");
1196 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1197 next();
1199 c = '\"';
1200 } else {
1201 int len;
1202 while (tok != TOK_LINEFEED) {
1203 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1204 next();
1206 len = strlen(buf);
1207 /* check syntax and remove '<>' */
1208 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1209 goto include_syntax;
1210 memmove(buf, buf + 1, len - 2);
1211 buf[len - 2] = '\0';
1212 c = '>';
1216 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1217 error("#include recursion too deep");
1219 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1220 for (i = -2; i < n; ++i) {
1221 char buf1[sizeof file->filename];
1222 BufferedFile *f;
1223 CachedInclude *e;
1224 const char *path;
1225 int size;
1227 if (i == -2) {
1228 /* check absolute include path */
1229 if (!IS_ABSPATH(buf))
1230 continue;
1231 buf1[0] = 0;
1233 } else if (i == -1) {
1234 /* search in current dir if "header.h" */
1235 if (c != '\"')
1236 continue;
1237 size = tcc_basename(file->filename) - file->filename;
1238 memcpy(buf1, file->filename, size);
1239 buf1[size] = '\0';
1241 } else {
1242 /* search in all the include paths */
1243 if (i < s1->nb_include_paths)
1244 path = s1->include_paths[i];
1245 else
1246 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1247 pstrcpy(buf1, sizeof(buf1), path);
1248 pstrcat(buf1, sizeof(buf1), "/");
1251 pstrcat(buf1, sizeof(buf1), buf);
1253 e = search_cached_include(s1, c, buf1);
1254 if (e && define_find(e->ifndef_macro)) {
1255 /* no need to parse the include because the 'ifndef macro'
1256 is defined */
1257 #ifdef INC_DEBUG
1258 printf("%s: skipping %s\n", file->filename, buf);
1259 #endif
1260 f = NULL;
1261 } else {
1262 f = tcc_open(s1, buf1);
1263 if (!f)
1264 continue;
1267 if (tok == TOK_INCLUDE_NEXT) {
1268 tok = TOK_INCLUDE;
1269 if (f)
1270 tcc_close(f);
1271 continue;
1274 if (!f)
1275 goto include_done;
1277 #ifdef INC_DEBUG
1278 printf("%s: including %s\n", file->filename, buf1);
1279 #endif
1281 /* XXX: fix current line init */
1282 /* push current file in stack */
1283 *s1->include_stack_ptr++ = file;
1284 f->inc_type = c;
1285 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
1286 file = f;
1287 /* add include file debug info */
1288 if (tcc_state->do_debug) {
1289 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1291 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1292 ch = file->buf_ptr[0];
1293 goto the_end;
1295 error("include file '%s' not found", buf);
1296 include_done:
1297 break;
1298 case TOK_IFNDEF:
1299 c = 1;
1300 goto do_ifdef;
1301 case TOK_IF:
1302 c = expr_preprocess();
1303 goto do_if;
1304 case TOK_IFDEF:
1305 c = 0;
1306 do_ifdef:
1307 next_nomacro();
1308 if (tok < TOK_IDENT)
1309 error("invalid argument for '#if%sdef'", c ? "n" : "");
1310 if (is_bof) {
1311 if (c) {
1312 #ifdef INC_DEBUG
1313 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1314 #endif
1315 file->ifndef_macro = tok;
1318 c = (define_find(tok) != 0) ^ c;
1319 do_if:
1320 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1321 error("memory full");
1322 *s1->ifdef_stack_ptr++ = c;
1323 goto test_skip;
1324 case TOK_ELSE:
1325 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1326 error("#else without matching #if");
1327 if (s1->ifdef_stack_ptr[-1] & 2)
1328 error("#else after #else");
1329 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1330 goto test_else;
1331 case TOK_ELIF:
1332 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1333 error("#elif without matching #if");
1334 c = s1->ifdef_stack_ptr[-1];
1335 if (c > 1)
1336 error("#elif after #else");
1337 /* last #if/#elif expression was true: we skip */
1338 if (c == 1)
1339 goto skip;
1340 c = expr_preprocess();
1341 s1->ifdef_stack_ptr[-1] = c;
1342 test_else:
1343 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1344 file->ifndef_macro = 0;
1345 test_skip:
1346 if (!(c & 1)) {
1347 skip:
1348 preprocess_skip();
1349 is_bof = 0;
1350 goto redo;
1352 break;
1353 case TOK_ENDIF:
1354 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1355 error("#endif without matching #if");
1356 s1->ifdef_stack_ptr--;
1357 /* '#ifndef macro' was at the start of file. Now we check if
1358 an '#endif' is exactly at the end of file */
1359 if (file->ifndef_macro &&
1360 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1361 file->ifndef_macro_saved = file->ifndef_macro;
1362 /* need to set to zero to avoid false matches if another
1363 #ifndef at middle of file */
1364 file->ifndef_macro = 0;
1365 while (tok != TOK_LINEFEED)
1366 next_nomacro();
1367 tok_flags |= TOK_FLAG_ENDIF;
1368 goto the_end;
1370 break;
1371 case TOK_LINE:
1372 next();
1373 if (tok != TOK_CINT)
1374 error("#line");
1375 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1376 next();
1377 if (tok != TOK_LINEFEED) {
1378 if (tok != TOK_STR)
1379 error("#line");
1380 pstrcpy(file->filename, sizeof(file->filename),
1381 (char *)tokc.cstr->data);
1383 break;
1384 case TOK_ERROR:
1385 case TOK_WARNING:
1386 c = tok;
1387 ch = file->buf_ptr[0];
1388 skip_spaces();
1389 q = buf;
1390 while (ch != '\n' && ch != CH_EOF) {
1391 if ((q - buf) < sizeof(buf) - 1)
1392 *q++ = ch;
1393 if (ch == '\\') {
1394 if (handle_stray_noerror() == 0)
1395 --q;
1396 } else
1397 inp();
1399 *q = '\0';
1400 if (c == TOK_ERROR)
1401 error("#error %s", buf);
1402 else
1403 warning("#warning %s", buf);
1404 break;
1405 case TOK_PRAGMA:
1406 pragma_parse(s1);
1407 break;
1408 default:
1409 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
1410 /* '!' is ignored to allow C scripts. numbers are ignored
1411 to emulate cpp behaviour */
1412 } else {
1413 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1414 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1416 break;
1418 /* ignore other preprocess commands or #! for C scripts */
1419 while (tok != TOK_LINEFEED)
1420 next_nomacro();
1421 the_end:
1422 parse_flags = saved_parse_flags;
1425 /* evaluate escape codes in a string. */
1426 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1428 int c, n;
1429 const uint8_t *p;
1431 p = buf;
1432 for(;;) {
1433 c = *p;
1434 if (c == '\0')
1435 break;
1436 if (c == '\\') {
1437 p++;
1438 /* escape */
1439 c = *p;
1440 switch(c) {
1441 case '0': case '1': case '2': case '3':
1442 case '4': case '5': case '6': case '7':
1443 /* at most three octal digits */
1444 n = c - '0';
1445 p++;
1446 c = *p;
1447 if (isoct(c)) {
1448 n = n * 8 + c - '0';
1449 p++;
1450 c = *p;
1451 if (isoct(c)) {
1452 n = n * 8 + c - '0';
1453 p++;
1456 c = n;
1457 goto add_char_nonext;
1458 case 'x':
1459 case 'u':
1460 case 'U':
1461 p++;
1462 n = 0;
1463 for(;;) {
1464 c = *p;
1465 if (c >= 'a' && c <= 'f')
1466 c = c - 'a' + 10;
1467 else if (c >= 'A' && c <= 'F')
1468 c = c - 'A' + 10;
1469 else if (isnum(c))
1470 c = c - '0';
1471 else
1472 break;
1473 n = n * 16 + c;
1474 p++;
1476 c = n;
1477 goto add_char_nonext;
1478 case 'a':
1479 c = '\a';
1480 break;
1481 case 'b':
1482 c = '\b';
1483 break;
1484 case 'f':
1485 c = '\f';
1486 break;
1487 case 'n':
1488 c = '\n';
1489 break;
1490 case 'r':
1491 c = '\r';
1492 break;
1493 case 't':
1494 c = '\t';
1495 break;
1496 case 'v':
1497 c = '\v';
1498 break;
1499 case 'e':
1500 if (!gnu_ext)
1501 goto invalid_escape;
1502 c = 27;
1503 break;
1504 case '\'':
1505 case '\"':
1506 case '\\':
1507 case '?':
1508 break;
1509 default:
1510 invalid_escape:
1511 if (c >= '!' && c <= '~')
1512 warning("unknown escape sequence: \'\\%c\'", c);
1513 else
1514 warning("unknown escape sequence: \'\\x%x\'", c);
1515 break;
1518 p++;
1519 add_char_nonext:
1520 if (!is_long)
1521 cstr_ccat(outstr, c);
1522 else
1523 cstr_wccat(outstr, c);
1525 /* add a trailing '\0' */
1526 if (!is_long)
1527 cstr_ccat(outstr, '\0');
1528 else
1529 cstr_wccat(outstr, '\0');
1532 /* we use 64 bit numbers */
1533 #define BN_SIZE 2
1535 /* bn = (bn << shift) | or_val */
1536 void bn_lshift(unsigned int *bn, int shift, int or_val)
1538 int i;
1539 unsigned int v;
1540 for(i=0;i<BN_SIZE;i++) {
1541 v = bn[i];
1542 bn[i] = (v << shift) | or_val;
1543 or_val = v >> (32 - shift);
1547 void bn_zero(unsigned int *bn)
1549 int i;
1550 for(i=0;i<BN_SIZE;i++) {
1551 bn[i] = 0;
1555 /* parse number in null terminated string 'p' and return it in the
1556 current token */
1557 void parse_number(const char *p)
1559 int b, t, shift, frac_bits, s, exp_val, ch;
1560 char *q;
1561 unsigned int bn[BN_SIZE];
1562 double d;
1564 /* number */
1565 q = token_buf;
1566 ch = *p++;
1567 t = ch;
1568 ch = *p++;
1569 *q++ = t;
1570 b = 10;
1571 if (t == '.') {
1572 goto float_frac_parse;
1573 } else if (t == '0') {
1574 if (ch == 'x' || ch == 'X') {
1575 q--;
1576 ch = *p++;
1577 b = 16;
1578 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1579 q--;
1580 ch = *p++;
1581 b = 2;
1584 /* parse all digits. cannot check octal numbers at this stage
1585 because of floating point constants */
1586 while (1) {
1587 if (ch >= 'a' && ch <= 'f')
1588 t = ch - 'a' + 10;
1589 else if (ch >= 'A' && ch <= 'F')
1590 t = ch - 'A' + 10;
1591 else if (isnum(ch))
1592 t = ch - '0';
1593 else
1594 break;
1595 if (t >= b)
1596 break;
1597 if (q >= token_buf + STRING_MAX_SIZE) {
1598 num_too_long:
1599 error("number too long");
1601 *q++ = ch;
1602 ch = *p++;
1604 if (ch == '.' ||
1605 ((ch == 'e' || ch == 'E') && b == 10) ||
1606 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1607 if (b != 10) {
1608 /* NOTE: strtox should support that for hexa numbers, but
1609 non ISOC99 libcs do not support it, so we prefer to do
1610 it by hand */
1611 /* hexadecimal or binary floats */
1612 /* XXX: handle overflows */
1613 *q = '\0';
1614 if (b == 16)
1615 shift = 4;
1616 else
1617 shift = 2;
1618 bn_zero(bn);
1619 q = token_buf;
1620 while (1) {
1621 t = *q++;
1622 if (t == '\0') {
1623 break;
1624 } else if (t >= 'a') {
1625 t = t - 'a' + 10;
1626 } else if (t >= 'A') {
1627 t = t - 'A' + 10;
1628 } else {
1629 t = t - '0';
1631 bn_lshift(bn, shift, t);
1633 frac_bits = 0;
1634 if (ch == '.') {
1635 ch = *p++;
1636 while (1) {
1637 t = ch;
1638 if (t >= 'a' && t <= 'f') {
1639 t = t - 'a' + 10;
1640 } else if (t >= 'A' && t <= 'F') {
1641 t = t - 'A' + 10;
1642 } else if (t >= '0' && t <= '9') {
1643 t = t - '0';
1644 } else {
1645 break;
1647 if (t >= b)
1648 error("invalid digit");
1649 bn_lshift(bn, shift, t);
1650 frac_bits += shift;
1651 ch = *p++;
1654 if (ch != 'p' && ch != 'P')
1655 expect("exponent");
1656 ch = *p++;
1657 s = 1;
1658 exp_val = 0;
1659 if (ch == '+') {
1660 ch = *p++;
1661 } else if (ch == '-') {
1662 s = -1;
1663 ch = *p++;
1665 if (ch < '0' || ch > '9')
1666 expect("exponent digits");
1667 while (ch >= '0' && ch <= '9') {
1668 exp_val = exp_val * 10 + ch - '0';
1669 ch = *p++;
1671 exp_val = exp_val * s;
1673 /* now we can generate the number */
1674 /* XXX: should patch directly float number */
1675 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1676 d = ldexp(d, exp_val - frac_bits);
1677 t = toup(ch);
1678 if (t == 'F') {
1679 ch = *p++;
1680 tok = TOK_CFLOAT;
1681 /* float : should handle overflow */
1682 tokc.f = (float)d;
1683 } else if (t == 'L') {
1684 ch = *p++;
1685 #ifdef TCC_TARGET_PE
1686 tok = TOK_CDOUBLE;
1687 tokc.d = d;
1688 #else
1689 tok = TOK_CLDOUBLE;
1690 /* XXX: not large enough */
1691 tokc.ld = (long double)d;
1692 #endif
1693 } else {
1694 tok = TOK_CDOUBLE;
1695 tokc.d = d;
1697 } else {
1698 /* decimal floats */
1699 if (ch == '.') {
1700 if (q >= token_buf + STRING_MAX_SIZE)
1701 goto num_too_long;
1702 *q++ = ch;
1703 ch = *p++;
1704 float_frac_parse:
1705 while (ch >= '0' && ch <= '9') {
1706 if (q >= token_buf + STRING_MAX_SIZE)
1707 goto num_too_long;
1708 *q++ = ch;
1709 ch = *p++;
1712 if (ch == 'e' || ch == 'E') {
1713 if (q >= token_buf + STRING_MAX_SIZE)
1714 goto num_too_long;
1715 *q++ = ch;
1716 ch = *p++;
1717 if (ch == '-' || ch == '+') {
1718 if (q >= token_buf + STRING_MAX_SIZE)
1719 goto num_too_long;
1720 *q++ = ch;
1721 ch = *p++;
1723 if (ch < '0' || ch > '9')
1724 expect("exponent digits");
1725 while (ch >= '0' && ch <= '9') {
1726 if (q >= token_buf + STRING_MAX_SIZE)
1727 goto num_too_long;
1728 *q++ = ch;
1729 ch = *p++;
1732 *q = '\0';
1733 t = toup(ch);
1734 errno = 0;
1735 if (t == 'F') {
1736 ch = *p++;
1737 tok = TOK_CFLOAT;
1738 tokc.f = strtof(token_buf, NULL);
1739 } else if (t == 'L') {
1740 ch = *p++;
1741 #ifdef TCC_TARGET_PE
1742 tok = TOK_CDOUBLE;
1743 tokc.d = strtod(token_buf, NULL);
1744 #else
1745 tok = TOK_CLDOUBLE;
1746 tokc.ld = strtold(token_buf, NULL);
1747 #endif
1748 } else {
1749 tok = TOK_CDOUBLE;
1750 tokc.d = strtod(token_buf, NULL);
1753 } else {
1754 unsigned long long n, n1;
1755 int lcount, ucount;
1757 /* integer number */
1758 *q = '\0';
1759 q = token_buf;
1760 if (b == 10 && *q == '0') {
1761 b = 8;
1762 q++;
1764 n = 0;
1765 while(1) {
1766 t = *q++;
1767 /* no need for checks except for base 10 / 8 errors */
1768 if (t == '\0') {
1769 break;
1770 } else if (t >= 'a') {
1771 t = t - 'a' + 10;
1772 } else if (t >= 'A') {
1773 t = t - 'A' + 10;
1774 } else {
1775 t = t - '0';
1776 if (t >= b)
1777 error("invalid digit");
1779 n1 = n;
1780 n = n * b + t;
1781 /* detect overflow */
1782 /* XXX: this test is not reliable */
1783 if (n < n1)
1784 error("integer constant overflow");
1787 /* XXX: not exactly ANSI compliant */
1788 if ((n & 0xffffffff00000000LL) != 0) {
1789 if ((n >> 63) != 0)
1790 tok = TOK_CULLONG;
1791 else
1792 tok = TOK_CLLONG;
1793 } else if (n > 0x7fffffff) {
1794 tok = TOK_CUINT;
1795 } else {
1796 tok = TOK_CINT;
1798 lcount = 0;
1799 ucount = 0;
1800 for(;;) {
1801 t = toup(ch);
1802 if (t == 'L') {
1803 if (lcount >= 2)
1804 error("three 'l's in integer constant");
1805 lcount++;
1806 if (lcount == 2) {
1807 if (tok == TOK_CINT)
1808 tok = TOK_CLLONG;
1809 else if (tok == TOK_CUINT)
1810 tok = TOK_CULLONG;
1812 ch = *p++;
1813 } else if (t == 'U') {
1814 if (ucount >= 1)
1815 error("two 'u's in integer constant");
1816 ucount++;
1817 if (tok == TOK_CINT)
1818 tok = TOK_CUINT;
1819 else if (tok == TOK_CLLONG)
1820 tok = TOK_CULLONG;
1821 ch = *p++;
1822 } else {
1823 break;
1826 if (tok == TOK_CINT || tok == TOK_CUINT)
1827 tokc.ui = n;
1828 else
1829 tokc.ull = n;
1831 if (ch)
1832 error("invalid number\n");
1836 #define PARSE2(c1, tok1, c2, tok2) \
1837 case c1: \
1838 PEEKC(c, p); \
1839 if (c == c2) { \
1840 p++; \
1841 tok = tok2; \
1842 } else { \
1843 tok = tok1; \
1845 break;
1847 /* return next token without macro substitution */
1848 static inline void next_nomacro1(void)
1850 int t, c, is_long;
1851 TokenSym *ts;
1852 uint8_t *p, *p1;
1853 unsigned int h;
1855 p = file->buf_ptr;
1856 redo_no_start:
1857 c = *p;
1858 switch(c) {
1859 case ' ':
1860 case '\t':
1861 tok = c;
1862 p++;
1863 goto keep_tok_flags;
1864 case '\f':
1865 case '\v':
1866 case '\r':
1867 p++;
1868 goto redo_no_start;
1869 case '\\':
1870 /* first look if it is in fact an end of buffer */
1871 if (p >= file->buf_end) {
1872 file->buf_ptr = p;
1873 handle_eob();
1874 p = file->buf_ptr;
1875 if (p >= file->buf_end)
1876 goto parse_eof;
1877 else
1878 goto redo_no_start;
1879 } else {
1880 file->buf_ptr = p;
1881 ch = *p;
1882 handle_stray();
1883 p = file->buf_ptr;
1884 goto redo_no_start;
1886 parse_eof:
1888 TCCState *s1 = tcc_state;
1889 if ((parse_flags & PARSE_FLAG_LINEFEED)
1890 && !(tok_flags & TOK_FLAG_EOF)) {
1891 tok_flags |= TOK_FLAG_EOF;
1892 tok = TOK_LINEFEED;
1893 goto keep_tok_flags;
1894 } else if (s1->include_stack_ptr == s1->include_stack ||
1895 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
1896 /* no include left : end of file. */
1897 tok = TOK_EOF;
1898 } else {
1899 tok_flags &= ~TOK_FLAG_EOF;
1900 /* pop include file */
1902 /* test if previous '#endif' was after a #ifdef at
1903 start of file */
1904 if (tok_flags & TOK_FLAG_ENDIF) {
1905 #ifdef INC_DEBUG
1906 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
1907 #endif
1908 add_cached_include(s1, file->inc_type, file->inc_filename,
1909 file->ifndef_macro_saved);
1912 /* add end of include file debug info */
1913 if (tcc_state->do_debug) {
1914 put_stabd(N_EINCL, 0, 0);
1916 /* pop include stack */
1917 tcc_close(file);
1918 s1->include_stack_ptr--;
1919 file = *s1->include_stack_ptr;
1920 p = file->buf_ptr;
1921 goto redo_no_start;
1924 break;
1926 case '\n':
1927 file->line_num++;
1928 tok_flags |= TOK_FLAG_BOL;
1929 p++;
1930 maybe_newline:
1931 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
1932 goto redo_no_start;
1933 tok = TOK_LINEFEED;
1934 goto keep_tok_flags;
1936 case '#':
1937 /* XXX: simplify */
1938 PEEKC(c, p);
1939 if ((tok_flags & TOK_FLAG_BOL) &&
1940 (parse_flags & PARSE_FLAG_PREPROCESS)) {
1941 file->buf_ptr = p;
1942 preprocess(tok_flags & TOK_FLAG_BOF);
1943 p = file->buf_ptr;
1944 goto maybe_newline;
1945 } else {
1946 if (c == '#') {
1947 p++;
1948 tok = TOK_TWOSHARPS;
1949 } else {
1950 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
1951 p = parse_line_comment(p - 1);
1952 goto redo_no_start;
1953 } else {
1954 tok = '#';
1958 break;
1960 case 'a': case 'b': case 'c': case 'd':
1961 case 'e': case 'f': case 'g': case 'h':
1962 case 'i': case 'j': case 'k': case 'l':
1963 case 'm': case 'n': case 'o': case 'p':
1964 case 'q': case 'r': case 's': case 't':
1965 case 'u': case 'v': case 'w': case 'x':
1966 case 'y': case 'z':
1967 case 'A': case 'B': case 'C': case 'D':
1968 case 'E': case 'F': case 'G': case 'H':
1969 case 'I': case 'J': case 'K':
1970 case 'M': case 'N': case 'O': case 'P':
1971 case 'Q': case 'R': case 'S': case 'T':
1972 case 'U': case 'V': case 'W': case 'X':
1973 case 'Y': case 'Z':
1974 case '_':
1975 parse_ident_fast:
1976 p1 = p;
1977 h = TOK_HASH_INIT;
1978 h = TOK_HASH_FUNC(h, c);
1979 p++;
1980 for(;;) {
1981 c = *p;
1982 if (!isidnum_table[c-CH_EOF])
1983 break;
1984 h = TOK_HASH_FUNC(h, c);
1985 p++;
1987 if (c != '\\') {
1988 TokenSym **pts;
1989 int len;
1991 /* fast case : no stray found, so we have the full token
1992 and we have already hashed it */
1993 len = p - p1;
1994 h &= (TOK_HASH_SIZE - 1);
1995 pts = &hash_ident[h];
1996 for(;;) {
1997 ts = *pts;
1998 if (!ts)
1999 break;
2000 if (ts->len == len && !memcmp(ts->str, p1, len))
2001 goto token_found;
2002 pts = &(ts->hash_next);
2004 ts = tok_alloc_new(pts, p1, len);
2005 token_found: ;
2006 } else {
2007 /* slower case */
2008 cstr_reset(&tokcstr);
2010 while (p1 < p) {
2011 cstr_ccat(&tokcstr, *p1);
2012 p1++;
2014 p--;
2015 PEEKC(c, p);
2016 parse_ident_slow:
2017 while (isidnum_table[c-CH_EOF]) {
2018 cstr_ccat(&tokcstr, c);
2019 PEEKC(c, p);
2021 ts = tok_alloc(tokcstr.data, tokcstr.size);
2023 tok = ts->tok;
2024 break;
2025 case 'L':
2026 t = p[1];
2027 if (t != '\\' && t != '\'' && t != '\"') {
2028 /* fast case */
2029 goto parse_ident_fast;
2030 } else {
2031 PEEKC(c, p);
2032 if (c == '\'' || c == '\"') {
2033 is_long = 1;
2034 goto str_const;
2035 } else {
2036 cstr_reset(&tokcstr);
2037 cstr_ccat(&tokcstr, 'L');
2038 goto parse_ident_slow;
2041 break;
2042 case '0': case '1': case '2': case '3':
2043 case '4': case '5': case '6': case '7':
2044 case '8': case '9':
2046 cstr_reset(&tokcstr);
2047 /* after the first digit, accept digits, alpha, '.' or sign if
2048 prefixed by 'eEpP' */
2049 parse_num:
2050 for(;;) {
2051 t = c;
2052 cstr_ccat(&tokcstr, c);
2053 PEEKC(c, p);
2054 if (!(isnum(c) || isid(c) || c == '.' ||
2055 ((c == '+' || c == '-') &&
2056 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2057 break;
2059 /* We add a trailing '\0' to ease parsing */
2060 cstr_ccat(&tokcstr, '\0');
2061 tokc.cstr = &tokcstr;
2062 tok = TOK_PPNUM;
2063 break;
2064 case '.':
2065 /* special dot handling because it can also start a number */
2066 PEEKC(c, p);
2067 if (isnum(c)) {
2068 cstr_reset(&tokcstr);
2069 cstr_ccat(&tokcstr, '.');
2070 goto parse_num;
2071 } else if (c == '.') {
2072 PEEKC(c, p);
2073 if (c != '.')
2074 expect("'.'");
2075 PEEKC(c, p);
2076 tok = TOK_DOTS;
2077 } else {
2078 tok = '.';
2080 break;
2081 case '\'':
2082 case '\"':
2083 is_long = 0;
2084 str_const:
2086 CString str;
2087 int sep;
2089 sep = c;
2091 /* parse the string */
2092 cstr_new(&str);
2093 p = parse_pp_string(p, sep, &str);
2094 cstr_ccat(&str, '\0');
2096 /* eval the escape (should be done as TOK_PPNUM) */
2097 cstr_reset(&tokcstr);
2098 parse_escape_string(&tokcstr, str.data, is_long);
2099 cstr_free(&str);
2101 if (sep == '\'') {
2102 int char_size;
2103 /* XXX: make it portable */
2104 if (!is_long)
2105 char_size = 1;
2106 else
2107 char_size = sizeof(nwchar_t);
2108 if (tokcstr.size <= char_size)
2109 error("empty character constant");
2110 if (tokcstr.size > 2 * char_size)
2111 warning("multi-character character constant");
2112 if (!is_long) {
2113 tokc.i = *(int8_t *)tokcstr.data;
2114 tok = TOK_CCHAR;
2115 } else {
2116 tokc.i = *(nwchar_t *)tokcstr.data;
2117 tok = TOK_LCHAR;
2119 } else {
2120 tokc.cstr = &tokcstr;
2121 if (!is_long)
2122 tok = TOK_STR;
2123 else
2124 tok = TOK_LSTR;
2127 break;
2129 case '<':
2130 PEEKC(c, p);
2131 if (c == '=') {
2132 p++;
2133 tok = TOK_LE;
2134 } else if (c == '<') {
2135 PEEKC(c, p);
2136 if (c == '=') {
2137 p++;
2138 tok = TOK_A_SHL;
2139 } else {
2140 tok = TOK_SHL;
2142 } else {
2143 tok = TOK_LT;
2145 break;
2147 case '>':
2148 PEEKC(c, p);
2149 if (c == '=') {
2150 p++;
2151 tok = TOK_GE;
2152 } else if (c == '>') {
2153 PEEKC(c, p);
2154 if (c == '=') {
2155 p++;
2156 tok = TOK_A_SAR;
2157 } else {
2158 tok = TOK_SAR;
2160 } else {
2161 tok = TOK_GT;
2163 break;
2165 case '&':
2166 PEEKC(c, p);
2167 if (c == '&') {
2168 p++;
2169 tok = TOK_LAND;
2170 } else if (c == '=') {
2171 p++;
2172 tok = TOK_A_AND;
2173 } else {
2174 tok = '&';
2176 break;
2178 case '|':
2179 PEEKC(c, p);
2180 if (c == '|') {
2181 p++;
2182 tok = TOK_LOR;
2183 } else if (c == '=') {
2184 p++;
2185 tok = TOK_A_OR;
2186 } else {
2187 tok = '|';
2189 break;
2191 case '+':
2192 PEEKC(c, p);
2193 if (c == '+') {
2194 p++;
2195 tok = TOK_INC;
2196 } else if (c == '=') {
2197 p++;
2198 tok = TOK_A_ADD;
2199 } else {
2200 tok = '+';
2202 break;
2204 case '-':
2205 PEEKC(c, p);
2206 if (c == '-') {
2207 p++;
2208 tok = TOK_DEC;
2209 } else if (c == '=') {
2210 p++;
2211 tok = TOK_A_SUB;
2212 } else if (c == '>') {
2213 p++;
2214 tok = TOK_ARROW;
2215 } else {
2216 tok = '-';
2218 break;
2220 PARSE2('!', '!', '=', TOK_NE)
2221 PARSE2('=', '=', '=', TOK_EQ)
2222 PARSE2('*', '*', '=', TOK_A_MUL)
2223 PARSE2('%', '%', '=', TOK_A_MOD)
2224 PARSE2('^', '^', '=', TOK_A_XOR)
2226 /* comments or operator */
2227 case '/':
2228 PEEKC(c, p);
2229 if (c == '*') {
2230 p = parse_comment(p);
2231 goto redo_no_start;
2232 } else if (c == '/') {
2233 p = parse_line_comment(p);
2234 goto redo_no_start;
2235 } else if (c == '=') {
2236 p++;
2237 tok = TOK_A_DIV;
2238 } else {
2239 tok = '/';
2241 break;
2243 /* simple tokens */
2244 case '(':
2245 case ')':
2246 case '[':
2247 case ']':
2248 case '{':
2249 case '}':
2250 case ',':
2251 case ';':
2252 case ':':
2253 case '?':
2254 case '~':
2255 case '$': /* only used in assembler */
2256 case '@': /* dito */
2257 tok = c;
2258 p++;
2259 break;
2260 default:
2261 error("unrecognized character \\x%02x", c);
2262 break;
2264 tok_flags = 0;
2265 keep_tok_flags:
2266 file->buf_ptr = p;
2267 #if defined(PARSE_DEBUG)
2268 printf("token = %s\n", get_tok_str(tok, &tokc));
2269 #endif
2272 /* return next token without macro substitution. Can read input from
2273 macro_ptr buffer */
2274 static void next_nomacro_spc(void)
2276 if (macro_ptr) {
2277 redo:
2278 tok = *macro_ptr;
2279 if (tok) {
2280 TOK_GET(tok, macro_ptr, tokc);
2281 if (tok == TOK_LINENUM) {
2282 file->line_num = tokc.i;
2283 goto redo;
2286 } else {
2287 next_nomacro1();
2291 void next_nomacro(void)
2293 do {
2294 next_nomacro_spc();
2295 } while (is_space(tok));
2298 /* substitute args in macro_str and return allocated string */
2299 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
2301 int *st, last_tok, t, spc;
2302 Sym *s;
2303 CValue cval;
2304 TokenString str;
2305 CString cstr;
2307 tok_str_new(&str);
2308 last_tok = 0;
2309 while(1) {
2310 TOK_GET(t, macro_str, cval);
2311 if (!t)
2312 break;
2313 if (t == '#') {
2314 /* stringize */
2315 TOK_GET(t, macro_str, cval);
2316 if (!t)
2317 break;
2318 s = sym_find2(args, t);
2319 if (s) {
2320 cstr_new(&cstr);
2321 st = s->d;
2322 spc = 0;
2323 while (*st) {
2324 TOK_GET(t, st, cval);
2325 if (!check_space(t, &spc))
2326 cstr_cat(&cstr, get_tok_str(t, &cval));
2328 cstr.size -= spc;
2329 cstr_ccat(&cstr, '\0');
2330 #ifdef PP_DEBUG
2331 printf("stringize: %s\n", (char *)cstr.data);
2332 #endif
2333 /* add string */
2334 cval.cstr = &cstr;
2335 tok_str_add2(&str, TOK_STR, &cval);
2336 cstr_free(&cstr);
2337 } else {
2338 tok_str_add2(&str, t, &cval);
2340 } else if (t >= TOK_IDENT) {
2341 s = sym_find2(args, t);
2342 if (s) {
2343 st = s->d;
2344 /* if '##' is present before or after, no arg substitution */
2345 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2346 /* special case for var arg macros : ## eats the
2347 ',' if empty VA_ARGS variable. */
2348 /* XXX: test of the ',' is not 100%
2349 reliable. should fix it to avoid security
2350 problems */
2351 if (gnu_ext && s->type.t &&
2352 last_tok == TOK_TWOSHARPS &&
2353 str.len >= 2 && str.str[str.len - 2] == ',') {
2354 if (*st == 0) {
2355 /* suppress ',' '##' */
2356 str.len -= 2;
2357 } else {
2358 /* suppress '##' and add variable */
2359 str.len--;
2360 goto add_var;
2362 } else {
2363 int t1;
2364 add_var:
2365 for(;;) {
2366 TOK_GET(t1, st, cval);
2367 if (!t1)
2368 break;
2369 tok_str_add2(&str, t1, &cval);
2372 } else {
2373 /* NOTE: the stream cannot be read when macro
2374 substituing an argument */
2375 macro_subst(&str, nested_list, st, NULL);
2377 } else {
2378 tok_str_add(&str, t);
2380 } else {
2381 tok_str_add2(&str, t, &cval);
2383 last_tok = t;
2385 tok_str_add(&str, 0);
2386 return str.str;
2389 static char const ab_month_name[12][4] =
2391 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2392 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2395 /* do macro substitution of current token with macro 's' and add
2396 result to (tok_str,tok_len). 'nested_list' is the list of all
2397 macros we got inside to avoid recursing. Return non zero if no
2398 substitution needs to be done */
2399 static int macro_subst_tok(TokenString *tok_str,
2400 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2402 Sym *args, *sa, *sa1;
2403 int mstr_allocated, parlevel, *mstr, t, t1, *p, spc;
2404 TokenString str;
2405 char *cstrval;
2406 CValue cval;
2407 CString cstr;
2408 char buf[32];
2410 /* if symbol is a macro, prepare substitution */
2411 /* special macros */
2412 if (tok == TOK___LINE__) {
2413 snprintf(buf, sizeof(buf), "%d", file->line_num);
2414 cstrval = buf;
2415 t1 = TOK_PPNUM;
2416 goto add_cstr1;
2417 } else if (tok == TOK___FILE__) {
2418 cstrval = file->filename;
2419 goto add_cstr;
2420 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2421 time_t ti;
2422 struct tm *tm;
2424 time(&ti);
2425 tm = localtime(&ti);
2426 if (tok == TOK___DATE__) {
2427 snprintf(buf, sizeof(buf), "%s %2d %d",
2428 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2429 } else {
2430 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2431 tm->tm_hour, tm->tm_min, tm->tm_sec);
2433 cstrval = buf;
2434 add_cstr:
2435 t1 = TOK_STR;
2436 add_cstr1:
2437 cstr_new(&cstr);
2438 cstr_cat(&cstr, cstrval);
2439 cstr_ccat(&cstr, '\0');
2440 cval.cstr = &cstr;
2441 tok_str_add2(tok_str, t1, &cval);
2442 cstr_free(&cstr);
2443 } else {
2444 mstr = s->d;
2445 mstr_allocated = 0;
2446 if (s->type.t == MACRO_FUNC) {
2447 /* NOTE: we do not use next_nomacro to avoid eating the
2448 next token. XXX: find better solution */
2449 redo:
2450 if (macro_ptr) {
2451 p = macro_ptr;
2452 while (is_space(t = *p) || TOK_LINEFEED == t)
2453 ++p;
2454 if (t == 0 && can_read_stream) {
2455 /* end of macro stream: we must look at the token
2456 after in the file */
2457 struct macro_level *ml = *can_read_stream;
2458 macro_ptr = NULL;
2459 if (ml)
2461 macro_ptr = ml->p;
2462 ml->p = NULL;
2463 *can_read_stream = ml -> prev;
2465 goto redo;
2467 } else {
2468 /* XXX: incorrect with comments */
2469 ch = file->buf_ptr[0];
2470 while (is_space(ch) || ch == '\n')
2471 cinp();
2472 t = ch;
2474 if (t != '(') /* no macro subst */
2475 return -1;
2477 /* argument macro */
2478 next_nomacro();
2479 next_nomacro();
2480 args = NULL;
2481 sa = s->next;
2482 /* NOTE: empty args are allowed, except if no args */
2483 for(;;) {
2484 /* handle '()' case */
2485 if (!args && !sa && tok == ')')
2486 break;
2487 if (!sa)
2488 error("macro '%s' used with too many args",
2489 get_tok_str(s->v, 0));
2490 tok_str_new(&str);
2491 parlevel = spc = 0;
2492 /* NOTE: non zero sa->t indicates VA_ARGS */
2493 while ((parlevel > 0 ||
2494 (tok != ')' &&
2495 (tok != ',' || sa->type.t))) &&
2496 tok != -1) {
2497 if (tok == '(')
2498 parlevel++;
2499 else if (tok == ')')
2500 parlevel--;
2501 if (tok == TOK_LINEFEED)
2502 tok = ' ';
2503 if (!check_space(tok, &spc))
2504 tok_str_add2(&str, tok, &tokc);
2505 next_nomacro_spc();
2507 str.len -= spc;
2508 tok_str_add(&str, 0);
2509 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2510 sa1->d = str.str;
2511 sa = sa->next;
2512 if (tok == ')') {
2513 /* special case for gcc var args: add an empty
2514 var arg argument if it is omitted */
2515 if (sa && sa->type.t && gnu_ext)
2516 continue;
2517 else
2518 break;
2520 if (tok != ',')
2521 expect(",");
2522 next_nomacro();
2524 if (sa) {
2525 error("macro '%s' used with too few args",
2526 get_tok_str(s->v, 0));
2529 /* now subst each arg */
2530 mstr = macro_arg_subst(nested_list, mstr, args);
2531 /* free memory */
2532 sa = args;
2533 while (sa) {
2534 sa1 = sa->prev;
2535 tok_str_free(sa->d);
2536 sym_free(sa);
2537 sa = sa1;
2539 mstr_allocated = 1;
2541 sym_push2(nested_list, s->v, 0, 0);
2542 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2543 /* pop nested defined symbol */
2544 sa1 = *nested_list;
2545 *nested_list = sa1->prev;
2546 sym_free(sa1);
2547 if (mstr_allocated)
2548 tok_str_free(mstr);
2550 return 0;
2553 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2554 return the resulting string (which must be freed). */
2555 static inline int *macro_twosharps(const int *macro_str)
2557 const int *ptr;
2558 int t;
2559 CValue cval;
2560 TokenString macro_str1;
2561 CString cstr;
2562 char *p;
2563 int n;
2565 /* we search the first '##' */
2566 for(ptr = macro_str;;) {
2567 TOK_GET(t, ptr, cval);
2568 if (t == TOK_TWOSHARPS)
2569 break;
2570 /* nothing more to do if end of string */
2571 if (t == 0)
2572 return NULL;
2575 /* we saw '##', so we need more processing to handle it */
2576 tok_str_new(&macro_str1);
2577 for(ptr = macro_str;;) {
2578 TOK_GET(tok, ptr, tokc);
2579 if (tok == 0)
2580 break;
2581 if (tok == TOK_TWOSHARPS)
2582 continue;
2583 while (*ptr == TOK_TWOSHARPS) {
2584 t = *++ptr;
2585 if (t && t != TOK_TWOSHARPS) {
2586 TOK_GET(t, ptr, cval);
2588 /* We concatenate the two tokens */
2589 cstr_new(&cstr);
2590 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2591 n = cstr.size;
2592 cstr_cat(&cstr, get_tok_str(t, &cval));
2593 cstr_ccat(&cstr, '\0');
2595 p = file->buf_ptr;
2596 file->buf_ptr = cstr.data;
2597 for (;;) {
2598 next_nomacro1();
2599 if (0 == *file->buf_ptr)
2600 break;
2601 tok_str_add2(&macro_str1, tok, &tokc);
2603 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2604 n, cstr.data, (char*)cstr.data + n);
2606 file->buf_ptr = p;
2607 cstr_reset(&cstr);
2610 tok_str_add2(&macro_str1, tok, &tokc);
2612 tok_str_add(&macro_str1, 0);
2613 return macro_str1.str;
2617 /* do macro substitution of macro_str and add result to
2618 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2619 inside to avoid recursing. */
2620 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2621 const int *macro_str, struct macro_level ** can_read_stream)
2623 Sym *s;
2624 int *macro_str1;
2625 const int *ptr;
2626 int t, ret, spc;
2627 CValue cval;
2628 struct macro_level ml;
2630 /* first scan for '##' operator handling */
2631 ptr = macro_str;
2632 macro_str1 = macro_twosharps(ptr);
2633 if (macro_str1)
2634 ptr = macro_str1;
2635 spc = 0;
2636 while (1) {
2637 /* NOTE: ptr == NULL can only happen if tokens are read from
2638 file stream due to a macro function call */
2639 if (ptr == NULL)
2640 break;
2641 TOK_GET(t, ptr, cval);
2642 if (t == 0)
2643 break;
2644 s = define_find(t);
2645 if (s != NULL) {
2646 /* if nested substitution, do nothing */
2647 if (sym_find2(*nested_list, t))
2648 goto no_subst;
2649 ml.p = macro_ptr;
2650 if (can_read_stream)
2651 ml.prev = *can_read_stream, *can_read_stream = &ml;
2652 macro_ptr = (int *)ptr;
2653 tok = t;
2654 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
2655 ptr = (int *)macro_ptr;
2656 macro_ptr = ml.p;
2657 if (can_read_stream && *can_read_stream == &ml)
2658 *can_read_stream = ml.prev;
2659 if (ret != 0)
2660 goto no_subst;
2661 } else {
2662 no_subst:
2663 if (!check_space(t, &spc))
2664 tok_str_add2(tok_str, t, &cval);
2667 if (macro_str1)
2668 tok_str_free(macro_str1);
2671 /* return next token with macro substitution */
2672 void next(void)
2674 Sym *nested_list, *s;
2675 TokenString str;
2676 struct macro_level *ml;
2678 redo:
2679 if (parse_flags & PARSE_FLAG_SPACES)
2680 next_nomacro_spc();
2681 else
2682 next_nomacro();
2683 if (!macro_ptr) {
2684 /* if not reading from macro substituted string, then try
2685 to substitute macros */
2686 if (tok >= TOK_IDENT &&
2687 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2688 s = define_find(tok);
2689 if (s) {
2690 /* we have a macro: we try to substitute */
2691 tok_str_new(&str);
2692 nested_list = NULL;
2693 ml = NULL;
2694 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
2695 /* substitution done, NOTE: maybe empty */
2696 tok_str_add(&str, 0);
2697 macro_ptr = str.str;
2698 macro_ptr_allocated = str.str;
2699 goto redo;
2703 } else {
2704 if (tok == 0) {
2705 /* end of macro or end of unget buffer */
2706 if (unget_buffer_enabled) {
2707 macro_ptr = unget_saved_macro_ptr;
2708 unget_buffer_enabled = 0;
2709 } else {
2710 /* end of macro string: free it */
2711 tok_str_free(macro_ptr_allocated);
2712 macro_ptr_allocated = NULL;
2713 macro_ptr = NULL;
2715 goto redo;
2719 /* convert preprocessor tokens into C tokens */
2720 if (tok == TOK_PPNUM &&
2721 (parse_flags & PARSE_FLAG_TOK_NUM)) {
2722 parse_number((char *)tokc.cstr->data);
2726 /* better than nothing, but needs extension to handle '-E' option
2727 correctly too */
2728 void preprocess_init(TCCState *s1)
2730 s1->include_stack_ptr = s1->include_stack;
2731 /* XXX: move that before to avoid having to initialize
2732 file->ifdef_stack_ptr ? */
2733 s1->ifdef_stack_ptr = s1->ifdef_stack;
2734 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2736 /* XXX: not ANSI compliant: bound checking says error */
2737 vtop = vstack - 1;
2738 s1->pack_stack[0] = 0;
2739 s1->pack_stack_ptr = s1->pack_stack;
2742 void preprocess_new()
2744 int i, c;
2745 const char *p, *r;
2746 TokenSym *ts;
2748 /* init isid table */
2749 for(i=CH_EOF;i<256;i++)
2750 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
2752 /* add all tokens */
2753 table_ident = NULL;
2754 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
2756 tok_ident = TOK_IDENT;
2757 p = tcc_keywords;
2758 while (*p) {
2759 r = p;
2760 for(;;) {
2761 c = *r++;
2762 if (c == '\0')
2763 break;
2765 ts = tok_alloc(p, r - p - 1);
2766 p = r;
2770 /* Preprocess the current file */
2771 int tcc_preprocess(TCCState *s1)
2773 Sym *define_start;
2774 BufferedFile *file_ref, **iptr, **iptr_new;
2775 int token_seen, line_ref, d;
2776 const char *s;
2778 preprocess_init(s1);
2779 define_start = define_stack;
2780 ch = file->buf_ptr[0];
2781 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
2782 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
2783 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
2784 token_seen = 0;
2785 line_ref = 0;
2786 file_ref = NULL;
2788 iptr = s1->include_stack_ptr;
2789 for (;;) {
2790 next();
2791 if (tok == TOK_EOF) {
2792 break;
2793 } else if (file != file_ref) {
2794 goto print_line;
2795 } else if (tok == TOK_LINEFEED) {
2796 if (!token_seen)
2797 continue;
2798 ++line_ref;
2799 token_seen = 0;
2800 } else if (!token_seen) {
2801 d = file->line_num - line_ref;
2802 if (file != file_ref || d < 0 || d >= 8) {
2803 print_line:
2804 iptr_new = s1->include_stack_ptr;
2805 s = iptr_new > iptr ? " 1"
2806 : iptr_new < iptr ? " 2"
2807 : iptr_new > s1->include_stack ? " 3"
2808 : ""
2810 iptr = iptr_new;
2811 fprintf(s1->outfile, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
2812 } else {
2813 while (d)
2814 fputs("\n", s1->outfile), --d;
2816 line_ref = (file_ref = file)->line_num;
2817 token_seen = tok != TOK_LINEFEED;
2818 if (!token_seen)
2819 continue;
2821 fputs(get_tok_str(tok, &tokc), s1->outfile);
2823 free_defines(define_start);
2824 return 0;
2827 /* defines handling */
2828 void define_push(int v, int macro_type, int *str, Sym *first_arg)
2830 Sym *s;
2832 s = sym_push2(&define_stack, v, macro_type, 0);
2833 s->d = str;
2834 s->next = first_arg;
2835 table_ident[v - TOK_IDENT]->sym_define = s;