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