tccpp: restore -D symbols for multiple sources
[tinycc.git] / tccpp.c
blob88f3b472f706c8611bdb4490d5df826f578badbc
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 ST_DATA TinyAlloc *toksym_alloc;
42 ST_DATA TinyAlloc *tokstr_alloc;
43 ST_DATA TinyAlloc *cstr_alloc;
45 /* ------------------------------------------------------------------------- */
47 static TokenSym *hash_ident[TOK_HASH_SIZE];
48 static char token_buf[STRING_MAX_SIZE + 1];
49 static CString cstr_buf;
50 static TokenString tokstr_buf;
51 static unsigned char isidnum_table[256 - CH_EOF];
52 static int pp_debug_tok, pp_debug_symv;
53 static void tok_print(const char *msg, const int *str);
55 /* isidnum_table flags: */
56 #define IS_SPC 1
57 #define IS_ID 2
58 #define IS_NUM 4
60 static TokenString *macro_stack;
62 static const char tcc_keywords[] =
63 #define DEF(id, str) str "\0"
64 #include "tcctok.h"
65 #undef DEF
68 /* WARNING: the content of this string encodes token numbers */
69 static const unsigned char tok_two_chars[] =
70 /* outdated -- gr
71 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
72 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
73 */{
74 '<','=', TOK_LE,
75 '>','=', TOK_GE,
76 '!','=', TOK_NE,
77 '&','&', TOK_LAND,
78 '|','|', TOK_LOR,
79 '+','+', TOK_INC,
80 '-','-', TOK_DEC,
81 '=','=', TOK_EQ,
82 '<','<', TOK_SHL,
83 '>','>', TOK_SAR,
84 '+','=', TOK_A_ADD,
85 '-','=', TOK_A_SUB,
86 '*','=', TOK_A_MUL,
87 '/','=', TOK_A_DIV,
88 '%','=', TOK_A_MOD,
89 '&','=', TOK_A_AND,
90 '^','=', TOK_A_XOR,
91 '|','=', TOK_A_OR,
92 '-','>', TOK_ARROW,
93 '.','.', 0xa8, // C++ token ?
94 '#','#', TOK_TWOSHARPS,
98 static void next_nomacro_spc(void);
100 ST_FUNC void skip(int c)
102 if (tok != c)
103 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
104 next();
107 ST_FUNC void expect(const char *msg)
109 tcc_error("%s expected", msg);
112 ST_FUNC void begin_macro(TokenString *str, int alloc)
114 str->alloc = alloc;
115 str->prev = macro_stack;
116 str->prev_ptr = macro_ptr;
117 macro_ptr = str->str;
118 macro_stack = str;
121 ST_FUNC void end_macro(void)
123 TokenString *str = macro_stack;
124 macro_stack = str->prev;
125 macro_ptr = str->prev_ptr;
126 if (str->alloc == 2) {
127 str->alloc = 3; /* just mark as finished */
128 } else {
129 tok_str_free(str->str);
130 if (str->alloc == 1)
131 tcc_free(str);
135 ST_FUNC char *trimfront(char *p)
137 while (*p && (unsigned char)*p <= ' ')
138 ++p;
139 return p;
142 ST_FUNC char *trimback(char *a, char *e)
144 while (e > a && (unsigned char)e[-1] <= ' ')
145 --e;
146 *e = 0;;
147 return a;
150 /* ------------------------------------------------------------------------- */
151 /* Custom allocator for tiny objects */
153 ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t size)
155 TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
156 al->p = al->buffer = tcc_malloc(size);
157 al->limit = limit;
158 al->size = size;
159 if (pal) *pal = al;
160 return al;
163 ST_FUNC void tal_delete(TinyAlloc *al)
165 TinyAlloc *next;
167 tail_call:
168 if (!al)
169 return;
170 #ifdef TAL_INFO
171 fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
172 al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
173 (al->peak_p - al->buffer) * 100.0 / al->size);
174 #endif
175 #ifdef TAL_DEBUG
176 if (al->nb_allocs > 0) {
177 fprintf(stderr, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
178 al->nb_allocs, al->limit);
179 uint8_t *p = al->buffer;
180 while (p < al->p) {
181 tal_header_t *header = (tal_header_t *)p;
182 if (header->line_num > 0) {
183 fprintf(stderr, " file %s, line %u: %u bytes\n",
184 header->file_name, header->line_num, header->size);
186 p += header->size + sizeof(tal_header_t);
189 #endif
190 next = al->next;
191 tcc_free(al->buffer);
192 tcc_free(al);
193 al = next;
194 goto tail_call;
197 ST_FUNC void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
199 if (!p)
200 return;
201 tail_call:
202 if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
203 #ifdef TAL_DEBUG
204 tal_header_t *header = (((tal_header_t *)p) - 1);
205 if (header->line_num < 0) {
206 fprintf(stderr, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
207 file, line);
208 fprintf(stderr, " file %s, line %u: %u bytes\n",
209 header->file_name, -header->line_num, header->size);
210 } else
211 header->line_num = -header->line_num;
212 #endif
213 al->nb_allocs--;
214 if (!al->nb_allocs)
215 al->p = al->buffer;
216 } else if (al->next) {
217 al = al->next;
218 goto tail_call;
220 else
221 tcc_free(p);
224 ST_FUNC void *tal_realloc_impl(TinyAlloc **pal, void *p, size_t size TAL_DEBUG_PARAMS)
226 tal_header_t *header;
227 void *ret;
228 int is_own;
229 size_t adj_size = (size + 3) & -4;
230 TinyAlloc *al = *pal;
232 tail_call:
233 is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
234 if ((!p || is_own) && size <= al->limit) {
235 if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) {
236 header = (tal_header_t *)al->p;
237 header->size = adj_size;
238 #ifdef TAL_DEBUG
239 int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
240 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
241 header->file_name[TAL_DEBUG_FILE_LEN] = 0;
242 header->line_num = line;
243 #endif
244 ret = al->p + sizeof(tal_header_t);
245 al->p += adj_size + sizeof(tal_header_t);
246 if (is_own) {
247 header = (((tal_header_t *)p) - 1);
248 memcpy(ret, p, header->size);
249 #ifdef TAL_DEBUG
250 header->line_num = -header->line_num;
251 #endif
252 } else {
253 al->nb_allocs++;
255 #ifdef TAL_INFO
256 if (al->nb_peak < al->nb_allocs)
257 al->nb_peak = al->nb_allocs;
258 if (al->peak_p < al->p)
259 al->peak_p = al->p;
260 al->nb_total++;
261 #endif
262 return ret;
263 } else if (is_own) {
264 al->nb_allocs--;
265 ret = tal_realloc(*pal, 0, size);
266 header = (((tal_header_t *)p) - 1);
267 memcpy(ret, p, header->size);
268 #ifdef TAL_DEBUG
269 header->line_num = -header->line_num;
270 #endif
271 return ret;
273 if (al->next) {
274 al = al->next;
275 } else {
276 TinyAlloc *bottom = al, *next = al->top ? al->top : al;
278 al = tal_new(pal, next->limit, next->size * 2);
279 al->next = next;
280 bottom->top = al;
282 goto tail_call;
284 if (is_own) {
285 al->nb_allocs--;
286 ret = tcc_malloc(size);
287 header = (((tal_header_t *)p) - 1);
288 memcpy(ret, p, header->size);
289 #ifdef TAL_DEBUG
290 header->line_num = -header->line_num;
291 #endif
292 } else if (al->next) {
293 al = al->next;
294 goto tail_call;
295 } else
296 ret = tcc_realloc(p, size);
297 #ifdef TAL_INFO
298 al->nb_missed++;
299 #endif
300 return ret;
303 /* ------------------------------------------------------------------------- */
304 /* CString handling */
305 static void cstr_realloc(CString *cstr, int new_size)
307 int size;
308 void *data;
310 size = cstr->size_allocated;
311 if (size < 8)
312 size = 8; /* no need to allocate a too small first string */
313 while (size < new_size)
314 size = size * 2;
315 data = tal_realloc(cstr_alloc, cstr->data_allocated, size);
316 cstr->data_allocated = data;
317 cstr->size_allocated = size;
318 cstr->data = data;
321 /* add a byte */
322 ST_FUNC void cstr_ccat(CString *cstr, int ch)
324 int size;
325 size = cstr->size + 1;
326 if (size > cstr->size_allocated)
327 cstr_realloc(cstr, size);
328 ((unsigned char *)cstr->data)[size - 1] = ch;
329 cstr->size = size;
332 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len)
334 int size;
335 if (len <= 0)
336 len = strlen(str) + 1 + len;
337 size = cstr->size + len;
338 if (size > cstr->size_allocated)
339 cstr_realloc(cstr, size);
340 memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
341 cstr->size = size;
344 /* add a wide char */
345 ST_FUNC void cstr_wccat(CString *cstr, int ch)
347 int size;
348 size = cstr->size + sizeof(nwchar_t);
349 if (size > cstr->size_allocated)
350 cstr_realloc(cstr, size);
351 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
352 cstr->size = size;
355 ST_FUNC void cstr_new(CString *cstr)
357 memset(cstr, 0, sizeof(CString));
360 /* free string and reset it to NULL */
361 ST_FUNC void cstr_free(CString *cstr)
363 tal_free(cstr_alloc, cstr->data_allocated);
364 cstr_new(cstr);
367 /* reset string to empty */
368 ST_FUNC void cstr_reset(CString *cstr)
370 cstr->size = 0;
373 /* XXX: unicode ? */
374 static void add_char(CString *cstr, int c)
376 if (c == '\'' || c == '\"' || c == '\\') {
377 /* XXX: could be more precise if char or string */
378 cstr_ccat(cstr, '\\');
380 if (c >= 32 && c <= 126) {
381 cstr_ccat(cstr, c);
382 } else {
383 cstr_ccat(cstr, '\\');
384 if (c == '\n') {
385 cstr_ccat(cstr, 'n');
386 } else {
387 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
388 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
389 cstr_ccat(cstr, '0' + (c & 7));
394 /* ------------------------------------------------------------------------- */
395 /* allocate a new token */
396 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
398 TokenSym *ts, **ptable;
399 int i;
401 if (tok_ident >= SYM_FIRST_ANOM)
402 tcc_error("memory full (symbols)");
404 /* expand token table if needed */
405 i = tok_ident - TOK_IDENT;
406 if ((i % TOK_ALLOC_INCR) == 0) {
407 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
408 table_ident = ptable;
411 ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len);
412 table_ident[i] = ts;
413 ts->tok = tok_ident++;
414 ts->sym_define = NULL;
415 ts->sym_label = NULL;
416 ts->sym_struct = NULL;
417 ts->sym_identifier = NULL;
418 ts->len = len;
419 ts->hash_next = NULL;
420 memcpy(ts->str, str, len);
421 ts->str[len] = '\0';
422 *pts = ts;
423 return ts;
426 #define TOK_HASH_INIT 1
427 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
430 /* find a token and add it if not found */
431 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
433 TokenSym *ts, **pts;
434 int i;
435 unsigned int h;
437 h = TOK_HASH_INIT;
438 for(i=0;i<len;i++)
439 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
440 h &= (TOK_HASH_SIZE - 1);
442 pts = &hash_ident[h];
443 for(;;) {
444 ts = *pts;
445 if (!ts)
446 break;
447 if (ts->len == len && !memcmp(ts->str, str, len))
448 return ts;
449 pts = &(ts->hash_next);
451 return tok_alloc_new(pts, str, len);
454 /* XXX: buffer overflow */
455 /* XXX: float tokens */
456 ST_FUNC const char *get_tok_str(int v, CValue *cv)
458 char *p;
459 int i, len;
461 cstr_reset(&cstr_buf);
462 p = cstr_buf.data;
464 switch(v) {
465 case TOK_CINT:
466 case TOK_CUINT:
467 case TOK_CLLONG:
468 case TOK_CULLONG:
469 /* XXX: not quite exact, but only useful for testing */
470 #ifdef _WIN32
471 sprintf(p, "%u", (unsigned)cv->i);
472 #else
473 sprintf(p, "%llu", (unsigned long long)cv->i);
474 #endif
475 break;
476 case TOK_LCHAR:
477 cstr_ccat(&cstr_buf, 'L');
478 case TOK_CCHAR:
479 cstr_ccat(&cstr_buf, '\'');
480 add_char(&cstr_buf, cv->i);
481 cstr_ccat(&cstr_buf, '\'');
482 cstr_ccat(&cstr_buf, '\0');
483 break;
484 case TOK_PPNUM:
485 case TOK_PPSTR:
486 return (char*)cv->str.data;
487 case TOK_LSTR:
488 cstr_ccat(&cstr_buf, 'L');
489 case TOK_STR:
490 cstr_ccat(&cstr_buf, '\"');
491 if (v == TOK_STR) {
492 len = cv->str.size - 1;
493 for(i=0;i<len;i++)
494 add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
495 } else {
496 len = (cv->str.size / sizeof(nwchar_t)) - 1;
497 for(i=0;i<len;i++)
498 add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
500 cstr_ccat(&cstr_buf, '\"');
501 cstr_ccat(&cstr_buf, '\0');
502 break;
504 case TOK_CFLOAT:
505 cstr_cat(&cstr_buf, "<float>", 0);
506 break;
507 case TOK_CDOUBLE:
508 cstr_cat(&cstr_buf, "<double>", 0);
509 break;
510 case TOK_CLDOUBLE:
511 cstr_cat(&cstr_buf, "<long double>", 0);
512 break;
513 case TOK_LINENUM:
514 cstr_cat(&cstr_buf, "<linenumber>", 0);
515 break;
517 /* above tokens have value, the ones below don't */
519 case TOK_LT:
520 v = '<';
521 goto addv;
522 case TOK_GT:
523 v = '>';
524 goto addv;
525 case TOK_DOTS:
526 return strcpy(p, "...");
527 case TOK_A_SHL:
528 return strcpy(p, "<<=");
529 case TOK_A_SAR:
530 return strcpy(p, ">>=");
531 default:
532 if (v < TOK_IDENT) {
533 /* search in two bytes table */
534 const unsigned char *q = tok_two_chars;
535 while (*q) {
536 if (q[2] == v) {
537 *p++ = q[0];
538 *p++ = q[1];
539 *p = '\0';
540 return cstr_buf.data;
542 q += 3;
544 if (v >= 127) {
545 sprintf(cstr_buf.data, "<%02x>", v);
546 return cstr_buf.data;
548 addv:
549 *p++ = v;
550 *p = '\0';
551 } else if (v < tok_ident) {
552 return table_ident[v - TOK_IDENT]->str;
553 } else if (v >= SYM_FIRST_ANOM) {
554 /* special name for anonymous symbol */
555 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
556 } else {
557 /* should never happen */
558 return NULL;
560 break;
562 return cstr_buf.data;
565 /* return the current character, handling end of block if necessary
566 (but not stray) */
567 ST_FUNC int handle_eob(void)
569 BufferedFile *bf = file;
570 int len;
572 /* only tries to read if really end of buffer */
573 if (bf->buf_ptr >= bf->buf_end) {
574 if (bf->fd != -1) {
575 #if defined(PARSE_DEBUG)
576 len = 1;
577 #else
578 len = IO_BUF_SIZE;
579 #endif
580 len = read(bf->fd, bf->buffer, len);
581 if (len < 0)
582 len = 0;
583 } else {
584 len = 0;
586 total_bytes += len;
587 bf->buf_ptr = bf->buffer;
588 bf->buf_end = bf->buffer + len;
589 *bf->buf_end = CH_EOB;
591 if (bf->buf_ptr < bf->buf_end) {
592 return bf->buf_ptr[0];
593 } else {
594 bf->buf_ptr = bf->buf_end;
595 return CH_EOF;
599 /* read next char from current input file and handle end of input buffer */
600 ST_INLN void inp(void)
602 ch = *(++(file->buf_ptr));
603 /* end of buffer/file handling */
604 if (ch == CH_EOB)
605 ch = handle_eob();
608 /* handle '\[\r]\n' */
609 static int handle_stray_noerror(void)
611 while (ch == '\\') {
612 inp();
613 if (ch == '\n') {
614 file->line_num++;
615 inp();
616 } else if (ch == '\r') {
617 inp();
618 if (ch != '\n')
619 goto fail;
620 file->line_num++;
621 inp();
622 } else {
623 fail:
624 return 1;
627 return 0;
630 static void handle_stray(void)
632 if (handle_stray_noerror())
633 tcc_error("stray '\\' in program");
636 /* skip the stray and handle the \\n case. Output an error if
637 incorrect char after the stray */
638 static int handle_stray1(uint8_t *p)
640 int c;
642 file->buf_ptr = p;
643 if (p >= file->buf_end) {
644 c = handle_eob();
645 if (c != '\\')
646 return c;
647 p = file->buf_ptr;
649 ch = *p;
650 if (handle_stray_noerror()) {
651 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
652 tcc_error("stray '\\' in program");
653 *--file->buf_ptr = '\\';
655 p = file->buf_ptr;
656 c = *p;
657 return c;
660 /* handle just the EOB case, but not stray */
661 #define PEEKC_EOB(c, p)\
663 p++;\
664 c = *p;\
665 if (c == '\\') {\
666 file->buf_ptr = p;\
667 c = handle_eob();\
668 p = file->buf_ptr;\
672 /* handle the complicated stray case */
673 #define PEEKC(c, p)\
675 p++;\
676 c = *p;\
677 if (c == '\\') {\
678 c = handle_stray1(p);\
679 p = file->buf_ptr;\
683 /* input with '\[\r]\n' handling. Note that this function cannot
684 handle other characters after '\', so you cannot call it inside
685 strings or comments */
686 ST_FUNC void minp(void)
688 inp();
689 if (ch == '\\')
690 handle_stray();
693 /* single line C++ comments */
694 static uint8_t *parse_line_comment(uint8_t *p)
696 int c;
698 p++;
699 for(;;) {
700 c = *p;
701 redo:
702 if (c == '\n' || c == CH_EOF) {
703 break;
704 } else if (c == '\\') {
705 file->buf_ptr = p;
706 c = handle_eob();
707 p = file->buf_ptr;
708 if (c == '\\') {
709 PEEKC_EOB(c, p);
710 if (c == '\n') {
711 file->line_num++;
712 PEEKC_EOB(c, p);
713 } else if (c == '\r') {
714 PEEKC_EOB(c, p);
715 if (c == '\n') {
716 file->line_num++;
717 PEEKC_EOB(c, p);
720 } else {
721 goto redo;
723 } else {
724 p++;
727 return p;
730 /* C comments */
731 ST_FUNC uint8_t *parse_comment(uint8_t *p)
733 int c;
735 p++;
736 for(;;) {
737 /* fast skip loop */
738 for(;;) {
739 c = *p;
740 if (c == '\n' || c == '*' || c == '\\')
741 break;
742 p++;
743 c = *p;
744 if (c == '\n' || c == '*' || c == '\\')
745 break;
746 p++;
748 /* now we can handle all the cases */
749 if (c == '\n') {
750 file->line_num++;
751 p++;
752 } else if (c == '*') {
753 p++;
754 for(;;) {
755 c = *p;
756 if (c == '*') {
757 p++;
758 } else if (c == '/') {
759 goto end_of_comment;
760 } else if (c == '\\') {
761 file->buf_ptr = p;
762 c = handle_eob();
763 p = file->buf_ptr;
764 if (c == CH_EOF)
765 tcc_error("unexpected end of file in comment");
766 if (c == '\\') {
767 /* skip '\[\r]\n', otherwise just skip the stray */
768 while (c == '\\') {
769 PEEKC_EOB(c, p);
770 if (c == '\n') {
771 file->line_num++;
772 PEEKC_EOB(c, p);
773 } else if (c == '\r') {
774 PEEKC_EOB(c, p);
775 if (c == '\n') {
776 file->line_num++;
777 PEEKC_EOB(c, p);
779 } else {
780 goto after_star;
784 } else {
785 break;
788 after_star: ;
789 } else {
790 /* stray, eob or eof */
791 file->buf_ptr = p;
792 c = handle_eob();
793 p = file->buf_ptr;
794 if (c == CH_EOF) {
795 tcc_error("unexpected end of file in comment");
796 } else if (c == '\\') {
797 p++;
801 end_of_comment:
802 p++;
803 return p;
806 #define cinp minp
808 static inline void skip_spaces(void)
810 while (isidnum_table[ch - CH_EOF] & IS_SPC)
811 cinp();
814 static inline int check_space(int t, int *spc)
816 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
817 if (*spc)
818 return 1;
819 *spc = 1;
820 } else
821 *spc = 0;
822 return 0;
825 /* parse a string without interpreting escapes */
826 static uint8_t *parse_pp_string(uint8_t *p,
827 int sep, CString *str)
829 int c;
830 p++;
831 for(;;) {
832 c = *p;
833 if (c == sep) {
834 break;
835 } else if (c == '\\') {
836 file->buf_ptr = p;
837 c = handle_eob();
838 p = file->buf_ptr;
839 if (c == CH_EOF) {
840 unterminated_string:
841 /* XXX: indicate line number of start of string */
842 tcc_error("missing terminating %c character", sep);
843 } else if (c == '\\') {
844 /* escape : just skip \[\r]\n */
845 PEEKC_EOB(c, p);
846 if (c == '\n') {
847 file->line_num++;
848 p++;
849 } else if (c == '\r') {
850 PEEKC_EOB(c, p);
851 if (c != '\n')
852 expect("'\n' after '\r'");
853 file->line_num++;
854 p++;
855 } else if (c == CH_EOF) {
856 goto unterminated_string;
857 } else {
858 if (str) {
859 cstr_ccat(str, '\\');
860 cstr_ccat(str, c);
862 p++;
865 } else if (c == '\n') {
866 file->line_num++;
867 goto add_char;
868 } else if (c == '\r') {
869 PEEKC_EOB(c, p);
870 if (c != '\n') {
871 if (str)
872 cstr_ccat(str, '\r');
873 } else {
874 file->line_num++;
875 goto add_char;
877 } else {
878 add_char:
879 if (str)
880 cstr_ccat(str, c);
881 p++;
884 p++;
885 return p;
888 /* skip block of text until #else, #elif or #endif. skip also pairs of
889 #if/#endif */
890 static void preprocess_skip(void)
892 int a, start_of_line, c, in_warn_or_error;
893 uint8_t *p;
895 p = file->buf_ptr;
896 a = 0;
897 redo_start:
898 start_of_line = 1;
899 in_warn_or_error = 0;
900 for(;;) {
901 redo_no_start:
902 c = *p;
903 switch(c) {
904 case ' ':
905 case '\t':
906 case '\f':
907 case '\v':
908 case '\r':
909 p++;
910 goto redo_no_start;
911 case '\n':
912 file->line_num++;
913 p++;
914 goto redo_start;
915 case '\\':
916 file->buf_ptr = p;
917 c = handle_eob();
918 if (c == CH_EOF) {
919 expect("#endif");
920 } else if (c == '\\') {
921 ch = file->buf_ptr[0];
922 handle_stray_noerror();
924 p = file->buf_ptr;
925 goto redo_no_start;
926 /* skip strings */
927 case '\"':
928 case '\'':
929 if (in_warn_or_error)
930 goto _default;
931 p = parse_pp_string(p, c, NULL);
932 break;
933 /* skip comments */
934 case '/':
935 if (in_warn_or_error)
936 goto _default;
937 file->buf_ptr = p;
938 ch = *p;
939 minp();
940 p = file->buf_ptr;
941 if (ch == '*') {
942 p = parse_comment(p);
943 } else if (ch == '/') {
944 p = parse_line_comment(p);
946 break;
947 case '#':
948 p++;
949 if (start_of_line) {
950 file->buf_ptr = p;
951 next_nomacro();
952 p = file->buf_ptr;
953 if (a == 0 &&
954 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
955 goto the_end;
956 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
957 a++;
958 else if (tok == TOK_ENDIF)
959 a--;
960 else if( tok == TOK_ERROR || tok == TOK_WARNING)
961 in_warn_or_error = 1;
962 else if (tok == TOK_LINEFEED)
963 goto redo_start;
964 else if (parse_flags & PARSE_FLAG_ASM_FILE)
965 p = parse_line_comment(p);
966 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
967 p = parse_line_comment(p);
968 break;
969 _default:
970 default:
971 p++;
972 break;
974 start_of_line = 0;
976 the_end: ;
977 file->buf_ptr = p;
980 /* ParseState handling */
982 /* XXX: currently, no include file info is stored. Thus, we cannot display
983 accurate messages if the function or data definition spans multiple
984 files */
986 /* save current parse state in 's' */
987 ST_FUNC void save_parse_state(ParseState *s)
989 s->line_num = file->line_num;
990 s->macro_ptr = macro_ptr;
991 s->tok = tok;
992 s->tokc = tokc;
995 /* restore parse state from 's' */
996 ST_FUNC void restore_parse_state(ParseState *s)
998 file->line_num = s->line_num;
999 macro_ptr = s->macro_ptr;
1000 tok = s->tok;
1001 tokc = s->tokc;
1004 /* return the number of additional 'ints' necessary to store the
1005 token */
1006 static inline int tok_size(const int *p)
1008 switch(*p) {
1009 /* 4 bytes */
1010 case TOK_CINT:
1011 case TOK_CUINT:
1012 case TOK_CCHAR:
1013 case TOK_LCHAR:
1014 case TOK_CFLOAT:
1015 case TOK_LINENUM:
1016 return 1 + 1;
1017 case TOK_STR:
1018 case TOK_LSTR:
1019 case TOK_PPNUM:
1020 case TOK_PPSTR:
1021 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
1022 case TOK_CDOUBLE:
1023 case TOK_CLLONG:
1024 case TOK_CULLONG:
1025 return 1 + 2;
1026 case TOK_CLDOUBLE:
1027 return 1 + LDOUBLE_SIZE / 4;
1028 default:
1029 return 1 + 0;
1033 /* token string handling */
1035 ST_INLN void tok_str_new(TokenString *s)
1037 s->str = NULL;
1038 s->len = 0;
1039 s->allocated_len = 0;
1040 s->last_line_num = -1;
1043 ST_FUNC int *tok_str_dup(TokenString *s)
1045 int *str;
1047 str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
1048 memcpy(str, s->str, s->len * sizeof(int));
1049 return str;
1052 ST_FUNC void tok_str_free(int *str)
1054 tal_free(tokstr_alloc, str);
1057 ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
1059 int *str, size;
1061 size = s->allocated_len;
1062 if (size < 16)
1063 size = 16;
1064 while (size < new_size)
1065 size = size * 2;
1066 TCC_ASSERT((size & (size -1)) == 0);
1067 if (size > s->allocated_len) {
1068 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
1069 s->allocated_len = size;
1070 s->str = str;
1072 return s->str;
1075 ST_FUNC void tok_str_add(TokenString *s, int t)
1077 int len, *str;
1079 len = s->len;
1080 str = s->str;
1081 if (len >= s->allocated_len)
1082 str = tok_str_realloc(s, len + 1);
1083 str[len++] = t;
1084 s->len = len;
1087 static void tok_str_add2(TokenString *s, int t, CValue *cv)
1089 int len, *str;
1091 len = s->len;
1092 str = s->str;
1094 /* allocate space for worst case */
1095 if (len + TOK_MAX_SIZE >= s->allocated_len)
1096 str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
1097 str[len++] = t;
1098 switch(t) {
1099 case TOK_CINT:
1100 case TOK_CUINT:
1101 case TOK_CCHAR:
1102 case TOK_LCHAR:
1103 case TOK_CFLOAT:
1104 case TOK_LINENUM:
1105 str[len++] = cv->tab[0];
1106 break;
1107 case TOK_PPNUM:
1108 case TOK_PPSTR:
1109 case TOK_STR:
1110 case TOK_LSTR:
1112 /* Insert the string into the int array. */
1113 size_t nb_words =
1114 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
1115 if (len + nb_words >= s->allocated_len)
1116 str = tok_str_realloc(s, len + nb_words + 1);
1117 str[len] = cv->str.size;
1118 memcpy(&str[len + 1], cv->str.data, cv->str.size);
1119 len += nb_words;
1121 break;
1122 case TOK_CDOUBLE:
1123 case TOK_CLLONG:
1124 case TOK_CULLONG:
1125 #if LDOUBLE_SIZE == 8
1126 case TOK_CLDOUBLE:
1127 #endif
1128 str[len++] = cv->tab[0];
1129 str[len++] = cv->tab[1];
1130 break;
1131 #if LDOUBLE_SIZE == 12
1132 case TOK_CLDOUBLE:
1133 str[len++] = cv->tab[0];
1134 str[len++] = cv->tab[1];
1135 str[len++] = cv->tab[2];
1136 #elif LDOUBLE_SIZE == 16
1137 case TOK_CLDOUBLE:
1138 str[len++] = cv->tab[0];
1139 str[len++] = cv->tab[1];
1140 str[len++] = cv->tab[2];
1141 str[len++] = cv->tab[3];
1142 #elif LDOUBLE_SIZE != 8
1143 #error add long double size support
1144 #endif
1145 break;
1146 default:
1147 break;
1149 s->len = len;
1152 /* add the current parse token in token string 's' */
1153 ST_FUNC void tok_str_add_tok(TokenString *s)
1155 CValue cval;
1157 /* save line number info */
1158 if (file->line_num != s->last_line_num) {
1159 s->last_line_num = file->line_num;
1160 cval.i = s->last_line_num;
1161 tok_str_add2(s, TOK_LINENUM, &cval);
1163 tok_str_add2(s, tok, &tokc);
1166 /* get a token from an integer array and increment pointer
1167 accordingly. we code it as a macro to avoid pointer aliasing. */
1168 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
1170 const int *p = *pp;
1171 int n, *tab;
1173 tab = cv->tab;
1174 switch(*t = *p++) {
1175 case TOK_CINT:
1176 case TOK_CUINT:
1177 case TOK_CCHAR:
1178 case TOK_LCHAR:
1179 case TOK_CFLOAT:
1180 case TOK_LINENUM:
1181 tab[0] = *p++;
1182 break;
1183 case TOK_STR:
1184 case TOK_LSTR:
1185 case TOK_PPNUM:
1186 case TOK_PPSTR:
1187 cv->str.size = *p++;
1188 cv->str.data = p;
1189 cv->str.data_allocated = 0;
1190 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1191 break;
1192 case TOK_CDOUBLE:
1193 case TOK_CLLONG:
1194 case TOK_CULLONG:
1195 n = 2;
1196 goto copy;
1197 case TOK_CLDOUBLE:
1198 #if LDOUBLE_SIZE == 16
1199 n = 4;
1200 #elif LDOUBLE_SIZE == 12
1201 n = 3;
1202 #elif LDOUBLE_SIZE == 8
1203 n = 2;
1204 #else
1205 # error add long double size support
1206 #endif
1207 copy:
1209 *tab++ = *p++;
1210 while (--n);
1211 break;
1212 default:
1213 break;
1215 *pp = p;
1218 /* Calling this function is expensive, but it is not possible
1219 to read a token string backwards. */
1220 static int tok_last(const int *str0, const int *str1)
1222 const int *str = str0;
1223 int tok = 0;
1224 CValue cval;
1226 while (str < str1)
1227 TOK_GET(&tok, &str, &cval);
1228 return tok;
1231 static int macro_is_equal(const int *a, const int *b)
1233 CValue cv;
1234 int t;
1236 if (!a || !b)
1237 return 1;
1239 while (*a && *b) {
1240 /* first time preallocate static cstr_buf, next time only reset position to start */
1241 cstr_reset(&cstr_buf);
1242 TOK_GET(&t, &a, &cv);
1243 cstr_cat(&cstr_buf, get_tok_str(t, &cv), 0);
1244 TOK_GET(&t, &b, &cv);
1245 if (strcmp(cstr_buf.data, get_tok_str(t, &cv)))
1246 return 0;
1248 return !(*a || *b);
1251 /* defines handling */
1252 ST_INLN void define_push(int v, int macro_type, TokenString *str, Sym *first_arg)
1254 Sym *s, *o;
1256 o = define_find(v);
1257 s = sym_push2(&define_stack, v, macro_type, 0);
1258 s->d = str ? tok_str_dup(str) : NULL;
1259 s->next = first_arg;
1260 table_ident[v - TOK_IDENT]->sym_define = s;
1262 if (o && !macro_is_equal(o->d, s->d))
1263 tcc_warning("%s redefined", get_tok_str(v, NULL));
1266 /* undefined a define symbol. Its name is just set to zero */
1267 ST_FUNC void define_undef(Sym *s)
1269 int v = s->v;
1270 if (v >= TOK_IDENT && v < tok_ident)
1271 table_ident[v - TOK_IDENT]->sym_define = NULL;
1274 ST_INLN Sym *define_find(int v)
1276 v -= TOK_IDENT;
1277 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1278 return NULL;
1279 return table_ident[v]->sym_define;
1282 /* free define stack until top reaches 'b' */
1283 ST_FUNC void free_defines(Sym *b)
1285 while (define_stack != b) {
1286 Sym *top = define_stack;
1287 define_stack = top->prev;
1288 tok_str_free(top->d);
1289 define_undef(top);
1290 sym_free(top);
1293 /* restore remaining (-D or predefined) symbols */
1294 while (b) {
1295 int v = b->v;
1296 if (v >= TOK_IDENT && v < tok_ident) {
1297 Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
1298 if (!*d)
1299 *d = b;
1301 b = b->prev;
1305 /* label lookup */
1306 ST_FUNC Sym *label_find(int v)
1308 v -= TOK_IDENT;
1309 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1310 return NULL;
1311 return table_ident[v]->sym_label;
1314 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1316 Sym *s, **ps;
1317 s = sym_push2(ptop, v, 0, 0);
1318 s->r = flags;
1319 ps = &table_ident[v - TOK_IDENT]->sym_label;
1320 if (ptop == &global_label_stack) {
1321 /* modify the top most local identifier, so that
1322 sym_identifier will point to 's' when popped */
1323 while (*ps != NULL)
1324 ps = &(*ps)->prev_tok;
1326 s->prev_tok = *ps;
1327 *ps = s;
1328 return s;
1331 /* pop labels until element last is reached. Look if any labels are
1332 undefined. Define symbols if '&&label' was used. */
1333 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1335 Sym *s, *s1;
1336 for(s = *ptop; s != slast; s = s1) {
1337 s1 = s->prev;
1338 if (s->r == LABEL_DECLARED) {
1339 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1340 } else if (s->r == LABEL_FORWARD) {
1341 tcc_error("label '%s' used but not defined",
1342 get_tok_str(s->v, NULL));
1343 } else {
1344 if (s->c) {
1345 /* define corresponding symbol. A size of
1346 1 is put. */
1347 put_extern_sym(s, cur_text_section, s->jnext, 1);
1350 /* remove label */
1351 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1352 sym_free(s);
1354 *ptop = slast;
1357 /* eval an expression for #if/#elif */
1358 static int expr_preprocess(void)
1360 int c, t;
1361 TokenString str;
1363 tok_str_new(&str);
1364 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1365 next(); /* do macro subst */
1366 if (tok == TOK_DEFINED) {
1367 next_nomacro();
1368 t = tok;
1369 if (t == '(')
1370 next_nomacro();
1371 c = define_find(tok) != 0;
1372 if (t == '(')
1373 next_nomacro();
1374 tok = TOK_CINT;
1375 tokc.i = c;
1376 } else if (tok >= TOK_IDENT) {
1377 /* if undefined macro */
1378 tok = TOK_CINT;
1379 tokc.i = 0;
1381 tok_str_add_tok(&str);
1383 tok_str_add(&str, -1); /* simulate end of file */
1384 tok_str_add(&str, 0);
1385 /* now evaluate C constant expression */
1386 begin_macro(&str, 0);
1387 next();
1388 c = expr_const();
1389 end_macro();
1390 return c != 0;
1394 /* parse after #define */
1395 ST_FUNC void parse_define(void)
1397 Sym *s, *first, **ps;
1398 int v, t, varg, is_vaargs, spc;
1399 int saved_parse_flags = parse_flags;
1401 v = tok;
1402 if (v < TOK_IDENT)
1403 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1404 /* XXX: should check if same macro (ANSI) */
1405 first = NULL;
1406 t = MACRO_OBJ;
1407 /* '(' must be just after macro definition for MACRO_FUNC */
1408 parse_flags |= PARSE_FLAG_SPACES;
1409 next_nomacro_spc();
1410 if (tok == '(') {
1411 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1412 parse_flags &= ~PARSE_FLAG_ASM_FILE;
1413 isidnum_table['.' - CH_EOF] = 0;
1414 next_nomacro();
1415 ps = &first;
1416 if (tok != ')') for (;;) {
1417 varg = tok;
1418 next_nomacro();
1419 is_vaargs = 0;
1420 if (varg == TOK_DOTS) {
1421 varg = TOK___VA_ARGS__;
1422 is_vaargs = 1;
1423 } else if (tok == TOK_DOTS && gnu_ext) {
1424 is_vaargs = 1;
1425 next_nomacro();
1427 if (varg < TOK_IDENT)
1428 bad_list:
1429 tcc_error("bad macro parameter list");
1430 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1431 *ps = s;
1432 ps = &s->next;
1433 if (tok == ')')
1434 break;
1435 if (tok != ',' || is_vaargs)
1436 goto bad_list;
1437 next_nomacro();
1439 next_nomacro_spc();
1440 t = MACRO_FUNC;
1441 parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
1442 isidnum_table['.' - CH_EOF] =
1443 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
1446 tokstr_buf.len = 0;
1447 spc = 2;
1448 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1449 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1450 /* remove spaces around ## and after '#' */
1451 if (TOK_TWOSHARPS == tok) {
1452 if (2 == spc)
1453 goto bad_twosharp;
1454 if (1 == spc)
1455 --tokstr_buf.len;
1456 spc = 3;
1457 } else if ('#' == tok) {
1458 spc = 4;
1459 } else if (check_space(tok, &spc)) {
1460 goto skip;
1462 tok_str_add2(&tokstr_buf, tok, &tokc);
1463 skip:
1464 next_nomacro_spc();
1467 parse_flags = saved_parse_flags;
1468 if (spc == 1)
1469 --tokstr_buf.len; /* remove trailing space */
1470 tok_str_add(&tokstr_buf, 0);
1471 if (3 == spc)
1472 bad_twosharp:
1473 tcc_error("'##' cannot appear at either end of macro");
1474 define_push(v, t, &tokstr_buf, first);
1477 static inline int hash_cached_include(const char *filename)
1479 const unsigned char *s;
1480 unsigned int h;
1482 h = TOK_HASH_INIT;
1483 s = (unsigned char *) filename;
1484 while (*s) {
1485 h = TOK_HASH_FUNC(h, *s);
1486 s++;
1488 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1489 return h;
1492 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1494 CachedInclude *e;
1495 int i, h;
1496 h = hash_cached_include(filename);
1497 i = s1->cached_includes_hash[h];
1498 for(;;) {
1499 if (i == 0)
1500 break;
1501 e = s1->cached_includes[i - 1];
1502 if (0 == PATHCMP(e->filename, filename))
1503 return e;
1504 i = e->hash_next;
1506 return NULL;
1509 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1511 CachedInclude *e;
1512 int h;
1514 if (search_cached_include(s1, filename))
1515 return;
1516 #ifdef INC_DEBUG
1517 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1518 #endif
1519 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1520 strcpy(e->filename, filename);
1521 e->ifndef_macro = ifndef_macro;
1522 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1523 /* add in hash table */
1524 h = hash_cached_include(filename);
1525 e->hash_next = s1->cached_includes_hash[h];
1526 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1529 #define ONCE_PREFIX "#ONCE#"
1531 static void pragma_parse(TCCState *s1)
1533 next_nomacro();
1534 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1535 int t = tok, v;
1536 Sym *s;
1538 if (next(), tok != '(')
1539 goto pragma_err;
1540 if (next(), tok != TOK_STR)
1541 goto pragma_err;
1542 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1543 if (next(), tok != ')')
1544 goto pragma_err;
1545 if (t == TOK_push_macro) {
1546 while (NULL == (s = define_find(v)))
1547 define_push(v, 0, NULL, NULL);
1548 s->type.ref = s; /* set push boundary */
1549 } else {
1550 for (s = define_stack; s; s = s->prev)
1551 if (s->v == v && s->type.ref == s) {
1552 s->type.ref = NULL;
1553 break;
1556 if (s)
1557 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1558 else
1559 tcc_warning("unbalanced #pragma pop_macro");
1560 pp_debug_tok = t, pp_debug_symv = v;
1562 } else if (tok == TOK_once) {
1563 char buf1[sizeof(file->filename) + sizeof(ONCE_PREFIX)];
1564 strcpy(buf1, ONCE_PREFIX);
1565 strcat(buf1, file->filename);
1566 #ifdef PATH_NOCASE
1567 strupr(buf1);
1568 #endif
1569 add_cached_include(s1, file->filename, tok_alloc(buf1, strlen(buf1))->tok);
1570 } else if (s1->ppfp) {
1571 /* tcc -E: keep pragmas below unchanged */
1572 unget_tok(' ');
1573 unget_tok(TOK_PRAGMA);
1574 unget_tok('#');
1575 unget_tok(TOK_LINEFEED);
1577 } else if (tok == TOK_pack) {
1578 /* This may be:
1579 #pragma pack(1) // set
1580 #pragma pack() // reset to default
1581 #pragma pack(push,1) // push & set
1582 #pragma pack(pop) // restore previous */
1583 next();
1584 skip('(');
1585 if (tok == TOK_ASM_pop) {
1586 next();
1587 if (s1->pack_stack_ptr <= s1->pack_stack) {
1588 stk_error:
1589 tcc_error("out of pack stack");
1591 s1->pack_stack_ptr--;
1592 } else {
1593 int val = 0;
1594 if (tok != ')') {
1595 if (tok == TOK_ASM_push) {
1596 next();
1597 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1598 goto stk_error;
1599 s1->pack_stack_ptr++;
1600 skip(',');
1602 if (tok != TOK_CINT)
1603 goto pragma_err;
1604 val = tokc.i;
1605 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1606 goto pragma_err;
1607 next();
1609 *s1->pack_stack_ptr = val;
1611 if (tok != ')')
1612 goto pragma_err;
1614 } else if (tok == TOK_comment) {
1615 char *file;
1616 next();
1617 skip('(');
1618 if (tok != TOK_lib)
1619 goto pragma_warn;
1620 next();
1621 skip(',');
1622 if (tok != TOK_STR)
1623 goto pragma_err;
1624 file = tcc_strdup((char *)tokc.str.data);
1625 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1626 next();
1627 if (tok != ')')
1628 goto pragma_err;
1629 } else {
1630 pragma_warn:
1631 if (s1->warn_unsupported)
1632 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1634 return;
1636 pragma_err:
1637 tcc_error("malformed #pragma directive");
1638 return;
1641 /* is_bof is true if first non space token at beginning of file */
1642 ST_FUNC void preprocess(int is_bof)
1644 TCCState *s1 = tcc_state;
1645 int i, c, n, saved_parse_flags;
1646 char buf[1024], *q;
1647 Sym *s;
1649 saved_parse_flags = parse_flags;
1650 parse_flags = PARSE_FLAG_PREPROCESS
1651 | PARSE_FLAG_TOK_NUM
1652 | PARSE_FLAG_TOK_STR
1653 | PARSE_FLAG_LINEFEED
1654 | (parse_flags & PARSE_FLAG_ASM_FILE)
1657 next_nomacro();
1658 redo:
1659 switch(tok) {
1660 case TOK_DEFINE:
1661 pp_debug_tok = tok;
1662 next_nomacro();
1663 pp_debug_symv = tok;
1664 parse_define();
1665 break;
1666 case TOK_UNDEF:
1667 pp_debug_tok = tok;
1668 next_nomacro();
1669 pp_debug_symv = tok;
1670 s = define_find(tok);
1671 /* undefine symbol by putting an invalid name */
1672 if (s)
1673 define_undef(s);
1674 break;
1675 case TOK_INCLUDE:
1676 case TOK_INCLUDE_NEXT:
1677 ch = file->buf_ptr[0];
1678 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1679 skip_spaces();
1680 if (ch == '<') {
1681 c = '>';
1682 goto read_name;
1683 } else if (ch == '\"') {
1684 c = ch;
1685 read_name:
1686 inp();
1687 q = buf;
1688 while (ch != c && ch != '\n' && ch != CH_EOF) {
1689 if ((q - buf) < sizeof(buf) - 1)
1690 *q++ = ch;
1691 if (ch == '\\') {
1692 if (handle_stray_noerror() == 0)
1693 --q;
1694 } else
1695 inp();
1697 *q = '\0';
1698 minp();
1699 #if 0
1700 /* eat all spaces and comments after include */
1701 /* XXX: slightly incorrect */
1702 while (ch1 != '\n' && ch1 != CH_EOF)
1703 inp();
1704 #endif
1705 } else {
1706 /* computed #include : either we have only strings or
1707 we have anything enclosed in '<>' */
1708 next();
1709 buf[0] = '\0';
1710 if (tok == TOK_STR) {
1711 while (tok != TOK_LINEFEED) {
1712 if (tok != TOK_STR) {
1713 include_syntax:
1714 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1716 pstrcat(buf, sizeof(buf), (char *)tokc.str.data);
1717 next();
1719 c = '\"';
1720 } else {
1721 int len;
1722 while (tok != TOK_LINEFEED) {
1723 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1724 next();
1726 len = strlen(buf);
1727 /* check syntax and remove '<>' */
1728 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1729 goto include_syntax;
1730 memmove(buf, buf + 1, len - 2);
1731 buf[len - 2] = '\0';
1732 c = '>';
1736 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1737 tcc_error("#include recursion too deep");
1738 /* store current file in stack, but increment stack later below */
1739 *s1->include_stack_ptr = file;
1740 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1741 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1742 for (; i < n; ++i) {
1743 char buf1[sizeof file->filename];
1744 CachedInclude *e;
1745 const char *path;
1747 if (i == 0) {
1748 /* check absolute include path */
1749 if (!IS_ABSPATH(buf))
1750 continue;
1751 buf1[0] = 0;
1753 } else if (i == 1) {
1754 /* search in current dir if "header.h" */
1755 if (c != '\"')
1756 continue;
1757 path = file->filename;
1758 pstrncpy(buf1, path, tcc_basename(path) - path);
1760 } else {
1761 /* search in all the include paths */
1762 int j = i - 2, k = j - s1->nb_include_paths;
1763 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1764 if (path == 0) continue;
1765 pstrcpy(buf1, sizeof(buf1), path);
1766 pstrcat(buf1, sizeof(buf1), "/");
1769 pstrcat(buf1, sizeof(buf1), buf);
1770 e = search_cached_include(s1, buf1);
1771 if (e && define_find(e->ifndef_macro)) {
1772 /* no need to parse the include because the 'ifndef macro'
1773 is defined */
1774 #ifdef INC_DEBUG
1775 printf("%s: skipping cached %s\n", file->filename, buf1);
1776 #endif
1777 goto include_done;
1780 if (tcc_open(s1, buf1) < 0)
1781 continue;
1783 file->include_next_index = i + 1;
1784 #ifdef INC_DEBUG
1785 printf("%s: including %s\n", file->prev->filename, file->filename);
1786 #endif
1787 /* update target deps */
1788 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1789 tcc_strdup(buf1));
1790 /* push current file in stack */
1791 ++s1->include_stack_ptr;
1792 /* add include file debug info */
1793 if (s1->do_debug)
1794 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1795 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1796 ch = file->buf_ptr[0];
1797 goto the_end;
1799 tcc_error("include file '%s' not found", buf);
1800 include_done:
1801 break;
1802 case TOK_IFNDEF:
1803 c = 1;
1804 goto do_ifdef;
1805 case TOK_IF:
1806 c = expr_preprocess();
1807 goto do_if;
1808 case TOK_IFDEF:
1809 c = 0;
1810 do_ifdef:
1811 next_nomacro();
1812 if (tok < TOK_IDENT)
1813 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1814 if (is_bof) {
1815 if (c) {
1816 #ifdef INC_DEBUG
1817 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1818 #endif
1819 file->ifndef_macro = tok;
1822 c = (define_find(tok) != 0) ^ c;
1823 do_if:
1824 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1825 tcc_error("memory full (ifdef)");
1826 *s1->ifdef_stack_ptr++ = c;
1827 goto test_skip;
1828 case TOK_ELSE:
1829 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1830 tcc_error("#else without matching #if");
1831 if (s1->ifdef_stack_ptr[-1] & 2)
1832 tcc_error("#else after #else");
1833 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1834 goto test_else;
1835 case TOK_ELIF:
1836 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1837 tcc_error("#elif without matching #if");
1838 c = s1->ifdef_stack_ptr[-1];
1839 if (c > 1)
1840 tcc_error("#elif after #else");
1841 /* last #if/#elif expression was true: we skip */
1842 if (c == 1)
1843 goto skip;
1844 c = expr_preprocess();
1845 s1->ifdef_stack_ptr[-1] = c;
1846 test_else:
1847 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1848 file->ifndef_macro = 0;
1849 test_skip:
1850 if (!(c & 1)) {
1851 skip:
1852 preprocess_skip();
1853 is_bof = 0;
1854 goto redo;
1856 break;
1857 case TOK_ENDIF:
1858 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1859 tcc_error("#endif without matching #if");
1860 s1->ifdef_stack_ptr--;
1861 /* '#ifndef macro' was at the start of file. Now we check if
1862 an '#endif' is exactly at the end of file */
1863 if (file->ifndef_macro &&
1864 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1865 file->ifndef_macro_saved = file->ifndef_macro;
1866 /* need to set to zero to avoid false matches if another
1867 #ifndef at middle of file */
1868 file->ifndef_macro = 0;
1869 while (tok != TOK_LINEFEED)
1870 next_nomacro();
1871 tok_flags |= TOK_FLAG_ENDIF;
1872 goto the_end;
1874 break;
1875 case TOK_PPNUM:
1876 n = strtoul((char*)tokc.str.data, &q, 10);
1877 goto _line_num;
1878 case TOK_LINE:
1879 next();
1880 if (tok != TOK_CINT)
1881 _line_err:
1882 tcc_error("wrong #line format");
1883 n = tokc.i;
1884 _line_num:
1885 next();
1886 if (tok != TOK_LINEFEED) {
1887 if (tok == TOK_STR)
1888 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1889 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1890 break;
1891 else
1892 goto _line_err;
1893 --n;
1895 if (file->fd > 0)
1896 total_lines += file->line_num - n;
1897 file->line_num = n;
1898 if (s1->do_debug)
1899 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1900 break;
1901 case TOK_ERROR:
1902 case TOK_WARNING:
1903 c = tok;
1904 ch = file->buf_ptr[0];
1905 skip_spaces();
1906 q = buf;
1907 while (ch != '\n' && ch != CH_EOF) {
1908 if ((q - buf) < sizeof(buf) - 1)
1909 *q++ = ch;
1910 if (ch == '\\') {
1911 if (handle_stray_noerror() == 0)
1912 --q;
1913 } else
1914 inp();
1916 *q = '\0';
1917 if (c == TOK_ERROR)
1918 tcc_error("#error %s", buf);
1919 else
1920 tcc_warning("#warning %s", buf);
1921 break;
1922 case TOK_PRAGMA:
1923 pragma_parse(s1);
1924 break;
1925 case TOK_LINEFEED:
1926 goto the_end;
1927 default:
1928 /* ignore gas line comment in an 'S' file. */
1929 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1930 goto ignore;
1931 if (tok == '!' && is_bof)
1932 /* '!' is ignored at beginning to allow C scripts. */
1933 goto ignore;
1934 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1935 ignore:
1936 file->buf_ptr = parse_line_comment(file->buf_ptr);
1937 goto the_end;
1939 /* ignore other preprocess commands or #! for C scripts */
1940 while (tok != TOK_LINEFEED)
1941 next_nomacro();
1942 the_end:
1943 parse_flags = saved_parse_flags;
1946 /* evaluate escape codes in a string. */
1947 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1949 int c, n;
1950 const uint8_t *p;
1952 p = buf;
1953 for(;;) {
1954 c = *p;
1955 if (c == '\0')
1956 break;
1957 if (c == '\\') {
1958 p++;
1959 /* escape */
1960 c = *p;
1961 switch(c) {
1962 case '0': case '1': case '2': case '3':
1963 case '4': case '5': case '6': case '7':
1964 /* at most three octal digits */
1965 n = c - '0';
1966 p++;
1967 c = *p;
1968 if (isoct(c)) {
1969 n = n * 8 + c - '0';
1970 p++;
1971 c = *p;
1972 if (isoct(c)) {
1973 n = n * 8 + c - '0';
1974 p++;
1977 c = n;
1978 goto add_char_nonext;
1979 case 'x':
1980 case 'u':
1981 case 'U':
1982 p++;
1983 n = 0;
1984 for(;;) {
1985 c = *p;
1986 if (c >= 'a' && c <= 'f')
1987 c = c - 'a' + 10;
1988 else if (c >= 'A' && c <= 'F')
1989 c = c - 'A' + 10;
1990 else if (isnum(c))
1991 c = c - '0';
1992 else
1993 break;
1994 n = n * 16 + c;
1995 p++;
1997 c = n;
1998 goto add_char_nonext;
1999 case 'a':
2000 c = '\a';
2001 break;
2002 case 'b':
2003 c = '\b';
2004 break;
2005 case 'f':
2006 c = '\f';
2007 break;
2008 case 'n':
2009 c = '\n';
2010 break;
2011 case 'r':
2012 c = '\r';
2013 break;
2014 case 't':
2015 c = '\t';
2016 break;
2017 case 'v':
2018 c = '\v';
2019 break;
2020 case 'e':
2021 if (!gnu_ext)
2022 goto invalid_escape;
2023 c = 27;
2024 break;
2025 case '\'':
2026 case '\"':
2027 case '\\':
2028 case '?':
2029 break;
2030 default:
2031 invalid_escape:
2032 if (c >= '!' && c <= '~')
2033 tcc_warning("unknown escape sequence: \'\\%c\'", c);
2034 else
2035 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
2036 break;
2039 p++;
2040 add_char_nonext:
2041 if (!is_long)
2042 cstr_ccat(outstr, c);
2043 else
2044 cstr_wccat(outstr, c);
2046 /* add a trailing '\0' */
2047 if (!is_long)
2048 cstr_ccat(outstr, '\0');
2049 else
2050 cstr_wccat(outstr, '\0');
2053 static void parse_string(const char *s, int len)
2055 uint8_t buf[1000], *p = buf;
2056 int is_long, sep;
2058 if ((is_long = *s == 'L'))
2059 ++s, --len;
2060 sep = *s++;
2061 len -= 2;
2062 if (len >= sizeof buf)
2063 p = tcc_malloc(len + 1);
2064 memcpy(p, s, len);
2065 p[len] = 0;
2067 cstr_reset(&tokcstr);
2068 parse_escape_string(&tokcstr, p, is_long);
2069 if (p != buf)
2070 tcc_free(p);
2072 if (sep == '\'') {
2073 int char_size;
2074 /* XXX: make it portable */
2075 if (!is_long)
2076 char_size = 1;
2077 else
2078 char_size = sizeof(nwchar_t);
2079 if (tokcstr.size <= char_size)
2080 tcc_error("empty character constant");
2081 if (tokcstr.size > 2 * char_size)
2082 tcc_warning("multi-character character constant");
2083 if (!is_long) {
2084 tokc.i = *(int8_t *)tokcstr.data;
2085 tok = TOK_CCHAR;
2086 } else {
2087 tokc.i = *(nwchar_t *)tokcstr.data;
2088 tok = TOK_LCHAR;
2090 } else {
2091 tokc.str.size = tokcstr.size;
2092 tokc.str.data = tokcstr.data;
2093 tokc.str.data_allocated = tokcstr.data_allocated;
2094 if (!is_long)
2095 tok = TOK_STR;
2096 else
2097 tok = TOK_LSTR;
2101 /* we use 64 bit numbers */
2102 #define BN_SIZE 2
2104 /* bn = (bn << shift) | or_val */
2105 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2107 int i;
2108 unsigned int v;
2109 for(i=0;i<BN_SIZE;i++) {
2110 v = bn[i];
2111 bn[i] = (v << shift) | or_val;
2112 or_val = v >> (32 - shift);
2116 static void bn_zero(unsigned int *bn)
2118 int i;
2119 for(i=0;i<BN_SIZE;i++) {
2120 bn[i] = 0;
2124 /* parse number in null terminated string 'p' and return it in the
2125 current token */
2126 static void parse_number(const char *p)
2128 int b, t, shift, frac_bits, s, exp_val, ch;
2129 char *q;
2130 unsigned int bn[BN_SIZE];
2131 double d;
2133 /* number */
2134 q = token_buf;
2135 ch = *p++;
2136 t = ch;
2137 ch = *p++;
2138 *q++ = t;
2139 b = 10;
2140 if (t == '.') {
2141 goto float_frac_parse;
2142 } else if (t == '0') {
2143 if (ch == 'x' || ch == 'X') {
2144 q--;
2145 ch = *p++;
2146 b = 16;
2147 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2148 q--;
2149 ch = *p++;
2150 b = 2;
2153 /* parse all digits. cannot check octal numbers at this stage
2154 because of floating point constants */
2155 while (1) {
2156 if (ch >= 'a' && ch <= 'f')
2157 t = ch - 'a' + 10;
2158 else if (ch >= 'A' && ch <= 'F')
2159 t = ch - 'A' + 10;
2160 else if (isnum(ch))
2161 t = ch - '0';
2162 else
2163 break;
2164 if (t >= b)
2165 break;
2166 if (q >= token_buf + STRING_MAX_SIZE) {
2167 num_too_long:
2168 tcc_error("number too long");
2170 *q++ = ch;
2171 ch = *p++;
2173 if (ch == '.' ||
2174 ((ch == 'e' || ch == 'E') && b == 10) ||
2175 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2176 if (b != 10) {
2177 /* NOTE: strtox should support that for hexa numbers, but
2178 non ISOC99 libcs do not support it, so we prefer to do
2179 it by hand */
2180 /* hexadecimal or binary floats */
2181 /* XXX: handle overflows */
2182 *q = '\0';
2183 if (b == 16)
2184 shift = 4;
2185 else
2186 shift = 1;
2187 bn_zero(bn);
2188 q = token_buf;
2189 while (1) {
2190 t = *q++;
2191 if (t == '\0') {
2192 break;
2193 } else if (t >= 'a') {
2194 t = t - 'a' + 10;
2195 } else if (t >= 'A') {
2196 t = t - 'A' + 10;
2197 } else {
2198 t = t - '0';
2200 bn_lshift(bn, shift, t);
2202 frac_bits = 0;
2203 if (ch == '.') {
2204 ch = *p++;
2205 while (1) {
2206 t = ch;
2207 if (t >= 'a' && t <= 'f') {
2208 t = t - 'a' + 10;
2209 } else if (t >= 'A' && t <= 'F') {
2210 t = t - 'A' + 10;
2211 } else if (t >= '0' && t <= '9') {
2212 t = t - '0';
2213 } else {
2214 break;
2216 if (t >= b)
2217 tcc_error("invalid digit");
2218 bn_lshift(bn, shift, t);
2219 frac_bits += shift;
2220 ch = *p++;
2223 if (ch != 'p' && ch != 'P')
2224 expect("exponent");
2225 ch = *p++;
2226 s = 1;
2227 exp_val = 0;
2228 if (ch == '+') {
2229 ch = *p++;
2230 } else if (ch == '-') {
2231 s = -1;
2232 ch = *p++;
2234 if (ch < '0' || ch > '9')
2235 expect("exponent digits");
2236 while (ch >= '0' && ch <= '9') {
2237 exp_val = exp_val * 10 + ch - '0';
2238 ch = *p++;
2240 exp_val = exp_val * s;
2242 /* now we can generate the number */
2243 /* XXX: should patch directly float number */
2244 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2245 d = ldexp(d, exp_val - frac_bits);
2246 t = toup(ch);
2247 if (t == 'F') {
2248 ch = *p++;
2249 tok = TOK_CFLOAT;
2250 /* float : should handle overflow */
2251 tokc.f = (float)d;
2252 } else if (t == 'L') {
2253 ch = *p++;
2254 #ifdef TCC_TARGET_PE
2255 tok = TOK_CDOUBLE;
2256 tokc.d = d;
2257 #else
2258 tok = TOK_CLDOUBLE;
2259 /* XXX: not large enough */
2260 tokc.ld = (long double)d;
2261 #endif
2262 } else {
2263 tok = TOK_CDOUBLE;
2264 tokc.d = d;
2266 } else {
2267 /* decimal floats */
2268 if (ch == '.') {
2269 if (q >= token_buf + STRING_MAX_SIZE)
2270 goto num_too_long;
2271 *q++ = ch;
2272 ch = *p++;
2273 float_frac_parse:
2274 while (ch >= '0' && ch <= '9') {
2275 if (q >= token_buf + STRING_MAX_SIZE)
2276 goto num_too_long;
2277 *q++ = ch;
2278 ch = *p++;
2281 if (ch == 'e' || ch == 'E') {
2282 if (q >= token_buf + STRING_MAX_SIZE)
2283 goto num_too_long;
2284 *q++ = ch;
2285 ch = *p++;
2286 if (ch == '-' || ch == '+') {
2287 if (q >= token_buf + STRING_MAX_SIZE)
2288 goto num_too_long;
2289 *q++ = ch;
2290 ch = *p++;
2292 if (ch < '0' || ch > '9')
2293 expect("exponent digits");
2294 while (ch >= '0' && ch <= '9') {
2295 if (q >= token_buf + STRING_MAX_SIZE)
2296 goto num_too_long;
2297 *q++ = ch;
2298 ch = *p++;
2301 *q = '\0';
2302 t = toup(ch);
2303 errno = 0;
2304 if (t == 'F') {
2305 ch = *p++;
2306 tok = TOK_CFLOAT;
2307 tokc.f = strtof(token_buf, NULL);
2308 } else if (t == 'L') {
2309 ch = *p++;
2310 #ifdef TCC_TARGET_PE
2311 tok = TOK_CDOUBLE;
2312 tokc.d = strtod(token_buf, NULL);
2313 #else
2314 tok = TOK_CLDOUBLE;
2315 tokc.ld = strtold(token_buf, NULL);
2316 #endif
2317 } else {
2318 tok = TOK_CDOUBLE;
2319 tokc.d = strtod(token_buf, NULL);
2322 } else {
2323 unsigned long long n, n1;
2324 int lcount, ucount, must_64bit;
2325 const char *p1;
2327 /* integer number */
2328 *q = '\0';
2329 q = token_buf;
2330 if (b == 10 && *q == '0') {
2331 b = 8;
2332 q++;
2334 n = 0;
2335 while(1) {
2336 t = *q++;
2337 /* no need for checks except for base 10 / 8 errors */
2338 if (t == '\0')
2339 break;
2340 else if (t >= 'a')
2341 t = t - 'a' + 10;
2342 else if (t >= 'A')
2343 t = t - 'A' + 10;
2344 else
2345 t = t - '0';
2346 if (t >= b)
2347 tcc_error("invalid digit");
2348 n1 = n;
2349 n = n * b + t;
2350 /* detect overflow */
2351 /* XXX: this test is not reliable */
2352 if (n < n1)
2353 tcc_error("integer constant overflow");
2356 /* Determine the characteristics (unsigned and/or 64bit) the type of
2357 the constant must have according to the constant suffix(es) */
2358 lcount = ucount = must_64bit = 0;
2359 p1 = p;
2360 for(;;) {
2361 t = toup(ch);
2362 if (t == 'L') {
2363 if (lcount >= 2)
2364 tcc_error("three 'l's in integer constant");
2365 if (lcount && *(p - 1) != ch)
2366 tcc_error("incorrect integer suffix: %s", p1);
2367 lcount++;
2368 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2369 if (lcount == 2)
2370 #endif
2371 must_64bit = 1;
2372 ch = *p++;
2373 } else if (t == 'U') {
2374 if (ucount >= 1)
2375 tcc_error("two 'u's in integer constant");
2376 ucount++;
2377 ch = *p++;
2378 } else {
2379 break;
2383 /* Whether 64 bits are needed to hold the constant's value */
2384 if (n & 0xffffffff00000000LL || must_64bit) {
2385 tok = TOK_CLLONG;
2386 n1 = n >> 32;
2387 } else {
2388 tok = TOK_CINT;
2389 n1 = n;
2392 /* Whether type must be unsigned to hold the constant's value */
2393 if (ucount || ((n1 >> 31) && (b != 10))) {
2394 if (tok == TOK_CLLONG)
2395 tok = TOK_CULLONG;
2396 else
2397 tok = TOK_CUINT;
2398 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2399 } else if (n1 >> 31) {
2400 if (tok == TOK_CINT)
2401 tok = TOK_CLLONG;
2402 else
2403 tcc_error("integer constant overflow");
2406 tokc.i = n;
2408 if (ch)
2409 tcc_error("invalid number\n");
2413 #define PARSE2(c1, tok1, c2, tok2) \
2414 case c1: \
2415 PEEKC(c, p); \
2416 if (c == c2) { \
2417 p++; \
2418 tok = tok2; \
2419 } else { \
2420 tok = tok1; \
2422 break;
2424 /* return next token without macro substitution */
2425 static inline void next_nomacro1(void)
2427 int t, c, is_long, len;
2428 TokenSym *ts;
2429 uint8_t *p, *p1;
2430 unsigned int h;
2432 p = file->buf_ptr;
2433 redo_no_start:
2434 c = *p;
2435 #if (__TINYC__ || __GNUC__)
2436 #else
2437 if (c & 0x80)
2438 goto parse_ident_fast;
2439 #endif
2440 switch(c) {
2441 case ' ':
2442 case '\t':
2443 tok = c;
2444 p++;
2445 if (parse_flags & PARSE_FLAG_SPACES)
2446 goto keep_tok_flags;
2447 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2448 ++p;
2449 goto redo_no_start;
2450 case '\f':
2451 case '\v':
2452 case '\r':
2453 p++;
2454 goto redo_no_start;
2455 case '\\':
2456 /* first look if it is in fact an end of buffer */
2457 c = handle_stray1(p);
2458 p = file->buf_ptr;
2459 if (c == '\\')
2460 goto parse_simple;
2461 if (c != CH_EOF)
2462 goto redo_no_start;
2464 TCCState *s1 = tcc_state;
2465 if ((parse_flags & PARSE_FLAG_LINEFEED)
2466 && !(tok_flags & TOK_FLAG_EOF)) {
2467 tok_flags |= TOK_FLAG_EOF;
2468 tok = TOK_LINEFEED;
2469 goto keep_tok_flags;
2470 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2471 tok = TOK_EOF;
2472 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2473 tcc_error("missing #endif");
2474 } else if (s1->include_stack_ptr == s1->include_stack) {
2475 /* no include left : end of file. */
2476 tok = TOK_EOF;
2477 } else {
2478 tok_flags &= ~TOK_FLAG_EOF;
2479 /* pop include file */
2481 /* test if previous '#endif' was after a #ifdef at
2482 start of file */
2483 if (tok_flags & TOK_FLAG_ENDIF) {
2484 #ifdef INC_DEBUG
2485 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2486 #endif
2487 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2488 tok_flags &= ~TOK_FLAG_ENDIF;
2491 /* add end of include file debug info */
2492 if (tcc_state->do_debug) {
2493 put_stabd(N_EINCL, 0, 0);
2495 /* pop include stack */
2496 tcc_close();
2497 s1->include_stack_ptr--;
2498 p = file->buf_ptr;
2499 goto redo_no_start;
2502 break;
2504 case '\n':
2505 file->line_num++;
2506 tok_flags |= TOK_FLAG_BOL;
2507 p++;
2508 maybe_newline:
2509 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2510 goto redo_no_start;
2511 tok = TOK_LINEFEED;
2512 goto keep_tok_flags;
2514 case '#':
2515 /* XXX: simplify */
2516 PEEKC(c, p);
2517 if ((tok_flags & TOK_FLAG_BOL) &&
2518 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2519 file->buf_ptr = p;
2520 preprocess(tok_flags & TOK_FLAG_BOF);
2521 p = file->buf_ptr;
2522 goto maybe_newline;
2523 } else {
2524 if (c == '#') {
2525 p++;
2526 tok = TOK_TWOSHARPS;
2527 } else {
2528 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2529 p = parse_line_comment(p - 1);
2530 goto redo_no_start;
2531 } else {
2532 tok = '#';
2536 break;
2538 /* dollar is allowed to start identifiers when not parsing asm */
2539 case '$':
2540 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2541 || (parse_flags & PARSE_FLAG_ASM_FILE))
2542 goto parse_simple;
2544 #if (__TINYC__ || __GNUC__)
2545 case 'a' ... 'z':
2546 case 'A' ... 'K':
2547 case 'M' ... 'Z':
2548 case '_':
2549 case 0x80 ... 0xFF:
2550 #else
2551 case 'a': case 'b': case 'c': case 'd':
2552 case 'e': case 'f': case 'g': case 'h':
2553 case 'i': case 'j': case 'k': case 'l':
2554 case 'm': case 'n': case 'o': case 'p':
2555 case 'q': case 'r': case 's': case 't':
2556 case 'u': case 'v': case 'w': case 'x':
2557 case 'y': case 'z':
2558 case 'A': case 'B': case 'C': case 'D':
2559 case 'E': case 'F': case 'G': case 'H':
2560 case 'I': case 'J': case 'K':
2561 case 'M': case 'N': case 'O': case 'P':
2562 case 'Q': case 'R': case 'S': case 'T':
2563 case 'U': case 'V': case 'W': case 'X':
2564 case 'Y': case 'Z':
2565 case '_':
2566 #endif
2567 parse_ident_fast:
2568 p1 = p;
2569 h = TOK_HASH_INIT;
2570 h = TOK_HASH_FUNC(h, c);
2571 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2572 h = TOK_HASH_FUNC(h, c);
2573 len = p - p1;
2574 if (c != '\\') {
2575 TokenSym **pts;
2577 /* fast case : no stray found, so we have the full token
2578 and we have already hashed it */
2579 h &= (TOK_HASH_SIZE - 1);
2580 pts = &hash_ident[h];
2581 for(;;) {
2582 ts = *pts;
2583 if (!ts)
2584 break;
2585 if (ts->len == len && !memcmp(ts->str, p1, len))
2586 goto token_found;
2587 pts = &(ts->hash_next);
2589 ts = tok_alloc_new(pts, (char *) p1, len);
2590 token_found: ;
2591 } else {
2592 /* slower case */
2593 cstr_reset(&tokcstr);
2594 cstr_cat(&tokcstr, p1, len);
2595 p--;
2596 PEEKC(c, p);
2597 parse_ident_slow:
2598 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2600 cstr_ccat(&tokcstr, c);
2601 PEEKC(c, p);
2603 ts = tok_alloc(tokcstr.data, tokcstr.size);
2605 tok = ts->tok;
2606 break;
2607 case 'L':
2608 t = p[1];
2609 if (t != '\\' && t != '\'' && t != '\"') {
2610 /* fast case */
2611 goto parse_ident_fast;
2612 } else {
2613 PEEKC(c, p);
2614 if (c == '\'' || c == '\"') {
2615 is_long = 1;
2616 goto str_const;
2617 } else {
2618 cstr_reset(&tokcstr);
2619 cstr_ccat(&tokcstr, 'L');
2620 goto parse_ident_slow;
2623 break;
2625 case '0': case '1': case '2': case '3':
2626 case '4': case '5': case '6': case '7':
2627 case '8': case '9':
2628 cstr_reset(&tokcstr);
2629 /* after the first digit, accept digits, alpha, '.' or sign if
2630 prefixed by 'eEpP' */
2631 parse_num:
2632 for(;;) {
2633 t = c;
2634 cstr_ccat(&tokcstr, c);
2635 PEEKC(c, p);
2636 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2637 || c == '.'
2638 || ((c == '+' || c == '-')
2639 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2640 && !(parse_flags & PARSE_FLAG_ASM_FILE)
2642 break;
2644 /* We add a trailing '\0' to ease parsing */
2645 cstr_ccat(&tokcstr, '\0');
2646 tokc.str.size = tokcstr.size;
2647 tokc.str.data = tokcstr.data;
2648 tokc.str.data_allocated = tokcstr.data_allocated;
2649 tok = TOK_PPNUM;
2650 break;
2652 case '.':
2653 /* special dot handling because it can also start a number */
2654 PEEKC(c, p);
2655 if (isnum(c)) {
2656 cstr_reset(&tokcstr);
2657 cstr_ccat(&tokcstr, '.');
2658 goto parse_num;
2659 } else if ((parse_flags & PARSE_FLAG_ASM_FILE)
2660 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
2661 *--p = c = '.';
2662 goto parse_ident_fast;
2663 } else if (c == '.') {
2664 PEEKC(c, p);
2665 if (c == '.') {
2666 p++;
2667 tok = TOK_DOTS;
2668 } else {
2669 *--p = '.'; /* may underflow into file->unget[] */
2670 tok = '.';
2672 } else {
2673 tok = '.';
2675 break;
2676 case '\'':
2677 case '\"':
2678 is_long = 0;
2679 str_const:
2680 cstr_reset(&tokcstr);
2681 if (is_long)
2682 cstr_ccat(&tokcstr, 'L');
2683 cstr_ccat(&tokcstr, c);
2684 p = parse_pp_string(p, c, &tokcstr);
2685 cstr_ccat(&tokcstr, c);
2686 cstr_ccat(&tokcstr, '\0');
2687 tokc.str.size = tokcstr.size;
2688 tokc.str.data = tokcstr.data;
2689 tokc.str.data_allocated = tokcstr.data_allocated;
2690 tok = TOK_PPSTR;
2691 break;
2693 case '<':
2694 PEEKC(c, p);
2695 if (c == '=') {
2696 p++;
2697 tok = TOK_LE;
2698 } else if (c == '<') {
2699 PEEKC(c, p);
2700 if (c == '=') {
2701 p++;
2702 tok = TOK_A_SHL;
2703 } else {
2704 tok = TOK_SHL;
2706 } else {
2707 tok = TOK_LT;
2709 break;
2710 case '>':
2711 PEEKC(c, p);
2712 if (c == '=') {
2713 p++;
2714 tok = TOK_GE;
2715 } else if (c == '>') {
2716 PEEKC(c, p);
2717 if (c == '=') {
2718 p++;
2719 tok = TOK_A_SAR;
2720 } else {
2721 tok = TOK_SAR;
2723 } else {
2724 tok = TOK_GT;
2726 break;
2728 case '&':
2729 PEEKC(c, p);
2730 if (c == '&') {
2731 p++;
2732 tok = TOK_LAND;
2733 } else if (c == '=') {
2734 p++;
2735 tok = TOK_A_AND;
2736 } else {
2737 tok = '&';
2739 break;
2741 case '|':
2742 PEEKC(c, p);
2743 if (c == '|') {
2744 p++;
2745 tok = TOK_LOR;
2746 } else if (c == '=') {
2747 p++;
2748 tok = TOK_A_OR;
2749 } else {
2750 tok = '|';
2752 break;
2754 case '+':
2755 PEEKC(c, p);
2756 if (c == '+') {
2757 p++;
2758 tok = TOK_INC;
2759 } else if (c == '=') {
2760 p++;
2761 tok = TOK_A_ADD;
2762 } else {
2763 tok = '+';
2765 break;
2767 case '-':
2768 PEEKC(c, p);
2769 if (c == '-') {
2770 p++;
2771 tok = TOK_DEC;
2772 } else if (c == '=') {
2773 p++;
2774 tok = TOK_A_SUB;
2775 } else if (c == '>') {
2776 p++;
2777 tok = TOK_ARROW;
2778 } else {
2779 tok = '-';
2781 break;
2783 PARSE2('!', '!', '=', TOK_NE)
2784 PARSE2('=', '=', '=', TOK_EQ)
2785 PARSE2('*', '*', '=', TOK_A_MUL)
2786 PARSE2('%', '%', '=', TOK_A_MOD)
2787 PARSE2('^', '^', '=', TOK_A_XOR)
2789 /* comments or operator */
2790 case '/':
2791 PEEKC(c, p);
2792 if (c == '*') {
2793 p = parse_comment(p);
2794 /* comments replaced by a blank */
2795 tok = ' ';
2796 goto keep_tok_flags;
2797 } else if (c == '/') {
2798 p = parse_line_comment(p);
2799 tok = ' ';
2800 goto keep_tok_flags;
2801 } else if (c == '=') {
2802 p++;
2803 tok = TOK_A_DIV;
2804 } else {
2805 tok = '/';
2807 break;
2809 /* simple tokens */
2810 case '(':
2811 case ')':
2812 case '[':
2813 case ']':
2814 case '{':
2815 case '}':
2816 case ',':
2817 case ';':
2818 case ':':
2819 case '?':
2820 case '~':
2821 case '@': /* only used in assembler */
2822 parse_simple:
2823 tok = c;
2824 p++;
2825 break;
2826 default:
2827 if (parse_flags & PARSE_FLAG_ASM_FILE)
2828 goto parse_simple;
2829 tcc_error("unrecognized character \\x%02x", c);
2830 break;
2832 tok_flags = 0;
2833 keep_tok_flags:
2834 file->buf_ptr = p;
2835 #if defined(PARSE_DEBUG)
2836 printf("token = %s\n", get_tok_str(tok, &tokc));
2837 #endif
2840 /* return next token without macro substitution. Can read input from
2841 macro_ptr buffer */
2842 static void next_nomacro_spc(void)
2844 if (macro_ptr) {
2845 redo:
2846 tok = *macro_ptr;
2847 if (tok) {
2848 TOK_GET(&tok, &macro_ptr, &tokc);
2849 if (tok == TOK_LINENUM) {
2850 file->line_num = tokc.i;
2851 goto redo;
2854 } else {
2855 next_nomacro1();
2859 ST_FUNC void next_nomacro(void)
2861 do {
2862 next_nomacro_spc();
2863 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2867 static void macro_subst(
2868 TokenString *tok_str,
2869 Sym **nested_list,
2870 const int *macro_str,
2871 int can_read_stream
2874 /* substitute arguments in replacement lists in macro_str by the values in
2875 args (field d) and return allocated string */
2876 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2878 int t, t0, t1, spc;
2879 const int *st;
2880 Sym *s;
2881 CValue cval;
2882 TokenString str;
2883 CString cstr;
2885 tok_str_new(&str);
2886 t0 = t1 = 0;
2887 while(1) {
2888 TOK_GET(&t, &macro_str, &cval);
2889 if (!t)
2890 break;
2891 if (t == '#') {
2892 /* stringize */
2893 TOK_GET(&t, &macro_str, &cval);
2894 if (!t)
2895 goto bad_stringy;
2896 s = sym_find2(args, t);
2897 if (s) {
2898 cstr_new(&cstr);
2899 cstr_ccat(&cstr, '\"');
2900 st = s->d;
2901 spc = 0;
2902 while (*st) {
2903 TOK_GET(&t, &st, &cval);
2904 if (t != TOK_PLCHLDR
2905 && t != TOK_NOSUBST
2906 && 0 == check_space(t, &spc)) {
2907 const char *s = get_tok_str(t, &cval);
2908 while (*s) {
2909 if (t == TOK_PPSTR && *s != '\'')
2910 add_char(&cstr, *s);
2911 else
2912 cstr_ccat(&cstr, *s);
2913 ++s;
2917 cstr.size -= spc;
2918 cstr_ccat(&cstr, '\"');
2919 cstr_ccat(&cstr, '\0');
2920 #ifdef PP_DEBUG
2921 printf("\nstringize: <%s>\n", (char *)cstr.data);
2922 #endif
2923 /* add string */
2924 cval.str.size = cstr.size;
2925 cval.str.data = cstr.data;
2926 cval.str.data_allocated = cstr.data_allocated;
2927 tok_str_add2(&str, TOK_PPSTR, &cval);
2928 cstr_free(&cstr);
2929 } else {
2930 bad_stringy:
2931 expect("macro parameter after '#'");
2933 } else if (t >= TOK_IDENT) {
2934 s = sym_find2(args, t);
2935 if (s) {
2936 int l0 = str.len;
2937 st = s->d;
2938 /* if '##' is present before or after, no arg substitution */
2939 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2940 /* special case for var arg macros : ## eats the ','
2941 if empty VA_ARGS variable. */
2942 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2943 if (*st == 0) {
2944 /* suppress ',' '##' */
2945 str.len -= 2;
2946 } else {
2947 /* suppress '##' and add variable */
2948 str.len--;
2949 goto add_var;
2951 } else {
2952 for(;;) {
2953 int t1;
2954 TOK_GET(&t1, &st, &cval);
2955 if (!t1)
2956 break;
2957 tok_str_add2(&str, t1, &cval);
2961 } else {
2962 add_var:
2963 /* NOTE: the stream cannot be read when macro
2964 substituing an argument */
2965 macro_subst(&str, nested_list, st, 0);
2967 if (str.len == l0) /* exanded to empty string */
2968 tok_str_add(&str, TOK_PLCHLDR);
2969 } else {
2970 tok_str_add(&str, t);
2972 } else {
2973 tok_str_add2(&str, t, &cval);
2975 t0 = t1, t1 = t;
2977 tok_str_add(&str, 0);
2978 return str.str;
2981 static char const ab_month_name[12][4] =
2983 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2984 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2987 /* peek or read [ws_str == NULL] next token from function macro call,
2988 walking up macro levels up to the file if necessary */
2989 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2991 int t;
2992 const int *p;
2993 Sym *sa;
2995 for (;;) {
2996 if (macro_ptr) {
2997 p = macro_ptr, t = *p;
2998 if (ws_str) {
2999 while (is_space(t) || TOK_LINEFEED == t)
3000 tok_str_add(ws_str, t), t = *++p;
3002 if (t == 0 && can_read_stream) {
3003 end_macro();
3004 /* also, end of scope for nested defined symbol */
3005 sa = *nested_list;
3006 while (sa && sa->v == 0)
3007 sa = sa->prev;
3008 if (sa)
3009 sa->v = 0;
3010 continue;
3012 } else {
3013 ch = handle_eob();
3014 if (ws_str) {
3015 while (is_space(ch) || ch == '\n' || ch == '/') {
3016 if (ch == '/') {
3017 int c;
3018 uint8_t *p = file->buf_ptr;
3019 PEEKC(c, p);
3020 if (c == '*') {
3021 p = parse_comment(p);
3022 file->buf_ptr = p - 1;
3023 } else if (c == '/') {
3024 p = parse_line_comment(p);
3025 file->buf_ptr = p - 1;
3026 } else
3027 break;
3028 ch = ' ';
3030 tok_str_add(ws_str, ch);
3031 cinp();
3034 t = ch;
3037 if (ws_str)
3038 return t;
3039 next_nomacro_spc();
3040 return tok;
3044 /* do macro substitution of current token with macro 's' and add
3045 result to (tok_str,tok_len). 'nested_list' is the list of all
3046 macros we got inside to avoid recursing. Return non zero if no
3047 substitution needs to be done */
3048 static int macro_subst_tok(
3049 TokenString *tok_str,
3050 Sym **nested_list,
3051 Sym *s,
3052 int can_read_stream)
3054 Sym *args, *sa, *sa1;
3055 int parlevel, *mstr, t, t1, spc;
3056 TokenString str;
3057 char *cstrval;
3058 CValue cval;
3059 CString cstr;
3060 char buf[32];
3062 /* if symbol is a macro, prepare substitution */
3063 /* special macros */
3064 if (tok == TOK___LINE__) {
3065 snprintf(buf, sizeof(buf), "%d", file->line_num);
3066 cstrval = buf;
3067 t1 = TOK_PPNUM;
3068 goto add_cstr1;
3069 } else if (tok == TOK___FILE__) {
3070 cstrval = file->filename;
3071 goto add_cstr;
3072 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3073 time_t ti;
3074 struct tm *tm;
3076 time(&ti);
3077 tm = localtime(&ti);
3078 if (tok == TOK___DATE__) {
3079 snprintf(buf, sizeof(buf), "%s %2d %d",
3080 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3081 } else {
3082 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3083 tm->tm_hour, tm->tm_min, tm->tm_sec);
3085 cstrval = buf;
3086 add_cstr:
3087 t1 = TOK_STR;
3088 add_cstr1:
3089 cstr_new(&cstr);
3090 cstr_cat(&cstr, cstrval, 0);
3091 cval.str.size = cstr.size;
3092 cval.str.data = cstr.data;
3093 cval.str.data_allocated = cstr.data_allocated;
3094 tok_str_add2(tok_str, t1, &cval);
3095 cstr_free(&cstr);
3096 } else {
3097 int saved_parse_flags = parse_flags;
3099 mstr = s->d;
3100 if (s->type.t == MACRO_FUNC) {
3101 /* whitespace between macro name and argument list */
3102 TokenString ws_str;
3103 tok_str_new(&ws_str);
3105 spc = 0;
3106 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3107 | PARSE_FLAG_ACCEPT_STRAYS;
3109 /* get next token from argument stream */
3110 t = next_argstream(nested_list, can_read_stream, &ws_str);
3111 if (t != '(') {
3112 /* not a macro substitution after all, restore the
3113 * macro token plus all whitespace we've read.
3114 * whitespace is intentionally not merged to preserve
3115 * newlines. */
3116 parse_flags = saved_parse_flags;
3117 tok_str_add(tok_str, tok);
3118 if (parse_flags & PARSE_FLAG_SPACES) {
3119 int i;
3120 for (i = 0; i < ws_str.len; i++)
3121 tok_str_add(tok_str, ws_str.str[i]);
3123 tok_str_free(ws_str.str);
3124 return 0;
3125 } else {
3126 tok_str_free(ws_str.str);
3128 next_nomacro(); /* eat '(' */
3130 /* argument macro */
3131 args = NULL;
3132 sa = s->next;
3133 /* NOTE: empty args are allowed, except if no args */
3134 for(;;) {
3135 do {
3136 next_argstream(nested_list, can_read_stream, NULL);
3137 } while (is_space(tok) || TOK_LINEFEED == tok);
3138 empty_arg:
3139 /* handle '()' case */
3140 if (!args && !sa && tok == ')')
3141 break;
3142 if (!sa)
3143 tcc_error("macro '%s' used with too many args",
3144 get_tok_str(s->v, 0));
3145 tok_str_new(&str);
3146 parlevel = spc = 0;
3147 /* NOTE: non zero sa->t indicates VA_ARGS */
3148 while ((parlevel > 0 ||
3149 (tok != ')' &&
3150 (tok != ',' || sa->type.t)))) {
3151 if (tok == TOK_EOF || tok == 0)
3152 break;
3153 if (tok == '(')
3154 parlevel++;
3155 else if (tok == ')')
3156 parlevel--;
3157 if (tok == TOK_LINEFEED)
3158 tok = ' ';
3159 if (!check_space(tok, &spc))
3160 tok_str_add2(&str, tok, &tokc);
3161 next_argstream(nested_list, can_read_stream, NULL);
3163 if (parlevel)
3164 expect(")");
3165 str.len -= spc;
3166 tok_str_add(&str, 0);
3167 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3168 sa1->d = str.str;
3169 sa = sa->next;
3170 if (tok == ')') {
3171 /* special case for gcc var args: add an empty
3172 var arg argument if it is omitted */
3173 if (sa && sa->type.t && gnu_ext)
3174 goto empty_arg;
3175 break;
3177 if (tok != ',')
3178 expect(",");
3180 if (sa) {
3181 tcc_error("macro '%s' used with too few args",
3182 get_tok_str(s->v, 0));
3185 parse_flags = saved_parse_flags;
3187 /* now subst each arg */
3188 mstr = macro_arg_subst(nested_list, mstr, args);
3189 /* free memory */
3190 sa = args;
3191 while (sa) {
3192 sa1 = sa->prev;
3193 tok_str_free(sa->d);
3194 sym_free(sa);
3195 sa = sa1;
3199 sym_push2(nested_list, s->v, 0, 0);
3200 parse_flags = saved_parse_flags;
3201 macro_subst(tok_str, nested_list, mstr, can_read_stream | 2);
3203 /* pop nested defined symbol */
3204 sa1 = *nested_list;
3205 *nested_list = sa1->prev;
3206 sym_free(sa1);
3207 if (mstr != s->d)
3208 tok_str_free(mstr);
3210 return 0;
3213 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3215 CString cstr;
3216 int n;
3218 cstr_new(&cstr);
3219 if (t1 != TOK_PLCHLDR)
3220 cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3221 n = cstr.size;
3222 if (t2 != TOK_PLCHLDR)
3223 cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3224 cstr_ccat(&cstr, '\0');
3226 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3227 memcpy(file->buffer, cstr.data, cstr.size);
3228 for (;;) {
3229 next_nomacro1();
3230 if (0 == *file->buf_ptr)
3231 break;
3232 if (is_space(tok))
3233 continue;
3234 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3235 n, cstr.data, (char*)cstr.data + n);
3236 break;
3238 tcc_close();
3240 //printf("paste <%s>\n", (char*)cstr.data);
3241 cstr_free(&cstr);
3242 return 0;
3245 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3246 return the resulting string (which must be freed). */
3247 static inline int *macro_twosharps(const int *ptr0)
3249 int t;
3250 CValue cval;
3251 TokenString macro_str1;
3252 int start_of_nosubsts = -1;
3253 const int *ptr;
3255 /* we search the first '##' */
3256 for (ptr = ptr0;;) {
3257 TOK_GET(&t, &ptr, &cval);
3258 if (t == TOK_TWOSHARPS)
3259 break;
3260 if (t == 0)
3261 return NULL;
3264 tok_str_new(&macro_str1);
3266 //tok_print(" $$$", ptr0);
3267 for (ptr = ptr0;;) {
3268 TOK_GET(&t, &ptr, &cval);
3269 if (t == 0)
3270 break;
3271 if (t == TOK_TWOSHARPS)
3272 continue;
3273 while (*ptr == TOK_TWOSHARPS) {
3274 int t1; CValue cv1;
3275 /* given 'a##b', remove nosubsts preceding 'a' */
3276 if (start_of_nosubsts >= 0)
3277 macro_str1.len = start_of_nosubsts;
3278 /* given 'a##b', remove nosubsts preceding 'b' */
3279 while ((t1 = *++ptr) == TOK_NOSUBST)
3281 if (t1 && t1 != TOK_TWOSHARPS
3282 && t1 != ':') /* 'a##:' don't build a new token */
3284 TOK_GET(&t1, &ptr, &cv1);
3285 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3286 paste_tokens(t, &cval, t1, &cv1);
3287 t = tok, cval = tokc;
3291 if (t == TOK_NOSUBST) {
3292 if (start_of_nosubsts < 0)
3293 start_of_nosubsts = macro_str1.len;
3294 } else {
3295 start_of_nosubsts = -1;
3297 tok_str_add2(&macro_str1, t, &cval);
3299 tok_str_add(&macro_str1, 0);
3300 //tok_print(" ###", macro_str1.str);
3301 return macro_str1.str;
3304 /* do macro substitution of macro_str and add result to
3305 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3306 inside to avoid recursing. */
3307 static void macro_subst(
3308 TokenString *tok_str,
3309 Sym **nested_list,
3310 const int *macro_str,
3311 int can_read_stream
3314 Sym *s;
3315 const int *ptr;
3316 int t, spc, nosubst;
3317 CValue cval;
3318 int *macro_str1 = NULL;
3320 /* first scan for '##' operator handling */
3321 ptr = macro_str;
3322 spc = nosubst = 0;
3324 /* first scan for '##' operator handling */
3325 if (can_read_stream & 1) {
3326 macro_str1 = macro_twosharps(ptr);
3327 if (macro_str1)
3328 ptr = macro_str1;
3331 while (1) {
3332 TOK_GET(&t, &ptr, &cval);
3333 if (t == 0)
3334 break;
3336 if (t >= TOK_IDENT && 0 == nosubst) {
3337 s = define_find(t);
3338 if (s == NULL)
3339 goto no_subst;
3341 /* if nested substitution, do nothing */
3342 if (sym_find2(*nested_list, t)) {
3343 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3344 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3345 goto no_subst;
3349 TokenString str;
3350 str.str = (int*)ptr;
3351 begin_macro(&str, 2);
3353 tok = t;
3354 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3356 if (str.alloc == 3) {
3357 /* already finished by reading function macro arguments */
3358 break;
3361 ptr = macro_ptr;
3362 end_macro ();
3365 spc = (tok_str->len &&
3366 is_space(tok_last(tok_str->str,
3367 tok_str->str + tok_str->len)));
3369 } else {
3371 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3372 tcc_error("stray '\\' in program");
3374 no_subst:
3375 if (!check_space(t, &spc))
3376 tok_str_add2(tok_str, t, &cval);
3377 nosubst = 0;
3378 if (t == TOK_NOSUBST)
3379 nosubst = 1;
3382 if (macro_str1)
3383 tok_str_free(macro_str1);
3387 /* return next token with macro substitution */
3388 ST_FUNC void next(void)
3390 redo:
3391 if (parse_flags & PARSE_FLAG_SPACES)
3392 next_nomacro_spc();
3393 else
3394 next_nomacro();
3396 if (macro_ptr) {
3397 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3398 /* discard preprocessor markers */
3399 goto redo;
3400 } else if (tok == 0) {
3401 /* end of macro or unget token string */
3402 end_macro();
3403 goto redo;
3405 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3406 Sym *s;
3407 /* if reading from file, try to substitute macros */
3408 s = define_find(tok);
3409 if (s) {
3410 Sym *nested_list = NULL;
3411 tokstr_buf.len = 0;
3412 nested_list = NULL;
3413 macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
3414 tok_str_add(&tokstr_buf, 0);
3415 begin_macro(&tokstr_buf, 2);
3416 goto redo;
3419 /* convert preprocessor tokens into C tokens */
3420 if (tok == TOK_PPNUM) {
3421 if (parse_flags & PARSE_FLAG_TOK_NUM)
3422 parse_number((char *)tokc.str.data);
3423 } else if (tok == TOK_PPSTR) {
3424 if (parse_flags & PARSE_FLAG_TOK_STR)
3425 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3429 /* push back current token and set current token to 'last_tok'. Only
3430 identifier case handled for labels. */
3431 ST_INLN void unget_tok(int last_tok)
3433 TokenString *str = tcc_malloc(sizeof *str);
3434 tok_str_new(str);
3435 tok_str_add2(str, tok, &tokc);
3436 tok_str_add(str, 0);
3437 begin_macro(str, 1);
3438 tok = last_tok;
3441 ST_FUNC void preprocess_init(TCCState *s1)
3443 s1->include_stack_ptr = s1->include_stack;
3444 /* XXX: move that before to avoid having to initialize
3445 file->ifdef_stack_ptr ? */
3446 s1->ifdef_stack_ptr = s1->ifdef_stack;
3447 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3449 pvtop = vtop = vstack - 1;
3450 s1->pack_stack[0] = 0;
3451 s1->pack_stack_ptr = s1->pack_stack;
3453 isidnum_table['$' - CH_EOF] =
3454 s1->dollars_in_identifiers ? IS_ID : 0;
3455 isidnum_table['.' - CH_EOF] =
3456 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
3459 ST_FUNC void preprocess_new(void)
3461 int i, c;
3462 const char *p, *r;
3464 /* init isid table */
3465 for(i = CH_EOF; i<128; i++)
3466 isidnum_table[i - CH_EOF]
3467 = is_space(i) ? IS_SPC
3468 : isid(i) ? IS_ID
3469 : isnum(i) ? IS_NUM
3470 : 0;
3472 for(i = 128; i<256; i++)
3473 isidnum_table[i - CH_EOF] = IS_ID;
3475 /* init allocators */
3476 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
3477 tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
3478 tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
3480 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3481 cstr_new(&cstr_buf);
3482 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
3483 tok_str_new(&tokstr_buf);
3484 tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
3486 tok_ident = TOK_IDENT;
3487 p = tcc_keywords;
3488 while (*p) {
3489 r = p;
3490 for(;;) {
3491 c = *r++;
3492 if (c == '\0')
3493 break;
3495 tok_alloc(p, r - p - 1);
3496 p = r;
3500 ST_FUNC void preprocess_delete(void)
3502 int i, n;
3504 /* free -D and compiler defines */
3505 free_defines(NULL);
3507 /* cleanup from error/setjmp */
3508 while (macro_stack)
3509 end_macro();
3510 macro_ptr = NULL;
3512 /* free tokens */
3513 n = tok_ident - TOK_IDENT;
3514 for(i = 0; i < n; i++)
3515 tal_free(toksym_alloc, table_ident[i]);
3516 tcc_free(table_ident);
3517 table_ident = NULL;
3519 /* free static buffers */
3520 cstr_free(&tokcstr);
3521 cstr_free(&cstr_buf);
3522 tok_str_free(tokstr_buf.str);
3524 /* free allocators */
3525 tal_delete(toksym_alloc);
3526 toksym_alloc = NULL;
3527 tal_delete(tokstr_alloc);
3528 tokstr_alloc = NULL;
3529 tal_delete(cstr_alloc);
3530 cstr_alloc = NULL;
3533 /* ------------------------------------------------------------------------- */
3534 /* tcc -E [-P[1]] [-dD} support */
3536 static void tok_print(const char *msg, const int *str)
3538 FILE *fp;
3539 int t;
3540 CValue cval;
3542 fp = tcc_state->ppfp;
3543 if (!fp || !tcc_state->dflag)
3544 fp = stdout;
3546 fprintf(fp, "%s ", msg);
3547 while (str) {
3548 TOK_GET(&t, &str, &cval);
3549 if (!t)
3550 break;
3551 fprintf(fp,"%s", get_tok_str(t, &cval));
3553 fprintf(fp, "\n");
3556 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3558 int d = f->line_num - f->line_ref;
3560 if (s1->dflag & 4)
3561 return;
3563 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3564 if (level == 0 && f->line_ref && d) {
3565 d = 1;
3566 goto simple;
3568 } else if (level == 0 && f->line_ref && d < 8) {
3569 simple:
3570 while (d > 0)
3571 fputs("\n", s1->ppfp), --d;
3572 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3573 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3574 } else {
3575 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3576 level > 0 ? " 1" : level < 0 ? " 2" : "");
3578 f->line_ref = f->line_num;
3581 static void define_print(TCCState *s1, int v)
3583 FILE *fp;
3584 Sym *s;
3586 s = define_find(v);
3587 if (NULL == s || NULL == s->d)
3588 return;
3590 fp = s1->ppfp;
3591 fprintf(fp, "#define %s", get_tok_str(v, NULL));
3592 if (s->type.t == MACRO_FUNC) {
3593 Sym *a = s->next;
3594 fprintf(fp,"(");
3595 if (a)
3596 for (;;) {
3597 fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3598 if (!(a = a->next))
3599 break;
3600 fprintf(fp,",");
3602 fprintf(fp,")");
3604 tok_print("", s->d);
3607 static void pp_debug_defines(TCCState *s1)
3609 int v, t;
3610 const char *vs;
3611 FILE *fp;
3613 t = pp_debug_tok;
3614 if (t == 0)
3615 return;
3617 file->line_num--;
3618 pp_line(s1, file, 0);
3619 file->line_ref = ++file->line_num;
3621 fp = s1->ppfp;
3622 v = pp_debug_symv;
3623 vs = get_tok_str(v, NULL);
3624 if (t == TOK_DEFINE) {
3625 define_print(s1, v);
3626 } else if (t == TOK_UNDEF) {
3627 fprintf(fp, "#undef %s\n", vs);
3628 } else if (t == TOK_push_macro) {
3629 fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3630 } else if (t == TOK_pop_macro) {
3631 fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3633 pp_debug_tok = 0;
3636 static void pp_debug_builtins(TCCState *s1)
3638 int v;
3639 for (v = TOK_IDENT; v < tok_ident; ++v)
3640 define_print(s1, v);
3643 static int need_space(int prev_tok, int tok, const char *tokstr)
3645 const char *sp_chars = "";
3646 if ((prev_tok >= TOK_IDENT || prev_tok == TOK_PPNUM) &&
3647 (tok >= TOK_IDENT || tok == TOK_PPNUM))
3648 return 1;
3649 switch (prev_tok) {
3650 case '+':
3651 sp_chars = "+=";
3652 break;
3653 case '-':
3654 sp_chars = "-=>";
3655 break;
3656 case '*':
3657 case '/':
3658 case '%':
3659 case '^':
3660 case '=':
3661 case '!':
3662 case TOK_A_SHL:
3663 case TOK_A_SAR:
3664 sp_chars = "=";
3665 break;
3666 case '&':
3667 sp_chars = "&=";
3668 break;
3669 case '|':
3670 sp_chars = "|=";
3671 break;
3672 case '<':
3673 sp_chars = "<=";
3674 break;
3675 case '>':
3676 sp_chars = ">=";
3677 break;
3678 case '.':
3679 sp_chars = ".";
3680 break;
3681 case '#':
3682 sp_chars = "#";
3683 break;
3684 case TOK_PPNUM:
3685 sp_chars = "+-";
3686 break;
3688 return !!strchr(sp_chars, tokstr[0]);
3691 /* Preprocess the current file */
3692 ST_FUNC int tcc_preprocess(TCCState *s1)
3694 BufferedFile **iptr;
3695 int token_seen, spcs, level;
3696 Sym *define_start;
3697 const char *tokstr;
3699 preprocess_init(s1);
3700 ch = file->buf_ptr[0];
3701 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3702 parse_flags = PARSE_FLAG_PREPROCESS
3703 | (parse_flags & PARSE_FLAG_ASM_FILE)
3704 | PARSE_FLAG_LINEFEED
3705 | PARSE_FLAG_SPACES
3706 | PARSE_FLAG_ACCEPT_STRAYS
3708 define_start = define_stack;
3710 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3711 capability to compile and run itself, provided all numbers are
3712 given as decimals. tcc -E -P10 will do. */
3713 if (s1->Pflag == 1 + 10)
3714 parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
3716 #ifdef PP_BENCH
3717 /* for PP benchmarks */
3718 do next(); while (tok != TOK_EOF); return 0;
3719 #endif
3721 if (s1->dflag & 1) {
3722 pp_debug_builtins(s1);
3723 s1->dflag &= ~1;
3726 token_seen = TOK_LINEFEED, spcs = 0;
3727 pp_line(s1, file, 0);
3729 for (;;) {
3730 iptr = s1->include_stack_ptr;
3731 next();
3732 if (tok == TOK_EOF)
3733 break;
3734 level = s1->include_stack_ptr - iptr;
3735 if (level) {
3736 if (level > 0)
3737 pp_line(s1, *iptr, 0);
3738 pp_line(s1, file, level);
3741 if (s1->dflag) {
3742 pp_debug_defines(s1);
3743 if (s1->dflag & 4)
3744 continue;
3747 if (token_seen == TOK_LINEFEED) {
3748 if (tok == ' ') {
3749 ++spcs;
3750 continue;
3752 if (tok == TOK_LINEFEED) {
3753 spcs = 0;
3754 continue;
3756 pp_line(s1, file, 0);
3757 } else if (tok == TOK_LINEFEED) {
3758 ++file->line_ref;
3761 tokstr = get_tok_str(tok, &tokc);
3762 if (!spcs && need_space(token_seen, tok, tokstr))
3763 ++spcs;
3764 while (spcs)
3765 fputs(" ", s1->ppfp), --spcs;
3766 fputs(tokstr, s1->ppfp);
3768 token_seen = tok;
3770 /* reset define stack, but keep -D and built-ins */
3771 free_defines(define_start);
3772 return 0;
3775 /* ------------------------------------------------------------------------- */