VLA code: minor fix
[tinycc.git] / tccpp.c
blob95c3f233602dc71ac3f8ce76a9287af86b0938e6
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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
19 */
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 ST_DATA int tok_flags;
27 ST_DATA int parse_flags;
29 ST_DATA struct BufferedFile *file;
30 ST_DATA int ch, tok;
31 ST_DATA CValue tokc;
32 ST_DATA const int *macro_ptr;
33 ST_DATA CString tokcstr; /* current parsed string, if any */
35 /* display benchmark infos */
36 ST_DATA int total_lines;
37 ST_DATA int total_bytes;
38 ST_DATA int tok_ident;
39 ST_DATA TokenSym **table_ident;
41 /* ------------------------------------------------------------------------- */
43 static int *macro_ptr_allocated;
44 static const int *unget_saved_macro_ptr;
45 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
46 static int unget_buffer_enabled;
47 static TokenSym *hash_ident[TOK_HASH_SIZE];
48 static char token_buf[STRING_MAX_SIZE + 1];
49 /* true if isid(c) || isnum(c) */
50 static unsigned char isidnum_table[256-CH_EOF];
52 static const char tcc_keywords[] =
53 #define DEF(id, str) str "\0"
54 #include "tcctok.h"
55 #undef DEF
56 ;
58 /* WARNING: the content of this string encodes token numbers */
59 static const unsigned char tok_two_chars[] =
60 /* outdated -- gr
61 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
62 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
63 */{
64 '<','=', TOK_LE,
65 '>','=', TOK_GE,
66 '!','=', TOK_NE,
67 '&','&', TOK_LAND,
68 '|','|', TOK_LOR,
69 '+','+', TOK_INC,
70 '-','-', TOK_DEC,
71 '=','=', TOK_EQ,
72 '<','<', TOK_SHL,
73 '>','>', TOK_SAR,
74 '+','=', TOK_A_ADD,
75 '-','=', TOK_A_SUB,
76 '*','=', TOK_A_MUL,
77 '/','=', TOK_A_DIV,
78 '%','=', TOK_A_MOD,
79 '&','=', TOK_A_AND,
80 '^','=', TOK_A_XOR,
81 '|','=', TOK_A_OR,
82 '-','>', TOK_ARROW,
83 '.','.', 0xa8, // C++ token ?
84 '#','#', TOK_TWOSHARPS,
85 0
86 };
88 struct macro_level {
89 struct macro_level *prev;
90 const int *p;
91 };
93 static void next_nomacro_spc(void);
94 static void macro_subst(
95 TokenString *tok_str,
96 Sym **nested_list,
97 const int *macro_str,
98 struct macro_level **can_read_stream
99 );
101 ST_FUNC void skip(int c)
103 if (tok != c)
104 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
105 next();
108 ST_FUNC void expect(const char *msg)
110 tcc_error("%s expected", msg);
113 /* ------------------------------------------------------------------------- */
114 /* CString handling */
115 static void cstr_realloc(CString *cstr, int new_size)
117 int size;
118 void *data;
120 size = cstr->size_allocated;
121 if (size == 0)
122 size = 8; /* no need to allocate a too small first string */
123 while (size < new_size)
124 size = size * 2;
125 data = tcc_realloc(cstr->data_allocated, size);
126 cstr->data_allocated = data;
127 cstr->size_allocated = size;
128 cstr->data = data;
131 /* add a byte */
132 ST_FUNC void cstr_ccat(CString *cstr, int ch)
134 int size;
135 size = cstr->size + 1;
136 if (size > cstr->size_allocated)
137 cstr_realloc(cstr, size);
138 ((unsigned char *)cstr->data)[size - 1] = ch;
139 cstr->size = size;
142 ST_FUNC void cstr_cat(CString *cstr, const char *str)
144 int c;
145 for(;;) {
146 c = *str;
147 if (c == '\0')
148 break;
149 cstr_ccat(cstr, c);
150 str++;
154 /* add a wide char */
155 ST_FUNC void cstr_wccat(CString *cstr, int ch)
157 int size;
158 size = cstr->size + sizeof(nwchar_t);
159 if (size > cstr->size_allocated)
160 cstr_realloc(cstr, size);
161 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
162 cstr->size = size;
165 ST_FUNC void cstr_new(CString *cstr)
167 memset(cstr, 0, sizeof(CString));
170 /* free string and reset it to NULL */
171 ST_FUNC void cstr_free(CString *cstr)
173 tcc_free(cstr->data_allocated);
174 cstr_new(cstr);
177 /* reset string to empty */
178 ST_FUNC void cstr_reset(CString *cstr)
180 cstr->size = 0;
183 /* XXX: unicode ? */
184 static void add_char(CString *cstr, int c)
186 if (c == '\'' || c == '\"' || c == '\\') {
187 /* XXX: could be more precise if char or string */
188 cstr_ccat(cstr, '\\');
190 if (c >= 32 && c <= 126) {
191 cstr_ccat(cstr, c);
192 } else {
193 cstr_ccat(cstr, '\\');
194 if (c == '\n') {
195 cstr_ccat(cstr, 'n');
196 } else {
197 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
198 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
199 cstr_ccat(cstr, '0' + (c & 7));
204 /* ------------------------------------------------------------------------- */
205 /* allocate a new token */
206 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
208 TokenSym *ts, **ptable;
209 int i;
211 if (tok_ident >= SYM_FIRST_ANOM)
212 tcc_error("memory full (symbols)");
214 /* expand token table if needed */
215 i = tok_ident - TOK_IDENT;
216 if ((i % TOK_ALLOC_INCR) == 0) {
217 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
218 table_ident = ptable;
221 ts = tcc_malloc(sizeof(TokenSym) + len);
222 table_ident[i] = ts;
223 ts->tok = tok_ident++;
224 ts->sym_define = NULL;
225 ts->sym_label = NULL;
226 ts->sym_struct = NULL;
227 ts->sym_identifier = NULL;
228 ts->len = len;
229 ts->hash_next = NULL;
230 memcpy(ts->str, str, len);
231 ts->str[len] = '\0';
232 *pts = ts;
233 return ts;
236 #define TOK_HASH_INIT 1
237 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
239 /* find a token and add it if not found */
240 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
242 TokenSym *ts, **pts;
243 int i;
244 unsigned int h;
246 h = TOK_HASH_INIT;
247 for(i=0;i<len;i++)
248 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
249 h &= (TOK_HASH_SIZE - 1);
251 pts = &hash_ident[h];
252 for(;;) {
253 ts = *pts;
254 if (!ts)
255 break;
256 if (ts->len == len && !memcmp(ts->str, str, len))
257 return ts;
258 pts = &(ts->hash_next);
260 return tok_alloc_new(pts, str, len);
263 /* XXX: buffer overflow */
264 /* XXX: float tokens */
265 ST_FUNC char *get_tok_str(int v, CValue *cv)
267 static char buf[STRING_MAX_SIZE + 1];
268 static CString cstr_buf;
269 CString *cstr;
270 char *p;
271 int i, len;
273 /* NOTE: to go faster, we give a fixed buffer for small strings */
274 cstr_reset(&cstr_buf);
275 cstr_buf.data = buf;
276 cstr_buf.size_allocated = sizeof(buf);
277 p = buf;
279 /* just an explanation, should never happen:
280 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
281 tcc_error("internal error: get_tok_str"); */
283 switch(v) {
284 case TOK_CINT:
285 case TOK_CUINT:
286 /* XXX: not quite exact, but only useful for testing */
287 sprintf(p, "%u", cv->ui);
288 break;
289 case TOK_CLLONG:
290 case TOK_CULLONG:
291 /* XXX: not quite exact, but only useful for testing */
292 #ifdef _WIN32
293 sprintf(p, "%u", (unsigned)cv->ull);
294 #else
295 sprintf(p, "%llu", cv->ull);
296 #endif
297 break;
298 case TOK_LCHAR:
299 cstr_ccat(&cstr_buf, 'L');
300 case TOK_CCHAR:
301 cstr_ccat(&cstr_buf, '\'');
302 add_char(&cstr_buf, cv->i);
303 cstr_ccat(&cstr_buf, '\'');
304 cstr_ccat(&cstr_buf, '\0');
305 break;
306 case TOK_PPNUM:
307 cstr = cv->cstr;
308 len = cstr->size - 1;
309 for(i=0;i<len;i++)
310 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
311 cstr_ccat(&cstr_buf, '\0');
312 break;
313 case TOK_LSTR:
314 cstr_ccat(&cstr_buf, 'L');
315 case TOK_STR:
316 cstr = cv->cstr;
317 cstr_ccat(&cstr_buf, '\"');
318 if (v == TOK_STR) {
319 len = cstr->size - 1;
320 for(i=0;i<len;i++)
321 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
322 } else {
323 len = (cstr->size / sizeof(nwchar_t)) - 1;
324 for(i=0;i<len;i++)
325 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
327 cstr_ccat(&cstr_buf, '\"');
328 cstr_ccat(&cstr_buf, '\0');
329 break;
331 case TOK_CFLOAT:
332 case TOK_CDOUBLE:
333 case TOK_CLDOUBLE:
334 case TOK_LINENUM:
335 return NULL; /* should not happen */
337 /* above tokens have value, the ones below don't */
339 case TOK_LT:
340 v = '<';
341 goto addv;
342 case TOK_GT:
343 v = '>';
344 goto addv;
345 case TOK_DOTS:
346 return strcpy(p, "...");
347 case TOK_A_SHL:
348 return strcpy(p, "<<=");
349 case TOK_A_SAR:
350 return strcpy(p, ">>=");
351 default:
352 if (v < TOK_IDENT) {
353 /* search in two bytes table */
354 const unsigned char *q = tok_two_chars;
355 while (*q) {
356 if (q[2] == v) {
357 *p++ = q[0];
358 *p++ = q[1];
359 *p = '\0';
360 return buf;
362 q += 3;
364 addv:
365 *p++ = v;
366 *p = '\0';
367 } else if (v < tok_ident) {
368 return table_ident[v - TOK_IDENT]->str;
369 } else if (v >= SYM_FIRST_ANOM) {
370 /* special name for anonymous symbol */
371 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
372 } else {
373 /* should never happen */
374 return NULL;
376 break;
378 return cstr_buf.data;
381 /* fill input buffer and peek next char */
382 static int tcc_peekc_slow(BufferedFile *bf)
384 int len;
385 /* only tries to read if really end of buffer */
386 if (bf->buf_ptr >= bf->buf_end) {
387 if (bf->fd != -1) {
388 #if defined(PARSE_DEBUG)
389 len = 1;
390 #else
391 len = IO_BUF_SIZE;
392 #endif
393 len = read(bf->fd, bf->buffer, len);
394 if (len < 0)
395 len = 0;
396 } else {
397 len = 0;
399 total_bytes += len;
400 bf->buf_ptr = bf->buffer;
401 bf->buf_end = bf->buffer + len;
402 *bf->buf_end = CH_EOB;
404 if (bf->buf_ptr < bf->buf_end) {
405 return bf->buf_ptr[0];
406 } else {
407 bf->buf_ptr = bf->buf_end;
408 return CH_EOF;
412 /* return the current character, handling end of block if necessary
413 (but not stray) */
414 ST_FUNC int handle_eob(void)
416 return tcc_peekc_slow(file);
419 /* read next char from current input file and handle end of input buffer */
420 ST_INLN void inp(void)
422 ch = *(++(file->buf_ptr));
423 /* end of buffer/file handling */
424 if (ch == CH_EOB)
425 ch = handle_eob();
428 /* handle '\[\r]\n' */
429 static int handle_stray_noerror(void)
431 while (ch == '\\') {
432 inp();
433 if (ch == '\n') {
434 file->line_num++;
435 inp();
436 } else if (ch == '\r') {
437 inp();
438 if (ch != '\n')
439 goto fail;
440 file->line_num++;
441 inp();
442 } else {
443 fail:
444 return 1;
447 return 0;
450 static void handle_stray(void)
452 if (handle_stray_noerror())
453 tcc_error("stray '\\' in program");
456 /* skip the stray and handle the \\n case. Output an error if
457 incorrect char after the stray */
458 static int handle_stray1(uint8_t *p)
460 int c;
462 if (p >= file->buf_end) {
463 file->buf_ptr = p;
464 c = handle_eob();
465 p = file->buf_ptr;
466 if (c == '\\')
467 goto parse_stray;
468 } else {
469 parse_stray:
470 file->buf_ptr = p;
471 ch = *p;
472 handle_stray();
473 p = file->buf_ptr;
474 c = *p;
476 return c;
479 /* handle just the EOB case, but not stray */
480 #define PEEKC_EOB(c, p)\
481 {\
482 p++;\
483 c = *p;\
484 if (c == '\\') {\
485 file->buf_ptr = p;\
486 c = handle_eob();\
487 p = file->buf_ptr;\
488 }\
491 /* handle the complicated stray case */
492 #define PEEKC(c, p)\
493 {\
494 p++;\
495 c = *p;\
496 if (c == '\\') {\
497 c = handle_stray1(p);\
498 p = file->buf_ptr;\
499 }\
502 /* input with '\[\r]\n' handling. Note that this function cannot
503 handle other characters after '\', so you cannot call it inside
504 strings or comments */
505 ST_FUNC void minp(void)
507 inp();
508 if (ch == '\\')
509 handle_stray();
513 /* single line C++ comments */
514 static uint8_t *parse_line_comment(uint8_t *p)
516 int c;
518 p++;
519 for(;;) {
520 c = *p;
521 redo:
522 if (c == '\n' || c == CH_EOF) {
523 break;
524 } else if (c == '\\') {
525 file->buf_ptr = p;
526 c = handle_eob();
527 p = file->buf_ptr;
528 if (c == '\\') {
529 PEEKC_EOB(c, p);
530 if (c == '\n') {
531 file->line_num++;
532 PEEKC_EOB(c, p);
533 } else if (c == '\r') {
534 PEEKC_EOB(c, p);
535 if (c == '\n') {
536 file->line_num++;
537 PEEKC_EOB(c, p);
540 } else {
541 goto redo;
543 } else {
544 p++;
547 return p;
550 /* C comments */
551 ST_FUNC uint8_t *parse_comment(uint8_t *p)
553 int c;
555 p++;
556 for(;;) {
557 /* fast skip loop */
558 for(;;) {
559 c = *p;
560 if (c == '\n' || c == '*' || c == '\\')
561 break;
562 p++;
563 c = *p;
564 if (c == '\n' || c == '*' || c == '\\')
565 break;
566 p++;
568 /* now we can handle all the cases */
569 if (c == '\n') {
570 file->line_num++;
571 p++;
572 } else if (c == '*') {
573 p++;
574 for(;;) {
575 c = *p;
576 if (c == '*') {
577 p++;
578 } else if (c == '/') {
579 goto end_of_comment;
580 } else if (c == '\\') {
581 file->buf_ptr = p;
582 c = handle_eob();
583 p = file->buf_ptr;
584 if (c == '\\') {
585 /* skip '\[\r]\n', otherwise just skip the stray */
586 while (c == '\\') {
587 PEEKC_EOB(c, p);
588 if (c == '\n') {
589 file->line_num++;
590 PEEKC_EOB(c, p);
591 } else if (c == '\r') {
592 PEEKC_EOB(c, p);
593 if (c == '\n') {
594 file->line_num++;
595 PEEKC_EOB(c, p);
597 } else {
598 goto after_star;
602 } else {
603 break;
606 after_star: ;
607 } else {
608 /* stray, eob or eof */
609 file->buf_ptr = p;
610 c = handle_eob();
611 p = file->buf_ptr;
612 if (c == CH_EOF) {
613 tcc_error("unexpected end of file in comment");
614 } else if (c == '\\') {
615 p++;
619 end_of_comment:
620 p++;
621 return p;
624 #define cinp minp
626 static inline void skip_spaces(void)
628 while (is_space(ch))
629 cinp();
632 static inline int check_space(int t, int *spc)
634 if (is_space(t)) {
635 if (*spc)
636 return 1;
637 *spc = 1;
638 } else
639 *spc = 0;
640 return 0;
643 /* parse a string without interpreting escapes */
644 static uint8_t *parse_pp_string(uint8_t *p,
645 int sep, CString *str)
647 int c;
648 p++;
649 for(;;) {
650 c = *p;
651 if (c == sep) {
652 break;
653 } else if (c == '\\') {
654 file->buf_ptr = p;
655 c = handle_eob();
656 p = file->buf_ptr;
657 if (c == CH_EOF) {
658 unterminated_string:
659 /* XXX: indicate line number of start of string */
660 tcc_error("missing terminating %c character", sep);
661 } else if (c == '\\') {
662 /* escape : just skip \[\r]\n */
663 PEEKC_EOB(c, p);
664 if (c == '\n') {
665 file->line_num++;
666 p++;
667 } else if (c == '\r') {
668 PEEKC_EOB(c, p);
669 if (c != '\n')
670 expect("'\n' after '\r'");
671 file->line_num++;
672 p++;
673 } else if (c == CH_EOF) {
674 goto unterminated_string;
675 } else {
676 if (str) {
677 cstr_ccat(str, '\\');
678 cstr_ccat(str, c);
680 p++;
683 } else if (c == '\n') {
684 file->line_num++;
685 goto add_char;
686 } else if (c == '\r') {
687 PEEKC_EOB(c, p);
688 if (c != '\n') {
689 if (str)
690 cstr_ccat(str, '\r');
691 } else {
692 file->line_num++;
693 goto add_char;
695 } else {
696 add_char:
697 if (str)
698 cstr_ccat(str, c);
699 p++;
702 p++;
703 return p;
706 /* skip block of text until #else, #elif or #endif. skip also pairs of
707 #if/#endif */
708 static void preprocess_skip(void)
710 int a, start_of_line, c, in_warn_or_error;
711 uint8_t *p;
713 p = file->buf_ptr;
714 a = 0;
715 redo_start:
716 start_of_line = 1;
717 in_warn_or_error = 0;
718 for(;;) {
719 redo_no_start:
720 c = *p;
721 switch(c) {
722 case ' ':
723 case '\t':
724 case '\f':
725 case '\v':
726 case '\r':
727 p++;
728 goto redo_no_start;
729 case '\n':
730 file->line_num++;
731 p++;
732 goto redo_start;
733 case '\\':
734 file->buf_ptr = p;
735 c = handle_eob();
736 if (c == CH_EOF) {
737 expect("#endif");
738 } else if (c == '\\') {
739 ch = file->buf_ptr[0];
740 handle_stray_noerror();
742 p = file->buf_ptr;
743 goto redo_no_start;
744 /* skip strings */
745 case '\"':
746 case '\'':
747 if (in_warn_or_error)
748 goto _default;
749 p = parse_pp_string(p, c, NULL);
750 break;
751 /* skip comments */
752 case '/':
753 if (in_warn_or_error)
754 goto _default;
755 file->buf_ptr = p;
756 ch = *p;
757 minp();
758 p = file->buf_ptr;
759 if (ch == '*') {
760 p = parse_comment(p);
761 } else if (ch == '/') {
762 p = parse_line_comment(p);
764 break;
765 case '#':
766 p++;
767 if (start_of_line) {
768 file->buf_ptr = p;
769 next_nomacro();
770 p = file->buf_ptr;
771 if (a == 0 &&
772 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
773 goto the_end;
774 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
775 a++;
776 else if (tok == TOK_ENDIF)
777 a--;
778 else if( tok == TOK_ERROR || tok == TOK_WARNING)
779 in_warn_or_error = 1;
780 else if (tok == TOK_LINEFEED)
781 goto redo_start;
782 else if (parse_flags & PARSE_FLAG_ASM_FILE)
783 p = parse_line_comment(p);
785 else if (parse_flags & PARSE_FLAG_ASM_FILE)
786 p = parse_line_comment(p);
787 break;
788 _default:
789 default:
790 p++;
791 break;
793 start_of_line = 0;
795 the_end: ;
796 file->buf_ptr = p;
799 /* ParseState handling */
801 /* XXX: currently, no include file info is stored. Thus, we cannot display
802 accurate messages if the function or data definition spans multiple
803 files */
805 /* save current parse state in 's' */
806 ST_FUNC void save_parse_state(ParseState *s)
808 s->line_num = file->line_num;
809 s->macro_ptr = macro_ptr;
810 s->tok = tok;
811 s->tokc = tokc;
814 /* restore parse state from 's' */
815 ST_FUNC void restore_parse_state(ParseState *s)
817 file->line_num = s->line_num;
818 macro_ptr = s->macro_ptr;
819 tok = s->tok;
820 tokc = s->tokc;
823 /* return the number of additional 'ints' necessary to store the
824 token */
825 static inline int tok_ext_size(int t)
827 switch(t) {
828 /* 4 bytes */
829 case TOK_CINT:
830 case TOK_CUINT:
831 case TOK_CCHAR:
832 case TOK_LCHAR:
833 case TOK_CFLOAT:
834 case TOK_LINENUM:
835 return 1;
836 case TOK_STR:
837 case TOK_LSTR:
838 case TOK_PPNUM:
839 tcc_error("unsupported token");
840 return 1;
841 case TOK_CDOUBLE:
842 case TOK_CLLONG:
843 case TOK_CULLONG:
844 return 2;
845 case TOK_CLDOUBLE:
846 return LDOUBLE_SIZE / 4;
847 default:
848 return 0;
852 /* token string handling */
854 ST_INLN void tok_str_new(TokenString *s)
856 s->str = NULL;
857 s->len = 0;
858 s->allocated_len = 0;
859 s->last_line_num = -1;
862 ST_FUNC void tok_str_free(int *str)
864 tcc_free(str);
867 static int *tok_str_realloc(TokenString *s)
869 int *str, len;
871 if (s->allocated_len == 0) {
872 len = 8;
873 } else {
874 len = s->allocated_len * 2;
876 str = tcc_realloc(s->str, len * sizeof(int));
877 s->allocated_len = len;
878 s->str = str;
879 return str;
882 ST_FUNC void tok_str_add(TokenString *s, int t)
884 int len, *str;
886 len = s->len;
887 str = s->str;
888 if (len >= s->allocated_len)
889 str = tok_str_realloc(s);
890 str[len++] = t;
891 s->len = len;
894 static void tok_str_add2(TokenString *s, int t, CValue *cv)
896 int len, *str;
898 len = s->len;
899 str = s->str;
901 /* allocate space for worst case */
902 if (len + TOK_MAX_SIZE > s->allocated_len)
903 str = tok_str_realloc(s);
904 str[len++] = t;
905 switch(t) {
906 case TOK_CINT:
907 case TOK_CUINT:
908 case TOK_CCHAR:
909 case TOK_LCHAR:
910 case TOK_CFLOAT:
911 case TOK_LINENUM:
912 str[len++] = cv->tab[0];
913 break;
914 case TOK_PPNUM:
915 case TOK_STR:
916 case TOK_LSTR:
918 int nb_words;
919 CString *cstr;
921 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
922 while ((len + nb_words) > s->allocated_len)
923 str = tok_str_realloc(s);
924 cstr = (CString *)(str + len);
925 cstr->data = NULL;
926 cstr->size = cv->cstr->size;
927 cstr->data_allocated = NULL;
928 cstr->size_allocated = cstr->size;
929 memcpy((char *)cstr + sizeof(CString),
930 cv->cstr->data, cstr->size);
931 len += nb_words;
933 break;
934 case TOK_CDOUBLE:
935 case TOK_CLLONG:
936 case TOK_CULLONG:
937 #if LDOUBLE_SIZE == 8
938 case TOK_CLDOUBLE:
939 #endif
940 str[len++] = cv->tab[0];
941 str[len++] = cv->tab[1];
942 break;
943 #if LDOUBLE_SIZE == 12
944 case TOK_CLDOUBLE:
945 str[len++] = cv->tab[0];
946 str[len++] = cv->tab[1];
947 str[len++] = cv->tab[2];
948 #elif LDOUBLE_SIZE == 16
949 case TOK_CLDOUBLE:
950 str[len++] = cv->tab[0];
951 str[len++] = cv->tab[1];
952 str[len++] = cv->tab[2];
953 str[len++] = cv->tab[3];
954 #elif LDOUBLE_SIZE != 8
955 #error add long double size support
956 #endif
957 break;
958 default:
959 break;
961 s->len = len;
964 /* add the current parse token in token string 's' */
965 ST_FUNC void tok_str_add_tok(TokenString *s)
967 CValue cval;
969 /* save line number info */
970 if (file->line_num != s->last_line_num) {
971 s->last_line_num = file->line_num;
972 cval.i = s->last_line_num;
973 tok_str_add2(s, TOK_LINENUM, &cval);
975 tok_str_add2(s, tok, &tokc);
978 /* get a token from an integer array and increment pointer
979 accordingly. we code it as a macro to avoid pointer aliasing. */
980 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
982 const int *p = *pp;
983 int n, *tab;
985 tab = cv->tab;
986 switch(*t = *p++) {
987 case TOK_CINT:
988 case TOK_CUINT:
989 case TOK_CCHAR:
990 case TOK_LCHAR:
991 case TOK_CFLOAT:
992 case TOK_LINENUM:
993 tab[0] = *p++;
994 break;
995 case TOK_STR:
996 case TOK_LSTR:
997 case TOK_PPNUM:
998 cv->cstr = (CString *)p;
999 cv->cstr->data = (char *)p + sizeof(CString);
1000 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1001 break;
1002 case TOK_CDOUBLE:
1003 case TOK_CLLONG:
1004 case TOK_CULLONG:
1005 n = 2;
1006 goto copy;
1007 case TOK_CLDOUBLE:
1008 #if LDOUBLE_SIZE == 16
1009 n = 4;
1010 #elif LDOUBLE_SIZE == 12
1011 n = 3;
1012 #elif LDOUBLE_SIZE == 8
1013 n = 2;
1014 #else
1015 # error add long double size support
1016 #endif
1017 copy:
1018 do
1019 *tab++ = *p++;
1020 while (--n);
1021 break;
1022 default:
1023 break;
1025 *pp = p;
1028 static int macro_is_equal(const int *a, const int *b)
1030 char buf[STRING_MAX_SIZE + 1];
1031 CValue cv;
1032 int t;
1033 while (*a && *b) {
1034 TOK_GET(&t, &a, &cv);
1035 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1036 TOK_GET(&t, &b, &cv);
1037 if (strcmp(buf, get_tok_str(t, &cv)))
1038 return 0;
1040 return !(*a || *b);
1043 static void define_print(Sym *s, int is_undef)
1045 int c, t;
1046 CValue cval;
1047 const int *str;
1048 Sym *arg;
1050 if (tcc_state->dflag == 0 || !s || !tcc_state->ppfp)
1051 return;
1053 if (file) {
1054 c = file->line_num - file->line_ref - 1;
1055 if (c > 0) {
1056 while (c--)
1057 fputs("\n", tcc_state->ppfp);
1058 file->line_ref = file->line_num;
1062 if (is_undef) {
1063 fprintf(tcc_state->ppfp, "// #undef %s", get_tok_str(s->v, NULL));
1064 return;
1067 fprintf(tcc_state->ppfp, "// #define %s", get_tok_str(s->v, NULL));
1068 arg = s->next;
1069 if (arg) {
1070 char *sep = "(";
1071 while (arg) {
1072 fprintf(tcc_state->ppfp, "%s%s", sep, get_tok_str(arg->v & ~SYM_FIELD, NULL));
1073 sep = ",";
1074 arg = arg->next;
1076 fprintf(tcc_state->ppfp, ")");
1079 str = s->d;
1080 if (str)
1081 fprintf(tcc_state->ppfp, " ");
1083 while (str) {
1084 TOK_GET(&t, &str, &cval);
1085 if (!t)
1086 break;
1087 fprintf(tcc_state->ppfp, "%s", get_tok_str(t, &cval));
1091 /* defines handling */
1092 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1094 Sym *s;
1096 s = define_find(v);
1097 if (s && !macro_is_equal(s->d, str))
1098 tcc_warning("%s redefined", get_tok_str(v, NULL));
1100 s = sym_push2(&define_stack, v, macro_type, 0);
1101 s->d = str;
1102 s->next = first_arg;
1103 table_ident[v - TOK_IDENT]->sym_define = s;
1104 define_print(s, 0);
1107 /* undefined a define symbol. Its name is just set to zero */
1108 ST_FUNC void define_undef(Sym *s)
1110 int v;
1111 v = s->v;
1112 if (v >= TOK_IDENT && v < tok_ident) {
1113 define_print(s, 1);
1114 table_ident[v - TOK_IDENT]->sym_define = NULL;
1118 ST_INLN Sym *define_find(int v)
1120 v -= TOK_IDENT;
1121 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1122 return NULL;
1123 return table_ident[v]->sym_define;
1126 /* free define stack until top reaches 'b' */
1127 ST_FUNC void free_defines(Sym *b)
1129 Sym *top, *top1;
1130 int v;
1132 top = define_stack;
1133 while (top != b) {
1134 top1 = top->prev;
1135 /* do not free args or predefined defines */
1136 if (top->d)
1137 tok_str_free(top->d);
1138 v = top->v;
1139 if (v >= TOK_IDENT && v < tok_ident)
1140 table_ident[v - TOK_IDENT]->sym_define = NULL;
1141 sym_free(top);
1142 top = top1;
1144 define_stack = b;
1147 ST_FUNC void print_defines(void)
1149 Sym *top, *s;
1150 int v;
1152 top = define_stack;
1153 while (top) {
1154 v = top->v;
1155 if (v >= TOK_IDENT && v < tok_ident) {
1156 s = table_ident[v - TOK_IDENT]->sym_define;
1157 define_print(s, 0);
1159 top = top->prev;
1163 /* label lookup */
1164 ST_FUNC Sym *label_find(int v)
1166 v -= TOK_IDENT;
1167 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1168 return NULL;
1169 return table_ident[v]->sym_label;
1172 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1174 Sym *s, **ps;
1175 s = sym_push2(ptop, v, 0, 0);
1176 s->r = flags;
1177 ps = &table_ident[v - TOK_IDENT]->sym_label;
1178 if (ptop == &global_label_stack) {
1179 /* modify the top most local identifier, so that
1180 sym_identifier will point to 's' when popped */
1181 while (*ps != NULL)
1182 ps = &(*ps)->prev_tok;
1184 s->prev_tok = *ps;
1185 *ps = s;
1186 return s;
1189 /* pop labels until element last is reached. Look if any labels are
1190 undefined. Define symbols if '&&label' was used. */
1191 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1193 Sym *s, *s1;
1194 for(s = *ptop; s != slast; s = s1) {
1195 s1 = s->prev;
1196 if (s->r == LABEL_DECLARED) {
1197 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1198 } else if (s->r == LABEL_FORWARD) {
1199 tcc_error("label '%s' used but not defined",
1200 get_tok_str(s->v, NULL));
1201 } else {
1202 if (s->c) {
1203 /* define corresponding symbol. A size of
1204 1 is put. */
1205 put_extern_sym(s, cur_text_section, s->jnext, 1);
1208 /* remove label */
1209 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1210 sym_free(s);
1212 *ptop = slast;
1215 /* eval an expression for #if/#elif */
1216 static int expr_preprocess(void)
1218 int c, t;
1219 TokenString str;
1221 tok_str_new(&str);
1222 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1223 next(); /* do macro subst */
1224 if (tok == TOK_DEFINED) {
1225 next_nomacro();
1226 t = tok;
1227 if (t == '(')
1228 next_nomacro();
1229 c = define_find(tok) != 0;
1230 if (t == '(')
1231 next_nomacro();
1232 tok = TOK_CINT;
1233 tokc.i = c;
1234 } else if (tok >= TOK_IDENT) {
1235 /* if undefined macro */
1236 tok = TOK_CINT;
1237 tokc.i = 0;
1239 tok_str_add_tok(&str);
1241 tok_str_add(&str, -1); /* simulate end of file */
1242 tok_str_add(&str, 0);
1243 /* now evaluate C constant expression */
1244 macro_ptr = str.str;
1245 next();
1246 c = expr_const();
1247 macro_ptr = NULL;
1248 tok_str_free(str.str);
1249 return c != 0;
1252 /* parse after #define */
1253 ST_FUNC void parse_define(void)
1255 Sym *s, *first, **ps;
1256 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1257 int saved_parse_flags;
1258 TokenString str;
1260 v = tok;
1261 if (v < TOK_IDENT)
1262 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1263 /* XXX: should check if same macro (ANSI) */
1264 first = NULL;
1265 t = MACRO_OBJ;
1266 /* '(' must be just after macro definition for MACRO_FUNC */
1267 next_nomacro_spc();
1268 if (tok == '(') {
1269 next_nomacro();
1270 ps = &first;
1271 while (tok != ')') {
1272 varg = tok;
1273 next_nomacro();
1274 is_vaargs = 0;
1275 if (varg == TOK_DOTS) {
1276 varg = TOK___VA_ARGS__;
1277 is_vaargs = 1;
1278 } else if (tok == TOK_DOTS && gnu_ext) {
1279 is_vaargs = 1;
1280 next_nomacro();
1282 if (varg < TOK_IDENT)
1283 tcc_error( "\'%s\' may not appear in parameter list", get_tok_str(varg, NULL));
1284 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1285 *ps = s;
1286 ps = &s->next;
1287 if (tok != ',')
1288 continue;
1289 next_nomacro();
1291 next_nomacro_spc();
1292 t = MACRO_FUNC;
1294 tok_str_new(&str);
1295 spc = 2;
1296 /* EOF testing necessary for '-D' handling */
1297 ptok = 0;
1298 macro_list_start = 1;
1299 saved_parse_flags = parse_flags;
1300 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1301 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1302 if (macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1303 tcc_error("'##' invalid at start of macro");
1304 ptok = tok;
1305 /* remove spaces around ## and after '#' */
1306 if (TOK_TWOSHARPS == tok) {
1307 if (1 == spc)
1308 --str.len;
1309 spc = 2;
1310 } else if ('#' == tok) {
1311 spc = 2;
1312 } else if (check_space(tok, &spc)) {
1313 goto skip;
1315 tok_str_add2(&str, tok, &tokc);
1316 macro_list_start = 0;
1317 skip:
1318 next_nomacro_spc();
1320 parse_flags = saved_parse_flags;
1321 if (ptok == TOK_TWOSHARPS)
1322 tcc_error("'##' invalid at end of macro");
1323 if (spc == 1)
1324 --str.len; /* remove trailing space */
1325 tok_str_add(&str, 0);
1326 define_push(v, t, str.str, first);
1329 static inline int hash_cached_include(const char *filename)
1331 const unsigned char *s;
1332 unsigned int h;
1334 h = TOK_HASH_INIT;
1335 s = (unsigned char *) filename;
1336 while (*s) {
1337 h = TOK_HASH_FUNC(h, *s);
1338 s++;
1340 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1341 return h;
1344 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1346 CachedInclude *e;
1347 int i, h;
1348 h = hash_cached_include(filename);
1349 i = s1->cached_includes_hash[h];
1350 for(;;) {
1351 if (i == 0)
1352 break;
1353 e = s1->cached_includes[i - 1];
1354 if (0 == PATHCMP(e->filename, filename))
1355 return e;
1356 i = e->hash_next;
1358 return NULL;
1361 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1363 CachedInclude *e;
1364 int h;
1366 if (search_cached_include(s1, filename))
1367 return;
1368 #ifdef INC_DEBUG
1369 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1370 #endif
1371 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1372 strcpy(e->filename, filename);
1373 e->ifndef_macro = ifndef_macro;
1374 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1375 /* add in hash table */
1376 h = hash_cached_include(filename);
1377 e->hash_next = s1->cached_includes_hash[h];
1378 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1381 static void pragma_parse(TCCState *s1)
1383 next_nomacro();
1384 if (tok == TOK_pack) {
1385 /*
1386 This may be:
1387 #pragma pack(1) // set
1388 #pragma pack() // reset to default
1389 #pragma pack(push,1) // push & set
1390 #pragma pack(pop) // restore previous
1391 */
1392 next();
1393 skip('(');
1394 if (tok == TOK_ASM_pop) {
1395 next();
1396 if (s1->pack_stack_ptr <= s1->pack_stack) {
1397 stk_error:
1398 tcc_error("out of pack stack");
1400 s1->pack_stack_ptr--;
1401 } else {
1402 int val = 0;
1403 if (tok != ')') {
1404 if (tok == TOK_ASM_push) {
1405 next();
1406 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1407 goto stk_error;
1408 s1->pack_stack_ptr++;
1409 skip(',');
1411 if (tok != TOK_CINT)
1412 goto pragma_err;
1413 val = tokc.i;
1414 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1415 goto pragma_err;
1416 next();
1418 *s1->pack_stack_ptr = val;
1420 if (tok != ')')
1421 goto pragma_err;
1422 } else if (tok == TOK_comment) {
1423 if (s1->ms_extensions) {
1424 next();
1425 skip('(');
1426 if (tok == TOK_lib) {
1427 next();
1428 skip(',');
1429 if (tok != TOK_STR) {
1430 tcc_error("invalid library specification");
1431 } else {
1432 int len = strlen((char *)tokc.cstr->data);
1433 char *file = tcc_malloc(len + 4); /* filetype, "-l", and \0 at the end */
1434 file[0] = TCC_FILETYPE_BINARY;
1435 file[1] = '-';
1436 file[2] = 'l';
1437 strcpy(&file[3],(char *)tokc.cstr->data);
1438 dynarray_add((void ***)&s1->files, &s1->nb_files, file);
1440 next();
1441 tok = TOK_LINEFEED;
1442 } else {
1443 tcc_warning("unknown specifier '%s' in #pragma comment", get_tok_str(tok, &tokc));
1445 } else {
1446 tcc_warning("#pragma comment(lib) is ignored");
1448 } else if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1449 int t = tok, v;
1450 Sym *s;
1452 if (next_nomacro(), tok != '(')
1453 goto pragma_err;
1454 if (next_nomacro(), tok != TOK_STR)
1455 goto pragma_err;
1456 v = tok_alloc(tokc.cstr->data, tokc.cstr->size - 1)->tok;
1457 if (next_nomacro(), tok != ')')
1458 goto pragma_err;
1459 if (t == TOK_push_macro) {
1460 while (NULL == (s = define_find(v)))
1461 define_push(v, 0, NULL, NULL);
1462 s->type.ref = s; /* set push boundary */
1463 } else {
1464 for (s = define_stack; s; s = s->prev)
1465 if (s->v == v && s->type.ref == s) {
1466 s->type.ref = NULL;
1467 break;
1470 if (s)
1471 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1472 else
1473 tcc_warning("unbalanced #pragma pop_macro");
1475 /* print info when tcc is called with "-E -dD" switches */
1476 if (s1->dflag && s1->ppfp) {
1477 if (file) {
1478 int c = file->line_num - file->line_ref - 1;
1479 if (c > 0) {
1480 while (c--)
1481 fputs("\n", tcc_state->ppfp);
1482 file->line_ref = file->line_num;
1485 fprintf(s1->ppfp, "// #pragma %s_macro(\"%s\")",
1486 (t == TOK_push_macro) ? "push" : "pop",
1487 get_tok_str(v, NULL));
1489 } else if (tok == TOK_once) {
1490 add_cached_include(s1, file->filename, TOK_once);
1491 } else {
1492 tcc_warning("unknown #pragma %s", get_tok_str(tok, &tokc));
1494 return;
1495 pragma_err:
1496 tcc_error("malformed #pragma directive");
1499 /* is_bof is true if first non space token at beginning of file */
1500 ST_FUNC void preprocess(int is_bof)
1502 TCCState *s1 = tcc_state;
1503 int i, c, n, saved_parse_flags;
1504 char buf[1024], *q;
1505 Sym *s;
1507 saved_parse_flags = parse_flags;
1508 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1509 PARSE_FLAG_LINEFEED;
1510 parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
1511 next_nomacro();
1512 redo:
1513 switch(tok) {
1514 case TOK_DEFINE:
1515 next_nomacro();
1516 parse_define();
1517 break;
1518 case TOK_UNDEF:
1519 next_nomacro();
1520 s = define_find(tok);
1521 /* undefine symbol by putting an invalid name */
1522 if (s)
1523 define_undef(s);
1524 break;
1525 case TOK_INCLUDE:
1526 case TOK_INCLUDE_NEXT:
1527 ch = file->buf_ptr[0];
1528 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1529 skip_spaces();
1530 if (ch == '<') {
1531 c = '>';
1532 goto read_name;
1533 } else if (ch == '\"') {
1534 c = ch;
1535 read_name:
1536 inp();
1537 q = buf;
1538 while (ch != c && ch != '\n' && ch != CH_EOF) {
1539 if ((q - buf) < sizeof(buf) - 1)
1540 *q++ = ch;
1541 if (ch == '\\') {
1542 if (handle_stray_noerror() == 0)
1543 --q;
1544 } else
1545 inp();
1547 *q = '\0';
1548 minp();
1549 #if 0
1550 /* eat all spaces and comments after include */
1551 /* XXX: slightly incorrect */
1552 while (ch1 != '\n' && ch1 != CH_EOF)
1553 inp();
1554 #endif
1555 } else {
1556 /* computed #include : either we have only strings or
1557 we have anything enclosed in '<>' */
1558 next();
1559 buf[0] = '\0';
1560 if (tok == TOK_STR) {
1561 while (tok != TOK_LINEFEED) {
1562 if (tok != TOK_STR) {
1563 include_syntax:
1564 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1566 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1567 next();
1569 c = '\"';
1570 } else {
1571 int len;
1572 while (tok != TOK_LINEFEED) {
1573 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1574 next();
1576 len = strlen(buf);
1577 /* check syntax and remove '<>' */
1578 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1579 goto include_syntax;
1580 memmove(buf, buf + 1, len - 2);
1581 buf[len - 2] = '\0';
1582 c = '>';
1586 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1587 tcc_error("#include recursion too deep");
1588 /* store current file in stack, but increment stack later below */
1589 *s1->include_stack_ptr = file;
1591 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1592 for (i = -2; i < n; ++i) {
1593 char buf1[sizeof file->filename];
1594 CachedInclude *e;
1595 BufferedFile **f;
1596 const char *path;
1598 if (i == -2) {
1599 /* check absolute include path */
1600 if (!IS_ABSPATH(buf))
1601 continue;
1602 buf1[0] = 0;
1603 i = n; /* force end loop */
1605 } else if (i == -1) {
1606 /* search in current dir if "header.h" */
1607 if (c != '\"')
1608 continue;
1609 path = file->filename;
1610 pstrncpy(buf1, path, tcc_basename(path) - path);
1612 } else {
1613 /* search in all the include paths */
1614 if (i < s1->nb_include_paths)
1615 path = s1->include_paths[i];
1616 else
1617 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1618 pstrcpy(buf1, sizeof(buf1), path);
1619 pstrcat(buf1, sizeof(buf1), "/");
1622 pstrcat(buf1, sizeof(buf1), buf);
1624 if (tok == TOK_INCLUDE_NEXT)
1625 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1626 if (0 == PATHCMP((*f)->filename, buf1)) {
1627 #ifdef INC_DEBUG
1628 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1629 #endif
1630 goto include_trynext;
1633 e = search_cached_include(s1, buf1);
1634 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
1635 /* no need to parse the include because the 'ifndef macro'
1636 is defined */
1637 #ifdef INC_DEBUG
1638 printf("%s: skipping cached %s\n", file->filename, buf1);
1639 #endif
1640 goto include_done;
1643 if (tcc_open(s1, buf1) < 0)
1644 include_trynext:
1645 continue;
1647 #ifdef INC_DEBUG
1648 printf("%s: including %s\n", file->prev->filename, file->filename);
1649 #endif
1650 /* update target deps */
1651 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1652 tcc_strdup(buf1));
1653 /* push current file in stack */
1654 ++s1->include_stack_ptr;
1655 /* add include file debug info */
1656 if (s1->do_debug)
1657 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1658 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1659 ch = file->buf_ptr[0];
1660 goto the_end;
1662 tcc_error("include file '%s' not found", buf);
1663 include_done:
1664 break;
1665 case TOK_IFNDEF:
1666 c = 1;
1667 goto do_ifdef;
1668 case TOK_IF:
1669 c = expr_preprocess();
1670 goto do_if;
1671 case TOK_IFDEF:
1672 c = 0;
1673 do_ifdef:
1674 next_nomacro();
1675 if (tok < TOK_IDENT)
1676 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1677 if (is_bof) {
1678 if (c) {
1679 #ifdef INC_DEBUG
1680 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1681 #endif
1682 file->ifndef_macro = tok;
1685 c = (define_find(tok) != 0) ^ c;
1686 do_if:
1687 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1688 tcc_error("memory full (ifdef)");
1689 *s1->ifdef_stack_ptr++ = c;
1690 goto test_skip;
1691 case TOK_ELSE:
1692 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1693 tcc_error("#else without matching #if");
1694 if (s1->ifdef_stack_ptr[-1] & 2)
1695 tcc_error("#else after #else");
1696 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1697 goto test_else;
1698 case TOK_ELIF:
1699 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1700 tcc_error("#elif without matching #if");
1701 c = s1->ifdef_stack_ptr[-1];
1702 if (c > 1)
1703 tcc_error("#elif after #else");
1704 /* last #if/#elif expression was true: we skip */
1705 if (c == 1)
1706 goto skip;
1707 c = expr_preprocess();
1708 s1->ifdef_stack_ptr[-1] = c;
1709 test_else:
1710 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1711 file->ifndef_macro = 0;
1712 test_skip:
1713 if (!(c & 1)) {
1714 skip:
1715 preprocess_skip();
1716 is_bof = 0;
1717 goto redo;
1719 break;
1720 case TOK_ENDIF:
1721 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1722 tcc_error("#endif without matching #if");
1723 s1->ifdef_stack_ptr--;
1724 /* '#ifndef macro' was at the start of file. Now we check if
1725 an '#endif' is exactly at the end of file */
1726 if (file->ifndef_macro &&
1727 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1728 file->ifndef_macro_saved = file->ifndef_macro;
1729 /* need to set to zero to avoid false matches if another
1730 #ifndef at middle of file */
1731 file->ifndef_macro = 0;
1732 while (tok != TOK_LINEFEED)
1733 next_nomacro();
1734 tok_flags |= TOK_FLAG_ENDIF;
1735 goto the_end;
1737 break;
1738 case TOK_LINE:
1739 next();
1740 if (tok != TOK_CINT)
1741 tcc_error("A #line format is wrong");
1742 case TOK_PPNUM:
1743 if (tok != TOK_CINT) {
1744 char *p = tokc.cstr->data;
1745 tokc.i = strtoul(p, (char **)&p, 10);
1747 i = file->line_num;
1748 file->line_num = tokc.i - 1;
1749 next();
1750 if (tok != TOK_LINEFEED) {
1751 if (tok != TOK_STR) {
1752 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0) {
1753 file->line_num = i;
1754 tcc_error("#line format is wrong");
1756 break;
1758 pstrcpy(file->filename, sizeof(file->filename),
1759 (char *)tokc.cstr->data);
1761 if (s1->do_debug)
1762 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1763 break;
1764 case TOK_ERROR:
1765 case TOK_WARNING:
1766 c = tok;
1767 ch = file->buf_ptr[0];
1768 skip_spaces();
1769 q = buf;
1770 while (ch != '\n' && ch != CH_EOF) {
1771 if ((q - buf) < sizeof(buf) - 1)
1772 *q++ = ch;
1773 if (ch == '\\') {
1774 if (handle_stray_noerror() == 0)
1775 --q;
1776 } else
1777 inp();
1779 *q = '\0';
1780 if (c == TOK_ERROR)
1781 tcc_error("#error %s", buf);
1782 else
1783 tcc_warning("#warning %s", buf);
1784 break;
1785 case TOK_PRAGMA:
1786 pragma_parse(s1);
1787 break;
1788 default:
1789 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1790 /* '!' is ignored to allow C scripts. numbers are ignored
1791 to emulate cpp behaviour */
1792 } else {
1793 if (!(parse_flags & PARSE_FLAG_ASM_FILE))
1794 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1795 else {
1796 /* this is a gas line comment in an 'S' file. */
1797 file->buf_ptr = parse_line_comment(file->buf_ptr);
1798 goto the_end;
1801 break;
1803 /* ignore other preprocess commands or #! for C scripts */
1804 while (tok != TOK_LINEFEED)
1805 next_nomacro();
1806 the_end:
1807 parse_flags = saved_parse_flags;
1810 /* evaluate escape codes in a string. */
1811 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1813 int c, n;
1814 const uint8_t *p;
1816 p = buf;
1817 for(;;) {
1818 c = *p;
1819 if (c == '\0')
1820 break;
1821 if (c == '\\') {
1822 p++;
1823 /* escape */
1824 c = *p;
1825 switch(c) {
1826 case '0': case '1': case '2': case '3':
1827 case '4': case '5': case '6': case '7':
1828 /* at most three octal digits */
1829 n = c - '0';
1830 p++;
1831 c = *p;
1832 if (isoct(c)) {
1833 n = n * 8 + c - '0';
1834 p++;
1835 c = *p;
1836 if (isoct(c)) {
1837 n = n * 8 + c - '0';
1838 p++;
1841 c = n;
1842 goto add_char_nonext;
1843 case 'x':
1844 case 'u':
1845 case 'U':
1846 p++;
1847 n = 0;
1848 for(;;) {
1849 c = *p;
1850 if (c >= 'a' && c <= 'f')
1851 c = c - 'a' + 10;
1852 else if (c >= 'A' && c <= 'F')
1853 c = c - 'A' + 10;
1854 else if (isnum(c))
1855 c = c - '0';
1856 else
1857 break;
1858 n = n * 16 + c;
1859 p++;
1861 c = n;
1862 goto add_char_nonext;
1863 case 'a':
1864 c = '\a';
1865 break;
1866 case 'b':
1867 c = '\b';
1868 break;
1869 case 'f':
1870 c = '\f';
1871 break;
1872 case 'n':
1873 c = '\n';
1874 break;
1875 case 'r':
1876 c = '\r';
1877 break;
1878 case 't':
1879 c = '\t';
1880 break;
1881 case 'v':
1882 c = '\v';
1883 break;
1884 case 'e':
1885 if (!gnu_ext)
1886 goto invalid_escape;
1887 c = 27;
1888 break;
1889 case '\'':
1890 case '\"':
1891 case '\\':
1892 case '?':
1893 break;
1894 default:
1895 invalid_escape:
1896 if (c >= '!' && c <= '~')
1897 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1898 else
1899 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1900 break;
1903 p++;
1904 add_char_nonext:
1905 if (!is_long)
1906 cstr_ccat(outstr, c);
1907 else
1908 cstr_wccat(outstr, c);
1910 /* add a trailing '\0' */
1911 if (!is_long)
1912 cstr_ccat(outstr, '\0');
1913 else
1914 cstr_wccat(outstr, '\0');
1917 /* we use 64 bit numbers */
1918 #define BN_SIZE 2
1920 /* bn = (bn << shift) | or_val */
1921 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1923 int i;
1924 unsigned int v;
1925 for(i=0;i<BN_SIZE;i++) {
1926 v = bn[i];
1927 bn[i] = (v << shift) | or_val;
1928 or_val = v >> (32 - shift);
1932 static void bn_zero(unsigned int *bn)
1934 int i;
1935 for(i=0;i<BN_SIZE;i++) {
1936 bn[i] = 0;
1940 /* parse number in null terminated string 'p' and return it in the
1941 current token */
1942 static void parse_number(const char *p)
1944 int b, t, shift, frac_bits, s, exp_val, ch;
1945 char *q;
1946 unsigned int bn[BN_SIZE];
1947 double d;
1949 /* number */
1950 q = token_buf;
1951 ch = *p++;
1952 t = ch;
1953 ch = *p++;
1954 *q++ = t;
1955 b = 10;
1956 if (t == '.') {
1957 goto float_frac_parse;
1958 } else if (t == '0') {
1959 if (ch == 'x' || ch == 'X') {
1960 q--;
1961 ch = *p++;
1962 b = 16;
1963 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1964 q--;
1965 ch = *p++;
1966 b = 2;
1969 /* parse all digits. cannot check octal numbers at this stage
1970 because of floating point constants */
1971 while (1) {
1972 if (ch >= 'a' && ch <= 'f')
1973 t = ch - 'a' + 10;
1974 else if (ch >= 'A' && ch <= 'F')
1975 t = ch - 'A' + 10;
1976 else if (isnum(ch))
1977 t = ch - '0';
1978 else
1979 break;
1980 if (t >= b)
1981 break;
1982 if (q >= token_buf + STRING_MAX_SIZE) {
1983 num_too_long:
1984 tcc_error("number too long");
1986 *q++ = ch;
1987 ch = *p++;
1989 if (ch == '.' ||
1990 ((ch == 'e' || ch == 'E') && b == 10) ||
1991 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1992 if (b != 10) {
1993 /* NOTE: strtox should support that for hexa numbers, but
1994 non ISOC99 libcs do not support it, so we prefer to do
1995 it by hand */
1996 /* hexadecimal or binary floats */
1997 /* XXX: handle overflows */
1998 *q = '\0';
1999 if (b == 16)
2000 shift = 4;
2001 else
2002 shift = 1;
2003 bn_zero(bn);
2004 q = token_buf;
2005 while (1) {
2006 t = *q++;
2007 if (t == '\0') {
2008 break;
2009 } else if (t >= 'a') {
2010 t = t - 'a' + 10;
2011 } else if (t >= 'A') {
2012 t = t - 'A' + 10;
2013 } else {
2014 t = t - '0';
2016 bn_lshift(bn, shift, t);
2018 frac_bits = 0;
2019 if (ch == '.') {
2020 ch = *p++;
2021 while (1) {
2022 t = ch;
2023 if (t >= 'a' && t <= 'f') {
2024 t = t - 'a' + 10;
2025 } else if (t >= 'A' && t <= 'F') {
2026 t = t - 'A' + 10;
2027 } else if (t >= '0' && t <= '9') {
2028 t = t - '0';
2029 } else {
2030 break;
2032 if (t >= b)
2033 tcc_error("invalid digit");
2034 bn_lshift(bn, shift, t);
2035 frac_bits += shift;
2036 ch = *p++;
2039 if (ch != 'p' && ch != 'P')
2040 expect("exponent");
2041 ch = *p++;
2042 s = 1;
2043 exp_val = 0;
2044 if (ch == '+') {
2045 ch = *p++;
2046 } else if (ch == '-') {
2047 s = -1;
2048 ch = *p++;
2050 if (ch < '0' || ch > '9')
2051 expect("exponent digits");
2052 while (ch >= '0' && ch <= '9') {
2053 exp_val = exp_val * 10 + ch - '0';
2054 ch = *p++;
2056 exp_val = exp_val * s;
2058 /* now we can generate the number */
2059 /* XXX: should patch directly float number */
2060 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2061 d = ldexp(d, exp_val - frac_bits);
2062 t = toup(ch);
2063 if (t == 'F') {
2064 ch = *p++;
2065 tok = TOK_CFLOAT;
2066 /* float : should handle overflow */
2067 tokc.f = (float)d;
2068 } else if (t == 'L') {
2069 ch = *p++;
2070 #ifdef TCC_TARGET_PE
2071 tok = TOK_CDOUBLE;
2072 tokc.d = d;
2073 #else
2074 tok = TOK_CLDOUBLE;
2075 /* XXX: not large enough */
2076 tokc.ld = (long double)d;
2077 #endif
2078 } else {
2079 tok = TOK_CDOUBLE;
2080 tokc.d = d;
2082 } else {
2083 /* decimal floats */
2084 if (ch == '.') {
2085 if (q >= token_buf + STRING_MAX_SIZE)
2086 goto num_too_long;
2087 *q++ = ch;
2088 ch = *p++;
2089 float_frac_parse:
2090 while (ch >= '0' && ch <= '9') {
2091 if (q >= token_buf + STRING_MAX_SIZE)
2092 goto num_too_long;
2093 *q++ = ch;
2094 ch = *p++;
2097 if (ch == 'e' || ch == 'E') {
2098 if (q >= token_buf + STRING_MAX_SIZE)
2099 goto num_too_long;
2100 *q++ = ch;
2101 ch = *p++;
2102 if (ch == '-' || ch == '+') {
2103 if (q >= token_buf + STRING_MAX_SIZE)
2104 goto num_too_long;
2105 *q++ = ch;
2106 ch = *p++;
2108 if (ch < '0' || ch > '9')
2109 expect("exponent digits");
2110 while (ch >= '0' && ch <= '9') {
2111 if (q >= token_buf + STRING_MAX_SIZE)
2112 goto num_too_long;
2113 *q++ = ch;
2114 ch = *p++;
2117 *q = '\0';
2118 t = toup(ch);
2119 errno = 0;
2120 if (t == 'F') {
2121 ch = *p++;
2122 tok = TOK_CFLOAT;
2123 tokc.f = strtof(token_buf, NULL);
2124 } else if (t == 'L') {
2125 ch = *p++;
2126 #ifdef TCC_TARGET_PE
2127 tok = TOK_CDOUBLE;
2128 tokc.d = strtod(token_buf, NULL);
2129 #else
2130 tok = TOK_CLDOUBLE;
2131 tokc.ld = strtold(token_buf, NULL);
2132 #endif
2133 } else {
2134 tok = TOK_CDOUBLE;
2135 tokc.d = strtod(token_buf, NULL);
2138 } else {
2139 unsigned long long n, n1;
2140 int lcount, ucount, must_64bit;
2141 const char *p1;
2143 /* integer number */
2144 *q = '\0';
2145 q = token_buf;
2146 if (b == 10 && *q == '0') {
2147 b = 8;
2148 q++;
2150 n = 0;
2151 while(1) {
2152 t = *q++;
2153 /* no need for checks except for base 10 / 8 errors */
2154 if (t == '\0')
2155 break;
2156 else if (t >= 'a')
2157 t = t - 'a' + 10;
2158 else if (t >= 'A')
2159 t = t - 'A' + 10;
2160 else
2161 t = t - '0';
2162 if (t >= b)
2163 tcc_error("invalid digit");
2164 n1 = n;
2165 n = n * b + t;
2166 /* detect overflow */
2167 /* XXX: this test is not reliable */
2168 if (n < n1)
2169 tcc_error("integer constant overflow");
2172 /* Determine the characteristics (unsigned and/or 64bit) the type of
2173 the constant must have according to the constant suffix(es) */
2174 lcount = ucount = must_64bit = 0;
2175 p1 = p;
2176 for(;;) {
2177 t = toup(ch);
2178 if (t == 'L') {
2179 if (lcount >= 2)
2180 tcc_error("three 'l's in integer constant");
2181 if (lcount && *(p - 1) != ch)
2182 tcc_error("incorrect integer suffix: %s", p1);
2183 lcount++;
2184 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2185 if (lcount == 2)
2186 #endif
2187 must_64bit = 1;
2188 ch = *p++;
2189 } else if (t == 'U') {
2190 if (ucount >= 1)
2191 tcc_error("two 'u's in integer constant");
2192 ucount++;
2193 ch = *p++;
2194 } else {
2195 break;
2199 /* Whether 64 bits are needed to hold the constant's value */
2200 if (n & 0xffffffff00000000LL || must_64bit) {
2201 tok = TOK_CLLONG;
2202 n1 = n >> 32;
2203 } else {
2204 tok = TOK_CINT;
2205 n1 = n;
2208 /* Whether type must be unsigned to hold the constant's value */
2209 if (ucount || ((n1 >> 31) && (b != 10))) {
2210 if (tok == TOK_CLLONG)
2211 tok = TOK_CULLONG;
2212 else
2213 tok = TOK_CUINT;
2214 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2215 } else if (n1 >> 31) {
2216 if (tok == TOK_CINT)
2217 tok = TOK_CLLONG;
2218 else
2219 tcc_error("integer constant overflow");
2222 if (tok == TOK_CINT || tok == TOK_CUINT)
2223 tokc.ui = n;
2224 else
2225 tokc.ull = n;
2227 if (ch)
2228 tcc_error("invalid number\n");
2232 #define PARSE2(c1, tok1, c2, tok2) \
2233 case c1: \
2234 PEEKC(c, p); \
2235 if (c == c2) { \
2236 p++; \
2237 tok = tok2; \
2238 } else { \
2239 tok = tok1; \
2240 } \
2241 break;
2243 /* return next token without macro substitution */
2244 static inline void next_nomacro1(void)
2246 int t, c, is_long;
2247 TokenSym *ts;
2248 uint8_t *p, *p1;
2249 unsigned int h;
2251 p = file->buf_ptr;
2252 redo_no_start:
2253 c = *p;
2254 switch(c) {
2255 case ' ':
2256 case '\t':
2257 tok = c;
2258 p++;
2259 goto keep_tok_flags;
2260 case '\f':
2261 case '\v':
2262 case '\r':
2263 p++;
2264 goto redo_no_start;
2265 case '\\':
2266 /* first look if it is in fact an end of buffer */
2267 if (p >= file->buf_end) {
2268 file->buf_ptr = p;
2269 handle_eob();
2270 p = file->buf_ptr;
2271 if (p >= file->buf_end)
2272 goto parse_eof;
2273 else
2274 goto redo_no_start;
2275 } else {
2276 file->buf_ptr = p;
2277 ch = *p;
2278 if (parse_flags & PARSE_FLAG_ACCEPT_STRAYS) {
2279 if (handle_stray_noerror() != 0) {
2280 goto parse_simple;
2282 } else {
2283 handle_stray();
2285 p = file->buf_ptr;
2286 goto redo_no_start;
2288 parse_eof:
2290 TCCState *s1 = tcc_state;
2291 if ((parse_flags & PARSE_FLAG_LINEFEED)
2292 && !(tok_flags & TOK_FLAG_EOF)) {
2293 tok_flags |= TOK_FLAG_EOF;
2294 tok = TOK_LINEFEED;
2295 goto keep_tok_flags;
2296 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2297 tok = TOK_EOF;
2298 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2299 tcc_error("missing #endif");
2300 } else if (s1->include_stack_ptr == s1->include_stack) {
2301 /* no include left : end of file. */
2302 tok = TOK_EOF;
2303 } else {
2304 tok_flags &= ~TOK_FLAG_EOF;
2305 /* pop include file */
2307 /* test if previous '#endif' was after a #ifdef at
2308 start of file */
2309 if (tok_flags & TOK_FLAG_ENDIF) {
2310 #ifdef INC_DEBUG
2311 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2312 #endif
2313 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2314 tok_flags &= ~TOK_FLAG_ENDIF;
2317 /* add end of include file debug info */
2318 if (tcc_state->do_debug) {
2319 put_stabd(N_EINCL, 0, 0);
2321 /* pop include stack */
2322 tcc_close();
2323 s1->include_stack_ptr--;
2324 p = file->buf_ptr;
2325 goto redo_no_start;
2328 break;
2330 case '\n':
2331 file->line_num++;
2332 tok_flags |= TOK_FLAG_BOL;
2333 p++;
2334 maybe_newline:
2335 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2336 goto redo_no_start;
2337 tok = TOK_LINEFEED;
2338 goto keep_tok_flags;
2340 case '#':
2341 /* XXX: simplify */
2342 PEEKC(c, p);
2343 if (is_space(c) && (parse_flags & PARSE_FLAG_ASM_FILE)) {
2344 p = parse_line_comment(p);
2345 goto redo_no_start;
2347 else
2348 if ((tok_flags & TOK_FLAG_BOL) &&
2349 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2350 file->buf_ptr = p;
2351 preprocess(tok_flags & TOK_FLAG_BOF);
2352 p = file->buf_ptr;
2353 goto maybe_newline;
2354 } else {
2355 if (c == '#') {
2356 p++;
2357 tok = TOK_TWOSHARPS;
2358 } else {
2359 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2360 p = parse_line_comment(p - 1);
2361 goto redo_no_start;
2362 } else {
2363 tok = '#';
2367 break;
2369 /* dollar is allowed to start identifiers when not parsing asm */
2370 case '$':
2371 if (!tcc_state->dollars_in_identifiers
2372 || (parse_flags & PARSE_FLAG_ASM_FILE)) goto parse_simple;
2374 case 'a': case 'b': case 'c': case 'd':
2375 case 'e': case 'f': case 'g': case 'h':
2376 case 'i': case 'j': case 'k': case 'l':
2377 case 'm': case 'n': case 'o': case 'p':
2378 case 'q': case 'r': case 's': case 't':
2379 case 'u': case 'v': case 'w': case 'x':
2380 case 'y': case 'z':
2381 case 'A': case 'B': case 'C': case 'D':
2382 case 'E': case 'F': case 'G': case 'H':
2383 case 'I': case 'J': case 'K':
2384 case 'M': case 'N': case 'O': case 'P':
2385 case 'Q': case 'R': case 'S': case 'T':
2386 case 'U': case 'V': case 'W': case 'X':
2387 case 'Y': case 'Z':
2388 case '_':
2389 parse_ident_fast:
2390 p1 = p;
2391 h = TOK_HASH_INIT;
2392 h = TOK_HASH_FUNC(h, c);
2393 p++;
2394 for(;;) {
2395 c = *p;
2396 if (!isidnum_table[c-CH_EOF]
2397 && (tcc_state->dollars_in_identifiers ? (c != '$') : 1))
2398 break;
2399 h = TOK_HASH_FUNC(h, c);
2400 p++;
2402 if (c != '\\') {
2403 TokenSym **pts;
2404 int len;
2406 /* fast case : no stray found, so we have the full token
2407 and we have already hashed it */
2408 len = p - p1;
2409 h &= (TOK_HASH_SIZE - 1);
2410 pts = &hash_ident[h];
2411 for(;;) {
2412 ts = *pts;
2413 if (!ts)
2414 break;
2415 if (ts->len == len && !memcmp(ts->str, p1, len))
2416 goto token_found;
2417 pts = &(ts->hash_next);
2419 ts = tok_alloc_new(pts, (char *) p1, len);
2420 token_found: ;
2421 } else {
2422 /* slower case */
2423 cstr_reset(&tokcstr);
2425 while (p1 < p) {
2426 cstr_ccat(&tokcstr, *p1);
2427 p1++;
2429 p--;
2430 PEEKC(c, p);
2431 parse_ident_slow:
2432 while (isidnum_table[c-CH_EOF]
2433 || (tcc_state->dollars_in_identifiers ? (c == '$') : 0)) {
2434 cstr_ccat(&tokcstr, c);
2435 PEEKC(c, p);
2437 ts = tok_alloc(tokcstr.data, tokcstr.size);
2439 tok = ts->tok;
2440 break;
2441 case 'L':
2442 t = p[1];
2443 if (t != '\\' && t != '\'' && t != '\"') {
2444 /* fast case */
2445 goto parse_ident_fast;
2446 } else {
2447 PEEKC(c, p);
2448 if (c == '\'' || c == '\"') {
2449 is_long = 1;
2450 goto str_const;
2451 } else {
2452 cstr_reset(&tokcstr);
2453 cstr_ccat(&tokcstr, 'L');
2454 goto parse_ident_slow;
2457 break;
2458 case '0': case '1': case '2': case '3':
2459 case '4': case '5': case '6': case '7':
2460 case '8': case '9':
2462 cstr_reset(&tokcstr);
2463 /* after the first digit, accept digits, alpha, '.' or sign if
2464 prefixed by 'eEpP' */
2465 parse_num:
2466 for(;;) {
2467 t = c;
2468 cstr_ccat(&tokcstr, c);
2469 PEEKC(c, p);
2470 if (!(isnum(c) || isid(c) || c == '.' ||
2471 ((c == '+' || c == '-') &&
2472 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2473 break;
2475 /* We add a trailing '\0' to ease parsing */
2476 cstr_ccat(&tokcstr, '\0');
2477 tokc.cstr = &tokcstr;
2478 tok = TOK_PPNUM;
2479 break;
2480 case '.':
2481 /* special dot handling because it can also start a number */
2482 PEEKC(c, p);
2483 if (isnum(c)) {
2484 cstr_reset(&tokcstr);
2485 cstr_ccat(&tokcstr, '.');
2486 goto parse_num;
2487 } else if (c == '.') {
2488 PEEKC(c, p);
2489 if (c != '.') {
2490 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0)
2491 expect("'.'");
2492 tok = '.';
2493 break;
2495 PEEKC(c, p);
2496 tok = TOK_DOTS;
2497 } else {
2498 tok = '.';
2500 break;
2501 case '\'':
2502 case '\"':
2503 is_long = 0;
2504 str_const:
2506 CString str;
2507 int sep;
2509 sep = c;
2511 /* parse the string */
2512 cstr_new(&str);
2513 p = parse_pp_string(p, sep, &str);
2514 cstr_ccat(&str, '\0');
2516 /* eval the escape (should be done as TOK_PPNUM) */
2517 cstr_reset(&tokcstr);
2518 parse_escape_string(&tokcstr, str.data, is_long);
2519 cstr_free(&str);
2521 if (sep == '\'') {
2522 int char_size;
2523 /* XXX: make it portable */
2524 if (!is_long)
2525 char_size = 1;
2526 else
2527 char_size = sizeof(nwchar_t);
2528 if (tokcstr.size <= char_size)
2529 tcc_error("empty character constant");
2530 if (tokcstr.size > 2 * char_size)
2531 tcc_warning("multi-character character constant");
2532 if (!is_long) {
2533 tokc.i = *(int8_t *)tokcstr.data;
2534 tok = TOK_CCHAR;
2535 } else {
2536 tokc.i = *(nwchar_t *)tokcstr.data;
2537 tok = TOK_LCHAR;
2539 } else {
2540 tokc.cstr = &tokcstr;
2541 if (!is_long)
2542 tok = TOK_STR;
2543 else
2544 tok = TOK_LSTR;
2547 break;
2549 case '<':
2550 PEEKC(c, p);
2551 if (c == '=') {
2552 p++;
2553 tok = TOK_LE;
2554 } else if (c == '<') {
2555 PEEKC(c, p);
2556 if (c == '=') {
2557 p++;
2558 tok = TOK_A_SHL;
2559 } else {
2560 tok = TOK_SHL;
2562 } else {
2563 tok = TOK_LT;
2565 break;
2567 case '>':
2568 PEEKC(c, p);
2569 if (c == '=') {
2570 p++;
2571 tok = TOK_GE;
2572 } else if (c == '>') {
2573 PEEKC(c, p);
2574 if (c == '=') {
2575 p++;
2576 tok = TOK_A_SAR;
2577 } else {
2578 tok = TOK_SAR;
2580 } else {
2581 tok = TOK_GT;
2583 break;
2585 case '&':
2586 PEEKC(c, p);
2587 if (c == '&') {
2588 p++;
2589 tok = TOK_LAND;
2590 } else if (c == '=') {
2591 p++;
2592 tok = TOK_A_AND;
2593 } else {
2594 tok = '&';
2596 break;
2598 case '|':
2599 PEEKC(c, p);
2600 if (c == '|') {
2601 p++;
2602 tok = TOK_LOR;
2603 } else if (c == '=') {
2604 p++;
2605 tok = TOK_A_OR;
2606 } else {
2607 tok = '|';
2609 break;
2611 case '+':
2612 PEEKC(c, p);
2613 if (c == '+') {
2614 p++;
2615 tok = TOK_INC;
2616 } else if (c == '=') {
2617 p++;
2618 tok = TOK_A_ADD;
2619 } else {
2620 tok = '+';
2622 break;
2624 case '-':
2625 PEEKC(c, p);
2626 if (c == '-') {
2627 p++;
2628 tok = TOK_DEC;
2629 } else if (c == '=') {
2630 p++;
2631 tok = TOK_A_SUB;
2632 } else if (c == '>') {
2633 p++;
2634 tok = TOK_ARROW;
2635 } else {
2636 tok = '-';
2638 break;
2640 PARSE2('!', '!', '=', TOK_NE)
2641 PARSE2('=', '=', '=', TOK_EQ)
2642 PARSE2('*', '*', '=', TOK_A_MUL)
2643 PARSE2('%', '%', '=', TOK_A_MOD)
2644 PARSE2('^', '^', '=', TOK_A_XOR)
2646 /* comments or operator */
2647 case '/':
2648 PEEKC(c, p);
2649 if (c == '*') {
2650 p = parse_comment(p);
2651 /* comments replaced by a blank */
2652 tok = ' ';
2653 goto keep_tok_flags;
2654 } else if (c == '/') {
2655 p = parse_line_comment(p);
2656 tok = ' ';
2657 goto keep_tok_flags;
2658 } else if (c == '=') {
2659 p++;
2660 tok = TOK_A_DIV;
2661 } else {
2662 tok = '/';
2664 break;
2666 /* simple tokens */
2667 case '(':
2668 case ')':
2669 case '[':
2670 case ']':
2671 case '{':
2672 case '}':
2673 case ',':
2674 case ';':
2675 case ':':
2676 case '?':
2677 case '~':
2678 case '@': /* only used in assembler */
2679 parse_simple:
2680 tok = c;
2681 p++;
2682 break;
2683 default:
2684 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0)
2685 tcc_error("unrecognized character \\x%02x", c);
2686 else {
2687 tok = ' ';
2688 p++;
2690 break;
2692 tok_flags = 0;
2693 keep_tok_flags:
2694 file->buf_ptr = p;
2695 #if defined(PARSE_DEBUG)
2696 printf("token = %s\n", get_tok_str(tok, &tokc));
2697 #endif
2700 /* return next token without macro substitution. Can read input from
2701 macro_ptr buffer */
2702 static void next_nomacro_spc(void)
2704 if (macro_ptr) {
2705 redo:
2706 tok = *macro_ptr;
2707 if (tok) {
2708 TOK_GET(&tok, &macro_ptr, &tokc);
2709 if (tok == TOK_LINENUM) {
2710 file->line_num = tokc.i;
2711 goto redo;
2714 } else {
2715 next_nomacro1();
2719 ST_FUNC void next_nomacro(void)
2721 do {
2722 next_nomacro_spc();
2723 } while (is_space(tok));
2726 /* substitute arguments in replacement lists in macro_str by the values in
2727 args (field d) and return allocated string */
2728 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2730 int last_tok, t, spc;
2731 const int *st;
2732 Sym *s;
2733 CValue cval;
2734 TokenString str;
2735 CString cstr;
2736 CString cstr2;
2738 tok_str_new(&str);
2739 last_tok = 0;
2740 while(1) {
2741 TOK_GET(&t, &macro_str, &cval);
2742 if (!t)
2743 break;
2744 if (t == '#') {
2745 /* stringize */
2746 TOK_GET(&t, &macro_str, &cval);
2747 if (!t)
2748 break;
2749 s = sym_find2(args, t);
2750 if (s) {
2751 cstr_new(&cstr);
2752 st = s->d;
2753 spc = 0;
2754 while (*st) {
2755 TOK_GET(&t, &st, &cval);
2756 if (t != TOK_PLCHLDR && !check_space(t, &spc))
2757 cstr_cat(&cstr, get_tok_str(t, &cval));
2759 cstr.size -= spc;
2760 cstr_ccat(&cstr, '\0');
2761 #ifdef PP_DEBUG
2762 printf("stringize: %s\n", (char *)cstr.data);
2763 #endif
2764 /* add string */
2765 cstr_new(&cstr2);
2766 /* emulate GCC behaviour and parse escapes in the token string */
2767 parse_escape_string(&cstr2, cstr.data, 0);
2768 cstr_free(&cstr);
2769 cval.cstr = &cstr2;
2770 tok_str_add2(&str, TOK_STR, &cval);
2771 cstr_free(cval.cstr);
2772 } else {
2773 tok_str_add2(&str, t, &cval);
2775 } else if (t >= TOK_IDENT) {
2776 s = sym_find2(args, t);
2777 if (s) {
2778 st = s->d;
2779 /* if '##' is present before or after, no arg substitution */
2780 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2781 /* special case for var arg macros : ## eats the
2782 ',' if empty VA_ARGS variable. */
2783 /* XXX: test of the ',' is not 100%
2784 reliable. should fix it to avoid security
2785 problems */
2786 if (gnu_ext && s->type.t &&
2787 last_tok == TOK_TWOSHARPS &&
2788 str.len >= 2 && str.str[str.len - 2] == ',') {
2789 str.len -= 2;
2790 tok_str_add(&str, TOK_GNUCOMMA);
2793 for(;;) {
2794 int t1;
2795 TOK_GET(&t1, &st, &cval);
2796 if (!t1)
2797 break;
2798 tok_str_add2(&str, t1, &cval);
2800 } else {
2801 /* NOTE: the stream cannot be read when macro
2802 substituing an argument */
2803 macro_subst(&str, nested_list, st, NULL);
2805 } else {
2806 tok_str_add(&str, t);
2808 } else {
2809 tok_str_add2(&str, t, &cval);
2811 last_tok = t;
2813 if (str.len == 0)
2814 tok_str_add(&str, TOK_PLCHLDR);
2815 tok_str_add(&str, 0);
2816 return str.str;
2819 static char const ab_month_name[12][4] =
2821 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2822 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2823 };
2825 /* do macro substitution of current token with macro 's' and add
2826 result to (tok_str,tok_len). 'nested_list' is the list of all
2827 macros we got inside to avoid recursing. Return non zero if no
2828 substitution needs to be done */
2829 static int macro_subst_tok(TokenString *tok_str,
2830 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2832 Sym *args, *sa, *sa1;
2833 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2834 const int *p;
2835 TokenString str;
2836 char *cstrval;
2837 CValue cval;
2838 CString cstr;
2839 char buf[32];
2841 /* if symbol is a macro, prepare substitution */
2842 /* special macros */
2843 if (tok == TOK___LINE__) {
2844 snprintf(buf, sizeof(buf), "%d", file->line_num);
2845 cstrval = buf;
2846 t1 = TOK_PPNUM;
2847 goto add_cstr1;
2848 } else if (tok == TOK___FILE__) {
2849 cstrval = file->filename;
2850 goto add_cstr;
2851 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2852 time_t ti;
2853 struct tm *tm;
2855 time(&ti);
2856 tm = localtime(&ti);
2857 if (tok == TOK___DATE__) {
2858 snprintf(buf, sizeof(buf), "%s %2d %d",
2859 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2860 } else {
2861 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2862 tm->tm_hour, tm->tm_min, tm->tm_sec);
2864 cstrval = buf;
2865 add_cstr:
2866 t1 = TOK_STR;
2867 add_cstr1:
2868 cstr_new(&cstr);
2869 cstr_cat(&cstr, cstrval);
2870 cstr_ccat(&cstr, '\0');
2871 cval.cstr = &cstr;
2872 tok_str_add2(tok_str, t1, &cval);
2873 cstr_free(&cstr);
2874 } else {
2875 int mtok = tok;
2876 int saved_parse_flags = parse_flags;
2877 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
2879 mstr = s->d;
2880 mstr_allocated = 0;
2881 if (s->type.t == MACRO_FUNC) {
2882 TokenString ws_str; /* whitespace between macro name and
2883 * argument list */
2884 tok_str_new(&ws_str);
2885 spc = 0;
2886 /* NOTE: we do not use next_nomacro to avoid eating the
2887 next token. XXX: find better solution */
2888 redo:
2889 if (macro_ptr) {
2890 p = macro_ptr;
2891 while (is_space(t = *p) || TOK_LINEFEED == t) {
2892 if (saved_parse_flags & PARSE_FLAG_SPACES)
2893 tok_str_add(&ws_str, t);
2894 ++p;
2896 if (t == 0 && can_read_stream) {
2897 /* end of macro stream: we must look at the token
2898 after in the file */
2899 struct macro_level *ml = *can_read_stream;
2900 macro_ptr = NULL;
2901 if (ml)
2903 macro_ptr = ml->p;
2904 ml->p = NULL;
2905 *can_read_stream = ml -> prev;
2907 /* also, end of scope for nested defined symbol */
2908 (*nested_list)->v = -1;
2909 goto redo;
2911 } else {
2912 ch = tcc_peekc_slow(file);
2913 while (is_space(ch) || ch == '\n' || ch == '/')
2915 if (ch == '/')
2917 int c;
2918 uint8_t *p = file->buf_ptr;
2919 PEEKC(c, p);
2920 if (c == '*') {
2921 p = parse_comment(p);
2922 file->buf_ptr = p - 1;
2923 } else if (c == '/') {
2924 p = parse_line_comment(p);
2925 file->buf_ptr = p - 1;
2926 } else
2927 break;
2928 ch = ' ';
2930 if (saved_parse_flags & PARSE_FLAG_SPACES)
2931 tok_str_add(&ws_str, ch);
2932 cinp();
2934 t = ch;
2936 if (t != '(') {
2937 /* not a macro substitution after all, restore the
2938 * macro token plus all whitespace we've read.
2939 * whitespace is intentionally not merged to preserve
2940 * newlines. */
2941 int i;
2942 tok_str_add(tok_str, mtok);
2943 for(i=0; i<ws_str.len; i++)
2944 tok_str_add(tok_str, ws_str.str[i]);
2945 tok_str_free(ws_str.str);
2946 parse_flags = saved_parse_flags;
2947 return -1;
2948 } else {
2949 tok_str_free(ws_str.str);
2951 /* argument macro */
2952 next_nomacro();
2953 next_nomacro();
2954 args = NULL;
2955 sa = s->next;
2956 /* NOTE: empty args are allowed, except if no args */
2957 for(;;) {
2958 /* handle '()' case */
2959 if (!args && !sa && tok == ')')
2960 break;
2961 if (!sa)
2962 tcc_error("macro '%s' used with too many args",
2963 get_tok_str(s->v, 0));
2964 tok_str_new(&str);
2965 parlevel = spc = 0;
2966 /* NOTE: non zero sa->t indicates VA_ARGS */
2967 while ((parlevel > 0 ||
2968 (tok != ')' &&
2969 (tok != ',' || sa->type.t)))) {
2970 if (tok == TOK_EOF || tok == 0)
2971 break;
2972 if (tok == '(')
2973 parlevel++;
2974 else if (tok == ')')
2975 parlevel--;
2976 if (tok == TOK_LINEFEED)
2977 tok = ' ';
2978 if (!check_space(tok, &spc))
2979 tok_str_add2(&str, tok, &tokc);
2980 next_nomacro_spc();
2982 if (parlevel)
2983 expect(")");
2984 str.len -= spc;
2985 if (str.len == 0)
2986 tok_str_add(&str, TOK_PLCHLDR);
2987 tok_str_add(&str, 0);
2988 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2989 sa1->d = str.str;
2990 sa = sa->next;
2991 if (tok == ')') {
2992 /* special case for gcc var args: add an empty
2993 var arg argument if it is omitted */
2994 if (sa && sa->type.t && gnu_ext)
2995 continue;
2996 else
2997 break;
2999 if (tok != ',')
3000 expect(",");
3001 next_nomacro();
3003 if (sa) {
3004 tcc_error("macro '%s' used with too few args",
3005 get_tok_str(s->v, 0));
3008 /* now subst each arg */
3009 mstr = macro_arg_subst(nested_list, mstr, args);
3010 /* free memory */
3011 sa = args;
3012 while (sa) {
3013 sa1 = sa->prev;
3014 tok_str_free(sa->d);
3015 sym_free(sa);
3016 sa = sa1;
3018 mstr_allocated = 1;
3020 sym_push2(nested_list, s->v, 0, 0);
3021 parse_flags = saved_parse_flags;
3022 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3023 /* pop nested defined symbol */
3024 sa1 = *nested_list;
3025 *nested_list = sa1->prev;
3026 sym_free(sa1);
3027 if (mstr_allocated)
3028 tok_str_free(mstr);
3030 return 0;
3033 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3034 return the resulting string (which must be freed). */
3035 static inline int *macro_twosharps(const int *macro_str)
3037 const int *ptr;
3038 int t;
3039 TokenString macro_str1;
3040 CString cstr;
3041 int n, start_of_nosubsts;
3043 /* we search the first '##' */
3044 for(ptr = macro_str;;) {
3045 CValue cval;
3046 TOK_GET(&t, &ptr, &cval);
3047 if (t == TOK_TWOSHARPS)
3048 break;
3049 /* nothing more to do if end of string */
3050 if (t == 0)
3051 return NULL;
3054 /* we saw '##', so we need more processing to handle it */
3055 start_of_nosubsts = -1;
3056 tok_str_new(&macro_str1);
3057 for(ptr = macro_str;;) {
3058 TOK_GET(&tok, &ptr, &tokc);
3059 if (tok == 0)
3060 break;
3061 if (tok == TOK_TWOSHARPS)
3062 continue;
3063 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
3064 start_of_nosubsts = macro_str1.len;
3065 while (*ptr == TOK_TWOSHARPS) {
3066 /* given 'a##b', remove nosubsts preceding 'a' */
3067 if (start_of_nosubsts >= 0)
3068 macro_str1.len = start_of_nosubsts;
3069 /* given 'a##b', skip '##' */
3070 t = *++ptr;
3071 /* given 'a##b', remove nosubsts preceding 'b' */
3072 while (t == TOK_NOSUBST)
3073 t = *++ptr;
3074 if (t && t != TOK_TWOSHARPS) {
3075 CValue cval;
3076 TOK_GET(&t, &ptr, &cval);
3077 /* We concatenate the two tokens */
3078 cstr_new(&cstr);
3079 if (tok != TOK_PLCHLDR)
3080 cstr_cat(&cstr, get_tok_str(tok, &tokc));
3081 n = cstr.size;
3082 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
3083 cstr_cat(&cstr, get_tok_str(t, &cval));
3084 cstr_ccat(&cstr, '\0');
3086 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3087 memcpy(file->buffer, cstr.data, cstr.size);
3088 for (;;) {
3089 next_nomacro1();
3090 if (0 == *file->buf_ptr)
3091 break;
3092 tok_str_add2(&macro_str1, tok, &tokc);
3093 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
3094 n, cstr.data, (char*)cstr.data + n);
3096 tcc_close();
3097 cstr_free(&cstr);
3100 if (tok != TOK_NOSUBST) {
3101 tok_str_add2(&macro_str1, tok, &tokc);
3102 tok = ' ';
3103 start_of_nosubsts = -1;
3105 tok_str_add2(&macro_str1, tok, &tokc);
3107 tok_str_add(&macro_str1, 0);
3108 return macro_str1.str;
3112 /* do macro substitution of macro_str and add result to
3113 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3114 inside to avoid recursing. */
3115 static void macro_subst(TokenString *tok_str, Sym **nested_list,
3116 const int *macro_str, struct macro_level ** can_read_stream)
3118 Sym *s;
3119 int *macro_str1;
3120 const int *ptr;
3121 int t, spc;
3122 CValue cval;
3123 struct macro_level ml;
3124 int force_blank;
3125 int gnucomma_index = -1;
3127 /* first scan for '##' operator handling */
3128 ptr = macro_str;
3129 macro_str1 = macro_twosharps(ptr);
3131 if (macro_str1)
3132 ptr = macro_str1;
3133 spc = 0;
3134 force_blank = 0;
3136 while (1) {
3137 /* NOTE: ptr == NULL can only happen if tokens are read from
3138 file stream due to a macro function call */
3139 if (ptr == NULL)
3140 break;
3141 TOK_GET(&t, &ptr, &cval);
3142 if (t == 0)
3143 break;
3144 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS)) {
3145 tcc_error("stray '\\' in program");
3147 if (t == TOK_NOSUBST) {
3148 /* following token has already been subst'd. just copy it on */
3149 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3150 TOK_GET(&t, &ptr, &cval);
3151 goto no_subst;
3153 if (t == TOK_GNUCOMMA) {
3154 if (gnucomma_index != -1)
3155 tcc_error("two GNU commas in the same macro");
3156 gnucomma_index = tok_str->len;
3157 tok_str_add(tok_str, ',');
3158 TOK_GET(&t, &ptr, &cval);
3160 s = define_find(t);
3161 if (s != NULL) {
3162 int old_len = tok_str->len;
3163 /* if nested substitution, do nothing */
3164 if (sym_find2(*nested_list, t)) {
3165 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3166 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3167 goto no_subst;
3169 ml.p = macro_ptr;
3170 if (can_read_stream)
3171 ml.prev = *can_read_stream, *can_read_stream = &ml;
3172 macro_ptr = (int *)ptr;
3173 tok = t;
3174 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3175 spc = tok_str->len && is_space(tok_str->str[tok_str->len-1]);
3176 ptr = (int *)macro_ptr;
3177 macro_ptr = ml.p;
3178 if (can_read_stream && *can_read_stream == &ml)
3179 *can_read_stream = ml.prev;
3180 if (parse_flags & PARSE_FLAG_SPACES)
3181 force_blank = 1;
3182 if (old_len == tok_str->len)
3183 tok_str_add(tok_str, TOK_PLCHLDR);
3184 } else {
3185 no_subst:
3186 if (force_blank) {
3187 tok_str_add(tok_str, ' ');
3188 spc = 1;
3189 force_blank = 0;
3191 if (!check_space(t, &spc))
3192 tok_str_add2(tok_str, t, &cval);
3194 if (gnucomma_index != -1) {
3195 if (tok_str->len >= gnucomma_index+2) {
3196 if (tok_str->str[gnucomma_index+1] == TOK_PLCHLDR)
3197 tok_str->len -= 2;
3198 gnucomma_index = -1;
3201 if (tok_str->len && tok_str->str[tok_str->len-1] == TOK_PLCHLDR)
3202 tok_str->len--;
3204 if (macro_str1)
3205 tok_str_free(macro_str1);
3208 /* return next token with macro substitution */
3209 ST_FUNC void next(void)
3211 Sym *nested_list, *s;
3212 TokenString str;
3213 struct macro_level *ml;
3215 redo:
3216 if (parse_flags & PARSE_FLAG_SPACES)
3217 next_nomacro_spc();
3218 else
3219 next_nomacro();
3220 if (!macro_ptr) {
3221 /* if not reading from macro substituted string, then try
3222 to substitute macros */
3223 if (tok >= TOK_IDENT &&
3224 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3225 s = define_find(tok);
3226 if (s) {
3227 /* we have a macro: we try to substitute */
3228 tok_str_new(&str);
3229 nested_list = NULL;
3230 ml = NULL;
3231 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3232 /* substitution done, NOTE: maybe empty */
3233 tok_str_add(&str, 0);
3234 macro_ptr = str.str;
3235 macro_ptr_allocated = str.str;
3236 goto redo;
3237 } else {
3238 tok_str_add(&str, 0);
3239 if (str.len > 1) {
3240 macro_ptr = str.str + 1;
3241 macro_ptr_allocated = str.str;
3243 tok = str.str[0];
3247 } else {
3248 if (tok == 0) {
3249 /* end of macro or end of unget buffer */
3250 if (unget_buffer_enabled) {
3251 macro_ptr = unget_saved_macro_ptr;
3252 unget_buffer_enabled = 0;
3253 } else {
3254 /* end of macro string: free it */
3255 tok_str_free(macro_ptr_allocated);
3256 macro_ptr_allocated = NULL;
3257 macro_ptr = NULL;
3259 goto redo;
3260 } else if (tok == TOK_NOSUBST) {
3261 /* discard preprocessor's nosubst markers */
3262 goto redo;
3266 /* convert preprocessor tokens into C tokens */
3267 if (tok == TOK_PPNUM &&
3268 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3269 parse_number((char *)tokc.cstr->data);
3273 /* push back current token and set current token to 'last_tok'. Only
3274 identifier case handled for labels. */
3275 ST_INLN void unget_tok(int last_tok)
3277 int i, n;
3278 int *q;
3279 if (unget_buffer_enabled)
3281 /* assert(macro_ptr == unget_saved_buffer + 1);
3282 assert(*macro_ptr == 0); */
3284 else
3286 unget_saved_macro_ptr = macro_ptr;
3287 unget_buffer_enabled = 1;
3289 q = unget_saved_buffer;
3290 macro_ptr = q;
3291 *q++ = tok;
3292 n = tok_ext_size(tok) - 1;
3293 for(i=0;i<n;i++)
3294 *q++ = tokc.tab[i];
3295 *q = 0; /* end of token string */
3296 tok = last_tok;
3300 /* better than nothing, but needs extension to handle '-E' option
3301 correctly too */
3302 ST_FUNC void preprocess_init(TCCState *s1)
3304 s1->include_stack_ptr = s1->include_stack;
3305 /* XXX: move that before to avoid having to initialize
3306 file->ifdef_stack_ptr ? */
3307 s1->ifdef_stack_ptr = s1->ifdef_stack;
3308 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3310 vtop = vstack - 1;
3311 s1->pack_stack[0] = 0;
3312 s1->pack_stack_ptr = s1->pack_stack;
3315 ST_FUNC void preprocess_new(void)
3317 int i, c;
3318 const char *p, *r;
3320 /* init isid table */
3322 for(i=CH_EOF;i<256;i++)
3323 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3325 /* add all tokens */
3326 if (table_ident) {
3327 tcc_free (table_ident);
3328 table_ident = NULL;
3330 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3332 tok_ident = TOK_IDENT;
3333 p = tcc_keywords;
3334 while (*p) {
3335 r = p;
3336 for(;;) {
3337 c = *r++;
3338 if (c == '\0')
3339 break;
3341 tok_alloc(p, r - p - 1);
3342 p = r;
3346 static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3348 switch (s1->Pflag) {
3349 case LINE_MACRO_OUTPUT_FORMAT_STD:
3350 /* "tcc -E -P1" case */
3351 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3352 break;
3354 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3355 /* "tcc -E -P" case: don't output a line directive */
3356 break;
3358 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3359 default:
3360 /* "tcc -E" case: a gcc standard by default */
3361 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3362 break;
3366 /* Preprocess the current file */
3367 ST_FUNC int tcc_preprocess(TCCState *s1)
3369 BufferedFile *file_ref, **iptr, **iptr_new;
3370 int token_seen, d;
3371 const char *s;
3373 preprocess_init(s1);
3374 ch = file->buf_ptr[0];
3375 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3376 parse_flags &= PARSE_FLAG_ASM_FILE;
3377 parse_flags |= PARSE_FLAG_PREPROCESS | PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES | PARSE_FLAG_ACCEPT_STRAYS;
3378 token_seen = 0;
3379 file->line_ref = 0;
3380 file_ref = NULL;
3381 iptr = s1->include_stack_ptr;
3383 for (;;) {
3384 next();
3385 if (tok == TOK_EOF) {
3386 break;
3387 } else if (file != file_ref) {
3388 if (file_ref)
3389 line_macro_output(file_ref, "", s1);
3390 goto print_line;
3391 } else if (tok == TOK_LINEFEED) {
3392 if (!token_seen)
3393 continue;
3394 file->line_ref++;
3395 token_seen = 0;
3396 } else if (!token_seen) {
3397 d = file->line_num - file->line_ref;
3398 if (file != file_ref || d >= 8) {
3399 print_line:
3400 s = "";
3401 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3402 iptr_new = s1->include_stack_ptr;
3403 s = iptr_new > iptr ? " 1"
3404 : iptr_new < iptr ? " 2"
3405 : iptr_new > s1->include_stack ? " 3"
3406 : ""
3409 line_macro_output(file, s, s1);
3410 } else {
3411 while (d > 0)
3412 fputs("\n", s1->ppfp), --d;
3414 file->line_ref = (file_ref = file)->line_num;
3415 token_seen = tok != TOK_LINEFEED;
3416 if (!token_seen)
3417 continue;
3419 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3421 return 0;