tccpp: allow "0x1e+1" in asm
[tinycc.git] / tccpp.c
blob159ad75cc4d37990b164c97d5b367bbbcdee2b1b
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 CString cstr_buf;
46 static TokenString tokstr_buf;
47 static unsigned char isidnum_table[256 - CH_EOF];
48 static int pp_debug_tok, pp_debug_symv;
49 static int pp_once;
50 static void tok_print(const char *msg, const int *str);
52 static struct TinyAlloc *toksym_alloc;
53 static struct TinyAlloc *tokstr_alloc;
54 static struct TinyAlloc *cstr_alloc;
56 /* isidnum_table flags: */
57 #define IS_SPC 1
58 #define IS_ID 2
59 #define IS_NUM 4
61 static TokenString *macro_stack;
63 static const char tcc_keywords[] =
64 #define DEF(id, str) str "\0"
65 #include "tcctok.h"
66 #undef DEF
69 /* WARNING: the content of this string encodes token numbers */
70 static const unsigned char tok_two_chars[] =
71 /* outdated -- gr
72 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
73 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
74 */{
75 '<','=', TOK_LE,
76 '>','=', TOK_GE,
77 '!','=', TOK_NE,
78 '&','&', TOK_LAND,
79 '|','|', TOK_LOR,
80 '+','+', TOK_INC,
81 '-','-', TOK_DEC,
82 '=','=', TOK_EQ,
83 '<','<', TOK_SHL,
84 '>','>', TOK_SAR,
85 '+','=', TOK_A_ADD,
86 '-','=', TOK_A_SUB,
87 '*','=', TOK_A_MUL,
88 '/','=', TOK_A_DIV,
89 '%','=', TOK_A_MOD,
90 '&','=', TOK_A_AND,
91 '^','=', TOK_A_XOR,
92 '|','=', TOK_A_OR,
93 '-','>', TOK_ARROW,
94 '.','.', 0xa8, // C++ token ?
95 '#','#', TOK_TWOSHARPS,
99 static void next_nomacro_spc(void);
101 ST_FUNC void skip(int c)
103 if (tok != c)
104 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
105 next();
108 ST_FUNC void expect(const char *msg)
110 tcc_error("%s expected", msg);
113 /* ------------------------------------------------------------------------- */
114 /* Custom allocator for tiny objects */
116 #define USE_TAL
118 #ifndef USE_TAL
119 #define tal_free(al, p) tcc_free(p)
120 #define tal_realloc(al, p, size) tcc_realloc(p, size)
121 #define tal_new(a,b,c)
122 #define tal_delete(a)
123 #else
124 #if !defined(MEM_DEBUG)
125 #define tal_free(al, p) tal_free_impl(al, p)
126 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
127 #define TAL_DEBUG_PARAMS
128 #else
129 #define TAL_DEBUG 1
130 #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
131 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
132 #define TAL_DEBUG_PARAMS , const char *file, int line
133 #define TAL_DEBUG_FILE_LEN 15
134 #endif
135 //#define TAL_INFO 1 /* collect and dump allocators stats */
137 typedef struct TinyAlloc {
138 size_t limit;
139 size_t size;
140 uint8_t *buffer;
141 uint8_t *p;
142 size_t nb_allocs;
143 struct TinyAlloc *next, *top;
144 #ifdef TAL_INFO
145 size_t nb_peak;
146 size_t nb_total;
147 size_t nb_missed;
148 uint8_t *peak_p;
149 #endif
150 } TinyAlloc;
152 typedef struct tal_header_t {
153 size_t size;
154 #ifdef TAL_DEBUG
155 int line_num; /* negative line_num used for double free check */
156 char file_name[TAL_DEBUG_FILE_LEN + 1];
157 #endif
158 } tal_header_t;
160 /* ------------------------------------------------------------------------- */
162 ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t size)
164 TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
165 al->p = al->buffer = tcc_malloc(size);
166 al->limit = limit;
167 al->size = size;
168 if (pal) *pal = al;
169 return al;
172 ST_FUNC void tal_delete(TinyAlloc *al)
174 TinyAlloc *next;
176 tail_call:
177 if (!al)
178 return;
179 #ifdef TAL_INFO
180 fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
181 al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
182 (al->peak_p - al->buffer) * 100.0 / al->size);
183 #endif
184 #ifdef TAL_DEBUG
185 if (al->nb_allocs > 0) {
186 uint8_t *p;
187 fprintf(stderr, "TAL_DEBUG: mem leak %d chunks (limit= %d)\n",
188 al->nb_allocs, al->limit);
189 p = al->buffer;
190 while (p < al->p) {
191 tal_header_t *header = (tal_header_t *)p;
192 if (header->line_num > 0) {
193 fprintf(stderr, " file %s, line %u: %u bytes\n",
194 header->file_name, header->line_num, header->size);
196 p += header->size + sizeof(tal_header_t);
199 #endif
200 next = al->next;
201 tcc_free(al->buffer);
202 tcc_free(al);
203 al = next;
204 goto tail_call;
207 ST_FUNC void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
209 if (!p)
210 return;
211 tail_call:
212 if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
213 #ifdef TAL_DEBUG
214 tal_header_t *header = (((tal_header_t *)p) - 1);
215 if (header->line_num < 0) {
216 fprintf(stderr, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
217 file, line);
218 fprintf(stderr, " file %s, line %u: %u bytes\n",
219 header->file_name, -header->line_num, header->size);
220 } else
221 header->line_num = -header->line_num;
222 #endif
223 al->nb_allocs--;
224 if (!al->nb_allocs)
225 al->p = al->buffer;
226 } else if (al->next) {
227 al = al->next;
228 goto tail_call;
230 else
231 tcc_free(p);
234 ST_FUNC void *tal_realloc_impl(TinyAlloc **pal, void *p, size_t size TAL_DEBUG_PARAMS)
236 tal_header_t *header;
237 void *ret;
238 int is_own;
239 size_t adj_size = (size + 3) & -4;
240 TinyAlloc *al = *pal;
242 tail_call:
243 is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
244 if ((!p || is_own) && size <= al->limit) {
245 if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) {
246 header = (tal_header_t *)al->p;
247 header->size = adj_size;
248 #ifdef TAL_DEBUG
249 { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
250 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
251 header->file_name[TAL_DEBUG_FILE_LEN] = 0;
252 header->line_num = line; }
253 #endif
254 ret = al->p + sizeof(tal_header_t);
255 al->p += adj_size + sizeof(tal_header_t);
256 if (is_own) {
257 header = (((tal_header_t *)p) - 1);
258 memcpy(ret, p, header->size);
259 #ifdef TAL_DEBUG
260 header->line_num = -header->line_num;
261 #endif
262 } else {
263 al->nb_allocs++;
265 #ifdef TAL_INFO
266 if (al->nb_peak < al->nb_allocs)
267 al->nb_peak = al->nb_allocs;
268 if (al->peak_p < al->p)
269 al->peak_p = al->p;
270 al->nb_total++;
271 #endif
272 return ret;
273 } else if (is_own) {
274 al->nb_allocs--;
275 ret = tal_realloc(*pal, 0, size);
276 header = (((tal_header_t *)p) - 1);
277 memcpy(ret, p, header->size);
278 #ifdef TAL_DEBUG
279 header->line_num = -header->line_num;
280 #endif
281 return ret;
283 if (al->next) {
284 al = al->next;
285 } else {
286 TinyAlloc *bottom = al, *next = al->top ? al->top : al;
288 al = tal_new(pal, next->limit, next->size * 2);
289 al->next = next;
290 bottom->top = al;
292 goto tail_call;
294 if (is_own) {
295 al->nb_allocs--;
296 ret = tcc_malloc(size);
297 header = (((tal_header_t *)p) - 1);
298 memcpy(ret, p, header->size);
299 #ifdef TAL_DEBUG
300 header->line_num = -header->line_num;
301 #endif
302 } else if (al->next) {
303 al = al->next;
304 goto tail_call;
305 } else
306 ret = tcc_realloc(p, size);
307 #ifdef TAL_INFO
308 al->nb_missed++;
309 #endif
310 return ret;
313 #endif /* USE_TAL */
315 /* ------------------------------------------------------------------------- */
316 /* CString handling */
317 static void cstr_realloc(CString *cstr, int new_size)
319 int size;
321 size = cstr->size_allocated;
322 if (size < 8)
323 size = 8; /* no need to allocate a too small first string */
324 while (size < new_size)
325 size = size * 2;
326 cstr->data = tal_realloc(cstr_alloc, cstr->data, size);
327 cstr->size_allocated = size;
330 /* add a byte */
331 ST_INLN void cstr_ccat(CString *cstr, int ch)
333 int size;
334 size = cstr->size + 1;
335 if (size > cstr->size_allocated)
336 cstr_realloc(cstr, size);
337 ((unsigned char *)cstr->data)[size - 1] = ch;
338 cstr->size = size;
341 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len)
343 int size;
344 if (len <= 0)
345 len = strlen(str) + 1 + len;
346 size = cstr->size + len;
347 if (size > cstr->size_allocated)
348 cstr_realloc(cstr, size);
349 memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
350 cstr->size = size;
353 /* add a wide char */
354 ST_FUNC void cstr_wccat(CString *cstr, int ch)
356 int size;
357 size = cstr->size + sizeof(nwchar_t);
358 if (size > cstr->size_allocated)
359 cstr_realloc(cstr, size);
360 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
361 cstr->size = size;
364 ST_FUNC void cstr_new(CString *cstr)
366 memset(cstr, 0, sizeof(CString));
369 /* free string and reset it to NULL */
370 ST_FUNC void cstr_free(CString *cstr)
372 tal_free(cstr_alloc, cstr->data);
373 cstr_new(cstr);
376 /* reset string to empty */
377 ST_FUNC void cstr_reset(CString *cstr)
379 cstr->size = 0;
382 /* XXX: unicode ? */
383 static void add_char(CString *cstr, int c)
385 if (c == '\'' || c == '\"' || c == '\\') {
386 /* XXX: could be more precise if char or string */
387 cstr_ccat(cstr, '\\');
389 if (c >= 32 && c <= 126) {
390 cstr_ccat(cstr, c);
391 } else {
392 cstr_ccat(cstr, '\\');
393 if (c == '\n') {
394 cstr_ccat(cstr, 'n');
395 } else {
396 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
397 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
398 cstr_ccat(cstr, '0' + (c & 7));
403 /* ------------------------------------------------------------------------- */
404 /* allocate a new token */
405 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
407 TokenSym *ts, **ptable;
408 int i;
410 if (tok_ident >= SYM_FIRST_ANOM)
411 tcc_error("memory full (symbols)");
413 /* expand token table if needed */
414 i = tok_ident - TOK_IDENT;
415 if ((i % TOK_ALLOC_INCR) == 0) {
416 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
417 table_ident = ptable;
420 ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len);
421 table_ident[i] = ts;
422 ts->tok = tok_ident++;
423 ts->sym_define = NULL;
424 ts->sym_label = NULL;
425 ts->sym_struct = NULL;
426 ts->sym_identifier = NULL;
427 ts->len = len;
428 ts->hash_next = NULL;
429 memcpy(ts->str, str, len);
430 ts->str[len] = '\0';
431 *pts = ts;
432 return ts;
435 #define TOK_HASH_INIT 1
436 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
439 /* find a token and add it if not found */
440 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
442 TokenSym *ts, **pts;
443 int i;
444 unsigned int h;
446 h = TOK_HASH_INIT;
447 for(i=0;i<len;i++)
448 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
449 h &= (TOK_HASH_SIZE - 1);
451 pts = &hash_ident[h];
452 for(;;) {
453 ts = *pts;
454 if (!ts)
455 break;
456 if (ts->len == len && !memcmp(ts->str, str, len))
457 return ts;
458 pts = &(ts->hash_next);
460 return tok_alloc_new(pts, str, len);
463 /* XXX: buffer overflow */
464 /* XXX: float tokens */
465 ST_FUNC const char *get_tok_str(int v, CValue *cv)
467 char *p;
468 int i, len;
470 cstr_reset(&cstr_buf);
471 p = cstr_buf.data;
473 switch(v) {
474 case TOK_CINT:
475 case TOK_CUINT:
476 case TOK_CLLONG:
477 case TOK_CULLONG:
478 /* XXX: not quite exact, but only useful for testing */
479 #ifdef _WIN32
480 sprintf(p, "%u", (unsigned)cv->i);
481 #else
482 sprintf(p, "%llu", (unsigned long long)cv->i);
483 #endif
484 break;
485 case TOK_LCHAR:
486 cstr_ccat(&cstr_buf, 'L');
487 case TOK_CCHAR:
488 cstr_ccat(&cstr_buf, '\'');
489 add_char(&cstr_buf, cv->i);
490 cstr_ccat(&cstr_buf, '\'');
491 cstr_ccat(&cstr_buf, '\0');
492 break;
493 case TOK_PPNUM:
494 case TOK_PPSTR:
495 return (char*)cv->str.data;
496 case TOK_LSTR:
497 cstr_ccat(&cstr_buf, 'L');
498 case TOK_STR:
499 cstr_ccat(&cstr_buf, '\"');
500 if (v == TOK_STR) {
501 len = cv->str.size - 1;
502 for(i=0;i<len;i++)
503 add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
504 } else {
505 len = (cv->str.size / sizeof(nwchar_t)) - 1;
506 for(i=0;i<len;i++)
507 add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
509 cstr_ccat(&cstr_buf, '\"');
510 cstr_ccat(&cstr_buf, '\0');
511 break;
513 case TOK_CFLOAT:
514 cstr_cat(&cstr_buf, "<float>", 0);
515 break;
516 case TOK_CDOUBLE:
517 cstr_cat(&cstr_buf, "<double>", 0);
518 break;
519 case TOK_CLDOUBLE:
520 cstr_cat(&cstr_buf, "<long double>", 0);
521 break;
522 case TOK_LINENUM:
523 cstr_cat(&cstr_buf, "<linenumber>", 0);
524 break;
526 /* above tokens have value, the ones below don't */
528 case TOK_LT:
529 v = '<';
530 goto addv;
531 case TOK_GT:
532 v = '>';
533 goto addv;
534 case TOK_DOTS:
535 return strcpy(p, "...");
536 case TOK_A_SHL:
537 return strcpy(p, "<<=");
538 case TOK_A_SAR:
539 return strcpy(p, ">>=");
540 default:
541 if (v < TOK_IDENT) {
542 /* search in two bytes table */
543 const unsigned char *q = tok_two_chars;
544 while (*q) {
545 if (q[2] == v) {
546 *p++ = q[0];
547 *p++ = q[1];
548 *p = '\0';
549 return cstr_buf.data;
551 q += 3;
553 if (v >= 127) {
554 sprintf(cstr_buf.data, "<%02x>", v);
555 return cstr_buf.data;
557 addv:
558 *p++ = v;
559 *p = '\0';
560 } else if (v < tok_ident) {
561 return table_ident[v - TOK_IDENT]->str;
562 } else if (v >= SYM_FIRST_ANOM) {
563 /* special name for anonymous symbol */
564 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
565 } else {
566 /* should never happen */
567 return NULL;
569 break;
571 return cstr_buf.data;
574 /* return the current character, handling end of block if necessary
575 (but not stray) */
576 ST_FUNC int handle_eob(void)
578 BufferedFile *bf = file;
579 int len;
581 /* only tries to read if really end of buffer */
582 if (bf->buf_ptr >= bf->buf_end) {
583 if (bf->fd != -1) {
584 #if defined(PARSE_DEBUG)
585 len = 1;
586 #else
587 len = IO_BUF_SIZE;
588 #endif
589 len = read(bf->fd, bf->buffer, len);
590 if (len < 0)
591 len = 0;
592 } else {
593 len = 0;
595 total_bytes += len;
596 bf->buf_ptr = bf->buffer;
597 bf->buf_end = bf->buffer + len;
598 *bf->buf_end = CH_EOB;
600 if (bf->buf_ptr < bf->buf_end) {
601 return bf->buf_ptr[0];
602 } else {
603 bf->buf_ptr = bf->buf_end;
604 return CH_EOF;
608 /* read next char from current input file and handle end of input buffer */
609 ST_INLN void inp(void)
611 ch = *(++(file->buf_ptr));
612 /* end of buffer/file handling */
613 if (ch == CH_EOB)
614 ch = handle_eob();
617 /* handle '\[\r]\n' */
618 static int handle_stray_noerror(void)
620 while (ch == '\\') {
621 inp();
622 if (ch == '\n') {
623 file->line_num++;
624 inp();
625 } else if (ch == '\r') {
626 inp();
627 if (ch != '\n')
628 goto fail;
629 file->line_num++;
630 inp();
631 } else {
632 fail:
633 return 1;
636 return 0;
639 static void handle_stray(void)
641 if (handle_stray_noerror())
642 tcc_error("stray '\\' in program");
645 /* skip the stray and handle the \\n case. Output an error if
646 incorrect char after the stray */
647 static int handle_stray1(uint8_t *p)
649 int c;
651 file->buf_ptr = p;
652 if (p >= file->buf_end) {
653 c = handle_eob();
654 if (c != '\\')
655 return c;
656 p = file->buf_ptr;
658 ch = *p;
659 if (handle_stray_noerror()) {
660 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
661 tcc_error("stray '\\' in program");
662 *--file->buf_ptr = '\\';
664 p = file->buf_ptr;
665 c = *p;
666 return c;
669 /* handle just the EOB case, but not stray */
670 #define PEEKC_EOB(c, p)\
672 p++;\
673 c = *p;\
674 if (c == '\\') {\
675 file->buf_ptr = p;\
676 c = handle_eob();\
677 p = file->buf_ptr;\
681 /* handle the complicated stray case */
682 #define PEEKC(c, p)\
684 p++;\
685 c = *p;\
686 if (c == '\\') {\
687 c = handle_stray1(p);\
688 p = file->buf_ptr;\
692 /* input with '\[\r]\n' handling. Note that this function cannot
693 handle other characters after '\', so you cannot call it inside
694 strings or comments */
695 ST_FUNC void minp(void)
697 inp();
698 if (ch == '\\')
699 handle_stray();
702 /* single line C++ comments */
703 static uint8_t *parse_line_comment(uint8_t *p)
705 int c;
707 p++;
708 for(;;) {
709 c = *p;
710 redo:
711 if (c == '\n' || c == CH_EOF) {
712 break;
713 } else if (c == '\\') {
714 file->buf_ptr = p;
715 c = handle_eob();
716 p = file->buf_ptr;
717 if (c == '\\') {
718 PEEKC_EOB(c, p);
719 if (c == '\n') {
720 file->line_num++;
721 PEEKC_EOB(c, p);
722 } else if (c == '\r') {
723 PEEKC_EOB(c, p);
724 if (c == '\n') {
725 file->line_num++;
726 PEEKC_EOB(c, p);
729 } else {
730 goto redo;
732 } else {
733 p++;
736 return p;
739 /* C comments */
740 ST_FUNC uint8_t *parse_comment(uint8_t *p)
742 int c;
744 p++;
745 for(;;) {
746 /* fast skip loop */
747 for(;;) {
748 c = *p;
749 if (c == '\n' || c == '*' || c == '\\')
750 break;
751 p++;
752 c = *p;
753 if (c == '\n' || c == '*' || c == '\\')
754 break;
755 p++;
757 /* now we can handle all the cases */
758 if (c == '\n') {
759 file->line_num++;
760 p++;
761 } else if (c == '*') {
762 p++;
763 for(;;) {
764 c = *p;
765 if (c == '*') {
766 p++;
767 } else if (c == '/') {
768 goto end_of_comment;
769 } else if (c == '\\') {
770 file->buf_ptr = p;
771 c = handle_eob();
772 p = file->buf_ptr;
773 if (c == CH_EOF)
774 tcc_error("unexpected end of file in comment");
775 if (c == '\\') {
776 /* skip '\[\r]\n', otherwise just skip the stray */
777 while (c == '\\') {
778 PEEKC_EOB(c, p);
779 if (c == '\n') {
780 file->line_num++;
781 PEEKC_EOB(c, p);
782 } else if (c == '\r') {
783 PEEKC_EOB(c, p);
784 if (c == '\n') {
785 file->line_num++;
786 PEEKC_EOB(c, p);
788 } else {
789 goto after_star;
793 } else {
794 break;
797 after_star: ;
798 } else {
799 /* stray, eob or eof */
800 file->buf_ptr = p;
801 c = handle_eob();
802 p = file->buf_ptr;
803 if (c == CH_EOF) {
804 tcc_error("unexpected end of file in comment");
805 } else if (c == '\\') {
806 p++;
810 end_of_comment:
811 p++;
812 return p;
815 #define cinp minp
817 static inline void skip_spaces(void)
819 while (isidnum_table[ch - CH_EOF] & IS_SPC)
820 cinp();
823 static inline int check_space(int t, int *spc)
825 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
826 if (*spc)
827 return 1;
828 *spc = 1;
829 } else
830 *spc = 0;
831 return 0;
834 /* parse a string without interpreting escapes */
835 static uint8_t *parse_pp_string(uint8_t *p,
836 int sep, CString *str)
838 int c;
839 p++;
840 for(;;) {
841 c = *p;
842 if (c == sep) {
843 break;
844 } else if (c == '\\') {
845 file->buf_ptr = p;
846 c = handle_eob();
847 p = file->buf_ptr;
848 if (c == CH_EOF) {
849 unterminated_string:
850 /* XXX: indicate line number of start of string */
851 tcc_error("missing terminating %c character", sep);
852 } else if (c == '\\') {
853 /* escape : just skip \[\r]\n */
854 PEEKC_EOB(c, p);
855 if (c == '\n') {
856 file->line_num++;
857 p++;
858 } else if (c == '\r') {
859 PEEKC_EOB(c, p);
860 if (c != '\n')
861 expect("'\n' after '\r'");
862 file->line_num++;
863 p++;
864 } else if (c == CH_EOF) {
865 goto unterminated_string;
866 } else {
867 if (str) {
868 cstr_ccat(str, '\\');
869 cstr_ccat(str, c);
871 p++;
874 } else if (c == '\n') {
875 file->line_num++;
876 goto add_char;
877 } else if (c == '\r') {
878 PEEKC_EOB(c, p);
879 if (c != '\n') {
880 if (str)
881 cstr_ccat(str, '\r');
882 } else {
883 file->line_num++;
884 goto add_char;
886 } else {
887 add_char:
888 if (str)
889 cstr_ccat(str, c);
890 p++;
893 p++;
894 return p;
897 /* skip block of text until #else, #elif or #endif. skip also pairs of
898 #if/#endif */
899 static void preprocess_skip(void)
901 int a, start_of_line, c, in_warn_or_error;
902 uint8_t *p;
904 p = file->buf_ptr;
905 a = 0;
906 redo_start:
907 start_of_line = 1;
908 in_warn_or_error = 0;
909 for(;;) {
910 redo_no_start:
911 c = *p;
912 switch(c) {
913 case ' ':
914 case '\t':
915 case '\f':
916 case '\v':
917 case '\r':
918 p++;
919 goto redo_no_start;
920 case '\n':
921 file->line_num++;
922 p++;
923 goto redo_start;
924 case '\\':
925 file->buf_ptr = p;
926 c = handle_eob();
927 if (c == CH_EOF) {
928 expect("#endif");
929 } else if (c == '\\') {
930 ch = file->buf_ptr[0];
931 handle_stray_noerror();
933 p = file->buf_ptr;
934 goto redo_no_start;
935 /* skip strings */
936 case '\"':
937 case '\'':
938 if (in_warn_or_error)
939 goto _default;
940 p = parse_pp_string(p, c, NULL);
941 break;
942 /* skip comments */
943 case '/':
944 if (in_warn_or_error)
945 goto _default;
946 file->buf_ptr = p;
947 ch = *p;
948 minp();
949 p = file->buf_ptr;
950 if (ch == '*') {
951 p = parse_comment(p);
952 } else if (ch == '/') {
953 p = parse_line_comment(p);
955 break;
956 case '#':
957 p++;
958 if (start_of_line) {
959 file->buf_ptr = p;
960 next_nomacro();
961 p = file->buf_ptr;
962 if (a == 0 &&
963 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
964 goto the_end;
965 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
966 a++;
967 else if (tok == TOK_ENDIF)
968 a--;
969 else if( tok == TOK_ERROR || tok == TOK_WARNING)
970 in_warn_or_error = 1;
971 else if (tok == TOK_LINEFEED)
972 goto redo_start;
973 else if (parse_flags & PARSE_FLAG_ASM_FILE)
974 p = parse_line_comment(p);
975 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
976 p = parse_line_comment(p);
977 break;
978 _default:
979 default:
980 p++;
981 break;
983 start_of_line = 0;
985 the_end: ;
986 file->buf_ptr = p;
989 /* ParseState handling */
991 /* XXX: currently, no include file info is stored. Thus, we cannot display
992 accurate messages if the function or data definition spans multiple
993 files */
995 /* save current parse state in 's' */
996 ST_FUNC void save_parse_state(ParseState *s)
998 s->line_num = file->line_num;
999 s->macro_ptr = macro_ptr;
1000 s->tok = tok;
1001 s->tokc = tokc;
1004 /* restore parse state from 's' */
1005 ST_FUNC void restore_parse_state(ParseState *s)
1007 file->line_num = s->line_num;
1008 macro_ptr = s->macro_ptr;
1009 tok = s->tok;
1010 tokc = s->tokc;
1013 /* return the number of additional 'ints' necessary to store the
1014 token */
1015 static inline int tok_size(const int *p)
1017 switch(*p) {
1018 /* 4 bytes */
1019 case TOK_CINT:
1020 case TOK_CUINT:
1021 case TOK_CCHAR:
1022 case TOK_LCHAR:
1023 case TOK_CFLOAT:
1024 case TOK_LINENUM:
1025 return 1 + 1;
1026 case TOK_STR:
1027 case TOK_LSTR:
1028 case TOK_PPNUM:
1029 case TOK_PPSTR:
1030 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
1031 case TOK_CDOUBLE:
1032 case TOK_CLLONG:
1033 case TOK_CULLONG:
1034 return 1 + 2;
1035 case TOK_CLDOUBLE:
1036 return 1 + LDOUBLE_SIZE / 4;
1037 default:
1038 return 1 + 0;
1042 /* token string handling */
1044 ST_INLN void tok_str_new(TokenString *s)
1046 s->str = NULL;
1047 s->len = 0;
1048 s->allocated_len = 0;
1049 s->last_line_num = -1;
1052 ST_FUNC TokenString *tok_str_alloc(void)
1054 TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
1055 tok_str_new(str);
1056 return str;
1059 ST_FUNC int *tok_str_dup(TokenString *s)
1061 int *str;
1063 str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
1064 memcpy(str, s->str, s->len * sizeof(int));
1065 return str;
1068 ST_FUNC void tok_str_free(int *str)
1070 tal_free(tokstr_alloc, str);
1073 ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
1075 int *str, size;
1077 size = s->allocated_len;
1078 if (size < 16)
1079 size = 16;
1080 while (size < new_size)
1081 size = size * 2;
1082 if (size > s->allocated_len) {
1083 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
1084 s->allocated_len = size;
1085 s->str = str;
1087 return s->str;
1090 ST_FUNC void tok_str_add(TokenString *s, int t)
1092 int len, *str;
1094 len = s->len;
1095 str = s->str;
1096 if (len >= s->allocated_len)
1097 str = tok_str_realloc(s, len + 1);
1098 str[len++] = t;
1099 s->len = len;
1102 ST_FUNC void begin_macro(TokenString *str, int alloc)
1104 str->alloc = alloc;
1105 str->prev = macro_stack;
1106 str->prev_ptr = macro_ptr;
1107 macro_ptr = str->str;
1108 macro_stack = str;
1111 ST_FUNC void end_macro(void)
1113 TokenString *str = macro_stack;
1114 macro_stack = str->prev;
1115 macro_ptr = str->prev_ptr;
1116 if (str->alloc == 2) {
1117 str->alloc = 3; /* just mark as finished */
1118 } else {
1119 tok_str_free(str->str);
1120 if (str->alloc == 1)
1121 tal_free(tokstr_alloc, str);
1125 static void tok_str_add2(TokenString *s, int t, CValue *cv)
1127 int len, *str;
1129 len = s->len;
1130 str = s->str;
1132 /* allocate space for worst case */
1133 if (len + TOK_MAX_SIZE >= s->allocated_len)
1134 str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
1135 str[len++] = t;
1136 switch(t) {
1137 case TOK_CINT:
1138 case TOK_CUINT:
1139 case TOK_CCHAR:
1140 case TOK_LCHAR:
1141 case TOK_CFLOAT:
1142 case TOK_LINENUM:
1143 str[len++] = cv->tab[0];
1144 break;
1145 case TOK_PPNUM:
1146 case TOK_PPSTR:
1147 case TOK_STR:
1148 case TOK_LSTR:
1150 /* Insert the string into the int array. */
1151 size_t nb_words =
1152 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
1153 if (len + nb_words >= s->allocated_len)
1154 str = tok_str_realloc(s, len + nb_words + 1);
1155 str[len] = cv->str.size;
1156 memcpy(&str[len + 1], cv->str.data, cv->str.size);
1157 len += nb_words;
1159 break;
1160 case TOK_CDOUBLE:
1161 case TOK_CLLONG:
1162 case TOK_CULLONG:
1163 #if LDOUBLE_SIZE == 8
1164 case TOK_CLDOUBLE:
1165 #endif
1166 str[len++] = cv->tab[0];
1167 str[len++] = cv->tab[1];
1168 break;
1169 #if LDOUBLE_SIZE == 12
1170 case TOK_CLDOUBLE:
1171 str[len++] = cv->tab[0];
1172 str[len++] = cv->tab[1];
1173 str[len++] = cv->tab[2];
1174 #elif LDOUBLE_SIZE == 16
1175 case TOK_CLDOUBLE:
1176 str[len++] = cv->tab[0];
1177 str[len++] = cv->tab[1];
1178 str[len++] = cv->tab[2];
1179 str[len++] = cv->tab[3];
1180 #elif LDOUBLE_SIZE != 8
1181 #error add long double size support
1182 #endif
1183 break;
1184 default:
1185 break;
1187 s->len = len;
1190 /* add the current parse token in token string 's' */
1191 ST_FUNC void tok_str_add_tok(TokenString *s)
1193 CValue cval;
1195 /* save line number info */
1196 if (file->line_num != s->last_line_num) {
1197 s->last_line_num = file->line_num;
1198 cval.i = s->last_line_num;
1199 tok_str_add2(s, TOK_LINENUM, &cval);
1201 tok_str_add2(s, tok, &tokc);
1204 /* get a token from an integer array and increment pointer
1205 accordingly. we code it as a macro to avoid pointer aliasing. */
1206 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
1208 const int *p = *pp;
1209 int n, *tab;
1211 tab = cv->tab;
1212 switch(*t = *p++) {
1213 case TOK_CINT:
1214 case TOK_CUINT:
1215 case TOK_CCHAR:
1216 case TOK_LCHAR:
1217 case TOK_CFLOAT:
1218 case TOK_LINENUM:
1219 tab[0] = *p++;
1220 break;
1221 case TOK_STR:
1222 case TOK_LSTR:
1223 case TOK_PPNUM:
1224 case TOK_PPSTR:
1225 cv->str.size = *p++;
1226 cv->str.data = p;
1227 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1228 break;
1229 case TOK_CDOUBLE:
1230 case TOK_CLLONG:
1231 case TOK_CULLONG:
1232 n = 2;
1233 goto copy;
1234 case TOK_CLDOUBLE:
1235 #if LDOUBLE_SIZE == 16
1236 n = 4;
1237 #elif LDOUBLE_SIZE == 12
1238 n = 3;
1239 #elif LDOUBLE_SIZE == 8
1240 n = 2;
1241 #else
1242 # error add long double size support
1243 #endif
1244 copy:
1246 *tab++ = *p++;
1247 while (--n);
1248 break;
1249 default:
1250 break;
1252 *pp = p;
1255 /* Calling this function is expensive, but it is not possible
1256 to read a token string backwards. */
1257 static int tok_last(const int *str0, const int *str1)
1259 const int *str = str0;
1260 int tok = 0;
1261 CValue cval;
1263 while (str < str1)
1264 TOK_GET(&tok, &str, &cval);
1265 return tok;
1268 static int macro_is_equal(const int *a, const int *b)
1270 CValue cv;
1271 int t;
1273 if (!a || !b)
1274 return 1;
1276 while (*a && *b) {
1277 /* first time preallocate static cstr_buf, next time only reset position to start */
1278 cstr_reset(&cstr_buf);
1279 TOK_GET(&t, &a, &cv);
1280 cstr_cat(&cstr_buf, get_tok_str(t, &cv), 0);
1281 TOK_GET(&t, &b, &cv);
1282 if (strcmp(cstr_buf.data, get_tok_str(t, &cv)))
1283 return 0;
1285 return !(*a || *b);
1288 /* defines handling */
1289 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1291 Sym *s, *o;
1293 o = define_find(v);
1294 s = sym_push2(&define_stack, v, macro_type, 0);
1295 s->d = str;
1296 s->next = first_arg;
1297 table_ident[v - TOK_IDENT]->sym_define = s;
1299 if (o && !macro_is_equal(o->d, s->d))
1300 tcc_warning("%s redefined", get_tok_str(v, NULL));
1303 /* undefined a define symbol. Its name is just set to zero */
1304 ST_FUNC void define_undef(Sym *s)
1306 int v = s->v;
1307 if (v >= TOK_IDENT && v < tok_ident)
1308 table_ident[v - TOK_IDENT]->sym_define = NULL;
1311 ST_INLN Sym *define_find(int v)
1313 v -= TOK_IDENT;
1314 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1315 return NULL;
1316 return table_ident[v]->sym_define;
1319 /* free define stack until top reaches 'b' */
1320 ST_FUNC void free_defines(Sym *b)
1322 while (define_stack != b) {
1323 Sym *top = define_stack;
1324 define_stack = top->prev;
1325 tok_str_free(top->d);
1326 define_undef(top);
1327 sym_free(top);
1330 /* restore remaining (-D or predefined) symbols */
1331 while (b) {
1332 int v = b->v;
1333 if (v >= TOK_IDENT && v < tok_ident) {
1334 Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
1335 if (!*d)
1336 *d = b;
1338 b = b->prev;
1342 /* label lookup */
1343 ST_FUNC Sym *label_find(int v)
1345 v -= TOK_IDENT;
1346 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1347 return NULL;
1348 return table_ident[v]->sym_label;
1351 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1353 Sym *s, **ps;
1354 s = sym_push2(ptop, v, 0, 0);
1355 s->r = flags;
1356 ps = &table_ident[v - TOK_IDENT]->sym_label;
1357 if (ptop == &global_label_stack) {
1358 /* modify the top most local identifier, so that
1359 sym_identifier will point to 's' when popped */
1360 while (*ps != NULL)
1361 ps = &(*ps)->prev_tok;
1363 s->prev_tok = *ps;
1364 *ps = s;
1365 return s;
1368 /* pop labels until element last is reached. Look if any labels are
1369 undefined. Define symbols if '&&label' was used. */
1370 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1372 Sym *s, *s1;
1373 for(s = *ptop; s != slast; s = s1) {
1374 s1 = s->prev;
1375 if (s->r == LABEL_DECLARED) {
1376 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1377 } else if (s->r == LABEL_FORWARD) {
1378 tcc_error("label '%s' used but not defined",
1379 get_tok_str(s->v, NULL));
1380 } else {
1381 if (s->c) {
1382 /* define corresponding symbol. A size of
1383 1 is put. */
1384 put_extern_sym(s, cur_text_section, s->jnext, 1);
1387 /* remove label */
1388 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1389 sym_free(s);
1391 *ptop = slast;
1394 /* eval an expression for #if/#elif */
1395 static int expr_preprocess(void)
1397 int c, t;
1398 TokenString *str;
1400 str = tok_str_alloc();
1401 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1402 next(); /* do macro subst */
1403 if (tok == TOK_DEFINED) {
1404 next_nomacro();
1405 t = tok;
1406 if (t == '(')
1407 next_nomacro();
1408 c = define_find(tok) != 0;
1409 if (t == '(')
1410 next_nomacro();
1411 tok = TOK_CINT;
1412 tokc.i = c;
1413 } else if (tok >= TOK_IDENT) {
1414 /* if undefined macro */
1415 tok = TOK_CINT;
1416 tokc.i = 0;
1418 tok_str_add_tok(str);
1420 tok_str_add(str, -1); /* simulate end of file */
1421 tok_str_add(str, 0);
1422 /* now evaluate C constant expression */
1423 begin_macro(str, 1);
1424 next();
1425 c = expr_const();
1426 end_macro();
1427 return c != 0;
1431 /* parse after #define */
1432 ST_FUNC void parse_define(void)
1434 Sym *s, *first, **ps;
1435 int v, t, varg, is_vaargs, spc;
1436 int saved_parse_flags = parse_flags;
1438 v = tok;
1439 if (v < TOK_IDENT)
1440 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1441 /* XXX: should check if same macro (ANSI) */
1442 first = NULL;
1443 t = MACRO_OBJ;
1444 /* '(' must be just after macro definition for MACRO_FUNC */
1445 parse_flags |= PARSE_FLAG_SPACES;
1446 next_nomacro_spc();
1447 if (tok == '(') {
1448 /* must be able to parse TOK_DOTS (in asm mode '.' can be part of identifier) */
1449 parse_flags &= ~PARSE_FLAG_ASM_FILE;
1450 isidnum_table['.' - CH_EOF] = 0;
1451 next_nomacro();
1452 ps = &first;
1453 if (tok != ')') for (;;) {
1454 varg = tok;
1455 next_nomacro();
1456 is_vaargs = 0;
1457 if (varg == TOK_DOTS) {
1458 varg = TOK___VA_ARGS__;
1459 is_vaargs = 1;
1460 } else if (tok == TOK_DOTS && gnu_ext) {
1461 is_vaargs = 1;
1462 next_nomacro();
1464 if (varg < TOK_IDENT)
1465 bad_list:
1466 tcc_error("bad macro parameter list");
1467 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1468 *ps = s;
1469 ps = &s->next;
1470 if (tok == ')')
1471 break;
1472 if (tok != ',' || is_vaargs)
1473 goto bad_list;
1474 next_nomacro();
1476 next_nomacro_spc();
1477 t = MACRO_FUNC;
1478 parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_FILE);
1479 isidnum_table['.' - CH_EOF] =
1480 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
1483 tokstr_buf.len = 0;
1484 spc = 2;
1485 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1486 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1487 /* remove spaces around ## and after '#' */
1488 if (TOK_TWOSHARPS == tok) {
1489 if (2 == spc)
1490 goto bad_twosharp;
1491 if (1 == spc)
1492 --tokstr_buf.len;
1493 spc = 3;
1494 } else if ('#' == tok) {
1495 spc = 4;
1496 } else if (check_space(tok, &spc)) {
1497 goto skip;
1499 tok_str_add2(&tokstr_buf, tok, &tokc);
1500 skip:
1501 next_nomacro_spc();
1504 parse_flags = saved_parse_flags;
1505 if (spc == 1)
1506 --tokstr_buf.len; /* remove trailing space */
1507 tok_str_add(&tokstr_buf, 0);
1508 if (3 == spc)
1509 bad_twosharp:
1510 tcc_error("'##' cannot appear at either end of macro");
1511 define_push(v, t, tok_str_dup(&tokstr_buf), first);
1514 static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
1516 const unsigned char *s;
1517 unsigned int h;
1518 CachedInclude *e;
1519 int i;
1521 h = TOK_HASH_INIT;
1522 s = (unsigned char *) filename;
1523 while (*s) {
1524 #ifdef _WIN32
1525 h = TOK_HASH_FUNC(h, toup(*s));
1526 #else
1527 h = TOK_HASH_FUNC(h, *s);
1528 #endif
1529 s++;
1531 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1533 i = s1->cached_includes_hash[h];
1534 for(;;) {
1535 if (i == 0)
1536 break;
1537 e = s1->cached_includes[i - 1];
1538 if (0 == PATHCMP(e->filename, filename))
1539 return e;
1540 i = e->hash_next;
1542 if (!add)
1543 return NULL;
1545 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1546 strcpy(e->filename, filename);
1547 e->ifndef_macro = e->once = 0;
1548 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1549 /* add in hash table */
1550 e->hash_next = s1->cached_includes_hash[h];
1551 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1552 #ifdef INC_DEBUG
1553 printf("adding cached '%s'\n", filename);
1554 #endif
1555 return e;
1558 static void pragma_parse(TCCState *s1)
1560 next_nomacro();
1561 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1562 int t = tok, v;
1563 Sym *s;
1565 if (next(), tok != '(')
1566 goto pragma_err;
1567 if (next(), tok != TOK_STR)
1568 goto pragma_err;
1569 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1570 if (next(), tok != ')')
1571 goto pragma_err;
1572 if (t == TOK_push_macro) {
1573 while (NULL == (s = define_find(v)))
1574 define_push(v, 0, NULL, NULL);
1575 s->type.ref = s; /* set push boundary */
1576 } else {
1577 for (s = define_stack; s; s = s->prev)
1578 if (s->v == v && s->type.ref == s) {
1579 s->type.ref = NULL;
1580 break;
1583 if (s)
1584 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1585 else
1586 tcc_warning("unbalanced #pragma pop_macro");
1587 pp_debug_tok = t, pp_debug_symv = v;
1589 } else if (tok == TOK_once) {
1590 search_cached_include(s1, file->filename, 1)->once = pp_once;
1592 } else if (s1->ppfp) {
1593 /* tcc -E: keep pragmas below unchanged */
1594 unget_tok(' ');
1595 unget_tok(TOK_PRAGMA);
1596 unget_tok('#');
1597 unget_tok(TOK_LINEFEED);
1599 } else if (tok == TOK_pack) {
1600 /* This may be:
1601 #pragma pack(1) // set
1602 #pragma pack() // reset to default
1603 #pragma pack(push,1) // push & set
1604 #pragma pack(pop) // restore previous */
1605 next();
1606 skip('(');
1607 if (tok == TOK_ASM_pop) {
1608 next();
1609 if (s1->pack_stack_ptr <= s1->pack_stack) {
1610 stk_error:
1611 tcc_error("out of pack stack");
1613 s1->pack_stack_ptr--;
1614 } else {
1615 int val = 0;
1616 if (tok != ')') {
1617 if (tok == TOK_ASM_push) {
1618 next();
1619 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1620 goto stk_error;
1621 s1->pack_stack_ptr++;
1622 skip(',');
1624 if (tok != TOK_CINT)
1625 goto pragma_err;
1626 val = tokc.i;
1627 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1628 goto pragma_err;
1629 next();
1631 *s1->pack_stack_ptr = val;
1633 if (tok != ')')
1634 goto pragma_err;
1636 } else if (tok == TOK_comment) {
1637 char *file;
1638 next();
1639 skip('(');
1640 if (tok != TOK_lib)
1641 goto pragma_warn;
1642 next();
1643 skip(',');
1644 if (tok != TOK_STR)
1645 goto pragma_err;
1646 file = tcc_strdup((char *)tokc.str.data);
1647 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1648 next();
1649 if (tok != ')')
1650 goto pragma_err;
1651 } else {
1652 pragma_warn:
1653 if (s1->warn_unsupported)
1654 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1656 return;
1658 pragma_err:
1659 tcc_error("malformed #pragma directive");
1660 return;
1663 /* is_bof is true if first non space token at beginning of file */
1664 ST_FUNC void preprocess(int is_bof)
1666 TCCState *s1 = tcc_state;
1667 int i, c, n, saved_parse_flags;
1668 char buf[1024], *q;
1669 Sym *s;
1671 saved_parse_flags = parse_flags;
1672 parse_flags = PARSE_FLAG_PREPROCESS
1673 | PARSE_FLAG_TOK_NUM
1674 | PARSE_FLAG_TOK_STR
1675 | PARSE_FLAG_LINEFEED
1676 | (parse_flags & PARSE_FLAG_ASM_FILE)
1679 next_nomacro();
1680 redo:
1681 switch(tok) {
1682 case TOK_DEFINE:
1683 pp_debug_tok = tok;
1684 next_nomacro();
1685 pp_debug_symv = tok;
1686 parse_define();
1687 break;
1688 case TOK_UNDEF:
1689 pp_debug_tok = tok;
1690 next_nomacro();
1691 pp_debug_symv = tok;
1692 s = define_find(tok);
1693 /* undefine symbol by putting an invalid name */
1694 if (s)
1695 define_undef(s);
1696 break;
1697 case TOK_INCLUDE:
1698 case TOK_INCLUDE_NEXT:
1699 ch = file->buf_ptr[0];
1700 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1701 skip_spaces();
1702 if (ch == '<') {
1703 c = '>';
1704 goto read_name;
1705 } else if (ch == '\"') {
1706 c = ch;
1707 read_name:
1708 inp();
1709 q = buf;
1710 while (ch != c && ch != '\n' && ch != CH_EOF) {
1711 if ((q - buf) < sizeof(buf) - 1)
1712 *q++ = ch;
1713 if (ch == '\\') {
1714 if (handle_stray_noerror() == 0)
1715 --q;
1716 } else
1717 inp();
1719 *q = '\0';
1720 minp();
1721 #if 0
1722 /* eat all spaces and comments after include */
1723 /* XXX: slightly incorrect */
1724 while (ch1 != '\n' && ch1 != CH_EOF)
1725 inp();
1726 #endif
1727 } else {
1728 /* computed #include : either we have only strings or
1729 we have anything enclosed in '<>' */
1730 next();
1731 buf[0] = '\0';
1732 if (tok == TOK_STR) {
1733 while (tok != TOK_LINEFEED) {
1734 if (tok != TOK_STR) {
1735 include_syntax:
1736 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1738 pstrcat(buf, sizeof(buf), (char *)tokc.str.data);
1739 next();
1741 c = '\"';
1742 } else {
1743 int len;
1744 while (tok != TOK_LINEFEED) {
1745 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1746 next();
1748 len = strlen(buf);
1749 /* check syntax and remove '<>' */
1750 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1751 goto include_syntax;
1752 memmove(buf, buf + 1, len - 2);
1753 buf[len - 2] = '\0';
1754 c = '>';
1758 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1759 tcc_error("#include recursion too deep");
1760 /* store current file in stack, but increment stack later below */
1761 *s1->include_stack_ptr = file;
1762 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1763 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1764 for (; i < n; ++i) {
1765 char buf1[sizeof file->filename];
1766 CachedInclude *e;
1767 const char *path;
1769 if (i == 0) {
1770 /* check absolute include path */
1771 if (!IS_ABSPATH(buf))
1772 continue;
1773 buf1[0] = 0;
1775 } else if (i == 1) {
1776 /* search in current dir if "header.h" */
1777 if (c != '\"')
1778 continue;
1779 path = file->filename;
1780 pstrncpy(buf1, path, tcc_basename(path) - path);
1782 } else {
1783 /* search in all the include paths */
1784 int j = i - 2, k = j - s1->nb_include_paths;
1785 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1786 pstrcpy(buf1, sizeof(buf1), path);
1787 pstrcat(buf1, sizeof(buf1), "/");
1790 pstrcat(buf1, sizeof(buf1), buf);
1791 e = search_cached_include(s1, buf1, 0);
1792 if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
1793 /* no need to parse the include because the 'ifndef macro'
1794 is defined (or had #pragma once) */
1795 #ifdef INC_DEBUG
1796 printf("%s: skipping cached %s\n", file->filename, buf1);
1797 #endif
1798 goto include_done;
1801 if (tcc_open(s1, buf1) < 0)
1802 continue;
1804 file->include_next_index = i + 1;
1805 #ifdef INC_DEBUG
1806 printf("%s: including %s\n", file->prev->filename, file->filename);
1807 #endif
1808 /* update target deps */
1809 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1810 tcc_strdup(buf1));
1811 /* push current file in stack */
1812 ++s1->include_stack_ptr;
1813 /* add include file debug info */
1814 if (s1->do_debug)
1815 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1816 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1817 ch = file->buf_ptr[0];
1818 goto the_end;
1820 tcc_error("include file '%s' not found", buf);
1821 include_done:
1822 break;
1823 case TOK_IFNDEF:
1824 c = 1;
1825 goto do_ifdef;
1826 case TOK_IF:
1827 c = expr_preprocess();
1828 goto do_if;
1829 case TOK_IFDEF:
1830 c = 0;
1831 do_ifdef:
1832 next_nomacro();
1833 if (tok < TOK_IDENT)
1834 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1835 if (is_bof) {
1836 if (c) {
1837 #ifdef INC_DEBUG
1838 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1839 #endif
1840 file->ifndef_macro = tok;
1843 c = (define_find(tok) != 0) ^ c;
1844 do_if:
1845 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1846 tcc_error("memory full (ifdef)");
1847 *s1->ifdef_stack_ptr++ = c;
1848 goto test_skip;
1849 case TOK_ELSE:
1850 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1851 tcc_error("#else without matching #if");
1852 if (s1->ifdef_stack_ptr[-1] & 2)
1853 tcc_error("#else after #else");
1854 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1855 goto test_else;
1856 case TOK_ELIF:
1857 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1858 tcc_error("#elif without matching #if");
1859 c = s1->ifdef_stack_ptr[-1];
1860 if (c > 1)
1861 tcc_error("#elif after #else");
1862 /* last #if/#elif expression was true: we skip */
1863 if (c == 1)
1864 goto skip;
1865 c = expr_preprocess();
1866 s1->ifdef_stack_ptr[-1] = c;
1867 test_else:
1868 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1869 file->ifndef_macro = 0;
1870 test_skip:
1871 if (!(c & 1)) {
1872 skip:
1873 preprocess_skip();
1874 is_bof = 0;
1875 goto redo;
1877 break;
1878 case TOK_ENDIF:
1879 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1880 tcc_error("#endif without matching #if");
1881 s1->ifdef_stack_ptr--;
1882 /* '#ifndef macro' was at the start of file. Now we check if
1883 an '#endif' is exactly at the end of file */
1884 if (file->ifndef_macro &&
1885 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1886 file->ifndef_macro_saved = file->ifndef_macro;
1887 /* need to set to zero to avoid false matches if another
1888 #ifndef at middle of file */
1889 file->ifndef_macro = 0;
1890 while (tok != TOK_LINEFEED)
1891 next_nomacro();
1892 tok_flags |= TOK_FLAG_ENDIF;
1893 goto the_end;
1895 break;
1896 case TOK_PPNUM:
1897 n = strtoul((char*)tokc.str.data, &q, 10);
1898 goto _line_num;
1899 case TOK_LINE:
1900 next();
1901 if (tok != TOK_CINT)
1902 _line_err:
1903 tcc_error("wrong #line format");
1904 n = tokc.i;
1905 _line_num:
1906 next();
1907 if (tok != TOK_LINEFEED) {
1908 if (tok == TOK_STR)
1909 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1910 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1911 break;
1912 else
1913 goto _line_err;
1914 --n;
1916 if (file->fd > 0)
1917 total_lines += file->line_num - n;
1918 file->line_num = n;
1919 if (s1->do_debug)
1920 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1921 break;
1922 case TOK_ERROR:
1923 case TOK_WARNING:
1924 c = tok;
1925 ch = file->buf_ptr[0];
1926 skip_spaces();
1927 q = buf;
1928 while (ch != '\n' && ch != CH_EOF) {
1929 if ((q - buf) < sizeof(buf) - 1)
1930 *q++ = ch;
1931 if (ch == '\\') {
1932 if (handle_stray_noerror() == 0)
1933 --q;
1934 } else
1935 inp();
1937 *q = '\0';
1938 if (c == TOK_ERROR)
1939 tcc_error("#error %s", buf);
1940 else
1941 tcc_warning("#warning %s", buf);
1942 break;
1943 case TOK_PRAGMA:
1944 pragma_parse(s1);
1945 break;
1946 case TOK_LINEFEED:
1947 goto the_end;
1948 default:
1949 /* ignore gas line comment in an 'S' file. */
1950 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1951 goto ignore;
1952 if (tok == '!' && is_bof)
1953 /* '!' is ignored at beginning to allow C scripts. */
1954 goto ignore;
1955 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1956 ignore:
1957 file->buf_ptr = parse_line_comment(file->buf_ptr);
1958 goto the_end;
1960 /* ignore other preprocess commands or #! for C scripts */
1961 while (tok != TOK_LINEFEED)
1962 next_nomacro();
1963 the_end:
1964 parse_flags = saved_parse_flags;
1967 /* evaluate escape codes in a string. */
1968 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1970 int c, n;
1971 const uint8_t *p;
1973 p = buf;
1974 for(;;) {
1975 c = *p;
1976 if (c == '\0')
1977 break;
1978 if (c == '\\') {
1979 p++;
1980 /* escape */
1981 c = *p;
1982 switch(c) {
1983 case '0': case '1': case '2': case '3':
1984 case '4': case '5': case '6': case '7':
1985 /* at most three octal digits */
1986 n = c - '0';
1987 p++;
1988 c = *p;
1989 if (isoct(c)) {
1990 n = n * 8 + c - '0';
1991 p++;
1992 c = *p;
1993 if (isoct(c)) {
1994 n = n * 8 + c - '0';
1995 p++;
1998 c = n;
1999 goto add_char_nonext;
2000 case 'x':
2001 case 'u':
2002 case 'U':
2003 p++;
2004 n = 0;
2005 for(;;) {
2006 c = *p;
2007 if (c >= 'a' && c <= 'f')
2008 c = c - 'a' + 10;
2009 else if (c >= 'A' && c <= 'F')
2010 c = c - 'A' + 10;
2011 else if (isnum(c))
2012 c = c - '0';
2013 else
2014 break;
2015 n = n * 16 + c;
2016 p++;
2018 c = n;
2019 goto add_char_nonext;
2020 case 'a':
2021 c = '\a';
2022 break;
2023 case 'b':
2024 c = '\b';
2025 break;
2026 case 'f':
2027 c = '\f';
2028 break;
2029 case 'n':
2030 c = '\n';
2031 break;
2032 case 'r':
2033 c = '\r';
2034 break;
2035 case 't':
2036 c = '\t';
2037 break;
2038 case 'v':
2039 c = '\v';
2040 break;
2041 case 'e':
2042 if (!gnu_ext)
2043 goto invalid_escape;
2044 c = 27;
2045 break;
2046 case '\'':
2047 case '\"':
2048 case '\\':
2049 case '?':
2050 break;
2051 default:
2052 invalid_escape:
2053 if (c >= '!' && c <= '~')
2054 tcc_warning("unknown escape sequence: \'\\%c\'", c);
2055 else
2056 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
2057 break;
2060 p++;
2061 add_char_nonext:
2062 if (!is_long)
2063 cstr_ccat(outstr, c);
2064 else
2065 cstr_wccat(outstr, c);
2067 /* add a trailing '\0' */
2068 if (!is_long)
2069 cstr_ccat(outstr, '\0');
2070 else
2071 cstr_wccat(outstr, '\0');
2074 static void parse_string(const char *s, int len)
2076 uint8_t buf[1000], *p = buf;
2077 int is_long, sep;
2079 if ((is_long = *s == 'L'))
2080 ++s, --len;
2081 sep = *s++;
2082 len -= 2;
2083 if (len >= sizeof buf)
2084 p = tcc_malloc(len + 1);
2085 memcpy(p, s, len);
2086 p[len] = 0;
2088 cstr_reset(&tokcstr);
2089 parse_escape_string(&tokcstr, p, is_long);
2090 if (p != buf)
2091 tcc_free(p);
2093 if (sep == '\'') {
2094 int char_size;
2095 /* XXX: make it portable */
2096 if (!is_long)
2097 char_size = 1;
2098 else
2099 char_size = sizeof(nwchar_t);
2100 if (tokcstr.size <= char_size)
2101 tcc_error("empty character constant");
2102 if (tokcstr.size > 2 * char_size)
2103 tcc_warning("multi-character character constant");
2104 if (!is_long) {
2105 tokc.i = *(int8_t *)tokcstr.data;
2106 tok = TOK_CCHAR;
2107 } else {
2108 tokc.i = *(nwchar_t *)tokcstr.data;
2109 tok = TOK_LCHAR;
2111 } else {
2112 tokc.str.size = tokcstr.size;
2113 tokc.str.data = tokcstr.data;
2114 if (!is_long)
2115 tok = TOK_STR;
2116 else
2117 tok = TOK_LSTR;
2121 /* we use 64 bit numbers */
2122 #define BN_SIZE 2
2124 /* bn = (bn << shift) | or_val */
2125 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2127 int i;
2128 unsigned int v;
2129 for(i=0;i<BN_SIZE;i++) {
2130 v = bn[i];
2131 bn[i] = (v << shift) | or_val;
2132 or_val = v >> (32 - shift);
2136 static void bn_zero(unsigned int *bn)
2138 int i;
2139 for(i=0;i<BN_SIZE;i++) {
2140 bn[i] = 0;
2144 /* parse number in null terminated string 'p' and return it in the
2145 current token */
2146 static void parse_number(const char *p)
2148 int b, t, shift, frac_bits, s, exp_val, ch;
2149 char *q;
2150 unsigned int bn[BN_SIZE];
2151 double d;
2153 /* number */
2154 q = token_buf;
2155 ch = *p++;
2156 t = ch;
2157 ch = *p++;
2158 *q++ = t;
2159 b = 10;
2160 if (t == '.') {
2161 goto float_frac_parse;
2162 } else if (t == '0') {
2163 if (ch == 'x' || ch == 'X') {
2164 q--;
2165 ch = *p++;
2166 b = 16;
2167 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2168 q--;
2169 ch = *p++;
2170 b = 2;
2173 /* parse all digits. cannot check octal numbers at this stage
2174 because of floating point constants */
2175 while (1) {
2176 if (ch >= 'a' && ch <= 'f')
2177 t = ch - 'a' + 10;
2178 else if (ch >= 'A' && ch <= 'F')
2179 t = ch - 'A' + 10;
2180 else if (isnum(ch))
2181 t = ch - '0';
2182 else
2183 break;
2184 if (t >= b)
2185 break;
2186 if (q >= token_buf + STRING_MAX_SIZE) {
2187 num_too_long:
2188 tcc_error("number too long");
2190 *q++ = ch;
2191 ch = *p++;
2193 if (ch == '.' ||
2194 ((ch == 'e' || ch == 'E') && b == 10) ||
2195 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2196 if (b != 10) {
2197 /* NOTE: strtox should support that for hexa numbers, but
2198 non ISOC99 libcs do not support it, so we prefer to do
2199 it by hand */
2200 /* hexadecimal or binary floats */
2201 /* XXX: handle overflows */
2202 *q = '\0';
2203 if (b == 16)
2204 shift = 4;
2205 else
2206 shift = 1;
2207 bn_zero(bn);
2208 q = token_buf;
2209 while (1) {
2210 t = *q++;
2211 if (t == '\0') {
2212 break;
2213 } else if (t >= 'a') {
2214 t = t - 'a' + 10;
2215 } else if (t >= 'A') {
2216 t = t - 'A' + 10;
2217 } else {
2218 t = t - '0';
2220 bn_lshift(bn, shift, t);
2222 frac_bits = 0;
2223 if (ch == '.') {
2224 ch = *p++;
2225 while (1) {
2226 t = ch;
2227 if (t >= 'a' && t <= 'f') {
2228 t = t - 'a' + 10;
2229 } else if (t >= 'A' && t <= 'F') {
2230 t = t - 'A' + 10;
2231 } else if (t >= '0' && t <= '9') {
2232 t = t - '0';
2233 } else {
2234 break;
2236 if (t >= b)
2237 tcc_error("invalid digit");
2238 bn_lshift(bn, shift, t);
2239 frac_bits += shift;
2240 ch = *p++;
2243 if (ch != 'p' && ch != 'P')
2244 expect("exponent");
2245 ch = *p++;
2246 s = 1;
2247 exp_val = 0;
2248 if (ch == '+') {
2249 ch = *p++;
2250 } else if (ch == '-') {
2251 s = -1;
2252 ch = *p++;
2254 if (ch < '0' || ch > '9')
2255 expect("exponent digits");
2256 while (ch >= '0' && ch <= '9') {
2257 exp_val = exp_val * 10 + ch - '0';
2258 ch = *p++;
2260 exp_val = exp_val * s;
2262 /* now we can generate the number */
2263 /* XXX: should patch directly float number */
2264 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2265 d = ldexp(d, exp_val - frac_bits);
2266 t = toup(ch);
2267 if (t == 'F') {
2268 ch = *p++;
2269 tok = TOK_CFLOAT;
2270 /* float : should handle overflow */
2271 tokc.f = (float)d;
2272 } else if (t == 'L') {
2273 ch = *p++;
2274 #ifdef TCC_TARGET_PE
2275 tok = TOK_CDOUBLE;
2276 tokc.d = d;
2277 #else
2278 tok = TOK_CLDOUBLE;
2279 /* XXX: not large enough */
2280 tokc.ld = (long double)d;
2281 #endif
2282 } else {
2283 tok = TOK_CDOUBLE;
2284 tokc.d = d;
2286 } else {
2287 /* decimal floats */
2288 if (ch == '.') {
2289 if (q >= token_buf + STRING_MAX_SIZE)
2290 goto num_too_long;
2291 *q++ = ch;
2292 ch = *p++;
2293 float_frac_parse:
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 if (ch == 'e' || ch == 'E') {
2302 if (q >= token_buf + STRING_MAX_SIZE)
2303 goto num_too_long;
2304 *q++ = ch;
2305 ch = *p++;
2306 if (ch == '-' || ch == '+') {
2307 if (q >= token_buf + STRING_MAX_SIZE)
2308 goto num_too_long;
2309 *q++ = ch;
2310 ch = *p++;
2312 if (ch < '0' || ch > '9')
2313 expect("exponent digits");
2314 while (ch >= '0' && ch <= '9') {
2315 if (q >= token_buf + STRING_MAX_SIZE)
2316 goto num_too_long;
2317 *q++ = ch;
2318 ch = *p++;
2321 *q = '\0';
2322 t = toup(ch);
2323 errno = 0;
2324 if (t == 'F') {
2325 ch = *p++;
2326 tok = TOK_CFLOAT;
2327 tokc.f = strtof(token_buf, NULL);
2328 } else if (t == 'L') {
2329 ch = *p++;
2330 #ifdef TCC_TARGET_PE
2331 tok = TOK_CDOUBLE;
2332 tokc.d = strtod(token_buf, NULL);
2333 #else
2334 tok = TOK_CLDOUBLE;
2335 tokc.ld = strtold(token_buf, NULL);
2336 #endif
2337 } else {
2338 tok = TOK_CDOUBLE;
2339 tokc.d = strtod(token_buf, NULL);
2342 } else {
2343 unsigned long long n, n1;
2344 int lcount, ucount, must_64bit;
2345 const char *p1;
2347 /* integer number */
2348 *q = '\0';
2349 q = token_buf;
2350 if (b == 10 && *q == '0') {
2351 b = 8;
2352 q++;
2354 n = 0;
2355 while(1) {
2356 t = *q++;
2357 /* no need for checks except for base 10 / 8 errors */
2358 if (t == '\0')
2359 break;
2360 else if (t >= 'a')
2361 t = t - 'a' + 10;
2362 else if (t >= 'A')
2363 t = t - 'A' + 10;
2364 else
2365 t = t - '0';
2366 if (t >= b)
2367 tcc_error("invalid digit");
2368 n1 = n;
2369 n = n * b + t;
2370 /* detect overflow */
2371 /* XXX: this test is not reliable */
2372 if (n < n1)
2373 tcc_error("integer constant overflow");
2376 /* Determine the characteristics (unsigned and/or 64bit) the type of
2377 the constant must have according to the constant suffix(es) */
2378 lcount = ucount = must_64bit = 0;
2379 p1 = p;
2380 for(;;) {
2381 t = toup(ch);
2382 if (t == 'L') {
2383 if (lcount >= 2)
2384 tcc_error("three 'l's in integer constant");
2385 if (lcount && *(p - 1) != ch)
2386 tcc_error("incorrect integer suffix: %s", p1);
2387 lcount++;
2388 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2389 if (lcount == 2)
2390 #endif
2391 must_64bit = 1;
2392 ch = *p++;
2393 } else if (t == 'U') {
2394 if (ucount >= 1)
2395 tcc_error("two 'u's in integer constant");
2396 ucount++;
2397 ch = *p++;
2398 } else {
2399 break;
2403 /* Whether 64 bits are needed to hold the constant's value */
2404 if (n & 0xffffffff00000000LL || must_64bit) {
2405 tok = TOK_CLLONG;
2406 n1 = n >> 32;
2407 } else {
2408 tok = TOK_CINT;
2409 n1 = n;
2412 /* Whether type must be unsigned to hold the constant's value */
2413 if (ucount || ((n1 >> 31) && (b != 10))) {
2414 if (tok == TOK_CLLONG)
2415 tok = TOK_CULLONG;
2416 else
2417 tok = TOK_CUINT;
2418 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2419 } else if (n1 >> 31) {
2420 if (tok == TOK_CINT)
2421 tok = TOK_CLLONG;
2422 else
2423 tcc_error("integer constant overflow");
2426 tokc.i = n;
2428 if (ch)
2429 tcc_error("invalid number\n");
2433 #define PARSE2(c1, tok1, c2, tok2) \
2434 case c1: \
2435 PEEKC(c, p); \
2436 if (c == c2) { \
2437 p++; \
2438 tok = tok2; \
2439 } else { \
2440 tok = tok1; \
2442 break;
2444 /* return next token without macro substitution */
2445 static inline void next_nomacro1(void)
2447 int t, c, is_long, len;
2448 TokenSym *ts;
2449 uint8_t *p, *p1;
2450 unsigned int h;
2452 p = file->buf_ptr;
2453 redo_no_start:
2454 c = *p;
2455 switch(c) {
2456 case ' ':
2457 case '\t':
2458 tok = c;
2459 p++;
2460 if (parse_flags & PARSE_FLAG_SPACES)
2461 goto keep_tok_flags;
2462 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2463 ++p;
2464 goto redo_no_start;
2465 case '\f':
2466 case '\v':
2467 case '\r':
2468 p++;
2469 goto redo_no_start;
2470 case '\\':
2471 /* first look if it is in fact an end of buffer */
2472 c = handle_stray1(p);
2473 p = file->buf_ptr;
2474 if (c == '\\')
2475 goto parse_simple;
2476 if (c != CH_EOF)
2477 goto redo_no_start;
2479 TCCState *s1 = tcc_state;
2480 if ((parse_flags & PARSE_FLAG_LINEFEED)
2481 && !(tok_flags & TOK_FLAG_EOF)) {
2482 tok_flags |= TOK_FLAG_EOF;
2483 tok = TOK_LINEFEED;
2484 goto keep_tok_flags;
2485 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2486 tok = TOK_EOF;
2487 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2488 tcc_error("missing #endif");
2489 } else if (s1->include_stack_ptr == s1->include_stack) {
2490 /* no include left : end of file. */
2491 tok = TOK_EOF;
2492 } else {
2493 tok_flags &= ~TOK_FLAG_EOF;
2494 /* pop include file */
2496 /* test if previous '#endif' was after a #ifdef at
2497 start of file */
2498 if (tok_flags & TOK_FLAG_ENDIF) {
2499 #ifdef INC_DEBUG
2500 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2501 #endif
2502 search_cached_include(s1, file->filename, 1)
2503 ->ifndef_macro = file->ifndef_macro_saved;
2504 tok_flags &= ~TOK_FLAG_ENDIF;
2507 /* add end of include file debug info */
2508 if (tcc_state->do_debug) {
2509 put_stabd(N_EINCL, 0, 0);
2511 /* pop include stack */
2512 tcc_close();
2513 s1->include_stack_ptr--;
2514 p = file->buf_ptr;
2515 goto redo_no_start;
2518 break;
2520 case '\n':
2521 file->line_num++;
2522 tok_flags |= TOK_FLAG_BOL;
2523 p++;
2524 maybe_newline:
2525 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2526 goto redo_no_start;
2527 tok = TOK_LINEFEED;
2528 goto keep_tok_flags;
2530 case '#':
2531 /* XXX: simplify */
2532 PEEKC(c, p);
2533 if ((tok_flags & TOK_FLAG_BOL) &&
2534 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2535 file->buf_ptr = p;
2536 preprocess(tok_flags & TOK_FLAG_BOF);
2537 p = file->buf_ptr;
2538 goto maybe_newline;
2539 } else {
2540 if (c == '#') {
2541 p++;
2542 tok = TOK_TWOSHARPS;
2543 } else {
2544 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2545 p = parse_line_comment(p - 1);
2546 goto redo_no_start;
2547 } else {
2548 tok = '#';
2552 break;
2554 /* dollar is allowed to start identifiers when not parsing asm */
2555 case '$':
2556 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2557 || (parse_flags & PARSE_FLAG_ASM_FILE))
2558 goto parse_simple;
2560 case 'a': case 'b': case 'c': case 'd':
2561 case 'e': case 'f': case 'g': case 'h':
2562 case 'i': case 'j': case 'k': case 'l':
2563 case 'm': case 'n': case 'o': case 'p':
2564 case 'q': case 'r': case 's': case 't':
2565 case 'u': case 'v': case 'w': case 'x':
2566 case 'y': case 'z':
2567 case 'A': case 'B': case 'C': case 'D':
2568 case 'E': case 'F': case 'G': case 'H':
2569 case 'I': case 'J': case 'K':
2570 case 'M': case 'N': case 'O': case 'P':
2571 case 'Q': case 'R': case 'S': case 'T':
2572 case 'U': case 'V': case 'W': case 'X':
2573 case 'Y': case 'Z':
2574 case '_':
2575 parse_ident_fast:
2576 p1 = p;
2577 h = TOK_HASH_INIT;
2578 h = TOK_HASH_FUNC(h, c);
2579 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2580 h = TOK_HASH_FUNC(h, c);
2581 len = p - p1;
2582 if (c != '\\') {
2583 TokenSym **pts;
2585 /* fast case : no stray found, so we have the full token
2586 and we have already hashed it */
2587 h &= (TOK_HASH_SIZE - 1);
2588 pts = &hash_ident[h];
2589 for(;;) {
2590 ts = *pts;
2591 if (!ts)
2592 break;
2593 if (ts->len == len && !memcmp(ts->str, p1, len))
2594 goto token_found;
2595 pts = &(ts->hash_next);
2597 ts = tok_alloc_new(pts, (char *) p1, len);
2598 token_found: ;
2599 } else {
2600 /* slower case */
2601 cstr_reset(&tokcstr);
2602 cstr_cat(&tokcstr, p1, len);
2603 p--;
2604 PEEKC(c, p);
2605 parse_ident_slow:
2606 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2608 cstr_ccat(&tokcstr, c);
2609 PEEKC(c, p);
2611 ts = tok_alloc(tokcstr.data, tokcstr.size);
2613 tok = ts->tok;
2614 break;
2615 case 'L':
2616 t = p[1];
2617 if (t != '\\' && t != '\'' && t != '\"') {
2618 /* fast case */
2619 goto parse_ident_fast;
2620 } else {
2621 PEEKC(c, p);
2622 if (c == '\'' || c == '\"') {
2623 is_long = 1;
2624 goto str_const;
2625 } else {
2626 cstr_reset(&tokcstr);
2627 cstr_ccat(&tokcstr, 'L');
2628 goto parse_ident_slow;
2631 break;
2633 case '0': case '1': case '2': case '3':
2634 case '4': case '5': case '6': case '7':
2635 case '8': case '9':
2636 t = c;
2637 PEEKC(c, p);
2638 /* after the first digit, accept digits, alpha, '.' or sign if
2639 prefixed by 'eEpP' */
2640 parse_num:
2641 cstr_reset(&tokcstr);
2642 for(;;) {
2643 cstr_ccat(&tokcstr, t);
2644 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2645 || c == '.'
2646 || ((c == '+' || c == '-')
2647 && (((t == 'e' || t == 'E')
2648 && !(parse_flags & PARSE_FLAG_ASM_FILE
2649 /* 0xe+1 is 3 tokens in asm */
2650 && ((char*)tokcstr.data)[0] == '0'
2651 && toup(((char*)tokcstr.data)[1]) == 'X'))
2652 || t == 'p' || t == 'P'))))
2653 break;
2654 t = c;
2655 PEEKC(c, p);
2657 /* We add a trailing '\0' to ease parsing */
2658 cstr_ccat(&tokcstr, '\0');
2659 tokc.str.size = tokcstr.size;
2660 tokc.str.data = tokcstr.data;
2661 tok = TOK_PPNUM;
2662 break;
2664 case '.':
2665 /* special dot handling because it can also start a number */
2666 PEEKC(c, p);
2667 if (isnum(c)) {
2668 t = '.';
2669 goto parse_num;
2670 } else if ((parse_flags & PARSE_FLAG_ASM_FILE)
2671 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
2672 *--p = c = '.';
2673 goto parse_ident_fast;
2674 } else if (c == '.') {
2675 PEEKC(c, p);
2676 if (c == '.') {
2677 p++;
2678 tok = TOK_DOTS;
2679 } else {
2680 *--p = '.'; /* may underflow into file->unget[] */
2681 tok = '.';
2683 } else {
2684 tok = '.';
2686 break;
2687 case '\'':
2688 case '\"':
2689 is_long = 0;
2690 str_const:
2691 cstr_reset(&tokcstr);
2692 if (is_long)
2693 cstr_ccat(&tokcstr, 'L');
2694 cstr_ccat(&tokcstr, c);
2695 p = parse_pp_string(p, c, &tokcstr);
2696 cstr_ccat(&tokcstr, c);
2697 cstr_ccat(&tokcstr, '\0');
2698 tokc.str.size = tokcstr.size;
2699 tokc.str.data = tokcstr.data;
2700 tok = TOK_PPSTR;
2701 break;
2703 case '<':
2704 PEEKC(c, p);
2705 if (c == '=') {
2706 p++;
2707 tok = TOK_LE;
2708 } else if (c == '<') {
2709 PEEKC(c, p);
2710 if (c == '=') {
2711 p++;
2712 tok = TOK_A_SHL;
2713 } else {
2714 tok = TOK_SHL;
2716 } else {
2717 tok = TOK_LT;
2719 break;
2720 case '>':
2721 PEEKC(c, p);
2722 if (c == '=') {
2723 p++;
2724 tok = TOK_GE;
2725 } else if (c == '>') {
2726 PEEKC(c, p);
2727 if (c == '=') {
2728 p++;
2729 tok = TOK_A_SAR;
2730 } else {
2731 tok = TOK_SAR;
2733 } else {
2734 tok = TOK_GT;
2736 break;
2738 case '&':
2739 PEEKC(c, p);
2740 if (c == '&') {
2741 p++;
2742 tok = TOK_LAND;
2743 } else if (c == '=') {
2744 p++;
2745 tok = TOK_A_AND;
2746 } else {
2747 tok = '&';
2749 break;
2751 case '|':
2752 PEEKC(c, p);
2753 if (c == '|') {
2754 p++;
2755 tok = TOK_LOR;
2756 } else if (c == '=') {
2757 p++;
2758 tok = TOK_A_OR;
2759 } else {
2760 tok = '|';
2762 break;
2764 case '+':
2765 PEEKC(c, p);
2766 if (c == '+') {
2767 p++;
2768 tok = TOK_INC;
2769 } else if (c == '=') {
2770 p++;
2771 tok = TOK_A_ADD;
2772 } else {
2773 tok = '+';
2775 break;
2777 case '-':
2778 PEEKC(c, p);
2779 if (c == '-') {
2780 p++;
2781 tok = TOK_DEC;
2782 } else if (c == '=') {
2783 p++;
2784 tok = TOK_A_SUB;
2785 } else if (c == '>') {
2786 p++;
2787 tok = TOK_ARROW;
2788 } else {
2789 tok = '-';
2791 break;
2793 PARSE2('!', '!', '=', TOK_NE)
2794 PARSE2('=', '=', '=', TOK_EQ)
2795 PARSE2('*', '*', '=', TOK_A_MUL)
2796 PARSE2('%', '%', '=', TOK_A_MOD)
2797 PARSE2('^', '^', '=', TOK_A_XOR)
2799 /* comments or operator */
2800 case '/':
2801 PEEKC(c, p);
2802 if (c == '*') {
2803 p = parse_comment(p);
2804 /* comments replaced by a blank */
2805 tok = ' ';
2806 goto keep_tok_flags;
2807 } else if (c == '/') {
2808 p = parse_line_comment(p);
2809 tok = ' ';
2810 goto keep_tok_flags;
2811 } else if (c == '=') {
2812 p++;
2813 tok = TOK_A_DIV;
2814 } else {
2815 tok = '/';
2817 break;
2819 /* simple tokens */
2820 case '(':
2821 case ')':
2822 case '[':
2823 case ']':
2824 case '{':
2825 case '}':
2826 case ',':
2827 case ';':
2828 case ':':
2829 case '?':
2830 case '~':
2831 case '@': /* only used in assembler */
2832 parse_simple:
2833 tok = c;
2834 p++;
2835 break;
2836 default:
2837 if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
2838 goto parse_ident_fast;
2839 if (parse_flags & PARSE_FLAG_ASM_FILE)
2840 goto parse_simple;
2841 tcc_error("unrecognized character \\x%02x", c);
2842 break;
2844 tok_flags = 0;
2845 keep_tok_flags:
2846 file->buf_ptr = p;
2847 #if defined(PARSE_DEBUG)
2848 printf("token = %s\n", get_tok_str(tok, &tokc));
2849 #endif
2852 /* return next token without macro substitution. Can read input from
2853 macro_ptr buffer */
2854 static void next_nomacro_spc(void)
2856 if (macro_ptr) {
2857 redo:
2858 tok = *macro_ptr;
2859 if (tok) {
2860 TOK_GET(&tok, &macro_ptr, &tokc);
2861 if (tok == TOK_LINENUM) {
2862 file->line_num = tokc.i;
2863 goto redo;
2866 } else {
2867 next_nomacro1();
2871 ST_FUNC void next_nomacro(void)
2873 do {
2874 next_nomacro_spc();
2875 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2879 static void macro_subst(
2880 TokenString *tok_str,
2881 Sym **nested_list,
2882 const int *macro_str,
2883 int can_read_stream
2886 /* substitute arguments in replacement lists in macro_str by the values in
2887 args (field d) and return allocated string */
2888 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2890 int t, t0, t1, spc;
2891 const int *st;
2892 Sym *s;
2893 CValue cval;
2894 TokenString str;
2895 CString cstr;
2897 tok_str_new(&str);
2898 t0 = t1 = 0;
2899 while(1) {
2900 TOK_GET(&t, &macro_str, &cval);
2901 if (!t)
2902 break;
2903 if (t == '#') {
2904 /* stringize */
2905 TOK_GET(&t, &macro_str, &cval);
2906 if (!t)
2907 goto bad_stringy;
2908 s = sym_find2(args, t);
2909 if (s) {
2910 cstr_new(&cstr);
2911 cstr_ccat(&cstr, '\"');
2912 st = s->d;
2913 spc = 0;
2914 while (*st) {
2915 TOK_GET(&t, &st, &cval);
2916 if (t != TOK_PLCHLDR
2917 && t != TOK_NOSUBST
2918 && 0 == check_space(t, &spc)) {
2919 const char *s = get_tok_str(t, &cval);
2920 while (*s) {
2921 if (t == TOK_PPSTR && *s != '\'')
2922 add_char(&cstr, *s);
2923 else
2924 cstr_ccat(&cstr, *s);
2925 ++s;
2929 cstr.size -= spc;
2930 cstr_ccat(&cstr, '\"');
2931 cstr_ccat(&cstr, '\0');
2932 #ifdef PP_DEBUG
2933 printf("\nstringize: <%s>\n", (char *)cstr.data);
2934 #endif
2935 /* add string */
2936 cval.str.size = cstr.size;
2937 cval.str.data = cstr.data;
2938 tok_str_add2(&str, TOK_PPSTR, &cval);
2939 cstr_free(&cstr);
2940 } else {
2941 bad_stringy:
2942 expect("macro parameter after '#'");
2944 } else if (t >= TOK_IDENT) {
2945 s = sym_find2(args, t);
2946 if (s) {
2947 int l0 = str.len;
2948 st = s->d;
2949 /* if '##' is present before or after, no arg substitution */
2950 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2951 /* special case for var arg macros : ## eats the ','
2952 if empty VA_ARGS variable. */
2953 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2954 if (*st == 0) {
2955 /* suppress ',' '##' */
2956 str.len -= 2;
2957 } else {
2958 /* suppress '##' and add variable */
2959 str.len--;
2960 goto add_var;
2962 } else {
2963 for(;;) {
2964 int t1;
2965 TOK_GET(&t1, &st, &cval);
2966 if (!t1)
2967 break;
2968 tok_str_add2(&str, t1, &cval);
2972 } else {
2973 add_var:
2974 /* NOTE: the stream cannot be read when macro
2975 substituing an argument */
2976 macro_subst(&str, nested_list, st, 0);
2978 if (str.len == l0) /* exanded to empty string */
2979 tok_str_add(&str, TOK_PLCHLDR);
2980 } else {
2981 tok_str_add(&str, t);
2983 } else {
2984 tok_str_add2(&str, t, &cval);
2986 t0 = t1, t1 = t;
2988 tok_str_add(&str, 0);
2989 return str.str;
2992 static char const ab_month_name[12][4] =
2994 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2995 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2998 /* peek or read [ws_str == NULL] next token from function macro call,
2999 walking up macro levels up to the file if necessary */
3000 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
3002 int t;
3003 const int *p;
3004 Sym *sa;
3006 for (;;) {
3007 if (macro_ptr) {
3008 p = macro_ptr, t = *p;
3009 if (ws_str) {
3010 while (is_space(t) || TOK_LINEFEED == t)
3011 tok_str_add(ws_str, t), t = *++p;
3013 if (t == 0 && can_read_stream) {
3014 end_macro();
3015 /* also, end of scope for nested defined symbol */
3016 sa = *nested_list;
3017 while (sa && sa->v == 0)
3018 sa = sa->prev;
3019 if (sa)
3020 sa->v = 0;
3021 continue;
3023 } else {
3024 ch = handle_eob();
3025 if (ws_str) {
3026 while (is_space(ch) || ch == '\n' || ch == '/') {
3027 if (ch == '/') {
3028 int c;
3029 uint8_t *p = file->buf_ptr;
3030 PEEKC(c, p);
3031 if (c == '*') {
3032 p = parse_comment(p);
3033 file->buf_ptr = p - 1;
3034 } else if (c == '/') {
3035 p = parse_line_comment(p);
3036 file->buf_ptr = p - 1;
3037 } else
3038 break;
3039 ch = ' ';
3041 tok_str_add(ws_str, ch);
3042 cinp();
3045 t = ch;
3048 if (ws_str)
3049 return t;
3050 next_nomacro_spc();
3051 return tok;
3055 /* do macro substitution of current token with macro 's' and add
3056 result to (tok_str,tok_len). 'nested_list' is the list of all
3057 macros we got inside to avoid recursing. Return non zero if no
3058 substitution needs to be done */
3059 static int macro_subst_tok(
3060 TokenString *tok_str,
3061 Sym **nested_list,
3062 Sym *s,
3063 int can_read_stream)
3065 Sym *args, *sa, *sa1;
3066 int parlevel, *mstr, t, t1, spc;
3067 TokenString str;
3068 char *cstrval;
3069 CValue cval;
3070 CString cstr;
3071 char buf[32];
3073 /* if symbol is a macro, prepare substitution */
3074 /* special macros */
3075 if (tok == TOK___LINE__) {
3076 snprintf(buf, sizeof(buf), "%d", file->line_num);
3077 cstrval = buf;
3078 t1 = TOK_PPNUM;
3079 goto add_cstr1;
3080 } else if (tok == TOK___FILE__) {
3081 cstrval = file->filename;
3082 goto add_cstr;
3083 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3084 time_t ti;
3085 struct tm *tm;
3087 time(&ti);
3088 tm = localtime(&ti);
3089 if (tok == TOK___DATE__) {
3090 snprintf(buf, sizeof(buf), "%s %2d %d",
3091 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3092 } else {
3093 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3094 tm->tm_hour, tm->tm_min, tm->tm_sec);
3096 cstrval = buf;
3097 add_cstr:
3098 t1 = TOK_STR;
3099 add_cstr1:
3100 cstr_new(&cstr);
3101 cstr_cat(&cstr, cstrval, 0);
3102 cval.str.size = cstr.size;
3103 cval.str.data = cstr.data;
3104 tok_str_add2(tok_str, t1, &cval);
3105 cstr_free(&cstr);
3106 } else {
3107 int saved_parse_flags = parse_flags;
3109 mstr = s->d;
3110 if (s->type.t == MACRO_FUNC) {
3111 /* whitespace between macro name and argument list */
3112 TokenString ws_str;
3113 tok_str_new(&ws_str);
3115 spc = 0;
3116 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3117 | PARSE_FLAG_ACCEPT_STRAYS;
3119 /* get next token from argument stream */
3120 t = next_argstream(nested_list, can_read_stream, &ws_str);
3121 if (t != '(') {
3122 /* not a macro substitution after all, restore the
3123 * macro token plus all whitespace we've read.
3124 * whitespace is intentionally not merged to preserve
3125 * newlines. */
3126 parse_flags = saved_parse_flags;
3127 tok_str_add(tok_str, tok);
3128 if (parse_flags & PARSE_FLAG_SPACES) {
3129 int i;
3130 for (i = 0; i < ws_str.len; i++)
3131 tok_str_add(tok_str, ws_str.str[i]);
3133 tok_str_free(ws_str.str);
3134 return 0;
3135 } else {
3136 tok_str_free(ws_str.str);
3138 next_nomacro(); /* eat '(' */
3140 /* argument macro */
3141 args = NULL;
3142 sa = s->next;
3143 /* NOTE: empty args are allowed, except if no args */
3144 for(;;) {
3145 do {
3146 next_argstream(nested_list, can_read_stream, NULL);
3147 } while (is_space(tok) || TOK_LINEFEED == tok);
3148 empty_arg:
3149 /* handle '()' case */
3150 if (!args && !sa && tok == ')')
3151 break;
3152 if (!sa)
3153 tcc_error("macro '%s' used with too many args",
3154 get_tok_str(s->v, 0));
3155 tok_str_new(&str);
3156 parlevel = spc = 0;
3157 /* NOTE: non zero sa->t indicates VA_ARGS */
3158 while ((parlevel > 0 ||
3159 (tok != ')' &&
3160 (tok != ',' || sa->type.t)))) {
3161 if (tok == TOK_EOF || tok == 0)
3162 break;
3163 if (tok == '(')
3164 parlevel++;
3165 else if (tok == ')')
3166 parlevel--;
3167 if (tok == TOK_LINEFEED)
3168 tok = ' ';
3169 if (!check_space(tok, &spc))
3170 tok_str_add2(&str, tok, &tokc);
3171 next_argstream(nested_list, can_read_stream, NULL);
3173 if (parlevel)
3174 expect(")");
3175 str.len -= spc;
3176 tok_str_add(&str, 0);
3177 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3178 sa1->d = str.str;
3179 sa = sa->next;
3180 if (tok == ')') {
3181 /* special case for gcc var args: add an empty
3182 var arg argument if it is omitted */
3183 if (sa && sa->type.t && gnu_ext)
3184 goto empty_arg;
3185 break;
3187 if (tok != ',')
3188 expect(",");
3190 if (sa) {
3191 tcc_error("macro '%s' used with too few args",
3192 get_tok_str(s->v, 0));
3195 parse_flags = saved_parse_flags;
3197 /* now subst each arg */
3198 mstr = macro_arg_subst(nested_list, mstr, args);
3199 /* free memory */
3200 sa = args;
3201 while (sa) {
3202 sa1 = sa->prev;
3203 tok_str_free(sa->d);
3204 sym_free(sa);
3205 sa = sa1;
3209 sym_push2(nested_list, s->v, 0, 0);
3210 parse_flags = saved_parse_flags;
3211 macro_subst(tok_str, nested_list, mstr, can_read_stream | 2);
3213 /* pop nested defined symbol */
3214 sa1 = *nested_list;
3215 *nested_list = sa1->prev;
3216 sym_free(sa1);
3217 if (mstr != s->d)
3218 tok_str_free(mstr);
3220 return 0;
3223 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3225 CString cstr;
3226 int n, ret = 1;
3228 cstr_new(&cstr);
3229 if (t1 != TOK_PLCHLDR)
3230 cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3231 n = cstr.size;
3232 if (t2 != TOK_PLCHLDR)
3233 cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3234 cstr_ccat(&cstr, '\0');
3236 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3237 memcpy(file->buffer, cstr.data, cstr.size);
3238 for (;;) {
3239 next_nomacro1();
3240 if (0 == *file->buf_ptr)
3241 break;
3242 if (is_space(tok))
3243 continue;
3244 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
3245 " preprocessing token", n, cstr.data, (char*)cstr.data + n);
3246 ret = 0;
3247 break;
3249 tcc_close();
3250 //printf("paste <%s>\n", (char*)cstr.data);
3251 cstr_free(&cstr);
3252 return ret;
3255 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3256 return the resulting string (which must be freed). */
3257 static inline int *macro_twosharps(const int *ptr0)
3259 int t;
3260 CValue cval;
3261 TokenString macro_str1;
3262 int start_of_nosubsts = -1;
3263 const int *ptr;
3265 /* we search the first '##' */
3266 for (ptr = ptr0;;) {
3267 TOK_GET(&t, &ptr, &cval);
3268 if (t == TOK_TWOSHARPS)
3269 break;
3270 if (t == 0)
3271 return NULL;
3274 tok_str_new(&macro_str1);
3276 //tok_print(" $$$", ptr0);
3277 for (ptr = ptr0;;) {
3278 TOK_GET(&t, &ptr, &cval);
3279 if (t == 0)
3280 break;
3281 if (t == TOK_TWOSHARPS)
3282 continue;
3283 while (*ptr == TOK_TWOSHARPS) {
3284 int t1; CValue cv1;
3285 /* given 'a##b', remove nosubsts preceding 'a' */
3286 if (start_of_nosubsts >= 0)
3287 macro_str1.len = start_of_nosubsts;
3288 /* given 'a##b', remove nosubsts preceding 'b' */
3289 while ((t1 = *++ptr) == TOK_NOSUBST)
3291 if (t1 && t1 != TOK_TWOSHARPS) {
3292 TOK_GET(&t1, &ptr, &cv1);
3293 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3294 if (paste_tokens(t, &cval, t1, &cv1)) {
3295 t = tok, cval = tokc;
3296 } else {
3297 tok_str_add2(&macro_str1, t, &cval);
3298 t = t1, cval = cv1;
3303 if (t == TOK_NOSUBST) {
3304 if (start_of_nosubsts < 0)
3305 start_of_nosubsts = macro_str1.len;
3306 } else {
3307 start_of_nosubsts = -1;
3309 tok_str_add2(&macro_str1, t, &cval);
3311 tok_str_add(&macro_str1, 0);
3312 //tok_print(" ###", macro_str1.str);
3313 return macro_str1.str;
3316 /* do macro substitution of macro_str and add result to
3317 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3318 inside to avoid recursing. */
3319 static void macro_subst(
3320 TokenString *tok_str,
3321 Sym **nested_list,
3322 const int *macro_str,
3323 int can_read_stream
3326 Sym *s;
3327 const int *ptr;
3328 int t, spc, nosubst;
3329 CValue cval;
3330 int *macro_str1 = NULL;
3332 /* first scan for '##' operator handling */
3333 ptr = macro_str;
3334 spc = nosubst = 0;
3336 /* first scan for '##' operator handling */
3337 if (can_read_stream & 1) {
3338 macro_str1 = macro_twosharps(ptr);
3339 if (macro_str1)
3340 ptr = macro_str1;
3343 while (1) {
3344 TOK_GET(&t, &ptr, &cval);
3345 if (t == 0)
3346 break;
3348 if (t >= TOK_IDENT && 0 == nosubst) {
3349 s = define_find(t);
3350 if (s == NULL)
3351 goto no_subst;
3353 /* if nested substitution, do nothing */
3354 if (sym_find2(*nested_list, t)) {
3355 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3356 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3357 goto no_subst;
3361 TokenString str;
3362 str.str = (int*)ptr;
3363 begin_macro(&str, 2);
3365 tok = t;
3366 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3368 if (str.alloc == 3) {
3369 /* already finished by reading function macro arguments */
3370 break;
3373 ptr = macro_ptr;
3374 end_macro ();
3377 spc = (tok_str->len &&
3378 is_space(tok_last(tok_str->str,
3379 tok_str->str + tok_str->len)));
3381 } else {
3383 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3384 tcc_error("stray '\\' in program");
3386 no_subst:
3387 if (!check_space(t, &spc))
3388 tok_str_add2(tok_str, t, &cval);
3389 nosubst = 0;
3390 if (t == TOK_NOSUBST)
3391 nosubst = 1;
3394 if (macro_str1)
3395 tok_str_free(macro_str1);
3399 /* return next token with macro substitution */
3400 ST_FUNC void next(void)
3402 redo:
3403 if (parse_flags & PARSE_FLAG_SPACES)
3404 next_nomacro_spc();
3405 else
3406 next_nomacro();
3408 if (macro_ptr) {
3409 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3410 /* discard preprocessor markers */
3411 goto redo;
3412 } else if (tok == 0) {
3413 /* end of macro or unget token string */
3414 end_macro();
3415 goto redo;
3417 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3418 Sym *s;
3419 /* if reading from file, try to substitute macros */
3420 s = define_find(tok);
3421 if (s) {
3422 Sym *nested_list = NULL;
3423 tokstr_buf.len = 0;
3424 macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
3425 tok_str_add(&tokstr_buf, 0);
3426 begin_macro(&tokstr_buf, 2);
3427 goto redo;
3430 /* convert preprocessor tokens into C tokens */
3431 if (tok == TOK_PPNUM) {
3432 if (parse_flags & PARSE_FLAG_TOK_NUM)
3433 parse_number((char *)tokc.str.data);
3434 } else if (tok == TOK_PPSTR) {
3435 if (parse_flags & PARSE_FLAG_TOK_STR)
3436 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3440 /* push back current token and set current token to 'last_tok'. Only
3441 identifier case handled for labels. */
3442 ST_INLN void unget_tok(int last_tok)
3445 TokenString *str = tok_str_alloc();
3446 tok_str_add2(str, tok, &tokc);
3447 tok_str_add(str, 0);
3448 begin_macro(str, 1);
3449 tok = last_tok;
3452 ST_FUNC void preprocess_init(TCCState *s1)
3454 s1->include_stack_ptr = s1->include_stack;
3455 /* XXX: move that before to avoid having to initialize
3456 file->ifdef_stack_ptr ? */
3457 s1->ifdef_stack_ptr = s1->ifdef_stack;
3458 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3459 pp_once++;
3461 pvtop = vtop = vstack - 1;
3462 s1->pack_stack[0] = 0;
3463 s1->pack_stack_ptr = s1->pack_stack;
3465 isidnum_table['$' - CH_EOF] =
3466 s1->dollars_in_identifiers ? IS_ID : 0;
3467 isidnum_table['.' - CH_EOF] =
3468 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
3471 ST_FUNC void preprocess_new(void)
3473 int i, c;
3474 const char *p, *r;
3476 /* init isid table */
3477 for(i = CH_EOF; i<128; i++)
3478 isidnum_table[i - CH_EOF]
3479 = is_space(i) ? IS_SPC
3480 : isid(i) ? IS_ID
3481 : isnum(i) ? IS_NUM
3482 : 0;
3484 for(i = 128; i<256; i++)
3485 isidnum_table[i - CH_EOF] = IS_ID;
3487 /* init allocators */
3488 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
3489 tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
3490 tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
3492 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3493 cstr_new(&cstr_buf);
3494 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
3495 tok_str_new(&tokstr_buf);
3496 tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
3498 tok_ident = TOK_IDENT;
3499 p = tcc_keywords;
3500 while (*p) {
3501 r = p;
3502 for(;;) {
3503 c = *r++;
3504 if (c == '\0')
3505 break;
3507 tok_alloc(p, r - p - 1);
3508 p = r;
3512 ST_FUNC void preprocess_delete(void)
3514 int i, n;
3516 /* free -D and compiler defines */
3517 free_defines(NULL);
3519 /* cleanup from error/setjmp */
3520 while (macro_stack)
3521 end_macro();
3522 macro_ptr = NULL;
3524 /* free tokens */
3525 n = tok_ident - TOK_IDENT;
3526 for(i = 0; i < n; i++)
3527 tal_free(toksym_alloc, table_ident[i]);
3528 tcc_free(table_ident);
3529 table_ident = NULL;
3531 /* free static buffers */
3532 cstr_free(&tokcstr);
3533 cstr_free(&cstr_buf);
3534 tok_str_free(tokstr_buf.str);
3536 /* free allocators */
3537 tal_delete(toksym_alloc);
3538 toksym_alloc = NULL;
3539 tal_delete(tokstr_alloc);
3540 tokstr_alloc = NULL;
3541 tal_delete(cstr_alloc);
3542 cstr_alloc = NULL;
3545 /* ------------------------------------------------------------------------- */
3546 /* tcc -E [-P[1]] [-dD} support */
3548 static void tok_print(const char *msg, const int *str)
3550 FILE *fp;
3551 int t;
3552 CValue cval;
3554 fp = tcc_state->ppfp;
3555 if (!fp || !tcc_state->dflag)
3556 fp = stdout;
3558 fprintf(fp, "%s ", msg);
3559 while (str) {
3560 TOK_GET(&t, &str, &cval);
3561 if (!t)
3562 break;
3563 fprintf(fp,"%s", get_tok_str(t, &cval));
3565 fprintf(fp, "\n");
3568 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3570 int d = f->line_num - f->line_ref;
3571 if (s1->dflag & 4)
3572 return;
3573 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3574 if (level == 0 && f->line_ref && d) {
3575 d = 1;
3576 goto simple;
3578 } else if (level == 0 && f->line_ref && d < 8) {
3579 simple:
3580 while (d > 0)
3581 fputs("\n", s1->ppfp), --d;
3582 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3583 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3584 } else {
3585 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3586 level > 0 ? " 1" : level < 0 ? " 2" : "");
3588 f->line_ref = f->line_num;
3591 static void define_print(TCCState *s1, int v)
3593 FILE *fp;
3594 Sym *s;
3596 s = define_find(v);
3597 if (NULL == s || NULL == s->d)
3598 return;
3600 fp = s1->ppfp;
3601 fprintf(fp, "#define %s", get_tok_str(v, NULL));
3602 if (s->type.t == MACRO_FUNC) {
3603 Sym *a = s->next;
3604 fprintf(fp,"(");
3605 if (a)
3606 for (;;) {
3607 fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3608 if (!(a = a->next))
3609 break;
3610 fprintf(fp,",");
3612 fprintf(fp,")");
3614 tok_print("", s->d);
3617 static void pp_debug_defines(TCCState *s1)
3619 int v, t;
3620 const char *vs;
3621 FILE *fp;
3623 t = pp_debug_tok;
3624 if (t == 0)
3625 return;
3627 file->line_num--;
3628 pp_line(s1, file, 0);
3629 file->line_ref = ++file->line_num;
3631 fp = s1->ppfp;
3632 v = pp_debug_symv;
3633 vs = get_tok_str(v, NULL);
3634 if (t == TOK_DEFINE) {
3635 define_print(s1, v);
3636 } else if (t == TOK_UNDEF) {
3637 fprintf(fp, "#undef %s\n", vs);
3638 } else if (t == TOK_push_macro) {
3639 fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3640 } else if (t == TOK_pop_macro) {
3641 fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3643 pp_debug_tok = 0;
3646 static void pp_debug_builtins(TCCState *s1)
3648 int v;
3649 for (v = TOK_IDENT; v < tok_ident; ++v)
3650 define_print(s1, v);
3653 /* Add a space between tokens a and b to avoid unwanted textual pasting */
3654 static int pp_need_space(int a, int b)
3656 return 'E' == a ? '+' == b || '-' == b
3657 : '+' == a ? TOK_INC == b || '+' == b
3658 : '-' == a ? TOK_DEC == b || '-' == b
3659 : a >= TOK_IDENT ? b >= TOK_IDENT
3660 : 0;
3663 /* maybe hex like 0x1e */
3664 static int pp_check_he0xE(int t, const char *p)
3666 if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
3667 return 'E';
3668 return t;
3671 /* Preprocess the current file */
3672 ST_FUNC int tcc_preprocess(TCCState *s1)
3674 BufferedFile **iptr;
3675 int token_seen, spcs, level;
3676 const char *p;
3677 Sym *define_start;
3679 preprocess_init(s1);
3680 ch = file->buf_ptr[0];
3681 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3682 parse_flags = PARSE_FLAG_PREPROCESS
3683 | (parse_flags & PARSE_FLAG_ASM_FILE)
3684 | PARSE_FLAG_LINEFEED
3685 | PARSE_FLAG_SPACES
3686 | PARSE_FLAG_ACCEPT_STRAYS
3688 define_start = define_stack;
3690 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3691 capability to compile and run itself, provided all numbers are
3692 given as decimals. tcc -E -P10 will do. */
3693 if (s1->Pflag == 1 + 10)
3694 parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
3696 #ifdef PP_BENCH
3697 /* for PP benchmarks */
3698 do next(); while (tok != TOK_EOF); return 0;
3699 #endif
3701 if (s1->dflag & 1) {
3702 pp_debug_builtins(s1);
3703 s1->dflag &= ~1;
3706 token_seen = TOK_LINEFEED, spcs = 0;
3707 pp_line(s1, file, 0);
3709 for (;;) {
3710 iptr = s1->include_stack_ptr;
3711 next();
3712 if (tok == TOK_EOF)
3713 break;
3714 level = s1->include_stack_ptr - iptr;
3715 if (level) {
3716 if (level > 0)
3717 pp_line(s1, *iptr, 0);
3718 pp_line(s1, file, level);
3721 if (s1->dflag) {
3722 pp_debug_defines(s1);
3723 if (s1->dflag & 4)
3724 continue;
3727 if (token_seen == TOK_LINEFEED) {
3728 if (tok == ' ') {
3729 ++spcs;
3730 continue;
3732 if (tok == TOK_LINEFEED) {
3733 spcs = 0;
3734 continue;
3736 pp_line(s1, file, 0);
3737 } else if (tok == TOK_LINEFEED) {
3738 ++file->line_ref;
3739 } else {
3740 spcs = pp_need_space(token_seen, tok);
3743 while (spcs)
3744 fputs(" ", s1->ppfp), --spcs;
3745 fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
3746 token_seen = pp_check_he0xE(tok, p);;
3748 /* reset define stack, but keep -D and built-ins */
3749 free_defines(define_start);
3750 return 0;
3753 /* ------------------------------------------------------------------------- */