A reverse of the commit 14745bdeb because of the problems while compiling linux 2...
[tinycc.git] / tccpp.c
blob45e394fa360e9c2e9b4795e83f8beb7e2cd5b76b
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 if (s1->do_debug)
1691 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1692 break;
1693 case TOK_ERROR:
1694 case TOK_WARNING:
1695 c = tok;
1696 ch = file->buf_ptr[0];
1697 skip_spaces();
1698 q = buf;
1699 while (ch != '\n' && ch != CH_EOF) {
1700 if ((q - buf) < sizeof(buf) - 1)
1701 *q++ = ch;
1702 if (ch == '\\') {
1703 if (handle_stray_noerror() == 0)
1704 --q;
1705 } else
1706 inp();
1708 *q = '\0';
1709 if (c == TOK_ERROR)
1710 tcc_error("#error %s", buf);
1711 else
1712 tcc_warning("#warning %s", buf);
1713 break;
1714 case TOK_PRAGMA:
1715 pragma_parse(s1);
1716 break;
1717 default:
1718 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1719 /* '!' is ignored to allow C scripts. numbers are ignored
1720 to emulate cpp behaviour */
1721 } else {
1722 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1723 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1724 else {
1725 /* this is a gas line comment in an 'S' file. */
1726 file->buf_ptr = parse_line_comment(file->buf_ptr);
1727 goto the_end;
1730 break;
1732 /* ignore other preprocess commands or #! for C scripts */
1733 while (tok != TOK_LINEFEED)
1734 next_nomacro();
1735 the_end:
1736 parse_flags = saved_parse_flags;
1739 /* evaluate escape codes in a string. */
1740 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1742 int c, n;
1743 const uint8_t *p;
1745 p = buf;
1746 for(;;) {
1747 c = *p;
1748 if (c == '\0')
1749 break;
1750 if (c == '\\') {
1751 p++;
1752 /* escape */
1753 c = *p;
1754 switch(c) {
1755 case '0': case '1': case '2': case '3':
1756 case '4': case '5': case '6': case '7':
1757 /* at most three octal digits */
1758 n = c - '0';
1759 p++;
1760 c = *p;
1761 if (isoct(c)) {
1762 n = n * 8 + c - '0';
1763 p++;
1764 c = *p;
1765 if (isoct(c)) {
1766 n = n * 8 + c - '0';
1767 p++;
1770 c = n;
1771 goto add_char_nonext;
1772 case 'x':
1773 case 'u':
1774 case 'U':
1775 p++;
1776 n = 0;
1777 for(;;) {
1778 c = *p;
1779 if (c >= 'a' && c <= 'f')
1780 c = c - 'a' + 10;
1781 else if (c >= 'A' && c <= 'F')
1782 c = c - 'A' + 10;
1783 else if (isnum(c))
1784 c = c - '0';
1785 else
1786 break;
1787 n = n * 16 + c;
1788 p++;
1790 c = n;
1791 goto add_char_nonext;
1792 case 'a':
1793 c = '\a';
1794 break;
1795 case 'b':
1796 c = '\b';
1797 break;
1798 case 'f':
1799 c = '\f';
1800 break;
1801 case 'n':
1802 c = '\n';
1803 break;
1804 case 'r':
1805 c = '\r';
1806 break;
1807 case 't':
1808 c = '\t';
1809 break;
1810 case 'v':
1811 c = '\v';
1812 break;
1813 case 'e':
1814 if (!gnu_ext)
1815 goto invalid_escape;
1816 c = 27;
1817 break;
1818 case '\'':
1819 case '\"':
1820 case '\\':
1821 case '?':
1822 break;
1823 default:
1824 invalid_escape:
1825 if (c >= '!' && c <= '~')
1826 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1827 else
1828 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1829 break;
1832 p++;
1833 add_char_nonext:
1834 if (!is_long)
1835 cstr_ccat(outstr, c);
1836 else
1837 cstr_wccat(outstr, c);
1839 /* add a trailing '\0' */
1840 if (!is_long)
1841 cstr_ccat(outstr, '\0');
1842 else
1843 cstr_wccat(outstr, '\0');
1846 /* we use 64 bit numbers */
1847 #define BN_SIZE 2
1849 /* bn = (bn << shift) | or_val */
1850 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1852 int i;
1853 unsigned int v;
1854 for(i=0;i<BN_SIZE;i++) {
1855 v = bn[i];
1856 bn[i] = (v << shift) | or_val;
1857 or_val = v >> (32 - shift);
1861 static void bn_zero(unsigned int *bn)
1863 int i;
1864 for(i=0;i<BN_SIZE;i++) {
1865 bn[i] = 0;
1869 /* parse number in null terminated string 'p' and return it in the
1870 current token */
1871 static void parse_number(const char *p)
1873 int b, t, shift, frac_bits, s, exp_val, ch;
1874 char *q;
1875 unsigned int bn[BN_SIZE];
1876 double d;
1878 /* number */
1879 q = token_buf;
1880 ch = *p++;
1881 t = ch;
1882 ch = *p++;
1883 *q++ = t;
1884 b = 10;
1885 if (t == '.') {
1886 goto float_frac_parse;
1887 } else if (t == '0') {
1888 if (ch == 'x' || ch == 'X') {
1889 q--;
1890 ch = *p++;
1891 b = 16;
1892 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1893 q--;
1894 ch = *p++;
1895 b = 2;
1898 /* parse all digits. cannot check octal numbers at this stage
1899 because of floating point constants */
1900 while (1) {
1901 if (ch >= 'a' && ch <= 'f')
1902 t = ch - 'a' + 10;
1903 else if (ch >= 'A' && ch <= 'F')
1904 t = ch - 'A' + 10;
1905 else if (isnum(ch))
1906 t = ch - '0';
1907 else
1908 break;
1909 if (t >= b)
1910 break;
1911 if (q >= token_buf + STRING_MAX_SIZE) {
1912 num_too_long:
1913 tcc_error("number too long");
1915 *q++ = ch;
1916 ch = *p++;
1918 if (ch == '.' ||
1919 ((ch == 'e' || ch == 'E') && b == 10) ||
1920 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1921 if (b != 10) {
1922 /* NOTE: strtox should support that for hexa numbers, but
1923 non ISOC99 libcs do not support it, so we prefer to do
1924 it by hand */
1925 /* hexadecimal or binary floats */
1926 /* XXX: handle overflows */
1927 *q = '\0';
1928 if (b == 16)
1929 shift = 4;
1930 else
1931 shift = 1;
1932 bn_zero(bn);
1933 q = token_buf;
1934 while (1) {
1935 t = *q++;
1936 if (t == '\0') {
1937 break;
1938 } else if (t >= 'a') {
1939 t = t - 'a' + 10;
1940 } else if (t >= 'A') {
1941 t = t - 'A' + 10;
1942 } else {
1943 t = t - '0';
1945 bn_lshift(bn, shift, t);
1947 frac_bits = 0;
1948 if (ch == '.') {
1949 ch = *p++;
1950 while (1) {
1951 t = ch;
1952 if (t >= 'a' && t <= 'f') {
1953 t = t - 'a' + 10;
1954 } else if (t >= 'A' && t <= 'F') {
1955 t = t - 'A' + 10;
1956 } else if (t >= '0' && t <= '9') {
1957 t = t - '0';
1958 } else {
1959 break;
1961 if (t >= b)
1962 tcc_error("invalid digit");
1963 bn_lshift(bn, shift, t);
1964 frac_bits += shift;
1965 ch = *p++;
1968 if (ch != 'p' && ch != 'P')
1969 expect("exponent");
1970 ch = *p++;
1971 s = 1;
1972 exp_val = 0;
1973 if (ch == '+') {
1974 ch = *p++;
1975 } else if (ch == '-') {
1976 s = -1;
1977 ch = *p++;
1979 if (ch < '0' || ch > '9')
1980 expect("exponent digits");
1981 while (ch >= '0' && ch <= '9') {
1982 exp_val = exp_val * 10 + ch - '0';
1983 ch = *p++;
1985 exp_val = exp_val * s;
1987 /* now we can generate the number */
1988 /* XXX: should patch directly float number */
1989 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1990 d = ldexp(d, exp_val - frac_bits);
1991 t = toup(ch);
1992 if (t == 'F') {
1993 ch = *p++;
1994 tok = TOK_CFLOAT;
1995 /* float : should handle overflow */
1996 tokc.f = (float)d;
1997 } else if (t == 'L') {
1998 ch = *p++;
1999 #ifdef TCC_TARGET_PE
2000 tok = TOK_CDOUBLE;
2001 tokc.d = d;
2002 #else
2003 tok = TOK_CLDOUBLE;
2004 /* XXX: not large enough */
2005 tokc.ld = (long double)d;
2006 #endif
2007 } else {
2008 tok = TOK_CDOUBLE;
2009 tokc.d = d;
2011 } else {
2012 /* decimal floats */
2013 if (ch == '.') {
2014 if (q >= token_buf + STRING_MAX_SIZE)
2015 goto num_too_long;
2016 *q++ = ch;
2017 ch = *p++;
2018 float_frac_parse:
2019 while (ch >= '0' && ch <= '9') {
2020 if (q >= token_buf + STRING_MAX_SIZE)
2021 goto num_too_long;
2022 *q++ = ch;
2023 ch = *p++;
2026 if (ch == 'e' || ch == 'E') {
2027 if (q >= token_buf + STRING_MAX_SIZE)
2028 goto num_too_long;
2029 *q++ = ch;
2030 ch = *p++;
2031 if (ch == '-' || ch == '+') {
2032 if (q >= token_buf + STRING_MAX_SIZE)
2033 goto num_too_long;
2034 *q++ = ch;
2035 ch = *p++;
2037 if (ch < '0' || ch > '9')
2038 expect("exponent digits");
2039 while (ch >= '0' && ch <= '9') {
2040 if (q >= token_buf + STRING_MAX_SIZE)
2041 goto num_too_long;
2042 *q++ = ch;
2043 ch = *p++;
2046 *q = '\0';
2047 t = toup(ch);
2048 errno = 0;
2049 if (t == 'F') {
2050 ch = *p++;
2051 tok = TOK_CFLOAT;
2052 tokc.f = strtof(token_buf, NULL);
2053 } else if (t == 'L') {
2054 ch = *p++;
2055 #ifdef TCC_TARGET_PE
2056 tok = TOK_CDOUBLE;
2057 tokc.d = strtod(token_buf, NULL);
2058 #else
2059 tok = TOK_CLDOUBLE;
2060 tokc.ld = strtold(token_buf, NULL);
2061 #endif
2062 } else {
2063 tok = TOK_CDOUBLE;
2064 tokc.d = strtod(token_buf, NULL);
2067 } else {
2068 unsigned long long n, n1;
2069 int lcount, ucount, must_64bit;
2070 const char *p1;
2072 /* integer number */
2073 *q = '\0';
2074 q = token_buf;
2075 if (b == 10 && *q == '0') {
2076 b = 8;
2077 q++;
2079 n = 0;
2080 while(1) {
2081 t = *q++;
2082 /* no need for checks except for base 10 / 8 errors */
2083 if (t == '\0')
2084 break;
2085 else if (t >= 'a')
2086 t = t - 'a' + 10;
2087 else if (t >= 'A')
2088 t = t - 'A' + 10;
2089 else
2090 t = t - '0';
2091 if (t >= b)
2092 tcc_error("invalid digit");
2093 n1 = n;
2094 n = n * b + t;
2095 /* detect overflow */
2096 /* XXX: this test is not reliable */
2097 if (n < n1)
2098 tcc_error("integer constant overflow");
2101 /* Determine the characteristics (unsigned and/or 64bit) the type of
2102 the constant must have according to the constant suffix(es) */
2103 lcount = ucount = must_64bit = 0;
2104 p1 = p;
2105 for(;;) {
2106 t = toup(ch);
2107 if (t == 'L') {
2108 if (lcount >= 2)
2109 tcc_error("three 'l's in integer constant");
2110 if (lcount && *(p - 1) != ch)
2111 tcc_error("incorrect integer suffix: %s", p1);
2112 lcount++;
2113 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2114 if (lcount == 2)
2115 #endif
2116 must_64bit = 1;
2117 ch = *p++;
2118 } else if (t == 'U') {
2119 if (ucount >= 1)
2120 tcc_error("two 'u's in integer constant");
2121 ucount++;
2122 ch = *p++;
2123 } else {
2124 break;
2128 /* Whether 64 bits are needed to hold the constant's value */
2129 if (n & 0xffffffff00000000LL || must_64bit) {
2130 tok = TOK_CLLONG;
2131 n1 = n >> 32;
2132 } else {
2133 tok = TOK_CINT;
2134 n1 = n;
2137 /* Whether type must be unsigned to hold the constant's value */
2138 if (ucount || ((n1 >> 31) && (b != 10))) {
2139 if (tok == TOK_CLLONG)
2140 tok = TOK_CULLONG;
2141 else
2142 tok = TOK_CUINT;
2143 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2144 } else if (n1 >> 31) {
2145 if (tok == TOK_CINT)
2146 tok = TOK_CLLONG;
2147 else
2148 tcc_error("integer constant overflow");
2151 if (tok == TOK_CINT || tok == TOK_CUINT)
2152 tokc.ui = n;
2153 else
2154 tokc.ull = n;
2156 if (ch)
2157 tcc_error("invalid number\n");
2161 #define PARSE2(c1, tok1, c2, tok2) \
2162 case c1: \
2163 PEEKC(c, p); \
2164 if (c == c2) { \
2165 p++; \
2166 tok = tok2; \
2167 } else { \
2168 tok = tok1; \
2170 break;
2172 /* return next token without macro substitution */
2173 static inline void next_nomacro1(void)
2175 int t, c, is_long;
2176 TokenSym *ts;
2177 uint8_t *p, *p1;
2178 unsigned int h;
2180 p = file->buf_ptr;
2181 redo_no_start:
2182 c = *p;
2183 switch(c) {
2184 case ' ':
2185 case '\t':
2186 tok = c;
2187 p++;
2188 goto keep_tok_flags;
2189 case '\f':
2190 case '\v':
2191 case '\r':
2192 p++;
2193 goto redo_no_start;
2194 case '\\':
2195 /* first look if it is in fact an end of buffer */
2196 if (p >= file->buf_end) {
2197 file->buf_ptr = p;
2198 handle_eob();
2199 p = file->buf_ptr;
2200 if (p >= file->buf_end)
2201 goto parse_eof;
2202 else
2203 goto redo_no_start;
2204 } else {
2205 file->buf_ptr = p;
2206 ch = *p;
2207 handle_stray();
2208 p = file->buf_ptr;
2209 goto redo_no_start;
2211 parse_eof:
2213 TCCState *s1 = tcc_state;
2214 if ((parse_flags & PARSE_FLAG_LINEFEED)
2215 && !(tok_flags & TOK_FLAG_EOF)) {
2216 tok_flags |= TOK_FLAG_EOF;
2217 tok = TOK_LINEFEED;
2218 goto keep_tok_flags;
2219 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2220 tok = TOK_EOF;
2221 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2222 tcc_error("missing #endif");
2223 } else if (s1->include_stack_ptr == s1->include_stack) {
2224 /* no include left : end of file. */
2225 tok = TOK_EOF;
2226 } else {
2227 tok_flags &= ~TOK_FLAG_EOF;
2228 /* pop include file */
2230 /* test if previous '#endif' was after a #ifdef at
2231 start of file */
2232 if (tok_flags & TOK_FLAG_ENDIF) {
2233 #ifdef INC_DEBUG
2234 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2235 #endif
2236 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2237 tok_flags &= ~TOK_FLAG_ENDIF;
2240 /* add end of include file debug info */
2241 if (tcc_state->do_debug) {
2242 put_stabd(N_EINCL, 0, 0);
2244 /* pop include stack */
2245 tcc_close();
2246 s1->include_stack_ptr--;
2247 p = file->buf_ptr;
2248 goto redo_no_start;
2251 break;
2253 case '\n':
2254 file->line_num++;
2255 tok_flags |= TOK_FLAG_BOL;
2256 p++;
2257 maybe_newline:
2258 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2259 goto redo_no_start;
2260 tok = TOK_LINEFEED;
2261 goto keep_tok_flags;
2263 case '#':
2264 /* XXX: simplify */
2265 PEEKC(c, p);
2266 if ((tok_flags & TOK_FLAG_BOL) &&
2267 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2268 file->buf_ptr = p;
2269 preprocess(tok_flags & TOK_FLAG_BOF);
2270 p = file->buf_ptr;
2271 goto maybe_newline;
2272 } else {
2273 if (c == '#') {
2274 p++;
2275 tok = TOK_TWOSHARPS;
2276 } else {
2277 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2278 p = parse_line_comment(p - 1);
2279 goto redo_no_start;
2280 } else {
2281 tok = '#';
2285 break;
2287 case 'a': case 'b': case 'c': case 'd':
2288 case 'e': case 'f': case 'g': case 'h':
2289 case 'i': case 'j': case 'k': case 'l':
2290 case 'm': case 'n': case 'o': case 'p':
2291 case 'q': case 'r': case 's': case 't':
2292 case 'u': case 'v': case 'w': case 'x':
2293 case 'y': case 'z':
2294 case 'A': case 'B': case 'C': case 'D':
2295 case 'E': case 'F': case 'G': case 'H':
2296 case 'I': case 'J': case 'K':
2297 case 'M': case 'N': case 'O': case 'P':
2298 case 'Q': case 'R': case 'S': case 'T':
2299 case 'U': case 'V': case 'W': case 'X':
2300 case 'Y': case 'Z':
2301 case '_':
2302 parse_ident_fast:
2303 p1 = p;
2304 h = TOK_HASH_INIT;
2305 h = TOK_HASH_FUNC(h, c);
2306 p++;
2307 for(;;) {
2308 c = *p;
2309 if (!isidnum_table[c-CH_EOF])
2310 break;
2311 h = TOK_HASH_FUNC(h, c);
2312 p++;
2314 if (c != '\\') {
2315 TokenSym **pts;
2316 int len;
2318 /* fast case : no stray found, so we have the full token
2319 and we have already hashed it */
2320 len = p - p1;
2321 h &= (TOK_HASH_SIZE - 1);
2322 pts = &hash_ident[h];
2323 for(;;) {
2324 ts = *pts;
2325 if (!ts)
2326 break;
2327 if (ts->len == len && !memcmp(ts->str, p1, len))
2328 goto token_found;
2329 pts = &(ts->hash_next);
2331 ts = tok_alloc_new(pts, (char *) p1, len);
2332 token_found: ;
2333 } else {
2334 /* slower case */
2335 cstr_reset(&tokcstr);
2337 while (p1 < p) {
2338 cstr_ccat(&tokcstr, *p1);
2339 p1++;
2341 p--;
2342 PEEKC(c, p);
2343 parse_ident_slow:
2344 while (isidnum_table[c-CH_EOF]) {
2345 cstr_ccat(&tokcstr, c);
2346 PEEKC(c, p);
2348 ts = tok_alloc(tokcstr.data, tokcstr.size);
2350 tok = ts->tok;
2351 break;
2352 case 'L':
2353 t = p[1];
2354 if (t != '\\' && t != '\'' && t != '\"') {
2355 /* fast case */
2356 goto parse_ident_fast;
2357 } else {
2358 PEEKC(c, p);
2359 if (c == '\'' || c == '\"') {
2360 is_long = 1;
2361 goto str_const;
2362 } else {
2363 cstr_reset(&tokcstr);
2364 cstr_ccat(&tokcstr, 'L');
2365 goto parse_ident_slow;
2368 break;
2369 case '0': case '1': case '2': case '3':
2370 case '4': case '5': case '6': case '7':
2371 case '8': case '9':
2373 cstr_reset(&tokcstr);
2374 /* after the first digit, accept digits, alpha, '.' or sign if
2375 prefixed by 'eEpP' */
2376 parse_num:
2377 for(;;) {
2378 t = c;
2379 cstr_ccat(&tokcstr, c);
2380 PEEKC(c, p);
2381 if (!(isnum(c) || isid(c) || c == '.' ||
2382 ((c == '+' || c == '-') &&
2383 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2384 break;
2386 /* We add a trailing '\0' to ease parsing */
2387 cstr_ccat(&tokcstr, '\0');
2388 tokc.cstr = &tokcstr;
2389 tok = TOK_PPNUM;
2390 break;
2391 case '.':
2392 /* special dot handling because it can also start a number */
2393 PEEKC(c, p);
2394 if (isnum(c)) {
2395 cstr_reset(&tokcstr);
2396 cstr_ccat(&tokcstr, '.');
2397 goto parse_num;
2398 } else if (c == '.') {
2399 PEEKC(c, p);
2400 if (c != '.')
2401 expect("'.'");
2402 PEEKC(c, p);
2403 tok = TOK_DOTS;
2404 } else {
2405 tok = '.';
2407 break;
2408 case '\'':
2409 case '\"':
2410 is_long = 0;
2411 str_const:
2413 CString str;
2414 int sep;
2416 sep = c;
2418 /* parse the string */
2419 cstr_new(&str);
2420 p = parse_pp_string(p, sep, &str);
2421 cstr_ccat(&str, '\0');
2423 /* eval the escape (should be done as TOK_PPNUM) */
2424 cstr_reset(&tokcstr);
2425 parse_escape_string(&tokcstr, str.data, is_long);
2426 cstr_free(&str);
2428 if (sep == '\'') {
2429 int char_size;
2430 /* XXX: make it portable */
2431 if (!is_long)
2432 char_size = 1;
2433 else
2434 char_size = sizeof(nwchar_t);
2435 if (tokcstr.size <= char_size)
2436 tcc_error("empty character constant");
2437 if (tokcstr.size > 2 * char_size)
2438 tcc_warning("multi-character character constant");
2439 if (!is_long) {
2440 tokc.i = *(int8_t *)tokcstr.data;
2441 tok = TOK_CCHAR;
2442 } else {
2443 tokc.i = *(nwchar_t *)tokcstr.data;
2444 tok = TOK_LCHAR;
2446 } else {
2447 tokc.cstr = &tokcstr;
2448 if (!is_long)
2449 tok = TOK_STR;
2450 else
2451 tok = TOK_LSTR;
2454 break;
2456 case '<':
2457 PEEKC(c, p);
2458 if (c == '=') {
2459 p++;
2460 tok = TOK_LE;
2461 } else if (c == '<') {
2462 PEEKC(c, p);
2463 if (c == '=') {
2464 p++;
2465 tok = TOK_A_SHL;
2466 } else {
2467 tok = TOK_SHL;
2469 } else {
2470 tok = TOK_LT;
2472 break;
2474 case '>':
2475 PEEKC(c, p);
2476 if (c == '=') {
2477 p++;
2478 tok = TOK_GE;
2479 } else if (c == '>') {
2480 PEEKC(c, p);
2481 if (c == '=') {
2482 p++;
2483 tok = TOK_A_SAR;
2484 } else {
2485 tok = TOK_SAR;
2487 } else {
2488 tok = TOK_GT;
2490 break;
2492 case '&':
2493 PEEKC(c, p);
2494 if (c == '&') {
2495 p++;
2496 tok = TOK_LAND;
2497 } else if (c == '=') {
2498 p++;
2499 tok = TOK_A_AND;
2500 } else {
2501 tok = '&';
2503 break;
2505 case '|':
2506 PEEKC(c, p);
2507 if (c == '|') {
2508 p++;
2509 tok = TOK_LOR;
2510 } else if (c == '=') {
2511 p++;
2512 tok = TOK_A_OR;
2513 } else {
2514 tok = '|';
2516 break;
2518 case '+':
2519 PEEKC(c, p);
2520 if (c == '+') {
2521 p++;
2522 tok = TOK_INC;
2523 } else if (c == '=') {
2524 p++;
2525 tok = TOK_A_ADD;
2526 } else {
2527 tok = '+';
2529 break;
2531 case '-':
2532 PEEKC(c, p);
2533 if (c == '-') {
2534 p++;
2535 tok = TOK_DEC;
2536 } else if (c == '=') {
2537 p++;
2538 tok = TOK_A_SUB;
2539 } else if (c == '>') {
2540 p++;
2541 tok = TOK_ARROW;
2542 } else {
2543 tok = '-';
2545 break;
2547 PARSE2('!', '!', '=', TOK_NE)
2548 PARSE2('=', '=', '=', TOK_EQ)
2549 PARSE2('*', '*', '=', TOK_A_MUL)
2550 PARSE2('%', '%', '=', TOK_A_MOD)
2551 PARSE2('^', '^', '=', TOK_A_XOR)
2553 /* comments or operator */
2554 case '/':
2555 PEEKC(c, p);
2556 if (c == '*') {
2557 p = parse_comment(p);
2558 /* comments replaced by a blank */
2559 tok = ' ';
2560 goto keep_tok_flags;
2561 } else if (c == '/') {
2562 p = parse_line_comment(p);
2563 tok = ' ';
2564 goto keep_tok_flags;
2565 } else if (c == '=') {
2566 p++;
2567 tok = TOK_A_DIV;
2568 } else {
2569 tok = '/';
2571 break;
2573 /* simple tokens */
2574 case '(':
2575 case ')':
2576 case '[':
2577 case ']':
2578 case '{':
2579 case '}':
2580 case ',':
2581 case ';':
2582 case ':':
2583 case '?':
2584 case '~':
2585 case '$': /* only used in assembler */
2586 case '@': /* dito */
2587 tok = c;
2588 p++;
2589 break;
2590 default:
2591 tcc_error("unrecognized character \\x%02x", c);
2592 break;
2594 tok_flags = 0;
2595 keep_tok_flags:
2596 file->buf_ptr = p;
2597 #if defined(PARSE_DEBUG)
2598 printf("token = %s\n", get_tok_str(tok, &tokc));
2599 #endif
2602 /* return next token without macro substitution. Can read input from
2603 macro_ptr buffer */
2604 static void next_nomacro_spc(void)
2606 if (macro_ptr) {
2607 redo:
2608 tok = *macro_ptr;
2609 if (tok) {
2610 TOK_GET(&tok, &macro_ptr, &tokc);
2611 if (tok == TOK_LINENUM) {
2612 file->line_num = tokc.i;
2613 goto redo;
2616 } else {
2617 next_nomacro1();
2621 ST_FUNC void next_nomacro(void)
2623 do {
2624 next_nomacro_spc();
2625 } while (is_space(tok));
2628 /* substitute arguments in replacement lists in macro_str by the values in
2629 args (field d) and return allocated string */
2630 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2632 int last_tok, t, spc;
2633 const int *st;
2634 Sym *s;
2635 CValue cval;
2636 TokenString str;
2637 CString cstr;
2639 tok_str_new(&str);
2640 last_tok = 0;
2641 while(1) {
2642 TOK_GET(&t, &macro_str, &cval);
2643 if (!t)
2644 break;
2645 if (t == '#') {
2646 /* stringize */
2647 TOK_GET(&t, &macro_str, &cval);
2648 if (!t)
2649 break;
2650 s = sym_find2(args, t);
2651 if (s) {
2652 cstr_new(&cstr);
2653 st = s->d;
2654 spc = 0;
2655 while (*st) {
2656 TOK_GET(&t, &st, &cval);
2657 if (!check_space(t, &spc))
2658 cstr_cat(&cstr, get_tok_str(t, &cval));
2660 cstr.size -= spc;
2661 cstr_ccat(&cstr, '\0');
2662 #ifdef PP_DEBUG
2663 printf("stringize: %s\n", (char *)cstr.data);
2664 #endif
2665 /* add string */
2666 cval.cstr = &cstr;
2667 tok_str_add2(&str, TOK_STR, &cval);
2668 cstr_free(&cstr);
2669 } else {
2670 tok_str_add2(&str, t, &cval);
2672 } else if (t >= TOK_IDENT) {
2673 s = sym_find2(args, t);
2674 if (s) {
2675 st = s->d;
2676 /* if '##' is present before or after, no arg substitution */
2677 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2678 /* special case for var arg macros : ## eats the
2679 ',' if empty VA_ARGS variable. */
2680 /* XXX: test of the ',' is not 100%
2681 reliable. should fix it to avoid security
2682 problems */
2683 if (gnu_ext && s->type.t &&
2684 last_tok == TOK_TWOSHARPS &&
2685 str.len >= 2 && str.str[str.len - 2] == ',') {
2686 if (*st == TOK_PLCHLDR) {
2687 /* suppress ',' '##' */
2688 str.len -= 2;
2689 } else {
2690 /* suppress '##' and add variable */
2691 str.len--;
2692 goto add_var;
2694 } else {
2695 int t1;
2696 add_var:
2697 for(;;) {
2698 TOK_GET(&t1, &st, &cval);
2699 if (!t1)
2700 break;
2701 tok_str_add2(&str, t1, &cval);
2704 } else if (*st != TOK_PLCHLDR) {
2705 /* NOTE: the stream cannot be read when macro
2706 substituing an argument */
2707 macro_subst(&str, nested_list, st, NULL);
2709 } else {
2710 tok_str_add(&str, t);
2712 } else {
2713 tok_str_add2(&str, t, &cval);
2715 last_tok = t;
2717 tok_str_add(&str, 0);
2718 return str.str;
2721 static char const ab_month_name[12][4] =
2723 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2724 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2727 /* do macro substitution of current token with macro 's' and add
2728 result to (tok_str,tok_len). 'nested_list' is the list of all
2729 macros we got inside to avoid recursing. Return non zero if no
2730 substitution needs to be done */
2731 static int macro_subst_tok(TokenString *tok_str,
2732 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2734 Sym *args, *sa, *sa1;
2735 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2736 const int *p;
2737 TokenString str;
2738 char *cstrval;
2739 CValue cval;
2740 CString cstr;
2741 char buf[32];
2743 /* if symbol is a macro, prepare substitution */
2744 /* special macros */
2745 if (tok == TOK___LINE__) {
2746 snprintf(buf, sizeof(buf), "%d", file->line_num);
2747 cstrval = buf;
2748 t1 = TOK_PPNUM;
2749 goto add_cstr1;
2750 } else if (tok == TOK___FILE__) {
2751 cstrval = file->filename;
2752 goto add_cstr;
2753 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2754 time_t ti;
2755 struct tm *tm;
2757 time(&ti);
2758 tm = localtime(&ti);
2759 if (tok == TOK___DATE__) {
2760 snprintf(buf, sizeof(buf), "%s %2d %d",
2761 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2762 } else {
2763 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2764 tm->tm_hour, tm->tm_min, tm->tm_sec);
2766 cstrval = buf;
2767 add_cstr:
2768 t1 = TOK_STR;
2769 add_cstr1:
2770 cstr_new(&cstr);
2771 cstr_cat(&cstr, cstrval);
2772 cstr_ccat(&cstr, '\0');
2773 cval.cstr = &cstr;
2774 tok_str_add2(tok_str, t1, &cval);
2775 cstr_free(&cstr);
2776 } else {
2777 mstr = s->d;
2778 mstr_allocated = 0;
2779 if (s->type.t == MACRO_FUNC) {
2780 /* NOTE: we do not use next_nomacro to avoid eating the
2781 next token. XXX: find better solution */
2782 redo:
2783 if (macro_ptr) {
2784 p = macro_ptr;
2785 while (is_space(t = *p) || TOK_LINEFEED == t)
2786 ++p;
2787 if (t == 0 && can_read_stream) {
2788 /* end of macro stream: we must look at the token
2789 after in the file */
2790 struct macro_level *ml = *can_read_stream;
2791 macro_ptr = NULL;
2792 if (ml)
2794 macro_ptr = ml->p;
2795 ml->p = NULL;
2796 *can_read_stream = ml -> prev;
2798 /* also, end of scope for nested defined symbol */
2799 (*nested_list)->v = -1;
2800 goto redo;
2802 } else {
2803 ch = file->buf_ptr[0];
2804 while (is_space(ch) || ch == '\n' || ch == '/')
2806 if (ch == '/')
2808 int c;
2809 uint8_t *p = file->buf_ptr;
2810 PEEKC(c, p);
2811 if (c == '*') {
2812 p = parse_comment(p);
2813 file->buf_ptr = p - 1;
2814 } else if (c == '/') {
2815 p = parse_line_comment(p);
2816 file->buf_ptr = p - 1;
2817 } else
2818 break;
2820 cinp();
2822 t = ch;
2824 if (t != '(') /* no macro subst */
2825 return -1;
2827 /* argument macro */
2828 next_nomacro();
2829 next_nomacro();
2830 args = NULL;
2831 sa = s->next;
2832 /* NOTE: empty args are allowed, except if no args */
2833 for(;;) {
2834 /* handle '()' case */
2835 if (!args && !sa && tok == ')')
2836 break;
2837 if (!sa)
2838 tcc_error("macro '%s' used with too many args",
2839 get_tok_str(s->v, 0));
2840 tok_str_new(&str);
2841 parlevel = spc = 0;
2842 /* NOTE: non zero sa->t indicates VA_ARGS */
2843 while ((parlevel > 0 ||
2844 (tok != ')' &&
2845 (tok != ',' || sa->type.t))) &&
2846 tok != -1) {
2847 if (tok == '(')
2848 parlevel++;
2849 else if (tok == ')')
2850 parlevel--;
2851 if (tok == TOK_LINEFEED)
2852 tok = ' ';
2853 if (!check_space(tok, &spc))
2854 tok_str_add2(&str, tok, &tokc);
2855 next_nomacro_spc();
2857 if (!str.len)
2858 tok_str_add(&str, TOK_PLCHLDR);
2859 str.len -= spc;
2860 tok_str_add(&str, 0);
2861 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2862 sa1->d = str.str;
2863 sa = sa->next;
2864 if (tok == ')') {
2865 /* special case for gcc var args: add an empty
2866 var arg argument if it is omitted */
2867 if (sa && sa->type.t && gnu_ext)
2868 continue;
2869 else
2870 break;
2872 if (tok != ',')
2873 expect(",");
2874 next_nomacro();
2876 if (sa) {
2877 tcc_error("macro '%s' used with too few args",
2878 get_tok_str(s->v, 0));
2881 /* now subst each arg */
2882 mstr = macro_arg_subst(nested_list, mstr, args);
2883 /* free memory */
2884 sa = args;
2885 while (sa) {
2886 sa1 = sa->prev;
2887 tok_str_free(sa->d);
2888 sym_free(sa);
2889 sa = sa1;
2891 mstr_allocated = 1;
2893 sym_push2(nested_list, s->v, 0, 0);
2894 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2895 /* pop nested defined symbol */
2896 sa1 = *nested_list;
2897 *nested_list = sa1->prev;
2898 sym_free(sa1);
2899 if (mstr_allocated)
2900 tok_str_free(mstr);
2902 return 0;
2905 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2906 return the resulting string (which must be freed). */
2907 static inline int *macro_twosharps(const int *macro_str)
2909 const int *ptr;
2910 int t;
2911 TokenString macro_str1;
2912 CString cstr;
2913 int n, start_of_nosubsts;
2915 /* we search the first '##' */
2916 for(ptr = macro_str;;) {
2917 CValue cval;
2918 TOK_GET(&t, &ptr, &cval);
2919 if (t == TOK_TWOSHARPS)
2920 break;
2921 /* nothing more to do if end of string */
2922 if (t == 0)
2923 return NULL;
2926 /* we saw '##', so we need more processing to handle it */
2927 start_of_nosubsts = -1;
2928 tok_str_new(&macro_str1);
2929 for(ptr = macro_str;;) {
2930 TOK_GET(&tok, &ptr, &tokc);
2931 if (tok == 0)
2932 break;
2933 if (tok == TOK_TWOSHARPS)
2934 continue;
2935 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2936 start_of_nosubsts = macro_str1.len;
2937 while (*ptr == TOK_TWOSHARPS) {
2938 /* given 'a##b', remove nosubsts preceding 'a' */
2939 if (start_of_nosubsts >= 0)
2940 macro_str1.len = start_of_nosubsts;
2941 /* given 'a##b', skip '##' */
2942 t = *++ptr;
2943 /* given 'a##b', remove nosubsts preceding 'b' */
2944 while (t == TOK_NOSUBST)
2945 t = *++ptr;
2946 if (t && t != TOK_TWOSHARPS) {
2947 CValue cval;
2948 TOK_GET(&t, &ptr, &cval);
2949 /* We concatenate the two tokens */
2950 cstr_new(&cstr);
2951 if (tok != TOK_PLCHLDR)
2952 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2953 n = cstr.size;
2954 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
2955 cstr_cat(&cstr, get_tok_str(t, &cval));
2956 cstr_ccat(&cstr, '\0');
2958 tcc_open_bf(tcc_state, ":paste:", cstr.size);
2959 memcpy(file->buffer, cstr.data, cstr.size);
2960 for (;;) {
2961 next_nomacro1();
2962 if (0 == *file->buf_ptr)
2963 break;
2964 tok_str_add2(&macro_str1, tok, &tokc);
2965 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2966 n, cstr.data, (char*)cstr.data + n);
2968 tcc_close();
2969 cstr_free(&cstr);
2972 if (tok != TOK_NOSUBST) {
2973 tok_str_add2(&macro_str1, tok, &tokc);
2974 tok = ' ';
2975 start_of_nosubsts = -1;
2977 tok_str_add2(&macro_str1, tok, &tokc);
2979 tok_str_add(&macro_str1, 0);
2980 return macro_str1.str;
2984 /* do macro substitution of macro_str and add result to
2985 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2986 inside to avoid recursing. */
2987 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2988 const int *macro_str, struct macro_level ** can_read_stream)
2990 Sym *s;
2991 int *macro_str1;
2992 const int *ptr;
2993 int t, ret, spc;
2994 CValue cval;
2995 struct macro_level ml;
2996 int force_blank;
2998 /* first scan for '##' operator handling */
2999 ptr = macro_str;
3000 macro_str1 = macro_twosharps(ptr);
3002 if (macro_str1)
3003 ptr = macro_str1;
3004 spc = 0;
3005 force_blank = 0;
3007 while (1) {
3008 /* NOTE: ptr == NULL can only happen if tokens are read from
3009 file stream due to a macro function call */
3010 if (ptr == NULL)
3011 break;
3012 TOK_GET(&t, &ptr, &cval);
3013 if (t == 0)
3014 break;
3015 if (t == TOK_NOSUBST) {
3016 /* following token has already been subst'd. just copy it on */
3017 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3018 TOK_GET(&t, &ptr, &cval);
3019 goto no_subst;
3021 s = define_find(t);
3022 if (s != NULL) {
3023 /* if nested substitution, do nothing */
3024 if (sym_find2(*nested_list, t)) {
3025 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3026 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3027 goto no_subst;
3029 ml.p = macro_ptr;
3030 if (can_read_stream)
3031 ml.prev = *can_read_stream, *can_read_stream = &ml;
3032 macro_ptr = (int *)ptr;
3033 tok = t;
3034 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3035 ptr = (int *)macro_ptr;
3036 macro_ptr = ml.p;
3037 if (can_read_stream && *can_read_stream == &ml)
3038 *can_read_stream = ml.prev;
3039 if (ret != 0)
3040 goto no_subst;
3041 if (parse_flags & PARSE_FLAG_SPACES)
3042 force_blank = 1;
3043 } else {
3044 no_subst:
3045 if (force_blank) {
3046 tok_str_add(tok_str, ' ');
3047 spc = 1;
3048 force_blank = 0;
3050 if (!check_space(t, &spc))
3051 tok_str_add2(tok_str, t, &cval);
3054 if (macro_str1)
3055 tok_str_free(macro_str1);
3058 /* return next token with macro substitution */
3059 ST_FUNC void next(void)
3061 Sym *nested_list, *s;
3062 TokenString str;
3063 struct macro_level *ml;
3065 redo:
3066 if (parse_flags & PARSE_FLAG_SPACES)
3067 next_nomacro_spc();
3068 else
3069 next_nomacro();
3070 if (!macro_ptr) {
3071 /* if not reading from macro substituted string, then try
3072 to substitute macros */
3073 if (tok >= TOK_IDENT &&
3074 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3075 s = define_find(tok);
3076 if (s) {
3077 /* we have a macro: we try to substitute */
3078 tok_str_new(&str);
3079 nested_list = NULL;
3080 ml = NULL;
3081 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3082 /* substitution done, NOTE: maybe empty */
3083 tok_str_add(&str, 0);
3084 macro_ptr = str.str;
3085 macro_ptr_allocated = str.str;
3086 goto redo;
3090 } else {
3091 if (tok == 0) {
3092 /* end of macro or end of unget buffer */
3093 if (unget_buffer_enabled) {
3094 macro_ptr = unget_saved_macro_ptr;
3095 unget_buffer_enabled = 0;
3096 } else {
3097 /* end of macro string: free it */
3098 tok_str_free(macro_ptr_allocated);
3099 macro_ptr_allocated = NULL;
3100 macro_ptr = NULL;
3102 goto redo;
3103 } else if (tok == TOK_NOSUBST) {
3104 /* discard preprocessor's nosubst markers */
3105 goto redo;
3109 /* convert preprocessor tokens into C tokens */
3110 if (tok == TOK_PPNUM &&
3111 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3112 parse_number((char *)tokc.cstr->data);
3116 /* push back current token and set current token to 'last_tok'. Only
3117 identifier case handled for labels. */
3118 ST_INLN void unget_tok(int last_tok)
3120 int i, n;
3121 int *q;
3122 if (unget_buffer_enabled)
3124 /* assert(macro_ptr == unget_saved_buffer + 1);
3125 assert(*macro_ptr == 0); */
3127 else
3129 unget_saved_macro_ptr = macro_ptr;
3130 unget_buffer_enabled = 1;
3132 q = unget_saved_buffer;
3133 macro_ptr = q;
3134 *q++ = tok;
3135 n = tok_ext_size(tok) - 1;
3136 for(i=0;i<n;i++)
3137 *q++ = tokc.tab[i];
3138 *q = 0; /* end of token string */
3139 tok = last_tok;
3143 /* better than nothing, but needs extension to handle '-E' option
3144 correctly too */
3145 ST_FUNC void preprocess_init(TCCState *s1)
3147 s1->include_stack_ptr = s1->include_stack;
3148 /* XXX: move that before to avoid having to initialize
3149 file->ifdef_stack_ptr ? */
3150 s1->ifdef_stack_ptr = s1->ifdef_stack;
3151 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3153 vtop = vstack - 1;
3154 s1->pack_stack[0] = 0;
3155 s1->pack_stack_ptr = s1->pack_stack;
3158 ST_FUNC void preprocess_new(void)
3160 int i, c;
3161 const char *p, *r;
3163 /* init isid table */
3164 for(i=CH_EOF;i<256;i++)
3165 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3167 /* add all tokens */
3168 if (table_ident) {
3169 tcc_free (table_ident);
3170 table_ident = NULL;
3172 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3174 tok_ident = TOK_IDENT;
3175 p = tcc_keywords;
3176 while (*p) {
3177 r = p;
3178 for(;;) {
3179 c = *r++;
3180 if (c == '\0')
3181 break;
3183 tok_alloc(p, r - p - 1);
3184 p = r;
3188 static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3190 switch (s1->Pflag) {
3191 case LINE_MACRO_OUTPUT_FORMAT_STD:
3192 /* "tcc -E -P1" case */
3193 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3194 break;
3196 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3197 /* "tcc -E -P" case: don't output a line directive */
3198 break;
3200 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3201 default:
3202 /* "tcc -E" case: a gcc standard by default */
3203 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3204 break;
3208 /* Preprocess the current file */
3209 ST_FUNC int tcc_preprocess(TCCState *s1)
3211 BufferedFile *file_ref, **iptr, **iptr_new;
3212 int token_seen, d;
3213 const char *s;
3215 preprocess_init(s1);
3216 ch = file->buf_ptr[0];
3217 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3218 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3219 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3220 token_seen = 0;
3221 file->line_ref = 0;
3222 file_ref = NULL;
3223 iptr = s1->include_stack_ptr;
3225 for (;;) {
3226 next();
3227 if (tok == TOK_EOF) {
3228 break;
3229 } else if (file != file_ref) {
3230 if (file_ref)
3231 line_macro_output(file_ref, "", s1);
3232 goto print_line;
3233 } else if (tok == TOK_LINEFEED) {
3234 if (!token_seen)
3235 continue;
3236 file->line_ref++;
3237 token_seen = 0;
3238 } else if (!token_seen) {
3239 d = file->line_num - file->line_ref;
3240 if (file != file_ref || d >= 8) {
3241 print_line:
3242 s = "";
3243 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3244 iptr_new = s1->include_stack_ptr;
3245 s = iptr_new > iptr ? " 1"
3246 : iptr_new < iptr ? " 2"
3247 : iptr_new > s1->include_stack ? " 3"
3248 : ""
3251 line_macro_output(file, s, s1);
3252 } else {
3253 while (d > 0)
3254 fputs("\n", s1->ppfp), --d;
3256 file->line_ref = (file_ref = file)->line_num;
3257 token_seen = tok != TOK_LINEFEED;
3258 if (!token_seen)
3259 continue;
3261 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3263 return 0;