Migrate static STRING_MAX_SIZE buffers to CString instances for large macros expansion
[tinycc.git] / tccpp.c
blob4993d13296cfb150868d400ef0fd0c82a7a12d8f
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->ppfp;
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->ppfp || tcc_state->dflag == 0)
1112 return 0;
1114 if (s->v < TOK_IDENT || s->v >= tok_ident)
1115 return 0;
1117 if (file) {
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->ppfp;
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->ppfp;
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 next_nomacro();
1332 ps = &first;
1333 if (tok != ')') for (;;) {
1334 varg = tok;
1335 next_nomacro();
1336 is_vaargs = 0;
1337 if (varg == TOK_DOTS) {
1338 varg = TOK___VA_ARGS__;
1339 is_vaargs = 1;
1340 } else if (tok == TOK_DOTS && gnu_ext) {
1341 is_vaargs = 1;
1342 next_nomacro();
1344 if (varg < TOK_IDENT)
1345 bad_list:
1346 tcc_error("bad macro parameter list");
1347 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1348 *ps = s;
1349 ps = &s->next;
1350 if (tok == ')')
1351 break;
1352 if (tok != ',' || is_vaargs)
1353 goto bad_list;
1354 next_nomacro();
1356 next_nomacro_spc();
1357 t = MACRO_FUNC;
1359 tok_str_new(&str);
1360 spc = 2;
1361 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1362 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1363 /* remove spaces around ## and after '#' */
1364 if (TOK_TWOSHARPS == tok) {
1365 if (2 == spc)
1366 goto bad_twosharp;
1367 if (1 == spc)
1368 --str.len;
1369 spc = 3;
1370 } else if ('#' == tok) {
1371 spc = 4;
1372 } else if (check_space(tok, &spc)) {
1373 goto skip;
1375 tok_str_add2(&str, tok, &tokc);
1376 skip:
1377 next_nomacro_spc();
1380 parse_flags = saved_parse_flags;
1381 if (spc == 1)
1382 --str.len; /* remove trailing space */
1383 tok_str_add(&str, 0);
1384 if (3 == spc)
1385 bad_twosharp:
1386 tcc_error("'##' cannot appear at either end of macro");
1387 define_push(v, t, str.str, first);
1388 define_print(v);
1391 static inline int hash_cached_include(const char *filename)
1393 const unsigned char *s;
1394 unsigned int h;
1396 h = TOK_HASH_INIT;
1397 s = (unsigned char *) filename;
1398 while (*s) {
1399 h = TOK_HASH_FUNC(h, *s);
1400 s++;
1402 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1403 return h;
1406 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1408 CachedInclude *e;
1409 int i, h;
1410 h = hash_cached_include(filename);
1411 i = s1->cached_includes_hash[h];
1412 for(;;) {
1413 if (i == 0)
1414 break;
1415 e = s1->cached_includes[i - 1];
1416 if (0 == PATHCMP(e->filename, filename))
1417 return e;
1418 i = e->hash_next;
1420 return NULL;
1423 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1425 CachedInclude *e;
1426 int h;
1428 if (search_cached_include(s1, filename))
1429 return;
1430 #ifdef INC_DEBUG
1431 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1432 #endif
1433 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1434 strcpy(e->filename, filename);
1435 e->ifndef_macro = ifndef_macro;
1436 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1437 /* add in hash table */
1438 h = hash_cached_include(filename);
1439 e->hash_next = s1->cached_includes_hash[h];
1440 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1443 static void pragma_parse(TCCState *s1)
1445 next_nomacro();
1446 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1447 int t = tok, v;
1448 Sym *s;
1450 if (next(), tok != '(')
1451 goto pragma_err;
1452 if (next(), tok != TOK_STR)
1453 goto pragma_err;
1454 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1455 if (next(), tok != ')')
1456 goto pragma_err;
1457 if (t == TOK_push_macro) {
1458 while (NULL == (s = define_find(v)))
1459 define_push(v, 0, NULL, NULL);
1460 s->type.ref = s; /* set push boundary */
1461 } else {
1462 for (s = define_stack; s; s = s->prev)
1463 if (s->v == v && s->type.ref == s) {
1464 s->type.ref = NULL;
1465 break;
1468 if (s)
1469 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1470 else
1471 tcc_warning("unbalanced #pragma pop_macro");
1473 } else if (tok == TOK_once) {
1474 add_cached_include(s1, file->filename, TOK_once);
1476 } else if (s1->ppfp) {
1477 /* tcc -E: keep pragmas below unchanged */
1478 unget_tok(' ');
1479 unget_tok(TOK_PRAGMA);
1480 unget_tok('#');
1481 unget_tok(TOK_LINEFEED);
1483 } else if (tok == TOK_pack) {
1484 /* This may be:
1485 #pragma pack(1) // set
1486 #pragma pack() // reset to default
1487 #pragma pack(push,1) // push & set
1488 #pragma pack(pop) // restore previous */
1489 next();
1490 skip('(');
1491 if (tok == TOK_ASM_pop) {
1492 next();
1493 if (s1->pack_stack_ptr <= s1->pack_stack) {
1494 stk_error:
1495 tcc_error("out of pack stack");
1497 s1->pack_stack_ptr--;
1498 } else {
1499 int val = 0;
1500 if (tok != ')') {
1501 if (tok == TOK_ASM_push) {
1502 next();
1503 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1504 goto stk_error;
1505 s1->pack_stack_ptr++;
1506 skip(',');
1508 if (tok != TOK_CINT)
1509 goto pragma_err;
1510 val = tokc.i;
1511 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1512 goto pragma_err;
1513 next();
1515 *s1->pack_stack_ptr = val;
1517 if (tok != ')')
1518 goto pragma_err;
1520 } else if (tok == TOK_comment) {
1521 char *file;
1522 next();
1523 skip('(');
1524 if (tok != TOK_lib)
1525 goto pragma_warn;
1526 next();
1527 skip(',');
1528 if (tok != TOK_STR)
1529 goto pragma_err;
1530 file = tcc_strdup((char *)tokc.str.data);
1531 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1532 next();
1533 if (tok != ')')
1534 goto pragma_err;
1535 } else {
1536 pragma_warn:
1537 if (s1->warn_unsupported)
1538 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1540 return;
1542 pragma_err:
1543 tcc_error("malformed #pragma directive");
1544 return;
1547 /* is_bof is true if first non space token at beginning of file */
1548 ST_FUNC void preprocess(int is_bof)
1550 TCCState *s1 = tcc_state;
1551 int i, c, n, saved_parse_flags;
1552 char buf[1024], *q;
1553 Sym *s;
1555 saved_parse_flags = parse_flags;
1556 parse_flags = PARSE_FLAG_PREPROCESS
1557 | PARSE_FLAG_TOK_NUM
1558 | PARSE_FLAG_TOK_STR
1559 | PARSE_FLAG_LINEFEED
1560 | (parse_flags & PARSE_FLAG_ASM_FILE)
1563 next_nomacro();
1564 redo:
1565 switch(tok) {
1566 case TOK_DEFINE:
1567 next_nomacro();
1568 parse_define();
1569 break;
1570 case TOK_UNDEF:
1571 next_nomacro();
1572 s = define_find(tok);
1573 /* undefine symbol by putting an invalid name */
1574 if (s)
1575 define_undef(s);
1576 break;
1577 case TOK_INCLUDE:
1578 case TOK_INCLUDE_NEXT:
1579 ch = file->buf_ptr[0];
1580 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1581 skip_spaces();
1582 if (ch == '<') {
1583 c = '>';
1584 goto read_name;
1585 } else if (ch == '\"') {
1586 c = ch;
1587 read_name:
1588 inp();
1589 q = buf;
1590 while (ch != c && ch != '\n' && ch != CH_EOF) {
1591 if ((q - buf) < sizeof(buf) - 1)
1592 *q++ = ch;
1593 if (ch == '\\') {
1594 if (handle_stray_noerror() == 0)
1595 --q;
1596 } else
1597 inp();
1599 *q = '\0';
1600 minp();
1601 #if 0
1602 /* eat all spaces and comments after include */
1603 /* XXX: slightly incorrect */
1604 while (ch1 != '\n' && ch1 != CH_EOF)
1605 inp();
1606 #endif
1607 } else {
1608 /* computed #include : either we have only strings or
1609 we have anything enclosed in '<>' */
1610 next();
1611 buf[0] = '\0';
1612 if (tok == TOK_STR) {
1613 while (tok != TOK_LINEFEED) {
1614 if (tok != TOK_STR) {
1615 include_syntax:
1616 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1618 pstrcat(buf, sizeof(buf), (char *)tokc.str.data);
1619 next();
1621 c = '\"';
1622 } else {
1623 int len;
1624 while (tok != TOK_LINEFEED) {
1625 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1626 next();
1628 len = strlen(buf);
1629 /* check syntax and remove '<>' */
1630 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1631 goto include_syntax;
1632 memmove(buf, buf + 1, len - 2);
1633 buf[len - 2] = '\0';
1634 c = '>';
1638 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1639 tcc_error("#include recursion too deep");
1640 /* store current file in stack, but increment stack later below */
1641 *s1->include_stack_ptr = file;
1642 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1643 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1644 for (; i < n; ++i) {
1645 char buf1[sizeof file->filename];
1646 CachedInclude *e;
1647 const char *path;
1649 if (i == 0) {
1650 /* check absolute include path */
1651 if (!IS_ABSPATH(buf))
1652 continue;
1653 buf1[0] = 0;
1655 } else if (i == 1) {
1656 /* search in current dir if "header.h" */
1657 if (c != '\"')
1658 continue;
1659 path = file->filename;
1660 pstrncpy(buf1, path, tcc_basename(path) - path);
1662 } else {
1663 /* search in all the include paths */
1664 int j = i - 2, k = j - s1->nb_include_paths;
1665 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1666 pstrcpy(buf1, sizeof(buf1), path);
1667 pstrcat(buf1, sizeof(buf1), "/");
1670 pstrcat(buf1, sizeof(buf1), buf);
1671 e = search_cached_include(s1, buf1);
1672 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
1673 /* no need to parse the include because the 'ifndef macro'
1674 is defined */
1675 #ifdef INC_DEBUG
1676 printf("%s: skipping cached %s\n", file->filename, buf1);
1677 #endif
1678 goto include_done;
1681 if (tcc_open(s1, buf1) < 0)
1682 continue;
1684 file->include_next_index = i + 1;
1685 #ifdef INC_DEBUG
1686 printf("%s: including %s\n", file->prev->filename, file->filename);
1687 #endif
1688 /* update target deps */
1689 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1690 tcc_strdup(buf1));
1691 /* push current file in stack */
1692 ++s1->include_stack_ptr;
1693 /* add include file debug info */
1694 if (s1->do_debug)
1695 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1696 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1697 ch = file->buf_ptr[0];
1698 goto the_end;
1700 tcc_error("include file '%s' not found", buf);
1701 include_done:
1702 break;
1703 case TOK_IFNDEF:
1704 c = 1;
1705 goto do_ifdef;
1706 case TOK_IF:
1707 c = expr_preprocess();
1708 goto do_if;
1709 case TOK_IFDEF:
1710 c = 0;
1711 do_ifdef:
1712 next_nomacro();
1713 if (tok < TOK_IDENT)
1714 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1715 if (is_bof) {
1716 if (c) {
1717 #ifdef INC_DEBUG
1718 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1719 #endif
1720 file->ifndef_macro = tok;
1723 c = (define_find(tok) != 0) ^ c;
1724 do_if:
1725 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1726 tcc_error("memory full (ifdef)");
1727 *s1->ifdef_stack_ptr++ = c;
1728 goto test_skip;
1729 case TOK_ELSE:
1730 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1731 tcc_error("#else without matching #if");
1732 if (s1->ifdef_stack_ptr[-1] & 2)
1733 tcc_error("#else after #else");
1734 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1735 goto test_else;
1736 case TOK_ELIF:
1737 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1738 tcc_error("#elif without matching #if");
1739 c = s1->ifdef_stack_ptr[-1];
1740 if (c > 1)
1741 tcc_error("#elif after #else");
1742 /* last #if/#elif expression was true: we skip */
1743 if (c == 1)
1744 goto skip;
1745 c = expr_preprocess();
1746 s1->ifdef_stack_ptr[-1] = c;
1747 test_else:
1748 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1749 file->ifndef_macro = 0;
1750 test_skip:
1751 if (!(c & 1)) {
1752 skip:
1753 preprocess_skip();
1754 is_bof = 0;
1755 goto redo;
1757 break;
1758 case TOK_ENDIF:
1759 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1760 tcc_error("#endif without matching #if");
1761 s1->ifdef_stack_ptr--;
1762 /* '#ifndef macro' was at the start of file. Now we check if
1763 an '#endif' is exactly at the end of file */
1764 if (file->ifndef_macro &&
1765 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1766 file->ifndef_macro_saved = file->ifndef_macro;
1767 /* need to set to zero to avoid false matches if another
1768 #ifndef at middle of file */
1769 file->ifndef_macro = 0;
1770 while (tok != TOK_LINEFEED)
1771 next_nomacro();
1772 tok_flags |= TOK_FLAG_ENDIF;
1773 goto the_end;
1775 break;
1776 case TOK_PPNUM:
1777 n = strtoul((char*)tokc.str.data, &q, 10);
1778 goto _line_num;
1779 case TOK_LINE:
1780 next();
1781 if (tok != TOK_CINT)
1782 _line_err:
1783 tcc_error("wrong #line format");
1784 n = tokc.i;
1785 _line_num:
1786 next();
1787 if (tok != TOK_LINEFEED) {
1788 if (tok == TOK_STR)
1789 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1790 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1791 break;
1792 else
1793 goto _line_err;
1794 --n;
1796 if (file->fd > 0)
1797 total_lines += file->line_num - n;
1798 file->line_num = n;
1799 if (s1->do_debug)
1800 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1801 break;
1802 case TOK_ERROR:
1803 case TOK_WARNING:
1804 c = tok;
1805 ch = file->buf_ptr[0];
1806 skip_spaces();
1807 q = buf;
1808 while (ch != '\n' && ch != CH_EOF) {
1809 if ((q - buf) < sizeof(buf) - 1)
1810 *q++ = ch;
1811 if (ch == '\\') {
1812 if (handle_stray_noerror() == 0)
1813 --q;
1814 } else
1815 inp();
1817 *q = '\0';
1818 if (c == TOK_ERROR)
1819 tcc_error("#error %s", buf);
1820 else
1821 tcc_warning("#warning %s", buf);
1822 break;
1823 case TOK_PRAGMA:
1824 pragma_parse(s1);
1825 break;
1826 case TOK_LINEFEED:
1827 goto the_end;
1828 default:
1829 /* ignore gas line comment in an 'S' file. */
1830 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1831 goto ignore;
1832 if (tok == '!' && is_bof)
1833 /* '!' is ignored at beginning to allow C scripts. */
1834 goto ignore;
1835 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1836 ignore:
1837 file->buf_ptr = parse_line_comment(file->buf_ptr);
1838 goto the_end;
1840 /* ignore other preprocess commands or #! for C scripts */
1841 while (tok != TOK_LINEFEED)
1842 next_nomacro();
1843 the_end:
1844 parse_flags = saved_parse_flags;
1847 /* evaluate escape codes in a string. */
1848 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1850 int c, n;
1851 const uint8_t *p;
1853 p = buf;
1854 for(;;) {
1855 c = *p;
1856 if (c == '\0')
1857 break;
1858 if (c == '\\') {
1859 p++;
1860 /* escape */
1861 c = *p;
1862 switch(c) {
1863 case '0': case '1': case '2': case '3':
1864 case '4': case '5': case '6': case '7':
1865 /* at most three octal digits */
1866 n = c - '0';
1867 p++;
1868 c = *p;
1869 if (isoct(c)) {
1870 n = n * 8 + c - '0';
1871 p++;
1872 c = *p;
1873 if (isoct(c)) {
1874 n = n * 8 + c - '0';
1875 p++;
1878 c = n;
1879 goto add_char_nonext;
1880 case 'x':
1881 case 'u':
1882 case 'U':
1883 p++;
1884 n = 0;
1885 for(;;) {
1886 c = *p;
1887 if (c >= 'a' && c <= 'f')
1888 c = c - 'a' + 10;
1889 else if (c >= 'A' && c <= 'F')
1890 c = c - 'A' + 10;
1891 else if (isnum(c))
1892 c = c - '0';
1893 else
1894 break;
1895 n = n * 16 + c;
1896 p++;
1898 c = n;
1899 goto add_char_nonext;
1900 case 'a':
1901 c = '\a';
1902 break;
1903 case 'b':
1904 c = '\b';
1905 break;
1906 case 'f':
1907 c = '\f';
1908 break;
1909 case 'n':
1910 c = '\n';
1911 break;
1912 case 'r':
1913 c = '\r';
1914 break;
1915 case 't':
1916 c = '\t';
1917 break;
1918 case 'v':
1919 c = '\v';
1920 break;
1921 case 'e':
1922 if (!gnu_ext)
1923 goto invalid_escape;
1924 c = 27;
1925 break;
1926 case '\'':
1927 case '\"':
1928 case '\\':
1929 case '?':
1930 break;
1931 default:
1932 invalid_escape:
1933 if (c >= '!' && c <= '~')
1934 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1935 else
1936 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1937 break;
1940 p++;
1941 add_char_nonext:
1942 if (!is_long)
1943 cstr_ccat(outstr, c);
1944 else
1945 cstr_wccat(outstr, c);
1947 /* add a trailing '\0' */
1948 if (!is_long)
1949 cstr_ccat(outstr, '\0');
1950 else
1951 cstr_wccat(outstr, '\0');
1954 void parse_string(const char *s, int len)
1956 uint8_t buf[1000], *p = buf;
1957 int is_long, sep;
1959 if ((is_long = *s == 'L'))
1960 ++s, --len;
1961 sep = *s++;
1962 len -= 2;
1963 if (len >= sizeof buf)
1964 p = tcc_malloc(len + 1);
1965 memcpy(p, s, len);
1966 p[len] = 0;
1968 cstr_reset(&tokcstr);
1969 parse_escape_string(&tokcstr, p, is_long);
1970 if (p != buf)
1971 tcc_free(p);
1973 if (sep == '\'') {
1974 int char_size;
1975 /* XXX: make it portable */
1976 if (!is_long)
1977 char_size = 1;
1978 else
1979 char_size = sizeof(nwchar_t);
1980 if (tokcstr.size <= char_size)
1981 tcc_error("empty character constant");
1982 if (tokcstr.size > 2 * char_size)
1983 tcc_warning("multi-character character constant");
1984 if (!is_long) {
1985 tokc.i = *(int8_t *)tokcstr.data;
1986 tok = TOK_CCHAR;
1987 } else {
1988 tokc.i = *(nwchar_t *)tokcstr.data;
1989 tok = TOK_LCHAR;
1991 } else {
1992 tokc.str.size = tokcstr.size;
1993 tokc.str.data = tokcstr.data;
1994 tokc.str.data_allocated = tokcstr.data_allocated;
1995 if (!is_long)
1996 tok = TOK_STR;
1997 else
1998 tok = TOK_LSTR;
2002 /* we use 64 bit numbers */
2003 #define BN_SIZE 2
2005 /* bn = (bn << shift) | or_val */
2006 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2008 int i;
2009 unsigned int v;
2010 for(i=0;i<BN_SIZE;i++) {
2011 v = bn[i];
2012 bn[i] = (v << shift) | or_val;
2013 or_val = v >> (32 - shift);
2017 static void bn_zero(unsigned int *bn)
2019 int i;
2020 for(i=0;i<BN_SIZE;i++) {
2021 bn[i] = 0;
2025 /* parse number in null terminated string 'p' and return it in the
2026 current token */
2027 static void parse_number(const char *p)
2029 int b, t, shift, frac_bits, s, exp_val, ch;
2030 char *q;
2031 unsigned int bn[BN_SIZE];
2032 double d;
2034 /* number */
2035 q = token_buf;
2036 ch = *p++;
2037 t = ch;
2038 ch = *p++;
2039 *q++ = t;
2040 b = 10;
2041 if (t == '.') {
2042 goto float_frac_parse;
2043 } else if (t == '0') {
2044 if (ch == 'x' || ch == 'X') {
2045 q--;
2046 ch = *p++;
2047 b = 16;
2048 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2049 q--;
2050 ch = *p++;
2051 b = 2;
2054 /* parse all digits. cannot check octal numbers at this stage
2055 because of floating point constants */
2056 while (1) {
2057 if (ch >= 'a' && ch <= 'f')
2058 t = ch - 'a' + 10;
2059 else if (ch >= 'A' && ch <= 'F')
2060 t = ch - 'A' + 10;
2061 else if (isnum(ch))
2062 t = ch - '0';
2063 else
2064 break;
2065 if (t >= b)
2066 break;
2067 if (q >= token_buf + STRING_MAX_SIZE) {
2068 num_too_long:
2069 tcc_error("number too long");
2071 *q++ = ch;
2072 ch = *p++;
2074 if (ch == '.' ||
2075 ((ch == 'e' || ch == 'E') && b == 10) ||
2076 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2077 if (b != 10) {
2078 /* NOTE: strtox should support that for hexa numbers, but
2079 non ISOC99 libcs do not support it, so we prefer to do
2080 it by hand */
2081 /* hexadecimal or binary floats */
2082 /* XXX: handle overflows */
2083 *q = '\0';
2084 if (b == 16)
2085 shift = 4;
2086 else
2087 shift = 1;
2088 bn_zero(bn);
2089 q = token_buf;
2090 while (1) {
2091 t = *q++;
2092 if (t == '\0') {
2093 break;
2094 } else if (t >= 'a') {
2095 t = t - 'a' + 10;
2096 } else if (t >= 'A') {
2097 t = t - 'A' + 10;
2098 } else {
2099 t = t - '0';
2101 bn_lshift(bn, shift, t);
2103 frac_bits = 0;
2104 if (ch == '.') {
2105 ch = *p++;
2106 while (1) {
2107 t = ch;
2108 if (t >= 'a' && t <= 'f') {
2109 t = t - 'a' + 10;
2110 } else if (t >= 'A' && t <= 'F') {
2111 t = t - 'A' + 10;
2112 } else if (t >= '0' && t <= '9') {
2113 t = t - '0';
2114 } else {
2115 break;
2117 if (t >= b)
2118 tcc_error("invalid digit");
2119 bn_lshift(bn, shift, t);
2120 frac_bits += shift;
2121 ch = *p++;
2124 if (ch != 'p' && ch != 'P')
2125 expect("exponent");
2126 ch = *p++;
2127 s = 1;
2128 exp_val = 0;
2129 if (ch == '+') {
2130 ch = *p++;
2131 } else if (ch == '-') {
2132 s = -1;
2133 ch = *p++;
2135 if (ch < '0' || ch > '9')
2136 expect("exponent digits");
2137 while (ch >= '0' && ch <= '9') {
2138 exp_val = exp_val * 10 + ch - '0';
2139 ch = *p++;
2141 exp_val = exp_val * s;
2143 /* now we can generate the number */
2144 /* XXX: should patch directly float number */
2145 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2146 d = ldexp(d, exp_val - frac_bits);
2147 t = toup(ch);
2148 if (t == 'F') {
2149 ch = *p++;
2150 tok = TOK_CFLOAT;
2151 /* float : should handle overflow */
2152 tokc.f = (float)d;
2153 } else if (t == 'L') {
2154 ch = *p++;
2155 #ifdef TCC_TARGET_PE
2156 tok = TOK_CDOUBLE;
2157 tokc.d = d;
2158 #else
2159 tok = TOK_CLDOUBLE;
2160 /* XXX: not large enough */
2161 tokc.ld = (long double)d;
2162 #endif
2163 } else {
2164 tok = TOK_CDOUBLE;
2165 tokc.d = d;
2167 } else {
2168 /* decimal floats */
2169 if (ch == '.') {
2170 if (q >= token_buf + STRING_MAX_SIZE)
2171 goto num_too_long;
2172 *q++ = ch;
2173 ch = *p++;
2174 float_frac_parse:
2175 while (ch >= '0' && ch <= '9') {
2176 if (q >= token_buf + STRING_MAX_SIZE)
2177 goto num_too_long;
2178 *q++ = ch;
2179 ch = *p++;
2182 if (ch == 'e' || ch == 'E') {
2183 if (q >= token_buf + STRING_MAX_SIZE)
2184 goto num_too_long;
2185 *q++ = ch;
2186 ch = *p++;
2187 if (ch == '-' || ch == '+') {
2188 if (q >= token_buf + STRING_MAX_SIZE)
2189 goto num_too_long;
2190 *q++ = ch;
2191 ch = *p++;
2193 if (ch < '0' || ch > '9')
2194 expect("exponent digits");
2195 while (ch >= '0' && ch <= '9') {
2196 if (q >= token_buf + STRING_MAX_SIZE)
2197 goto num_too_long;
2198 *q++ = ch;
2199 ch = *p++;
2202 *q = '\0';
2203 t = toup(ch);
2204 errno = 0;
2205 if (t == 'F') {
2206 ch = *p++;
2207 tok = TOK_CFLOAT;
2208 tokc.f = strtof(token_buf, NULL);
2209 } else if (t == 'L') {
2210 ch = *p++;
2211 #ifdef TCC_TARGET_PE
2212 tok = TOK_CDOUBLE;
2213 tokc.d = strtod(token_buf, NULL);
2214 #else
2215 tok = TOK_CLDOUBLE;
2216 tokc.ld = strtold(token_buf, NULL);
2217 #endif
2218 } else {
2219 tok = TOK_CDOUBLE;
2220 tokc.d = strtod(token_buf, NULL);
2223 } else {
2224 unsigned long long n, n1;
2225 int lcount, ucount, must_64bit;
2226 const char *p1;
2228 /* integer number */
2229 *q = '\0';
2230 q = token_buf;
2231 if (b == 10 && *q == '0') {
2232 b = 8;
2233 q++;
2235 n = 0;
2236 while(1) {
2237 t = *q++;
2238 /* no need for checks except for base 10 / 8 errors */
2239 if (t == '\0')
2240 break;
2241 else if (t >= 'a')
2242 t = t - 'a' + 10;
2243 else if (t >= 'A')
2244 t = t - 'A' + 10;
2245 else
2246 t = t - '0';
2247 if (t >= b)
2248 tcc_error("invalid digit");
2249 n1 = n;
2250 n = n * b + t;
2251 /* detect overflow */
2252 /* XXX: this test is not reliable */
2253 if (n < n1)
2254 tcc_error("integer constant overflow");
2257 /* Determine the characteristics (unsigned and/or 64bit) the type of
2258 the constant must have according to the constant suffix(es) */
2259 lcount = ucount = must_64bit = 0;
2260 p1 = p;
2261 for(;;) {
2262 t = toup(ch);
2263 if (t == 'L') {
2264 if (lcount >= 2)
2265 tcc_error("three 'l's in integer constant");
2266 if (lcount && *(p - 1) != ch)
2267 tcc_error("incorrect integer suffix: %s", p1);
2268 lcount++;
2269 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2270 if (lcount == 2)
2271 #endif
2272 must_64bit = 1;
2273 ch = *p++;
2274 } else if (t == 'U') {
2275 if (ucount >= 1)
2276 tcc_error("two 'u's in integer constant");
2277 ucount++;
2278 ch = *p++;
2279 } else {
2280 break;
2284 /* Whether 64 bits are needed to hold the constant's value */
2285 if (n & 0xffffffff00000000LL || must_64bit) {
2286 tok = TOK_CLLONG;
2287 n1 = n >> 32;
2288 } else {
2289 tok = TOK_CINT;
2290 n1 = n;
2293 /* Whether type must be unsigned to hold the constant's value */
2294 if (ucount || ((n1 >> 31) && (b != 10))) {
2295 if (tok == TOK_CLLONG)
2296 tok = TOK_CULLONG;
2297 else
2298 tok = TOK_CUINT;
2299 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2300 } else if (n1 >> 31) {
2301 if (tok == TOK_CINT)
2302 tok = TOK_CLLONG;
2303 else
2304 tcc_error("integer constant overflow");
2307 if (tok == TOK_CINT || tok == TOK_CUINT)
2308 tokc.i = n;
2309 else
2310 tokc.i = n;
2312 if (ch)
2313 tcc_error("invalid number\n");
2317 #define PARSE2(c1, tok1, c2, tok2) \
2318 case c1: \
2319 PEEKC(c, p); \
2320 if (c == c2) { \
2321 p++; \
2322 tok = tok2; \
2323 } else { \
2324 tok = tok1; \
2326 break;
2328 /* return next token without macro substitution */
2329 static inline void next_nomacro1(void)
2331 int t, c, is_long;
2332 TokenSym *ts;
2333 uint8_t *p, *p1;
2334 unsigned int h;
2336 p = file->buf_ptr;
2337 redo_no_start:
2338 c = *p;
2339 switch(c) {
2340 case ' ':
2341 case '\t':
2342 tok = c;
2343 p++;
2344 if (parse_flags & PARSE_FLAG_SPACES)
2345 goto keep_tok_flags;
2346 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2347 ++p;
2348 goto redo_no_start;
2349 case '\f':
2350 case '\v':
2351 case '\r':
2352 p++;
2353 goto redo_no_start;
2354 case '\\':
2355 /* first look if it is in fact an end of buffer */
2356 c = handle_stray1(p);
2357 p = file->buf_ptr;
2358 if (c == '\\')
2359 goto parse_simple;
2360 if (c != CH_EOF)
2361 goto redo_no_start;
2363 TCCState *s1 = tcc_state;
2364 if ((parse_flags & PARSE_FLAG_LINEFEED)
2365 && !(tok_flags & TOK_FLAG_EOF)) {
2366 tok_flags |= TOK_FLAG_EOF;
2367 tok = TOK_LINEFEED;
2368 goto keep_tok_flags;
2369 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2370 tok = TOK_EOF;
2371 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2372 tcc_error("missing #endif");
2373 } else if (s1->include_stack_ptr == s1->include_stack) {
2374 /* no include left : end of file. */
2375 tok = TOK_EOF;
2376 } else {
2377 tok_flags &= ~TOK_FLAG_EOF;
2378 /* pop include file */
2380 /* test if previous '#endif' was after a #ifdef at
2381 start of file */
2382 if (tok_flags & TOK_FLAG_ENDIF) {
2383 #ifdef INC_DEBUG
2384 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2385 #endif
2386 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2387 tok_flags &= ~TOK_FLAG_ENDIF;
2390 /* add end of include file debug info */
2391 if (tcc_state->do_debug) {
2392 put_stabd(N_EINCL, 0, 0);
2394 /* pop include stack */
2395 tcc_close();
2396 s1->include_stack_ptr--;
2397 p = file->buf_ptr;
2398 goto redo_no_start;
2401 break;
2403 case '\n':
2404 file->line_num++;
2405 tok_flags |= TOK_FLAG_BOL;
2406 p++;
2407 maybe_newline:
2408 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2409 goto redo_no_start;
2410 tok = TOK_LINEFEED;
2411 goto keep_tok_flags;
2413 case '#':
2414 /* XXX: simplify */
2415 PEEKC(c, p);
2416 if ((tok_flags & TOK_FLAG_BOL) &&
2417 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2418 file->buf_ptr = p;
2419 preprocess(tok_flags & TOK_FLAG_BOF);
2420 p = file->buf_ptr;
2421 goto maybe_newline;
2422 } else {
2423 if (c == '#') {
2424 p++;
2425 tok = TOK_TWOSHARPS;
2426 } else {
2427 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2428 p = parse_line_comment(p - 1);
2429 goto redo_no_start;
2430 } else {
2431 tok = '#';
2435 break;
2437 /* dollar is allowed to start identifiers when not parsing asm */
2438 case '$':
2439 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2440 || (parse_flags & PARSE_FLAG_ASM_FILE))
2441 goto parse_simple;
2443 case 'a': case 'b': case 'c': case 'd':
2444 case 'e': case 'f': case 'g': case 'h':
2445 case 'i': case 'j': case 'k': case 'l':
2446 case 'm': case 'n': case 'o': case 'p':
2447 case 'q': case 'r': case 's': case 't':
2448 case 'u': case 'v': case 'w': case 'x':
2449 case 'y': case 'z':
2450 case 'A': case 'B': case 'C': case 'D':
2451 case 'E': case 'F': case 'G': case 'H':
2452 case 'I': case 'J': case 'K':
2453 case 'M': case 'N': case 'O': case 'P':
2454 case 'Q': case 'R': case 'S': case 'T':
2455 case 'U': case 'V': case 'W': case 'X':
2456 case 'Y': case 'Z':
2457 case '_':
2458 parse_ident_fast:
2459 p1 = p;
2460 h = TOK_HASH_INIT;
2461 h = TOK_HASH_FUNC(h, c);
2462 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2463 h = TOK_HASH_FUNC(h, c);
2464 if (c != '\\') {
2465 TokenSym **pts;
2466 int len;
2468 /* fast case : no stray found, so we have the full token
2469 and we have already hashed it */
2470 len = p - p1;
2471 h &= (TOK_HASH_SIZE - 1);
2472 pts = &hash_ident[h];
2473 for(;;) {
2474 ts = *pts;
2475 if (!ts)
2476 break;
2477 if (ts->len == len && !memcmp(ts->str, p1, len))
2478 goto token_found;
2479 pts = &(ts->hash_next);
2481 ts = tok_alloc_new(pts, (char *) p1, len);
2482 token_found: ;
2483 } else {
2484 /* slower case */
2485 cstr_reset(&tokcstr);
2487 while (p1 < p) {
2488 cstr_ccat(&tokcstr, *p1);
2489 p1++;
2491 p--;
2492 PEEKC(c, p);
2493 parse_ident_slow:
2494 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) {
2495 cstr_ccat(&tokcstr, c);
2496 PEEKC(c, p);
2498 ts = tok_alloc(tokcstr.data, tokcstr.size);
2500 tok = ts->tok;
2501 break;
2502 case 'L':
2503 t = p[1];
2504 if (t != '\\' && t != '\'' && t != '\"') {
2505 /* fast case */
2506 goto parse_ident_fast;
2507 } else {
2508 PEEKC(c, p);
2509 if (c == '\'' || c == '\"') {
2510 is_long = 1;
2511 goto str_const;
2512 } else {
2513 cstr_reset(&tokcstr);
2514 cstr_ccat(&tokcstr, 'L');
2515 goto parse_ident_slow;
2518 break;
2520 case '0': case '1': case '2': case '3':
2521 case '4': case '5': case '6': case '7':
2522 case '8': case '9':
2523 cstr_reset(&tokcstr);
2524 /* after the first digit, accept digits, alpha, '.' or sign if
2525 prefixed by 'eEpP' */
2526 parse_num:
2527 for(;;) {
2528 t = c;
2529 cstr_ccat(&tokcstr, c);
2530 PEEKC(c, p);
2531 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2532 || c == '.'
2533 || ((c == '+' || c == '-')
2534 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2536 break;
2538 /* We add a trailing '\0' to ease parsing */
2539 cstr_ccat(&tokcstr, '\0');
2540 tokc.str.size = tokcstr.size;
2541 tokc.str.data = tokcstr.data;
2542 tokc.str.data_allocated = tokcstr.data_allocated;
2543 tok = TOK_PPNUM;
2544 break;
2546 case '.':
2547 /* special dot handling because it can also start a number */
2548 PEEKC(c, p);
2549 if (isnum(c)) {
2550 cstr_reset(&tokcstr);
2551 cstr_ccat(&tokcstr, '.');
2552 goto parse_num;
2553 } else if (c == '.') {
2554 PEEKC(c, p);
2555 if (c == '.') {
2556 p++;
2557 tok = TOK_DOTS;
2558 } else {
2559 *--p = '.'; /* may underflow into file->unget[] */
2560 tok = '.';
2562 } else {
2563 tok = '.';
2565 break;
2566 case '\'':
2567 case '\"':
2568 is_long = 0;
2569 str_const:
2570 cstr_reset(&tokcstr);
2571 if (is_long)
2572 cstr_ccat(&tokcstr, 'L');
2573 cstr_ccat(&tokcstr, c);
2574 p = parse_pp_string(p, c, &tokcstr);
2575 cstr_ccat(&tokcstr, c);
2576 cstr_ccat(&tokcstr, '\0');
2577 tokc.str.size = tokcstr.size;
2578 tokc.str.data = tokcstr.data;
2579 tokc.str.data_allocated = tokcstr.data_allocated;
2580 tok = TOK_PPSTR;
2581 break;
2583 case '<':
2584 PEEKC(c, p);
2585 if (c == '=') {
2586 p++;
2587 tok = TOK_LE;
2588 } else if (c == '<') {
2589 PEEKC(c, p);
2590 if (c == '=') {
2591 p++;
2592 tok = TOK_A_SHL;
2593 } else {
2594 tok = TOK_SHL;
2596 } else {
2597 tok = TOK_LT;
2599 break;
2600 case '>':
2601 PEEKC(c, p);
2602 if (c == '=') {
2603 p++;
2604 tok = TOK_GE;
2605 } else if (c == '>') {
2606 PEEKC(c, p);
2607 if (c == '=') {
2608 p++;
2609 tok = TOK_A_SAR;
2610 } else {
2611 tok = TOK_SAR;
2613 } else {
2614 tok = TOK_GT;
2616 break;
2618 case '&':
2619 PEEKC(c, p);
2620 if (c == '&') {
2621 p++;
2622 tok = TOK_LAND;
2623 } else if (c == '=') {
2624 p++;
2625 tok = TOK_A_AND;
2626 } else {
2627 tok = '&';
2629 break;
2631 case '|':
2632 PEEKC(c, p);
2633 if (c == '|') {
2634 p++;
2635 tok = TOK_LOR;
2636 } else if (c == '=') {
2637 p++;
2638 tok = TOK_A_OR;
2639 } else {
2640 tok = '|';
2642 break;
2644 case '+':
2645 PEEKC(c, p);
2646 if (c == '+') {
2647 p++;
2648 tok = TOK_INC;
2649 } else if (c == '=') {
2650 p++;
2651 tok = TOK_A_ADD;
2652 } else {
2653 tok = '+';
2655 break;
2657 case '-':
2658 PEEKC(c, p);
2659 if (c == '-') {
2660 p++;
2661 tok = TOK_DEC;
2662 } else if (c == '=') {
2663 p++;
2664 tok = TOK_A_SUB;
2665 } else if (c == '>') {
2666 p++;
2667 tok = TOK_ARROW;
2668 } else {
2669 tok = '-';
2671 break;
2673 PARSE2('!', '!', '=', TOK_NE)
2674 PARSE2('=', '=', '=', TOK_EQ)
2675 PARSE2('*', '*', '=', TOK_A_MUL)
2676 PARSE2('%', '%', '=', TOK_A_MOD)
2677 PARSE2('^', '^', '=', TOK_A_XOR)
2679 /* comments or operator */
2680 case '/':
2681 PEEKC(c, p);
2682 if (c == '*') {
2683 p = parse_comment(p);
2684 /* comments replaced by a blank */
2685 tok = ' ';
2686 goto keep_tok_flags;
2687 } else if (c == '/') {
2688 p = parse_line_comment(p);
2689 tok = ' ';
2690 goto keep_tok_flags;
2691 } else if (c == '=') {
2692 p++;
2693 tok = TOK_A_DIV;
2694 } else {
2695 tok = '/';
2697 break;
2699 /* simple tokens */
2700 case '(':
2701 case ')':
2702 case '[':
2703 case ']':
2704 case '{':
2705 case '}':
2706 case ',':
2707 case ';':
2708 case ':':
2709 case '?':
2710 case '~':
2711 case '@': /* only used in assembler */
2712 parse_simple:
2713 tok = c;
2714 p++;
2715 break;
2716 default:
2717 tcc_error("unrecognized character \\x%02x", c);
2718 break;
2720 tok_flags = 0;
2721 keep_tok_flags:
2722 file->buf_ptr = p;
2723 #if defined(PARSE_DEBUG)
2724 printf("token = %s\n", get_tok_str(tok, &tokc));
2725 #endif
2728 /* return next token without macro substitution. Can read input from
2729 macro_ptr buffer */
2730 static void next_nomacro_spc(void)
2732 if (macro_ptr) {
2733 redo:
2734 tok = *macro_ptr;
2735 if (tok) {
2736 TOK_GET(&tok, &macro_ptr, &tokc);
2737 if (tok == TOK_LINENUM) {
2738 file->line_num = tokc.i;
2739 goto redo;
2742 } else {
2743 next_nomacro1();
2747 ST_FUNC void next_nomacro(void)
2749 do {
2750 next_nomacro_spc();
2751 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2755 static void macro_subst(
2756 TokenString *tok_str,
2757 Sym **nested_list,
2758 const int *macro_str,
2759 int can_read_stream
2762 /* substitute arguments in replacement lists in macro_str by the values in
2763 args (field d) and return allocated string */
2764 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2766 int t, t0, t1, spc;
2767 const int *st;
2768 Sym *s;
2769 CValue cval;
2770 TokenString str;
2771 CString cstr;
2773 tok_str_new(&str);
2774 t0 = t1 = 0;
2775 while(1) {
2776 TOK_GET(&t, &macro_str, &cval);
2777 if (!t)
2778 break;
2779 if (t == '#') {
2780 /* stringize */
2781 TOK_GET(&t, &macro_str, &cval);
2782 if (!t)
2783 goto bad_stringy;
2784 s = sym_find2(args, t);
2785 if (s) {
2786 cstr_new(&cstr);
2787 cstr_ccat(&cstr, '\"');
2788 st = s->d;
2789 spc = 0;
2790 while (*st) {
2791 TOK_GET(&t, &st, &cval);
2792 if (t != TOK_PLCHLDR
2793 && t != TOK_NOSUBST
2794 && 0 == check_space(t, &spc)) {
2795 const char *s = get_tok_str(t, &cval);
2796 while (*s) {
2797 if (/*t == TOK_PPSTR &&*/ *s != '\'')
2798 add_char(&cstr, *s);
2799 else
2800 cstr_ccat(&cstr, *s);
2801 ++s;
2805 cstr.size -= spc;
2806 cstr_ccat(&cstr, '\"');
2807 cstr_ccat(&cstr, '\0');
2808 #ifdef PP_DEBUG
2809 printf("\nstringize: <%s>\n", (char *)cstr.data);
2810 #endif
2811 /* add string */
2812 cval.str.size = cstr.size;
2813 cval.str.data = cstr.data;
2814 cval.str.data_allocated = cstr.data_allocated;
2815 tok_str_add2(&str, TOK_PPSTR, &cval);
2816 tcc_free(cval.str.data_allocated);
2817 } else {
2818 bad_stringy:
2819 expect("macro parameter after '#'");
2821 } else if (t >= TOK_IDENT) {
2822 s = sym_find2(args, t);
2823 if (s) {
2824 int l0 = str.len;
2825 st = s->d;
2826 /* if '##' is present before or after, no arg substitution */
2827 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2828 /* special case for var arg macros : ## eats the ','
2829 if empty VA_ARGS variable. */
2830 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2831 if (*st == 0) {
2832 /* suppress ',' '##' */
2833 str.len -= 2;
2834 } else {
2835 /* suppress '##' and add variable */
2836 str.len--;
2837 goto add_var;
2839 } else {
2840 for(;;) {
2841 int t1;
2842 TOK_GET(&t1, &st, &cval);
2843 if (!t1)
2844 break;
2845 tok_str_add2(&str, t1, &cval);
2849 } else {
2850 add_var:
2851 /* NOTE: the stream cannot be read when macro
2852 substituing an argument */
2853 macro_subst(&str, nested_list, st, 0);
2855 if (str.len == l0) /* exanded to empty string */
2856 tok_str_add(&str, TOK_PLCHLDR);
2857 } else {
2858 tok_str_add(&str, t);
2860 } else {
2861 tok_str_add2(&str, t, &cval);
2863 t0 = t1, t1 = t;
2865 tok_str_add(&str, 0);
2866 return str.str;
2869 static char const ab_month_name[12][4] =
2871 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2872 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2875 /* peek or read [ws_str == NULL] next token from function macro call,
2876 walking up macro levels up to the file if necessary */
2877 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2879 int t;
2880 const int *p;
2881 Sym *sa;
2883 for (;;) {
2884 if (macro_ptr) {
2885 p = macro_ptr, t = *p;
2886 if (ws_str) {
2887 while (is_space(t) || TOK_LINEFEED == t)
2888 tok_str_add(ws_str, t), t = *++p;
2890 if (t == 0 && can_read_stream) {
2891 end_macro();
2892 /* also, end of scope for nested defined symbol */
2893 sa = *nested_list;
2894 while (sa && sa->v == -1)
2895 sa = sa->prev;
2896 if (sa)
2897 sa->v = -1;
2898 continue;
2900 } else {
2901 ch = handle_eob();
2902 if (ws_str) {
2903 while (is_space(ch) || ch == '\n' || ch == '/') {
2904 if (ch == '/') {
2905 int c;
2906 uint8_t *p = file->buf_ptr;
2907 PEEKC(c, p);
2908 if (c == '*') {
2909 p = parse_comment(p);
2910 file->buf_ptr = p - 1;
2911 } else if (c == '/') {
2912 p = parse_line_comment(p);
2913 file->buf_ptr = p - 1;
2914 } else
2915 break;
2916 ch = ' ';
2918 tok_str_add(ws_str, ch);
2919 cinp();
2922 t = ch;
2925 if (ws_str)
2926 return t;
2927 next_nomacro_spc();
2928 return tok;
2932 /* do macro substitution of current token with macro 's' and add
2933 result to (tok_str,tok_len). 'nested_list' is the list of all
2934 macros we got inside to avoid recursing. Return non zero if no
2935 substitution needs to be done */
2936 static int macro_subst_tok(
2937 TokenString *tok_str,
2938 Sym **nested_list,
2939 Sym *s,
2940 int can_read_stream)
2942 Sym *args, *sa, *sa1;
2943 int parlevel, *mstr, t, t1, spc;
2944 TokenString str;
2945 char *cstrval;
2946 CValue cval;
2947 CString cstr;
2948 char buf[32];
2950 /* if symbol is a macro, prepare substitution */
2951 /* special macros */
2952 if (tok == TOK___LINE__) {
2953 snprintf(buf, sizeof(buf), "%d", file->line_num);
2954 cstrval = buf;
2955 t1 = TOK_PPNUM;
2956 goto add_cstr1;
2957 } else if (tok == TOK___FILE__) {
2958 cstrval = file->filename;
2959 goto add_cstr;
2960 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2961 time_t ti;
2962 struct tm *tm;
2964 time(&ti);
2965 tm = localtime(&ti);
2966 if (tok == TOK___DATE__) {
2967 snprintf(buf, sizeof(buf), "%s %2d %d",
2968 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2969 } else {
2970 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2971 tm->tm_hour, tm->tm_min, tm->tm_sec);
2973 cstrval = buf;
2974 add_cstr:
2975 t1 = TOK_STR;
2976 add_cstr1:
2977 cstr_new(&cstr);
2978 cstr_cat(&cstr, cstrval);
2979 cstr_ccat(&cstr, '\0');
2980 cval.str.size = cstr.size;
2981 cval.str.data = cstr.data;
2982 cval.str.data_allocated = cstr.data_allocated;
2983 tok_str_add2(tok_str, t1, &cval);
2984 cstr_free(&cstr);
2985 } else {
2986 int saved_parse_flags = parse_flags;
2988 mstr = s->d;
2989 if (s->type.t == MACRO_FUNC) {
2990 /* whitespace between macro name and argument list */
2991 TokenString ws_str;
2992 tok_str_new(&ws_str);
2994 spc = 0;
2995 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
2996 | PARSE_FLAG_ACCEPT_STRAYS;
2998 /* get next token from argument stream */
2999 t = next_argstream(nested_list, can_read_stream, &ws_str);
3000 if (t != '(') {
3001 /* not a macro substitution after all, restore the
3002 * macro token plus all whitespace we've read.
3003 * whitespace is intentionally not merged to preserve
3004 * newlines. */
3005 parse_flags = saved_parse_flags;
3006 tok_str_add(tok_str, tok);
3007 if (parse_flags & PARSE_FLAG_SPACES) {
3008 int i;
3009 for (i = 0; i < ws_str.len; i++)
3010 tok_str_add(tok_str, ws_str.str[i]);
3012 tok_str_free(ws_str.str);
3013 return 0;
3014 } else {
3015 tok_str_free(ws_str.str);
3017 next_nomacro(); /* eat '(' */
3019 /* argument macro */
3020 args = NULL;
3021 sa = s->next;
3022 /* NOTE: empty args are allowed, except if no args */
3023 for(;;) {
3024 do {
3025 next_argstream(nested_list, can_read_stream, NULL);
3026 } while (is_space(tok) || TOK_LINEFEED == tok);
3027 empty_arg:
3028 /* handle '()' case */
3029 if (!args && !sa && tok == ')')
3030 break;
3031 if (!sa)
3032 tcc_error("macro '%s' used with too many args",
3033 get_tok_str(s->v, 0));
3034 tok_str_new(&str);
3035 parlevel = spc = 0;
3036 /* NOTE: non zero sa->t indicates VA_ARGS */
3037 while ((parlevel > 0 ||
3038 (tok != ')' &&
3039 (tok != ',' || sa->type.t)))) {
3040 if (tok == TOK_EOF || tok == 0)
3041 break;
3042 if (tok == '(')
3043 parlevel++;
3044 else if (tok == ')')
3045 parlevel--;
3046 if (tok == TOK_LINEFEED)
3047 tok = ' ';
3048 if (!check_space(tok, &spc))
3049 tok_str_add2(&str, tok, &tokc);
3050 next_argstream(nested_list, can_read_stream, NULL);
3052 if (parlevel)
3053 expect(")");
3054 str.len -= spc;
3055 tok_str_add(&str, 0);
3056 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3057 sa1->d = str.str;
3058 sa = sa->next;
3059 if (tok == ')') {
3060 /* special case for gcc var args: add an empty
3061 var arg argument if it is omitted */
3062 if (sa && sa->type.t && gnu_ext)
3063 goto empty_arg;
3064 break;
3066 if (tok != ',')
3067 expect(",");
3069 if (sa) {
3070 tcc_error("macro '%s' used with too few args",
3071 get_tok_str(s->v, 0));
3074 parse_flags = saved_parse_flags;
3076 /* now subst each arg */
3077 mstr = macro_arg_subst(nested_list, mstr, args);
3078 /* free memory */
3079 sa = args;
3080 while (sa) {
3081 sa1 = sa->prev;
3082 tok_str_free(sa->d);
3083 sym_free(sa);
3084 sa = sa1;
3088 sym_push2(nested_list, s->v, 0, 0);
3089 parse_flags = saved_parse_flags;
3090 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3092 /* pop nested defined symbol */
3093 sa1 = *nested_list;
3094 *nested_list = sa1->prev;
3095 sym_free(sa1);
3096 if (mstr != s->d)
3097 tok_str_free(mstr);
3099 return 0;
3102 int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3104 CString cstr;
3105 int n;
3107 cstr_new(&cstr);
3108 if (t1 != TOK_PLCHLDR)
3109 cstr_cat(&cstr, get_tok_str(t1, v1));
3110 n = cstr.size;
3111 if (t2 != TOK_PLCHLDR)
3112 cstr_cat(&cstr, get_tok_str(t2, v2));
3113 cstr_ccat(&cstr, '\0');
3115 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3116 memcpy(file->buffer, cstr.data, cstr.size);
3117 for (;;) {
3118 next_nomacro1();
3119 if (0 == *file->buf_ptr)
3120 break;
3121 if (is_space(tok))
3122 continue;
3123 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3124 n, cstr.data, (char*)cstr.data + n);
3125 break;
3127 tcc_close();
3129 //printf("paste <%s>\n", (char*)cstr.data);
3130 cstr_free(&cstr);
3131 return 0;
3134 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3135 return the resulting string (which must be freed). */
3136 static inline int *macro_twosharps(const int *ptr0)
3138 int t;
3139 CValue cval;
3140 TokenString macro_str1;
3141 int start_of_nosubsts = -1;
3142 const int *ptr;
3144 /* we search the first '##' */
3145 for (ptr = ptr0;;) {
3146 TOK_GET(&t, &ptr, &cval);
3147 if (t == TOK_TWOSHARPS)
3148 break;
3149 if (t == 0)
3150 return NULL;
3153 tok_str_new(&macro_str1);
3155 //tok_print(" $$$", ptr0);
3156 for (ptr = ptr0;;) {
3157 TOK_GET(&t, &ptr, &cval);
3158 if (t == 0)
3159 break;
3160 if (t == TOK_TWOSHARPS)
3161 continue;
3162 while (*ptr == TOK_TWOSHARPS) {
3163 int t1; CValue cv1;
3164 /* given 'a##b', remove nosubsts preceding 'a' */
3165 if (start_of_nosubsts >= 0)
3166 macro_str1.len = start_of_nosubsts;
3167 /* given 'a##b', remove nosubsts preceding 'b' */
3168 while ((t1 = *++ptr) == TOK_NOSUBST)
3170 if (t1 && t1 != TOK_TWOSHARPS
3171 && t1 != ':') /* 'a##:' don't build a new token */
3173 TOK_GET(&t1, &ptr, &cv1);
3174 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3175 paste_tokens(t, &cval, t1, &cv1);
3176 t = tok, cval = tokc;
3180 if (t == TOK_NOSUBST) {
3181 if (start_of_nosubsts < 0)
3182 start_of_nosubsts = macro_str1.len;
3183 } else {
3184 start_of_nosubsts = -1;
3186 tok_str_add2(&macro_str1, t, &cval);
3188 tok_str_add(&macro_str1, 0);
3189 //tok_print(" ###", macro_str1.str);
3190 return macro_str1.str;
3193 /* do macro substitution of macro_str and add result to
3194 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3195 inside to avoid recursing. */
3196 static void macro_subst(
3197 TokenString *tok_str,
3198 Sym **nested_list,
3199 const int *macro_str,
3200 int can_read_stream
3203 Sym *s;
3204 const int *ptr;
3205 int t, spc, nosubst;
3206 CValue cval;
3207 int *macro_str1 = NULL;
3209 /* first scan for '##' operator handling */
3210 ptr = macro_str;
3211 spc = nosubst = 0;
3213 /* first scan for '##' operator handling */
3214 if (can_read_stream) {
3215 macro_str1 = macro_twosharps(ptr);
3216 if (macro_str1)
3217 ptr = macro_str1;
3220 while (1) {
3221 TOK_GET(&t, &ptr, &cval);
3222 if (t == 0)
3223 break;
3225 if (t >= TOK_IDENT && 0 == nosubst) {
3226 s = define_find(t);
3227 if (s == NULL)
3228 goto no_subst;
3230 /* if nested substitution, do nothing */
3231 if (sym_find2(*nested_list, t)) {
3232 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3233 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3234 goto no_subst;
3238 TokenString str;
3239 str.str = (int*)ptr;
3240 begin_macro(&str, 2);
3242 tok = t;
3243 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3245 if (str.alloc == 3) {
3246 /* already finished by reading function macro arguments */
3247 break;
3250 ptr = macro_ptr;
3251 end_macro ();
3254 spc = (tok_str->len &&
3255 is_space(tok_last(tok_str->str,
3256 tok_str->str + tok_str->len)));
3258 } else {
3260 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3261 tcc_error("stray '\\' in program");
3263 no_subst:
3264 if (!check_space(t, &spc))
3265 tok_str_add2(tok_str, t, &cval);
3266 nosubst = 0;
3267 if (t == TOK_NOSUBST)
3268 nosubst = 1;
3271 if (macro_str1)
3272 tok_str_free(macro_str1);
3276 /* return next token with macro substitution */
3277 ST_FUNC void next(void)
3279 redo:
3280 if (parse_flags & PARSE_FLAG_SPACES)
3281 next_nomacro_spc();
3282 else
3283 next_nomacro();
3285 if (macro_ptr) {
3286 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3287 /* discard preprocessor markers */
3288 goto redo;
3289 } else if (tok == 0) {
3290 /* end of macro or unget token string */
3291 end_macro();
3292 goto redo;
3294 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3295 Sym *s;
3296 /* if reading from file, try to substitute macros */
3297 s = define_find(tok);
3298 if (s) {
3299 static TokenString str; /* using static string for speed */
3300 Sym *nested_list = NULL;
3301 tok_str_new(&str);
3302 nested_list = NULL;
3303 macro_subst_tok(&str, &nested_list, s, 1);
3304 tok_str_add(&str, 0);
3305 begin_macro(&str, 0);
3306 goto redo;
3309 /* convert preprocessor tokens into C tokens */
3310 if (tok == TOK_PPNUM) {
3311 if (parse_flags & PARSE_FLAG_TOK_NUM)
3312 parse_number((char *)tokc.str.data);
3313 } else if (tok == TOK_PPSTR) {
3314 if (parse_flags & PARSE_FLAG_TOK_STR)
3315 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3319 /* push back current token and set current token to 'last_tok'. Only
3320 identifier case handled for labels. */
3321 ST_INLN void unget_tok(int last_tok)
3323 TokenString *str = tcc_malloc(sizeof *str);
3324 tok_str_new(str);
3325 tok_str_add2(str, tok, &tokc);
3326 tok_str_add(str, 0);
3327 begin_macro(str, 1);
3328 tok = last_tok;
3331 /* better than nothing, but needs extension to handle '-E' option
3332 correctly too */
3333 ST_FUNC void preprocess_init(TCCState *s1)
3335 s1->include_stack_ptr = s1->include_stack;
3336 /* XXX: move that before to avoid having to initialize
3337 file->ifdef_stack_ptr ? */
3338 s1->ifdef_stack_ptr = s1->ifdef_stack;
3339 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3341 pvtop = vtop = vstack - 1;
3342 s1->pack_stack[0] = 0;
3343 s1->pack_stack_ptr = s1->pack_stack;
3345 isidnum_table['$' - CH_EOF] =
3346 tcc_state->dollars_in_identifiers ? IS_ID : 0;
3349 ST_FUNC void preprocess_new(void)
3351 int i, c;
3352 const char *p, *r;
3354 /* init isid table */
3355 for(i = CH_EOF; i<256; i++)
3356 isidnum_table[i - CH_EOF]
3357 = is_space(i) ? IS_SPC
3358 : isid(i) ? IS_ID
3359 : isnum(i) ? IS_NUM
3360 : 0;
3362 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3364 tok_ident = TOK_IDENT;
3365 p = tcc_keywords;
3366 while (*p) {
3367 r = p;
3368 for(;;) {
3369 c = *r++;
3370 if (c == '\0')
3371 break;
3373 tok_alloc(p, r - p - 1);
3374 p = r;
3378 ST_FUNC void preprocess_delete(void)
3380 int i, n;
3382 /* free -D and compiler defines */
3383 free_defines(NULL);
3385 /* cleanup from error/setjmp */
3386 while (macro_stack)
3387 end_macro();
3388 macro_ptr = NULL;
3390 /* free tokens */
3391 n = tok_ident - TOK_IDENT;
3392 for(i = 0; i < n; i++)
3393 tcc_free(table_ident[i]);
3394 tcc_free(table_ident);
3395 table_ident = NULL;
3398 /* Preprocess the current file */
3399 ST_FUNC int tcc_preprocess(TCCState *s1)
3401 BufferedFile **iptr;
3402 int token_seen, spcs, level;
3404 preprocess_init(s1);
3405 ch = file->buf_ptr[0];
3406 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3407 parse_flags = PARSE_FLAG_PREPROCESS
3408 | (parse_flags & PARSE_FLAG_ASM_FILE)
3409 | PARSE_FLAG_LINEFEED
3410 | PARSE_FLAG_SPACES
3411 | PARSE_FLAG_ACCEPT_STRAYS
3414 #ifdef PP_BENCH
3415 do next(); while (tok != TOK_EOF); return 0;
3416 #endif
3418 token_seen = spcs = 0;
3419 pp_line(s1, file, 0);
3421 for (;;) {
3422 iptr = s1->include_stack_ptr;
3423 next();
3424 if (tok == TOK_EOF)
3425 break;
3426 level = s1->include_stack_ptr - iptr;
3427 if (level) {
3428 if (level > 0)
3429 pp_line(s1, *iptr, 0);
3430 pp_line(s1, file, level);
3433 if (0 == token_seen) {
3434 if (tok == ' ') {
3435 ++spcs;
3436 continue;
3438 if (tok == TOK_LINEFEED) {
3439 spcs = 0;
3440 continue;
3442 pp_line(s1, file, 0);
3443 while (spcs)
3444 fputs(" ", s1->ppfp), --spcs;
3445 token_seen = 1;
3447 } else if (tok == TOK_LINEFEED) {
3448 ++file->line_ref;
3449 token_seen = 0;
3452 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3455 return 0;