fix tccpp.c
[tinycc.git] / tccpp.c
blob732c5eaa8061023b1ac3f2582f081c01ffcc9210
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 /* additional informations about token */
28 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
29 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
30 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
31 #define TOK_FLAG_EOF 0x0008 /* end of file */
33 ST_DATA int parse_flags;
34 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
35 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
36 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
37 token. line feed is also
38 returned at eof */
39 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
40 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
42 ST_DATA struct BufferedFile *file;
43 ST_DATA int ch, tok;
44 ST_DATA CValue tokc;
45 ST_DATA const int *macro_ptr;
46 ST_DATA CString tokcstr; /* current parsed string, if any */
48 /* display benchmark infos */
49 ST_DATA int total_lines;
50 ST_DATA int total_bytes;
51 ST_DATA int tok_ident;
52 ST_DATA TokenSym **table_ident;
54 /* ------------------------------------------------------------------------- */
56 static int *macro_ptr_allocated;
57 static const int *unget_saved_macro_ptr;
58 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
59 static int unget_buffer_enabled;
60 static TokenSym *hash_ident[TOK_HASH_SIZE];
61 /* true if isid(c) || isnum(c) */
62 static unsigned char isidnum_table[256-CH_EOF];
64 static const char tcc_keywords[] =
65 #define DEF(id, str) str "\0"
66 #include "tcctok.h"
67 #undef DEF
70 /* WARNING: the content of this string encodes token numbers */
71 static const unsigned char tok_two_chars[] =
72 /* outdated -- gr
73 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
74 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
75 */{
76 '<','=', TOK_LE,
77 '>','=', TOK_GE,
78 '!','=', TOK_NE,
79 '&','&', TOK_LAND,
80 '|','|', TOK_LOR,
81 '+','+', TOK_INC,
82 '-','-', TOK_DEC,
83 '=','=', TOK_EQ,
84 '<','<', TOK_SHL,
85 '>','>', TOK_SAR,
86 '+','=', TOK_A_ADD,
87 '-','=', TOK_A_SUB,
88 '*','=', TOK_A_MUL,
89 '/','=', TOK_A_DIV,
90 '%','=', TOK_A_MOD,
91 '&','=', TOK_A_AND,
92 '^','=', TOK_A_XOR,
93 '|','=', TOK_A_OR,
94 '-','>', TOK_ARROW,
95 '.','.', 0xa8, // C++ token ?
96 '#','#', TOK_TWOSHARPS,
100 struct macro_level {
101 struct macro_level *prev;
102 const int *p;
105 static void next_nomacro_spc(void);
106 static void macro_subst(
107 TokenString *tok_str,
108 Sym **nested_list,
109 const int *macro_str,
110 struct macro_level **can_read_stream
113 ST_FUNC void skip(int c)
115 if (tok != c)
116 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
117 next();
120 ST_FUNC void expect(const char *msg)
122 tcc_error("%s expected", msg);
125 /* ------------------------------------------------------------------------- */
126 /* CString handling */
127 static void cstr_realloc(CString *cstr, int new_size)
129 int size;
130 void *data;
132 size = cstr->size_allocated;
133 if (size == 0)
134 size = 8; /* no need to allocate a too small first string */
135 while (size < new_size)
136 size = size * 2;
137 data = tcc_realloc(cstr->data_allocated, size);
138 cstr->data_allocated = data;
139 cstr->size_allocated = size;
140 cstr->data = data;
143 /* add a byte */
144 ST_FUNC void cstr_ccat(CString *cstr, int ch)
146 int size;
147 size = cstr->size + 1;
148 if (size > cstr->size_allocated)
149 cstr_realloc(cstr, size);
150 ((unsigned char *)cstr->data)[size - 1] = ch;
151 cstr->size = size;
154 ST_FUNC void cstr_cat(CString *cstr, const char *str)
156 int c;
157 for(;;) {
158 c = *str;
159 if (c == '\0')
160 break;
161 cstr_ccat(cstr, c);
162 str++;
166 /* add a wide char */
167 ST_FUNC void cstr_wccat(CString *cstr, int ch)
169 int size;
170 size = cstr->size + sizeof(nwchar_t);
171 if (size > cstr->size_allocated)
172 cstr_realloc(cstr, size);
173 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
174 cstr->size = size;
177 ST_FUNC void cstr_new(CString *cstr)
179 memset(cstr, 0, sizeof(CString));
182 /* free string and reset it to NULL */
183 ST_FUNC void cstr_free(CString *cstr)
185 tcc_free(cstr->data_allocated);
186 cstr_new(cstr);
189 /* reset string to empty */
190 ST_FUNC void cstr_reset(CString *cstr)
192 cstr->size = 0;
195 /* XXX: unicode ? */
196 static void add_char(CString *cstr, int c)
198 if (c == '\'' || c == '\"' || c == '\\') {
199 /* XXX: could be more precise if char or string */
200 cstr_ccat(cstr, '\\');
202 if (c >= 32 && c <= 126) {
203 cstr_ccat(cstr, c);
204 } else {
205 cstr_ccat(cstr, '\\');
206 if (c == '\n') {
207 cstr_ccat(cstr, 'n');
208 } else {
209 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
210 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
211 cstr_ccat(cstr, '0' + (c & 7));
216 /* ------------------------------------------------------------------------- */
217 /* allocate a new token */
218 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
220 TokenSym *ts, **ptable;
221 int i;
223 if (tok_ident >= SYM_FIRST_ANOM)
224 tcc_error("memory full (symbols)");
226 /* expand token table if needed */
227 i = tok_ident - TOK_IDENT;
228 if ((i % TOK_ALLOC_INCR) == 0) {
229 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
230 table_ident = ptable;
233 ts = tcc_malloc(sizeof(TokenSym) + len);
234 table_ident[i] = ts;
235 ts->tok = tok_ident++;
236 ts->sym_define = NULL;
237 ts->sym_label = NULL;
238 ts->sym_struct = NULL;
239 ts->sym_identifier = NULL;
240 ts->len = len;
241 ts->hash_next = NULL;
242 memcpy(ts->str, str, len);
243 ts->str[len] = '\0';
244 *pts = ts;
245 return ts;
248 #define TOK_HASH_INIT 1
249 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
251 /* find a token and add it if not found */
252 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
254 TokenSym *ts, **pts;
255 int i;
256 unsigned int h;
258 h = TOK_HASH_INIT;
259 for(i=0;i<len;i++)
260 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
261 h &= (TOK_HASH_SIZE - 1);
263 pts = &hash_ident[h];
264 for(;;) {
265 ts = *pts;
266 if (!ts)
267 break;
268 if (ts->len == len && !memcmp(ts->str, str, len))
269 return ts;
270 pts = &(ts->hash_next);
272 return tok_alloc_new(pts, str, len);
275 /* XXX: buffer overflow */
276 /* XXX: float tokens */
277 ST_FUNC char *get_tok_str(int v, CValue *cv)
279 static char buf[STRING_MAX_SIZE + 1];
280 static CString cstr_buf;
281 CString *cstr;
282 char *p;
283 int i, len;
285 /* NOTE: to go faster, we give a fixed buffer for small strings */
286 cstr_reset(&cstr_buf);
287 cstr_buf.data = buf;
288 cstr_buf.size_allocated = sizeof(buf);
289 p = buf;
291 /* just an explanation, should never happen:
292 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
293 tcc_error("internal error: get_tok_str"); */
295 switch(v) {
296 case TOK_CINT:
297 case TOK_CUINT:
298 /* XXX: not quite exact, but only useful for testing */
299 sprintf(p, "%u", cv->ui);
300 break;
301 case TOK_CLLONG:
302 case TOK_CULLONG:
303 /* XXX: not quite exact, but only useful for testing */
304 #ifdef _WIN32
305 sprintf(p, "%u", (unsigned)cv->ull);
306 #else
307 sprintf(p, "%llu", cv->ull);
308 #endif
309 break;
310 case TOK_LCHAR:
311 cstr_ccat(&cstr_buf, 'L');
312 case TOK_CCHAR:
313 cstr_ccat(&cstr_buf, '\'');
314 add_char(&cstr_buf, cv->i);
315 cstr_ccat(&cstr_buf, '\'');
316 cstr_ccat(&cstr_buf, '\0');
317 break;
318 case TOK_PPNUM:
319 cstr = cv->cstr;
320 len = cstr->size - 1;
321 for(i=0;i<len;i++)
322 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
323 cstr_ccat(&cstr_buf, '\0');
324 break;
325 case TOK_LSTR:
326 cstr_ccat(&cstr_buf, 'L');
327 case TOK_STR:
328 cstr = cv->cstr;
329 cstr_ccat(&cstr_buf, '\"');
330 if (v == TOK_STR) {
331 len = cstr->size - 1;
332 for(i=0;i<len;i++)
333 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
334 } else {
335 len = (cstr->size / sizeof(nwchar_t)) - 1;
336 for(i=0;i<len;i++)
337 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
339 cstr_ccat(&cstr_buf, '\"');
340 cstr_ccat(&cstr_buf, '\0');
341 break;
343 case TOK_CFLOAT:
344 case TOK_CDOUBLE:
345 case TOK_CLDOUBLE:
346 case TOK_LINENUM:
347 return NULL; /* should not happen */
349 /* above tokens have value, the ones below don't */
351 case TOK_LT:
352 v = '<';
353 goto addv;
354 case TOK_GT:
355 v = '>';
356 goto addv;
357 case TOK_DOTS:
358 return strcpy(p, "...");
359 case TOK_A_SHL:
360 return strcpy(p, "<<=");
361 case TOK_A_SAR:
362 return strcpy(p, ">>=");
363 default:
364 if (v < TOK_IDENT) {
365 /* search in two bytes table */
366 const unsigned char *q = tok_two_chars;
367 while (*q) {
368 if (q[2] == v) {
369 *p++ = q[0];
370 *p++ = q[1];
371 *p = '\0';
372 return buf;
374 q += 3;
376 addv:
377 *p++ = v;
378 *p = '\0';
379 } else if (v < tok_ident) {
380 return table_ident[v - TOK_IDENT]->str;
381 } else if (v >= SYM_FIRST_ANOM) {
382 /* special name for anonymous symbol */
383 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
384 } else {
385 /* should never happen */
386 return NULL;
388 break;
390 return cstr_buf.data;
393 /* fill input buffer and peek next char */
394 static int tcc_peekc_slow(BufferedFile *bf)
396 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 = 8;
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 /* return the current character, handling end of block if necessary
425 (but not stray) */
426 ST_FUNC int handle_eob(void)
428 return tcc_peekc_slow(file);
431 /* read next char from current input file and handle end of input buffer */
432 ST_INLN void inp(void)
434 ch = *(++(file->buf_ptr));
435 /* end of buffer/file handling */
436 if (ch == CH_EOB)
437 ch = handle_eob();
440 /* handle '\[\r]\n' */
441 static int handle_stray_noerror(void)
443 while (ch == '\\') {
444 inp();
445 if (ch == '\n') {
446 file->line_num++;
447 inp();
448 } else if (ch == '\r') {
449 inp();
450 if (ch != '\n')
451 goto fail;
452 file->line_num++;
453 inp();
454 } else {
455 fail:
456 return 1;
459 return 0;
462 static void handle_stray(void)
464 if (handle_stray_noerror())
465 tcc_error("stray '\\' in program");
468 /* skip the stray and handle the \\n case. Output an error if
469 incorrect char after the stray */
470 static int handle_stray1(uint8_t *p)
472 int c;
474 if (p >= file->buf_end) {
475 file->buf_ptr = p;
476 c = handle_eob();
477 p = file->buf_ptr;
478 if (c == '\\')
479 goto parse_stray;
480 } else {
481 parse_stray:
482 file->buf_ptr = p;
483 ch = *p;
484 handle_stray();
485 p = file->buf_ptr;
486 c = *p;
488 return c;
491 /* handle just the EOB case, but not stray */
492 #define PEEKC_EOB(c, p)\
494 p++;\
495 c = *p;\
496 if (c == '\\') {\
497 file->buf_ptr = p;\
498 c = handle_eob();\
499 p = file->buf_ptr;\
503 /* handle the complicated stray case */
504 #define PEEKC(c, p)\
506 p++;\
507 c = *p;\
508 if (c == '\\') {\
509 c = handle_stray1(p);\
510 p = file->buf_ptr;\
514 /* input with '\[\r]\n' handling. Note that this function cannot
515 handle other characters after '\', so you cannot call it inside
516 strings or comments */
517 ST_FUNC void minp(void)
519 inp();
520 if (ch == '\\')
521 handle_stray();
525 /* single line C++ comments */
526 static uint8_t *parse_line_comment(uint8_t *p)
528 int c;
530 p++;
531 for(;;) {
532 c = *p;
533 redo:
534 if (c == '\n' || c == CH_EOF) {
535 break;
536 } else if (c == '\\') {
537 file->buf_ptr = p;
538 c = handle_eob();
539 p = file->buf_ptr;
540 if (c == '\\') {
541 PEEKC_EOB(c, p);
542 if (c == '\n') {
543 file->line_num++;
544 PEEKC_EOB(c, p);
545 } else if (c == '\r') {
546 PEEKC_EOB(c, p);
547 if (c == '\n') {
548 file->line_num++;
549 PEEKC_EOB(c, p);
552 } else {
553 goto redo;
555 } else {
556 p++;
559 return p;
562 /* C comments */
563 ST_FUNC uint8_t *parse_comment(uint8_t *p)
565 int c;
567 p++;
568 for(;;) {
569 /* fast skip loop */
570 for(;;) {
571 c = *p;
572 if (c == '\n' || c == '*' || c == '\\')
573 break;
574 p++;
575 c = *p;
576 if (c == '\n' || c == '*' || c == '\\')
577 break;
578 p++;
580 /* now we can handle all the cases */
581 if (c == '\n') {
582 file->line_num++;
583 p++;
584 } else if (c == '*') {
585 p++;
586 for(;;) {
587 c = *p;
588 if (c == '*') {
589 p++;
590 } else if (c == '/') {
591 goto end_of_comment;
592 } else if (c == '\\') {
593 file->buf_ptr = p;
594 c = handle_eob();
595 p = file->buf_ptr;
596 if (c == '\\') {
597 /* skip '\[\r]\n', otherwise just skip the stray */
598 while (c == '\\') {
599 PEEKC_EOB(c, p);
600 if (c == '\n') {
601 file->line_num++;
602 PEEKC_EOB(c, p);
603 } else if (c == '\r') {
604 PEEKC_EOB(c, p);
605 if (c == '\n') {
606 file->line_num++;
607 PEEKC_EOB(c, p);
609 } else {
610 goto after_star;
614 } else {
615 break;
618 after_star: ;
619 } else {
620 /* stray, eob or eof */
621 file->buf_ptr = p;
622 c = handle_eob();
623 p = file->buf_ptr;
624 if (c == CH_EOF) {
625 tcc_error("unexpected end of file in comment");
626 } else if (c == '\\') {
627 p++;
631 end_of_comment:
632 p++;
633 return p;
636 #define cinp minp
638 static inline void skip_spaces(void)
640 while (is_space(ch))
641 cinp();
644 static inline int check_space(int t, int *spc)
646 if (is_space(t)) {
647 if (*spc)
648 return 1;
649 *spc = 1;
650 } else
651 *spc = 0;
652 return 0;
655 /* parse a string without interpreting escapes */
656 static uint8_t *parse_pp_string(uint8_t *p,
657 int sep, CString *str)
659 int c;
660 p++;
661 for(;;) {
662 c = *p;
663 if (c == sep) {
664 break;
665 } else if (c == '\\') {
666 file->buf_ptr = p;
667 c = handle_eob();
668 p = file->buf_ptr;
669 if (c == CH_EOF) {
670 unterminated_string:
671 /* XXX: indicate line number of start of string */
672 tcc_error("missing terminating %c character", sep);
673 } else if (c == '\\') {
674 /* escape : just skip \[\r]\n */
675 PEEKC_EOB(c, p);
676 if (c == '\n') {
677 file->line_num++;
678 p++;
679 } else if (c == '\r') {
680 PEEKC_EOB(c, p);
681 if (c != '\n')
682 expect("'\n' after '\r'");
683 file->line_num++;
684 p++;
685 } else if (c == CH_EOF) {
686 goto unterminated_string;
687 } else {
688 if (str) {
689 cstr_ccat(str, '\\');
690 cstr_ccat(str, c);
692 p++;
695 } else if (c == '\n') {
696 file->line_num++;
697 goto add_char;
698 } else if (c == '\r') {
699 PEEKC_EOB(c, p);
700 if (c != '\n') {
701 if (str)
702 cstr_ccat(str, '\r');
703 } else {
704 file->line_num++;
705 goto add_char;
707 } else {
708 add_char:
709 if (str)
710 cstr_ccat(str, c);
711 p++;
714 p++;
715 return p;
718 /* skip block of text until #else, #elif or #endif. skip also pairs of
719 #if/#endif */
720 static void preprocess_skip(void)
722 int a, start_of_line, c, in_warn_or_error;
723 uint8_t *p;
725 p = file->buf_ptr;
726 a = 0;
727 redo_start:
728 start_of_line = 1;
729 in_warn_or_error = 0;
730 for(;;) {
731 redo_no_start:
732 c = *p;
733 switch(c) {
734 case ' ':
735 case '\t':
736 case '\f':
737 case '\v':
738 case '\r':
739 p++;
740 goto redo_no_start;
741 case '\n':
742 file->line_num++;
743 p++;
744 goto redo_start;
745 case '\\':
746 file->buf_ptr = p;
747 c = handle_eob();
748 if (c == CH_EOF) {
749 expect("#endif");
750 } else if (c == '\\') {
751 ch = file->buf_ptr[0];
752 handle_stray_noerror();
754 p = file->buf_ptr;
755 goto redo_no_start;
756 /* skip strings */
757 case '\"':
758 case '\'':
759 if (in_warn_or_error)
760 goto _default;
761 p = parse_pp_string(p, c, NULL);
762 break;
763 /* skip comments */
764 case '/':
765 if (in_warn_or_error)
766 goto _default;
767 file->buf_ptr = p;
768 ch = *p;
769 minp();
770 p = file->buf_ptr;
771 if (ch == '*') {
772 p = parse_comment(p);
773 } else if (ch == '/') {
774 p = parse_line_comment(p);
776 break;
777 case '#':
778 p++;
779 if (start_of_line) {
780 file->buf_ptr = p;
781 next_nomacro();
782 p = file->buf_ptr;
783 if (a == 0 &&
784 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
785 goto the_end;
786 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
787 a++;
788 else if (tok == TOK_ENDIF)
789 a--;
790 else if( tok == TOK_ERROR || tok == TOK_WARNING)
791 in_warn_or_error = 1;
792 else if (tok == TOK_LINEFEED)
793 goto redo_start;
795 break;
796 _default:
797 default:
798 p++;
799 break;
801 start_of_line = 0;
803 the_end: ;
804 file->buf_ptr = p;
807 /* ParseState handling */
809 /* XXX: currently, no include file info is stored. Thus, we cannot display
810 accurate messages if the function or data definition spans multiple
811 files */
813 /* save current parse state in 's' */
814 ST_FUNC void save_parse_state(ParseState *s)
816 s->line_num = file->line_num;
817 s->macro_ptr = macro_ptr;
818 s->tok = tok;
819 s->tokc = tokc;
822 /* restore parse state from 's' */
823 ST_FUNC void restore_parse_state(ParseState *s)
825 file->line_num = s->line_num;
826 macro_ptr = s->macro_ptr;
827 tok = s->tok;
828 tokc = s->tokc;
831 /* return the number of additional 'ints' necessary to store the
832 token */
833 static inline int tok_ext_size(int t)
835 switch(t) {
836 /* 4 bytes */
837 case TOK_CINT:
838 case TOK_CUINT:
839 case TOK_CCHAR:
840 case TOK_LCHAR:
841 case TOK_CFLOAT:
842 case TOK_LINENUM:
843 return 1;
844 case TOK_STR:
845 case TOK_LSTR:
846 case TOK_PPNUM:
847 tcc_error("unsupported token");
848 return 1;
849 case TOK_CDOUBLE:
850 case TOK_CLLONG:
851 case TOK_CULLONG:
852 return 2;
853 case TOK_CLDOUBLE:
854 return LDOUBLE_SIZE / 4;
855 default:
856 return 0;
860 /* token string handling */
862 ST_INLN void tok_str_new(TokenString *s)
864 s->str = NULL;
865 s->len = 0;
866 s->allocated_len = 0;
867 s->last_line_num = -1;
870 ST_FUNC void tok_str_free(int *str)
872 tcc_free(str);
875 static int *tok_str_realloc(TokenString *s)
877 int *str, len;
879 if (s->allocated_len == 0) {
880 len = 8;
881 } else {
882 len = s->allocated_len * 2;
884 str = tcc_realloc(s->str, len * sizeof(int));
885 s->allocated_len = len;
886 s->str = str;
887 return str;
890 ST_FUNC void tok_str_add(TokenString *s, int t)
892 int len, *str;
894 len = s->len;
895 str = s->str;
896 if (len >= s->allocated_len)
897 str = tok_str_realloc(s);
898 str[len++] = t;
899 s->len = len;
902 static void tok_str_add2(TokenString *s, int t, CValue *cv)
904 int len, *str;
906 len = s->len;
907 str = s->str;
909 /* allocate space for worst case */
910 if (len + TOK_MAX_SIZE > s->allocated_len)
911 str = tok_str_realloc(s);
912 str[len++] = t;
913 switch(t) {
914 case TOK_CINT:
915 case TOK_CUINT:
916 case TOK_CCHAR:
917 case TOK_LCHAR:
918 case TOK_CFLOAT:
919 case TOK_LINENUM:
920 str[len++] = cv->tab[0];
921 break;
922 case TOK_PPNUM:
923 case TOK_STR:
924 case TOK_LSTR:
926 int nb_words;
927 CString *cstr;
929 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
930 while ((len + nb_words) > s->allocated_len)
931 str = tok_str_realloc(s);
932 cstr = (CString *)(str + len);
933 cstr->data = NULL;
934 cstr->size = cv->cstr->size;
935 cstr->data_allocated = NULL;
936 cstr->size_allocated = cstr->size;
937 memcpy((char *)cstr + sizeof(CString),
938 cv->cstr->data, cstr->size);
939 len += nb_words;
941 break;
942 case TOK_CDOUBLE:
943 case TOK_CLLONG:
944 case TOK_CULLONG:
945 #if LDOUBLE_SIZE == 8
946 case TOK_CLDOUBLE:
947 #endif
948 str[len++] = cv->tab[0];
949 str[len++] = cv->tab[1];
950 break;
951 #if LDOUBLE_SIZE == 12
952 case TOK_CLDOUBLE:
953 str[len++] = cv->tab[0];
954 str[len++] = cv->tab[1];
955 str[len++] = cv->tab[2];
956 #elif LDOUBLE_SIZE == 16
957 case TOK_CLDOUBLE:
958 str[len++] = cv->tab[0];
959 str[len++] = cv->tab[1];
960 str[len++] = cv->tab[2];
961 str[len++] = cv->tab[3];
962 #elif LDOUBLE_SIZE != 8
963 #error add long double size support
964 #endif
965 break;
966 default:
967 break;
969 s->len = len;
972 /* add the current parse token in token string 's' */
973 ST_FUNC void tok_str_add_tok(TokenString *s)
975 CValue cval;
977 /* save line number info */
978 if (file->line_num != s->last_line_num) {
979 s->last_line_num = file->line_num;
980 cval.i = s->last_line_num;
981 tok_str_add2(s, TOK_LINENUM, &cval);
983 tok_str_add2(s, tok, &tokc);
986 /* get a token from an integer array and increment pointer
987 accordingly. we code it as a macro to avoid pointer aliasing. */
988 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
990 const int *p = *pp;
991 int n, *tab;
993 tab = cv->tab;
994 switch(*t = *p++) {
995 case TOK_CINT:
996 case TOK_CUINT:
997 case TOK_CCHAR:
998 case TOK_LCHAR:
999 case TOK_CFLOAT:
1000 case TOK_LINENUM:
1001 tab[0] = *p++;
1002 break;
1003 case TOK_STR:
1004 case TOK_LSTR:
1005 case TOK_PPNUM:
1006 cv->cstr = (CString *)p;
1007 cv->cstr->data = (char *)p + sizeof(CString);
1008 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1009 break;
1010 case TOK_CDOUBLE:
1011 case TOK_CLLONG:
1012 case TOK_CULLONG:
1013 n = 2;
1014 goto copy;
1015 case TOK_CLDOUBLE:
1016 #if LDOUBLE_SIZE == 16
1017 n = 4;
1018 #elif LDOUBLE_SIZE == 12
1019 n = 3;
1020 #elif LDOUBLE_SIZE == 8
1021 n = 2;
1022 #else
1023 # error add long double size support
1024 #endif
1025 copy:
1027 *tab++ = *p++;
1028 while (--n);
1029 break;
1030 default:
1031 break;
1033 *pp = p;
1036 static int macro_is_equal(const int *a, const int *b)
1038 char buf[STRING_MAX_SIZE + 1];
1039 CValue cv;
1040 int t;
1041 while (*a && *b) {
1042 TOK_GET(&t, &a, &cv);
1043 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1044 TOK_GET(&t, &b, &cv);
1045 if (strcmp(buf, get_tok_str(t, &cv)))
1046 return 0;
1048 return !(*a || *b);
1051 /* defines handling */
1052 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1054 Sym *s;
1056 s = define_find(v);
1057 if (s && !macro_is_equal(s->d, str))
1058 tcc_warning("%s redefined", get_tok_str(v, NULL));
1060 s = sym_push2(&define_stack, v, macro_type, 0);
1061 s->d = str;
1062 s->next = first_arg;
1063 table_ident[v - TOK_IDENT]->sym_define = s;
1066 /* undefined a define symbol. Its name is just set to zero */
1067 ST_FUNC void define_undef(Sym *s)
1069 int v;
1070 v = s->v;
1071 if (v >= TOK_IDENT && v < tok_ident)
1072 table_ident[v - TOK_IDENT]->sym_define = NULL;
1073 s->v = 0;
1076 ST_INLN Sym *define_find(int v)
1078 v -= TOK_IDENT;
1079 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1080 return NULL;
1081 return table_ident[v]->sym_define;
1084 /* free define stack until top reaches 'b' */
1085 ST_FUNC void free_defines(Sym *b)
1087 Sym *top, *top1;
1088 int v;
1090 top = define_stack;
1091 while (top != b) {
1092 top1 = top->prev;
1093 /* do not free args or predefined defines */
1094 if (top->d)
1095 tok_str_free(top->d);
1096 v = top->v;
1097 if (v >= TOK_IDENT && v < tok_ident)
1098 table_ident[v - TOK_IDENT]->sym_define = NULL;
1099 sym_free(top);
1100 top = top1;
1102 define_stack = b;
1105 /* label lookup */
1106 ST_FUNC Sym *label_find(int v)
1108 v -= TOK_IDENT;
1109 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1110 return NULL;
1111 return table_ident[v]->sym_label;
1114 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1116 Sym *s, **ps;
1117 s = sym_push2(ptop, v, 0, 0);
1118 s->r = flags;
1119 ps = &table_ident[v - TOK_IDENT]->sym_label;
1120 if (ptop == &global_label_stack) {
1121 /* modify the top most local identifier, so that
1122 sym_identifier will point to 's' when popped */
1123 while (*ps != NULL)
1124 ps = &(*ps)->prev_tok;
1126 s->prev_tok = *ps;
1127 *ps = s;
1128 return s;
1131 /* pop labels until element last is reached. Look if any labels are
1132 undefined. Define symbols if '&&label' was used. */
1133 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1135 Sym *s, *s1;
1136 for(s = *ptop; s != slast; s = s1) {
1137 s1 = s->prev;
1138 if (s->r == LABEL_DECLARED) {
1139 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1140 } else if (s->r == LABEL_FORWARD) {
1141 tcc_error("label '%s' used but not defined",
1142 get_tok_str(s->v, NULL));
1143 } else {
1144 if (s->c) {
1145 /* define corresponding symbol. A size of
1146 1 is put. */
1147 put_extern_sym(s, cur_text_section, s->jnext, 1);
1150 /* remove label */
1151 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1152 sym_free(s);
1154 *ptop = slast;
1157 /* eval an expression for #if/#elif */
1158 static int expr_preprocess(void)
1160 int c, t;
1161 TokenString str;
1163 tok_str_new(&str);
1164 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1165 next(); /* do macro subst */
1166 if (tok == TOK_DEFINED) {
1167 next_nomacro();
1168 t = tok;
1169 if (t == '(')
1170 next_nomacro();
1171 c = define_find(tok) != 0;
1172 if (t == '(')
1173 next_nomacro();
1174 tok = TOK_CINT;
1175 tokc.i = c;
1176 } else if (tok >= TOK_IDENT) {
1177 /* if undefined macro */
1178 tok = TOK_CINT;
1179 tokc.i = 0;
1181 tok_str_add_tok(&str);
1183 tok_str_add(&str, -1); /* simulate end of file */
1184 tok_str_add(&str, 0);
1185 /* now evaluate C constant expression */
1186 macro_ptr = str.str;
1187 next();
1188 c = expr_const();
1189 macro_ptr = NULL;
1190 tok_str_free(str.str);
1191 return c != 0;
1194 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1195 static void tok_print(int *str)
1197 int t;
1198 CValue cval;
1200 printf("<");
1201 while (1) {
1202 TOK_GET(&t, &str, &cval);
1203 if (!t)
1204 break;
1205 printf("%s", get_tok_str(t, &cval));
1207 printf(">\n");
1209 #endif
1211 /* parse after #define */
1212 ST_FUNC void parse_define(void)
1214 Sym *s, *first, **ps;
1215 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1216 TokenString str;
1218 v = tok;
1219 if (v < TOK_IDENT)
1220 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1221 /* XXX: should check if same macro (ANSI) */
1222 first = NULL;
1223 t = MACRO_OBJ;
1224 /* '(' must be just after macro definition for MACRO_FUNC */
1225 next_nomacro_spc();
1226 if (tok == '(') {
1227 next_nomacro();
1228 ps = &first;
1229 while (tok != ')') {
1230 varg = tok;
1231 next_nomacro();
1232 is_vaargs = 0;
1233 if (varg == TOK_DOTS) {
1234 varg = TOK___VA_ARGS__;
1235 is_vaargs = 1;
1236 } else if (tok == TOK_DOTS && gnu_ext) {
1237 is_vaargs = 1;
1238 next_nomacro();
1240 if (varg < TOK_IDENT)
1241 tcc_error("badly punctuated parameter list");
1242 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1243 *ps = s;
1244 ps = &s->next;
1245 if (tok != ',')
1246 break;
1247 next_nomacro();
1249 if (tok == ')')
1250 next_nomacro_spc();
1251 t = MACRO_FUNC;
1253 tok_str_new(&str);
1254 spc = 2;
1255 /* EOF testing necessary for '-D' handling */
1256 ptok = 0;
1257 macro_list_start = 1;
1258 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1259 if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1260 tcc_error("'##' invalid at start of macro");
1261 ptok = tok;
1262 /* remove spaces around ## and after '#' */
1263 if (TOK_TWOSHARPS == tok) {
1264 if (1 == spc)
1265 --str.len;
1266 spc = 2;
1267 } else if ('#' == tok) {
1268 spc = 2;
1269 } else if (check_space(tok, &spc)) {
1270 goto skip;
1272 tok_str_add2(&str, tok, &tokc);
1273 skip:
1274 next_nomacro_spc();
1275 macro_list_start = 0;
1277 if (ptok == TOK_TWOSHARPS)
1278 tcc_error("'##' invalid at end of macro");
1279 if (spc == 1)
1280 --str.len; /* remove trailing space */
1281 tok_str_add(&str, 0);
1282 #ifdef PP_DEBUG
1283 printf("define %s %d: ", get_tok_str(v, NULL), t);
1284 tok_print(str.str);
1285 #endif
1286 define_push(v, t, str.str, first);
1289 static inline int hash_cached_include(const char *filename)
1291 const unsigned char *s;
1292 unsigned int h;
1294 h = TOK_HASH_INIT;
1295 s = (unsigned char *) filename;
1296 while (*s) {
1297 h = TOK_HASH_FUNC(h, *s);
1298 s++;
1300 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1301 return h;
1304 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1306 CachedInclude *e;
1307 int i, h;
1308 h = hash_cached_include(filename);
1309 i = s1->cached_includes_hash[h];
1310 for(;;) {
1311 if (i == 0)
1312 break;
1313 e = s1->cached_includes[i - 1];
1314 if (0 == PATHCMP(e->filename, filename))
1315 return e;
1316 i = e->hash_next;
1318 return NULL;
1321 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1323 CachedInclude *e;
1324 int h;
1326 if (search_cached_include(s1, filename))
1327 return;
1328 #ifdef INC_DEBUG
1329 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1330 #endif
1331 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1332 strcpy(e->filename, filename);
1333 e->ifndef_macro = ifndef_macro;
1334 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1335 /* add in hash table */
1336 h = hash_cached_include(filename);
1337 e->hash_next = s1->cached_includes_hash[h];
1338 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1341 static void pragma_parse(TCCState *s1)
1343 int val;
1345 next();
1346 if (tok == TOK_pack) {
1348 This may be:
1349 #pragma pack(1) // set
1350 #pragma pack() // reset to default
1351 #pragma pack(push,1) // push & set
1352 #pragma pack(pop) // restore previous
1354 next();
1355 skip('(');
1356 if (tok == TOK_ASM_pop) {
1357 next();
1358 if (s1->pack_stack_ptr <= s1->pack_stack) {
1359 stk_error:
1360 tcc_error("out of pack stack");
1362 s1->pack_stack_ptr--;
1363 } else {
1364 val = 0;
1365 if (tok != ')') {
1366 if (tok == TOK_ASM_push) {
1367 next();
1368 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1369 goto stk_error;
1370 s1->pack_stack_ptr++;
1371 skip(',');
1373 if (tok != TOK_CINT) {
1374 pack_error:
1375 tcc_error("invalid pack pragma");
1377 val = tokc.i;
1378 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1379 goto pack_error;
1380 next();
1382 *s1->pack_stack_ptr = val;
1383 skip(')');
1388 /* is_bof is true if first non space token at beginning of file */
1389 ST_FUNC void preprocess(int is_bof)
1391 TCCState *s1 = tcc_state;
1392 int i, c, n, saved_parse_flags;
1393 char buf[1024], *q;
1394 Sym *s;
1396 saved_parse_flags = parse_flags;
1397 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1398 PARSE_FLAG_LINEFEED;
1399 next_nomacro();
1400 redo:
1401 switch(tok) {
1402 case TOK_DEFINE:
1403 next_nomacro();
1404 parse_define();
1405 break;
1406 case TOK_UNDEF:
1407 next_nomacro();
1408 s = define_find(tok);
1409 /* undefine symbol by putting an invalid name */
1410 if (s)
1411 define_undef(s);
1412 break;
1413 case TOK_INCLUDE:
1414 case TOK_INCLUDE_NEXT:
1415 ch = file->buf_ptr[0];
1416 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1417 skip_spaces();
1418 if (ch == '<') {
1419 c = '>';
1420 goto read_name;
1421 } else if (ch == '\"') {
1422 c = ch;
1423 read_name:
1424 inp();
1425 q = buf;
1426 while (ch != c && ch != '\n' && ch != CH_EOF) {
1427 if ((q - buf) < sizeof(buf) - 1)
1428 *q++ = ch;
1429 if (ch == '\\') {
1430 if (handle_stray_noerror() == 0)
1431 --q;
1432 } else
1433 inp();
1435 *q = '\0';
1436 minp();
1437 #if 0
1438 /* eat all spaces and comments after include */
1439 /* XXX: slightly incorrect */
1440 while (ch1 != '\n' && ch1 != CH_EOF)
1441 inp();
1442 #endif
1443 } else {
1444 /* computed #include : either we have only strings or
1445 we have anything enclosed in '<>' */
1446 next();
1447 buf[0] = '\0';
1448 if (tok == TOK_STR) {
1449 while (tok != TOK_LINEFEED) {
1450 if (tok != TOK_STR) {
1451 include_syntax:
1452 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1454 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1455 next();
1457 c = '\"';
1458 } else {
1459 int len;
1460 while (tok != TOK_LINEFEED) {
1461 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1462 next();
1464 len = strlen(buf);
1465 /* check syntax and remove '<>' */
1466 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1467 goto include_syntax;
1468 memmove(buf, buf + 1, len - 2);
1469 buf[len - 2] = '\0';
1470 c = '>';
1474 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1475 tcc_error("#include recursion too deep");
1476 /* store current file in stack, but increment stack later below */
1477 *s1->include_stack_ptr = file;
1479 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1480 for (i = -2; i < n; ++i) {
1481 char buf1[sizeof file->filename];
1482 CachedInclude *e;
1483 BufferedFile **f;
1484 const char *path;
1486 if (i == -2) {
1487 /* check absolute include path */
1488 if (!IS_ABSPATH(buf))
1489 continue;
1490 buf1[0] = 0;
1491 i = n; /* force end loop */
1493 } else if (i == -1) {
1494 /* search in current dir if "header.h" */
1495 if (c != '\"')
1496 continue;
1497 path = file->filename;
1498 pstrncpy(buf1, path, tcc_basename(path) - path);
1500 } else {
1501 /* search in all the include paths */
1502 if (i < s1->nb_include_paths)
1503 path = s1->include_paths[i];
1504 else
1505 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1506 pstrcpy(buf1, sizeof(buf1), path);
1507 pstrcat(buf1, sizeof(buf1), "/");
1510 pstrcat(buf1, sizeof(buf1), buf);
1512 if (tok == TOK_INCLUDE_NEXT)
1513 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1514 if (0 == PATHCMP((*f)->filename, buf1)) {
1515 #ifdef INC_DEBUG
1516 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1517 #endif
1518 goto include_trynext;
1521 e = search_cached_include(s1, buf1);
1522 if (e && define_find(e->ifndef_macro)) {
1523 /* no need to parse the include because the 'ifndef macro'
1524 is defined */
1525 #ifdef INC_DEBUG
1526 printf("%s: skipping cached %s\n", file->filename, buf1);
1527 #endif
1528 goto include_done;
1531 if (tcc_open(s1, buf1) < 0)
1532 include_trynext:
1533 continue;
1535 #ifdef INC_DEBUG
1536 printf("%s: including %s\n", file->prev->filename, file->filename);
1537 #endif
1538 /* update target deps */
1539 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1540 tcc_strdup(buf1));
1541 /* push current file in stack */
1542 ++s1->include_stack_ptr;
1543 /* add include file debug info */
1544 if (s1->do_debug)
1545 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1546 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1547 ch = file->buf_ptr[0];
1548 goto the_end;
1550 tcc_error("include file '%s' not found", buf);
1551 include_done:
1552 break;
1553 case TOK_IFNDEF:
1554 c = 1;
1555 goto do_ifdef;
1556 case TOK_IF:
1557 c = expr_preprocess();
1558 goto do_if;
1559 case TOK_IFDEF:
1560 c = 0;
1561 do_ifdef:
1562 next_nomacro();
1563 if (tok < TOK_IDENT)
1564 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1565 if (is_bof) {
1566 if (c) {
1567 #ifdef INC_DEBUG
1568 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1569 #endif
1570 file->ifndef_macro = tok;
1573 c = (define_find(tok) != 0) ^ c;
1574 do_if:
1575 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1576 tcc_error("memory full (ifdef)");
1577 *s1->ifdef_stack_ptr++ = c;
1578 goto test_skip;
1579 case TOK_ELSE:
1580 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1581 tcc_error("#else without matching #if");
1582 if (s1->ifdef_stack_ptr[-1] & 2)
1583 tcc_error("#else after #else");
1584 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1585 goto test_else;
1586 case TOK_ELIF:
1587 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1588 tcc_error("#elif without matching #if");
1589 c = s1->ifdef_stack_ptr[-1];
1590 if (c > 1)
1591 tcc_error("#elif after #else");
1592 /* last #if/#elif expression was true: we skip */
1593 if (c == 1)
1594 goto skip;
1595 c = expr_preprocess();
1596 s1->ifdef_stack_ptr[-1] = c;
1597 test_else:
1598 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1599 file->ifndef_macro = 0;
1600 test_skip:
1601 if (!(c & 1)) {
1602 skip:
1603 preprocess_skip();
1604 is_bof = 0;
1605 goto redo;
1607 break;
1608 case TOK_ENDIF:
1609 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1610 tcc_error("#endif without matching #if");
1611 s1->ifdef_stack_ptr--;
1612 /* '#ifndef macro' was at the start of file. Now we check if
1613 an '#endif' is exactly at the end of file */
1614 if (file->ifndef_macro &&
1615 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1616 file->ifndef_macro_saved = file->ifndef_macro;
1617 /* need to set to zero to avoid false matches if another
1618 #ifndef at middle of file */
1619 file->ifndef_macro = 0;
1620 while (tok != TOK_LINEFEED)
1621 next_nomacro();
1622 tok_flags |= TOK_FLAG_ENDIF;
1623 goto the_end;
1625 break;
1626 case TOK_LINE:
1627 next();
1628 if (tok != TOK_CINT)
1629 tcc_error("#line");
1630 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1631 next();
1632 if (tok != TOK_LINEFEED) {
1633 if (tok != TOK_STR)
1634 tcc_error("#line");
1635 pstrcpy(file->filename, sizeof(file->filename),
1636 (char *)tokc.cstr->data);
1638 break;
1639 case TOK_ERROR:
1640 case TOK_WARNING:
1641 c = tok;
1642 ch = file->buf_ptr[0];
1643 skip_spaces();
1644 q = buf;
1645 while (ch != '\n' && ch != CH_EOF) {
1646 if ((q - buf) < sizeof(buf) - 1)
1647 *q++ = ch;
1648 if (ch == '\\') {
1649 if (handle_stray_noerror() == 0)
1650 --q;
1651 } else
1652 inp();
1654 *q = '\0';
1655 if (c == TOK_ERROR)
1656 tcc_error("#error %s", buf);
1657 else
1658 tcc_warning("#warning %s", buf);
1659 break;
1660 case TOK_PRAGMA:
1661 pragma_parse(s1);
1662 break;
1663 default:
1664 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1665 /* '!' is ignored to allow C scripts. numbers are ignored
1666 to emulate cpp behaviour */
1667 } else {
1668 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1669 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1670 else {
1671 /* this is a gas line comment in an 'S' file. */
1672 file->buf_ptr = parse_line_comment(file->buf_ptr);
1673 goto the_end;
1676 break;
1678 /* ignore other preprocess commands or #! for C scripts */
1679 while (tok != TOK_LINEFEED)
1680 next_nomacro();
1681 the_end:
1682 parse_flags = saved_parse_flags;
1685 /* evaluate escape codes in a string. */
1686 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1688 int c, n;
1689 const uint8_t *p;
1691 p = buf;
1692 for(;;) {
1693 c = *p;
1694 if (c == '\0')
1695 break;
1696 if (c == '\\') {
1697 p++;
1698 /* escape */
1699 c = *p;
1700 switch(c) {
1701 case '0': case '1': case '2': case '3':
1702 case '4': case '5': case '6': case '7':
1703 /* at most three octal digits */
1704 n = c - '0';
1705 p++;
1706 c = *p;
1707 if (isoct(c)) {
1708 n = n * 8 + c - '0';
1709 p++;
1710 c = *p;
1711 if (isoct(c)) {
1712 n = n * 8 + c - '0';
1713 p++;
1716 c = n;
1717 goto add_char_nonext;
1718 case 'x':
1719 case 'u':
1720 case 'U':
1721 p++;
1722 n = 0;
1723 for(;;) {
1724 c = *p;
1725 if (c >= 'a' && c <= 'f')
1726 c = c - 'a' + 10;
1727 else if (c >= 'A' && c <= 'F')
1728 c = c - 'A' + 10;
1729 else if (isnum(c))
1730 c = c - '0';
1731 else
1732 break;
1733 n = n * 16 + c;
1734 p++;
1736 c = n;
1737 goto add_char_nonext;
1738 case 'a':
1739 c = '\a';
1740 break;
1741 case 'b':
1742 c = '\b';
1743 break;
1744 case 'f':
1745 c = '\f';
1746 break;
1747 case 'n':
1748 c = '\n';
1749 break;
1750 case 'r':
1751 c = '\r';
1752 break;
1753 case 't':
1754 c = '\t';
1755 break;
1756 case 'v':
1757 c = '\v';
1758 break;
1759 case 'e':
1760 if (!gnu_ext)
1761 goto invalid_escape;
1762 c = 27;
1763 break;
1764 case '\'':
1765 case '\"':
1766 case '\\':
1767 case '?':
1768 break;
1769 default:
1770 invalid_escape:
1771 if (c >= '!' && c <= '~')
1772 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1773 else
1774 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1775 break;
1778 p++;
1779 add_char_nonext:
1780 if (!is_long)
1781 cstr_ccat(outstr, c);
1782 else
1783 cstr_wccat(outstr, c);
1785 /* add a trailing '\0' */
1786 if (!is_long)
1787 cstr_ccat(outstr, '\0');
1788 else
1789 cstr_wccat(outstr, '\0');
1792 /* parse number in null terminated string 'p' and return it in the
1793 current token */
1794 static void parse_number(const char *p)
1796 int b, t, c;
1798 c = *p++;
1799 t = *p++;
1800 b = 10;
1801 if(c=='.'){
1802 --p;
1803 goto float_frac_parse;
1805 if(c == '0'){
1806 if (t == 'x' || t == 'X') {
1807 b = 16;
1808 c = *p++;
1809 } else if (tcc_ext && (t == 'b' || t == 'B')) {
1810 b = 2;
1811 c = *p++;
1812 }else{
1813 --p;
1815 }else
1816 --p;
1817 if(strchr(p , '.') || (b == 10 && (strchr(p,'e') || strchr(p,'E'))) ||
1818 ((b == 2 || b == 16)&& (strchr(p,'p') || strchr(p,'P')))){
1819 long double ld, sh, fb;
1820 int exp;
1821 /* NOTE: strtox should support that for hexa numbers, but
1822 non ISOC99 libcs do not support it, so we prefer to do
1823 it by hand */
1824 /* hexadecimal or binary floats */
1825 /* XXX: handle overflows */
1826 float_frac_parse:
1827 fb = 1.0L/b;
1828 sh = b;
1829 ld = 0.0;
1831 while(1){
1832 if (c == '\0')
1833 break;
1834 if (c >= 'a' && c <= 'f')
1835 t = c - 'a' + 10;
1836 else if (c >= 'A' && c <= 'F')
1837 t = c - 'A' + 10;
1838 else if(isnum(c))
1839 t = c - '0';
1840 else
1841 break;
1842 if (t >= b)
1843 tcc_error("invalid digit");
1844 ld = ld * b + t;
1845 c = *p++;
1847 if (c == '.'){
1848 c = *p++;
1849 sh = fb;
1850 while (1){
1851 if (c == '\0')
1852 break;
1853 if (c >= 'a' && c <= 'f')
1854 t = c - 'a' + 10;
1855 else if (c >= 'A' && c <= 'F')
1856 t = c - 'A' + 10;
1857 else if (isnum(c))
1858 t =c - '0';
1859 else
1860 break;
1861 if (t >= b){
1862 if(b == 10 && (c == 'e' || c == 'E' || c == 'f' || c == 'F'))
1863 break;
1864 tcc_error("invalid digit");
1866 ld += sh*t;
1867 sh*=fb;
1868 c = *p++;
1871 if ((b == 16 || b == 2) && c != 'p' && c != 'P')
1872 expect("exponent");
1873 if(((c == 'e' || c == 'E') && b == 10) ||
1874 ((c == 'p' || c == 'P') && (b == 16 || b == 2))){
1875 c = *p++;
1876 if(c == '+' || c == '-'){
1877 if (c == '-')
1878 sh = fb;
1879 c = *p++;
1880 }else
1881 sh = b;
1882 if (!isnum(c))
1883 expect("exponent digits");
1884 exp = 0;
1886 exp = exp * 10 + c - '0';
1887 c = *p++;
1888 }while(isnum(c));
1889 while (exp != 0){
1890 if (exp & 1)
1891 ld *= sh;
1892 exp >>= 1;
1893 sh *= sh;
1896 t = toup(c);
1897 if (t == 'F') {
1898 c = *p++;
1899 tok = TOK_CFLOAT;
1900 tokc.f = (float)ld;
1901 } else if (t == 'L') {
1902 c = *p++;
1903 #ifdef TCC_TARGET_PE
1904 tok = TOK_CDOUBLE;
1905 tokc.d = (double)ld;
1906 #else
1907 tok = TOK_CLDOUBLE;
1908 tokc.ld = ld;
1909 #endif
1910 } else {
1911 tok = TOK_CDOUBLE;
1912 tokc.d = (double)ld;
1914 } else {
1915 uint64_t n = 0, n1;
1916 int warn = 1;
1917 int lcount, ucount;
1918 if (b == 10 && c == '0') {
1919 b = 8;
1921 while(1){
1922 if (c == '\0')
1923 break;
1924 if (c >= 'a' && c <= 'f')
1925 t = c - 'a' + 10;
1926 else if (c >= 'A' && c <= 'F')
1927 t = c - 'A' + 10;
1928 else if(isnum(c))
1929 t = c - '0';
1930 else
1931 break;
1932 if (t >= b)
1933 tcc_error("invalid digit");
1934 n1 = n;
1935 n = n * b + t;
1936 if (n < n1 && warn){
1937 tcc_warning("integer constant overflow");
1938 warn = 0;
1940 c = *p++;
1942 /* XXX: not exactly ANSI compliant */
1943 if ((n & 0xffffffff00000000LL) != 0) {
1944 if ((n >> 63) != 0)
1945 tok = TOK_CULLONG;
1946 else
1947 tok = TOK_CLLONG;
1948 } else if (n > 0x7fffffff) {
1949 tok = TOK_CUINT;
1950 } else {
1951 tok = TOK_CINT;
1953 lcount = 0;
1954 ucount = 0;
1955 for(;;) {
1956 t = toup(c);
1957 if (t == 'L') {
1958 if (lcount >= 2)
1959 tcc_error("three 'l's in integer constant");
1960 lcount++;
1961 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
1962 if (lcount == 2) {
1963 #endif
1964 if (tok == TOK_CINT)
1965 tok = TOK_CLLONG;
1966 else if (tok == TOK_CUINT)
1967 tok = TOK_CULLONG;
1968 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
1970 #endif
1971 c = *p++;
1972 } else if (t == 'U') {
1973 if (ucount >= 1)
1974 tcc_error("two 'u's in integer constant");
1975 ucount++;
1976 if (tok == TOK_CINT)
1977 tok = TOK_CUINT;
1978 else if (tok == TOK_CLLONG)
1979 tok = TOK_CULLONG;
1980 c = *p++;
1981 } else {
1982 break;
1985 if (tok == TOK_CINT || tok == TOK_CUINT)
1986 tokc.ui = n;
1987 else
1988 tokc.ull = n;
1990 if (c)
1991 tcc_error("invalid number\n");
1995 #define PARSE2(c1, tok1, c2, tok2) \
1996 case c1: \
1997 PEEKC(c, p); \
1998 if (c == c2) { \
1999 p++; \
2000 tok = tok2; \
2001 } else { \
2002 tok = tok1; \
2004 break;
2006 /* return next token without macro substitution */
2007 static inline void next_nomacro1(void)
2009 int t, c, is_long;
2010 TokenSym *ts;
2011 uint8_t *p, *p1;
2012 unsigned int h;
2014 p = file->buf_ptr;
2015 redo_no_start:
2016 c = *p;
2017 switch(c) {
2018 case ' ':
2019 case '\t':
2020 tok = c;
2021 p++;
2022 goto keep_tok_flags;
2023 case '\f':
2024 case '\v':
2025 case '\r':
2026 p++;
2027 goto redo_no_start;
2028 case '\\':
2029 /* first look if it is in fact an end of buffer */
2030 if (p >= file->buf_end) {
2031 file->buf_ptr = p;
2032 handle_eob();
2033 p = file->buf_ptr;
2034 if (p >= file->buf_end)
2035 goto parse_eof;
2036 else
2037 goto redo_no_start;
2038 } else {
2039 file->buf_ptr = p;
2040 ch = *p;
2041 handle_stray();
2042 p = file->buf_ptr;
2043 goto redo_no_start;
2045 parse_eof:
2047 TCCState *s1 = tcc_state;
2048 if ((parse_flags & PARSE_FLAG_LINEFEED)
2049 && !(tok_flags & TOK_FLAG_EOF)) {
2050 tok_flags |= TOK_FLAG_EOF;
2051 tok = TOK_LINEFEED;
2052 goto keep_tok_flags;
2053 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2054 tok = TOK_EOF;
2055 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2056 tcc_error("missing #endif");
2057 } else if (s1->include_stack_ptr == s1->include_stack) {
2058 /* no include left : end of file. */
2059 tok = TOK_EOF;
2060 } else {
2061 tok_flags &= ~TOK_FLAG_EOF;
2062 /* pop include file */
2064 /* test if previous '#endif' was after a #ifdef at
2065 start of file */
2066 if (tok_flags & TOK_FLAG_ENDIF) {
2067 #ifdef INC_DEBUG
2068 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2069 #endif
2070 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2071 tok_flags &= ~TOK_FLAG_ENDIF;
2074 /* add end of include file debug info */
2075 if (tcc_state->do_debug) {
2076 put_stabd(N_EINCL, 0, 0);
2078 /* pop include stack */
2079 tcc_close();
2080 s1->include_stack_ptr--;
2081 p = file->buf_ptr;
2082 goto redo_no_start;
2085 break;
2087 case '\n':
2088 file->line_num++;
2089 tok_flags |= TOK_FLAG_BOL;
2090 p++;
2091 maybe_newline:
2092 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2093 goto redo_no_start;
2094 tok = TOK_LINEFEED;
2095 goto keep_tok_flags;
2097 case '#':
2098 /* XXX: simplify */
2099 PEEKC(c, p);
2100 if ((tok_flags & TOK_FLAG_BOL) &&
2101 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2102 file->buf_ptr = p;
2103 preprocess(tok_flags & TOK_FLAG_BOF);
2104 p = file->buf_ptr;
2105 goto maybe_newline;
2106 } else {
2107 if (c == '#') {
2108 p++;
2109 tok = TOK_TWOSHARPS;
2110 } else {
2111 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2112 p = parse_line_comment(p - 1);
2113 goto redo_no_start;
2114 } else {
2115 tok = '#';
2119 break;
2121 case 'a': case 'b': case 'c': case 'd':
2122 case 'e': case 'f': case 'g': case 'h':
2123 case 'i': case 'j': case 'k': case 'l':
2124 case 'm': case 'n': case 'o': case 'p':
2125 case 'q': case 'r': case 's': case 't':
2126 case 'u': case 'v': case 'w': case 'x':
2127 case 'y': case 'z':
2128 case 'A': case 'B': case 'C': case 'D':
2129 case 'E': case 'F': case 'G': case 'H':
2130 case 'I': case 'J': case 'K':
2131 case 'M': case 'N': case 'O': case 'P':
2132 case 'Q': case 'R': case 'S': case 'T':
2133 case 'U': case 'V': case 'W': case 'X':
2134 case 'Y': case 'Z':
2135 case '_':
2136 parse_ident_fast:
2137 p1 = p;
2138 h = TOK_HASH_INIT;
2139 h = TOK_HASH_FUNC(h, c);
2140 p++;
2141 for(;;) {
2142 c = *p;
2143 if (!isidnum_table[c-CH_EOF])
2144 break;
2145 h = TOK_HASH_FUNC(h, c);
2146 p++;
2148 if (c != '\\') {
2149 TokenSym **pts;
2150 int len;
2152 /* fast case : no stray found, so we have the full token
2153 and we have already hashed it */
2154 len = p - p1;
2155 h &= (TOK_HASH_SIZE - 1);
2156 pts = &hash_ident[h];
2157 for(;;) {
2158 ts = *pts;
2159 if (!ts)
2160 break;
2161 if (ts->len == len && !memcmp(ts->str, p1, len))
2162 goto token_found;
2163 pts = &(ts->hash_next);
2165 ts = tok_alloc_new(pts, (char *) p1, len);
2166 token_found: ;
2167 } else {
2168 /* slower case */
2169 cstr_reset(&tokcstr);
2171 while (p1 < p) {
2172 cstr_ccat(&tokcstr, *p1);
2173 p1++;
2175 p--;
2176 PEEKC(c, p);
2177 parse_ident_slow:
2178 while (isidnum_table[c-CH_EOF]) {
2179 cstr_ccat(&tokcstr, c);
2180 PEEKC(c, p);
2182 ts = tok_alloc(tokcstr.data, tokcstr.size);
2184 tok = ts->tok;
2185 break;
2186 case 'L':
2187 t = p[1];
2188 if (t != '\\' && t != '\'' && t != '\"') {
2189 /* fast case */
2190 goto parse_ident_fast;
2191 } else {
2192 PEEKC(c, p);
2193 if (c == '\'' || c == '\"') {
2194 is_long = 1;
2195 goto str_const;
2196 } else {
2197 cstr_reset(&tokcstr);
2198 cstr_ccat(&tokcstr, 'L');
2199 goto parse_ident_slow;
2202 break;
2203 case '0': case '1': case '2': case '3':
2204 case '4': case '5': case '6': case '7':
2205 case '8': case '9':
2207 cstr_reset(&tokcstr);
2208 /* after the first digit, accept digits, alpha, '.' or sign if
2209 prefixed by 'eEpP' */
2210 parse_num:
2211 for(;;) {
2212 t = c;
2213 cstr_ccat(&tokcstr, c);
2214 PEEKC(c, p);
2215 if (!(isnum(c) || isid(c) || c == '.' ||
2216 ((c == '+' || c == '-') &&
2217 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2218 break;
2220 /* We add a trailing '\0' to ease parsing */
2221 cstr_ccat(&tokcstr, '\0');
2222 tokc.cstr = &tokcstr;
2223 tok = TOK_PPNUM;
2224 break;
2225 case '.':
2226 /* special dot handling because it can also start a number */
2227 PEEKC(c, p);
2228 if (isnum(c)) {
2229 cstr_reset(&tokcstr);
2230 cstr_ccat(&tokcstr, '.');
2231 goto parse_num;
2232 } else if (c == '.') {
2233 PEEKC(c, p);
2234 if (c != '.')
2235 expect("'.'");
2236 PEEKC(c, p);
2237 tok = TOK_DOTS;
2238 } else {
2239 tok = '.';
2241 break;
2242 case '\'':
2243 case '\"':
2244 is_long = 0;
2245 str_const:
2247 CString str;
2248 int sep;
2250 sep = c;
2252 /* parse the string */
2253 cstr_new(&str);
2254 p = parse_pp_string(p, sep, &str);
2255 cstr_ccat(&str, '\0');
2257 /* eval the escape (should be done as TOK_PPNUM) */
2258 cstr_reset(&tokcstr);
2259 parse_escape_string(&tokcstr, str.data, is_long);
2260 cstr_free(&str);
2262 if (sep == '\'') {
2263 int char_size;
2264 /* XXX: make it portable */
2265 if (!is_long)
2266 char_size = 1;
2267 else
2268 char_size = sizeof(nwchar_t);
2269 if (tokcstr.size <= char_size)
2270 tcc_error("empty character constant");
2271 if (tokcstr.size > 2 * char_size)
2272 tcc_warning("multi-character character constant");
2273 if (!is_long) {
2274 tokc.i = *(int8_t *)tokcstr.data;
2275 tok = TOK_CCHAR;
2276 } else {
2277 tokc.i = *(nwchar_t *)tokcstr.data;
2278 tok = TOK_LCHAR;
2280 } else {
2281 tokc.cstr = &tokcstr;
2282 if (!is_long)
2283 tok = TOK_STR;
2284 else
2285 tok = TOK_LSTR;
2288 break;
2290 case '<':
2291 PEEKC(c, p);
2292 if (c == '=') {
2293 p++;
2294 tok = TOK_LE;
2295 } else if (c == '<') {
2296 PEEKC(c, p);
2297 if (c == '=') {
2298 p++;
2299 tok = TOK_A_SHL;
2300 } else {
2301 tok = TOK_SHL;
2303 } else {
2304 tok = TOK_LT;
2306 break;
2308 case '>':
2309 PEEKC(c, p);
2310 if (c == '=') {
2311 p++;
2312 tok = TOK_GE;
2313 } else if (c == '>') {
2314 PEEKC(c, p);
2315 if (c == '=') {
2316 p++;
2317 tok = TOK_A_SAR;
2318 } else {
2319 tok = TOK_SAR;
2321 } else {
2322 tok = TOK_GT;
2324 break;
2326 case '&':
2327 PEEKC(c, p);
2328 if (c == '&') {
2329 p++;
2330 tok = TOK_LAND;
2331 } else if (c == '=') {
2332 p++;
2333 tok = TOK_A_AND;
2334 } else {
2335 tok = '&';
2337 break;
2339 case '|':
2340 PEEKC(c, p);
2341 if (c == '|') {
2342 p++;
2343 tok = TOK_LOR;
2344 } else if (c == '=') {
2345 p++;
2346 tok = TOK_A_OR;
2347 } else {
2348 tok = '|';
2350 break;
2352 case '+':
2353 PEEKC(c, p);
2354 if (c == '+') {
2355 p++;
2356 tok = TOK_INC;
2357 } else if (c == '=') {
2358 p++;
2359 tok = TOK_A_ADD;
2360 } else {
2361 tok = '+';
2363 break;
2365 case '-':
2366 PEEKC(c, p);
2367 if (c == '-') {
2368 p++;
2369 tok = TOK_DEC;
2370 } else if (c == '=') {
2371 p++;
2372 tok = TOK_A_SUB;
2373 } else if (c == '>') {
2374 p++;
2375 tok = TOK_ARROW;
2376 } else {
2377 tok = '-';
2379 break;
2381 PARSE2('!', '!', '=', TOK_NE)
2382 PARSE2('=', '=', '=', TOK_EQ)
2383 PARSE2('*', '*', '=', TOK_A_MUL)
2384 PARSE2('%', '%', '=', TOK_A_MOD)
2385 PARSE2('^', '^', '=', TOK_A_XOR)
2387 /* comments or operator */
2388 case '/':
2389 PEEKC(c, p);
2390 if (c == '*') {
2391 p = parse_comment(p);
2392 /* comments replaced by a blank */
2393 tok = ' ';
2394 goto keep_tok_flags;
2395 } else if (c == '/') {
2396 p = parse_line_comment(p);
2397 tok = ' ';
2398 goto keep_tok_flags;
2399 } else if (c == '=') {
2400 p++;
2401 tok = TOK_A_DIV;
2402 } else {
2403 tok = '/';
2405 break;
2407 /* simple tokens */
2408 case '(':
2409 case ')':
2410 case '[':
2411 case ']':
2412 case '{':
2413 case '}':
2414 case ',':
2415 case ';':
2416 case ':':
2417 case '?':
2418 case '~':
2419 case '$': /* only used in assembler */
2420 case '@': /* dito */
2421 tok = c;
2422 p++;
2423 break;
2424 default:
2425 tcc_error("unrecognized character \\x%02x", c);
2426 break;
2428 tok_flags = 0;
2429 keep_tok_flags:
2430 file->buf_ptr = p;
2431 #if defined(PARSE_DEBUG)
2432 printf("token = %s\n", get_tok_str(tok, &tokc));
2433 #endif
2436 /* return next token without macro substitution. Can read input from
2437 macro_ptr buffer */
2438 static void next_nomacro_spc(void)
2440 if (macro_ptr) {
2441 redo:
2442 tok = *macro_ptr;
2443 if (tok) {
2444 TOK_GET(&tok, &macro_ptr, &tokc);
2445 if (tok == TOK_LINENUM) {
2446 file->line_num = tokc.i;
2447 goto redo;
2450 } else {
2451 next_nomacro1();
2455 ST_FUNC void next_nomacro(void)
2457 do {
2458 next_nomacro_spc();
2459 } while (is_space(tok));
2462 /* substitute arguments in replacement lists in macro_str by the values in
2463 args (field d) and return allocated string */
2464 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2466 int last_tok, t, spc;
2467 const int *st;
2468 Sym *s;
2469 CValue cval;
2470 TokenString str;
2471 CString cstr;
2473 tok_str_new(&str);
2474 last_tok = 0;
2475 while(1) {
2476 TOK_GET(&t, &macro_str, &cval);
2477 if (!t)
2478 break;
2479 if (t == '#') {
2480 /* stringize */
2481 TOK_GET(&t, &macro_str, &cval);
2482 if (!t)
2483 break;
2484 s = sym_find2(args, t);
2485 if (s) {
2486 cstr_new(&cstr);
2487 st = s->d;
2488 spc = 0;
2489 while (*st) {
2490 TOK_GET(&t, &st, &cval);
2491 if (!check_space(t, &spc))
2492 cstr_cat(&cstr, get_tok_str(t, &cval));
2494 cstr.size -= spc;
2495 cstr_ccat(&cstr, '\0');
2496 #ifdef PP_DEBUG
2497 printf("stringize: %s\n", (char *)cstr.data);
2498 #endif
2499 /* add string */
2500 cval.cstr = &cstr;
2501 tok_str_add2(&str, TOK_STR, &cval);
2502 cstr_free(&cstr);
2503 } else {
2504 tok_str_add2(&str, t, &cval);
2506 } else if (t >= TOK_IDENT) {
2507 s = sym_find2(args, t);
2508 if (s) {
2509 st = s->d;
2510 /* if '##' is present before or after, no arg substitution */
2511 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2512 /* special case for var arg macros : ## eats the
2513 ',' if empty VA_ARGS variable. */
2514 /* XXX: test of the ',' is not 100%
2515 reliable. should fix it to avoid security
2516 problems */
2517 if (gnu_ext && s->type.t &&
2518 last_tok == TOK_TWOSHARPS &&
2519 str.len >= 2 && str.str[str.len - 2] == ',') {
2520 if (*st == TOK_PLCHLDR) {
2521 /* suppress ',' '##' */
2522 str.len -= 2;
2523 } else {
2524 /* suppress '##' and add variable */
2525 str.len--;
2526 goto add_var;
2528 } else {
2529 int t1;
2530 add_var:
2531 for(;;) {
2532 TOK_GET(&t1, &st, &cval);
2533 if (!t1)
2534 break;
2535 tok_str_add2(&str, t1, &cval);
2538 } else {
2539 /* NOTE: the stream cannot be read when macro
2540 substituing an argument */
2541 macro_subst(&str, nested_list, st, NULL);
2543 } else {
2544 tok_str_add(&str, t);
2546 } else {
2547 tok_str_add2(&str, t, &cval);
2549 last_tok = t;
2551 tok_str_add(&str, 0);
2552 return str.str;
2555 static char const ab_month_name[12][4] =
2557 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2558 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2561 /* do macro substitution of current token with macro 's' and add
2562 result to (tok_str,tok_len). 'nested_list' is the list of all
2563 macros we got inside to avoid recursing. Return non zero if no
2564 substitution needs to be done */
2565 static int macro_subst_tok(TokenString *tok_str,
2566 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2568 Sym *args, *sa, *sa1;
2569 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2570 const int *p;
2571 TokenString str;
2572 char *cstrval;
2573 CValue cval;
2574 CString cstr;
2575 char buf[32];
2577 /* if symbol is a macro, prepare substitution */
2578 /* special macros */
2579 if (tok == TOK___LINE__) {
2580 snprintf(buf, sizeof(buf), "%d", file->line_num);
2581 cstrval = buf;
2582 t1 = TOK_PPNUM;
2583 goto add_cstr1;
2584 } else if (tok == TOK___FILE__) {
2585 cstrval = file->filename;
2586 goto add_cstr;
2587 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2588 time_t ti;
2589 struct tm *tm;
2591 time(&ti);
2592 tm = localtime(&ti);
2593 if (tok == TOK___DATE__) {
2594 snprintf(buf, sizeof(buf), "%s %2d %d",
2595 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2596 } else {
2597 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2598 tm->tm_hour, tm->tm_min, tm->tm_sec);
2600 cstrval = buf;
2601 add_cstr:
2602 t1 = TOK_STR;
2603 add_cstr1:
2604 cstr_new(&cstr);
2605 cstr_cat(&cstr, cstrval);
2606 cstr_ccat(&cstr, '\0');
2607 cval.cstr = &cstr;
2608 tok_str_add2(tok_str, t1, &cval);
2609 cstr_free(&cstr);
2610 } else {
2611 mstr = s->d;
2612 mstr_allocated = 0;
2613 if (s->type.t == MACRO_FUNC) {
2614 /* NOTE: we do not use next_nomacro to avoid eating the
2615 next token. XXX: find better solution */
2616 redo:
2617 if (macro_ptr) {
2618 p = macro_ptr;
2619 while (is_space(t = *p) || TOK_LINEFEED == t)
2620 ++p;
2621 if (t == 0 && can_read_stream) {
2622 /* end of macro stream: we must look at the token
2623 after in the file */
2624 struct macro_level *ml = *can_read_stream;
2625 macro_ptr = NULL;
2626 if (ml)
2628 macro_ptr = ml->p;
2629 ml->p = NULL;
2630 *can_read_stream = ml -> prev;
2632 /* also, end of scope for nested defined symbol */
2633 (*nested_list)->v = -1;
2634 goto redo;
2636 } else {
2637 ch = file->buf_ptr[0];
2638 while (is_space(ch) || ch == '\n' || ch == '/')
2640 if (ch == '/')
2642 int c;
2643 uint8_t *p = file->buf_ptr;
2644 PEEKC(c, p);
2645 if (c == '*') {
2646 p = parse_comment(p);
2647 file->buf_ptr = p - 1;
2648 } else if (c == '/') {
2649 p = parse_line_comment(p);
2650 file->buf_ptr = p - 1;
2651 } else
2652 break;
2654 cinp();
2656 t = ch;
2658 if (t != '(') /* no macro subst */
2659 return -1;
2661 /* argument macro */
2662 next_nomacro();
2663 next_nomacro();
2664 args = NULL;
2665 sa = s->next;
2666 /* NOTE: empty args are allowed, except if no args */
2667 for(;;) {
2668 /* handle '()' case */
2669 if (!args && !sa && tok == ')')
2670 break;
2671 if (!sa)
2672 tcc_error("macro '%s' used with too many args",
2673 get_tok_str(s->v, 0));
2674 tok_str_new(&str);
2675 parlevel = spc = 0;
2676 /* NOTE: non zero sa->t indicates VA_ARGS */
2677 while ((parlevel > 0 ||
2678 (tok != ')' &&
2679 (tok != ',' || sa->type.t))) &&
2680 tok != -1) {
2681 if (tok == '(')
2682 parlevel++;
2683 else if (tok == ')')
2684 parlevel--;
2685 if (tok == TOK_LINEFEED)
2686 tok = ' ';
2687 if (!check_space(tok, &spc))
2688 tok_str_add2(&str, tok, &tokc);
2689 next_nomacro_spc();
2691 if (!str.len)
2692 tok_str_add(&str, TOK_PLCHLDR);
2693 str.len -= spc;
2694 tok_str_add(&str, 0);
2695 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2696 sa1->d = str.str;
2697 sa = sa->next;
2698 if (tok == ')') {
2699 /* special case for gcc var args: add an empty
2700 var arg argument if it is omitted */
2701 if (sa && sa->type.t && gnu_ext)
2702 continue;
2703 else
2704 break;
2706 if (tok != ',')
2707 expect(",");
2708 next_nomacro();
2710 if (sa) {
2711 tcc_error("macro '%s' used with too few args",
2712 get_tok_str(s->v, 0));
2715 /* now subst each arg */
2716 mstr = macro_arg_subst(nested_list, mstr, args);
2717 /* free memory */
2718 sa = args;
2719 while (sa) {
2720 sa1 = sa->prev;
2721 tok_str_free(sa->d);
2722 sym_free(sa);
2723 sa = sa1;
2725 mstr_allocated = 1;
2727 sym_push2(nested_list, s->v, 0, 0);
2728 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2729 /* pop nested defined symbol */
2730 sa1 = *nested_list;
2731 *nested_list = sa1->prev;
2732 sym_free(sa1);
2733 if (mstr_allocated)
2734 tok_str_free(mstr);
2736 return 0;
2739 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2740 return the resulting string (which must be freed). */
2741 static inline int *macro_twosharps(const int *macro_str)
2743 const int *ptr;
2744 int t;
2745 TokenString macro_str1;
2746 CString cstr;
2747 int n, start_of_nosubsts;
2749 /* we search the first '##' */
2750 for(ptr = macro_str;;) {
2751 CValue cval;
2752 TOK_GET(&t, &ptr, &cval);
2753 if (t == TOK_TWOSHARPS)
2754 break;
2755 /* nothing more to do if end of string */
2756 if (t == 0)
2757 return NULL;
2760 /* we saw '##', so we need more processing to handle it */
2761 start_of_nosubsts = -1;
2762 tok_str_new(&macro_str1);
2763 for(ptr = macro_str;;) {
2764 TOK_GET(&tok, &ptr, &tokc);
2765 if (tok == 0)
2766 break;
2767 if (tok == TOK_TWOSHARPS)
2768 continue;
2769 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2770 start_of_nosubsts = macro_str1.len;
2771 while (*ptr == TOK_TWOSHARPS) {
2772 /* given 'a##b', remove nosubsts preceding 'a' */
2773 if (start_of_nosubsts >= 0)
2774 macro_str1.len = start_of_nosubsts;
2775 /* given 'a##b', skip '##' */
2776 t = *++ptr;
2777 /* given 'a##b', remove nosubsts preceding 'b' */
2778 while (t == TOK_NOSUBST)
2779 t = *++ptr;
2780 if (t && t != TOK_TWOSHARPS) {
2781 CValue cval;
2782 TOK_GET(&t, &ptr, &cval);
2783 /* We concatenate the two tokens */
2784 cstr_new(&cstr);
2785 if (tok != TOK_PLCHLDR)
2786 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2787 n = cstr.size;
2788 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
2789 cstr_cat(&cstr, get_tok_str(t, &cval));
2790 cstr_ccat(&cstr, '\0');
2792 tcc_open_bf(tcc_state, ":paste:", cstr.size);
2793 memcpy(file->buffer, cstr.data, cstr.size);
2794 for (;;) {
2795 next_nomacro1();
2796 if (0 == *file->buf_ptr)
2797 break;
2798 tok_str_add2(&macro_str1, tok, &tokc);
2799 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2800 n, cstr.data, (char*)cstr.data + n);
2802 tcc_close();
2803 cstr_free(&cstr);
2806 if (tok != TOK_NOSUBST) {
2807 tok_str_add2(&macro_str1, tok, &tokc);
2808 tok = ' ';
2809 start_of_nosubsts = -1;
2811 tok_str_add2(&macro_str1, tok, &tokc);
2813 tok_str_add(&macro_str1, 0);
2814 return macro_str1.str;
2818 /* do macro substitution of macro_str and add result to
2819 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2820 inside to avoid recursing. */
2821 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2822 const int *macro_str, struct macro_level ** can_read_stream)
2824 Sym *s;
2825 int *macro_str1;
2826 const int *ptr;
2827 int t, ret, spc;
2828 CValue cval;
2829 struct macro_level ml;
2830 int force_blank;
2832 /* first scan for '##' operator handling */
2833 ptr = macro_str;
2834 macro_str1 = macro_twosharps(ptr);
2836 if (macro_str1)
2837 ptr = macro_str1;
2838 spc = 0;
2839 force_blank = 0;
2841 while (1) {
2842 /* NOTE: ptr == NULL can only happen if tokens are read from
2843 file stream due to a macro function call */
2844 if (ptr == NULL)
2845 break;
2846 TOK_GET(&t, &ptr, &cval);
2847 if (t == 0)
2848 break;
2849 if (t == TOK_NOSUBST) {
2850 /* following token has already been subst'd. just copy it on */
2851 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
2852 TOK_GET(&t, &ptr, &cval);
2853 goto no_subst;
2855 s = define_find(t);
2856 if (s != NULL) {
2857 /* if nested substitution, do nothing */
2858 if (sym_find2(*nested_list, t)) {
2859 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
2860 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
2861 goto no_subst;
2863 ml.p = macro_ptr;
2864 if (can_read_stream)
2865 ml.prev = *can_read_stream, *can_read_stream = &ml;
2866 macro_ptr = (int *)ptr;
2867 tok = t;
2868 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
2869 ptr = (int *)macro_ptr;
2870 macro_ptr = ml.p;
2871 if (can_read_stream && *can_read_stream == &ml)
2872 *can_read_stream = ml.prev;
2873 if (ret != 0)
2874 goto no_subst;
2875 if (parse_flags & PARSE_FLAG_SPACES)
2876 force_blank = 1;
2877 } else {
2878 no_subst:
2879 if (force_blank) {
2880 tok_str_add(tok_str, ' ');
2881 spc = 1;
2882 force_blank = 0;
2884 if (!check_space(t, &spc))
2885 tok_str_add2(tok_str, t, &cval);
2888 if (macro_str1)
2889 tok_str_free(macro_str1);
2892 /* return next token with macro substitution */
2893 ST_FUNC void next(void)
2895 Sym *nested_list, *s;
2896 TokenString str;
2897 struct macro_level *ml;
2899 redo:
2900 if (parse_flags & PARSE_FLAG_SPACES)
2901 next_nomacro_spc();
2902 else
2903 next_nomacro();
2904 if (!macro_ptr) {
2905 /* if not reading from macro substituted string, then try
2906 to substitute macros */
2907 if (tok >= TOK_IDENT &&
2908 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2909 s = define_find(tok);
2910 if (s) {
2911 /* we have a macro: we try to substitute */
2912 tok_str_new(&str);
2913 nested_list = NULL;
2914 ml = NULL;
2915 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
2916 /* substitution done, NOTE: maybe empty */
2917 tok_str_add(&str, 0);
2918 macro_ptr = str.str;
2919 macro_ptr_allocated = str.str;
2920 goto redo;
2924 } else {
2925 if (tok == 0) {
2926 /* end of macro or end of unget buffer */
2927 if (unget_buffer_enabled) {
2928 macro_ptr = unget_saved_macro_ptr;
2929 unget_buffer_enabled = 0;
2930 } else {
2931 /* end of macro string: free it */
2932 tok_str_free(macro_ptr_allocated);
2933 macro_ptr_allocated = NULL;
2934 macro_ptr = NULL;
2936 goto redo;
2937 } else if (tok == TOK_NOSUBST) {
2938 /* discard preprocessor's nosubst markers */
2939 goto redo;
2943 /* convert preprocessor tokens into C tokens */
2944 if (tok == TOK_PPNUM &&
2945 (parse_flags & PARSE_FLAG_TOK_NUM)) {
2946 parse_number((char *)tokc.cstr->data);
2950 /* push back current token and set current token to 'last_tok'. Only
2951 identifier case handled for labels. */
2952 ST_INLN void unget_tok(int last_tok)
2954 int i, n;
2955 int *q;
2956 if (unget_buffer_enabled)
2958 /* assert(macro_ptr == unget_saved_buffer + 1);
2959 assert(*macro_ptr == 0); */
2961 else
2963 unget_saved_macro_ptr = macro_ptr;
2964 unget_buffer_enabled = 1;
2966 q = unget_saved_buffer;
2967 macro_ptr = q;
2968 *q++ = tok;
2969 n = tok_ext_size(tok) - 1;
2970 for(i=0;i<n;i++)
2971 *q++ = tokc.tab[i];
2972 *q = 0; /* end of token string */
2973 tok = last_tok;
2977 /* better than nothing, but needs extension to handle '-E' option
2978 correctly too */
2979 ST_FUNC void preprocess_init(TCCState *s1)
2981 s1->include_stack_ptr = s1->include_stack;
2982 /* XXX: move that before to avoid having to initialize
2983 file->ifdef_stack_ptr ? */
2984 s1->ifdef_stack_ptr = s1->ifdef_stack;
2985 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2987 vtop = vstack - 1;
2988 s1->pack_stack[0] = 0;
2989 s1->pack_stack_ptr = s1->pack_stack;
2992 ST_FUNC void preprocess_new(void)
2994 int i, c;
2995 const char *p, *r;
2997 /* init isid table */
2998 for(i=CH_EOF;i<256;i++)
2999 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3001 /* add all tokens */
3002 table_ident = NULL;
3003 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3005 tok_ident = TOK_IDENT;
3006 p = tcc_keywords;
3007 while (*p) {
3008 r = p;
3009 for(;;) {
3010 c = *r++;
3011 if (c == '\0')
3012 break;
3014 tok_alloc(p, r - p - 1);
3015 p = r;
3019 /* Preprocess the current file */
3020 ST_FUNC int tcc_preprocess(TCCState *s1)
3022 Sym *define_start;
3024 BufferedFile *file_ref, **iptr, **iptr_new;
3025 int token_seen, line_ref, d;
3026 const char *s;
3028 preprocess_init(s1);
3029 define_start = define_stack;
3030 ch = file->buf_ptr[0];
3031 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3032 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3033 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3034 token_seen = 0;
3035 line_ref = 0;
3036 file_ref = NULL;
3037 iptr = s1->include_stack_ptr;
3039 for (;;) {
3040 next();
3041 if (tok == TOK_EOF) {
3042 break;
3043 } else if (file != file_ref) {
3044 goto print_line;
3045 } else if (tok == TOK_LINEFEED) {
3046 if (!token_seen)
3047 continue;
3048 ++line_ref;
3049 token_seen = 0;
3050 } else if (!token_seen) {
3051 d = file->line_num - line_ref;
3052 if (file != file_ref || d < 0 || d >= 8) {
3053 print_line:
3054 iptr_new = s1->include_stack_ptr;
3055 s = iptr_new > iptr ? " 1"
3056 : iptr_new < iptr ? " 2"
3057 : iptr_new > s1->include_stack ? " 3"
3058 : ""
3060 iptr = iptr_new;
3061 fprintf(s1->ppfp, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
3062 } else {
3063 while (d)
3064 fputs("\n", s1->ppfp), --d;
3066 line_ref = (file_ref = file)->line_num;
3067 token_seen = tok != TOK_LINEFEED;
3068 if (!token_seen)
3069 continue;
3071 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3073 free_defines(define_start);
3074 return 0;