tccpp: fix issues, add tests
[tinycc.git] / tccpp.c
blob1c03febf2421405008e979aaed6d87554508b7c3
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 /* defines handling */
1049 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1051 Sym *s;
1053 s = define_find(v);
1054 if (s && !macro_is_equal(s->d, str))
1055 tcc_warning("%s redefined", get_tok_str(v, NULL));
1057 s = sym_push2(&define_stack, v, macro_type, 0);
1058 s->d = str;
1059 s->next = first_arg;
1060 table_ident[v - TOK_IDENT]->sym_define = s;
1063 /* undefined a define symbol. Its name is just set to zero */
1064 ST_FUNC void define_undef(Sym *s)
1066 int v;
1067 v = s->v;
1068 if (v >= TOK_IDENT && v < tok_ident)
1069 table_ident[v - TOK_IDENT]->sym_define = NULL;
1072 ST_INLN Sym *define_find(int v)
1074 v -= TOK_IDENT;
1075 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1076 return NULL;
1077 return table_ident[v]->sym_define;
1080 /* free define stack until top reaches 'b' */
1081 ST_FUNC void free_defines(Sym *b)
1083 Sym *top, *top1;
1084 int v;
1086 top = define_stack;
1087 while (top != b) {
1088 top1 = top->prev;
1089 /* do not free args or predefined defines */
1090 if (top->d)
1091 tok_str_free(top->d);
1092 v = top->v;
1093 if (v >= TOK_IDENT && v < tok_ident)
1094 table_ident[v - TOK_IDENT]->sym_define = NULL;
1095 sym_free(top);
1096 top = top1;
1098 define_stack = b;
1101 /* label lookup */
1102 ST_FUNC Sym *label_find(int v)
1104 v -= TOK_IDENT;
1105 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1106 return NULL;
1107 return table_ident[v]->sym_label;
1110 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1112 Sym *s, **ps;
1113 s = sym_push2(ptop, v, 0, 0);
1114 s->r = flags;
1115 ps = &table_ident[v - TOK_IDENT]->sym_label;
1116 if (ptop == &global_label_stack) {
1117 /* modify the top most local identifier, so that
1118 sym_identifier will point to 's' when popped */
1119 while (*ps != NULL)
1120 ps = &(*ps)->prev_tok;
1122 s->prev_tok = *ps;
1123 *ps = s;
1124 return s;
1127 /* pop labels until element last is reached. Look if any labels are
1128 undefined. Define symbols if '&&label' was used. */
1129 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1131 Sym *s, *s1;
1132 for(s = *ptop; s != slast; s = s1) {
1133 s1 = s->prev;
1134 if (s->r == LABEL_DECLARED) {
1135 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1136 } else if (s->r == LABEL_FORWARD) {
1137 tcc_error("label '%s' used but not defined",
1138 get_tok_str(s->v, NULL));
1139 } else {
1140 if (s->c) {
1141 /* define corresponding symbol. A size of
1142 1 is put. */
1143 put_extern_sym(s, cur_text_section, s->jnext, 1);
1146 /* remove label */
1147 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1148 sym_free(s);
1150 *ptop = slast;
1153 /* eval an expression for #if/#elif */
1154 static int expr_preprocess(void)
1156 int c, t;
1157 TokenString str;
1159 tok_str_new(&str);
1160 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1161 next(); /* do macro subst */
1162 if (tok == TOK_DEFINED) {
1163 next_nomacro();
1164 t = tok;
1165 if (t == '(')
1166 next_nomacro();
1167 c = define_find(tok) != 0;
1168 if (t == '(')
1169 next_nomacro();
1170 tok = TOK_CINT;
1171 tokc.i = c;
1172 } else if (tok >= TOK_IDENT) {
1173 /* if undefined macro */
1174 tok = TOK_CINT;
1175 tokc.i = 0;
1177 tok_str_add_tok(&str);
1179 tok_str_add(&str, -1); /* simulate end of file */
1180 tok_str_add(&str, 0);
1181 /* now evaluate C constant expression */
1182 begin_macro(&str, 0);
1183 next();
1184 c = expr_const();
1185 end_macro();
1186 return c != 0;
1189 //#define PP_DEBUG
1191 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1192 static void tok_print(const char *msg, const int *str)
1194 int t;
1195 CValue cval;
1197 printf("%s ", msg);
1198 while (1) {
1199 TOK_GET(&t, &str, &cval);
1200 if (!t)
1201 break;
1202 printf("%s", get_tok_str(t, &cval));
1204 printf("\n");
1207 static void define_print(int v)
1209 Sym *s, *a;
1211 s = define_find(v);
1212 printf("#define %s", get_tok_str(v, NULL));
1213 if (s->type.t == MACRO_FUNC) {
1214 a = s->next;
1215 printf("(");
1216 if (a)
1217 for (;;) {
1218 printf("%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
1219 if (!(a = a->next))
1220 break;
1221 printf(",");
1223 printf(")");
1225 tok_print("", s->d);
1227 #endif
1229 /* parse after #define */
1230 ST_FUNC void parse_define(void)
1232 Sym *s, *first, **ps;
1233 int v, t, varg, is_vaargs, spc;
1234 int saved_parse_flags = parse_flags;
1235 TokenString str;
1237 v = tok;
1238 if (v < TOK_IDENT)
1239 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1240 /* XXX: should check if same macro (ANSI) */
1241 first = NULL;
1242 t = MACRO_OBJ;
1243 /* '(' must be just after macro definition for MACRO_FUNC */
1244 parse_flags |= PARSE_FLAG_SPACES;
1245 next_nomacro_spc();
1246 if (tok == '(') {
1247 next_nomacro();
1248 ps = &first;
1249 if (tok != ')') for (;;) {
1250 varg = tok;
1251 next_nomacro();
1252 is_vaargs = 0;
1253 if (varg == TOK_DOTS) {
1254 varg = TOK___VA_ARGS__;
1255 is_vaargs = 1;
1256 } else if (tok == TOK_DOTS && gnu_ext) {
1257 is_vaargs = 1;
1258 next_nomacro();
1260 if (varg < TOK_IDENT)
1261 bad_list:
1262 tcc_error("bad macro parameter list");
1263 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1264 *ps = s;
1265 ps = &s->next;
1266 if (tok == ')')
1267 break;
1268 if (tok != ',' || is_vaargs)
1269 goto bad_list;
1270 next_nomacro();
1272 next_nomacro_spc();
1273 t = MACRO_FUNC;
1275 tok_str_new(&str);
1276 spc = 2;
1277 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1278 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1279 /* remove spaces around ## and after '#' */
1280 if (TOK_TWOSHARPS == tok) {
1281 if (2 == spc)
1282 goto bad_twosharp;
1283 if (1 == spc)
1284 --str.len;
1285 spc = 3;
1286 } else if ('#' == tok) {
1287 spc = 4;
1288 } else if (check_space(tok, &spc)) {
1289 goto skip;
1291 tok_str_add2(&str, tok, &tokc);
1292 skip:
1293 next_nomacro_spc();
1296 parse_flags = saved_parse_flags;
1297 if (spc == 1)
1298 --str.len; /* remove trailing space */
1299 tok_str_add(&str, 0);
1300 if (3 == spc)
1301 bad_twosharp:
1302 tcc_error("'##' cannot appear at either end of macro");
1303 define_push(v, t, str.str, first);
1304 #ifdef PP_DEBUG
1305 define_print(v);
1306 #endif
1309 static inline int hash_cached_include(const char *filename)
1311 const unsigned char *s;
1312 unsigned int h;
1314 h = TOK_HASH_INIT;
1315 s = (unsigned char *) filename;
1316 while (*s) {
1317 h = TOK_HASH_FUNC(h, *s);
1318 s++;
1320 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1321 return h;
1324 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1326 CachedInclude *e;
1327 int i, h;
1328 h = hash_cached_include(filename);
1329 i = s1->cached_includes_hash[h];
1330 for(;;) {
1331 if (i == 0)
1332 break;
1333 e = s1->cached_includes[i - 1];
1334 if (0 == PATHCMP(e->filename, filename))
1335 return e;
1336 i = e->hash_next;
1338 return NULL;
1341 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1343 CachedInclude *e;
1344 int h;
1346 if (search_cached_include(s1, filename))
1347 return;
1348 #ifdef INC_DEBUG
1349 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1350 #endif
1351 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1352 strcpy(e->filename, filename);
1353 e->ifndef_macro = ifndef_macro;
1354 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1355 /* add in hash table */
1356 h = hash_cached_include(filename);
1357 e->hash_next = s1->cached_includes_hash[h];
1358 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1361 static void pragma_parse(TCCState *s1)
1363 next_nomacro();
1364 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1365 int t = tok, v;
1366 Sym *s;
1368 if (next(), tok != '(')
1369 goto pragma_err;
1370 if (next(), tok != TOK_STR)
1371 goto pragma_err;
1372 v = tok_alloc(tokc.cstr->data, tokc.cstr->size - 1)->tok;
1373 if (next(), tok != ')')
1374 goto pragma_err;
1375 if (t == TOK_push_macro) {
1376 while (NULL == (s = define_find(v)))
1377 define_push(v, 0, NULL, NULL);
1378 s->type.ref = s; /* set push boundary */
1379 } else {
1380 for (s = define_stack; s; s = s->prev)
1381 if (s->v == v && s->type.ref == s) {
1382 s->type.ref = NULL;
1383 break;
1386 if (s)
1387 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1388 else
1389 tcc_warning("unbalanced #pragma pop_macro");
1391 } else if (tok == TOK_once) {
1392 add_cached_include(s1, file->filename, TOK_once);
1394 } else if (s1->ppfp) {
1395 /* tcc -E: keep pragmas below unchanged */
1396 unget_tok(' ');
1397 unget_tok(TOK_PRAGMA);
1398 unget_tok('#');
1399 unget_tok(TOK_LINEFEED);
1401 } else if (tok == TOK_pack) {
1402 /* This may be:
1403 #pragma pack(1) // set
1404 #pragma pack() // reset to default
1405 #pragma pack(push,1) // push & set
1406 #pragma pack(pop) // restore previous */
1407 next();
1408 skip('(');
1409 if (tok == TOK_ASM_pop) {
1410 next();
1411 if (s1->pack_stack_ptr <= s1->pack_stack) {
1412 stk_error:
1413 tcc_error("out of pack stack");
1415 s1->pack_stack_ptr--;
1416 } else {
1417 int val = 0;
1418 if (tok != ')') {
1419 if (tok == TOK_ASM_push) {
1420 next();
1421 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1422 goto stk_error;
1423 s1->pack_stack_ptr++;
1424 skip(',');
1426 if (tok != TOK_CINT)
1427 goto pragma_err;
1428 val = tokc.i;
1429 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1430 goto pragma_err;
1431 next();
1433 *s1->pack_stack_ptr = val;
1435 if (tok != ')')
1436 goto pragma_err;
1438 } else if (tok == TOK_comment) {
1439 char *file;
1440 next();
1441 skip('(');
1442 if (tok != TOK_lib)
1443 goto pragma_warn;
1444 next();
1445 skip(',');
1446 if (tok != TOK_STR)
1447 goto pragma_err;
1448 file = tcc_strdup((char *)tokc.cstr->data);
1449 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1450 next();
1451 if (tok != ')')
1452 goto pragma_err;
1453 } else {
1454 pragma_warn:
1455 if (s1->warn_unsupported)
1456 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1458 return;
1460 pragma_err:
1461 tcc_error("malformed #pragma directive");
1462 return;
1465 /* is_bof is true if first non space token at beginning of file */
1466 ST_FUNC void preprocess(int is_bof)
1468 TCCState *s1 = tcc_state;
1469 int i, c, n, saved_parse_flags;
1470 char buf[1024], *q;
1471 Sym *s;
1473 saved_parse_flags = parse_flags;
1474 parse_flags = PARSE_FLAG_PREPROCESS
1475 | PARSE_FLAG_TOK_NUM
1476 | PARSE_FLAG_TOK_STR
1477 | PARSE_FLAG_LINEFEED
1478 | (parse_flags & PARSE_FLAG_ASM_FILE)
1481 next_nomacro();
1482 redo:
1483 switch(tok) {
1484 case TOK_DEFINE:
1485 next_nomacro();
1486 parse_define();
1487 break;
1488 case TOK_UNDEF:
1489 next_nomacro();
1490 s = define_find(tok);
1491 /* undefine symbol by putting an invalid name */
1492 if (s)
1493 define_undef(s);
1494 break;
1495 case TOK_INCLUDE:
1496 case TOK_INCLUDE_NEXT:
1497 ch = file->buf_ptr[0];
1498 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1499 skip_spaces();
1500 if (ch == '<') {
1501 c = '>';
1502 goto read_name;
1503 } else if (ch == '\"') {
1504 c = ch;
1505 read_name:
1506 inp();
1507 q = buf;
1508 while (ch != c && ch != '\n' && ch != CH_EOF) {
1509 if ((q - buf) < sizeof(buf) - 1)
1510 *q++ = ch;
1511 if (ch == '\\') {
1512 if (handle_stray_noerror() == 0)
1513 --q;
1514 } else
1515 inp();
1517 *q = '\0';
1518 minp();
1519 #if 0
1520 /* eat all spaces and comments after include */
1521 /* XXX: slightly incorrect */
1522 while (ch1 != '\n' && ch1 != CH_EOF)
1523 inp();
1524 #endif
1525 } else {
1526 /* computed #include : either we have only strings or
1527 we have anything enclosed in '<>' */
1528 next();
1529 buf[0] = '\0';
1530 if (tok == TOK_STR) {
1531 while (tok != TOK_LINEFEED) {
1532 if (tok != TOK_STR) {
1533 include_syntax:
1534 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1536 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1537 next();
1539 c = '\"';
1540 } else {
1541 int len;
1542 while (tok != TOK_LINEFEED) {
1543 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1544 next();
1546 len = strlen(buf);
1547 /* check syntax and remove '<>' */
1548 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1549 goto include_syntax;
1550 memmove(buf, buf + 1, len - 2);
1551 buf[len - 2] = '\0';
1552 c = '>';
1556 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1557 tcc_error("#include recursion too deep");
1558 /* store current file in stack, but increment stack later below */
1559 *s1->include_stack_ptr = file;
1561 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1562 for (i = -2; i < n; ++i) {
1563 char buf1[sizeof file->filename];
1564 CachedInclude *e;
1565 BufferedFile **f;
1566 const char *path;
1568 if (i == -2) {
1569 /* check absolute include path */
1570 if (!IS_ABSPATH(buf))
1571 continue;
1572 buf1[0] = 0;
1573 i = n; /* force end loop */
1575 } else if (i == -1) {
1576 /* search in current dir if "header.h" */
1577 if (c != '\"')
1578 continue;
1579 path = file->filename;
1580 pstrncpy(buf1, path, tcc_basename(path) - path);
1582 } else {
1583 /* search in all the include paths */
1584 if (i < s1->nb_include_paths)
1585 path = s1->include_paths[i];
1586 else
1587 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1588 pstrcpy(buf1, sizeof(buf1), path);
1589 pstrcat(buf1, sizeof(buf1), "/");
1592 pstrcat(buf1, sizeof(buf1), buf);
1594 if (tok == TOK_INCLUDE_NEXT)
1595 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1596 if (0 == PATHCMP((*f)->filename, buf1)) {
1597 #ifdef INC_DEBUG
1598 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1599 #endif
1600 goto include_trynext;
1603 e = search_cached_include(s1, buf1);
1604 if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) {
1605 /* no need to parse the include because the 'ifndef macro'
1606 is defined */
1607 #ifdef INC_DEBUG
1608 printf("%s: skipping cached %s\n", file->filename, buf1);
1609 #endif
1610 goto include_done;
1613 if (tcc_open(s1, buf1) < 0)
1614 include_trynext:
1615 continue;
1617 #ifdef INC_DEBUG
1618 printf("%s: including %s\n", file->prev->filename, file->filename);
1619 #endif
1620 /* update target deps */
1621 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1622 tcc_strdup(buf1));
1623 /* push current file in stack */
1624 ++s1->include_stack_ptr;
1625 /* add include file debug info */
1626 if (s1->do_debug)
1627 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1628 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1629 ch = file->buf_ptr[0];
1630 goto the_end;
1632 tcc_error("include file '%s' not found", buf);
1633 include_done:
1634 break;
1635 case TOK_IFNDEF:
1636 c = 1;
1637 goto do_ifdef;
1638 case TOK_IF:
1639 c = expr_preprocess();
1640 goto do_if;
1641 case TOK_IFDEF:
1642 c = 0;
1643 do_ifdef:
1644 next_nomacro();
1645 if (tok < TOK_IDENT)
1646 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1647 if (is_bof) {
1648 if (c) {
1649 #ifdef INC_DEBUG
1650 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1651 #endif
1652 file->ifndef_macro = tok;
1655 c = (define_find(tok) != 0) ^ c;
1656 do_if:
1657 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1658 tcc_error("memory full (ifdef)");
1659 *s1->ifdef_stack_ptr++ = c;
1660 goto test_skip;
1661 case TOK_ELSE:
1662 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1663 tcc_error("#else without matching #if");
1664 if (s1->ifdef_stack_ptr[-1] & 2)
1665 tcc_error("#else after #else");
1666 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1667 goto test_else;
1668 case TOK_ELIF:
1669 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1670 tcc_error("#elif without matching #if");
1671 c = s1->ifdef_stack_ptr[-1];
1672 if (c > 1)
1673 tcc_error("#elif after #else");
1674 /* last #if/#elif expression was true: we skip */
1675 if (c == 1)
1676 goto skip;
1677 c = expr_preprocess();
1678 s1->ifdef_stack_ptr[-1] = c;
1679 test_else:
1680 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1681 file->ifndef_macro = 0;
1682 test_skip:
1683 if (!(c & 1)) {
1684 skip:
1685 preprocess_skip();
1686 is_bof = 0;
1687 goto redo;
1689 break;
1690 case TOK_ENDIF:
1691 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1692 tcc_error("#endif without matching #if");
1693 s1->ifdef_stack_ptr--;
1694 /* '#ifndef macro' was at the start of file. Now we check if
1695 an '#endif' is exactly at the end of file */
1696 if (file->ifndef_macro &&
1697 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1698 file->ifndef_macro_saved = file->ifndef_macro;
1699 /* need to set to zero to avoid false matches if another
1700 #ifndef at middle of file */
1701 file->ifndef_macro = 0;
1702 while (tok != TOK_LINEFEED)
1703 next_nomacro();
1704 tok_flags |= TOK_FLAG_ENDIF;
1705 goto the_end;
1707 break;
1708 case TOK_PPNUM:
1709 n = strtoul((char*)tokc.cstr->data, &q, 10);
1710 goto _line_num;
1711 case TOK_LINE:
1712 next();
1713 if (tok != TOK_CINT)
1714 _line_err:
1715 tcc_error("wrong #line format");
1716 n = tokc.i;
1717 _line_num:
1718 next();
1719 if (tok != TOK_LINEFEED) {
1720 if (tok == TOK_STR)
1721 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.cstr->data);
1722 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1723 break;
1724 else
1725 goto _line_err;
1726 --n;
1728 if (file->fd > 0)
1729 total_lines += file->line_num - n;
1730 file->line_num = n;
1731 if (s1->do_debug)
1732 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1733 break;
1734 case TOK_ERROR:
1735 case TOK_WARNING:
1736 c = tok;
1737 ch = file->buf_ptr[0];
1738 skip_spaces();
1739 q = buf;
1740 while (ch != '\n' && ch != CH_EOF) {
1741 if ((q - buf) < sizeof(buf) - 1)
1742 *q++ = ch;
1743 if (ch == '\\') {
1744 if (handle_stray_noerror() == 0)
1745 --q;
1746 } else
1747 inp();
1749 *q = '\0';
1750 if (c == TOK_ERROR)
1751 tcc_error("#error %s", buf);
1752 else
1753 tcc_warning("#warning %s", buf);
1754 break;
1755 case TOK_PRAGMA:
1756 pragma_parse(s1);
1757 break;
1758 case TOK_LINEFEED:
1759 goto the_end;
1760 default:
1761 /* ignore gas line comment in an 'S' file. */
1762 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1763 goto ignore;
1764 if (tok == '!' && is_bof)
1765 /* '!' is ignored at beginning to allow C scripts. */
1766 goto ignore;
1767 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1768 ignore:
1769 file->buf_ptr = parse_line_comment(file->buf_ptr);
1770 goto the_end;
1772 /* ignore other preprocess commands or #! for C scripts */
1773 while (tok != TOK_LINEFEED)
1774 next_nomacro();
1775 the_end:
1776 parse_flags = saved_parse_flags;
1779 /* evaluate escape codes in a string. */
1780 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1782 int c, n;
1783 const uint8_t *p;
1785 p = buf;
1786 for(;;) {
1787 c = *p;
1788 if (c == '\0')
1789 break;
1790 if (c == '\\') {
1791 p++;
1792 /* escape */
1793 c = *p;
1794 switch(c) {
1795 case '0': case '1': case '2': case '3':
1796 case '4': case '5': case '6': case '7':
1797 /* at most three octal digits */
1798 n = c - '0';
1799 p++;
1800 c = *p;
1801 if (isoct(c)) {
1802 n = n * 8 + c - '0';
1803 p++;
1804 c = *p;
1805 if (isoct(c)) {
1806 n = n * 8 + c - '0';
1807 p++;
1810 c = n;
1811 goto add_char_nonext;
1812 case 'x':
1813 case 'u':
1814 case 'U':
1815 p++;
1816 n = 0;
1817 for(;;) {
1818 c = *p;
1819 if (c >= 'a' && c <= 'f')
1820 c = c - 'a' + 10;
1821 else if (c >= 'A' && c <= 'F')
1822 c = c - 'A' + 10;
1823 else if (isnum(c))
1824 c = c - '0';
1825 else
1826 break;
1827 n = n * 16 + c;
1828 p++;
1830 c = n;
1831 goto add_char_nonext;
1832 case 'a':
1833 c = '\a';
1834 break;
1835 case 'b':
1836 c = '\b';
1837 break;
1838 case 'f':
1839 c = '\f';
1840 break;
1841 case 'n':
1842 c = '\n';
1843 break;
1844 case 'r':
1845 c = '\r';
1846 break;
1847 case 't':
1848 c = '\t';
1849 break;
1850 case 'v':
1851 c = '\v';
1852 break;
1853 case 'e':
1854 if (!gnu_ext)
1855 goto invalid_escape;
1856 c = 27;
1857 break;
1858 case '\'':
1859 case '\"':
1860 case '\\':
1861 case '?':
1862 break;
1863 default:
1864 invalid_escape:
1865 if (c >= '!' && c <= '~')
1866 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1867 else
1868 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1869 break;
1872 p++;
1873 add_char_nonext:
1874 if (!is_long)
1875 cstr_ccat(outstr, c);
1876 else
1877 cstr_wccat(outstr, c);
1879 /* add a trailing '\0' */
1880 if (!is_long)
1881 cstr_ccat(outstr, '\0');
1882 else
1883 cstr_wccat(outstr, '\0');
1886 void parse_string(const char *s, int len)
1888 uint8_t buf[1000], *p = buf;
1889 int is_long, sep;
1891 if ((is_long = *s == 'L'))
1892 ++s, --len;
1893 sep = *s++;
1894 len -= 2;
1895 if (len >= sizeof buf)
1896 p = tcc_malloc(len + 1);
1897 memcpy(p, s, len);
1898 p[len] = 0;
1900 cstr_reset(&tokcstr);
1901 parse_escape_string(&tokcstr, p, is_long);
1902 if (p != buf)
1903 tcc_free(p);
1905 if (sep == '\'') {
1906 int char_size;
1907 /* XXX: make it portable */
1908 if (!is_long)
1909 char_size = 1;
1910 else
1911 char_size = sizeof(nwchar_t);
1912 if (tokcstr.size <= char_size)
1913 tcc_error("empty character constant");
1914 if (tokcstr.size > 2 * char_size)
1915 tcc_warning("multi-character character constant");
1916 if (!is_long) {
1917 tokc.i = *(int8_t *)tokcstr.data;
1918 tok = TOK_CCHAR;
1919 } else {
1920 tokc.i = *(nwchar_t *)tokcstr.data;
1921 tok = TOK_LCHAR;
1923 } else {
1924 tokc.cstr = &tokcstr;
1925 if (!is_long)
1926 tok = TOK_STR;
1927 else
1928 tok = TOK_LSTR;
1932 /* we use 64 bit numbers */
1933 #define BN_SIZE 2
1935 /* bn = (bn << shift) | or_val */
1936 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1938 int i;
1939 unsigned int v;
1940 for(i=0;i<BN_SIZE;i++) {
1941 v = bn[i];
1942 bn[i] = (v << shift) | or_val;
1943 or_val = v >> (32 - shift);
1947 static void bn_zero(unsigned int *bn)
1949 int i;
1950 for(i=0;i<BN_SIZE;i++) {
1951 bn[i] = 0;
1955 /* parse number in null terminated string 'p' and return it in the
1956 current token */
1957 static void parse_number(const char *p)
1959 int b, t, shift, frac_bits, s, exp_val, ch;
1960 char *q;
1961 unsigned int bn[BN_SIZE];
1962 double d;
1964 /* number */
1965 q = token_buf;
1966 ch = *p++;
1967 t = ch;
1968 ch = *p++;
1969 *q++ = t;
1970 b = 10;
1971 if (t == '.') {
1972 goto float_frac_parse;
1973 } else if (t == '0') {
1974 if (ch == 'x' || ch == 'X') {
1975 q--;
1976 ch = *p++;
1977 b = 16;
1978 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1979 q--;
1980 ch = *p++;
1981 b = 2;
1984 /* parse all digits. cannot check octal numbers at this stage
1985 because of floating point constants */
1986 while (1) {
1987 if (ch >= 'a' && ch <= 'f')
1988 t = ch - 'a' + 10;
1989 else if (ch >= 'A' && ch <= 'F')
1990 t = ch - 'A' + 10;
1991 else if (isnum(ch))
1992 t = ch - '0';
1993 else
1994 break;
1995 if (t >= b)
1996 break;
1997 if (q >= token_buf + STRING_MAX_SIZE) {
1998 num_too_long:
1999 tcc_error("number too long");
2001 *q++ = ch;
2002 ch = *p++;
2004 if (ch == '.' ||
2005 ((ch == 'e' || ch == 'E') && b == 10) ||
2006 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2007 if (b != 10) {
2008 /* NOTE: strtox should support that for hexa numbers, but
2009 non ISOC99 libcs do not support it, so we prefer to do
2010 it by hand */
2011 /* hexadecimal or binary floats */
2012 /* XXX: handle overflows */
2013 *q = '\0';
2014 if (b == 16)
2015 shift = 4;
2016 else
2017 shift = 1;
2018 bn_zero(bn);
2019 q = token_buf;
2020 while (1) {
2021 t = *q++;
2022 if (t == '\0') {
2023 break;
2024 } else if (t >= 'a') {
2025 t = t - 'a' + 10;
2026 } else if (t >= 'A') {
2027 t = t - 'A' + 10;
2028 } else {
2029 t = t - '0';
2031 bn_lshift(bn, shift, t);
2033 frac_bits = 0;
2034 if (ch == '.') {
2035 ch = *p++;
2036 while (1) {
2037 t = ch;
2038 if (t >= 'a' && t <= 'f') {
2039 t = t - 'a' + 10;
2040 } else if (t >= 'A' && t <= 'F') {
2041 t = t - 'A' + 10;
2042 } else if (t >= '0' && t <= '9') {
2043 t = t - '0';
2044 } else {
2045 break;
2047 if (t >= b)
2048 tcc_error("invalid digit");
2049 bn_lshift(bn, shift, t);
2050 frac_bits += shift;
2051 ch = *p++;
2054 if (ch != 'p' && ch != 'P')
2055 expect("exponent");
2056 ch = *p++;
2057 s = 1;
2058 exp_val = 0;
2059 if (ch == '+') {
2060 ch = *p++;
2061 } else if (ch == '-') {
2062 s = -1;
2063 ch = *p++;
2065 if (ch < '0' || ch > '9')
2066 expect("exponent digits");
2067 while (ch >= '0' && ch <= '9') {
2068 exp_val = exp_val * 10 + ch - '0';
2069 ch = *p++;
2071 exp_val = exp_val * s;
2073 /* now we can generate the number */
2074 /* XXX: should patch directly float number */
2075 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2076 d = ldexp(d, exp_val - frac_bits);
2077 t = toup(ch);
2078 if (t == 'F') {
2079 ch = *p++;
2080 tok = TOK_CFLOAT;
2081 /* float : should handle overflow */
2082 tokc.f = (float)d;
2083 } else if (t == 'L') {
2084 ch = *p++;
2085 #ifdef TCC_TARGET_PE
2086 tok = TOK_CDOUBLE;
2087 tokc.d = d;
2088 #else
2089 tok = TOK_CLDOUBLE;
2090 /* XXX: not large enough */
2091 tokc.ld = (long double)d;
2092 #endif
2093 } else {
2094 tok = TOK_CDOUBLE;
2095 tokc.d = d;
2097 } else {
2098 /* decimal floats */
2099 if (ch == '.') {
2100 if (q >= token_buf + STRING_MAX_SIZE)
2101 goto num_too_long;
2102 *q++ = ch;
2103 ch = *p++;
2104 float_frac_parse:
2105 while (ch >= '0' && ch <= '9') {
2106 if (q >= token_buf + STRING_MAX_SIZE)
2107 goto num_too_long;
2108 *q++ = ch;
2109 ch = *p++;
2112 if (ch == 'e' || ch == 'E') {
2113 if (q >= token_buf + STRING_MAX_SIZE)
2114 goto num_too_long;
2115 *q++ = ch;
2116 ch = *p++;
2117 if (ch == '-' || ch == '+') {
2118 if (q >= token_buf + STRING_MAX_SIZE)
2119 goto num_too_long;
2120 *q++ = ch;
2121 ch = *p++;
2123 if (ch < '0' || ch > '9')
2124 expect("exponent digits");
2125 while (ch >= '0' && ch <= '9') {
2126 if (q >= token_buf + STRING_MAX_SIZE)
2127 goto num_too_long;
2128 *q++ = ch;
2129 ch = *p++;
2132 *q = '\0';
2133 t = toup(ch);
2134 errno = 0;
2135 if (t == 'F') {
2136 ch = *p++;
2137 tok = TOK_CFLOAT;
2138 tokc.f = strtof(token_buf, NULL);
2139 } else if (t == 'L') {
2140 ch = *p++;
2141 #ifdef TCC_TARGET_PE
2142 tok = TOK_CDOUBLE;
2143 tokc.d = strtod(token_buf, NULL);
2144 #else
2145 tok = TOK_CLDOUBLE;
2146 tokc.ld = strtold(token_buf, NULL);
2147 #endif
2148 } else {
2149 tok = TOK_CDOUBLE;
2150 tokc.d = strtod(token_buf, NULL);
2153 } else {
2154 unsigned long long n, n1;
2155 int lcount, ucount, must_64bit;
2156 const char *p1;
2158 /* integer number */
2159 *q = '\0';
2160 q = token_buf;
2161 if (b == 10 && *q == '0') {
2162 b = 8;
2163 q++;
2165 n = 0;
2166 while(1) {
2167 t = *q++;
2168 /* no need for checks except for base 10 / 8 errors */
2169 if (t == '\0')
2170 break;
2171 else if (t >= 'a')
2172 t = t - 'a' + 10;
2173 else if (t >= 'A')
2174 t = t - 'A' + 10;
2175 else
2176 t = t - '0';
2177 if (t >= b)
2178 tcc_error("invalid digit");
2179 n1 = n;
2180 n = n * b + t;
2181 /* detect overflow */
2182 /* XXX: this test is not reliable */
2183 if (n < n1)
2184 tcc_error("integer constant overflow");
2187 /* Determine the characteristics (unsigned and/or 64bit) the type of
2188 the constant must have according to the constant suffix(es) */
2189 lcount = ucount = must_64bit = 0;
2190 p1 = p;
2191 for(;;) {
2192 t = toup(ch);
2193 if (t == 'L') {
2194 if (lcount >= 2)
2195 tcc_error("three 'l's in integer constant");
2196 if (lcount && *(p - 1) != ch)
2197 tcc_error("incorrect integer suffix: %s", p1);
2198 lcount++;
2199 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2200 if (lcount == 2)
2201 #endif
2202 must_64bit = 1;
2203 ch = *p++;
2204 } else if (t == 'U') {
2205 if (ucount >= 1)
2206 tcc_error("two 'u's in integer constant");
2207 ucount++;
2208 ch = *p++;
2209 } else {
2210 break;
2214 /* Whether 64 bits are needed to hold the constant's value */
2215 if (n & 0xffffffff00000000LL || must_64bit) {
2216 tok = TOK_CLLONG;
2217 n1 = n >> 32;
2218 } else {
2219 tok = TOK_CINT;
2220 n1 = n;
2223 /* Whether type must be unsigned to hold the constant's value */
2224 if (ucount || ((n1 >> 31) && (b != 10))) {
2225 if (tok == TOK_CLLONG)
2226 tok = TOK_CULLONG;
2227 else
2228 tok = TOK_CUINT;
2229 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2230 } else if (n1 >> 31) {
2231 if (tok == TOK_CINT)
2232 tok = TOK_CLLONG;
2233 else
2234 tcc_error("integer constant overflow");
2237 if (tok == TOK_CINT || tok == TOK_CUINT)
2238 tokc.ui = n;
2239 else
2240 tokc.ull = n;
2242 if (ch)
2243 tcc_error("invalid number\n");
2247 #define PARSE2(c1, tok1, c2, tok2) \
2248 case c1: \
2249 PEEKC(c, p); \
2250 if (c == c2) { \
2251 p++; \
2252 tok = tok2; \
2253 } else { \
2254 tok = tok1; \
2256 break;
2258 /* return next token without macro substitution */
2259 static inline void next_nomacro1(void)
2261 int t, c, is_long;
2262 TokenSym *ts;
2263 uint8_t *p, *p1;
2264 unsigned int h;
2266 p = file->buf_ptr;
2267 redo_no_start:
2268 c = *p;
2269 switch(c) {
2270 case ' ':
2271 case '\t':
2272 tok = c;
2273 p++;
2274 if (parse_flags & PARSE_FLAG_SPACES)
2275 goto keep_tok_flags;
2276 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2277 ++p;
2278 goto redo_no_start;
2279 case '\f':
2280 case '\v':
2281 case '\r':
2282 p++;
2283 goto redo_no_start;
2284 case '\\':
2285 /* first look if it is in fact an end of buffer */
2286 c = handle_stray1(p);
2287 p = file->buf_ptr;
2288 if (c == '\\')
2289 goto parse_simple;
2290 if (c != CH_EOF)
2291 goto redo_no_start;
2293 TCCState *s1 = tcc_state;
2294 if ((parse_flags & PARSE_FLAG_LINEFEED)
2295 && !(tok_flags & TOK_FLAG_EOF)) {
2296 tok_flags |= TOK_FLAG_EOF;
2297 tok = TOK_LINEFEED;
2298 goto keep_tok_flags;
2299 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2300 tok = TOK_EOF;
2301 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2302 tcc_error("missing #endif");
2303 } else if (s1->include_stack_ptr == s1->include_stack) {
2304 /* no include left : end of file. */
2305 tok = TOK_EOF;
2306 } else {
2307 tok_flags &= ~TOK_FLAG_EOF;
2308 /* pop include file */
2310 /* test if previous '#endif' was after a #ifdef at
2311 start of file */
2312 if (tok_flags & TOK_FLAG_ENDIF) {
2313 #ifdef INC_DEBUG
2314 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2315 #endif
2316 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2317 tok_flags &= ~TOK_FLAG_ENDIF;
2320 /* add end of include file debug info */
2321 if (tcc_state->do_debug) {
2322 put_stabd(N_EINCL, 0, 0);
2324 /* pop include stack */
2325 tcc_close();
2326 s1->include_stack_ptr--;
2327 p = file->buf_ptr;
2328 goto redo_no_start;
2331 break;
2333 case '\n':
2334 file->line_num++;
2335 tok_flags |= TOK_FLAG_BOL;
2336 p++;
2337 maybe_newline:
2338 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2339 goto redo_no_start;
2340 tok = TOK_LINEFEED;
2341 goto keep_tok_flags;
2343 case '#':
2344 /* XXX: simplify */
2345 PEEKC(c, p);
2346 if ((tok_flags & TOK_FLAG_BOL) &&
2347 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2348 file->buf_ptr = p;
2349 preprocess(tok_flags & TOK_FLAG_BOF);
2350 p = file->buf_ptr;
2351 goto maybe_newline;
2352 } else {
2353 if (c == '#') {
2354 p++;
2355 tok = TOK_TWOSHARPS;
2356 } else {
2357 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2358 p = parse_line_comment(p - 1);
2359 goto redo_no_start;
2360 } else {
2361 tok = '#';
2365 break;
2367 /* dollar is allowed to start identifiers when not parsing asm */
2368 case '$':
2369 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2370 || (parse_flags & PARSE_FLAG_ASM_FILE))
2371 goto parse_simple;
2373 case 'a': case 'b': case 'c': case 'd':
2374 case 'e': case 'f': case 'g': case 'h':
2375 case 'i': case 'j': case 'k': case 'l':
2376 case 'm': case 'n': case 'o': case 'p':
2377 case 'q': case 'r': case 's': case 't':
2378 case 'u': case 'v': case 'w': case 'x':
2379 case 'y': case 'z':
2380 case 'A': case 'B': case 'C': case 'D':
2381 case 'E': case 'F': case 'G': case 'H':
2382 case 'I': case 'J': case 'K':
2383 case 'M': case 'N': case 'O': case 'P':
2384 case 'Q': case 'R': case 'S': case 'T':
2385 case 'U': case 'V': case 'W': case 'X':
2386 case 'Y': case 'Z':
2387 case '_':
2388 parse_ident_fast:
2389 p1 = p;
2390 h = TOK_HASH_INIT;
2391 h = TOK_HASH_FUNC(h, c);
2392 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2393 h = TOK_HASH_FUNC(h, c);
2394 if (c != '\\') {
2395 TokenSym **pts;
2396 int len;
2398 /* fast case : no stray found, so we have the full token
2399 and we have already hashed it */
2400 len = p - p1;
2401 h &= (TOK_HASH_SIZE - 1);
2402 pts = &hash_ident[h];
2403 for(;;) {
2404 ts = *pts;
2405 if (!ts)
2406 break;
2407 if (ts->len == len && !memcmp(ts->str, p1, len))
2408 goto token_found;
2409 pts = &(ts->hash_next);
2411 ts = tok_alloc_new(pts, (char *) p1, len);
2412 token_found: ;
2413 } else {
2414 /* slower case */
2415 cstr_reset(&tokcstr);
2417 while (p1 < p) {
2418 cstr_ccat(&tokcstr, *p1);
2419 p1++;
2421 p--;
2422 PEEKC(c, p);
2423 parse_ident_slow:
2424 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM)) {
2425 cstr_ccat(&tokcstr, c);
2426 PEEKC(c, p);
2428 ts = tok_alloc(tokcstr.data, tokcstr.size);
2430 tok = ts->tok;
2431 break;
2432 case 'L':
2433 t = p[1];
2434 if (t != '\\' && t != '\'' && t != '\"') {
2435 /* fast case */
2436 goto parse_ident_fast;
2437 } else {
2438 PEEKC(c, p);
2439 if (c == '\'' || c == '\"') {
2440 is_long = 1;
2441 goto str_const;
2442 } else {
2443 cstr_reset(&tokcstr);
2444 cstr_ccat(&tokcstr, 'L');
2445 goto parse_ident_slow;
2448 break;
2450 case '0': case '1': case '2': case '3':
2451 case '4': case '5': case '6': case '7':
2452 case '8': case '9':
2453 cstr_reset(&tokcstr);
2454 /* after the first digit, accept digits, alpha, '.' or sign if
2455 prefixed by 'eEpP' */
2456 parse_num:
2457 for(;;) {
2458 t = c;
2459 cstr_ccat(&tokcstr, c);
2460 PEEKC(c, p);
2461 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2462 || c == '.'
2463 || ((c == '+' || c == '-')
2464 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2466 break;
2468 /* We add a trailing '\0' to ease parsing */
2469 cstr_ccat(&tokcstr, '\0');
2470 tokc.cstr = &tokcstr;
2471 tok = TOK_PPNUM;
2472 break;
2474 case '.':
2475 /* special dot handling because it can also start a number */
2476 PEEKC(c, p);
2477 if (isnum(c)) {
2478 cstr_reset(&tokcstr);
2479 cstr_ccat(&tokcstr, '.');
2480 goto parse_num;
2481 } else if (c == '.') {
2482 PEEKC(c, p);
2483 if (c != '.')
2484 expect("'.'");
2485 PEEKC(c, p);
2486 tok = TOK_DOTS;
2487 } else {
2488 tok = '.';
2490 break;
2491 case '\'':
2492 case '\"':
2493 is_long = 0;
2494 str_const:
2495 cstr_reset(&tokcstr);
2496 if (is_long)
2497 cstr_ccat(&tokcstr, 'L');
2498 cstr_ccat(&tokcstr, c);
2499 p = parse_pp_string(p, c, &tokcstr);
2500 cstr_ccat(&tokcstr, c);
2501 cstr_ccat(&tokcstr, '\0');
2502 tokc.cstr = &tokcstr;
2503 tok = TOK_PPSTR;
2504 break;
2506 case '<':
2507 PEEKC(c, p);
2508 if (c == '=') {
2509 p++;
2510 tok = TOK_LE;
2511 } else if (c == '<') {
2512 PEEKC(c, p);
2513 if (c == '=') {
2514 p++;
2515 tok = TOK_A_SHL;
2516 } else {
2517 tok = TOK_SHL;
2519 } else {
2520 tok = TOK_LT;
2522 break;
2523 case '>':
2524 PEEKC(c, p);
2525 if (c == '=') {
2526 p++;
2527 tok = TOK_GE;
2528 } else if (c == '>') {
2529 PEEKC(c, p);
2530 if (c == '=') {
2531 p++;
2532 tok = TOK_A_SAR;
2533 } else {
2534 tok = TOK_SAR;
2536 } else {
2537 tok = TOK_GT;
2539 break;
2541 case '&':
2542 PEEKC(c, p);
2543 if (c == '&') {
2544 p++;
2545 tok = TOK_LAND;
2546 } else if (c == '=') {
2547 p++;
2548 tok = TOK_A_AND;
2549 } else {
2550 tok = '&';
2552 break;
2554 case '|':
2555 PEEKC(c, p);
2556 if (c == '|') {
2557 p++;
2558 tok = TOK_LOR;
2559 } else if (c == '=') {
2560 p++;
2561 tok = TOK_A_OR;
2562 } else {
2563 tok = '|';
2565 break;
2567 case '+':
2568 PEEKC(c, p);
2569 if (c == '+') {
2570 p++;
2571 tok = TOK_INC;
2572 } else if (c == '=') {
2573 p++;
2574 tok = TOK_A_ADD;
2575 } else {
2576 tok = '+';
2578 break;
2580 case '-':
2581 PEEKC(c, p);
2582 if (c == '-') {
2583 p++;
2584 tok = TOK_DEC;
2585 } else if (c == '=') {
2586 p++;
2587 tok = TOK_A_SUB;
2588 } else if (c == '>') {
2589 p++;
2590 tok = TOK_ARROW;
2591 } else {
2592 tok = '-';
2594 break;
2596 PARSE2('!', '!', '=', TOK_NE)
2597 PARSE2('=', '=', '=', TOK_EQ)
2598 PARSE2('*', '*', '=', TOK_A_MUL)
2599 PARSE2('%', '%', '=', TOK_A_MOD)
2600 PARSE2('^', '^', '=', TOK_A_XOR)
2602 /* comments or operator */
2603 case '/':
2604 PEEKC(c, p);
2605 if (c == '*') {
2606 p = parse_comment(p);
2607 /* comments replaced by a blank */
2608 tok = ' ';
2609 goto keep_tok_flags;
2610 } else if (c == '/') {
2611 p = parse_line_comment(p);
2612 tok = ' ';
2613 goto keep_tok_flags;
2614 } else if (c == '=') {
2615 p++;
2616 tok = TOK_A_DIV;
2617 } else {
2618 tok = '/';
2620 break;
2622 /* simple tokens */
2623 case '(':
2624 case ')':
2625 case '[':
2626 case ']':
2627 case '{':
2628 case '}':
2629 case ',':
2630 case ';':
2631 case ':':
2632 case '?':
2633 case '~':
2634 case '@': /* only used in assembler */
2635 parse_simple:
2636 tok = c;
2637 p++;
2638 break;
2639 default:
2640 tcc_error("unrecognized character \\x%02x", c);
2641 break;
2643 tok_flags = 0;
2644 keep_tok_flags:
2645 file->buf_ptr = p;
2646 #if defined(PARSE_DEBUG)
2647 printf("token = %s\n", get_tok_str(tok, &tokc));
2648 #endif
2651 /* return next token without macro substitution. Can read input from
2652 macro_ptr buffer */
2653 static void next_nomacro_spc(void)
2655 if (macro_ptr) {
2656 redo:
2657 tok = *macro_ptr;
2658 if (tok) {
2659 TOK_GET(&tok, &macro_ptr, &tokc);
2660 if (tok == TOK_LINENUM) {
2661 file->line_num = tokc.i;
2662 goto redo;
2665 } else {
2666 next_nomacro1();
2670 ST_FUNC void next_nomacro(void)
2672 do {
2673 next_nomacro_spc();
2674 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2678 static void macro_subst(
2679 TokenString *tok_str,
2680 Sym **nested_list,
2681 const int *macro_str,
2682 int can_read_stream
2685 /* substitute arguments in replacement lists in macro_str by the values in
2686 args (field d) and return allocated string */
2687 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2689 int t, t0, t1, spc;
2690 const int *st;
2691 Sym *s;
2692 CValue cval;
2693 TokenString str;
2694 CString cstr;
2696 tok_str_new(&str);
2697 t0 = t1 = 0;
2698 while(1) {
2699 TOK_GET(&t, &macro_str, &cval);
2700 if (!t)
2701 break;
2702 if (t == '#') {
2703 /* stringize */
2704 TOK_GET(&t, &macro_str, &cval);
2705 if (!t)
2706 goto bad_stringy;
2707 s = sym_find2(args, t);
2708 if (s) {
2709 cstr_new(&cstr);
2710 cstr_ccat(&cstr, '\"');
2711 st = s->d;
2712 spc = 0;
2713 while (*st) {
2714 TOK_GET(&t, &st, &cval);
2715 if (t != TOK_PLCHLDR
2716 && t != TOK_NOSUBST
2717 && 0 == check_space(t, &spc)) {
2718 const char *s = get_tok_str(t, &cval);
2719 while (*s) {
2720 if (t == TOK_PPSTR && *s != '\'')
2721 add_char(&cstr, *s);
2722 else
2723 cstr_ccat(&cstr, *s);
2724 ++s;
2728 cstr.size -= spc;
2729 cstr_ccat(&cstr, '\"');
2730 cstr_ccat(&cstr, '\0');
2731 #ifdef PP_DEBUG
2732 printf("\nstringize: <%s>\n", (char *)cstr.data);
2733 #endif
2734 /* add string */
2735 cval.cstr = &cstr;
2736 tok_str_add2(&str, TOK_PPSTR, &cval);
2737 cstr_free(cval.cstr);
2738 } else {
2739 bad_stringy:
2740 expect("macro parameter after '#'");
2742 } else if (t >= TOK_IDENT) {
2743 s = sym_find2(args, t);
2744 if (s) {
2745 int l0 = str.len;
2746 st = s->d;
2747 /* if '##' is present before or after, no arg substitution */
2748 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2749 /* special case for var arg macros : ## eats the ','
2750 if empty VA_ARGS variable. */
2751 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2752 if (*st == 0) {
2753 /* suppress ',' '##' */
2754 str.len -= 2;
2755 } else {
2756 /* suppress '##' and add variable */
2757 str.len--;
2758 goto add_var;
2760 } else {
2761 for(;;) {
2762 int t1;
2763 TOK_GET(&t1, &st, &cval);
2764 if (!t1)
2765 break;
2766 tok_str_add2(&str, t1, &cval);
2770 } else {
2771 add_var:
2772 /* NOTE: the stream cannot be read when macro
2773 substituing an argument */
2774 macro_subst(&str, nested_list, st, 0);
2776 if (str.len == l0) /* exanded to empty string */
2777 tok_str_add(&str, TOK_PLCHLDR);
2778 } else {
2779 tok_str_add(&str, t);
2781 } else {
2782 tok_str_add2(&str, t, &cval);
2784 t0 = t1, t1 = t;
2786 tok_str_add(&str, 0);
2787 return str.str;
2790 static char const ab_month_name[12][4] =
2792 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2793 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2796 /* peek or read [ws_str == NULL] next token from function macro call,
2797 walking up macro levels up to the file if necessary */
2798 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2800 int t;
2801 const int *p;
2802 Sym *sa;
2804 for (;;) {
2805 if (macro_ptr) {
2806 p = macro_ptr, t = *p;
2807 if (ws_str) {
2808 while (is_space(t) || TOK_LINEFEED == t)
2809 tok_str_add(ws_str, t), t = *++p;
2811 if (t == 0 && can_read_stream) {
2812 end_macro();
2813 /* also, end of scope for nested defined symbol */
2814 sa = *nested_list;
2815 while (sa && sa->v == -1)
2816 sa = sa->prev;
2817 if (sa)
2818 sa->v = -1;
2819 continue;
2821 } else {
2822 ch = handle_eob();
2823 if (ws_str) {
2824 while (is_space(ch) || ch == '\n' || ch == '/') {
2825 if (ch == '/') {
2826 int c;
2827 uint8_t *p = file->buf_ptr;
2828 PEEKC(c, p);
2829 if (c == '*') {
2830 p = parse_comment(p);
2831 file->buf_ptr = p - 1;
2832 } else if (c == '/') {
2833 p = parse_line_comment(p);
2834 file->buf_ptr = p - 1;
2835 } else
2836 break;
2837 ch = ' ';
2839 tok_str_add(ws_str, ch);
2840 cinp();
2843 t = ch;
2846 if (ws_str)
2847 return t;
2848 next_nomacro_spc();
2849 return tok;
2853 /* do macro substitution of current token with macro 's' and add
2854 result to (tok_str,tok_len). 'nested_list' is the list of all
2855 macros we got inside to avoid recursing. Return non zero if no
2856 substitution needs to be done */
2857 static int macro_subst_tok(
2858 TokenString *tok_str,
2859 Sym **nested_list,
2860 Sym *s,
2861 int can_read_stream)
2863 Sym *args, *sa, *sa1;
2864 int parlevel, *mstr, t, t1, spc;
2865 TokenString str;
2866 char *cstrval;
2867 CValue cval;
2868 CString cstr;
2869 char buf[32];
2871 /* if symbol is a macro, prepare substitution */
2872 /* special macros */
2873 if (tok == TOK___LINE__) {
2874 snprintf(buf, sizeof(buf), "%d", file->line_num);
2875 cstrval = buf;
2876 t1 = TOK_PPNUM;
2877 goto add_cstr1;
2878 } else if (tok == TOK___FILE__) {
2879 cstrval = file->filename;
2880 goto add_cstr;
2881 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2882 time_t ti;
2883 struct tm *tm;
2885 time(&ti);
2886 tm = localtime(&ti);
2887 if (tok == TOK___DATE__) {
2888 snprintf(buf, sizeof(buf), "%s %2d %d",
2889 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2890 } else {
2891 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2892 tm->tm_hour, tm->tm_min, tm->tm_sec);
2894 cstrval = buf;
2895 add_cstr:
2896 t1 = TOK_STR;
2897 add_cstr1:
2898 cstr_new(&cstr);
2899 cstr_cat(&cstr, cstrval);
2900 cstr_ccat(&cstr, '\0');
2901 cval.cstr = &cstr;
2902 tok_str_add2(tok_str, t1, &cval);
2903 cstr_free(&cstr);
2904 } else {
2905 int saved_parse_flags = parse_flags;
2907 mstr = s->d;
2908 if (s->type.t == MACRO_FUNC) {
2909 /* whitespace between macro name and argument list */
2910 TokenString ws_str;
2911 tok_str_new(&ws_str);
2913 spc = 0;
2914 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
2915 | PARSE_FLAG_ACCEPT_STRAYS;
2917 /* get next token from argument stream */
2918 t = next_argstream(nested_list, can_read_stream, &ws_str);
2919 if (t != '(') {
2920 /* not a macro substitution after all, restore the
2921 * macro token plus all whitespace we've read.
2922 * whitespace is intentionally not merged to preserve
2923 * newlines. */
2924 parse_flags = saved_parse_flags;
2925 tok_str_add(tok_str, tok);
2926 if (parse_flags & PARSE_FLAG_SPACES) {
2927 int i;
2928 for (i = 0; i < ws_str.len; i++)
2929 tok_str_add(tok_str, ws_str.str[i]);
2931 tok_str_free(ws_str.str);
2932 return 0;
2933 } else {
2934 tok_str_free(ws_str.str);
2936 next_nomacro(); /* eat '(' */
2938 /* argument macro */
2939 args = NULL;
2940 sa = s->next;
2941 /* NOTE: empty args are allowed, except if no args */
2942 for(;;) {
2943 do {
2944 next_argstream(nested_list, can_read_stream, NULL);
2945 } while (is_space(tok) || TOK_LINEFEED == tok);
2946 empty_arg:
2947 /* handle '()' case */
2948 if (!args && !sa && tok == ')')
2949 break;
2950 if (!sa)
2951 tcc_error("macro '%s' used with too many args",
2952 get_tok_str(s->v, 0));
2953 tok_str_new(&str);
2954 parlevel = spc = 0;
2955 /* NOTE: non zero sa->t indicates VA_ARGS */
2956 while ((parlevel > 0 ||
2957 (tok != ')' &&
2958 (tok != ',' || sa->type.t)))) {
2959 if (tok == TOK_EOF || tok == 0)
2960 break;
2961 if (tok == '(')
2962 parlevel++;
2963 else if (tok == ')')
2964 parlevel--;
2965 if (tok == TOK_LINEFEED)
2966 tok = ' ';
2967 if (!check_space(tok, &spc))
2968 tok_str_add2(&str, tok, &tokc);
2969 next_argstream(nested_list, can_read_stream, NULL);
2971 if (parlevel)
2972 expect(")");
2973 str.len -= spc;
2974 tok_str_add(&str, 0);
2975 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2976 sa1->d = str.str;
2977 sa = sa->next;
2978 if (tok == ')') {
2979 /* special case for gcc var args: add an empty
2980 var arg argument if it is omitted */
2981 if (sa && sa->type.t && gnu_ext)
2982 goto empty_arg;
2983 break;
2985 if (tok != ',')
2986 expect(",");
2988 if (sa) {
2989 tcc_error("macro '%s' used with too few args",
2990 get_tok_str(s->v, 0));
2993 parse_flags = saved_parse_flags;
2995 /* now subst each arg */
2996 mstr = macro_arg_subst(nested_list, mstr, args);
2997 /* free memory */
2998 sa = args;
2999 while (sa) {
3000 sa1 = sa->prev;
3001 tok_str_free(sa->d);
3002 sym_free(sa);
3003 sa = sa1;
3007 sym_push2(nested_list, s->v, 0, 0);
3008 parse_flags = saved_parse_flags;
3009 macro_subst(tok_str, nested_list, mstr, can_read_stream);
3011 /* pop nested defined symbol */
3012 sa1 = *nested_list;
3013 *nested_list = sa1->prev;
3014 sym_free(sa1);
3015 if (mstr != s->d)
3016 tok_str_free(mstr);
3018 return 0;
3021 int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3023 CString cstr;
3024 int n;
3026 cstr_new(&cstr);
3027 if (t1 != TOK_PLCHLDR)
3028 cstr_cat(&cstr, get_tok_str(t1, v1));
3029 n = cstr.size;
3030 if (t2 != TOK_PLCHLDR)
3031 cstr_cat(&cstr, get_tok_str(t2, v2));
3032 cstr_ccat(&cstr, '\0');
3034 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3035 memcpy(file->buffer, cstr.data, cstr.size);
3036 for (;;) {
3037 next_nomacro1();
3038 if (0 == *file->buf_ptr)
3039 break;
3040 if (is_space(tok))
3041 continue;
3042 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3043 n, cstr.data, (char*)cstr.data + n);
3044 break;
3046 tcc_close();
3048 //printf("paste <%s>\n", (char*)cstr.data);
3049 cstr_free(&cstr);
3050 return 0;
3053 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3054 return the resulting string (which must be freed). */
3055 static inline int *macro_twosharps(const int *ptr0)
3057 int t;
3058 CValue cval;
3059 TokenString macro_str1;
3060 int start_of_nosubsts = -1;
3061 const int *ptr;
3063 /* we search the first '##' */
3064 for (ptr = ptr0;;) {
3065 TOK_GET(&t, &ptr, &cval);
3066 if (t == TOK_TWOSHARPS)
3067 break;
3068 if (t == 0)
3069 return NULL;
3072 tok_str_new(&macro_str1);
3074 //tok_print(" $$$", ptr0);
3075 for (ptr = ptr0;;) {
3076 TOK_GET(&t, &ptr, &cval);
3077 if (t == 0)
3078 break;
3079 if (t == TOK_TWOSHARPS)
3080 continue;
3081 while (*ptr == TOK_TWOSHARPS) {
3082 int t1; CValue cv1;
3083 /* given 'a##b', remove nosubsts preceding 'a' */
3084 if (start_of_nosubsts >= 0)
3085 macro_str1.len = start_of_nosubsts;
3086 /* given 'a##b', remove nosubsts preceding 'b' */
3087 while ((t1 = *++ptr) == TOK_NOSUBST)
3089 if (t1 && t1 != TOK_TWOSHARPS) {
3090 TOK_GET(&t1, &ptr, &cv1);
3091 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3092 paste_tokens(t, &cval, t1, &cv1);
3093 t = tok, cval = tokc;
3097 if (t == TOK_NOSUBST) {
3098 if (start_of_nosubsts < 0)
3099 start_of_nosubsts = macro_str1.len;
3100 } else {
3101 start_of_nosubsts = -1;
3103 tok_str_add2(&macro_str1, t, &cval);
3105 tok_str_add(&macro_str1, 0);
3106 //tok_print(" ###", macro_str1.str);
3107 return macro_str1.str;
3110 /* do macro substitution of macro_str and add result to
3111 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3112 inside to avoid recursing. */
3113 static void macro_subst(
3114 TokenString *tok_str,
3115 Sym **nested_list,
3116 const int *macro_str,
3117 int can_read_stream
3120 Sym *s;
3121 const int *ptr;
3122 int t, spc, nosubst;
3123 CValue cval;
3124 int *macro_str1 = NULL;
3126 /* first scan for '##' operator handling */
3127 ptr = macro_str;
3128 spc = nosubst = 0;
3130 /* first scan for '##' operator handling */
3131 if (can_read_stream) {
3132 macro_str1 = macro_twosharps(ptr);
3133 if (macro_str1)
3134 ptr = macro_str1;
3137 while (1) {
3138 TOK_GET(&t, &ptr, &cval);
3139 if (t == 0)
3140 break;
3142 if (t >= TOK_IDENT && 0 == nosubst) {
3143 s = define_find(t);
3144 if (s == NULL)
3145 goto no_subst;
3147 /* if nested substitution, do nothing */
3148 if (sym_find2(*nested_list, t)) {
3149 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3150 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3151 goto no_subst;
3155 TokenString str;
3156 str.str = (int*)ptr;
3157 begin_macro(&str, 2);
3159 tok = t;
3160 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3162 if (str.alloc == 3) {
3163 /* already finished by reading function macro arguments */
3164 break;
3167 ptr = macro_ptr;
3168 end_macro ();
3171 spc = tok_str->len && is_space(tok_str->str[tok_str->len-1]);
3173 } else {
3175 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3176 tcc_error("stray '\\' in program");
3178 no_subst:
3179 if (!check_space(t, &spc))
3180 tok_str_add2(tok_str, t, &cval);
3181 nosubst = 0;
3182 if (t == TOK_NOSUBST)
3183 nosubst = 1;
3186 if (macro_str1)
3187 tok_str_free(macro_str1);
3191 /* return next token with macro substitution */
3192 ST_FUNC void next(void)
3194 redo:
3195 if (parse_flags & PARSE_FLAG_SPACES)
3196 next_nomacro_spc();
3197 else
3198 next_nomacro();
3200 if (macro_ptr) {
3201 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3202 /* discard preprocessor markers */
3203 goto redo;
3204 } else if (tok == 0) {
3205 /* end of macro or unget token string */
3206 end_macro();
3207 goto redo;
3209 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3210 Sym *s;
3211 /* if reading from file, try to substitute macros */
3212 s = define_find(tok);
3213 if (s) {
3214 static TokenString str; /* using static string for speed */
3215 Sym *nested_list = NULL;
3216 tok_str_new(&str);
3217 nested_list = NULL;
3218 macro_subst_tok(&str, &nested_list, s, 1);
3219 tok_str_add(&str, 0);
3220 begin_macro(&str, 0);
3221 goto redo;
3224 /* convert preprocessor tokens into C tokens */
3225 if (tok == TOK_PPNUM) {
3226 if (parse_flags & PARSE_FLAG_TOK_NUM)
3227 parse_number((char *)tokc.cstr->data);
3228 } else if (tok == TOK_PPSTR) {
3229 if (parse_flags & PARSE_FLAG_TOK_STR)
3230 parse_string((char *)tokc.cstr->data, tokc.cstr->size - 1);
3234 /* push back current token and set current token to 'last_tok'. Only
3235 identifier case handled for labels. */
3236 ST_INLN void unget_tok(int last_tok)
3238 TokenString *str = tcc_malloc(sizeof *str);
3239 tok_str_new(str);
3240 tok_str_add2(str, tok, &tokc);
3241 tok_str_add(str, 0);
3242 begin_macro(str, 1);
3243 tok = last_tok;
3246 /* better than nothing, but needs extension to handle '-E' option
3247 correctly too */
3248 ST_FUNC void preprocess_init(TCCState *s1)
3250 s1->include_stack_ptr = s1->include_stack;
3251 /* XXX: move that before to avoid having to initialize
3252 file->ifdef_stack_ptr ? */
3253 s1->ifdef_stack_ptr = s1->ifdef_stack;
3254 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3256 pvtop = vtop = vstack - 1;
3257 s1->pack_stack[0] = 0;
3258 s1->pack_stack_ptr = s1->pack_stack;
3260 isidnum_table['$' - CH_EOF] =
3261 tcc_state->dollars_in_identifiers ? IS_ID : 0;
3264 ST_FUNC void preprocess_new(void)
3266 int i, c;
3267 const char *p, *r;
3269 /* init isid table */
3270 for(i = CH_EOF; i<256; i++)
3271 isidnum_table[i - CH_EOF]
3272 = is_space(i) ? IS_SPC
3273 : isid(i) ? IS_ID
3274 : isnum(i) ? IS_NUM
3275 : 0;
3277 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3279 tok_ident = TOK_IDENT;
3280 p = tcc_keywords;
3281 while (*p) {
3282 r = p;
3283 for(;;) {
3284 c = *r++;
3285 if (c == '\0')
3286 break;
3288 tok_alloc(p, r - p - 1);
3289 p = r;
3293 ST_FUNC void preprocess_delete(void)
3295 int i, n;
3297 /* free -D and compiler defines */
3298 free_defines(NULL);
3300 /* cleanup from error/setjmp */
3301 while (macro_stack)
3302 end_macro();
3303 macro_ptr = NULL;
3305 /* free tokens */
3306 n = tok_ident - TOK_IDENT;
3307 for(i = 0; i < n; i++)
3308 tcc_free(table_ident[i]);
3309 tcc_free(table_ident);
3310 table_ident = NULL;
3313 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3315 int d;
3316 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE)
3317 return;
3318 if (level == 0 && f->line_ref && (d = f->line_num - f->line_ref) < 8) {
3319 while (d > 0)
3320 fputs("\n", s1->ppfp), --d;
3321 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3322 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3323 } else {
3324 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3325 level > 0 ? " 1" : level < 0 ? " 2" : "");
3327 f->line_ref = f->line_num;
3330 /* Preprocess the current file */
3331 ST_FUNC int tcc_preprocess(TCCState *s1)
3333 Sym *define_start;
3334 BufferedFile **iptr;
3335 int token_seen, spcs, level;
3337 preprocess_init(s1);
3338 define_start = define_stack;
3339 ch = file->buf_ptr[0];
3340 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3341 parse_flags = PARSE_FLAG_PREPROCESS
3342 | (parse_flags & PARSE_FLAG_ASM_FILE)
3343 | PARSE_FLAG_LINEFEED
3344 | PARSE_FLAG_SPACES
3345 | PARSE_FLAG_ACCEPT_STRAYS
3348 #ifdef PP_BENCH
3349 do next(); while (tok != TOK_EOF); return 0;
3350 #endif
3352 token_seen = spcs = 0;
3353 pp_line(s1, file, 0);
3355 for (;;) {
3356 iptr = s1->include_stack_ptr;
3357 next();
3358 if (tok == TOK_EOF)
3359 break;
3360 level = s1->include_stack_ptr - iptr;
3361 if (level) {
3362 if (level > 0)
3363 pp_line(s1, *iptr, 0);
3364 pp_line(s1, file, level);
3367 if (0 == token_seen) {
3368 if (tok == ' ') {
3369 ++spcs;
3370 continue;
3372 if (tok == TOK_LINEFEED) {
3373 spcs = 0;
3374 continue;
3376 pp_line(s1, file, 0);
3377 while (spcs)
3378 fputs(" ", s1->ppfp), --spcs;
3379 token_seen = 1;
3381 } else if (tok == TOK_LINEFEED) {
3382 ++file->line_ref;
3383 token_seen = 0;
3386 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3389 free_defines(define_start);
3390 return 0;