pp-many-files: don't drop a preprocessor defines when tcc going to preprocess a next...
[tinycc.git] / tccpp.c
blob4ecc6646a298ae3734fb2d4ce7fe4235efca5278
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
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 ST_DATA int tok_flags;
27 /* additional informations about token */
28 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
29 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
30 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
31 #define TOK_FLAG_EOF 0x0008 /* end of file */
33 ST_DATA int parse_flags;
34 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
35 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
36 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
37 token. line feed is also
38 returned at eof */
39 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
40 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
42 ST_DATA struct BufferedFile *file;
43 ST_DATA int ch, tok;
44 ST_DATA CValue tokc;
45 ST_DATA const int *macro_ptr;
46 ST_DATA CString tokcstr; /* current parsed string, if any */
48 /* display benchmark infos */
49 ST_DATA int total_lines;
50 ST_DATA int total_bytes;
51 ST_DATA int tok_ident;
52 ST_DATA TokenSym **table_ident;
54 /* ------------------------------------------------------------------------- */
56 static int *macro_ptr_allocated;
57 static const int *unget_saved_macro_ptr;
58 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
59 static int unget_buffer_enabled;
60 static TokenSym *hash_ident[TOK_HASH_SIZE];
61 static char token_buf[STRING_MAX_SIZE + 1];
62 /* true if isid(c) || isnum(c) */
63 static unsigned char isidnum_table[256-CH_EOF];
65 static const char tcc_keywords[] =
66 #define DEF(id, str) str "\0"
67 #include "tcctok.h"
68 #undef DEF
71 /* WARNING: the content of this string encodes token numbers */
72 static const unsigned char tok_two_chars[] =
73 /* outdated -- gr
74 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
75 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
76 */{
77 '<','=', TOK_LE,
78 '>','=', TOK_GE,
79 '!','=', TOK_NE,
80 '&','&', TOK_LAND,
81 '|','|', TOK_LOR,
82 '+','+', TOK_INC,
83 '-','-', TOK_DEC,
84 '=','=', TOK_EQ,
85 '<','<', TOK_SHL,
86 '>','>', TOK_SAR,
87 '+','=', TOK_A_ADD,
88 '-','=', TOK_A_SUB,
89 '*','=', TOK_A_MUL,
90 '/','=', TOK_A_DIV,
91 '%','=', TOK_A_MOD,
92 '&','=', TOK_A_AND,
93 '^','=', TOK_A_XOR,
94 '|','=', TOK_A_OR,
95 '-','>', TOK_ARROW,
96 '.','.', 0xa8, // C++ token ?
97 '#','#', TOK_TWOSHARPS,
101 struct macro_level {
102 struct macro_level *prev;
103 const int *p;
106 static void next_nomacro_spc(void);
107 static void macro_subst(
108 TokenString *tok_str,
109 Sym **nested_list,
110 const int *macro_str,
111 struct macro_level **can_read_stream
114 ST_FUNC void skip(int c)
116 if (tok != c)
117 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
118 next();
121 ST_FUNC void expect(const char *msg)
123 tcc_error("%s expected", msg);
126 /* ------------------------------------------------------------------------- */
127 /* CString handling */
128 static void cstr_realloc(CString *cstr, int new_size)
130 int size;
131 void *data;
133 size = cstr->size_allocated;
134 if (size == 0)
135 size = 8; /* no need to allocate a too small first string */
136 while (size < new_size)
137 size = size * 2;
138 data = tcc_realloc(cstr->data_allocated, size);
139 cstr->data_allocated = data;
140 cstr->size_allocated = size;
141 cstr->data = data;
144 /* add a byte */
145 ST_FUNC void cstr_ccat(CString *cstr, int ch)
147 int size;
148 size = cstr->size + 1;
149 if (size > cstr->size_allocated)
150 cstr_realloc(cstr, size);
151 ((unsigned char *)cstr->data)[size - 1] = ch;
152 cstr->size = size;
155 ST_FUNC void cstr_cat(CString *cstr, const char *str)
157 int c;
158 for(;;) {
159 c = *str;
160 if (c == '\0')
161 break;
162 cstr_ccat(cstr, c);
163 str++;
167 /* add a wide char */
168 ST_FUNC void cstr_wccat(CString *cstr, int ch)
170 int size;
171 size = cstr->size + sizeof(nwchar_t);
172 if (size > cstr->size_allocated)
173 cstr_realloc(cstr, size);
174 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
175 cstr->size = size;
178 ST_FUNC void cstr_new(CString *cstr)
180 memset(cstr, 0, sizeof(CString));
183 /* free string and reset it to NULL */
184 ST_FUNC void cstr_free(CString *cstr)
186 tcc_free(cstr->data_allocated);
187 cstr_new(cstr);
190 /* reset string to empty */
191 ST_FUNC void cstr_reset(CString *cstr)
193 cstr->size = 0;
196 /* XXX: unicode ? */
197 static void add_char(CString *cstr, int c)
199 if (c == '\'' || c == '\"' || c == '\\') {
200 /* XXX: could be more precise if char or string */
201 cstr_ccat(cstr, '\\');
203 if (c >= 32 && c <= 126) {
204 cstr_ccat(cstr, c);
205 } else {
206 cstr_ccat(cstr, '\\');
207 if (c == '\n') {
208 cstr_ccat(cstr, 'n');
209 } else {
210 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
211 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
212 cstr_ccat(cstr, '0' + (c & 7));
217 /* ------------------------------------------------------------------------- */
218 /* allocate a new token */
219 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
221 TokenSym *ts, **ptable;
222 int i;
224 if (tok_ident >= SYM_FIRST_ANOM)
225 tcc_error("memory full (symbols)");
227 /* expand token table if needed */
228 i = tok_ident - TOK_IDENT;
229 if ((i % TOK_ALLOC_INCR) == 0) {
230 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
231 table_ident = ptable;
234 ts = tcc_malloc(sizeof(TokenSym) + len);
235 table_ident[i] = ts;
236 ts->tok = tok_ident++;
237 ts->sym_define = NULL;
238 ts->sym_label = NULL;
239 ts->sym_struct = NULL;
240 ts->sym_identifier = NULL;
241 ts->len = len;
242 ts->hash_next = NULL;
243 memcpy(ts->str, str, len);
244 ts->str[len] = '\0';
245 *pts = ts;
246 return ts;
249 #define TOK_HASH_INIT 1
250 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
252 /* find a token and add it if not found */
253 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
255 TokenSym *ts, **pts;
256 int i;
257 unsigned int h;
259 h = TOK_HASH_INIT;
260 for(i=0;i<len;i++)
261 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
262 h &= (TOK_HASH_SIZE - 1);
264 pts = &hash_ident[h];
265 for(;;) {
266 ts = *pts;
267 if (!ts)
268 break;
269 if (ts->len == len && !memcmp(ts->str, str, len))
270 return ts;
271 pts = &(ts->hash_next);
273 return tok_alloc_new(pts, str, len);
276 /* XXX: buffer overflow */
277 /* XXX: float tokens */
278 ST_FUNC char *get_tok_str(int v, CValue *cv)
280 static char buf[STRING_MAX_SIZE + 1];
281 static CString cstr_buf;
282 CString *cstr;
283 char *p;
284 int i, len;
286 /* NOTE: to go faster, we give a fixed buffer for small strings */
287 cstr_reset(&cstr_buf);
288 cstr_buf.data = buf;
289 cstr_buf.size_allocated = sizeof(buf);
290 p = buf;
292 /* just an explanation, should never happen:
293 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
294 tcc_error("internal error: get_tok_str"); */
296 switch(v) {
297 case TOK_CINT:
298 case TOK_CUINT:
299 /* XXX: not quite exact, but only useful for testing */
300 sprintf(p, "%u", cv->ui);
301 break;
302 case TOK_CLLONG:
303 case TOK_CULLONG:
304 /* XXX: not quite exact, but only useful for testing */
305 #ifdef _WIN32
306 sprintf(p, "%u", (unsigned)cv->ull);
307 #else
308 sprintf(p, "%llu", cv->ull);
309 #endif
310 break;
311 case TOK_LCHAR:
312 cstr_ccat(&cstr_buf, 'L');
313 case TOK_CCHAR:
314 cstr_ccat(&cstr_buf, '\'');
315 add_char(&cstr_buf, cv->i);
316 cstr_ccat(&cstr_buf, '\'');
317 cstr_ccat(&cstr_buf, '\0');
318 break;
319 case TOK_PPNUM:
320 cstr = cv->cstr;
321 len = cstr->size - 1;
322 for(i=0;i<len;i++)
323 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
324 cstr_ccat(&cstr_buf, '\0');
325 break;
326 case TOK_LSTR:
327 cstr_ccat(&cstr_buf, 'L');
328 case TOK_STR:
329 cstr = cv->cstr;
330 cstr_ccat(&cstr_buf, '\"');
331 if (v == TOK_STR) {
332 len = cstr->size - 1;
333 for(i=0;i<len;i++)
334 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
335 } else {
336 len = (cstr->size / sizeof(nwchar_t)) - 1;
337 for(i=0;i<len;i++)
338 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
340 cstr_ccat(&cstr_buf, '\"');
341 cstr_ccat(&cstr_buf, '\0');
342 break;
344 case TOK_CFLOAT:
345 case TOK_CDOUBLE:
346 case TOK_CLDOUBLE:
347 case TOK_LINENUM:
348 return NULL; /* should not happen */
350 /* above tokens have value, the ones below don't */
352 case TOK_LT:
353 v = '<';
354 goto addv;
355 case TOK_GT:
356 v = '>';
357 goto addv;
358 case TOK_DOTS:
359 return strcpy(p, "...");
360 case TOK_A_SHL:
361 return strcpy(p, "<<=");
362 case TOK_A_SAR:
363 return strcpy(p, ">>=");
364 default:
365 if (v < TOK_IDENT) {
366 /* search in two bytes table */
367 const unsigned char *q = tok_two_chars;
368 while (*q) {
369 if (q[2] == v) {
370 *p++ = q[0];
371 *p++ = q[1];
372 *p = '\0';
373 return buf;
375 q += 3;
377 addv:
378 *p++ = v;
379 *p = '\0';
380 } else if (v < tok_ident) {
381 return table_ident[v - TOK_IDENT]->str;
382 } else if (v >= SYM_FIRST_ANOM) {
383 /* special name for anonymous symbol */
384 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
385 } else {
386 /* should never happen */
387 return NULL;
389 break;
391 return cstr_buf.data;
394 /* fill input buffer and peek next char */
395 static int tcc_peekc_slow(BufferedFile *bf)
397 int len;
398 /* only tries to read if really end of buffer */
399 if (bf->buf_ptr >= bf->buf_end) {
400 if (bf->fd != -1) {
401 #if defined(PARSE_DEBUG)
402 len = 8;
403 #else
404 len = IO_BUF_SIZE;
405 #endif
406 len = read(bf->fd, bf->buffer, len);
407 if (len < 0)
408 len = 0;
409 } else {
410 len = 0;
412 total_bytes += len;
413 bf->buf_ptr = bf->buffer;
414 bf->buf_end = bf->buffer + len;
415 *bf->buf_end = CH_EOB;
417 if (bf->buf_ptr < bf->buf_end) {
418 return bf->buf_ptr[0];
419 } else {
420 bf->buf_ptr = bf->buf_end;
421 return CH_EOF;
425 /* return the current character, handling end of block if necessary
426 (but not stray) */
427 ST_FUNC int handle_eob(void)
429 return tcc_peekc_slow(file);
432 /* read next char from current input file and handle end of input buffer */
433 ST_INLN void inp(void)
435 ch = *(++(file->buf_ptr));
436 /* end of buffer/file handling */
437 if (ch == CH_EOB)
438 ch = handle_eob();
441 /* handle '\[\r]\n' */
442 static int handle_stray_noerror(void)
444 while (ch == '\\') {
445 inp();
446 if (ch == '\n') {
447 file->line_num++;
448 inp();
449 } else if (ch == '\r') {
450 inp();
451 if (ch != '\n')
452 goto fail;
453 file->line_num++;
454 inp();
455 } else {
456 fail:
457 return 1;
460 return 0;
463 static void handle_stray(void)
465 if (handle_stray_noerror())
466 tcc_error("stray '\\' in program");
469 /* skip the stray and handle the \\n case. Output an error if
470 incorrect char after the stray */
471 static int handle_stray1(uint8_t *p)
473 int c;
475 if (p >= file->buf_end) {
476 file->buf_ptr = p;
477 c = handle_eob();
478 p = file->buf_ptr;
479 if (c == '\\')
480 goto parse_stray;
481 } else {
482 parse_stray:
483 file->buf_ptr = p;
484 ch = *p;
485 handle_stray();
486 p = file->buf_ptr;
487 c = *p;
489 return c;
492 /* handle just the EOB case, but not stray */
493 #define PEEKC_EOB(c, p)\
495 p++;\
496 c = *p;\
497 if (c == '\\') {\
498 file->buf_ptr = p;\
499 c = handle_eob();\
500 p = file->buf_ptr;\
504 /* handle the complicated stray case */
505 #define PEEKC(c, p)\
507 p++;\
508 c = *p;\
509 if (c == '\\') {\
510 c = handle_stray1(p);\
511 p = file->buf_ptr;\
515 /* input with '\[\r]\n' handling. Note that this function cannot
516 handle other characters after '\', so you cannot call it inside
517 strings or comments */
518 ST_FUNC void minp(void)
520 inp();
521 if (ch == '\\')
522 handle_stray();
526 /* single line C++ comments */
527 static uint8_t *parse_line_comment(uint8_t *p)
529 int c;
531 p++;
532 for(;;) {
533 c = *p;
534 redo:
535 if (c == '\n' || c == CH_EOF) {
536 break;
537 } else if (c == '\\') {
538 file->buf_ptr = p;
539 c = handle_eob();
540 p = file->buf_ptr;
541 if (c == '\\') {
542 PEEKC_EOB(c, p);
543 if (c == '\n') {
544 file->line_num++;
545 PEEKC_EOB(c, p);
546 } else if (c == '\r') {
547 PEEKC_EOB(c, p);
548 if (c == '\n') {
549 file->line_num++;
550 PEEKC_EOB(c, p);
553 } else {
554 goto redo;
556 } else {
557 p++;
560 return p;
563 /* C comments */
564 ST_FUNC uint8_t *parse_comment(uint8_t *p)
566 int c;
568 p++;
569 for(;;) {
570 /* fast skip loop */
571 for(;;) {
572 c = *p;
573 if (c == '\n' || c == '*' || c == '\\')
574 break;
575 p++;
576 c = *p;
577 if (c == '\n' || c == '*' || c == '\\')
578 break;
579 p++;
581 /* now we can handle all the cases */
582 if (c == '\n') {
583 file->line_num++;
584 p++;
585 } else if (c == '*') {
586 p++;
587 for(;;) {
588 c = *p;
589 if (c == '*') {
590 p++;
591 } else if (c == '/') {
592 goto end_of_comment;
593 } else if (c == '\\') {
594 file->buf_ptr = p;
595 c = handle_eob();
596 p = file->buf_ptr;
597 if (c == '\\') {
598 /* skip '\[\r]\n', otherwise just skip the stray */
599 while (c == '\\') {
600 PEEKC_EOB(c, p);
601 if (c == '\n') {
602 file->line_num++;
603 PEEKC_EOB(c, p);
604 } else if (c == '\r') {
605 PEEKC_EOB(c, p);
606 if (c == '\n') {
607 file->line_num++;
608 PEEKC_EOB(c, p);
610 } else {
611 goto after_star;
615 } else {
616 break;
619 after_star: ;
620 } else {
621 /* stray, eob or eof */
622 file->buf_ptr = p;
623 c = handle_eob();
624 p = file->buf_ptr;
625 if (c == CH_EOF) {
626 tcc_error("unexpected end of file in comment");
627 } else if (c == '\\') {
628 p++;
632 end_of_comment:
633 p++;
634 return p;
637 #define cinp minp
639 static inline void skip_spaces(void)
641 while (is_space(ch))
642 cinp();
645 static inline int check_space(int t, int *spc)
647 if (is_space(t)) {
648 if (*spc)
649 return 1;
650 *spc = 1;
651 } else
652 *spc = 0;
653 return 0;
656 /* parse a string without interpreting escapes */
657 static uint8_t *parse_pp_string(uint8_t *p,
658 int sep, CString *str)
660 int c;
661 p++;
662 for(;;) {
663 c = *p;
664 if (c == sep) {
665 break;
666 } else if (c == '\\') {
667 file->buf_ptr = p;
668 c = handle_eob();
669 p = file->buf_ptr;
670 if (c == CH_EOF) {
671 unterminated_string:
672 /* XXX: indicate line number of start of string */
673 tcc_error("missing terminating %c character", sep);
674 } else if (c == '\\') {
675 /* escape : just skip \[\r]\n */
676 PEEKC_EOB(c, p);
677 if (c == '\n') {
678 file->line_num++;
679 p++;
680 } else if (c == '\r') {
681 PEEKC_EOB(c, p);
682 if (c != '\n')
683 expect("'\n' after '\r'");
684 file->line_num++;
685 p++;
686 } else if (c == CH_EOF) {
687 goto unterminated_string;
688 } else {
689 if (str) {
690 cstr_ccat(str, '\\');
691 cstr_ccat(str, c);
693 p++;
696 } else if (c == '\n') {
697 file->line_num++;
698 goto add_char;
699 } else if (c == '\r') {
700 PEEKC_EOB(c, p);
701 if (c != '\n') {
702 if (str)
703 cstr_ccat(str, '\r');
704 } else {
705 file->line_num++;
706 goto add_char;
708 } else {
709 add_char:
710 if (str)
711 cstr_ccat(str, c);
712 p++;
715 p++;
716 return p;
719 /* skip block of text until #else, #elif or #endif. skip also pairs of
720 #if/#endif */
721 static void preprocess_skip(void)
723 int a, start_of_line, c, in_warn_or_error;
724 uint8_t *p;
726 p = file->buf_ptr;
727 a = 0;
728 redo_start:
729 start_of_line = 1;
730 in_warn_or_error = 0;
731 for(;;) {
732 redo_no_start:
733 c = *p;
734 switch(c) {
735 case ' ':
736 case '\t':
737 case '\f':
738 case '\v':
739 case '\r':
740 p++;
741 goto redo_no_start;
742 case '\n':
743 file->line_num++;
744 p++;
745 goto redo_start;
746 case '\\':
747 file->buf_ptr = p;
748 c = handle_eob();
749 if (c == CH_EOF) {
750 expect("#endif");
751 } else if (c == '\\') {
752 ch = file->buf_ptr[0];
753 handle_stray_noerror();
755 p = file->buf_ptr;
756 goto redo_no_start;
757 /* skip strings */
758 case '\"':
759 case '\'':
760 if (in_warn_or_error)
761 goto _default;
762 p = parse_pp_string(p, c, NULL);
763 break;
764 /* skip comments */
765 case '/':
766 if (in_warn_or_error)
767 goto _default;
768 file->buf_ptr = p;
769 ch = *p;
770 minp();
771 p = file->buf_ptr;
772 if (ch == '*') {
773 p = parse_comment(p);
774 } else if (ch == '/') {
775 p = parse_line_comment(p);
777 break;
778 case '#':
779 p++;
780 if (start_of_line) {
781 file->buf_ptr = p;
782 next_nomacro();
783 p = file->buf_ptr;
784 if (a == 0 &&
785 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
786 goto the_end;
787 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
788 a++;
789 else if (tok == TOK_ENDIF)
790 a--;
791 else if( tok == TOK_ERROR || tok == TOK_WARNING)
792 in_warn_or_error = 1;
793 else if (tok == TOK_LINEFEED)
794 goto redo_start;
796 break;
797 _default:
798 default:
799 p++;
800 break;
802 start_of_line = 0;
804 the_end: ;
805 file->buf_ptr = p;
808 /* ParseState handling */
810 /* XXX: currently, no include file info is stored. Thus, we cannot display
811 accurate messages if the function or data definition spans multiple
812 files */
814 /* save current parse state in 's' */
815 ST_FUNC void save_parse_state(ParseState *s)
817 s->line_num = file->line_num;
818 s->macro_ptr = macro_ptr;
819 s->tok = tok;
820 s->tokc = tokc;
823 /* restore parse state from 's' */
824 ST_FUNC void restore_parse_state(ParseState *s)
826 file->line_num = s->line_num;
827 macro_ptr = s->macro_ptr;
828 tok = s->tok;
829 tokc = s->tokc;
832 /* return the number of additional 'ints' necessary to store the
833 token */
834 static inline int tok_ext_size(int t)
836 switch(t) {
837 /* 4 bytes */
838 case TOK_CINT:
839 case TOK_CUINT:
840 case TOK_CCHAR:
841 case TOK_LCHAR:
842 case TOK_CFLOAT:
843 case TOK_LINENUM:
844 return 1;
845 case TOK_STR:
846 case TOK_LSTR:
847 case TOK_PPNUM:
848 tcc_error("unsupported token");
849 return 1;
850 case TOK_CDOUBLE:
851 case TOK_CLLONG:
852 case TOK_CULLONG:
853 return 2;
854 case TOK_CLDOUBLE:
855 return LDOUBLE_SIZE / 4;
856 default:
857 return 0;
861 /* token string handling */
863 ST_INLN void tok_str_new(TokenString *s)
865 s->str = NULL;
866 s->len = 0;
867 s->allocated_len = 0;
868 s->last_line_num = -1;
871 ST_FUNC void tok_str_free(int *str)
873 tcc_free(str);
876 static int *tok_str_realloc(TokenString *s)
878 int *str, len;
880 if (s->allocated_len == 0) {
881 len = 8;
882 } else {
883 len = s->allocated_len * 2;
885 str = tcc_realloc(s->str, len * sizeof(int));
886 s->allocated_len = len;
887 s->str = str;
888 return str;
891 ST_FUNC void tok_str_add(TokenString *s, int t)
893 int len, *str;
895 len = s->len;
896 str = s->str;
897 if (len >= s->allocated_len)
898 str = tok_str_realloc(s);
899 str[len++] = t;
900 s->len = len;
903 static void tok_str_add2(TokenString *s, int t, CValue *cv)
905 int len, *str;
907 len = s->len;
908 str = s->str;
910 /* allocate space for worst case */
911 if (len + TOK_MAX_SIZE > s->allocated_len)
912 str = tok_str_realloc(s);
913 str[len++] = t;
914 switch(t) {
915 case TOK_CINT:
916 case TOK_CUINT:
917 case TOK_CCHAR:
918 case TOK_LCHAR:
919 case TOK_CFLOAT:
920 case TOK_LINENUM:
921 str[len++] = cv->tab[0];
922 break;
923 case TOK_PPNUM:
924 case TOK_STR:
925 case TOK_LSTR:
927 int nb_words;
928 CString *cstr;
930 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
931 while ((len + nb_words) > s->allocated_len)
932 str = tok_str_realloc(s);
933 cstr = (CString *)(str + len);
934 cstr->data = NULL;
935 cstr->size = cv->cstr->size;
936 cstr->data_allocated = NULL;
937 cstr->size_allocated = cstr->size;
938 memcpy((char *)cstr + sizeof(CString),
939 cv->cstr->data, cstr->size);
940 len += nb_words;
942 break;
943 case TOK_CDOUBLE:
944 case TOK_CLLONG:
945 case TOK_CULLONG:
946 #if LDOUBLE_SIZE == 8
947 case TOK_CLDOUBLE:
948 #endif
949 str[len++] = cv->tab[0];
950 str[len++] = cv->tab[1];
951 break;
952 #if LDOUBLE_SIZE == 12
953 case TOK_CLDOUBLE:
954 str[len++] = cv->tab[0];
955 str[len++] = cv->tab[1];
956 str[len++] = cv->tab[2];
957 #elif LDOUBLE_SIZE == 16
958 case TOK_CLDOUBLE:
959 str[len++] = cv->tab[0];
960 str[len++] = cv->tab[1];
961 str[len++] = cv->tab[2];
962 str[len++] = cv->tab[3];
963 #elif LDOUBLE_SIZE != 8
964 #error add long double size support
965 #endif
966 break;
967 default:
968 break;
970 s->len = len;
973 /* add the current parse token in token string 's' */
974 ST_FUNC void tok_str_add_tok(TokenString *s)
976 CValue cval;
978 /* save line number info */
979 if (file->line_num != s->last_line_num) {
980 s->last_line_num = file->line_num;
981 cval.i = s->last_line_num;
982 tok_str_add2(s, TOK_LINENUM, &cval);
984 tok_str_add2(s, tok, &tokc);
987 /* get a token from an integer array and increment pointer
988 accordingly. we code it as a macro to avoid pointer aliasing. */
989 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
991 const int *p = *pp;
992 int n, *tab;
994 tab = cv->tab;
995 switch(*t = *p++) {
996 case TOK_CINT:
997 case TOK_CUINT:
998 case TOK_CCHAR:
999 case TOK_LCHAR:
1000 case TOK_CFLOAT:
1001 case TOK_LINENUM:
1002 tab[0] = *p++;
1003 break;
1004 case TOK_STR:
1005 case TOK_LSTR:
1006 case TOK_PPNUM:
1007 cv->cstr = (CString *)p;
1008 cv->cstr->data = (char *)p + sizeof(CString);
1009 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1010 break;
1011 case TOK_CDOUBLE:
1012 case TOK_CLLONG:
1013 case TOK_CULLONG:
1014 n = 2;
1015 goto copy;
1016 case TOK_CLDOUBLE:
1017 #if LDOUBLE_SIZE == 16
1018 n = 4;
1019 #elif LDOUBLE_SIZE == 12
1020 n = 3;
1021 #elif LDOUBLE_SIZE == 8
1022 n = 2;
1023 #else
1024 # error add long double size support
1025 #endif
1026 copy:
1028 *tab++ = *p++;
1029 while (--n);
1030 break;
1031 default:
1032 break;
1034 *pp = p;
1037 static int macro_is_equal(const int *a, const int *b)
1039 char buf[STRING_MAX_SIZE + 1];
1040 CValue cv;
1041 int t;
1042 while (*a && *b) {
1043 TOK_GET(&t, &a, &cv);
1044 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1045 TOK_GET(&t, &b, &cv);
1046 if (strcmp(buf, get_tok_str(t, &cv)))
1047 return 0;
1049 return !(*a || *b);
1052 static void define_print(Sym *s, int is_undef)
1054 int c, t;
1055 CValue cval;
1056 const int *str;
1057 Sym *arg;
1059 if (tcc_state->dflag == 0 || !s || !tcc_state->ppfp)
1060 return;
1062 if (file) {
1063 c = file->line_num - file->line_ref - 1;
1064 if (c > 0) {
1065 while (c--)
1066 fputs("\n", tcc_state->ppfp);
1067 file->line_ref = file->line_num;
1071 if (is_undef) {
1072 fprintf(tcc_state->ppfp, "// #undef %s\n", get_tok_str(s->v, NULL));
1073 return;
1076 fprintf(tcc_state->ppfp, "// #define %s", get_tok_str(s->v, NULL));
1077 arg = s->next;
1078 if (arg) {
1079 char *sep = "(";
1080 while (arg) {
1081 fprintf(tcc_state->ppfp, "%s%s", sep, get_tok_str(arg->v & ~SYM_FIELD, NULL));
1082 sep = ",";
1083 arg = arg->next;
1085 fprintf(tcc_state->ppfp, ")");
1088 str = s->d;
1089 if (str)
1090 fprintf(tcc_state->ppfp, " ");
1092 while (str) {
1093 TOK_GET(&t, &str, &cval);
1094 if (!t)
1095 break;
1096 fprintf(tcc_state->ppfp, "%s", get_tok_str(t, &cval));
1098 fprintf(tcc_state->ppfp, "\n");
1101 /* defines handling */
1102 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1104 Sym *s;
1106 s = define_find(v);
1107 if (s && !macro_is_equal(s->d, str))
1108 tcc_warning("%s redefined", get_tok_str(v, NULL));
1110 s = sym_push2(&define_stack, v, macro_type, 0);
1111 s->d = str;
1112 s->next = first_arg;
1113 table_ident[v - TOK_IDENT]->sym_define = s;
1114 define_print(s, 0);
1117 /* undefined a define symbol. Its name is just set to zero */
1118 ST_FUNC void define_undef(Sym *s)
1120 int v;
1121 v = s->v;
1122 if (v >= TOK_IDENT && v < tok_ident) {
1123 define_print(s, 1);
1124 table_ident[v - TOK_IDENT]->sym_define = NULL;
1126 s->v = 0;
1129 ST_INLN Sym *define_find(int v)
1131 v -= TOK_IDENT;
1132 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1133 return NULL;
1134 return table_ident[v]->sym_define;
1137 /* free define stack until top reaches 'b' */
1138 ST_FUNC void free_defines(Sym *b)
1140 Sym *top, *top1;
1141 int v;
1143 top = define_stack;
1144 while (top != b) {
1145 top1 = top->prev;
1146 /* do not free args or predefined defines */
1147 if (top->d)
1148 tok_str_free(top->d);
1149 v = top->v;
1150 if (v >= TOK_IDENT && v < tok_ident)
1151 table_ident[v - TOK_IDENT]->sym_define = NULL;
1152 sym_free(top);
1153 top = top1;
1155 define_stack = b;
1158 void print_defines(void)
1160 Sym *top, *s;
1161 int v;
1163 top = define_stack;
1164 while (top) {
1165 v = top->v;
1166 if (v >= TOK_IDENT && v < tok_ident) {
1167 s = table_ident[v - TOK_IDENT]->sym_define;
1168 define_print(s, 0);
1170 top = top->prev;
1174 /* label lookup */
1175 ST_FUNC Sym *label_find(int v)
1177 v -= TOK_IDENT;
1178 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1179 return NULL;
1180 return table_ident[v]->sym_label;
1183 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1185 Sym *s, **ps;
1186 s = sym_push2(ptop, v, 0, 0);
1187 s->r = flags;
1188 ps = &table_ident[v - TOK_IDENT]->sym_label;
1189 if (ptop == &global_label_stack) {
1190 /* modify the top most local identifier, so that
1191 sym_identifier will point to 's' when popped */
1192 while (*ps != NULL)
1193 ps = &(*ps)->prev_tok;
1195 s->prev_tok = *ps;
1196 *ps = s;
1197 return s;
1200 /* pop labels until element last is reached. Look if any labels are
1201 undefined. Define symbols if '&&label' was used. */
1202 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1204 Sym *s, *s1;
1205 for(s = *ptop; s != slast; s = s1) {
1206 s1 = s->prev;
1207 if (s->r == LABEL_DECLARED) {
1208 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1209 } else if (s->r == LABEL_FORWARD) {
1210 tcc_error("label '%s' used but not defined",
1211 get_tok_str(s->v, NULL));
1212 } else {
1213 if (s->c) {
1214 /* define corresponding symbol. A size of
1215 1 is put. */
1216 put_extern_sym(s, cur_text_section, s->jnext, 1);
1219 /* remove label */
1220 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1221 sym_free(s);
1223 *ptop = slast;
1226 /* eval an expression for #if/#elif */
1227 static int expr_preprocess(void)
1229 int c, t;
1230 TokenString str;
1232 tok_str_new(&str);
1233 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1234 next(); /* do macro subst */
1235 if (tok == TOK_DEFINED) {
1236 next_nomacro();
1237 t = tok;
1238 if (t == '(')
1239 next_nomacro();
1240 c = define_find(tok) != 0;
1241 if (t == '(')
1242 next_nomacro();
1243 tok = TOK_CINT;
1244 tokc.i = c;
1245 } else if (tok >= TOK_IDENT) {
1246 /* if undefined macro */
1247 tok = TOK_CINT;
1248 tokc.i = 0;
1250 tok_str_add_tok(&str);
1252 tok_str_add(&str, -1); /* simulate end of file */
1253 tok_str_add(&str, 0);
1254 /* now evaluate C constant expression */
1255 macro_ptr = str.str;
1256 next();
1257 c = expr_const();
1258 macro_ptr = NULL;
1259 tok_str_free(str.str);
1260 return c != 0;
1263 /* parse after #define */
1264 ST_FUNC void parse_define(void)
1266 Sym *s, *first, **ps;
1267 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1268 TokenString str;
1270 v = tok;
1271 if (v < TOK_IDENT)
1272 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1273 /* XXX: should check if same macro (ANSI) */
1274 first = NULL;
1275 t = MACRO_OBJ;
1276 /* '(' must be just after macro definition for MACRO_FUNC */
1277 next_nomacro_spc();
1278 if (tok == '(') {
1279 next_nomacro();
1280 ps = &first;
1281 while (tok != ')') {
1282 varg = tok;
1283 next_nomacro();
1284 is_vaargs = 0;
1285 if (varg == TOK_DOTS) {
1286 varg = TOK___VA_ARGS__;
1287 is_vaargs = 1;
1288 } else if (tok == TOK_DOTS && gnu_ext) {
1289 is_vaargs = 1;
1290 next_nomacro();
1292 if (varg < TOK_IDENT)
1293 tcc_error( "\'%s\' may not appear in parameter list", get_tok_str(varg, NULL));
1294 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1295 *ps = s;
1296 ps = &s->next;
1297 if (tok != ',')
1298 continue;
1299 next_nomacro();
1301 next_nomacro_spc();
1302 t = MACRO_FUNC;
1304 tok_str_new(&str);
1305 spc = 2;
1306 /* EOF testing necessary for '-D' handling */
1307 ptok = 0;
1308 macro_list_start = 1;
1309 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1310 if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1311 tcc_error("'##' invalid at start of macro");
1312 ptok = tok;
1313 /* remove spaces around ## and after '#' */
1314 if (TOK_TWOSHARPS == tok) {
1315 if (1 == spc)
1316 --str.len;
1317 spc = 2;
1318 } else if ('#' == tok) {
1319 spc = 2;
1320 } else if (check_space(tok, &spc)) {
1321 goto skip;
1323 tok_str_add2(&str, tok, &tokc);
1324 skip:
1325 next_nomacro_spc();
1326 macro_list_start = 0;
1328 if (ptok == TOK_TWOSHARPS)
1329 tcc_error("'##' invalid at end of macro");
1330 if (spc == 1)
1331 --str.len; /* remove trailing space */
1332 tok_str_add(&str, 0);
1333 define_push(v, t, str.str, first);
1336 static inline int hash_cached_include(const char *filename)
1338 const unsigned char *s;
1339 unsigned int h;
1341 h = TOK_HASH_INIT;
1342 s = (unsigned char *) filename;
1343 while (*s) {
1344 h = TOK_HASH_FUNC(h, *s);
1345 s++;
1347 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1348 return h;
1351 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1353 CachedInclude *e;
1354 int i, h;
1355 h = hash_cached_include(filename);
1356 i = s1->cached_includes_hash[h];
1357 for(;;) {
1358 if (i == 0)
1359 break;
1360 e = s1->cached_includes[i - 1];
1361 if (0 == PATHCMP(e->filename, filename))
1362 return e;
1363 i = e->hash_next;
1365 return NULL;
1368 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1370 CachedInclude *e;
1371 int h;
1373 if (search_cached_include(s1, filename))
1374 return;
1375 #ifdef INC_DEBUG
1376 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1377 #endif
1378 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1379 strcpy(e->filename, filename);
1380 e->ifndef_macro = ifndef_macro;
1381 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1382 /* add in hash table */
1383 h = hash_cached_include(filename);
1384 e->hash_next = s1->cached_includes_hash[h];
1385 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1388 static void pragma_parse(TCCState *s1)
1390 int val;
1392 next();
1393 if (tok == TOK_pack) {
1395 This may be:
1396 #pragma pack(1) // set
1397 #pragma pack() // reset to default
1398 #pragma pack(push,1) // push & set
1399 #pragma pack(pop) // restore previous
1401 next();
1402 skip('(');
1403 if (tok == TOK_ASM_pop) {
1404 next();
1405 if (s1->pack_stack_ptr <= s1->pack_stack) {
1406 stk_error:
1407 tcc_error("out of pack stack");
1409 s1->pack_stack_ptr--;
1410 } else {
1411 val = 0;
1412 if (tok != ')') {
1413 if (tok == TOK_ASM_push) {
1414 next();
1415 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1416 goto stk_error;
1417 s1->pack_stack_ptr++;
1418 skip(',');
1420 if (tok != TOK_CINT) {
1421 pack_error:
1422 tcc_error("invalid pack pragma");
1424 val = tokc.i;
1425 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1426 goto pack_error;
1427 next();
1429 *s1->pack_stack_ptr = val;
1430 skip(')');
1435 /* is_bof is true if first non space token at beginning of file */
1436 ST_FUNC void preprocess(int is_bof)
1438 TCCState *s1 = tcc_state;
1439 int i, c, n, saved_parse_flags;
1440 char buf[1024], *q;
1441 Sym *s;
1443 saved_parse_flags = parse_flags;
1444 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1445 PARSE_FLAG_LINEFEED;
1446 next_nomacro();
1447 redo:
1448 switch(tok) {
1449 case TOK_DEFINE:
1450 next_nomacro();
1451 parse_define();
1452 break;
1453 case TOK_UNDEF:
1454 next_nomacro();
1455 s = define_find(tok);
1456 /* undefine symbol by putting an invalid name */
1457 if (s)
1458 define_undef(s);
1459 break;
1460 case TOK_INCLUDE:
1461 case TOK_INCLUDE_NEXT:
1462 ch = file->buf_ptr[0];
1463 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1464 skip_spaces();
1465 if (ch == '<') {
1466 c = '>';
1467 goto read_name;
1468 } else if (ch == '\"') {
1469 c = ch;
1470 read_name:
1471 inp();
1472 q = buf;
1473 while (ch != c && ch != '\n' && ch != CH_EOF) {
1474 if ((q - buf) < sizeof(buf) - 1)
1475 *q++ = ch;
1476 if (ch == '\\') {
1477 if (handle_stray_noerror() == 0)
1478 --q;
1479 } else
1480 inp();
1482 *q = '\0';
1483 minp();
1484 #if 0
1485 /* eat all spaces and comments after include */
1486 /* XXX: slightly incorrect */
1487 while (ch1 != '\n' && ch1 != CH_EOF)
1488 inp();
1489 #endif
1490 } else {
1491 /* computed #include : either we have only strings or
1492 we have anything enclosed in '<>' */
1493 next();
1494 buf[0] = '\0';
1495 if (tok == TOK_STR) {
1496 while (tok != TOK_LINEFEED) {
1497 if (tok != TOK_STR) {
1498 include_syntax:
1499 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1501 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1502 next();
1504 c = '\"';
1505 } else {
1506 int len;
1507 while (tok != TOK_LINEFEED) {
1508 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1509 next();
1511 len = strlen(buf);
1512 /* check syntax and remove '<>' */
1513 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1514 goto include_syntax;
1515 memmove(buf, buf + 1, len - 2);
1516 buf[len - 2] = '\0';
1517 c = '>';
1521 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1522 tcc_error("#include recursion too deep");
1523 /* store current file in stack, but increment stack later below */
1524 *s1->include_stack_ptr = file;
1526 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1527 for (i = -2; i < n; ++i) {
1528 char buf1[sizeof file->filename];
1529 CachedInclude *e;
1530 BufferedFile **f;
1531 const char *path;
1533 if (i == -2) {
1534 /* check absolute include path */
1535 if (!IS_ABSPATH(buf))
1536 continue;
1537 buf1[0] = 0;
1538 i = n; /* force end loop */
1540 } else if (i == -1) {
1541 /* search in current dir if "header.h" */
1542 if (c != '\"')
1543 continue;
1544 path = file->filename;
1545 pstrncpy(buf1, path, tcc_basename(path) - path);
1547 } else {
1548 /* search in all the include paths */
1549 if (i < s1->nb_include_paths)
1550 path = s1->include_paths[i];
1551 else
1552 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1553 pstrcpy(buf1, sizeof(buf1), path);
1554 pstrcat(buf1, sizeof(buf1), "/");
1557 pstrcat(buf1, sizeof(buf1), buf);
1559 if (tok == TOK_INCLUDE_NEXT)
1560 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1561 if (0 == PATHCMP((*f)->filename, buf1)) {
1562 #ifdef INC_DEBUG
1563 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1564 #endif
1565 goto include_trynext;
1568 e = search_cached_include(s1, buf1);
1569 if (e && define_find(e->ifndef_macro)) {
1570 /* no need to parse the include because the 'ifndef macro'
1571 is defined */
1572 #ifdef INC_DEBUG
1573 printf("%s: skipping cached %s\n", file->filename, buf1);
1574 #endif
1575 goto include_done;
1578 if (tcc_open(s1, buf1) < 0)
1579 include_trynext:
1580 continue;
1582 #ifdef INC_DEBUG
1583 printf("%s: including %s\n", file->prev->filename, file->filename);
1584 #endif
1585 /* update target deps */
1586 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1587 tcc_strdup(buf1));
1588 /* push current file in stack */
1589 ++s1->include_stack_ptr;
1590 /* add include file debug info */
1591 if (s1->do_debug)
1592 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1593 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1594 ch = file->buf_ptr[0];
1595 goto the_end;
1597 tcc_error("include file '%s' not found", buf);
1598 include_done:
1599 break;
1600 case TOK_IFNDEF:
1601 c = 1;
1602 goto do_ifdef;
1603 case TOK_IF:
1604 c = expr_preprocess();
1605 goto do_if;
1606 case TOK_IFDEF:
1607 c = 0;
1608 do_ifdef:
1609 next_nomacro();
1610 if (tok < TOK_IDENT)
1611 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1612 if (is_bof) {
1613 if (c) {
1614 #ifdef INC_DEBUG
1615 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1616 #endif
1617 file->ifndef_macro = tok;
1620 c = (define_find(tok) != 0) ^ c;
1621 do_if:
1622 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1623 tcc_error("memory full (ifdef)");
1624 *s1->ifdef_stack_ptr++ = c;
1625 goto test_skip;
1626 case TOK_ELSE:
1627 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1628 tcc_error("#else without matching #if");
1629 if (s1->ifdef_stack_ptr[-1] & 2)
1630 tcc_error("#else after #else");
1631 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1632 goto test_else;
1633 case TOK_ELIF:
1634 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1635 tcc_error("#elif without matching #if");
1636 c = s1->ifdef_stack_ptr[-1];
1637 if (c > 1)
1638 tcc_error("#elif after #else");
1639 /* last #if/#elif expression was true: we skip */
1640 if (c == 1)
1641 goto skip;
1642 c = expr_preprocess();
1643 s1->ifdef_stack_ptr[-1] = c;
1644 test_else:
1645 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1646 file->ifndef_macro = 0;
1647 test_skip:
1648 if (!(c & 1)) {
1649 skip:
1650 preprocess_skip();
1651 is_bof = 0;
1652 goto redo;
1654 break;
1655 case TOK_ENDIF:
1656 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1657 tcc_error("#endif without matching #if");
1658 s1->ifdef_stack_ptr--;
1659 /* '#ifndef macro' was at the start of file. Now we check if
1660 an '#endif' is exactly at the end of file */
1661 if (file->ifndef_macro &&
1662 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1663 file->ifndef_macro_saved = file->ifndef_macro;
1664 /* need to set to zero to avoid false matches if another
1665 #ifndef at middle of file */
1666 file->ifndef_macro = 0;
1667 while (tok != TOK_LINEFEED)
1668 next_nomacro();
1669 tok_flags |= TOK_FLAG_ENDIF;
1670 goto the_end;
1672 break;
1673 case TOK_LINE:
1674 next();
1675 if (tok != TOK_CINT)
1676 tcc_error("A #line format is wrong");
1677 case TOK_PPNUM:
1678 if (tok != TOK_CINT) {
1679 char *p = tokc.cstr->data;
1680 tokc.i = strtoul(p, (char **)&p, 10);
1682 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1683 next();
1684 if (tok != TOK_LINEFEED) {
1685 if (tok != TOK_STR)
1686 tcc_error("#line");
1687 pstrcpy(file->filename, sizeof(file->filename),
1688 (char *)tokc.cstr->data);
1690 break;
1691 case TOK_ERROR:
1692 case TOK_WARNING:
1693 c = tok;
1694 ch = file->buf_ptr[0];
1695 skip_spaces();
1696 q = buf;
1697 while (ch != '\n' && ch != CH_EOF) {
1698 if ((q - buf) < sizeof(buf) - 1)
1699 *q++ = ch;
1700 if (ch == '\\') {
1701 if (handle_stray_noerror() == 0)
1702 --q;
1703 } else
1704 inp();
1706 *q = '\0';
1707 if (c == TOK_ERROR)
1708 tcc_error("#error %s", buf);
1709 else
1710 tcc_warning("#warning %s", buf);
1711 break;
1712 case TOK_PRAGMA:
1713 pragma_parse(s1);
1714 break;
1715 default:
1716 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1717 /* '!' is ignored to allow C scripts. numbers are ignored
1718 to emulate cpp behaviour */
1719 } else {
1720 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1721 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1722 else {
1723 /* this is a gas line comment in an 'S' file. */
1724 file->buf_ptr = parse_line_comment(file->buf_ptr);
1725 goto the_end;
1728 break;
1730 /* ignore other preprocess commands or #! for C scripts */
1731 while (tok != TOK_LINEFEED)
1732 next_nomacro();
1733 the_end:
1734 parse_flags = saved_parse_flags;
1737 /* evaluate escape codes in a string. */
1738 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1740 int c, n;
1741 const uint8_t *p;
1743 p = buf;
1744 for(;;) {
1745 c = *p;
1746 if (c == '\0')
1747 break;
1748 if (c == '\\') {
1749 p++;
1750 /* escape */
1751 c = *p;
1752 switch(c) {
1753 case '0': case '1': case '2': case '3':
1754 case '4': case '5': case '6': case '7':
1755 /* at most three octal digits */
1756 n = c - '0';
1757 p++;
1758 c = *p;
1759 if (isoct(c)) {
1760 n = n * 8 + c - '0';
1761 p++;
1762 c = *p;
1763 if (isoct(c)) {
1764 n = n * 8 + c - '0';
1765 p++;
1768 c = n;
1769 goto add_char_nonext;
1770 case 'x':
1771 case 'u':
1772 case 'U':
1773 p++;
1774 n = 0;
1775 for(;;) {
1776 c = *p;
1777 if (c >= 'a' && c <= 'f')
1778 c = c - 'a' + 10;
1779 else if (c >= 'A' && c <= 'F')
1780 c = c - 'A' + 10;
1781 else if (isnum(c))
1782 c = c - '0';
1783 else
1784 break;
1785 n = n * 16 + c;
1786 p++;
1788 c = n;
1789 goto add_char_nonext;
1790 case 'a':
1791 c = '\a';
1792 break;
1793 case 'b':
1794 c = '\b';
1795 break;
1796 case 'f':
1797 c = '\f';
1798 break;
1799 case 'n':
1800 c = '\n';
1801 break;
1802 case 'r':
1803 c = '\r';
1804 break;
1805 case 't':
1806 c = '\t';
1807 break;
1808 case 'v':
1809 c = '\v';
1810 break;
1811 case 'e':
1812 if (!gnu_ext)
1813 goto invalid_escape;
1814 c = 27;
1815 break;
1816 case '\'':
1817 case '\"':
1818 case '\\':
1819 case '?':
1820 break;
1821 default:
1822 invalid_escape:
1823 if (c >= '!' && c <= '~')
1824 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1825 else
1826 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1827 break;
1830 p++;
1831 add_char_nonext:
1832 if (!is_long)
1833 cstr_ccat(outstr, c);
1834 else
1835 cstr_wccat(outstr, c);
1837 /* add a trailing '\0' */
1838 if (!is_long)
1839 cstr_ccat(outstr, '\0');
1840 else
1841 cstr_wccat(outstr, '\0');
1844 /* we use 64 bit numbers */
1845 #define BN_SIZE 2
1847 /* bn = (bn << shift) | or_val */
1848 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1850 int i;
1851 unsigned int v;
1852 for(i=0;i<BN_SIZE;i++) {
1853 v = bn[i];
1854 bn[i] = (v << shift) | or_val;
1855 or_val = v >> (32 - shift);
1859 static void bn_zero(unsigned int *bn)
1861 int i;
1862 for(i=0;i<BN_SIZE;i++) {
1863 bn[i] = 0;
1867 /* parse number in null terminated string 'p' and return it in the
1868 current token */
1869 static void parse_number(const char *p)
1871 int b, t, shift, frac_bits, s, exp_val, ch;
1872 char *q;
1873 unsigned int bn[BN_SIZE];
1874 double d;
1876 /* number */
1877 q = token_buf;
1878 ch = *p++;
1879 t = ch;
1880 ch = *p++;
1881 *q++ = t;
1882 b = 10;
1883 if (t == '.') {
1884 goto float_frac_parse;
1885 } else if (t == '0') {
1886 if (ch == 'x' || ch == 'X') {
1887 q--;
1888 ch = *p++;
1889 b = 16;
1890 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1891 q--;
1892 ch = *p++;
1893 b = 2;
1896 /* parse all digits. cannot check octal numbers at this stage
1897 because of floating point constants */
1898 while (1) {
1899 if (ch >= 'a' && ch <= 'f')
1900 t = ch - 'a' + 10;
1901 else if (ch >= 'A' && ch <= 'F')
1902 t = ch - 'A' + 10;
1903 else if (isnum(ch))
1904 t = ch - '0';
1905 else
1906 break;
1907 if (t >= b)
1908 break;
1909 if (q >= token_buf + STRING_MAX_SIZE) {
1910 num_too_long:
1911 tcc_error("number too long");
1913 *q++ = ch;
1914 ch = *p++;
1916 if (ch == '.' ||
1917 ((ch == 'e' || ch == 'E') && b == 10) ||
1918 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1919 if (b != 10) {
1920 /* NOTE: strtox should support that for hexa numbers, but
1921 non ISOC99 libcs do not support it, so we prefer to do
1922 it by hand */
1923 /* hexadecimal or binary floats */
1924 /* XXX: handle overflows */
1925 *q = '\0';
1926 if (b == 16)
1927 shift = 4;
1928 else
1929 shift = 1;
1930 bn_zero(bn);
1931 q = token_buf;
1932 while (1) {
1933 t = *q++;
1934 if (t == '\0') {
1935 break;
1936 } else if (t >= 'a') {
1937 t = t - 'a' + 10;
1938 } else if (t >= 'A') {
1939 t = t - 'A' + 10;
1940 } else {
1941 t = t - '0';
1943 bn_lshift(bn, shift, t);
1945 frac_bits = 0;
1946 if (ch == '.') {
1947 ch = *p++;
1948 while (1) {
1949 t = ch;
1950 if (t >= 'a' && t <= 'f') {
1951 t = t - 'a' + 10;
1952 } else if (t >= 'A' && t <= 'F') {
1953 t = t - 'A' + 10;
1954 } else if (t >= '0' && t <= '9') {
1955 t = t - '0';
1956 } else {
1957 break;
1959 if (t >= b)
1960 tcc_error("invalid digit");
1961 bn_lshift(bn, shift, t);
1962 frac_bits += shift;
1963 ch = *p++;
1966 if (ch != 'p' && ch != 'P')
1967 expect("exponent");
1968 ch = *p++;
1969 s = 1;
1970 exp_val = 0;
1971 if (ch == '+') {
1972 ch = *p++;
1973 } else if (ch == '-') {
1974 s = -1;
1975 ch = *p++;
1977 if (ch < '0' || ch > '9')
1978 expect("exponent digits");
1979 while (ch >= '0' && ch <= '9') {
1980 exp_val = exp_val * 10 + ch - '0';
1981 ch = *p++;
1983 exp_val = exp_val * s;
1985 /* now we can generate the number */
1986 /* XXX: should patch directly float number */
1987 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1988 d = ldexp(d, exp_val - frac_bits);
1989 t = toup(ch);
1990 if (t == 'F') {
1991 ch = *p++;
1992 tok = TOK_CFLOAT;
1993 /* float : should handle overflow */
1994 tokc.f = (float)d;
1995 } else if (t == 'L') {
1996 ch = *p++;
1997 #ifdef TCC_TARGET_PE
1998 tok = TOK_CDOUBLE;
1999 tokc.d = d;
2000 #else
2001 tok = TOK_CLDOUBLE;
2002 /* XXX: not large enough */
2003 tokc.ld = (long double)d;
2004 #endif
2005 } else {
2006 tok = TOK_CDOUBLE;
2007 tokc.d = d;
2009 } else {
2010 /* decimal floats */
2011 if (ch == '.') {
2012 if (q >= token_buf + STRING_MAX_SIZE)
2013 goto num_too_long;
2014 *q++ = ch;
2015 ch = *p++;
2016 float_frac_parse:
2017 while (ch >= '0' && ch <= '9') {
2018 if (q >= token_buf + STRING_MAX_SIZE)
2019 goto num_too_long;
2020 *q++ = ch;
2021 ch = *p++;
2024 if (ch == 'e' || ch == 'E') {
2025 if (q >= token_buf + STRING_MAX_SIZE)
2026 goto num_too_long;
2027 *q++ = ch;
2028 ch = *p++;
2029 if (ch == '-' || ch == '+') {
2030 if (q >= token_buf + STRING_MAX_SIZE)
2031 goto num_too_long;
2032 *q++ = ch;
2033 ch = *p++;
2035 if (ch < '0' || ch > '9')
2036 expect("exponent digits");
2037 while (ch >= '0' && ch <= '9') {
2038 if (q >= token_buf + STRING_MAX_SIZE)
2039 goto num_too_long;
2040 *q++ = ch;
2041 ch = *p++;
2044 *q = '\0';
2045 t = toup(ch);
2046 errno = 0;
2047 if (t == 'F') {
2048 ch = *p++;
2049 tok = TOK_CFLOAT;
2050 tokc.f = strtof(token_buf, NULL);
2051 } else if (t == 'L') {
2052 ch = *p++;
2053 #ifdef TCC_TARGET_PE
2054 tok = TOK_CDOUBLE;
2055 tokc.d = strtod(token_buf, NULL);
2056 #else
2057 tok = TOK_CLDOUBLE;
2058 tokc.ld = strtold(token_buf, NULL);
2059 #endif
2060 } else {
2061 tok = TOK_CDOUBLE;
2062 tokc.d = strtod(token_buf, NULL);
2065 } else {
2066 unsigned long long n, n1;
2067 int lcount, ucount, must_64bit;
2068 const char *p1;
2070 /* integer number */
2071 *q = '\0';
2072 q = token_buf;
2073 if (b == 10 && *q == '0') {
2074 b = 8;
2075 q++;
2077 n = 0;
2078 while(1) {
2079 t = *q++;
2080 /* no need for checks except for base 10 / 8 errors */
2081 if (t == '\0')
2082 break;
2083 else if (t >= 'a')
2084 t = t - 'a' + 10;
2085 else if (t >= 'A')
2086 t = t - 'A' + 10;
2087 else
2088 t = t - '0';
2089 if (t >= b)
2090 tcc_error("invalid digit");
2091 n1 = n;
2092 n = n * b + t;
2093 /* detect overflow */
2094 /* XXX: this test is not reliable */
2095 if (n < n1)
2096 tcc_error("integer constant overflow");
2099 /* Determine the characteristics (unsigned and/or 64bit) the type of
2100 the constant must have according to the constant suffix(es) */
2101 lcount = ucount = must_64bit = 0;
2102 p1 = p;
2103 for(;;) {
2104 t = toup(ch);
2105 if (t == 'L') {
2106 if (lcount >= 2)
2107 tcc_error("three 'l's in integer constant");
2108 if (lcount && *(p - 1) != ch)
2109 tcc_error("incorrect integer suffix: %s", p1);
2110 lcount++;
2111 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2112 if (lcount == 2)
2113 #endif
2114 must_64bit = 1;
2115 ch = *p++;
2116 } else if (t == 'U') {
2117 if (ucount >= 1)
2118 tcc_error("two 'u's in integer constant");
2119 ucount++;
2120 ch = *p++;
2121 } else {
2122 break;
2126 /* Whether 64 bits are needed to hold the constant's value */
2127 if (n & 0xffffffff00000000LL || must_64bit) {
2128 tok = TOK_CLLONG;
2129 n1 = n >> 32;
2130 } else {
2131 tok = TOK_CINT;
2132 n1 = n;
2135 /* Whether type must be unsigned to hold the constant's value */
2136 if (ucount || ((n1 >> 31) && (b != 10))) {
2137 if (tok == TOK_CLLONG)
2138 tok = TOK_CULLONG;
2139 else
2140 tok = TOK_CUINT;
2141 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2142 } else if (n1 >> 31) {
2143 if (tok == TOK_CINT)
2144 tok = TOK_CLLONG;
2145 else
2146 tcc_error("integer constant overflow");
2149 if (tok == TOK_CINT || tok == TOK_CUINT)
2150 tokc.ui = n;
2151 else
2152 tokc.ull = n;
2154 if (ch)
2155 tcc_error("invalid number\n");
2159 #define PARSE2(c1, tok1, c2, tok2) \
2160 case c1: \
2161 PEEKC(c, p); \
2162 if (c == c2) { \
2163 p++; \
2164 tok = tok2; \
2165 } else { \
2166 tok = tok1; \
2168 break;
2170 /* return next token without macro substitution */
2171 static inline void next_nomacro1(void)
2173 int t, c, is_long;
2174 TokenSym *ts;
2175 uint8_t *p, *p1;
2176 unsigned int h;
2178 p = file->buf_ptr;
2179 redo_no_start:
2180 c = *p;
2181 switch(c) {
2182 case ' ':
2183 case '\t':
2184 tok = c;
2185 p++;
2186 goto keep_tok_flags;
2187 case '\f':
2188 case '\v':
2189 case '\r':
2190 p++;
2191 goto redo_no_start;
2192 case '\\':
2193 /* first look if it is in fact an end of buffer */
2194 if (p >= file->buf_end) {
2195 file->buf_ptr = p;
2196 handle_eob();
2197 p = file->buf_ptr;
2198 if (p >= file->buf_end)
2199 goto parse_eof;
2200 else
2201 goto redo_no_start;
2202 } else {
2203 file->buf_ptr = p;
2204 ch = *p;
2205 handle_stray();
2206 p = file->buf_ptr;
2207 goto redo_no_start;
2209 parse_eof:
2211 TCCState *s1 = tcc_state;
2212 if ((parse_flags & PARSE_FLAG_LINEFEED)
2213 && !(tok_flags & TOK_FLAG_EOF)) {
2214 tok_flags |= TOK_FLAG_EOF;
2215 tok = TOK_LINEFEED;
2216 goto keep_tok_flags;
2217 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2218 tok = TOK_EOF;
2219 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2220 tcc_error("missing #endif");
2221 } else if (s1->include_stack_ptr == s1->include_stack) {
2222 /* no include left : end of file. */
2223 tok = TOK_EOF;
2224 } else {
2225 tok_flags &= ~TOK_FLAG_EOF;
2226 /* pop include file */
2228 /* test if previous '#endif' was after a #ifdef at
2229 start of file */
2230 if (tok_flags & TOK_FLAG_ENDIF) {
2231 #ifdef INC_DEBUG
2232 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2233 #endif
2234 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2235 tok_flags &= ~TOK_FLAG_ENDIF;
2238 /* add end of include file debug info */
2239 if (tcc_state->do_debug) {
2240 put_stabd(N_EINCL, 0, 0);
2242 /* pop include stack */
2243 tcc_close();
2244 s1->include_stack_ptr--;
2245 p = file->buf_ptr;
2246 goto redo_no_start;
2249 break;
2251 case '\n':
2252 file->line_num++;
2253 tok_flags |= TOK_FLAG_BOL;
2254 p++;
2255 maybe_newline:
2256 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2257 goto redo_no_start;
2258 tok = TOK_LINEFEED;
2259 goto keep_tok_flags;
2261 case '#':
2262 /* XXX: simplify */
2263 PEEKC(c, p);
2264 if ((tok_flags & TOK_FLAG_BOL) &&
2265 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2266 file->buf_ptr = p;
2267 preprocess(tok_flags & TOK_FLAG_BOF);
2268 p = file->buf_ptr;
2269 goto maybe_newline;
2270 } else {
2271 if (c == '#') {
2272 p++;
2273 tok = TOK_TWOSHARPS;
2274 } else {
2275 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2276 p = parse_line_comment(p - 1);
2277 goto redo_no_start;
2278 } else {
2279 tok = '#';
2283 break;
2285 case 'a': case 'b': case 'c': case 'd':
2286 case 'e': case 'f': case 'g': case 'h':
2287 case 'i': case 'j': case 'k': case 'l':
2288 case 'm': case 'n': case 'o': case 'p':
2289 case 'q': case 'r': case 's': case 't':
2290 case 'u': case 'v': case 'w': case 'x':
2291 case 'y': case 'z':
2292 case 'A': case 'B': case 'C': case 'D':
2293 case 'E': case 'F': case 'G': case 'H':
2294 case 'I': case 'J': case 'K':
2295 case 'M': case 'N': case 'O': case 'P':
2296 case 'Q': case 'R': case 'S': case 'T':
2297 case 'U': case 'V': case 'W': case 'X':
2298 case 'Y': case 'Z':
2299 case '_':
2300 parse_ident_fast:
2301 p1 = p;
2302 h = TOK_HASH_INIT;
2303 h = TOK_HASH_FUNC(h, c);
2304 p++;
2305 for(;;) {
2306 c = *p;
2307 if (!isidnum_table[c-CH_EOF])
2308 break;
2309 h = TOK_HASH_FUNC(h, c);
2310 p++;
2312 if (c != '\\') {
2313 TokenSym **pts;
2314 int len;
2316 /* fast case : no stray found, so we have the full token
2317 and we have already hashed it */
2318 len = p - p1;
2319 h &= (TOK_HASH_SIZE - 1);
2320 pts = &hash_ident[h];
2321 for(;;) {
2322 ts = *pts;
2323 if (!ts)
2324 break;
2325 if (ts->len == len && !memcmp(ts->str, p1, len))
2326 goto token_found;
2327 pts = &(ts->hash_next);
2329 ts = tok_alloc_new(pts, (char *) p1, len);
2330 token_found: ;
2331 } else {
2332 /* slower case */
2333 cstr_reset(&tokcstr);
2335 while (p1 < p) {
2336 cstr_ccat(&tokcstr, *p1);
2337 p1++;
2339 p--;
2340 PEEKC(c, p);
2341 parse_ident_slow:
2342 while (isidnum_table[c-CH_EOF]) {
2343 cstr_ccat(&tokcstr, c);
2344 PEEKC(c, p);
2346 ts = tok_alloc(tokcstr.data, tokcstr.size);
2348 tok = ts->tok;
2349 break;
2350 case 'L':
2351 t = p[1];
2352 if (t != '\\' && t != '\'' && t != '\"') {
2353 /* fast case */
2354 goto parse_ident_fast;
2355 } else {
2356 PEEKC(c, p);
2357 if (c == '\'' || c == '\"') {
2358 is_long = 1;
2359 goto str_const;
2360 } else {
2361 cstr_reset(&tokcstr);
2362 cstr_ccat(&tokcstr, 'L');
2363 goto parse_ident_slow;
2366 break;
2367 case '0': case '1': case '2': case '3':
2368 case '4': case '5': case '6': case '7':
2369 case '8': case '9':
2371 cstr_reset(&tokcstr);
2372 /* after the first digit, accept digits, alpha, '.' or sign if
2373 prefixed by 'eEpP' */
2374 parse_num:
2375 for(;;) {
2376 t = c;
2377 cstr_ccat(&tokcstr, c);
2378 PEEKC(c, p);
2379 if (!(isnum(c) || isid(c) || c == '.' ||
2380 ((c == '+' || c == '-') &&
2381 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2382 break;
2384 /* We add a trailing '\0' to ease parsing */
2385 cstr_ccat(&tokcstr, '\0');
2386 tokc.cstr = &tokcstr;
2387 tok = TOK_PPNUM;
2388 break;
2389 case '.':
2390 /* special dot handling because it can also start a number */
2391 PEEKC(c, p);
2392 if (isnum(c)) {
2393 cstr_reset(&tokcstr);
2394 cstr_ccat(&tokcstr, '.');
2395 goto parse_num;
2396 } else if (c == '.') {
2397 PEEKC(c, p);
2398 if (c != '.')
2399 expect("'.'");
2400 PEEKC(c, p);
2401 tok = TOK_DOTS;
2402 } else {
2403 tok = '.';
2405 break;
2406 case '\'':
2407 case '\"':
2408 is_long = 0;
2409 str_const:
2411 CString str;
2412 int sep;
2414 sep = c;
2416 /* parse the string */
2417 cstr_new(&str);
2418 p = parse_pp_string(p, sep, &str);
2419 cstr_ccat(&str, '\0');
2421 /* eval the escape (should be done as TOK_PPNUM) */
2422 cstr_reset(&tokcstr);
2423 parse_escape_string(&tokcstr, str.data, is_long);
2424 cstr_free(&str);
2426 if (sep == '\'') {
2427 int char_size;
2428 /* XXX: make it portable */
2429 if (!is_long)
2430 char_size = 1;
2431 else
2432 char_size = sizeof(nwchar_t);
2433 if (tokcstr.size <= char_size)
2434 tcc_error("empty character constant");
2435 if (tokcstr.size > 2 * char_size)
2436 tcc_warning("multi-character character constant");
2437 if (!is_long) {
2438 tokc.i = *(int8_t *)tokcstr.data;
2439 tok = TOK_CCHAR;
2440 } else {
2441 tokc.i = *(nwchar_t *)tokcstr.data;
2442 tok = TOK_LCHAR;
2444 } else {
2445 tokc.cstr = &tokcstr;
2446 if (!is_long)
2447 tok = TOK_STR;
2448 else
2449 tok = TOK_LSTR;
2452 break;
2454 case '<':
2455 PEEKC(c, p);
2456 if (c == '=') {
2457 p++;
2458 tok = TOK_LE;
2459 } else if (c == '<') {
2460 PEEKC(c, p);
2461 if (c == '=') {
2462 p++;
2463 tok = TOK_A_SHL;
2464 } else {
2465 tok = TOK_SHL;
2467 } else {
2468 tok = TOK_LT;
2470 break;
2472 case '>':
2473 PEEKC(c, p);
2474 if (c == '=') {
2475 p++;
2476 tok = TOK_GE;
2477 } else if (c == '>') {
2478 PEEKC(c, p);
2479 if (c == '=') {
2480 p++;
2481 tok = TOK_A_SAR;
2482 } else {
2483 tok = TOK_SAR;
2485 } else {
2486 tok = TOK_GT;
2488 break;
2490 case '&':
2491 PEEKC(c, p);
2492 if (c == '&') {
2493 p++;
2494 tok = TOK_LAND;
2495 } else if (c == '=') {
2496 p++;
2497 tok = TOK_A_AND;
2498 } else {
2499 tok = '&';
2501 break;
2503 case '|':
2504 PEEKC(c, p);
2505 if (c == '|') {
2506 p++;
2507 tok = TOK_LOR;
2508 } else if (c == '=') {
2509 p++;
2510 tok = TOK_A_OR;
2511 } else {
2512 tok = '|';
2514 break;
2516 case '+':
2517 PEEKC(c, p);
2518 if (c == '+') {
2519 p++;
2520 tok = TOK_INC;
2521 } else if (c == '=') {
2522 p++;
2523 tok = TOK_A_ADD;
2524 } else {
2525 tok = '+';
2527 break;
2529 case '-':
2530 PEEKC(c, p);
2531 if (c == '-') {
2532 p++;
2533 tok = TOK_DEC;
2534 } else if (c == '=') {
2535 p++;
2536 tok = TOK_A_SUB;
2537 } else if (c == '>') {
2538 p++;
2539 tok = TOK_ARROW;
2540 } else {
2541 tok = '-';
2543 break;
2545 PARSE2('!', '!', '=', TOK_NE)
2546 PARSE2('=', '=', '=', TOK_EQ)
2547 PARSE2('*', '*', '=', TOK_A_MUL)
2548 PARSE2('%', '%', '=', TOK_A_MOD)
2549 PARSE2('^', '^', '=', TOK_A_XOR)
2551 /* comments or operator */
2552 case '/':
2553 PEEKC(c, p);
2554 if (c == '*') {
2555 p = parse_comment(p);
2556 /* comments replaced by a blank */
2557 tok = ' ';
2558 goto keep_tok_flags;
2559 } else if (c == '/') {
2560 p = parse_line_comment(p);
2561 tok = ' ';
2562 goto keep_tok_flags;
2563 } else if (c == '=') {
2564 p++;
2565 tok = TOK_A_DIV;
2566 } else {
2567 tok = '/';
2569 break;
2571 /* simple tokens */
2572 case '(':
2573 case ')':
2574 case '[':
2575 case ']':
2576 case '{':
2577 case '}':
2578 case ',':
2579 case ';':
2580 case ':':
2581 case '?':
2582 case '~':
2583 case '$': /* only used in assembler */
2584 case '@': /* dito */
2585 tok = c;
2586 p++;
2587 break;
2588 default:
2589 tcc_error("unrecognized character \\x%02x", c);
2590 break;
2592 tok_flags = 0;
2593 keep_tok_flags:
2594 file->buf_ptr = p;
2595 #if defined(PARSE_DEBUG)
2596 printf("token = %s\n", get_tok_str(tok, &tokc));
2597 #endif
2600 /* return next token without macro substitution. Can read input from
2601 macro_ptr buffer */
2602 static void next_nomacro_spc(void)
2604 if (macro_ptr) {
2605 redo:
2606 tok = *macro_ptr;
2607 if (tok) {
2608 TOK_GET(&tok, &macro_ptr, &tokc);
2609 if (tok == TOK_LINENUM) {
2610 file->line_num = tokc.i;
2611 goto redo;
2614 } else {
2615 next_nomacro1();
2619 ST_FUNC void next_nomacro(void)
2621 do {
2622 next_nomacro_spc();
2623 } while (is_space(tok));
2626 /* substitute arguments in replacement lists in macro_str by the values in
2627 args (field d) and return allocated string */
2628 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2630 int last_tok, t, spc;
2631 const int *st;
2632 Sym *s;
2633 CValue cval;
2634 TokenString str;
2635 CString cstr;
2637 tok_str_new(&str);
2638 last_tok = 0;
2639 while(1) {
2640 TOK_GET(&t, &macro_str, &cval);
2641 if (!t)
2642 break;
2643 if (t == '#') {
2644 /* stringize */
2645 TOK_GET(&t, &macro_str, &cval);
2646 if (!t)
2647 break;
2648 s = sym_find2(args, t);
2649 if (s) {
2650 cstr_new(&cstr);
2651 st = s->d;
2652 spc = 0;
2653 while (*st) {
2654 TOK_GET(&t, &st, &cval);
2655 if (!check_space(t, &spc))
2656 cstr_cat(&cstr, get_tok_str(t, &cval));
2658 cstr.size -= spc;
2659 cstr_ccat(&cstr, '\0');
2660 #ifdef PP_DEBUG
2661 printf("stringize: %s\n", (char *)cstr.data);
2662 #endif
2663 /* add string */
2664 cval.cstr = &cstr;
2665 tok_str_add2(&str, TOK_STR, &cval);
2666 cstr_free(&cstr);
2667 } else {
2668 tok_str_add2(&str, t, &cval);
2670 } else if (t >= TOK_IDENT) {
2671 s = sym_find2(args, t);
2672 if (s) {
2673 st = s->d;
2674 /* if '##' is present before or after, no arg substitution */
2675 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2676 /* special case for var arg macros : ## eats the
2677 ',' if empty VA_ARGS variable. */
2678 /* XXX: test of the ',' is not 100%
2679 reliable. should fix it to avoid security
2680 problems */
2681 if (gnu_ext && s->type.t &&
2682 last_tok == TOK_TWOSHARPS &&
2683 str.len >= 2 && str.str[str.len - 2] == ',') {
2684 if (*st == TOK_PLCHLDR) {
2685 /* suppress ',' '##' */
2686 str.len -= 2;
2687 } else {
2688 /* suppress '##' and add variable */
2689 str.len--;
2690 goto add_var;
2692 } else {
2693 int t1;
2694 add_var:
2695 for(;;) {
2696 TOK_GET(&t1, &st, &cval);
2697 if (!t1)
2698 break;
2699 tok_str_add2(&str, t1, &cval);
2702 } else if (*st != TOK_PLCHLDR) {
2703 /* NOTE: the stream cannot be read when macro
2704 substituing an argument */
2705 macro_subst(&str, nested_list, st, NULL);
2707 } else {
2708 tok_str_add(&str, t);
2710 } else {
2711 tok_str_add2(&str, t, &cval);
2713 last_tok = t;
2715 tok_str_add(&str, 0);
2716 return str.str;
2719 static char const ab_month_name[12][4] =
2721 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2722 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2725 /* do macro substitution of current token with macro 's' and add
2726 result to (tok_str,tok_len). 'nested_list' is the list of all
2727 macros we got inside to avoid recursing. Return non zero if no
2728 substitution needs to be done */
2729 static int macro_subst_tok(TokenString *tok_str,
2730 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2732 Sym *args, *sa, *sa1;
2733 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2734 const int *p;
2735 TokenString str;
2736 char *cstrval;
2737 CValue cval;
2738 CString cstr;
2739 char buf[32];
2741 /* if symbol is a macro, prepare substitution */
2742 /* special macros */
2743 if (tok == TOK___LINE__) {
2744 snprintf(buf, sizeof(buf), "%d", file->line_num);
2745 cstrval = buf;
2746 t1 = TOK_PPNUM;
2747 goto add_cstr1;
2748 } else if (tok == TOK___FILE__) {
2749 cstrval = file->filename;
2750 goto add_cstr;
2751 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2752 time_t ti;
2753 struct tm *tm;
2755 time(&ti);
2756 tm = localtime(&ti);
2757 if (tok == TOK___DATE__) {
2758 snprintf(buf, sizeof(buf), "%s %2d %d",
2759 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2760 } else {
2761 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2762 tm->tm_hour, tm->tm_min, tm->tm_sec);
2764 cstrval = buf;
2765 add_cstr:
2766 t1 = TOK_STR;
2767 add_cstr1:
2768 cstr_new(&cstr);
2769 cstr_cat(&cstr, cstrval);
2770 cstr_ccat(&cstr, '\0');
2771 cval.cstr = &cstr;
2772 tok_str_add2(tok_str, t1, &cval);
2773 cstr_free(&cstr);
2774 } else {
2775 mstr = s->d;
2776 mstr_allocated = 0;
2777 if (s->type.t == MACRO_FUNC) {
2778 /* NOTE: we do not use next_nomacro to avoid eating the
2779 next token. XXX: find better solution */
2780 redo:
2781 if (macro_ptr) {
2782 p = macro_ptr;
2783 while (is_space(t = *p) || TOK_LINEFEED == t)
2784 ++p;
2785 if (t == 0 && can_read_stream) {
2786 /* end of macro stream: we must look at the token
2787 after in the file */
2788 struct macro_level *ml = *can_read_stream;
2789 macro_ptr = NULL;
2790 if (ml)
2792 macro_ptr = ml->p;
2793 ml->p = NULL;
2794 *can_read_stream = ml -> prev;
2796 /* also, end of scope for nested defined symbol */
2797 (*nested_list)->v = -1;
2798 goto redo;
2800 } else {
2801 ch = file->buf_ptr[0];
2802 while (is_space(ch) || ch == '\n' || ch == '/')
2804 if (ch == '/')
2806 int c;
2807 uint8_t *p = file->buf_ptr;
2808 PEEKC(c, p);
2809 if (c == '*') {
2810 p = parse_comment(p);
2811 file->buf_ptr = p - 1;
2812 } else if (c == '/') {
2813 p = parse_line_comment(p);
2814 file->buf_ptr = p - 1;
2815 } else
2816 break;
2818 cinp();
2820 t = ch;
2822 if (t != '(') /* no macro subst */
2823 return -1;
2825 /* argument macro */
2826 next_nomacro();
2827 next_nomacro();
2828 args = NULL;
2829 sa = s->next;
2830 /* NOTE: empty args are allowed, except if no args */
2831 for(;;) {
2832 /* handle '()' case */
2833 if (!args && !sa && tok == ')')
2834 break;
2835 if (!sa)
2836 tcc_error("macro '%s' used with too many args",
2837 get_tok_str(s->v, 0));
2838 tok_str_new(&str);
2839 parlevel = spc = 0;
2840 /* NOTE: non zero sa->t indicates VA_ARGS */
2841 while ((parlevel > 0 ||
2842 (tok != ')' &&
2843 (tok != ',' || sa->type.t))) &&
2844 tok != -1) {
2845 if (tok == '(')
2846 parlevel++;
2847 else if (tok == ')')
2848 parlevel--;
2849 if (tok == TOK_LINEFEED)
2850 tok = ' ';
2851 if (!check_space(tok, &spc))
2852 tok_str_add2(&str, tok, &tokc);
2853 next_nomacro_spc();
2855 if (!str.len)
2856 tok_str_add(&str, TOK_PLCHLDR);
2857 str.len -= spc;
2858 tok_str_add(&str, 0);
2859 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2860 sa1->d = str.str;
2861 sa = sa->next;
2862 if (tok == ')') {
2863 /* special case for gcc var args: add an empty
2864 var arg argument if it is omitted */
2865 if (sa && sa->type.t && gnu_ext)
2866 continue;
2867 else
2868 break;
2870 if (tok != ',')
2871 expect(",");
2872 next_nomacro();
2874 if (sa) {
2875 tcc_error("macro '%s' used with too few args",
2876 get_tok_str(s->v, 0));
2879 /* now subst each arg */
2880 mstr = macro_arg_subst(nested_list, mstr, args);
2881 /* free memory */
2882 sa = args;
2883 while (sa) {
2884 sa1 = sa->prev;
2885 tok_str_free(sa->d);
2886 sym_free(sa);
2887 sa = sa1;
2889 mstr_allocated = 1;
2891 sym_push2(nested_list, s->v, 0, 0);
2892 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2893 /* pop nested defined symbol */
2894 sa1 = *nested_list;
2895 *nested_list = sa1->prev;
2896 sym_free(sa1);
2897 if (mstr_allocated)
2898 tok_str_free(mstr);
2900 return 0;
2903 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2904 return the resulting string (which must be freed). */
2905 static inline int *macro_twosharps(const int *macro_str)
2907 const int *ptr;
2908 int t;
2909 TokenString macro_str1;
2910 CString cstr;
2911 int n, start_of_nosubsts;
2913 /* we search the first '##' */
2914 for(ptr = macro_str;;) {
2915 CValue cval;
2916 TOK_GET(&t, &ptr, &cval);
2917 if (t == TOK_TWOSHARPS)
2918 break;
2919 /* nothing more to do if end of string */
2920 if (t == 0)
2921 return NULL;
2924 /* we saw '##', so we need more processing to handle it */
2925 start_of_nosubsts = -1;
2926 tok_str_new(&macro_str1);
2927 for(ptr = macro_str;;) {
2928 TOK_GET(&tok, &ptr, &tokc);
2929 if (tok == 0)
2930 break;
2931 if (tok == TOK_TWOSHARPS)
2932 continue;
2933 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2934 start_of_nosubsts = macro_str1.len;
2935 while (*ptr == TOK_TWOSHARPS) {
2936 /* given 'a##b', remove nosubsts preceding 'a' */
2937 if (start_of_nosubsts >= 0)
2938 macro_str1.len = start_of_nosubsts;
2939 /* given 'a##b', skip '##' */
2940 t = *++ptr;
2941 /* given 'a##b', remove nosubsts preceding 'b' */
2942 while (t == TOK_NOSUBST)
2943 t = *++ptr;
2944 if (t && t != TOK_TWOSHARPS) {
2945 CValue cval;
2946 TOK_GET(&t, &ptr, &cval);
2947 /* We concatenate the two tokens */
2948 cstr_new(&cstr);
2949 if (tok != TOK_PLCHLDR)
2950 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2951 n = cstr.size;
2952 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
2953 cstr_cat(&cstr, get_tok_str(t, &cval));
2954 cstr_ccat(&cstr, '\0');
2956 tcc_open_bf(tcc_state, ":paste:", cstr.size);
2957 memcpy(file->buffer, cstr.data, cstr.size);
2958 for (;;) {
2959 next_nomacro1();
2960 if (0 == *file->buf_ptr)
2961 break;
2962 tok_str_add2(&macro_str1, tok, &tokc);
2963 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2964 n, cstr.data, (char*)cstr.data + n);
2966 tcc_close();
2967 cstr_free(&cstr);
2970 if (tok != TOK_NOSUBST) {
2971 tok_str_add2(&macro_str1, tok, &tokc);
2972 tok = ' ';
2973 start_of_nosubsts = -1;
2975 tok_str_add2(&macro_str1, tok, &tokc);
2977 tok_str_add(&macro_str1, 0);
2978 return macro_str1.str;
2982 /* do macro substitution of macro_str and add result to
2983 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2984 inside to avoid recursing. */
2985 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2986 const int *macro_str, struct macro_level ** can_read_stream)
2988 Sym *s;
2989 int *macro_str1;
2990 const int *ptr;
2991 int t, ret, spc;
2992 CValue cval;
2993 struct macro_level ml;
2994 int force_blank;
2996 /* first scan for '##' operator handling */
2997 ptr = macro_str;
2998 macro_str1 = macro_twosharps(ptr);
3000 if (macro_str1)
3001 ptr = macro_str1;
3002 spc = 0;
3003 force_blank = 0;
3005 while (1) {
3006 /* NOTE: ptr == NULL can only happen if tokens are read from
3007 file stream due to a macro function call */
3008 if (ptr == NULL)
3009 break;
3010 TOK_GET(&t, &ptr, &cval);
3011 if (t == 0)
3012 break;
3013 if (t == TOK_NOSUBST) {
3014 /* following token has already been subst'd. just copy it on */
3015 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3016 TOK_GET(&t, &ptr, &cval);
3017 goto no_subst;
3019 s = define_find(t);
3020 if (s != NULL) {
3021 /* if nested substitution, do nothing */
3022 if (sym_find2(*nested_list, t)) {
3023 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3024 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3025 goto no_subst;
3027 ml.p = macro_ptr;
3028 if (can_read_stream)
3029 ml.prev = *can_read_stream, *can_read_stream = &ml;
3030 macro_ptr = (int *)ptr;
3031 tok = t;
3032 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3033 ptr = (int *)macro_ptr;
3034 macro_ptr = ml.p;
3035 if (can_read_stream && *can_read_stream == &ml)
3036 *can_read_stream = ml.prev;
3037 if (ret != 0)
3038 goto no_subst;
3039 if (parse_flags & PARSE_FLAG_SPACES)
3040 force_blank = 1;
3041 } else {
3042 no_subst:
3043 if (force_blank) {
3044 tok_str_add(tok_str, ' ');
3045 spc = 1;
3046 force_blank = 0;
3048 if (!check_space(t, &spc))
3049 tok_str_add2(tok_str, t, &cval);
3052 if (macro_str1)
3053 tok_str_free(macro_str1);
3056 /* return next token with macro substitution */
3057 ST_FUNC void next(void)
3059 Sym *nested_list, *s;
3060 TokenString str;
3061 struct macro_level *ml;
3063 redo:
3064 if (parse_flags & PARSE_FLAG_SPACES)
3065 next_nomacro_spc();
3066 else
3067 next_nomacro();
3068 if (!macro_ptr) {
3069 /* if not reading from macro substituted string, then try
3070 to substitute macros */
3071 if (tok >= TOK_IDENT &&
3072 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3073 s = define_find(tok);
3074 if (s) {
3075 /* we have a macro: we try to substitute */
3076 tok_str_new(&str);
3077 nested_list = NULL;
3078 ml = NULL;
3079 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3080 /* substitution done, NOTE: maybe empty */
3081 tok_str_add(&str, 0);
3082 macro_ptr = str.str;
3083 macro_ptr_allocated = str.str;
3084 goto redo;
3088 } else {
3089 if (tok == 0) {
3090 /* end of macro or end of unget buffer */
3091 if (unget_buffer_enabled) {
3092 macro_ptr = unget_saved_macro_ptr;
3093 unget_buffer_enabled = 0;
3094 } else {
3095 /* end of macro string: free it */
3096 tok_str_free(macro_ptr_allocated);
3097 macro_ptr_allocated = NULL;
3098 macro_ptr = NULL;
3100 goto redo;
3101 } else if (tok == TOK_NOSUBST) {
3102 /* discard preprocessor's nosubst markers */
3103 goto redo;
3107 /* convert preprocessor tokens into C tokens */
3108 if (tok == TOK_PPNUM &&
3109 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3110 parse_number((char *)tokc.cstr->data);
3114 /* push back current token and set current token to 'last_tok'. Only
3115 identifier case handled for labels. */
3116 ST_INLN void unget_tok(int last_tok)
3118 int i, n;
3119 int *q;
3120 if (unget_buffer_enabled)
3122 /* assert(macro_ptr == unget_saved_buffer + 1);
3123 assert(*macro_ptr == 0); */
3125 else
3127 unget_saved_macro_ptr = macro_ptr;
3128 unget_buffer_enabled = 1;
3130 q = unget_saved_buffer;
3131 macro_ptr = q;
3132 *q++ = tok;
3133 n = tok_ext_size(tok) - 1;
3134 for(i=0;i<n;i++)
3135 *q++ = tokc.tab[i];
3136 *q = 0; /* end of token string */
3137 tok = last_tok;
3141 /* better than nothing, but needs extension to handle '-E' option
3142 correctly too */
3143 ST_FUNC void preprocess_init(TCCState *s1)
3145 s1->include_stack_ptr = s1->include_stack;
3146 /* XXX: move that before to avoid having to initialize
3147 file->ifdef_stack_ptr ? */
3148 s1->ifdef_stack_ptr = s1->ifdef_stack;
3149 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3151 vtop = vstack - 1;
3152 s1->pack_stack[0] = 0;
3153 s1->pack_stack_ptr = s1->pack_stack;
3156 ST_FUNC void preprocess_new(void)
3158 int i, c;
3159 const char *p, *r;
3161 /* init isid table */
3162 for(i=CH_EOF;i<256;i++)
3163 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3165 /* add all tokens */
3166 table_ident = NULL;
3167 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3169 tok_ident = TOK_IDENT;
3170 p = tcc_keywords;
3171 while (*p) {
3172 r = p;
3173 for(;;) {
3174 c = *r++;
3175 if (c == '\0')
3176 break;
3178 tok_alloc(p, r - p - 1);
3179 p = r;
3183 static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3185 switch (s1->Pflag) {
3186 case LINE_MACRO_OUTPUT_FORMAT_STD:
3187 /* "tcc -E -P1" case */
3188 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3189 break;
3191 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3192 /* "tcc -E -P" case: don't output a line directive */
3193 break;
3195 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3196 default:
3197 /* "tcc -E" case: a gcc standard by default */
3198 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3199 break;
3203 /* Preprocess the current file */
3204 ST_FUNC int tcc_preprocess(TCCState *s1)
3206 BufferedFile *file_ref, **iptr, **iptr_new;
3207 int token_seen, d;
3208 const char *s;
3210 preprocess_init(s1);
3211 ch = file->buf_ptr[0];
3212 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3213 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3214 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3215 token_seen = 0;
3216 file->line_ref = 0;
3217 file_ref = NULL;
3218 iptr = s1->include_stack_ptr;
3220 for (;;) {
3221 next();
3222 if (tok == TOK_EOF) {
3223 break;
3224 } else if (file != file_ref) {
3225 if (file_ref)
3226 line_macro_output(file_ref, "", s1);
3227 goto print_line;
3228 } else if (tok == TOK_LINEFEED) {
3229 if (!token_seen)
3230 continue;
3231 file->line_ref++;
3232 token_seen = 0;
3233 } else if (!token_seen) {
3234 d = file->line_num - file->line_ref;
3235 if (file != file_ref || d >= 8) {
3236 print_line:
3237 s = "";
3238 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3239 iptr_new = s1->include_stack_ptr;
3240 s = iptr_new > iptr ? " 1"
3241 : iptr_new < iptr ? " 2"
3242 : iptr_new > s1->include_stack ? " 3"
3243 : ""
3246 line_macro_output(file, s, s1);
3247 } else {
3248 while (d > 0)
3249 fputs("\n", s1->ppfp), --d;
3251 file->line_ref = (file_ref = file)->line_num;
3252 token_seen = tok != TOK_LINEFEED;
3253 if (!token_seen)
3254 continue;
3256 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3258 return 0;