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