win64: add tiny unwind data for setjmp/longjmp
[tinycc/kirr.git] / tccpp.c
blob162d0818b87817124b231b65d571baa052bb8759
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 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 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 static char token_buf[STRING_MAX_SIZE + 1];
62 /* true if isid(c) || isnum(c) */
63 static unsigned char isidnum_table[256-CH_EOF];
65 static const char tcc_keywords[] =
66 #define DEF(id, str) str "\0"
67 #include "tcctok.h"
68 #undef DEF
71 /* WARNING: the content of this string encodes token numbers */
72 static const unsigned char tok_two_chars[] =
73 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
74 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
76 struct macro_level {
77 struct macro_level *prev;
78 int *p;
81 ST_FUNC void next_nomacro(void);
82 static void next_nomacro_spc(void);
83 static void macro_subst(
84 TokenString *tok_str,
85 Sym **nested_list,
86 const int *macro_str,
87 struct macro_level **can_read_stream
90 ST_FUNC void skip(int c)
92 if (tok != c)
93 error("'%c' expected", c);
94 next();
97 /* ------------------------------------------------------------------------- */
98 /* CString handling */
99 static void cstr_realloc(CString *cstr, int new_size)
101 int size;
102 void *data;
104 size = cstr->size_allocated;
105 if (size == 0)
106 size = 8; /* no need to allocate a too small first string */
107 while (size < new_size)
108 size = size * 2;
109 data = tcc_realloc(cstr->data_allocated, size);
110 if (!data)
111 error("memory full");
112 cstr->data_allocated = data;
113 cstr->size_allocated = size;
114 cstr->data = data;
117 /* add a byte */
118 ST_INLN void cstr_ccat(CString *cstr, int ch)
120 int size;
121 size = cstr->size + 1;
122 if (size > cstr->size_allocated)
123 cstr_realloc(cstr, size);
124 ((unsigned char *)cstr->data)[size - 1] = ch;
125 cstr->size = size;
128 ST_FUNC void cstr_cat(CString *cstr, const char *str)
130 int c;
131 for(;;) {
132 c = *str;
133 if (c == '\0')
134 break;
135 cstr_ccat(cstr, c);
136 str++;
140 /* add a wide char */
141 ST_FUNC void cstr_wccat(CString *cstr, int ch)
143 int size;
144 size = cstr->size + sizeof(nwchar_t);
145 if (size > cstr->size_allocated)
146 cstr_realloc(cstr, size);
147 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
148 cstr->size = size;
151 ST_FUNC void cstr_new(CString *cstr)
153 memset(cstr, 0, sizeof(CString));
156 /* free string and reset it to NULL */
157 ST_FUNC void cstr_free(CString *cstr)
159 tcc_free(cstr->data_allocated);
160 cstr_new(cstr);
163 /* XXX: unicode ? */
164 ST_FUNC void add_char(CString *cstr, int c)
166 if (c == '\'' || c == '\"' || c == '\\') {
167 /* XXX: could be more precise if char or string */
168 cstr_ccat(cstr, '\\');
170 if (c >= 32 && c <= 126) {
171 cstr_ccat(cstr, c);
172 } else {
173 cstr_ccat(cstr, '\\');
174 if (c == '\n') {
175 cstr_ccat(cstr, 'n');
176 } else {
177 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
178 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
179 cstr_ccat(cstr, '0' + (c & 7));
184 /* ------------------------------------------------------------------------- */
185 /* allocate a new token */
186 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
188 TokenSym *ts, **ptable;
189 int i;
191 if (tok_ident >= SYM_FIRST_ANOM)
192 error("memory full");
194 /* expand token table if needed */
195 i = tok_ident - TOK_IDENT;
196 if ((i % TOK_ALLOC_INCR) == 0) {
197 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
198 if (!ptable)
199 error("memory full");
200 table_ident = ptable;
203 ts = tcc_malloc(sizeof(TokenSym) + len);
204 table_ident[i] = ts;
205 ts->tok = tok_ident++;
206 ts->sym_define = NULL;
207 ts->sym_label = NULL;
208 ts->sym_struct = NULL;
209 ts->sym_identifier = NULL;
210 ts->len = len;
211 ts->hash_next = NULL;
212 memcpy(ts->str, str, len);
213 ts->str[len] = '\0';
214 *pts = ts;
215 return ts;
218 #define TOK_HASH_INIT 1
219 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
221 /* find a token and add it if not found */
222 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
224 TokenSym *ts, **pts;
225 int i;
226 unsigned int h;
228 h = TOK_HASH_INIT;
229 for(i=0;i<len;i++)
230 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
231 h &= (TOK_HASH_SIZE - 1);
233 pts = &hash_ident[h];
234 for(;;) {
235 ts = *pts;
236 if (!ts)
237 break;
238 if (ts->len == len && !memcmp(ts->str, str, len))
239 return ts;
240 pts = &(ts->hash_next);
242 return tok_alloc_new(pts, str, len);
245 /* XXX: buffer overflow */
246 /* XXX: float tokens */
247 ST_FUNC char *get_tok_str(int v, CValue *cv)
249 static char buf[STRING_MAX_SIZE + 1];
250 static CString cstr_buf;
251 CString *cstr;
252 char *p;
253 int i, len;
255 /* NOTE: to go faster, we give a fixed buffer for small strings */
256 cstr_reset(&cstr_buf);
257 cstr_buf.data = buf;
258 cstr_buf.size_allocated = sizeof(buf);
259 p = buf;
261 switch(v) {
262 case TOK_CINT:
263 case TOK_CUINT:
264 /* XXX: not quite exact, but only useful for testing */
265 sprintf(p, "%u", cv->ui);
266 break;
267 case TOK_CLLONG:
268 case TOK_CULLONG:
269 /* XXX: not quite exact, but only useful for testing */
270 #ifdef _WIN32
271 sprintf(p, "%u", (unsigned)cv->ull);
272 #else
273 sprintf(p, "%Lu", cv->ull);
274 #endif
275 break;
276 case TOK_LCHAR:
277 cstr_ccat(&cstr_buf, 'L');
278 case TOK_CCHAR:
279 cstr_ccat(&cstr_buf, '\'');
280 add_char(&cstr_buf, cv->i);
281 cstr_ccat(&cstr_buf, '\'');
282 cstr_ccat(&cstr_buf, '\0');
283 break;
284 case TOK_PPNUM:
285 cstr = cv->cstr;
286 len = cstr->size - 1;
287 for(i=0;i<len;i++)
288 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
289 cstr_ccat(&cstr_buf, '\0');
290 break;
291 case TOK_LSTR:
292 cstr_ccat(&cstr_buf, 'L');
293 case TOK_STR:
294 cstr = cv->cstr;
295 cstr_ccat(&cstr_buf, '\"');
296 if (v == TOK_STR) {
297 len = cstr->size - 1;
298 for(i=0;i<len;i++)
299 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
300 } else {
301 len = (cstr->size / sizeof(nwchar_t)) - 1;
302 for(i=0;i<len;i++)
303 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
305 cstr_ccat(&cstr_buf, '\"');
306 cstr_ccat(&cstr_buf, '\0');
307 break;
308 case TOK_LT:
309 v = '<';
310 goto addv;
311 case TOK_GT:
312 v = '>';
313 goto addv;
314 case TOK_DOTS:
315 return strcpy(p, "...");
316 case TOK_A_SHL:
317 return strcpy(p, "<<=");
318 case TOK_A_SAR:
319 return strcpy(p, ">>=");
320 default:
321 if (v < TOK_IDENT) {
322 /* search in two bytes table */
323 const unsigned char *q = tok_two_chars;
324 while (*q) {
325 if (q[2] == v) {
326 *p++ = q[0];
327 *p++ = q[1];
328 *p = '\0';
329 return buf;
331 q += 3;
333 addv:
334 *p++ = v;
335 *p = '\0';
336 } else if (v < tok_ident) {
337 return table_ident[v - TOK_IDENT]->str;
338 } else if (v >= SYM_FIRST_ANOM) {
339 /* special name for anonymous symbol */
340 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
341 } else {
342 /* should never happen */
343 return NULL;
345 break;
347 return cstr_buf.data;
350 /* fill input buffer and peek next char */
351 static int tcc_peekc_slow(BufferedFile *bf)
353 int len;
354 /* only tries to read if really end of buffer */
355 if (bf->buf_ptr >= bf->buf_end) {
356 if (bf->fd != -1) {
357 #if defined(PARSE_DEBUG)
358 len = 8;
359 #else
360 len = IO_BUF_SIZE;
361 #endif
362 len = read(bf->fd, bf->buffer, len);
363 if (len < 0)
364 len = 0;
365 } else {
366 len = 0;
368 total_bytes += len;
369 bf->buf_ptr = bf->buffer;
370 bf->buf_end = bf->buffer + len;
371 *bf->buf_end = CH_EOB;
373 if (bf->buf_ptr < bf->buf_end) {
374 return bf->buf_ptr[0];
375 } else {
376 bf->buf_ptr = bf->buf_end;
377 return CH_EOF;
381 /* return the current character, handling end of block if necessary
382 (but not stray) */
383 ST_FUNC int handle_eob(void)
385 return tcc_peekc_slow(file);
388 /* read next char from current input file and handle end of input buffer */
389 ST_INLN void inp(void)
391 ch = *(++(file->buf_ptr));
392 /* end of buffer/file handling */
393 if (ch == CH_EOB)
394 ch = handle_eob();
397 /* handle '\[\r]\n' */
398 static int handle_stray_noerror(void)
400 while (ch == '\\') {
401 inp();
402 if (ch == '\n') {
403 file->line_num++;
404 inp();
405 } else if (ch == '\r') {
406 inp();
407 if (ch != '\n')
408 goto fail;
409 file->line_num++;
410 inp();
411 } else {
412 fail:
413 return 1;
416 return 0;
419 static void handle_stray(void)
421 if (handle_stray_noerror())
422 error("stray '\\' in program");
425 /* skip the stray and handle the \\n case. Output an error if
426 incorrect char after the stray */
427 static int handle_stray1(uint8_t *p)
429 int c;
431 if (p >= file->buf_end) {
432 file->buf_ptr = p;
433 c = handle_eob();
434 p = file->buf_ptr;
435 if (c == '\\')
436 goto parse_stray;
437 } else {
438 parse_stray:
439 file->buf_ptr = p;
440 ch = *p;
441 handle_stray();
442 p = file->buf_ptr;
443 c = *p;
445 return c;
448 /* handle just the EOB case, but not stray */
449 #define PEEKC_EOB(c, p)\
451 p++;\
452 c = *p;\
453 if (c == '\\') {\
454 file->buf_ptr = p;\
455 c = handle_eob();\
456 p = file->buf_ptr;\
460 /* handle the complicated stray case */
461 #define PEEKC(c, p)\
463 p++;\
464 c = *p;\
465 if (c == '\\') {\
466 c = handle_stray1(p);\
467 p = file->buf_ptr;\
471 /* input with '\[\r]\n' handling. Note that this function cannot
472 handle other characters after '\', so you cannot call it inside
473 strings or comments */
474 ST_FUNC void minp(void)
476 inp();
477 if (ch == '\\')
478 handle_stray();
482 /* single line C++ comments */
483 static uint8_t *parse_line_comment(uint8_t *p)
485 int c;
487 p++;
488 for(;;) {
489 c = *p;
490 redo:
491 if (c == '\n' || c == CH_EOF) {
492 break;
493 } else if (c == '\\') {
494 file->buf_ptr = p;
495 c = handle_eob();
496 p = file->buf_ptr;
497 if (c == '\\') {
498 PEEKC_EOB(c, p);
499 if (c == '\n') {
500 file->line_num++;
501 PEEKC_EOB(c, p);
502 } else if (c == '\r') {
503 PEEKC_EOB(c, p);
504 if (c == '\n') {
505 file->line_num++;
506 PEEKC_EOB(c, p);
509 } else {
510 goto redo;
512 } else {
513 p++;
516 return p;
519 /* C comments */
520 ST_FUNC uint8_t *parse_comment(uint8_t *p)
522 int c;
524 p++;
525 for(;;) {
526 /* fast skip loop */
527 for(;;) {
528 c = *p;
529 if (c == '\n' || c == '*' || c == '\\')
530 break;
531 p++;
532 c = *p;
533 if (c == '\n' || c == '*' || c == '\\')
534 break;
535 p++;
537 /* now we can handle all the cases */
538 if (c == '\n') {
539 file->line_num++;
540 p++;
541 } else if (c == '*') {
542 p++;
543 for(;;) {
544 c = *p;
545 if (c == '*') {
546 p++;
547 } else if (c == '/') {
548 goto end_of_comment;
549 } else if (c == '\\') {
550 file->buf_ptr = p;
551 c = handle_eob();
552 p = file->buf_ptr;
553 if (c == '\\') {
554 /* skip '\[\r]\n', otherwise just skip the stray */
555 while (c == '\\') {
556 PEEKC_EOB(c, p);
557 if (c == '\n') {
558 file->line_num++;
559 PEEKC_EOB(c, p);
560 } else if (c == '\r') {
561 PEEKC_EOB(c, p);
562 if (c == '\n') {
563 file->line_num++;
564 PEEKC_EOB(c, p);
566 } else {
567 goto after_star;
571 } else {
572 break;
575 after_star: ;
576 } else {
577 /* stray, eob or eof */
578 file->buf_ptr = p;
579 c = handle_eob();
580 p = file->buf_ptr;
581 if (c == CH_EOF) {
582 error("unexpected end of file in comment");
583 } else if (c == '\\') {
584 p++;
588 end_of_comment:
589 p++;
590 return p;
593 #define cinp minp
595 static inline void skip_spaces(void)
597 while (is_space(ch))
598 cinp();
601 static inline int check_space(int t, int *spc)
603 if (is_space(t)) {
604 if (*spc)
605 return 1;
606 *spc = 1;
607 } else
608 *spc = 0;
609 return 0;
612 /* parse a string without interpreting escapes */
613 static uint8_t *parse_pp_string(uint8_t *p,
614 int sep, CString *str)
616 int c;
617 p++;
618 for(;;) {
619 c = *p;
620 if (c == sep) {
621 break;
622 } else if (c == '\\') {
623 file->buf_ptr = p;
624 c = handle_eob();
625 p = file->buf_ptr;
626 if (c == CH_EOF) {
627 unterminated_string:
628 /* XXX: indicate line number of start of string */
629 error("missing terminating %c character", sep);
630 } else if (c == '\\') {
631 /* escape : just skip \[\r]\n */
632 PEEKC_EOB(c, p);
633 if (c == '\n') {
634 file->line_num++;
635 p++;
636 } else if (c == '\r') {
637 PEEKC_EOB(c, p);
638 if (c != '\n')
639 expect("'\n' after '\r'");
640 file->line_num++;
641 p++;
642 } else if (c == CH_EOF) {
643 goto unterminated_string;
644 } else {
645 if (str) {
646 cstr_ccat(str, '\\');
647 cstr_ccat(str, c);
649 p++;
652 } else if (c == '\n') {
653 file->line_num++;
654 goto add_char;
655 } else if (c == '\r') {
656 PEEKC_EOB(c, p);
657 if (c != '\n') {
658 if (str)
659 cstr_ccat(str, '\r');
660 } else {
661 file->line_num++;
662 goto add_char;
664 } else {
665 add_char:
666 if (str)
667 cstr_ccat(str, c);
668 p++;
671 p++;
672 return p;
675 /* skip block of text until #else, #elif or #endif. skip also pairs of
676 #if/#endif */
677 static void preprocess_skip(void)
679 int a, start_of_line, c, in_warn_or_error;
680 uint8_t *p;
682 p = file->buf_ptr;
683 a = 0;
684 redo_start:
685 start_of_line = 1;
686 in_warn_or_error = 0;
687 for(;;) {
688 redo_no_start:
689 c = *p;
690 switch(c) {
691 case ' ':
692 case '\t':
693 case '\f':
694 case '\v':
695 case '\r':
696 p++;
697 goto redo_no_start;
698 case '\n':
699 file->line_num++;
700 p++;
701 goto redo_start;
702 case '\\':
703 file->buf_ptr = p;
704 c = handle_eob();
705 if (c == CH_EOF) {
706 expect("#endif");
707 } else if (c == '\\') {
708 ch = file->buf_ptr[0];
709 handle_stray_noerror();
711 p = file->buf_ptr;
712 goto redo_no_start;
713 /* skip strings */
714 case '\"':
715 case '\'':
716 if (in_warn_or_error)
717 goto _default;
718 p = parse_pp_string(p, c, NULL);
719 break;
720 /* skip comments */
721 case '/':
722 if (in_warn_or_error)
723 goto _default;
724 file->buf_ptr = p;
725 ch = *p;
726 minp();
727 p = file->buf_ptr;
728 if (ch == '*') {
729 p = parse_comment(p);
730 } else if (ch == '/') {
731 p = parse_line_comment(p);
733 break;
734 case '#':
735 p++;
736 if (start_of_line) {
737 file->buf_ptr = p;
738 next_nomacro();
739 p = file->buf_ptr;
740 if (a == 0 &&
741 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
742 goto the_end;
743 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
744 a++;
745 else if (tok == TOK_ENDIF)
746 a--;
747 else if( tok == TOK_ERROR || tok == TOK_WARNING)
748 in_warn_or_error = 1;
750 break;
751 _default:
752 default:
753 p++;
754 break;
756 start_of_line = 0;
758 the_end: ;
759 file->buf_ptr = p;
762 /* ParseState handling */
764 /* XXX: currently, no include file info is stored. Thus, we cannot display
765 accurate messages if the function or data definition spans multiple
766 files */
768 /* save current parse state in 's' */
769 ST_FUNC void save_parse_state(ParseState *s)
771 s->line_num = file->line_num;
772 s->macro_ptr = macro_ptr;
773 s->tok = tok;
774 s->tokc = tokc;
777 /* restore parse state from 's' */
778 ST_FUNC void restore_parse_state(ParseState *s)
780 file->line_num = s->line_num;
781 macro_ptr = s->macro_ptr;
782 tok = s->tok;
783 tokc = s->tokc;
786 /* return the number of additional 'ints' necessary to store the
787 token */
788 static inline int tok_ext_size(int t)
790 switch(t) {
791 /* 4 bytes */
792 case TOK_CINT:
793 case TOK_CUINT:
794 case TOK_CCHAR:
795 case TOK_LCHAR:
796 case TOK_CFLOAT:
797 case TOK_LINENUM:
798 return 1;
799 case TOK_STR:
800 case TOK_LSTR:
801 case TOK_PPNUM:
802 error("unsupported token");
803 return 1;
804 case TOK_CDOUBLE:
805 case TOK_CLLONG:
806 case TOK_CULLONG:
807 return 2;
808 case TOK_CLDOUBLE:
809 return LDOUBLE_SIZE / 4;
810 default:
811 return 0;
815 /* token string handling */
817 ST_INLN void tok_str_new(TokenString *s)
819 s->str = NULL;
820 s->len = 0;
821 s->allocated_len = 0;
822 s->last_line_num = -1;
825 ST_FUNC void tok_str_free(int *str)
827 tcc_free(str);
830 static int *tok_str_realloc(TokenString *s)
832 int *str, len;
834 if (s->allocated_len == 0) {
835 len = 8;
836 } else {
837 len = s->allocated_len * 2;
839 str = tcc_realloc(s->str, len * sizeof(int));
840 if (!str)
841 error("memory full");
842 s->allocated_len = len;
843 s->str = str;
844 return str;
847 ST_FUNC void tok_str_add(TokenString *s, int t)
849 int len, *str;
851 len = s->len;
852 str = s->str;
853 if (len >= s->allocated_len)
854 str = tok_str_realloc(s);
855 str[len++] = t;
856 s->len = len;
859 static void tok_str_add2(TokenString *s, int t, CValue *cv)
861 int len, *str;
863 len = s->len;
864 str = s->str;
866 /* allocate space for worst case */
867 if (len + TOK_MAX_SIZE > s->allocated_len)
868 str = tok_str_realloc(s);
869 str[len++] = t;
870 switch(t) {
871 case TOK_CINT:
872 case TOK_CUINT:
873 case TOK_CCHAR:
874 case TOK_LCHAR:
875 case TOK_CFLOAT:
876 case TOK_LINENUM:
877 str[len++] = cv->tab[0];
878 break;
879 case TOK_PPNUM:
880 case TOK_STR:
881 case TOK_LSTR:
883 int nb_words;
884 CString *cstr;
886 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
887 while ((len + nb_words) > s->allocated_len)
888 str = tok_str_realloc(s);
889 cstr = (CString *)(str + len);
890 cstr->data = NULL;
891 cstr->size = cv->cstr->size;
892 cstr->data_allocated = NULL;
893 cstr->size_allocated = cstr->size;
894 memcpy((char *)cstr + sizeof(CString),
895 cv->cstr->data, cstr->size);
896 len += nb_words;
898 break;
899 case TOK_CDOUBLE:
900 case TOK_CLLONG:
901 case TOK_CULLONG:
902 #if LDOUBLE_SIZE == 8
903 case TOK_CLDOUBLE:
904 #endif
905 str[len++] = cv->tab[0];
906 str[len++] = cv->tab[1];
907 break;
908 #if LDOUBLE_SIZE == 12
909 case TOK_CLDOUBLE:
910 str[len++] = cv->tab[0];
911 str[len++] = cv->tab[1];
912 str[len++] = cv->tab[2];
913 #elif LDOUBLE_SIZE == 16
914 case TOK_CLDOUBLE:
915 str[len++] = cv->tab[0];
916 str[len++] = cv->tab[1];
917 str[len++] = cv->tab[2];
918 str[len++] = cv->tab[3];
919 #elif LDOUBLE_SIZE != 8
920 #error add long double size support
921 #endif
922 break;
923 default:
924 break;
926 s->len = len;
929 /* add the current parse token in token string 's' */
930 ST_FUNC void tok_str_add_tok(TokenString *s)
932 CValue cval;
934 /* save line number info */
935 if (file->line_num != s->last_line_num) {
936 s->last_line_num = file->line_num;
937 cval.i = s->last_line_num;
938 tok_str_add2(s, TOK_LINENUM, &cval);
940 tok_str_add2(s, tok, &tokc);
943 #if LDOUBLE_SIZE == 16
944 #define LDOUBLE_GET(p, cv) \
945 cv.tab[0] = p[0]; \
946 cv.tab[1] = p[1]; \
947 cv.tab[2] = p[2]; \
948 cv.tab[3] = p[3];
949 #elif LDOUBLE_SIZE == 12
950 #define LDOUBLE_GET(p, cv) \
951 cv.tab[0] = p[0]; \
952 cv.tab[1] = p[1]; \
953 cv.tab[2] = p[2];
954 #elif LDOUBLE_SIZE == 8
955 #define LDOUBLE_GET(p, cv) \
956 cv.tab[0] = p[0]; \
957 cv.tab[1] = p[1];
958 #else
959 #error add long double size support
960 #endif
963 /* get a token from an integer array and increment pointer
964 accordingly. we code it as a macro to avoid pointer aliasing. */
965 #define TOK_GET(t, p, cv) \
967 t = *p++; \
968 switch(t) { \
969 case TOK_CINT: \
970 case TOK_CUINT: \
971 case TOK_CCHAR: \
972 case TOK_LCHAR: \
973 case TOK_CFLOAT: \
974 case TOK_LINENUM: \
975 cv.tab[0] = *p++; \
976 break; \
977 case TOK_STR: \
978 case TOK_LSTR: \
979 case TOK_PPNUM: \
980 cv.cstr = (CString *)p; \
981 cv.cstr->data = (char *)p + sizeof(CString);\
982 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
983 break; \
984 case TOK_CDOUBLE: \
985 case TOK_CLLONG: \
986 case TOK_CULLONG: \
987 cv.tab[0] = p[0]; \
988 cv.tab[1] = p[1]; \
989 p += 2; \
990 break; \
991 case TOK_CLDOUBLE: \
992 LDOUBLE_GET(p, cv); \
993 p += LDOUBLE_SIZE / 4; \
994 break; \
995 default: \
996 break; \
1000 /* defines handling */
1001 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1003 Sym *s;
1005 s = sym_push2(&define_stack, v, macro_type, 0);
1006 s->d = str;
1007 s->next = first_arg;
1008 table_ident[v - TOK_IDENT]->sym_define = s;
1011 /* undefined a define symbol. Its name is just set to zero */
1012 ST_FUNC void define_undef(Sym *s)
1014 int v;
1015 v = s->v;
1016 if (v >= TOK_IDENT && v < tok_ident)
1017 table_ident[v - TOK_IDENT]->sym_define = NULL;
1018 s->v = 0;
1021 ST_INLN Sym *define_find(int v)
1023 v -= TOK_IDENT;
1024 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1025 return NULL;
1026 return table_ident[v]->sym_define;
1029 /* free define stack until top reaches 'b' */
1030 ST_FUNC void free_defines(Sym *b)
1032 Sym *top, *top1;
1033 int v;
1035 top = define_stack;
1036 while (top != b) {
1037 top1 = top->prev;
1038 /* do not free args or predefined defines */
1039 if (top->d)
1040 tok_str_free(top->d);
1041 v = top->v;
1042 if (v >= TOK_IDENT && v < tok_ident)
1043 table_ident[v - TOK_IDENT]->sym_define = NULL;
1044 sym_free(top);
1045 top = top1;
1047 define_stack = b;
1050 /* label lookup */
1051 ST_FUNC Sym *label_find(int v)
1053 v -= TOK_IDENT;
1054 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1055 return NULL;
1056 return table_ident[v]->sym_label;
1059 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1061 Sym *s, **ps;
1062 s = sym_push2(ptop, v, 0, 0);
1063 s->r = flags;
1064 ps = &table_ident[v - TOK_IDENT]->sym_label;
1065 if (ptop == &global_label_stack) {
1066 /* modify the top most local identifier, so that
1067 sym_identifier will point to 's' when popped */
1068 while (*ps != NULL)
1069 ps = &(*ps)->prev_tok;
1071 s->prev_tok = *ps;
1072 *ps = s;
1073 return s;
1076 /* pop labels until element last is reached. Look if any labels are
1077 undefined. Define symbols if '&&label' was used. */
1078 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1080 Sym *s, *s1;
1081 for(s = *ptop; s != slast; s = s1) {
1082 s1 = s->prev;
1083 if (s->r == LABEL_DECLARED) {
1084 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1085 } else if (s->r == LABEL_FORWARD) {
1086 error("label '%s' used but not defined",
1087 get_tok_str(s->v, NULL));
1088 } else {
1089 if (s->c) {
1090 /* define corresponding symbol. A size of
1091 1 is put. */
1092 put_extern_sym(s, cur_text_section, s->jnext, 1);
1095 /* remove label */
1096 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1097 sym_free(s);
1099 *ptop = slast;
1102 /* eval an expression for #if/#elif */
1103 static int expr_preprocess(void)
1105 int c, t;
1106 TokenString str;
1108 tok_str_new(&str);
1109 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1110 next(); /* do macro subst */
1111 if (tok == TOK_DEFINED) {
1112 next_nomacro();
1113 t = tok;
1114 if (t == '(')
1115 next_nomacro();
1116 c = define_find(tok) != 0;
1117 if (t == '(')
1118 next_nomacro();
1119 tok = TOK_CINT;
1120 tokc.i = c;
1121 } else if (tok >= TOK_IDENT) {
1122 /* if undefined macro */
1123 tok = TOK_CINT;
1124 tokc.i = 0;
1126 tok_str_add_tok(&str);
1128 tok_str_add(&str, -1); /* simulate end of file */
1129 tok_str_add(&str, 0);
1130 /* now evaluate C constant expression */
1131 macro_ptr = str.str;
1132 next();
1133 c = expr_const();
1134 macro_ptr = NULL;
1135 tok_str_free(str.str);
1136 return c != 0;
1139 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1140 static void tok_print(int *str)
1142 int t;
1143 CValue cval;
1145 printf("<");
1146 while (1) {
1147 TOK_GET(t, str, cval);
1148 if (!t)
1149 break;
1150 printf("%s", get_tok_str(t, &cval));
1152 printf(">\n");
1154 #endif
1156 /* parse after #define */
1157 ST_FUNC void parse_define(void)
1159 Sym *s, *first, **ps;
1160 int v, t, varg, is_vaargs, spc;
1161 TokenString str;
1163 v = tok;
1164 if (v < TOK_IDENT)
1165 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1166 /* XXX: should check if same macro (ANSI) */
1167 first = NULL;
1168 t = MACRO_OBJ;
1169 /* '(' must be just after macro definition for MACRO_FUNC */
1170 next_nomacro_spc();
1171 if (tok == '(') {
1172 next_nomacro();
1173 ps = &first;
1174 while (tok != ')') {
1175 varg = tok;
1176 next_nomacro();
1177 is_vaargs = 0;
1178 if (varg == TOK_DOTS) {
1179 varg = TOK___VA_ARGS__;
1180 is_vaargs = 1;
1181 } else if (tok == TOK_DOTS && gnu_ext) {
1182 is_vaargs = 1;
1183 next_nomacro();
1185 if (varg < TOK_IDENT)
1186 error("badly punctuated parameter list");
1187 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1188 *ps = s;
1189 ps = &s->next;
1190 if (tok != ',')
1191 break;
1192 next_nomacro();
1194 if (tok == ')')
1195 next_nomacro_spc();
1196 t = MACRO_FUNC;
1198 tok_str_new(&str);
1199 spc = 2;
1200 /* EOF testing necessary for '-D' handling */
1201 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1202 /* remove spaces around ## and after '#' */
1203 if (TOK_TWOSHARPS == tok) {
1204 if (1 == spc)
1205 --str.len;
1206 spc = 2;
1207 } else if ('#' == tok) {
1208 spc = 2;
1209 } else if (check_space(tok, &spc)) {
1210 goto skip;
1212 tok_str_add2(&str, tok, &tokc);
1213 skip:
1214 next_nomacro_spc();
1216 if (spc == 1)
1217 --str.len; /* remove trailing space */
1218 tok_str_add(&str, 0);
1219 #ifdef PP_DEBUG
1220 printf("define %s %d: ", get_tok_str(v, NULL), t);
1221 tok_print(str.str);
1222 #endif
1223 define_push(v, t, str.str, first);
1226 static inline int hash_cached_include(int type, const char *filename)
1228 const unsigned char *s;
1229 unsigned int h;
1231 h = TOK_HASH_INIT;
1232 h = TOK_HASH_FUNC(h, type);
1233 s = filename;
1234 while (*s) {
1235 h = TOK_HASH_FUNC(h, *s);
1236 s++;
1238 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1239 return h;
1242 /* XXX: use a token or a hash table to accelerate matching ? */
1243 static CachedInclude *search_cached_include(TCCState *s1,
1244 int type, const char *filename)
1246 CachedInclude *e;
1247 int i, h;
1248 h = hash_cached_include(type, filename);
1249 i = s1->cached_includes_hash[h];
1250 for(;;) {
1251 if (i == 0)
1252 break;
1253 e = s1->cached_includes[i - 1];
1254 if (e->type == type && !PATHCMP(e->filename, filename))
1255 return e;
1256 i = e->hash_next;
1258 return NULL;
1261 static inline void add_cached_include(TCCState *s1, int type,
1262 const char *filename, int ifndef_macro)
1264 CachedInclude *e;
1265 int h;
1267 if (search_cached_include(s1, type, filename))
1268 return;
1269 #ifdef INC_DEBUG
1270 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1271 #endif
1272 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1273 if (!e)
1274 return;
1275 e->type = type;
1276 strcpy(e->filename, filename);
1277 e->ifndef_macro = ifndef_macro;
1278 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1279 /* add in hash table */
1280 h = hash_cached_include(type, filename);
1281 e->hash_next = s1->cached_includes_hash[h];
1282 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1285 static void pragma_parse(TCCState *s1)
1287 int val;
1289 next();
1290 if (tok == TOK_pack) {
1292 This may be:
1293 #pragma pack(1) // set
1294 #pragma pack() // reset to default
1295 #pragma pack(push,1) // push & set
1296 #pragma pack(pop) // restore previous
1298 next();
1299 skip('(');
1300 if (tok == TOK_ASM_pop) {
1301 next();
1302 if (s1->pack_stack_ptr <= s1->pack_stack) {
1303 stk_error:
1304 error("out of pack stack");
1306 s1->pack_stack_ptr--;
1307 } else {
1308 val = 0;
1309 if (tok != ')') {
1310 if (tok == TOK_ASM_push) {
1311 next();
1312 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1313 goto stk_error;
1314 s1->pack_stack_ptr++;
1315 skip(',');
1317 if (tok != TOK_CINT) {
1318 pack_error:
1319 error("invalid pack pragma");
1321 val = tokc.i;
1322 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1323 goto pack_error;
1324 next();
1326 *s1->pack_stack_ptr = val;
1327 skip(')');
1332 /* is_bof is true if first non space token at beginning of file */
1333 ST_FUNC void preprocess(int is_bof)
1335 TCCState *s1 = tcc_state;
1336 int i, c, n, saved_parse_flags;
1337 char buf[1024], *q;
1338 Sym *s;
1340 saved_parse_flags = parse_flags;
1341 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1342 PARSE_FLAG_LINEFEED;
1343 next_nomacro();
1344 redo:
1345 switch(tok) {
1346 case TOK_DEFINE:
1347 next_nomacro();
1348 parse_define();
1349 break;
1350 case TOK_UNDEF:
1351 next_nomacro();
1352 s = define_find(tok);
1353 /* undefine symbol by putting an invalid name */
1354 if (s)
1355 define_undef(s);
1356 break;
1357 case TOK_INCLUDE:
1358 case TOK_INCLUDE_NEXT:
1359 ch = file->buf_ptr[0];
1360 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1361 skip_spaces();
1362 if (ch == '<') {
1363 c = '>';
1364 goto read_name;
1365 } else if (ch == '\"') {
1366 c = ch;
1367 read_name:
1368 inp();
1369 q = buf;
1370 while (ch != c && ch != '\n' && ch != CH_EOF) {
1371 if ((q - buf) < sizeof(buf) - 1)
1372 *q++ = ch;
1373 if (ch == '\\') {
1374 if (handle_stray_noerror() == 0)
1375 --q;
1376 } else
1377 inp();
1379 *q = '\0';
1380 minp();
1381 #if 0
1382 /* eat all spaces and comments after include */
1383 /* XXX: slightly incorrect */
1384 while (ch1 != '\n' && ch1 != CH_EOF)
1385 inp();
1386 #endif
1387 } else {
1388 /* computed #include : either we have only strings or
1389 we have anything enclosed in '<>' */
1390 next();
1391 buf[0] = '\0';
1392 if (tok == TOK_STR) {
1393 while (tok != TOK_LINEFEED) {
1394 if (tok != TOK_STR) {
1395 include_syntax:
1396 error("'#include' expects \"FILENAME\" or <FILENAME>");
1398 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1399 next();
1401 c = '\"';
1402 } else {
1403 int len;
1404 while (tok != TOK_LINEFEED) {
1405 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1406 next();
1408 len = strlen(buf);
1409 /* check syntax and remove '<>' */
1410 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1411 goto include_syntax;
1412 memmove(buf, buf + 1, len - 2);
1413 buf[len - 2] = '\0';
1414 c = '>';
1418 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1419 error("#include recursion too deep");
1421 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1422 for (i = -2; i < n; ++i) {
1423 char buf1[sizeof file->filename];
1424 BufferedFile *f;
1425 CachedInclude *e;
1426 const char *path;
1427 int size;
1429 if (i == -2) {
1430 /* check absolute include path */
1431 if (!IS_ABSPATH(buf))
1432 continue;
1433 buf1[0] = 0;
1435 } else if (i == -1) {
1436 /* search in current dir if "header.h" */
1437 if (c != '\"')
1438 continue;
1439 size = tcc_basename(file->filename) - file->filename;
1440 memcpy(buf1, file->filename, size);
1441 buf1[size] = '\0';
1443 } else {
1444 /* search in all the include paths */
1445 if (i < s1->nb_include_paths)
1446 path = s1->include_paths[i];
1447 else
1448 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1449 pstrcpy(buf1, sizeof(buf1), path);
1450 pstrcat(buf1, sizeof(buf1), "/");
1453 pstrcat(buf1, sizeof(buf1), buf);
1455 e = search_cached_include(s1, c, buf1);
1456 if (e && define_find(e->ifndef_macro)) {
1457 /* no need to parse the include because the 'ifndef macro'
1458 is defined */
1459 #ifdef INC_DEBUG
1460 printf("%s: skipping %s\n", file->filename, buf);
1461 #endif
1462 f = NULL;
1463 } else {
1464 f = tcc_open(s1, buf1);
1465 if (!f)
1466 continue;
1469 if (tok == TOK_INCLUDE_NEXT) {
1470 tok = TOK_INCLUDE;
1471 if (f)
1472 tcc_close(f);
1473 continue;
1476 if (!f)
1477 goto include_done;
1479 #ifdef INC_DEBUG
1480 printf("%s: including %s\n", file->filename, buf1);
1481 #endif
1483 /* XXX: fix current line init */
1484 /* push current file in stack */
1485 *s1->include_stack_ptr++ = file;
1486 f->inc_type = c;
1487 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
1488 file = f;
1489 /* add include file debug info */
1490 if (s1->do_debug) {
1491 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1493 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1494 ch = file->buf_ptr[0];
1495 goto the_end;
1497 error("include file '%s' not found", buf);
1498 include_done:
1499 break;
1500 case TOK_IFNDEF:
1501 c = 1;
1502 goto do_ifdef;
1503 case TOK_IF:
1504 c = expr_preprocess();
1505 goto do_if;
1506 case TOK_IFDEF:
1507 c = 0;
1508 do_ifdef:
1509 next_nomacro();
1510 if (tok < TOK_IDENT)
1511 error("invalid argument for '#if%sdef'", c ? "n" : "");
1512 if (is_bof) {
1513 if (c) {
1514 #ifdef INC_DEBUG
1515 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1516 #endif
1517 file->ifndef_macro = tok;
1520 c = (define_find(tok) != 0) ^ c;
1521 do_if:
1522 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1523 error("memory full");
1524 *s1->ifdef_stack_ptr++ = c;
1525 goto test_skip;
1526 case TOK_ELSE:
1527 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1528 error("#else without matching #if");
1529 if (s1->ifdef_stack_ptr[-1] & 2)
1530 error("#else after #else");
1531 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1532 goto test_else;
1533 case TOK_ELIF:
1534 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1535 error("#elif without matching #if");
1536 c = s1->ifdef_stack_ptr[-1];
1537 if (c > 1)
1538 error("#elif after #else");
1539 /* last #if/#elif expression was true: we skip */
1540 if (c == 1)
1541 goto skip;
1542 c = expr_preprocess();
1543 s1->ifdef_stack_ptr[-1] = c;
1544 test_else:
1545 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1546 file->ifndef_macro = 0;
1547 test_skip:
1548 if (!(c & 1)) {
1549 skip:
1550 preprocess_skip();
1551 is_bof = 0;
1552 goto redo;
1554 break;
1555 case TOK_ENDIF:
1556 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1557 error("#endif without matching #if");
1558 s1->ifdef_stack_ptr--;
1559 /* '#ifndef macro' was at the start of file. Now we check if
1560 an '#endif' is exactly at the end of file */
1561 if (file->ifndef_macro &&
1562 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1563 file->ifndef_macro_saved = file->ifndef_macro;
1564 /* need to set to zero to avoid false matches if another
1565 #ifndef at middle of file */
1566 file->ifndef_macro = 0;
1567 while (tok != TOK_LINEFEED)
1568 next_nomacro();
1569 tok_flags |= TOK_FLAG_ENDIF;
1570 goto the_end;
1572 break;
1573 case TOK_LINE:
1574 next();
1575 if (tok != TOK_CINT)
1576 error("#line");
1577 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1578 next();
1579 if (tok != TOK_LINEFEED) {
1580 if (tok != TOK_STR)
1581 error("#line");
1582 pstrcpy(file->filename, sizeof(file->filename),
1583 (char *)tokc.cstr->data);
1585 break;
1586 case TOK_ERROR:
1587 case TOK_WARNING:
1588 c = tok;
1589 ch = file->buf_ptr[0];
1590 skip_spaces();
1591 q = buf;
1592 while (ch != '\n' && ch != CH_EOF) {
1593 if ((q - buf) < sizeof(buf) - 1)
1594 *q++ = ch;
1595 if (ch == '\\') {
1596 if (handle_stray_noerror() == 0)
1597 --q;
1598 } else
1599 inp();
1601 *q = '\0';
1602 if (c == TOK_ERROR)
1603 error("#error %s", buf);
1604 else
1605 warning("#warning %s", buf);
1606 break;
1607 case TOK_PRAGMA:
1608 pragma_parse(s1);
1609 break;
1610 default:
1611 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
1612 /* '!' is ignored to allow C scripts. numbers are ignored
1613 to emulate cpp behaviour */
1614 } else {
1615 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1616 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1618 break;
1620 /* ignore other preprocess commands or #! for C scripts */
1621 while (tok != TOK_LINEFEED)
1622 next_nomacro();
1623 the_end:
1624 parse_flags = saved_parse_flags;
1627 /* evaluate escape codes in a string. */
1628 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1630 int c, n;
1631 const uint8_t *p;
1633 p = buf;
1634 for(;;) {
1635 c = *p;
1636 if (c == '\0')
1637 break;
1638 if (c == '\\') {
1639 p++;
1640 /* escape */
1641 c = *p;
1642 switch(c) {
1643 case '0': case '1': case '2': case '3':
1644 case '4': case '5': case '6': case '7':
1645 /* at most three octal digits */
1646 n = c - '0';
1647 p++;
1648 c = *p;
1649 if (isoct(c)) {
1650 n = n * 8 + c - '0';
1651 p++;
1652 c = *p;
1653 if (isoct(c)) {
1654 n = n * 8 + c - '0';
1655 p++;
1658 c = n;
1659 goto add_char_nonext;
1660 case 'x':
1661 case 'u':
1662 case 'U':
1663 p++;
1664 n = 0;
1665 for(;;) {
1666 c = *p;
1667 if (c >= 'a' && c <= 'f')
1668 c = c - 'a' + 10;
1669 else if (c >= 'A' && c <= 'F')
1670 c = c - 'A' + 10;
1671 else if (isnum(c))
1672 c = c - '0';
1673 else
1674 break;
1675 n = n * 16 + c;
1676 p++;
1678 c = n;
1679 goto add_char_nonext;
1680 case 'a':
1681 c = '\a';
1682 break;
1683 case 'b':
1684 c = '\b';
1685 break;
1686 case 'f':
1687 c = '\f';
1688 break;
1689 case 'n':
1690 c = '\n';
1691 break;
1692 case 'r':
1693 c = '\r';
1694 break;
1695 case 't':
1696 c = '\t';
1697 break;
1698 case 'v':
1699 c = '\v';
1700 break;
1701 case 'e':
1702 if (!gnu_ext)
1703 goto invalid_escape;
1704 c = 27;
1705 break;
1706 case '\'':
1707 case '\"':
1708 case '\\':
1709 case '?':
1710 break;
1711 default:
1712 invalid_escape:
1713 if (c >= '!' && c <= '~')
1714 warning("unknown escape sequence: \'\\%c\'", c);
1715 else
1716 warning("unknown escape sequence: \'\\x%x\'", c);
1717 break;
1720 p++;
1721 add_char_nonext:
1722 if (!is_long)
1723 cstr_ccat(outstr, c);
1724 else
1725 cstr_wccat(outstr, c);
1727 /* add a trailing '\0' */
1728 if (!is_long)
1729 cstr_ccat(outstr, '\0');
1730 else
1731 cstr_wccat(outstr, '\0');
1734 /* we use 64 bit numbers */
1735 #define BN_SIZE 2
1737 /* bn = (bn << shift) | or_val */
1738 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1740 int i;
1741 unsigned int v;
1742 for(i=0;i<BN_SIZE;i++) {
1743 v = bn[i];
1744 bn[i] = (v << shift) | or_val;
1745 or_val = v >> (32 - shift);
1749 static void bn_zero(unsigned int *bn)
1751 int i;
1752 for(i=0;i<BN_SIZE;i++) {
1753 bn[i] = 0;
1757 /* parse number in null terminated string 'p' and return it in the
1758 current token */
1759 static void parse_number(const char *p)
1761 int b, t, shift, frac_bits, s, exp_val, ch;
1762 char *q;
1763 unsigned int bn[BN_SIZE];
1764 double d;
1766 /* number */
1767 q = token_buf;
1768 ch = *p++;
1769 t = ch;
1770 ch = *p++;
1771 *q++ = t;
1772 b = 10;
1773 if (t == '.') {
1774 goto float_frac_parse;
1775 } else if (t == '0') {
1776 if (ch == 'x' || ch == 'X') {
1777 q--;
1778 ch = *p++;
1779 b = 16;
1780 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1781 q--;
1782 ch = *p++;
1783 b = 2;
1786 /* parse all digits. cannot check octal numbers at this stage
1787 because of floating point constants */
1788 while (1) {
1789 if (ch >= 'a' && ch <= 'f')
1790 t = ch - 'a' + 10;
1791 else if (ch >= 'A' && ch <= 'F')
1792 t = ch - 'A' + 10;
1793 else if (isnum(ch))
1794 t = ch - '0';
1795 else
1796 break;
1797 if (t >= b)
1798 break;
1799 if (q >= token_buf + STRING_MAX_SIZE) {
1800 num_too_long:
1801 error("number too long");
1803 *q++ = ch;
1804 ch = *p++;
1806 if (ch == '.' ||
1807 ((ch == 'e' || ch == 'E') && b == 10) ||
1808 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1809 if (b != 10) {
1810 /* NOTE: strtox should support that for hexa numbers, but
1811 non ISOC99 libcs do not support it, so we prefer to do
1812 it by hand */
1813 /* hexadecimal or binary floats */
1814 /* XXX: handle overflows */
1815 *q = '\0';
1816 if (b == 16)
1817 shift = 4;
1818 else
1819 shift = 2;
1820 bn_zero(bn);
1821 q = token_buf;
1822 while (1) {
1823 t = *q++;
1824 if (t == '\0') {
1825 break;
1826 } else if (t >= 'a') {
1827 t = t - 'a' + 10;
1828 } else if (t >= 'A') {
1829 t = t - 'A' + 10;
1830 } else {
1831 t = t - '0';
1833 bn_lshift(bn, shift, t);
1835 frac_bits = 0;
1836 if (ch == '.') {
1837 ch = *p++;
1838 while (1) {
1839 t = ch;
1840 if (t >= 'a' && t <= 'f') {
1841 t = t - 'a' + 10;
1842 } else if (t >= 'A' && t <= 'F') {
1843 t = t - 'A' + 10;
1844 } else if (t >= '0' && t <= '9') {
1845 t = t - '0';
1846 } else {
1847 break;
1849 if (t >= b)
1850 error("invalid digit");
1851 bn_lshift(bn, shift, t);
1852 frac_bits += shift;
1853 ch = *p++;
1856 if (ch != 'p' && ch != 'P')
1857 expect("exponent");
1858 ch = *p++;
1859 s = 1;
1860 exp_val = 0;
1861 if (ch == '+') {
1862 ch = *p++;
1863 } else if (ch == '-') {
1864 s = -1;
1865 ch = *p++;
1867 if (ch < '0' || ch > '9')
1868 expect("exponent digits");
1869 while (ch >= '0' && ch <= '9') {
1870 exp_val = exp_val * 10 + ch - '0';
1871 ch = *p++;
1873 exp_val = exp_val * s;
1875 /* now we can generate the number */
1876 /* XXX: should patch directly float number */
1877 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1878 d = ldexp(d, exp_val - frac_bits);
1879 t = toup(ch);
1880 if (t == 'F') {
1881 ch = *p++;
1882 tok = TOK_CFLOAT;
1883 /* float : should handle overflow */
1884 tokc.f = (float)d;
1885 } else if (t == 'L') {
1886 ch = *p++;
1887 #ifdef TCC_TARGET_PE
1888 tok = TOK_CDOUBLE;
1889 tokc.d = d;
1890 #else
1891 tok = TOK_CLDOUBLE;
1892 /* XXX: not large enough */
1893 tokc.ld = (long double)d;
1894 #endif
1895 } else {
1896 tok = TOK_CDOUBLE;
1897 tokc.d = d;
1899 } else {
1900 /* decimal floats */
1901 if (ch == '.') {
1902 if (q >= token_buf + STRING_MAX_SIZE)
1903 goto num_too_long;
1904 *q++ = ch;
1905 ch = *p++;
1906 float_frac_parse:
1907 while (ch >= '0' && ch <= '9') {
1908 if (q >= token_buf + STRING_MAX_SIZE)
1909 goto num_too_long;
1910 *q++ = ch;
1911 ch = *p++;
1914 if (ch == 'e' || ch == 'E') {
1915 if (q >= token_buf + STRING_MAX_SIZE)
1916 goto num_too_long;
1917 *q++ = ch;
1918 ch = *p++;
1919 if (ch == '-' || ch == '+') {
1920 if (q >= token_buf + STRING_MAX_SIZE)
1921 goto num_too_long;
1922 *q++ = ch;
1923 ch = *p++;
1925 if (ch < '0' || ch > '9')
1926 expect("exponent digits");
1927 while (ch >= '0' && ch <= '9') {
1928 if (q >= token_buf + STRING_MAX_SIZE)
1929 goto num_too_long;
1930 *q++ = ch;
1931 ch = *p++;
1934 *q = '\0';
1935 t = toup(ch);
1936 errno = 0;
1937 if (t == 'F') {
1938 ch = *p++;
1939 tok = TOK_CFLOAT;
1940 tokc.f = strtof(token_buf, NULL);
1941 } else if (t == 'L') {
1942 ch = *p++;
1943 #ifdef TCC_TARGET_PE
1944 tok = TOK_CDOUBLE;
1945 tokc.d = strtod(token_buf, NULL);
1946 #else
1947 tok = TOK_CLDOUBLE;
1948 tokc.ld = strtold(token_buf, NULL);
1949 #endif
1950 } else {
1951 tok = TOK_CDOUBLE;
1952 tokc.d = strtod(token_buf, NULL);
1955 } else {
1956 unsigned long long n, n1;
1957 int lcount, ucount;
1959 /* integer number */
1960 *q = '\0';
1961 q = token_buf;
1962 if (b == 10 && *q == '0') {
1963 b = 8;
1964 q++;
1966 n = 0;
1967 while(1) {
1968 t = *q++;
1969 /* no need for checks except for base 10 / 8 errors */
1970 if (t == '\0') {
1971 break;
1972 } else if (t >= 'a') {
1973 t = t - 'a' + 10;
1974 } else if (t >= 'A') {
1975 t = t - 'A' + 10;
1976 } else {
1977 t = t - '0';
1978 if (t >= b)
1979 error("invalid digit");
1981 n1 = n;
1982 n = n * b + t;
1983 /* detect overflow */
1984 /* XXX: this test is not reliable */
1985 if (n < n1)
1986 error("integer constant overflow");
1989 /* XXX: not exactly ANSI compliant */
1990 if ((n & 0xffffffff00000000LL) != 0) {
1991 if ((n >> 63) != 0)
1992 tok = TOK_CULLONG;
1993 else
1994 tok = TOK_CLLONG;
1995 } else if (n > 0x7fffffff) {
1996 tok = TOK_CUINT;
1997 } else {
1998 tok = TOK_CINT;
2000 lcount = 0;
2001 ucount = 0;
2002 for(;;) {
2003 t = toup(ch);
2004 if (t == 'L') {
2005 if (lcount >= 2)
2006 error("three 'l's in integer constant");
2007 lcount++;
2008 if (lcount == 2) {
2009 if (tok == TOK_CINT)
2010 tok = TOK_CLLONG;
2011 else if (tok == TOK_CUINT)
2012 tok = TOK_CULLONG;
2014 ch = *p++;
2015 } else if (t == 'U') {
2016 if (ucount >= 1)
2017 error("two 'u's in integer constant");
2018 ucount++;
2019 if (tok == TOK_CINT)
2020 tok = TOK_CUINT;
2021 else if (tok == TOK_CLLONG)
2022 tok = TOK_CULLONG;
2023 ch = *p++;
2024 } else {
2025 break;
2028 if (tok == TOK_CINT || tok == TOK_CUINT)
2029 tokc.ui = n;
2030 else
2031 tokc.ull = n;
2033 if (ch)
2034 error("invalid number\n");
2038 #define PARSE2(c1, tok1, c2, tok2) \
2039 case c1: \
2040 PEEKC(c, p); \
2041 if (c == c2) { \
2042 p++; \
2043 tok = tok2; \
2044 } else { \
2045 tok = tok1; \
2047 break;
2049 /* return next token without macro substitution */
2050 static inline void next_nomacro1(void)
2052 int t, c, is_long;
2053 TokenSym *ts;
2054 uint8_t *p, *p1;
2055 unsigned int h;
2057 p = file->buf_ptr;
2058 redo_no_start:
2059 c = *p;
2060 switch(c) {
2061 case ' ':
2062 case '\t':
2063 tok = c;
2064 p++;
2065 goto keep_tok_flags;
2066 case '\f':
2067 case '\v':
2068 case '\r':
2069 p++;
2070 goto redo_no_start;
2071 case '\\':
2072 /* first look if it is in fact an end of buffer */
2073 if (p >= file->buf_end) {
2074 file->buf_ptr = p;
2075 handle_eob();
2076 p = file->buf_ptr;
2077 if (p >= file->buf_end)
2078 goto parse_eof;
2079 else
2080 goto redo_no_start;
2081 } else {
2082 file->buf_ptr = p;
2083 ch = *p;
2084 handle_stray();
2085 p = file->buf_ptr;
2086 goto redo_no_start;
2088 parse_eof:
2090 TCCState *s1 = tcc_state;
2091 if ((parse_flags & PARSE_FLAG_LINEFEED)
2092 && !(tok_flags & TOK_FLAG_EOF)) {
2093 tok_flags |= TOK_FLAG_EOF;
2094 tok = TOK_LINEFEED;
2095 goto keep_tok_flags;
2096 } else if (s1->include_stack_ptr == s1->include_stack ||
2097 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
2098 /* no include left : end of file. */
2099 tok = TOK_EOF;
2100 } else {
2101 tok_flags &= ~TOK_FLAG_EOF;
2102 /* pop include file */
2104 /* test if previous '#endif' was after a #ifdef at
2105 start of file */
2106 if (tok_flags & TOK_FLAG_ENDIF) {
2107 #ifdef INC_DEBUG
2108 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2109 #endif
2110 add_cached_include(s1, file->inc_type, file->inc_filename,
2111 file->ifndef_macro_saved);
2114 /* add end of include file debug info */
2115 if (tcc_state->do_debug) {
2116 put_stabd(N_EINCL, 0, 0);
2118 /* pop include stack */
2119 tcc_close(file);
2120 s1->include_stack_ptr--;
2121 file = *s1->include_stack_ptr;
2122 p = file->buf_ptr;
2123 goto redo_no_start;
2126 break;
2128 case '\n':
2129 file->line_num++;
2130 tok_flags |= TOK_FLAG_BOL;
2131 p++;
2132 maybe_newline:
2133 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2134 goto redo_no_start;
2135 tok = TOK_LINEFEED;
2136 goto keep_tok_flags;
2138 case '#':
2139 /* XXX: simplify */
2140 PEEKC(c, p);
2141 if ((tok_flags & TOK_FLAG_BOL) &&
2142 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2143 file->buf_ptr = p;
2144 preprocess(tok_flags & TOK_FLAG_BOF);
2145 p = file->buf_ptr;
2146 goto maybe_newline;
2147 } else {
2148 if (c == '#') {
2149 p++;
2150 tok = TOK_TWOSHARPS;
2151 } else {
2152 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2153 p = parse_line_comment(p - 1);
2154 goto redo_no_start;
2155 } else {
2156 tok = '#';
2160 break;
2162 case 'a': case 'b': case 'c': case 'd':
2163 case 'e': case 'f': case 'g': case 'h':
2164 case 'i': case 'j': case 'k': case 'l':
2165 case 'm': case 'n': case 'o': case 'p':
2166 case 'q': case 'r': case 's': case 't':
2167 case 'u': case 'v': case 'w': case 'x':
2168 case 'y': case 'z':
2169 case 'A': case 'B': case 'C': case 'D':
2170 case 'E': case 'F': case 'G': case 'H':
2171 case 'I': case 'J': case 'K':
2172 case 'M': case 'N': case 'O': case 'P':
2173 case 'Q': case 'R': case 'S': case 'T':
2174 case 'U': case 'V': case 'W': case 'X':
2175 case 'Y': case 'Z':
2176 case '_':
2177 parse_ident_fast:
2178 p1 = p;
2179 h = TOK_HASH_INIT;
2180 h = TOK_HASH_FUNC(h, c);
2181 p++;
2182 for(;;) {
2183 c = *p;
2184 if (!isidnum_table[c-CH_EOF])
2185 break;
2186 h = TOK_HASH_FUNC(h, c);
2187 p++;
2189 if (c != '\\') {
2190 TokenSym **pts;
2191 int len;
2193 /* fast case : no stray found, so we have the full token
2194 and we have already hashed it */
2195 len = p - p1;
2196 h &= (TOK_HASH_SIZE - 1);
2197 pts = &hash_ident[h];
2198 for(;;) {
2199 ts = *pts;
2200 if (!ts)
2201 break;
2202 if (ts->len == len && !memcmp(ts->str, p1, len))
2203 goto token_found;
2204 pts = &(ts->hash_next);
2206 ts = tok_alloc_new(pts, p1, len);
2207 token_found: ;
2208 } else {
2209 /* slower case */
2210 cstr_reset(&tokcstr);
2212 while (p1 < p) {
2213 cstr_ccat(&tokcstr, *p1);
2214 p1++;
2216 p--;
2217 PEEKC(c, p);
2218 parse_ident_slow:
2219 while (isidnum_table[c-CH_EOF]) {
2220 cstr_ccat(&tokcstr, c);
2221 PEEKC(c, p);
2223 ts = tok_alloc(tokcstr.data, tokcstr.size);
2225 tok = ts->tok;
2226 break;
2227 case 'L':
2228 t = p[1];
2229 if (t != '\\' && t != '\'' && t != '\"') {
2230 /* fast case */
2231 goto parse_ident_fast;
2232 } else {
2233 PEEKC(c, p);
2234 if (c == '\'' || c == '\"') {
2235 is_long = 1;
2236 goto str_const;
2237 } else {
2238 cstr_reset(&tokcstr);
2239 cstr_ccat(&tokcstr, 'L');
2240 goto parse_ident_slow;
2243 break;
2244 case '0': case '1': case '2': case '3':
2245 case '4': case '5': case '6': case '7':
2246 case '8': case '9':
2248 cstr_reset(&tokcstr);
2249 /* after the first digit, accept digits, alpha, '.' or sign if
2250 prefixed by 'eEpP' */
2251 parse_num:
2252 for(;;) {
2253 t = c;
2254 cstr_ccat(&tokcstr, c);
2255 PEEKC(c, p);
2256 if (!(isnum(c) || isid(c) || c == '.' ||
2257 ((c == '+' || c == '-') &&
2258 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2259 break;
2261 /* We add a trailing '\0' to ease parsing */
2262 cstr_ccat(&tokcstr, '\0');
2263 tokc.cstr = &tokcstr;
2264 tok = TOK_PPNUM;
2265 break;
2266 case '.':
2267 /* special dot handling because it can also start a number */
2268 PEEKC(c, p);
2269 if (isnum(c)) {
2270 cstr_reset(&tokcstr);
2271 cstr_ccat(&tokcstr, '.');
2272 goto parse_num;
2273 } else if (c == '.') {
2274 PEEKC(c, p);
2275 if (c != '.')
2276 expect("'.'");
2277 PEEKC(c, p);
2278 tok = TOK_DOTS;
2279 } else {
2280 tok = '.';
2282 break;
2283 case '\'':
2284 case '\"':
2285 is_long = 0;
2286 str_const:
2288 CString str;
2289 int sep;
2291 sep = c;
2293 /* parse the string */
2294 cstr_new(&str);
2295 p = parse_pp_string(p, sep, &str);
2296 cstr_ccat(&str, '\0');
2298 /* eval the escape (should be done as TOK_PPNUM) */
2299 cstr_reset(&tokcstr);
2300 parse_escape_string(&tokcstr, str.data, is_long);
2301 cstr_free(&str);
2303 if (sep == '\'') {
2304 int char_size;
2305 /* XXX: make it portable */
2306 if (!is_long)
2307 char_size = 1;
2308 else
2309 char_size = sizeof(nwchar_t);
2310 if (tokcstr.size <= char_size)
2311 error("empty character constant");
2312 if (tokcstr.size > 2 * char_size)
2313 warning("multi-character character constant");
2314 if (!is_long) {
2315 tokc.i = *(int8_t *)tokcstr.data;
2316 tok = TOK_CCHAR;
2317 } else {
2318 tokc.i = *(nwchar_t *)tokcstr.data;
2319 tok = TOK_LCHAR;
2321 } else {
2322 tokc.cstr = &tokcstr;
2323 if (!is_long)
2324 tok = TOK_STR;
2325 else
2326 tok = TOK_LSTR;
2329 break;
2331 case '<':
2332 PEEKC(c, p);
2333 if (c == '=') {
2334 p++;
2335 tok = TOK_LE;
2336 } else if (c == '<') {
2337 PEEKC(c, p);
2338 if (c == '=') {
2339 p++;
2340 tok = TOK_A_SHL;
2341 } else {
2342 tok = TOK_SHL;
2344 } else {
2345 tok = TOK_LT;
2347 break;
2349 case '>':
2350 PEEKC(c, p);
2351 if (c == '=') {
2352 p++;
2353 tok = TOK_GE;
2354 } else if (c == '>') {
2355 PEEKC(c, p);
2356 if (c == '=') {
2357 p++;
2358 tok = TOK_A_SAR;
2359 } else {
2360 tok = TOK_SAR;
2362 } else {
2363 tok = TOK_GT;
2365 break;
2367 case '&':
2368 PEEKC(c, p);
2369 if (c == '&') {
2370 p++;
2371 tok = TOK_LAND;
2372 } else if (c == '=') {
2373 p++;
2374 tok = TOK_A_AND;
2375 } else {
2376 tok = '&';
2378 break;
2380 case '|':
2381 PEEKC(c, p);
2382 if (c == '|') {
2383 p++;
2384 tok = TOK_LOR;
2385 } else if (c == '=') {
2386 p++;
2387 tok = TOK_A_OR;
2388 } else {
2389 tok = '|';
2391 break;
2393 case '+':
2394 PEEKC(c, p);
2395 if (c == '+') {
2396 p++;
2397 tok = TOK_INC;
2398 } else if (c == '=') {
2399 p++;
2400 tok = TOK_A_ADD;
2401 } else {
2402 tok = '+';
2404 break;
2406 case '-':
2407 PEEKC(c, p);
2408 if (c == '-') {
2409 p++;
2410 tok = TOK_DEC;
2411 } else if (c == '=') {
2412 p++;
2413 tok = TOK_A_SUB;
2414 } else if (c == '>') {
2415 p++;
2416 tok = TOK_ARROW;
2417 } else {
2418 tok = '-';
2420 break;
2422 PARSE2('!', '!', '=', TOK_NE)
2423 PARSE2('=', '=', '=', TOK_EQ)
2424 PARSE2('*', '*', '=', TOK_A_MUL)
2425 PARSE2('%', '%', '=', TOK_A_MOD)
2426 PARSE2('^', '^', '=', TOK_A_XOR)
2428 /* comments or operator */
2429 case '/':
2430 PEEKC(c, p);
2431 if (c == '*') {
2432 p = parse_comment(p);
2433 goto redo_no_start;
2434 } else if (c == '/') {
2435 p = parse_line_comment(p);
2436 goto redo_no_start;
2437 } else if (c == '=') {
2438 p++;
2439 tok = TOK_A_DIV;
2440 } else {
2441 tok = '/';
2443 break;
2445 /* simple tokens */
2446 case '(':
2447 case ')':
2448 case '[':
2449 case ']':
2450 case '{':
2451 case '}':
2452 case ',':
2453 case ';':
2454 case ':':
2455 case '?':
2456 case '~':
2457 case '$': /* only used in assembler */
2458 case '@': /* dito */
2459 tok = c;
2460 p++;
2461 break;
2462 default:
2463 error("unrecognized character \\x%02x", c);
2464 break;
2466 tok_flags = 0;
2467 keep_tok_flags:
2468 file->buf_ptr = p;
2469 #if defined(PARSE_DEBUG)
2470 printf("token = %s\n", get_tok_str(tok, &tokc));
2471 #endif
2474 /* return next token without macro substitution. Can read input from
2475 macro_ptr buffer */
2476 static void next_nomacro_spc(void)
2478 if (macro_ptr) {
2479 redo:
2480 tok = *macro_ptr;
2481 if (tok) {
2482 TOK_GET(tok, macro_ptr, tokc);
2483 if (tok == TOK_LINENUM) {
2484 file->line_num = tokc.i;
2485 goto redo;
2488 } else {
2489 next_nomacro1();
2493 ST_FUNC void next_nomacro(void)
2495 do {
2496 next_nomacro_spc();
2497 } while (is_space(tok));
2500 /* substitute args in macro_str and return allocated string */
2501 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
2503 int *st, last_tok, t, spc;
2504 Sym *s;
2505 CValue cval;
2506 TokenString str;
2507 CString cstr;
2509 tok_str_new(&str);
2510 last_tok = 0;
2511 while(1) {
2512 TOK_GET(t, macro_str, cval);
2513 if (!t)
2514 break;
2515 if (t == '#') {
2516 /* stringize */
2517 TOK_GET(t, macro_str, cval);
2518 if (!t)
2519 break;
2520 s = sym_find2(args, t);
2521 if (s) {
2522 cstr_new(&cstr);
2523 st = s->d;
2524 spc = 0;
2525 while (*st) {
2526 TOK_GET(t, st, cval);
2527 if (!check_space(t, &spc))
2528 cstr_cat(&cstr, get_tok_str(t, &cval));
2530 cstr.size -= spc;
2531 cstr_ccat(&cstr, '\0');
2532 #ifdef PP_DEBUG
2533 printf("stringize: %s\n", (char *)cstr.data);
2534 #endif
2535 /* add string */
2536 cval.cstr = &cstr;
2537 tok_str_add2(&str, TOK_STR, &cval);
2538 cstr_free(&cstr);
2539 } else {
2540 tok_str_add2(&str, t, &cval);
2542 } else if (t >= TOK_IDENT) {
2543 s = sym_find2(args, t);
2544 if (s) {
2545 st = s->d;
2546 /* if '##' is present before or after, no arg substitution */
2547 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2548 /* special case for var arg macros : ## eats the
2549 ',' if empty VA_ARGS variable. */
2550 /* XXX: test of the ',' is not 100%
2551 reliable. should fix it to avoid security
2552 problems */
2553 if (gnu_ext && s->type.t &&
2554 last_tok == TOK_TWOSHARPS &&
2555 str.len >= 2 && str.str[str.len - 2] == ',') {
2556 if (*st == 0) {
2557 /* suppress ',' '##' */
2558 str.len -= 2;
2559 } else {
2560 /* suppress '##' and add variable */
2561 str.len--;
2562 goto add_var;
2564 } else {
2565 int t1;
2566 add_var:
2567 for(;;) {
2568 TOK_GET(t1, st, cval);
2569 if (!t1)
2570 break;
2571 tok_str_add2(&str, t1, &cval);
2574 } else {
2575 /* NOTE: the stream cannot be read when macro
2576 substituing an argument */
2577 macro_subst(&str, nested_list, st, NULL);
2579 } else {
2580 tok_str_add(&str, t);
2582 } else {
2583 tok_str_add2(&str, t, &cval);
2585 last_tok = t;
2587 tok_str_add(&str, 0);
2588 return str.str;
2591 static char const ab_month_name[12][4] =
2593 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2594 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2597 /* do macro substitution of current token with macro 's' and add
2598 result to (tok_str,tok_len). 'nested_list' is the list of all
2599 macros we got inside to avoid recursing. Return non zero if no
2600 substitution needs to be done */
2601 static int macro_subst_tok(TokenString *tok_str,
2602 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2604 Sym *args, *sa, *sa1;
2605 int mstr_allocated, parlevel, *mstr, t, t1, *p, spc;
2606 TokenString str;
2607 char *cstrval;
2608 CValue cval;
2609 CString cstr;
2610 char buf[32];
2612 /* if symbol is a macro, prepare substitution */
2613 /* special macros */
2614 if (tok == TOK___LINE__) {
2615 snprintf(buf, sizeof(buf), "%d", file->line_num);
2616 cstrval = buf;
2617 t1 = TOK_PPNUM;
2618 goto add_cstr1;
2619 } else if (tok == TOK___FILE__) {
2620 cstrval = file->filename;
2621 goto add_cstr;
2622 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2623 time_t ti;
2624 struct tm *tm;
2626 time(&ti);
2627 tm = localtime(&ti);
2628 if (tok == TOK___DATE__) {
2629 snprintf(buf, sizeof(buf), "%s %2d %d",
2630 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2631 } else {
2632 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2633 tm->tm_hour, tm->tm_min, tm->tm_sec);
2635 cstrval = buf;
2636 add_cstr:
2637 t1 = TOK_STR;
2638 add_cstr1:
2639 cstr_new(&cstr);
2640 cstr_cat(&cstr, cstrval);
2641 cstr_ccat(&cstr, '\0');
2642 cval.cstr = &cstr;
2643 tok_str_add2(tok_str, t1, &cval);
2644 cstr_free(&cstr);
2645 } else {
2646 mstr = s->d;
2647 mstr_allocated = 0;
2648 if (s->type.t == MACRO_FUNC) {
2649 /* NOTE: we do not use next_nomacro to avoid eating the
2650 next token. XXX: find better solution */
2651 redo:
2652 if (macro_ptr) {
2653 p = macro_ptr;
2654 while (is_space(t = *p) || TOK_LINEFEED == t)
2655 ++p;
2656 if (t == 0 && can_read_stream) {
2657 /* end of macro stream: we must look at the token
2658 after in the file */
2659 struct macro_level *ml = *can_read_stream;
2660 macro_ptr = NULL;
2661 if (ml)
2663 macro_ptr = ml->p;
2664 ml->p = NULL;
2665 *can_read_stream = ml -> prev;
2667 goto redo;
2669 } else {
2670 /* XXX: incorrect with comments */
2671 ch = file->buf_ptr[0];
2672 while (is_space(ch) || ch == '\n')
2673 cinp();
2674 t = ch;
2676 if (t != '(') /* no macro subst */
2677 return -1;
2679 /* argument macro */
2680 next_nomacro();
2681 next_nomacro();
2682 args = NULL;
2683 sa = s->next;
2684 /* NOTE: empty args are allowed, except if no args */
2685 for(;;) {
2686 /* handle '()' case */
2687 if (!args && !sa && tok == ')')
2688 break;
2689 if (!sa)
2690 error("macro '%s' used with too many args",
2691 get_tok_str(s->v, 0));
2692 tok_str_new(&str);
2693 parlevel = spc = 0;
2694 /* NOTE: non zero sa->t indicates VA_ARGS */
2695 while ((parlevel > 0 ||
2696 (tok != ')' &&
2697 (tok != ',' || sa->type.t))) &&
2698 tok != -1) {
2699 if (tok == '(')
2700 parlevel++;
2701 else if (tok == ')')
2702 parlevel--;
2703 if (tok == TOK_LINEFEED)
2704 tok = ' ';
2705 if (!check_space(tok, &spc))
2706 tok_str_add2(&str, tok, &tokc);
2707 next_nomacro_spc();
2709 str.len -= spc;
2710 tok_str_add(&str, 0);
2711 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2712 sa1->d = str.str;
2713 sa = sa->next;
2714 if (tok == ')') {
2715 /* special case for gcc var args: add an empty
2716 var arg argument if it is omitted */
2717 if (sa && sa->type.t && gnu_ext)
2718 continue;
2719 else
2720 break;
2722 if (tok != ',')
2723 expect(",");
2724 next_nomacro();
2726 if (sa) {
2727 error("macro '%s' used with too few args",
2728 get_tok_str(s->v, 0));
2731 /* now subst each arg */
2732 mstr = macro_arg_subst(nested_list, mstr, args);
2733 /* free memory */
2734 sa = args;
2735 while (sa) {
2736 sa1 = sa->prev;
2737 tok_str_free(sa->d);
2738 sym_free(sa);
2739 sa = sa1;
2741 mstr_allocated = 1;
2743 sym_push2(nested_list, s->v, 0, 0);
2744 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2745 /* pop nested defined symbol */
2746 sa1 = *nested_list;
2747 *nested_list = sa1->prev;
2748 sym_free(sa1);
2749 if (mstr_allocated)
2750 tok_str_free(mstr);
2752 return 0;
2755 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2756 return the resulting string (which must be freed). */
2757 static inline int *macro_twosharps(const int *macro_str)
2759 const int *ptr;
2760 int t;
2761 CValue cval;
2762 TokenString macro_str1;
2763 CString cstr;
2764 char *p;
2765 int n;
2767 /* we search the first '##' */
2768 for(ptr = macro_str;;) {
2769 TOK_GET(t, ptr, cval);
2770 if (t == TOK_TWOSHARPS)
2771 break;
2772 /* nothing more to do if end of string */
2773 if (t == 0)
2774 return NULL;
2777 /* we saw '##', so we need more processing to handle it */
2778 tok_str_new(&macro_str1);
2779 for(ptr = macro_str;;) {
2780 TOK_GET(tok, ptr, tokc);
2781 if (tok == 0)
2782 break;
2783 if (tok == TOK_TWOSHARPS)
2784 continue;
2785 while (*ptr == TOK_TWOSHARPS) {
2786 t = *++ptr;
2787 if (t && t != TOK_TWOSHARPS) {
2788 TOK_GET(t, ptr, cval);
2790 /* We concatenate the two tokens */
2791 cstr_new(&cstr);
2792 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2793 n = cstr.size;
2794 cstr_cat(&cstr, get_tok_str(t, &cval));
2795 cstr_ccat(&cstr, '\0');
2797 p = file->buf_ptr;
2798 file->buf_ptr = cstr.data;
2799 for (;;) {
2800 next_nomacro1();
2801 if (0 == *file->buf_ptr)
2802 break;
2803 tok_str_add2(&macro_str1, tok, &tokc);
2805 warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2806 n, cstr.data, (char*)cstr.data + n);
2808 file->buf_ptr = p;
2809 cstr_reset(&cstr);
2812 tok_str_add2(&macro_str1, tok, &tokc);
2814 tok_str_add(&macro_str1, 0);
2815 return macro_str1.str;
2819 /* do macro substitution of macro_str and add result to
2820 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2821 inside to avoid recursing. */
2822 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2823 const int *macro_str, struct macro_level ** can_read_stream)
2825 Sym *s;
2826 int *macro_str1;
2827 const int *ptr;
2828 int t, ret, spc;
2829 CValue cval;
2830 struct macro_level ml;
2832 /* first scan for '##' operator handling */
2833 ptr = macro_str;
2834 macro_str1 = macro_twosharps(ptr);
2835 if (macro_str1)
2836 ptr = macro_str1;
2837 spc = 0;
2838 while (1) {
2839 /* NOTE: ptr == NULL can only happen if tokens are read from
2840 file stream due to a macro function call */
2841 if (ptr == NULL)
2842 break;
2843 TOK_GET(t, ptr, cval);
2844 if (t == 0)
2845 break;
2846 s = define_find(t);
2847 if (s != NULL) {
2848 /* if nested substitution, do nothing */
2849 if (sym_find2(*nested_list, t))
2850 goto no_subst;
2851 ml.p = macro_ptr;
2852 if (can_read_stream)
2853 ml.prev = *can_read_stream, *can_read_stream = &ml;
2854 macro_ptr = (int *)ptr;
2855 tok = t;
2856 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
2857 ptr = (int *)macro_ptr;
2858 macro_ptr = ml.p;
2859 if (can_read_stream && *can_read_stream == &ml)
2860 *can_read_stream = ml.prev;
2861 if (ret != 0)
2862 goto no_subst;
2863 } else {
2864 no_subst:
2865 if (!check_space(t, &spc))
2866 tok_str_add2(tok_str, t, &cval);
2869 if (macro_str1)
2870 tok_str_free(macro_str1);
2873 /* return next token with macro substitution */
2874 ST_FUNC void next(void)
2876 Sym *nested_list, *s;
2877 TokenString str;
2878 struct macro_level *ml;
2880 redo:
2881 if (parse_flags & PARSE_FLAG_SPACES)
2882 next_nomacro_spc();
2883 else
2884 next_nomacro();
2885 if (!macro_ptr) {
2886 /* if not reading from macro substituted string, then try
2887 to substitute macros */
2888 if (tok >= TOK_IDENT &&
2889 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2890 s = define_find(tok);
2891 if (s) {
2892 /* we have a macro: we try to substitute */
2893 tok_str_new(&str);
2894 nested_list = NULL;
2895 ml = NULL;
2896 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
2897 /* substitution done, NOTE: maybe empty */
2898 tok_str_add(&str, 0);
2899 macro_ptr = str.str;
2900 macro_ptr_allocated = str.str;
2901 goto redo;
2905 } else {
2906 if (tok == 0) {
2907 /* end of macro or end of unget buffer */
2908 if (unget_buffer_enabled) {
2909 macro_ptr = unget_saved_macro_ptr;
2910 unget_buffer_enabled = 0;
2911 } else {
2912 /* end of macro string: free it */
2913 tok_str_free(macro_ptr_allocated);
2914 macro_ptr_allocated = NULL;
2915 macro_ptr = NULL;
2917 goto redo;
2921 /* convert preprocessor tokens into C tokens */
2922 if (tok == TOK_PPNUM &&
2923 (parse_flags & PARSE_FLAG_TOK_NUM)) {
2924 parse_number((char *)tokc.cstr->data);
2928 /* push back current token and set current token to 'last_tok'. Only
2929 identifier case handled for labels. */
2930 ST_INLN void unget_tok(int last_tok)
2932 int i, n;
2933 int *q;
2934 unget_saved_macro_ptr = macro_ptr;
2935 unget_buffer_enabled = 1;
2936 q = unget_saved_buffer;
2937 macro_ptr = q;
2938 *q++ = tok;
2939 n = tok_ext_size(tok) - 1;
2940 for(i=0;i<n;i++)
2941 *q++ = tokc.tab[i];
2942 *q = 0; /* end of token string */
2943 tok = last_tok;
2947 /* better than nothing, but needs extension to handle '-E' option
2948 correctly too */
2949 ST_FUNC void preprocess_init(TCCState *s1)
2951 s1->include_stack_ptr = s1->include_stack;
2952 /* XXX: move that before to avoid having to initialize
2953 file->ifdef_stack_ptr ? */
2954 s1->ifdef_stack_ptr = s1->ifdef_stack;
2955 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2957 /* XXX: not ANSI compliant: bound checking says error */
2958 vtop = vstack - 1;
2959 s1->pack_stack[0] = 0;
2960 s1->pack_stack_ptr = s1->pack_stack;
2963 ST_FUNC void preprocess_new()
2965 int i, c;
2966 const char *p, *r;
2967 TokenSym *ts;
2969 /* init isid table */
2970 for(i=CH_EOF;i<256;i++)
2971 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
2973 /* add all tokens */
2974 table_ident = NULL;
2975 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
2977 tok_ident = TOK_IDENT;
2978 p = tcc_keywords;
2979 while (*p) {
2980 r = p;
2981 for(;;) {
2982 c = *r++;
2983 if (c == '\0')
2984 break;
2986 ts = tok_alloc(p, r - p - 1);
2987 p = r;
2991 /* Preprocess the current file */
2992 ST_FUNC int tcc_preprocess(TCCState *s1)
2994 Sym *define_start;
2996 BufferedFile *file_ref, **iptr, **iptr_new;
2997 int token_seen, line_ref, d;
2998 const char *s;
3000 preprocess_init(s1);
3001 define_start = define_stack;
3002 ch = file->buf_ptr[0];
3003 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3004 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3005 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3006 token_seen = 0;
3007 line_ref = 0;
3008 file_ref = NULL;
3009 iptr = s1->include_stack_ptr;
3011 for (;;) {
3012 next();
3013 if (tok == TOK_EOF) {
3014 break;
3015 } else if (file != file_ref) {
3016 goto print_line;
3017 } else if (tok == TOK_LINEFEED) {
3018 if (!token_seen)
3019 continue;
3020 ++line_ref;
3021 token_seen = 0;
3022 } else if (!token_seen) {
3023 d = file->line_num - line_ref;
3024 if (file != file_ref || d < 0 || d >= 8) {
3025 print_line:
3026 iptr_new = s1->include_stack_ptr;
3027 s = iptr_new > iptr ? " 1"
3028 : iptr_new < iptr ? " 2"
3029 : iptr_new > s1->include_stack ? " 3"
3030 : ""
3032 iptr = iptr_new;
3033 fprintf(s1->outfile, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
3034 } else {
3035 while (d)
3036 fputs("\n", s1->outfile), --d;
3038 line_ref = (file_ref = file)->line_num;
3039 token_seen = tok != TOK_LINEFEED;
3040 if (!token_seen)
3041 continue;
3043 fputs(get_tok_str(tok, &tokc), s1->outfile);
3045 free_defines(define_start);
3046 return 0;