implement #pragma comment(lib,...)
[tinycc.git] / tccpp.c
blob51353aae19a0e2f729b32edf544aa1735f577e48
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 /**/
1434 int len = strlen((char *)tokc.cstr->data);
1435 char *file = tcc_malloc(len+4); /* filetype, "-l" and 0 at the end */
1436 file[0] = TCC_FILETYPE_BINARY;
1437 file[1] = '-';
1438 file[2] = 'l';
1439 strcpy(&file[3], (char *)tokc.cstr->data);
1440 dynarray_add((void ***)&s1->files, &s1->nb_files, file);
1441 /**/
1442 /* we can't use
1443 tcc_add_library(s1,(char *)tokc.cstr->data);
1444 while compiling some file
1447 if (strrchr((char *)tokc.cstr->data,'.') == NULL)
1448 tcc_add_library(s1,(char *)tokc.cstr->data);
1449 else
1450 tcc_add_file(s1,(char *)tokc.cstr->data,TCC_FILETYPE_BINARY);
1453 next();
1454 tok = TOK_LINEFEED;
1457 else
1458 tcc_warning("#pragma comment(lib) is ignored");
1460 else
1461 tcc_warning("unknown #pragma %s", get_tok_str(tok, &tokc));
1464 /* is_bof is true if first non space token at beginning of file */
1465 ST_FUNC void preprocess(int is_bof)
1467 TCCState *s1 = tcc_state;
1468 int i, c, n, saved_parse_flags;
1469 char buf[1024], *q;
1470 Sym *s;
1472 saved_parse_flags = parse_flags;
1473 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1474 PARSE_FLAG_LINEFEED;
1475 parse_flags |= (saved_parse_flags & (PARSE_FLAG_ASM_FILE | PARSE_FLAG_ASM_COMMENTS));
1476 next_nomacro();
1477 redo:
1478 switch(tok) {
1479 case TOK_DEFINE:
1480 next_nomacro();
1481 parse_define();
1482 break;
1483 case TOK_UNDEF:
1484 next_nomacro();
1485 s = define_find(tok);
1486 /* undefine symbol by putting an invalid name */
1487 if (s)
1488 define_undef(s);
1489 break;
1490 case TOK_INCLUDE:
1491 case TOK_INCLUDE_NEXT:
1492 ch = file->buf_ptr[0];
1493 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1494 skip_spaces();
1495 if (ch == '<') {
1496 c = '>';
1497 goto read_name;
1498 } else if (ch == '\"') {
1499 c = ch;
1500 read_name:
1501 inp();
1502 q = buf;
1503 while (ch != c && ch != '\n' && ch != CH_EOF) {
1504 if ((q - buf) < sizeof(buf) - 1)
1505 *q++ = ch;
1506 if (ch == '\\') {
1507 if (handle_stray_noerror() == 0)
1508 --q;
1509 } else
1510 inp();
1512 *q = '\0';
1513 minp();
1514 #if 0
1515 /* eat all spaces and comments after include */
1516 /* XXX: slightly incorrect */
1517 while (ch1 != '\n' && ch1 != CH_EOF)
1518 inp();
1519 #endif
1520 } else {
1521 /* computed #include : either we have only strings or
1522 we have anything enclosed in '<>' */
1523 next();
1524 buf[0] = '\0';
1525 if (tok == TOK_STR) {
1526 while (tok != TOK_LINEFEED) {
1527 if (tok != TOK_STR) {
1528 include_syntax:
1529 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1531 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1532 next();
1534 c = '\"';
1535 } else {
1536 int len;
1537 while (tok != TOK_LINEFEED) {
1538 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1539 next();
1541 len = strlen(buf);
1542 /* check syntax and remove '<>' */
1543 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1544 goto include_syntax;
1545 memmove(buf, buf + 1, len - 2);
1546 buf[len - 2] = '\0';
1547 c = '>';
1551 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1552 tcc_error("#include recursion too deep");
1553 /* store current file in stack, but increment stack later below */
1554 *s1->include_stack_ptr = file;
1556 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1557 for (i = -2; i < n; ++i) {
1558 char buf1[sizeof file->filename];
1559 CachedInclude *e;
1560 BufferedFile **f;
1561 const char *path;
1563 if (i == -2) {
1564 /* check absolute include path */
1565 if (!IS_ABSPATH(buf))
1566 continue;
1567 buf1[0] = 0;
1568 i = n; /* force end loop */
1570 } else if (i == -1) {
1571 /* search in current dir if "header.h" */
1572 if (c != '\"')
1573 continue;
1574 path = file->filename;
1575 pstrncpy(buf1, path, tcc_basename(path) - path);
1577 } else {
1578 /* search in all the include paths */
1579 if (i < s1->nb_include_paths)
1580 path = s1->include_paths[i];
1581 else
1582 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1583 pstrcpy(buf1, sizeof(buf1), path);
1584 pstrcat(buf1, sizeof(buf1), "/");
1587 pstrcat(buf1, sizeof(buf1), buf);
1589 if (tok == TOK_INCLUDE_NEXT)
1590 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1591 if (0 == PATHCMP((*f)->filename, buf1)) {
1592 #ifdef INC_DEBUG
1593 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1594 #endif
1595 goto include_trynext;
1598 e = search_cached_include(s1, buf1);
1599 if (e && define_find(e->ifndef_macro)) {
1600 /* no need to parse the include because the 'ifndef macro'
1601 is defined */
1602 #ifdef INC_DEBUG
1603 printf("%s: skipping cached %s\n", file->filename, buf1);
1604 #endif
1605 goto include_done;
1608 if (tcc_open(s1, buf1) < 0)
1609 include_trynext:
1610 continue;
1612 #ifdef INC_DEBUG
1613 printf("%s: including %s\n", file->prev->filename, file->filename);
1614 #endif
1615 /* update target deps */
1616 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1617 tcc_strdup(buf1));
1618 /* push current file in stack */
1619 ++s1->include_stack_ptr;
1620 /* add include file debug info */
1621 if (s1->do_debug)
1622 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1623 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1624 ch = file->buf_ptr[0];
1625 goto the_end;
1627 tcc_error("include file '%s' not found", buf);
1628 include_done:
1629 break;
1630 case TOK_IFNDEF:
1631 c = 1;
1632 goto do_ifdef;
1633 case TOK_IF:
1634 c = expr_preprocess();
1635 goto do_if;
1636 case TOK_IFDEF:
1637 c = 0;
1638 do_ifdef:
1639 next_nomacro();
1640 if (tok < TOK_IDENT)
1641 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1642 if (is_bof) {
1643 if (c) {
1644 #ifdef INC_DEBUG
1645 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1646 #endif
1647 file->ifndef_macro = tok;
1650 c = (define_find(tok) != 0) ^ c;
1651 do_if:
1652 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1653 tcc_error("memory full (ifdef)");
1654 *s1->ifdef_stack_ptr++ = c;
1655 goto test_skip;
1656 case TOK_ELSE:
1657 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1658 tcc_error("#else without matching #if");
1659 if (s1->ifdef_stack_ptr[-1] & 2)
1660 tcc_error("#else after #else");
1661 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1662 goto test_else;
1663 case TOK_ELIF:
1664 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1665 tcc_error("#elif without matching #if");
1666 c = s1->ifdef_stack_ptr[-1];
1667 if (c > 1)
1668 tcc_error("#elif after #else");
1669 /* last #if/#elif expression was true: we skip */
1670 if (c == 1)
1671 goto skip;
1672 c = expr_preprocess();
1673 s1->ifdef_stack_ptr[-1] = c;
1674 test_else:
1675 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1676 file->ifndef_macro = 0;
1677 test_skip:
1678 if (!(c & 1)) {
1679 skip:
1680 preprocess_skip();
1681 is_bof = 0;
1682 goto redo;
1684 break;
1685 case TOK_ENDIF:
1686 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1687 tcc_error("#endif without matching #if");
1688 s1->ifdef_stack_ptr--;
1689 /* '#ifndef macro' was at the start of file. Now we check if
1690 an '#endif' is exactly at the end of file */
1691 if (file->ifndef_macro &&
1692 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1693 file->ifndef_macro_saved = file->ifndef_macro;
1694 /* need to set to zero to avoid false matches if another
1695 #ifndef at middle of file */
1696 file->ifndef_macro = 0;
1697 while (tok != TOK_LINEFEED)
1698 next_nomacro();
1699 tok_flags |= TOK_FLAG_ENDIF;
1700 goto the_end;
1702 break;
1703 case TOK_LINE:
1704 next();
1705 if (tok != TOK_CINT)
1706 tcc_error("A #line format is wrong");
1707 case TOK_PPNUM:
1708 if (tok != TOK_CINT) {
1709 char *p = tokc.cstr->data;
1710 tokc.i = strtoul(p, (char **)&p, 10);
1712 i = file->line_num;
1713 file->line_num = tokc.i - 1;
1714 next();
1715 if (tok != TOK_LINEFEED) {
1716 if (tok != TOK_STR) {
1717 if ((parse_flags & PARSE_FLAG_ASM_COMMENTS) == 0) {
1718 file->line_num = i;
1719 tcc_error("#line format is wrong");
1721 break;
1723 pstrcpy(file->filename, sizeof(file->filename),
1724 (char *)tokc.cstr->data);
1726 if (s1->do_debug)
1727 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1728 break;
1729 case TOK_ERROR:
1730 case TOK_WARNING:
1731 c = tok;
1732 ch = file->buf_ptr[0];
1733 skip_spaces();
1734 q = buf;
1735 while (ch != '\n' && ch != CH_EOF) {
1736 if ((q - buf) < sizeof(buf) - 1)
1737 *q++ = ch;
1738 if (ch == '\\') {
1739 if (handle_stray_noerror() == 0)
1740 --q;
1741 } else
1742 inp();
1744 *q = '\0';
1745 if (c == TOK_ERROR)
1746 tcc_error("#error %s", buf);
1747 else
1748 tcc_warning("#warning %s", buf);
1749 break;
1750 case TOK_PRAGMA:
1751 pragma_parse(s1);
1752 break;
1753 default:
1754 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1755 /* '!' is ignored to allow C scripts. numbers are ignored
1756 to emulate cpp behaviour */
1757 } else {
1758 if (!(parse_flags & PARSE_FLAG_ASM_COMMENTS))
1759 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1760 else {
1761 /* this is a gas line comment in an 'S' file. */
1762 file->buf_ptr = parse_line_comment(file->buf_ptr);
1763 goto the_end;
1766 break;
1768 /* ignore other preprocess commands or #! for C scripts */
1769 while (tok != TOK_LINEFEED)
1770 next_nomacro();
1771 the_end:
1772 parse_flags = saved_parse_flags;
1775 /* evaluate escape codes in a string. */
1776 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1778 int c, n;
1779 const uint8_t *p;
1781 p = buf;
1782 for(;;) {
1783 c = *p;
1784 if (c == '\0')
1785 break;
1786 if (c == '\\') {
1787 p++;
1788 /* escape */
1789 c = *p;
1790 switch(c) {
1791 case '0': case '1': case '2': case '3':
1792 case '4': case '5': case '6': case '7':
1793 /* at most three octal digits */
1794 n = c - '0';
1795 p++;
1796 c = *p;
1797 if (isoct(c)) {
1798 n = n * 8 + c - '0';
1799 p++;
1800 c = *p;
1801 if (isoct(c)) {
1802 n = n * 8 + c - '0';
1803 p++;
1806 c = n;
1807 goto add_char_nonext;
1808 case 'x':
1809 case 'u':
1810 case 'U':
1811 p++;
1812 n = 0;
1813 for(;;) {
1814 c = *p;
1815 if (c >= 'a' && c <= 'f')
1816 c = c - 'a' + 10;
1817 else if (c >= 'A' && c <= 'F')
1818 c = c - 'A' + 10;
1819 else if (isnum(c))
1820 c = c - '0';
1821 else
1822 break;
1823 n = n * 16 + c;
1824 p++;
1826 c = n;
1827 goto add_char_nonext;
1828 case 'a':
1829 c = '\a';
1830 break;
1831 case 'b':
1832 c = '\b';
1833 break;
1834 case 'f':
1835 c = '\f';
1836 break;
1837 case 'n':
1838 c = '\n';
1839 break;
1840 case 'r':
1841 c = '\r';
1842 break;
1843 case 't':
1844 c = '\t';
1845 break;
1846 case 'v':
1847 c = '\v';
1848 break;
1849 case 'e':
1850 if (!gnu_ext)
1851 goto invalid_escape;
1852 c = 27;
1853 break;
1854 case '\'':
1855 case '\"':
1856 case '\\':
1857 case '?':
1858 break;
1859 default:
1860 invalid_escape:
1861 if (c >= '!' && c <= '~')
1862 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1863 else
1864 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1865 break;
1868 p++;
1869 add_char_nonext:
1870 if (!is_long)
1871 cstr_ccat(outstr, c);
1872 else
1873 cstr_wccat(outstr, c);
1875 /* add a trailing '\0' */
1876 if (!is_long)
1877 cstr_ccat(outstr, '\0');
1878 else
1879 cstr_wccat(outstr, '\0');
1882 /* we use 64 bit numbers */
1883 #define BN_SIZE 2
1885 /* bn = (bn << shift) | or_val */
1886 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1888 int i;
1889 unsigned int v;
1890 for(i=0;i<BN_SIZE;i++) {
1891 v = bn[i];
1892 bn[i] = (v << shift) | or_val;
1893 or_val = v >> (32 - shift);
1897 static void bn_zero(unsigned int *bn)
1899 int i;
1900 for(i=0;i<BN_SIZE;i++) {
1901 bn[i] = 0;
1905 /* parse number in null terminated string 'p' and return it in the
1906 current token */
1907 static void parse_number(const char *p)
1909 int b, t, shift, frac_bits, s, exp_val, ch;
1910 char *q;
1911 unsigned int bn[BN_SIZE];
1912 double d;
1914 /* number */
1915 q = token_buf;
1916 ch = *p++;
1917 t = ch;
1918 ch = *p++;
1919 *q++ = t;
1920 b = 10;
1921 if (t == '.') {
1922 goto float_frac_parse;
1923 } else if (t == '0') {
1924 if (ch == 'x' || ch == 'X') {
1925 q--;
1926 ch = *p++;
1927 b = 16;
1928 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1929 q--;
1930 ch = *p++;
1931 b = 2;
1934 /* parse all digits. cannot check octal numbers at this stage
1935 because of floating point constants */
1936 while (1) {
1937 if (ch >= 'a' && ch <= 'f')
1938 t = ch - 'a' + 10;
1939 else if (ch >= 'A' && ch <= 'F')
1940 t = ch - 'A' + 10;
1941 else if (isnum(ch))
1942 t = ch - '0';
1943 else
1944 break;
1945 if (t >= b)
1946 break;
1947 if (q >= token_buf + STRING_MAX_SIZE) {
1948 num_too_long:
1949 tcc_error("number too long");
1951 *q++ = ch;
1952 ch = *p++;
1954 if (ch == '.' ||
1955 ((ch == 'e' || ch == 'E') && b == 10) ||
1956 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1957 if (b != 10) {
1958 /* NOTE: strtox should support that for hexa numbers, but
1959 non ISOC99 libcs do not support it, so we prefer to do
1960 it by hand */
1961 /* hexadecimal or binary floats */
1962 /* XXX: handle overflows */
1963 *q = '\0';
1964 if (b == 16)
1965 shift = 4;
1966 else
1967 shift = 1;
1968 bn_zero(bn);
1969 q = token_buf;
1970 while (1) {
1971 t = *q++;
1972 if (t == '\0') {
1973 break;
1974 } else if (t >= 'a') {
1975 t = t - 'a' + 10;
1976 } else if (t >= 'A') {
1977 t = t - 'A' + 10;
1978 } else {
1979 t = t - '0';
1981 bn_lshift(bn, shift, t);
1983 frac_bits = 0;
1984 if (ch == '.') {
1985 ch = *p++;
1986 while (1) {
1987 t = ch;
1988 if (t >= 'a' && t <= 'f') {
1989 t = t - 'a' + 10;
1990 } else if (t >= 'A' && t <= 'F') {
1991 t = t - 'A' + 10;
1992 } else if (t >= '0' && t <= '9') {
1993 t = t - '0';
1994 } else {
1995 break;
1997 if (t >= b)
1998 tcc_error("invalid digit");
1999 bn_lshift(bn, shift, t);
2000 frac_bits += shift;
2001 ch = *p++;
2004 if (ch != 'p' && ch != 'P')
2005 expect("exponent");
2006 ch = *p++;
2007 s = 1;
2008 exp_val = 0;
2009 if (ch == '+') {
2010 ch = *p++;
2011 } else if (ch == '-') {
2012 s = -1;
2013 ch = *p++;
2015 if (ch < '0' || ch > '9')
2016 expect("exponent digits");
2017 while (ch >= '0' && ch <= '9') {
2018 exp_val = exp_val * 10 + ch - '0';
2019 ch = *p++;
2021 exp_val = exp_val * s;
2023 /* now we can generate the number */
2024 /* XXX: should patch directly float number */
2025 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2026 d = ldexp(d, exp_val - frac_bits);
2027 t = toup(ch);
2028 if (t == 'F') {
2029 ch = *p++;
2030 tok = TOK_CFLOAT;
2031 /* float : should handle overflow */
2032 tokc.f = (float)d;
2033 } else if (t == 'L') {
2034 ch = *p++;
2035 #ifdef TCC_TARGET_PE
2036 tok = TOK_CDOUBLE;
2037 tokc.d = d;
2038 #else
2039 tok = TOK_CLDOUBLE;
2040 /* XXX: not large enough */
2041 tokc.ld = (long double)d;
2042 #endif
2043 } else {
2044 tok = TOK_CDOUBLE;
2045 tokc.d = d;
2047 } else {
2048 /* decimal floats */
2049 if (ch == '.') {
2050 if (q >= token_buf + STRING_MAX_SIZE)
2051 goto num_too_long;
2052 *q++ = ch;
2053 ch = *p++;
2054 float_frac_parse:
2055 while (ch >= '0' && ch <= '9') {
2056 if (q >= token_buf + STRING_MAX_SIZE)
2057 goto num_too_long;
2058 *q++ = ch;
2059 ch = *p++;
2062 if (ch == 'e' || ch == 'E') {
2063 if (q >= token_buf + STRING_MAX_SIZE)
2064 goto num_too_long;
2065 *q++ = ch;
2066 ch = *p++;
2067 if (ch == '-' || ch == '+') {
2068 if (q >= token_buf + STRING_MAX_SIZE)
2069 goto num_too_long;
2070 *q++ = ch;
2071 ch = *p++;
2073 if (ch < '0' || ch > '9')
2074 expect("exponent digits");
2075 while (ch >= '0' && ch <= '9') {
2076 if (q >= token_buf + STRING_MAX_SIZE)
2077 goto num_too_long;
2078 *q++ = ch;
2079 ch = *p++;
2082 *q = '\0';
2083 t = toup(ch);
2084 errno = 0;
2085 if (t == 'F') {
2086 ch = *p++;
2087 tok = TOK_CFLOAT;
2088 tokc.f = strtof(token_buf, NULL);
2089 } else if (t == 'L') {
2090 ch = *p++;
2091 #ifdef TCC_TARGET_PE
2092 tok = TOK_CDOUBLE;
2093 tokc.d = strtod(token_buf, NULL);
2094 #else
2095 tok = TOK_CLDOUBLE;
2096 tokc.ld = strtold(token_buf, NULL);
2097 #endif
2098 } else {
2099 tok = TOK_CDOUBLE;
2100 tokc.d = strtod(token_buf, NULL);
2103 } else {
2104 unsigned long long n, n1;
2105 int lcount, ucount, must_64bit;
2106 const char *p1;
2108 /* integer number */
2109 *q = '\0';
2110 q = token_buf;
2111 if (b == 10 && *q == '0') {
2112 b = 8;
2113 q++;
2115 n = 0;
2116 while(1) {
2117 t = *q++;
2118 /* no need for checks except for base 10 / 8 errors */
2119 if (t == '\0')
2120 break;
2121 else if (t >= 'a')
2122 t = t - 'a' + 10;
2123 else if (t >= 'A')
2124 t = t - 'A' + 10;
2125 else
2126 t = t - '0';
2127 if (t >= b)
2128 tcc_error("invalid digit");
2129 n1 = n;
2130 n = n * b + t;
2131 /* detect overflow */
2132 /* XXX: this test is not reliable */
2133 if (n < n1)
2134 tcc_error("integer constant overflow");
2137 /* Determine the characteristics (unsigned and/or 64bit) the type of
2138 the constant must have according to the constant suffix(es) */
2139 lcount = ucount = must_64bit = 0;
2140 p1 = p;
2141 for(;;) {
2142 t = toup(ch);
2143 if (t == 'L') {
2144 if (lcount >= 2)
2145 tcc_error("three 'l's in integer constant");
2146 if (lcount && *(p - 1) != ch)
2147 tcc_error("incorrect integer suffix: %s", p1);
2148 lcount++;
2149 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2150 if (lcount == 2)
2151 #endif
2152 must_64bit = 1;
2153 ch = *p++;
2154 } else if (t == 'U') {
2155 if (ucount >= 1)
2156 tcc_error("two 'u's in integer constant");
2157 ucount++;
2158 ch = *p++;
2159 } else {
2160 break;
2164 /* Whether 64 bits are needed to hold the constant's value */
2165 if (n & 0xffffffff00000000LL || must_64bit) {
2166 tok = TOK_CLLONG;
2167 n1 = n >> 32;
2168 } else {
2169 tok = TOK_CINT;
2170 n1 = n;
2173 /* Whether type must be unsigned to hold the constant's value */
2174 if (ucount || ((n1 >> 31) && (b != 10))) {
2175 if (tok == TOK_CLLONG)
2176 tok = TOK_CULLONG;
2177 else
2178 tok = TOK_CUINT;
2179 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2180 } else if (n1 >> 31) {
2181 if (tok == TOK_CINT)
2182 tok = TOK_CLLONG;
2183 else
2184 tcc_error("integer constant overflow");
2187 if (tok == TOK_CINT || tok == TOK_CUINT)
2188 tokc.ui = n;
2189 else
2190 tokc.ull = n;
2192 if (ch)
2193 tcc_error("invalid number\n");
2197 #define PARSE2(c1, tok1, c2, tok2) \
2198 case c1: \
2199 PEEKC(c, p); \
2200 if (c == c2) { \
2201 p++; \
2202 tok = tok2; \
2203 } else { \
2204 tok = tok1; \
2206 break;
2208 /* return next token without macro substitution */
2209 static inline void next_nomacro1(void)
2211 int t, c, is_long;
2212 TokenSym *ts;
2213 uint8_t *p, *p1;
2214 unsigned int h;
2216 p = file->buf_ptr;
2217 redo_no_start:
2218 c = *p;
2219 switch(c) {
2220 case ' ':
2221 case '\t':
2222 tok = c;
2223 p++;
2224 goto keep_tok_flags;
2225 case '\f':
2226 case '\v':
2227 case '\r':
2228 p++;
2229 goto redo_no_start;
2230 case '\\':
2231 /* first look if it is in fact an end of buffer */
2232 if (p >= file->buf_end) {
2233 file->buf_ptr = p;
2234 handle_eob();
2235 p = file->buf_ptr;
2236 if (p >= file->buf_end)
2237 goto parse_eof;
2238 else
2239 goto redo_no_start;
2240 } else {
2241 file->buf_ptr = p;
2242 ch = *p;
2243 handle_stray();
2244 p = file->buf_ptr;
2245 goto redo_no_start;
2247 parse_eof:
2249 TCCState *s1 = tcc_state;
2250 if ((parse_flags & PARSE_FLAG_LINEFEED)
2251 && !(tok_flags & TOK_FLAG_EOF)) {
2252 tok_flags |= TOK_FLAG_EOF;
2253 tok = TOK_LINEFEED;
2254 goto keep_tok_flags;
2255 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2256 tok = TOK_EOF;
2257 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2258 tcc_error("missing #endif");
2259 } else if (s1->include_stack_ptr == s1->include_stack) {
2260 /* no include left : end of file. */
2261 tok = TOK_EOF;
2262 } else {
2263 tok_flags &= ~TOK_FLAG_EOF;
2264 /* pop include file */
2266 /* test if previous '#endif' was after a #ifdef at
2267 start of file */
2268 if (tok_flags & TOK_FLAG_ENDIF) {
2269 #ifdef INC_DEBUG
2270 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2271 #endif
2272 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2273 tok_flags &= ~TOK_FLAG_ENDIF;
2276 /* add end of include file debug info */
2277 if (tcc_state->do_debug) {
2278 put_stabd(N_EINCL, 0, 0);
2280 /* pop include stack */
2281 tcc_close();
2282 s1->include_stack_ptr--;
2283 p = file->buf_ptr;
2284 goto redo_no_start;
2287 break;
2289 case '\n':
2290 file->line_num++;
2291 tok_flags |= TOK_FLAG_BOL;
2292 p++;
2293 maybe_newline:
2294 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2295 goto redo_no_start;
2296 tok = TOK_LINEFEED;
2297 goto keep_tok_flags;
2299 case '#':
2300 /* XXX: simplify */
2301 PEEKC(c, p);
2302 if (is_space(c) && (parse_flags & PARSE_FLAG_ASM_FILE)) {
2303 p = parse_line_comment(p);
2304 goto redo_no_start;
2306 else
2307 if ((tok_flags & TOK_FLAG_BOL) &&
2308 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2309 file->buf_ptr = p;
2310 preprocess(tok_flags & TOK_FLAG_BOF);
2311 p = file->buf_ptr;
2312 goto maybe_newline;
2313 } else {
2314 if (c == '#') {
2315 p++;
2316 tok = TOK_TWOSHARPS;
2317 } else {
2318 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2319 p = parse_line_comment(p - 1);
2320 goto redo_no_start;
2321 } else {
2322 tok = '#';
2326 break;
2328 /* treat $ as allowed char in indentifier */
2329 case '$': if (!tcc_state->dollars_in_identifiers) goto parse_simple;
2331 case 'a': case 'b': case 'c': case 'd':
2332 case 'e': case 'f': case 'g': case 'h':
2333 case 'i': case 'j': case 'k': case 'l':
2334 case 'm': case 'n': case 'o': case 'p':
2335 case 'q': case 'r': case 's': case 't':
2336 case 'u': case 'v': case 'w': case 'x':
2337 case 'y': case 'z':
2338 case 'A': case 'B': case 'C': case 'D':
2339 case 'E': case 'F': case 'G': case 'H':
2340 case 'I': case 'J': case 'K':
2341 case 'M': case 'N': case 'O': case 'P':
2342 case 'Q': case 'R': case 'S': case 'T':
2343 case 'U': case 'V': case 'W': case 'X':
2344 case 'Y': case 'Z':
2345 case '_':
2346 parse_ident_fast:
2347 p1 = p;
2348 h = TOK_HASH_INIT;
2349 h = TOK_HASH_FUNC(h, c);
2350 p++;
2351 for(;;) {
2352 c = *p;
2353 if (!isidnum_table[c-CH_EOF])
2354 break;
2355 h = TOK_HASH_FUNC(h, c);
2356 p++;
2358 if (c != '\\') {
2359 TokenSym **pts;
2360 int len;
2362 /* fast case : no stray found, so we have the full token
2363 and we have already hashed it */
2364 len = p - p1;
2365 h &= (TOK_HASH_SIZE - 1);
2366 pts = &hash_ident[h];
2367 for(;;) {
2368 ts = *pts;
2369 if (!ts)
2370 break;
2371 if (ts->len == len && !memcmp(ts->str, p1, len))
2372 goto token_found;
2373 pts = &(ts->hash_next);
2375 ts = tok_alloc_new(pts, (char *) p1, len);
2376 token_found: ;
2377 } else {
2378 /* slower case */
2379 cstr_reset(&tokcstr);
2381 while (p1 < p) {
2382 cstr_ccat(&tokcstr, *p1);
2383 p1++;
2385 p--;
2386 PEEKC(c, p);
2387 parse_ident_slow:
2388 while (isidnum_table[c-CH_EOF]) {
2389 cstr_ccat(&tokcstr, c);
2390 PEEKC(c, p);
2392 ts = tok_alloc(tokcstr.data, tokcstr.size);
2394 tok = ts->tok;
2395 break;
2396 case 'L':
2397 t = p[1];
2398 if (t != '\\' && t != '\'' && t != '\"') {
2399 /* fast case */
2400 goto parse_ident_fast;
2401 } else {
2402 PEEKC(c, p);
2403 if (c == '\'' || c == '\"') {
2404 is_long = 1;
2405 goto str_const;
2406 } else {
2407 cstr_reset(&tokcstr);
2408 cstr_ccat(&tokcstr, 'L');
2409 goto parse_ident_slow;
2412 break;
2413 case '0': case '1': case '2': case '3':
2414 case '4': case '5': case '6': case '7':
2415 case '8': case '9':
2417 cstr_reset(&tokcstr);
2418 /* after the first digit, accept digits, alpha, '.' or sign if
2419 prefixed by 'eEpP' */
2420 parse_num:
2421 for(;;) {
2422 t = c;
2423 cstr_ccat(&tokcstr, c);
2424 PEEKC(c, p);
2425 if (!(isnum(c) || isid(c) || c == '.' ||
2426 ((c == '+' || c == '-') &&
2427 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2428 break;
2430 /* We add a trailing '\0' to ease parsing */
2431 cstr_ccat(&tokcstr, '\0');
2432 tokc.cstr = &tokcstr;
2433 tok = TOK_PPNUM;
2434 break;
2435 case '.':
2436 /* special dot handling because it can also start a number */
2437 PEEKC(c, p);
2438 if (isnum(c)) {
2439 cstr_reset(&tokcstr);
2440 cstr_ccat(&tokcstr, '.');
2441 goto parse_num;
2442 } else if (c == '.') {
2443 PEEKC(c, p);
2444 if (c != '.') {
2445 if ((parse_flags & PARSE_FLAG_ASM_COMMENTS) == 0)
2446 expect("'.'");
2447 tok = '.';
2448 break;
2450 PEEKC(c, p);
2451 tok = TOK_DOTS;
2452 } else {
2453 tok = '.';
2455 break;
2456 case '\'':
2457 case '\"':
2458 is_long = 0;
2459 str_const:
2461 CString str;
2462 int sep;
2464 sep = c;
2466 /* parse the string */
2467 cstr_new(&str);
2468 p = parse_pp_string(p, sep, &str);
2469 cstr_ccat(&str, '\0');
2471 /* eval the escape (should be done as TOK_PPNUM) */
2472 cstr_reset(&tokcstr);
2473 parse_escape_string(&tokcstr, str.data, is_long);
2474 cstr_free(&str);
2476 if (sep == '\'') {
2477 int char_size;
2478 /* XXX: make it portable */
2479 if (!is_long)
2480 char_size = 1;
2481 else
2482 char_size = sizeof(nwchar_t);
2483 if (tokcstr.size <= char_size)
2484 tcc_error("empty character constant");
2485 if (tokcstr.size > 2 * char_size)
2486 tcc_warning("multi-character character constant");
2487 if (!is_long) {
2488 tokc.i = *(int8_t *)tokcstr.data;
2489 tok = TOK_CCHAR;
2490 } else {
2491 tokc.i = *(nwchar_t *)tokcstr.data;
2492 tok = TOK_LCHAR;
2494 } else {
2495 tokc.cstr = &tokcstr;
2496 if (!is_long)
2497 tok = TOK_STR;
2498 else
2499 tok = TOK_LSTR;
2502 break;
2504 case '<':
2505 PEEKC(c, p);
2506 if (c == '=') {
2507 p++;
2508 tok = TOK_LE;
2509 } else if (c == '<') {
2510 PEEKC(c, p);
2511 if (c == '=') {
2512 p++;
2513 tok = TOK_A_SHL;
2514 } else {
2515 tok = TOK_SHL;
2517 } else {
2518 tok = TOK_LT;
2520 break;
2522 case '>':
2523 PEEKC(c, p);
2524 if (c == '=') {
2525 p++;
2526 tok = TOK_GE;
2527 } else if (c == '>') {
2528 PEEKC(c, p);
2529 if (c == '=') {
2530 p++;
2531 tok = TOK_A_SAR;
2532 } else {
2533 tok = TOK_SAR;
2535 } else {
2536 tok = TOK_GT;
2538 break;
2540 case '&':
2541 PEEKC(c, p);
2542 if (c == '&') {
2543 p++;
2544 tok = TOK_LAND;
2545 } else if (c == '=') {
2546 p++;
2547 tok = TOK_A_AND;
2548 } else {
2549 tok = '&';
2551 break;
2553 case '|':
2554 PEEKC(c, p);
2555 if (c == '|') {
2556 p++;
2557 tok = TOK_LOR;
2558 } else if (c == '=') {
2559 p++;
2560 tok = TOK_A_OR;
2561 } else {
2562 tok = '|';
2564 break;
2566 case '+':
2567 PEEKC(c, p);
2568 if (c == '+') {
2569 p++;
2570 tok = TOK_INC;
2571 } else if (c == '=') {
2572 p++;
2573 tok = TOK_A_ADD;
2574 } else {
2575 tok = '+';
2577 break;
2579 case '-':
2580 PEEKC(c, p);
2581 if (c == '-') {
2582 p++;
2583 tok = TOK_DEC;
2584 } else if (c == '=') {
2585 p++;
2586 tok = TOK_A_SUB;
2587 } else if (c == '>') {
2588 p++;
2589 tok = TOK_ARROW;
2590 } else {
2591 tok = '-';
2593 break;
2595 PARSE2('!', '!', '=', TOK_NE)
2596 PARSE2('=', '=', '=', TOK_EQ)
2597 PARSE2('*', '*', '=', TOK_A_MUL)
2598 PARSE2('%', '%', '=', TOK_A_MOD)
2599 PARSE2('^', '^', '=', TOK_A_XOR)
2601 /* comments or operator */
2602 case '/':
2603 PEEKC(c, p);
2604 if (c == '*') {
2605 p = parse_comment(p);
2606 /* comments replaced by a blank */
2607 tok = ' ';
2608 goto keep_tok_flags;
2609 } else if (c == '/') {
2610 p = parse_line_comment(p);
2611 tok = ' ';
2612 goto keep_tok_flags;
2613 } else if (c == '=') {
2614 p++;
2615 tok = TOK_A_DIV;
2616 } else {
2617 tok = '/';
2619 break;
2621 /* simple tokens */
2622 case '(':
2623 case ')':
2624 case '[':
2625 case ']':
2626 case '{':
2627 case '}':
2628 case ',':
2629 case ';':
2630 case ':':
2631 case '?':
2632 case '~':
2633 case '@': /* only used in assembler */
2634 parse_simple:
2635 tok = c;
2636 p++;
2637 break;
2638 default:
2639 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0)
2640 tcc_error("unrecognized character \\x%02x", c);
2641 else {
2642 tok = ' ';
2643 p++;
2645 break;
2647 tok_flags = 0;
2648 keep_tok_flags:
2649 file->buf_ptr = p;
2650 #if defined(PARSE_DEBUG)
2651 printf("token = %s\n", get_tok_str(tok, &tokc));
2652 #endif
2655 /* return next token without macro substitution. Can read input from
2656 macro_ptr buffer */
2657 static void next_nomacro_spc(void)
2659 if (macro_ptr) {
2660 redo:
2661 tok = *macro_ptr;
2662 if (tok) {
2663 TOK_GET(&tok, &macro_ptr, &tokc);
2664 if (tok == TOK_LINENUM) {
2665 file->line_num = tokc.i;
2666 goto redo;
2669 } else {
2670 next_nomacro1();
2674 ST_FUNC void next_nomacro(void)
2676 do {
2677 next_nomacro_spc();
2678 } while (is_space(tok));
2681 /* substitute arguments in replacement lists in macro_str by the values in
2682 args (field d) and return allocated string */
2683 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2685 int last_tok, t, spc;
2686 const int *st;
2687 Sym *s;
2688 CValue cval;
2689 TokenString str;
2690 CString cstr;
2692 tok_str_new(&str);
2693 last_tok = 0;
2694 while(1) {
2695 TOK_GET(&t, &macro_str, &cval);
2696 if (!t)
2697 break;
2698 if (t == '#') {
2699 /* stringize */
2700 TOK_GET(&t, &macro_str, &cval);
2701 if (!t)
2702 break;
2703 s = sym_find2(args, t);
2704 if (s) {
2705 cstr_new(&cstr);
2706 st = s->d;
2707 spc = 0;
2708 while (*st) {
2709 TOK_GET(&t, &st, &cval);
2710 if (!check_space(t, &spc))
2711 cstr_cat(&cstr, get_tok_str(t, &cval));
2713 cstr.size -= spc;
2714 cstr_ccat(&cstr, '\0');
2715 #ifdef PP_DEBUG
2716 printf("stringize: %s\n", (char *)cstr.data);
2717 #endif
2718 /* add string */
2719 cval.cstr = &cstr;
2720 tok_str_add2(&str, TOK_STR, &cval);
2721 cstr_free(&cstr);
2722 } else {
2723 tok_str_add2(&str, t, &cval);
2725 } else if (t >= TOK_IDENT) {
2726 s = sym_find2(args, t);
2727 if (s) {
2728 st = s->d;
2729 /* if '##' is present before or after, no arg substitution */
2730 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2731 /* special case for var arg macros : ## eats the
2732 ',' if empty VA_ARGS variable. */
2733 /* XXX: test of the ',' is not 100%
2734 reliable. should fix it to avoid security
2735 problems */
2736 if (gnu_ext && s->type.t &&
2737 last_tok == TOK_TWOSHARPS &&
2738 str.len >= 2 && str.str[str.len - 2] == ',') {
2739 if (*st == TOK_PLCHLDR) {
2740 /* suppress ',' '##' */
2741 str.len -= 2;
2742 } else {
2743 /* suppress '##' and add variable */
2744 str.len--;
2745 goto add_var;
2747 } else {
2748 int t1;
2749 add_var:
2750 for(;;) {
2751 TOK_GET(&t1, &st, &cval);
2752 if (!t1)
2753 break;
2754 tok_str_add2(&str, t1, &cval);
2757 } else if (*st != TOK_PLCHLDR) {
2758 /* NOTE: the stream cannot be read when macro
2759 substituing an argument */
2760 macro_subst(&str, nested_list, st, NULL);
2762 } else {
2763 tok_str_add(&str, t);
2765 } else {
2766 tok_str_add2(&str, t, &cval);
2768 last_tok = t;
2770 tok_str_add(&str, 0);
2771 return str.str;
2774 static char const ab_month_name[12][4] =
2776 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2777 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2780 /* do macro substitution of current token with macro 's' and add
2781 result to (tok_str,tok_len). 'nested_list' is the list of all
2782 macros we got inside to avoid recursing. Return non zero if no
2783 substitution needs to be done */
2784 static int macro_subst_tok(TokenString *tok_str,
2785 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2787 Sym *args, *sa, *sa1;
2788 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2789 const int *p;
2790 TokenString str;
2791 char *cstrval;
2792 CValue cval;
2793 CString cstr;
2794 char buf[32];
2796 /* if symbol is a macro, prepare substitution */
2797 /* special macros */
2798 if (tok == TOK___LINE__) {
2799 snprintf(buf, sizeof(buf), "%d", file->line_num);
2800 cstrval = buf;
2801 t1 = TOK_PPNUM;
2802 goto add_cstr1;
2803 } else if (tok == TOK___FILE__) {
2804 cstrval = file->filename;
2805 goto add_cstr;
2806 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2807 time_t ti;
2808 struct tm *tm;
2810 time(&ti);
2811 tm = localtime(&ti);
2812 if (tok == TOK___DATE__) {
2813 snprintf(buf, sizeof(buf), "%s %2d %d",
2814 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2815 } else {
2816 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2817 tm->tm_hour, tm->tm_min, tm->tm_sec);
2819 cstrval = buf;
2820 add_cstr:
2821 t1 = TOK_STR;
2822 add_cstr1:
2823 cstr_new(&cstr);
2824 cstr_cat(&cstr, cstrval);
2825 cstr_ccat(&cstr, '\0');
2826 cval.cstr = &cstr;
2827 tok_str_add2(tok_str, t1, &cval);
2828 cstr_free(&cstr);
2829 } else {
2830 mstr = s->d;
2831 mstr_allocated = 0;
2832 if (s->type.t == MACRO_FUNC) {
2833 /* NOTE: we do not use next_nomacro to avoid eating the
2834 next token. XXX: find better solution */
2835 redo:
2836 if (macro_ptr) {
2837 p = macro_ptr;
2838 while (is_space(t = *p) || TOK_LINEFEED == t)
2839 ++p;
2840 if (t == 0 && can_read_stream) {
2841 /* end of macro stream: we must look at the token
2842 after in the file */
2843 struct macro_level *ml = *can_read_stream;
2844 macro_ptr = NULL;
2845 if (ml)
2847 macro_ptr = ml->p;
2848 ml->p = NULL;
2849 *can_read_stream = ml -> prev;
2851 /* also, end of scope for nested defined symbol */
2852 (*nested_list)->v = -1;
2853 goto redo;
2855 } else {
2856 ch = file->buf_ptr[0];
2857 while (is_space(ch) || ch == '\n' || ch == '/')
2859 if (ch == '/')
2861 int c;
2862 uint8_t *p = file->buf_ptr;
2863 PEEKC(c, p);
2864 if (c == '*') {
2865 p = parse_comment(p);
2866 file->buf_ptr = p - 1;
2867 } else if (c == '/') {
2868 p = parse_line_comment(p);
2869 file->buf_ptr = p - 1;
2870 } else
2871 break;
2873 cinp();
2875 t = ch;
2877 if (t != '(') /* no macro subst */
2878 return -1;
2880 /* argument macro */
2881 next_nomacro();
2882 next_nomacro();
2883 args = NULL;
2884 sa = s->next;
2885 /* NOTE: empty args are allowed, except if no args */
2886 for(;;) {
2887 /* handle '()' case */
2888 if (!args && !sa && tok == ')')
2889 break;
2890 if (!sa)
2891 tcc_error("macro '%s' used with too many args",
2892 get_tok_str(s->v, 0));
2893 tok_str_new(&str);
2894 parlevel = spc = 0;
2895 /* NOTE: non zero sa->t indicates VA_ARGS */
2896 while ((parlevel > 0 ||
2897 (tok != ')' &&
2898 (tok != ',' || sa->type.t))) &&
2899 tok != -1) {
2900 if (tok == '(')
2901 parlevel++;
2902 else if (tok == ')')
2903 parlevel--;
2904 if (tok == TOK_LINEFEED)
2905 tok = ' ';
2906 if (!check_space(tok, &spc))
2907 tok_str_add2(&str, tok, &tokc);
2908 next_nomacro_spc();
2910 if (!str.len)
2911 tok_str_add(&str, TOK_PLCHLDR);
2912 str.len -= spc;
2913 tok_str_add(&str, 0);
2914 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2915 sa1->d = str.str;
2916 sa = sa->next;
2917 if (tok == ')') {
2918 /* special case for gcc var args: add an empty
2919 var arg argument if it is omitted */
2920 if (sa && sa->type.t && gnu_ext)
2921 continue;
2922 else
2923 break;
2925 if (tok != ',')
2926 expect(",");
2927 next_nomacro();
2929 if (sa) {
2930 tcc_error("macro '%s' used with too few args",
2931 get_tok_str(s->v, 0));
2934 /* now subst each arg */
2935 mstr = macro_arg_subst(nested_list, mstr, args);
2936 /* free memory */
2937 sa = args;
2938 while (sa) {
2939 sa1 = sa->prev;
2940 tok_str_free(sa->d);
2941 sym_free(sa);
2942 sa = sa1;
2944 mstr_allocated = 1;
2946 sym_push2(nested_list, s->v, 0, 0);
2947 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2948 /* pop nested defined symbol */
2949 sa1 = *nested_list;
2950 *nested_list = sa1->prev;
2951 sym_free(sa1);
2952 if (mstr_allocated)
2953 tok_str_free(mstr);
2955 return 0;
2958 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2959 return the resulting string (which must be freed). */
2960 static inline int *macro_twosharps(const int *macro_str)
2962 const int *ptr;
2963 int t;
2964 TokenString macro_str1;
2965 CString cstr;
2966 int n, start_of_nosubsts;
2968 /* we search the first '##' */
2969 for(ptr = macro_str;;) {
2970 CValue cval;
2971 TOK_GET(&t, &ptr, &cval);
2972 if (t == TOK_TWOSHARPS)
2973 break;
2974 /* nothing more to do if end of string */
2975 if (t == 0)
2976 return NULL;
2979 /* we saw '##', so we need more processing to handle it */
2980 start_of_nosubsts = -1;
2981 tok_str_new(&macro_str1);
2982 for(ptr = macro_str;;) {
2983 TOK_GET(&tok, &ptr, &tokc);
2984 if (tok == 0)
2985 break;
2986 if (tok == TOK_TWOSHARPS)
2987 continue;
2988 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2989 start_of_nosubsts = macro_str1.len;
2990 while (*ptr == TOK_TWOSHARPS) {
2991 /* given 'a##b', remove nosubsts preceding 'a' */
2992 if (start_of_nosubsts >= 0)
2993 macro_str1.len = start_of_nosubsts;
2994 /* given 'a##b', skip '##' */
2995 t = *++ptr;
2996 /* given 'a##b', remove nosubsts preceding 'b' */
2997 while (t == TOK_NOSUBST)
2998 t = *++ptr;
2999 if (t && t != TOK_TWOSHARPS) {
3000 CValue cval;
3001 TOK_GET(&t, &ptr, &cval);
3002 /* We concatenate the two tokens */
3003 cstr_new(&cstr);
3004 if (tok != TOK_PLCHLDR)
3005 cstr_cat(&cstr, get_tok_str(tok, &tokc));
3006 n = cstr.size;
3007 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
3008 cstr_cat(&cstr, get_tok_str(t, &cval));
3009 cstr_ccat(&cstr, '\0');
3011 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3012 memcpy(file->buffer, cstr.data, cstr.size);
3013 for (;;) {
3014 next_nomacro1();
3015 if (0 == *file->buf_ptr)
3016 break;
3017 tok_str_add2(&macro_str1, tok, &tokc);
3018 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
3019 n, cstr.data, (char*)cstr.data + n);
3021 tcc_close();
3022 cstr_free(&cstr);
3025 if (tok != TOK_NOSUBST) {
3026 tok_str_add2(&macro_str1, tok, &tokc);
3027 tok = ' ';
3028 start_of_nosubsts = -1;
3030 tok_str_add2(&macro_str1, tok, &tokc);
3032 tok_str_add(&macro_str1, 0);
3033 return macro_str1.str;
3037 /* do macro substitution of macro_str and add result to
3038 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3039 inside to avoid recursing. */
3040 static void macro_subst(TokenString *tok_str, Sym **nested_list,
3041 const int *macro_str, struct macro_level ** can_read_stream)
3043 Sym *s;
3044 int *macro_str1;
3045 const int *ptr;
3046 int t, ret, spc;
3047 CValue cval;
3048 struct macro_level ml;
3049 int force_blank;
3051 /* first scan for '##' operator handling */
3052 ptr = macro_str;
3053 macro_str1 = macro_twosharps(ptr);
3055 if (macro_str1)
3056 ptr = macro_str1;
3057 spc = 0;
3058 force_blank = 0;
3060 while (1) {
3061 /* NOTE: ptr == NULL can only happen if tokens are read from
3062 file stream due to a macro function call */
3063 if (ptr == NULL)
3064 break;
3065 TOK_GET(&t, &ptr, &cval);
3066 if (t == 0)
3067 break;
3068 if (t == TOK_NOSUBST) {
3069 /* following token has already been subst'd. just copy it on */
3070 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3071 TOK_GET(&t, &ptr, &cval);
3072 goto no_subst;
3074 s = define_find(t);
3075 if (s != NULL) {
3076 /* if nested substitution, do nothing */
3077 if (sym_find2(*nested_list, t)) {
3078 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3079 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3080 goto no_subst;
3082 ml.p = macro_ptr;
3083 if (can_read_stream)
3084 ml.prev = *can_read_stream, *can_read_stream = &ml;
3085 macro_ptr = (int *)ptr;
3086 tok = t;
3087 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3088 ptr = (int *)macro_ptr;
3089 macro_ptr = ml.p;
3090 if (can_read_stream && *can_read_stream == &ml)
3091 *can_read_stream = ml.prev;
3092 if (ret != 0)
3093 goto no_subst;
3094 if (parse_flags & PARSE_FLAG_SPACES)
3095 force_blank = 1;
3096 } else {
3097 no_subst:
3098 if (force_blank) {
3099 tok_str_add(tok_str, ' ');
3100 spc = 1;
3101 force_blank = 0;
3103 if (!check_space(t, &spc))
3104 tok_str_add2(tok_str, t, &cval);
3107 if (macro_str1)
3108 tok_str_free(macro_str1);
3111 /* return next token with macro substitution */
3112 ST_FUNC void next(void)
3114 Sym *nested_list, *s;
3115 TokenString str;
3116 struct macro_level *ml;
3118 redo:
3119 if (parse_flags & PARSE_FLAG_SPACES)
3120 next_nomacro_spc();
3121 else
3122 next_nomacro();
3123 if (!macro_ptr) {
3124 /* if not reading from macro substituted string, then try
3125 to substitute macros */
3126 if (tok >= TOK_IDENT &&
3127 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3128 s = define_find(tok);
3129 if (s) {
3130 /* we have a macro: we try to substitute */
3131 tok_str_new(&str);
3132 nested_list = NULL;
3133 ml = NULL;
3134 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3135 /* substitution done, NOTE: maybe empty */
3136 tok_str_add(&str, 0);
3137 macro_ptr = str.str;
3138 macro_ptr_allocated = str.str;
3139 goto redo;
3143 } else {
3144 if (tok == 0) {
3145 /* end of macro or end of unget buffer */
3146 if (unget_buffer_enabled) {
3147 macro_ptr = unget_saved_macro_ptr;
3148 unget_buffer_enabled = 0;
3149 } else {
3150 /* end of macro string: free it */
3151 tok_str_free(macro_ptr_allocated);
3152 macro_ptr_allocated = NULL;
3153 macro_ptr = NULL;
3155 goto redo;
3156 } else if (tok == TOK_NOSUBST) {
3157 /* discard preprocessor's nosubst markers */
3158 goto redo;
3162 /* convert preprocessor tokens into C tokens */
3163 if (tok == TOK_PPNUM &&
3164 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3165 parse_number((char *)tokc.cstr->data);
3169 /* push back current token and set current token to 'last_tok'. Only
3170 identifier case handled for labels. */
3171 ST_INLN void unget_tok(int last_tok)
3173 int i, n;
3174 int *q;
3175 if (unget_buffer_enabled)
3177 /* assert(macro_ptr == unget_saved_buffer + 1);
3178 assert(*macro_ptr == 0); */
3180 else
3182 unget_saved_macro_ptr = macro_ptr;
3183 unget_buffer_enabled = 1;
3185 q = unget_saved_buffer;
3186 macro_ptr = q;
3187 *q++ = tok;
3188 n = tok_ext_size(tok) - 1;
3189 for(i=0;i<n;i++)
3190 *q++ = tokc.tab[i];
3191 *q = 0; /* end of token string */
3192 tok = last_tok;
3196 /* better than nothing, but needs extension to handle '-E' option
3197 correctly too */
3198 ST_FUNC void preprocess_init(TCCState *s1)
3200 s1->include_stack_ptr = s1->include_stack;
3201 /* XXX: move that before to avoid having to initialize
3202 file->ifdef_stack_ptr ? */
3203 s1->ifdef_stack_ptr = s1->ifdef_stack;
3204 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3206 vtop = vstack - 1;
3207 s1->pack_stack[0] = 0;
3208 s1->pack_stack_ptr = s1->pack_stack;
3211 ST_FUNC void preprocess_new(void)
3213 int i, c;
3214 const char *p, *r;
3216 /* init isid table */
3217 for(i=CH_EOF;i<256;i++)
3218 isidnum_table[i-CH_EOF] = (isid(i) || isnum(i) ||
3219 (tcc_state->dollars_in_identifiers ? i == '$' : 0));
3221 /* add all tokens */
3222 if (table_ident) {
3223 tcc_free (table_ident);
3224 table_ident = NULL;
3226 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3228 tok_ident = TOK_IDENT;
3229 p = tcc_keywords;
3230 while (*p) {
3231 r = p;
3232 for(;;) {
3233 c = *r++;
3234 if (c == '\0')
3235 break;
3237 tok_alloc(p, r - p - 1);
3238 p = r;
3242 static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3244 switch (s1->Pflag) {
3245 case LINE_MACRO_OUTPUT_FORMAT_STD:
3246 /* "tcc -E -P1" case */
3247 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3248 break;
3250 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3251 /* "tcc -E -P" case: don't output a line directive */
3252 break;
3254 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3255 default:
3256 /* "tcc -E" case: a gcc standard by default */
3257 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3258 break;
3262 /* Preprocess the current file */
3263 ST_FUNC int tcc_preprocess(TCCState *s1)
3265 BufferedFile *file_ref, **iptr, **iptr_new;
3266 int token_seen, d;
3267 const char *s;
3269 preprocess_init(s1);
3270 ch = file->buf_ptr[0];
3271 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3272 parse_flags = (parse_flags & PARSE_FLAG_ASM_FILE);
3273 parse_flags |= PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3274 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3275 token_seen = 0;
3276 file->line_ref = 0;
3277 file_ref = NULL;
3278 iptr = s1->include_stack_ptr;
3280 for (;;) {
3281 next();
3282 if (tok == TOK_EOF) {
3283 break;
3284 } else if (file != file_ref) {
3285 if (file_ref)
3286 line_macro_output(file_ref, "", s1);
3287 goto print_line;
3288 } else if (tok == TOK_LINEFEED) {
3289 if (!token_seen)
3290 continue;
3291 file->line_ref++;
3292 token_seen = 0;
3293 } else if (!token_seen) {
3294 d = file->line_num - file->line_ref;
3295 if (file != file_ref || d >= 8) {
3296 print_line:
3297 s = "";
3298 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3299 iptr_new = s1->include_stack_ptr;
3300 s = iptr_new > iptr ? " 1"
3301 : iptr_new < iptr ? " 2"
3302 : iptr_new > s1->include_stack ? " 3"
3303 : ""
3306 line_macro_output(file, s, s1);
3307 } else {
3308 while (d > 0)
3309 fputs("\n", s1->ppfp), --d;
3311 file->line_ref = (file_ref = file)->line_num;
3312 token_seen = tok != TOK_LINEFEED;
3313 if (!token_seen)
3314 continue;
3316 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3318 return 0;