Move utility functions `trimfront/back` to tccpp.c
[tinycc.git] / tccpp.c
blob4164c7e485bddfb590b9e488d01f73176ad157ee
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 ST_DATA int tok_flags;
27 ST_DATA int parse_flags;
29 ST_DATA struct BufferedFile *file;
30 ST_DATA int ch, tok;
31 ST_DATA CValue tokc;
32 ST_DATA const int *macro_ptr;
33 ST_DATA CString tokcstr; /* current parsed string, if any */
35 /* display benchmark infos */
36 ST_DATA int total_lines;
37 ST_DATA int total_bytes;
38 ST_DATA int tok_ident;
39 ST_DATA TokenSym **table_ident;
41 /* ------------------------------------------------------------------------- */
43 static TokenSym *hash_ident[TOK_HASH_SIZE];
44 static char token_buf[STRING_MAX_SIZE + 1];
45 static unsigned char isidnum_table[256 - CH_EOF];
46 /* isidnum_table flags: */
47 #define IS_SPC 1
48 #define IS_ID 2
49 #define IS_NUM 4
51 static TokenString *macro_stack;
53 static const char tcc_keywords[] =
54 #define DEF(id, str) str "\0"
55 #include "tcctok.h"
56 #undef DEF
59 /* WARNING: the content of this string encodes token numbers */
60 static const unsigned char tok_two_chars[] =
61 /* outdated -- gr
62 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
63 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
64 */{
65 '<','=', TOK_LE,
66 '>','=', TOK_GE,
67 '!','=', TOK_NE,
68 '&','&', TOK_LAND,
69 '|','|', TOK_LOR,
70 '+','+', TOK_INC,
71 '-','-', TOK_DEC,
72 '=','=', TOK_EQ,
73 '<','<', TOK_SHL,
74 '>','>', TOK_SAR,
75 '+','=', TOK_A_ADD,
76 '-','=', TOK_A_SUB,
77 '*','=', TOK_A_MUL,
78 '/','=', TOK_A_DIV,
79 '%','=', TOK_A_MOD,
80 '&','=', TOK_A_AND,
81 '^','=', TOK_A_XOR,
82 '|','=', TOK_A_OR,
83 '-','>', TOK_ARROW,
84 '.','.', 0xa8, // C++ token ?
85 '#','#', TOK_TWOSHARPS,
89 static void next_nomacro_spc(void);
91 ST_FUNC void skip(int c)
93 if (tok != c)
94 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
95 next();
98 ST_FUNC void expect(const char *msg)
100 tcc_error("%s expected", msg);
103 ST_FUNC void begin_macro(TokenString *str, int alloc)
105 str->alloc = alloc;
106 str->prev = macro_stack;
107 str->prev_ptr = macro_ptr;
108 macro_ptr = str->str;
109 macro_stack = str;
112 ST_FUNC void end_macro(void)
114 TokenString *str = macro_stack;
115 macro_stack = str->prev;
116 macro_ptr = str->prev_ptr;
117 if (str->alloc == 2) {
118 str->alloc = 3; /* just mark as finished */
119 } else {
120 tok_str_free(str->str);
121 if (str->alloc == 1)
122 tcc_free(str);
126 /* ------------------------------------------------------------------------- */
127 /* CString handling */
128 static void cstr_realloc(CString *cstr, int new_size)
130 int size;
131 void *data;
133 size = cstr->size_allocated;
134 if (size == 0)
135 size = 8; /* no need to allocate a too small first string */
136 while (size < new_size)
137 size = size * 2;
138 data = tcc_realloc(cstr->data_allocated, size);
139 cstr->data_allocated = data;
140 cstr->size_allocated = size;
141 cstr->data = data;
144 /* add a byte */
145 ST_FUNC void cstr_ccat(CString *cstr, int ch)
147 int size;
148 size = cstr->size + 1;
149 if (size > cstr->size_allocated)
150 cstr_realloc(cstr, size);
151 ((unsigned char *)cstr->data)[size - 1] = ch;
152 cstr->size = size;
155 ST_FUNC void cstr_cat(CString *cstr, const char *str)
157 int c;
158 for(;;) {
159 c = *str;
160 if (c == '\0')
161 break;
162 cstr_ccat(cstr, c);
163 str++;
167 /* add a wide char */
168 ST_FUNC void cstr_wccat(CString *cstr, int ch)
170 int size;
171 size = cstr->size + sizeof(nwchar_t);
172 if (size > cstr->size_allocated)
173 cstr_realloc(cstr, size);
174 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
175 cstr->size = size;
178 ST_FUNC void cstr_new(CString *cstr)
180 memset(cstr, 0, sizeof(CString));
183 /* free string and reset it to NULL */
184 ST_FUNC void cstr_free(CString *cstr)
186 tcc_free(cstr->data_allocated);
187 cstr_new(cstr);
190 /* reset string to empty */
191 ST_FUNC void cstr_reset(CString *cstr)
193 cstr->size = 0;
196 /* XXX: unicode ? */
197 static void add_char(CString *cstr, int c)
199 if (c == '\'' || c == '\"' || c == '\\') {
200 /* XXX: could be more precise if char or string */
201 cstr_ccat(cstr, '\\');
203 if (c >= 32 && c <= 126) {
204 cstr_ccat(cstr, c);
205 } else {
206 cstr_ccat(cstr, '\\');
207 if (c == '\n') {
208 cstr_ccat(cstr, 'n');
209 } else {
210 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
211 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
212 cstr_ccat(cstr, '0' + (c & 7));
217 /* ------------------------------------------------------------------------- */
218 /* allocate a new token */
219 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
221 TokenSym *ts, **ptable;
222 int i;
224 if (tok_ident >= SYM_FIRST_ANOM)
225 tcc_error("memory full (symbols)");
227 /* expand token table if needed */
228 i = tok_ident - TOK_IDENT;
229 if ((i % TOK_ALLOC_INCR) == 0) {
230 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
231 table_ident = ptable;
234 ts = tcc_malloc(sizeof(TokenSym) + len);
235 table_ident[i] = ts;
236 ts->tok = tok_ident++;
237 ts->sym_define = NULL;
238 ts->sym_label = NULL;
239 ts->sym_struct = NULL;
240 ts->sym_identifier = NULL;
241 ts->len = len;
242 ts->hash_next = NULL;
243 memcpy(ts->str, str, len);
244 ts->str[len] = '\0';
245 *pts = ts;
246 return ts;
249 #define TOK_HASH_INIT 1
250 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
252 /* find a token and add it if not found */
253 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
255 TokenSym *ts, **pts;
256 int i;
257 unsigned int h;
259 h = TOK_HASH_INIT;
260 for(i=0;i<len;i++)
261 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
262 h &= (TOK_HASH_SIZE - 1);
264 pts = &hash_ident[h];
265 for(;;) {
266 ts = *pts;
267 if (!ts)
268 break;
269 if (ts->len == len && !memcmp(ts->str, str, len))
270 return ts;
271 pts = &(ts->hash_next);
273 return tok_alloc_new(pts, str, len);
276 /* XXX: buffer overflow */
277 /* XXX: float tokens */
278 ST_FUNC const char *get_tok_str(int v, CValue *cv)
280 static CString cstr_buf;
281 char *p;
282 int i, len;
284 /* first time preallocate static cstr_buf, next time only reset position to start */
285 if (!cstr_buf.data_allocated)
286 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
287 else
288 cstr_buf.size = 0;
289 p = cstr_buf.data;
291 switch(v) {
292 case TOK_CINT:
293 case TOK_CUINT:
294 /* XXX: not quite exact, but only useful for testing */
295 sprintf(p, "%llu", (unsigned long long)cv->i);
296 break;
297 case TOK_CLLONG:
298 case TOK_CULLONG:
299 /* XXX: not quite exact, but only useful for testing */
300 #ifdef _WIN32
301 sprintf(p, "%u", (unsigned)cv->i);
302 #else
303 sprintf(p, "%llu", (unsigned long long)cv->i);
304 #endif
305 break;
306 case TOK_LCHAR:
307 cstr_ccat(&cstr_buf, 'L');
308 case TOK_CCHAR:
309 cstr_ccat(&cstr_buf, '\'');
310 add_char(&cstr_buf, cv->i);
311 cstr_ccat(&cstr_buf, '\'');
312 cstr_ccat(&cstr_buf, '\0');
313 break;
314 case TOK_PPNUM:
315 case TOK_PPSTR:
316 return (char*)cv->str.data;
317 case TOK_LSTR:
318 cstr_ccat(&cstr_buf, 'L');
319 case TOK_STR:
320 cstr_ccat(&cstr_buf, '\"');
321 if (v == TOK_STR) {
322 len = cv->str.size - 1;
323 for(i=0;i<len;i++)
324 add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
325 } else {
326 len = (cv->str.size / sizeof(nwchar_t)) - 1;
327 for(i=0;i<len;i++)
328 add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
330 cstr_ccat(&cstr_buf, '\"');
331 cstr_ccat(&cstr_buf, '\0');
332 break;
334 case TOK_CFLOAT:
335 cstr_cat(&cstr_buf, "<float>");
336 cstr_ccat(&cstr_buf, '\0');
337 break;
338 case TOK_CDOUBLE:
339 cstr_cat(&cstr_buf, "<double>");
340 cstr_ccat(&cstr_buf, '\0');
341 break;
342 case TOK_CLDOUBLE:
343 cstr_cat(&cstr_buf, "<long double>");
344 cstr_ccat(&cstr_buf, '\0');
345 break;
346 case TOK_LINENUM:
347 cstr_cat(&cstr_buf, "<linenumber>");
348 cstr_ccat(&cstr_buf, '\0');
349 break;
351 /* above tokens have value, the ones below don't */
353 case TOK_LT:
354 v = '<';
355 goto addv;
356 case TOK_GT:
357 v = '>';
358 goto addv;
359 case TOK_DOTS:
360 return strcpy(p, "...");
361 case TOK_A_SHL:
362 return strcpy(p, "<<=");
363 case TOK_A_SAR:
364 return strcpy(p, ">>=");
365 default:
366 if (v < TOK_IDENT) {
367 /* search in two bytes table */
368 const unsigned char *q = tok_two_chars;
369 while (*q) {
370 if (q[2] == v) {
371 *p++ = q[0];
372 *p++ = q[1];
373 *p = '\0';
374 return cstr_buf.data;
376 q += 3;
378 if (v >= 127) {
379 sprintf(cstr_buf.data, "<%02x>", v);
380 return cstr_buf.data;
382 addv:
383 *p++ = v;
384 *p = '\0';
385 } else if (v < tok_ident) {
386 return table_ident[v - TOK_IDENT]->str;
387 } else if (v >= SYM_FIRST_ANOM) {
388 /* special name for anonymous symbol */
389 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
390 } else {
391 /* should never happen */
392 return NULL;
394 break;
396 return cstr_buf.data;
399 /* return the current character, handling end of block if necessary
400 (but not stray) */
401 ST_FUNC int handle_eob(void)
403 BufferedFile *bf = file;
404 int len;
406 /* only tries to read if really end of buffer */
407 if (bf->buf_ptr >= bf->buf_end) {
408 if (bf->fd != -1) {
409 #if defined(PARSE_DEBUG)
410 len = 1;
411 #else
412 len = IO_BUF_SIZE;
413 #endif
414 len = read(bf->fd, bf->buffer, len);
415 if (len < 0)
416 len = 0;
417 } else {
418 len = 0;
420 total_bytes += len;
421 bf->buf_ptr = bf->buffer;
422 bf->buf_end = bf->buffer + len;
423 *bf->buf_end = CH_EOB;
425 if (bf->buf_ptr < bf->buf_end) {
426 return bf->buf_ptr[0];
427 } else {
428 bf->buf_ptr = bf->buf_end;
429 return CH_EOF;
433 /* read next char from current input file and handle end of input buffer */
434 ST_INLN void inp(void)
436 ch = *(++(file->buf_ptr));
437 /* end of buffer/file handling */
438 if (ch == CH_EOB)
439 ch = handle_eob();
442 /* handle '\[\r]\n' */
443 static int handle_stray_noerror(void)
445 while (ch == '\\') {
446 inp();
447 if (ch == '\n') {
448 file->line_num++;
449 inp();
450 } else if (ch == '\r') {
451 inp();
452 if (ch != '\n')
453 goto fail;
454 file->line_num++;
455 inp();
456 } else {
457 fail:
458 return 1;
461 return 0;
464 static void handle_stray(void)
466 if (handle_stray_noerror())
467 tcc_error("stray '\\' in program");
470 /* skip the stray and handle the \\n case. Output an error if
471 incorrect char after the stray */
472 static int handle_stray1(uint8_t *p)
474 int c;
476 file->buf_ptr = p;
477 if (p >= file->buf_end) {
478 c = handle_eob();
479 if (c != '\\')
480 return c;
481 p = file->buf_ptr;
483 ch = *p;
484 if (handle_stray_noerror()) {
485 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
486 tcc_error("stray '\\' in program");
487 *--file->buf_ptr = '\\';
489 p = file->buf_ptr;
490 c = *p;
491 return c;
494 /* handle just the EOB case, but not stray */
495 #define PEEKC_EOB(c, p)\
497 p++;\
498 c = *p;\
499 if (c == '\\') {\
500 file->buf_ptr = p;\
501 c = handle_eob();\
502 p = file->buf_ptr;\
506 /* handle the complicated stray case */
507 #define PEEKC(c, p)\
509 p++;\
510 c = *p;\
511 if (c == '\\') {\
512 c = handle_stray1(p);\
513 p = file->buf_ptr;\
517 /* input with '\[\r]\n' handling. Note that this function cannot
518 handle other characters after '\', so you cannot call it inside
519 strings or comments */
520 ST_FUNC void minp(void)
522 inp();
523 if (ch == '\\')
524 handle_stray();
528 /* single line C++ comments */
529 static uint8_t *parse_line_comment(uint8_t *p)
531 int c;
533 p++;
534 for(;;) {
535 c = *p;
536 redo:
537 if (c == '\n' || c == CH_EOF) {
538 break;
539 } else if (c == '\\') {
540 file->buf_ptr = p;
541 c = handle_eob();
542 p = file->buf_ptr;
543 if (c == '\\') {
544 PEEKC_EOB(c, p);
545 if (c == '\n') {
546 file->line_num++;
547 PEEKC_EOB(c, p);
548 } else if (c == '\r') {
549 PEEKC_EOB(c, p);
550 if (c == '\n') {
551 file->line_num++;
552 PEEKC_EOB(c, p);
555 } else {
556 goto redo;
558 } else {
559 p++;
562 return p;
565 /* C comments */
566 ST_FUNC uint8_t *parse_comment(uint8_t *p)
568 int c;
570 p++;
571 for(;;) {
572 /* fast skip loop */
573 for(;;) {
574 c = *p;
575 if (c == '\n' || c == '*' || c == '\\')
576 break;
577 p++;
578 c = *p;
579 if (c == '\n' || c == '*' || c == '\\')
580 break;
581 p++;
583 /* now we can handle all the cases */
584 if (c == '\n') {
585 file->line_num++;
586 p++;
587 } else if (c == '*') {
588 p++;
589 for(;;) {
590 c = *p;
591 if (c == '*') {
592 p++;
593 } else if (c == '/') {
594 goto end_of_comment;
595 } else if (c == '\\') {
596 file->buf_ptr = p;
597 c = handle_eob();
598 p = file->buf_ptr;
599 if (c == CH_EOF)
600 tcc_error("unexpected end of file in comment");
601 if (c == '\\') {
602 /* skip '\[\r]\n', otherwise just skip the stray */
603 while (c == '\\') {
604 PEEKC_EOB(c, p);
605 if (c == '\n') {
606 file->line_num++;
607 PEEKC_EOB(c, p);
608 } else if (c == '\r') {
609 PEEKC_EOB(c, p);
610 if (c == '\n') {
611 file->line_num++;
612 PEEKC_EOB(c, p);
614 } else {
615 goto after_star;
619 } else {
620 break;
623 after_star: ;
624 } else {
625 /* stray, eob or eof */
626 file->buf_ptr = p;
627 c = handle_eob();
628 p = file->buf_ptr;
629 if (c == CH_EOF) {
630 tcc_error("unexpected end of file in comment");
631 } else if (c == '\\') {
632 p++;
636 end_of_comment:
637 p++;
638 return p;
641 #define cinp minp
643 static inline void skip_spaces(void)
645 while (isidnum_table[ch - CH_EOF] & IS_SPC)
646 cinp();
649 static inline int check_space(int t, int *spc)
651 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
652 if (*spc)
653 return 1;
654 *spc = 1;
655 } else
656 *spc = 0;
657 return 0;
660 /* parse a string without interpreting escapes */
661 static uint8_t *parse_pp_string(uint8_t *p,
662 int sep, CString *str)
664 int c;
665 p++;
666 for(;;) {
667 c = *p;
668 if (c == sep) {
669 break;
670 } else if (c == '\\') {
671 file->buf_ptr = p;
672 c = handle_eob();
673 p = file->buf_ptr;
674 if (c == CH_EOF) {
675 unterminated_string:
676 /* XXX: indicate line number of start of string */
677 tcc_error("missing terminating %c character", sep);
678 } else if (c == '\\') {
679 /* escape : just skip \[\r]\n */
680 PEEKC_EOB(c, p);
681 if (c == '\n') {
682 file->line_num++;
683 p++;
684 } else if (c == '\r') {
685 PEEKC_EOB(c, p);
686 if (c != '\n')
687 expect("'\n' after '\r'");
688 file->line_num++;
689 p++;
690 } else if (c == CH_EOF) {
691 goto unterminated_string;
692 } else {
693 if (str) {
694 cstr_ccat(str, '\\');
695 cstr_ccat(str, c);
697 p++;
700 } else if (c == '\n') {
701 file->line_num++;
702 goto add_char;
703 } else if (c == '\r') {
704 PEEKC_EOB(c, p);
705 if (c != '\n') {
706 if (str)
707 cstr_ccat(str, '\r');
708 } else {
709 file->line_num++;
710 goto add_char;
712 } else {
713 add_char:
714 if (str)
715 cstr_ccat(str, c);
716 p++;
719 p++;
720 return p;
723 /* skip block of text until #else, #elif or #endif. skip also pairs of
724 #if/#endif */
725 static void preprocess_skip(void)
727 int a, start_of_line, c, in_warn_or_error;
728 uint8_t *p;
730 p = file->buf_ptr;
731 a = 0;
732 redo_start:
733 start_of_line = 1;
734 in_warn_or_error = 0;
735 for(;;) {
736 redo_no_start:
737 c = *p;
738 switch(c) {
739 case ' ':
740 case '\t':
741 case '\f':
742 case '\v':
743 case '\r':
744 p++;
745 goto redo_no_start;
746 case '\n':
747 file->line_num++;
748 p++;
749 goto redo_start;
750 case '\\':
751 file->buf_ptr = p;
752 c = handle_eob();
753 if (c == CH_EOF) {
754 expect("#endif");
755 } else if (c == '\\') {
756 ch = file->buf_ptr[0];
757 handle_stray_noerror();
759 p = file->buf_ptr;
760 goto redo_no_start;
761 /* skip strings */
762 case '\"':
763 case '\'':
764 if (in_warn_or_error)
765 goto _default;
766 p = parse_pp_string(p, c, NULL);
767 break;
768 /* skip comments */
769 case '/':
770 if (in_warn_or_error)
771 goto _default;
772 file->buf_ptr = p;
773 ch = *p;
774 minp();
775 p = file->buf_ptr;
776 if (ch == '*') {
777 p = parse_comment(p);
778 } else if (ch == '/') {
779 p = parse_line_comment(p);
781 break;
782 case '#':
783 p++;
784 if (start_of_line) {
785 file->buf_ptr = p;
786 next_nomacro();
787 p = file->buf_ptr;
788 if (a == 0 &&
789 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
790 goto the_end;
791 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
792 a++;
793 else if (tok == TOK_ENDIF)
794 a--;
795 else if( tok == TOK_ERROR || tok == TOK_WARNING)
796 in_warn_or_error = 1;
797 else if (tok == TOK_LINEFEED)
798 goto redo_start;
799 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
800 p = parse_line_comment(p);
801 break;
802 _default:
803 default:
804 p++;
805 break;
807 start_of_line = 0;
809 the_end: ;
810 file->buf_ptr = p;
813 /* ParseState handling */
815 /* XXX: currently, no include file info is stored. Thus, we cannot display
816 accurate messages if the function or data definition spans multiple
817 files */
819 /* save current parse state in 's' */
820 ST_FUNC void save_parse_state(ParseState *s)
822 s->line_num = file->line_num;
823 s->macro_ptr = macro_ptr;
824 s->tok = tok;
825 s->tokc = tokc;
828 /* restore parse state from 's' */
829 ST_FUNC void restore_parse_state(ParseState *s)
831 file->line_num = s->line_num;
832 macro_ptr = s->macro_ptr;
833 tok = s->tok;
834 tokc = s->tokc;
837 /* return the number of additional 'ints' necessary to store the
838 token */
839 static inline int tok_size(const int *p)
841 switch(*p) {
842 /* 4 bytes */
843 case TOK_CINT:
844 case TOK_CUINT:
845 case TOK_CCHAR:
846 case TOK_LCHAR:
847 case TOK_CFLOAT:
848 case TOK_LINENUM:
849 return 1 + 1;
850 case TOK_STR:
851 case TOK_LSTR:
852 case TOK_PPNUM:
853 case TOK_PPSTR:
854 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
855 case TOK_CDOUBLE:
856 case TOK_CLLONG:
857 case TOK_CULLONG:
858 return 1 + 2;
859 case TOK_CLDOUBLE:
860 return 1 + LDOUBLE_SIZE / 4;
861 default:
862 return 1 + 0;
866 /* token string handling */
868 ST_INLN void tok_str_new(TokenString *s)
870 s->str = NULL;
871 s->len = 0;
872 s->allocated_len = 0;
873 s->last_line_num = -1;
876 ST_FUNC void tok_str_free(int *str)
878 tcc_free(str);
881 static int *tok_str_realloc(TokenString *s)
883 int *str, len;
885 if (s->allocated_len == 0) {
886 len = 8;
887 } else {
888 len = s->allocated_len * 2;
890 str = tcc_realloc(s->str, len * sizeof(int));
891 s->allocated_len = len;
892 s->str = str;
893 return str;
896 ST_FUNC void tok_str_add(TokenString *s, int t)
898 int len, *str;
900 len = s->len;
901 str = s->str;
902 if (len >= s->allocated_len)
903 str = tok_str_realloc(s);
904 str[len++] = t;
905 s->len = len;
908 static void tok_str_add2(TokenString *s, int t, CValue *cv)
910 int len, *str;
912 len = s->len;
913 str = s->str;
915 /* allocate space for worst case */
916 if (len + TOK_MAX_SIZE > s->allocated_len)
917 str = tok_str_realloc(s);
918 str[len++] = t;
919 switch(t) {
920 case TOK_CINT:
921 case TOK_CUINT:
922 case TOK_CCHAR:
923 case TOK_LCHAR:
924 case TOK_CFLOAT:
925 case TOK_LINENUM:
926 str[len++] = cv->tab[0];
927 break;
928 case TOK_PPNUM:
929 case TOK_PPSTR:
930 case TOK_STR:
931 case TOK_LSTR:
933 /* Insert the string into the int array. */
934 size_t nb_words =
935 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
936 while ((len + nb_words) > s->allocated_len)
937 str = tok_str_realloc(s);
938 str[len] = cv->str.size;
939 memcpy(&str[len + 1], cv->str.data, cv->str.size);
940 len += nb_words;
942 break;
943 case TOK_CDOUBLE:
944 case TOK_CLLONG:
945 case TOK_CULLONG:
946 #if LDOUBLE_SIZE == 8
947 case TOK_CLDOUBLE:
948 #endif
949 str[len++] = cv->tab[0];
950 str[len++] = cv->tab[1];
951 break;
952 #if LDOUBLE_SIZE == 12
953 case TOK_CLDOUBLE:
954 str[len++] = cv->tab[0];
955 str[len++] = cv->tab[1];
956 str[len++] = cv->tab[2];
957 #elif LDOUBLE_SIZE == 16
958 case TOK_CLDOUBLE:
959 str[len++] = cv->tab[0];
960 str[len++] = cv->tab[1];
961 str[len++] = cv->tab[2];
962 str[len++] = cv->tab[3];
963 #elif LDOUBLE_SIZE != 8
964 #error add long double size support
965 #endif
966 break;
967 default:
968 break;
970 s->len = len;
973 /* add the current parse token in token string 's' */
974 ST_FUNC void tok_str_add_tok(TokenString *s)
976 CValue cval;
978 /* save line number info */
979 if (file->line_num != s->last_line_num) {
980 s->last_line_num = file->line_num;
981 cval.i = s->last_line_num;
982 tok_str_add2(s, TOK_LINENUM, &cval);
984 tok_str_add2(s, tok, &tokc);
987 /* get a token from an integer array and increment pointer
988 accordingly. we code it as a macro to avoid pointer aliasing. */
989 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
991 const int *p = *pp;
992 int n, *tab;
994 tab = cv->tab;
995 switch(*t = *p++) {
996 case TOK_CINT:
997 case TOK_CUINT:
998 case TOK_CCHAR:
999 case TOK_LCHAR:
1000 case TOK_CFLOAT:
1001 case TOK_LINENUM:
1002 tab[0] = *p++;
1003 break;
1004 case TOK_STR:
1005 case TOK_LSTR:
1006 case TOK_PPNUM:
1007 case TOK_PPSTR:
1008 cv->str.size = *p++;
1009 cv->str.data = p;
1010 cv->str.data_allocated = 0;
1011 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1012 break;
1013 case TOK_CDOUBLE:
1014 case TOK_CLLONG:
1015 case TOK_CULLONG:
1016 n = 2;
1017 goto copy;
1018 case TOK_CLDOUBLE:
1019 #if LDOUBLE_SIZE == 16
1020 n = 4;
1021 #elif LDOUBLE_SIZE == 12
1022 n = 3;
1023 #elif LDOUBLE_SIZE == 8
1024 n = 2;
1025 #else
1026 # error add long double size support
1027 #endif
1028 copy:
1030 *tab++ = *p++;
1031 while (--n);
1032 break;
1033 default:
1034 break;
1036 *pp = p;
1039 /* Calling this function is expensive, but it is not possible
1040 to read a token string backwards. */
1041 static int tok_last(const int *str0, const int *str1)
1043 const int *str = str0;
1044 int tok = 0;
1045 CValue cval;
1047 while (str < str1)
1048 TOK_GET(&tok, &str, &cval);
1049 return tok;
1052 static int macro_is_equal(const int *a, const int *b)
1054 static CString cstr_buf;
1055 CValue cv;
1056 int t;
1057 while (*a && *b) {
1058 /* first time preallocate static cstr_buf, next time only reset position to start */
1059 if (!cstr_buf.data_allocated)
1060 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
1061 else
1062 cstr_buf.size = 0;
1063 TOK_GET(&t, &a, &cv);
1064 cstr_cat(&cstr_buf, get_tok_str(t, &cv));
1065 cstr_ccat(&cstr_buf, '\0');
1066 TOK_GET(&t, &b, &cv);
1067 if (strcmp(cstr_buf.data, get_tok_str(t, &cv)))
1068 return 0;
1070 return !(*a || *b);
1073 static void pp_line(TCCState *s1, BufferedFile *f, int level)
1075 if (s1->ppfp) {
1076 int d = f->line_num - f->line_ref;
1077 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE
1078 || (level == 0 && f->line_ref && d < 8)) {
1079 while (d > 0)
1080 fputs("\n", s1->ppfp), --d;
1081 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
1082 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
1083 } else {
1084 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
1085 level > 0 ? " 1" : level < 0 ? " 2" : "");
1088 f->line_ref = f->line_num;
1091 static void tok_print(const char *msg, const int *str)
1093 FILE *pr = tcc_state->dffp;
1094 int t;
1095 CValue cval;
1097 fprintf(pr, "%s ", msg);
1098 while (str) {
1099 TOK_GET(&t, &str, &cval);
1100 if (!t)
1101 break;
1102 fprintf(pr,"%s", get_tok_str(t, &cval));
1104 fprintf(pr, "\n");
1107 static int define_print_prepared(Sym *s)
1109 if (!s || !tcc_state->dffp || tcc_state->dflag == 0)
1110 return 0;
1112 if (s->v < TOK_IDENT || s->v >= tok_ident)
1113 return 0;
1115 if (file && tcc_state->dflag == 'D') {
1116 file->line_num--;
1117 pp_line(tcc_state, file, 0);
1118 file->line_ref = ++file->line_num;
1120 return 1;
1123 static void define_print(int v)
1125 FILE *pr = tcc_state->dffp;
1126 Sym *s, *a;
1128 s = define_find(v);
1129 if (define_print_prepared(s) == 0)
1130 return;
1132 fprintf(pr, "// #define %s", get_tok_str(v, NULL));
1133 if (s->type.t == MACRO_FUNC) {
1134 a = s->next;
1135 fprintf(pr,"(");
1136 if (a)
1137 for (;;) {
1138 fprintf(pr,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
1139 if (!(a = a->next))
1140 break;
1141 fprintf(pr,",");
1143 fprintf(pr,")");
1145 tok_print("", s->d);
1148 static void undef_print(int v)
1150 FILE *pr = tcc_state->dffp;
1151 Sym *s;
1153 s = define_find(v);
1154 if (define_print_prepared(s) == 0)
1155 return;
1157 fprintf(pr, "// #undef %s\n", get_tok_str(s->v, NULL));
1160 ST_FUNC void print_defines(void)
1162 Sym *top = define_stack;
1163 while (top) {
1164 define_print(top->v);
1165 top = top->prev;
1169 /* defines handling */
1170 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1172 Sym *s;
1174 s = define_find(v);
1175 if (s && !macro_is_equal(s->d, str))
1176 tcc_warning("%s redefined", get_tok_str(v, NULL));
1178 s = sym_push2(&define_stack, v, macro_type, 0);
1179 s->d = str;
1180 s->next = first_arg;
1181 table_ident[v - TOK_IDENT]->sym_define = s;
1184 /* undefined a define symbol. Its name is just set to zero */
1185 ST_FUNC void define_undef(Sym *s)
1187 int v = s->v;
1188 undef_print(v);
1189 if (v >= TOK_IDENT && v < tok_ident)
1190 table_ident[v - TOK_IDENT]->sym_define = NULL;
1193 ST_INLN Sym *define_find(int v)
1195 v -= TOK_IDENT;
1196 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1197 return NULL;
1198 return table_ident[v]->sym_define;
1201 /* free define stack until top reaches 'b' */
1202 ST_FUNC void free_defines(Sym *b)
1204 Sym *top, *top1;
1205 int v;
1207 top = define_stack;
1208 while (top != b) {
1209 top1 = top->prev;
1210 /* do not free args or predefined defines */
1211 if (top->d)
1212 tok_str_free(top->d);
1213 v = top->v;
1214 if (v >= TOK_IDENT && v < tok_ident)
1215 table_ident[v - TOK_IDENT]->sym_define = NULL;
1216 sym_free(top);
1217 top = top1;
1219 define_stack = b;
1222 /* label lookup */
1223 ST_FUNC Sym *label_find(int v)
1225 v -= TOK_IDENT;
1226 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1227 return NULL;
1228 return table_ident[v]->sym_label;
1231 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1233 Sym *s, **ps;
1234 s = sym_push2(ptop, v, 0, 0);
1235 s->r = flags;
1236 ps = &table_ident[v - TOK_IDENT]->sym_label;
1237 if (ptop == &global_label_stack) {
1238 /* modify the top most local identifier, so that
1239 sym_identifier will point to 's' when popped */
1240 while (*ps != NULL)
1241 ps = &(*ps)->prev_tok;
1243 s->prev_tok = *ps;
1244 *ps = s;
1245 return s;
1248 /* pop labels until element last is reached. Look if any labels are
1249 undefined. Define symbols if '&&label' was used. */
1250 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1252 Sym *s, *s1;
1253 for(s = *ptop; s != slast; s = s1) {
1254 s1 = s->prev;
1255 if (s->r == LABEL_DECLARED) {
1256 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1257 } else if (s->r == LABEL_FORWARD) {
1258 tcc_error("label '%s' used but not defined",
1259 get_tok_str(s->v, NULL));
1260 } else {
1261 if (s->c) {
1262 /* define corresponding symbol. A size of
1263 1 is put. */
1264 put_extern_sym(s, cur_text_section, s->jnext, 1);
1267 /* remove label */
1268 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1269 sym_free(s);
1271 *ptop = slast;
1274 /* eval an expression for #if/#elif */
1275 static int expr_preprocess(void)
1277 int c, t;
1278 TokenString str;
1280 tok_str_new(&str);
1281 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1282 next(); /* do macro subst */
1283 if (tok == TOK_DEFINED) {
1284 next_nomacro();
1285 t = tok;
1286 if (t == '(')
1287 next_nomacro();
1288 c = define_find(tok) != 0;
1289 if (t == '(')
1290 next_nomacro();
1291 tok = TOK_CINT;
1292 tokc.i = c;
1293 } else if (tok >= TOK_IDENT) {
1294 /* if undefined macro */
1295 tok = TOK_CINT;
1296 tokc.i = 0;
1298 tok_str_add_tok(&str);
1300 tok_str_add(&str, -1); /* simulate end of file */
1301 tok_str_add(&str, 0);
1302 /* now evaluate C constant expression */
1303 begin_macro(&str, 0);
1304 next();
1305 c = expr_const();
1306 end_macro();
1307 return c != 0;
1311 /* parse after #define */
1312 ST_FUNC void parse_define(void)
1314 Sym *s, *first, **ps;
1315 int v, t, varg, is_vaargs, spc;
1316 int saved_parse_flags = parse_flags;
1317 TokenString str;
1319 v = tok;
1320 if (v < TOK_IDENT)
1321 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1322 /* XXX: should check if same macro (ANSI) */
1323 first = NULL;
1324 t = MACRO_OBJ;
1325 /* '(' must be just after macro definition for MACRO_FUNC */
1326 parse_flags |= PARSE_FLAG_SPACES;
1327 next_nomacro_spc();
1328 if (tok == '(') {
1329 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1330 parse_flags &= ~PARSE_FLAG_ASM_FILE;
1331 isidnum_table['.' - CH_EOF] = 0;
1332 next_nomacro();
1333 ps = &first;
1334 if (tok != ')') for (;;) {
1335 varg = tok;
1336 next_nomacro();
1337 is_vaargs = 0;
1338 if (varg == TOK_DOTS) {
1339 varg = TOK___VA_ARGS__;
1340 is_vaargs = 1;
1341 } else if (tok == TOK_DOTS && gnu_ext) {
1342 is_vaargs = 1;
1343 next_nomacro();
1345 if (varg < TOK_IDENT)
1346 bad_list:
1347 tcc_error("bad macro parameter list");
1348 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1349 *ps = s;
1350 ps = &s->next;
1351 if (tok == ')')
1352 break;
1353 if (tok != ',' || is_vaargs)
1354 goto bad_list;
1355 next_nomacro();
1357 next_nomacro_spc();
1358 t = MACRO_FUNC;
1359 parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
1360 isidnum_table['.' - CH_EOF] =
1361 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
1363 tok_str_new(&str);
1364 spc = 2;
1365 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1366 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1367 /* remove spaces around ## and after '#' */
1368 if (TOK_TWOSHARPS == tok) {
1369 if (2 == spc)
1370 goto bad_twosharp;
1371 if (1 == spc)
1372 --str.len;
1373 spc = 3;
1374 } else if ('#' == tok) {
1375 spc = 4;
1376 } else if (check_space(tok, &spc)) {
1377 goto skip;
1379 tok_str_add2(&str, tok, &tokc);
1380 skip:
1381 next_nomacro_spc();
1384 parse_flags = saved_parse_flags;
1385 if (spc == 1)
1386 --str.len; /* remove trailing space */
1387 tok_str_add(&str, 0);
1388 if (3 == spc)
1389 bad_twosharp:
1390 tcc_error("'##' cannot appear at either end of macro");
1391 define_push(v, t, str.str, first);
1392 define_print(v);
1395 static inline int hash_cached_include(const char *filename)
1397 const unsigned char *s;
1398 unsigned int h;
1400 h = TOK_HASH_INIT;
1401 s = (unsigned char *) filename;
1402 while (*s) {
1403 h = TOK_HASH_FUNC(h, *s);
1404 s++;
1406 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1407 return h;
1410 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1412 CachedInclude *e;
1413 int i, h;
1414 h = hash_cached_include(filename);
1415 i = s1->cached_includes_hash[h];
1416 for(;;) {
1417 if (i == 0)
1418 break;
1419 e = s1->cached_includes[i - 1];
1420 if (0 == PATHCMP(e->filename, filename))
1421 return e;
1422 i = e->hash_next;
1424 return NULL;
1427 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1429 CachedInclude *e;
1430 int h;
1432 if (search_cached_include(s1, filename))
1433 return;
1434 #ifdef INC_DEBUG
1435 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1436 #endif
1437 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1438 strcpy(e->filename, filename);
1439 e->ifndef_macro = ifndef_macro;
1440 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1441 /* add in hash table */
1442 h = hash_cached_include(filename);
1443 e->hash_next = s1->cached_includes_hash[h];
1444 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1447 #define ONCE_PREFIX "#ONCE#"
1449 static void pragma_parse(TCCState *s1)
1451 next_nomacro();
1452 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1453 int t = tok, v;
1454 Sym *s;
1456 if (next(), tok != '(')
1457 goto pragma_err;
1458 if (next(), tok != TOK_STR)
1459 goto pragma_err;
1460 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1461 if (next(), tok != ')')
1462 goto pragma_err;
1463 if (t == TOK_push_macro) {
1464 while (NULL == (s = define_find(v)))
1465 define_push(v, 0, NULL, NULL);
1466 s->type.ref = s; /* set push boundary */
1467 } else {
1468 for (s = define_stack; s; s = s->prev)
1469 if (s->v == v && s->type.ref == s) {
1470 s->type.ref = NULL;
1471 break;
1474 if (s)
1475 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1476 else
1477 tcc_warning("unbalanced #pragma pop_macro");
1479 } else if (tok == TOK_once) {
1480 char buf1[sizeof(file->filename) + sizeof(ONCE_PREFIX)];
1481 strcpy(buf1, ONCE_PREFIX);
1482 strcat(buf1, file->filename);
1483 #if PATHCMP==stricmp
1484 strupr(buf1);
1485 #endif
1486 add_cached_include(s1, file->filename, tok_alloc(buf1, strlen(buf1))->tok);
1487 } else if (s1->ppfp) {
1488 /* tcc -E: keep pragmas below unchanged */
1489 unget_tok(' ');
1490 unget_tok(TOK_PRAGMA);
1491 unget_tok('#');
1492 unget_tok(TOK_LINEFEED);
1494 } else if (tok == TOK_pack) {
1495 /* This may be:
1496 #pragma pack(1) // set
1497 #pragma pack() // reset to default
1498 #pragma pack(push,1) // push & set
1499 #pragma pack(pop) // restore previous */
1500 next();
1501 skip('(');
1502 if (tok == TOK_ASM_pop) {
1503 next();
1504 if (s1->pack_stack_ptr <= s1->pack_stack) {
1505 stk_error:
1506 tcc_error("out of pack stack");
1508 s1->pack_stack_ptr--;
1509 } else {
1510 int val = 0;
1511 if (tok != ')') {
1512 if (tok == TOK_ASM_push) {
1513 next();
1514 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1515 goto stk_error;
1516 s1->pack_stack_ptr++;
1517 skip(',');
1519 if (tok != TOK_CINT)
1520 goto pragma_err;
1521 val = tokc.i;
1522 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1523 goto pragma_err;
1524 next();
1526 *s1->pack_stack_ptr = val;
1528 if (tok != ')')
1529 goto pragma_err;
1531 } else if (tok == TOK_comment) {
1532 char *file;
1533 next();
1534 skip('(');
1535 if (tok != TOK_lib)
1536 goto pragma_warn;
1537 next();
1538 skip(',');
1539 if (tok != TOK_STR)
1540 goto pragma_err;
1541 file = tcc_strdup((char *)tokc.str.data);
1542 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1543 next();
1544 if (tok != ')')
1545 goto pragma_err;
1546 } else {
1547 pragma_warn:
1548 if (s1->warn_unsupported)
1549 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1551 return;
1553 pragma_err:
1554 tcc_error("malformed #pragma directive");
1555 return;
1558 /* is_bof is true if first non space token at beginning of file */
1559 ST_FUNC void preprocess(int is_bof)
1561 TCCState *s1 = tcc_state;
1562 int i, c, n, saved_parse_flags;
1563 char buf[1024], *q;
1564 Sym *s;
1566 saved_parse_flags = parse_flags;
1567 parse_flags = PARSE_FLAG_PREPROCESS
1568 | PARSE_FLAG_TOK_NUM
1569 | PARSE_FLAG_TOK_STR
1570 | PARSE_FLAG_LINEFEED
1571 | (parse_flags & PARSE_FLAG_ASM_FILE)
1574 next_nomacro();
1575 redo:
1576 switch(tok) {
1577 case TOK_DEFINE:
1578 next_nomacro();
1579 parse_define();
1580 break;
1581 case TOK_UNDEF:
1582 next_nomacro();
1583 s = define_find(tok);
1584 /* undefine symbol by putting an invalid name */
1585 if (s)
1586 define_undef(s);
1587 break;
1588 case TOK_INCLUDE:
1589 case TOK_INCLUDE_NEXT:
1590 ch = file->buf_ptr[0];
1591 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1592 skip_spaces();
1593 if (ch == '<') {
1594 c = '>';
1595 goto read_name;
1596 } else if (ch == '\"') {
1597 c = ch;
1598 read_name:
1599 inp();
1600 q = buf;
1601 while (ch != c && ch != '\n' && ch != CH_EOF) {
1602 if ((q - buf) < sizeof(buf) - 1)
1603 *q++ = ch;
1604 if (ch == '\\') {
1605 if (handle_stray_noerror() == 0)
1606 --q;
1607 } else
1608 inp();
1610 *q = '\0';
1611 minp();
1612 #if 0
1613 /* eat all spaces and comments after include */
1614 /* XXX: slightly incorrect */
1615 while (ch1 != '\n' && ch1 != CH_EOF)
1616 inp();
1617 #endif
1618 } else {
1619 /* computed #include : either we have only strings or
1620 we have anything enclosed in '<>' */
1621 next();
1622 buf[0] = '\0';
1623 if (tok == TOK_STR) {
1624 while (tok != TOK_LINEFEED) {
1625 if (tok != TOK_STR) {
1626 include_syntax:
1627 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1629 pstrcat(buf, sizeof(buf), (char *)tokc.str.data);
1630 next();
1632 c = '\"';
1633 } else {
1634 int len;
1635 while (tok != TOK_LINEFEED) {
1636 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1637 next();
1639 len = strlen(buf);
1640 /* check syntax and remove '<>' */
1641 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1642 goto include_syntax;
1643 memmove(buf, buf + 1, len - 2);
1644 buf[len - 2] = '\0';
1645 c = '>';
1649 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1650 tcc_error("#include recursion too deep");
1651 /* store current file in stack, but increment stack later below */
1652 *s1->include_stack_ptr = file;
1653 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1654 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1655 for (; i < n; ++i) {
1656 char buf1[sizeof file->filename];
1657 CachedInclude *e;
1658 const char *path;
1660 if (i == 0) {
1661 /* check absolute include path */
1662 if (!IS_ABSPATH(buf))
1663 continue;
1664 buf1[0] = 0;
1666 } else if (i == 1) {
1667 /* search in current dir if "header.h" */
1668 if (c != '\"')
1669 continue;
1670 path = file->filename;
1671 pstrncpy(buf1, path, tcc_basename(path) - path);
1673 } else {
1674 /* search in all the include paths */
1675 int j = i - 2, k = j - s1->nb_include_paths;
1676 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1677 if (path == 0) continue;
1678 pstrcpy(buf1, sizeof(buf1), path);
1679 pstrcat(buf1, sizeof(buf1), "/");
1682 pstrcat(buf1, sizeof(buf1), buf);
1683 e = search_cached_include(s1, buf1);
1684 if (e && define_find(e->ifndef_macro)) {
1685 /* no need to parse the include because the 'ifndef macro'
1686 is defined */
1687 #ifdef INC_DEBUG
1688 printf("%s: skipping cached %s\n", file->filename, buf1);
1689 #endif
1690 goto include_done;
1693 if (tcc_open(s1, buf1) < 0)
1694 continue;
1696 file->include_next_index = i + 1;
1697 #ifdef INC_DEBUG
1698 printf("%s: including %s\n", file->prev->filename, file->filename);
1699 #endif
1700 /* update target deps */
1701 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1702 tcc_strdup(buf1));
1703 /* push current file in stack */
1704 ++s1->include_stack_ptr;
1705 /* add include file debug info */
1706 if (s1->do_debug)
1707 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1708 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1709 ch = file->buf_ptr[0];
1710 goto the_end;
1712 tcc_error("include file '%s' not found", buf);
1713 include_done:
1714 break;
1715 case TOK_IFNDEF:
1716 c = 1;
1717 goto do_ifdef;
1718 case TOK_IF:
1719 c = expr_preprocess();
1720 goto do_if;
1721 case TOK_IFDEF:
1722 c = 0;
1723 do_ifdef:
1724 next_nomacro();
1725 if (tok < TOK_IDENT)
1726 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1727 if (is_bof) {
1728 if (c) {
1729 #ifdef INC_DEBUG
1730 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1731 #endif
1732 file->ifndef_macro = tok;
1735 c = (define_find(tok) != 0) ^ c;
1736 do_if:
1737 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1738 tcc_error("memory full (ifdef)");
1739 *s1->ifdef_stack_ptr++ = c;
1740 goto test_skip;
1741 case TOK_ELSE:
1742 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1743 tcc_error("#else without matching #if");
1744 if (s1->ifdef_stack_ptr[-1] & 2)
1745 tcc_error("#else after #else");
1746 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1747 goto test_else;
1748 case TOK_ELIF:
1749 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1750 tcc_error("#elif without matching #if");
1751 c = s1->ifdef_stack_ptr[-1];
1752 if (c > 1)
1753 tcc_error("#elif after #else");
1754 /* last #if/#elif expression was true: we skip */
1755 if (c == 1)
1756 goto skip;
1757 c = expr_preprocess();
1758 s1->ifdef_stack_ptr[-1] = c;
1759 test_else:
1760 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1761 file->ifndef_macro = 0;
1762 test_skip:
1763 if (!(c & 1)) {
1764 skip:
1765 preprocess_skip();
1766 is_bof = 0;
1767 goto redo;
1769 break;
1770 case TOK_ENDIF:
1771 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1772 tcc_error("#endif without matching #if");
1773 s1->ifdef_stack_ptr--;
1774 /* '#ifndef macro' was at the start of file. Now we check if
1775 an '#endif' is exactly at the end of file */
1776 if (file->ifndef_macro &&
1777 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1778 file->ifndef_macro_saved = file->ifndef_macro;
1779 /* need to set to zero to avoid false matches if another
1780 #ifndef at middle of file */
1781 file->ifndef_macro = 0;
1782 while (tok != TOK_LINEFEED)
1783 next_nomacro();
1784 tok_flags |= TOK_FLAG_ENDIF;
1785 goto the_end;
1787 break;
1788 case TOK_PPNUM:
1789 n = strtoul((char*)tokc.str.data, &q, 10);
1790 goto _line_num;
1791 case TOK_LINE:
1792 next();
1793 if (tok != TOK_CINT)
1794 _line_err:
1795 tcc_error("wrong #line format");
1796 n = tokc.i;
1797 _line_num:
1798 next();
1799 if (tok != TOK_LINEFEED) {
1800 if (tok == TOK_STR)
1801 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1802 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1803 break;
1804 else
1805 goto _line_err;
1806 --n;
1808 if (file->fd > 0)
1809 total_lines += file->line_num - n;
1810 file->line_num = n;
1811 if (s1->do_debug)
1812 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1813 break;
1814 case TOK_ERROR:
1815 case TOK_WARNING:
1816 c = tok;
1817 ch = file->buf_ptr[0];
1818 skip_spaces();
1819 q = buf;
1820 while (ch != '\n' && ch != CH_EOF) {
1821 if ((q - buf) < sizeof(buf) - 1)
1822 *q++ = ch;
1823 if (ch == '\\') {
1824 if (handle_stray_noerror() == 0)
1825 --q;
1826 } else
1827 inp();
1829 *q = '\0';
1830 if (c == TOK_ERROR)
1831 tcc_error("#error %s", buf);
1832 else
1833 tcc_warning("#warning %s", buf);
1834 break;
1835 case TOK_PRAGMA:
1836 pragma_parse(s1);
1837 break;
1838 case TOK_LINEFEED:
1839 goto the_end;
1840 default:
1841 /* ignore gas line comment in an 'S' file. */
1842 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1843 goto ignore;
1844 if (tok == '!' && is_bof)
1845 /* '!' is ignored at beginning to allow C scripts. */
1846 goto ignore;
1847 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1848 ignore:
1849 file->buf_ptr = parse_line_comment(file->buf_ptr);
1850 goto the_end;
1852 /* ignore other preprocess commands or #! for C scripts */
1853 while (tok != TOK_LINEFEED)
1854 next_nomacro();
1855 the_end:
1856 parse_flags = saved_parse_flags;
1859 /* evaluate escape codes in a string. */
1860 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1862 int c, n;
1863 const uint8_t *p;
1865 p = buf;
1866 for(;;) {
1867 c = *p;
1868 if (c == '\0')
1869 break;
1870 if (c == '\\') {
1871 p++;
1872 /* escape */
1873 c = *p;
1874 switch(c) {
1875 case '0': case '1': case '2': case '3':
1876 case '4': case '5': case '6': case '7':
1877 /* at most three octal digits */
1878 n = c - '0';
1879 p++;
1880 c = *p;
1881 if (isoct(c)) {
1882 n = n * 8 + c - '0';
1883 p++;
1884 c = *p;
1885 if (isoct(c)) {
1886 n = n * 8 + c - '0';
1887 p++;
1890 c = n;
1891 goto add_char_nonext;
1892 case 'x':
1893 case 'u':
1894 case 'U':
1895 p++;
1896 n = 0;
1897 for(;;) {
1898 c = *p;
1899 if (c >= 'a' && c <= 'f')
1900 c = c - 'a' + 10;
1901 else if (c >= 'A' && c <= 'F')
1902 c = c - 'A' + 10;
1903 else if (isnum(c))
1904 c = c - '0';
1905 else
1906 break;
1907 n = n * 16 + c;
1908 p++;
1910 c = n;
1911 goto add_char_nonext;
1912 case 'a':
1913 c = '\a';
1914 break;
1915 case 'b':
1916 c = '\b';
1917 break;
1918 case 'f':
1919 c = '\f';
1920 break;
1921 case 'n':
1922 c = '\n';
1923 break;
1924 case 'r':
1925 c = '\r';
1926 break;
1927 case 't':
1928 c = '\t';
1929 break;
1930 case 'v':
1931 c = '\v';
1932 break;
1933 case 'e':
1934 if (!gnu_ext)
1935 goto invalid_escape;
1936 c = 27;
1937 break;
1938 case '\'':
1939 case '\"':
1940 case '\\':
1941 case '?':
1942 break;
1943 default:
1944 invalid_escape:
1945 if (c >= '!' && c <= '~')
1946 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1947 else
1948 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1949 break;
1952 p++;
1953 add_char_nonext:
1954 if (!is_long)
1955 cstr_ccat(outstr, c);
1956 else
1957 cstr_wccat(outstr, c);
1959 /* add a trailing '\0' */
1960 if (!is_long)
1961 cstr_ccat(outstr, '\0');
1962 else
1963 cstr_wccat(outstr, '\0');
1966 void parse_string(const char *s, int len)
1968 uint8_t buf[1000], *p = buf;
1969 int is_long, sep;
1971 if ((is_long = *s == 'L'))
1972 ++s, --len;
1973 sep = *s++;
1974 len -= 2;
1975 if (len >= sizeof buf)
1976 p = tcc_malloc(len + 1);
1977 memcpy(p, s, len);
1978 p[len] = 0;
1980 cstr_reset(&tokcstr);
1981 parse_escape_string(&tokcstr, p, is_long);
1982 if (p != buf)
1983 tcc_free(p);
1985 if (sep == '\'') {
1986 int char_size;
1987 /* XXX: make it portable */
1988 if (!is_long)
1989 char_size = 1;
1990 else
1991 char_size = sizeof(nwchar_t);
1992 if (tokcstr.size <= char_size)
1993 tcc_error("empty character constant");
1994 if (tokcstr.size > 2 * char_size)
1995 tcc_warning("multi-character character constant");
1996 if (!is_long) {
1997 tokc.i = *(int8_t *)tokcstr.data;
1998 tok = TOK_CCHAR;
1999 } else {
2000 tokc.i = *(nwchar_t *)tokcstr.data;
2001 tok = TOK_LCHAR;
2003 } else {
2004 tokc.str.size = tokcstr.size;
2005 tokc.str.data = tokcstr.data;
2006 tokc.str.data_allocated = tokcstr.data_allocated;
2007 if (!is_long)
2008 tok = TOK_STR;
2009 else
2010 tok = TOK_LSTR;
2014 /* we use 64 bit numbers */
2015 #define BN_SIZE 2
2017 /* bn = (bn << shift) | or_val */
2018 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2020 int i;
2021 unsigned int v;
2022 for(i=0;i<BN_SIZE;i++) {
2023 v = bn[i];
2024 bn[i] = (v << shift) | or_val;
2025 or_val = v >> (32 - shift);
2029 static void bn_zero(unsigned int *bn)
2031 int i;
2032 for(i=0;i<BN_SIZE;i++) {
2033 bn[i] = 0;
2037 /* parse number in null terminated string 'p' and return it in the
2038 current token */
2039 static void parse_number(const char *p)
2041 int b, t, shift, frac_bits, s, exp_val, ch;
2042 char *q;
2043 unsigned int bn[BN_SIZE];
2044 double d;
2046 /* number */
2047 q = token_buf;
2048 ch = *p++;
2049 t = ch;
2050 ch = *p++;
2051 *q++ = t;
2052 b = 10;
2053 if (t == '.') {
2054 goto float_frac_parse;
2055 } else if (t == '0') {
2056 if (ch == 'x' || ch == 'X') {
2057 q--;
2058 ch = *p++;
2059 b = 16;
2060 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2061 q--;
2062 ch = *p++;
2063 b = 2;
2066 /* parse all digits. cannot check octal numbers at this stage
2067 because of floating point constants */
2068 while (1) {
2069 if (ch >= 'a' && ch <= 'f')
2070 t = ch - 'a' + 10;
2071 else if (ch >= 'A' && ch <= 'F')
2072 t = ch - 'A' + 10;
2073 else if (isnum(ch))
2074 t = ch - '0';
2075 else
2076 break;
2077 if (t >= b)
2078 break;
2079 if (q >= token_buf + STRING_MAX_SIZE) {
2080 num_too_long:
2081 tcc_error("number too long");
2083 *q++ = ch;
2084 ch = *p++;
2086 if (ch == '.' ||
2087 ((ch == 'e' || ch == 'E') && b == 10) ||
2088 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2089 if (b != 10) {
2090 /* NOTE: strtox should support that for hexa numbers, but
2091 non ISOC99 libcs do not support it, so we prefer to do
2092 it by hand */
2093 /* hexadecimal or binary floats */
2094 /* XXX: handle overflows */
2095 *q = '\0';
2096 if (b == 16)
2097 shift = 4;
2098 else
2099 shift = 1;
2100 bn_zero(bn);
2101 q = token_buf;
2102 while (1) {
2103 t = *q++;
2104 if (t == '\0') {
2105 break;
2106 } else if (t >= 'a') {
2107 t = t - 'a' + 10;
2108 } else if (t >= 'A') {
2109 t = t - 'A' + 10;
2110 } else {
2111 t = t - '0';
2113 bn_lshift(bn, shift, t);
2115 frac_bits = 0;
2116 if (ch == '.') {
2117 ch = *p++;
2118 while (1) {
2119 t = ch;
2120 if (t >= 'a' && t <= 'f') {
2121 t = t - 'a' + 10;
2122 } else if (t >= 'A' && t <= 'F') {
2123 t = t - 'A' + 10;
2124 } else if (t >= '0' && t <= '9') {
2125 t = t - '0';
2126 } else {
2127 break;
2129 if (t >= b)
2130 tcc_error("invalid digit");
2131 bn_lshift(bn, shift, t);
2132 frac_bits += shift;
2133 ch = *p++;
2136 if (ch != 'p' && ch != 'P')
2137 expect("exponent");
2138 ch = *p++;
2139 s = 1;
2140 exp_val = 0;
2141 if (ch == '+') {
2142 ch = *p++;
2143 } else if (ch == '-') {
2144 s = -1;
2145 ch = *p++;
2147 if (ch < '0' || ch > '9')
2148 expect("exponent digits");
2149 while (ch >= '0' && ch <= '9') {
2150 exp_val = exp_val * 10 + ch - '0';
2151 ch = *p++;
2153 exp_val = exp_val * s;
2155 /* now we can generate the number */
2156 /* XXX: should patch directly float number */
2157 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2158 d = ldexp(d, exp_val - frac_bits);
2159 t = toup(ch);
2160 if (t == 'F') {
2161 ch = *p++;
2162 tok = TOK_CFLOAT;
2163 /* float : should handle overflow */
2164 tokc.f = (float)d;
2165 } else if (t == 'L') {
2166 ch = *p++;
2167 #ifdef TCC_TARGET_PE
2168 tok = TOK_CDOUBLE;
2169 tokc.d = d;
2170 #else
2171 tok = TOK_CLDOUBLE;
2172 /* XXX: not large enough */
2173 tokc.ld = (long double)d;
2174 #endif
2175 } else {
2176 tok = TOK_CDOUBLE;
2177 tokc.d = d;
2179 } else {
2180 /* decimal floats */
2181 if (ch == '.') {
2182 if (q >= token_buf + STRING_MAX_SIZE)
2183 goto num_too_long;
2184 *q++ = ch;
2185 ch = *p++;
2186 float_frac_parse:
2187 while (ch >= '0' && ch <= '9') {
2188 if (q >= token_buf + STRING_MAX_SIZE)
2189 goto num_too_long;
2190 *q++ = ch;
2191 ch = *p++;
2194 if (ch == 'e' || ch == 'E') {
2195 if (q >= token_buf + STRING_MAX_SIZE)
2196 goto num_too_long;
2197 *q++ = ch;
2198 ch = *p++;
2199 if (ch == '-' || ch == '+') {
2200 if (q >= token_buf + STRING_MAX_SIZE)
2201 goto num_too_long;
2202 *q++ = ch;
2203 ch = *p++;
2205 if (ch < '0' || ch > '9')
2206 expect("exponent digits");
2207 while (ch >= '0' && ch <= '9') {
2208 if (q >= token_buf + STRING_MAX_SIZE)
2209 goto num_too_long;
2210 *q++ = ch;
2211 ch = *p++;
2214 *q = '\0';
2215 t = toup(ch);
2216 errno = 0;
2217 if (t == 'F') {
2218 ch = *p++;
2219 tok = TOK_CFLOAT;
2220 tokc.f = strtof(token_buf, NULL);
2221 } else if (t == 'L') {
2222 ch = *p++;
2223 #ifdef TCC_TARGET_PE
2224 tok = TOK_CDOUBLE;
2225 tokc.d = strtod(token_buf, NULL);
2226 #else
2227 tok = TOK_CLDOUBLE;
2228 tokc.ld = strtold(token_buf, NULL);
2229 #endif
2230 } else {
2231 tok = TOK_CDOUBLE;
2232 tokc.d = strtod(token_buf, NULL);
2235 } else {
2236 unsigned long long n, n1;
2237 int lcount, ucount, must_64bit;
2238 const char *p1;
2240 /* integer number */
2241 *q = '\0';
2242 q = token_buf;
2243 if (b == 10 && *q == '0') {
2244 b = 8;
2245 q++;
2247 n = 0;
2248 while(1) {
2249 t = *q++;
2250 /* no need for checks except for base 10 / 8 errors */
2251 if (t == '\0')
2252 break;
2253 else if (t >= 'a')
2254 t = t - 'a' + 10;
2255 else if (t >= 'A')
2256 t = t - 'A' + 10;
2257 else
2258 t = t - '0';
2259 if (t >= b)
2260 tcc_error("invalid digit");
2261 n1 = n;
2262 n = n * b + t;
2263 /* detect overflow */
2264 /* XXX: this test is not reliable */
2265 if (n < n1)
2266 tcc_error("integer constant overflow");
2269 /* Determine the characteristics (unsigned and/or 64bit) the type of
2270 the constant must have according to the constant suffix(es) */
2271 lcount = ucount = must_64bit = 0;
2272 p1 = p;
2273 for(;;) {
2274 t = toup(ch);
2275 if (t == 'L') {
2276 if (lcount >= 2)
2277 tcc_error("three 'l's in integer constant");
2278 if (lcount && *(p - 1) != ch)
2279 tcc_error("incorrect integer suffix: %s", p1);
2280 lcount++;
2281 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2282 if (lcount == 2)
2283 #endif
2284 must_64bit = 1;
2285 ch = *p++;
2286 } else if (t == 'U') {
2287 if (ucount >= 1)
2288 tcc_error("two 'u's in integer constant");
2289 ucount++;
2290 ch = *p++;
2291 } else {
2292 break;
2296 /* Whether 64 bits are needed to hold the constant's value */
2297 if (n & 0xffffffff00000000LL || must_64bit) {
2298 tok = TOK_CLLONG;
2299 n1 = n >> 32;
2300 } else {
2301 tok = TOK_CINT;
2302 n1 = n;
2305 /* Whether type must be unsigned to hold the constant's value */
2306 if (ucount || ((n1 >> 31) && (b != 10))) {
2307 if (tok == TOK_CLLONG)
2308 tok = TOK_CULLONG;
2309 else
2310 tok = TOK_CUINT;
2311 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2312 } else if (n1 >> 31) {
2313 if (tok == TOK_CINT)
2314 tok = TOK_CLLONG;
2315 else
2316 tcc_error("integer constant overflow");
2319 if (tok == TOK_CINT || tok == TOK_CUINT)
2320 tokc.i = n;
2321 else
2322 tokc.i = n;
2324 if (ch)
2325 tcc_error("invalid number\n");
2329 #define PARSE2(c1, tok1, c2, tok2) \
2330 case c1: \
2331 PEEKC(c, p); \
2332 if (c == c2) { \
2333 p++; \
2334 tok = tok2; \
2335 } else { \
2336 tok = tok1; \
2338 break;
2340 /* return next token without macro substitution */
2341 static inline void next_nomacro1(void)
2343 int t, c, is_long;
2344 TokenSym *ts;
2345 uint8_t *p, *p1;
2346 unsigned int h;
2348 p = file->buf_ptr;
2349 redo_no_start:
2350 c = *p;
2351 #if (__TINYC__ || __GNUC__)
2352 #else
2353 if (c & 0x80)
2354 goto parse_ident_fast;
2355 #endif
2356 switch(c) {
2357 case ' ':
2358 case '\t':
2359 tok = c;
2360 p++;
2361 if (parse_flags & PARSE_FLAG_SPACES)
2362 goto keep_tok_flags;
2363 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2364 ++p;
2365 goto redo_no_start;
2366 case '\f':
2367 case '\v':
2368 case '\r':
2369 p++;
2370 goto redo_no_start;
2371 case '\\':
2372 /* first look if it is in fact an end of buffer */
2373 c = handle_stray1(p);
2374 p = file->buf_ptr;
2375 if (c == '\\')
2376 goto parse_simple;
2377 if (c != CH_EOF)
2378 goto redo_no_start;
2380 TCCState *s1 = tcc_state;
2381 if ((parse_flags & PARSE_FLAG_LINEFEED)
2382 && !(tok_flags & TOK_FLAG_EOF)) {
2383 tok_flags |= TOK_FLAG_EOF;
2384 tok = TOK_LINEFEED;
2385 goto keep_tok_flags;
2386 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2387 tok = TOK_EOF;
2388 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2389 tcc_error("missing #endif");
2390 } else if (s1->include_stack_ptr == s1->include_stack) {
2391 /* no include left : end of file. */
2392 tok = TOK_EOF;
2393 } else {
2394 tok_flags &= ~TOK_FLAG_EOF;
2395 /* pop include file */
2397 /* test if previous '#endif' was after a #ifdef at
2398 start of file */
2399 if (tok_flags & TOK_FLAG_ENDIF) {
2400 #ifdef INC_DEBUG
2401 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2402 #endif
2403 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2404 tok_flags &= ~TOK_FLAG_ENDIF;
2407 /* add end of include file debug info */
2408 if (tcc_state->do_debug) {
2409 put_stabd(N_EINCL, 0, 0);
2411 /* pop include stack */
2412 tcc_close();
2413 s1->include_stack_ptr--;
2414 p = file->buf_ptr;
2415 goto redo_no_start;
2418 break;
2420 case '\n':
2421 file->line_num++;
2422 tok_flags |= TOK_FLAG_BOL;
2423 p++;
2424 maybe_newline:
2425 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2426 goto redo_no_start;
2427 tok = TOK_LINEFEED;
2428 goto keep_tok_flags;
2430 case '#':
2431 /* XXX: simplify */
2432 PEEKC(c, p);
2433 if ((tok_flags & TOK_FLAG_BOL) &&
2434 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2435 file->buf_ptr = p;
2436 preprocess(tok_flags & TOK_FLAG_BOF);
2437 p = file->buf_ptr;
2438 goto maybe_newline;
2439 } else {
2440 if (c == '#') {
2441 p++;
2442 tok = TOK_TWOSHARPS;
2443 } else {
2444 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2445 p = parse_line_comment(p - 1);
2446 goto redo_no_start;
2447 } else {
2448 tok = '#';
2452 break;
2454 /* dollar is allowed to start identifiers when not parsing asm */
2455 case '$':
2456 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2457 || (parse_flags & PARSE_FLAG_ASM_FILE))
2458 goto parse_simple;
2460 #if (__TINYC__ || __GNUC__)
2461 case 'a' ... 'z':
2462 case 'A' ... 'K':
2463 case 'M' ... 'Z':
2464 case '_':
2465 case 0x80 ... 0xFF:
2466 #else
2467 case 'a': case 'b': case 'c': case 'd':
2468 case 'e': case 'f': case 'g': case 'h':
2469 case 'i': case 'j': case 'k': case 'l':
2470 case 'm': case 'n': case 'o': case 'p':
2471 case 'q': case 'r': case 's': case 't':
2472 case 'u': case 'v': case 'w': case 'x':
2473 case 'y': case 'z':
2474 case 'A': case 'B': case 'C': case 'D':
2475 case 'E': case 'F': case 'G': case 'H':
2476 case 'I': case 'J': case 'K':
2477 case 'M': case 'N': case 'O': case 'P':
2478 case 'Q': case 'R': case 'S': case 'T':
2479 case 'U': case 'V': case 'W': case 'X':
2480 case 'Y': case 'Z':
2481 case '_':
2482 #endif
2483 parse_ident_fast:
2484 p1 = p;
2485 h = TOK_HASH_INIT;
2486 h = TOK_HASH_FUNC(h, c);
2487 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2488 h = TOK_HASH_FUNC(h, c);
2489 if (c != '\\') {
2490 TokenSym **pts;
2491 int len;
2493 /* fast case : no stray found, so we have the full token
2494 and we have already hashed it */
2495 len = p - p1;
2496 h &= (TOK_HASH_SIZE - 1);
2497 pts = &hash_ident[h];
2498 for(;;) {
2499 ts = *pts;
2500 if (!ts)
2501 break;
2502 if (ts->len == len && !memcmp(ts->str, p1, len))
2503 goto token_found;
2504 pts = &(ts->hash_next);
2506 ts = tok_alloc_new(pts, (char *) p1, len);
2507 token_found: ;
2508 } else {
2509 /* slower case */
2510 cstr_reset(&tokcstr);
2512 while (p1 < p) {
2513 cstr_ccat(&tokcstr, *p1);
2514 p1++;
2516 p--;
2517 PEEKC(c, p);
2518 parse_ident_slow:
2519 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2521 cstr_ccat(&tokcstr, c);
2522 PEEKC(c, p);
2524 ts = tok_alloc(tokcstr.data, tokcstr.size);
2526 tok = ts->tok;
2527 break;
2528 case 'L':
2529 t = p[1];
2530 if (t != '\\' && t != '\'' && t != '\"') {
2531 /* fast case */
2532 goto parse_ident_fast;
2533 } else {
2534 PEEKC(c, p);
2535 if (c == '\'' || c == '\"') {
2536 is_long = 1;
2537 goto str_const;
2538 } else {
2539 cstr_reset(&tokcstr);
2540 cstr_ccat(&tokcstr, 'L');
2541 goto parse_ident_slow;
2544 break;
2546 case '0': case '1': case '2': case '3':
2547 case '4': case '5': case '6': case '7':
2548 case '8': case '9':
2549 cstr_reset(&tokcstr);
2550 /* after the first digit, accept digits, alpha, '.' or sign if
2551 prefixed by 'eEpP' */
2552 parse_num:
2553 for(;;) {
2554 t = c;
2555 cstr_ccat(&tokcstr, c);
2556 PEEKC(c, p);
2557 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2558 || c == '.'
2559 || ((c == '+' || c == '-')
2560 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2562 break;
2564 /* We add a trailing '\0' to ease parsing */
2565 cstr_ccat(&tokcstr, '\0');
2566 tokc.str.size = tokcstr.size;
2567 tokc.str.data = tokcstr.data;
2568 tokc.str.data_allocated = tokcstr.data_allocated;
2569 tok = TOK_PPNUM;
2570 break;
2572 case '.':
2573 /* special dot handling because it can also start a number */
2574 PEEKC(c, p);
2575 if (isnum(c)) {
2576 cstr_reset(&tokcstr);
2577 cstr_ccat(&tokcstr, '.');
2578 goto parse_num;
2579 } else if (parse_flags & PARSE_FLAG_ASM_FILE) {
2580 *--p = c = '.';
2581 goto parse_ident_fast;
2582 } else if (c == '.') {
2583 PEEKC(c, p);
2584 if (c == '.') {
2585 p++;
2586 tok = TOK_DOTS;
2587 } else {
2588 *--p = '.'; /* may underflow into file->unget[] */
2589 tok = '.';
2591 } else {
2592 tok = '.';
2594 break;
2595 case '\'':
2596 case '\"':
2597 is_long = 0;
2598 str_const:
2599 cstr_reset(&tokcstr);
2600 if (is_long)
2601 cstr_ccat(&tokcstr, 'L');
2602 cstr_ccat(&tokcstr, c);
2603 p = parse_pp_string(p, c, &tokcstr);
2604 cstr_ccat(&tokcstr, c);
2605 cstr_ccat(&tokcstr, '\0');
2606 tokc.str.size = tokcstr.size;
2607 tokc.str.data = tokcstr.data;
2608 tokc.str.data_allocated = tokcstr.data_allocated;
2609 tok = TOK_PPSTR;
2610 break;
2612 case '<':
2613 PEEKC(c, p);
2614 if (c == '=') {
2615 p++;
2616 tok = TOK_LE;
2617 } else if (c == '<') {
2618 PEEKC(c, p);
2619 if (c == '=') {
2620 p++;
2621 tok = TOK_A_SHL;
2622 } else {
2623 tok = TOK_SHL;
2625 } else {
2626 tok = TOK_LT;
2628 break;
2629 case '>':
2630 PEEKC(c, p);
2631 if (c == '=') {
2632 p++;
2633 tok = TOK_GE;
2634 } else if (c == '>') {
2635 PEEKC(c, p);
2636 if (c == '=') {
2637 p++;
2638 tok = TOK_A_SAR;
2639 } else {
2640 tok = TOK_SAR;
2642 } else {
2643 tok = TOK_GT;
2645 break;
2647 case '&':
2648 PEEKC(c, p);
2649 if (c == '&') {
2650 p++;
2651 tok = TOK_LAND;
2652 } else if (c == '=') {
2653 p++;
2654 tok = TOK_A_AND;
2655 } else {
2656 tok = '&';
2658 break;
2660 case '|':
2661 PEEKC(c, p);
2662 if (c == '|') {
2663 p++;
2664 tok = TOK_LOR;
2665 } else if (c == '=') {
2666 p++;
2667 tok = TOK_A_OR;
2668 } else {
2669 tok = '|';
2671 break;
2673 case '+':
2674 PEEKC(c, p);
2675 if (c == '+') {
2676 p++;
2677 tok = TOK_INC;
2678 } else if (c == '=') {
2679 p++;
2680 tok = TOK_A_ADD;
2681 } else {
2682 tok = '+';
2684 break;
2686 case '-':
2687 PEEKC(c, p);
2688 if (c == '-') {
2689 p++;
2690 tok = TOK_DEC;
2691 } else if (c == '=') {
2692 p++;
2693 tok = TOK_A_SUB;
2694 } else if (c == '>') {
2695 p++;
2696 tok = TOK_ARROW;
2697 } else {
2698 tok = '-';
2700 break;
2702 PARSE2('!', '!', '=', TOK_NE)
2703 PARSE2('=', '=', '=', TOK_EQ)
2704 PARSE2('*', '*', '=', TOK_A_MUL)
2705 PARSE2('%', '%', '=', TOK_A_MOD)
2706 PARSE2('^', '^', '=', TOK_A_XOR)
2708 /* comments or operator */
2709 case '/':
2710 PEEKC(c, p);
2711 if (c == '*') {
2712 p = parse_comment(p);
2713 /* comments replaced by a blank */
2714 tok = ' ';
2715 goto keep_tok_flags;
2716 } else if (c == '/') {
2717 p = parse_line_comment(p);
2718 tok = ' ';
2719 goto keep_tok_flags;
2720 } else if (c == '=') {
2721 p++;
2722 tok = TOK_A_DIV;
2723 } else {
2724 tok = '/';
2726 break;
2728 /* simple tokens */
2729 case '(':
2730 case ')':
2731 case '[':
2732 case ']':
2733 case '{':
2734 case '}':
2735 case ',':
2736 case ';':
2737 case ':':
2738 case '?':
2739 case '~':
2740 case '@': /* only used in assembler */
2741 parse_simple:
2742 tok = c;
2743 p++;
2744 break;
2745 default:
2746 tcc_error("unrecognized character \\x%02x", c);
2747 break;
2749 tok_flags = 0;
2750 keep_tok_flags:
2751 file->buf_ptr = p;
2752 #if defined(PARSE_DEBUG)
2753 printf("token = %s\n", get_tok_str(tok, &tokc));
2754 #endif
2757 /* return next token without macro substitution. Can read input from
2758 macro_ptr buffer */
2759 static void next_nomacro_spc(void)
2761 if (macro_ptr) {
2762 redo:
2763 tok = *macro_ptr;
2764 if (tok) {
2765 TOK_GET(&tok, &macro_ptr, &tokc);
2766 if (tok == TOK_LINENUM) {
2767 file->line_num = tokc.i;
2768 goto redo;
2771 } else {
2772 next_nomacro1();
2776 ST_FUNC void next_nomacro(void)
2778 do {
2779 next_nomacro_spc();
2780 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2784 static void macro_subst(
2785 TokenString *tok_str,
2786 Sym **nested_list,
2787 const int *macro_str,
2788 int can_read_stream
2791 /* substitute arguments in replacement lists in macro_str by the values in
2792 args (field d) and return allocated string */
2793 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2795 int t, t0, t1, spc;
2796 const int *st;
2797 Sym *s;
2798 CValue cval;
2799 TokenString str;
2800 CString cstr;
2802 tok_str_new(&str);
2803 t0 = t1 = 0;
2804 while(1) {
2805 TOK_GET(&t, &macro_str, &cval);
2806 if (!t)
2807 break;
2808 if (t == '#') {
2809 /* stringize */
2810 TOK_GET(&t, &macro_str, &cval);
2811 if (!t)
2812 goto bad_stringy;
2813 s = sym_find2(args, t);
2814 if (s) {
2815 cstr_new(&cstr);
2816 cstr_ccat(&cstr, '\"');
2817 st = s->d;
2818 spc = 0;
2819 while (*st) {
2820 TOK_GET(&t, &st, &cval);
2821 if (t != TOK_PLCHLDR
2822 && t != TOK_NOSUBST
2823 && 0 == check_space(t, &spc)) {
2824 const char *s = get_tok_str(t, &cval);
2825 while (*s) {
2826 if (t == TOK_PPSTR && *s != '\'')
2827 add_char(&cstr, *s);
2828 else
2829 cstr_ccat(&cstr, *s);
2830 ++s;
2834 cstr.size -= spc;
2835 cstr_ccat(&cstr, '\"');
2836 cstr_ccat(&cstr, '\0');
2837 #ifdef PP_DEBUG
2838 printf("\nstringize: <%s>\n", (char *)cstr.data);
2839 #endif
2840 /* add string */
2841 cval.str.size = cstr.size;
2842 cval.str.data = cstr.data;
2843 cval.str.data_allocated = cstr.data_allocated;
2844 tok_str_add2(&str, TOK_PPSTR, &cval);
2845 tcc_free(cval.str.data_allocated);
2846 } else {
2847 bad_stringy:
2848 expect("macro parameter after '#'");
2850 } else if (t >= TOK_IDENT) {
2851 s = sym_find2(args, t);
2852 if (s) {
2853 int l0 = str.len;
2854 st = s->d;
2855 /* if '##' is present before or after, no arg substitution */
2856 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2857 /* special case for var arg macros : ## eats the ','
2858 if empty VA_ARGS variable. */
2859 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2860 if (*st == 0) {
2861 /* suppress ',' '##' */
2862 str.len -= 2;
2863 } else {
2864 /* suppress '##' and add variable */
2865 str.len--;
2866 goto add_var;
2868 } else {
2869 for(;;) {
2870 int t1;
2871 TOK_GET(&t1, &st, &cval);
2872 if (!t1)
2873 break;
2874 tok_str_add2(&str, t1, &cval);
2878 } else {
2879 add_var:
2880 /* NOTE: the stream cannot be read when macro
2881 substituing an argument */
2882 macro_subst(&str, nested_list, st, 0);
2884 if (str.len == l0) /* exanded to empty string */
2885 tok_str_add(&str, TOK_PLCHLDR);
2886 } else {
2887 tok_str_add(&str, t);
2889 } else {
2890 tok_str_add2(&str, t, &cval);
2892 t0 = t1, t1 = t;
2894 tok_str_add(&str, 0);
2895 return str.str;
2898 static char const ab_month_name[12][4] =
2900 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2901 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2904 /* peek or read [ws_str == NULL] next token from function macro call,
2905 walking up macro levels up to the file if necessary */
2906 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2908 int t;
2909 const int *p;
2910 Sym *sa;
2912 for (;;) {
2913 if (macro_ptr) {
2914 p = macro_ptr, t = *p;
2915 if (ws_str) {
2916 while (is_space(t) || TOK_LINEFEED == t)
2917 tok_str_add(ws_str, t), t = *++p;
2919 if (t == 0 && can_read_stream) {
2920 end_macro();
2921 /* also, end of scope for nested defined symbol */
2922 sa = *nested_list;
2923 while (sa && sa->v == -1)
2924 sa = sa->prev;
2925 if (sa)
2926 sa->v = -1;
2927 continue;
2929 } else {
2930 ch = handle_eob();
2931 if (ws_str) {
2932 while (is_space(ch) || ch == '\n' || ch == '/') {
2933 if (ch == '/') {
2934 int c;
2935 uint8_t *p = file->buf_ptr;
2936 PEEKC(c, p);
2937 if (c == '*') {
2938 p = parse_comment(p);
2939 file->buf_ptr = p - 1;
2940 } else if (c == '/') {
2941 p = parse_line_comment(p);
2942 file->buf_ptr = p - 1;
2943 } else
2944 break;
2945 ch = ' ';
2947 tok_str_add(ws_str, ch);
2948 cinp();
2951 t = ch;
2954 if (ws_str)
2955 return t;
2956 next_nomacro_spc();
2957 return tok;
2961 /* do macro substitution of current token with macro 's' and add
2962 result to (tok_str,tok_len). 'nested_list' is the list of all
2963 macros we got inside to avoid recursing. Return non zero if no
2964 substitution needs to be done */
2965 static int macro_subst_tok(
2966 TokenString *tok_str,
2967 Sym **nested_list,
2968 Sym *s,
2969 int can_read_stream)
2971 Sym *args, *sa, *sa1;
2972 int parlevel, *mstr, t, t1, spc;
2973 TokenString str;
2974 char *cstrval;
2975 CValue cval;
2976 CString cstr;
2977 char buf[32];
2979 /* if symbol is a macro, prepare substitution */
2980 /* special macros */
2981 if (tok == TOK___LINE__) {
2982 snprintf(buf, sizeof(buf), "%d", file->line_num);
2983 cstrval = buf;
2984 t1 = TOK_PPNUM;
2985 goto add_cstr1;
2986 } else if (tok == TOK___FILE__) {
2987 cstrval = file->filename;
2988 goto add_cstr;
2989 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2990 time_t ti;
2991 struct tm *tm;
2993 time(&ti);
2994 tm = localtime(&ti);
2995 if (tok == TOK___DATE__) {
2996 snprintf(buf, sizeof(buf), "%s %2d %d",
2997 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2998 } else {
2999 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3000 tm->tm_hour, tm->tm_min, tm->tm_sec);
3002 cstrval = buf;
3003 add_cstr:
3004 t1 = TOK_STR;
3005 add_cstr1:
3006 cstr_new(&cstr);
3007 cstr_cat(&cstr, cstrval);
3008 cstr_ccat(&cstr, '\0');
3009 cval.str.size = cstr.size;
3010 cval.str.data = cstr.data;
3011 cval.str.data_allocated = cstr.data_allocated;
3012 tok_str_add2(tok_str, t1, &cval);
3013 cstr_free(&cstr);
3014 } else {
3015 int saved_parse_flags = parse_flags;
3017 mstr = s->d;
3018 if (s->type.t == MACRO_FUNC) {
3019 /* whitespace between macro name and argument list */
3020 TokenString ws_str;
3021 tok_str_new(&ws_str);
3023 spc = 0;
3024 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3025 | PARSE_FLAG_ACCEPT_STRAYS;
3027 /* get next token from argument stream */
3028 t = next_argstream(nested_list, can_read_stream, &ws_str);
3029 if (t != '(') {
3030 /* not a macro substitution after all, restore the
3031 * macro token plus all whitespace we've read.
3032 * whitespace is intentionally not merged to preserve
3033 * newlines. */
3034 parse_flags = saved_parse_flags;
3035 tok_str_add(tok_str, tok);
3036 if (parse_flags & PARSE_FLAG_SPACES) {
3037 int i;
3038 for (i = 0; i < ws_str.len; i++)
3039 tok_str_add(tok_str, ws_str.str[i]);
3041 tok_str_free(ws_str.str);
3042 return 0;
3043 } else {
3044 tok_str_free(ws_str.str);
3046 next_nomacro(); /* eat '(' */
3048 /* argument macro */
3049 args = NULL;
3050 sa = s->next;
3051 /* NOTE: empty args are allowed, except if no args */
3052 for(;;) {
3053 do {
3054 next_argstream(nested_list, can_read_stream, NULL);
3055 } while (is_space(tok) || TOK_LINEFEED == tok);
3056 empty_arg:
3057 /* handle '()' case */
3058 if (!args && !sa && tok == ')')
3059 break;
3060 if (!sa)
3061 tcc_error("macro '%s' used with too many args",
3062 get_tok_str(s->v, 0));
3063 tok_str_new(&str);
3064 parlevel = spc = 0;
3065 /* NOTE: non zero sa->t indicates VA_ARGS */
3066 while ((parlevel > 0 ||
3067 (tok != ')' &&
3068 (tok != ',' || sa->type.t)))) {
3069 if (tok == TOK_EOF || tok == 0)
3070 break;
3071 if (tok == '(')
3072 parlevel++;
3073 else if (tok == ')')
3074 parlevel--;
3075 if (tok == TOK_LINEFEED)
3076 tok = ' ';
3077 if (!check_space(tok, &spc))
3078 tok_str_add2(&str, tok, &tokc);
3079 next_argstream(nested_list, can_read_stream, NULL);
3081 if (parlevel)
3082 expect(")");
3083 str.len -= spc;
3084 tok_str_add(&str, 0);
3085 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3086 sa1->d = str.str;
3087 sa = sa->next;
3088 if (tok == ')') {
3089 /* special case for gcc var args: add an empty
3090 var arg argument if it is omitted */
3091 if (sa && sa->type.t && gnu_ext)
3092 goto empty_arg;
3093 break;
3095 if (tok != ',')
3096 expect(",");
3098 if (sa) {
3099 tcc_error("macro '%s' used with too few args",
3100 get_tok_str(s->v, 0));
3103 parse_flags = saved_parse_flags;
3105 /* now subst each arg */
3106 mstr = macro_arg_subst(nested_list, mstr, args);
3107 /* free memory */
3108 sa = args;
3109 while (sa) {
3110 sa1 = sa->prev;
3111 tok_str_free(sa->d);
3112 sym_free(sa);
3113 sa = sa1;
3117 sym_push2(nested_list, s->v, 0, 0);
3118 parse_flags = saved_parse_flags;
3119 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3121 /* pop nested defined symbol */
3122 sa1 = *nested_list;
3123 *nested_list = sa1->prev;
3124 sym_free(sa1);
3125 if (mstr != s->d)
3126 tok_str_free(mstr);
3128 return 0;
3131 int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3133 CString cstr;
3134 int n;
3136 cstr_new(&cstr);
3137 if (t1 != TOK_PLCHLDR)
3138 cstr_cat(&cstr, get_tok_str(t1, v1));
3139 n = cstr.size;
3140 if (t2 != TOK_PLCHLDR)
3141 cstr_cat(&cstr, get_tok_str(t2, v2));
3142 cstr_ccat(&cstr, '\0');
3144 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3145 memcpy(file->buffer, cstr.data, cstr.size);
3146 for (;;) {
3147 next_nomacro1();
3148 if (0 == *file->buf_ptr)
3149 break;
3150 if (is_space(tok))
3151 continue;
3152 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3153 n, cstr.data, (char*)cstr.data + n);
3154 break;
3156 tcc_close();
3158 //printf("paste <%s>\n", (char*)cstr.data);
3159 cstr_free(&cstr);
3160 return 0;
3163 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3164 return the resulting string (which must be freed). */
3165 static inline int *macro_twosharps(const int *ptr0)
3167 int t;
3168 CValue cval;
3169 TokenString macro_str1;
3170 int start_of_nosubsts = -1;
3171 const int *ptr;
3173 /* we search the first '##' */
3174 for (ptr = ptr0;;) {
3175 TOK_GET(&t, &ptr, &cval);
3176 if (t == TOK_TWOSHARPS)
3177 break;
3178 if (t == 0)
3179 return NULL;
3182 tok_str_new(&macro_str1);
3184 //tok_print(" $$$", ptr0);
3185 for (ptr = ptr0;;) {
3186 TOK_GET(&t, &ptr, &cval);
3187 if (t == 0)
3188 break;
3189 if (t == TOK_TWOSHARPS)
3190 continue;
3191 while (*ptr == TOK_TWOSHARPS) {
3192 int t1; CValue cv1;
3193 /* given 'a##b', remove nosubsts preceding 'a' */
3194 if (start_of_nosubsts >= 0)
3195 macro_str1.len = start_of_nosubsts;
3196 /* given 'a##b', remove nosubsts preceding 'b' */
3197 while ((t1 = *++ptr) == TOK_NOSUBST)
3199 if (t1 && t1 != TOK_TWOSHARPS
3200 && t1 != ':') /* 'a##:' don't build a new token */
3202 TOK_GET(&t1, &ptr, &cv1);
3203 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3204 paste_tokens(t, &cval, t1, &cv1);
3205 t = tok, cval = tokc;
3209 if (t == TOK_NOSUBST) {
3210 if (start_of_nosubsts < 0)
3211 start_of_nosubsts = macro_str1.len;
3212 } else {
3213 start_of_nosubsts = -1;
3215 tok_str_add2(&macro_str1, t, &cval);
3217 tok_str_add(&macro_str1, 0);
3218 //tok_print(" ###", macro_str1.str);
3219 return macro_str1.str;
3222 /* do macro substitution of macro_str and add result to
3223 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3224 inside to avoid recursing. */
3225 static void macro_subst(
3226 TokenString *tok_str,
3227 Sym **nested_list,
3228 const int *macro_str,
3229 int can_read_stream
3232 Sym *s;
3233 const int *ptr;
3234 int t, spc, nosubst;
3235 CValue cval;
3236 int *macro_str1 = NULL;
3238 /* first scan for '##' operator handling */
3239 ptr = macro_str;
3240 spc = nosubst = 0;
3242 /* first scan for '##' operator handling */
3243 if (can_read_stream) {
3244 macro_str1 = macro_twosharps(ptr);
3245 if (macro_str1)
3246 ptr = macro_str1;
3249 while (1) {
3250 TOK_GET(&t, &ptr, &cval);
3251 if (t == 0)
3252 break;
3254 if (t >= TOK_IDENT && 0 == nosubst) {
3255 s = define_find(t);
3256 if (s == NULL)
3257 goto no_subst;
3259 /* if nested substitution, do nothing */
3260 if (sym_find2(*nested_list, t)) {
3261 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3262 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3263 goto no_subst;
3267 TokenString str;
3268 str.str = (int*)ptr;
3269 begin_macro(&str, 2);
3271 tok = t;
3272 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3274 if (str.alloc == 3) {
3275 /* already finished by reading function macro arguments */
3276 break;
3279 ptr = macro_ptr;
3280 end_macro ();
3283 spc = (tok_str->len &&
3284 is_space(tok_last(tok_str->str,
3285 tok_str->str + tok_str->len)));
3287 } else {
3289 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3290 tcc_error("stray '\\' in program");
3292 no_subst:
3293 if (!check_space(t, &spc))
3294 tok_str_add2(tok_str, t, &cval);
3295 nosubst = 0;
3296 if (t == TOK_NOSUBST)
3297 nosubst = 1;
3300 if (macro_str1)
3301 tok_str_free(macro_str1);
3305 /* return next token with macro substitution */
3306 ST_FUNC void next(void)
3308 redo:
3309 if (parse_flags & PARSE_FLAG_SPACES)
3310 next_nomacro_spc();
3311 else
3312 next_nomacro();
3314 if (macro_ptr) {
3315 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3316 /* discard preprocessor markers */
3317 goto redo;
3318 } else if (tok == 0) {
3319 /* end of macro or unget token string */
3320 end_macro();
3321 goto redo;
3323 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3324 Sym *s;
3325 /* if reading from file, try to substitute macros */
3326 s = define_find(tok);
3327 if (s) {
3328 static TokenString str; /* using static string for speed */
3329 Sym *nested_list = NULL;
3330 tok_str_new(&str);
3331 nested_list = NULL;
3332 macro_subst_tok(&str, &nested_list, s, 1);
3333 tok_str_add(&str, 0);
3334 begin_macro(&str, 0);
3335 goto redo;
3338 /* convert preprocessor tokens into C tokens */
3339 if (tok == TOK_PPNUM) {
3340 if (parse_flags & PARSE_FLAG_TOK_NUM)
3341 parse_number((char *)tokc.str.data);
3342 } else if (tok == TOK_PPSTR) {
3343 if (parse_flags & PARSE_FLAG_TOK_STR)
3344 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3348 /* push back current token and set current token to 'last_tok'. Only
3349 identifier case handled for labels. */
3350 ST_INLN void unget_tok(int last_tok)
3352 TokenString *str = tcc_malloc(sizeof *str);
3353 tok_str_new(str);
3354 tok_str_add2(str, tok, &tokc);
3355 tok_str_add(str, 0);
3356 begin_macro(str, 1);
3357 tok = last_tok;
3360 /* better than nothing, but needs extension to handle '-E' option
3361 correctly too */
3362 ST_FUNC void preprocess_init(TCCState *s1)
3364 s1->include_stack_ptr = s1->include_stack;
3365 /* XXX: move that before to avoid having to initialize
3366 file->ifdef_stack_ptr ? */
3367 s1->ifdef_stack_ptr = s1->ifdef_stack;
3368 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3370 pvtop = vtop = vstack - 1;
3371 s1->pack_stack[0] = 0;
3372 s1->pack_stack_ptr = s1->pack_stack;
3374 isidnum_table['$' - CH_EOF] =
3375 tcc_state->dollars_in_identifiers ? IS_ID : 0;
3377 isidnum_table['.' - CH_EOF] =
3378 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
3381 ST_FUNC void preprocess_new(void)
3383 int i, c;
3384 const char *p, *r;
3386 /* init isid table */
3387 for(i = CH_EOF; i<128; i++)
3388 isidnum_table[i - CH_EOF]
3389 = is_space(i) ? IS_SPC
3390 : isid(i) ? IS_ID
3391 : isnum(i) ? IS_NUM
3392 : 0;
3394 for(i = 128; i<256; i++)
3395 isidnum_table[i - CH_EOF] = IS_ID;
3397 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3399 tok_ident = TOK_IDENT;
3400 p = tcc_keywords;
3401 while (*p) {
3402 r = p;
3403 for(;;) {
3404 c = *r++;
3405 if (c == '\0')
3406 break;
3408 tok_alloc(p, r - p - 1);
3409 p = r;
3413 ST_FUNC void preprocess_delete(void)
3415 int i, n;
3417 /* free -D and compiler defines */
3418 free_defines(NULL);
3420 /* cleanup from error/setjmp */
3421 while (macro_stack)
3422 end_macro();
3423 macro_ptr = NULL;
3425 /* free tokens */
3426 n = tok_ident - TOK_IDENT;
3427 for(i = 0; i < n; i++)
3428 tcc_free(table_ident[i]);
3429 tcc_free(table_ident);
3430 table_ident = NULL;
3433 /* Preprocess the current file */
3434 ST_FUNC int tcc_preprocess(TCCState *s1)
3436 BufferedFile **iptr;
3437 int token_seen, spcs, level;
3439 preprocess_init(s1);
3440 ch = file->buf_ptr[0];
3441 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3442 parse_flags = PARSE_FLAG_PREPROCESS
3443 | (parse_flags & PARSE_FLAG_ASM_FILE)
3444 | PARSE_FLAG_LINEFEED
3445 | PARSE_FLAG_SPACES
3446 | PARSE_FLAG_ACCEPT_STRAYS
3449 #ifdef PP_BENCH
3450 do next(); while (tok != TOK_EOF); return 0;
3451 #endif
3453 token_seen = spcs = 0;
3454 pp_line(s1, file, 0);
3456 for (;;) {
3457 iptr = s1->include_stack_ptr;
3458 next();
3459 if (tok == TOK_EOF)
3460 break;
3461 level = s1->include_stack_ptr - iptr;
3462 if (level) {
3463 if (level > 0)
3464 pp_line(s1, *iptr, 0);
3465 pp_line(s1, file, level);
3468 if (0 == token_seen) {
3469 if (tok == ' ') {
3470 ++spcs;
3471 continue;
3473 if (tok == TOK_LINEFEED) {
3474 spcs = 0;
3475 continue;
3477 pp_line(s1, file, 0);
3478 while (s1->ppfp && spcs > 0)
3479 fputs(" ", s1->ppfp), --spcs;
3480 spcs = 0;
3481 token_seen = 1;
3483 } else if (tok == TOK_LINEFEED) {
3484 ++file->line_ref;
3485 token_seen = 0;
3487 if (s1->ppfp)
3488 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3491 return 0;
3494 ST_FUNC char *trimfront(char *p)
3496 while (*p && (unsigned char)*p <= ' ')
3497 ++p;
3498 return p;
3501 ST_FUNC char *trimback(char *a, char *e)
3503 while (e > a && (unsigned char)e[-1] <= ' ')
3504 --e;
3505 *e = 0;;
3506 return a;