fixes for "tcc -E -dD"
[tinycc.git] / tccpp.c
blob921d589628e89a0965748ddd8f837e0ca46c43f1
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", 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));
1091 /* defines handling */
1092 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1094 Sym *s;
1096 s = define_find(v);
1097 if (s && !macro_is_equal(s->d, str))
1098 tcc_warning("%s redefined", get_tok_str(v, NULL));
1100 s = sym_push2(&define_stack, v, macro_type, 0);
1101 s->d = str;
1102 s->next = first_arg;
1103 table_ident[v - TOK_IDENT]->sym_define = s;
1104 define_print(s, 0);
1107 /* undefined a define symbol. Its name is just set to zero */
1108 ST_FUNC void define_undef(Sym *s)
1110 int v;
1111 v = s->v;
1112 if (v >= TOK_IDENT && v < tok_ident) {
1113 define_print(s, 1);
1114 table_ident[v - TOK_IDENT]->sym_define = NULL;
1118 ST_INLN Sym *define_find(int v)
1120 v -= TOK_IDENT;
1121 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1122 return NULL;
1123 return table_ident[v]->sym_define;
1126 /* free define stack until top reaches 'b' */
1127 ST_FUNC void free_defines(Sym *b)
1129 Sym *top, *top1;
1130 int v;
1132 top = define_stack;
1133 while (top != b) {
1134 top1 = top->prev;
1135 /* do not free args or predefined defines */
1136 if (top->d)
1137 tok_str_free(top->d);
1138 v = top->v;
1139 if (v >= TOK_IDENT && v < tok_ident)
1140 table_ident[v - TOK_IDENT]->sym_define = NULL;
1141 sym_free(top);
1142 top = top1;
1144 define_stack = b;
1147 ST_FUNC void print_defines(void)
1149 Sym *top, *s;
1150 int v;
1152 top = define_stack;
1153 while (top) {
1154 v = top->v;
1155 if (v >= TOK_IDENT && v < tok_ident) {
1156 s = table_ident[v - TOK_IDENT]->sym_define;
1157 define_print(s, 0);
1159 top = top->prev;
1163 /* label lookup */
1164 ST_FUNC Sym *label_find(int v)
1166 v -= TOK_IDENT;
1167 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1168 return NULL;
1169 return table_ident[v]->sym_label;
1172 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1174 Sym *s, **ps;
1175 s = sym_push2(ptop, v, 0, 0);
1176 s->r = flags;
1177 ps = &table_ident[v - TOK_IDENT]->sym_label;
1178 if (ptop == &global_label_stack) {
1179 /* modify the top most local identifier, so that
1180 sym_identifier will point to 's' when popped */
1181 while (*ps != NULL)
1182 ps = &(*ps)->prev_tok;
1184 s->prev_tok = *ps;
1185 *ps = s;
1186 return s;
1189 /* pop labels until element last is reached. Look if any labels are
1190 undefined. Define symbols if '&&label' was used. */
1191 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1193 Sym *s, *s1;
1194 for(s = *ptop; s != slast; s = s1) {
1195 s1 = s->prev;
1196 if (s->r == LABEL_DECLARED) {
1197 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1198 } else if (s->r == LABEL_FORWARD) {
1199 tcc_error("label '%s' used but not defined",
1200 get_tok_str(s->v, NULL));
1201 } else {
1202 if (s->c) {
1203 /* define corresponding symbol. A size of
1204 1 is put. */
1205 put_extern_sym(s, cur_text_section, s->jnext, 1);
1208 /* remove label */
1209 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1210 sym_free(s);
1212 *ptop = slast;
1215 /* eval an expression for #if/#elif */
1216 static int expr_preprocess(void)
1218 int c, t;
1219 TokenString str;
1221 tok_str_new(&str);
1222 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1223 next(); /* do macro subst */
1224 if (tok == TOK_DEFINED) {
1225 next_nomacro();
1226 t = tok;
1227 if (t == '(')
1228 next_nomacro();
1229 c = define_find(tok) != 0;
1230 if (t == '(')
1231 next_nomacro();
1232 tok = TOK_CINT;
1233 tokc.i = c;
1234 } else if (tok >= TOK_IDENT) {
1235 /* if undefined macro */
1236 tok = TOK_CINT;
1237 tokc.i = 0;
1239 tok_str_add_tok(&str);
1241 tok_str_add(&str, -1); /* simulate end of file */
1242 tok_str_add(&str, 0);
1243 /* now evaluate C constant expression */
1244 macro_ptr = str.str;
1245 next();
1246 c = expr_const();
1247 macro_ptr = NULL;
1248 tok_str_free(str.str);
1249 return c != 0;
1252 /* parse after #define */
1253 ST_FUNC void parse_define(void)
1255 Sym *s, *first, **ps;
1256 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1257 TokenString str;
1259 v = tok;
1260 if (v < TOK_IDENT)
1261 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1262 /* XXX: should check if same macro (ANSI) */
1263 first = NULL;
1264 t = MACRO_OBJ;
1265 /* '(' must be just after macro definition for MACRO_FUNC */
1266 next_nomacro_spc();
1267 if (tok == '(') {
1268 next_nomacro();
1269 ps = &first;
1270 while (tok != ')') {
1271 varg = tok;
1272 next_nomacro();
1273 is_vaargs = 0;
1274 if (varg == TOK_DOTS) {
1275 varg = TOK___VA_ARGS__;
1276 is_vaargs = 1;
1277 } else if (tok == TOK_DOTS && gnu_ext) {
1278 is_vaargs = 1;
1279 next_nomacro();
1281 if (varg < TOK_IDENT)
1282 tcc_error( "\'%s\' may not appear in parameter list", get_tok_str(varg, NULL));
1283 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1284 *ps = s;
1285 ps = &s->next;
1286 if (tok != ',')
1287 continue;
1288 next_nomacro();
1290 next_nomacro_spc();
1291 t = MACRO_FUNC;
1293 tok_str_new(&str);
1294 spc = 2;
1295 /* EOF testing necessary for '-D' handling */
1296 ptok = 0;
1297 macro_list_start = 1;
1298 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1299 if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1300 tcc_error("'##' invalid at start of macro");
1301 ptok = tok;
1302 /* remove spaces around ## and after '#' */
1303 if (TOK_TWOSHARPS == tok) {
1304 if (1 == spc)
1305 --str.len;
1306 spc = 2;
1307 } else if ('#' == tok) {
1308 spc = 2;
1309 } else if (check_space(tok, &spc)) {
1310 goto skip;
1312 tok_str_add2(&str, tok, &tokc);
1313 skip:
1314 next_nomacro_spc();
1315 macro_list_start = 0;
1317 if (ptok == TOK_TWOSHARPS)
1318 tcc_error("'##' invalid at end of macro");
1319 if (spc == 1)
1320 --str.len; /* remove trailing space */
1321 tok_str_add(&str, 0);
1322 define_push(v, t, str.str, first);
1325 static inline int hash_cached_include(const char *filename)
1327 const unsigned char *s;
1328 unsigned int h;
1330 h = TOK_HASH_INIT;
1331 s = (unsigned char *) filename;
1332 while (*s) {
1333 h = TOK_HASH_FUNC(h, *s);
1334 s++;
1336 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1337 return h;
1340 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1342 CachedInclude *e;
1343 int i, h;
1344 h = hash_cached_include(filename);
1345 i = s1->cached_includes_hash[h];
1346 for(;;) {
1347 if (i == 0)
1348 break;
1349 e = s1->cached_includes[i - 1];
1350 if (0 == PATHCMP(e->filename, filename))
1351 return e;
1352 i = e->hash_next;
1354 return NULL;
1357 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1359 CachedInclude *e;
1360 int h;
1362 if (search_cached_include(s1, filename))
1363 return;
1364 #ifdef INC_DEBUG
1365 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1366 #endif
1367 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1368 strcpy(e->filename, filename);
1369 e->ifndef_macro = ifndef_macro;
1370 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1371 /* add in hash table */
1372 h = hash_cached_include(filename);
1373 e->hash_next = s1->cached_includes_hash[h];
1374 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1377 static void pragma_parse(TCCState *s1)
1379 next_nomacro();
1380 if (tok == TOK_pack) {
1382 This may be:
1383 #pragma pack(1) // set
1384 #pragma pack() // reset to default
1385 #pragma pack(push,1) // push & set
1386 #pragma pack(pop) // restore previous
1388 next();
1389 skip('(');
1390 if (tok == TOK_ASM_pop) {
1391 next();
1392 if (s1->pack_stack_ptr <= s1->pack_stack) {
1393 stk_error:
1394 tcc_error("out of pack stack");
1396 s1->pack_stack_ptr--;
1397 } else {
1398 int val = 0;
1399 if (tok != ')') {
1400 if (tok == TOK_ASM_push) {
1401 next();
1402 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1403 goto stk_error;
1404 s1->pack_stack_ptr++;
1405 skip(',');
1407 if (tok != TOK_CINT)
1408 goto pragma_err;
1409 val = tokc.i;
1410 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1411 goto pragma_err;
1412 next();
1414 *s1->pack_stack_ptr = val;
1416 if (tok != ')')
1417 goto pragma_err;
1418 } else if (tok == TOK_comment) {
1419 if (s1->ms_extensions) {
1420 next();
1421 skip('(');
1422 if (tok == TOK_lib) {
1423 next();
1424 skip(',');
1425 if (tok != TOK_STR) {
1426 tcc_error("invalid library specification");
1427 } else {
1428 int len = strlen((char *)tokc.cstr->data);
1429 char *file = tcc_malloc(len + 4); /* filetype, "-l", and \0 at the end */
1430 file[0] = TCC_FILETYPE_BINARY;
1431 file[1] = '-';
1432 file[2] = 'l';
1433 strcpy(&file[3],(char *)tokc.cstr->data);
1434 dynarray_add((void ***)&s1->files, &s1->nb_files, file);
1436 next();
1437 tok = TOK_LINEFEED;
1438 } else {
1439 tcc_warning("unknown specifier '%s' in #pragma comment", get_tok_str(tok, &tokc));
1441 } else {
1442 tcc_warning("#pragma comment(lib) is ignored");
1444 } else if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1445 int t = tok, v;
1446 Sym *s;
1448 if (next_nomacro(), tok != '(')
1449 goto pragma_err;
1450 if (next_nomacro(), tok != TOK_STR)
1451 goto pragma_err;
1452 v = tok_alloc(tokc.cstr->data, tokc.cstr->size - 1)->tok;
1453 if (next_nomacro(), tok != ')')
1454 goto pragma_err;
1455 if (t == TOK_push_macro) {
1456 while (NULL == (s = define_find(v)))
1457 define_push(v, 0, NULL, NULL);
1458 s->type.ref = s; /* set push boundary */
1459 } else {
1460 for (s = define_stack; s; s = s->prev)
1461 if (s->v == v && s->type.ref == s) {
1462 s->type.ref = NULL;
1463 break;
1466 if (s)
1467 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1468 else
1469 tcc_warning("unbalanced #pragma pop_macro");
1471 /* print info when tcc is called with "-E -dD" switches */
1472 if (s1->dflag && s1->ppfp) {
1473 if (file) {
1474 int c = file->line_num - file->line_ref - 1;
1475 if (c > 0) {
1476 while (c--)
1477 fputs("\n", tcc_state->ppfp);
1478 file->line_ref = file->line_num;
1481 fprintf(s1->ppfp, "// #pragma %s_macro(\"%s\")",
1482 (t == TOK_push_macro) ? "push" : "pop",
1483 get_tok_str(v, NULL));
1485 } else if (tok == TOK_once) {
1486 add_cached_include(s1, file->filename, TOK_once);
1487 } else {
1488 tcc_warning("unknown #pragma %s", get_tok_str(tok, &tokc));
1490 return;
1491 pragma_err:
1492 tcc_error("malformed #pragma directive");
1495 /* is_bof is true if first non space token at beginning of file */
1496 ST_FUNC void preprocess(int is_bof)
1498 TCCState *s1 = tcc_state;
1499 int i, c, n, saved_parse_flags;
1500 char buf[1024], *q;
1501 Sym *s;
1503 saved_parse_flags = parse_flags;
1504 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1505 PARSE_FLAG_LINEFEED;
1506 parse_flags |= (saved_parse_flags & (PARSE_FLAG_ASM_FILE | PARSE_FLAG_ASM_COMMENTS));
1507 next_nomacro();
1508 redo:
1509 switch(tok) {
1510 case TOK_DEFINE:
1511 next_nomacro();
1512 parse_define();
1513 break;
1514 case TOK_UNDEF:
1515 next_nomacro();
1516 s = define_find(tok);
1517 /* undefine symbol by putting an invalid name */
1518 if (s)
1519 define_undef(s);
1520 break;
1521 case TOK_INCLUDE:
1522 case TOK_INCLUDE_NEXT:
1523 ch = file->buf_ptr[0];
1524 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1525 skip_spaces();
1526 if (ch == '<') {
1527 c = '>';
1528 goto read_name;
1529 } else if (ch == '\"') {
1530 c = ch;
1531 read_name:
1532 inp();
1533 q = buf;
1534 while (ch != c && ch != '\n' && ch != CH_EOF) {
1535 if ((q - buf) < sizeof(buf) - 1)
1536 *q++ = ch;
1537 if (ch == '\\') {
1538 if (handle_stray_noerror() == 0)
1539 --q;
1540 } else
1541 inp();
1543 *q = '\0';
1544 minp();
1545 #if 0
1546 /* eat all spaces and comments after include */
1547 /* XXX: slightly incorrect */
1548 while (ch1 != '\n' && ch1 != CH_EOF)
1549 inp();
1550 #endif
1551 } else {
1552 /* computed #include : either we have only strings or
1553 we have anything enclosed in '<>' */
1554 next();
1555 buf[0] = '\0';
1556 if (tok == TOK_STR) {
1557 while (tok != TOK_LINEFEED) {
1558 if (tok != TOK_STR) {
1559 include_syntax:
1560 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1562 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1563 next();
1565 c = '\"';
1566 } else {
1567 int len;
1568 while (tok != TOK_LINEFEED) {
1569 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1570 next();
1572 len = strlen(buf);
1573 /* check syntax and remove '<>' */
1574 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1575 goto include_syntax;
1576 memmove(buf, buf + 1, len - 2);
1577 buf[len - 2] = '\0';
1578 c = '>';
1582 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1583 tcc_error("#include recursion too deep");
1584 /* store current file in stack, but increment stack later below */
1585 *s1->include_stack_ptr = file;
1587 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1588 for (i = -2; i < n; ++i) {
1589 char buf1[sizeof file->filename];
1590 CachedInclude *e;
1591 BufferedFile **f;
1592 const char *path;
1594 if (i == -2) {
1595 /* check absolute include path */
1596 if (!IS_ABSPATH(buf))
1597 continue;
1598 buf1[0] = 0;
1599 i = n; /* force end loop */
1601 } else if (i == -1) {
1602 /* search in current dir if "header.h" */
1603 if (c != '\"')
1604 continue;
1605 path = file->filename;
1606 pstrncpy(buf1, path, tcc_basename(path) - path);
1608 } else {
1609 /* search in all the include paths */
1610 if (i < s1->nb_include_paths)
1611 path = s1->include_paths[i];
1612 else
1613 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1614 pstrcpy(buf1, sizeof(buf1), path);
1615 pstrcat(buf1, sizeof(buf1), "/");
1618 pstrcat(buf1, sizeof(buf1), buf);
1620 if (tok == TOK_INCLUDE_NEXT)
1621 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1622 if (0 == PATHCMP((*f)->filename, buf1)) {
1623 #ifdef INC_DEBUG
1624 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1625 #endif
1626 goto include_trynext;
1629 e = search_cached_include(s1, buf1);
1630 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
1631 /* no need to parse the include because the 'ifndef macro'
1632 is defined */
1633 #ifdef INC_DEBUG
1634 printf("%s: skipping cached %s\n", file->filename, buf1);
1635 #endif
1636 goto include_done;
1639 if (tcc_open(s1, buf1) < 0)
1640 include_trynext:
1641 continue;
1643 #ifdef INC_DEBUG
1644 printf("%s: including %s\n", file->prev->filename, file->filename);
1645 #endif
1646 /* update target deps */
1647 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1648 tcc_strdup(buf1));
1649 /* push current file in stack */
1650 ++s1->include_stack_ptr;
1651 /* add include file debug info */
1652 if (s1->do_debug)
1653 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1654 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1655 ch = file->buf_ptr[0];
1656 goto the_end;
1658 tcc_error("include file '%s' not found", buf);
1659 include_done:
1660 break;
1661 case TOK_IFNDEF:
1662 c = 1;
1663 goto do_ifdef;
1664 case TOK_IF:
1665 c = expr_preprocess();
1666 goto do_if;
1667 case TOK_IFDEF:
1668 c = 0;
1669 do_ifdef:
1670 next_nomacro();
1671 if (tok < TOK_IDENT)
1672 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1673 if (is_bof) {
1674 if (c) {
1675 #ifdef INC_DEBUG
1676 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1677 #endif
1678 file->ifndef_macro = tok;
1681 c = (define_find(tok) != 0) ^ c;
1682 do_if:
1683 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1684 tcc_error("memory full (ifdef)");
1685 *s1->ifdef_stack_ptr++ = c;
1686 goto test_skip;
1687 case TOK_ELSE:
1688 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1689 tcc_error("#else without matching #if");
1690 if (s1->ifdef_stack_ptr[-1] & 2)
1691 tcc_error("#else after #else");
1692 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1693 goto test_else;
1694 case TOK_ELIF:
1695 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1696 tcc_error("#elif without matching #if");
1697 c = s1->ifdef_stack_ptr[-1];
1698 if (c > 1)
1699 tcc_error("#elif after #else");
1700 /* last #if/#elif expression was true: we skip */
1701 if (c == 1)
1702 goto skip;
1703 c = expr_preprocess();
1704 s1->ifdef_stack_ptr[-1] = c;
1705 test_else:
1706 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1707 file->ifndef_macro = 0;
1708 test_skip:
1709 if (!(c & 1)) {
1710 skip:
1711 preprocess_skip();
1712 is_bof = 0;
1713 goto redo;
1715 break;
1716 case TOK_ENDIF:
1717 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1718 tcc_error("#endif without matching #if");
1719 s1->ifdef_stack_ptr--;
1720 /* '#ifndef macro' was at the start of file. Now we check if
1721 an '#endif' is exactly at the end of file */
1722 if (file->ifndef_macro &&
1723 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1724 file->ifndef_macro_saved = file->ifndef_macro;
1725 /* need to set to zero to avoid false matches if another
1726 #ifndef at middle of file */
1727 file->ifndef_macro = 0;
1728 while (tok != TOK_LINEFEED)
1729 next_nomacro();
1730 tok_flags |= TOK_FLAG_ENDIF;
1731 goto the_end;
1733 break;
1734 case TOK_LINE:
1735 next();
1736 if (tok != TOK_CINT)
1737 tcc_error("A #line format is wrong");
1738 case TOK_PPNUM:
1739 if (tok != TOK_CINT) {
1740 char *p = tokc.cstr->data;
1741 tokc.i = strtoul(p, (char **)&p, 10);
1743 i = file->line_num;
1744 file->line_num = tokc.i - 1;
1745 next();
1746 if (tok != TOK_LINEFEED) {
1747 if (tok != TOK_STR) {
1748 if ((parse_flags & PARSE_FLAG_ASM_COMMENTS) == 0) {
1749 file->line_num = i;
1750 tcc_error("#line format is wrong");
1752 break;
1754 pstrcpy(file->filename, sizeof(file->filename),
1755 (char *)tokc.cstr->data);
1757 if (s1->do_debug)
1758 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1759 break;
1760 case TOK_ERROR:
1761 case TOK_WARNING:
1762 c = tok;
1763 ch = file->buf_ptr[0];
1764 skip_spaces();
1765 q = buf;
1766 while (ch != '\n' && ch != CH_EOF) {
1767 if ((q - buf) < sizeof(buf) - 1)
1768 *q++ = ch;
1769 if (ch == '\\') {
1770 if (handle_stray_noerror() == 0)
1771 --q;
1772 } else
1773 inp();
1775 *q = '\0';
1776 if (c == TOK_ERROR)
1777 tcc_error("#error %s", buf);
1778 else
1779 tcc_warning("#warning %s", buf);
1780 break;
1781 case TOK_PRAGMA:
1782 pragma_parse(s1);
1783 break;
1784 default:
1785 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1786 /* '!' is ignored to allow C scripts. numbers are ignored
1787 to emulate cpp behaviour */
1788 } else {
1789 if (!(parse_flags & PARSE_FLAG_ASM_COMMENTS))
1790 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1791 else {
1792 /* this is a gas line comment in an 'S' file. */
1793 file->buf_ptr = parse_line_comment(file->buf_ptr);
1794 goto the_end;
1797 break;
1799 /* ignore other preprocess commands or #! for C scripts */
1800 while (tok != TOK_LINEFEED)
1801 next_nomacro();
1802 the_end:
1803 parse_flags = saved_parse_flags;
1806 /* evaluate escape codes in a string. */
1807 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1809 int c, n;
1810 const uint8_t *p;
1812 p = buf;
1813 for(;;) {
1814 c = *p;
1815 if (c == '\0')
1816 break;
1817 if (c == '\\') {
1818 p++;
1819 /* escape */
1820 c = *p;
1821 switch(c) {
1822 case '0': case '1': case '2': case '3':
1823 case '4': case '5': case '6': case '7':
1824 /* at most three octal digits */
1825 n = c - '0';
1826 p++;
1827 c = *p;
1828 if (isoct(c)) {
1829 n = n * 8 + c - '0';
1830 p++;
1831 c = *p;
1832 if (isoct(c)) {
1833 n = n * 8 + c - '0';
1834 p++;
1837 c = n;
1838 goto add_char_nonext;
1839 case 'x':
1840 case 'u':
1841 case 'U':
1842 p++;
1843 n = 0;
1844 for(;;) {
1845 c = *p;
1846 if (c >= 'a' && c <= 'f')
1847 c = c - 'a' + 10;
1848 else if (c >= 'A' && c <= 'F')
1849 c = c - 'A' + 10;
1850 else if (isnum(c))
1851 c = c - '0';
1852 else
1853 break;
1854 n = n * 16 + c;
1855 p++;
1857 c = n;
1858 goto add_char_nonext;
1859 case 'a':
1860 c = '\a';
1861 break;
1862 case 'b':
1863 c = '\b';
1864 break;
1865 case 'f':
1866 c = '\f';
1867 break;
1868 case 'n':
1869 c = '\n';
1870 break;
1871 case 'r':
1872 c = '\r';
1873 break;
1874 case 't':
1875 c = '\t';
1876 break;
1877 case 'v':
1878 c = '\v';
1879 break;
1880 case 'e':
1881 if (!gnu_ext)
1882 goto invalid_escape;
1883 c = 27;
1884 break;
1885 case '\'':
1886 case '\"':
1887 case '\\':
1888 case '?':
1889 break;
1890 default:
1891 invalid_escape:
1892 if (c >= '!' && c <= '~')
1893 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1894 else
1895 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1896 break;
1899 p++;
1900 add_char_nonext:
1901 if (!is_long)
1902 cstr_ccat(outstr, c);
1903 else
1904 cstr_wccat(outstr, c);
1906 /* add a trailing '\0' */
1907 if (!is_long)
1908 cstr_ccat(outstr, '\0');
1909 else
1910 cstr_wccat(outstr, '\0');
1913 /* we use 64 bit numbers */
1914 #define BN_SIZE 2
1916 /* bn = (bn << shift) | or_val */
1917 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1919 int i;
1920 unsigned int v;
1921 for(i=0;i<BN_SIZE;i++) {
1922 v = bn[i];
1923 bn[i] = (v << shift) | or_val;
1924 or_val = v >> (32 - shift);
1928 static void bn_zero(unsigned int *bn)
1930 int i;
1931 for(i=0;i<BN_SIZE;i++) {
1932 bn[i] = 0;
1936 /* parse number in null terminated string 'p' and return it in the
1937 current token */
1938 static void parse_number(const char *p)
1940 int b, t, shift, frac_bits, s, exp_val, ch;
1941 char *q;
1942 unsigned int bn[BN_SIZE];
1943 double d;
1945 /* number */
1946 q = token_buf;
1947 ch = *p++;
1948 t = ch;
1949 ch = *p++;
1950 *q++ = t;
1951 b = 10;
1952 if (t == '.') {
1953 goto float_frac_parse;
1954 } else if (t == '0') {
1955 if (ch == 'x' || ch == 'X') {
1956 q--;
1957 ch = *p++;
1958 b = 16;
1959 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1960 q--;
1961 ch = *p++;
1962 b = 2;
1965 /* parse all digits. cannot check octal numbers at this stage
1966 because of floating point constants */
1967 while (1) {
1968 if (ch >= 'a' && ch <= 'f')
1969 t = ch - 'a' + 10;
1970 else if (ch >= 'A' && ch <= 'F')
1971 t = ch - 'A' + 10;
1972 else if (isnum(ch))
1973 t = ch - '0';
1974 else
1975 break;
1976 if (t >= b)
1977 break;
1978 if (q >= token_buf + STRING_MAX_SIZE) {
1979 num_too_long:
1980 tcc_error("number too long");
1982 *q++ = ch;
1983 ch = *p++;
1985 if (ch == '.' ||
1986 ((ch == 'e' || ch == 'E') && b == 10) ||
1987 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1988 if (b != 10) {
1989 /* NOTE: strtox should support that for hexa numbers, but
1990 non ISOC99 libcs do not support it, so we prefer to do
1991 it by hand */
1992 /* hexadecimal or binary floats */
1993 /* XXX: handle overflows */
1994 *q = '\0';
1995 if (b == 16)
1996 shift = 4;
1997 else
1998 shift = 1;
1999 bn_zero(bn);
2000 q = token_buf;
2001 while (1) {
2002 t = *q++;
2003 if (t == '\0') {
2004 break;
2005 } else if (t >= 'a') {
2006 t = t - 'a' + 10;
2007 } else if (t >= 'A') {
2008 t = t - 'A' + 10;
2009 } else {
2010 t = t - '0';
2012 bn_lshift(bn, shift, t);
2014 frac_bits = 0;
2015 if (ch == '.') {
2016 ch = *p++;
2017 while (1) {
2018 t = ch;
2019 if (t >= 'a' && t <= 'f') {
2020 t = t - 'a' + 10;
2021 } else if (t >= 'A' && t <= 'F') {
2022 t = t - 'A' + 10;
2023 } else if (t >= '0' && t <= '9') {
2024 t = t - '0';
2025 } else {
2026 break;
2028 if (t >= b)
2029 tcc_error("invalid digit");
2030 bn_lshift(bn, shift, t);
2031 frac_bits += shift;
2032 ch = *p++;
2035 if (ch != 'p' && ch != 'P')
2036 expect("exponent");
2037 ch = *p++;
2038 s = 1;
2039 exp_val = 0;
2040 if (ch == '+') {
2041 ch = *p++;
2042 } else if (ch == '-') {
2043 s = -1;
2044 ch = *p++;
2046 if (ch < '0' || ch > '9')
2047 expect("exponent digits");
2048 while (ch >= '0' && ch <= '9') {
2049 exp_val = exp_val * 10 + ch - '0';
2050 ch = *p++;
2052 exp_val = exp_val * s;
2054 /* now we can generate the number */
2055 /* XXX: should patch directly float number */
2056 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2057 d = ldexp(d, exp_val - frac_bits);
2058 t = toup(ch);
2059 if (t == 'F') {
2060 ch = *p++;
2061 tok = TOK_CFLOAT;
2062 /* float : should handle overflow */
2063 tokc.f = (float)d;
2064 } else if (t == 'L') {
2065 ch = *p++;
2066 #ifdef TCC_TARGET_PE
2067 tok = TOK_CDOUBLE;
2068 tokc.d = d;
2069 #else
2070 tok = TOK_CLDOUBLE;
2071 /* XXX: not large enough */
2072 tokc.ld = (long double)d;
2073 #endif
2074 } else {
2075 tok = TOK_CDOUBLE;
2076 tokc.d = d;
2078 } else {
2079 /* decimal floats */
2080 if (ch == '.') {
2081 if (q >= token_buf + STRING_MAX_SIZE)
2082 goto num_too_long;
2083 *q++ = ch;
2084 ch = *p++;
2085 float_frac_parse:
2086 while (ch >= '0' && ch <= '9') {
2087 if (q >= token_buf + STRING_MAX_SIZE)
2088 goto num_too_long;
2089 *q++ = ch;
2090 ch = *p++;
2093 if (ch == 'e' || ch == 'E') {
2094 if (q >= token_buf + STRING_MAX_SIZE)
2095 goto num_too_long;
2096 *q++ = ch;
2097 ch = *p++;
2098 if (ch == '-' || ch == '+') {
2099 if (q >= token_buf + STRING_MAX_SIZE)
2100 goto num_too_long;
2101 *q++ = ch;
2102 ch = *p++;
2104 if (ch < '0' || ch > '9')
2105 expect("exponent digits");
2106 while (ch >= '0' && ch <= '9') {
2107 if (q >= token_buf + STRING_MAX_SIZE)
2108 goto num_too_long;
2109 *q++ = ch;
2110 ch = *p++;
2113 *q = '\0';
2114 t = toup(ch);
2115 errno = 0;
2116 if (t == 'F') {
2117 ch = *p++;
2118 tok = TOK_CFLOAT;
2119 tokc.f = strtof(token_buf, NULL);
2120 } else if (t == 'L') {
2121 ch = *p++;
2122 #ifdef TCC_TARGET_PE
2123 tok = TOK_CDOUBLE;
2124 tokc.d = strtod(token_buf, NULL);
2125 #else
2126 tok = TOK_CLDOUBLE;
2127 tokc.ld = strtold(token_buf, NULL);
2128 #endif
2129 } else {
2130 tok = TOK_CDOUBLE;
2131 tokc.d = strtod(token_buf, NULL);
2134 } else {
2135 unsigned long long n, n1;
2136 int lcount, ucount, must_64bit;
2137 const char *p1;
2139 /* integer number */
2140 *q = '\0';
2141 q = token_buf;
2142 if (b == 10 && *q == '0') {
2143 b = 8;
2144 q++;
2146 n = 0;
2147 while(1) {
2148 t = *q++;
2149 /* no need for checks except for base 10 / 8 errors */
2150 if (t == '\0')
2151 break;
2152 else if (t >= 'a')
2153 t = t - 'a' + 10;
2154 else if (t >= 'A')
2155 t = t - 'A' + 10;
2156 else
2157 t = t - '0';
2158 if (t >= b)
2159 tcc_error("invalid digit");
2160 n1 = n;
2161 n = n * b + t;
2162 /* detect overflow */
2163 /* XXX: this test is not reliable */
2164 if (n < n1)
2165 tcc_error("integer constant overflow");
2168 /* Determine the characteristics (unsigned and/or 64bit) the type of
2169 the constant must have according to the constant suffix(es) */
2170 lcount = ucount = must_64bit = 0;
2171 p1 = p;
2172 for(;;) {
2173 t = toup(ch);
2174 if (t == 'L') {
2175 if (lcount >= 2)
2176 tcc_error("three 'l's in integer constant");
2177 if (lcount && *(p - 1) != ch)
2178 tcc_error("incorrect integer suffix: %s", p1);
2179 lcount++;
2180 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2181 if (lcount == 2)
2182 #endif
2183 must_64bit = 1;
2184 ch = *p++;
2185 } else if (t == 'U') {
2186 if (ucount >= 1)
2187 tcc_error("two 'u's in integer constant");
2188 ucount++;
2189 ch = *p++;
2190 } else {
2191 break;
2195 /* Whether 64 bits are needed to hold the constant's value */
2196 if (n & 0xffffffff00000000LL || must_64bit) {
2197 tok = TOK_CLLONG;
2198 n1 = n >> 32;
2199 } else {
2200 tok = TOK_CINT;
2201 n1 = n;
2204 /* Whether type must be unsigned to hold the constant's value */
2205 if (ucount || ((n1 >> 31) && (b != 10))) {
2206 if (tok == TOK_CLLONG)
2207 tok = TOK_CULLONG;
2208 else
2209 tok = TOK_CUINT;
2210 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2211 } else if (n1 >> 31) {
2212 if (tok == TOK_CINT)
2213 tok = TOK_CLLONG;
2214 else
2215 tcc_error("integer constant overflow");
2218 if (tok == TOK_CINT || tok == TOK_CUINT)
2219 tokc.ui = n;
2220 else
2221 tokc.ull = n;
2223 if (ch)
2224 tcc_error("invalid number\n");
2228 #define PARSE2(c1, tok1, c2, tok2) \
2229 case c1: \
2230 PEEKC(c, p); \
2231 if (c == c2) { \
2232 p++; \
2233 tok = tok2; \
2234 } else { \
2235 tok = tok1; \
2237 break;
2239 /* return next token without macro substitution */
2240 static inline void next_nomacro1(void)
2242 int t, c, is_long;
2243 TokenSym *ts;
2244 uint8_t *p, *p1;
2245 unsigned int h;
2247 p = file->buf_ptr;
2248 redo_no_start:
2249 c = *p;
2250 switch(c) {
2251 case ' ':
2252 case '\t':
2253 tok = c;
2254 p++;
2255 goto keep_tok_flags;
2256 case '\f':
2257 case '\v':
2258 case '\r':
2259 p++;
2260 goto redo_no_start;
2261 case '\\':
2262 /* first look if it is in fact an end of buffer */
2263 if (p >= file->buf_end) {
2264 file->buf_ptr = p;
2265 handle_eob();
2266 p = file->buf_ptr;
2267 if (p >= file->buf_end)
2268 goto parse_eof;
2269 else
2270 goto redo_no_start;
2271 } else {
2272 file->buf_ptr = p;
2273 ch = *p;
2274 handle_stray();
2275 p = file->buf_ptr;
2276 goto redo_no_start;
2278 parse_eof:
2280 TCCState *s1 = tcc_state;
2281 if ((parse_flags & PARSE_FLAG_LINEFEED)
2282 && !(tok_flags & TOK_FLAG_EOF)) {
2283 tok_flags |= TOK_FLAG_EOF;
2284 tok = TOK_LINEFEED;
2285 goto keep_tok_flags;
2286 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2287 tok = TOK_EOF;
2288 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2289 tcc_error("missing #endif");
2290 } else if (s1->include_stack_ptr == s1->include_stack) {
2291 /* no include left : end of file. */
2292 tok = TOK_EOF;
2293 } else {
2294 tok_flags &= ~TOK_FLAG_EOF;
2295 /* pop include file */
2297 /* test if previous '#endif' was after a #ifdef at
2298 start of file */
2299 if (tok_flags & TOK_FLAG_ENDIF) {
2300 #ifdef INC_DEBUG
2301 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2302 #endif
2303 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2304 tok_flags &= ~TOK_FLAG_ENDIF;
2307 /* add end of include file debug info */
2308 if (tcc_state->do_debug) {
2309 put_stabd(N_EINCL, 0, 0);
2311 /* pop include stack */
2312 tcc_close();
2313 s1->include_stack_ptr--;
2314 p = file->buf_ptr;
2315 goto redo_no_start;
2318 break;
2320 case '\n':
2321 file->line_num++;
2322 tok_flags |= TOK_FLAG_BOL;
2323 p++;
2324 maybe_newline:
2325 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2326 goto redo_no_start;
2327 tok = TOK_LINEFEED;
2328 goto keep_tok_flags;
2330 case '#':
2331 /* XXX: simplify */
2332 PEEKC(c, p);
2333 if (is_space(c) && (parse_flags & PARSE_FLAG_ASM_FILE)) {
2334 p = parse_line_comment(p);
2335 goto redo_no_start;
2337 else
2338 if ((tok_flags & TOK_FLAG_BOL) &&
2339 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2340 file->buf_ptr = p;
2341 preprocess(tok_flags & TOK_FLAG_BOF);
2342 p = file->buf_ptr;
2343 goto maybe_newline;
2344 } else {
2345 if (c == '#') {
2346 p++;
2347 tok = TOK_TWOSHARPS;
2348 } else {
2349 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2350 p = parse_line_comment(p - 1);
2351 goto redo_no_start;
2352 } else {
2353 tok = '#';
2357 break;
2359 /* dollar is allowed to start identifiers when not parsing asm */
2360 case '$':
2361 if (!tcc_state->dollars_in_identifiers
2362 || (parse_flags & PARSE_FLAG_ASM_FILE)) goto parse_simple;
2364 case 'a': case 'b': case 'c': case 'd':
2365 case 'e': case 'f': case 'g': case 'h':
2366 case 'i': case 'j': case 'k': case 'l':
2367 case 'm': case 'n': case 'o': case 'p':
2368 case 'q': case 'r': case 's': case 't':
2369 case 'u': case 'v': case 'w': case 'x':
2370 case 'y': case 'z':
2371 case 'A': case 'B': case 'C': case 'D':
2372 case 'E': case 'F': case 'G': case 'H':
2373 case 'I': case 'J': case 'K':
2374 case 'M': case 'N': case 'O': case 'P':
2375 case 'Q': case 'R': case 'S': case 'T':
2376 case 'U': case 'V': case 'W': case 'X':
2377 case 'Y': case 'Z':
2378 case '_':
2379 parse_ident_fast:
2380 p1 = p;
2381 h = TOK_HASH_INIT;
2382 h = TOK_HASH_FUNC(h, c);
2383 p++;
2384 for(;;) {
2385 c = *p;
2386 if (!isidnum_table[c-CH_EOF]
2387 && (tcc_state->dollars_in_identifiers ? (c != '$') : 1))
2388 break;
2389 h = TOK_HASH_FUNC(h, c);
2390 p++;
2392 if (c != '\\') {
2393 TokenSym **pts;
2394 int len;
2396 /* fast case : no stray found, so we have the full token
2397 and we have already hashed it */
2398 len = p - p1;
2399 h &= (TOK_HASH_SIZE - 1);
2400 pts = &hash_ident[h];
2401 for(;;) {
2402 ts = *pts;
2403 if (!ts)
2404 break;
2405 if (ts->len == len && !memcmp(ts->str, p1, len))
2406 goto token_found;
2407 pts = &(ts->hash_next);
2409 ts = tok_alloc_new(pts, (char *) p1, len);
2410 token_found: ;
2411 } else {
2412 /* slower case */
2413 cstr_reset(&tokcstr);
2415 while (p1 < p) {
2416 cstr_ccat(&tokcstr, *p1);
2417 p1++;
2419 p--;
2420 PEEKC(c, p);
2421 parse_ident_slow:
2422 while (isidnum_table[c-CH_EOF]
2423 || (tcc_state->dollars_in_identifiers ? (c == '$') : 0)) {
2424 cstr_ccat(&tokcstr, c);
2425 PEEKC(c, p);
2427 ts = tok_alloc(tokcstr.data, tokcstr.size);
2429 tok = ts->tok;
2430 break;
2431 case 'L':
2432 t = p[1];
2433 if (t != '\\' && t != '\'' && t != '\"') {
2434 /* fast case */
2435 goto parse_ident_fast;
2436 } else {
2437 PEEKC(c, p);
2438 if (c == '\'' || c == '\"') {
2439 is_long = 1;
2440 goto str_const;
2441 } else {
2442 cstr_reset(&tokcstr);
2443 cstr_ccat(&tokcstr, 'L');
2444 goto parse_ident_slow;
2447 break;
2448 case '0': case '1': case '2': case '3':
2449 case '4': case '5': case '6': case '7':
2450 case '8': case '9':
2452 cstr_reset(&tokcstr);
2453 /* after the first digit, accept digits, alpha, '.' or sign if
2454 prefixed by 'eEpP' */
2455 parse_num:
2456 for(;;) {
2457 t = c;
2458 cstr_ccat(&tokcstr, c);
2459 PEEKC(c, p);
2460 if (!(isnum(c) || isid(c) || c == '.' ||
2461 ((c == '+' || c == '-') &&
2462 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2463 break;
2465 /* We add a trailing '\0' to ease parsing */
2466 cstr_ccat(&tokcstr, '\0');
2467 tokc.cstr = &tokcstr;
2468 tok = TOK_PPNUM;
2469 break;
2470 case '.':
2471 /* special dot handling because it can also start a number */
2472 PEEKC(c, p);
2473 if (isnum(c)) {
2474 cstr_reset(&tokcstr);
2475 cstr_ccat(&tokcstr, '.');
2476 goto parse_num;
2477 } else if (c == '.') {
2478 PEEKC(c, p);
2479 if (c != '.') {
2480 if ((parse_flags & PARSE_FLAG_ASM_COMMENTS) == 0)
2481 expect("'.'");
2482 tok = '.';
2483 break;
2485 PEEKC(c, p);
2486 tok = TOK_DOTS;
2487 } else {
2488 tok = '.';
2490 break;
2491 case '\'':
2492 case '\"':
2493 is_long = 0;
2494 str_const:
2496 CString str;
2497 int sep;
2499 sep = c;
2501 /* parse the string */
2502 cstr_new(&str);
2503 p = parse_pp_string(p, sep, &str);
2504 cstr_ccat(&str, '\0');
2506 /* eval the escape (should be done as TOK_PPNUM) */
2507 cstr_reset(&tokcstr);
2508 parse_escape_string(&tokcstr, str.data, is_long);
2509 cstr_free(&str);
2511 if (sep == '\'') {
2512 int char_size;
2513 /* XXX: make it portable */
2514 if (!is_long)
2515 char_size = 1;
2516 else
2517 char_size = sizeof(nwchar_t);
2518 if (tokcstr.size <= char_size)
2519 tcc_error("empty character constant");
2520 if (tokcstr.size > 2 * char_size)
2521 tcc_warning("multi-character character constant");
2522 if (!is_long) {
2523 tokc.i = *(int8_t *)tokcstr.data;
2524 tok = TOK_CCHAR;
2525 } else {
2526 tokc.i = *(nwchar_t *)tokcstr.data;
2527 tok = TOK_LCHAR;
2529 } else {
2530 tokc.cstr = &tokcstr;
2531 if (!is_long)
2532 tok = TOK_STR;
2533 else
2534 tok = TOK_LSTR;
2537 break;
2539 case '<':
2540 PEEKC(c, p);
2541 if (c == '=') {
2542 p++;
2543 tok = TOK_LE;
2544 } else if (c == '<') {
2545 PEEKC(c, p);
2546 if (c == '=') {
2547 p++;
2548 tok = TOK_A_SHL;
2549 } else {
2550 tok = TOK_SHL;
2552 } else {
2553 tok = TOK_LT;
2555 break;
2557 case '>':
2558 PEEKC(c, p);
2559 if (c == '=') {
2560 p++;
2561 tok = TOK_GE;
2562 } else if (c == '>') {
2563 PEEKC(c, p);
2564 if (c == '=') {
2565 p++;
2566 tok = TOK_A_SAR;
2567 } else {
2568 tok = TOK_SAR;
2570 } else {
2571 tok = TOK_GT;
2573 break;
2575 case '&':
2576 PEEKC(c, p);
2577 if (c == '&') {
2578 p++;
2579 tok = TOK_LAND;
2580 } else if (c == '=') {
2581 p++;
2582 tok = TOK_A_AND;
2583 } else {
2584 tok = '&';
2586 break;
2588 case '|':
2589 PEEKC(c, p);
2590 if (c == '|') {
2591 p++;
2592 tok = TOK_LOR;
2593 } else if (c == '=') {
2594 p++;
2595 tok = TOK_A_OR;
2596 } else {
2597 tok = '|';
2599 break;
2601 case '+':
2602 PEEKC(c, p);
2603 if (c == '+') {
2604 p++;
2605 tok = TOK_INC;
2606 } else if (c == '=') {
2607 p++;
2608 tok = TOK_A_ADD;
2609 } else {
2610 tok = '+';
2612 break;
2614 case '-':
2615 PEEKC(c, p);
2616 if (c == '-') {
2617 p++;
2618 tok = TOK_DEC;
2619 } else if (c == '=') {
2620 p++;
2621 tok = TOK_A_SUB;
2622 } else if (c == '>') {
2623 p++;
2624 tok = TOK_ARROW;
2625 } else {
2626 tok = '-';
2628 break;
2630 PARSE2('!', '!', '=', TOK_NE)
2631 PARSE2('=', '=', '=', TOK_EQ)
2632 PARSE2('*', '*', '=', TOK_A_MUL)
2633 PARSE2('%', '%', '=', TOK_A_MOD)
2634 PARSE2('^', '^', '=', TOK_A_XOR)
2636 /* comments or operator */
2637 case '/':
2638 PEEKC(c, p);
2639 if (c == '*') {
2640 p = parse_comment(p);
2641 /* comments replaced by a blank */
2642 tok = ' ';
2643 goto keep_tok_flags;
2644 } else if (c == '/') {
2645 p = parse_line_comment(p);
2646 tok = ' ';
2647 goto keep_tok_flags;
2648 } else if (c == '=') {
2649 p++;
2650 tok = TOK_A_DIV;
2651 } else {
2652 tok = '/';
2654 break;
2656 /* simple tokens */
2657 case '(':
2658 case ')':
2659 case '[':
2660 case ']':
2661 case '{':
2662 case '}':
2663 case ',':
2664 case ';':
2665 case ':':
2666 case '?':
2667 case '~':
2668 case '@': /* only used in assembler */
2669 parse_simple:
2670 tok = c;
2671 p++;
2672 break;
2673 default:
2674 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0)
2675 tcc_error("unrecognized character \\x%02x", c);
2676 else {
2677 tok = ' ';
2678 p++;
2680 break;
2682 tok_flags = 0;
2683 keep_tok_flags:
2684 file->buf_ptr = p;
2685 #if defined(PARSE_DEBUG)
2686 printf("token = %s\n", get_tok_str(tok, &tokc));
2687 #endif
2690 /* return next token without macro substitution. Can read input from
2691 macro_ptr buffer */
2692 static void next_nomacro_spc(void)
2694 if (macro_ptr) {
2695 redo:
2696 tok = *macro_ptr;
2697 if (tok) {
2698 TOK_GET(&tok, &macro_ptr, &tokc);
2699 if (tok == TOK_LINENUM) {
2700 file->line_num = tokc.i;
2701 goto redo;
2704 } else {
2705 next_nomacro1();
2709 ST_FUNC void next_nomacro(void)
2711 do {
2712 next_nomacro_spc();
2713 } while (is_space(tok));
2716 /* substitute arguments in replacement lists in macro_str by the values in
2717 args (field d) and return allocated string */
2718 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2720 int last_tok, t, spc;
2721 const int *st;
2722 Sym *s;
2723 CValue cval;
2724 TokenString str;
2725 CString cstr;
2727 tok_str_new(&str);
2728 last_tok = 0;
2729 while(1) {
2730 TOK_GET(&t, &macro_str, &cval);
2731 if (!t)
2732 break;
2733 if (t == '#') {
2734 /* stringize */
2735 TOK_GET(&t, &macro_str, &cval);
2736 if (!t)
2737 break;
2738 s = sym_find2(args, t);
2739 if (s) {
2740 cstr_new(&cstr);
2741 st = s->d;
2742 spc = 0;
2743 while (*st) {
2744 TOK_GET(&t, &st, &cval);
2745 if (!check_space(t, &spc))
2746 cstr_cat(&cstr, get_tok_str(t, &cval));
2748 cstr.size -= spc;
2749 cstr_ccat(&cstr, '\0');
2750 #ifdef PP_DEBUG
2751 printf("stringize: %s\n", (char *)cstr.data);
2752 #endif
2753 /* add string */
2754 cval.cstr = &cstr;
2755 tok_str_add2(&str, TOK_STR, &cval);
2756 cstr_free(&cstr);
2757 } else {
2758 tok_str_add2(&str, t, &cval);
2760 } else if (t >= TOK_IDENT) {
2761 s = sym_find2(args, t);
2762 if (s) {
2763 st = s->d;
2764 /* if '##' is present before or after, no arg substitution */
2765 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2766 /* special case for var arg macros : ## eats the
2767 ',' if empty VA_ARGS variable. */
2768 /* XXX: test of the ',' is not 100%
2769 reliable. should fix it to avoid security
2770 problems */
2771 if (gnu_ext && s->type.t &&
2772 last_tok == TOK_TWOSHARPS &&
2773 str.len >= 2 && str.str[str.len - 2] == ',') {
2774 if (*st == TOK_PLCHLDR) {
2775 /* suppress ',' '##' */
2776 str.len -= 2;
2777 } else {
2778 /* suppress '##' and add variable */
2779 str.len--;
2780 goto add_var;
2782 } else {
2783 int t1;
2784 add_var:
2785 for(;;) {
2786 TOK_GET(&t1, &st, &cval);
2787 if (!t1)
2788 break;
2789 tok_str_add2(&str, t1, &cval);
2792 } else if (*st != TOK_PLCHLDR) {
2793 /* NOTE: the stream cannot be read when macro
2794 substituing an argument */
2795 macro_subst(&str, nested_list, st, NULL);
2797 } else {
2798 tok_str_add(&str, t);
2800 } else {
2801 tok_str_add2(&str, t, &cval);
2803 last_tok = t;
2805 tok_str_add(&str, 0);
2806 return str.str;
2809 static char const ab_month_name[12][4] =
2811 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2812 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2815 /* do macro substitution of current token with macro 's' and add
2816 result to (tok_str,tok_len). 'nested_list' is the list of all
2817 macros we got inside to avoid recursing. Return non zero if no
2818 substitution needs to be done */
2819 static int macro_subst_tok(TokenString *tok_str,
2820 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2822 Sym *args, *sa, *sa1;
2823 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2824 const int *p;
2825 TokenString str;
2826 char *cstrval;
2827 CValue cval;
2828 CString cstr;
2829 char buf[32];
2831 /* if symbol is a macro, prepare substitution */
2832 /* special macros */
2833 if (tok == TOK___LINE__) {
2834 snprintf(buf, sizeof(buf), "%d", file->line_num);
2835 cstrval = buf;
2836 t1 = TOK_PPNUM;
2837 goto add_cstr1;
2838 } else if (tok == TOK___FILE__) {
2839 cstrval = file->filename;
2840 goto add_cstr;
2841 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2842 time_t ti;
2843 struct tm *tm;
2845 time(&ti);
2846 tm = localtime(&ti);
2847 if (tok == TOK___DATE__) {
2848 snprintf(buf, sizeof(buf), "%s %2d %d",
2849 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2850 } else {
2851 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2852 tm->tm_hour, tm->tm_min, tm->tm_sec);
2854 cstrval = buf;
2855 add_cstr:
2856 t1 = TOK_STR;
2857 add_cstr1:
2858 cstr_new(&cstr);
2859 cstr_cat(&cstr, cstrval);
2860 cstr_ccat(&cstr, '\0');
2861 cval.cstr = &cstr;
2862 tok_str_add2(tok_str, t1, &cval);
2863 cstr_free(&cstr);
2864 } else {
2865 mstr = s->d;
2866 mstr_allocated = 0;
2867 if (s->type.t == MACRO_FUNC) {
2868 /* NOTE: we do not use next_nomacro to avoid eating the
2869 next token. XXX: find better solution */
2870 redo:
2871 if (macro_ptr) {
2872 p = macro_ptr;
2873 while (is_space(t = *p) || TOK_LINEFEED == t)
2874 ++p;
2875 if (t == 0 && can_read_stream) {
2876 /* end of macro stream: we must look at the token
2877 after in the file */
2878 struct macro_level *ml = *can_read_stream;
2879 macro_ptr = NULL;
2880 if (ml)
2882 macro_ptr = ml->p;
2883 ml->p = NULL;
2884 *can_read_stream = ml -> prev;
2886 /* also, end of scope for nested defined symbol */
2887 (*nested_list)->v = -1;
2888 goto redo;
2890 } else {
2891 ch = file->buf_ptr[0];
2892 while (is_space(ch) || ch == '\n' || ch == '/')
2894 if (ch == '/')
2896 int c;
2897 uint8_t *p = file->buf_ptr;
2898 PEEKC(c, p);
2899 if (c == '*') {
2900 p = parse_comment(p);
2901 file->buf_ptr = p - 1;
2902 } else if (c == '/') {
2903 p = parse_line_comment(p);
2904 file->buf_ptr = p - 1;
2905 } else
2906 break;
2908 cinp();
2910 t = ch;
2912 if (t != '(') /* no macro subst */
2913 return -1;
2915 /* argument macro */
2916 next_nomacro();
2917 next_nomacro();
2918 args = NULL;
2919 sa = s->next;
2920 /* NOTE: empty args are allowed, except if no args */
2921 for(;;) {
2922 /* handle '()' case */
2923 if (!args && !sa && tok == ')')
2924 break;
2925 if (!sa)
2926 tcc_error("macro '%s' used with too many args",
2927 get_tok_str(s->v, 0));
2928 tok_str_new(&str);
2929 parlevel = spc = 0;
2930 /* NOTE: non zero sa->t indicates VA_ARGS */
2931 while ((parlevel > 0 ||
2932 (tok != ')' &&
2933 (tok != ',' || sa->type.t))) &&
2934 tok != -1) {
2935 if (tok == '(')
2936 parlevel++;
2937 else if (tok == ')')
2938 parlevel--;
2939 if (tok == TOK_LINEFEED)
2940 tok = ' ';
2941 if (!check_space(tok, &spc))
2942 tok_str_add2(&str, tok, &tokc);
2943 next_nomacro_spc();
2945 if (!str.len)
2946 tok_str_add(&str, TOK_PLCHLDR);
2947 str.len -= spc;
2948 tok_str_add(&str, 0);
2949 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2950 sa1->d = str.str;
2951 sa = sa->next;
2952 if (tok == ')') {
2953 /* special case for gcc var args: add an empty
2954 var arg argument if it is omitted */
2955 if (sa && sa->type.t && gnu_ext)
2956 continue;
2957 else
2958 break;
2960 if (tok != ',')
2961 expect(",");
2962 next_nomacro();
2964 if (sa) {
2965 tcc_error("macro '%s' used with too few args",
2966 get_tok_str(s->v, 0));
2969 /* now subst each arg */
2970 mstr = macro_arg_subst(nested_list, mstr, args);
2971 /* free memory */
2972 sa = args;
2973 while (sa) {
2974 sa1 = sa->prev;
2975 tok_str_free(sa->d);
2976 sym_free(sa);
2977 sa = sa1;
2979 mstr_allocated = 1;
2981 sym_push2(nested_list, s->v, 0, 0);
2982 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2983 /* pop nested defined symbol */
2984 sa1 = *nested_list;
2985 *nested_list = sa1->prev;
2986 sym_free(sa1);
2987 if (mstr_allocated)
2988 tok_str_free(mstr);
2990 return 0;
2993 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2994 return the resulting string (which must be freed). */
2995 static inline int *macro_twosharps(const int *macro_str)
2997 const int *ptr;
2998 int t;
2999 TokenString macro_str1;
3000 CString cstr;
3001 int n, start_of_nosubsts;
3003 /* we search the first '##' */
3004 for(ptr = macro_str;;) {
3005 CValue cval;
3006 TOK_GET(&t, &ptr, &cval);
3007 if (t == TOK_TWOSHARPS)
3008 break;
3009 /* nothing more to do if end of string */
3010 if (t == 0)
3011 return NULL;
3014 /* we saw '##', so we need more processing to handle it */
3015 start_of_nosubsts = -1;
3016 tok_str_new(&macro_str1);
3017 for(ptr = macro_str;;) {
3018 TOK_GET(&tok, &ptr, &tokc);
3019 if (tok == 0)
3020 break;
3021 if (tok == TOK_TWOSHARPS)
3022 continue;
3023 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
3024 start_of_nosubsts = macro_str1.len;
3025 while (*ptr == TOK_TWOSHARPS) {
3026 /* given 'a##b', remove nosubsts preceding 'a' */
3027 if (start_of_nosubsts >= 0)
3028 macro_str1.len = start_of_nosubsts;
3029 /* given 'a##b', skip '##' */
3030 t = *++ptr;
3031 /* given 'a##b', remove nosubsts preceding 'b' */
3032 while (t == TOK_NOSUBST)
3033 t = *++ptr;
3034 if (t && t != TOK_TWOSHARPS) {
3035 CValue cval;
3036 TOK_GET(&t, &ptr, &cval);
3037 /* We concatenate the two tokens */
3038 cstr_new(&cstr);
3039 if (tok != TOK_PLCHLDR)
3040 cstr_cat(&cstr, get_tok_str(tok, &tokc));
3041 n = cstr.size;
3042 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
3043 cstr_cat(&cstr, get_tok_str(t, &cval));
3044 cstr_ccat(&cstr, '\0');
3046 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3047 memcpy(file->buffer, cstr.data, cstr.size);
3048 for (;;) {
3049 next_nomacro1();
3050 if (0 == *file->buf_ptr)
3051 break;
3052 tok_str_add2(&macro_str1, tok, &tokc);
3053 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
3054 n, cstr.data, (char*)cstr.data + n);
3056 tcc_close();
3057 cstr_free(&cstr);
3060 if (tok != TOK_NOSUBST) {
3061 tok_str_add2(&macro_str1, tok, &tokc);
3062 tok = ' ';
3063 start_of_nosubsts = -1;
3065 tok_str_add2(&macro_str1, tok, &tokc);
3067 tok_str_add(&macro_str1, 0);
3068 return macro_str1.str;
3072 /* do macro substitution of macro_str and add result to
3073 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3074 inside to avoid recursing. */
3075 static void macro_subst(TokenString *tok_str, Sym **nested_list,
3076 const int *macro_str, struct macro_level ** can_read_stream)
3078 Sym *s;
3079 int *macro_str1;
3080 const int *ptr;
3081 int t, ret, spc;
3082 CValue cval;
3083 struct macro_level ml;
3084 int force_blank;
3086 /* first scan for '##' operator handling */
3087 ptr = macro_str;
3088 macro_str1 = macro_twosharps(ptr);
3090 if (macro_str1)
3091 ptr = macro_str1;
3092 spc = 0;
3093 force_blank = 0;
3095 while (1) {
3096 /* NOTE: ptr == NULL can only happen if tokens are read from
3097 file stream due to a macro function call */
3098 if (ptr == NULL)
3099 break;
3100 TOK_GET(&t, &ptr, &cval);
3101 if (t == 0)
3102 break;
3103 if (t == TOK_NOSUBST) {
3104 /* following token has already been subst'd. just copy it on */
3105 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3106 TOK_GET(&t, &ptr, &cval);
3107 goto no_subst;
3109 s = define_find(t);
3110 if (s != NULL) {
3111 /* if nested substitution, do nothing */
3112 if (sym_find2(*nested_list, t)) {
3113 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3114 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3115 goto no_subst;
3117 ml.p = macro_ptr;
3118 if (can_read_stream)
3119 ml.prev = *can_read_stream, *can_read_stream = &ml;
3120 macro_ptr = (int *)ptr;
3121 tok = t;
3122 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3123 ptr = (int *)macro_ptr;
3124 macro_ptr = ml.p;
3125 if (can_read_stream && *can_read_stream == &ml)
3126 *can_read_stream = ml.prev;
3127 if (ret != 0)
3128 goto no_subst;
3129 if (parse_flags & PARSE_FLAG_SPACES)
3130 force_blank = 1;
3131 } else {
3132 no_subst:
3133 if (force_blank) {
3134 tok_str_add(tok_str, ' ');
3135 spc = 1;
3136 force_blank = 0;
3138 if (!check_space(t, &spc))
3139 tok_str_add2(tok_str, t, &cval);
3142 if (macro_str1)
3143 tok_str_free(macro_str1);
3146 /* return next token with macro substitution */
3147 ST_FUNC void next(void)
3149 Sym *nested_list, *s;
3150 TokenString str;
3151 struct macro_level *ml;
3153 redo:
3154 if (parse_flags & PARSE_FLAG_SPACES)
3155 next_nomacro_spc();
3156 else
3157 next_nomacro();
3158 if (!macro_ptr) {
3159 /* if not reading from macro substituted string, then try
3160 to substitute macros */
3161 if (tok >= TOK_IDENT &&
3162 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3163 s = define_find(tok);
3164 if (s) {
3165 /* we have a macro: we try to substitute */
3166 tok_str_new(&str);
3167 nested_list = NULL;
3168 ml = NULL;
3169 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3170 /* substitution done, NOTE: maybe empty */
3171 tok_str_add(&str, 0);
3172 macro_ptr = str.str;
3173 macro_ptr_allocated = str.str;
3174 goto redo;
3178 } else {
3179 if (tok == 0) {
3180 /* end of macro or end of unget buffer */
3181 if (unget_buffer_enabled) {
3182 macro_ptr = unget_saved_macro_ptr;
3183 unget_buffer_enabled = 0;
3184 } else {
3185 /* end of macro string: free it */
3186 tok_str_free(macro_ptr_allocated);
3187 macro_ptr_allocated = NULL;
3188 macro_ptr = NULL;
3190 goto redo;
3191 } else if (tok == TOK_NOSUBST) {
3192 /* discard preprocessor's nosubst markers */
3193 goto redo;
3197 /* convert preprocessor tokens into C tokens */
3198 if (tok == TOK_PPNUM &&
3199 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3200 parse_number((char *)tokc.cstr->data);
3204 /* push back current token and set current token to 'last_tok'. Only
3205 identifier case handled for labels. */
3206 ST_INLN void unget_tok(int last_tok)
3208 int i, n;
3209 int *q;
3210 if (unget_buffer_enabled)
3212 /* assert(macro_ptr == unget_saved_buffer + 1);
3213 assert(*macro_ptr == 0); */
3215 else
3217 unget_saved_macro_ptr = macro_ptr;
3218 unget_buffer_enabled = 1;
3220 q = unget_saved_buffer;
3221 macro_ptr = q;
3222 *q++ = tok;
3223 n = tok_ext_size(tok) - 1;
3224 for(i=0;i<n;i++)
3225 *q++ = tokc.tab[i];
3226 *q = 0; /* end of token string */
3227 tok = last_tok;
3231 /* better than nothing, but needs extension to handle '-E' option
3232 correctly too */
3233 ST_FUNC void preprocess_init(TCCState *s1)
3235 s1->include_stack_ptr = s1->include_stack;
3236 /* XXX: move that before to avoid having to initialize
3237 file->ifdef_stack_ptr ? */
3238 s1->ifdef_stack_ptr = s1->ifdef_stack;
3239 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3241 vtop = vstack - 1;
3242 s1->pack_stack[0] = 0;
3243 s1->pack_stack_ptr = s1->pack_stack;
3246 ST_FUNC void preprocess_new(void)
3248 int i, c;
3249 const char *p, *r;
3251 /* init isid table */
3253 for(i=CH_EOF;i<256;i++)
3254 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3256 /* add all tokens */
3257 if (table_ident) {
3258 tcc_free (table_ident);
3259 table_ident = NULL;
3261 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3263 tok_ident = TOK_IDENT;
3264 p = tcc_keywords;
3265 while (*p) {
3266 r = p;
3267 for(;;) {
3268 c = *r++;
3269 if (c == '\0')
3270 break;
3272 tok_alloc(p, r - p - 1);
3273 p = r;
3277 static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3279 switch (s1->Pflag) {
3280 case LINE_MACRO_OUTPUT_FORMAT_STD:
3281 /* "tcc -E -P1" case */
3282 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3283 break;
3285 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3286 /* "tcc -E -P" case: don't output a line directive */
3287 break;
3289 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3290 default:
3291 /* "tcc -E" case: a gcc standard by default */
3292 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3293 break;
3297 /* Preprocess the current file */
3298 ST_FUNC int tcc_preprocess(TCCState *s1)
3300 BufferedFile *file_ref, **iptr, **iptr_new;
3301 int token_seen, d;
3302 const char *s;
3304 preprocess_init(s1);
3305 ch = file->buf_ptr[0];
3306 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3307 parse_flags &= (PARSE_FLAG_ASM_FILE | PARSE_FLAG_ASM_COMMENTS);
3308 parse_flags |= PARSE_FLAG_PREPROCESS | PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3309 token_seen = 0;
3310 file->line_ref = 0;
3311 file_ref = NULL;
3312 iptr = s1->include_stack_ptr;
3314 for (;;) {
3315 next();
3316 if (tok == TOK_EOF) {
3317 break;
3318 } else if (file != file_ref) {
3319 if (file_ref)
3320 line_macro_output(file_ref, "", s1);
3321 goto print_line;
3322 } else if (tok == TOK_LINEFEED) {
3323 if (!token_seen)
3324 continue;
3325 file->line_ref++;
3326 token_seen = 0;
3327 } else if (!token_seen) {
3328 d = file->line_num - file->line_ref;
3329 if (file != file_ref || d >= 8) {
3330 print_line:
3331 s = "";
3332 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3333 iptr_new = s1->include_stack_ptr;
3334 s = iptr_new > iptr ? " 1"
3335 : iptr_new < iptr ? " 2"
3336 : iptr_new > s1->include_stack ? " 3"
3337 : ""
3340 line_macro_output(file, s, s1);
3341 } else {
3342 while (d > 0)
3343 fputs("\n", s1->ppfp), --d;
3345 file->line_ref = (file_ref = file)->line_num;
3346 token_seen = tok != TOK_LINEFEED;
3347 if (!token_seen)
3348 continue;
3350 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3352 return 0;