Implement -dM preprocessor option as in gcc
[tinycc.git] / tccpp.c
blob80c7dd841d66ad7808af037680d8d0cc8dcb0b4c
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 TokenSym *hash_ident[TOK_HASH_SIZE];
44 static char token_buf[STRING_MAX_SIZE + 1];
45 static unsigned char isidnum_table[256 - CH_EOF];
46 /* isidnum_table flags: */
47 #define IS_SPC 1
48 #define IS_ID 2
49 #define IS_NUM 4
51 static TokenString *macro_stack;
53 static const char tcc_keywords[] =
54 #define DEF(id, str) str "\0"
55 #include "tcctok.h"
56 #undef DEF
59 /* WARNING: the content of this string encodes token numbers */
60 static const unsigned char tok_two_chars[] =
61 /* outdated -- gr
62 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
63 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
64 */{
65 '<','=', TOK_LE,
66 '>','=', TOK_GE,
67 '!','=', TOK_NE,
68 '&','&', TOK_LAND,
69 '|','|', TOK_LOR,
70 '+','+', TOK_INC,
71 '-','-', TOK_DEC,
72 '=','=', TOK_EQ,
73 '<','<', TOK_SHL,
74 '>','>', TOK_SAR,
75 '+','=', TOK_A_ADD,
76 '-','=', TOK_A_SUB,
77 '*','=', TOK_A_MUL,
78 '/','=', TOK_A_DIV,
79 '%','=', TOK_A_MOD,
80 '&','=', TOK_A_AND,
81 '^','=', TOK_A_XOR,
82 '|','=', TOK_A_OR,
83 '-','>', TOK_ARROW,
84 '.','.', 0xa8, // C++ token ?
85 '#','#', TOK_TWOSHARPS,
89 static void next_nomacro_spc(void);
91 ST_FUNC void skip(int c)
93 if (tok != c)
94 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
95 next();
98 ST_FUNC void expect(const char *msg)
100 tcc_error("%s expected", msg);
103 ST_FUNC void begin_macro(TokenString *str, int alloc)
105 str->alloc = alloc;
106 str->prev = macro_stack;
107 str->prev_ptr = macro_ptr;
108 macro_ptr = str->str;
109 macro_stack = str;
112 ST_FUNC void end_macro(void)
114 TokenString *str = macro_stack;
115 macro_stack = str->prev;
116 macro_ptr = str->prev_ptr;
117 if (str->alloc == 2) {
118 str->alloc = 3; /* just mark as finished */
119 } else {
120 tok_str_free(str->str);
121 if (str->alloc == 1)
122 tcc_free(str);
126 /* ------------------------------------------------------------------------- */
127 /* CString handling */
128 static void cstr_realloc(CString *cstr, int new_size)
130 int size;
131 void *data;
133 size = cstr->size_allocated;
134 if (size == 0)
135 size = 8; /* no need to allocate a too small first string */
136 while (size < new_size)
137 size = size * 2;
138 data = tcc_realloc(cstr->data_allocated, size);
139 cstr->data_allocated = data;
140 cstr->size_allocated = size;
141 cstr->data = data;
144 /* add a byte */
145 ST_FUNC void cstr_ccat(CString *cstr, int ch)
147 int size;
148 size = cstr->size + 1;
149 if (size > cstr->size_allocated)
150 cstr_realloc(cstr, size);
151 ((unsigned char *)cstr->data)[size - 1] = ch;
152 cstr->size = size;
155 ST_FUNC void cstr_cat(CString *cstr, const char *str)
157 int c;
158 for(;;) {
159 c = *str;
160 if (c == '\0')
161 break;
162 cstr_ccat(cstr, c);
163 str++;
167 /* add a wide char */
168 ST_FUNC void cstr_wccat(CString *cstr, int ch)
170 int size;
171 size = cstr->size + sizeof(nwchar_t);
172 if (size > cstr->size_allocated)
173 cstr_realloc(cstr, size);
174 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
175 cstr->size = size;
178 ST_FUNC void cstr_new(CString *cstr)
180 memset(cstr, 0, sizeof(CString));
183 /* free string and reset it to NULL */
184 ST_FUNC void cstr_free(CString *cstr)
186 tcc_free(cstr->data_allocated);
187 cstr_new(cstr);
190 /* reset string to empty */
191 ST_FUNC void cstr_reset(CString *cstr)
193 cstr->size = 0;
196 /* XXX: unicode ? */
197 static void add_char(CString *cstr, int c)
199 if (c == '\'' || c == '\"' || c == '\\') {
200 /* XXX: could be more precise if char or string */
201 cstr_ccat(cstr, '\\');
203 if (c >= 32 && c <= 126) {
204 cstr_ccat(cstr, c);
205 } else {
206 cstr_ccat(cstr, '\\');
207 if (c == '\n') {
208 cstr_ccat(cstr, 'n');
209 } else {
210 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
211 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
212 cstr_ccat(cstr, '0' + (c & 7));
217 /* ------------------------------------------------------------------------- */
218 /* allocate a new token */
219 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
221 TokenSym *ts, **ptable;
222 int i;
224 if (tok_ident >= SYM_FIRST_ANOM)
225 tcc_error("memory full (symbols)");
227 /* expand token table if needed */
228 i = tok_ident - TOK_IDENT;
229 if ((i % TOK_ALLOC_INCR) == 0) {
230 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
231 table_ident = ptable;
234 ts = tcc_malloc(sizeof(TokenSym) + len);
235 table_ident[i] = ts;
236 ts->tok = tok_ident++;
237 ts->sym_define = NULL;
238 ts->sym_label = NULL;
239 ts->sym_struct = NULL;
240 ts->sym_identifier = NULL;
241 ts->len = len;
242 ts->hash_next = NULL;
243 memcpy(ts->str, str, len);
244 ts->str[len] = '\0';
245 *pts = ts;
246 return ts;
249 #define TOK_HASH_INIT 1
250 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
252 /* find a token and add it if not found */
253 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
255 TokenSym *ts, **pts;
256 int i;
257 unsigned int h;
259 h = TOK_HASH_INIT;
260 for(i=0;i<len;i++)
261 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
262 h &= (TOK_HASH_SIZE - 1);
264 pts = &hash_ident[h];
265 for(;;) {
266 ts = *pts;
267 if (!ts)
268 break;
269 if (ts->len == len && !memcmp(ts->str, str, len))
270 return ts;
271 pts = &(ts->hash_next);
273 return tok_alloc_new(pts, str, len);
276 /* XXX: buffer overflow */
277 /* XXX: float tokens */
278 ST_FUNC const char *get_tok_str(int v, CValue *cv)
280 static CString cstr_buf;
281 char *p;
282 int i, len;
284 /* first time preallocate static cstr_buf, next time only reset position to start */
285 if (!cstr_buf.data_allocated)
286 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
287 else
288 cstr_buf.size = 0;
289 p = cstr_buf.data;
291 switch(v) {
292 case TOK_CINT:
293 case TOK_CUINT:
294 /* XXX: not quite exact, but only useful for testing */
295 sprintf(p, "%llu", (unsigned long long)cv->i);
296 break;
297 case TOK_CLLONG:
298 case TOK_CULLONG:
299 /* XXX: not quite exact, but only useful for testing */
300 #ifdef _WIN32
301 sprintf(p, "%u", (unsigned)cv->i);
302 #else
303 sprintf(p, "%llu", (unsigned long long)cv->i);
304 #endif
305 break;
306 case TOK_LCHAR:
307 cstr_ccat(&cstr_buf, 'L');
308 case TOK_CCHAR:
309 cstr_ccat(&cstr_buf, '\'');
310 add_char(&cstr_buf, cv->i);
311 cstr_ccat(&cstr_buf, '\'');
312 cstr_ccat(&cstr_buf, '\0');
313 break;
314 case TOK_PPNUM:
315 case TOK_PPSTR:
316 return (char*)cv->str.data;
317 case TOK_LSTR:
318 cstr_ccat(&cstr_buf, 'L');
319 case TOK_STR:
320 cstr_ccat(&cstr_buf, '\"');
321 if (v == TOK_STR) {
322 len = cv->str.size - 1;
323 for(i=0;i<len;i++)
324 add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
325 } else {
326 len = (cv->str.size / sizeof(nwchar_t)) - 1;
327 for(i=0;i<len;i++)
328 add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
330 cstr_ccat(&cstr_buf, '\"');
331 cstr_ccat(&cstr_buf, '\0');
332 break;
334 case TOK_CFLOAT:
335 cstr_cat(&cstr_buf, "<float>");
336 cstr_ccat(&cstr_buf, '\0');
337 break;
338 case TOK_CDOUBLE:
339 cstr_cat(&cstr_buf, "<double>");
340 cstr_ccat(&cstr_buf, '\0');
341 break;
342 case TOK_CLDOUBLE:
343 cstr_cat(&cstr_buf, "<long double>");
344 cstr_ccat(&cstr_buf, '\0');
345 break;
346 case TOK_LINENUM:
347 cstr_cat(&cstr_buf, "<linenumber>");
348 cstr_ccat(&cstr_buf, '\0');
349 break;
351 /* above tokens have value, the ones below don't */
353 case TOK_LT:
354 v = '<';
355 goto addv;
356 case TOK_GT:
357 v = '>';
358 goto addv;
359 case TOK_DOTS:
360 return strcpy(p, "...");
361 case TOK_A_SHL:
362 return strcpy(p, "<<=");
363 case TOK_A_SAR:
364 return strcpy(p, ">>=");
365 default:
366 if (v < TOK_IDENT) {
367 /* search in two bytes table */
368 const unsigned char *q = tok_two_chars;
369 while (*q) {
370 if (q[2] == v) {
371 *p++ = q[0];
372 *p++ = q[1];
373 *p = '\0';
374 return cstr_buf.data;
376 q += 3;
378 if (v >= 127) {
379 sprintf(cstr_buf.data, "<%02x>", v);
380 return cstr_buf.data;
382 addv:
383 *p++ = v;
384 *p = '\0';
385 } else if (v < tok_ident) {
386 return table_ident[v - TOK_IDENT]->str;
387 } else if (v >= SYM_FIRST_ANOM) {
388 /* special name for anonymous symbol */
389 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
390 } else {
391 /* should never happen */
392 return NULL;
394 break;
396 return cstr_buf.data;
399 /* return the current character, handling end of block if necessary
400 (but not stray) */
401 ST_FUNC int handle_eob(void)
403 BufferedFile *bf = file;
404 int len;
406 /* only tries to read if really end of buffer */
407 if (bf->buf_ptr >= bf->buf_end) {
408 if (bf->fd != -1) {
409 #if defined(PARSE_DEBUG)
410 len = 1;
411 #else
412 len = IO_BUF_SIZE;
413 #endif
414 len = read(bf->fd, bf->buffer, len);
415 if (len < 0)
416 len = 0;
417 } else {
418 len = 0;
420 total_bytes += len;
421 bf->buf_ptr = bf->buffer;
422 bf->buf_end = bf->buffer + len;
423 *bf->buf_end = CH_EOB;
425 if (bf->buf_ptr < bf->buf_end) {
426 return bf->buf_ptr[0];
427 } else {
428 bf->buf_ptr = bf->buf_end;
429 return CH_EOF;
433 /* read next char from current input file and handle end of input buffer */
434 ST_INLN void inp(void)
436 ch = *(++(file->buf_ptr));
437 /* end of buffer/file handling */
438 if (ch == CH_EOB)
439 ch = handle_eob();
442 /* handle '\[\r]\n' */
443 static int handle_stray_noerror(void)
445 while (ch == '\\') {
446 inp();
447 if (ch == '\n') {
448 file->line_num++;
449 inp();
450 } else if (ch == '\r') {
451 inp();
452 if (ch != '\n')
453 goto fail;
454 file->line_num++;
455 inp();
456 } else {
457 fail:
458 return 1;
461 return 0;
464 static void handle_stray(void)
466 if (handle_stray_noerror())
467 tcc_error("stray '\\' in program");
470 /* skip the stray and handle the \\n case. Output an error if
471 incorrect char after the stray */
472 static int handle_stray1(uint8_t *p)
474 int c;
476 file->buf_ptr = p;
477 if (p >= file->buf_end) {
478 c = handle_eob();
479 if (c != '\\')
480 return c;
481 p = file->buf_ptr;
483 ch = *p;
484 if (handle_stray_noerror()) {
485 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
486 tcc_error("stray '\\' in program");
487 *--file->buf_ptr = '\\';
489 p = file->buf_ptr;
490 c = *p;
491 return c;
494 /* handle just the EOB case, but not stray */
495 #define PEEKC_EOB(c, p)\
497 p++;\
498 c = *p;\
499 if (c == '\\') {\
500 file->buf_ptr = p;\
501 c = handle_eob();\
502 p = file->buf_ptr;\
506 /* handle the complicated stray case */
507 #define PEEKC(c, p)\
509 p++;\
510 c = *p;\
511 if (c == '\\') {\
512 c = handle_stray1(p);\
513 p = file->buf_ptr;\
517 /* input with '\[\r]\n' handling. Note that this function cannot
518 handle other characters after '\', so you cannot call it inside
519 strings or comments */
520 ST_FUNC void minp(void)
522 inp();
523 if (ch == '\\')
524 handle_stray();
528 /* single line C++ comments */
529 static uint8_t *parse_line_comment(uint8_t *p)
531 int c;
533 p++;
534 for(;;) {
535 c = *p;
536 redo:
537 if (c == '\n' || c == CH_EOF) {
538 break;
539 } else if (c == '\\') {
540 file->buf_ptr = p;
541 c = handle_eob();
542 p = file->buf_ptr;
543 if (c == '\\') {
544 PEEKC_EOB(c, p);
545 if (c == '\n') {
546 file->line_num++;
547 PEEKC_EOB(c, p);
548 } else if (c == '\r') {
549 PEEKC_EOB(c, p);
550 if (c == '\n') {
551 file->line_num++;
552 PEEKC_EOB(c, p);
555 } else {
556 goto redo;
558 } else {
559 p++;
562 return p;
565 /* C comments */
566 ST_FUNC uint8_t *parse_comment(uint8_t *p)
568 int c;
570 p++;
571 for(;;) {
572 /* fast skip loop */
573 for(;;) {
574 c = *p;
575 if (c == '\n' || c == '*' || c == '\\')
576 break;
577 p++;
578 c = *p;
579 if (c == '\n' || c == '*' || c == '\\')
580 break;
581 p++;
583 /* now we can handle all the cases */
584 if (c == '\n') {
585 file->line_num++;
586 p++;
587 } else if (c == '*') {
588 p++;
589 for(;;) {
590 c = *p;
591 if (c == '*') {
592 p++;
593 } else if (c == '/') {
594 goto end_of_comment;
595 } else if (c == '\\') {
596 file->buf_ptr = p;
597 c = handle_eob();
598 p = file->buf_ptr;
599 if (c == CH_EOF)
600 tcc_error("unexpected end of file in comment");
601 if (c == '\\') {
602 /* skip '\[\r]\n', otherwise just skip the stray */
603 while (c == '\\') {
604 PEEKC_EOB(c, p);
605 if (c == '\n') {
606 file->line_num++;
607 PEEKC_EOB(c, p);
608 } else if (c == '\r') {
609 PEEKC_EOB(c, p);
610 if (c == '\n') {
611 file->line_num++;
612 PEEKC_EOB(c, p);
614 } else {
615 goto after_star;
619 } else {
620 break;
623 after_star: ;
624 } else {
625 /* stray, eob or eof */
626 file->buf_ptr = p;
627 c = handle_eob();
628 p = file->buf_ptr;
629 if (c == CH_EOF) {
630 tcc_error("unexpected end of file in comment");
631 } else if (c == '\\') {
632 p++;
636 end_of_comment:
637 p++;
638 return p;
641 #define cinp minp
643 static inline void skip_spaces(void)
645 while (isidnum_table[ch - CH_EOF] & IS_SPC)
646 cinp();
649 static inline int check_space(int t, int *spc)
651 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
652 if (*spc)
653 return 1;
654 *spc = 1;
655 } else
656 *spc = 0;
657 return 0;
660 /* parse a string without interpreting escapes */
661 static uint8_t *parse_pp_string(uint8_t *p,
662 int sep, CString *str)
664 int c;
665 p++;
666 for(;;) {
667 c = *p;
668 if (c == sep) {
669 break;
670 } else if (c == '\\') {
671 file->buf_ptr = p;
672 c = handle_eob();
673 p = file->buf_ptr;
674 if (c == CH_EOF) {
675 unterminated_string:
676 /* XXX: indicate line number of start of string */
677 tcc_error("missing terminating %c character", sep);
678 } else if (c == '\\') {
679 /* escape : just skip \[\r]\n */
680 PEEKC_EOB(c, p);
681 if (c == '\n') {
682 file->line_num++;
683 p++;
684 } else if (c == '\r') {
685 PEEKC_EOB(c, p);
686 if (c != '\n')
687 expect("'\n' after '\r'");
688 file->line_num++;
689 p++;
690 } else if (c == CH_EOF) {
691 goto unterminated_string;
692 } else {
693 if (str) {
694 cstr_ccat(str, '\\');
695 cstr_ccat(str, c);
697 p++;
700 } else if (c == '\n') {
701 file->line_num++;
702 goto add_char;
703 } else if (c == '\r') {
704 PEEKC_EOB(c, p);
705 if (c != '\n') {
706 if (str)
707 cstr_ccat(str, '\r');
708 } else {
709 file->line_num++;
710 goto add_char;
712 } else {
713 add_char:
714 if (str)
715 cstr_ccat(str, c);
716 p++;
719 p++;
720 return p;
723 /* skip block of text until #else, #elif or #endif. skip also pairs of
724 #if/#endif */
725 static void preprocess_skip(void)
727 int a, start_of_line, c, in_warn_or_error;
728 uint8_t *p;
730 p = file->buf_ptr;
731 a = 0;
732 redo_start:
733 start_of_line = 1;
734 in_warn_or_error = 0;
735 for(;;) {
736 redo_no_start:
737 c = *p;
738 switch(c) {
739 case ' ':
740 case '\t':
741 case '\f':
742 case '\v':
743 case '\r':
744 p++;
745 goto redo_no_start;
746 case '\n':
747 file->line_num++;
748 p++;
749 goto redo_start;
750 case '\\':
751 file->buf_ptr = p;
752 c = handle_eob();
753 if (c == CH_EOF) {
754 expect("#endif");
755 } else if (c == '\\') {
756 ch = file->buf_ptr[0];
757 handle_stray_noerror();
759 p = file->buf_ptr;
760 goto redo_no_start;
761 /* skip strings */
762 case '\"':
763 case '\'':
764 if (in_warn_or_error)
765 goto _default;
766 p = parse_pp_string(p, c, NULL);
767 break;
768 /* skip comments */
769 case '/':
770 if (in_warn_or_error)
771 goto _default;
772 file->buf_ptr = p;
773 ch = *p;
774 minp();
775 p = file->buf_ptr;
776 if (ch == '*') {
777 p = parse_comment(p);
778 } else if (ch == '/') {
779 p = parse_line_comment(p);
781 break;
782 case '#':
783 p++;
784 if (start_of_line) {
785 file->buf_ptr = p;
786 next_nomacro();
787 p = file->buf_ptr;
788 if (a == 0 &&
789 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
790 goto the_end;
791 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
792 a++;
793 else if (tok == TOK_ENDIF)
794 a--;
795 else if( tok == TOK_ERROR || tok == TOK_WARNING)
796 in_warn_or_error = 1;
797 else if (tok == TOK_LINEFEED)
798 goto redo_start;
799 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
800 p = parse_line_comment(p);
801 break;
802 _default:
803 default:
804 p++;
805 break;
807 start_of_line = 0;
809 the_end: ;
810 file->buf_ptr = p;
813 /* ParseState handling */
815 /* XXX: currently, no include file info is stored. Thus, we cannot display
816 accurate messages if the function or data definition spans multiple
817 files */
819 /* save current parse state in 's' */
820 ST_FUNC void save_parse_state(ParseState *s)
822 s->line_num = file->line_num;
823 s->macro_ptr = macro_ptr;
824 s->tok = tok;
825 s->tokc = tokc;
828 /* restore parse state from 's' */
829 ST_FUNC void restore_parse_state(ParseState *s)
831 file->line_num = s->line_num;
832 macro_ptr = s->macro_ptr;
833 tok = s->tok;
834 tokc = s->tokc;
837 /* return the number of additional 'ints' necessary to store the
838 token */
839 static inline int tok_size(const int *p)
841 switch(*p) {
842 /* 4 bytes */
843 case TOK_CINT:
844 case TOK_CUINT:
845 case TOK_CCHAR:
846 case TOK_LCHAR:
847 case TOK_CFLOAT:
848 case TOK_LINENUM:
849 return 1 + 1;
850 case TOK_STR:
851 case TOK_LSTR:
852 case TOK_PPNUM:
853 case TOK_PPSTR:
854 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
855 case TOK_CDOUBLE:
856 case TOK_CLLONG:
857 case TOK_CULLONG:
858 return 1 + 2;
859 case TOK_CLDOUBLE:
860 return 1 + LDOUBLE_SIZE / 4;
861 default:
862 return 1 + 0;
866 /* token string handling */
868 ST_INLN void tok_str_new(TokenString *s)
870 s->str = NULL;
871 s->len = 0;
872 s->allocated_len = 0;
873 s->last_line_num = -1;
876 ST_FUNC void tok_str_free(int *str)
878 tcc_free(str);
881 static int *tok_str_realloc(TokenString *s)
883 int *str, len;
885 if (s->allocated_len == 0) {
886 len = 8;
887 } else {
888 len = s->allocated_len * 2;
890 str = tcc_realloc(s->str, len * sizeof(int));
891 s->allocated_len = len;
892 s->str = str;
893 return str;
896 ST_FUNC void tok_str_add(TokenString *s, int t)
898 int len, *str;
900 len = s->len;
901 str = s->str;
902 if (len >= s->allocated_len)
903 str = tok_str_realloc(s);
904 str[len++] = t;
905 s->len = len;
908 static void tok_str_add2(TokenString *s, int t, CValue *cv)
910 int len, *str;
912 len = s->len;
913 str = s->str;
915 /* allocate space for worst case */
916 if (len + TOK_MAX_SIZE > s->allocated_len)
917 str = tok_str_realloc(s);
918 str[len++] = t;
919 switch(t) {
920 case TOK_CINT:
921 case TOK_CUINT:
922 case TOK_CCHAR:
923 case TOK_LCHAR:
924 case TOK_CFLOAT:
925 case TOK_LINENUM:
926 str[len++] = cv->tab[0];
927 break;
928 case TOK_PPNUM:
929 case TOK_PPSTR:
930 case TOK_STR:
931 case TOK_LSTR:
933 /* Insert the string into the int array. */
934 size_t nb_words =
935 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
936 while ((len + nb_words) > s->allocated_len)
937 str = tok_str_realloc(s);
938 str[len] = cv->str.size;
939 memcpy(&str[len + 1], cv->str.data, cv->str.size);
940 len += nb_words;
942 break;
943 case TOK_CDOUBLE:
944 case TOK_CLLONG:
945 case TOK_CULLONG:
946 #if LDOUBLE_SIZE == 8
947 case TOK_CLDOUBLE:
948 #endif
949 str[len++] = cv->tab[0];
950 str[len++] = cv->tab[1];
951 break;
952 #if LDOUBLE_SIZE == 12
953 case TOK_CLDOUBLE:
954 str[len++] = cv->tab[0];
955 str[len++] = cv->tab[1];
956 str[len++] = cv->tab[2];
957 #elif LDOUBLE_SIZE == 16
958 case TOK_CLDOUBLE:
959 str[len++] = cv->tab[0];
960 str[len++] = cv->tab[1];
961 str[len++] = cv->tab[2];
962 str[len++] = cv->tab[3];
963 #elif LDOUBLE_SIZE != 8
964 #error add long double size support
965 #endif
966 break;
967 default:
968 break;
970 s->len = len;
973 /* add the current parse token in token string 's' */
974 ST_FUNC void tok_str_add_tok(TokenString *s)
976 CValue cval;
978 /* save line number info */
979 if (file->line_num != s->last_line_num) {
980 s->last_line_num = file->line_num;
981 cval.i = s->last_line_num;
982 tok_str_add2(s, TOK_LINENUM, &cval);
984 tok_str_add2(s, tok, &tokc);
987 /* get a token from an integer array and increment pointer
988 accordingly. we code it as a macro to avoid pointer aliasing. */
989 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
991 const int *p = *pp;
992 int n, *tab;
994 tab = cv->tab;
995 switch(*t = *p++) {
996 case TOK_CINT:
997 case TOK_CUINT:
998 case TOK_CCHAR:
999 case TOK_LCHAR:
1000 case TOK_CFLOAT:
1001 case TOK_LINENUM:
1002 tab[0] = *p++;
1003 break;
1004 case TOK_STR:
1005 case TOK_LSTR:
1006 case TOK_PPNUM:
1007 case TOK_PPSTR:
1008 cv->str.size = *p++;
1009 cv->str.data = p;
1010 cv->str.data_allocated = 0;
1011 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1012 break;
1013 case TOK_CDOUBLE:
1014 case TOK_CLLONG:
1015 case TOK_CULLONG:
1016 n = 2;
1017 goto copy;
1018 case TOK_CLDOUBLE:
1019 #if LDOUBLE_SIZE == 16
1020 n = 4;
1021 #elif LDOUBLE_SIZE == 12
1022 n = 3;
1023 #elif LDOUBLE_SIZE == 8
1024 n = 2;
1025 #else
1026 # error add long double size support
1027 #endif
1028 copy:
1030 *tab++ = *p++;
1031 while (--n);
1032 break;
1033 default:
1034 break;
1036 *pp = p;
1039 /* Calling this function is expensive, but it is not possible
1040 to read a token string backwards. */
1041 static int tok_last(const int *str0, const int *str1)
1043 const int *str = str0;
1044 int tok = 0;
1045 CValue cval;
1047 while (str < str1)
1048 TOK_GET(&tok, &str, &cval);
1049 return tok;
1052 static int macro_is_equal(const int *a, const int *b)
1054 static CString cstr_buf;
1055 CValue cv;
1056 int t;
1057 while (*a && *b) {
1058 /* first time preallocate static cstr_buf, next time only reset position to start */
1059 if (!cstr_buf.data_allocated)
1060 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
1061 else
1062 cstr_buf.size = 0;
1063 TOK_GET(&t, &a, &cv);
1064 cstr_cat(&cstr_buf, get_tok_str(t, &cv));
1065 cstr_ccat(&cstr_buf, '\0');
1066 TOK_GET(&t, &b, &cv);
1067 if (strcmp(cstr_buf.data, get_tok_str(t, &cv)))
1068 return 0;
1070 return !(*a || *b);
1073 static void pp_line(TCCState *s1, BufferedFile *f, int level)
1075 int d = f->line_num - f->line_ref;
1076 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE
1077 || (level == 0 && f->line_ref && d < 8))
1079 while (d > 0)
1080 fputs("\n", s1->ppfp), --d;
1082 else
1083 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
1084 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
1086 else {
1087 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
1088 level > 0 ? " 1" : level < 0 ? " 2" : "");
1090 f->line_ref = f->line_num;
1093 static void tok_print(const char *msg, const int *str)
1095 FILE *pr = tcc_state->dffp;
1096 int t;
1097 CValue cval;
1099 fprintf(pr, "%s ", msg);
1100 while (str) {
1101 TOK_GET(&t, &str, &cval);
1102 if (!t)
1103 break;
1104 fprintf(pr,"%s", get_tok_str(t, &cval));
1106 fprintf(pr, "\n");
1109 static int define_print_prepared(Sym *s)
1111 if (!s || !tcc_state->dffp || tcc_state->dflag == 0)
1112 return 0;
1114 if (s->v < TOK_IDENT || s->v >= tok_ident)
1115 return 0;
1117 if (file && tcc_state->dflag == 'D') {
1118 file->line_num--;
1119 pp_line(tcc_state, file, 0);
1120 file->line_ref = ++file->line_num;
1122 return 1;
1125 static void define_print(int v)
1127 FILE *pr = tcc_state->dffp;
1128 Sym *s, *a;
1130 s = define_find(v);
1131 if (define_print_prepared(s) == 0)
1132 return;
1134 fprintf(pr, "// #define %s", get_tok_str(v, NULL));
1135 if (s->type.t == MACRO_FUNC) {
1136 a = s->next;
1137 fprintf(pr,"(");
1138 if (a)
1139 for (;;) {
1140 fprintf(pr,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
1141 if (!(a = a->next))
1142 break;
1143 fprintf(pr,",");
1145 fprintf(pr,")");
1147 tok_print("", s->d);
1150 static void undef_print(int v)
1152 FILE *pr = tcc_state->dffp;
1153 Sym *s;
1155 s = define_find(v);
1156 if (define_print_prepared(s) == 0)
1157 return;
1159 fprintf(pr, "// #undef %s\n", get_tok_str(s->v, NULL));
1162 ST_FUNC void print_defines(void)
1164 Sym *top = define_stack;
1165 while (top) {
1166 define_print(top->v);
1167 top = top->prev;
1171 /* defines handling */
1172 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1174 Sym *s;
1176 s = define_find(v);
1177 if (s && !macro_is_equal(s->d, str))
1178 tcc_warning("%s redefined", get_tok_str(v, NULL));
1180 s = sym_push2(&define_stack, v, macro_type, 0);
1181 s->d = str;
1182 s->next = first_arg;
1183 table_ident[v - TOK_IDENT]->sym_define = s;
1186 /* undefined a define symbol. Its name is just set to zero */
1187 ST_FUNC void define_undef(Sym *s)
1189 int v = s->v;
1190 undef_print(v);
1191 if (v >= TOK_IDENT && v < tok_ident)
1192 table_ident[v - TOK_IDENT]->sym_define = NULL;
1195 ST_INLN Sym *define_find(int v)
1197 v -= TOK_IDENT;
1198 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1199 return NULL;
1200 return table_ident[v]->sym_define;
1203 /* free define stack until top reaches 'b' */
1204 ST_FUNC void free_defines(Sym *b)
1206 Sym *top, *top1;
1207 int v;
1209 top = define_stack;
1210 while (top != b) {
1211 top1 = top->prev;
1212 /* do not free args or predefined defines */
1213 if (top->d)
1214 tok_str_free(top->d);
1215 v = top->v;
1216 if (v >= TOK_IDENT && v < tok_ident)
1217 table_ident[v - TOK_IDENT]->sym_define = NULL;
1218 sym_free(top);
1219 top = top1;
1221 define_stack = b;
1224 /* label lookup */
1225 ST_FUNC Sym *label_find(int v)
1227 v -= TOK_IDENT;
1228 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1229 return NULL;
1230 return table_ident[v]->sym_label;
1233 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1235 Sym *s, **ps;
1236 s = sym_push2(ptop, v, 0, 0);
1237 s->r = flags;
1238 ps = &table_ident[v - TOK_IDENT]->sym_label;
1239 if (ptop == &global_label_stack) {
1240 /* modify the top most local identifier, so that
1241 sym_identifier will point to 's' when popped */
1242 while (*ps != NULL)
1243 ps = &(*ps)->prev_tok;
1245 s->prev_tok = *ps;
1246 *ps = s;
1247 return s;
1250 /* pop labels until element last is reached. Look if any labels are
1251 undefined. Define symbols if '&&label' was used. */
1252 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1254 Sym *s, *s1;
1255 for(s = *ptop; s != slast; s = s1) {
1256 s1 = s->prev;
1257 if (s->r == LABEL_DECLARED) {
1258 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1259 } else if (s->r == LABEL_FORWARD) {
1260 tcc_error("label '%s' used but not defined",
1261 get_tok_str(s->v, NULL));
1262 } else {
1263 if (s->c) {
1264 /* define corresponding symbol. A size of
1265 1 is put. */
1266 put_extern_sym(s, cur_text_section, s->jnext, 1);
1269 /* remove label */
1270 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1271 sym_free(s);
1273 *ptop = slast;
1276 /* eval an expression for #if/#elif */
1277 static int expr_preprocess(void)
1279 int c, t;
1280 TokenString str;
1282 tok_str_new(&str);
1283 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1284 next(); /* do macro subst */
1285 if (tok == TOK_DEFINED) {
1286 next_nomacro();
1287 t = tok;
1288 if (t == '(')
1289 next_nomacro();
1290 c = define_find(tok) != 0;
1291 if (t == '(')
1292 next_nomacro();
1293 tok = TOK_CINT;
1294 tokc.i = c;
1295 } else if (tok >= TOK_IDENT) {
1296 /* if undefined macro */
1297 tok = TOK_CINT;
1298 tokc.i = 0;
1300 tok_str_add_tok(&str);
1302 tok_str_add(&str, -1); /* simulate end of file */
1303 tok_str_add(&str, 0);
1304 /* now evaluate C constant expression */
1305 begin_macro(&str, 0);
1306 next();
1307 c = expr_const();
1308 end_macro();
1309 return c != 0;
1313 /* parse after #define */
1314 ST_FUNC void parse_define(void)
1316 Sym *s, *first, **ps;
1317 int v, t, varg, is_vaargs, spc;
1318 int saved_parse_flags = parse_flags;
1319 TokenString str;
1321 v = tok;
1322 if (v < TOK_IDENT)
1323 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1324 /* XXX: should check if same macro (ANSI) */
1325 first = NULL;
1326 t = MACRO_OBJ;
1327 /* '(' must be just after macro definition for MACRO_FUNC */
1328 parse_flags |= PARSE_FLAG_SPACES;
1329 next_nomacro_spc();
1330 if (tok == '(') {
1331 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1332 parse_flags &= ~PARSE_FLAG_ASM_FILE;
1333 isidnum_table['.' - CH_EOF] = 0;
1334 next_nomacro();
1335 ps = &first;
1336 if (tok != ')') for (;;) {
1337 varg = tok;
1338 next_nomacro();
1339 is_vaargs = 0;
1340 if (varg == TOK_DOTS) {
1341 varg = TOK___VA_ARGS__;
1342 is_vaargs = 1;
1343 } else if (tok == TOK_DOTS && gnu_ext) {
1344 is_vaargs = 1;
1345 next_nomacro();
1347 if (varg < TOK_IDENT)
1348 bad_list:
1349 tcc_error("bad macro parameter list");
1350 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1351 *ps = s;
1352 ps = &s->next;
1353 if (tok == ')')
1354 break;
1355 if (tok != ',' || is_vaargs)
1356 goto bad_list;
1357 next_nomacro();
1359 next_nomacro_spc();
1360 t = MACRO_FUNC;
1361 parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
1362 isidnum_table['.' - CH_EOF] =
1363 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
1365 tok_str_new(&str);
1366 spc = 2;
1367 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1368 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1369 /* remove spaces around ## and after '#' */
1370 if (TOK_TWOSHARPS == tok) {
1371 if (2 == spc)
1372 goto bad_twosharp;
1373 if (1 == spc)
1374 --str.len;
1375 spc = 3;
1376 } else if ('#' == tok) {
1377 spc = 4;
1378 } else if (check_space(tok, &spc)) {
1379 goto skip;
1381 tok_str_add2(&str, tok, &tokc);
1382 skip:
1383 next_nomacro_spc();
1386 parse_flags = saved_parse_flags;
1387 if (spc == 1)
1388 --str.len; /* remove trailing space */
1389 tok_str_add(&str, 0);
1390 if (3 == spc)
1391 bad_twosharp:
1392 tcc_error("'##' cannot appear at either end of macro");
1393 define_push(v, t, str.str, first);
1394 define_print(v);
1397 static inline int hash_cached_include(const char *filename)
1399 const unsigned char *s;
1400 unsigned int h;
1402 h = TOK_HASH_INIT;
1403 s = (unsigned char *) filename;
1404 while (*s) {
1405 h = TOK_HASH_FUNC(h, *s);
1406 s++;
1408 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1409 return h;
1412 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1414 CachedInclude *e;
1415 int i, h;
1416 h = hash_cached_include(filename);
1417 i = s1->cached_includes_hash[h];
1418 for(;;) {
1419 if (i == 0)
1420 break;
1421 e = s1->cached_includes[i - 1];
1422 if (0 == PATHCMP(e->filename, filename))
1423 return e;
1424 i = e->hash_next;
1426 return NULL;
1429 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1431 CachedInclude *e;
1432 int h;
1434 if (search_cached_include(s1, filename))
1435 return;
1436 #ifdef INC_DEBUG
1437 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1438 #endif
1439 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1440 strcpy(e->filename, filename);
1441 e->ifndef_macro = ifndef_macro;
1442 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1443 /* add in hash table */
1444 h = hash_cached_include(filename);
1445 e->hash_next = s1->cached_includes_hash[h];
1446 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1449 static void pragma_parse(TCCState *s1)
1451 next_nomacro();
1452 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1453 int t = tok, v;
1454 Sym *s;
1456 if (next(), tok != '(')
1457 goto pragma_err;
1458 if (next(), tok != TOK_STR)
1459 goto pragma_err;
1460 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1461 if (next(), tok != ')')
1462 goto pragma_err;
1463 if (t == TOK_push_macro) {
1464 while (NULL == (s = define_find(v)))
1465 define_push(v, 0, NULL, NULL);
1466 s->type.ref = s; /* set push boundary */
1467 } else {
1468 for (s = define_stack; s; s = s->prev)
1469 if (s->v == v && s->type.ref == s) {
1470 s->type.ref = NULL;
1471 break;
1474 if (s)
1475 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1476 else
1477 tcc_warning("unbalanced #pragma pop_macro");
1479 } else if (tok == TOK_once) {
1480 add_cached_include(s1, file->filename, TOK_once);
1482 } else if (s1->ppfp) {
1483 /* tcc -E: keep pragmas below unchanged */
1484 unget_tok(' ');
1485 unget_tok(TOK_PRAGMA);
1486 unget_tok('#');
1487 unget_tok(TOK_LINEFEED);
1489 } else if (tok == TOK_pack) {
1490 /* This may be:
1491 #pragma pack(1) // set
1492 #pragma pack() // reset to default
1493 #pragma pack(push,1) // push & set
1494 #pragma pack(pop) // restore previous */
1495 next();
1496 skip('(');
1497 if (tok == TOK_ASM_pop) {
1498 next();
1499 if (s1->pack_stack_ptr <= s1->pack_stack) {
1500 stk_error:
1501 tcc_error("out of pack stack");
1503 s1->pack_stack_ptr--;
1504 } else {
1505 int val = 0;
1506 if (tok != ')') {
1507 if (tok == TOK_ASM_push) {
1508 next();
1509 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1510 goto stk_error;
1511 s1->pack_stack_ptr++;
1512 skip(',');
1514 if (tok != TOK_CINT)
1515 goto pragma_err;
1516 val = tokc.i;
1517 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1518 goto pragma_err;
1519 next();
1521 *s1->pack_stack_ptr = val;
1523 if (tok != ')')
1524 goto pragma_err;
1526 } else if (tok == TOK_comment) {
1527 char *file;
1528 next();
1529 skip('(');
1530 if (tok != TOK_lib)
1531 goto pragma_warn;
1532 next();
1533 skip(',');
1534 if (tok != TOK_STR)
1535 goto pragma_err;
1536 file = tcc_strdup((char *)tokc.str.data);
1537 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1538 next();
1539 if (tok != ')')
1540 goto pragma_err;
1541 } else {
1542 pragma_warn:
1543 if (s1->warn_unsupported)
1544 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1546 return;
1548 pragma_err:
1549 tcc_error("malformed #pragma directive");
1550 return;
1553 /* is_bof is true if first non space token at beginning of file */
1554 ST_FUNC void preprocess(int is_bof)
1556 TCCState *s1 = tcc_state;
1557 int i, c, n, saved_parse_flags;
1558 char buf[1024], *q;
1559 Sym *s;
1561 saved_parse_flags = parse_flags;
1562 parse_flags = PARSE_FLAG_PREPROCESS
1563 | PARSE_FLAG_TOK_NUM
1564 | PARSE_FLAG_TOK_STR
1565 | PARSE_FLAG_LINEFEED
1566 | (parse_flags & PARSE_FLAG_ASM_FILE)
1569 next_nomacro();
1570 redo:
1571 switch(tok) {
1572 case TOK_DEFINE:
1573 next_nomacro();
1574 parse_define();
1575 break;
1576 case TOK_UNDEF:
1577 next_nomacro();
1578 s = define_find(tok);
1579 /* undefine symbol by putting an invalid name */
1580 if (s)
1581 define_undef(s);
1582 break;
1583 case TOK_INCLUDE:
1584 case TOK_INCLUDE_NEXT:
1585 ch = file->buf_ptr[0];
1586 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1587 skip_spaces();
1588 if (ch == '<') {
1589 c = '>';
1590 goto read_name;
1591 } else if (ch == '\"') {
1592 c = ch;
1593 read_name:
1594 inp();
1595 q = buf;
1596 while (ch != c && ch != '\n' && ch != CH_EOF) {
1597 if ((q - buf) < sizeof(buf) - 1)
1598 *q++ = ch;
1599 if (ch == '\\') {
1600 if (handle_stray_noerror() == 0)
1601 --q;
1602 } else
1603 inp();
1605 *q = '\0';
1606 minp();
1607 #if 0
1608 /* eat all spaces and comments after include */
1609 /* XXX: slightly incorrect */
1610 while (ch1 != '\n' && ch1 != CH_EOF)
1611 inp();
1612 #endif
1613 } else {
1614 /* computed #include : either we have only strings or
1615 we have anything enclosed in '<>' */
1616 next();
1617 buf[0] = '\0';
1618 if (tok == TOK_STR) {
1619 while (tok != TOK_LINEFEED) {
1620 if (tok != TOK_STR) {
1621 include_syntax:
1622 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1624 pstrcat(buf, sizeof(buf), (char *)tokc.str.data);
1625 next();
1627 c = '\"';
1628 } else {
1629 int len;
1630 while (tok != TOK_LINEFEED) {
1631 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1632 next();
1634 len = strlen(buf);
1635 /* check syntax and remove '<>' */
1636 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1637 goto include_syntax;
1638 memmove(buf, buf + 1, len - 2);
1639 buf[len - 2] = '\0';
1640 c = '>';
1644 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1645 tcc_error("#include recursion too deep");
1646 /* store current file in stack, but increment stack later below */
1647 *s1->include_stack_ptr = file;
1648 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1649 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1650 for (; i < n; ++i) {
1651 char buf1[sizeof file->filename];
1652 CachedInclude *e;
1653 const char *path;
1655 if (i == 0) {
1656 /* check absolute include path */
1657 if (!IS_ABSPATH(buf))
1658 continue;
1659 buf1[0] = 0;
1661 } else if (i == 1) {
1662 /* search in current dir if "header.h" */
1663 if (c != '\"')
1664 continue;
1665 path = file->filename;
1666 pstrncpy(buf1, path, tcc_basename(path) - path);
1668 } else {
1669 /* search in all the include paths */
1670 int j = i - 2, k = j - s1->nb_include_paths;
1671 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1672 if (path == 0) continue;
1673 pstrcpy(buf1, sizeof(buf1), path);
1674 pstrcat(buf1, sizeof(buf1), "/");
1677 pstrcat(buf1, sizeof(buf1), buf);
1678 e = search_cached_include(s1, buf1);
1679 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
1680 /* no need to parse the include because the 'ifndef macro'
1681 is defined */
1682 #ifdef INC_DEBUG
1683 printf("%s: skipping cached %s\n", file->filename, buf1);
1684 #endif
1685 goto include_done;
1688 if (tcc_open(s1, buf1) < 0)
1689 continue;
1691 file->include_next_index = i + 1;
1692 #ifdef INC_DEBUG
1693 printf("%s: including %s\n", file->prev->filename, file->filename);
1694 #endif
1695 /* update target deps */
1696 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1697 tcc_strdup(buf1));
1698 /* push current file in stack */
1699 ++s1->include_stack_ptr;
1700 /* add include file debug info */
1701 if (s1->do_debug)
1702 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1703 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1704 ch = file->buf_ptr[0];
1705 goto the_end;
1707 tcc_error("include file '%s' not found", buf);
1708 include_done:
1709 break;
1710 case TOK_IFNDEF:
1711 c = 1;
1712 goto do_ifdef;
1713 case TOK_IF:
1714 c = expr_preprocess();
1715 goto do_if;
1716 case TOK_IFDEF:
1717 c = 0;
1718 do_ifdef:
1719 next_nomacro();
1720 if (tok < TOK_IDENT)
1721 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1722 if (is_bof) {
1723 if (c) {
1724 #ifdef INC_DEBUG
1725 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1726 #endif
1727 file->ifndef_macro = tok;
1730 c = (define_find(tok) != 0) ^ c;
1731 do_if:
1732 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1733 tcc_error("memory full (ifdef)");
1734 *s1->ifdef_stack_ptr++ = c;
1735 goto test_skip;
1736 case TOK_ELSE:
1737 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1738 tcc_error("#else without matching #if");
1739 if (s1->ifdef_stack_ptr[-1] & 2)
1740 tcc_error("#else after #else");
1741 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1742 goto test_else;
1743 case TOK_ELIF:
1744 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1745 tcc_error("#elif without matching #if");
1746 c = s1->ifdef_stack_ptr[-1];
1747 if (c > 1)
1748 tcc_error("#elif after #else");
1749 /* last #if/#elif expression was true: we skip */
1750 if (c == 1)
1751 goto skip;
1752 c = expr_preprocess();
1753 s1->ifdef_stack_ptr[-1] = c;
1754 test_else:
1755 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1756 file->ifndef_macro = 0;
1757 test_skip:
1758 if (!(c & 1)) {
1759 skip:
1760 preprocess_skip();
1761 is_bof = 0;
1762 goto redo;
1764 break;
1765 case TOK_ENDIF:
1766 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1767 tcc_error("#endif without matching #if");
1768 s1->ifdef_stack_ptr--;
1769 /* '#ifndef macro' was at the start of file. Now we check if
1770 an '#endif' is exactly at the end of file */
1771 if (file->ifndef_macro &&
1772 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1773 file->ifndef_macro_saved = file->ifndef_macro;
1774 /* need to set to zero to avoid false matches if another
1775 #ifndef at middle of file */
1776 file->ifndef_macro = 0;
1777 while (tok != TOK_LINEFEED)
1778 next_nomacro();
1779 tok_flags |= TOK_FLAG_ENDIF;
1780 goto the_end;
1782 break;
1783 case TOK_PPNUM:
1784 n = strtoul((char*)tokc.str.data, &q, 10);
1785 goto _line_num;
1786 case TOK_LINE:
1787 next();
1788 if (tok != TOK_CINT)
1789 _line_err:
1790 tcc_error("wrong #line format");
1791 n = tokc.i;
1792 _line_num:
1793 next();
1794 if (tok != TOK_LINEFEED) {
1795 if (tok == TOK_STR)
1796 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1797 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1798 break;
1799 else
1800 goto _line_err;
1801 --n;
1803 if (file->fd > 0)
1804 total_lines += file->line_num - n;
1805 file->line_num = n;
1806 if (s1->do_debug)
1807 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1808 break;
1809 case TOK_ERROR:
1810 case TOK_WARNING:
1811 c = tok;
1812 ch = file->buf_ptr[0];
1813 skip_spaces();
1814 q = buf;
1815 while (ch != '\n' && ch != CH_EOF) {
1816 if ((q - buf) < sizeof(buf) - 1)
1817 *q++ = ch;
1818 if (ch == '\\') {
1819 if (handle_stray_noerror() == 0)
1820 --q;
1821 } else
1822 inp();
1824 *q = '\0';
1825 if (c == TOK_ERROR)
1826 tcc_error("#error %s", buf);
1827 else
1828 tcc_warning("#warning %s", buf);
1829 break;
1830 case TOK_PRAGMA:
1831 pragma_parse(s1);
1832 break;
1833 case TOK_LINEFEED:
1834 goto the_end;
1835 default:
1836 /* ignore gas line comment in an 'S' file. */
1837 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1838 goto ignore;
1839 if (tok == '!' && is_bof)
1840 /* '!' is ignored at beginning to allow C scripts. */
1841 goto ignore;
1842 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1843 ignore:
1844 file->buf_ptr = parse_line_comment(file->buf_ptr);
1845 goto the_end;
1847 /* ignore other preprocess commands or #! for C scripts */
1848 while (tok != TOK_LINEFEED)
1849 next_nomacro();
1850 the_end:
1851 parse_flags = saved_parse_flags;
1854 /* evaluate escape codes in a string. */
1855 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1857 int c, n;
1858 const uint8_t *p;
1860 p = buf;
1861 for(;;) {
1862 c = *p;
1863 if (c == '\0')
1864 break;
1865 if (c == '\\') {
1866 p++;
1867 /* escape */
1868 c = *p;
1869 switch(c) {
1870 case '0': case '1': case '2': case '3':
1871 case '4': case '5': case '6': case '7':
1872 /* at most three octal digits */
1873 n = c - '0';
1874 p++;
1875 c = *p;
1876 if (isoct(c)) {
1877 n = n * 8 + c - '0';
1878 p++;
1879 c = *p;
1880 if (isoct(c)) {
1881 n = n * 8 + c - '0';
1882 p++;
1885 c = n;
1886 goto add_char_nonext;
1887 case 'x':
1888 case 'u':
1889 case 'U':
1890 p++;
1891 n = 0;
1892 for(;;) {
1893 c = *p;
1894 if (c >= 'a' && c <= 'f')
1895 c = c - 'a' + 10;
1896 else if (c >= 'A' && c <= 'F')
1897 c = c - 'A' + 10;
1898 else if (isnum(c))
1899 c = c - '0';
1900 else
1901 break;
1902 n = n * 16 + c;
1903 p++;
1905 c = n;
1906 goto add_char_nonext;
1907 case 'a':
1908 c = '\a';
1909 break;
1910 case 'b':
1911 c = '\b';
1912 break;
1913 case 'f':
1914 c = '\f';
1915 break;
1916 case 'n':
1917 c = '\n';
1918 break;
1919 case 'r':
1920 c = '\r';
1921 break;
1922 case 't':
1923 c = '\t';
1924 break;
1925 case 'v':
1926 c = '\v';
1927 break;
1928 case 'e':
1929 if (!gnu_ext)
1930 goto invalid_escape;
1931 c = 27;
1932 break;
1933 case '\'':
1934 case '\"':
1935 case '\\':
1936 case '?':
1937 break;
1938 default:
1939 invalid_escape:
1940 if (c >= '!' && c <= '~')
1941 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1942 else
1943 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1944 break;
1947 p++;
1948 add_char_nonext:
1949 if (!is_long)
1950 cstr_ccat(outstr, c);
1951 else
1952 cstr_wccat(outstr, c);
1954 /* add a trailing '\0' */
1955 if (!is_long)
1956 cstr_ccat(outstr, '\0');
1957 else
1958 cstr_wccat(outstr, '\0');
1961 void parse_string(const char *s, int len)
1963 uint8_t buf[1000], *p = buf;
1964 int is_long, sep;
1966 if ((is_long = *s == 'L'))
1967 ++s, --len;
1968 sep = *s++;
1969 len -= 2;
1970 if (len >= sizeof buf)
1971 p = tcc_malloc(len + 1);
1972 memcpy(p, s, len);
1973 p[len] = 0;
1975 cstr_reset(&tokcstr);
1976 parse_escape_string(&tokcstr, p, is_long);
1977 if (p != buf)
1978 tcc_free(p);
1980 if (sep == '\'') {
1981 int char_size;
1982 /* XXX: make it portable */
1983 if (!is_long)
1984 char_size = 1;
1985 else
1986 char_size = sizeof(nwchar_t);
1987 if (tokcstr.size <= char_size)
1988 tcc_error("empty character constant");
1989 if (tokcstr.size > 2 * char_size)
1990 tcc_warning("multi-character character constant");
1991 if (!is_long) {
1992 tokc.i = *(int8_t *)tokcstr.data;
1993 tok = TOK_CCHAR;
1994 } else {
1995 tokc.i = *(nwchar_t *)tokcstr.data;
1996 tok = TOK_LCHAR;
1998 } else {
1999 tokc.str.size = tokcstr.size;
2000 tokc.str.data = tokcstr.data;
2001 tokc.str.data_allocated = tokcstr.data_allocated;
2002 if (!is_long)
2003 tok = TOK_STR;
2004 else
2005 tok = TOK_LSTR;
2009 /* we use 64 bit numbers */
2010 #define BN_SIZE 2
2012 /* bn = (bn << shift) | or_val */
2013 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2015 int i;
2016 unsigned int v;
2017 for(i=0;i<BN_SIZE;i++) {
2018 v = bn[i];
2019 bn[i] = (v << shift) | or_val;
2020 or_val = v >> (32 - shift);
2024 static void bn_zero(unsigned int *bn)
2026 int i;
2027 for(i=0;i<BN_SIZE;i++) {
2028 bn[i] = 0;
2032 /* parse number in null terminated string 'p' and return it in the
2033 current token */
2034 static void parse_number(const char *p)
2036 int b, t, shift, frac_bits, s, exp_val, ch;
2037 char *q;
2038 unsigned int bn[BN_SIZE];
2039 double d;
2041 /* number */
2042 q = token_buf;
2043 ch = *p++;
2044 t = ch;
2045 ch = *p++;
2046 *q++ = t;
2047 b = 10;
2048 if (t == '.') {
2049 goto float_frac_parse;
2050 } else if (t == '0') {
2051 if (ch == 'x' || ch == 'X') {
2052 q--;
2053 ch = *p++;
2054 b = 16;
2055 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2056 q--;
2057 ch = *p++;
2058 b = 2;
2061 /* parse all digits. cannot check octal numbers at this stage
2062 because of floating point constants */
2063 while (1) {
2064 if (ch >= 'a' && ch <= 'f')
2065 t = ch - 'a' + 10;
2066 else if (ch >= 'A' && ch <= 'F')
2067 t = ch - 'A' + 10;
2068 else if (isnum(ch))
2069 t = ch - '0';
2070 else
2071 break;
2072 if (t >= b)
2073 break;
2074 if (q >= token_buf + STRING_MAX_SIZE) {
2075 num_too_long:
2076 tcc_error("number too long");
2078 *q++ = ch;
2079 ch = *p++;
2081 if (ch == '.' ||
2082 ((ch == 'e' || ch == 'E') && b == 10) ||
2083 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2084 if (b != 10) {
2085 /* NOTE: strtox should support that for hexa numbers, but
2086 non ISOC99 libcs do not support it, so we prefer to do
2087 it by hand */
2088 /* hexadecimal or binary floats */
2089 /* XXX: handle overflows */
2090 *q = '\0';
2091 if (b == 16)
2092 shift = 4;
2093 else
2094 shift = 1;
2095 bn_zero(bn);
2096 q = token_buf;
2097 while (1) {
2098 t = *q++;
2099 if (t == '\0') {
2100 break;
2101 } else if (t >= 'a') {
2102 t = t - 'a' + 10;
2103 } else if (t >= 'A') {
2104 t = t - 'A' + 10;
2105 } else {
2106 t = t - '0';
2108 bn_lshift(bn, shift, t);
2110 frac_bits = 0;
2111 if (ch == '.') {
2112 ch = *p++;
2113 while (1) {
2114 t = ch;
2115 if (t >= 'a' && t <= 'f') {
2116 t = t - 'a' + 10;
2117 } else if (t >= 'A' && t <= 'F') {
2118 t = t - 'A' + 10;
2119 } else if (t >= '0' && t <= '9') {
2120 t = t - '0';
2121 } else {
2122 break;
2124 if (t >= b)
2125 tcc_error("invalid digit");
2126 bn_lshift(bn, shift, t);
2127 frac_bits += shift;
2128 ch = *p++;
2131 if (ch != 'p' && ch != 'P')
2132 expect("exponent");
2133 ch = *p++;
2134 s = 1;
2135 exp_val = 0;
2136 if (ch == '+') {
2137 ch = *p++;
2138 } else if (ch == '-') {
2139 s = -1;
2140 ch = *p++;
2142 if (ch < '0' || ch > '9')
2143 expect("exponent digits");
2144 while (ch >= '0' && ch <= '9') {
2145 exp_val = exp_val * 10 + ch - '0';
2146 ch = *p++;
2148 exp_val = exp_val * s;
2150 /* now we can generate the number */
2151 /* XXX: should patch directly float number */
2152 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2153 d = ldexp(d, exp_val - frac_bits);
2154 t = toup(ch);
2155 if (t == 'F') {
2156 ch = *p++;
2157 tok = TOK_CFLOAT;
2158 /* float : should handle overflow */
2159 tokc.f = (float)d;
2160 } else if (t == 'L') {
2161 ch = *p++;
2162 #ifdef TCC_TARGET_PE
2163 tok = TOK_CDOUBLE;
2164 tokc.d = d;
2165 #else
2166 tok = TOK_CLDOUBLE;
2167 /* XXX: not large enough */
2168 tokc.ld = (long double)d;
2169 #endif
2170 } else {
2171 tok = TOK_CDOUBLE;
2172 tokc.d = d;
2174 } else {
2175 /* decimal floats */
2176 if (ch == '.') {
2177 if (q >= token_buf + STRING_MAX_SIZE)
2178 goto num_too_long;
2179 *q++ = ch;
2180 ch = *p++;
2181 float_frac_parse:
2182 while (ch >= '0' && ch <= '9') {
2183 if (q >= token_buf + STRING_MAX_SIZE)
2184 goto num_too_long;
2185 *q++ = ch;
2186 ch = *p++;
2189 if (ch == 'e' || ch == 'E') {
2190 if (q >= token_buf + STRING_MAX_SIZE)
2191 goto num_too_long;
2192 *q++ = ch;
2193 ch = *p++;
2194 if (ch == '-' || ch == '+') {
2195 if (q >= token_buf + STRING_MAX_SIZE)
2196 goto num_too_long;
2197 *q++ = ch;
2198 ch = *p++;
2200 if (ch < '0' || ch > '9')
2201 expect("exponent digits");
2202 while (ch >= '0' && ch <= '9') {
2203 if (q >= token_buf + STRING_MAX_SIZE)
2204 goto num_too_long;
2205 *q++ = ch;
2206 ch = *p++;
2209 *q = '\0';
2210 t = toup(ch);
2211 errno = 0;
2212 if (t == 'F') {
2213 ch = *p++;
2214 tok = TOK_CFLOAT;
2215 tokc.f = strtof(token_buf, NULL);
2216 } else if (t == 'L') {
2217 ch = *p++;
2218 #ifdef TCC_TARGET_PE
2219 tok = TOK_CDOUBLE;
2220 tokc.d = strtod(token_buf, NULL);
2221 #else
2222 tok = TOK_CLDOUBLE;
2223 tokc.ld = strtold(token_buf, NULL);
2224 #endif
2225 } else {
2226 tok = TOK_CDOUBLE;
2227 tokc.d = strtod(token_buf, NULL);
2230 } else {
2231 unsigned long long n, n1;
2232 int lcount, ucount, must_64bit;
2233 const char *p1;
2235 /* integer number */
2236 *q = '\0';
2237 q = token_buf;
2238 if (b == 10 && *q == '0') {
2239 b = 8;
2240 q++;
2242 n = 0;
2243 while(1) {
2244 t = *q++;
2245 /* no need for checks except for base 10 / 8 errors */
2246 if (t == '\0')
2247 break;
2248 else if (t >= 'a')
2249 t = t - 'a' + 10;
2250 else if (t >= 'A')
2251 t = t - 'A' + 10;
2252 else
2253 t = t - '0';
2254 if (t >= b)
2255 tcc_error("invalid digit");
2256 n1 = n;
2257 n = n * b + t;
2258 /* detect overflow */
2259 /* XXX: this test is not reliable */
2260 if (n < n1)
2261 tcc_error("integer constant overflow");
2264 /* Determine the characteristics (unsigned and/or 64bit) the type of
2265 the constant must have according to the constant suffix(es) */
2266 lcount = ucount = must_64bit = 0;
2267 p1 = p;
2268 for(;;) {
2269 t = toup(ch);
2270 if (t == 'L') {
2271 if (lcount >= 2)
2272 tcc_error("three 'l's in integer constant");
2273 if (lcount && *(p - 1) != ch)
2274 tcc_error("incorrect integer suffix: %s", p1);
2275 lcount++;
2276 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2277 if (lcount == 2)
2278 #endif
2279 must_64bit = 1;
2280 ch = *p++;
2281 } else if (t == 'U') {
2282 if (ucount >= 1)
2283 tcc_error("two 'u's in integer constant");
2284 ucount++;
2285 ch = *p++;
2286 } else {
2287 break;
2291 /* Whether 64 bits are needed to hold the constant's value */
2292 if (n & 0xffffffff00000000LL || must_64bit) {
2293 tok = TOK_CLLONG;
2294 n1 = n >> 32;
2295 } else {
2296 tok = TOK_CINT;
2297 n1 = n;
2300 /* Whether type must be unsigned to hold the constant's value */
2301 if (ucount || ((n1 >> 31) && (b != 10))) {
2302 if (tok == TOK_CLLONG)
2303 tok = TOK_CULLONG;
2304 else
2305 tok = TOK_CUINT;
2306 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2307 } else if (n1 >> 31) {
2308 if (tok == TOK_CINT)
2309 tok = TOK_CLLONG;
2310 else
2311 tcc_error("integer constant overflow");
2314 if (tok == TOK_CINT || tok == TOK_CUINT)
2315 tokc.i = n;
2316 else
2317 tokc.i = n;
2319 if (ch)
2320 tcc_error("invalid number\n");
2324 #define PARSE2(c1, tok1, c2, tok2) \
2325 case c1: \
2326 PEEKC(c, p); \
2327 if (c == c2) { \
2328 p++; \
2329 tok = tok2; \
2330 } else { \
2331 tok = tok1; \
2333 break;
2335 /* return next token without macro substitution */
2336 static inline void next_nomacro1(void)
2338 int t, c, is_long;
2339 TokenSym *ts;
2340 uint8_t *p, *p1;
2341 unsigned int h;
2343 p = file->buf_ptr;
2344 redo_no_start:
2345 c = *p;
2346 #if (__TINYC__ || __GNUC__)
2347 #else
2348 if (c & 0x80)
2349 goto parse_ident_fast;
2350 #endif
2351 switch(c) {
2352 case ' ':
2353 case '\t':
2354 tok = c;
2355 p++;
2356 if (parse_flags & PARSE_FLAG_SPACES)
2357 goto keep_tok_flags;
2358 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2359 ++p;
2360 goto redo_no_start;
2361 case '\f':
2362 case '\v':
2363 case '\r':
2364 p++;
2365 goto redo_no_start;
2366 case '\\':
2367 /* first look if it is in fact an end of buffer */
2368 c = handle_stray1(p);
2369 p = file->buf_ptr;
2370 if (c == '\\')
2371 goto parse_simple;
2372 if (c != CH_EOF)
2373 goto redo_no_start;
2375 TCCState *s1 = tcc_state;
2376 if ((parse_flags & PARSE_FLAG_LINEFEED)
2377 && !(tok_flags & TOK_FLAG_EOF)) {
2378 tok_flags |= TOK_FLAG_EOF;
2379 tok = TOK_LINEFEED;
2380 goto keep_tok_flags;
2381 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2382 tok = TOK_EOF;
2383 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2384 tcc_error("missing #endif");
2385 } else if (s1->include_stack_ptr == s1->include_stack) {
2386 /* no include left : end of file. */
2387 tok = TOK_EOF;
2388 } else {
2389 tok_flags &= ~TOK_FLAG_EOF;
2390 /* pop include file */
2392 /* test if previous '#endif' was after a #ifdef at
2393 start of file */
2394 if (tok_flags & TOK_FLAG_ENDIF) {
2395 #ifdef INC_DEBUG
2396 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2397 #endif
2398 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2399 tok_flags &= ~TOK_FLAG_ENDIF;
2402 /* add end of include file debug info */
2403 if (tcc_state->do_debug) {
2404 put_stabd(N_EINCL, 0, 0);
2406 /* pop include stack */
2407 tcc_close();
2408 s1->include_stack_ptr--;
2409 p = file->buf_ptr;
2410 goto redo_no_start;
2413 break;
2415 case '\n':
2416 file->line_num++;
2417 tok_flags |= TOK_FLAG_BOL;
2418 p++;
2419 maybe_newline:
2420 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2421 goto redo_no_start;
2422 tok = TOK_LINEFEED;
2423 goto keep_tok_flags;
2425 case '#':
2426 /* XXX: simplify */
2427 PEEKC(c, p);
2428 if ((tok_flags & TOK_FLAG_BOL) &&
2429 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2430 file->buf_ptr = p;
2431 preprocess(tok_flags & TOK_FLAG_BOF);
2432 p = file->buf_ptr;
2433 goto maybe_newline;
2434 } else {
2435 if (c == '#') {
2436 p++;
2437 tok = TOK_TWOSHARPS;
2438 } else {
2439 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2440 p = parse_line_comment(p - 1);
2441 goto redo_no_start;
2442 } else {
2443 tok = '#';
2447 break;
2449 /* dollar is allowed to start identifiers when not parsing asm */
2450 case '$':
2451 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2452 || (parse_flags & PARSE_FLAG_ASM_FILE))
2453 goto parse_simple;
2455 #if (__TINYC__ || __GNUC__)
2456 case 'a' ... 'z':
2457 case 'A' ... 'K':
2458 case 'M' ... 'Z':
2459 case '_':
2460 case 0x80 ... 0xFF:
2461 #else
2462 case 'a': case 'b': case 'c': case 'd':
2463 case 'e': case 'f': case 'g': case 'h':
2464 case 'i': case 'j': case 'k': case 'l':
2465 case 'm': case 'n': case 'o': case 'p':
2466 case 'q': case 'r': case 's': case 't':
2467 case 'u': case 'v': case 'w': case 'x':
2468 case 'y': case 'z':
2469 case 'A': case 'B': case 'C': case 'D':
2470 case 'E': case 'F': case 'G': case 'H':
2471 case 'I': case 'J': case 'K':
2472 case 'M': case 'N': case 'O': case 'P':
2473 case 'Q': case 'R': case 'S': case 'T':
2474 case 'U': case 'V': case 'W': case 'X':
2475 case 'Y': case 'Z':
2476 case '_':
2477 #endif
2478 parse_ident_fast:
2479 p1 = p;
2480 h = TOK_HASH_INIT;
2481 h = TOK_HASH_FUNC(h, c);
2482 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2483 h = TOK_HASH_FUNC(h, c);
2484 if (c != '\\') {
2485 TokenSym **pts;
2486 int len;
2488 /* fast case : no stray found, so we have the full token
2489 and we have already hashed it */
2490 len = p - p1;
2491 h &= (TOK_HASH_SIZE - 1);
2492 pts = &hash_ident[h];
2493 for(;;) {
2494 ts = *pts;
2495 if (!ts)
2496 break;
2497 if (ts->len == len && !memcmp(ts->str, p1, len))
2498 goto token_found;
2499 pts = &(ts->hash_next);
2501 ts = tok_alloc_new(pts, (char *) p1, len);
2502 token_found: ;
2503 } else {
2504 /* slower case */
2505 cstr_reset(&tokcstr);
2507 while (p1 < p) {
2508 cstr_ccat(&tokcstr, *p1);
2509 p1++;
2511 p--;
2512 PEEKC(c, p);
2513 parse_ident_slow:
2514 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2516 cstr_ccat(&tokcstr, c);
2517 PEEKC(c, p);
2519 ts = tok_alloc(tokcstr.data, tokcstr.size);
2521 tok = ts->tok;
2522 break;
2523 case 'L':
2524 t = p[1];
2525 if (t != '\\' && t != '\'' && t != '\"') {
2526 /* fast case */
2527 goto parse_ident_fast;
2528 } else {
2529 PEEKC(c, p);
2530 if (c == '\'' || c == '\"') {
2531 is_long = 1;
2532 goto str_const;
2533 } else {
2534 cstr_reset(&tokcstr);
2535 cstr_ccat(&tokcstr, 'L');
2536 goto parse_ident_slow;
2539 break;
2541 case '0': case '1': case '2': case '3':
2542 case '4': case '5': case '6': case '7':
2543 case '8': case '9':
2544 cstr_reset(&tokcstr);
2545 /* after the first digit, accept digits, alpha, '.' or sign if
2546 prefixed by 'eEpP' */
2547 parse_num:
2548 for(;;) {
2549 t = c;
2550 cstr_ccat(&tokcstr, c);
2551 PEEKC(c, p);
2552 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2553 || c == '.'
2554 || ((c == '+' || c == '-')
2555 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2557 break;
2559 /* We add a trailing '\0' to ease parsing */
2560 cstr_ccat(&tokcstr, '\0');
2561 tokc.str.size = tokcstr.size;
2562 tokc.str.data = tokcstr.data;
2563 tokc.str.data_allocated = tokcstr.data_allocated;
2564 tok = TOK_PPNUM;
2565 break;
2567 case '.':
2568 /* special dot handling because it can also start a number */
2569 PEEKC(c, p);
2570 if (isnum(c)) {
2571 cstr_reset(&tokcstr);
2572 cstr_ccat(&tokcstr, '.');
2573 goto parse_num;
2574 } else if (parse_flags & PARSE_FLAG_ASM_FILE) {
2575 *--p = c = '.';
2576 goto parse_ident_fast;
2577 } else if (c == '.') {
2578 PEEKC(c, p);
2579 if (c == '.') {
2580 p++;
2581 tok = TOK_DOTS;
2582 } else {
2583 *--p = '.'; /* may underflow into file->unget[] */
2584 tok = '.';
2586 } else {
2587 tok = '.';
2589 break;
2590 case '\'':
2591 case '\"':
2592 is_long = 0;
2593 str_const:
2594 cstr_reset(&tokcstr);
2595 if (is_long)
2596 cstr_ccat(&tokcstr, 'L');
2597 cstr_ccat(&tokcstr, c);
2598 p = parse_pp_string(p, c, &tokcstr);
2599 cstr_ccat(&tokcstr, c);
2600 cstr_ccat(&tokcstr, '\0');
2601 tokc.str.size = tokcstr.size;
2602 tokc.str.data = tokcstr.data;
2603 tokc.str.data_allocated = tokcstr.data_allocated;
2604 tok = TOK_PPSTR;
2605 break;
2607 case '<':
2608 PEEKC(c, p);
2609 if (c == '=') {
2610 p++;
2611 tok = TOK_LE;
2612 } else if (c == '<') {
2613 PEEKC(c, p);
2614 if (c == '=') {
2615 p++;
2616 tok = TOK_A_SHL;
2617 } else {
2618 tok = TOK_SHL;
2620 } else {
2621 tok = TOK_LT;
2623 break;
2624 case '>':
2625 PEEKC(c, p);
2626 if (c == '=') {
2627 p++;
2628 tok = TOK_GE;
2629 } else if (c == '>') {
2630 PEEKC(c, p);
2631 if (c == '=') {
2632 p++;
2633 tok = TOK_A_SAR;
2634 } else {
2635 tok = TOK_SAR;
2637 } else {
2638 tok = TOK_GT;
2640 break;
2642 case '&':
2643 PEEKC(c, p);
2644 if (c == '&') {
2645 p++;
2646 tok = TOK_LAND;
2647 } else if (c == '=') {
2648 p++;
2649 tok = TOK_A_AND;
2650 } else {
2651 tok = '&';
2653 break;
2655 case '|':
2656 PEEKC(c, p);
2657 if (c == '|') {
2658 p++;
2659 tok = TOK_LOR;
2660 } else if (c == '=') {
2661 p++;
2662 tok = TOK_A_OR;
2663 } else {
2664 tok = '|';
2666 break;
2668 case '+':
2669 PEEKC(c, p);
2670 if (c == '+') {
2671 p++;
2672 tok = TOK_INC;
2673 } else if (c == '=') {
2674 p++;
2675 tok = TOK_A_ADD;
2676 } else {
2677 tok = '+';
2679 break;
2681 case '-':
2682 PEEKC(c, p);
2683 if (c == '-') {
2684 p++;
2685 tok = TOK_DEC;
2686 } else if (c == '=') {
2687 p++;
2688 tok = TOK_A_SUB;
2689 } else if (c == '>') {
2690 p++;
2691 tok = TOK_ARROW;
2692 } else {
2693 tok = '-';
2695 break;
2697 PARSE2('!', '!', '=', TOK_NE)
2698 PARSE2('=', '=', '=', TOK_EQ)
2699 PARSE2('*', '*', '=', TOK_A_MUL)
2700 PARSE2('%', '%', '=', TOK_A_MOD)
2701 PARSE2('^', '^', '=', TOK_A_XOR)
2703 /* comments or operator */
2704 case '/':
2705 PEEKC(c, p);
2706 if (c == '*') {
2707 p = parse_comment(p);
2708 /* comments replaced by a blank */
2709 tok = ' ';
2710 goto keep_tok_flags;
2711 } else if (c == '/') {
2712 p = parse_line_comment(p);
2713 tok = ' ';
2714 goto keep_tok_flags;
2715 } else if (c == '=') {
2716 p++;
2717 tok = TOK_A_DIV;
2718 } else {
2719 tok = '/';
2721 break;
2723 /* simple tokens */
2724 case '(':
2725 case ')':
2726 case '[':
2727 case ']':
2728 case '{':
2729 case '}':
2730 case ',':
2731 case ';':
2732 case ':':
2733 case '?':
2734 case '~':
2735 case '@': /* only used in assembler */
2736 parse_simple:
2737 tok = c;
2738 p++;
2739 break;
2740 default:
2741 tcc_error("unrecognized character \\x%02x", c);
2742 break;
2744 tok_flags = 0;
2745 keep_tok_flags:
2746 file->buf_ptr = p;
2747 #if defined(PARSE_DEBUG)
2748 printf("token = %s\n", get_tok_str(tok, &tokc));
2749 #endif
2752 /* return next token without macro substitution. Can read input from
2753 macro_ptr buffer */
2754 static void next_nomacro_spc(void)
2756 if (macro_ptr) {
2757 redo:
2758 tok = *macro_ptr;
2759 if (tok) {
2760 TOK_GET(&tok, &macro_ptr, &tokc);
2761 if (tok == TOK_LINENUM) {
2762 file->line_num = tokc.i;
2763 goto redo;
2766 } else {
2767 next_nomacro1();
2771 ST_FUNC void next_nomacro(void)
2773 do {
2774 next_nomacro_spc();
2775 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2779 static void macro_subst(
2780 TokenString *tok_str,
2781 Sym **nested_list,
2782 const int *macro_str,
2783 int can_read_stream
2786 /* substitute arguments in replacement lists in macro_str by the values in
2787 args (field d) and return allocated string */
2788 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2790 int t, t0, t1, spc;
2791 const int *st;
2792 Sym *s;
2793 CValue cval;
2794 TokenString str;
2795 CString cstr;
2797 tok_str_new(&str);
2798 t0 = t1 = 0;
2799 while(1) {
2800 TOK_GET(&t, &macro_str, &cval);
2801 if (!t)
2802 break;
2803 if (t == '#') {
2804 /* stringize */
2805 TOK_GET(&t, &macro_str, &cval);
2806 if (!t)
2807 goto bad_stringy;
2808 s = sym_find2(args, t);
2809 if (s) {
2810 cstr_new(&cstr);
2811 cstr_ccat(&cstr, '\"');
2812 st = s->d;
2813 spc = 0;
2814 while (*st) {
2815 TOK_GET(&t, &st, &cval);
2816 if (t != TOK_PLCHLDR
2817 && t != TOK_NOSUBST
2818 && 0 == check_space(t, &spc)) {
2819 const char *s = get_tok_str(t, &cval);
2820 while (*s) {
2821 if (t == TOK_PPSTR && *s != '\'')
2822 add_char(&cstr, *s);
2823 else
2824 cstr_ccat(&cstr, *s);
2825 ++s;
2829 cstr.size -= spc;
2830 cstr_ccat(&cstr, '\"');
2831 cstr_ccat(&cstr, '\0');
2832 #ifdef PP_DEBUG
2833 printf("\nstringize: <%s>\n", (char *)cstr.data);
2834 #endif
2835 /* add string */
2836 cval.str.size = cstr.size;
2837 cval.str.data = cstr.data;
2838 cval.str.data_allocated = cstr.data_allocated;
2839 tok_str_add2(&str, TOK_PPSTR, &cval);
2840 tcc_free(cval.str.data_allocated);
2841 } else {
2842 bad_stringy:
2843 expect("macro parameter after '#'");
2845 } else if (t >= TOK_IDENT) {
2846 s = sym_find2(args, t);
2847 if (s) {
2848 int l0 = str.len;
2849 st = s->d;
2850 /* if '##' is present before or after, no arg substitution */
2851 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2852 /* special case for var arg macros : ## eats the ','
2853 if empty VA_ARGS variable. */
2854 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2855 if (*st == 0) {
2856 /* suppress ',' '##' */
2857 str.len -= 2;
2858 } else {
2859 /* suppress '##' and add variable */
2860 str.len--;
2861 goto add_var;
2863 } else {
2864 for(;;) {
2865 int t1;
2866 TOK_GET(&t1, &st, &cval);
2867 if (!t1)
2868 break;
2869 tok_str_add2(&str, t1, &cval);
2873 } else {
2874 add_var:
2875 /* NOTE: the stream cannot be read when macro
2876 substituing an argument */
2877 macro_subst(&str, nested_list, st, 0);
2879 if (str.len == l0) /* exanded to empty string */
2880 tok_str_add(&str, TOK_PLCHLDR);
2881 } else {
2882 tok_str_add(&str, t);
2884 } else {
2885 tok_str_add2(&str, t, &cval);
2887 t0 = t1, t1 = t;
2889 tok_str_add(&str, 0);
2890 return str.str;
2893 static char const ab_month_name[12][4] =
2895 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2896 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2899 /* peek or read [ws_str == NULL] next token from function macro call,
2900 walking up macro levels up to the file if necessary */
2901 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2903 int t;
2904 const int *p;
2905 Sym *sa;
2907 for (;;) {
2908 if (macro_ptr) {
2909 p = macro_ptr, t = *p;
2910 if (ws_str) {
2911 while (is_space(t) || TOK_LINEFEED == t)
2912 tok_str_add(ws_str, t), t = *++p;
2914 if (t == 0 && can_read_stream) {
2915 end_macro();
2916 /* also, end of scope for nested defined symbol */
2917 sa = *nested_list;
2918 while (sa && sa->v == -1)
2919 sa = sa->prev;
2920 if (sa)
2921 sa->v = -1;
2922 continue;
2924 } else {
2925 ch = handle_eob();
2926 if (ws_str) {
2927 while (is_space(ch) || ch == '\n' || ch == '/') {
2928 if (ch == '/') {
2929 int c;
2930 uint8_t *p = file->buf_ptr;
2931 PEEKC(c, p);
2932 if (c == '*') {
2933 p = parse_comment(p);
2934 file->buf_ptr = p - 1;
2935 } else if (c == '/') {
2936 p = parse_line_comment(p);
2937 file->buf_ptr = p - 1;
2938 } else
2939 break;
2940 ch = ' ';
2942 tok_str_add(ws_str, ch);
2943 cinp();
2946 t = ch;
2949 if (ws_str)
2950 return t;
2951 next_nomacro_spc();
2952 return tok;
2956 /* do macro substitution of current token with macro 's' and add
2957 result to (tok_str,tok_len). 'nested_list' is the list of all
2958 macros we got inside to avoid recursing. Return non zero if no
2959 substitution needs to be done */
2960 static int macro_subst_tok(
2961 TokenString *tok_str,
2962 Sym **nested_list,
2963 Sym *s,
2964 int can_read_stream)
2966 Sym *args, *sa, *sa1;
2967 int parlevel, *mstr, t, t1, spc;
2968 TokenString str;
2969 char *cstrval;
2970 CValue cval;
2971 CString cstr;
2972 char buf[32];
2974 /* if symbol is a macro, prepare substitution */
2975 /* special macros */
2976 if (tok == TOK___LINE__) {
2977 snprintf(buf, sizeof(buf), "%d", file->line_num);
2978 cstrval = buf;
2979 t1 = TOK_PPNUM;
2980 goto add_cstr1;
2981 } else if (tok == TOK___FILE__) {
2982 cstrval = file->filename;
2983 goto add_cstr;
2984 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2985 time_t ti;
2986 struct tm *tm;
2988 time(&ti);
2989 tm = localtime(&ti);
2990 if (tok == TOK___DATE__) {
2991 snprintf(buf, sizeof(buf), "%s %2d %d",
2992 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2993 } else {
2994 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2995 tm->tm_hour, tm->tm_min, tm->tm_sec);
2997 cstrval = buf;
2998 add_cstr:
2999 t1 = TOK_STR;
3000 add_cstr1:
3001 cstr_new(&cstr);
3002 cstr_cat(&cstr, cstrval);
3003 cstr_ccat(&cstr, '\0');
3004 cval.str.size = cstr.size;
3005 cval.str.data = cstr.data;
3006 cval.str.data_allocated = cstr.data_allocated;
3007 tok_str_add2(tok_str, t1, &cval);
3008 cstr_free(&cstr);
3009 } else {
3010 int saved_parse_flags = parse_flags;
3012 mstr = s->d;
3013 if (s->type.t == MACRO_FUNC) {
3014 /* whitespace between macro name and argument list */
3015 TokenString ws_str;
3016 tok_str_new(&ws_str);
3018 spc = 0;
3019 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3020 | PARSE_FLAG_ACCEPT_STRAYS;
3022 /* get next token from argument stream */
3023 t = next_argstream(nested_list, can_read_stream, &ws_str);
3024 if (t != '(') {
3025 /* not a macro substitution after all, restore the
3026 * macro token plus all whitespace we've read.
3027 * whitespace is intentionally not merged to preserve
3028 * newlines. */
3029 parse_flags = saved_parse_flags;
3030 tok_str_add(tok_str, tok);
3031 if (parse_flags & PARSE_FLAG_SPACES) {
3032 int i;
3033 for (i = 0; i < ws_str.len; i++)
3034 tok_str_add(tok_str, ws_str.str[i]);
3036 tok_str_free(ws_str.str);
3037 return 0;
3038 } else {
3039 tok_str_free(ws_str.str);
3041 next_nomacro(); /* eat '(' */
3043 /* argument macro */
3044 args = NULL;
3045 sa = s->next;
3046 /* NOTE: empty args are allowed, except if no args */
3047 for(;;) {
3048 do {
3049 next_argstream(nested_list, can_read_stream, NULL);
3050 } while (is_space(tok) || TOK_LINEFEED == tok);
3051 empty_arg:
3052 /* handle '()' case */
3053 if (!args && !sa && tok == ')')
3054 break;
3055 if (!sa)
3056 tcc_error("macro '%s' used with too many args",
3057 get_tok_str(s->v, 0));
3058 tok_str_new(&str);
3059 parlevel = spc = 0;
3060 /* NOTE: non zero sa->t indicates VA_ARGS */
3061 while ((parlevel > 0 ||
3062 (tok != ')' &&
3063 (tok != ',' || sa->type.t)))) {
3064 if (tok == TOK_EOF || tok == 0)
3065 break;
3066 if (tok == '(')
3067 parlevel++;
3068 else if (tok == ')')
3069 parlevel--;
3070 if (tok == TOK_LINEFEED)
3071 tok = ' ';
3072 if (!check_space(tok, &spc))
3073 tok_str_add2(&str, tok, &tokc);
3074 next_argstream(nested_list, can_read_stream, NULL);
3076 if (parlevel)
3077 expect(")");
3078 str.len -= spc;
3079 tok_str_add(&str, 0);
3080 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3081 sa1->d = str.str;
3082 sa = sa->next;
3083 if (tok == ')') {
3084 /* special case for gcc var args: add an empty
3085 var arg argument if it is omitted */
3086 if (sa && sa->type.t && gnu_ext)
3087 goto empty_arg;
3088 break;
3090 if (tok != ',')
3091 expect(",");
3093 if (sa) {
3094 tcc_error("macro '%s' used with too few args",
3095 get_tok_str(s->v, 0));
3098 parse_flags = saved_parse_flags;
3100 /* now subst each arg */
3101 mstr = macro_arg_subst(nested_list, mstr, args);
3102 /* free memory */
3103 sa = args;
3104 while (sa) {
3105 sa1 = sa->prev;
3106 tok_str_free(sa->d);
3107 sym_free(sa);
3108 sa = sa1;
3112 sym_push2(nested_list, s->v, 0, 0);
3113 parse_flags = saved_parse_flags;
3114 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3116 /* pop nested defined symbol */
3117 sa1 = *nested_list;
3118 *nested_list = sa1->prev;
3119 sym_free(sa1);
3120 if (mstr != s->d)
3121 tok_str_free(mstr);
3123 return 0;
3126 int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3128 CString cstr;
3129 int n;
3131 cstr_new(&cstr);
3132 if (t1 != TOK_PLCHLDR)
3133 cstr_cat(&cstr, get_tok_str(t1, v1));
3134 n = cstr.size;
3135 if (t2 != TOK_PLCHLDR)
3136 cstr_cat(&cstr, get_tok_str(t2, v2));
3137 cstr_ccat(&cstr, '\0');
3139 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3140 memcpy(file->buffer, cstr.data, cstr.size);
3141 for (;;) {
3142 next_nomacro1();
3143 if (0 == *file->buf_ptr)
3144 break;
3145 if (is_space(tok))
3146 continue;
3147 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3148 n, cstr.data, (char*)cstr.data + n);
3149 break;
3151 tcc_close();
3153 //printf("paste <%s>\n", (char*)cstr.data);
3154 cstr_free(&cstr);
3155 return 0;
3158 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3159 return the resulting string (which must be freed). */
3160 static inline int *macro_twosharps(const int *ptr0)
3162 int t;
3163 CValue cval;
3164 TokenString macro_str1;
3165 int start_of_nosubsts = -1;
3166 const int *ptr;
3168 /* we search the first '##' */
3169 for (ptr = ptr0;;) {
3170 TOK_GET(&t, &ptr, &cval);
3171 if (t == TOK_TWOSHARPS)
3172 break;
3173 if (t == 0)
3174 return NULL;
3177 tok_str_new(&macro_str1);
3179 //tok_print(" $$$", ptr0);
3180 for (ptr = ptr0;;) {
3181 TOK_GET(&t, &ptr, &cval);
3182 if (t == 0)
3183 break;
3184 if (t == TOK_TWOSHARPS)
3185 continue;
3186 while (*ptr == TOK_TWOSHARPS) {
3187 int t1; CValue cv1;
3188 /* given 'a##b', remove nosubsts preceding 'a' */
3189 if (start_of_nosubsts >= 0)
3190 macro_str1.len = start_of_nosubsts;
3191 /* given 'a##b', remove nosubsts preceding 'b' */
3192 while ((t1 = *++ptr) == TOK_NOSUBST)
3194 if (t1 && t1 != TOK_TWOSHARPS
3195 && t1 != ':') /* 'a##:' don't build a new token */
3197 TOK_GET(&t1, &ptr, &cv1);
3198 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3199 paste_tokens(t, &cval, t1, &cv1);
3200 t = tok, cval = tokc;
3204 if (t == TOK_NOSUBST) {
3205 if (start_of_nosubsts < 0)
3206 start_of_nosubsts = macro_str1.len;
3207 } else {
3208 start_of_nosubsts = -1;
3210 tok_str_add2(&macro_str1, t, &cval);
3212 tok_str_add(&macro_str1, 0);
3213 //tok_print(" ###", macro_str1.str);
3214 return macro_str1.str;
3217 /* do macro substitution of macro_str and add result to
3218 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3219 inside to avoid recursing. */
3220 static void macro_subst(
3221 TokenString *tok_str,
3222 Sym **nested_list,
3223 const int *macro_str,
3224 int can_read_stream
3227 Sym *s;
3228 const int *ptr;
3229 int t, spc, nosubst;
3230 CValue cval;
3231 int *macro_str1 = NULL;
3233 /* first scan for '##' operator handling */
3234 ptr = macro_str;
3235 spc = nosubst = 0;
3237 /* first scan for '##' operator handling */
3238 if (can_read_stream) {
3239 macro_str1 = macro_twosharps(ptr);
3240 if (macro_str1)
3241 ptr = macro_str1;
3244 while (1) {
3245 TOK_GET(&t, &ptr, &cval);
3246 if (t == 0)
3247 break;
3249 if (t >= TOK_IDENT && 0 == nosubst) {
3250 s = define_find(t);
3251 if (s == NULL)
3252 goto no_subst;
3254 /* if nested substitution, do nothing */
3255 if (sym_find2(*nested_list, t)) {
3256 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3257 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3258 goto no_subst;
3262 TokenString str;
3263 str.str = (int*)ptr;
3264 begin_macro(&str, 2);
3266 tok = t;
3267 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3269 if (str.alloc == 3) {
3270 /* already finished by reading function macro arguments */
3271 break;
3274 ptr = macro_ptr;
3275 end_macro ();
3278 spc = (tok_str->len &&
3279 is_space(tok_last(tok_str->str,
3280 tok_str->str + tok_str->len)));
3282 } else {
3284 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3285 tcc_error("stray '\\' in program");
3287 no_subst:
3288 if (!check_space(t, &spc))
3289 tok_str_add2(tok_str, t, &cval);
3290 nosubst = 0;
3291 if (t == TOK_NOSUBST)
3292 nosubst = 1;
3295 if (macro_str1)
3296 tok_str_free(macro_str1);
3300 /* return next token with macro substitution */
3301 ST_FUNC void next(void)
3303 redo:
3304 if (parse_flags & PARSE_FLAG_SPACES)
3305 next_nomacro_spc();
3306 else
3307 next_nomacro();
3309 if (macro_ptr) {
3310 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3311 /* discard preprocessor markers */
3312 goto redo;
3313 } else if (tok == 0) {
3314 /* end of macro or unget token string */
3315 end_macro();
3316 goto redo;
3318 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3319 Sym *s;
3320 /* if reading from file, try to substitute macros */
3321 s = define_find(tok);
3322 if (s) {
3323 static TokenString str; /* using static string for speed */
3324 Sym *nested_list = NULL;
3325 tok_str_new(&str);
3326 nested_list = NULL;
3327 macro_subst_tok(&str, &nested_list, s, 1);
3328 tok_str_add(&str, 0);
3329 begin_macro(&str, 0);
3330 goto redo;
3333 /* convert preprocessor tokens into C tokens */
3334 if (tok == TOK_PPNUM) {
3335 if (parse_flags & PARSE_FLAG_TOK_NUM)
3336 parse_number((char *)tokc.str.data);
3337 } else if (tok == TOK_PPSTR) {
3338 if (parse_flags & PARSE_FLAG_TOK_STR)
3339 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3343 /* push back current token and set current token to 'last_tok'. Only
3344 identifier case handled for labels. */
3345 ST_INLN void unget_tok(int last_tok)
3347 TokenString *str = tcc_malloc(sizeof *str);
3348 tok_str_new(str);
3349 tok_str_add2(str, tok, &tokc);
3350 tok_str_add(str, 0);
3351 begin_macro(str, 1);
3352 tok = last_tok;
3355 /* better than nothing, but needs extension to handle '-E' option
3356 correctly too */
3357 ST_FUNC void preprocess_init(TCCState *s1)
3359 s1->include_stack_ptr = s1->include_stack;
3360 /* XXX: move that before to avoid having to initialize
3361 file->ifdef_stack_ptr ? */
3362 s1->ifdef_stack_ptr = s1->ifdef_stack;
3363 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3365 pvtop = vtop = vstack - 1;
3366 s1->pack_stack[0] = 0;
3367 s1->pack_stack_ptr = s1->pack_stack;
3369 isidnum_table['$' - CH_EOF] =
3370 tcc_state->dollars_in_identifiers ? IS_ID : 0;
3372 isidnum_table['.' - CH_EOF] =
3373 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
3376 ST_FUNC void preprocess_new(void)
3378 int i, c;
3379 const char *p, *r;
3381 /* init isid table */
3382 for(i = CH_EOF; i<128; i++)
3383 isidnum_table[i - CH_EOF]
3384 = is_space(i) ? IS_SPC
3385 : isid(i) ? IS_ID
3386 : isnum(i) ? IS_NUM
3387 : 0;
3389 for(i = 128; i<256; i++)
3390 isidnum_table[i - CH_EOF] = IS_ID;
3392 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3394 tok_ident = TOK_IDENT;
3395 p = tcc_keywords;
3396 while (*p) {
3397 r = p;
3398 for(;;) {
3399 c = *r++;
3400 if (c == '\0')
3401 break;
3403 tok_alloc(p, r - p - 1);
3404 p = r;
3408 ST_FUNC void preprocess_delete(void)
3410 int i, n;
3412 /* free -D and compiler defines */
3413 free_defines(NULL);
3415 /* cleanup from error/setjmp */
3416 while (macro_stack)
3417 end_macro();
3418 macro_ptr = NULL;
3420 /* free tokens */
3421 n = tok_ident - TOK_IDENT;
3422 for(i = 0; i < n; i++)
3423 tcc_free(table_ident[i]);
3424 tcc_free(table_ident);
3425 table_ident = NULL;
3428 /* Preprocess the current file */
3429 ST_FUNC int tcc_preprocess(TCCState *s1)
3431 BufferedFile **iptr;
3432 int token_seen, spcs, level;
3434 preprocess_init(s1);
3435 ch = file->buf_ptr[0];
3436 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3437 parse_flags = PARSE_FLAG_PREPROCESS
3438 | (parse_flags & PARSE_FLAG_ASM_FILE)
3439 | PARSE_FLAG_LINEFEED
3440 | PARSE_FLAG_SPACES
3441 | PARSE_FLAG_ACCEPT_STRAYS
3444 #ifdef PP_BENCH
3445 do next(); while (tok != TOK_EOF); return 0;
3446 #endif
3448 token_seen = spcs = 0;
3449 pp_line(s1, file, 0);
3451 for (;;) {
3452 iptr = s1->include_stack_ptr;
3453 next();
3454 if (tok == TOK_EOF)
3455 break;
3456 level = s1->include_stack_ptr - iptr;
3457 if (level) {
3458 if (level > 0)
3459 pp_line(s1, *iptr, 0);
3460 pp_line(s1, file, level);
3463 if (0 == token_seen) {
3464 if (tok == ' ') {
3465 ++spcs;
3466 continue;
3468 if (tok == TOK_LINEFEED) {
3469 spcs = 0;
3470 continue;
3472 pp_line(s1, file, 0);
3473 while (spcs)
3474 fputs(" ", s1->ppfp), --spcs;
3475 token_seen = 1;
3477 } else if (tok == TOK_LINEFEED) {
3478 ++file->line_ref;
3479 token_seen = 0;
3482 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3485 return 0;