tcc help output for the -xc -xa - options
[tinycc.git] / tccpp.c
blobb1bf11377c507b4b19af5ed3dc4236fa54ffa591
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 char buf[STRING_MAX_SIZE + 1];
281 static CString cstr_buf;
282 CString *cstr;
283 char *p;
284 int i, len;
286 /* NOTE: to go faster, we give a fixed buffer for small strings */
287 cstr_reset(&cstr_buf);
288 cstr_buf.data = buf;
289 cstr_buf.size_allocated = sizeof(buf);
290 p = buf;
292 switch(v) {
293 case TOK_CINT:
294 case TOK_CUINT:
295 /* XXX: not quite exact, but only useful for testing */
296 sprintf(p, "%u", cv->ui);
297 break;
298 case TOK_CLLONG:
299 case TOK_CULLONG:
300 /* XXX: not quite exact, but only useful for testing */
301 #ifdef _WIN32
302 sprintf(p, "%u", (unsigned)cv->ull);
303 #else
304 sprintf(p, "%llu", cv->ull);
305 #endif
306 break;
307 case TOK_LCHAR:
308 cstr_ccat(&cstr_buf, 'L');
309 case TOK_CCHAR:
310 cstr_ccat(&cstr_buf, '\'');
311 add_char(&cstr_buf, cv->i);
312 cstr_ccat(&cstr_buf, '\'');
313 cstr_ccat(&cstr_buf, '\0');
314 break;
315 case TOK_PPNUM:
316 case TOK_PPSTR:
317 return (char*)cv->cstr->data;
318 case TOK_LSTR:
319 cstr_ccat(&cstr_buf, 'L');
320 case TOK_STR:
321 cstr = cv->cstr;
322 cstr_ccat(&cstr_buf, '\"');
323 if (v == TOK_STR) {
324 len = cstr->size - 1;
325 for(i=0;i<len;i++)
326 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
327 } else {
328 len = (cstr->size / sizeof(nwchar_t)) - 1;
329 for(i=0;i<len;i++)
330 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
332 cstr_ccat(&cstr_buf, '\"');
333 cstr_ccat(&cstr_buf, '\0');
334 break;
336 case TOK_CFLOAT:
337 case TOK_CDOUBLE:
338 case TOK_CLDOUBLE:
339 case TOK_LINENUM:
340 return NULL; /* should not happen */
342 /* above tokens have value, the ones below don't */
344 case TOK_LT:
345 v = '<';
346 goto addv;
347 case TOK_GT:
348 v = '>';
349 goto addv;
350 case TOK_DOTS:
351 return strcpy(p, "...");
352 case TOK_A_SHL:
353 return strcpy(p, "<<=");
354 case TOK_A_SAR:
355 return strcpy(p, ">>=");
356 default:
357 if (v < TOK_IDENT) {
358 /* search in two bytes table */
359 const unsigned char *q = tok_two_chars;
360 while (*q) {
361 if (q[2] == v) {
362 *p++ = q[0];
363 *p++ = q[1];
364 *p = '\0';
365 return buf;
367 q += 3;
369 if (v >= 127) {
370 sprintf(buf, "<%02x>", v);
371 return buf;
373 addv:
374 *p++ = v;
375 *p = '\0';
376 } else if (v < tok_ident) {
377 return table_ident[v - TOK_IDENT]->str;
378 } else if (v >= SYM_FIRST_ANOM) {
379 /* special name for anonymous symbol */
380 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
381 } else {
382 /* should never happen */
383 return NULL;
385 break;
387 return cstr_buf.data;
390 /* return the current character, handling end of block if necessary
391 (but not stray) */
392 ST_FUNC int handle_eob(void)
394 BufferedFile *bf = file;
395 int len;
397 /* only tries to read if really end of buffer */
398 if (bf->buf_ptr >= bf->buf_end) {
399 if (bf->fd != -1) {
400 #if defined(PARSE_DEBUG)
401 len = 1;
402 #else
403 len = IO_BUF_SIZE;
404 #endif
405 len = read(bf->fd, bf->buffer, len);
406 if (len < 0)
407 len = 0;
408 } else {
409 len = 0;
411 total_bytes += len;
412 bf->buf_ptr = bf->buffer;
413 bf->buf_end = bf->buffer + len;
414 *bf->buf_end = CH_EOB;
416 if (bf->buf_ptr < bf->buf_end) {
417 return bf->buf_ptr[0];
418 } else {
419 bf->buf_ptr = bf->buf_end;
420 return CH_EOF;
424 /* read next char from current input file and handle end of input buffer */
425 ST_INLN void inp(void)
427 ch = *(++(file->buf_ptr));
428 /* end of buffer/file handling */
429 if (ch == CH_EOB)
430 ch = handle_eob();
433 /* handle '\[\r]\n' */
434 static int handle_stray_noerror(void)
436 while (ch == '\\') {
437 inp();
438 if (ch == '\n') {
439 file->line_num++;
440 inp();
441 } else if (ch == '\r') {
442 inp();
443 if (ch != '\n')
444 goto fail;
445 file->line_num++;
446 inp();
447 } else {
448 fail:
449 return 1;
452 return 0;
455 static void handle_stray(void)
457 if (handle_stray_noerror())
458 tcc_error("stray '\\' in program");
461 /* skip the stray and handle the \\n case. Output an error if
462 incorrect char after the stray */
463 static int handle_stray1(uint8_t *p)
465 int c;
467 file->buf_ptr = p;
468 if (p >= file->buf_end) {
469 c = handle_eob();
470 if (c != '\\')
471 return c;
472 p = file->buf_ptr;
474 ch = *p;
475 if (handle_stray_noerror()) {
476 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
477 tcc_error("stray '\\' in program");
478 *--file->buf_ptr = '\\';
480 p = file->buf_ptr;
481 c = *p;
482 return c;
485 /* handle just the EOB case, but not stray */
486 #define PEEKC_EOB(c, p)\
488 p++;\
489 c = *p;\
490 if (c == '\\') {\
491 file->buf_ptr = p;\
492 c = handle_eob();\
493 p = file->buf_ptr;\
497 /* handle the complicated stray case */
498 #define PEEKC(c, p)\
500 p++;\
501 c = *p;\
502 if (c == '\\') {\
503 c = handle_stray1(p);\
504 p = file->buf_ptr;\
508 /* input with '\[\r]\n' handling. Note that this function cannot
509 handle other characters after '\', so you cannot call it inside
510 strings or comments */
511 ST_FUNC void minp(void)
513 inp();
514 if (ch == '\\')
515 handle_stray();
519 /* single line C++ comments */
520 static uint8_t *parse_line_comment(uint8_t *p)
522 int c;
524 p++;
525 for(;;) {
526 c = *p;
527 redo:
528 if (c == '\n' || c == CH_EOF) {
529 break;
530 } else if (c == '\\') {
531 file->buf_ptr = p;
532 c = handle_eob();
533 p = file->buf_ptr;
534 if (c == '\\') {
535 PEEKC_EOB(c, p);
536 if (c == '\n') {
537 file->line_num++;
538 PEEKC_EOB(c, p);
539 } else if (c == '\r') {
540 PEEKC_EOB(c, p);
541 if (c == '\n') {
542 file->line_num++;
543 PEEKC_EOB(c, p);
546 } else {
547 goto redo;
549 } else {
550 p++;
553 return p;
556 /* C comments */
557 ST_FUNC uint8_t *parse_comment(uint8_t *p)
559 int c;
561 p++;
562 for(;;) {
563 /* fast skip loop */
564 for(;;) {
565 c = *p;
566 if (c == '\n' || c == '*' || c == '\\')
567 break;
568 p++;
569 c = *p;
570 if (c == '\n' || c == '*' || c == '\\')
571 break;
572 p++;
574 /* now we can handle all the cases */
575 if (c == '\n') {
576 file->line_num++;
577 p++;
578 } else if (c == '*') {
579 p++;
580 for(;;) {
581 c = *p;
582 if (c == '*') {
583 p++;
584 } else if (c == '/') {
585 goto end_of_comment;
586 } else if (c == '\\') {
587 file->buf_ptr = p;
588 c = handle_eob();
589 p = file->buf_ptr;
590 if (c == CH_EOF)
591 tcc_error("unexpected end of file in comment");
592 if (c == '\\') {
593 /* skip '\[\r]\n', otherwise just skip the stray */
594 while (c == '\\') {
595 PEEKC_EOB(c, p);
596 if (c == '\n') {
597 file->line_num++;
598 PEEKC_EOB(c, p);
599 } else if (c == '\r') {
600 PEEKC_EOB(c, p);
601 if (c == '\n') {
602 file->line_num++;
603 PEEKC_EOB(c, p);
605 } else {
606 goto after_star;
610 } else {
611 break;
614 after_star: ;
615 } else {
616 /* stray, eob or eof */
617 file->buf_ptr = p;
618 c = handle_eob();
619 p = file->buf_ptr;
620 if (c == CH_EOF) {
621 tcc_error("unexpected end of file in comment");
622 } else if (c == '\\') {
623 p++;
627 end_of_comment:
628 p++;
629 return p;
632 #define cinp minp
634 static inline void skip_spaces(void)
636 while (isidnum_table[ch - CH_EOF] & IS_SPC)
637 cinp();
640 static inline int check_space(int t, int *spc)
642 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
643 if (*spc)
644 return 1;
645 *spc = 1;
646 } else
647 *spc = 0;
648 return 0;
651 /* parse a string without interpreting escapes */
652 static uint8_t *parse_pp_string(uint8_t *p,
653 int sep, CString *str)
655 int c;
656 p++;
657 for(;;) {
658 c = *p;
659 if (c == sep) {
660 break;
661 } else if (c == '\\') {
662 file->buf_ptr = p;
663 c = handle_eob();
664 p = file->buf_ptr;
665 if (c == CH_EOF) {
666 unterminated_string:
667 /* XXX: indicate line number of start of string */
668 tcc_error("missing terminating %c character", sep);
669 } else if (c == '\\') {
670 /* escape : just skip \[\r]\n */
671 PEEKC_EOB(c, p);
672 if (c == '\n') {
673 file->line_num++;
674 p++;
675 } else if (c == '\r') {
676 PEEKC_EOB(c, p);
677 if (c != '\n')
678 expect("'\n' after '\r'");
679 file->line_num++;
680 p++;
681 } else if (c == CH_EOF) {
682 goto unterminated_string;
683 } else {
684 if (str) {
685 cstr_ccat(str, '\\');
686 cstr_ccat(str, c);
688 p++;
691 } else if (c == '\n') {
692 file->line_num++;
693 goto add_char;
694 } else if (c == '\r') {
695 PEEKC_EOB(c, p);
696 if (c != '\n') {
697 if (str)
698 cstr_ccat(str, '\r');
699 } else {
700 file->line_num++;
701 goto add_char;
703 } else {
704 add_char:
705 if (str)
706 cstr_ccat(str, c);
707 p++;
710 p++;
711 return p;
714 /* skip block of text until #else, #elif or #endif. skip also pairs of
715 #if/#endif */
716 static void preprocess_skip(void)
718 int a, start_of_line, c, in_warn_or_error;
719 uint8_t *p;
721 p = file->buf_ptr;
722 a = 0;
723 redo_start:
724 start_of_line = 1;
725 in_warn_or_error = 0;
726 for(;;) {
727 redo_no_start:
728 c = *p;
729 switch(c) {
730 case ' ':
731 case '\t':
732 case '\f':
733 case '\v':
734 case '\r':
735 p++;
736 goto redo_no_start;
737 case '\n':
738 file->line_num++;
739 p++;
740 goto redo_start;
741 case '\\':
742 file->buf_ptr = p;
743 c = handle_eob();
744 if (c == CH_EOF) {
745 expect("#endif");
746 } else if (c == '\\') {
747 ch = file->buf_ptr[0];
748 handle_stray_noerror();
750 p = file->buf_ptr;
751 goto redo_no_start;
752 /* skip strings */
753 case '\"':
754 case '\'':
755 if (in_warn_or_error)
756 goto _default;
757 p = parse_pp_string(p, c, NULL);
758 break;
759 /* skip comments */
760 case '/':
761 if (in_warn_or_error)
762 goto _default;
763 file->buf_ptr = p;
764 ch = *p;
765 minp();
766 p = file->buf_ptr;
767 if (ch == '*') {
768 p = parse_comment(p);
769 } else if (ch == '/') {
770 p = parse_line_comment(p);
772 break;
773 case '#':
774 p++;
775 if (start_of_line) {
776 file->buf_ptr = p;
777 next_nomacro();
778 p = file->buf_ptr;
779 if (a == 0 &&
780 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
781 goto the_end;
782 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
783 a++;
784 else if (tok == TOK_ENDIF)
785 a--;
786 else if( tok == TOK_ERROR || tok == TOK_WARNING)
787 in_warn_or_error = 1;
788 else if (tok == TOK_LINEFEED)
789 goto redo_start;
790 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
791 p = parse_line_comment(p);
792 break;
793 _default:
794 default:
795 p++;
796 break;
798 start_of_line = 0;
800 the_end: ;
801 file->buf_ptr = p;
804 /* ParseState handling */
806 /* XXX: currently, no include file info is stored. Thus, we cannot display
807 accurate messages if the function or data definition spans multiple
808 files */
810 /* save current parse state in 's' */
811 ST_FUNC void save_parse_state(ParseState *s)
813 s->line_num = file->line_num;
814 s->macro_ptr = macro_ptr;
815 s->tok = tok;
816 s->tokc = tokc;
819 /* restore parse state from 's' */
820 ST_FUNC void restore_parse_state(ParseState *s)
822 file->line_num = s->line_num;
823 macro_ptr = s->macro_ptr;
824 tok = s->tok;
825 tokc = s->tokc;
828 /* return the number of additional 'ints' necessary to store the
829 token */
830 static inline int tok_size(const int *p)
832 switch(*p) {
833 /* 4 bytes */
834 case TOK_CINT:
835 case TOK_CUINT:
836 case TOK_CCHAR:
837 case TOK_LCHAR:
838 case TOK_CFLOAT:
839 case TOK_LINENUM:
840 return 1 + 1;
841 case TOK_STR:
842 case TOK_LSTR:
843 case TOK_PPNUM:
844 case TOK_PPSTR:
845 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
846 case TOK_CDOUBLE:
847 case TOK_CLLONG:
848 case TOK_CULLONG:
849 return 1 + 2;
850 case TOK_CLDOUBLE:
851 return 1 + LDOUBLE_SIZE / 4;
852 default:
853 return 1 + 0;
857 /* token string handling */
859 ST_INLN void tok_str_new(TokenString *s)
861 s->str = NULL;
862 s->len = 0;
863 s->allocated_len = 0;
864 s->last_line_num = -1;
867 ST_FUNC void tok_str_free(int *str)
869 tcc_free(str);
872 static int *tok_str_realloc(TokenString *s)
874 int *str, len;
876 if (s->allocated_len == 0) {
877 len = 8;
878 } else {
879 len = s->allocated_len * 2;
881 str = tcc_realloc(s->str, len * sizeof(int));
882 s->allocated_len = len;
883 s->str = str;
884 return str;
887 ST_FUNC void tok_str_add(TokenString *s, int t)
889 int len, *str;
891 len = s->len;
892 str = s->str;
893 if (len >= s->allocated_len)
894 str = tok_str_realloc(s);
895 str[len++] = t;
896 s->len = len;
899 static void tok_str_add2(TokenString *s, int t, CValue *cv)
901 int len, *str;
903 len = s->len;
904 str = s->str;
906 /* allocate space for worst case */
907 if (len + TOK_MAX_SIZE > s->allocated_len)
908 str = tok_str_realloc(s);
909 str[len++] = t;
910 switch(t) {
911 case TOK_CINT:
912 case TOK_CUINT:
913 case TOK_CCHAR:
914 case TOK_LCHAR:
915 case TOK_CFLOAT:
916 case TOK_LINENUM:
917 str[len++] = cv->tab[0];
918 break;
919 case TOK_PPNUM:
920 case TOK_PPSTR:
921 case TOK_STR:
922 case TOK_LSTR:
924 int nb_words;
925 CString *cstr;
927 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
928 while ((len + nb_words) > s->allocated_len)
929 str = tok_str_realloc(s);
930 cstr = (CString *)(str + len);
931 cstr->data = NULL;
932 cstr->size = cv->cstr->size;
933 cstr->data_allocated = NULL;
934 cstr->size_allocated = cstr->size;
935 memcpy((char *)cstr + sizeof(CString),
936 cv->cstr->data, cstr->size);
937 len += nb_words;
939 break;
940 case TOK_CDOUBLE:
941 case TOK_CLLONG:
942 case TOK_CULLONG:
943 #if LDOUBLE_SIZE == 8
944 case TOK_CLDOUBLE:
945 #endif
946 str[len++] = cv->tab[0];
947 str[len++] = cv->tab[1];
948 break;
949 #if LDOUBLE_SIZE == 12
950 case TOK_CLDOUBLE:
951 str[len++] = cv->tab[0];
952 str[len++] = cv->tab[1];
953 str[len++] = cv->tab[2];
954 #elif LDOUBLE_SIZE == 16
955 case TOK_CLDOUBLE:
956 str[len++] = cv->tab[0];
957 str[len++] = cv->tab[1];
958 str[len++] = cv->tab[2];
959 str[len++] = cv->tab[3];
960 #elif LDOUBLE_SIZE != 8
961 #error add long double size support
962 #endif
963 break;
964 default:
965 break;
967 s->len = len;
970 /* add the current parse token in token string 's' */
971 ST_FUNC void tok_str_add_tok(TokenString *s)
973 CValue cval;
975 /* save line number info */
976 if (file->line_num != s->last_line_num) {
977 s->last_line_num = file->line_num;
978 cval.i = s->last_line_num;
979 tok_str_add2(s, TOK_LINENUM, &cval);
981 tok_str_add2(s, tok, &tokc);
984 /* get a token from an integer array and increment pointer
985 accordingly. we code it as a macro to avoid pointer aliasing. */
986 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
988 const int *p = *pp;
989 int n, *tab;
991 tab = cv->tab;
992 switch(*t = *p++) {
993 case TOK_CINT:
994 case TOK_CUINT:
995 case TOK_CCHAR:
996 case TOK_LCHAR:
997 case TOK_CFLOAT:
998 case TOK_LINENUM:
999 tab[0] = *p++;
1000 break;
1001 case TOK_STR:
1002 case TOK_LSTR:
1003 case TOK_PPNUM:
1004 case TOK_PPSTR:
1005 cv->cstr = (CString *)p;
1006 cv->cstr->data = (char *)p + sizeof(CString);
1007 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1008 break;
1009 case TOK_CDOUBLE:
1010 case TOK_CLLONG:
1011 case TOK_CULLONG:
1012 n = 2;
1013 goto copy;
1014 case TOK_CLDOUBLE:
1015 #if LDOUBLE_SIZE == 16
1016 n = 4;
1017 #elif LDOUBLE_SIZE == 12
1018 n = 3;
1019 #elif LDOUBLE_SIZE == 8
1020 n = 2;
1021 #else
1022 # error add long double size support
1023 #endif
1024 copy:
1026 *tab++ = *p++;
1027 while (--n);
1028 break;
1029 default:
1030 break;
1032 *pp = p;
1035 static int macro_is_equal(const int *a, const int *b)
1037 char buf[STRING_MAX_SIZE + 1];
1038 CValue cv;
1039 int t;
1040 while (*a && *b) {
1041 TOK_GET(&t, &a, &cv);
1042 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1043 TOK_GET(&t, &b, &cv);
1044 if (strcmp(buf, get_tok_str(t, &cv)))
1045 return 0;
1047 return !(*a || *b);
1050 static void pp_line(TCCState *s1, BufferedFile *f, int level)
1052 int d = f->line_num - f->line_ref;
1053 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE
1054 || (level == 0 && f->line_ref && d < 8))
1056 while (d > 0)
1057 fputs("\n", s1->ppfp), --d;
1059 else
1060 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
1061 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
1063 else {
1064 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
1065 level > 0 ? " 1" : level < 0 ? " 2" : "");
1067 f->line_ref = f->line_num;
1070 static void tok_print(const char *msg, const int *str)
1072 FILE *pr = tcc_state->ppfp;
1073 int t;
1074 CValue cval;
1076 fprintf(pr, "%s ", msg);
1077 while (str) {
1078 TOK_GET(&t, &str, &cval);
1079 if (!t)
1080 break;
1081 fprintf(pr,"%s", get_tok_str(t, &cval));
1083 fprintf(pr, "\n");
1086 static int define_print_prepared(Sym *s)
1088 if (!s || !tcc_state->ppfp || tcc_state->dflag == 0)
1089 return 0;
1091 if (s->v < TOK_IDENT || s->v >= tok_ident)
1092 return 0;
1094 if (file) {
1095 file->line_num--;
1096 pp_line(tcc_state, file, 0);
1097 file->line_ref = ++file->line_num;
1099 return 1;
1102 static void define_print(int v)
1104 FILE *pr = tcc_state->ppfp;
1105 Sym *s, *a;
1107 s = define_find(v);
1108 if (define_print_prepared(s) == 0)
1109 return;
1111 fprintf(pr, "// #define %s", get_tok_str(v, NULL));
1112 if (s->type.t == MACRO_FUNC) {
1113 a = s->next;
1114 fprintf(pr,"(");
1115 if (a)
1116 for (;;) {
1117 fprintf(pr,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
1118 if (!(a = a->next))
1119 break;
1120 fprintf(pr,",");
1122 fprintf(pr,")");
1124 tok_print("", s->d);
1127 static void undef_print(int v)
1129 FILE *pr = tcc_state->ppfp;
1130 Sym *s;
1132 s = define_find(v);
1133 if (define_print_prepared(s) == 0)
1134 return;
1136 fprintf(pr, "// #undef %s\n", get_tok_str(s->v, NULL));
1139 ST_FUNC void print_defines(void)
1141 Sym *top = define_stack;
1142 while (top) {
1143 define_print(top->v);
1144 top = top->prev;
1148 /* defines handling */
1149 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1151 Sym *s;
1153 s = define_find(v);
1154 if (s && !macro_is_equal(s->d, str))
1155 tcc_warning("%s redefined", get_tok_str(v, NULL));
1157 s = sym_push2(&define_stack, v, macro_type, 0);
1158 s->d = str;
1159 s->next = first_arg;
1160 table_ident[v - TOK_IDENT]->sym_define = s;
1163 /* undefined a define symbol. Its name is just set to zero */
1164 ST_FUNC void define_undef(Sym *s)
1166 int v = s->v;
1167 undef_print(v);
1168 if (v >= TOK_IDENT && v < tok_ident)
1169 table_ident[v - TOK_IDENT]->sym_define = NULL;
1172 ST_INLN Sym *define_find(int v)
1174 v -= TOK_IDENT;
1175 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1176 return NULL;
1177 return table_ident[v]->sym_define;
1180 /* free define stack until top reaches 'b' */
1181 ST_FUNC void free_defines(Sym *b)
1183 Sym *top, *top1;
1184 int v;
1186 top = define_stack;
1187 while (top != b) {
1188 top1 = top->prev;
1189 /* do not free args or predefined defines */
1190 if (top->d)
1191 tok_str_free(top->d);
1192 v = top->v;
1193 if (v >= TOK_IDENT && v < tok_ident)
1194 table_ident[v - TOK_IDENT]->sym_define = NULL;
1195 sym_free(top);
1196 top = top1;
1198 define_stack = b;
1201 /* label lookup */
1202 ST_FUNC Sym *label_find(int v)
1204 v -= TOK_IDENT;
1205 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1206 return NULL;
1207 return table_ident[v]->sym_label;
1210 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1212 Sym *s, **ps;
1213 s = sym_push2(ptop, v, 0, 0);
1214 s->r = flags;
1215 ps = &table_ident[v - TOK_IDENT]->sym_label;
1216 if (ptop == &global_label_stack) {
1217 /* modify the top most local identifier, so that
1218 sym_identifier will point to 's' when popped */
1219 while (*ps != NULL)
1220 ps = &(*ps)->prev_tok;
1222 s->prev_tok = *ps;
1223 *ps = s;
1224 return s;
1227 /* pop labels until element last is reached. Look if any labels are
1228 undefined. Define symbols if '&&label' was used. */
1229 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1231 Sym *s, *s1;
1232 for(s = *ptop; s != slast; s = s1) {
1233 s1 = s->prev;
1234 if (s->r == LABEL_DECLARED) {
1235 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1236 } else if (s->r == LABEL_FORWARD) {
1237 tcc_error("label '%s' used but not defined",
1238 get_tok_str(s->v, NULL));
1239 } else {
1240 if (s->c) {
1241 /* define corresponding symbol. A size of
1242 1 is put. */
1243 put_extern_sym(s, cur_text_section, s->jnext, 1);
1246 /* remove label */
1247 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1248 sym_free(s);
1250 *ptop = slast;
1253 /* eval an expression for #if/#elif */
1254 static int expr_preprocess(void)
1256 int c, t;
1257 TokenString str;
1259 tok_str_new(&str);
1260 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1261 next(); /* do macro subst */
1262 if (tok == TOK_DEFINED) {
1263 next_nomacro();
1264 t = tok;
1265 if (t == '(')
1266 next_nomacro();
1267 c = define_find(tok) != 0;
1268 if (t == '(')
1269 next_nomacro();
1270 tok = TOK_CINT;
1271 tokc.i = c;
1272 } else if (tok >= TOK_IDENT) {
1273 /* if undefined macro */
1274 tok = TOK_CINT;
1275 tokc.i = 0;
1277 tok_str_add_tok(&str);
1279 tok_str_add(&str, -1); /* simulate end of file */
1280 tok_str_add(&str, 0);
1281 /* now evaluate C constant expression */
1282 begin_macro(&str, 0);
1283 next();
1284 c = expr_const();
1285 end_macro();
1286 return c != 0;
1290 /* parse after #define */
1291 ST_FUNC void parse_define(void)
1293 Sym *s, *first, **ps;
1294 int v, t, varg, is_vaargs, spc;
1295 int saved_parse_flags = parse_flags;
1296 TokenString str;
1298 v = tok;
1299 if (v < TOK_IDENT)
1300 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1301 /* XXX: should check if same macro (ANSI) */
1302 first = NULL;
1303 t = MACRO_OBJ;
1304 /* '(' must be just after macro definition for MACRO_FUNC */
1305 parse_flags |= PARSE_FLAG_SPACES;
1306 next_nomacro_spc();
1307 if (tok == '(') {
1308 next_nomacro();
1309 ps = &first;
1310 if (tok != ')') for (;;) {
1311 varg = tok;
1312 next_nomacro();
1313 is_vaargs = 0;
1314 if (varg == TOK_DOTS) {
1315 varg = TOK___VA_ARGS__;
1316 is_vaargs = 1;
1317 } else if (tok == TOK_DOTS && gnu_ext) {
1318 is_vaargs = 1;
1319 next_nomacro();
1321 if (varg < TOK_IDENT)
1322 bad_list:
1323 tcc_error("bad macro parameter list");
1324 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1325 *ps = s;
1326 ps = &s->next;
1327 if (tok == ')')
1328 break;
1329 if (tok != ',' || is_vaargs)
1330 goto bad_list;
1331 next_nomacro();
1333 next_nomacro_spc();
1334 t = MACRO_FUNC;
1336 tok_str_new(&str);
1337 spc = 2;
1338 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1339 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1340 /* remove spaces around ## and after '#' */
1341 if (TOK_TWOSHARPS == tok) {
1342 if (2 == spc)
1343 goto bad_twosharp;
1344 if (1 == spc)
1345 --str.len;
1346 spc = 3;
1347 } else if ('#' == tok) {
1348 spc = 4;
1349 } else if (check_space(tok, &spc)) {
1350 goto skip;
1352 tok_str_add2(&str, tok, &tokc);
1353 skip:
1354 next_nomacro_spc();
1357 parse_flags = saved_parse_flags;
1358 if (spc == 1)
1359 --str.len; /* remove trailing space */
1360 tok_str_add(&str, 0);
1361 if (3 == spc)
1362 bad_twosharp:
1363 tcc_error("'##' cannot appear at either end of macro");
1364 define_push(v, t, str.str, first);
1365 define_print(v);
1368 static inline int hash_cached_include(const char *filename)
1370 const unsigned char *s;
1371 unsigned int h;
1373 h = TOK_HASH_INIT;
1374 s = (unsigned char *) filename;
1375 while (*s) {
1376 h = TOK_HASH_FUNC(h, *s);
1377 s++;
1379 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1380 return h;
1383 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1385 CachedInclude *e;
1386 int i, h;
1387 h = hash_cached_include(filename);
1388 i = s1->cached_includes_hash[h];
1389 for(;;) {
1390 if (i == 0)
1391 break;
1392 e = s1->cached_includes[i - 1];
1393 if (0 == PATHCMP(e->filename, filename))
1394 return e;
1395 i = e->hash_next;
1397 return NULL;
1400 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1402 CachedInclude *e;
1403 int h;
1405 if (search_cached_include(s1, filename))
1406 return;
1407 #ifdef INC_DEBUG
1408 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1409 #endif
1410 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1411 strcpy(e->filename, filename);
1412 e->ifndef_macro = ifndef_macro;
1413 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1414 /* add in hash table */
1415 h = hash_cached_include(filename);
1416 e->hash_next = s1->cached_includes_hash[h];
1417 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1420 static void pragma_parse(TCCState *s1)
1422 next_nomacro();
1423 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1424 int t = tok, v;
1425 Sym *s;
1427 if (next(), tok != '(')
1428 goto pragma_err;
1429 if (next(), tok != TOK_STR)
1430 goto pragma_err;
1431 v = tok_alloc(tokc.cstr->data, tokc.cstr->size - 1)->tok;
1432 if (next(), tok != ')')
1433 goto pragma_err;
1434 if (t == TOK_push_macro) {
1435 while (NULL == (s = define_find(v)))
1436 define_push(v, 0, NULL, NULL);
1437 s->type.ref = s; /* set push boundary */
1438 } else {
1439 for (s = define_stack; s; s = s->prev)
1440 if (s->v == v && s->type.ref == s) {
1441 s->type.ref = NULL;
1442 break;
1445 if (s)
1446 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1447 else
1448 tcc_warning("unbalanced #pragma pop_macro");
1450 } else if (tok == TOK_once) {
1451 add_cached_include(s1, file->filename, TOK_once);
1453 } else if (s1->ppfp) {
1454 /* tcc -E: keep pragmas below unchanged */
1455 unget_tok(' ');
1456 unget_tok(TOK_PRAGMA);
1457 unget_tok('#');
1458 unget_tok(TOK_LINEFEED);
1460 } else if (tok == TOK_pack) {
1461 /* This may be:
1462 #pragma pack(1) // set
1463 #pragma pack() // reset to default
1464 #pragma pack(push,1) // push & set
1465 #pragma pack(pop) // restore previous */
1466 next();
1467 skip('(');
1468 if (tok == TOK_ASM_pop) {
1469 next();
1470 if (s1->pack_stack_ptr <= s1->pack_stack) {
1471 stk_error:
1472 tcc_error("out of pack stack");
1474 s1->pack_stack_ptr--;
1475 } else {
1476 int val = 0;
1477 if (tok != ')') {
1478 if (tok == TOK_ASM_push) {
1479 next();
1480 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1481 goto stk_error;
1482 s1->pack_stack_ptr++;
1483 skip(',');
1485 if (tok != TOK_CINT)
1486 goto pragma_err;
1487 val = tokc.i;
1488 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1489 goto pragma_err;
1490 next();
1492 *s1->pack_stack_ptr = val;
1494 if (tok != ')')
1495 goto pragma_err;
1497 } else if (tok == TOK_comment) {
1498 char *file;
1499 next();
1500 skip('(');
1501 if (tok != TOK_lib)
1502 goto pragma_warn;
1503 next();
1504 skip(',');
1505 if (tok != TOK_STR)
1506 goto pragma_err;
1507 file = tcc_strdup((char *)tokc.cstr->data);
1508 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1509 next();
1510 if (tok != ')')
1511 goto pragma_err;
1512 } else {
1513 pragma_warn:
1514 if (s1->warn_unsupported)
1515 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1517 return;
1519 pragma_err:
1520 tcc_error("malformed #pragma directive");
1521 return;
1524 /* is_bof is true if first non space token at beginning of file */
1525 ST_FUNC void preprocess(int is_bof)
1527 TCCState *s1 = tcc_state;
1528 int i, c, n, saved_parse_flags;
1529 char buf[1024], *q;
1530 Sym *s;
1531 char *p3 = 0;
1533 saved_parse_flags = parse_flags;
1534 parse_flags = PARSE_FLAG_PREPROCESS
1535 | PARSE_FLAG_TOK_NUM
1536 | PARSE_FLAG_TOK_STR
1537 | PARSE_FLAG_LINEFEED
1538 | (parse_flags & PARSE_FLAG_ASM_FILE)
1541 next_nomacro();
1542 redo:
1543 switch(tok) {
1544 case TOK_DEFINE:
1545 next_nomacro();
1546 parse_define();
1547 break;
1548 case TOK_UNDEF:
1549 next_nomacro();
1550 s = define_find(tok);
1551 /* undefine symbol by putting an invalid name */
1552 if (s)
1553 define_undef(s);
1554 break;
1555 case TOK_INCLUDE:
1556 case TOK_INCLUDE_NEXT:
1557 ch = file->buf_ptr[0];
1558 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1559 skip_spaces();
1560 if (ch == '<') {
1561 c = '>';
1562 goto read_name;
1563 } else if (ch == '\"') {
1564 c = ch;
1565 read_name:
1566 inp();
1567 q = buf;
1568 while (ch != c && ch != '\n' && ch != CH_EOF) {
1569 if ((q - buf) < sizeof(buf) - 1)
1570 *q++ = ch;
1571 if (ch == '\\') {
1572 if (handle_stray_noerror() == 0)
1573 --q;
1574 } else
1575 inp();
1577 *q = '\0';
1578 minp();
1579 #if 0
1580 /* eat all spaces and comments after include */
1581 /* XXX: slightly incorrect */
1582 while (ch1 != '\n' && ch1 != CH_EOF)
1583 inp();
1584 #endif
1585 } else {
1586 /* computed #include : either we have only strings or
1587 we have anything enclosed in '<>' */
1588 next();
1589 buf[0] = '\0';
1590 if (tok == TOK_STR) {
1591 while (tok != TOK_LINEFEED) {
1592 if (tok != TOK_STR) {
1593 include_syntax:
1594 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1596 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1597 next();
1599 c = '\"';
1600 } else {
1601 int len;
1602 while (tok != TOK_LINEFEED) {
1603 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1604 next();
1606 len = strlen(buf);
1607 /* check syntax and remove '<>' */
1608 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1609 goto include_syntax;
1610 memmove(buf, buf + 1, len - 2);
1611 buf[len - 2] = '\0';
1612 c = '>';
1616 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1617 tcc_error("#include recursion too deep");
1618 /* store current file in stack, but increment stack later below */
1619 *s1->include_stack_ptr = file;
1621 #ifdef INC_DEBUG
1622 if (tok == TOK_INCLUDE_NEXT)
1623 printf("%s (1) include_next file %s\n", file->filename, buf);
1624 #endif
1626 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1627 for (i = -2; i < n; ++i) {
1628 char buf1[sizeof file->filename];
1629 CachedInclude *e;
1630 BufferedFile **f;
1631 const char *path;
1633 if (i == -2) {
1634 /* check absolute include path */
1635 if (!IS_ABSPATH(buf))
1636 continue;
1637 buf1[0] = 0;
1638 i = n; /* force end loop */
1640 } else if (i == -1) {
1641 /* search in current dir if "header.h" */
1642 if (c != '\"')
1643 continue;
1644 path = file->filename;
1645 pstrncpy(buf1, path, tcc_basename(path) - path);
1647 } else {
1648 /* search in all the include paths */
1649 if (i < s1->nb_include_paths)
1650 path = s1->include_paths[i];
1651 else
1652 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1653 pstrcpy(buf1, sizeof(buf1), path);
1654 pstrcat(buf1, sizeof(buf1), "/");
1657 #ifdef INC_DEBUG
1658 if (tok == TOK_INCLUDE_NEXT)
1659 printf("%s (2) include_next path <%s> name <%s>\n", file->filename, buf1, buf);
1660 #endif
1662 pstrcat(buf1, sizeof(buf1), buf);
1664 if (tok == TOK_INCLUDE_NEXT) {
1665 char *p1 = buf1;
1666 if ((p1[0] == '.') && IS_DIRSEP(p1[1])) p1 += 2;
1668 if (p3) {
1669 if (0 == PATHCMP(p1, p3)) {
1670 #ifdef INC_DEBUG
1671 printf("%s (3) include_next skipping <%s>\n", file->filename, p1);
1672 #endif
1673 goto include_trynext;
1676 else {
1677 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f) {
1678 char *p2 = (*f)->filename;
1679 if ((p2[0] == '.') && IS_DIRSEP(p2[1])) p2 += 2;
1680 if (0 == PATHCMP(p2, p1)) {
1681 p3 = p2;
1682 break;
1685 #ifdef INC_DEBUG
1686 printf("%s: (4) include_next skipping <%s> (p3=%p)\n", file->filename, buf1, p3);
1687 #endif
1688 goto include_trynext;
1690 #ifdef INC_DEBUG
1691 printf("%s (5) include_next considering <%s>\n", file->filename, buf1);
1692 #endif
1695 e = search_cached_include(s1, buf1);
1696 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
1697 /* no need to parse the include because the 'ifndef macro'
1698 is defined */
1700 #ifdef INC_DEBUG
1701 printf("%s: skipping cached <%s>\n", file->filename, buf1);
1702 #endif
1703 goto include_done;
1706 if (tcc_open(s1, buf1) < 0) {
1707 #ifdef INC_DEBUG
1708 printf("%s: include open failed <%s>\n", file->filename, buf1);
1709 #endif
1710 include_trynext:
1711 continue;
1714 #ifdef INC_DEBUG
1715 printf("%s: including <%s>\n", file->prev->filename, file->filename);
1716 #endif
1718 /* update target deps */
1719 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1720 tcc_strdup(buf1));
1721 /* push current file in stack */
1722 ++s1->include_stack_ptr;
1723 /* add include file debug info */
1724 if (s1->do_debug)
1725 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1726 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1727 ch = file->buf_ptr[0];
1728 goto the_end;
1730 tcc_error("include file '%s' not found", buf);
1731 include_done:
1732 break;
1733 case TOK_IFNDEF:
1734 c = 1;
1735 goto do_ifdef;
1736 case TOK_IF:
1737 c = expr_preprocess();
1738 goto do_if;
1739 case TOK_IFDEF:
1740 c = 0;
1741 do_ifdef:
1742 next_nomacro();
1743 if (tok < TOK_IDENT)
1744 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1745 if (is_bof) {
1746 if (c) {
1747 #ifdef INC_DEBUG
1748 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1749 #endif
1750 file->ifndef_macro = tok;
1753 c = (define_find(tok) != 0) ^ c;
1754 do_if:
1755 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1756 tcc_error("memory full (ifdef)");
1757 *s1->ifdef_stack_ptr++ = c;
1758 goto test_skip;
1759 case TOK_ELSE:
1760 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1761 tcc_error("#else without matching #if");
1762 if (s1->ifdef_stack_ptr[-1] & 2)
1763 tcc_error("#else after #else");
1764 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1765 goto test_else;
1766 case TOK_ELIF:
1767 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1768 tcc_error("#elif without matching #if");
1769 c = s1->ifdef_stack_ptr[-1];
1770 if (c > 1)
1771 tcc_error("#elif after #else");
1772 /* last #if/#elif expression was true: we skip */
1773 if (c == 1)
1774 goto skip;
1775 c = expr_preprocess();
1776 s1->ifdef_stack_ptr[-1] = c;
1777 test_else:
1778 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1779 file->ifndef_macro = 0;
1780 test_skip:
1781 if (!(c & 1)) {
1782 skip:
1783 preprocess_skip();
1784 is_bof = 0;
1785 goto redo;
1787 break;
1788 case TOK_ENDIF:
1789 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1790 tcc_error("#endif without matching #if");
1791 s1->ifdef_stack_ptr--;
1792 /* '#ifndef macro' was at the start of file. Now we check if
1793 an '#endif' is exactly at the end of file */
1794 if (file->ifndef_macro &&
1795 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1796 file->ifndef_macro_saved = file->ifndef_macro;
1797 /* need to set to zero to avoid false matches if another
1798 #ifndef at middle of file */
1799 file->ifndef_macro = 0;
1800 while (tok != TOK_LINEFEED)
1801 next_nomacro();
1802 tok_flags |= TOK_FLAG_ENDIF;
1803 goto the_end;
1805 break;
1806 case TOK_PPNUM:
1807 n = strtoul((char*)tokc.cstr->data, &q, 10);
1808 goto _line_num;
1809 case TOK_LINE:
1810 next();
1811 if (tok != TOK_CINT)
1812 _line_err:
1813 tcc_error("wrong #line format");
1814 n = tokc.i;
1815 _line_num:
1816 next();
1817 if (tok != TOK_LINEFEED) {
1818 if (tok == TOK_STR)
1819 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.cstr->data);
1820 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1821 break;
1822 else
1823 goto _line_err;
1824 --n;
1826 if (file->fd > 0)
1827 total_lines += file->line_num - n;
1828 file->line_num = n;
1829 if (s1->do_debug)
1830 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1831 break;
1832 case TOK_ERROR:
1833 case TOK_WARNING:
1834 c = tok;
1835 ch = file->buf_ptr[0];
1836 skip_spaces();
1837 q = buf;
1838 while (ch != '\n' && ch != CH_EOF) {
1839 if ((q - buf) < sizeof(buf) - 1)
1840 *q++ = ch;
1841 if (ch == '\\') {
1842 if (handle_stray_noerror() == 0)
1843 --q;
1844 } else
1845 inp();
1847 *q = '\0';
1848 if (c == TOK_ERROR)
1849 tcc_error("#error %s", buf);
1850 else
1851 tcc_warning("#warning %s", buf);
1852 break;
1853 case TOK_PRAGMA:
1854 pragma_parse(s1);
1855 break;
1856 case TOK_LINEFEED:
1857 goto the_end;
1858 default:
1859 /* ignore gas line comment in an 'S' file. */
1860 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1861 goto ignore;
1862 if (tok == '!' && is_bof)
1863 /* '!' is ignored at beginning to allow C scripts. */
1864 goto ignore;
1865 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1866 ignore:
1867 file->buf_ptr = parse_line_comment(file->buf_ptr);
1868 goto the_end;
1870 /* ignore other preprocess commands or #! for C scripts */
1871 while (tok != TOK_LINEFEED)
1872 next_nomacro();
1873 the_end:
1874 parse_flags = saved_parse_flags;
1877 /* evaluate escape codes in a string. */
1878 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1880 int c, n;
1881 const uint8_t *p;
1883 p = buf;
1884 for(;;) {
1885 c = *p;
1886 if (c == '\0')
1887 break;
1888 if (c == '\\') {
1889 p++;
1890 /* escape */
1891 c = *p;
1892 switch(c) {
1893 case '0': case '1': case '2': case '3':
1894 case '4': case '5': case '6': case '7':
1895 /* at most three octal digits */
1896 n = c - '0';
1897 p++;
1898 c = *p;
1899 if (isoct(c)) {
1900 n = n * 8 + c - '0';
1901 p++;
1902 c = *p;
1903 if (isoct(c)) {
1904 n = n * 8 + c - '0';
1905 p++;
1908 c = n;
1909 goto add_char_nonext;
1910 case 'x':
1911 case 'u':
1912 case 'U':
1913 p++;
1914 n = 0;
1915 for(;;) {
1916 c = *p;
1917 if (c >= 'a' && c <= 'f')
1918 c = c - 'a' + 10;
1919 else if (c >= 'A' && c <= 'F')
1920 c = c - 'A' + 10;
1921 else if (isnum(c))
1922 c = c - '0';
1923 else
1924 break;
1925 n = n * 16 + c;
1926 p++;
1928 c = n;
1929 goto add_char_nonext;
1930 case 'a':
1931 c = '\a';
1932 break;
1933 case 'b':
1934 c = '\b';
1935 break;
1936 case 'f':
1937 c = '\f';
1938 break;
1939 case 'n':
1940 c = '\n';
1941 break;
1942 case 'r':
1943 c = '\r';
1944 break;
1945 case 't':
1946 c = '\t';
1947 break;
1948 case 'v':
1949 c = '\v';
1950 break;
1951 case 'e':
1952 if (!gnu_ext)
1953 goto invalid_escape;
1954 c = 27;
1955 break;
1956 case '\'':
1957 case '\"':
1958 case '\\':
1959 case '?':
1960 break;
1961 default:
1962 invalid_escape:
1963 if (c >= '!' && c <= '~')
1964 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1965 else
1966 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1967 break;
1970 p++;
1971 add_char_nonext:
1972 if (!is_long)
1973 cstr_ccat(outstr, c);
1974 else
1975 cstr_wccat(outstr, c);
1977 /* add a trailing '\0' */
1978 if (!is_long)
1979 cstr_ccat(outstr, '\0');
1980 else
1981 cstr_wccat(outstr, '\0');
1984 void parse_string(const char *s, int len)
1986 uint8_t buf[1000], *p = buf;
1987 int is_long, sep;
1989 if ((is_long = *s == 'L'))
1990 ++s, --len;
1991 sep = *s++;
1992 len -= 2;
1993 if (len >= sizeof buf)
1994 p = tcc_malloc(len + 1);
1995 memcpy(p, s, len);
1996 p[len] = 0;
1998 cstr_reset(&tokcstr);
1999 parse_escape_string(&tokcstr, p, is_long);
2000 if (p != buf)
2001 tcc_free(p);
2003 if (sep == '\'') {
2004 int char_size;
2005 /* XXX: make it portable */
2006 if (!is_long)
2007 char_size = 1;
2008 else
2009 char_size = sizeof(nwchar_t);
2010 if (tokcstr.size <= char_size)
2011 tcc_error("empty character constant");
2012 if (tokcstr.size > 2 * char_size)
2013 tcc_warning("multi-character character constant");
2014 if (!is_long) {
2015 tokc.i = *(int8_t *)tokcstr.data;
2016 tok = TOK_CCHAR;
2017 } else {
2018 tokc.i = *(nwchar_t *)tokcstr.data;
2019 tok = TOK_LCHAR;
2021 } else {
2022 tokc.cstr = &tokcstr;
2023 if (!is_long)
2024 tok = TOK_STR;
2025 else
2026 tok = TOK_LSTR;
2030 /* we use 64 bit numbers */
2031 #define BN_SIZE 2
2033 /* bn = (bn << shift) | or_val */
2034 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2036 int i;
2037 unsigned int v;
2038 for(i=0;i<BN_SIZE;i++) {
2039 v = bn[i];
2040 bn[i] = (v << shift) | or_val;
2041 or_val = v >> (32 - shift);
2045 static void bn_zero(unsigned int *bn)
2047 int i;
2048 for(i=0;i<BN_SIZE;i++) {
2049 bn[i] = 0;
2053 /* parse number in null terminated string 'p' and return it in the
2054 current token */
2055 static void parse_number(const char *p)
2057 int b, t, shift, frac_bits, s, exp_val, ch;
2058 char *q;
2059 unsigned int bn[BN_SIZE];
2060 double d;
2062 /* number */
2063 q = token_buf;
2064 ch = *p++;
2065 t = ch;
2066 ch = *p++;
2067 *q++ = t;
2068 b = 10;
2069 if (t == '.') {
2070 goto float_frac_parse;
2071 } else if (t == '0') {
2072 if (ch == 'x' || ch == 'X') {
2073 q--;
2074 ch = *p++;
2075 b = 16;
2076 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2077 q--;
2078 ch = *p++;
2079 b = 2;
2082 /* parse all digits. cannot check octal numbers at this stage
2083 because of floating point constants */
2084 while (1) {
2085 if (ch >= 'a' && ch <= 'f')
2086 t = ch - 'a' + 10;
2087 else if (ch >= 'A' && ch <= 'F')
2088 t = ch - 'A' + 10;
2089 else if (isnum(ch))
2090 t = ch - '0';
2091 else
2092 break;
2093 if (t >= b)
2094 break;
2095 if (q >= token_buf + STRING_MAX_SIZE) {
2096 num_too_long:
2097 tcc_error("number too long");
2099 *q++ = ch;
2100 ch = *p++;
2102 if (ch == '.' ||
2103 ((ch == 'e' || ch == 'E') && b == 10) ||
2104 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2105 if (b != 10) {
2106 /* NOTE: strtox should support that for hexa numbers, but
2107 non ISOC99 libcs do not support it, so we prefer to do
2108 it by hand */
2109 /* hexadecimal or binary floats */
2110 /* XXX: handle overflows */
2111 *q = '\0';
2112 if (b == 16)
2113 shift = 4;
2114 else
2115 shift = 1;
2116 bn_zero(bn);
2117 q = token_buf;
2118 while (1) {
2119 t = *q++;
2120 if (t == '\0') {
2121 break;
2122 } else if (t >= 'a') {
2123 t = t - 'a' + 10;
2124 } else if (t >= 'A') {
2125 t = t - 'A' + 10;
2126 } else {
2127 t = t - '0';
2129 bn_lshift(bn, shift, t);
2131 frac_bits = 0;
2132 if (ch == '.') {
2133 ch = *p++;
2134 while (1) {
2135 t = ch;
2136 if (t >= 'a' && t <= 'f') {
2137 t = t - 'a' + 10;
2138 } else if (t >= 'A' && t <= 'F') {
2139 t = t - 'A' + 10;
2140 } else if (t >= '0' && t <= '9') {
2141 t = t - '0';
2142 } else {
2143 break;
2145 if (t >= b)
2146 tcc_error("invalid digit");
2147 bn_lshift(bn, shift, t);
2148 frac_bits += shift;
2149 ch = *p++;
2152 if (ch != 'p' && ch != 'P')
2153 expect("exponent");
2154 ch = *p++;
2155 s = 1;
2156 exp_val = 0;
2157 if (ch == '+') {
2158 ch = *p++;
2159 } else if (ch == '-') {
2160 s = -1;
2161 ch = *p++;
2163 if (ch < '0' || ch > '9')
2164 expect("exponent digits");
2165 while (ch >= '0' && ch <= '9') {
2166 exp_val = exp_val * 10 + ch - '0';
2167 ch = *p++;
2169 exp_val = exp_val * s;
2171 /* now we can generate the number */
2172 /* XXX: should patch directly float number */
2173 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2174 d = ldexp(d, exp_val - frac_bits);
2175 t = toup(ch);
2176 if (t == 'F') {
2177 ch = *p++;
2178 tok = TOK_CFLOAT;
2179 /* float : should handle overflow */
2180 tokc.f = (float)d;
2181 } else if (t == 'L') {
2182 ch = *p++;
2183 #ifdef TCC_TARGET_PE
2184 tok = TOK_CDOUBLE;
2185 tokc.d = d;
2186 #else
2187 tok = TOK_CLDOUBLE;
2188 /* XXX: not large enough */
2189 tokc.ld = (long double)d;
2190 #endif
2191 } else {
2192 tok = TOK_CDOUBLE;
2193 tokc.d = d;
2195 } else {
2196 /* decimal floats */
2197 if (ch == '.') {
2198 if (q >= token_buf + STRING_MAX_SIZE)
2199 goto num_too_long;
2200 *q++ = ch;
2201 ch = *p++;
2202 float_frac_parse:
2203 while (ch >= '0' && ch <= '9') {
2204 if (q >= token_buf + STRING_MAX_SIZE)
2205 goto num_too_long;
2206 *q++ = ch;
2207 ch = *p++;
2210 if (ch == 'e' || ch == 'E') {
2211 if (q >= token_buf + STRING_MAX_SIZE)
2212 goto num_too_long;
2213 *q++ = ch;
2214 ch = *p++;
2215 if (ch == '-' || ch == '+') {
2216 if (q >= token_buf + STRING_MAX_SIZE)
2217 goto num_too_long;
2218 *q++ = ch;
2219 ch = *p++;
2221 if (ch < '0' || ch > '9')
2222 expect("exponent digits");
2223 while (ch >= '0' && ch <= '9') {
2224 if (q >= token_buf + STRING_MAX_SIZE)
2225 goto num_too_long;
2226 *q++ = ch;
2227 ch = *p++;
2230 *q = '\0';
2231 t = toup(ch);
2232 errno = 0;
2233 if (t == 'F') {
2234 ch = *p++;
2235 tok = TOK_CFLOAT;
2236 tokc.f = strtof(token_buf, NULL);
2237 } else if (t == 'L') {
2238 ch = *p++;
2239 #ifdef TCC_TARGET_PE
2240 tok = TOK_CDOUBLE;
2241 tokc.d = strtod(token_buf, NULL);
2242 #else
2243 tok = TOK_CLDOUBLE;
2244 tokc.ld = strtold(token_buf, NULL);
2245 #endif
2246 } else {
2247 tok = TOK_CDOUBLE;
2248 tokc.d = strtod(token_buf, NULL);
2251 } else {
2252 unsigned long long n, n1;
2253 int lcount, ucount, must_64bit;
2254 const char *p1;
2256 /* integer number */
2257 *q = '\0';
2258 q = token_buf;
2259 if (b == 10 && *q == '0') {
2260 b = 8;
2261 q++;
2263 n = 0;
2264 while(1) {
2265 t = *q++;
2266 /* no need for checks except for base 10 / 8 errors */
2267 if (t == '\0')
2268 break;
2269 else if (t >= 'a')
2270 t = t - 'a' + 10;
2271 else if (t >= 'A')
2272 t = t - 'A' + 10;
2273 else
2274 t = t - '0';
2275 if (t >= b)
2276 tcc_error("invalid digit");
2277 n1 = n;
2278 n = n * b + t;
2279 /* detect overflow */
2280 /* XXX: this test is not reliable */
2281 if (n < n1)
2282 tcc_error("integer constant overflow");
2285 /* Determine the characteristics (unsigned and/or 64bit) the type of
2286 the constant must have according to the constant suffix(es) */
2287 lcount = ucount = must_64bit = 0;
2288 p1 = p;
2289 for(;;) {
2290 t = toup(ch);
2291 if (t == 'L') {
2292 if (lcount >= 2)
2293 tcc_error("three 'l's in integer constant");
2294 if (lcount && *(p - 1) != ch)
2295 tcc_error("incorrect integer suffix: %s", p1);
2296 lcount++;
2297 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2298 if (lcount == 2)
2299 #endif
2300 must_64bit = 1;
2301 ch = *p++;
2302 } else if (t == 'U') {
2303 if (ucount >= 1)
2304 tcc_error("two 'u's in integer constant");
2305 ucount++;
2306 ch = *p++;
2307 } else {
2308 break;
2312 /* Whether 64 bits are needed to hold the constant's value */
2313 if (n & 0xffffffff00000000LL || must_64bit) {
2314 tok = TOK_CLLONG;
2315 n1 = n >> 32;
2316 } else {
2317 tok = TOK_CINT;
2318 n1 = n;
2321 /* Whether type must be unsigned to hold the constant's value */
2322 if (ucount || ((n1 >> 31) && (b != 10))) {
2323 if (tok == TOK_CLLONG)
2324 tok = TOK_CULLONG;
2325 else
2326 tok = TOK_CUINT;
2327 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2328 } else if (n1 >> 31) {
2329 if (tok == TOK_CINT)
2330 tok = TOK_CLLONG;
2331 else
2332 tcc_error("integer constant overflow");
2335 if (tok == TOK_CINT || tok == TOK_CUINT)
2336 tokc.ui = n;
2337 else
2338 tokc.ull = n;
2340 if (ch)
2341 tcc_error("invalid number\n");
2345 #define PARSE2(c1, tok1, c2, tok2) \
2346 case c1: \
2347 PEEKC(c, p); \
2348 if (c == c2) { \
2349 p++; \
2350 tok = tok2; \
2351 } else { \
2352 tok = tok1; \
2354 break;
2356 /* return next token without macro substitution */
2357 static inline void next_nomacro1(void)
2359 int t, c, is_long;
2360 TokenSym *ts;
2361 uint8_t *p, *p1;
2362 unsigned int h;
2364 p = file->buf_ptr;
2365 redo_no_start:
2366 c = *p;
2367 switch(c) {
2368 case ' ':
2369 case '\t':
2370 tok = c;
2371 p++;
2372 if (parse_flags & PARSE_FLAG_SPACES)
2373 goto keep_tok_flags;
2374 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2375 ++p;
2376 goto redo_no_start;
2377 case '\f':
2378 case '\v':
2379 case '\r':
2380 p++;
2381 goto redo_no_start;
2382 case '\\':
2383 /* first look if it is in fact an end of buffer */
2384 c = handle_stray1(p);
2385 p = file->buf_ptr;
2386 if (c == '\\')
2387 goto parse_simple;
2388 if (c != CH_EOF)
2389 goto redo_no_start;
2391 TCCState *s1 = tcc_state;
2392 if ((parse_flags & PARSE_FLAG_LINEFEED)
2393 && !(tok_flags & TOK_FLAG_EOF)) {
2394 tok_flags |= TOK_FLAG_EOF;
2395 tok = TOK_LINEFEED;
2396 goto keep_tok_flags;
2397 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2398 tok = TOK_EOF;
2399 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2400 tcc_error("missing #endif");
2401 } else if (s1->include_stack_ptr == s1->include_stack) {
2402 /* no include left : end of file. */
2403 tok = TOK_EOF;
2404 } else {
2405 tok_flags &= ~TOK_FLAG_EOF;
2406 /* pop include file */
2408 /* test if previous '#endif' was after a #ifdef at
2409 start of file */
2410 if (tok_flags & TOK_FLAG_ENDIF) {
2411 #ifdef INC_DEBUG
2412 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2413 #endif
2414 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2415 tok_flags &= ~TOK_FLAG_ENDIF;
2418 /* add end of include file debug info */
2419 if (tcc_state->do_debug) {
2420 put_stabd(N_EINCL, 0, 0);
2422 /* pop include stack */
2423 tcc_close();
2424 s1->include_stack_ptr--;
2425 p = file->buf_ptr;
2426 goto redo_no_start;
2429 break;
2431 case '\n':
2432 file->line_num++;
2433 tok_flags |= TOK_FLAG_BOL;
2434 p++;
2435 maybe_newline:
2436 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2437 goto redo_no_start;
2438 tok = TOK_LINEFEED;
2439 goto keep_tok_flags;
2441 case '#':
2442 /* XXX: simplify */
2443 PEEKC(c, p);
2444 if ((tok_flags & TOK_FLAG_BOL) &&
2445 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2446 file->buf_ptr = p;
2447 preprocess(tok_flags & TOK_FLAG_BOF);
2448 p = file->buf_ptr;
2449 goto maybe_newline;
2450 } else {
2451 if (c == '#') {
2452 p++;
2453 tok = TOK_TWOSHARPS;
2454 } else {
2455 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2456 p = parse_line_comment(p - 1);
2457 goto redo_no_start;
2458 } else {
2459 tok = '#';
2463 break;
2465 /* dollar is allowed to start identifiers when not parsing asm */
2466 case '$':
2467 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2468 || (parse_flags & PARSE_FLAG_ASM_FILE))
2469 goto parse_simple;
2471 case 'a': case 'b': case 'c': case 'd':
2472 case 'e': case 'f': case 'g': case 'h':
2473 case 'i': case 'j': case 'k': case 'l':
2474 case 'm': case 'n': case 'o': case 'p':
2475 case 'q': case 'r': case 's': case 't':
2476 case 'u': case 'v': case 'w': case 'x':
2477 case 'y': case 'z':
2478 case 'A': case 'B': case 'C': case 'D':
2479 case 'E': case 'F': case 'G': case 'H':
2480 case 'I': case 'J': case 'K':
2481 case 'M': case 'N': case 'O': case 'P':
2482 case 'Q': case 'R': case 'S': case 'T':
2483 case 'U': case 'V': case 'W': case 'X':
2484 case 'Y': case 'Z':
2485 case '_':
2486 parse_ident_fast:
2487 p1 = p;
2488 h = TOK_HASH_INIT;
2489 h = TOK_HASH_FUNC(h, c);
2490 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2491 h = TOK_HASH_FUNC(h, c);
2492 if (c != '\\') {
2493 TokenSym **pts;
2494 int len;
2496 /* fast case : no stray found, so we have the full token
2497 and we have already hashed it */
2498 len = p - p1;
2499 h &= (TOK_HASH_SIZE - 1);
2500 pts = &hash_ident[h];
2501 for(;;) {
2502 ts = *pts;
2503 if (!ts)
2504 break;
2505 if (ts->len == len && !memcmp(ts->str, p1, len))
2506 goto token_found;
2507 pts = &(ts->hash_next);
2509 ts = tok_alloc_new(pts, (char *) p1, len);
2510 token_found: ;
2511 } else {
2512 /* slower case */
2513 cstr_reset(&tokcstr);
2515 while (p1 < p) {
2516 cstr_ccat(&tokcstr, *p1);
2517 p1++;
2519 p--;
2520 PEEKC(c, p);
2521 parse_ident_slow:
2522 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) {
2523 cstr_ccat(&tokcstr, c);
2524 PEEKC(c, p);
2526 ts = tok_alloc(tokcstr.data, tokcstr.size);
2528 tok = ts->tok;
2529 break;
2530 case 'L':
2531 t = p[1];
2532 if (t != '\\' && t != '\'' && t != '\"') {
2533 /* fast case */
2534 goto parse_ident_fast;
2535 } else {
2536 PEEKC(c, p);
2537 if (c == '\'' || c == '\"') {
2538 is_long = 1;
2539 goto str_const;
2540 } else {
2541 cstr_reset(&tokcstr);
2542 cstr_ccat(&tokcstr, 'L');
2543 goto parse_ident_slow;
2546 break;
2548 case '0': case '1': case '2': case '3':
2549 case '4': case '5': case '6': case '7':
2550 case '8': case '9':
2551 cstr_reset(&tokcstr);
2552 /* after the first digit, accept digits, alpha, '.' or sign if
2553 prefixed by 'eEpP' */
2554 parse_num:
2555 for(;;) {
2556 t = c;
2557 cstr_ccat(&tokcstr, c);
2558 PEEKC(c, p);
2559 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2560 || c == '.'
2561 || ((c == '+' || c == '-')
2562 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2564 break;
2566 /* We add a trailing '\0' to ease parsing */
2567 cstr_ccat(&tokcstr, '\0');
2568 tokc.cstr = &tokcstr;
2569 tok = TOK_PPNUM;
2570 break;
2572 case '.':
2573 /* special dot handling because it can also start a number */
2574 PEEKC(c, p);
2575 if (isnum(c)) {
2576 cstr_reset(&tokcstr);
2577 cstr_ccat(&tokcstr, '.');
2578 goto parse_num;
2579 } else if ((c == '.') && (p[1] == '.')){
2580 PEEKC(c, p);
2581 PEEKC(c, p);
2582 tok = TOK_DOTS;
2583 } else {
2584 tok = '.';
2586 break;
2587 case '\'':
2588 case '\"':
2589 is_long = 0;
2590 str_const:
2591 cstr_reset(&tokcstr);
2592 if (is_long)
2593 cstr_ccat(&tokcstr, 'L');
2594 cstr_ccat(&tokcstr, c);
2595 p = parse_pp_string(p, c, &tokcstr);
2596 cstr_ccat(&tokcstr, c);
2597 cstr_ccat(&tokcstr, '\0');
2598 tokc.cstr = &tokcstr;
2599 tok = TOK_PPSTR;
2600 break;
2602 case '<':
2603 PEEKC(c, p);
2604 if (c == '=') {
2605 p++;
2606 tok = TOK_LE;
2607 } else if (c == '<') {
2608 PEEKC(c, p);
2609 if (c == '=') {
2610 p++;
2611 tok = TOK_A_SHL;
2612 } else {
2613 tok = TOK_SHL;
2615 } else {
2616 tok = TOK_LT;
2618 break;
2619 case '>':
2620 PEEKC(c, p);
2621 if (c == '=') {
2622 p++;
2623 tok = TOK_GE;
2624 } else if (c == '>') {
2625 PEEKC(c, p);
2626 if (c == '=') {
2627 p++;
2628 tok = TOK_A_SAR;
2629 } else {
2630 tok = TOK_SAR;
2632 } else {
2633 tok = TOK_GT;
2635 break;
2637 case '&':
2638 PEEKC(c, p);
2639 if (c == '&') {
2640 p++;
2641 tok = TOK_LAND;
2642 } else if (c == '=') {
2643 p++;
2644 tok = TOK_A_AND;
2645 } else {
2646 tok = '&';
2648 break;
2650 case '|':
2651 PEEKC(c, p);
2652 if (c == '|') {
2653 p++;
2654 tok = TOK_LOR;
2655 } else if (c == '=') {
2656 p++;
2657 tok = TOK_A_OR;
2658 } else {
2659 tok = '|';
2661 break;
2663 case '+':
2664 PEEKC(c, p);
2665 if (c == '+') {
2666 p++;
2667 tok = TOK_INC;
2668 } else if (c == '=') {
2669 p++;
2670 tok = TOK_A_ADD;
2671 } else {
2672 tok = '+';
2674 break;
2676 case '-':
2677 PEEKC(c, p);
2678 if (c == '-') {
2679 p++;
2680 tok = TOK_DEC;
2681 } else if (c == '=') {
2682 p++;
2683 tok = TOK_A_SUB;
2684 } else if (c == '>') {
2685 p++;
2686 tok = TOK_ARROW;
2687 } else {
2688 tok = '-';
2690 break;
2692 PARSE2('!', '!', '=', TOK_NE)
2693 PARSE2('=', '=', '=', TOK_EQ)
2694 PARSE2('*', '*', '=', TOK_A_MUL)
2695 PARSE2('%', '%', '=', TOK_A_MOD)
2696 PARSE2('^', '^', '=', TOK_A_XOR)
2698 /* comments or operator */
2699 case '/':
2700 PEEKC(c, p);
2701 if (c == '*') {
2702 p = parse_comment(p);
2703 /* comments replaced by a blank */
2704 tok = ' ';
2705 goto keep_tok_flags;
2706 } else if (c == '/') {
2707 p = parse_line_comment(p);
2708 tok = ' ';
2709 goto keep_tok_flags;
2710 } else if (c == '=') {
2711 p++;
2712 tok = TOK_A_DIV;
2713 } else {
2714 tok = '/';
2716 break;
2718 /* simple tokens */
2719 case '(':
2720 case ')':
2721 case '[':
2722 case ']':
2723 case '{':
2724 case '}':
2725 case ',':
2726 case ';':
2727 case ':':
2728 case '?':
2729 case '~':
2730 case '@': /* only used in assembler */
2731 parse_simple:
2732 tok = c;
2733 p++;
2734 break;
2735 default:
2736 tcc_error("unrecognized character \\x%02x", c);
2737 break;
2739 tok_flags = 0;
2740 keep_tok_flags:
2741 file->buf_ptr = p;
2742 #if defined(PARSE_DEBUG)
2743 printf("token = %s\n", get_tok_str(tok, &tokc));
2744 #endif
2747 /* return next token without macro substitution. Can read input from
2748 macro_ptr buffer */
2749 static void next_nomacro_spc(void)
2751 if (macro_ptr) {
2752 redo:
2753 tok = *macro_ptr;
2754 if (tok) {
2755 TOK_GET(&tok, &macro_ptr, &tokc);
2756 if (tok == TOK_LINENUM) {
2757 file->line_num = tokc.i;
2758 goto redo;
2761 } else {
2762 next_nomacro1();
2766 ST_FUNC void next_nomacro(void)
2768 do {
2769 next_nomacro_spc();
2770 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2774 static void macro_subst(
2775 TokenString *tok_str,
2776 Sym **nested_list,
2777 const int *macro_str,
2778 int can_read_stream
2781 /* substitute arguments in replacement lists in macro_str by the values in
2782 args (field d) and return allocated string */
2783 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2785 int t, t0, t1, spc;
2786 const int *st;
2787 Sym *s;
2788 CValue cval;
2789 TokenString str;
2790 CString cstr;
2792 tok_str_new(&str);
2793 t0 = t1 = 0;
2794 while(1) {
2795 TOK_GET(&t, &macro_str, &cval);
2796 if (!t)
2797 break;
2798 if (t == '#') {
2799 /* stringize */
2800 TOK_GET(&t, &macro_str, &cval);
2801 if (!t)
2802 goto bad_stringy;
2803 s = sym_find2(args, t);
2804 if (s) {
2805 cstr_new(&cstr);
2806 cstr_ccat(&cstr, '\"');
2807 st = s->d;
2808 spc = 0;
2809 while (*st) {
2810 TOK_GET(&t, &st, &cval);
2811 if (t != TOK_PLCHLDR
2812 && t != TOK_NOSUBST
2813 && 0 == check_space(t, &spc)) {
2814 const char *s = get_tok_str(t, &cval);
2815 while (*s) {
2816 if (t == TOK_PPSTR && *s != '\'')
2817 add_char(&cstr, *s);
2818 else
2819 cstr_ccat(&cstr, *s);
2820 ++s;
2824 cstr.size -= spc;
2825 cstr_ccat(&cstr, '\"');
2826 cstr_ccat(&cstr, '\0');
2827 #ifdef PP_DEBUG
2828 printf("\nstringize: <%s>\n", (char *)cstr.data);
2829 #endif
2830 /* add string */
2831 cval.cstr = &cstr;
2832 tok_str_add2(&str, TOK_PPSTR, &cval);
2833 cstr_free(cval.cstr);
2834 } else {
2835 bad_stringy:
2836 expect("macro parameter after '#'");
2838 } else if (t >= TOK_IDENT) {
2839 s = sym_find2(args, t);
2840 if (s) {
2841 int l0 = str.len;
2842 st = s->d;
2843 /* if '##' is present before or after, no arg substitution */
2844 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2845 /* special case for var arg macros : ## eats the ','
2846 if empty VA_ARGS variable. */
2847 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2848 if (*st == 0) {
2849 /* suppress ',' '##' */
2850 str.len -= 2;
2851 } else {
2852 /* suppress '##' and add variable */
2853 str.len--;
2854 goto add_var;
2856 } else {
2857 for(;;) {
2858 int t1;
2859 TOK_GET(&t1, &st, &cval);
2860 if (!t1)
2861 break;
2862 tok_str_add2(&str, t1, &cval);
2866 } else {
2867 add_var:
2868 /* NOTE: the stream cannot be read when macro
2869 substituing an argument */
2870 macro_subst(&str, nested_list, st, 0);
2872 if (str.len == l0) /* exanded to empty string */
2873 tok_str_add(&str, TOK_PLCHLDR);
2874 } else {
2875 tok_str_add(&str, t);
2877 } else {
2878 tok_str_add2(&str, t, &cval);
2880 t0 = t1, t1 = t;
2882 tok_str_add(&str, 0);
2883 return str.str;
2886 static char const ab_month_name[12][4] =
2888 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2889 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2892 /* peek or read [ws_str == NULL] next token from function macro call,
2893 walking up macro levels up to the file if necessary */
2894 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2896 int t;
2897 const int *p;
2898 Sym *sa;
2900 for (;;) {
2901 if (macro_ptr) {
2902 p = macro_ptr, t = *p;
2903 if (ws_str) {
2904 while (is_space(t) || TOK_LINEFEED == t)
2905 tok_str_add(ws_str, t), t = *++p;
2907 if (t == 0 && can_read_stream) {
2908 end_macro();
2909 /* also, end of scope for nested defined symbol */
2910 sa = *nested_list;
2911 while (sa && sa->v == -1)
2912 sa = sa->prev;
2913 if (sa)
2914 sa->v = -1;
2915 continue;
2917 } else {
2918 ch = handle_eob();
2919 if (ws_str) {
2920 while (is_space(ch) || ch == '\n' || ch == '/') {
2921 if (ch == '/') {
2922 int c;
2923 uint8_t *p = file->buf_ptr;
2924 PEEKC(c, p);
2925 if (c == '*') {
2926 p = parse_comment(p);
2927 file->buf_ptr = p - 1;
2928 } else if (c == '/') {
2929 p = parse_line_comment(p);
2930 file->buf_ptr = p - 1;
2931 } else
2932 break;
2933 ch = ' ';
2935 tok_str_add(ws_str, ch);
2936 cinp();
2939 t = ch;
2942 if (ws_str)
2943 return t;
2944 next_nomacro_spc();
2945 return tok;
2949 /* do macro substitution of current token with macro 's' and add
2950 result to (tok_str,tok_len). 'nested_list' is the list of all
2951 macros we got inside to avoid recursing. Return non zero if no
2952 substitution needs to be done */
2953 static int macro_subst_tok(
2954 TokenString *tok_str,
2955 Sym **nested_list,
2956 Sym *s,
2957 int can_read_stream)
2959 Sym *args, *sa, *sa1;
2960 int parlevel, *mstr, t, t1, spc;
2961 TokenString str;
2962 char *cstrval;
2963 CValue cval;
2964 CString cstr;
2965 char buf[32];
2967 /* if symbol is a macro, prepare substitution */
2968 /* special macros */
2969 if (tok == TOK___LINE__) {
2970 snprintf(buf, sizeof(buf), "%d", file->line_num);
2971 cstrval = buf;
2972 t1 = TOK_PPNUM;
2973 goto add_cstr1;
2974 } else if (tok == TOK___FILE__) {
2975 cstrval = file->filename;
2976 goto add_cstr;
2977 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2978 time_t ti;
2979 struct tm *tm;
2981 time(&ti);
2982 tm = localtime(&ti);
2983 if (tok == TOK___DATE__) {
2984 snprintf(buf, sizeof(buf), "%s %2d %d",
2985 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2986 } else {
2987 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2988 tm->tm_hour, tm->tm_min, tm->tm_sec);
2990 cstrval = buf;
2991 add_cstr:
2992 t1 = TOK_STR;
2993 add_cstr1:
2994 cstr_new(&cstr);
2995 cstr_cat(&cstr, cstrval);
2996 cstr_ccat(&cstr, '\0');
2997 cval.cstr = &cstr;
2998 tok_str_add2(tok_str, t1, &cval);
2999 cstr_free(&cstr);
3000 } else {
3001 int saved_parse_flags = parse_flags;
3003 mstr = s->d;
3004 if (s->type.t == MACRO_FUNC) {
3005 /* whitespace between macro name and argument list */
3006 TokenString ws_str;
3007 tok_str_new(&ws_str);
3009 spc = 0;
3010 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3011 | PARSE_FLAG_ACCEPT_STRAYS;
3013 /* get next token from argument stream */
3014 t = next_argstream(nested_list, can_read_stream, &ws_str);
3015 if (t != '(') {
3016 /* not a macro substitution after all, restore the
3017 * macro token plus all whitespace we've read.
3018 * whitespace is intentionally not merged to preserve
3019 * newlines. */
3020 parse_flags = saved_parse_flags;
3021 tok_str_add(tok_str, tok);
3022 if (parse_flags & PARSE_FLAG_SPACES) {
3023 int i;
3024 for (i = 0; i < ws_str.len; i++)
3025 tok_str_add(tok_str, ws_str.str[i]);
3027 tok_str_free(ws_str.str);
3028 return 0;
3029 } else {
3030 tok_str_free(ws_str.str);
3032 next_nomacro(); /* eat '(' */
3034 /* argument macro */
3035 args = NULL;
3036 sa = s->next;
3037 /* NOTE: empty args are allowed, except if no args */
3038 for(;;) {
3039 do {
3040 next_argstream(nested_list, can_read_stream, NULL);
3041 } while (is_space(tok) || TOK_LINEFEED == tok);
3042 empty_arg:
3043 /* handle '()' case */
3044 if (!args && !sa && tok == ')')
3045 break;
3046 if (!sa)
3047 tcc_error("macro '%s' used with too many args",
3048 get_tok_str(s->v, 0));
3049 tok_str_new(&str);
3050 parlevel = spc = 0;
3051 /* NOTE: non zero sa->t indicates VA_ARGS */
3052 while ((parlevel > 0 ||
3053 (tok != ')' &&
3054 (tok != ',' || sa->type.t)))) {
3055 if (tok == TOK_EOF || tok == 0)
3056 break;
3057 if (tok == '(')
3058 parlevel++;
3059 else if (tok == ')')
3060 parlevel--;
3061 if (tok == TOK_LINEFEED)
3062 tok = ' ';
3063 if (!check_space(tok, &spc))
3064 tok_str_add2(&str, tok, &tokc);
3065 next_argstream(nested_list, can_read_stream, NULL);
3067 if (parlevel)
3068 expect(")");
3069 str.len -= spc;
3070 tok_str_add(&str, 0);
3071 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3072 sa1->d = str.str;
3073 sa = sa->next;
3074 if (tok == ')') {
3075 /* special case for gcc var args: add an empty
3076 var arg argument if it is omitted */
3077 if (sa && sa->type.t && gnu_ext)
3078 goto empty_arg;
3079 break;
3081 if (tok != ',')
3082 expect(",");
3084 if (sa) {
3085 tcc_error("macro '%s' used with too few args",
3086 get_tok_str(s->v, 0));
3089 parse_flags = saved_parse_flags;
3091 /* now subst each arg */
3092 mstr = macro_arg_subst(nested_list, mstr, args);
3093 /* free memory */
3094 sa = args;
3095 while (sa) {
3096 sa1 = sa->prev;
3097 tok_str_free(sa->d);
3098 sym_free(sa);
3099 sa = sa1;
3103 sym_push2(nested_list, s->v, 0, 0);
3104 parse_flags = saved_parse_flags;
3105 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3107 /* pop nested defined symbol */
3108 sa1 = *nested_list;
3109 *nested_list = sa1->prev;
3110 sym_free(sa1);
3111 if (mstr != s->d)
3112 tok_str_free(mstr);
3114 return 0;
3117 int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3119 CString cstr;
3120 int n;
3122 cstr_new(&cstr);
3123 if (t1 != TOK_PLCHLDR)
3124 cstr_cat(&cstr, get_tok_str(t1, v1));
3125 n = cstr.size;
3126 if (t2 != TOK_PLCHLDR)
3127 cstr_cat(&cstr, get_tok_str(t2, v2));
3128 cstr_ccat(&cstr, '\0');
3130 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3131 memcpy(file->buffer, cstr.data, cstr.size);
3132 for (;;) {
3133 next_nomacro1();
3134 if (0 == *file->buf_ptr)
3135 break;
3136 if (is_space(tok))
3137 continue;
3138 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3139 n, cstr.data, (char*)cstr.data + n);
3140 break;
3142 tcc_close();
3144 //printf("paste <%s>\n", (char*)cstr.data);
3145 cstr_free(&cstr);
3146 return 0;
3149 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3150 return the resulting string (which must be freed). */
3151 static inline int *macro_twosharps(const int *ptr0)
3153 int t;
3154 CValue cval;
3155 TokenString macro_str1;
3156 int start_of_nosubsts = -1;
3157 const int *ptr;
3159 /* we search the first '##' */
3160 for (ptr = ptr0;;) {
3161 TOK_GET(&t, &ptr, &cval);
3162 if (t == TOK_TWOSHARPS)
3163 break;
3164 if (t == 0)
3165 return NULL;
3168 tok_str_new(&macro_str1);
3170 //tok_print(" $$$", ptr0);
3171 for (ptr = ptr0;;) {
3172 TOK_GET(&t, &ptr, &cval);
3173 if (t == 0)
3174 break;
3175 if (t == TOK_TWOSHARPS)
3176 continue;
3177 while (*ptr == TOK_TWOSHARPS) {
3178 int t1; CValue cv1;
3179 /* given 'a##b', remove nosubsts preceding 'a' */
3180 if (start_of_nosubsts >= 0)
3181 macro_str1.len = start_of_nosubsts;
3182 /* given 'a##b', remove nosubsts preceding 'b' */
3183 while ((t1 = *++ptr) == TOK_NOSUBST)
3185 if (t1 && t1 != TOK_TWOSHARPS
3186 && t1 != ':') /* 'a##:' don't build a new token */
3188 TOK_GET(&t1, &ptr, &cv1);
3189 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3190 paste_tokens(t, &cval, t1, &cv1);
3191 t = tok, cval = tokc;
3195 if (t == TOK_NOSUBST) {
3196 if (start_of_nosubsts < 0)
3197 start_of_nosubsts = macro_str1.len;
3198 } else {
3199 start_of_nosubsts = -1;
3201 tok_str_add2(&macro_str1, t, &cval);
3203 tok_str_add(&macro_str1, 0);
3204 //tok_print(" ###", macro_str1.str);
3205 return macro_str1.str;
3208 /* do macro substitution of macro_str and add result to
3209 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3210 inside to avoid recursing. */
3211 static void macro_subst(
3212 TokenString *tok_str,
3213 Sym **nested_list,
3214 const int *macro_str,
3215 int can_read_stream
3218 Sym *s;
3219 const int *ptr;
3220 int t, spc, nosubst;
3221 CValue cval;
3222 int *macro_str1 = NULL;
3224 /* first scan for '##' operator handling */
3225 ptr = macro_str;
3226 spc = nosubst = 0;
3228 /* first scan for '##' operator handling */
3229 if (can_read_stream) {
3230 macro_str1 = macro_twosharps(ptr);
3231 if (macro_str1)
3232 ptr = macro_str1;
3235 while (1) {
3236 TOK_GET(&t, &ptr, &cval);
3237 if (t == 0)
3238 break;
3240 if (t >= TOK_IDENT && 0 == nosubst) {
3241 s = define_find(t);
3242 if (s == NULL)
3243 goto no_subst;
3245 /* if nested substitution, do nothing */
3246 if (sym_find2(*nested_list, t)) {
3247 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3248 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3249 goto no_subst;
3253 TokenString str;
3254 str.str = (int*)ptr;
3255 begin_macro(&str, 2);
3257 tok = t;
3258 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3260 if (str.alloc == 3) {
3261 /* already finished by reading function macro arguments */
3262 break;
3265 ptr = macro_ptr;
3266 end_macro ();
3269 spc = tok_str->len && is_space(tok_str->str[tok_str->len-1]);
3271 } else {
3273 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3274 tcc_error("stray '\\' in program");
3276 no_subst:
3277 if (!check_space(t, &spc))
3278 tok_str_add2(tok_str, t, &cval);
3279 nosubst = 0;
3280 if (t == TOK_NOSUBST)
3281 nosubst = 1;
3284 if (macro_str1)
3285 tok_str_free(macro_str1);
3289 /* return next token with macro substitution */
3290 ST_FUNC void next(void)
3292 redo:
3293 if (parse_flags & PARSE_FLAG_SPACES)
3294 next_nomacro_spc();
3295 else
3296 next_nomacro();
3298 if (macro_ptr) {
3299 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3300 /* discard preprocessor markers */
3301 goto redo;
3302 } else if (tok == 0) {
3303 /* end of macro or unget token string */
3304 end_macro();
3305 goto redo;
3307 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3308 Sym *s;
3309 /* if reading from file, try to substitute macros */
3310 s = define_find(tok);
3311 if (s) {
3312 static TokenString str; /* using static string for speed */
3313 Sym *nested_list = NULL;
3314 tok_str_new(&str);
3315 nested_list = NULL;
3316 macro_subst_tok(&str, &nested_list, s, 1);
3317 tok_str_add(&str, 0);
3318 begin_macro(&str, 0);
3319 goto redo;
3322 /* convert preprocessor tokens into C tokens */
3323 if (tok == TOK_PPNUM) {
3324 if (parse_flags & PARSE_FLAG_TOK_NUM)
3325 parse_number((char *)tokc.cstr->data);
3326 } else if (tok == TOK_PPSTR) {
3327 if (parse_flags & PARSE_FLAG_TOK_STR)
3328 parse_string((char *)tokc.cstr->data, tokc.cstr->size - 1);
3332 /* push back current token and set current token to 'last_tok'. Only
3333 identifier case handled for labels. */
3334 ST_INLN void unget_tok(int last_tok)
3336 TokenString *str = tcc_malloc(sizeof *str);
3337 tok_str_new(str);
3338 tok_str_add2(str, tok, &tokc);
3339 tok_str_add(str, 0);
3340 begin_macro(str, 1);
3341 tok = last_tok;
3344 /* better than nothing, but needs extension to handle '-E' option
3345 correctly too */
3346 ST_FUNC void preprocess_init(TCCState *s1)
3348 s1->include_stack_ptr = s1->include_stack;
3349 /* XXX: move that before to avoid having to initialize
3350 file->ifdef_stack_ptr ? */
3351 s1->ifdef_stack_ptr = s1->ifdef_stack;
3352 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3354 pvtop = vtop = vstack - 1;
3355 s1->pack_stack[0] = 0;
3356 s1->pack_stack_ptr = s1->pack_stack;
3358 isidnum_table['$' - CH_EOF] =
3359 tcc_state->dollars_in_identifiers ? IS_ID : 0;
3362 ST_FUNC void preprocess_new(void)
3364 int i, c;
3365 const char *p, *r;
3367 /* init isid table */
3368 for(i = CH_EOF; i<256; i++)
3369 isidnum_table[i - CH_EOF]
3370 = is_space(i) ? IS_SPC
3371 : isid(i) ? IS_ID
3372 : isnum(i) ? IS_NUM
3373 : 0;
3375 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3377 tok_ident = TOK_IDENT;
3378 p = tcc_keywords;
3379 while (*p) {
3380 r = p;
3381 for(;;) {
3382 c = *r++;
3383 if (c == '\0')
3384 break;
3386 tok_alloc(p, r - p - 1);
3387 p = r;
3391 ST_FUNC void preprocess_delete(void)
3393 int i, n;
3395 /* free -D and compiler defines */
3396 free_defines(NULL);
3398 /* cleanup from error/setjmp */
3399 while (macro_stack)
3400 end_macro();
3401 macro_ptr = NULL;
3403 /* free tokens */
3404 n = tok_ident - TOK_IDENT;
3405 for(i = 0; i < n; i++)
3406 tcc_free(table_ident[i]);
3407 tcc_free(table_ident);
3408 table_ident = NULL;
3411 /* Preprocess the current file */
3412 ST_FUNC int tcc_preprocess(TCCState *s1)
3414 BufferedFile **iptr;
3415 int token_seen, spcs, level;
3417 preprocess_init(s1);
3418 ch = file->buf_ptr[0];
3419 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3420 parse_flags = PARSE_FLAG_PREPROCESS
3421 | (parse_flags & PARSE_FLAG_ASM_FILE)
3422 | PARSE_FLAG_LINEFEED
3423 | PARSE_FLAG_SPACES
3424 | PARSE_FLAG_ACCEPT_STRAYS
3427 #ifdef PP_BENCH
3428 do next(); while (tok != TOK_EOF); return 0;
3429 #endif
3431 token_seen = spcs = 0;
3432 pp_line(s1, file, 0);
3434 for (;;) {
3435 iptr = s1->include_stack_ptr;
3436 next();
3437 if (tok == TOK_EOF)
3438 break;
3439 level = s1->include_stack_ptr - iptr;
3440 if (level) {
3441 if (level > 0)
3442 pp_line(s1, *iptr, 0);
3443 pp_line(s1, file, level);
3446 if (0 == token_seen) {
3447 if (tok == ' ') {
3448 ++spcs;
3449 continue;
3451 if (tok == TOK_LINEFEED) {
3452 spcs = 0;
3453 continue;
3455 pp_line(s1, file, 0);
3456 while (spcs)
3457 fputs(" ", s1->ppfp), --spcs;
3458 token_seen = 1;
3460 } else if (tok == TOK_LINEFEED) {
3461 ++file->line_ref;
3462 token_seen = 0;
3465 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3468 return 0;