win32: treat long double as double
[tinycc.git] / tccpp.c
blob25b6cbbf98bf76526b7b6e3b41b86340169e09c4
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 static const char tcc_keywords[] =
23 #define DEF(id, str) str "\0"
24 #include "tcctok.h"
25 #undef DEF
28 /* WARNING: the content of this string encodes token numbers */
29 static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
31 /* true if isid(c) || isnum(c) */
32 static unsigned char isidnum_table[256-CH_EOF];
35 struct macro_level {
36 struct macro_level *prev;
37 int *p;
40 static void next_nomacro(void);
41 static void next_nomacro_spc(void);
42 static void macro_subst(TokenString *tok_str, Sym **nested_list,
43 const int *macro_str, struct macro_level **can_read_stream);
46 /* allocate a new token */
47 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
49 TokenSym *ts, **ptable;
50 int i;
52 if (tok_ident >= SYM_FIRST_ANOM)
53 error("memory full");
55 /* expand token table if needed */
56 i = tok_ident - TOK_IDENT;
57 if ((i % TOK_ALLOC_INCR) == 0) {
58 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
59 if (!ptable)
60 error("memory full");
61 table_ident = ptable;
64 ts = tcc_malloc(sizeof(TokenSym) + len);
65 table_ident[i] = ts;
66 ts->tok = tok_ident++;
67 ts->sym_define = NULL;
68 ts->sym_label = NULL;
69 ts->sym_struct = NULL;
70 ts->sym_identifier = NULL;
71 ts->len = len;
72 ts->hash_next = NULL;
73 memcpy(ts->str, str, len);
74 ts->str[len] = '\0';
75 *pts = ts;
76 return ts;
79 #define TOK_HASH_INIT 1
80 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
82 /* find a token and add it if not found */
83 static TokenSym *tok_alloc(const char *str, int len)
85 TokenSym *ts, **pts;
86 int i;
87 unsigned int h;
89 h = TOK_HASH_INIT;
90 for(i=0;i<len;i++)
91 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
92 h &= (TOK_HASH_SIZE - 1);
94 pts = &hash_ident[h];
95 for(;;) {
96 ts = *pts;
97 if (!ts)
98 break;
99 if (ts->len == len && !memcmp(ts->str, str, len))
100 return ts;
101 pts = &(ts->hash_next);
103 return tok_alloc_new(pts, str, len);
106 /* XXX: buffer overflow */
107 /* XXX: float tokens */
108 char *get_tok_str(int v, CValue *cv)
110 static char buf[STRING_MAX_SIZE + 1];
111 static CString cstr_buf;
112 CString *cstr;
113 unsigned char *q;
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 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 static int handle_eob(void)
247 return tcc_peekc_slow(file);
250 /* read next char from current input file and handle end of input buffer */
251 static inline void inp(void)
253 ch = *(++(file->buf_ptr));
254 /* end of buffer/file handling */
255 if (ch == CH_EOB)
256 ch = handle_eob();
259 /* handle '\[\r]\n' */
260 static int handle_stray_noerror(void)
262 while (ch == '\\') {
263 inp();
264 if (ch == '\n') {
265 file->line_num++;
266 inp();
267 } else if (ch == '\r') {
268 inp();
269 if (ch != '\n')
270 goto fail;
271 file->line_num++;
272 inp();
273 } else {
274 fail:
275 return 1;
278 return 0;
281 static void handle_stray(void)
283 if (handle_stray_noerror())
284 error("stray '\\' in program");
287 /* skip the stray and handle the \\n case. Output an error if
288 incorrect char after the stray */
289 static int handle_stray1(uint8_t *p)
291 int c;
293 if (p >= file->buf_end) {
294 file->buf_ptr = p;
295 c = handle_eob();
296 p = file->buf_ptr;
297 if (c == '\\')
298 goto parse_stray;
299 } else {
300 parse_stray:
301 file->buf_ptr = p;
302 ch = *p;
303 handle_stray();
304 p = file->buf_ptr;
305 c = *p;
307 return c;
310 /* handle just the EOB case, but not stray */
311 #define PEEKC_EOB(c, p)\
313 p++;\
314 c = *p;\
315 if (c == '\\') {\
316 file->buf_ptr = p;\
317 c = handle_eob();\
318 p = file->buf_ptr;\
322 /* handle the complicated stray case */
323 #define PEEKC(c, p)\
325 p++;\
326 c = *p;\
327 if (c == '\\') {\
328 c = handle_stray1(p);\
329 p = file->buf_ptr;\
333 /* input with '\[\r]\n' handling. Note that this function cannot
334 handle other characters after '\', so you cannot call it inside
335 strings or comments */
336 static void minp(void)
338 inp();
339 if (ch == '\\')
340 handle_stray();
344 /* single line C++ comments */
345 static uint8_t *parse_line_comment(uint8_t *p)
347 int c;
349 p++;
350 for(;;) {
351 c = *p;
352 redo:
353 if (c == '\n' || c == CH_EOF) {
354 break;
355 } else if (c == '\\') {
356 file->buf_ptr = p;
357 c = handle_eob();
358 p = file->buf_ptr;
359 if (c == '\\') {
360 PEEKC_EOB(c, p);
361 if (c == '\n') {
362 file->line_num++;
363 PEEKC_EOB(c, p);
364 } else if (c == '\r') {
365 PEEKC_EOB(c, p);
366 if (c == '\n') {
367 file->line_num++;
368 PEEKC_EOB(c, p);
371 } else {
372 goto redo;
374 } else {
375 p++;
378 return p;
381 /* C comments */
382 static uint8_t *parse_comment(uint8_t *p)
384 int c;
386 p++;
387 for(;;) {
388 /* fast skip loop */
389 for(;;) {
390 c = *p;
391 if (c == '\n' || c == '*' || c == '\\')
392 break;
393 p++;
394 c = *p;
395 if (c == '\n' || c == '*' || c == '\\')
396 break;
397 p++;
399 /* now we can handle all the cases */
400 if (c == '\n') {
401 file->line_num++;
402 p++;
403 } else if (c == '*') {
404 p++;
405 for(;;) {
406 c = *p;
407 if (c == '*') {
408 p++;
409 } else if (c == '/') {
410 goto end_of_comment;
411 } else if (c == '\\') {
412 file->buf_ptr = p;
413 c = handle_eob();
414 p = file->buf_ptr;
415 if (c == '\\') {
416 /* skip '\[\r]\n', otherwise just skip the stray */
417 while (c == '\\') {
418 PEEKC_EOB(c, p);
419 if (c == '\n') {
420 file->line_num++;
421 PEEKC_EOB(c, p);
422 } else if (c == '\r') {
423 PEEKC_EOB(c, p);
424 if (c == '\n') {
425 file->line_num++;
426 PEEKC_EOB(c, p);
428 } else {
429 goto after_star;
433 } else {
434 break;
437 after_star: ;
438 } else {
439 /* stray, eob or eof */
440 file->buf_ptr = p;
441 c = handle_eob();
442 p = file->buf_ptr;
443 if (c == CH_EOF) {
444 error("unexpected end of file in comment");
445 } else if (c == '\\') {
446 p++;
450 end_of_comment:
451 p++;
452 return p;
455 #define cinp minp
457 static inline void skip_spaces(void)
459 while (is_space(ch))
460 cinp();
463 static inline int check_space(int t, int *spc)
465 if (is_space(t)) {
466 if (*spc)
467 return 1;
468 *spc = 1;
469 } else
470 *spc = 0;
471 return 0;
474 /* parse a string without interpreting escapes */
475 static uint8_t *parse_pp_string(uint8_t *p,
476 int sep, CString *str)
478 int c;
479 p++;
480 for(;;) {
481 c = *p;
482 if (c == sep) {
483 break;
484 } else if (c == '\\') {
485 file->buf_ptr = p;
486 c = handle_eob();
487 p = file->buf_ptr;
488 if (c == CH_EOF) {
489 unterminated_string:
490 /* XXX: indicate line number of start of string */
491 error("missing terminating %c character", sep);
492 } else if (c == '\\') {
493 /* escape : just skip \[\r]\n */
494 PEEKC_EOB(c, p);
495 if (c == '\n') {
496 file->line_num++;
497 p++;
498 } else if (c == '\r') {
499 PEEKC_EOB(c, p);
500 if (c != '\n')
501 expect("'\n' after '\r'");
502 file->line_num++;
503 p++;
504 } else if (c == CH_EOF) {
505 goto unterminated_string;
506 } else {
507 if (str) {
508 cstr_ccat(str, '\\');
509 cstr_ccat(str, c);
511 p++;
514 } else if (c == '\n') {
515 file->line_num++;
516 goto add_char;
517 } else if (c == '\r') {
518 PEEKC_EOB(c, p);
519 if (c != '\n') {
520 if (str)
521 cstr_ccat(str, '\r');
522 } else {
523 file->line_num++;
524 goto add_char;
526 } else {
527 add_char:
528 if (str)
529 cstr_ccat(str, c);
530 p++;
533 p++;
534 return p;
537 /* skip block of text until #else, #elif or #endif. skip also pairs of
538 #if/#endif */
539 void preprocess_skip(void)
541 int a, start_of_line, c, in_warn_or_error;
542 uint8_t *p;
544 p = file->buf_ptr;
545 a = 0;
546 redo_start:
547 start_of_line = 1;
548 in_warn_or_error = 0;
549 for(;;) {
550 redo_no_start:
551 c = *p;
552 switch(c) {
553 case ' ':
554 case '\t':
555 case '\f':
556 case '\v':
557 case '\r':
558 p++;
559 goto redo_no_start;
560 case '\n':
561 file->line_num++;
562 p++;
563 goto redo_start;
564 case '\\':
565 file->buf_ptr = p;
566 c = handle_eob();
567 if (c == CH_EOF) {
568 expect("#endif");
569 } else if (c == '\\') {
570 ch = file->buf_ptr[0];
571 handle_stray_noerror();
573 p = file->buf_ptr;
574 goto redo_no_start;
575 /* skip strings */
576 case '\"':
577 case '\'':
578 if (in_warn_or_error)
579 goto _default;
580 p = parse_pp_string(p, c, NULL);
581 break;
582 /* skip comments */
583 case '/':
584 if (in_warn_or_error)
585 goto _default;
586 file->buf_ptr = p;
587 ch = *p;
588 minp();
589 p = file->buf_ptr;
590 if (ch == '*') {
591 p = parse_comment(p);
592 } else if (ch == '/') {
593 p = parse_line_comment(p);
595 break;
596 case '#':
597 p++;
598 if (start_of_line) {
599 file->buf_ptr = p;
600 next_nomacro();
601 p = file->buf_ptr;
602 if (a == 0 &&
603 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
604 goto the_end;
605 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
606 a++;
607 else if (tok == TOK_ENDIF)
608 a--;
609 else if( tok == TOK_ERROR || tok == TOK_WARNING)
610 in_warn_or_error = 1;
612 break;
613 _default:
614 default:
615 p++;
616 break;
618 start_of_line = 0;
620 the_end: ;
621 file->buf_ptr = p;
624 /* ParseState handling */
626 /* XXX: currently, no include file info is stored. Thus, we cannot display
627 accurate messages if the function or data definition spans multiple
628 files */
630 /* save current parse state in 's' */
631 void save_parse_state(ParseState *s)
633 s->line_num = file->line_num;
634 s->macro_ptr = macro_ptr;
635 s->tok = tok;
636 s->tokc = tokc;
639 /* restore parse state from 's' */
640 void restore_parse_state(ParseState *s)
642 file->line_num = s->line_num;
643 macro_ptr = s->macro_ptr;
644 tok = s->tok;
645 tokc = s->tokc;
648 /* return the number of additional 'ints' necessary to store the
649 token */
650 static inline int tok_ext_size(int t)
652 switch(t) {
653 /* 4 bytes */
654 case TOK_CINT:
655 case TOK_CUINT:
656 case TOK_CCHAR:
657 case TOK_LCHAR:
658 case TOK_CFLOAT:
659 case TOK_LINENUM:
660 return 1;
661 case TOK_STR:
662 case TOK_LSTR:
663 case TOK_PPNUM:
664 error("unsupported token");
665 return 1;
666 case TOK_CDOUBLE:
667 case TOK_CLLONG:
668 case TOK_CULLONG:
669 return 2;
670 case TOK_CLDOUBLE:
671 return LDOUBLE_SIZE / 4;
672 default:
673 return 0;
677 /* token string handling */
679 static inline void tok_str_new(TokenString *s)
681 s->str = NULL;
682 s->len = 0;
683 s->allocated_len = 0;
684 s->last_line_num = -1;
687 static void tok_str_free(int *str)
689 tcc_free(str);
692 static int *tok_str_realloc(TokenString *s)
694 int *str, len;
696 if (s->allocated_len == 0) {
697 len = 8;
698 } else {
699 len = s->allocated_len * 2;
701 str = tcc_realloc(s->str, len * sizeof(int));
702 if (!str)
703 error("memory full");
704 s->allocated_len = len;
705 s->str = str;
706 return str;
709 static void tok_str_add(TokenString *s, int t)
711 int len, *str;
713 len = s->len;
714 str = s->str;
715 if (len >= s->allocated_len)
716 str = tok_str_realloc(s);
717 str[len++] = t;
718 s->len = len;
721 static void tok_str_add2(TokenString *s, int t, CValue *cv)
723 int len, *str;
725 len = s->len;
726 str = s->str;
728 /* allocate space for worst case */
729 if (len + TOK_MAX_SIZE > s->allocated_len)
730 str = tok_str_realloc(s);
731 str[len++] = t;
732 switch(t) {
733 case TOK_CINT:
734 case TOK_CUINT:
735 case TOK_CCHAR:
736 case TOK_LCHAR:
737 case TOK_CFLOAT:
738 case TOK_LINENUM:
739 str[len++] = cv->tab[0];
740 break;
741 case TOK_PPNUM:
742 case TOK_STR:
743 case TOK_LSTR:
745 int nb_words;
746 CString *cstr;
748 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
749 while ((len + nb_words) > s->allocated_len)
750 str = tok_str_realloc(s);
751 cstr = (CString *)(str + len);
752 cstr->data = NULL;
753 cstr->size = cv->cstr->size;
754 cstr->data_allocated = NULL;
755 cstr->size_allocated = cstr->size;
756 memcpy((char *)cstr + sizeof(CString),
757 cv->cstr->data, cstr->size);
758 len += nb_words;
760 break;
761 case TOK_CDOUBLE:
762 case TOK_CLLONG:
763 case TOK_CULLONG:
764 #if LDOUBLE_SIZE == 8
765 case TOK_CLDOUBLE:
766 #endif
767 str[len++] = cv->tab[0];
768 str[len++] = cv->tab[1];
769 break;
770 #if LDOUBLE_SIZE == 12
771 case TOK_CLDOUBLE:
772 str[len++] = cv->tab[0];
773 str[len++] = cv->tab[1];
774 str[len++] = cv->tab[2];
775 #elif LDOUBLE_SIZE == 16
776 case TOK_CLDOUBLE:
777 str[len++] = cv->tab[0];
778 str[len++] = cv->tab[1];
779 str[len++] = cv->tab[2];
780 str[len++] = cv->tab[3];
781 #elif LDOUBLE_SIZE != 8
782 #error add long double size support
783 #endif
784 break;
785 default:
786 break;
788 s->len = len;
791 /* add the current parse token in token string 's' */
792 static void tok_str_add_tok(TokenString *s)
794 CValue cval;
796 /* save line number info */
797 if (file->line_num != s->last_line_num) {
798 s->last_line_num = file->line_num;
799 cval.i = s->last_line_num;
800 tok_str_add2(s, TOK_LINENUM, &cval);
802 tok_str_add2(s, tok, &tokc);
805 #if LDOUBLE_SIZE == 16
806 #define LDOUBLE_GET(p, cv) \
807 cv.tab[0] = p[0]; \
808 cv.tab[1] = p[1]; \
809 cv.tab[2] = p[2]; \
810 cv.tab[3] = p[3];
811 #elif LDOUBLE_SIZE == 12
812 #define LDOUBLE_GET(p, cv) \
813 cv.tab[0] = p[0]; \
814 cv.tab[1] = p[1]; \
815 cv.tab[2] = p[2];
816 #elif LDOUBLE_SIZE == 8
817 #define LDOUBLE_GET(p, cv) \
818 cv.tab[0] = p[0]; \
819 cv.tab[1] = p[1];
820 #else
821 #error add long double size support
822 #endif
825 /* get a token from an integer array and increment pointer
826 accordingly. we code it as a macro to avoid pointer aliasing. */
827 #define TOK_GET(t, p, cv) \
829 t = *p++; \
830 switch(t) { \
831 case TOK_CINT: \
832 case TOK_CUINT: \
833 case TOK_CCHAR: \
834 case TOK_LCHAR: \
835 case TOK_CFLOAT: \
836 case TOK_LINENUM: \
837 cv.tab[0] = *p++; \
838 break; \
839 case TOK_STR: \
840 case TOK_LSTR: \
841 case TOK_PPNUM: \
842 cv.cstr = (CString *)p; \
843 cv.cstr->data = (char *)p + sizeof(CString);\
844 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
845 break; \
846 case TOK_CDOUBLE: \
847 case TOK_CLLONG: \
848 case TOK_CULLONG: \
849 cv.tab[0] = p[0]; \
850 cv.tab[1] = p[1]; \
851 p += 2; \
852 break; \
853 case TOK_CLDOUBLE: \
854 LDOUBLE_GET(p, cv); \
855 p += LDOUBLE_SIZE / 4; \
856 break; \
857 default: \
858 break; \
862 /* defines handling */
863 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
865 Sym *s;
867 s = sym_push2(&define_stack, v, macro_type, 0);
868 s->d = str;
869 s->next = first_arg;
870 table_ident[v - TOK_IDENT]->sym_define = s;
873 /* undefined a define symbol. Its name is just set to zero */
874 static void define_undef(Sym *s)
876 int v;
877 v = s->v;
878 if (v >= TOK_IDENT && v < tok_ident)
879 table_ident[v - TOK_IDENT]->sym_define = NULL;
880 s->v = 0;
883 static inline Sym *define_find(int v)
885 v -= TOK_IDENT;
886 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
887 return NULL;
888 return table_ident[v]->sym_define;
891 /* free define stack until top reaches 'b' */
892 static void free_defines(Sym *b)
894 Sym *top, *top1;
895 int v;
897 top = define_stack;
898 while (top != b) {
899 top1 = top->prev;
900 /* do not free args or predefined defines */
901 if (top->d)
902 tok_str_free(top->d);
903 v = top->v;
904 if (v >= TOK_IDENT && v < tok_ident)
905 table_ident[v - TOK_IDENT]->sym_define = NULL;
906 sym_free(top);
907 top = top1;
909 define_stack = b;
912 /* label lookup */
913 static Sym *label_find(int v)
915 v -= TOK_IDENT;
916 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
917 return NULL;
918 return table_ident[v]->sym_label;
921 static Sym *label_push(Sym **ptop, int v, int flags)
923 Sym *s, **ps;
924 s = sym_push2(ptop, v, 0, 0);
925 s->r = flags;
926 ps = &table_ident[v - TOK_IDENT]->sym_label;
927 if (ptop == &global_label_stack) {
928 /* modify the top most local identifier, so that
929 sym_identifier will point to 's' when popped */
930 while (*ps != NULL)
931 ps = &(*ps)->prev_tok;
933 s->prev_tok = *ps;
934 *ps = s;
935 return s;
938 /* pop labels until element last is reached. Look if any labels are
939 undefined. Define symbols if '&&label' was used. */
940 static void label_pop(Sym **ptop, Sym *slast)
942 Sym *s, *s1;
943 for(s = *ptop; s != slast; s = s1) {
944 s1 = s->prev;
945 if (s->r == LABEL_DECLARED) {
946 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
947 } else if (s->r == LABEL_FORWARD) {
948 error("label '%s' used but not defined",
949 get_tok_str(s->v, NULL));
950 } else {
951 if (s->c) {
952 /* define corresponding symbol. A size of
953 1 is put. */
954 put_extern_sym(s, cur_text_section, s->jnext, 1);
957 /* remove label */
958 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
959 sym_free(s);
961 *ptop = slast;
964 /* eval an expression for #if/#elif */
965 static int expr_preprocess(void)
967 int c, t;
968 TokenString str;
970 tok_str_new(&str);
971 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
972 next(); /* do macro subst */
973 if (tok == TOK_DEFINED) {
974 next_nomacro();
975 t = tok;
976 if (t == '(')
977 next_nomacro();
978 c = define_find(tok) != 0;
979 if (t == '(')
980 next_nomacro();
981 tok = TOK_CINT;
982 tokc.i = c;
983 } else if (tok >= TOK_IDENT) {
984 /* if undefined macro */
985 tok = TOK_CINT;
986 tokc.i = 0;
988 tok_str_add_tok(&str);
990 tok_str_add(&str, -1); /* simulate end of file */
991 tok_str_add(&str, 0);
992 /* now evaluate C constant expression */
993 macro_ptr = str.str;
994 next();
995 c = expr_const();
996 macro_ptr = NULL;
997 tok_str_free(str.str);
998 return c != 0;
1001 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1002 static void tok_print(int *str)
1004 int t;
1005 CValue cval;
1007 printf("<");
1008 while (1) {
1009 TOK_GET(t, str, cval);
1010 if (!t)
1011 break;
1012 printf("%s", get_tok_str(t, &cval));
1014 printf(">\n");
1016 #endif
1018 /* parse after #define */
1019 static void parse_define(void)
1021 Sym *s, *first, **ps;
1022 int v, t, varg, is_vaargs, spc;
1023 TokenString str;
1025 v = tok;
1026 if (v < TOK_IDENT)
1027 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1028 /* XXX: should check if same macro (ANSI) */
1029 first = NULL;
1030 t = MACRO_OBJ;
1031 /* '(' must be just after macro definition for MACRO_FUNC */
1032 next_nomacro_spc();
1033 if (tok == '(') {
1034 next_nomacro();
1035 ps = &first;
1036 while (tok != ')') {
1037 varg = tok;
1038 next_nomacro();
1039 is_vaargs = 0;
1040 if (varg == TOK_DOTS) {
1041 varg = TOK___VA_ARGS__;
1042 is_vaargs = 1;
1043 } else if (tok == TOK_DOTS && gnu_ext) {
1044 is_vaargs = 1;
1045 next_nomacro();
1047 if (varg < TOK_IDENT)
1048 error("badly punctuated parameter list");
1049 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1050 *ps = s;
1051 ps = &s->next;
1052 if (tok != ',')
1053 break;
1054 next_nomacro();
1056 if (tok == ')')
1057 next_nomacro_spc();
1058 t = MACRO_FUNC;
1060 tok_str_new(&str);
1061 spc = 2;
1062 /* EOF testing necessary for '-D' handling */
1063 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1064 /* remove spaces around ## and after '#' */
1065 if (TOK_TWOSHARPS == tok) {
1066 if (1 == spc)
1067 --str.len;
1068 spc = 2;
1069 } else if ('#' == tok) {
1070 spc = 2;
1071 } else if (check_space(tok, &spc)) {
1072 goto skip;
1074 tok_str_add2(&str, tok, &tokc);
1075 skip:
1076 next_nomacro_spc();
1078 if (spc == 1)
1079 --str.len; /* remove trailing space */
1080 tok_str_add(&str, 0);
1081 #ifdef PP_DEBUG
1082 printf("define %s %d: ", get_tok_str(v, NULL), t);
1083 tok_print(str.str);
1084 #endif
1085 define_push(v, t, str.str, first);
1088 static inline int hash_cached_include(int type, const char *filename)
1090 const unsigned char *s;
1091 unsigned int h;
1093 h = TOK_HASH_INIT;
1094 h = TOK_HASH_FUNC(h, type);
1095 s = filename;
1096 while (*s) {
1097 h = TOK_HASH_FUNC(h, *s);
1098 s++;
1100 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1101 return h;
1104 /* XXX: use a token or a hash table to accelerate matching ? */
1105 static CachedInclude *search_cached_include(TCCState *s1,
1106 int type, const char *filename)
1108 CachedInclude *e;
1109 int i, h;
1110 h = hash_cached_include(type, filename);
1111 i = s1->cached_includes_hash[h];
1112 for(;;) {
1113 if (i == 0)
1114 break;
1115 e = s1->cached_includes[i - 1];
1116 if (e->type == type && !PATHCMP(e->filename, filename))
1117 return e;
1118 i = e->hash_next;
1120 return NULL;
1123 static inline void add_cached_include(TCCState *s1, int type,
1124 const char *filename, int ifndef_macro)
1126 CachedInclude *e;
1127 int h;
1129 if (search_cached_include(s1, type, filename))
1130 return;
1131 #ifdef INC_DEBUG
1132 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1133 #endif
1134 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1135 if (!e)
1136 return;
1137 e->type = type;
1138 strcpy(e->filename, filename);
1139 e->ifndef_macro = ifndef_macro;
1140 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1141 /* add in hash table */
1142 h = hash_cached_include(type, filename);
1143 e->hash_next = s1->cached_includes_hash[h];
1144 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1147 static void pragma_parse(TCCState *s1)
1149 int val;
1151 next();
1152 if (tok == TOK_pack) {
1154 This may be:
1155 #pragma pack(1) // set
1156 #pragma pack() // reset to default
1157 #pragma pack(push,1) // push & set
1158 #pragma pack(pop) // restore previous
1160 next();
1161 skip('(');
1162 if (tok == TOK_ASM_pop) {
1163 next();
1164 if (s1->pack_stack_ptr <= s1->pack_stack) {
1165 stk_error:
1166 error("out of pack stack");
1168 s1->pack_stack_ptr--;
1169 } else {
1170 val = 0;
1171 if (tok != ')') {
1172 if (tok == TOK_ASM_push) {
1173 next();
1174 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1175 goto stk_error;
1176 s1->pack_stack_ptr++;
1177 skip(',');
1179 if (tok != TOK_CINT) {
1180 pack_error:
1181 error("invalid pack pragma");
1183 val = tokc.i;
1184 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1185 goto pack_error;
1186 next();
1188 *s1->pack_stack_ptr = val;
1189 skip(')');
1194 /* is_bof is true if first non space token at beginning of file */
1195 static void preprocess(int is_bof)
1197 TCCState *s1 = tcc_state;
1198 int i, c, n, saved_parse_flags;
1199 char buf[1024], *q;
1200 Sym *s;
1202 saved_parse_flags = parse_flags;
1203 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1204 PARSE_FLAG_LINEFEED;
1205 next_nomacro();
1206 redo:
1207 switch(tok) {
1208 case TOK_DEFINE:
1209 next_nomacro();
1210 parse_define();
1211 break;
1212 case TOK_UNDEF:
1213 next_nomacro();
1214 s = define_find(tok);
1215 /* undefine symbol by putting an invalid name */
1216 if (s)
1217 define_undef(s);
1218 break;
1219 case TOK_INCLUDE:
1220 case TOK_INCLUDE_NEXT:
1221 ch = file->buf_ptr[0];
1222 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1223 skip_spaces();
1224 if (ch == '<') {
1225 c = '>';
1226 goto read_name;
1227 } else if (ch == '\"') {
1228 c = ch;
1229 read_name:
1230 inp();
1231 q = buf;
1232 while (ch != c && ch != '\n' && ch != CH_EOF) {
1233 if ((q - buf) < sizeof(buf) - 1)
1234 *q++ = ch;
1235 if (ch == '\\') {
1236 if (handle_stray_noerror() == 0)
1237 --q;
1238 } else
1239 inp();
1241 *q = '\0';
1242 minp();
1243 #if 0
1244 /* eat all spaces and comments after include */
1245 /* XXX: slightly incorrect */
1246 while (ch1 != '\n' && ch1 != CH_EOF)
1247 inp();
1248 #endif
1249 } else {
1250 /* computed #include : either we have only strings or
1251 we have anything enclosed in '<>' */
1252 next();
1253 buf[0] = '\0';
1254 if (tok == TOK_STR) {
1255 while (tok != TOK_LINEFEED) {
1256 if (tok != TOK_STR) {
1257 include_syntax:
1258 error("'#include' expects \"FILENAME\" or <FILENAME>");
1260 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1261 next();
1263 c = '\"';
1264 } else {
1265 int len;
1266 while (tok != TOK_LINEFEED) {
1267 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1268 next();
1270 len = strlen(buf);
1271 /* check syntax and remove '<>' */
1272 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1273 goto include_syntax;
1274 memmove(buf, buf + 1, len - 2);
1275 buf[len - 2] = '\0';
1276 c = '>';
1280 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1281 error("#include recursion too deep");
1283 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1284 for (i = -2; i < n; ++i) {
1285 char buf1[sizeof file->filename];
1286 BufferedFile *f;
1287 CachedInclude *e;
1288 const char *path;
1289 int size;
1291 if (i == -2) {
1292 /* check absolute include path */
1293 if (!IS_ABSPATH(buf))
1294 continue;
1295 buf1[0] = 0;
1297 } else if (i == -1) {
1298 /* search in current dir if "header.h" */
1299 if (c != '\"')
1300 continue;
1301 size = tcc_basename(file->filename) - file->filename;
1302 memcpy(buf1, file->filename, size);
1303 buf1[size] = '\0';
1305 } else {
1306 /* search in all the include paths */
1307 if (i < s1->nb_include_paths)
1308 path = s1->include_paths[i];
1309 else
1310 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1311 pstrcpy(buf1, sizeof(buf1), path);
1312 pstrcat(buf1, sizeof(buf1), "/");
1315 pstrcat(buf1, sizeof(buf1), buf);
1317 e = search_cached_include(s1, c, buf1);
1318 if (e && define_find(e->ifndef_macro)) {
1319 /* no need to parse the include because the 'ifndef macro'
1320 is defined */
1321 #ifdef INC_DEBUG
1322 printf("%s: skipping %s\n", file->filename, buf);
1323 #endif
1324 f = NULL;
1325 } else {
1326 f = tcc_open(s1, buf1);
1327 if (!f)
1328 continue;
1331 if (tok == TOK_INCLUDE_NEXT) {
1332 tok = TOK_INCLUDE;
1333 if (f)
1334 tcc_close(f);
1335 continue;
1338 if (!f)
1339 goto include_done;
1341 #ifdef INC_DEBUG
1342 printf("%s: including %s\n", file->filename, buf1);
1343 #endif
1345 /* XXX: fix current line init */
1346 /* push current file in stack */
1347 *s1->include_stack_ptr++ = file;
1348 f->inc_type = c;
1349 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
1350 file = f;
1351 /* add include file debug info */
1352 if (tcc_state->do_debug) {
1353 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1355 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1356 ch = file->buf_ptr[0];
1357 goto the_end;
1359 error("include file '%s' not found", buf);
1360 include_done:
1361 break;
1362 case TOK_IFNDEF:
1363 c = 1;
1364 goto do_ifdef;
1365 case TOK_IF:
1366 c = expr_preprocess();
1367 goto do_if;
1368 case TOK_IFDEF:
1369 c = 0;
1370 do_ifdef:
1371 next_nomacro();
1372 if (tok < TOK_IDENT)
1373 error("invalid argument for '#if%sdef'", c ? "n" : "");
1374 if (is_bof) {
1375 if (c) {
1376 #ifdef INC_DEBUG
1377 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1378 #endif
1379 file->ifndef_macro = tok;
1382 c = (define_find(tok) != 0) ^ c;
1383 do_if:
1384 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1385 error("memory full");
1386 *s1->ifdef_stack_ptr++ = c;
1387 goto test_skip;
1388 case TOK_ELSE:
1389 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1390 error("#else without matching #if");
1391 if (s1->ifdef_stack_ptr[-1] & 2)
1392 error("#else after #else");
1393 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1394 goto test_skip;
1395 case TOK_ELIF:
1396 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1397 error("#elif without matching #if");
1398 c = s1->ifdef_stack_ptr[-1];
1399 if (c > 1)
1400 error("#elif after #else");
1401 /* last #if/#elif expression was true: we skip */
1402 if (c == 1)
1403 goto skip;
1404 c = expr_preprocess();
1405 s1->ifdef_stack_ptr[-1] = c;
1406 test_skip:
1407 if (!(c & 1)) {
1408 skip:
1409 preprocess_skip();
1410 is_bof = 0;
1411 goto redo;
1413 break;
1414 case TOK_ENDIF:
1415 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1416 error("#endif without matching #if");
1417 s1->ifdef_stack_ptr--;
1418 /* '#ifndef macro' was at the start of file. Now we check if
1419 an '#endif' is exactly at the end of file */
1420 if (file->ifndef_macro &&
1421 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1422 file->ifndef_macro_saved = file->ifndef_macro;
1423 /* need to set to zero to avoid false matches if another
1424 #ifndef at middle of file */
1425 file->ifndef_macro = 0;
1426 while (tok != TOK_LINEFEED)
1427 next_nomacro();
1428 tok_flags |= TOK_FLAG_ENDIF;
1429 goto the_end;
1431 break;
1432 case TOK_LINE:
1433 next();
1434 if (tok != TOK_CINT)
1435 error("#line");
1436 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1437 next();
1438 if (tok != TOK_LINEFEED) {
1439 if (tok != TOK_STR)
1440 error("#line");
1441 pstrcpy(file->filename, sizeof(file->filename),
1442 (char *)tokc.cstr->data);
1444 break;
1445 case TOK_ERROR:
1446 case TOK_WARNING:
1447 c = tok;
1448 ch = file->buf_ptr[0];
1449 skip_spaces();
1450 q = buf;
1451 while (ch != '\n' && ch != CH_EOF) {
1452 if ((q - buf) < sizeof(buf) - 1)
1453 *q++ = ch;
1454 if (ch == '\\') {
1455 if (handle_stray_noerror() == 0)
1456 --q;
1457 } else
1458 inp();
1460 *q = '\0';
1461 if (c == TOK_ERROR)
1462 error("#error %s", buf);
1463 else
1464 warning("#warning %s", buf);
1465 break;
1466 case TOK_PRAGMA:
1467 pragma_parse(s1);
1468 break;
1469 default:
1470 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
1471 /* '!' is ignored to allow C scripts. numbers are ignored
1472 to emulate cpp behaviour */
1473 } else {
1474 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1475 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1477 break;
1479 /* ignore other preprocess commands or #! for C scripts */
1480 while (tok != TOK_LINEFEED)
1481 next_nomacro();
1482 the_end:
1483 parse_flags = saved_parse_flags;
1486 /* evaluate escape codes in a string. */
1487 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1489 int c, n;
1490 const uint8_t *p;
1492 p = buf;
1493 for(;;) {
1494 c = *p;
1495 if (c == '\0')
1496 break;
1497 if (c == '\\') {
1498 p++;
1499 /* escape */
1500 c = *p;
1501 switch(c) {
1502 case '0': case '1': case '2': case '3':
1503 case '4': case '5': case '6': case '7':
1504 /* at most three octal digits */
1505 n = c - '0';
1506 p++;
1507 c = *p;
1508 if (isoct(c)) {
1509 n = n * 8 + c - '0';
1510 p++;
1511 c = *p;
1512 if (isoct(c)) {
1513 n = n * 8 + c - '0';
1514 p++;
1517 c = n;
1518 goto add_char_nonext;
1519 case 'x':
1520 case 'u':
1521 case 'U':
1522 p++;
1523 n = 0;
1524 for(;;) {
1525 c = *p;
1526 if (c >= 'a' && c <= 'f')
1527 c = c - 'a' + 10;
1528 else if (c >= 'A' && c <= 'F')
1529 c = c - 'A' + 10;
1530 else if (isnum(c))
1531 c = c - '0';
1532 else
1533 break;
1534 n = n * 16 + c;
1535 p++;
1537 c = n;
1538 goto add_char_nonext;
1539 case 'a':
1540 c = '\a';
1541 break;
1542 case 'b':
1543 c = '\b';
1544 break;
1545 case 'f':
1546 c = '\f';
1547 break;
1548 case 'n':
1549 c = '\n';
1550 break;
1551 case 'r':
1552 c = '\r';
1553 break;
1554 case 't':
1555 c = '\t';
1556 break;
1557 case 'v':
1558 c = '\v';
1559 break;
1560 case 'e':
1561 if (!gnu_ext)
1562 goto invalid_escape;
1563 c = 27;
1564 break;
1565 case '\'':
1566 case '\"':
1567 case '\\':
1568 case '?':
1569 break;
1570 default:
1571 invalid_escape:
1572 if (c >= '!' && c <= '~')
1573 warning("unknown escape sequence: \'\\%c\'", c);
1574 else
1575 warning("unknown escape sequence: \'\\x%x\'", c);
1576 break;
1579 p++;
1580 add_char_nonext:
1581 if (!is_long)
1582 cstr_ccat(outstr, c);
1583 else
1584 cstr_wccat(outstr, c);
1586 /* add a trailing '\0' */
1587 if (!is_long)
1588 cstr_ccat(outstr, '\0');
1589 else
1590 cstr_wccat(outstr, '\0');
1593 /* we use 64 bit numbers */
1594 #define BN_SIZE 2
1596 /* bn = (bn << shift) | or_val */
1597 void bn_lshift(unsigned int *bn, int shift, int or_val)
1599 int i;
1600 unsigned int v;
1601 for(i=0;i<BN_SIZE;i++) {
1602 v = bn[i];
1603 bn[i] = (v << shift) | or_val;
1604 or_val = v >> (32 - shift);
1608 void bn_zero(unsigned int *bn)
1610 int i;
1611 for(i=0;i<BN_SIZE;i++) {
1612 bn[i] = 0;
1616 /* parse number in null terminated string 'p' and return it in the
1617 current token */
1618 void parse_number(const char *p)
1620 int b, t, shift, frac_bits, s, exp_val, ch;
1621 char *q;
1622 unsigned int bn[BN_SIZE];
1623 double d;
1625 /* number */
1626 q = token_buf;
1627 ch = *p++;
1628 t = ch;
1629 ch = *p++;
1630 *q++ = t;
1631 b = 10;
1632 if (t == '.') {
1633 goto float_frac_parse;
1634 } else if (t == '0') {
1635 if (ch == 'x' || ch == 'X') {
1636 q--;
1637 ch = *p++;
1638 b = 16;
1639 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1640 q--;
1641 ch = *p++;
1642 b = 2;
1645 /* parse all digits. cannot check octal numbers at this stage
1646 because of floating point constants */
1647 while (1) {
1648 if (ch >= 'a' && ch <= 'f')
1649 t = ch - 'a' + 10;
1650 else if (ch >= 'A' && ch <= 'F')
1651 t = ch - 'A' + 10;
1652 else if (isnum(ch))
1653 t = ch - '0';
1654 else
1655 break;
1656 if (t >= b)
1657 break;
1658 if (q >= token_buf + STRING_MAX_SIZE) {
1659 num_too_long:
1660 error("number too long");
1662 *q++ = ch;
1663 ch = *p++;
1665 if (ch == '.' ||
1666 ((ch == 'e' || ch == 'E') && b == 10) ||
1667 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1668 if (b != 10) {
1669 /* NOTE: strtox should support that for hexa numbers, but
1670 non ISOC99 libcs do not support it, so we prefer to do
1671 it by hand */
1672 /* hexadecimal or binary floats */
1673 /* XXX: handle overflows */
1674 *q = '\0';
1675 if (b == 16)
1676 shift = 4;
1677 else
1678 shift = 2;
1679 bn_zero(bn);
1680 q = token_buf;
1681 while (1) {
1682 t = *q++;
1683 if (t == '\0') {
1684 break;
1685 } else if (t >= 'a') {
1686 t = t - 'a' + 10;
1687 } else if (t >= 'A') {
1688 t = t - 'A' + 10;
1689 } else {
1690 t = t - '0';
1692 bn_lshift(bn, shift, t);
1694 frac_bits = 0;
1695 if (ch == '.') {
1696 ch = *p++;
1697 while (1) {
1698 t = ch;
1699 if (t >= 'a' && t <= 'f') {
1700 t = t - 'a' + 10;
1701 } else if (t >= 'A' && t <= 'F') {
1702 t = t - 'A' + 10;
1703 } else if (t >= '0' && t <= '9') {
1704 t = t - '0';
1705 } else {
1706 break;
1708 if (t >= b)
1709 error("invalid digit");
1710 bn_lshift(bn, shift, t);
1711 frac_bits += shift;
1712 ch = *p++;
1715 if (ch != 'p' && ch != 'P')
1716 expect("exponent");
1717 ch = *p++;
1718 s = 1;
1719 exp_val = 0;
1720 if (ch == '+') {
1721 ch = *p++;
1722 } else if (ch == '-') {
1723 s = -1;
1724 ch = *p++;
1726 if (ch < '0' || ch > '9')
1727 expect("exponent digits");
1728 while (ch >= '0' && ch <= '9') {
1729 exp_val = exp_val * 10 + ch - '0';
1730 ch = *p++;
1732 exp_val = exp_val * s;
1734 /* now we can generate the number */
1735 /* XXX: should patch directly float number */
1736 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1737 d = ldexp(d, exp_val - frac_bits);
1738 t = toup(ch);
1739 if (t == 'F') {
1740 ch = *p++;
1741 tok = TOK_CFLOAT;
1742 /* float : should handle overflow */
1743 tokc.f = (float)d;
1744 } else if (t == 'L') {
1745 ch = *p++;
1746 #ifdef TCC_TARGET_PE
1747 tok = TOK_CDOUBLE;
1748 tokc.d = d;
1749 #else
1750 tok = TOK_CLDOUBLE;
1751 /* XXX: not large enough */
1752 tokc.ld = (long double)d;
1753 #endif
1754 } else {
1755 tok = TOK_CDOUBLE;
1756 tokc.d = d;
1758 } else {
1759 /* decimal floats */
1760 if (ch == '.') {
1761 if (q >= token_buf + STRING_MAX_SIZE)
1762 goto num_too_long;
1763 *q++ = ch;
1764 ch = *p++;
1765 float_frac_parse:
1766 while (ch >= '0' && ch <= '9') {
1767 if (q >= token_buf + STRING_MAX_SIZE)
1768 goto num_too_long;
1769 *q++ = ch;
1770 ch = *p++;
1773 if (ch == 'e' || ch == 'E') {
1774 if (q >= token_buf + STRING_MAX_SIZE)
1775 goto num_too_long;
1776 *q++ = ch;
1777 ch = *p++;
1778 if (ch == '-' || ch == '+') {
1779 if (q >= token_buf + STRING_MAX_SIZE)
1780 goto num_too_long;
1781 *q++ = ch;
1782 ch = *p++;
1784 if (ch < '0' || ch > '9')
1785 expect("exponent digits");
1786 while (ch >= '0' && ch <= '9') {
1787 if (q >= token_buf + STRING_MAX_SIZE)
1788 goto num_too_long;
1789 *q++ = ch;
1790 ch = *p++;
1793 *q = '\0';
1794 t = toup(ch);
1795 errno = 0;
1796 if (t == 'F') {
1797 ch = *p++;
1798 tok = TOK_CFLOAT;
1799 tokc.f = strtof(token_buf, NULL);
1800 } else if (t == 'L') {
1801 ch = *p++;
1802 #ifdef TCC_TARGET_PE
1803 tok = TOK_CDOUBLE;
1804 tokc.d = strtod(token_buf, NULL);
1805 #else
1806 tok = TOK_CLDOUBLE;
1807 tokc.ld = strtold(token_buf, NULL);
1808 #endif
1809 } else {
1810 tok = TOK_CDOUBLE;
1811 tokc.d = strtod(token_buf, NULL);
1814 } else {
1815 unsigned long long n, n1;
1816 int lcount, ucount;
1818 /* integer number */
1819 *q = '\0';
1820 q = token_buf;
1821 if (b == 10 && *q == '0') {
1822 b = 8;
1823 q++;
1825 n = 0;
1826 while(1) {
1827 t = *q++;
1828 /* no need for checks except for base 10 / 8 errors */
1829 if (t == '\0') {
1830 break;
1831 } else if (t >= 'a') {
1832 t = t - 'a' + 10;
1833 } else if (t >= 'A') {
1834 t = t - 'A' + 10;
1835 } else {
1836 t = t - '0';
1837 if (t >= b)
1838 error("invalid digit");
1840 n1 = n;
1841 n = n * b + t;
1842 /* detect overflow */
1843 /* XXX: this test is not reliable */
1844 if (n < n1)
1845 error("integer constant overflow");
1848 /* XXX: not exactly ANSI compliant */
1849 if ((n & 0xffffffff00000000LL) != 0) {
1850 if ((n >> 63) != 0)
1851 tok = TOK_CULLONG;
1852 else
1853 tok = TOK_CLLONG;
1854 } else if (n > 0x7fffffff) {
1855 tok = TOK_CUINT;
1856 } else {
1857 tok = TOK_CINT;
1859 lcount = 0;
1860 ucount = 0;
1861 for(;;) {
1862 t = toup(ch);
1863 if (t == 'L') {
1864 if (lcount >= 2)
1865 error("three 'l's in integer constant");
1866 lcount++;
1867 if (lcount == 2) {
1868 if (tok == TOK_CINT)
1869 tok = TOK_CLLONG;
1870 else if (tok == TOK_CUINT)
1871 tok = TOK_CULLONG;
1873 ch = *p++;
1874 } else if (t == 'U') {
1875 if (ucount >= 1)
1876 error("two 'u's in integer constant");
1877 ucount++;
1878 if (tok == TOK_CINT)
1879 tok = TOK_CUINT;
1880 else if (tok == TOK_CLLONG)
1881 tok = TOK_CULLONG;
1882 ch = *p++;
1883 } else {
1884 break;
1887 if (tok == TOK_CINT || tok == TOK_CUINT)
1888 tokc.ui = n;
1889 else
1890 tokc.ull = n;
1892 if (ch)
1893 error("invalid number\n");
1897 #define PARSE2(c1, tok1, c2, tok2) \
1898 case c1: \
1899 PEEKC(c, p); \
1900 if (c == c2) { \
1901 p++; \
1902 tok = tok2; \
1903 } else { \
1904 tok = tok1; \
1906 break;
1908 /* return next token without macro substitution */
1909 static inline void next_nomacro1(void)
1911 int t, c, is_long;
1912 TokenSym *ts;
1913 uint8_t *p, *p1;
1914 unsigned int h;
1916 p = file->buf_ptr;
1917 redo_no_start:
1918 c = *p;
1919 switch(c) {
1920 case ' ':
1921 case '\t':
1922 tok = c;
1923 p++;
1924 goto keep_tok_flags;
1925 case '\f':
1926 case '\v':
1927 case '\r':
1928 p++;
1929 goto redo_no_start;
1930 case '\\':
1931 /* first look if it is in fact an end of buffer */
1932 if (p >= file->buf_end) {
1933 file->buf_ptr = p;
1934 handle_eob();
1935 p = file->buf_ptr;
1936 if (p >= file->buf_end)
1937 goto parse_eof;
1938 else
1939 goto redo_no_start;
1940 } else {
1941 file->buf_ptr = p;
1942 ch = *p;
1943 handle_stray();
1944 p = file->buf_ptr;
1945 goto redo_no_start;
1947 parse_eof:
1949 TCCState *s1 = tcc_state;
1950 if ((parse_flags & PARSE_FLAG_LINEFEED)
1951 && !(tok_flags & TOK_FLAG_EOF)) {
1952 tok_flags |= TOK_FLAG_EOF;
1953 tok = TOK_LINEFEED;
1954 goto keep_tok_flags;
1955 } else if (s1->include_stack_ptr == s1->include_stack ||
1956 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
1957 /* no include left : end of file. */
1958 tok = TOK_EOF;
1959 } else {
1960 tok_flags &= ~TOK_FLAG_EOF;
1961 /* pop include file */
1963 /* test if previous '#endif' was after a #ifdef at
1964 start of file */
1965 if (tok_flags & TOK_FLAG_ENDIF) {
1966 #ifdef INC_DEBUG
1967 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
1968 #endif
1969 add_cached_include(s1, file->inc_type, file->inc_filename,
1970 file->ifndef_macro_saved);
1973 /* add end of include file debug info */
1974 if (tcc_state->do_debug) {
1975 put_stabd(N_EINCL, 0, 0);
1977 /* pop include stack */
1978 tcc_close(file);
1979 s1->include_stack_ptr--;
1980 file = *s1->include_stack_ptr;
1981 p = file->buf_ptr;
1982 goto redo_no_start;
1985 break;
1987 case '\n':
1988 file->line_num++;
1989 tok_flags |= TOK_FLAG_BOL;
1990 p++;
1991 maybe_newline:
1992 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
1993 goto redo_no_start;
1994 tok = TOK_LINEFEED;
1995 goto keep_tok_flags;
1997 case '#':
1998 /* XXX: simplify */
1999 PEEKC(c, p);
2000 if ((tok_flags & TOK_FLAG_BOL) &&
2001 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2002 file->buf_ptr = p;
2003 preprocess(tok_flags & TOK_FLAG_BOF);
2004 p = file->buf_ptr;
2005 goto maybe_newline;
2006 } else {
2007 if (c == '#') {
2008 p++;
2009 tok = TOK_TWOSHARPS;
2010 } else {
2011 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2012 p = parse_line_comment(p - 1);
2013 goto redo_no_start;
2014 } else {
2015 tok = '#';
2019 break;
2021 case 'a': case 'b': case 'c': case 'd':
2022 case 'e': case 'f': case 'g': case 'h':
2023 case 'i': case 'j': case 'k': case 'l':
2024 case 'm': case 'n': case 'o': case 'p':
2025 case 'q': case 'r': case 's': case 't':
2026 case 'u': case 'v': case 'w': case 'x':
2027 case 'y': case 'z':
2028 case 'A': case 'B': case 'C': case 'D':
2029 case 'E': case 'F': case 'G': case 'H':
2030 case 'I': case 'J': case 'K':
2031 case 'M': case 'N': case 'O': case 'P':
2032 case 'Q': case 'R': case 'S': case 'T':
2033 case 'U': case 'V': case 'W': case 'X':
2034 case 'Y': case 'Z':
2035 case '_':
2036 parse_ident_fast:
2037 p1 = p;
2038 h = TOK_HASH_INIT;
2039 h = TOK_HASH_FUNC(h, c);
2040 p++;
2041 for(;;) {
2042 c = *p;
2043 if (!isidnum_table[c-CH_EOF])
2044 break;
2045 h = TOK_HASH_FUNC(h, c);
2046 p++;
2048 if (c != '\\') {
2049 TokenSym **pts;
2050 int len;
2052 /* fast case : no stray found, so we have the full token
2053 and we have already hashed it */
2054 len = p - p1;
2055 h &= (TOK_HASH_SIZE - 1);
2056 pts = &hash_ident[h];
2057 for(;;) {
2058 ts = *pts;
2059 if (!ts)
2060 break;
2061 if (ts->len == len && !memcmp(ts->str, p1, len))
2062 goto token_found;
2063 pts = &(ts->hash_next);
2065 ts = tok_alloc_new(pts, p1, len);
2066 token_found: ;
2067 } else {
2068 /* slower case */
2069 cstr_reset(&tokcstr);
2071 while (p1 < p) {
2072 cstr_ccat(&tokcstr, *p1);
2073 p1++;
2075 p--;
2076 PEEKC(c, p);
2077 parse_ident_slow:
2078 while (isidnum_table[c-CH_EOF]) {
2079 cstr_ccat(&tokcstr, c);
2080 PEEKC(c, p);
2082 ts = tok_alloc(tokcstr.data, tokcstr.size);
2084 tok = ts->tok;
2085 break;
2086 case 'L':
2087 t = p[1];
2088 if (t != '\\' && t != '\'' && t != '\"') {
2089 /* fast case */
2090 goto parse_ident_fast;
2091 } else {
2092 PEEKC(c, p);
2093 if (c == '\'' || c == '\"') {
2094 is_long = 1;
2095 goto str_const;
2096 } else {
2097 cstr_reset(&tokcstr);
2098 cstr_ccat(&tokcstr, 'L');
2099 goto parse_ident_slow;
2102 break;
2103 case '0': case '1': case '2': case '3':
2104 case '4': case '5': case '6': case '7':
2105 case '8': case '9':
2107 cstr_reset(&tokcstr);
2108 /* after the first digit, accept digits, alpha, '.' or sign if
2109 prefixed by 'eEpP' */
2110 parse_num:
2111 for(;;) {
2112 t = c;
2113 cstr_ccat(&tokcstr, c);
2114 PEEKC(c, p);
2115 if (!(isnum(c) || isid(c) || c == '.' ||
2116 ((c == '+' || c == '-') &&
2117 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2118 break;
2120 /* We add a trailing '\0' to ease parsing */
2121 cstr_ccat(&tokcstr, '\0');
2122 tokc.cstr = &tokcstr;
2123 tok = TOK_PPNUM;
2124 break;
2125 case '.':
2126 /* special dot handling because it can also start a number */
2127 PEEKC(c, p);
2128 if (isnum(c)) {
2129 cstr_reset(&tokcstr);
2130 cstr_ccat(&tokcstr, '.');
2131 goto parse_num;
2132 } else if (c == '.') {
2133 PEEKC(c, p);
2134 if (c != '.')
2135 expect("'.'");
2136 PEEKC(c, p);
2137 tok = TOK_DOTS;
2138 } else {
2139 tok = '.';
2141 break;
2142 case '\'':
2143 case '\"':
2144 is_long = 0;
2145 str_const:
2147 CString str;
2148 int sep;
2150 sep = c;
2152 /* parse the string */
2153 cstr_new(&str);
2154 p = parse_pp_string(p, sep, &str);
2155 cstr_ccat(&str, '\0');
2157 /* eval the escape (should be done as TOK_PPNUM) */
2158 cstr_reset(&tokcstr);
2159 parse_escape_string(&tokcstr, str.data, is_long);
2160 cstr_free(&str);
2162 if (sep == '\'') {
2163 int char_size;
2164 /* XXX: make it portable */
2165 if (!is_long)
2166 char_size = 1;
2167 else
2168 char_size = sizeof(nwchar_t);
2169 if (tokcstr.size <= char_size)
2170 error("empty character constant");
2171 if (tokcstr.size > 2 * char_size)
2172 warning("multi-character character constant");
2173 if (!is_long) {
2174 tokc.i = *(int8_t *)tokcstr.data;
2175 tok = TOK_CCHAR;
2176 } else {
2177 tokc.i = *(nwchar_t *)tokcstr.data;
2178 tok = TOK_LCHAR;
2180 } else {
2181 tokc.cstr = &tokcstr;
2182 if (!is_long)
2183 tok = TOK_STR;
2184 else
2185 tok = TOK_LSTR;
2188 break;
2190 case '<':
2191 PEEKC(c, p);
2192 if (c == '=') {
2193 p++;
2194 tok = TOK_LE;
2195 } else if (c == '<') {
2196 PEEKC(c, p);
2197 if (c == '=') {
2198 p++;
2199 tok = TOK_A_SHL;
2200 } else {
2201 tok = TOK_SHL;
2203 } else {
2204 tok = TOK_LT;
2206 break;
2208 case '>':
2209 PEEKC(c, p);
2210 if (c == '=') {
2211 p++;
2212 tok = TOK_GE;
2213 } else if (c == '>') {
2214 PEEKC(c, p);
2215 if (c == '=') {
2216 p++;
2217 tok = TOK_A_SAR;
2218 } else {
2219 tok = TOK_SAR;
2221 } else {
2222 tok = TOK_GT;
2224 break;
2226 case '&':
2227 PEEKC(c, p);
2228 if (c == '&') {
2229 p++;
2230 tok = TOK_LAND;
2231 } else if (c == '=') {
2232 p++;
2233 tok = TOK_A_AND;
2234 } else {
2235 tok = '&';
2237 break;
2239 case '|':
2240 PEEKC(c, p);
2241 if (c == '|') {
2242 p++;
2243 tok = TOK_LOR;
2244 } else if (c == '=') {
2245 p++;
2246 tok = TOK_A_OR;
2247 } else {
2248 tok = '|';
2250 break;
2252 case '+':
2253 PEEKC(c, p);
2254 if (c == '+') {
2255 p++;
2256 tok = TOK_INC;
2257 } else if (c == '=') {
2258 p++;
2259 tok = TOK_A_ADD;
2260 } else {
2261 tok = '+';
2263 break;
2265 case '-':
2266 PEEKC(c, p);
2267 if (c == '-') {
2268 p++;
2269 tok = TOK_DEC;
2270 } else if (c == '=') {
2271 p++;
2272 tok = TOK_A_SUB;
2273 } else if (c == '>') {
2274 p++;
2275 tok = TOK_ARROW;
2276 } else {
2277 tok = '-';
2279 break;
2281 PARSE2('!', '!', '=', TOK_NE)
2282 PARSE2('=', '=', '=', TOK_EQ)
2283 PARSE2('*', '*', '=', TOK_A_MUL)
2284 PARSE2('%', '%', '=', TOK_A_MOD)
2285 PARSE2('^', '^', '=', TOK_A_XOR)
2287 /* comments or operator */
2288 case '/':
2289 PEEKC(c, p);
2290 if (c == '*') {
2291 p = parse_comment(p);
2292 goto redo_no_start;
2293 } else if (c == '/') {
2294 p = parse_line_comment(p);
2295 goto redo_no_start;
2296 } else if (c == '=') {
2297 p++;
2298 tok = TOK_A_DIV;
2299 } else {
2300 tok = '/';
2302 break;
2304 /* simple tokens */
2305 case '(':
2306 case ')':
2307 case '[':
2308 case ']':
2309 case '{':
2310 case '}':
2311 case ',':
2312 case ';':
2313 case ':':
2314 case '?':
2315 case '~':
2316 case '$': /* only used in assembler */
2317 case '@': /* dito */
2318 tok = c;
2319 p++;
2320 break;
2321 default:
2322 error("unrecognized character \\x%02x", c);
2323 break;
2325 tok_flags = 0;
2326 keep_tok_flags:
2327 file->buf_ptr = p;
2328 #if defined(PARSE_DEBUG)
2329 printf("token = %s\n", get_tok_str(tok, &tokc));
2330 #endif
2333 /* return next token without macro substitution. Can read input from
2334 macro_ptr buffer */
2335 static void next_nomacro_spc(void)
2337 if (macro_ptr) {
2338 redo:
2339 tok = *macro_ptr;
2340 if (tok) {
2341 TOK_GET(tok, macro_ptr, tokc);
2342 if (tok == TOK_LINENUM) {
2343 file->line_num = tokc.i;
2344 goto redo;
2347 } else {
2348 next_nomacro1();
2352 static void next_nomacro(void)
2354 do {
2355 next_nomacro_spc();
2356 } while (is_space(tok));
2359 /* substitute args in macro_str and return allocated string */
2360 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
2362 int *st, last_tok, t, spc;
2363 Sym *s;
2364 CValue cval;
2365 TokenString str;
2366 CString cstr;
2368 tok_str_new(&str);
2369 last_tok = 0;
2370 while(1) {
2371 TOK_GET(t, macro_str, cval);
2372 if (!t)
2373 break;
2374 if (t == '#') {
2375 /* stringize */
2376 TOK_GET(t, macro_str, cval);
2377 if (!t)
2378 break;
2379 s = sym_find2(args, t);
2380 if (s) {
2381 cstr_new(&cstr);
2382 st = s->d;
2383 spc = 0;
2384 while (*st) {
2385 TOK_GET(t, st, cval);
2386 if (!check_space(t, &spc))
2387 cstr_cat(&cstr, get_tok_str(t, &cval));
2389 cstr.size -= spc;
2390 cstr_ccat(&cstr, '\0');
2391 #ifdef PP_DEBUG
2392 printf("stringize: %s\n", (char *)cstr.data);
2393 #endif
2394 /* add string */
2395 cval.cstr = &cstr;
2396 tok_str_add2(&str, TOK_STR, &cval);
2397 cstr_free(&cstr);
2398 } else {
2399 tok_str_add2(&str, t, &cval);
2401 } else if (t >= TOK_IDENT) {
2402 s = sym_find2(args, t);
2403 if (s) {
2404 st = s->d;
2405 /* if '##' is present before or after, no arg substitution */
2406 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2407 /* special case for var arg macros : ## eats the
2408 ',' if empty VA_ARGS variable. */
2409 /* XXX: test of the ',' is not 100%
2410 reliable. should fix it to avoid security
2411 problems */
2412 if (gnu_ext && s->type.t &&
2413 last_tok == TOK_TWOSHARPS &&
2414 str.len >= 2 && str.str[str.len - 2] == ',') {
2415 if (*st == 0) {
2416 /* suppress ',' '##' */
2417 str.len -= 2;
2418 } else {
2419 /* suppress '##' and add variable */
2420 str.len--;
2421 goto add_var;
2423 } else {
2424 int t1;
2425 add_var:
2426 for(;;) {
2427 TOK_GET(t1, st, cval);
2428 if (!t1)
2429 break;
2430 tok_str_add2(&str, t1, &cval);
2433 } else {
2434 /* NOTE: the stream cannot be read when macro
2435 substituing an argument */
2436 macro_subst(&str, nested_list, st, NULL);
2438 } else {
2439 tok_str_add(&str, t);
2441 } else {
2442 tok_str_add2(&str, t, &cval);
2444 last_tok = t;
2446 tok_str_add(&str, 0);
2447 return str.str;
2450 static char const ab_month_name[12][4] =
2452 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2453 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2456 /* do macro substitution of current token with macro 's' and add
2457 result to (tok_str,tok_len). 'nested_list' is the list of all
2458 macros we got inside to avoid recursing. Return non zero if no
2459 substitution needs to be done */
2460 static int macro_subst_tok(TokenString *tok_str,
2461 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2463 Sym *args, *sa, *sa1;
2464 int mstr_allocated, parlevel, *mstr, t, t1, *p, spc;
2465 TokenString str;
2466 char *cstrval;
2467 CValue cval;
2468 CString cstr;
2469 char buf[32];
2471 /* if symbol is a macro, prepare substitution */
2472 /* special macros */
2473 if (tok == TOK___LINE__) {
2474 snprintf(buf, sizeof(buf), "%d", file->line_num);
2475 cstrval = buf;
2476 t1 = TOK_PPNUM;
2477 goto add_cstr1;
2478 } else if (tok == TOK___FILE__) {
2479 cstrval = file->filename;
2480 goto add_cstr;
2481 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2482 time_t ti;
2483 struct tm *tm;
2485 time(&ti);
2486 tm = localtime(&ti);
2487 if (tok == TOK___DATE__) {
2488 snprintf(buf, sizeof(buf), "%s %2d %d",
2489 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2490 } else {
2491 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2492 tm->tm_hour, tm->tm_min, tm->tm_sec);
2494 cstrval = buf;
2495 add_cstr:
2496 t1 = TOK_STR;
2497 add_cstr1:
2498 cstr_new(&cstr);
2499 cstr_cat(&cstr, cstrval);
2500 cstr_ccat(&cstr, '\0');
2501 cval.cstr = &cstr;
2502 tok_str_add2(tok_str, t1, &cval);
2503 cstr_free(&cstr);
2504 } else {
2505 mstr = s->d;
2506 mstr_allocated = 0;
2507 if (s->type.t == MACRO_FUNC) {
2508 /* NOTE: we do not use next_nomacro to avoid eating the
2509 next token. XXX: find better solution */
2510 redo:
2511 if (macro_ptr) {
2512 p = macro_ptr;
2513 while (is_space(t = *p) || TOK_LINEFEED == t)
2514 ++p;
2515 if (t == 0 && can_read_stream) {
2516 /* end of macro stream: we must look at the token
2517 after in the file */
2518 struct macro_level *ml = *can_read_stream;
2519 macro_ptr = NULL;
2520 if (ml)
2522 macro_ptr = ml->p;
2523 ml->p = NULL;
2524 *can_read_stream = ml -> prev;
2526 goto redo;
2528 } else {
2529 /* XXX: incorrect with comments */
2530 ch = file->buf_ptr[0];
2531 while (is_space(ch) || ch == '\n')
2532 cinp();
2533 t = ch;
2535 if (t != '(') /* no macro subst */
2536 return -1;
2538 /* argument macro */
2539 next_nomacro();
2540 next_nomacro();
2541 args = NULL;
2542 sa = s->next;
2543 /* NOTE: empty args are allowed, except if no args */
2544 for(;;) {
2545 /* handle '()' case */
2546 if (!args && !sa && tok == ')')
2547 break;
2548 if (!sa)
2549 error("macro '%s' used with too many args",
2550 get_tok_str(s->v, 0));
2551 tok_str_new(&str);
2552 parlevel = spc = 0;
2553 /* NOTE: non zero sa->t indicates VA_ARGS */
2554 while ((parlevel > 0 ||
2555 (tok != ')' &&
2556 (tok != ',' || sa->type.t))) &&
2557 tok != -1) {
2558 if (tok == '(')
2559 parlevel++;
2560 else if (tok == ')')
2561 parlevel--;
2562 if (tok == TOK_LINEFEED)
2563 tok = ' ';
2564 if (!check_space(tok, &spc))
2565 tok_str_add2(&str, tok, &tokc);
2566 next_nomacro_spc();
2568 str.len -= spc;
2569 tok_str_add(&str, 0);
2570 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2571 sa1->d = str.str;
2572 sa = sa->next;
2573 if (tok == ')') {
2574 /* special case for gcc var args: add an empty
2575 var arg argument if it is omitted */
2576 if (sa && sa->type.t && gnu_ext)
2577 continue;
2578 else
2579 break;
2581 if (tok != ',')
2582 expect(",");
2583 next_nomacro();
2585 if (sa) {
2586 error("macro '%s' used with too few args",
2587 get_tok_str(s->v, 0));
2590 /* now subst each arg */
2591 mstr = macro_arg_subst(nested_list, mstr, args);
2592 /* free memory */
2593 sa = args;
2594 while (sa) {
2595 sa1 = sa->prev;
2596 tok_str_free(sa->d);
2597 sym_free(sa);
2598 sa = sa1;
2600 mstr_allocated = 1;
2602 sym_push2(nested_list, s->v, 0, 0);
2603 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2604 /* pop nested defined symbol */
2605 sa1 = *nested_list;
2606 *nested_list = sa1->prev;
2607 sym_free(sa1);
2608 if (mstr_allocated)
2609 tok_str_free(mstr);
2611 return 0;
2614 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2615 return the resulting string (which must be freed). */
2616 static inline int *macro_twosharps(const int *macro_str)
2618 const int *ptr;
2619 int t;
2620 CValue cval;
2621 TokenString macro_str1;
2622 CString cstr;
2623 char *p;
2624 int n;
2626 /* we search the first '##' */
2627 for(ptr = macro_str;;) {
2628 TOK_GET(t, ptr, cval);
2629 if (t == TOK_TWOSHARPS)
2630 break;
2631 /* nothing more to do if end of string */
2632 if (t == 0)
2633 return NULL;
2636 /* we saw '##', so we need more processing to handle it */
2637 tok_str_new(&macro_str1);
2638 for(ptr = macro_str;;) {
2639 TOK_GET(tok, ptr, tokc);
2640 if (tok == 0)
2641 break;
2642 if (tok == TOK_TWOSHARPS)
2643 continue;
2644 while (*ptr == TOK_TWOSHARPS) {
2645 t = *++ptr;
2646 if (t && t != TOK_TWOSHARPS) {
2647 TOK_GET(t, ptr, cval);
2649 /* We concatenate the two tokens */
2650 cstr_new(&cstr);
2651 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2652 n = cstr.size;
2653 cstr_cat(&cstr, get_tok_str(t, &cval));
2654 cstr_ccat(&cstr, '\0');
2656 p = file->buf_ptr;
2657 file->buf_ptr = cstr.data;
2658 for (;;) {
2659 next_nomacro1();
2660 if (0 == *file->buf_ptr)
2661 break;
2662 tok_str_add2(&macro_str1, tok, &tokc);
2664 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2665 n, cstr.data, (char*)cstr.data + n);
2667 file->buf_ptr = p;
2668 cstr_reset(&cstr);
2671 tok_str_add2(&macro_str1, tok, &tokc);
2673 tok_str_add(&macro_str1, 0);
2674 return macro_str1.str;
2678 /* do macro substitution of macro_str and add result to
2679 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2680 inside to avoid recursing. */
2681 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2682 const int *macro_str, struct macro_level ** can_read_stream)
2684 Sym *s;
2685 int *macro_str1;
2686 const int *ptr;
2687 int t, ret, spc;
2688 CValue cval;
2689 struct macro_level ml;
2691 /* first scan for '##' operator handling */
2692 ptr = macro_str;
2693 macro_str1 = macro_twosharps(ptr);
2694 if (macro_str1)
2695 ptr = macro_str1;
2696 spc = 0;
2697 while (1) {
2698 /* NOTE: ptr == NULL can only happen if tokens are read from
2699 file stream due to a macro function call */
2700 if (ptr == NULL)
2701 break;
2702 TOK_GET(t, ptr, cval);
2703 if (t == 0)
2704 break;
2705 s = define_find(t);
2706 if (s != NULL) {
2707 /* if nested substitution, do nothing */
2708 if (sym_find2(*nested_list, t))
2709 goto no_subst;
2710 ml.p = macro_ptr;
2711 if (can_read_stream)
2712 ml.prev = *can_read_stream, *can_read_stream = &ml;
2713 macro_ptr = (int *)ptr;
2714 tok = t;
2715 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
2716 ptr = (int *)macro_ptr;
2717 macro_ptr = ml.p;
2718 if (can_read_stream && *can_read_stream == &ml)
2719 *can_read_stream = ml.prev;
2720 if (ret != 0)
2721 goto no_subst;
2722 } else {
2723 no_subst:
2724 if (!check_space(t, &spc))
2725 tok_str_add2(tok_str, t, &cval);
2728 if (macro_str1)
2729 tok_str_free(macro_str1);
2732 /* return next token with macro substitution */
2733 static void next(void)
2735 Sym *nested_list, *s;
2736 TokenString str;
2737 struct macro_level *ml;
2739 redo:
2740 if (parse_flags & PARSE_FLAG_SPACES)
2741 next_nomacro_spc();
2742 else
2743 next_nomacro();
2744 if (!macro_ptr) {
2745 /* if not reading from macro substituted string, then try
2746 to substitute macros */
2747 if (tok >= TOK_IDENT &&
2748 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2749 s = define_find(tok);
2750 if (s) {
2751 /* we have a macro: we try to substitute */
2752 tok_str_new(&str);
2753 nested_list = NULL;
2754 ml = NULL;
2755 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
2756 /* substitution done, NOTE: maybe empty */
2757 tok_str_add(&str, 0);
2758 macro_ptr = str.str;
2759 macro_ptr_allocated = str.str;
2760 goto redo;
2764 } else {
2765 if (tok == 0) {
2766 /* end of macro or end of unget buffer */
2767 if (unget_buffer_enabled) {
2768 macro_ptr = unget_saved_macro_ptr;
2769 unget_buffer_enabled = 0;
2770 } else {
2771 /* end of macro string: free it */
2772 tok_str_free(macro_ptr_allocated);
2773 macro_ptr = NULL;
2775 goto redo;
2779 /* convert preprocessor tokens into C tokens */
2780 if (tok == TOK_PPNUM &&
2781 (parse_flags & PARSE_FLAG_TOK_NUM)) {
2782 parse_number((char *)tokc.cstr->data);
2786 /* push back current token and set current token to 'last_tok'. Only
2787 identifier case handled for labels. */
2788 static inline void unget_tok(int last_tok)
2790 int i, n;
2791 int *q;
2792 unget_saved_macro_ptr = macro_ptr;
2793 unget_buffer_enabled = 1;
2794 q = unget_saved_buffer;
2795 macro_ptr = q;
2796 *q++ = tok;
2797 n = tok_ext_size(tok) - 1;
2798 for(i=0;i<n;i++)
2799 *q++ = tokc.tab[i];
2800 *q = 0; /* end of token string */
2801 tok = last_tok;
2805 /* better than nothing, but needs extension to handle '-E' option
2806 correctly too */
2807 static void preprocess_init(TCCState *s1)
2809 s1->include_stack_ptr = s1->include_stack;
2810 /* XXX: move that before to avoid having to initialize
2811 file->ifdef_stack_ptr ? */
2812 s1->ifdef_stack_ptr = s1->ifdef_stack;
2813 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2815 /* XXX: not ANSI compliant: bound checking says error */
2816 vtop = vstack - 1;
2817 s1->pack_stack[0] = 0;
2818 s1->pack_stack_ptr = s1->pack_stack;
2821 void preprocess_new()
2823 int i, c;
2824 const char *p, *r;
2825 TokenSym *ts;
2827 /* init isid table */
2828 for(i=CH_EOF;i<256;i++)
2829 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
2831 /* add all tokens */
2832 table_ident = NULL;
2833 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
2835 tok_ident = TOK_IDENT;
2836 p = tcc_keywords;
2837 while (*p) {
2838 r = p;
2839 for(;;) {
2840 c = *r++;
2841 if (c == '\0')
2842 break;
2844 ts = tok_alloc(p, r - p - 1);
2845 p = r;
2849 /* Preprocess the current file */
2850 static int tcc_preprocess(TCCState *s1)
2852 Sym *define_start;
2853 BufferedFile *file_ref, **iptr, **iptr_new;
2854 int token_seen, line_ref, d;
2855 const char *s;
2857 preprocess_init(s1);
2858 define_start = define_stack;
2859 ch = file->buf_ptr[0];
2860 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
2861 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
2862 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
2863 token_seen = 0;
2864 line_ref = 0;
2865 file_ref = NULL;
2867 iptr = s1->include_stack_ptr;
2868 for (;;) {
2869 next();
2870 if (tok == TOK_EOF) {
2871 break;
2872 } else if (file != file_ref) {
2873 goto print_line;
2874 } else if (tok == TOK_LINEFEED) {
2875 if (!token_seen)
2876 continue;
2877 ++line_ref;
2878 token_seen = 0;
2879 } else if (!token_seen) {
2880 d = file->line_num - line_ref;
2881 if (file != file_ref || d < 0 || d >= 8) {
2882 print_line:
2883 iptr_new = s1->include_stack_ptr;
2884 s = iptr_new > iptr ? " 1"
2885 : iptr_new < iptr ? " 2"
2886 : iptr_new > s1->include_stack ? " 3"
2887 : ""
2889 iptr = iptr_new;
2890 fprintf(s1->outfile, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
2891 } else {
2892 while (d)
2893 fputs("\n", s1->outfile), --d;
2895 line_ref = (file_ref = file)->line_num;
2896 token_seen = tok != TOK_LINEFEED;
2897 if (!token_seen)
2898 continue;
2900 fputs(get_tok_str(tok, &tokc), s1->outfile);
2902 free_defines(define_start);
2903 return 0;