* and #pragma pop_macro("macro_name")
[tinycc.git] / tccpp.c
blob4d9430d15bd2b894abb4bd7d64dc7e9f8de0d7dc
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_define_stack = NULL;
226 ts->sym_label = NULL;
227 ts->sym_struct = NULL;
228 ts->sym_identifier = NULL;
229 ts->len = len;
230 ts->hash_next = NULL;
231 memcpy(ts->str, str, len);
232 ts->str[len] = '\0';
233 *pts = ts;
234 return ts;
237 #define TOK_HASH_INIT 1
238 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
240 /* find a token and add it if not found */
241 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
243 TokenSym *ts, **pts;
244 int i;
245 unsigned int h;
247 h = TOK_HASH_INIT;
248 for(i=0;i<len;i++)
249 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
250 h &= (TOK_HASH_SIZE - 1);
252 pts = &hash_ident[h];
253 for(;;) {
254 ts = *pts;
255 if (!ts)
256 break;
257 if (ts->len == len && !memcmp(ts->str, str, len))
258 return ts;
259 pts = &(ts->hash_next);
261 return tok_alloc_new(pts, str, len);
264 /* XXX: buffer overflow */
265 /* XXX: float tokens */
266 ST_FUNC char *get_tok_str(int v, CValue *cv)
268 static char buf[STRING_MAX_SIZE + 1];
269 static CString cstr_buf;
270 CString *cstr;
271 char *p;
272 int i, len;
274 /* NOTE: to go faster, we give a fixed buffer for small strings */
275 cstr_reset(&cstr_buf);
276 cstr_buf.data = buf;
277 cstr_buf.size_allocated = sizeof(buf);
278 p = buf;
280 /* just an explanation, should never happen:
281 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
282 tcc_error("internal error: get_tok_str"); */
284 switch(v) {
285 case TOK_CINT:
286 case TOK_CUINT:
287 /* XXX: not quite exact, but only useful for testing */
288 sprintf(p, "%u", cv->ui);
289 break;
290 case TOK_CLLONG:
291 case TOK_CULLONG:
292 /* XXX: not quite exact, but only useful for testing */
293 #ifdef _WIN32
294 sprintf(p, "%u", (unsigned)cv->ull);
295 #else
296 sprintf(p, "%llu", cv->ull);
297 #endif
298 break;
299 case TOK_LCHAR:
300 cstr_ccat(&cstr_buf, 'L');
301 case TOK_CCHAR:
302 cstr_ccat(&cstr_buf, '\'');
303 add_char(&cstr_buf, cv->i);
304 cstr_ccat(&cstr_buf, '\'');
305 cstr_ccat(&cstr_buf, '\0');
306 break;
307 case TOK_PPNUM:
308 cstr = cv->cstr;
309 len = cstr->size - 1;
310 for(i=0;i<len;i++)
311 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
312 cstr_ccat(&cstr_buf, '\0');
313 break;
314 case TOK_LSTR:
315 cstr_ccat(&cstr_buf, 'L');
316 case TOK_STR:
317 cstr = cv->cstr;
318 cstr_ccat(&cstr_buf, '\"');
319 if (v == TOK_STR) {
320 len = cstr->size - 1;
321 for(i=0;i<len;i++)
322 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
323 } else {
324 len = (cstr->size / sizeof(nwchar_t)) - 1;
325 for(i=0;i<len;i++)
326 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
328 cstr_ccat(&cstr_buf, '\"');
329 cstr_ccat(&cstr_buf, '\0');
330 break;
332 case TOK_CFLOAT:
333 case TOK_CDOUBLE:
334 case TOK_CLDOUBLE:
335 case TOK_LINENUM:
336 return NULL; /* should not happen */
338 /* above tokens have value, the ones below don't */
340 case TOK_LT:
341 v = '<';
342 goto addv;
343 case TOK_GT:
344 v = '>';
345 goto addv;
346 case TOK_DOTS:
347 return strcpy(p, "...");
348 case TOK_A_SHL:
349 return strcpy(p, "<<=");
350 case TOK_A_SAR:
351 return strcpy(p, ">>=");
352 default:
353 if (v < TOK_IDENT) {
354 /* search in two bytes table */
355 const unsigned char *q = tok_two_chars;
356 while (*q) {
357 if (q[2] == v) {
358 *p++ = q[0];
359 *p++ = q[1];
360 *p = '\0';
361 return buf;
363 q += 3;
365 addv:
366 *p++ = v;
367 *p = '\0';
368 } else if (v < tok_ident) {
369 return table_ident[v - TOK_IDENT]->str;
370 } else if (v >= SYM_FIRST_ANOM) {
371 /* special name for anonymous symbol */
372 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
373 } else {
374 /* should never happen */
375 return NULL;
377 break;
379 return cstr_buf.data;
382 /* fill input buffer and peek next char */
383 static int tcc_peekc_slow(BufferedFile *bf)
385 int len;
386 /* only tries to read if really end of buffer */
387 if (bf->buf_ptr >= bf->buf_end) {
388 if (bf->fd != -1) {
389 #if defined(PARSE_DEBUG)
390 len = 8;
391 #else
392 len = IO_BUF_SIZE;
393 #endif
394 len = read(bf->fd, bf->buffer, len);
395 if (len < 0)
396 len = 0;
397 } else {
398 len = 0;
400 total_bytes += len;
401 bf->buf_ptr = bf->buffer;
402 bf->buf_end = bf->buffer + len;
403 *bf->buf_end = CH_EOB;
405 if (bf->buf_ptr < bf->buf_end) {
406 return bf->buf_ptr[0];
407 } else {
408 bf->buf_ptr = bf->buf_end;
409 return CH_EOF;
413 /* return the current character, handling end of block if necessary
414 (but not stray) */
415 ST_FUNC int handle_eob(void)
417 return tcc_peekc_slow(file);
420 /* read next char from current input file and handle end of input buffer */
421 ST_INLN void inp(void)
423 ch = *(++(file->buf_ptr));
424 /* end of buffer/file handling */
425 if (ch == CH_EOB)
426 ch = handle_eob();
429 /* handle '\[\r]\n' */
430 static int handle_stray_noerror(void)
432 while (ch == '\\') {
433 inp();
434 if (ch == '\n') {
435 file->line_num++;
436 inp();
437 } else if (ch == '\r') {
438 inp();
439 if (ch != '\n')
440 goto fail;
441 file->line_num++;
442 inp();
443 } else {
444 fail:
445 return 1;
448 return 0;
451 static void handle_stray(void)
453 if (handle_stray_noerror())
454 tcc_error("stray '\\' in program");
457 /* skip the stray and handle the \\n case. Output an error if
458 incorrect char after the stray */
459 static int handle_stray1(uint8_t *p)
461 int c;
463 if (p >= file->buf_end) {
464 file->buf_ptr = p;
465 c = handle_eob();
466 p = file->buf_ptr;
467 if (c == '\\')
468 goto parse_stray;
469 } else {
470 parse_stray:
471 file->buf_ptr = p;
472 ch = *p;
473 handle_stray();
474 p = file->buf_ptr;
475 c = *p;
477 return c;
480 /* handle just the EOB case, but not stray */
481 #define PEEKC_EOB(c, p)\
483 p++;\
484 c = *p;\
485 if (c == '\\') {\
486 file->buf_ptr = p;\
487 c = handle_eob();\
488 p = file->buf_ptr;\
492 /* handle the complicated stray case */
493 #define PEEKC(c, p)\
495 p++;\
496 c = *p;\
497 if (c == '\\') {\
498 c = handle_stray1(p);\
499 p = file->buf_ptr;\
503 /* input with '\[\r]\n' handling. Note that this function cannot
504 handle other characters after '\', so you cannot call it inside
505 strings or comments */
506 ST_FUNC void minp(void)
508 inp();
509 if (ch == '\\')
510 handle_stray();
514 /* single line C++ comments */
515 static uint8_t *parse_line_comment(uint8_t *p)
517 int c;
519 p++;
520 for(;;) {
521 c = *p;
522 redo:
523 if (c == '\n' || c == CH_EOF) {
524 break;
525 } else if (c == '\\') {
526 file->buf_ptr = p;
527 c = handle_eob();
528 p = file->buf_ptr;
529 if (c == '\\') {
530 PEEKC_EOB(c, p);
531 if (c == '\n') {
532 file->line_num++;
533 PEEKC_EOB(c, p);
534 } else if (c == '\r') {
535 PEEKC_EOB(c, p);
536 if (c == '\n') {
537 file->line_num++;
538 PEEKC_EOB(c, p);
541 } else {
542 goto redo;
544 } else {
545 p++;
548 return p;
551 /* C comments */
552 ST_FUNC uint8_t *parse_comment(uint8_t *p)
554 int c;
556 p++;
557 for(;;) {
558 /* fast skip loop */
559 for(;;) {
560 c = *p;
561 if (c == '\n' || c == '*' || c == '\\')
562 break;
563 p++;
564 c = *p;
565 if (c == '\n' || c == '*' || c == '\\')
566 break;
567 p++;
569 /* now we can handle all the cases */
570 if (c == '\n') {
571 file->line_num++;
572 p++;
573 } else if (c == '*') {
574 p++;
575 for(;;) {
576 c = *p;
577 if (c == '*') {
578 p++;
579 } else if (c == '/') {
580 goto end_of_comment;
581 } else if (c == '\\') {
582 file->buf_ptr = p;
583 c = handle_eob();
584 p = file->buf_ptr;
585 if (c == '\\') {
586 /* skip '\[\r]\n', otherwise just skip the stray */
587 while (c == '\\') {
588 PEEKC_EOB(c, p);
589 if (c == '\n') {
590 file->line_num++;
591 PEEKC_EOB(c, p);
592 } else if (c == '\r') {
593 PEEKC_EOB(c, p);
594 if (c == '\n') {
595 file->line_num++;
596 PEEKC_EOB(c, p);
598 } else {
599 goto after_star;
603 } else {
604 break;
607 after_star: ;
608 } else {
609 /* stray, eob or eof */
610 file->buf_ptr = p;
611 c = handle_eob();
612 p = file->buf_ptr;
613 if (c == CH_EOF) {
614 tcc_error("unexpected end of file in comment");
615 } else if (c == '\\') {
616 p++;
620 end_of_comment:
621 p++;
622 return p;
625 #define cinp minp
627 static inline void skip_spaces(void)
629 while (is_space(ch))
630 cinp();
633 static inline int check_space(int t, int *spc)
635 if (is_space(t)) {
636 if (*spc)
637 return 1;
638 *spc = 1;
639 } else
640 *spc = 0;
641 return 0;
644 /* parse a string without interpreting escapes */
645 static uint8_t *parse_pp_string(uint8_t *p,
646 int sep, CString *str)
648 int c;
649 p++;
650 for(;;) {
651 c = *p;
652 if (c == sep) {
653 break;
654 } else if (c == '\\') {
655 file->buf_ptr = p;
656 c = handle_eob();
657 p = file->buf_ptr;
658 if (c == CH_EOF) {
659 unterminated_string:
660 /* XXX: indicate line number of start of string */
661 tcc_error("missing terminating %c character", sep);
662 } else if (c == '\\') {
663 /* escape : just skip \[\r]\n */
664 PEEKC_EOB(c, p);
665 if (c == '\n') {
666 file->line_num++;
667 p++;
668 } else if (c == '\r') {
669 PEEKC_EOB(c, p);
670 if (c != '\n')
671 expect("'\n' after '\r'");
672 file->line_num++;
673 p++;
674 } else if (c == CH_EOF) {
675 goto unterminated_string;
676 } else {
677 if (str) {
678 cstr_ccat(str, '\\');
679 cstr_ccat(str, c);
681 p++;
684 } else if (c == '\n') {
685 file->line_num++;
686 goto add_char;
687 } else if (c == '\r') {
688 PEEKC_EOB(c, p);
689 if (c != '\n') {
690 if (str)
691 cstr_ccat(str, '\r');
692 } else {
693 file->line_num++;
694 goto add_char;
696 } else {
697 add_char:
698 if (str)
699 cstr_ccat(str, c);
700 p++;
703 p++;
704 return p;
707 /* skip block of text until #else, #elif or #endif. skip also pairs of
708 #if/#endif */
709 static void preprocess_skip(void)
711 int a, start_of_line, c, in_warn_or_error;
712 uint8_t *p;
714 p = file->buf_ptr;
715 a = 0;
716 redo_start:
717 start_of_line = 1;
718 in_warn_or_error = 0;
719 for(;;) {
720 redo_no_start:
721 c = *p;
722 switch(c) {
723 case ' ':
724 case '\t':
725 case '\f':
726 case '\v':
727 case '\r':
728 p++;
729 goto redo_no_start;
730 case '\n':
731 file->line_num++;
732 p++;
733 goto redo_start;
734 case '\\':
735 file->buf_ptr = p;
736 c = handle_eob();
737 if (c == CH_EOF) {
738 expect("#endif");
739 } else if (c == '\\') {
740 ch = file->buf_ptr[0];
741 handle_stray_noerror();
743 p = file->buf_ptr;
744 goto redo_no_start;
745 /* skip strings */
746 case '\"':
747 case '\'':
748 if (in_warn_or_error)
749 goto _default;
750 p = parse_pp_string(p, c, NULL);
751 break;
752 /* skip comments */
753 case '/':
754 if (in_warn_or_error)
755 goto _default;
756 file->buf_ptr = p;
757 ch = *p;
758 minp();
759 p = file->buf_ptr;
760 if (ch == '*') {
761 p = parse_comment(p);
762 } else if (ch == '/') {
763 p = parse_line_comment(p);
765 break;
766 case '#':
767 p++;
768 if (start_of_line) {
769 file->buf_ptr = p;
770 next_nomacro();
771 p = file->buf_ptr;
772 if (a == 0 &&
773 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
774 goto the_end;
775 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
776 a++;
777 else if (tok == TOK_ENDIF)
778 a--;
779 else if( tok == TOK_ERROR || tok == TOK_WARNING)
780 in_warn_or_error = 1;
781 else if (tok == TOK_LINEFEED)
782 goto redo_start;
783 else if (parse_flags & PARSE_FLAG_ASM_COMMENTS)
784 p = parse_line_comment(p);
786 else if (parse_flags & PARSE_FLAG_ASM_FILE)
787 p = parse_line_comment(p);
788 break;
789 _default:
790 default:
791 p++;
792 break;
794 start_of_line = 0;
796 the_end: ;
797 file->buf_ptr = p;
800 /* ParseState handling */
802 /* XXX: currently, no include file info is stored. Thus, we cannot display
803 accurate messages if the function or data definition spans multiple
804 files */
806 /* save current parse state in 's' */
807 ST_FUNC void save_parse_state(ParseState *s)
809 s->line_num = file->line_num;
810 s->macro_ptr = macro_ptr;
811 s->tok = tok;
812 s->tokc = tokc;
815 /* restore parse state from 's' */
816 ST_FUNC void restore_parse_state(ParseState *s)
818 file->line_num = s->line_num;
819 macro_ptr = s->macro_ptr;
820 tok = s->tok;
821 tokc = s->tokc;
824 /* return the number of additional 'ints' necessary to store the
825 token */
826 static inline int tok_ext_size(int t)
828 switch(t) {
829 /* 4 bytes */
830 case TOK_CINT:
831 case TOK_CUINT:
832 case TOK_CCHAR:
833 case TOK_LCHAR:
834 case TOK_CFLOAT:
835 case TOK_LINENUM:
836 return 1;
837 case TOK_STR:
838 case TOK_LSTR:
839 case TOK_PPNUM:
840 tcc_error("unsupported token");
841 return 1;
842 case TOK_CDOUBLE:
843 case TOK_CLLONG:
844 case TOK_CULLONG:
845 return 2;
846 case TOK_CLDOUBLE:
847 return LDOUBLE_SIZE / 4;
848 default:
849 return 0;
853 /* token string handling */
855 ST_INLN void tok_str_new(TokenString *s)
857 s->str = NULL;
858 s->len = 0;
859 s->allocated_len = 0;
860 s->last_line_num = -1;
863 ST_FUNC void tok_str_free(int *str)
865 tcc_free(str);
868 static int *tok_str_realloc(TokenString *s)
870 int *str, len;
872 if (s->allocated_len == 0) {
873 len = 8;
874 } else {
875 len = s->allocated_len * 2;
877 str = tcc_realloc(s->str, len * sizeof(int));
878 s->allocated_len = len;
879 s->str = str;
880 return str;
883 ST_FUNC void tok_str_add(TokenString *s, int t)
885 int len, *str;
887 len = s->len;
888 str = s->str;
889 if (len >= s->allocated_len)
890 str = tok_str_realloc(s);
891 str[len++] = t;
892 s->len = len;
895 static void tok_str_add2(TokenString *s, int t, CValue *cv)
897 int len, *str;
899 len = s->len;
900 str = s->str;
902 /* allocate space for worst case */
903 if (len + TOK_MAX_SIZE > s->allocated_len)
904 str = tok_str_realloc(s);
905 str[len++] = t;
906 switch(t) {
907 case TOK_CINT:
908 case TOK_CUINT:
909 case TOK_CCHAR:
910 case TOK_LCHAR:
911 case TOK_CFLOAT:
912 case TOK_LINENUM:
913 str[len++] = cv->tab[0];
914 break;
915 case TOK_PPNUM:
916 case TOK_STR:
917 case TOK_LSTR:
919 int nb_words;
920 CString *cstr;
922 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
923 while ((len + nb_words) > s->allocated_len)
924 str = tok_str_realloc(s);
925 cstr = (CString *)(str + len);
926 cstr->data = NULL;
927 cstr->size = cv->cstr->size;
928 cstr->data_allocated = NULL;
929 cstr->size_allocated = cstr->size;
930 memcpy((char *)cstr + sizeof(CString),
931 cv->cstr->data, cstr->size);
932 len += nb_words;
934 break;
935 case TOK_CDOUBLE:
936 case TOK_CLLONG:
937 case TOK_CULLONG:
938 #if LDOUBLE_SIZE == 8
939 case TOK_CLDOUBLE:
940 #endif
941 str[len++] = cv->tab[0];
942 str[len++] = cv->tab[1];
943 break;
944 #if LDOUBLE_SIZE == 12
945 case TOK_CLDOUBLE:
946 str[len++] = cv->tab[0];
947 str[len++] = cv->tab[1];
948 str[len++] = cv->tab[2];
949 #elif LDOUBLE_SIZE == 16
950 case TOK_CLDOUBLE:
951 str[len++] = cv->tab[0];
952 str[len++] = cv->tab[1];
953 str[len++] = cv->tab[2];
954 str[len++] = cv->tab[3];
955 #elif LDOUBLE_SIZE != 8
956 #error add long double size support
957 #endif
958 break;
959 default:
960 break;
962 s->len = len;
965 /* add the current parse token in token string 's' */
966 ST_FUNC void tok_str_add_tok(TokenString *s)
968 CValue cval;
970 /* save line number info */
971 if (file->line_num != s->last_line_num) {
972 s->last_line_num = file->line_num;
973 cval.i = s->last_line_num;
974 tok_str_add2(s, TOK_LINENUM, &cval);
976 tok_str_add2(s, tok, &tokc);
979 /* get a token from an integer array and increment pointer
980 accordingly. we code it as a macro to avoid pointer aliasing. */
981 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
983 const int *p = *pp;
984 int n, *tab;
986 tab = cv->tab;
987 switch(*t = *p++) {
988 case TOK_CINT:
989 case TOK_CUINT:
990 case TOK_CCHAR:
991 case TOK_LCHAR:
992 case TOK_CFLOAT:
993 case TOK_LINENUM:
994 tab[0] = *p++;
995 break;
996 case TOK_STR:
997 case TOK_LSTR:
998 case TOK_PPNUM:
999 cv->cstr = (CString *)p;
1000 cv->cstr->data = (char *)p + sizeof(CString);
1001 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1002 break;
1003 case TOK_CDOUBLE:
1004 case TOK_CLLONG:
1005 case TOK_CULLONG:
1006 n = 2;
1007 goto copy;
1008 case TOK_CLDOUBLE:
1009 #if LDOUBLE_SIZE == 16
1010 n = 4;
1011 #elif LDOUBLE_SIZE == 12
1012 n = 3;
1013 #elif LDOUBLE_SIZE == 8
1014 n = 2;
1015 #else
1016 # error add long double size support
1017 #endif
1018 copy:
1020 *tab++ = *p++;
1021 while (--n);
1022 break;
1023 default:
1024 break;
1026 *pp = p;
1029 static int macro_is_equal(const int *a, const int *b)
1031 char buf[STRING_MAX_SIZE + 1];
1032 CValue cv;
1033 int t;
1034 while (*a && *b) {
1035 TOK_GET(&t, &a, &cv);
1036 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1037 TOK_GET(&t, &b, &cv);
1038 if (strcmp(buf, get_tok_str(t, &cv)))
1039 return 0;
1041 return !(*a || *b);
1044 static void define_print(Sym *s, int is_undef)
1046 int c, t;
1047 CValue cval;
1048 const int *str;
1049 Sym *arg;
1051 if (tcc_state->dflag == 0 || !s || !tcc_state->ppfp)
1052 return;
1054 if (file) {
1055 c = file->line_num - file->line_ref - 1;
1056 if (c > 0) {
1057 while (c--)
1058 fputs("\n", tcc_state->ppfp);
1059 file->line_ref = file->line_num;
1063 if (is_undef) {
1064 fprintf(tcc_state->ppfp, "// #undef %s\n", get_tok_str(s->v, NULL));
1065 return;
1068 fprintf(tcc_state->ppfp, "// #define %s", get_tok_str(s->v, NULL));
1069 arg = s->next;
1070 if (arg) {
1071 char *sep = "(";
1072 while (arg) {
1073 fprintf(tcc_state->ppfp, "%s%s", sep, get_tok_str(arg->v & ~SYM_FIELD, NULL));
1074 sep = ",";
1075 arg = arg->next;
1077 fprintf(tcc_state->ppfp, ")");
1080 str = s->d;
1081 if (str)
1082 fprintf(tcc_state->ppfp, " ");
1084 while (str) {
1085 TOK_GET(&t, &str, &cval);
1086 if (!t)
1087 break;
1088 fprintf(tcc_state->ppfp, "%s", get_tok_str(t, &cval));
1090 fprintf(tcc_state->ppfp, "\n");
1093 /* defines handling */
1094 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1096 Sym *s;
1098 s = define_find(v);
1099 if (s && !macro_is_equal(s->d, str))
1100 tcc_warning("%s redefined", get_tok_str(v, NULL));
1102 s = sym_push2(&define_stack, v, macro_type, 0);
1103 s->d = str;
1104 s->next = first_arg;
1105 table_ident[v - TOK_IDENT]->sym_define = s;
1106 define_print(s, 0);
1109 /* undefined a define symbol. Its name is just set to zero */
1110 ST_FUNC void define_undef(Sym *s)
1112 int v;
1113 v = s->v;
1114 if (v >= TOK_IDENT && v < tok_ident) {
1115 define_print(s, 1);
1116 table_ident[v - TOK_IDENT]->sym_define = NULL;
1118 s->v = 0;
1121 ST_INLN Sym *define_find(int v)
1123 v -= TOK_IDENT;
1124 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1125 return NULL;
1126 return table_ident[v]->sym_define;
1129 /* free define stack until top reaches 'b' */
1130 ST_FUNC void free_defines(Sym *b)
1132 Sym *top, *top1;
1133 int v;
1135 top = define_stack;
1136 while (top != b) {
1137 top1 = top->prev;
1138 /* do not free args or predefined defines */
1139 if (top->d)
1140 tok_str_free(top->d);
1141 v = top->v;
1142 if (v >= TOK_IDENT && v < tok_ident)
1143 table_ident[v - TOK_IDENT]->sym_define = NULL;
1144 sym_free(top);
1145 top = top1;
1147 define_stack = b;
1150 ST_FUNC void print_defines(void)
1152 Sym *top, *s;
1153 int v;
1155 top = define_stack;
1156 while (top) {
1157 v = top->v;
1158 if (v >= TOK_IDENT && v < tok_ident) {
1159 s = table_ident[v - TOK_IDENT]->sym_define;
1160 define_print(s, 0);
1162 top = top->prev;
1166 /* label lookup */
1167 ST_FUNC Sym *label_find(int v)
1169 v -= TOK_IDENT;
1170 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1171 return NULL;
1172 return table_ident[v]->sym_label;
1175 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1177 Sym *s, **ps;
1178 s = sym_push2(ptop, v, 0, 0);
1179 s->r = flags;
1180 ps = &table_ident[v - TOK_IDENT]->sym_label;
1181 if (ptop == &global_label_stack) {
1182 /* modify the top most local identifier, so that
1183 sym_identifier will point to 's' when popped */
1184 while (*ps != NULL)
1185 ps = &(*ps)->prev_tok;
1187 s->prev_tok = *ps;
1188 *ps = s;
1189 return s;
1192 /* pop labels until element last is reached. Look if any labels are
1193 undefined. Define symbols if '&&label' was used. */
1194 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1196 Sym *s, *s1;
1197 for(s = *ptop; s != slast; s = s1) {
1198 s1 = s->prev;
1199 if (s->r == LABEL_DECLARED) {
1200 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1201 } else if (s->r == LABEL_FORWARD) {
1202 tcc_error("label '%s' used but not defined",
1203 get_tok_str(s->v, NULL));
1204 } else {
1205 if (s->c) {
1206 /* define corresponding symbol. A size of
1207 1 is put. */
1208 put_extern_sym(s, cur_text_section, s->jnext, 1);
1211 /* remove label */
1212 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1213 sym_free(s);
1215 *ptop = slast;
1218 /* eval an expression for #if/#elif */
1219 static int expr_preprocess(void)
1221 int c, t;
1222 TokenString str;
1224 tok_str_new(&str);
1225 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1226 next(); /* do macro subst */
1227 if (tok == TOK_DEFINED) {
1228 next_nomacro();
1229 t = tok;
1230 if (t == '(')
1231 next_nomacro();
1232 c = define_find(tok) != 0;
1233 if (t == '(')
1234 next_nomacro();
1235 tok = TOK_CINT;
1236 tokc.i = c;
1237 } else if (tok >= TOK_IDENT) {
1238 /* if undefined macro */
1239 tok = TOK_CINT;
1240 tokc.i = 0;
1242 tok_str_add_tok(&str);
1244 tok_str_add(&str, -1); /* simulate end of file */
1245 tok_str_add(&str, 0);
1246 /* now evaluate C constant expression */
1247 macro_ptr = str.str;
1248 next();
1249 c = expr_const();
1250 macro_ptr = NULL;
1251 tok_str_free(str.str);
1252 return c != 0;
1255 /* parse after #define */
1256 ST_FUNC void parse_define(void)
1258 Sym *s, *first, **ps;
1259 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1260 TokenString str;
1262 v = tok;
1263 if (v < TOK_IDENT)
1264 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1265 /* XXX: should check if same macro (ANSI) */
1266 first = NULL;
1267 t = MACRO_OBJ;
1268 /* '(' must be just after macro definition for MACRO_FUNC */
1269 next_nomacro_spc();
1270 if (tok == '(') {
1271 next_nomacro();
1272 ps = &first;
1273 while (tok != ')') {
1274 varg = tok;
1275 next_nomacro();
1276 is_vaargs = 0;
1277 if (varg == TOK_DOTS) {
1278 varg = TOK___VA_ARGS__;
1279 is_vaargs = 1;
1280 } else if (tok == TOK_DOTS && gnu_ext) {
1281 is_vaargs = 1;
1282 next_nomacro();
1284 if (varg < TOK_IDENT)
1285 tcc_error( "\'%s\' may not appear in parameter list", get_tok_str(varg, NULL));
1286 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1287 *ps = s;
1288 ps = &s->next;
1289 if (tok != ',')
1290 continue;
1291 next_nomacro();
1293 next_nomacro_spc();
1294 t = MACRO_FUNC;
1296 tok_str_new(&str);
1297 spc = 2;
1298 /* EOF testing necessary for '-D' handling */
1299 ptok = 0;
1300 macro_list_start = 1;
1301 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1302 if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1303 tcc_error("'##' invalid at start of macro");
1304 ptok = tok;
1305 /* remove spaces around ## and after '#' */
1306 if (TOK_TWOSHARPS == tok) {
1307 if (1 == spc)
1308 --str.len;
1309 spc = 2;
1310 } else if ('#' == tok) {
1311 spc = 2;
1312 } else if (check_space(tok, &spc)) {
1313 goto skip;
1315 tok_str_add2(&str, tok, &tokc);
1316 skip:
1317 next_nomacro_spc();
1318 macro_list_start = 0;
1320 if (ptok == TOK_TWOSHARPS)
1321 tcc_error("'##' invalid at end of macro");
1322 if (spc == 1)
1323 --str.len; /* remove trailing space */
1324 tok_str_add(&str, 0);
1325 define_push(v, t, str.str, first);
1328 static inline int hash_cached_include(const char *filename)
1330 const unsigned char *s;
1331 unsigned int h;
1333 h = TOK_HASH_INIT;
1334 s = (unsigned char *) filename;
1335 while (*s) {
1336 h = TOK_HASH_FUNC(h, *s);
1337 s++;
1339 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1340 return h;
1343 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1345 CachedInclude *e;
1346 int i, h;
1347 h = hash_cached_include(filename);
1348 i = s1->cached_includes_hash[h];
1349 for(;;) {
1350 if (i == 0)
1351 break;
1352 e = s1->cached_includes[i - 1];
1353 if (0 == PATHCMP(e->filename, filename))
1354 return e;
1355 i = e->hash_next;
1357 return NULL;
1360 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1362 CachedInclude *e;
1363 int h;
1365 if (search_cached_include(s1, filename))
1366 return;
1367 #ifdef INC_DEBUG
1368 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1369 #endif
1370 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1371 strcpy(e->filename, filename);
1372 e->ifndef_macro = ifndef_macro;
1373 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1374 /* add in hash table */
1375 h = hash_cached_include(filename);
1376 e->hash_next = s1->cached_includes_hash[h];
1377 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1380 static void pragma_parse(TCCState *s1)
1382 int val;
1384 next();
1385 if (tok == TOK_pack) {
1387 This may be:
1388 #pragma pack(1) // set
1389 #pragma pack() // reset to default
1390 #pragma pack(push,1) // push & set
1391 #pragma pack(pop) // restore previous
1393 next();
1394 skip('(');
1395 if (tok == TOK_ASM_pop) {
1396 next();
1397 if (s1->pack_stack_ptr <= s1->pack_stack) {
1398 stk_error:
1399 tcc_error("out of pack stack");
1401 s1->pack_stack_ptr--;
1402 } else {
1403 val = 0;
1404 if (tok != ')') {
1405 if (tok == TOK_ASM_push) {
1406 next();
1407 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1408 goto stk_error;
1409 s1->pack_stack_ptr++;
1410 skip(',');
1412 if (tok != TOK_CINT) {
1413 pack_error:
1414 tcc_error("invalid pack pragma");
1416 val = tokc.i;
1417 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1418 goto pack_error;
1419 next();
1421 *s1->pack_stack_ptr = val;
1422 skip(')');
1424 } else if (tok == TOK_comment) {
1425 if (s1->ms_extensions) {
1426 next();
1427 skip('(');
1428 if (tok == TOK_lib) {
1429 next();
1430 skip(',');
1431 if (tok != TOK_STR) {
1432 tcc_error("invalid library specification");
1433 } else {
1434 int len = strlen((char *)tokc.cstr->data);
1435 char *file = tcc_malloc(len + 4); /* filetype, "-l", and \0 at the end */
1436 file[0] = TCC_FILETYPE_BINARY;
1437 file[1] = '-';
1438 file[2] = 'l';
1439 strcpy(&file[3],(char *)tokc.cstr->data);
1440 dynarray_add((void ***)&s1->files, &s1->nb_files, file);
1442 next();
1443 tok = TOK_LINEFEED;
1444 } else {
1445 tcc_warning("unknown specifier '%s' in #pragma comment", get_tok_str(tok, &tokc));
1447 } else {
1448 tcc_warning("#pragma comment(lib) is ignored");
1450 } else if ((tok == TOK_push_macro) || (tok == TOK_pop_macro)) {
1451 int push_macro = (tok == TOK_push_macro);
1452 next();
1453 skip('(');
1454 if (tok != TOK_STR) {
1455 expect("\"");
1456 } else {
1457 int len = strlen((char *)tokc.cstr->data);
1458 tcc_open_bf(s1, "<push_pop_macro>", len);
1459 memcpy(file->buffer, tokc.cstr->data, len);
1460 ch = file->buf_ptr[0];
1461 next_nomacro();
1462 if (tok >= TOK_IDENT) {
1463 Sym *s = table_ident[tok - TOK_IDENT]->sym_define;
1464 Sym *ss = table_ident[tok - TOK_IDENT]->sym_define_stack;
1465 if (push_macro) {
1466 if (s) {
1467 s->prev_tok = ss;
1468 table_ident[tok - TOK_IDENT]->sym_define_stack = s;
1470 } else {
1471 if (ss) {
1472 table_ident[tok - TOK_IDENT]->sym_define = ss;
1473 table_ident[tok - TOK_IDENT]->sym_define_stack = ss->prev_tok;
1477 tcc_close();
1479 next();
1480 skip(')');
1481 } else {
1482 tcc_warning("unknown #pragma %s", get_tok_str(tok, &tokc));
1486 /* is_bof is true if first non space token at beginning of file */
1487 ST_FUNC void preprocess(int is_bof)
1489 TCCState *s1 = tcc_state;
1490 int i, c, n, saved_parse_flags;
1491 char buf[1024], *q;
1492 Sym *s;
1494 saved_parse_flags = parse_flags;
1495 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1496 PARSE_FLAG_LINEFEED;
1497 parse_flags |= (saved_parse_flags & (PARSE_FLAG_ASM_FILE | PARSE_FLAG_ASM_COMMENTS));
1498 next_nomacro();
1499 redo:
1500 switch(tok) {
1501 case TOK_DEFINE:
1502 next_nomacro();
1503 parse_define();
1504 break;
1505 case TOK_UNDEF:
1506 next_nomacro();
1507 s = define_find(tok);
1508 /* undefine symbol by putting an invalid name */
1509 if (s)
1510 define_undef(s);
1511 break;
1512 case TOK_INCLUDE:
1513 case TOK_INCLUDE_NEXT:
1514 ch = file->buf_ptr[0];
1515 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1516 skip_spaces();
1517 if (ch == '<') {
1518 c = '>';
1519 goto read_name;
1520 } else if (ch == '\"') {
1521 c = ch;
1522 read_name:
1523 inp();
1524 q = buf;
1525 while (ch != c && ch != '\n' && ch != CH_EOF) {
1526 if ((q - buf) < sizeof(buf) - 1)
1527 *q++ = ch;
1528 if (ch == '\\') {
1529 if (handle_stray_noerror() == 0)
1530 --q;
1531 } else
1532 inp();
1534 *q = '\0';
1535 minp();
1536 #if 0
1537 /* eat all spaces and comments after include */
1538 /* XXX: slightly incorrect */
1539 while (ch1 != '\n' && ch1 != CH_EOF)
1540 inp();
1541 #endif
1542 } else {
1543 /* computed #include : either we have only strings or
1544 we have anything enclosed in '<>' */
1545 next();
1546 buf[0] = '\0';
1547 if (tok == TOK_STR) {
1548 while (tok != TOK_LINEFEED) {
1549 if (tok != TOK_STR) {
1550 include_syntax:
1551 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1553 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1554 next();
1556 c = '\"';
1557 } else {
1558 int len;
1559 while (tok != TOK_LINEFEED) {
1560 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1561 next();
1563 len = strlen(buf);
1564 /* check syntax and remove '<>' */
1565 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1566 goto include_syntax;
1567 memmove(buf, buf + 1, len - 2);
1568 buf[len - 2] = '\0';
1569 c = '>';
1573 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1574 tcc_error("#include recursion too deep");
1575 /* store current file in stack, but increment stack later below */
1576 *s1->include_stack_ptr = file;
1578 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1579 for (i = -2; i < n; ++i) {
1580 char buf1[sizeof file->filename];
1581 CachedInclude *e;
1582 BufferedFile **f;
1583 const char *path;
1585 if (i == -2) {
1586 /* check absolute include path */
1587 if (!IS_ABSPATH(buf))
1588 continue;
1589 buf1[0] = 0;
1590 i = n; /* force end loop */
1592 } else if (i == -1) {
1593 /* search in current dir if "header.h" */
1594 if (c != '\"')
1595 continue;
1596 path = file->filename;
1597 pstrncpy(buf1, path, tcc_basename(path) - path);
1599 } else {
1600 /* search in all the include paths */
1601 if (i < s1->nb_include_paths)
1602 path = s1->include_paths[i];
1603 else
1604 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1605 pstrcpy(buf1, sizeof(buf1), path);
1606 pstrcat(buf1, sizeof(buf1), "/");
1609 pstrcat(buf1, sizeof(buf1), buf);
1611 if (tok == TOK_INCLUDE_NEXT)
1612 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1613 if (0 == PATHCMP((*f)->filename, buf1)) {
1614 #ifdef INC_DEBUG
1615 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1616 #endif
1617 goto include_trynext;
1620 e = search_cached_include(s1, buf1);
1621 if (e && define_find(e->ifndef_macro)) {
1622 /* no need to parse the include because the 'ifndef macro'
1623 is defined */
1624 #ifdef INC_DEBUG
1625 printf("%s: skipping cached %s\n", file->filename, buf1);
1626 #endif
1627 goto include_done;
1630 if (tcc_open(s1, buf1) < 0)
1631 include_trynext:
1632 continue;
1634 #ifdef INC_DEBUG
1635 printf("%s: including %s\n", file->prev->filename, file->filename);
1636 #endif
1637 /* update target deps */
1638 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1639 tcc_strdup(buf1));
1640 /* push current file in stack */
1641 ++s1->include_stack_ptr;
1642 /* add include file debug info */
1643 if (s1->do_debug)
1644 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1645 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1646 ch = file->buf_ptr[0];
1647 goto the_end;
1649 tcc_error("include file '%s' not found", buf);
1650 include_done:
1651 break;
1652 case TOK_IFNDEF:
1653 c = 1;
1654 goto do_ifdef;
1655 case TOK_IF:
1656 c = expr_preprocess();
1657 goto do_if;
1658 case TOK_IFDEF:
1659 c = 0;
1660 do_ifdef:
1661 next_nomacro();
1662 if (tok < TOK_IDENT)
1663 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1664 if (is_bof) {
1665 if (c) {
1666 #ifdef INC_DEBUG
1667 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1668 #endif
1669 file->ifndef_macro = tok;
1672 c = (define_find(tok) != 0) ^ c;
1673 do_if:
1674 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1675 tcc_error("memory full (ifdef)");
1676 *s1->ifdef_stack_ptr++ = c;
1677 goto test_skip;
1678 case TOK_ELSE:
1679 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1680 tcc_error("#else without matching #if");
1681 if (s1->ifdef_stack_ptr[-1] & 2)
1682 tcc_error("#else after #else");
1683 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1684 goto test_else;
1685 case TOK_ELIF:
1686 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1687 tcc_error("#elif without matching #if");
1688 c = s1->ifdef_stack_ptr[-1];
1689 if (c > 1)
1690 tcc_error("#elif after #else");
1691 /* last #if/#elif expression was true: we skip */
1692 if (c == 1)
1693 goto skip;
1694 c = expr_preprocess();
1695 s1->ifdef_stack_ptr[-1] = c;
1696 test_else:
1697 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1698 file->ifndef_macro = 0;
1699 test_skip:
1700 if (!(c & 1)) {
1701 skip:
1702 preprocess_skip();
1703 is_bof = 0;
1704 goto redo;
1706 break;
1707 case TOK_ENDIF:
1708 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1709 tcc_error("#endif without matching #if");
1710 s1->ifdef_stack_ptr--;
1711 /* '#ifndef macro' was at the start of file. Now we check if
1712 an '#endif' is exactly at the end of file */
1713 if (file->ifndef_macro &&
1714 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1715 file->ifndef_macro_saved = file->ifndef_macro;
1716 /* need to set to zero to avoid false matches if another
1717 #ifndef at middle of file */
1718 file->ifndef_macro = 0;
1719 while (tok != TOK_LINEFEED)
1720 next_nomacro();
1721 tok_flags |= TOK_FLAG_ENDIF;
1722 goto the_end;
1724 break;
1725 case TOK_LINE:
1726 next();
1727 if (tok != TOK_CINT)
1728 tcc_error("A #line format is wrong");
1729 case TOK_PPNUM:
1730 if (tok != TOK_CINT) {
1731 char *p = tokc.cstr->data;
1732 tokc.i = strtoul(p, (char **)&p, 10);
1734 i = file->line_num;
1735 file->line_num = tokc.i - 1;
1736 next();
1737 if (tok != TOK_LINEFEED) {
1738 if (tok != TOK_STR) {
1739 if ((parse_flags & PARSE_FLAG_ASM_COMMENTS) == 0) {
1740 file->line_num = i;
1741 tcc_error("#line format is wrong");
1743 break;
1745 pstrcpy(file->filename, sizeof(file->filename),
1746 (char *)tokc.cstr->data);
1748 if (s1->do_debug)
1749 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1750 break;
1751 case TOK_ERROR:
1752 case TOK_WARNING:
1753 c = tok;
1754 ch = file->buf_ptr[0];
1755 skip_spaces();
1756 q = buf;
1757 while (ch != '\n' && ch != CH_EOF) {
1758 if ((q - buf) < sizeof(buf) - 1)
1759 *q++ = ch;
1760 if (ch == '\\') {
1761 if (handle_stray_noerror() == 0)
1762 --q;
1763 } else
1764 inp();
1766 *q = '\0';
1767 if (c == TOK_ERROR)
1768 tcc_error("#error %s", buf);
1769 else
1770 tcc_warning("#warning %s", buf);
1771 break;
1772 case TOK_PRAGMA:
1773 pragma_parse(s1);
1774 break;
1775 default:
1776 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1777 /* '!' is ignored to allow C scripts. numbers are ignored
1778 to emulate cpp behaviour */
1779 } else {
1780 if (!(parse_flags & PARSE_FLAG_ASM_COMMENTS))
1781 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1782 else {
1783 /* this is a gas line comment in an 'S' file. */
1784 file->buf_ptr = parse_line_comment(file->buf_ptr);
1785 goto the_end;
1788 break;
1790 /* ignore other preprocess commands or #! for C scripts */
1791 while (tok != TOK_LINEFEED)
1792 next_nomacro();
1793 the_end:
1794 parse_flags = saved_parse_flags;
1797 /* evaluate escape codes in a string. */
1798 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1800 int c, n;
1801 const uint8_t *p;
1803 p = buf;
1804 for(;;) {
1805 c = *p;
1806 if (c == '\0')
1807 break;
1808 if (c == '\\') {
1809 p++;
1810 /* escape */
1811 c = *p;
1812 switch(c) {
1813 case '0': case '1': case '2': case '3':
1814 case '4': case '5': case '6': case '7':
1815 /* at most three octal digits */
1816 n = c - '0';
1817 p++;
1818 c = *p;
1819 if (isoct(c)) {
1820 n = n * 8 + c - '0';
1821 p++;
1822 c = *p;
1823 if (isoct(c)) {
1824 n = n * 8 + c - '0';
1825 p++;
1828 c = n;
1829 goto add_char_nonext;
1830 case 'x':
1831 case 'u':
1832 case 'U':
1833 p++;
1834 n = 0;
1835 for(;;) {
1836 c = *p;
1837 if (c >= 'a' && c <= 'f')
1838 c = c - 'a' + 10;
1839 else if (c >= 'A' && c <= 'F')
1840 c = c - 'A' + 10;
1841 else if (isnum(c))
1842 c = c - '0';
1843 else
1844 break;
1845 n = n * 16 + c;
1846 p++;
1848 c = n;
1849 goto add_char_nonext;
1850 case 'a':
1851 c = '\a';
1852 break;
1853 case 'b':
1854 c = '\b';
1855 break;
1856 case 'f':
1857 c = '\f';
1858 break;
1859 case 'n':
1860 c = '\n';
1861 break;
1862 case 'r':
1863 c = '\r';
1864 break;
1865 case 't':
1866 c = '\t';
1867 break;
1868 case 'v':
1869 c = '\v';
1870 break;
1871 case 'e':
1872 if (!gnu_ext)
1873 goto invalid_escape;
1874 c = 27;
1875 break;
1876 case '\'':
1877 case '\"':
1878 case '\\':
1879 case '?':
1880 break;
1881 default:
1882 invalid_escape:
1883 if (c >= '!' && c <= '~')
1884 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1885 else
1886 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1887 break;
1890 p++;
1891 add_char_nonext:
1892 if (!is_long)
1893 cstr_ccat(outstr, c);
1894 else
1895 cstr_wccat(outstr, c);
1897 /* add a trailing '\0' */
1898 if (!is_long)
1899 cstr_ccat(outstr, '\0');
1900 else
1901 cstr_wccat(outstr, '\0');
1904 /* we use 64 bit numbers */
1905 #define BN_SIZE 2
1907 /* bn = (bn << shift) | or_val */
1908 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1910 int i;
1911 unsigned int v;
1912 for(i=0;i<BN_SIZE;i++) {
1913 v = bn[i];
1914 bn[i] = (v << shift) | or_val;
1915 or_val = v >> (32 - shift);
1919 static void bn_zero(unsigned int *bn)
1921 int i;
1922 for(i=0;i<BN_SIZE;i++) {
1923 bn[i] = 0;
1927 /* parse number in null terminated string 'p' and return it in the
1928 current token */
1929 static void parse_number(const char *p)
1931 int b, t, shift, frac_bits, s, exp_val, ch;
1932 char *q;
1933 unsigned int bn[BN_SIZE];
1934 double d;
1936 /* number */
1937 q = token_buf;
1938 ch = *p++;
1939 t = ch;
1940 ch = *p++;
1941 *q++ = t;
1942 b = 10;
1943 if (t == '.') {
1944 goto float_frac_parse;
1945 } else if (t == '0') {
1946 if (ch == 'x' || ch == 'X') {
1947 q--;
1948 ch = *p++;
1949 b = 16;
1950 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1951 q--;
1952 ch = *p++;
1953 b = 2;
1956 /* parse all digits. cannot check octal numbers at this stage
1957 because of floating point constants */
1958 while (1) {
1959 if (ch >= 'a' && ch <= 'f')
1960 t = ch - 'a' + 10;
1961 else if (ch >= 'A' && ch <= 'F')
1962 t = ch - 'A' + 10;
1963 else if (isnum(ch))
1964 t = ch - '0';
1965 else
1966 break;
1967 if (t >= b)
1968 break;
1969 if (q >= token_buf + STRING_MAX_SIZE) {
1970 num_too_long:
1971 tcc_error("number too long");
1973 *q++ = ch;
1974 ch = *p++;
1976 if (ch == '.' ||
1977 ((ch == 'e' || ch == 'E') && b == 10) ||
1978 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1979 if (b != 10) {
1980 /* NOTE: strtox should support that for hexa numbers, but
1981 non ISOC99 libcs do not support it, so we prefer to do
1982 it by hand */
1983 /* hexadecimal or binary floats */
1984 /* XXX: handle overflows */
1985 *q = '\0';
1986 if (b == 16)
1987 shift = 4;
1988 else
1989 shift = 1;
1990 bn_zero(bn);
1991 q = token_buf;
1992 while (1) {
1993 t = *q++;
1994 if (t == '\0') {
1995 break;
1996 } else if (t >= 'a') {
1997 t = t - 'a' + 10;
1998 } else if (t >= 'A') {
1999 t = t - 'A' + 10;
2000 } else {
2001 t = t - '0';
2003 bn_lshift(bn, shift, t);
2005 frac_bits = 0;
2006 if (ch == '.') {
2007 ch = *p++;
2008 while (1) {
2009 t = ch;
2010 if (t >= 'a' && t <= 'f') {
2011 t = t - 'a' + 10;
2012 } else if (t >= 'A' && t <= 'F') {
2013 t = t - 'A' + 10;
2014 } else if (t >= '0' && t <= '9') {
2015 t = t - '0';
2016 } else {
2017 break;
2019 if (t >= b)
2020 tcc_error("invalid digit");
2021 bn_lshift(bn, shift, t);
2022 frac_bits += shift;
2023 ch = *p++;
2026 if (ch != 'p' && ch != 'P')
2027 expect("exponent");
2028 ch = *p++;
2029 s = 1;
2030 exp_val = 0;
2031 if (ch == '+') {
2032 ch = *p++;
2033 } else if (ch == '-') {
2034 s = -1;
2035 ch = *p++;
2037 if (ch < '0' || ch > '9')
2038 expect("exponent digits");
2039 while (ch >= '0' && ch <= '9') {
2040 exp_val = exp_val * 10 + ch - '0';
2041 ch = *p++;
2043 exp_val = exp_val * s;
2045 /* now we can generate the number */
2046 /* XXX: should patch directly float number */
2047 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2048 d = ldexp(d, exp_val - frac_bits);
2049 t = toup(ch);
2050 if (t == 'F') {
2051 ch = *p++;
2052 tok = TOK_CFLOAT;
2053 /* float : should handle overflow */
2054 tokc.f = (float)d;
2055 } else if (t == 'L') {
2056 ch = *p++;
2057 #ifdef TCC_TARGET_PE
2058 tok = TOK_CDOUBLE;
2059 tokc.d = d;
2060 #else
2061 tok = TOK_CLDOUBLE;
2062 /* XXX: not large enough */
2063 tokc.ld = (long double)d;
2064 #endif
2065 } else {
2066 tok = TOK_CDOUBLE;
2067 tokc.d = d;
2069 } else {
2070 /* decimal floats */
2071 if (ch == '.') {
2072 if (q >= token_buf + STRING_MAX_SIZE)
2073 goto num_too_long;
2074 *q++ = ch;
2075 ch = *p++;
2076 float_frac_parse:
2077 while (ch >= '0' && ch <= '9') {
2078 if (q >= token_buf + STRING_MAX_SIZE)
2079 goto num_too_long;
2080 *q++ = ch;
2081 ch = *p++;
2084 if (ch == 'e' || ch == 'E') {
2085 if (q >= token_buf + STRING_MAX_SIZE)
2086 goto num_too_long;
2087 *q++ = ch;
2088 ch = *p++;
2089 if (ch == '-' || ch == '+') {
2090 if (q >= token_buf + STRING_MAX_SIZE)
2091 goto num_too_long;
2092 *q++ = ch;
2093 ch = *p++;
2095 if (ch < '0' || ch > '9')
2096 expect("exponent digits");
2097 while (ch >= '0' && ch <= '9') {
2098 if (q >= token_buf + STRING_MAX_SIZE)
2099 goto num_too_long;
2100 *q++ = ch;
2101 ch = *p++;
2104 *q = '\0';
2105 t = toup(ch);
2106 errno = 0;
2107 if (t == 'F') {
2108 ch = *p++;
2109 tok = TOK_CFLOAT;
2110 tokc.f = strtof(token_buf, NULL);
2111 } else if (t == 'L') {
2112 ch = *p++;
2113 #ifdef TCC_TARGET_PE
2114 tok = TOK_CDOUBLE;
2115 tokc.d = strtod(token_buf, NULL);
2116 #else
2117 tok = TOK_CLDOUBLE;
2118 tokc.ld = strtold(token_buf, NULL);
2119 #endif
2120 } else {
2121 tok = TOK_CDOUBLE;
2122 tokc.d = strtod(token_buf, NULL);
2125 } else {
2126 unsigned long long n, n1;
2127 int lcount, ucount, must_64bit;
2128 const char *p1;
2130 /* integer number */
2131 *q = '\0';
2132 q = token_buf;
2133 if (b == 10 && *q == '0') {
2134 b = 8;
2135 q++;
2137 n = 0;
2138 while(1) {
2139 t = *q++;
2140 /* no need for checks except for base 10 / 8 errors */
2141 if (t == '\0')
2142 break;
2143 else if (t >= 'a')
2144 t = t - 'a' + 10;
2145 else if (t >= 'A')
2146 t = t - 'A' + 10;
2147 else
2148 t = t - '0';
2149 if (t >= b)
2150 tcc_error("invalid digit");
2151 n1 = n;
2152 n = n * b + t;
2153 /* detect overflow */
2154 /* XXX: this test is not reliable */
2155 if (n < n1)
2156 tcc_error("integer constant overflow");
2159 /* Determine the characteristics (unsigned and/or 64bit) the type of
2160 the constant must have according to the constant suffix(es) */
2161 lcount = ucount = must_64bit = 0;
2162 p1 = p;
2163 for(;;) {
2164 t = toup(ch);
2165 if (t == 'L') {
2166 if (lcount >= 2)
2167 tcc_error("three 'l's in integer constant");
2168 if (lcount && *(p - 1) != ch)
2169 tcc_error("incorrect integer suffix: %s", p1);
2170 lcount++;
2171 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2172 if (lcount == 2)
2173 #endif
2174 must_64bit = 1;
2175 ch = *p++;
2176 } else if (t == 'U') {
2177 if (ucount >= 1)
2178 tcc_error("two 'u's in integer constant");
2179 ucount++;
2180 ch = *p++;
2181 } else {
2182 break;
2186 /* Whether 64 bits are needed to hold the constant's value */
2187 if (n & 0xffffffff00000000LL || must_64bit) {
2188 tok = TOK_CLLONG;
2189 n1 = n >> 32;
2190 } else {
2191 tok = TOK_CINT;
2192 n1 = n;
2195 /* Whether type must be unsigned to hold the constant's value */
2196 if (ucount || ((n1 >> 31) && (b != 10))) {
2197 if (tok == TOK_CLLONG)
2198 tok = TOK_CULLONG;
2199 else
2200 tok = TOK_CUINT;
2201 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2202 } else if (n1 >> 31) {
2203 if (tok == TOK_CINT)
2204 tok = TOK_CLLONG;
2205 else
2206 tcc_error("integer constant overflow");
2209 if (tok == TOK_CINT || tok == TOK_CUINT)
2210 tokc.ui = n;
2211 else
2212 tokc.ull = n;
2214 if (ch)
2215 tcc_error("invalid number\n");
2219 #define PARSE2(c1, tok1, c2, tok2) \
2220 case c1: \
2221 PEEKC(c, p); \
2222 if (c == c2) { \
2223 p++; \
2224 tok = tok2; \
2225 } else { \
2226 tok = tok1; \
2228 break;
2230 /* return next token without macro substitution */
2231 static inline void next_nomacro1(void)
2233 int t, c, is_long;
2234 TokenSym *ts;
2235 uint8_t *p, *p1;
2236 unsigned int h;
2238 p = file->buf_ptr;
2239 redo_no_start:
2240 c = *p;
2241 switch(c) {
2242 case ' ':
2243 case '\t':
2244 tok = c;
2245 p++;
2246 goto keep_tok_flags;
2247 case '\f':
2248 case '\v':
2249 case '\r':
2250 p++;
2251 goto redo_no_start;
2252 case '\\':
2253 /* first look if it is in fact an end of buffer */
2254 if (p >= file->buf_end) {
2255 file->buf_ptr = p;
2256 handle_eob();
2257 p = file->buf_ptr;
2258 if (p >= file->buf_end)
2259 goto parse_eof;
2260 else
2261 goto redo_no_start;
2262 } else {
2263 file->buf_ptr = p;
2264 ch = *p;
2265 handle_stray();
2266 p = file->buf_ptr;
2267 goto redo_no_start;
2269 parse_eof:
2271 TCCState *s1 = tcc_state;
2272 if ((parse_flags & PARSE_FLAG_LINEFEED)
2273 && !(tok_flags & TOK_FLAG_EOF)) {
2274 tok_flags |= TOK_FLAG_EOF;
2275 tok = TOK_LINEFEED;
2276 goto keep_tok_flags;
2277 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2278 tok = TOK_EOF;
2279 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2280 tcc_error("missing #endif");
2281 } else if (s1->include_stack_ptr == s1->include_stack) {
2282 /* no include left : end of file. */
2283 tok = TOK_EOF;
2284 } else {
2285 tok_flags &= ~TOK_FLAG_EOF;
2286 /* pop include file */
2288 /* test if previous '#endif' was after a #ifdef at
2289 start of file */
2290 if (tok_flags & TOK_FLAG_ENDIF) {
2291 #ifdef INC_DEBUG
2292 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2293 #endif
2294 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2295 tok_flags &= ~TOK_FLAG_ENDIF;
2298 /* add end of include file debug info */
2299 if (tcc_state->do_debug) {
2300 put_stabd(N_EINCL, 0, 0);
2302 /* pop include stack */
2303 tcc_close();
2304 s1->include_stack_ptr--;
2305 p = file->buf_ptr;
2306 goto redo_no_start;
2309 break;
2311 case '\n':
2312 file->line_num++;
2313 tok_flags |= TOK_FLAG_BOL;
2314 p++;
2315 maybe_newline:
2316 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2317 goto redo_no_start;
2318 tok = TOK_LINEFEED;
2319 goto keep_tok_flags;
2321 case '#':
2322 /* XXX: simplify */
2323 PEEKC(c, p);
2324 if (is_space(c) && (parse_flags & PARSE_FLAG_ASM_FILE)) {
2325 p = parse_line_comment(p);
2326 goto redo_no_start;
2328 else
2329 if ((tok_flags & TOK_FLAG_BOL) &&
2330 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2331 file->buf_ptr = p;
2332 preprocess(tok_flags & TOK_FLAG_BOF);
2333 p = file->buf_ptr;
2334 goto maybe_newline;
2335 } else {
2336 if (c == '#') {
2337 p++;
2338 tok = TOK_TWOSHARPS;
2339 } else {
2340 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2341 p = parse_line_comment(p - 1);
2342 goto redo_no_start;
2343 } else {
2344 tok = '#';
2348 break;
2350 /* dollar is allowed to start identifiers when not parsing asm */
2351 case '$':
2352 if (!tcc_state->dollars_in_identifiers
2353 || (parse_flags & PARSE_FLAG_ASM_FILE)) goto parse_simple;
2355 case 'a': case 'b': case 'c': case 'd':
2356 case 'e': case 'f': case 'g': case 'h':
2357 case 'i': case 'j': case 'k': case 'l':
2358 case 'm': case 'n': case 'o': case 'p':
2359 case 'q': case 'r': case 's': case 't':
2360 case 'u': case 'v': case 'w': case 'x':
2361 case 'y': case 'z':
2362 case 'A': case 'B': case 'C': case 'D':
2363 case 'E': case 'F': case 'G': case 'H':
2364 case 'I': case 'J': case 'K':
2365 case 'M': case 'N': case 'O': case 'P':
2366 case 'Q': case 'R': case 'S': case 'T':
2367 case 'U': case 'V': case 'W': case 'X':
2368 case 'Y': case 'Z':
2369 case '_':
2370 parse_ident_fast:
2371 p1 = p;
2372 h = TOK_HASH_INIT;
2373 h = TOK_HASH_FUNC(h, c);
2374 p++;
2375 for(;;) {
2376 c = *p;
2377 if (!isidnum_table[c-CH_EOF]
2378 && (tcc_state->dollars_in_identifiers ? (c != '$') : 1))
2379 break;
2380 h = TOK_HASH_FUNC(h, c);
2381 p++;
2383 if (c != '\\') {
2384 TokenSym **pts;
2385 int len;
2387 /* fast case : no stray found, so we have the full token
2388 and we have already hashed it */
2389 len = p - p1;
2390 h &= (TOK_HASH_SIZE - 1);
2391 pts = &hash_ident[h];
2392 for(;;) {
2393 ts = *pts;
2394 if (!ts)
2395 break;
2396 if (ts->len == len && !memcmp(ts->str, p1, len))
2397 goto token_found;
2398 pts = &(ts->hash_next);
2400 ts = tok_alloc_new(pts, (char *) p1, len);
2401 token_found: ;
2402 } else {
2403 /* slower case */
2404 cstr_reset(&tokcstr);
2406 while (p1 < p) {
2407 cstr_ccat(&tokcstr, *p1);
2408 p1++;
2410 p--;
2411 PEEKC(c, p);
2412 parse_ident_slow:
2413 while (isidnum_table[c-CH_EOF]
2414 || (tcc_state->dollars_in_identifiers ? (c == '$') : 0)) {
2415 cstr_ccat(&tokcstr, c);
2416 PEEKC(c, p);
2418 ts = tok_alloc(tokcstr.data, tokcstr.size);
2420 tok = ts->tok;
2421 break;
2422 case 'L':
2423 t = p[1];
2424 if (t != '\\' && t != '\'' && t != '\"') {
2425 /* fast case */
2426 goto parse_ident_fast;
2427 } else {
2428 PEEKC(c, p);
2429 if (c == '\'' || c == '\"') {
2430 is_long = 1;
2431 goto str_const;
2432 } else {
2433 cstr_reset(&tokcstr);
2434 cstr_ccat(&tokcstr, 'L');
2435 goto parse_ident_slow;
2438 break;
2439 case '0': case '1': case '2': case '3':
2440 case '4': case '5': case '6': case '7':
2441 case '8': case '9':
2443 cstr_reset(&tokcstr);
2444 /* after the first digit, accept digits, alpha, '.' or sign if
2445 prefixed by 'eEpP' */
2446 parse_num:
2447 for(;;) {
2448 t = c;
2449 cstr_ccat(&tokcstr, c);
2450 PEEKC(c, p);
2451 if (!(isnum(c) || isid(c) || c == '.' ||
2452 ((c == '+' || c == '-') &&
2453 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2454 break;
2456 /* We add a trailing '\0' to ease parsing */
2457 cstr_ccat(&tokcstr, '\0');
2458 tokc.cstr = &tokcstr;
2459 tok = TOK_PPNUM;
2460 break;
2461 case '.':
2462 /* special dot handling because it can also start a number */
2463 PEEKC(c, p);
2464 if (isnum(c)) {
2465 cstr_reset(&tokcstr);
2466 cstr_ccat(&tokcstr, '.');
2467 goto parse_num;
2468 } else if (c == '.') {
2469 PEEKC(c, p);
2470 if (c != '.') {
2471 if ((parse_flags & PARSE_FLAG_ASM_COMMENTS) == 0)
2472 expect("'.'");
2473 tok = '.';
2474 break;
2476 PEEKC(c, p);
2477 tok = TOK_DOTS;
2478 } else {
2479 tok = '.';
2481 break;
2482 case '\'':
2483 case '\"':
2484 is_long = 0;
2485 str_const:
2487 CString str;
2488 int sep;
2490 sep = c;
2492 /* parse the string */
2493 cstr_new(&str);
2494 p = parse_pp_string(p, sep, &str);
2495 cstr_ccat(&str, '\0');
2497 /* eval the escape (should be done as TOK_PPNUM) */
2498 cstr_reset(&tokcstr);
2499 parse_escape_string(&tokcstr, str.data, is_long);
2500 cstr_free(&str);
2502 if (sep == '\'') {
2503 int char_size;
2504 /* XXX: make it portable */
2505 if (!is_long)
2506 char_size = 1;
2507 else
2508 char_size = sizeof(nwchar_t);
2509 if (tokcstr.size <= char_size)
2510 tcc_error("empty character constant");
2511 if (tokcstr.size > 2 * char_size)
2512 tcc_warning("multi-character character constant");
2513 if (!is_long) {
2514 tokc.i = *(int8_t *)tokcstr.data;
2515 tok = TOK_CCHAR;
2516 } else {
2517 tokc.i = *(nwchar_t *)tokcstr.data;
2518 tok = TOK_LCHAR;
2520 } else {
2521 tokc.cstr = &tokcstr;
2522 if (!is_long)
2523 tok = TOK_STR;
2524 else
2525 tok = TOK_LSTR;
2528 break;
2530 case '<':
2531 PEEKC(c, p);
2532 if (c == '=') {
2533 p++;
2534 tok = TOK_LE;
2535 } else if (c == '<') {
2536 PEEKC(c, p);
2537 if (c == '=') {
2538 p++;
2539 tok = TOK_A_SHL;
2540 } else {
2541 tok = TOK_SHL;
2543 } else {
2544 tok = TOK_LT;
2546 break;
2548 case '>':
2549 PEEKC(c, p);
2550 if (c == '=') {
2551 p++;
2552 tok = TOK_GE;
2553 } else if (c == '>') {
2554 PEEKC(c, p);
2555 if (c == '=') {
2556 p++;
2557 tok = TOK_A_SAR;
2558 } else {
2559 tok = TOK_SAR;
2561 } else {
2562 tok = TOK_GT;
2564 break;
2566 case '&':
2567 PEEKC(c, p);
2568 if (c == '&') {
2569 p++;
2570 tok = TOK_LAND;
2571 } else if (c == '=') {
2572 p++;
2573 tok = TOK_A_AND;
2574 } else {
2575 tok = '&';
2577 break;
2579 case '|':
2580 PEEKC(c, p);
2581 if (c == '|') {
2582 p++;
2583 tok = TOK_LOR;
2584 } else if (c == '=') {
2585 p++;
2586 tok = TOK_A_OR;
2587 } else {
2588 tok = '|';
2590 break;
2592 case '+':
2593 PEEKC(c, p);
2594 if (c == '+') {
2595 p++;
2596 tok = TOK_INC;
2597 } else if (c == '=') {
2598 p++;
2599 tok = TOK_A_ADD;
2600 } else {
2601 tok = '+';
2603 break;
2605 case '-':
2606 PEEKC(c, p);
2607 if (c == '-') {
2608 p++;
2609 tok = TOK_DEC;
2610 } else if (c == '=') {
2611 p++;
2612 tok = TOK_A_SUB;
2613 } else if (c == '>') {
2614 p++;
2615 tok = TOK_ARROW;
2616 } else {
2617 tok = '-';
2619 break;
2621 PARSE2('!', '!', '=', TOK_NE)
2622 PARSE2('=', '=', '=', TOK_EQ)
2623 PARSE2('*', '*', '=', TOK_A_MUL)
2624 PARSE2('%', '%', '=', TOK_A_MOD)
2625 PARSE2('^', '^', '=', TOK_A_XOR)
2627 /* comments or operator */
2628 case '/':
2629 PEEKC(c, p);
2630 if (c == '*') {
2631 p = parse_comment(p);
2632 /* comments replaced by a blank */
2633 tok = ' ';
2634 goto keep_tok_flags;
2635 } else if (c == '/') {
2636 p = parse_line_comment(p);
2637 tok = ' ';
2638 goto keep_tok_flags;
2639 } else if (c == '=') {
2640 p++;
2641 tok = TOK_A_DIV;
2642 } else {
2643 tok = '/';
2645 break;
2647 /* simple tokens */
2648 case '(':
2649 case ')':
2650 case '[':
2651 case ']':
2652 case '{':
2653 case '}':
2654 case ',':
2655 case ';':
2656 case ':':
2657 case '?':
2658 case '~':
2659 case '@': /* only used in assembler */
2660 parse_simple:
2661 tok = c;
2662 p++;
2663 break;
2664 default:
2665 if ((parse_flags & PARSE_FLAG_ASM_FILE) == 0)
2666 tcc_error("unrecognized character \\x%02x", c);
2667 else {
2668 tok = ' ';
2669 p++;
2671 break;
2673 tok_flags = 0;
2674 keep_tok_flags:
2675 file->buf_ptr = p;
2676 #if defined(PARSE_DEBUG)
2677 printf("token = %s\n", get_tok_str(tok, &tokc));
2678 #endif
2681 /* return next token without macro substitution. Can read input from
2682 macro_ptr buffer */
2683 static void next_nomacro_spc(void)
2685 if (macro_ptr) {
2686 redo:
2687 tok = *macro_ptr;
2688 if (tok) {
2689 TOK_GET(&tok, &macro_ptr, &tokc);
2690 if (tok == TOK_LINENUM) {
2691 file->line_num = tokc.i;
2692 goto redo;
2695 } else {
2696 next_nomacro1();
2700 ST_FUNC void next_nomacro(void)
2702 do {
2703 next_nomacro_spc();
2704 } while (is_space(tok));
2707 /* substitute arguments in replacement lists in macro_str by the values in
2708 args (field d) and return allocated string */
2709 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2711 int last_tok, t, spc;
2712 const int *st;
2713 Sym *s;
2714 CValue cval;
2715 TokenString str;
2716 CString cstr;
2718 tok_str_new(&str);
2719 last_tok = 0;
2720 while(1) {
2721 TOK_GET(&t, &macro_str, &cval);
2722 if (!t)
2723 break;
2724 if (t == '#') {
2725 /* stringize */
2726 TOK_GET(&t, &macro_str, &cval);
2727 if (!t)
2728 break;
2729 s = sym_find2(args, t);
2730 if (s) {
2731 cstr_new(&cstr);
2732 st = s->d;
2733 spc = 0;
2734 while (*st) {
2735 TOK_GET(&t, &st, &cval);
2736 if (!check_space(t, &spc))
2737 cstr_cat(&cstr, get_tok_str(t, &cval));
2739 cstr.size -= spc;
2740 cstr_ccat(&cstr, '\0');
2741 #ifdef PP_DEBUG
2742 printf("stringize: %s\n", (char *)cstr.data);
2743 #endif
2744 /* add string */
2745 cval.cstr = &cstr;
2746 tok_str_add2(&str, TOK_STR, &cval);
2747 cstr_free(&cstr);
2748 } else {
2749 tok_str_add2(&str, t, &cval);
2751 } else if (t >= TOK_IDENT) {
2752 s = sym_find2(args, t);
2753 if (s) {
2754 st = s->d;
2755 /* if '##' is present before or after, no arg substitution */
2756 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2757 /* special case for var arg macros : ## eats the
2758 ',' if empty VA_ARGS variable. */
2759 /* XXX: test of the ',' is not 100%
2760 reliable. should fix it to avoid security
2761 problems */
2762 if (gnu_ext && s->type.t &&
2763 last_tok == TOK_TWOSHARPS &&
2764 str.len >= 2 && str.str[str.len - 2] == ',') {
2765 if (*st == TOK_PLCHLDR) {
2766 /* suppress ',' '##' */
2767 str.len -= 2;
2768 } else {
2769 /* suppress '##' and add variable */
2770 str.len--;
2771 goto add_var;
2773 } else {
2774 int t1;
2775 add_var:
2776 for(;;) {
2777 TOK_GET(&t1, &st, &cval);
2778 if (!t1)
2779 break;
2780 tok_str_add2(&str, t1, &cval);
2783 } else if (*st != TOK_PLCHLDR) {
2784 /* NOTE: the stream cannot be read when macro
2785 substituing an argument */
2786 macro_subst(&str, nested_list, st, NULL);
2788 } else {
2789 tok_str_add(&str, t);
2791 } else {
2792 tok_str_add2(&str, t, &cval);
2794 last_tok = t;
2796 tok_str_add(&str, 0);
2797 return str.str;
2800 static char const ab_month_name[12][4] =
2802 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2803 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2806 /* do macro substitution of current token with macro 's' and add
2807 result to (tok_str,tok_len). 'nested_list' is the list of all
2808 macros we got inside to avoid recursing. Return non zero if no
2809 substitution needs to be done */
2810 static int macro_subst_tok(TokenString *tok_str,
2811 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2813 Sym *args, *sa, *sa1;
2814 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2815 const int *p;
2816 TokenString str;
2817 char *cstrval;
2818 CValue cval;
2819 CString cstr;
2820 char buf[32];
2822 /* if symbol is a macro, prepare substitution */
2823 /* special macros */
2824 if (tok == TOK___LINE__) {
2825 snprintf(buf, sizeof(buf), "%d", file->line_num);
2826 cstrval = buf;
2827 t1 = TOK_PPNUM;
2828 goto add_cstr1;
2829 } else if (tok == TOK___FILE__) {
2830 cstrval = file->filename;
2831 goto add_cstr;
2832 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2833 time_t ti;
2834 struct tm *tm;
2836 time(&ti);
2837 tm = localtime(&ti);
2838 if (tok == TOK___DATE__) {
2839 snprintf(buf, sizeof(buf), "%s %2d %d",
2840 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2841 } else {
2842 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2843 tm->tm_hour, tm->tm_min, tm->tm_sec);
2845 cstrval = buf;
2846 add_cstr:
2847 t1 = TOK_STR;
2848 add_cstr1:
2849 cstr_new(&cstr);
2850 cstr_cat(&cstr, cstrval);
2851 cstr_ccat(&cstr, '\0');
2852 cval.cstr = &cstr;
2853 tok_str_add2(tok_str, t1, &cval);
2854 cstr_free(&cstr);
2855 } else {
2856 mstr = s->d;
2857 mstr_allocated = 0;
2858 if (s->type.t == MACRO_FUNC) {
2859 /* NOTE: we do not use next_nomacro to avoid eating the
2860 next token. XXX: find better solution */
2861 redo:
2862 if (macro_ptr) {
2863 p = macro_ptr;
2864 while (is_space(t = *p) || TOK_LINEFEED == t)
2865 ++p;
2866 if (t == 0 && can_read_stream) {
2867 /* end of macro stream: we must look at the token
2868 after in the file */
2869 struct macro_level *ml = *can_read_stream;
2870 macro_ptr = NULL;
2871 if (ml)
2873 macro_ptr = ml->p;
2874 ml->p = NULL;
2875 *can_read_stream = ml -> prev;
2877 /* also, end of scope for nested defined symbol */
2878 (*nested_list)->v = -1;
2879 goto redo;
2881 } else {
2882 ch = file->buf_ptr[0];
2883 while (is_space(ch) || ch == '\n' || ch == '/')
2885 if (ch == '/')
2887 int c;
2888 uint8_t *p = file->buf_ptr;
2889 PEEKC(c, p);
2890 if (c == '*') {
2891 p = parse_comment(p);
2892 file->buf_ptr = p - 1;
2893 } else if (c == '/') {
2894 p = parse_line_comment(p);
2895 file->buf_ptr = p - 1;
2896 } else
2897 break;
2899 cinp();
2901 t = ch;
2903 if (t != '(') /* no macro subst */
2904 return -1;
2906 /* argument macro */
2907 next_nomacro();
2908 next_nomacro();
2909 args = NULL;
2910 sa = s->next;
2911 /* NOTE: empty args are allowed, except if no args */
2912 for(;;) {
2913 /* handle '()' case */
2914 if (!args && !sa && tok == ')')
2915 break;
2916 if (!sa)
2917 tcc_error("macro '%s' used with too many args",
2918 get_tok_str(s->v, 0));
2919 tok_str_new(&str);
2920 parlevel = spc = 0;
2921 /* NOTE: non zero sa->t indicates VA_ARGS */
2922 while ((parlevel > 0 ||
2923 (tok != ')' &&
2924 (tok != ',' || sa->type.t))) &&
2925 tok != -1) {
2926 if (tok == '(')
2927 parlevel++;
2928 else if (tok == ')')
2929 parlevel--;
2930 if (tok == TOK_LINEFEED)
2931 tok = ' ';
2932 if (!check_space(tok, &spc))
2933 tok_str_add2(&str, tok, &tokc);
2934 next_nomacro_spc();
2936 if (!str.len)
2937 tok_str_add(&str, TOK_PLCHLDR);
2938 str.len -= spc;
2939 tok_str_add(&str, 0);
2940 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2941 sa1->d = str.str;
2942 sa = sa->next;
2943 if (tok == ')') {
2944 /* special case for gcc var args: add an empty
2945 var arg argument if it is omitted */
2946 if (sa && sa->type.t && gnu_ext)
2947 continue;
2948 else
2949 break;
2951 if (tok != ',')
2952 expect(",");
2953 next_nomacro();
2955 if (sa) {
2956 tcc_error("macro '%s' used with too few args",
2957 get_tok_str(s->v, 0));
2960 /* now subst each arg */
2961 mstr = macro_arg_subst(nested_list, mstr, args);
2962 /* free memory */
2963 sa = args;
2964 while (sa) {
2965 sa1 = sa->prev;
2966 tok_str_free(sa->d);
2967 sym_free(sa);
2968 sa = sa1;
2970 mstr_allocated = 1;
2972 sym_push2(nested_list, s->v, 0, 0);
2973 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2974 /* pop nested defined symbol */
2975 sa1 = *nested_list;
2976 *nested_list = sa1->prev;
2977 sym_free(sa1);
2978 if (mstr_allocated)
2979 tok_str_free(mstr);
2981 return 0;
2984 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2985 return the resulting string (which must be freed). */
2986 static inline int *macro_twosharps(const int *macro_str)
2988 const int *ptr;
2989 int t;
2990 TokenString macro_str1;
2991 CString cstr;
2992 int n, start_of_nosubsts;
2994 /* we search the first '##' */
2995 for(ptr = macro_str;;) {
2996 CValue cval;
2997 TOK_GET(&t, &ptr, &cval);
2998 if (t == TOK_TWOSHARPS)
2999 break;
3000 /* nothing more to do if end of string */
3001 if (t == 0)
3002 return NULL;
3005 /* we saw '##', so we need more processing to handle it */
3006 start_of_nosubsts = -1;
3007 tok_str_new(&macro_str1);
3008 for(ptr = macro_str;;) {
3009 TOK_GET(&tok, &ptr, &tokc);
3010 if (tok == 0)
3011 break;
3012 if (tok == TOK_TWOSHARPS)
3013 continue;
3014 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
3015 start_of_nosubsts = macro_str1.len;
3016 while (*ptr == TOK_TWOSHARPS) {
3017 /* given 'a##b', remove nosubsts preceding 'a' */
3018 if (start_of_nosubsts >= 0)
3019 macro_str1.len = start_of_nosubsts;
3020 /* given 'a##b', skip '##' */
3021 t = *++ptr;
3022 /* given 'a##b', remove nosubsts preceding 'b' */
3023 while (t == TOK_NOSUBST)
3024 t = *++ptr;
3025 if (t && t != TOK_TWOSHARPS) {
3026 CValue cval;
3027 TOK_GET(&t, &ptr, &cval);
3028 /* We concatenate the two tokens */
3029 cstr_new(&cstr);
3030 if (tok != TOK_PLCHLDR)
3031 cstr_cat(&cstr, get_tok_str(tok, &tokc));
3032 n = cstr.size;
3033 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
3034 cstr_cat(&cstr, get_tok_str(t, &cval));
3035 cstr_ccat(&cstr, '\0');
3037 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3038 memcpy(file->buffer, cstr.data, cstr.size);
3039 for (;;) {
3040 next_nomacro1();
3041 if (0 == *file->buf_ptr)
3042 break;
3043 tok_str_add2(&macro_str1, tok, &tokc);
3044 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
3045 n, cstr.data, (char*)cstr.data + n);
3047 tcc_close();
3048 cstr_free(&cstr);
3051 if (tok != TOK_NOSUBST) {
3052 tok_str_add2(&macro_str1, tok, &tokc);
3053 tok = ' ';
3054 start_of_nosubsts = -1;
3056 tok_str_add2(&macro_str1, tok, &tokc);
3058 tok_str_add(&macro_str1, 0);
3059 return macro_str1.str;
3063 /* do macro substitution of macro_str and add result to
3064 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3065 inside to avoid recursing. */
3066 static void macro_subst(TokenString *tok_str, Sym **nested_list,
3067 const int *macro_str, struct macro_level ** can_read_stream)
3069 Sym *s;
3070 int *macro_str1;
3071 const int *ptr;
3072 int t, ret, spc;
3073 CValue cval;
3074 struct macro_level ml;
3075 int force_blank;
3077 /* first scan for '##' operator handling */
3078 ptr = macro_str;
3079 macro_str1 = macro_twosharps(ptr);
3081 if (macro_str1)
3082 ptr = macro_str1;
3083 spc = 0;
3084 force_blank = 0;
3086 while (1) {
3087 /* NOTE: ptr == NULL can only happen if tokens are read from
3088 file stream due to a macro function call */
3089 if (ptr == NULL)
3090 break;
3091 TOK_GET(&t, &ptr, &cval);
3092 if (t == 0)
3093 break;
3094 if (t == TOK_NOSUBST) {
3095 /* following token has already been subst'd. just copy it on */
3096 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3097 TOK_GET(&t, &ptr, &cval);
3098 goto no_subst;
3100 s = define_find(t);
3101 if (s != NULL) {
3102 /* if nested substitution, do nothing */
3103 if (sym_find2(*nested_list, t)) {
3104 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3105 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3106 goto no_subst;
3108 ml.p = macro_ptr;
3109 if (can_read_stream)
3110 ml.prev = *can_read_stream, *can_read_stream = &ml;
3111 macro_ptr = (int *)ptr;
3112 tok = t;
3113 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3114 ptr = (int *)macro_ptr;
3115 macro_ptr = ml.p;
3116 if (can_read_stream && *can_read_stream == &ml)
3117 *can_read_stream = ml.prev;
3118 if (ret != 0)
3119 goto no_subst;
3120 if (parse_flags & PARSE_FLAG_SPACES)
3121 force_blank = 1;
3122 } else {
3123 no_subst:
3124 if (force_blank) {
3125 tok_str_add(tok_str, ' ');
3126 spc = 1;
3127 force_blank = 0;
3129 if (!check_space(t, &spc))
3130 tok_str_add2(tok_str, t, &cval);
3133 if (macro_str1)
3134 tok_str_free(macro_str1);
3137 /* return next token with macro substitution */
3138 ST_FUNC void next(void)
3140 Sym *nested_list, *s;
3141 TokenString str;
3142 struct macro_level *ml;
3144 redo:
3145 if (parse_flags & PARSE_FLAG_SPACES)
3146 next_nomacro_spc();
3147 else
3148 next_nomacro();
3149 if (!macro_ptr) {
3150 /* if not reading from macro substituted string, then try
3151 to substitute macros */
3152 if (tok >= TOK_IDENT &&
3153 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3154 s = define_find(tok);
3155 if (s) {
3156 /* we have a macro: we try to substitute */
3157 tok_str_new(&str);
3158 nested_list = NULL;
3159 ml = NULL;
3160 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3161 /* substitution done, NOTE: maybe empty */
3162 tok_str_add(&str, 0);
3163 macro_ptr = str.str;
3164 macro_ptr_allocated = str.str;
3165 goto redo;
3169 } else {
3170 if (tok == 0) {
3171 /* end of macro or end of unget buffer */
3172 if (unget_buffer_enabled) {
3173 macro_ptr = unget_saved_macro_ptr;
3174 unget_buffer_enabled = 0;
3175 } else {
3176 /* end of macro string: free it */
3177 tok_str_free(macro_ptr_allocated);
3178 macro_ptr_allocated = NULL;
3179 macro_ptr = NULL;
3181 goto redo;
3182 } else if (tok == TOK_NOSUBST) {
3183 /* discard preprocessor's nosubst markers */
3184 goto redo;
3188 /* convert preprocessor tokens into C tokens */
3189 if (tok == TOK_PPNUM &&
3190 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3191 parse_number((char *)tokc.cstr->data);
3195 /* push back current token and set current token to 'last_tok'. Only
3196 identifier case handled for labels. */
3197 ST_INLN void unget_tok(int last_tok)
3199 int i, n;
3200 int *q;
3201 if (unget_buffer_enabled)
3203 /* assert(macro_ptr == unget_saved_buffer + 1);
3204 assert(*macro_ptr == 0); */
3206 else
3208 unget_saved_macro_ptr = macro_ptr;
3209 unget_buffer_enabled = 1;
3211 q = unget_saved_buffer;
3212 macro_ptr = q;
3213 *q++ = tok;
3214 n = tok_ext_size(tok) - 1;
3215 for(i=0;i<n;i++)
3216 *q++ = tokc.tab[i];
3217 *q = 0; /* end of token string */
3218 tok = last_tok;
3222 /* better than nothing, but needs extension to handle '-E' option
3223 correctly too */
3224 ST_FUNC void preprocess_init(TCCState *s1)
3226 s1->include_stack_ptr = s1->include_stack;
3227 /* XXX: move that before to avoid having to initialize
3228 file->ifdef_stack_ptr ? */
3229 s1->ifdef_stack_ptr = s1->ifdef_stack;
3230 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3232 vtop = vstack - 1;
3233 s1->pack_stack[0] = 0;
3234 s1->pack_stack_ptr = s1->pack_stack;
3237 ST_FUNC void preprocess_new(void)
3239 int i, c;
3240 const char *p, *r;
3242 /* init isid table */
3244 for(i=CH_EOF;i<256;i++)
3245 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3247 /* add all tokens */
3248 if (table_ident) {
3249 tcc_free (table_ident);
3250 table_ident = NULL;
3252 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3254 tok_ident = TOK_IDENT;
3255 p = tcc_keywords;
3256 while (*p) {
3257 r = p;
3258 for(;;) {
3259 c = *r++;
3260 if (c == '\0')
3261 break;
3263 tok_alloc(p, r - p - 1);
3264 p = r;
3268 static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3270 switch (s1->Pflag) {
3271 case LINE_MACRO_OUTPUT_FORMAT_STD:
3272 /* "tcc -E -P1" case */
3273 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3274 break;
3276 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3277 /* "tcc -E -P" case: don't output a line directive */
3278 break;
3280 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3281 default:
3282 /* "tcc -E" case: a gcc standard by default */
3283 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3284 break;
3288 /* Preprocess the current file */
3289 ST_FUNC int tcc_preprocess(TCCState *s1)
3291 BufferedFile *file_ref, **iptr, **iptr_new;
3292 int token_seen, d;
3293 const char *s;
3295 preprocess_init(s1);
3296 ch = file->buf_ptr[0];
3297 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3298 parse_flags = (parse_flags & PARSE_FLAG_ASM_FILE);
3299 parse_flags |= PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3300 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3301 token_seen = 0;
3302 file->line_ref = 0;
3303 file_ref = NULL;
3304 iptr = s1->include_stack_ptr;
3306 for (;;) {
3307 next();
3308 if (tok == TOK_EOF) {
3309 break;
3310 } else if (file != file_ref) {
3311 if (file_ref)
3312 line_macro_output(file_ref, "", s1);
3313 goto print_line;
3314 } else if (tok == TOK_LINEFEED) {
3315 if (!token_seen)
3316 continue;
3317 file->line_ref++;
3318 token_seen = 0;
3319 } else if (!token_seen) {
3320 d = file->line_num - file->line_ref;
3321 if (file != file_ref || d >= 8) {
3322 print_line:
3323 s = "";
3324 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3325 iptr_new = s1->include_stack_ptr;
3326 s = iptr_new > iptr ? " 1"
3327 : iptr_new < iptr ? " 2"
3328 : iptr_new > s1->include_stack ? " 3"
3329 : ""
3332 line_macro_output(file, s, s1);
3333 } else {
3334 while (d > 0)
3335 fputs("\n", s1->ppfp), --d;
3337 file->line_ref = (file_ref = file)->line_num;
3338 token_seen = tok != TOK_LINEFEED;
3339 if (!token_seen)
3340 continue;
3342 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3344 return 0;