fix-mixed-struct (patch by Pip Cet)
[tinycc.git] / tccpp.c
blob2936ef7c5a263dce57db11275d5c8354171087f8
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 ST_DATA int tok_flags;
27 ST_DATA int parse_flags;
29 ST_DATA struct BufferedFile *file;
30 ST_DATA int ch, tok;
31 ST_DATA CValue tokc;
32 ST_DATA const int *macro_ptr;
33 ST_DATA CString tokcstr; /* current parsed string, if any */
35 /* display benchmark infos */
36 ST_DATA int total_lines;
37 ST_DATA int total_bytes;
38 ST_DATA int tok_ident;
39 ST_DATA TokenSym **table_ident;
41 /* ------------------------------------------------------------------------- */
43 static TokenSym *hash_ident[TOK_HASH_SIZE];
44 static char token_buf[STRING_MAX_SIZE + 1];
45 static unsigned char isidnum_table[256 - CH_EOF];
46 /* isidnum_table flags: */
47 #define IS_SPC 1
48 #define IS_ID 2
49 #define IS_NUM 4
51 static TokenString *macro_stack;
53 static const char tcc_keywords[] =
54 #define DEF(id, str) str "\0"
55 #include "tcctok.h"
56 #undef DEF
59 /* WARNING: the content of this string encodes token numbers */
60 static const unsigned char tok_two_chars[] =
61 /* outdated -- gr
62 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
63 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
64 */{
65 '<','=', TOK_LE,
66 '>','=', TOK_GE,
67 '!','=', TOK_NE,
68 '&','&', TOK_LAND,
69 '|','|', TOK_LOR,
70 '+','+', TOK_INC,
71 '-','-', TOK_DEC,
72 '=','=', TOK_EQ,
73 '<','<', TOK_SHL,
74 '>','>', TOK_SAR,
75 '+','=', TOK_A_ADD,
76 '-','=', TOK_A_SUB,
77 '*','=', TOK_A_MUL,
78 '/','=', TOK_A_DIV,
79 '%','=', TOK_A_MOD,
80 '&','=', TOK_A_AND,
81 '^','=', TOK_A_XOR,
82 '|','=', TOK_A_OR,
83 '-','>', TOK_ARROW,
84 '.','.', 0xa8, // C++ token ?
85 '#','#', TOK_TWOSHARPS,
89 static void next_nomacro_spc(void);
91 ST_FUNC void skip(int c)
93 if (tok != c)
94 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
95 next();
98 ST_FUNC void expect(const char *msg)
100 tcc_error("%s expected", msg);
103 ST_FUNC void begin_macro(TokenString *str, int alloc)
105 str->alloc = alloc;
106 str->prev = macro_stack;
107 str->prev_ptr = macro_ptr;
108 macro_ptr = str->str;
109 macro_stack = str;
112 ST_FUNC void end_macro(void)
114 TokenString *str = macro_stack;
115 macro_stack = str->prev;
116 macro_ptr = str->prev_ptr;
117 if (str->alloc == 2) {
118 str->alloc = 3; /* just mark as finished */
119 } else {
120 tok_str_free(str->str);
121 if (str->alloc == 1)
122 tcc_free(str);
126 /* ------------------------------------------------------------------------- */
127 /* CString handling */
128 static void cstr_realloc(CString *cstr, int new_size)
130 int size;
131 void *data;
133 size = cstr->size_allocated;
134 if (size == 0)
135 size = 8; /* no need to allocate a too small first string */
136 while (size < new_size)
137 size = size * 2;
138 data = tcc_realloc(cstr->data_allocated, size);
139 cstr->data_allocated = data;
140 cstr->size_allocated = size;
141 cstr->data = data;
144 /* add a byte */
145 ST_FUNC void cstr_ccat(CString *cstr, int ch)
147 int size;
148 size = cstr->size + 1;
149 if (size > cstr->size_allocated)
150 cstr_realloc(cstr, size);
151 ((unsigned char *)cstr->data)[size - 1] = ch;
152 cstr->size = size;
155 ST_FUNC void cstr_cat(CString *cstr, const char *str)
157 int c;
158 for(;;) {
159 c = *str;
160 if (c == '\0')
161 break;
162 cstr_ccat(cstr, c);
163 str++;
167 /* add a wide char */
168 ST_FUNC void cstr_wccat(CString *cstr, int ch)
170 int size;
171 size = cstr->size + sizeof(nwchar_t);
172 if (size > cstr->size_allocated)
173 cstr_realloc(cstr, size);
174 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
175 cstr->size = size;
178 ST_FUNC void cstr_new(CString *cstr)
180 memset(cstr, 0, sizeof(CString));
183 /* free string and reset it to NULL */
184 ST_FUNC void cstr_free(CString *cstr)
186 tcc_free(cstr->data_allocated);
187 cstr_new(cstr);
190 /* reset string to empty */
191 ST_FUNC void cstr_reset(CString *cstr)
193 cstr->size = 0;
196 /* XXX: unicode ? */
197 static void add_char(CString *cstr, int c)
199 if (c == '\'' || c == '\"' || c == '\\') {
200 /* XXX: could be more precise if char or string */
201 cstr_ccat(cstr, '\\');
203 if (c >= 32 && c <= 126) {
204 cstr_ccat(cstr, c);
205 } else {
206 cstr_ccat(cstr, '\\');
207 if (c == '\n') {
208 cstr_ccat(cstr, 'n');
209 } else {
210 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
211 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
212 cstr_ccat(cstr, '0' + (c & 7));
217 /* ------------------------------------------------------------------------- */
218 /* allocate a new token */
219 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
221 TokenSym *ts, **ptable;
222 int i;
224 if (tok_ident >= SYM_FIRST_ANOM)
225 tcc_error("memory full (symbols)");
227 /* expand token table if needed */
228 i = tok_ident - TOK_IDENT;
229 if ((i % TOK_ALLOC_INCR) == 0) {
230 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
231 table_ident = ptable;
234 ts = tcc_malloc(sizeof(TokenSym) + len);
235 table_ident[i] = ts;
236 ts->tok = tok_ident++;
237 ts->sym_define = NULL;
238 ts->sym_label = NULL;
239 ts->sym_struct = NULL;
240 ts->sym_identifier = NULL;
241 ts->len = len;
242 ts->hash_next = NULL;
243 memcpy(ts->str, str, len);
244 ts->str[len] = '\0';
245 *pts = ts;
246 return ts;
249 #define TOK_HASH_INIT 1
250 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
252 /* find a token and add it if not found */
253 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
255 TokenSym *ts, **pts;
256 int i;
257 unsigned int h;
259 h = TOK_HASH_INIT;
260 for(i=0;i<len;i++)
261 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
262 h &= (TOK_HASH_SIZE - 1);
264 pts = &hash_ident[h];
265 for(;;) {
266 ts = *pts;
267 if (!ts)
268 break;
269 if (ts->len == len && !memcmp(ts->str, str, len))
270 return ts;
271 pts = &(ts->hash_next);
273 return tok_alloc_new(pts, str, len);
276 /* XXX: buffer overflow */
277 /* XXX: float tokens */
278 ST_FUNC const char *get_tok_str(int v, CValue *cv)
280 static char buf[STRING_MAX_SIZE + 1];
281 static CString cstr_buf;
282 CString *cstr;
283 char *p;
284 int i, len;
286 /* NOTE: to go faster, we give a fixed buffer for small strings */
287 cstr_reset(&cstr_buf);
288 cstr_buf.data = buf;
289 cstr_buf.size_allocated = sizeof(buf);
290 p = buf;
292 switch(v) {
293 case TOK_CINT:
294 case TOK_CUINT:
295 /* XXX: not quite exact, but only useful for testing */
296 sprintf(p, "%u", cv->ui);
297 break;
298 case TOK_CLLONG:
299 case TOK_CULLONG:
300 /* XXX: not quite exact, but only useful for testing */
301 #ifdef _WIN32
302 sprintf(p, "%u", (unsigned)cv->ull);
303 #else
304 sprintf(p, "%llu", cv->ull);
305 #endif
306 break;
307 case TOK_LCHAR:
308 cstr_ccat(&cstr_buf, 'L');
309 case TOK_CCHAR:
310 cstr_ccat(&cstr_buf, '\'');
311 add_char(&cstr_buf, cv->i);
312 cstr_ccat(&cstr_buf, '\'');
313 cstr_ccat(&cstr_buf, '\0');
314 break;
315 case TOK_PPNUM:
316 case TOK_PPSTR:
317 return (char*)cv->cstr->data;
318 case TOK_LSTR:
319 cstr_ccat(&cstr_buf, 'L');
320 case TOK_STR:
321 cstr = cv->cstr;
322 cstr_ccat(&cstr_buf, '\"');
323 if (v == TOK_STR) {
324 len = cstr->size - 1;
325 for(i=0;i<len;i++)
326 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
327 } else {
328 len = (cstr->size / sizeof(nwchar_t)) - 1;
329 for(i=0;i<len;i++)
330 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
332 cstr_ccat(&cstr_buf, '\"');
333 cstr_ccat(&cstr_buf, '\0');
334 break;
336 case TOK_CFLOAT:
337 case TOK_CDOUBLE:
338 case TOK_CLDOUBLE:
339 case TOK_LINENUM:
340 return NULL; /* should not happen */
342 /* above tokens have value, the ones below don't */
344 case TOK_LT:
345 v = '<';
346 goto addv;
347 case TOK_GT:
348 v = '>';
349 goto addv;
350 case TOK_DOTS:
351 return strcpy(p, "...");
352 case TOK_A_SHL:
353 return strcpy(p, "<<=");
354 case TOK_A_SAR:
355 return strcpy(p, ">>=");
356 default:
357 if (v < TOK_IDENT) {
358 /* search in two bytes table */
359 const unsigned char *q = tok_two_chars;
360 while (*q) {
361 if (q[2] == v) {
362 *p++ = q[0];
363 *p++ = q[1];
364 *p = '\0';
365 return buf;
367 q += 3;
369 if (v >= 127) {
370 sprintf(buf, "<%02x>", v);
371 return buf;
373 addv:
374 *p++ = v;
375 *p = '\0';
376 } else if (v < tok_ident) {
377 return table_ident[v - TOK_IDENT]->str;
378 } else if (v >= SYM_FIRST_ANOM) {
379 /* special name for anonymous symbol */
380 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
381 } else {
382 /* should never happen */
383 return NULL;
385 break;
387 return cstr_buf.data;
390 /* return the current character, handling end of block if necessary
391 (but not stray) */
392 ST_FUNC int handle_eob(void)
394 BufferedFile *bf = file;
395 int len;
397 /* only tries to read if really end of buffer */
398 if (bf->buf_ptr >= bf->buf_end) {
399 if (bf->fd != -1) {
400 #if defined(PARSE_DEBUG)
401 len = 1;
402 #else
403 len = IO_BUF_SIZE;
404 #endif
405 len = read(bf->fd, bf->buffer, len);
406 if (len < 0)
407 len = 0;
408 } else {
409 len = 0;
411 total_bytes += len;
412 bf->buf_ptr = bf->buffer;
413 bf->buf_end = bf->buffer + len;
414 *bf->buf_end = CH_EOB;
416 if (bf->buf_ptr < bf->buf_end) {
417 return bf->buf_ptr[0];
418 } else {
419 bf->buf_ptr = bf->buf_end;
420 return CH_EOF;
424 /* read next char from current input file and handle end of input buffer */
425 ST_INLN void inp(void)
427 ch = *(++(file->buf_ptr));
428 /* end of buffer/file handling */
429 if (ch == CH_EOB)
430 ch = handle_eob();
433 /* handle '\[\r]\n' */
434 static int handle_stray_noerror(void)
436 while (ch == '\\') {
437 inp();
438 if (ch == '\n') {
439 file->line_num++;
440 inp();
441 } else if (ch == '\r') {
442 inp();
443 if (ch != '\n')
444 goto fail;
445 file->line_num++;
446 inp();
447 } else {
448 fail:
449 return 1;
452 return 0;
455 static void handle_stray(void)
457 if (handle_stray_noerror())
458 tcc_error("stray '\\' in program");
461 /* skip the stray and handle the \\n case. Output an error if
462 incorrect char after the stray */
463 static int handle_stray1(uint8_t *p)
465 int c;
467 file->buf_ptr = p;
468 if (p >= file->buf_end) {
469 c = handle_eob();
470 if (c != '\\')
471 return c;
472 p = file->buf_ptr;
474 ch = *p;
475 if (handle_stray_noerror()) {
476 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
477 tcc_error("stray '\\' in program");
478 *--file->buf_ptr = '\\';
480 p = file->buf_ptr;
481 c = *p;
482 return c;
485 /* handle just the EOB case, but not stray */
486 #define PEEKC_EOB(c, p)\
488 p++;\
489 c = *p;\
490 if (c == '\\') {\
491 file->buf_ptr = p;\
492 c = handle_eob();\
493 p = file->buf_ptr;\
497 /* handle the complicated stray case */
498 #define PEEKC(c, p)\
500 p++;\
501 c = *p;\
502 if (c == '\\') {\
503 c = handle_stray1(p);\
504 p = file->buf_ptr;\
508 /* input with '\[\r]\n' handling. Note that this function cannot
509 handle other characters after '\', so you cannot call it inside
510 strings or comments */
511 ST_FUNC void minp(void)
513 inp();
514 if (ch == '\\')
515 handle_stray();
519 /* single line C++ comments */
520 static uint8_t *parse_line_comment(uint8_t *p)
522 int c;
524 p++;
525 for(;;) {
526 c = *p;
527 redo:
528 if (c == '\n' || c == CH_EOF) {
529 break;
530 } else if (c == '\\') {
531 file->buf_ptr = p;
532 c = handle_eob();
533 p = file->buf_ptr;
534 if (c == '\\') {
535 PEEKC_EOB(c, p);
536 if (c == '\n') {
537 file->line_num++;
538 PEEKC_EOB(c, p);
539 } else if (c == '\r') {
540 PEEKC_EOB(c, p);
541 if (c == '\n') {
542 file->line_num++;
543 PEEKC_EOB(c, p);
546 } else {
547 goto redo;
549 } else {
550 p++;
553 return p;
556 /* C comments */
557 ST_FUNC uint8_t *parse_comment(uint8_t *p)
559 int c;
561 p++;
562 for(;;) {
563 /* fast skip loop */
564 for(;;) {
565 c = *p;
566 if (c == '\n' || c == '*' || c == '\\')
567 break;
568 p++;
569 c = *p;
570 if (c == '\n' || c == '*' || c == '\\')
571 break;
572 p++;
574 /* now we can handle all the cases */
575 if (c == '\n') {
576 file->line_num++;
577 p++;
578 } else if (c == '*') {
579 p++;
580 for(;;) {
581 c = *p;
582 if (c == '*') {
583 p++;
584 } else if (c == '/') {
585 goto end_of_comment;
586 } else if (c == '\\') {
587 file->buf_ptr = p;
588 c = handle_eob();
589 p = file->buf_ptr;
590 if (c == '\\') {
591 /* skip '\[\r]\n', otherwise just skip the stray */
592 while (c == '\\') {
593 PEEKC_EOB(c, p);
594 if (c == '\n') {
595 file->line_num++;
596 PEEKC_EOB(c, p);
597 } else if (c == '\r') {
598 PEEKC_EOB(c, p);
599 if (c == '\n') {
600 file->line_num++;
601 PEEKC_EOB(c, p);
603 } else {
604 goto after_star;
608 } else {
609 break;
612 after_star: ;
613 } else {
614 /* stray, eob or eof */
615 file->buf_ptr = p;
616 c = handle_eob();
617 p = file->buf_ptr;
618 if (c == CH_EOF) {
619 tcc_error("unexpected end of file in comment");
620 } else if (c == '\\') {
621 p++;
625 end_of_comment:
626 p++;
627 return p;
630 #define cinp minp
632 static inline void skip_spaces(void)
634 while (isidnum_table[ch - CH_EOF] & IS_SPC)
635 cinp();
638 static inline int check_space(int t, int *spc)
640 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
641 if (*spc)
642 return 1;
643 *spc = 1;
644 } else
645 *spc = 0;
646 return 0;
649 /* parse a string without interpreting escapes */
650 static uint8_t *parse_pp_string(uint8_t *p,
651 int sep, CString *str)
653 int c;
654 p++;
655 for(;;) {
656 c = *p;
657 if (c == sep) {
658 break;
659 } else if (c == '\\') {
660 file->buf_ptr = p;
661 c = handle_eob();
662 p = file->buf_ptr;
663 if (c == CH_EOF) {
664 unterminated_string:
665 /* XXX: indicate line number of start of string */
666 tcc_error("missing terminating %c character", sep);
667 } else if (c == '\\') {
668 /* escape : just skip \[\r]\n */
669 PEEKC_EOB(c, p);
670 if (c == '\n') {
671 file->line_num++;
672 p++;
673 } else if (c == '\r') {
674 PEEKC_EOB(c, p);
675 if (c != '\n')
676 expect("'\n' after '\r'");
677 file->line_num++;
678 p++;
679 } else if (c == CH_EOF) {
680 goto unterminated_string;
681 } else {
682 if (str) {
683 cstr_ccat(str, '\\');
684 cstr_ccat(str, c);
686 p++;
689 } else if (c == '\n') {
690 file->line_num++;
691 goto add_char;
692 } else if (c == '\r') {
693 PEEKC_EOB(c, p);
694 if (c != '\n') {
695 if (str)
696 cstr_ccat(str, '\r');
697 } else {
698 file->line_num++;
699 goto add_char;
701 } else {
702 add_char:
703 if (str)
704 cstr_ccat(str, c);
705 p++;
708 p++;
709 return p;
712 /* skip block of text until #else, #elif or #endif. skip also pairs of
713 #if/#endif */
714 static void preprocess_skip(void)
716 int a, start_of_line, c, in_warn_or_error;
717 uint8_t *p;
719 p = file->buf_ptr;
720 a = 0;
721 redo_start:
722 start_of_line = 1;
723 in_warn_or_error = 0;
724 for(;;) {
725 redo_no_start:
726 c = *p;
727 switch(c) {
728 case ' ':
729 case '\t':
730 case '\f':
731 case '\v':
732 case '\r':
733 p++;
734 goto redo_no_start;
735 case '\n':
736 file->line_num++;
737 p++;
738 goto redo_start;
739 case '\\':
740 file->buf_ptr = p;
741 c = handle_eob();
742 if (c == CH_EOF) {
743 expect("#endif");
744 } else if (c == '\\') {
745 ch = file->buf_ptr[0];
746 handle_stray_noerror();
748 p = file->buf_ptr;
749 goto redo_no_start;
750 /* skip strings */
751 case '\"':
752 case '\'':
753 if (in_warn_or_error)
754 goto _default;
755 p = parse_pp_string(p, c, NULL);
756 break;
757 /* skip comments */
758 case '/':
759 if (in_warn_or_error)
760 goto _default;
761 file->buf_ptr = p;
762 ch = *p;
763 minp();
764 p = file->buf_ptr;
765 if (ch == '*') {
766 p = parse_comment(p);
767 } else if (ch == '/') {
768 p = parse_line_comment(p);
770 break;
771 case '#':
772 p++;
773 if (start_of_line) {
774 file->buf_ptr = p;
775 next_nomacro();
776 p = file->buf_ptr;
777 if (a == 0 &&
778 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
779 goto the_end;
780 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
781 a++;
782 else if (tok == TOK_ENDIF)
783 a--;
784 else if( tok == TOK_ERROR || tok == TOK_WARNING)
785 in_warn_or_error = 1;
786 else if (tok == TOK_LINEFEED)
787 goto redo_start;
788 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
789 p = parse_line_comment(p);
790 break;
791 _default:
792 default:
793 p++;
794 break;
796 start_of_line = 0;
798 the_end: ;
799 file->buf_ptr = p;
802 /* ParseState handling */
804 /* XXX: currently, no include file info is stored. Thus, we cannot display
805 accurate messages if the function or data definition spans multiple
806 files */
808 /* save current parse state in 's' */
809 ST_FUNC void save_parse_state(ParseState *s)
811 s->line_num = file->line_num;
812 s->macro_ptr = macro_ptr;
813 s->tok = tok;
814 s->tokc = tokc;
817 /* restore parse state from 's' */
818 ST_FUNC void restore_parse_state(ParseState *s)
820 file->line_num = s->line_num;
821 macro_ptr = s->macro_ptr;
822 tok = s->tok;
823 tokc = s->tokc;
826 /* return the number of additional 'ints' necessary to store the
827 token */
828 static inline int tok_size(const int *p)
830 switch(*p) {
831 /* 4 bytes */
832 case TOK_CINT:
833 case TOK_CUINT:
834 case TOK_CCHAR:
835 case TOK_LCHAR:
836 case TOK_CFLOAT:
837 case TOK_LINENUM:
838 return 1 + 1;
839 case TOK_STR:
840 case TOK_LSTR:
841 case TOK_PPNUM:
842 case TOK_PPSTR:
843 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
844 case TOK_CDOUBLE:
845 case TOK_CLLONG:
846 case TOK_CULLONG:
847 return 1 + 2;
848 case TOK_CLDOUBLE:
849 return 1 + LDOUBLE_SIZE / 4;
850 default:
851 return 1 + 0;
855 /* token string handling */
857 ST_INLN void tok_str_new(TokenString *s)
859 s->str = NULL;
860 s->len = 0;
861 s->allocated_len = 0;
862 s->last_line_num = -1;
865 ST_FUNC void tok_str_free(int *str)
867 tcc_free(str);
870 static int *tok_str_realloc(TokenString *s)
872 int *str, len;
874 if (s->allocated_len == 0) {
875 len = 8;
876 } else {
877 len = s->allocated_len * 2;
879 str = tcc_realloc(s->str, len * sizeof(int));
880 s->allocated_len = len;
881 s->str = str;
882 return str;
885 ST_FUNC void tok_str_add(TokenString *s, int t)
887 int len, *str;
889 len = s->len;
890 str = s->str;
891 if (len >= s->allocated_len)
892 str = tok_str_realloc(s);
893 str[len++] = t;
894 s->len = len;
897 static void tok_str_add2(TokenString *s, int t, CValue *cv)
899 int len, *str;
901 len = s->len;
902 str = s->str;
904 /* allocate space for worst case */
905 if (len + TOK_MAX_SIZE > s->allocated_len)
906 str = tok_str_realloc(s);
907 str[len++] = t;
908 switch(t) {
909 case TOK_CINT:
910 case TOK_CUINT:
911 case TOK_CCHAR:
912 case TOK_LCHAR:
913 case TOK_CFLOAT:
914 case TOK_LINENUM:
915 str[len++] = cv->tab[0];
916 break;
917 case TOK_PPNUM:
918 case TOK_PPSTR:
919 case TOK_STR:
920 case TOK_LSTR:
922 int nb_words;
923 CString *cstr;
925 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
926 while ((len + nb_words) > s->allocated_len)
927 str = tok_str_realloc(s);
928 cstr = (CString *)(str + len);
929 cstr->data = NULL;
930 cstr->size = cv->cstr->size;
931 cstr->data_allocated = NULL;
932 cstr->size_allocated = cstr->size;
933 memcpy((char *)cstr + sizeof(CString),
934 cv->cstr->data, cstr->size);
935 len += nb_words;
937 break;
938 case TOK_CDOUBLE:
939 case TOK_CLLONG:
940 case TOK_CULLONG:
941 #if LDOUBLE_SIZE == 8
942 case TOK_CLDOUBLE:
943 #endif
944 str[len++] = cv->tab[0];
945 str[len++] = cv->tab[1];
946 break;
947 #if LDOUBLE_SIZE == 12
948 case TOK_CLDOUBLE:
949 str[len++] = cv->tab[0];
950 str[len++] = cv->tab[1];
951 str[len++] = cv->tab[2];
952 #elif LDOUBLE_SIZE == 16
953 case TOK_CLDOUBLE:
954 str[len++] = cv->tab[0];
955 str[len++] = cv->tab[1];
956 str[len++] = cv->tab[2];
957 str[len++] = cv->tab[3];
958 #elif LDOUBLE_SIZE != 8
959 #error add long double size support
960 #endif
961 break;
962 default:
963 break;
965 s->len = len;
968 /* add the current parse token in token string 's' */
969 ST_FUNC void tok_str_add_tok(TokenString *s)
971 CValue cval;
973 /* save line number info */
974 if (file->line_num != s->last_line_num) {
975 s->last_line_num = file->line_num;
976 cval.i = s->last_line_num;
977 tok_str_add2(s, TOK_LINENUM, &cval);
979 tok_str_add2(s, tok, &tokc);
982 /* get a token from an integer array and increment pointer
983 accordingly. we code it as a macro to avoid pointer aliasing. */
984 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
986 const int *p = *pp;
987 int n, *tab;
989 tab = cv->tab;
990 switch(*t = *p++) {
991 case TOK_CINT:
992 case TOK_CUINT:
993 case TOK_CCHAR:
994 case TOK_LCHAR:
995 case TOK_CFLOAT:
996 case TOK_LINENUM:
997 tab[0] = *p++;
998 break;
999 case TOK_STR:
1000 case TOK_LSTR:
1001 case TOK_PPNUM:
1002 case TOK_PPSTR:
1003 cv->cstr = (CString *)p;
1004 cv->cstr->data = (char *)p + sizeof(CString);
1005 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1006 break;
1007 case TOK_CDOUBLE:
1008 case TOK_CLLONG:
1009 case TOK_CULLONG:
1010 n = 2;
1011 goto copy;
1012 case TOK_CLDOUBLE:
1013 #if LDOUBLE_SIZE == 16
1014 n = 4;
1015 #elif LDOUBLE_SIZE == 12
1016 n = 3;
1017 #elif LDOUBLE_SIZE == 8
1018 n = 2;
1019 #else
1020 # error add long double size support
1021 #endif
1022 copy:
1024 *tab++ = *p++;
1025 while (--n);
1026 break;
1027 default:
1028 break;
1030 *pp = p;
1033 static int macro_is_equal(const int *a, const int *b)
1035 char buf[STRING_MAX_SIZE + 1];
1036 CValue cv;
1037 int t;
1038 while (*a && *b) {
1039 TOK_GET(&t, &a, &cv);
1040 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1041 TOK_GET(&t, &b, &cv);
1042 if (strcmp(buf, get_tok_str(t, &cv)))
1043 return 0;
1045 return !(*a || *b);
1048 static void pp_line(TCCState *s1, BufferedFile *f, int level)
1050 int d = f->line_num - f->line_ref;
1051 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE
1052 || (level == 0 && f->line_ref && d < 8))
1054 while (d > 0)
1055 fputs("\n", s1->ppfp), --d;
1057 else
1058 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
1059 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
1061 else {
1062 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
1063 level > 0 ? " 1" : level < 0 ? " 2" : "");
1065 f->line_ref = f->line_num;
1068 static void tok_print(const char *msg, const int *str)
1070 FILE *pr = tcc_state->ppfp;
1071 int t;
1072 CValue cval;
1074 fprintf(pr, "%s ", msg);
1075 while (str) {
1076 TOK_GET(&t, &str, &cval);
1077 if (!t)
1078 break;
1079 fprintf(pr,"%s", get_tok_str(t, &cval));
1081 fprintf(pr, "\n");
1084 static int define_print_prepared(Sym *s)
1086 if (!s || !tcc_state->ppfp || tcc_state->dflag == 0)
1087 return 0;
1089 if (s->v < TOK_IDENT || s->v >= tok_ident)
1090 return 0;
1092 if (file) {
1093 file->line_num--;
1094 pp_line(tcc_state, file, 0);
1095 file->line_ref = ++file->line_num;
1097 return 1;
1100 static void define_print(int v)
1102 FILE *pr = tcc_state->ppfp;
1103 Sym *s, *a;
1105 s = define_find(v);
1106 if (define_print_prepared(s) == 0)
1107 return;
1109 fprintf(pr, "// #define %s", get_tok_str(v, NULL));
1110 if (s->type.t == MACRO_FUNC) {
1111 a = s->next;
1112 fprintf(pr,"(");
1113 if (a)
1114 for (;;) {
1115 fprintf(pr,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
1116 if (!(a = a->next))
1117 break;
1118 fprintf(pr,",");
1120 fprintf(pr,")");
1122 tok_print("", s->d);
1125 static void undef_print(int v)
1127 FILE *pr = tcc_state->ppfp;
1128 Sym *s;
1130 s = define_find(v);
1131 if (define_print_prepared(s) == 0)
1132 return;
1134 fprintf(pr, "// #undef %s\n", get_tok_str(s->v, NULL));
1137 ST_FUNC void print_defines(void)
1139 Sym *top = define_stack;
1140 while (top) {
1141 define_print(top->v);
1142 top = top->prev;
1146 /* defines handling */
1147 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1149 Sym *s;
1151 s = define_find(v);
1152 if (s && !macro_is_equal(s->d, str))
1153 tcc_warning("%s redefined", get_tok_str(v, NULL));
1155 s = sym_push2(&define_stack, v, macro_type, 0);
1156 s->d = str;
1157 s->next = first_arg;
1158 table_ident[v - TOK_IDENT]->sym_define = s;
1161 /* undefined a define symbol. Its name is just set to zero */
1162 ST_FUNC void define_undef(Sym *s)
1164 int v = s->v;
1165 undef_print(v);
1166 if (v >= TOK_IDENT && v < tok_ident)
1167 table_ident[v - TOK_IDENT]->sym_define = NULL;
1170 ST_INLN Sym *define_find(int v)
1172 v -= TOK_IDENT;
1173 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1174 return NULL;
1175 return table_ident[v]->sym_define;
1178 /* free define stack until top reaches 'b' */
1179 ST_FUNC void free_defines(Sym *b)
1181 Sym *top, *top1;
1182 int v;
1184 top = define_stack;
1185 while (top != b) {
1186 top1 = top->prev;
1187 /* do not free args or predefined defines */
1188 if (top->d)
1189 tok_str_free(top->d);
1190 v = top->v;
1191 if (v >= TOK_IDENT && v < tok_ident)
1192 table_ident[v - TOK_IDENT]->sym_define = NULL;
1193 sym_free(top);
1194 top = top1;
1196 define_stack = b;
1199 /* label lookup */
1200 ST_FUNC Sym *label_find(int v)
1202 v -= TOK_IDENT;
1203 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1204 return NULL;
1205 return table_ident[v]->sym_label;
1208 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1210 Sym *s, **ps;
1211 s = sym_push2(ptop, v, 0, 0);
1212 s->r = flags;
1213 ps = &table_ident[v - TOK_IDENT]->sym_label;
1214 if (ptop == &global_label_stack) {
1215 /* modify the top most local identifier, so that
1216 sym_identifier will point to 's' when popped */
1217 while (*ps != NULL)
1218 ps = &(*ps)->prev_tok;
1220 s->prev_tok = *ps;
1221 *ps = s;
1222 return s;
1225 /* pop labels until element last is reached. Look if any labels are
1226 undefined. Define symbols if '&&label' was used. */
1227 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1229 Sym *s, *s1;
1230 for(s = *ptop; s != slast; s = s1) {
1231 s1 = s->prev;
1232 if (s->r == LABEL_DECLARED) {
1233 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1234 } else if (s->r == LABEL_FORWARD) {
1235 tcc_error("label '%s' used but not defined",
1236 get_tok_str(s->v, NULL));
1237 } else {
1238 if (s->c) {
1239 /* define corresponding symbol. A size of
1240 1 is put. */
1241 put_extern_sym(s, cur_text_section, s->jnext, 1);
1244 /* remove label */
1245 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1246 sym_free(s);
1248 *ptop = slast;
1251 /* eval an expression for #if/#elif */
1252 static int expr_preprocess(void)
1254 int c, t;
1255 TokenString str;
1257 tok_str_new(&str);
1258 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1259 next(); /* do macro subst */
1260 if (tok == TOK_DEFINED) {
1261 next_nomacro();
1262 t = tok;
1263 if (t == '(')
1264 next_nomacro();
1265 c = define_find(tok) != 0;
1266 if (t == '(')
1267 next_nomacro();
1268 tok = TOK_CINT;
1269 tokc.i = c;
1270 } else if (tok >= TOK_IDENT) {
1271 /* if undefined macro */
1272 tok = TOK_CINT;
1273 tokc.i = 0;
1275 tok_str_add_tok(&str);
1277 tok_str_add(&str, -1); /* simulate end of file */
1278 tok_str_add(&str, 0);
1279 /* now evaluate C constant expression */
1280 begin_macro(&str, 0);
1281 next();
1282 c = expr_const();
1283 end_macro();
1284 return c != 0;
1288 /* parse after #define */
1289 ST_FUNC void parse_define(void)
1291 Sym *s, *first, **ps;
1292 int v, t, varg, is_vaargs, spc;
1293 int saved_parse_flags = parse_flags;
1294 TokenString str;
1296 v = tok;
1297 if (v < TOK_IDENT)
1298 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1299 /* XXX: should check if same macro (ANSI) */
1300 first = NULL;
1301 t = MACRO_OBJ;
1302 /* '(' must be just after macro definition for MACRO_FUNC */
1303 parse_flags |= PARSE_FLAG_SPACES;
1304 next_nomacro_spc();
1305 if (tok == '(') {
1306 next_nomacro();
1307 ps = &first;
1308 if (tok != ')') for (;;) {
1309 varg = tok;
1310 next_nomacro();
1311 is_vaargs = 0;
1312 if (varg == TOK_DOTS) {
1313 varg = TOK___VA_ARGS__;
1314 is_vaargs = 1;
1315 } else if (tok == TOK_DOTS && gnu_ext) {
1316 is_vaargs = 1;
1317 next_nomacro();
1319 if (varg < TOK_IDENT)
1320 bad_list:
1321 tcc_error("bad macro parameter list");
1322 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1323 *ps = s;
1324 ps = &s->next;
1325 if (tok == ')')
1326 break;
1327 if (tok != ',' || is_vaargs)
1328 goto bad_list;
1329 next_nomacro();
1331 next_nomacro_spc();
1332 t = MACRO_FUNC;
1334 tok_str_new(&str);
1335 spc = 2;
1336 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1337 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1338 /* remove spaces around ## and after '#' */
1339 if (TOK_TWOSHARPS == tok) {
1340 if (2 == spc)
1341 goto bad_twosharp;
1342 if (1 == spc)
1343 --str.len;
1344 spc = 3;
1345 } else if ('#' == tok) {
1346 spc = 4;
1347 } else if (check_space(tok, &spc)) {
1348 goto skip;
1350 tok_str_add2(&str, tok, &tokc);
1351 skip:
1352 next_nomacro_spc();
1355 parse_flags = saved_parse_flags;
1356 if (spc == 1)
1357 --str.len; /* remove trailing space */
1358 tok_str_add(&str, 0);
1359 if (3 == spc)
1360 bad_twosharp:
1361 tcc_error("'##' cannot appear at either end of macro");
1362 define_push(v, t, str.str, first);
1363 define_print(v);
1366 static inline int hash_cached_include(const char *filename)
1368 const unsigned char *s;
1369 unsigned int h;
1371 h = TOK_HASH_INIT;
1372 s = (unsigned char *) filename;
1373 while (*s) {
1374 h = TOK_HASH_FUNC(h, *s);
1375 s++;
1377 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1378 return h;
1381 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1383 CachedInclude *e;
1384 int i, h;
1385 h = hash_cached_include(filename);
1386 i = s1->cached_includes_hash[h];
1387 for(;;) {
1388 if (i == 0)
1389 break;
1390 e = s1->cached_includes[i - 1];
1391 if (0 == PATHCMP(e->filename, filename))
1392 return e;
1393 i = e->hash_next;
1395 return NULL;
1398 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1400 CachedInclude *e;
1401 int h;
1403 if (search_cached_include(s1, filename))
1404 return;
1405 #ifdef INC_DEBUG
1406 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1407 #endif
1408 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1409 strcpy(e->filename, filename);
1410 e->ifndef_macro = ifndef_macro;
1411 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1412 /* add in hash table */
1413 h = hash_cached_include(filename);
1414 e->hash_next = s1->cached_includes_hash[h];
1415 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1418 static void pragma_parse(TCCState *s1)
1420 next_nomacro();
1421 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1422 int t = tok, v;
1423 Sym *s;
1425 if (next(), tok != '(')
1426 goto pragma_err;
1427 if (next(), tok != TOK_STR)
1428 goto pragma_err;
1429 v = tok_alloc(tokc.cstr->data, tokc.cstr->size - 1)->tok;
1430 if (next(), tok != ')')
1431 goto pragma_err;
1432 if (t == TOK_push_macro) {
1433 while (NULL == (s = define_find(v)))
1434 define_push(v, 0, NULL, NULL);
1435 s->type.ref = s; /* set push boundary */
1436 } else {
1437 for (s = define_stack; s; s = s->prev)
1438 if (s->v == v && s->type.ref == s) {
1439 s->type.ref = NULL;
1440 break;
1443 if (s)
1444 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1445 else
1446 tcc_warning("unbalanced #pragma pop_macro");
1448 } else if (tok == TOK_once) {
1449 add_cached_include(s1, file->filename, TOK_once);
1451 } else if (s1->ppfp) {
1452 /* tcc -E: keep pragmas below unchanged */
1453 unget_tok(' ');
1454 unget_tok(TOK_PRAGMA);
1455 unget_tok('#');
1456 unget_tok(TOK_LINEFEED);
1458 } else if (tok == TOK_pack) {
1459 /* This may be:
1460 #pragma pack(1) // set
1461 #pragma pack() // reset to default
1462 #pragma pack(push,1) // push & set
1463 #pragma pack(pop) // restore previous */
1464 next();
1465 skip('(');
1466 if (tok == TOK_ASM_pop) {
1467 next();
1468 if (s1->pack_stack_ptr <= s1->pack_stack) {
1469 stk_error:
1470 tcc_error("out of pack stack");
1472 s1->pack_stack_ptr--;
1473 } else {
1474 int val = 0;
1475 if (tok != ')') {
1476 if (tok == TOK_ASM_push) {
1477 next();
1478 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1479 goto stk_error;
1480 s1->pack_stack_ptr++;
1481 skip(',');
1483 if (tok != TOK_CINT)
1484 goto pragma_err;
1485 val = tokc.i;
1486 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1487 goto pragma_err;
1488 next();
1490 *s1->pack_stack_ptr = val;
1492 if (tok != ')')
1493 goto pragma_err;
1495 } else if (tok == TOK_comment) {
1496 char *file;
1497 next();
1498 skip('(');
1499 if (tok != TOK_lib)
1500 goto pragma_warn;
1501 next();
1502 skip(',');
1503 if (tok != TOK_STR)
1504 goto pragma_err;
1505 file = tcc_strdup((char *)tokc.cstr->data);
1506 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1507 next();
1508 if (tok != ')')
1509 goto pragma_err;
1510 } else {
1511 pragma_warn:
1512 if (s1->warn_unsupported)
1513 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1515 return;
1517 pragma_err:
1518 tcc_error("malformed #pragma directive");
1519 return;
1522 /* is_bof is true if first non space token at beginning of file */
1523 ST_FUNC void preprocess(int is_bof)
1525 TCCState *s1 = tcc_state;
1526 int i, c, n, saved_parse_flags;
1527 char buf[1024], *q;
1528 Sym *s;
1530 saved_parse_flags = parse_flags;
1531 parse_flags = PARSE_FLAG_PREPROCESS
1532 | PARSE_FLAG_TOK_NUM
1533 | PARSE_FLAG_TOK_STR
1534 | PARSE_FLAG_LINEFEED
1535 | (parse_flags & PARSE_FLAG_ASM_FILE)
1538 next_nomacro();
1539 redo:
1540 switch(tok) {
1541 case TOK_DEFINE:
1542 next_nomacro();
1543 parse_define();
1544 break;
1545 case TOK_UNDEF:
1546 next_nomacro();
1547 s = define_find(tok);
1548 /* undefine symbol by putting an invalid name */
1549 if (s)
1550 define_undef(s);
1551 break;
1552 case TOK_INCLUDE:
1553 case TOK_INCLUDE_NEXT:
1554 ch = file->buf_ptr[0];
1555 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1556 skip_spaces();
1557 if (ch == '<') {
1558 c = '>';
1559 goto read_name;
1560 } else if (ch == '\"') {
1561 c = ch;
1562 read_name:
1563 inp();
1564 q = buf;
1565 while (ch != c && ch != '\n' && ch != CH_EOF) {
1566 if ((q - buf) < sizeof(buf) - 1)
1567 *q++ = ch;
1568 if (ch == '\\') {
1569 if (handle_stray_noerror() == 0)
1570 --q;
1571 } else
1572 inp();
1574 *q = '\0';
1575 minp();
1576 #if 0
1577 /* eat all spaces and comments after include */
1578 /* XXX: slightly incorrect */
1579 while (ch1 != '\n' && ch1 != CH_EOF)
1580 inp();
1581 #endif
1582 } else {
1583 /* computed #include : either we have only strings or
1584 we have anything enclosed in '<>' */
1585 next();
1586 buf[0] = '\0';
1587 if (tok == TOK_STR) {
1588 while (tok != TOK_LINEFEED) {
1589 if (tok != TOK_STR) {
1590 include_syntax:
1591 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1593 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1594 next();
1596 c = '\"';
1597 } else {
1598 int len;
1599 while (tok != TOK_LINEFEED) {
1600 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1601 next();
1603 len = strlen(buf);
1604 /* check syntax and remove '<>' */
1605 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1606 goto include_syntax;
1607 memmove(buf, buf + 1, len - 2);
1608 buf[len - 2] = '\0';
1609 c = '>';
1613 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1614 tcc_error("#include recursion too deep");
1615 /* store current file in stack, but increment stack later below */
1616 *s1->include_stack_ptr = file;
1618 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1619 for (i = -2; i < n; ++i) {
1620 char buf1[sizeof file->filename];
1621 CachedInclude *e;
1622 BufferedFile **f;
1623 const char *path;
1625 if (i == -2) {
1626 /* check absolute include path */
1627 if (!IS_ABSPATH(buf))
1628 continue;
1629 buf1[0] = 0;
1630 i = n; /* force end loop */
1632 } else if (i == -1) {
1633 /* search in current dir if "header.h" */
1634 if (c != '\"')
1635 continue;
1636 path = file->filename;
1637 pstrncpy(buf1, path, tcc_basename(path) - path);
1639 } else {
1640 /* search in all the include paths */
1641 if (i < s1->nb_include_paths)
1642 path = s1->include_paths[i];
1643 else
1644 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1645 pstrcpy(buf1, sizeof(buf1), path);
1646 pstrcat(buf1, sizeof(buf1), "/");
1649 pstrcat(buf1, sizeof(buf1), buf);
1651 if (tok == TOK_INCLUDE_NEXT)
1652 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1653 if (0 == PATHCMP((*f)->filename, buf1)) {
1654 #ifdef INC_DEBUG
1655 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1656 #endif
1657 goto include_trynext;
1660 e = search_cached_include(s1, buf1);
1661 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
1662 /* no need to parse the include because the 'ifndef macro'
1663 is defined */
1664 #ifdef INC_DEBUG
1665 printf("%s: skipping cached %s\n", file->filename, buf1);
1666 #endif
1667 goto include_done;
1670 if (tcc_open(s1, buf1) < 0)
1671 include_trynext:
1672 continue;
1674 #ifdef INC_DEBUG
1675 printf("%s: including %s\n", file->prev->filename, file->filename);
1676 #endif
1677 /* update target deps */
1678 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1679 tcc_strdup(buf1));
1680 /* push current file in stack */
1681 ++s1->include_stack_ptr;
1682 /* add include file debug info */
1683 if (s1->do_debug)
1684 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1685 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1686 ch = file->buf_ptr[0];
1687 goto the_end;
1689 tcc_error("include file '%s' not found", buf);
1690 include_done:
1691 break;
1692 case TOK_IFNDEF:
1693 c = 1;
1694 goto do_ifdef;
1695 case TOK_IF:
1696 c = expr_preprocess();
1697 goto do_if;
1698 case TOK_IFDEF:
1699 c = 0;
1700 do_ifdef:
1701 next_nomacro();
1702 if (tok < TOK_IDENT)
1703 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1704 if (is_bof) {
1705 if (c) {
1706 #ifdef INC_DEBUG
1707 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1708 #endif
1709 file->ifndef_macro = tok;
1712 c = (define_find(tok) != 0) ^ c;
1713 do_if:
1714 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1715 tcc_error("memory full (ifdef)");
1716 *s1->ifdef_stack_ptr++ = c;
1717 goto test_skip;
1718 case TOK_ELSE:
1719 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1720 tcc_error("#else without matching #if");
1721 if (s1->ifdef_stack_ptr[-1] & 2)
1722 tcc_error("#else after #else");
1723 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1724 goto test_else;
1725 case TOK_ELIF:
1726 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1727 tcc_error("#elif without matching #if");
1728 c = s1->ifdef_stack_ptr[-1];
1729 if (c > 1)
1730 tcc_error("#elif after #else");
1731 /* last #if/#elif expression was true: we skip */
1732 if (c == 1)
1733 goto skip;
1734 c = expr_preprocess();
1735 s1->ifdef_stack_ptr[-1] = c;
1736 test_else:
1737 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1738 file->ifndef_macro = 0;
1739 test_skip:
1740 if (!(c & 1)) {
1741 skip:
1742 preprocess_skip();
1743 is_bof = 0;
1744 goto redo;
1746 break;
1747 case TOK_ENDIF:
1748 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1749 tcc_error("#endif without matching #if");
1750 s1->ifdef_stack_ptr--;
1751 /* '#ifndef macro' was at the start of file. Now we check if
1752 an '#endif' is exactly at the end of file */
1753 if (file->ifndef_macro &&
1754 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1755 file->ifndef_macro_saved = file->ifndef_macro;
1756 /* need to set to zero to avoid false matches if another
1757 #ifndef at middle of file */
1758 file->ifndef_macro = 0;
1759 while (tok != TOK_LINEFEED)
1760 next_nomacro();
1761 tok_flags |= TOK_FLAG_ENDIF;
1762 goto the_end;
1764 break;
1765 case TOK_PPNUM:
1766 n = strtoul((char*)tokc.cstr->data, &q, 10);
1767 goto _line_num;
1768 case TOK_LINE:
1769 next();
1770 if (tok != TOK_CINT)
1771 _line_err:
1772 tcc_error("wrong #line format");
1773 n = tokc.i;
1774 _line_num:
1775 next();
1776 if (tok != TOK_LINEFEED) {
1777 if (tok == TOK_STR)
1778 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.cstr->data);
1779 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1780 break;
1781 else
1782 goto _line_err;
1783 --n;
1785 if (file->fd > 0)
1786 total_lines += file->line_num - n;
1787 file->line_num = n;
1788 if (s1->do_debug)
1789 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1790 break;
1791 case TOK_ERROR:
1792 case TOK_WARNING:
1793 c = tok;
1794 ch = file->buf_ptr[0];
1795 skip_spaces();
1796 q = buf;
1797 while (ch != '\n' && ch != CH_EOF) {
1798 if ((q - buf) < sizeof(buf) - 1)
1799 *q++ = ch;
1800 if (ch == '\\') {
1801 if (handle_stray_noerror() == 0)
1802 --q;
1803 } else
1804 inp();
1806 *q = '\0';
1807 if (c == TOK_ERROR)
1808 tcc_error("#error %s", buf);
1809 else
1810 tcc_warning("#warning %s", buf);
1811 break;
1812 case TOK_PRAGMA:
1813 pragma_parse(s1);
1814 break;
1815 case TOK_LINEFEED:
1816 goto the_end;
1817 default:
1818 /* ignore gas line comment in an 'S' file. */
1819 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1820 goto ignore;
1821 if (tok == '!' && is_bof)
1822 /* '!' is ignored at beginning to allow C scripts. */
1823 goto ignore;
1824 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1825 ignore:
1826 file->buf_ptr = parse_line_comment(file->buf_ptr);
1827 goto the_end;
1829 /* ignore other preprocess commands or #! for C scripts */
1830 while (tok != TOK_LINEFEED)
1831 next_nomacro();
1832 the_end:
1833 parse_flags = saved_parse_flags;
1836 /* evaluate escape codes in a string. */
1837 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1839 int c, n;
1840 const uint8_t *p;
1842 p = buf;
1843 for(;;) {
1844 c = *p;
1845 if (c == '\0')
1846 break;
1847 if (c == '\\') {
1848 p++;
1849 /* escape */
1850 c = *p;
1851 switch(c) {
1852 case '0': case '1': case '2': case '3':
1853 case '4': case '5': case '6': case '7':
1854 /* at most three octal digits */
1855 n = c - '0';
1856 p++;
1857 c = *p;
1858 if (isoct(c)) {
1859 n = n * 8 + c - '0';
1860 p++;
1861 c = *p;
1862 if (isoct(c)) {
1863 n = n * 8 + c - '0';
1864 p++;
1867 c = n;
1868 goto add_char_nonext;
1869 case 'x':
1870 case 'u':
1871 case 'U':
1872 p++;
1873 n = 0;
1874 for(;;) {
1875 c = *p;
1876 if (c >= 'a' && c <= 'f')
1877 c = c - 'a' + 10;
1878 else if (c >= 'A' && c <= 'F')
1879 c = c - 'A' + 10;
1880 else if (isnum(c))
1881 c = c - '0';
1882 else
1883 break;
1884 n = n * 16 + c;
1885 p++;
1887 c = n;
1888 goto add_char_nonext;
1889 case 'a':
1890 c = '\a';
1891 break;
1892 case 'b':
1893 c = '\b';
1894 break;
1895 case 'f':
1896 c = '\f';
1897 break;
1898 case 'n':
1899 c = '\n';
1900 break;
1901 case 'r':
1902 c = '\r';
1903 break;
1904 case 't':
1905 c = '\t';
1906 break;
1907 case 'v':
1908 c = '\v';
1909 break;
1910 case 'e':
1911 if (!gnu_ext)
1912 goto invalid_escape;
1913 c = 27;
1914 break;
1915 case '\'':
1916 case '\"':
1917 case '\\':
1918 case '?':
1919 break;
1920 default:
1921 invalid_escape:
1922 if (c >= '!' && c <= '~')
1923 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1924 else
1925 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1926 break;
1929 p++;
1930 add_char_nonext:
1931 if (!is_long)
1932 cstr_ccat(outstr, c);
1933 else
1934 cstr_wccat(outstr, c);
1936 /* add a trailing '\0' */
1937 if (!is_long)
1938 cstr_ccat(outstr, '\0');
1939 else
1940 cstr_wccat(outstr, '\0');
1943 void parse_string(const char *s, int len)
1945 uint8_t buf[1000], *p = buf;
1946 int is_long, sep;
1948 if ((is_long = *s == 'L'))
1949 ++s, --len;
1950 sep = *s++;
1951 len -= 2;
1952 if (len >= sizeof buf)
1953 p = tcc_malloc(len + 1);
1954 memcpy(p, s, len);
1955 p[len] = 0;
1957 cstr_reset(&tokcstr);
1958 parse_escape_string(&tokcstr, p, is_long);
1959 if (p != buf)
1960 tcc_free(p);
1962 if (sep == '\'') {
1963 int char_size;
1964 /* XXX: make it portable */
1965 if (!is_long)
1966 char_size = 1;
1967 else
1968 char_size = sizeof(nwchar_t);
1969 if (tokcstr.size <= char_size)
1970 tcc_error("empty character constant");
1971 if (tokcstr.size > 2 * char_size)
1972 tcc_warning("multi-character character constant");
1973 if (!is_long) {
1974 tokc.i = *(int8_t *)tokcstr.data;
1975 tok = TOK_CCHAR;
1976 } else {
1977 tokc.i = *(nwchar_t *)tokcstr.data;
1978 tok = TOK_LCHAR;
1980 } else {
1981 tokc.cstr = &tokcstr;
1982 if (!is_long)
1983 tok = TOK_STR;
1984 else
1985 tok = TOK_LSTR;
1989 /* we use 64 bit numbers */
1990 #define BN_SIZE 2
1992 /* bn = (bn << shift) | or_val */
1993 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1995 int i;
1996 unsigned int v;
1997 for(i=0;i<BN_SIZE;i++) {
1998 v = bn[i];
1999 bn[i] = (v << shift) | or_val;
2000 or_val = v >> (32 - shift);
2004 static void bn_zero(unsigned int *bn)
2006 int i;
2007 for(i=0;i<BN_SIZE;i++) {
2008 bn[i] = 0;
2012 /* parse number in null terminated string 'p' and return it in the
2013 current token */
2014 static void parse_number(const char *p)
2016 int b, t, shift, frac_bits, s, exp_val, ch;
2017 char *q;
2018 unsigned int bn[BN_SIZE];
2019 double d;
2021 /* number */
2022 q = token_buf;
2023 ch = *p++;
2024 t = ch;
2025 ch = *p++;
2026 *q++ = t;
2027 b = 10;
2028 if (t == '.') {
2029 goto float_frac_parse;
2030 } else if (t == '0') {
2031 if (ch == 'x' || ch == 'X') {
2032 q--;
2033 ch = *p++;
2034 b = 16;
2035 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2036 q--;
2037 ch = *p++;
2038 b = 2;
2041 /* parse all digits. cannot check octal numbers at this stage
2042 because of floating point constants */
2043 while (1) {
2044 if (ch >= 'a' && ch <= 'f')
2045 t = ch - 'a' + 10;
2046 else if (ch >= 'A' && ch <= 'F')
2047 t = ch - 'A' + 10;
2048 else if (isnum(ch))
2049 t = ch - '0';
2050 else
2051 break;
2052 if (t >= b)
2053 break;
2054 if (q >= token_buf + STRING_MAX_SIZE) {
2055 num_too_long:
2056 tcc_error("number too long");
2058 *q++ = ch;
2059 ch = *p++;
2061 if (ch == '.' ||
2062 ((ch == 'e' || ch == 'E') && b == 10) ||
2063 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2064 if (b != 10) {
2065 /* NOTE: strtox should support that for hexa numbers, but
2066 non ISOC99 libcs do not support it, so we prefer to do
2067 it by hand */
2068 /* hexadecimal or binary floats */
2069 /* XXX: handle overflows */
2070 *q = '\0';
2071 if (b == 16)
2072 shift = 4;
2073 else
2074 shift = 1;
2075 bn_zero(bn);
2076 q = token_buf;
2077 while (1) {
2078 t = *q++;
2079 if (t == '\0') {
2080 break;
2081 } else if (t >= 'a') {
2082 t = t - 'a' + 10;
2083 } else if (t >= 'A') {
2084 t = t - 'A' + 10;
2085 } else {
2086 t = t - '0';
2088 bn_lshift(bn, shift, t);
2090 frac_bits = 0;
2091 if (ch == '.') {
2092 ch = *p++;
2093 while (1) {
2094 t = ch;
2095 if (t >= 'a' && t <= 'f') {
2096 t = t - 'a' + 10;
2097 } else if (t >= 'A' && t <= 'F') {
2098 t = t - 'A' + 10;
2099 } else if (t >= '0' && t <= '9') {
2100 t = t - '0';
2101 } else {
2102 break;
2104 if (t >= b)
2105 tcc_error("invalid digit");
2106 bn_lshift(bn, shift, t);
2107 frac_bits += shift;
2108 ch = *p++;
2111 if (ch != 'p' && ch != 'P')
2112 expect("exponent");
2113 ch = *p++;
2114 s = 1;
2115 exp_val = 0;
2116 if (ch == '+') {
2117 ch = *p++;
2118 } else if (ch == '-') {
2119 s = -1;
2120 ch = *p++;
2122 if (ch < '0' || ch > '9')
2123 expect("exponent digits");
2124 while (ch >= '0' && ch <= '9') {
2125 exp_val = exp_val * 10 + ch - '0';
2126 ch = *p++;
2128 exp_val = exp_val * s;
2130 /* now we can generate the number */
2131 /* XXX: should patch directly float number */
2132 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2133 d = ldexp(d, exp_val - frac_bits);
2134 t = toup(ch);
2135 if (t == 'F') {
2136 ch = *p++;
2137 tok = TOK_CFLOAT;
2138 /* float : should handle overflow */
2139 tokc.f = (float)d;
2140 } else if (t == 'L') {
2141 ch = *p++;
2142 #ifdef TCC_TARGET_PE
2143 tok = TOK_CDOUBLE;
2144 tokc.d = d;
2145 #else
2146 tok = TOK_CLDOUBLE;
2147 /* XXX: not large enough */
2148 tokc.ld = (long double)d;
2149 #endif
2150 } else {
2151 tok = TOK_CDOUBLE;
2152 tokc.d = d;
2154 } else {
2155 /* decimal floats */
2156 if (ch == '.') {
2157 if (q >= token_buf + STRING_MAX_SIZE)
2158 goto num_too_long;
2159 *q++ = ch;
2160 ch = *p++;
2161 float_frac_parse:
2162 while (ch >= '0' && ch <= '9') {
2163 if (q >= token_buf + STRING_MAX_SIZE)
2164 goto num_too_long;
2165 *q++ = ch;
2166 ch = *p++;
2169 if (ch == 'e' || ch == 'E') {
2170 if (q >= token_buf + STRING_MAX_SIZE)
2171 goto num_too_long;
2172 *q++ = ch;
2173 ch = *p++;
2174 if (ch == '-' || ch == '+') {
2175 if (q >= token_buf + STRING_MAX_SIZE)
2176 goto num_too_long;
2177 *q++ = ch;
2178 ch = *p++;
2180 if (ch < '0' || ch > '9')
2181 expect("exponent digits");
2182 while (ch >= '0' && ch <= '9') {
2183 if (q >= token_buf + STRING_MAX_SIZE)
2184 goto num_too_long;
2185 *q++ = ch;
2186 ch = *p++;
2189 *q = '\0';
2190 t = toup(ch);
2191 errno = 0;
2192 if (t == 'F') {
2193 ch = *p++;
2194 tok = TOK_CFLOAT;
2195 tokc.f = strtof(token_buf, NULL);
2196 } else if (t == 'L') {
2197 ch = *p++;
2198 #ifdef TCC_TARGET_PE
2199 tok = TOK_CDOUBLE;
2200 tokc.d = strtod(token_buf, NULL);
2201 #else
2202 tok = TOK_CLDOUBLE;
2203 tokc.ld = strtold(token_buf, NULL);
2204 #endif
2205 } else {
2206 tok = TOK_CDOUBLE;
2207 tokc.d = strtod(token_buf, NULL);
2210 } else {
2211 unsigned long long n, n1;
2212 int lcount, ucount, must_64bit;
2213 const char *p1;
2215 /* integer number */
2216 *q = '\0';
2217 q = token_buf;
2218 if (b == 10 && *q == '0') {
2219 b = 8;
2220 q++;
2222 n = 0;
2223 while(1) {
2224 t = *q++;
2225 /* no need for checks except for base 10 / 8 errors */
2226 if (t == '\0')
2227 break;
2228 else if (t >= 'a')
2229 t = t - 'a' + 10;
2230 else if (t >= 'A')
2231 t = t - 'A' + 10;
2232 else
2233 t = t - '0';
2234 if (t >= b)
2235 tcc_error("invalid digit");
2236 n1 = n;
2237 n = n * b + t;
2238 /* detect overflow */
2239 /* XXX: this test is not reliable */
2240 if (n < n1)
2241 tcc_error("integer constant overflow");
2244 /* Determine the characteristics (unsigned and/or 64bit) the type of
2245 the constant must have according to the constant suffix(es) */
2246 lcount = ucount = must_64bit = 0;
2247 p1 = p;
2248 for(;;) {
2249 t = toup(ch);
2250 if (t == 'L') {
2251 if (lcount >= 2)
2252 tcc_error("three 'l's in integer constant");
2253 if (lcount && *(p - 1) != ch)
2254 tcc_error("incorrect integer suffix: %s", p1);
2255 lcount++;
2256 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2257 if (lcount == 2)
2258 #endif
2259 must_64bit = 1;
2260 ch = *p++;
2261 } else if (t == 'U') {
2262 if (ucount >= 1)
2263 tcc_error("two 'u's in integer constant");
2264 ucount++;
2265 ch = *p++;
2266 } else {
2267 break;
2271 /* Whether 64 bits are needed to hold the constant's value */
2272 if (n & 0xffffffff00000000LL || must_64bit) {
2273 tok = TOK_CLLONG;
2274 n1 = n >> 32;
2275 } else {
2276 tok = TOK_CINT;
2277 n1 = n;
2280 /* Whether type must be unsigned to hold the constant's value */
2281 if (ucount || ((n1 >> 31) && (b != 10))) {
2282 if (tok == TOK_CLLONG)
2283 tok = TOK_CULLONG;
2284 else
2285 tok = TOK_CUINT;
2286 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2287 } else if (n1 >> 31) {
2288 if (tok == TOK_CINT)
2289 tok = TOK_CLLONG;
2290 else
2291 tcc_error("integer constant overflow");
2294 if (tok == TOK_CINT || tok == TOK_CUINT)
2295 tokc.ui = n;
2296 else
2297 tokc.ull = n;
2299 if (ch)
2300 tcc_error("invalid number\n");
2304 #define PARSE2(c1, tok1, c2, tok2) \
2305 case c1: \
2306 PEEKC(c, p); \
2307 if (c == c2) { \
2308 p++; \
2309 tok = tok2; \
2310 } else { \
2311 tok = tok1; \
2313 break;
2315 /* return next token without macro substitution */
2316 static inline void next_nomacro1(void)
2318 int t, c, is_long;
2319 TokenSym *ts;
2320 uint8_t *p, *p1;
2321 unsigned int h;
2323 p = file->buf_ptr;
2324 redo_no_start:
2325 c = *p;
2326 switch(c) {
2327 case ' ':
2328 case '\t':
2329 tok = c;
2330 p++;
2331 if (parse_flags & PARSE_FLAG_SPACES)
2332 goto keep_tok_flags;
2333 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2334 ++p;
2335 goto redo_no_start;
2336 case '\f':
2337 case '\v':
2338 case '\r':
2339 p++;
2340 goto redo_no_start;
2341 case '\\':
2342 /* first look if it is in fact an end of buffer */
2343 c = handle_stray1(p);
2344 p = file->buf_ptr;
2345 if (c == '\\')
2346 goto parse_simple;
2347 if (c != CH_EOF)
2348 goto redo_no_start;
2350 TCCState *s1 = tcc_state;
2351 if ((parse_flags & PARSE_FLAG_LINEFEED)
2352 && !(tok_flags & TOK_FLAG_EOF)) {
2353 tok_flags |= TOK_FLAG_EOF;
2354 tok = TOK_LINEFEED;
2355 goto keep_tok_flags;
2356 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2357 tok = TOK_EOF;
2358 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2359 tcc_error("missing #endif");
2360 } else if (s1->include_stack_ptr == s1->include_stack) {
2361 /* no include left : end of file. */
2362 tok = TOK_EOF;
2363 } else {
2364 tok_flags &= ~TOK_FLAG_EOF;
2365 /* pop include file */
2367 /* test if previous '#endif' was after a #ifdef at
2368 start of file */
2369 if (tok_flags & TOK_FLAG_ENDIF) {
2370 #ifdef INC_DEBUG
2371 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2372 #endif
2373 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2374 tok_flags &= ~TOK_FLAG_ENDIF;
2377 /* add end of include file debug info */
2378 if (tcc_state->do_debug) {
2379 put_stabd(N_EINCL, 0, 0);
2381 /* pop include stack */
2382 tcc_close();
2383 s1->include_stack_ptr--;
2384 p = file->buf_ptr;
2385 goto redo_no_start;
2388 break;
2390 case '\n':
2391 file->line_num++;
2392 tok_flags |= TOK_FLAG_BOL;
2393 p++;
2394 maybe_newline:
2395 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2396 goto redo_no_start;
2397 tok = TOK_LINEFEED;
2398 goto keep_tok_flags;
2400 case '#':
2401 /* XXX: simplify */
2402 PEEKC(c, p);
2403 if ((tok_flags & TOK_FLAG_BOL) &&
2404 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2405 file->buf_ptr = p;
2406 preprocess(tok_flags & TOK_FLAG_BOF);
2407 p = file->buf_ptr;
2408 goto maybe_newline;
2409 } else {
2410 if (c == '#') {
2411 p++;
2412 tok = TOK_TWOSHARPS;
2413 } else {
2414 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2415 p = parse_line_comment(p - 1);
2416 goto redo_no_start;
2417 } else {
2418 tok = '#';
2422 break;
2424 /* dollar is allowed to start identifiers when not parsing asm */
2425 case '$':
2426 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2427 || (parse_flags & PARSE_FLAG_ASM_FILE))
2428 goto parse_simple;
2430 case 'a': case 'b': case 'c': case 'd':
2431 case 'e': case 'f': case 'g': case 'h':
2432 case 'i': case 'j': case 'k': case 'l':
2433 case 'm': case 'n': case 'o': case 'p':
2434 case 'q': case 'r': case 's': case 't':
2435 case 'u': case 'v': case 'w': case 'x':
2436 case 'y': case 'z':
2437 case 'A': case 'B': case 'C': case 'D':
2438 case 'E': case 'F': case 'G': case 'H':
2439 case 'I': case 'J': case 'K':
2440 case 'M': case 'N': case 'O': case 'P':
2441 case 'Q': case 'R': case 'S': case 'T':
2442 case 'U': case 'V': case 'W': case 'X':
2443 case 'Y': case 'Z':
2444 case '_':
2445 parse_ident_fast:
2446 p1 = p;
2447 h = TOK_HASH_INIT;
2448 h = TOK_HASH_FUNC(h, c);
2449 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2450 h = TOK_HASH_FUNC(h, c);
2451 if (c != '\\') {
2452 TokenSym **pts;
2453 int len;
2455 /* fast case : no stray found, so we have the full token
2456 and we have already hashed it */
2457 len = p - p1;
2458 h &= (TOK_HASH_SIZE - 1);
2459 pts = &hash_ident[h];
2460 for(;;) {
2461 ts = *pts;
2462 if (!ts)
2463 break;
2464 if (ts->len == len && !memcmp(ts->str, p1, len))
2465 goto token_found;
2466 pts = &(ts->hash_next);
2468 ts = tok_alloc_new(pts, (char *) p1, len);
2469 token_found: ;
2470 } else {
2471 /* slower case */
2472 cstr_reset(&tokcstr);
2474 while (p1 < p) {
2475 cstr_ccat(&tokcstr, *p1);
2476 p1++;
2478 p--;
2479 PEEKC(c, p);
2480 parse_ident_slow:
2481 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) {
2482 cstr_ccat(&tokcstr, c);
2483 PEEKC(c, p);
2485 ts = tok_alloc(tokcstr.data, tokcstr.size);
2487 tok = ts->tok;
2488 break;
2489 case 'L':
2490 t = p[1];
2491 if (t != '\\' && t != '\'' && t != '\"') {
2492 /* fast case */
2493 goto parse_ident_fast;
2494 } else {
2495 PEEKC(c, p);
2496 if (c == '\'' || c == '\"') {
2497 is_long = 1;
2498 goto str_const;
2499 } else {
2500 cstr_reset(&tokcstr);
2501 cstr_ccat(&tokcstr, 'L');
2502 goto parse_ident_slow;
2505 break;
2507 case '0': case '1': case '2': case '3':
2508 case '4': case '5': case '6': case '7':
2509 case '8': case '9':
2510 cstr_reset(&tokcstr);
2511 /* after the first digit, accept digits, alpha, '.' or sign if
2512 prefixed by 'eEpP' */
2513 parse_num:
2514 for(;;) {
2515 t = c;
2516 cstr_ccat(&tokcstr, c);
2517 PEEKC(c, p);
2518 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2519 || c == '.'
2520 || ((c == '+' || c == '-')
2521 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2523 break;
2525 /* We add a trailing '\0' to ease parsing */
2526 cstr_ccat(&tokcstr, '\0');
2527 tokc.cstr = &tokcstr;
2528 tok = TOK_PPNUM;
2529 break;
2531 case '.':
2532 /* special dot handling because it can also start a number */
2533 PEEKC(c, p);
2534 if (isnum(c)) {
2535 cstr_reset(&tokcstr);
2536 cstr_ccat(&tokcstr, '.');
2537 goto parse_num;
2538 } else if (c == '.') {
2539 PEEKC(c, p);
2540 if (c != '.')
2541 expect("'.'");
2542 PEEKC(c, p);
2543 tok = TOK_DOTS;
2544 } else {
2545 tok = '.';
2547 break;
2548 case '\'':
2549 case '\"':
2550 is_long = 0;
2551 str_const:
2552 cstr_reset(&tokcstr);
2553 if (is_long)
2554 cstr_ccat(&tokcstr, 'L');
2555 cstr_ccat(&tokcstr, c);
2556 p = parse_pp_string(p, c, &tokcstr);
2557 cstr_ccat(&tokcstr, c);
2558 cstr_ccat(&tokcstr, '\0');
2559 tokc.cstr = &tokcstr;
2560 tok = TOK_PPSTR;
2561 break;
2563 case '<':
2564 PEEKC(c, p);
2565 if (c == '=') {
2566 p++;
2567 tok = TOK_LE;
2568 } else if (c == '<') {
2569 PEEKC(c, p);
2570 if (c == '=') {
2571 p++;
2572 tok = TOK_A_SHL;
2573 } else {
2574 tok = TOK_SHL;
2576 } else {
2577 tok = TOK_LT;
2579 break;
2580 case '>':
2581 PEEKC(c, p);
2582 if (c == '=') {
2583 p++;
2584 tok = TOK_GE;
2585 } else if (c == '>') {
2586 PEEKC(c, p);
2587 if (c == '=') {
2588 p++;
2589 tok = TOK_A_SAR;
2590 } else {
2591 tok = TOK_SAR;
2593 } else {
2594 tok = TOK_GT;
2596 break;
2598 case '&':
2599 PEEKC(c, p);
2600 if (c == '&') {
2601 p++;
2602 tok = TOK_LAND;
2603 } else if (c == '=') {
2604 p++;
2605 tok = TOK_A_AND;
2606 } else {
2607 tok = '&';
2609 break;
2611 case '|':
2612 PEEKC(c, p);
2613 if (c == '|') {
2614 p++;
2615 tok = TOK_LOR;
2616 } else if (c == '=') {
2617 p++;
2618 tok = TOK_A_OR;
2619 } else {
2620 tok = '|';
2622 break;
2624 case '+':
2625 PEEKC(c, p);
2626 if (c == '+') {
2627 p++;
2628 tok = TOK_INC;
2629 } else if (c == '=') {
2630 p++;
2631 tok = TOK_A_ADD;
2632 } else {
2633 tok = '+';
2635 break;
2637 case '-':
2638 PEEKC(c, p);
2639 if (c == '-') {
2640 p++;
2641 tok = TOK_DEC;
2642 } else if (c == '=') {
2643 p++;
2644 tok = TOK_A_SUB;
2645 } else if (c == '>') {
2646 p++;
2647 tok = TOK_ARROW;
2648 } else {
2649 tok = '-';
2651 break;
2653 PARSE2('!', '!', '=', TOK_NE)
2654 PARSE2('=', '=', '=', TOK_EQ)
2655 PARSE2('*', '*', '=', TOK_A_MUL)
2656 PARSE2('%', '%', '=', TOK_A_MOD)
2657 PARSE2('^', '^', '=', TOK_A_XOR)
2659 /* comments or operator */
2660 case '/':
2661 PEEKC(c, p);
2662 if (c == '*') {
2663 p = parse_comment(p);
2664 /* comments replaced by a blank */
2665 tok = ' ';
2666 goto keep_tok_flags;
2667 } else if (c == '/') {
2668 p = parse_line_comment(p);
2669 tok = ' ';
2670 goto keep_tok_flags;
2671 } else if (c == '=') {
2672 p++;
2673 tok = TOK_A_DIV;
2674 } else {
2675 tok = '/';
2677 break;
2679 /* simple tokens */
2680 case '(':
2681 case ')':
2682 case '[':
2683 case ']':
2684 case '{':
2685 case '}':
2686 case ',':
2687 case ';':
2688 case ':':
2689 case '?':
2690 case '~':
2691 case '@': /* only used in assembler */
2692 parse_simple:
2693 tok = c;
2694 p++;
2695 break;
2696 default:
2697 tcc_error("unrecognized character \\x%02x", c);
2698 break;
2700 tok_flags = 0;
2701 keep_tok_flags:
2702 file->buf_ptr = p;
2703 #if defined(PARSE_DEBUG)
2704 printf("token = %s\n", get_tok_str(tok, &tokc));
2705 #endif
2708 /* return next token without macro substitution. Can read input from
2709 macro_ptr buffer */
2710 static void next_nomacro_spc(void)
2712 if (macro_ptr) {
2713 redo:
2714 tok = *macro_ptr;
2715 if (tok) {
2716 TOK_GET(&tok, &macro_ptr, &tokc);
2717 if (tok == TOK_LINENUM) {
2718 file->line_num = tokc.i;
2719 goto redo;
2722 } else {
2723 next_nomacro1();
2727 ST_FUNC void next_nomacro(void)
2729 do {
2730 next_nomacro_spc();
2731 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2735 static void macro_subst(
2736 TokenString *tok_str,
2737 Sym **nested_list,
2738 const int *macro_str,
2739 int can_read_stream
2742 /* substitute arguments in replacement lists in macro_str by the values in
2743 args (field d) and return allocated string */
2744 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2746 int t, t0, t1, spc;
2747 const int *st;
2748 Sym *s;
2749 CValue cval;
2750 TokenString str;
2751 CString cstr;
2753 tok_str_new(&str);
2754 t0 = t1 = 0;
2755 while(1) {
2756 TOK_GET(&t, &macro_str, &cval);
2757 if (!t)
2758 break;
2759 if (t == '#') {
2760 /* stringize */
2761 TOK_GET(&t, &macro_str, &cval);
2762 if (!t)
2763 goto bad_stringy;
2764 s = sym_find2(args, t);
2765 if (s) {
2766 cstr_new(&cstr);
2767 cstr_ccat(&cstr, '\"');
2768 st = s->d;
2769 spc = 0;
2770 while (*st) {
2771 TOK_GET(&t, &st, &cval);
2772 if (t != TOK_PLCHLDR
2773 && t != TOK_NOSUBST
2774 && 0 == check_space(t, &spc)) {
2775 const char *s = get_tok_str(t, &cval);
2776 while (*s) {
2777 if (t == TOK_PPSTR && *s != '\'')
2778 add_char(&cstr, *s);
2779 else
2780 cstr_ccat(&cstr, *s);
2781 ++s;
2785 cstr.size -= spc;
2786 cstr_ccat(&cstr, '\"');
2787 cstr_ccat(&cstr, '\0');
2788 #ifdef PP_DEBUG
2789 printf("\nstringize: <%s>\n", (char *)cstr.data);
2790 #endif
2791 /* add string */
2792 cval.cstr = &cstr;
2793 tok_str_add2(&str, TOK_PPSTR, &cval);
2794 cstr_free(cval.cstr);
2795 } else {
2796 bad_stringy:
2797 expect("macro parameter after '#'");
2799 } else if (t >= TOK_IDENT) {
2800 s = sym_find2(args, t);
2801 if (s) {
2802 int l0 = str.len;
2803 st = s->d;
2804 /* if '##' is present before or after, no arg substitution */
2805 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2806 /* special case for var arg macros : ## eats the ','
2807 if empty VA_ARGS variable. */
2808 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2809 if (*st == 0) {
2810 /* suppress ',' '##' */
2811 str.len -= 2;
2812 } else {
2813 /* suppress '##' and add variable */
2814 str.len--;
2815 goto add_var;
2817 } else {
2818 for(;;) {
2819 int t1;
2820 TOK_GET(&t1, &st, &cval);
2821 if (!t1)
2822 break;
2823 tok_str_add2(&str, t1, &cval);
2827 } else {
2828 add_var:
2829 /* NOTE: the stream cannot be read when macro
2830 substituing an argument */
2831 macro_subst(&str, nested_list, st, 0);
2833 if (str.len == l0) /* exanded to empty string */
2834 tok_str_add(&str, TOK_PLCHLDR);
2835 } else {
2836 tok_str_add(&str, t);
2838 } else {
2839 tok_str_add2(&str, t, &cval);
2841 t0 = t1, t1 = t;
2843 tok_str_add(&str, 0);
2844 return str.str;
2847 static char const ab_month_name[12][4] =
2849 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2850 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2853 /* peek or read [ws_str == NULL] next token from function macro call,
2854 walking up macro levels up to the file if necessary */
2855 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2857 int t;
2858 const int *p;
2859 Sym *sa;
2861 for (;;) {
2862 if (macro_ptr) {
2863 p = macro_ptr, t = *p;
2864 if (ws_str) {
2865 while (is_space(t) || TOK_LINEFEED == t)
2866 tok_str_add(ws_str, t), t = *++p;
2868 if (t == 0 && can_read_stream) {
2869 end_macro();
2870 /* also, end of scope for nested defined symbol */
2871 sa = *nested_list;
2872 while (sa && sa->v == -1)
2873 sa = sa->prev;
2874 if (sa)
2875 sa->v = -1;
2876 continue;
2878 } else {
2879 ch = handle_eob();
2880 if (ws_str) {
2881 while (is_space(ch) || ch == '\n' || ch == '/') {
2882 if (ch == '/') {
2883 int c;
2884 uint8_t *p = file->buf_ptr;
2885 PEEKC(c, p);
2886 if (c == '*') {
2887 p = parse_comment(p);
2888 file->buf_ptr = p - 1;
2889 } else if (c == '/') {
2890 p = parse_line_comment(p);
2891 file->buf_ptr = p - 1;
2892 } else
2893 break;
2894 ch = ' ';
2896 tok_str_add(ws_str, ch);
2897 cinp();
2900 t = ch;
2903 if (ws_str)
2904 return t;
2905 next_nomacro_spc();
2906 return tok;
2910 /* do macro substitution of current token with macro 's' and add
2911 result to (tok_str,tok_len). 'nested_list' is the list of all
2912 macros we got inside to avoid recursing. Return non zero if no
2913 substitution needs to be done */
2914 static int macro_subst_tok(
2915 TokenString *tok_str,
2916 Sym **nested_list,
2917 Sym *s,
2918 int can_read_stream)
2920 Sym *args, *sa, *sa1;
2921 int parlevel, *mstr, t, t1, spc;
2922 TokenString str;
2923 char *cstrval;
2924 CValue cval;
2925 CString cstr;
2926 char buf[32];
2928 /* if symbol is a macro, prepare substitution */
2929 /* special macros */
2930 if (tok == TOK___LINE__) {
2931 snprintf(buf, sizeof(buf), "%d", file->line_num);
2932 cstrval = buf;
2933 t1 = TOK_PPNUM;
2934 goto add_cstr1;
2935 } else if (tok == TOK___FILE__) {
2936 cstrval = file->filename;
2937 goto add_cstr;
2938 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2939 time_t ti;
2940 struct tm *tm;
2942 time(&ti);
2943 tm = localtime(&ti);
2944 if (tok == TOK___DATE__) {
2945 snprintf(buf, sizeof(buf), "%s %2d %d",
2946 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2947 } else {
2948 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2949 tm->tm_hour, tm->tm_min, tm->tm_sec);
2951 cstrval = buf;
2952 add_cstr:
2953 t1 = TOK_STR;
2954 add_cstr1:
2955 cstr_new(&cstr);
2956 cstr_cat(&cstr, cstrval);
2957 cstr_ccat(&cstr, '\0');
2958 cval.cstr = &cstr;
2959 tok_str_add2(tok_str, t1, &cval);
2960 cstr_free(&cstr);
2961 } else {
2962 int saved_parse_flags = parse_flags;
2964 mstr = s->d;
2965 if (s->type.t == MACRO_FUNC) {
2966 /* whitespace between macro name and argument list */
2967 TokenString ws_str;
2968 tok_str_new(&ws_str);
2970 spc = 0;
2971 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
2972 | PARSE_FLAG_ACCEPT_STRAYS;
2974 /* get next token from argument stream */
2975 t = next_argstream(nested_list, can_read_stream, &ws_str);
2976 if (t != '(') {
2977 /* not a macro substitution after all, restore the
2978 * macro token plus all whitespace we've read.
2979 * whitespace is intentionally not merged to preserve
2980 * newlines. */
2981 parse_flags = saved_parse_flags;
2982 tok_str_add(tok_str, tok);
2983 if (parse_flags & PARSE_FLAG_SPACES) {
2984 int i;
2985 for (i = 0; i < ws_str.len; i++)
2986 tok_str_add(tok_str, ws_str.str[i]);
2988 tok_str_free(ws_str.str);
2989 return 0;
2990 } else {
2991 tok_str_free(ws_str.str);
2993 next_nomacro(); /* eat '(' */
2995 /* argument macro */
2996 args = NULL;
2997 sa = s->next;
2998 /* NOTE: empty args are allowed, except if no args */
2999 for(;;) {
3000 do {
3001 next_argstream(nested_list, can_read_stream, NULL);
3002 } while (is_space(tok) || TOK_LINEFEED == tok);
3003 empty_arg:
3004 /* handle '()' case */
3005 if (!args && !sa && tok == ')')
3006 break;
3007 if (!sa)
3008 tcc_error("macro '%s' used with too many args",
3009 get_tok_str(s->v, 0));
3010 tok_str_new(&str);
3011 parlevel = spc = 0;
3012 /* NOTE: non zero sa->t indicates VA_ARGS */
3013 while ((parlevel > 0 ||
3014 (tok != ')' &&
3015 (tok != ',' || sa->type.t)))) {
3016 if (tok == TOK_EOF || tok == 0)
3017 break;
3018 if (tok == '(')
3019 parlevel++;
3020 else if (tok == ')')
3021 parlevel--;
3022 if (tok == TOK_LINEFEED)
3023 tok = ' ';
3024 if (!check_space(tok, &spc))
3025 tok_str_add2(&str, tok, &tokc);
3026 next_argstream(nested_list, can_read_stream, NULL);
3028 if (parlevel)
3029 expect(")");
3030 str.len -= spc;
3031 tok_str_add(&str, 0);
3032 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3033 sa1->d = str.str;
3034 sa = sa->next;
3035 if (tok == ')') {
3036 /* special case for gcc var args: add an empty
3037 var arg argument if it is omitted */
3038 if (sa && sa->type.t && gnu_ext)
3039 goto empty_arg;
3040 break;
3042 if (tok != ',')
3043 expect(",");
3045 if (sa) {
3046 tcc_error("macro '%s' used with too few args",
3047 get_tok_str(s->v, 0));
3050 parse_flags = saved_parse_flags;
3052 /* now subst each arg */
3053 mstr = macro_arg_subst(nested_list, mstr, args);
3054 /* free memory */
3055 sa = args;
3056 while (sa) {
3057 sa1 = sa->prev;
3058 tok_str_free(sa->d);
3059 sym_free(sa);
3060 sa = sa1;
3064 sym_push2(nested_list, s->v, 0, 0);
3065 parse_flags = saved_parse_flags;
3066 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3068 /* pop nested defined symbol */
3069 sa1 = *nested_list;
3070 *nested_list = sa1->prev;
3071 sym_free(sa1);
3072 if (mstr != s->d)
3073 tok_str_free(mstr);
3075 return 0;
3078 int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3080 CString cstr;
3081 int n;
3083 cstr_new(&cstr);
3084 if (t1 != TOK_PLCHLDR)
3085 cstr_cat(&cstr, get_tok_str(t1, v1));
3086 n = cstr.size;
3087 if (t2 != TOK_PLCHLDR)
3088 cstr_cat(&cstr, get_tok_str(t2, v2));
3089 cstr_ccat(&cstr, '\0');
3091 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3092 memcpy(file->buffer, cstr.data, cstr.size);
3093 for (;;) {
3094 next_nomacro1();
3095 if (0 == *file->buf_ptr)
3096 break;
3097 if (is_space(tok))
3098 continue;
3099 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3100 n, cstr.data, (char*)cstr.data + n);
3101 break;
3103 tcc_close();
3105 //printf("paste <%s>\n", (char*)cstr.data);
3106 cstr_free(&cstr);
3107 return 0;
3110 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3111 return the resulting string (which must be freed). */
3112 static inline int *macro_twosharps(const int *ptr0)
3114 int t;
3115 CValue cval;
3116 TokenString macro_str1;
3117 int start_of_nosubsts = -1;
3118 const int *ptr;
3120 /* we search the first '##' */
3121 for (ptr = ptr0;;) {
3122 TOK_GET(&t, &ptr, &cval);
3123 if (t == TOK_TWOSHARPS)
3124 break;
3125 if (t == 0)
3126 return NULL;
3129 tok_str_new(&macro_str1);
3131 //tok_print(" $$$", ptr0);
3132 for (ptr = ptr0;;) {
3133 TOK_GET(&t, &ptr, &cval);
3134 if (t == 0)
3135 break;
3136 if (t == TOK_TWOSHARPS)
3137 continue;
3138 while (*ptr == TOK_TWOSHARPS) {
3139 int t1; CValue cv1;
3140 /* given 'a##b', remove nosubsts preceding 'a' */
3141 if (start_of_nosubsts >= 0)
3142 macro_str1.len = start_of_nosubsts;
3143 /* given 'a##b', remove nosubsts preceding 'b' */
3144 while ((t1 = *++ptr) == TOK_NOSUBST)
3146 if (t1 && t1 != TOK_TWOSHARPS
3147 && t1 != ':') /* 'a##:' don't build a new token */
3149 TOK_GET(&t1, &ptr, &cv1);
3150 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3151 paste_tokens(t, &cval, t1, &cv1);
3152 t = tok, cval = tokc;
3156 if (t == TOK_NOSUBST) {
3157 if (start_of_nosubsts < 0)
3158 start_of_nosubsts = macro_str1.len;
3159 } else {
3160 start_of_nosubsts = -1;
3162 tok_str_add2(&macro_str1, t, &cval);
3164 tok_str_add(&macro_str1, 0);
3165 //tok_print(" ###", macro_str1.str);
3166 return macro_str1.str;
3169 /* do macro substitution of macro_str and add result to
3170 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3171 inside to avoid recursing. */
3172 static void macro_subst(
3173 TokenString *tok_str,
3174 Sym **nested_list,
3175 const int *macro_str,
3176 int can_read_stream
3179 Sym *s;
3180 const int *ptr;
3181 int t, spc, nosubst;
3182 CValue cval;
3183 int *macro_str1 = NULL;
3185 /* first scan for '##' operator handling */
3186 ptr = macro_str;
3187 spc = nosubst = 0;
3189 /* first scan for '##' operator handling */
3190 if (can_read_stream) {
3191 macro_str1 = macro_twosharps(ptr);
3192 if (macro_str1)
3193 ptr = macro_str1;
3196 while (1) {
3197 TOK_GET(&t, &ptr, &cval);
3198 if (t == 0)
3199 break;
3201 if (t >= TOK_IDENT && 0 == nosubst) {
3202 s = define_find(t);
3203 if (s == NULL)
3204 goto no_subst;
3206 /* if nested substitution, do nothing */
3207 if (sym_find2(*nested_list, t)) {
3208 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3209 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3210 goto no_subst;
3214 TokenString str;
3215 str.str = (int*)ptr;
3216 begin_macro(&str, 2);
3218 tok = t;
3219 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3221 if (str.alloc == 3) {
3222 /* already finished by reading function macro arguments */
3223 break;
3226 ptr = macro_ptr;
3227 end_macro ();
3230 spc = tok_str->len && is_space(tok_str->str[tok_str->len-1]);
3232 } else {
3234 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3235 tcc_error("stray '\\' in program");
3237 no_subst:
3238 if (!check_space(t, &spc))
3239 tok_str_add2(tok_str, t, &cval);
3240 nosubst = 0;
3241 if (t == TOK_NOSUBST)
3242 nosubst = 1;
3245 if (macro_str1)
3246 tok_str_free(macro_str1);
3250 /* return next token with macro substitution */
3251 ST_FUNC void next(void)
3253 redo:
3254 if (parse_flags & PARSE_FLAG_SPACES)
3255 next_nomacro_spc();
3256 else
3257 next_nomacro();
3259 if (macro_ptr) {
3260 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3261 /* discard preprocessor markers */
3262 goto redo;
3263 } else if (tok == 0) {
3264 /* end of macro or unget token string */
3265 end_macro();
3266 goto redo;
3268 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3269 Sym *s;
3270 /* if reading from file, try to substitute macros */
3271 s = define_find(tok);
3272 if (s) {
3273 static TokenString str; /* using static string for speed */
3274 Sym *nested_list = NULL;
3275 tok_str_new(&str);
3276 nested_list = NULL;
3277 macro_subst_tok(&str, &nested_list, s, 1);
3278 tok_str_add(&str, 0);
3279 begin_macro(&str, 0);
3280 goto redo;
3283 /* convert preprocessor tokens into C tokens */
3284 if (tok == TOK_PPNUM) {
3285 if (parse_flags & PARSE_FLAG_TOK_NUM)
3286 parse_number((char *)tokc.cstr->data);
3287 } else if (tok == TOK_PPSTR) {
3288 if (parse_flags & PARSE_FLAG_TOK_STR)
3289 parse_string((char *)tokc.cstr->data, tokc.cstr->size - 1);
3293 /* push back current token and set current token to 'last_tok'. Only
3294 identifier case handled for labels. */
3295 ST_INLN void unget_tok(int last_tok)
3297 TokenString *str = tcc_malloc(sizeof *str);
3298 tok_str_new(str);
3299 tok_str_add2(str, tok, &tokc);
3300 tok_str_add(str, 0);
3301 begin_macro(str, 1);
3302 tok = last_tok;
3305 /* better than nothing, but needs extension to handle '-E' option
3306 correctly too */
3307 ST_FUNC void preprocess_init(TCCState *s1)
3309 s1->include_stack_ptr = s1->include_stack;
3310 /* XXX: move that before to avoid having to initialize
3311 file->ifdef_stack_ptr ? */
3312 s1->ifdef_stack_ptr = s1->ifdef_stack;
3313 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3315 pvtop = vtop = vstack - 1;
3316 s1->pack_stack[0] = 0;
3317 s1->pack_stack_ptr = s1->pack_stack;
3319 isidnum_table['$' - CH_EOF] =
3320 tcc_state->dollars_in_identifiers ? IS_ID : 0;
3323 ST_FUNC void preprocess_new(void)
3325 int i, c;
3326 const char *p, *r;
3328 /* init isid table */
3329 for(i = CH_EOF; i<256; i++)
3330 isidnum_table[i - CH_EOF]
3331 = is_space(i) ? IS_SPC
3332 : isid(i) ? IS_ID
3333 : isnum(i) ? IS_NUM
3334 : 0;
3336 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3338 tok_ident = TOK_IDENT;
3339 p = tcc_keywords;
3340 while (*p) {
3341 r = p;
3342 for(;;) {
3343 c = *r++;
3344 if (c == '\0')
3345 break;
3347 tok_alloc(p, r - p - 1);
3348 p = r;
3352 ST_FUNC void preprocess_delete(void)
3354 int i, n;
3356 /* free -D and compiler defines */
3357 free_defines(NULL);
3359 /* cleanup from error/setjmp */
3360 while (macro_stack)
3361 end_macro();
3362 macro_ptr = NULL;
3364 /* free tokens */
3365 n = tok_ident - TOK_IDENT;
3366 for(i = 0; i < n; i++)
3367 tcc_free(table_ident[i]);
3368 tcc_free(table_ident);
3369 table_ident = NULL;
3372 /* Preprocess the current file */
3373 ST_FUNC int tcc_preprocess(TCCState *s1)
3375 BufferedFile **iptr;
3376 int token_seen, spcs, level;
3378 preprocess_init(s1);
3379 ch = file->buf_ptr[0];
3380 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3381 parse_flags = PARSE_FLAG_PREPROCESS
3382 | (parse_flags & PARSE_FLAG_ASM_FILE)
3383 | PARSE_FLAG_LINEFEED
3384 | PARSE_FLAG_SPACES
3385 | PARSE_FLAG_ACCEPT_STRAYS
3388 #ifdef PP_BENCH
3389 do next(); while (tok != TOK_EOF); return 0;
3390 #endif
3392 token_seen = spcs = 0;
3393 pp_line(s1, file, 0);
3395 for (;;) {
3396 iptr = s1->include_stack_ptr;
3397 next();
3398 if (tok == TOK_EOF)
3399 break;
3400 level = s1->include_stack_ptr - iptr;
3401 if (level) {
3402 if (level > 0)
3403 pp_line(s1, *iptr, 0);
3404 pp_line(s1, file, level);
3407 if (0 == token_seen) {
3408 if (tok == ' ') {
3409 ++spcs;
3410 continue;
3412 if (tok == TOK_LINEFEED) {
3413 spcs = 0;
3414 continue;
3416 pp_line(s1, file, 0);
3417 while (spcs)
3418 fputs(" ", s1->ppfp), --spcs;
3419 token_seen = 1;
3421 } else if (tok == TOK_LINEFEED) {
3422 ++file->line_ref;
3423 token_seen = 0;
3426 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3429 return 0;