tcc -E: add one space in cases: tiny solution
[tinycc.git] / tccpp.c
blobd4ad514243d105169fce34b246c277a17ec02101
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 if (path == 0) continue;
1787 pstrcpy(buf1, sizeof(buf1), path);
1788 pstrcat(buf1, sizeof(buf1), "/");
1791 pstrcat(buf1, sizeof(buf1), buf);
1792 e = search_cached_include(s1, buf1, 0);
1793 if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
1794 /* no need to parse the include because the 'ifndef macro'
1795 is defined (or had #pragma once) */
1796 #ifdef INC_DEBUG
1797 printf("%s: skipping cached %s\n", file->filename, buf1);
1798 #endif
1799 goto include_done;
1802 if (tcc_open(s1, buf1) < 0)
1803 continue;
1805 file->include_next_index = i + 1;
1806 #ifdef INC_DEBUG
1807 printf("%s: including %s\n", file->prev->filename, file->filename);
1808 #endif
1809 /* update target deps */
1810 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1811 tcc_strdup(buf1));
1812 /* push current file in stack */
1813 ++s1->include_stack_ptr;
1814 /* add include file debug info */
1815 if (s1->do_debug)
1816 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1817 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1818 ch = file->buf_ptr[0];
1819 goto the_end;
1821 tcc_error("include file '%s' not found", buf);
1822 include_done:
1823 break;
1824 case TOK_IFNDEF:
1825 c = 1;
1826 goto do_ifdef;
1827 case TOK_IF:
1828 c = expr_preprocess();
1829 goto do_if;
1830 case TOK_IFDEF:
1831 c = 0;
1832 do_ifdef:
1833 next_nomacro();
1834 if (tok < TOK_IDENT)
1835 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1836 if (is_bof) {
1837 if (c) {
1838 #ifdef INC_DEBUG
1839 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1840 #endif
1841 file->ifndef_macro = tok;
1844 c = (define_find(tok) != 0) ^ c;
1845 do_if:
1846 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1847 tcc_error("memory full (ifdef)");
1848 *s1->ifdef_stack_ptr++ = c;
1849 goto test_skip;
1850 case TOK_ELSE:
1851 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1852 tcc_error("#else without matching #if");
1853 if (s1->ifdef_stack_ptr[-1] & 2)
1854 tcc_error("#else after #else");
1855 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1856 goto test_else;
1857 case TOK_ELIF:
1858 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1859 tcc_error("#elif without matching #if");
1860 c = s1->ifdef_stack_ptr[-1];
1861 if (c > 1)
1862 tcc_error("#elif after #else");
1863 /* last #if/#elif expression was true: we skip */
1864 if (c == 1)
1865 goto skip;
1866 c = expr_preprocess();
1867 s1->ifdef_stack_ptr[-1] = c;
1868 test_else:
1869 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1870 file->ifndef_macro = 0;
1871 test_skip:
1872 if (!(c & 1)) {
1873 skip:
1874 preprocess_skip();
1875 is_bof = 0;
1876 goto redo;
1878 break;
1879 case TOK_ENDIF:
1880 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1881 tcc_error("#endif without matching #if");
1882 s1->ifdef_stack_ptr--;
1883 /* '#ifndef macro' was at the start of file. Now we check if
1884 an '#endif' is exactly at the end of file */
1885 if (file->ifndef_macro &&
1886 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1887 file->ifndef_macro_saved = file->ifndef_macro;
1888 /* need to set to zero to avoid false matches if another
1889 #ifndef at middle of file */
1890 file->ifndef_macro = 0;
1891 while (tok != TOK_LINEFEED)
1892 next_nomacro();
1893 tok_flags |= TOK_FLAG_ENDIF;
1894 goto the_end;
1896 break;
1897 case TOK_PPNUM:
1898 n = strtoul((char*)tokc.str.data, &q, 10);
1899 goto _line_num;
1900 case TOK_LINE:
1901 next();
1902 if (tok != TOK_CINT)
1903 _line_err:
1904 tcc_error("wrong #line format");
1905 n = tokc.i;
1906 _line_num:
1907 next();
1908 if (tok != TOK_LINEFEED) {
1909 if (tok == TOK_STR)
1910 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1911 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1912 break;
1913 else
1914 goto _line_err;
1915 --n;
1917 if (file->fd > 0)
1918 total_lines += file->line_num - n;
1919 file->line_num = n;
1920 if (s1->do_debug)
1921 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1922 break;
1923 case TOK_ERROR:
1924 case TOK_WARNING:
1925 c = tok;
1926 ch = file->buf_ptr[0];
1927 skip_spaces();
1928 q = buf;
1929 while (ch != '\n' && ch != CH_EOF) {
1930 if ((q - buf) < sizeof(buf) - 1)
1931 *q++ = ch;
1932 if (ch == '\\') {
1933 if (handle_stray_noerror() == 0)
1934 --q;
1935 } else
1936 inp();
1938 *q = '\0';
1939 if (c == TOK_ERROR)
1940 tcc_error("#error %s", buf);
1941 else
1942 tcc_warning("#warning %s", buf);
1943 break;
1944 case TOK_PRAGMA:
1945 pragma_parse(s1);
1946 break;
1947 case TOK_LINEFEED:
1948 goto the_end;
1949 default:
1950 /* ignore gas line comment in an 'S' file. */
1951 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1952 goto ignore;
1953 if (tok == '!' && is_bof)
1954 /* '!' is ignored at beginning to allow C scripts. */
1955 goto ignore;
1956 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1957 ignore:
1958 file->buf_ptr = parse_line_comment(file->buf_ptr);
1959 goto the_end;
1961 /* ignore other preprocess commands or #! for C scripts */
1962 while (tok != TOK_LINEFEED)
1963 next_nomacro();
1964 the_end:
1965 parse_flags = saved_parse_flags;
1968 /* evaluate escape codes in a string. */
1969 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1971 int c, n;
1972 const uint8_t *p;
1974 p = buf;
1975 for(;;) {
1976 c = *p;
1977 if (c == '\0')
1978 break;
1979 if (c == '\\') {
1980 p++;
1981 /* escape */
1982 c = *p;
1983 switch(c) {
1984 case '0': case '1': case '2': case '3':
1985 case '4': case '5': case '6': case '7':
1986 /* at most three octal digits */
1987 n = c - '0';
1988 p++;
1989 c = *p;
1990 if (isoct(c)) {
1991 n = n * 8 + c - '0';
1992 p++;
1993 c = *p;
1994 if (isoct(c)) {
1995 n = n * 8 + c - '0';
1996 p++;
1999 c = n;
2000 goto add_char_nonext;
2001 case 'x':
2002 case 'u':
2003 case 'U':
2004 p++;
2005 n = 0;
2006 for(;;) {
2007 c = *p;
2008 if (c >= 'a' && c <= 'f')
2009 c = c - 'a' + 10;
2010 else if (c >= 'A' && c <= 'F')
2011 c = c - 'A' + 10;
2012 else if (isnum(c))
2013 c = c - '0';
2014 else
2015 break;
2016 n = n * 16 + c;
2017 p++;
2019 c = n;
2020 goto add_char_nonext;
2021 case 'a':
2022 c = '\a';
2023 break;
2024 case 'b':
2025 c = '\b';
2026 break;
2027 case 'f':
2028 c = '\f';
2029 break;
2030 case 'n':
2031 c = '\n';
2032 break;
2033 case 'r':
2034 c = '\r';
2035 break;
2036 case 't':
2037 c = '\t';
2038 break;
2039 case 'v':
2040 c = '\v';
2041 break;
2042 case 'e':
2043 if (!gnu_ext)
2044 goto invalid_escape;
2045 c = 27;
2046 break;
2047 case '\'':
2048 case '\"':
2049 case '\\':
2050 case '?':
2051 break;
2052 default:
2053 invalid_escape:
2054 if (c >= '!' && c <= '~')
2055 tcc_warning("unknown escape sequence: \'\\%c\'", c);
2056 else
2057 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
2058 break;
2061 p++;
2062 add_char_nonext:
2063 if (!is_long)
2064 cstr_ccat(outstr, c);
2065 else
2066 cstr_wccat(outstr, c);
2068 /* add a trailing '\0' */
2069 if (!is_long)
2070 cstr_ccat(outstr, '\0');
2071 else
2072 cstr_wccat(outstr, '\0');
2075 static void parse_string(const char *s, int len)
2077 uint8_t buf[1000], *p = buf;
2078 int is_long, sep;
2080 if ((is_long = *s == 'L'))
2081 ++s, --len;
2082 sep = *s++;
2083 len -= 2;
2084 if (len >= sizeof buf)
2085 p = tcc_malloc(len + 1);
2086 memcpy(p, s, len);
2087 p[len] = 0;
2089 cstr_reset(&tokcstr);
2090 parse_escape_string(&tokcstr, p, is_long);
2091 if (p != buf)
2092 tcc_free(p);
2094 if (sep == '\'') {
2095 int char_size;
2096 /* XXX: make it portable */
2097 if (!is_long)
2098 char_size = 1;
2099 else
2100 char_size = sizeof(nwchar_t);
2101 if (tokcstr.size <= char_size)
2102 tcc_error("empty character constant");
2103 if (tokcstr.size > 2 * char_size)
2104 tcc_warning("multi-character character constant");
2105 if (!is_long) {
2106 tokc.i = *(int8_t *)tokcstr.data;
2107 tok = TOK_CCHAR;
2108 } else {
2109 tokc.i = *(nwchar_t *)tokcstr.data;
2110 tok = TOK_LCHAR;
2112 } else {
2113 tokc.str.size = tokcstr.size;
2114 tokc.str.data = tokcstr.data;
2115 if (!is_long)
2116 tok = TOK_STR;
2117 else
2118 tok = TOK_LSTR;
2122 /* we use 64 bit numbers */
2123 #define BN_SIZE 2
2125 /* bn = (bn << shift) | or_val */
2126 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2128 int i;
2129 unsigned int v;
2130 for(i=0;i<BN_SIZE;i++) {
2131 v = bn[i];
2132 bn[i] = (v << shift) | or_val;
2133 or_val = v >> (32 - shift);
2137 static void bn_zero(unsigned int *bn)
2139 int i;
2140 for(i=0;i<BN_SIZE;i++) {
2141 bn[i] = 0;
2145 /* parse number in null terminated string 'p' and return it in the
2146 current token */
2147 static void parse_number(const char *p)
2149 int b, t, shift, frac_bits, s, exp_val, ch;
2150 char *q;
2151 unsigned int bn[BN_SIZE];
2152 double d;
2154 /* number */
2155 q = token_buf;
2156 ch = *p++;
2157 t = ch;
2158 ch = *p++;
2159 *q++ = t;
2160 b = 10;
2161 if (t == '.') {
2162 goto float_frac_parse;
2163 } else if (t == '0') {
2164 if (ch == 'x' || ch == 'X') {
2165 q--;
2166 ch = *p++;
2167 b = 16;
2168 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2169 q--;
2170 ch = *p++;
2171 b = 2;
2174 /* parse all digits. cannot check octal numbers at this stage
2175 because of floating point constants */
2176 while (1) {
2177 if (ch >= 'a' && ch <= 'f')
2178 t = ch - 'a' + 10;
2179 else if (ch >= 'A' && ch <= 'F')
2180 t = ch - 'A' + 10;
2181 else if (isnum(ch))
2182 t = ch - '0';
2183 else
2184 break;
2185 if (t >= b)
2186 break;
2187 if (q >= token_buf + STRING_MAX_SIZE) {
2188 num_too_long:
2189 tcc_error("number too long");
2191 *q++ = ch;
2192 ch = *p++;
2194 if (ch == '.' ||
2195 ((ch == 'e' || ch == 'E') && b == 10) ||
2196 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2197 if (b != 10) {
2198 /* NOTE: strtox should support that for hexa numbers, but
2199 non ISOC99 libcs do not support it, so we prefer to do
2200 it by hand */
2201 /* hexadecimal or binary floats */
2202 /* XXX: handle overflows */
2203 *q = '\0';
2204 if (b == 16)
2205 shift = 4;
2206 else
2207 shift = 1;
2208 bn_zero(bn);
2209 q = token_buf;
2210 while (1) {
2211 t = *q++;
2212 if (t == '\0') {
2213 break;
2214 } else if (t >= 'a') {
2215 t = t - 'a' + 10;
2216 } else if (t >= 'A') {
2217 t = t - 'A' + 10;
2218 } else {
2219 t = t - '0';
2221 bn_lshift(bn, shift, t);
2223 frac_bits = 0;
2224 if (ch == '.') {
2225 ch = *p++;
2226 while (1) {
2227 t = ch;
2228 if (t >= 'a' && t <= 'f') {
2229 t = t - 'a' + 10;
2230 } else if (t >= 'A' && t <= 'F') {
2231 t = t - 'A' + 10;
2232 } else if (t >= '0' && t <= '9') {
2233 t = t - '0';
2234 } else {
2235 break;
2237 if (t >= b)
2238 tcc_error("invalid digit");
2239 bn_lshift(bn, shift, t);
2240 frac_bits += shift;
2241 ch = *p++;
2244 if (ch != 'p' && ch != 'P')
2245 expect("exponent");
2246 ch = *p++;
2247 s = 1;
2248 exp_val = 0;
2249 if (ch == '+') {
2250 ch = *p++;
2251 } else if (ch == '-') {
2252 s = -1;
2253 ch = *p++;
2255 if (ch < '0' || ch > '9')
2256 expect("exponent digits");
2257 while (ch >= '0' && ch <= '9') {
2258 exp_val = exp_val * 10 + ch - '0';
2259 ch = *p++;
2261 exp_val = exp_val * s;
2263 /* now we can generate the number */
2264 /* XXX: should patch directly float number */
2265 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2266 d = ldexp(d, exp_val - frac_bits);
2267 t = toup(ch);
2268 if (t == 'F') {
2269 ch = *p++;
2270 tok = TOK_CFLOAT;
2271 /* float : should handle overflow */
2272 tokc.f = (float)d;
2273 } else if (t == 'L') {
2274 ch = *p++;
2275 #ifdef TCC_TARGET_PE
2276 tok = TOK_CDOUBLE;
2277 tokc.d = d;
2278 #else
2279 tok = TOK_CLDOUBLE;
2280 /* XXX: not large enough */
2281 tokc.ld = (long double)d;
2282 #endif
2283 } else {
2284 tok = TOK_CDOUBLE;
2285 tokc.d = d;
2287 } else {
2288 /* decimal floats */
2289 if (ch == '.') {
2290 if (q >= token_buf + STRING_MAX_SIZE)
2291 goto num_too_long;
2292 *q++ = ch;
2293 ch = *p++;
2294 float_frac_parse:
2295 while (ch >= '0' && ch <= '9') {
2296 if (q >= token_buf + STRING_MAX_SIZE)
2297 goto num_too_long;
2298 *q++ = ch;
2299 ch = *p++;
2302 if (ch == 'e' || ch == 'E') {
2303 if (q >= token_buf + STRING_MAX_SIZE)
2304 goto num_too_long;
2305 *q++ = ch;
2306 ch = *p++;
2307 if (ch == '-' || ch == '+') {
2308 if (q >= token_buf + STRING_MAX_SIZE)
2309 goto num_too_long;
2310 *q++ = ch;
2311 ch = *p++;
2313 if (ch < '0' || ch > '9')
2314 expect("exponent digits");
2315 while (ch >= '0' && ch <= '9') {
2316 if (q >= token_buf + STRING_MAX_SIZE)
2317 goto num_too_long;
2318 *q++ = ch;
2319 ch = *p++;
2322 *q = '\0';
2323 t = toup(ch);
2324 errno = 0;
2325 if (t == 'F') {
2326 ch = *p++;
2327 tok = TOK_CFLOAT;
2328 tokc.f = strtof(token_buf, NULL);
2329 } else if (t == 'L') {
2330 ch = *p++;
2331 #ifdef TCC_TARGET_PE
2332 tok = TOK_CDOUBLE;
2333 tokc.d = strtod(token_buf, NULL);
2334 #else
2335 tok = TOK_CLDOUBLE;
2336 tokc.ld = strtold(token_buf, NULL);
2337 #endif
2338 } else {
2339 tok = TOK_CDOUBLE;
2340 tokc.d = strtod(token_buf, NULL);
2343 } else {
2344 unsigned long long n, n1;
2345 int lcount, ucount, must_64bit;
2346 const char *p1;
2348 /* integer number */
2349 *q = '\0';
2350 q = token_buf;
2351 if (b == 10 && *q == '0') {
2352 b = 8;
2353 q++;
2355 n = 0;
2356 while(1) {
2357 t = *q++;
2358 /* no need for checks except for base 10 / 8 errors */
2359 if (t == '\0')
2360 break;
2361 else if (t >= 'a')
2362 t = t - 'a' + 10;
2363 else if (t >= 'A')
2364 t = t - 'A' + 10;
2365 else
2366 t = t - '0';
2367 if (t >= b)
2368 tcc_error("invalid digit");
2369 n1 = n;
2370 n = n * b + t;
2371 /* detect overflow */
2372 /* XXX: this test is not reliable */
2373 if (n < n1)
2374 tcc_error("integer constant overflow");
2377 /* Determine the characteristics (unsigned and/or 64bit) the type of
2378 the constant must have according to the constant suffix(es) */
2379 lcount = ucount = must_64bit = 0;
2380 p1 = p;
2381 for(;;) {
2382 t = toup(ch);
2383 if (t == 'L') {
2384 if (lcount >= 2)
2385 tcc_error("three 'l's in integer constant");
2386 if (lcount && *(p - 1) != ch)
2387 tcc_error("incorrect integer suffix: %s", p1);
2388 lcount++;
2389 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2390 if (lcount == 2)
2391 #endif
2392 must_64bit = 1;
2393 ch = *p++;
2394 } else if (t == 'U') {
2395 if (ucount >= 1)
2396 tcc_error("two 'u's in integer constant");
2397 ucount++;
2398 ch = *p++;
2399 } else {
2400 break;
2404 /* Whether 64 bits are needed to hold the constant's value */
2405 if (n & 0xffffffff00000000LL || must_64bit) {
2406 tok = TOK_CLLONG;
2407 n1 = n >> 32;
2408 } else {
2409 tok = TOK_CINT;
2410 n1 = n;
2413 /* Whether type must be unsigned to hold the constant's value */
2414 if (ucount || ((n1 >> 31) && (b != 10))) {
2415 if (tok == TOK_CLLONG)
2416 tok = TOK_CULLONG;
2417 else
2418 tok = TOK_CUINT;
2419 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2420 } else if (n1 >> 31) {
2421 if (tok == TOK_CINT)
2422 tok = TOK_CLLONG;
2423 else
2424 tcc_error("integer constant overflow");
2427 tokc.i = n;
2429 if (ch)
2430 tcc_error("invalid number\n");
2434 #define PARSE2(c1, tok1, c2, tok2) \
2435 case c1: \
2436 PEEKC(c, p); \
2437 if (c == c2) { \
2438 p++; \
2439 tok = tok2; \
2440 } else { \
2441 tok = tok1; \
2443 break;
2445 /* return next token without macro substitution */
2446 static inline void next_nomacro1(void)
2448 int t, c, is_long, len;
2449 TokenSym *ts;
2450 uint8_t *p, *p1;
2451 unsigned int h;
2453 p = file->buf_ptr;
2454 redo_no_start:
2455 c = *p;
2456 switch(c) {
2457 case ' ':
2458 case '\t':
2459 tok = c;
2460 p++;
2461 if (parse_flags & PARSE_FLAG_SPACES)
2462 goto keep_tok_flags;
2463 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2464 ++p;
2465 goto redo_no_start;
2466 case '\f':
2467 case '\v':
2468 case '\r':
2469 p++;
2470 goto redo_no_start;
2471 case '\\':
2472 /* first look if it is in fact an end of buffer */
2473 c = handle_stray1(p);
2474 p = file->buf_ptr;
2475 if (c == '\\')
2476 goto parse_simple;
2477 if (c != CH_EOF)
2478 goto redo_no_start;
2480 TCCState *s1 = tcc_state;
2481 if ((parse_flags & PARSE_FLAG_LINEFEED)
2482 && !(tok_flags & TOK_FLAG_EOF)) {
2483 tok_flags |= TOK_FLAG_EOF;
2484 tok = TOK_LINEFEED;
2485 goto keep_tok_flags;
2486 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2487 tok = TOK_EOF;
2488 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2489 tcc_error("missing #endif");
2490 } else if (s1->include_stack_ptr == s1->include_stack) {
2491 /* no include left : end of file. */
2492 tok = TOK_EOF;
2493 } else {
2494 tok_flags &= ~TOK_FLAG_EOF;
2495 /* pop include file */
2497 /* test if previous '#endif' was after a #ifdef at
2498 start of file */
2499 if (tok_flags & TOK_FLAG_ENDIF) {
2500 #ifdef INC_DEBUG
2501 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2502 #endif
2503 search_cached_include(s1, file->filename, 1)
2504 ->ifndef_macro = file->ifndef_macro_saved;
2505 tok_flags &= ~TOK_FLAG_ENDIF;
2508 /* add end of include file debug info */
2509 if (tcc_state->do_debug) {
2510 put_stabd(N_EINCL, 0, 0);
2512 /* pop include stack */
2513 tcc_close();
2514 s1->include_stack_ptr--;
2515 p = file->buf_ptr;
2516 goto redo_no_start;
2519 break;
2521 case '\n':
2522 file->line_num++;
2523 tok_flags |= TOK_FLAG_BOL;
2524 p++;
2525 maybe_newline:
2526 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2527 goto redo_no_start;
2528 tok = TOK_LINEFEED;
2529 goto keep_tok_flags;
2531 case '#':
2532 /* XXX: simplify */
2533 PEEKC(c, p);
2534 if ((tok_flags & TOK_FLAG_BOL) &&
2535 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2536 file->buf_ptr = p;
2537 preprocess(tok_flags & TOK_FLAG_BOF);
2538 p = file->buf_ptr;
2539 goto maybe_newline;
2540 } else {
2541 if (c == '#') {
2542 p++;
2543 tok = TOK_TWOSHARPS;
2544 } else {
2545 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2546 p = parse_line_comment(p - 1);
2547 goto redo_no_start;
2548 } else {
2549 tok = '#';
2553 break;
2555 /* dollar is allowed to start identifiers when not parsing asm */
2556 case '$':
2557 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2558 || (parse_flags & PARSE_FLAG_ASM_FILE))
2559 goto parse_simple;
2561 case 'a': case 'b': case 'c': case 'd':
2562 case 'e': case 'f': case 'g': case 'h':
2563 case 'i': case 'j': case 'k': case 'l':
2564 case 'm': case 'n': case 'o': case 'p':
2565 case 'q': case 'r': case 's': case 't':
2566 case 'u': case 'v': case 'w': case 'x':
2567 case 'y': case 'z':
2568 case 'A': case 'B': case 'C': case 'D':
2569 case 'E': case 'F': case 'G': case 'H':
2570 case 'I': case 'J': case 'K':
2571 case 'M': case 'N': case 'O': case 'P':
2572 case 'Q': case 'R': case 'S': case 'T':
2573 case 'U': case 'V': case 'W': case 'X':
2574 case 'Y': case 'Z':
2575 case '_':
2576 parse_ident_fast:
2577 p1 = p;
2578 h = TOK_HASH_INIT;
2579 h = TOK_HASH_FUNC(h, c);
2580 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2581 h = TOK_HASH_FUNC(h, c);
2582 len = p - p1;
2583 if (c != '\\') {
2584 TokenSym **pts;
2586 /* fast case : no stray found, so we have the full token
2587 and we have already hashed it */
2588 h &= (TOK_HASH_SIZE - 1);
2589 pts = &hash_ident[h];
2590 for(;;) {
2591 ts = *pts;
2592 if (!ts)
2593 break;
2594 if (ts->len == len && !memcmp(ts->str, p1, len))
2595 goto token_found;
2596 pts = &(ts->hash_next);
2598 ts = tok_alloc_new(pts, (char *) p1, len);
2599 token_found: ;
2600 } else {
2601 /* slower case */
2602 cstr_reset(&tokcstr);
2603 cstr_cat(&tokcstr, p1, len);
2604 p--;
2605 PEEKC(c, p);
2606 parse_ident_slow:
2607 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2609 cstr_ccat(&tokcstr, c);
2610 PEEKC(c, p);
2612 ts = tok_alloc(tokcstr.data, tokcstr.size);
2614 tok = ts->tok;
2615 break;
2616 case 'L':
2617 t = p[1];
2618 if (t != '\\' && t != '\'' && t != '\"') {
2619 /* fast case */
2620 goto parse_ident_fast;
2621 } else {
2622 PEEKC(c, p);
2623 if (c == '\'' || c == '\"') {
2624 is_long = 1;
2625 goto str_const;
2626 } else {
2627 cstr_reset(&tokcstr);
2628 cstr_ccat(&tokcstr, 'L');
2629 goto parse_ident_slow;
2632 break;
2634 case '0': case '1': case '2': case '3':
2635 case '4': case '5': case '6': case '7':
2636 case '8': case '9':
2637 cstr_reset(&tokcstr);
2638 /* after the first digit, accept digits, alpha, '.' or sign if
2639 prefixed by 'eEpP' */
2640 parse_num:
2641 for(;;) {
2642 t = c;
2643 cstr_ccat(&tokcstr, c);
2644 PEEKC(c, p);
2645 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2646 || c == '.'
2647 || ((c == '+' || c == '-')
2648 && (t == 'e' || t == 'E' || t == 'p' || t == 'P')
2649 && !(parse_flags & PARSE_FLAG_ASM_FILE)
2651 break;
2653 /* We add a trailing '\0' to ease parsing */
2654 cstr_ccat(&tokcstr, '\0');
2655 tokc.str.size = tokcstr.size;
2656 tokc.str.data = tokcstr.data;
2657 tok = TOK_PPNUM;
2658 break;
2660 case '.':
2661 /* special dot handling because it can also start a number */
2662 PEEKC(c, p);
2663 if (isnum(c)) {
2664 cstr_reset(&tokcstr);
2665 cstr_ccat(&tokcstr, '.');
2666 goto parse_num;
2667 } else if ((parse_flags & PARSE_FLAG_ASM_FILE)
2668 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
2669 *--p = c = '.';
2670 goto parse_ident_fast;
2671 } else if (c == '.') {
2672 PEEKC(c, p);
2673 if (c == '.') {
2674 p++;
2675 tok = TOK_DOTS;
2676 } else {
2677 *--p = '.'; /* may underflow into file->unget[] */
2678 tok = '.';
2680 } else {
2681 tok = '.';
2683 break;
2684 case '\'':
2685 case '\"':
2686 is_long = 0;
2687 str_const:
2688 cstr_reset(&tokcstr);
2689 if (is_long)
2690 cstr_ccat(&tokcstr, 'L');
2691 cstr_ccat(&tokcstr, c);
2692 p = parse_pp_string(p, c, &tokcstr);
2693 cstr_ccat(&tokcstr, c);
2694 cstr_ccat(&tokcstr, '\0');
2695 tokc.str.size = tokcstr.size;
2696 tokc.str.data = tokcstr.data;
2697 tok = TOK_PPSTR;
2698 break;
2700 case '<':
2701 PEEKC(c, p);
2702 if (c == '=') {
2703 p++;
2704 tok = TOK_LE;
2705 } else if (c == '<') {
2706 PEEKC(c, p);
2707 if (c == '=') {
2708 p++;
2709 tok = TOK_A_SHL;
2710 } else {
2711 tok = TOK_SHL;
2713 } else {
2714 tok = TOK_LT;
2716 break;
2717 case '>':
2718 PEEKC(c, p);
2719 if (c == '=') {
2720 p++;
2721 tok = TOK_GE;
2722 } else if (c == '>') {
2723 PEEKC(c, p);
2724 if (c == '=') {
2725 p++;
2726 tok = TOK_A_SAR;
2727 } else {
2728 tok = TOK_SAR;
2730 } else {
2731 tok = TOK_GT;
2733 break;
2735 case '&':
2736 PEEKC(c, p);
2737 if (c == '&') {
2738 p++;
2739 tok = TOK_LAND;
2740 } else if (c == '=') {
2741 p++;
2742 tok = TOK_A_AND;
2743 } else {
2744 tok = '&';
2746 break;
2748 case '|':
2749 PEEKC(c, p);
2750 if (c == '|') {
2751 p++;
2752 tok = TOK_LOR;
2753 } else if (c == '=') {
2754 p++;
2755 tok = TOK_A_OR;
2756 } else {
2757 tok = '|';
2759 break;
2761 case '+':
2762 PEEKC(c, p);
2763 if (c == '+') {
2764 p++;
2765 tok = TOK_INC;
2766 } else if (c == '=') {
2767 p++;
2768 tok = TOK_A_ADD;
2769 } else {
2770 tok = '+';
2772 break;
2774 case '-':
2775 PEEKC(c, p);
2776 if (c == '-') {
2777 p++;
2778 tok = TOK_DEC;
2779 } else if (c == '=') {
2780 p++;
2781 tok = TOK_A_SUB;
2782 } else if (c == '>') {
2783 p++;
2784 tok = TOK_ARROW;
2785 } else {
2786 tok = '-';
2788 break;
2790 PARSE2('!', '!', '=', TOK_NE)
2791 PARSE2('=', '=', '=', TOK_EQ)
2792 PARSE2('*', '*', '=', TOK_A_MUL)
2793 PARSE2('%', '%', '=', TOK_A_MOD)
2794 PARSE2('^', '^', '=', TOK_A_XOR)
2796 /* comments or operator */
2797 case '/':
2798 PEEKC(c, p);
2799 if (c == '*') {
2800 p = parse_comment(p);
2801 /* comments replaced by a blank */
2802 tok = ' ';
2803 goto keep_tok_flags;
2804 } else if (c == '/') {
2805 p = parse_line_comment(p);
2806 tok = ' ';
2807 goto keep_tok_flags;
2808 } else if (c == '=') {
2809 p++;
2810 tok = TOK_A_DIV;
2811 } else {
2812 tok = '/';
2814 break;
2816 /* simple tokens */
2817 case '(':
2818 case ')':
2819 case '[':
2820 case ']':
2821 case '{':
2822 case '}':
2823 case ',':
2824 case ';':
2825 case ':':
2826 case '?':
2827 case '~':
2828 case '@': /* only used in assembler */
2829 parse_simple:
2830 tok = c;
2831 p++;
2832 break;
2833 default:
2834 if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
2835 goto parse_ident_fast;
2836 if (parse_flags & PARSE_FLAG_ASM_FILE)
2837 goto parse_simple;
2838 tcc_error("unrecognized character \\x%02x", c);
2839 break;
2841 tok_flags = 0;
2842 keep_tok_flags:
2843 file->buf_ptr = p;
2844 #if defined(PARSE_DEBUG)
2845 printf("token = %s\n", get_tok_str(tok, &tokc));
2846 #endif
2849 /* return next token without macro substitution. Can read input from
2850 macro_ptr buffer */
2851 static void next_nomacro_spc(void)
2853 if (macro_ptr) {
2854 redo:
2855 tok = *macro_ptr;
2856 if (tok) {
2857 TOK_GET(&tok, &macro_ptr, &tokc);
2858 if (tok == TOK_LINENUM) {
2859 file->line_num = tokc.i;
2860 goto redo;
2863 } else {
2864 next_nomacro1();
2868 ST_FUNC void next_nomacro(void)
2870 do {
2871 next_nomacro_spc();
2872 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2876 static void macro_subst(
2877 TokenString *tok_str,
2878 Sym **nested_list,
2879 const int *macro_str,
2880 int can_read_stream
2883 /* substitute arguments in replacement lists in macro_str by the values in
2884 args (field d) and return allocated string */
2885 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2887 int t, t0, t1, spc;
2888 const int *st;
2889 Sym *s;
2890 CValue cval;
2891 TokenString str;
2892 CString cstr;
2894 tok_str_new(&str);
2895 t0 = t1 = 0;
2896 while(1) {
2897 TOK_GET(&t, &macro_str, &cval);
2898 if (!t)
2899 break;
2900 if (t == '#') {
2901 /* stringize */
2902 TOK_GET(&t, &macro_str, &cval);
2903 if (!t)
2904 goto bad_stringy;
2905 s = sym_find2(args, t);
2906 if (s) {
2907 cstr_new(&cstr);
2908 cstr_ccat(&cstr, '\"');
2909 st = s->d;
2910 spc = 0;
2911 while (*st) {
2912 TOK_GET(&t, &st, &cval);
2913 if (t != TOK_PLCHLDR
2914 && t != TOK_NOSUBST
2915 && 0 == check_space(t, &spc)) {
2916 const char *s = get_tok_str(t, &cval);
2917 while (*s) {
2918 if (t == TOK_PPSTR && *s != '\'')
2919 add_char(&cstr, *s);
2920 else
2921 cstr_ccat(&cstr, *s);
2922 ++s;
2926 cstr.size -= spc;
2927 cstr_ccat(&cstr, '\"');
2928 cstr_ccat(&cstr, '\0');
2929 #ifdef PP_DEBUG
2930 printf("\nstringize: <%s>\n", (char *)cstr.data);
2931 #endif
2932 /* add string */
2933 cval.str.size = cstr.size;
2934 cval.str.data = cstr.data;
2935 tok_str_add2(&str, TOK_PPSTR, &cval);
2936 cstr_free(&cstr);
2937 } else {
2938 bad_stringy:
2939 expect("macro parameter after '#'");
2941 } else if (t >= TOK_IDENT) {
2942 s = sym_find2(args, t);
2943 if (s) {
2944 int l0 = str.len;
2945 st = s->d;
2946 /* if '##' is present before or after, no arg substitution */
2947 if (*macro_str == TOK_TWOSHARPS || t1 == TOK_TWOSHARPS) {
2948 /* special case for var arg macros : ## eats the ','
2949 if empty VA_ARGS variable. */
2950 if (t1 == TOK_TWOSHARPS && t0 == ',' && gnu_ext && s->type.t) {
2951 if (*st == 0) {
2952 /* suppress ',' '##' */
2953 str.len -= 2;
2954 } else {
2955 /* suppress '##' and add variable */
2956 str.len--;
2957 goto add_var;
2959 } else {
2960 for(;;) {
2961 int t1;
2962 TOK_GET(&t1, &st, &cval);
2963 if (!t1)
2964 break;
2965 tok_str_add2(&str, t1, &cval);
2969 } else {
2970 add_var:
2971 /* NOTE: the stream cannot be read when macro
2972 substituing an argument */
2973 macro_subst(&str, nested_list, st, 0);
2975 if (str.len == l0) /* exanded to empty string */
2976 tok_str_add(&str, TOK_PLCHLDR);
2977 } else {
2978 tok_str_add(&str, t);
2980 } else {
2981 tok_str_add2(&str, t, &cval);
2983 t0 = t1, t1 = t;
2985 tok_str_add(&str, 0);
2986 return str.str;
2989 static char const ab_month_name[12][4] =
2991 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2992 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2995 /* peek or read [ws_str == NULL] next token from function macro call,
2996 walking up macro levels up to the file if necessary */
2997 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
2999 int t;
3000 const int *p;
3001 Sym *sa;
3003 for (;;) {
3004 if (macro_ptr) {
3005 p = macro_ptr, t = *p;
3006 if (ws_str) {
3007 while (is_space(t) || TOK_LINEFEED == t)
3008 tok_str_add(ws_str, t), t = *++p;
3010 if (t == 0 && can_read_stream) {
3011 end_macro();
3012 /* also, end of scope for nested defined symbol */
3013 sa = *nested_list;
3014 while (sa && sa->v == 0)
3015 sa = sa->prev;
3016 if (sa)
3017 sa->v = 0;
3018 continue;
3020 } else {
3021 ch = handle_eob();
3022 if (ws_str) {
3023 while (is_space(ch) || ch == '\n' || ch == '/') {
3024 if (ch == '/') {
3025 int c;
3026 uint8_t *p = file->buf_ptr;
3027 PEEKC(c, p);
3028 if (c == '*') {
3029 p = parse_comment(p);
3030 file->buf_ptr = p - 1;
3031 } else if (c == '/') {
3032 p = parse_line_comment(p);
3033 file->buf_ptr = p - 1;
3034 } else
3035 break;
3036 ch = ' ';
3038 tok_str_add(ws_str, ch);
3039 cinp();
3042 t = ch;
3045 if (ws_str)
3046 return t;
3047 next_nomacro_spc();
3048 return tok;
3052 /* do macro substitution of current token with macro 's' and add
3053 result to (tok_str,tok_len). 'nested_list' is the list of all
3054 macros we got inside to avoid recursing. Return non zero if no
3055 substitution needs to be done */
3056 static int macro_subst_tok(
3057 TokenString *tok_str,
3058 Sym **nested_list,
3059 Sym *s,
3060 int can_read_stream)
3062 Sym *args, *sa, *sa1;
3063 int parlevel, *mstr, t, t1, spc;
3064 TokenString str;
3065 char *cstrval;
3066 CValue cval;
3067 CString cstr;
3068 char buf[32];
3070 /* if symbol is a macro, prepare substitution */
3071 /* special macros */
3072 if (tok == TOK___LINE__) {
3073 snprintf(buf, sizeof(buf), "%d", file->line_num);
3074 cstrval = buf;
3075 t1 = TOK_PPNUM;
3076 goto add_cstr1;
3077 } else if (tok == TOK___FILE__) {
3078 cstrval = file->filename;
3079 goto add_cstr;
3080 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3081 time_t ti;
3082 struct tm *tm;
3084 time(&ti);
3085 tm = localtime(&ti);
3086 if (tok == TOK___DATE__) {
3087 snprintf(buf, sizeof(buf), "%s %2d %d",
3088 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3089 } else {
3090 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3091 tm->tm_hour, tm->tm_min, tm->tm_sec);
3093 cstrval = buf;
3094 add_cstr:
3095 t1 = TOK_STR;
3096 add_cstr1:
3097 cstr_new(&cstr);
3098 cstr_cat(&cstr, cstrval, 0);
3099 cval.str.size = cstr.size;
3100 cval.str.data = cstr.data;
3101 tok_str_add2(tok_str, t1, &cval);
3102 cstr_free(&cstr);
3103 } else {
3104 int saved_parse_flags = parse_flags;
3106 mstr = s->d;
3107 if (s->type.t == MACRO_FUNC) {
3108 /* whitespace between macro name and argument list */
3109 TokenString ws_str;
3110 tok_str_new(&ws_str);
3112 spc = 0;
3113 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3114 | PARSE_FLAG_ACCEPT_STRAYS;
3116 /* get next token from argument stream */
3117 t = next_argstream(nested_list, can_read_stream, &ws_str);
3118 if (t != '(') {
3119 /* not a macro substitution after all, restore the
3120 * macro token plus all whitespace we've read.
3121 * whitespace is intentionally not merged to preserve
3122 * newlines. */
3123 parse_flags = saved_parse_flags;
3124 tok_str_add(tok_str, tok);
3125 if (parse_flags & PARSE_FLAG_SPACES) {
3126 int i;
3127 for (i = 0; i < ws_str.len; i++)
3128 tok_str_add(tok_str, ws_str.str[i]);
3130 tok_str_free(ws_str.str);
3131 return 0;
3132 } else {
3133 tok_str_free(ws_str.str);
3135 next_nomacro(); /* eat '(' */
3137 /* argument macro */
3138 args = NULL;
3139 sa = s->next;
3140 /* NOTE: empty args are allowed, except if no args */
3141 for(;;) {
3142 do {
3143 next_argstream(nested_list, can_read_stream, NULL);
3144 } while (is_space(tok) || TOK_LINEFEED == tok);
3145 empty_arg:
3146 /* handle '()' case */
3147 if (!args && !sa && tok == ')')
3148 break;
3149 if (!sa)
3150 tcc_error("macro '%s' used with too many args",
3151 get_tok_str(s->v, 0));
3152 tok_str_new(&str);
3153 parlevel = spc = 0;
3154 /* NOTE: non zero sa->t indicates VA_ARGS */
3155 while ((parlevel > 0 ||
3156 (tok != ')' &&
3157 (tok != ',' || sa->type.t)))) {
3158 if (tok == TOK_EOF || tok == 0)
3159 break;
3160 if (tok == '(')
3161 parlevel++;
3162 else if (tok == ')')
3163 parlevel--;
3164 if (tok == TOK_LINEFEED)
3165 tok = ' ';
3166 if (!check_space(tok, &spc))
3167 tok_str_add2(&str, tok, &tokc);
3168 next_argstream(nested_list, can_read_stream, NULL);
3170 if (parlevel)
3171 expect(")");
3172 str.len -= spc;
3173 tok_str_add(&str, 0);
3174 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3175 sa1->d = str.str;
3176 sa = sa->next;
3177 if (tok == ')') {
3178 /* special case for gcc var args: add an empty
3179 var arg argument if it is omitted */
3180 if (sa && sa->type.t && gnu_ext)
3181 goto empty_arg;
3182 break;
3184 if (tok != ',')
3185 expect(",");
3187 if (sa) {
3188 tcc_error("macro '%s' used with too few args",
3189 get_tok_str(s->v, 0));
3192 parse_flags = saved_parse_flags;
3194 /* now subst each arg */
3195 mstr = macro_arg_subst(nested_list, mstr, args);
3196 /* free memory */
3197 sa = args;
3198 while (sa) {
3199 sa1 = sa->prev;
3200 tok_str_free(sa->d);
3201 sym_free(sa);
3202 sa = sa1;
3206 sym_push2(nested_list, s->v, 0, 0);
3207 parse_flags = saved_parse_flags;
3208 macro_subst(tok_str, nested_list, mstr, can_read_stream | 2);
3210 /* pop nested defined symbol */
3211 sa1 = *nested_list;
3212 *nested_list = sa1->prev;
3213 sym_free(sa1);
3214 if (mstr != s->d)
3215 tok_str_free(mstr);
3217 return 0;
3220 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3222 CString cstr;
3223 int n;
3225 cstr_new(&cstr);
3226 if (t1 != TOK_PLCHLDR)
3227 cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3228 n = cstr.size;
3229 if (t2 != TOK_PLCHLDR)
3230 cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3231 cstr_ccat(&cstr, '\0');
3233 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3234 memcpy(file->buffer, cstr.data, cstr.size);
3235 for (;;) {
3236 next_nomacro1();
3237 if (0 == *file->buf_ptr)
3238 break;
3239 if (is_space(tok))
3240 continue;
3241 tcc_warning("pasting <%.*s> and <%s> does not give a valid preprocessing token",
3242 n, cstr.data, (char*)cstr.data + n);
3243 break;
3245 tcc_close();
3247 //printf("paste <%s>\n", (char*)cstr.data);
3248 cstr_free(&cstr);
3249 return 0;
3252 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3253 return the resulting string (which must be freed). */
3254 static inline int *macro_twosharps(const int *ptr0)
3256 int t;
3257 CValue cval;
3258 TokenString macro_str1;
3259 int start_of_nosubsts = -1;
3260 const int *ptr;
3262 /* we search the first '##' */
3263 for (ptr = ptr0;;) {
3264 TOK_GET(&t, &ptr, &cval);
3265 if (t == TOK_TWOSHARPS)
3266 break;
3267 if (t == 0)
3268 return NULL;
3271 tok_str_new(&macro_str1);
3273 //tok_print(" $$$", ptr0);
3274 for (ptr = ptr0;;) {
3275 TOK_GET(&t, &ptr, &cval);
3276 if (t == 0)
3277 break;
3278 if (t == TOK_TWOSHARPS)
3279 continue;
3280 while (*ptr == TOK_TWOSHARPS) {
3281 int t1; CValue cv1;
3282 /* given 'a##b', remove nosubsts preceding 'a' */
3283 if (start_of_nosubsts >= 0)
3284 macro_str1.len = start_of_nosubsts;
3285 /* given 'a##b', remove nosubsts preceding 'b' */
3286 while ((t1 = *++ptr) == TOK_NOSUBST)
3288 if (t1 && t1 != TOK_TWOSHARPS
3289 && t1 != ':') /* 'a##:' don't build a new token */
3291 TOK_GET(&t1, &ptr, &cv1);
3292 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3293 paste_tokens(t, &cval, t1, &cv1);
3294 t = tok, cval = tokc;
3298 if (t == TOK_NOSUBST) {
3299 if (start_of_nosubsts < 0)
3300 start_of_nosubsts = macro_str1.len;
3301 } else {
3302 start_of_nosubsts = -1;
3304 tok_str_add2(&macro_str1, t, &cval);
3306 tok_str_add(&macro_str1, 0);
3307 //tok_print(" ###", macro_str1.str);
3308 return macro_str1.str;
3311 /* do macro substitution of macro_str and add result to
3312 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3313 inside to avoid recursing. */
3314 static void macro_subst(
3315 TokenString *tok_str,
3316 Sym **nested_list,
3317 const int *macro_str,
3318 int can_read_stream
3321 Sym *s;
3322 const int *ptr;
3323 int t, spc, nosubst;
3324 CValue cval;
3325 int *macro_str1 = NULL;
3327 /* first scan for '##' operator handling */
3328 ptr = macro_str;
3329 spc = nosubst = 0;
3331 /* first scan for '##' operator handling */
3332 if (can_read_stream & 1) {
3333 macro_str1 = macro_twosharps(ptr);
3334 if (macro_str1)
3335 ptr = macro_str1;
3338 while (1) {
3339 TOK_GET(&t, &ptr, &cval);
3340 if (t == 0)
3341 break;
3343 if (t >= TOK_IDENT && 0 == nosubst) {
3344 s = define_find(t);
3345 if (s == NULL)
3346 goto no_subst;
3348 /* if nested substitution, do nothing */
3349 if (sym_find2(*nested_list, t)) {
3350 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3351 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3352 goto no_subst;
3356 TokenString str;
3357 str.str = (int*)ptr;
3358 begin_macro(&str, 2);
3360 tok = t;
3361 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3363 if (str.alloc == 3) {
3364 /* already finished by reading function macro arguments */
3365 break;
3368 ptr = macro_ptr;
3369 end_macro ();
3372 spc = (tok_str->len &&
3373 is_space(tok_last(tok_str->str,
3374 tok_str->str + tok_str->len)));
3376 } else {
3378 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3379 tcc_error("stray '\\' in program");
3381 no_subst:
3382 if (!check_space(t, &spc))
3383 tok_str_add2(tok_str, t, &cval);
3384 nosubst = 0;
3385 if (t == TOK_NOSUBST)
3386 nosubst = 1;
3389 if (macro_str1)
3390 tok_str_free(macro_str1);
3394 /* return next token with macro substitution */
3395 ST_FUNC void next(void)
3397 redo:
3398 if (parse_flags & PARSE_FLAG_SPACES)
3399 next_nomacro_spc();
3400 else
3401 next_nomacro();
3403 if (macro_ptr) {
3404 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3405 /* discard preprocessor markers */
3406 goto redo;
3407 } else if (tok == 0) {
3408 /* end of macro or unget token string */
3409 end_macro();
3410 goto redo;
3412 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3413 Sym *s;
3414 /* if reading from file, try to substitute macros */
3415 s = define_find(tok);
3416 if (s) {
3417 Sym *nested_list = NULL;
3418 tokstr_buf.len = 0;
3419 macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
3420 tok_str_add(&tokstr_buf, 0);
3421 begin_macro(&tokstr_buf, 2);
3422 goto redo;
3425 /* convert preprocessor tokens into C tokens */
3426 if (tok == TOK_PPNUM) {
3427 if (parse_flags & PARSE_FLAG_TOK_NUM)
3428 parse_number((char *)tokc.str.data);
3429 } else if (tok == TOK_PPSTR) {
3430 if (parse_flags & PARSE_FLAG_TOK_STR)
3431 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3435 /* push back current token and set current token to 'last_tok'. Only
3436 identifier case handled for labels. */
3437 ST_INLN void unget_tok(int last_tok)
3440 TokenString *str = tok_str_alloc();
3441 tok_str_add2(str, tok, &tokc);
3442 tok_str_add(str, 0);
3443 begin_macro(str, 1);
3444 tok = last_tok;
3447 ST_FUNC void preprocess_init(TCCState *s1)
3449 s1->include_stack_ptr = s1->include_stack;
3450 /* XXX: move that before to avoid having to initialize
3451 file->ifdef_stack_ptr ? */
3452 s1->ifdef_stack_ptr = s1->ifdef_stack;
3453 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3454 pp_once++;
3456 pvtop = vtop = vstack - 1;
3457 s1->pack_stack[0] = 0;
3458 s1->pack_stack_ptr = s1->pack_stack;
3460 isidnum_table['$' - CH_EOF] =
3461 s1->dollars_in_identifiers ? IS_ID : 0;
3462 isidnum_table['.' - CH_EOF] =
3463 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
3466 ST_FUNC void preprocess_new(void)
3468 int i, c;
3469 const char *p, *r;
3471 /* init isid table */
3472 for(i = CH_EOF; i<128; i++)
3473 isidnum_table[i - CH_EOF]
3474 = is_space(i) ? IS_SPC
3475 : isid(i) ? IS_ID
3476 : isnum(i) ? IS_NUM
3477 : 0;
3479 for(i = 128; i<256; i++)
3480 isidnum_table[i - CH_EOF] = IS_ID;
3482 /* init allocators */
3483 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
3484 tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
3485 tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
3487 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3488 cstr_new(&cstr_buf);
3489 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
3490 tok_str_new(&tokstr_buf);
3491 tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
3493 tok_ident = TOK_IDENT;
3494 p = tcc_keywords;
3495 while (*p) {
3496 r = p;
3497 for(;;) {
3498 c = *r++;
3499 if (c == '\0')
3500 break;
3502 tok_alloc(p, r - p - 1);
3503 p = r;
3507 ST_FUNC void preprocess_delete(void)
3509 int i, n;
3511 /* free -D and compiler defines */
3512 free_defines(NULL);
3514 /* cleanup from error/setjmp */
3515 while (macro_stack)
3516 end_macro();
3517 macro_ptr = NULL;
3519 /* free tokens */
3520 n = tok_ident - TOK_IDENT;
3521 for(i = 0; i < n; i++)
3522 tal_free(toksym_alloc, table_ident[i]);
3523 tcc_free(table_ident);
3524 table_ident = NULL;
3526 /* free static buffers */
3527 cstr_free(&tokcstr);
3528 cstr_free(&cstr_buf);
3529 tok_str_free(tokstr_buf.str);
3531 /* free allocators */
3532 tal_delete(toksym_alloc);
3533 toksym_alloc = NULL;
3534 tal_delete(tokstr_alloc);
3535 tokstr_alloc = NULL;
3536 tal_delete(cstr_alloc);
3537 cstr_alloc = NULL;
3540 /* ------------------------------------------------------------------------- */
3541 /* tcc -E [-P[1]] [-dD} support */
3543 static void tok_print(const char *msg, const int *str)
3545 FILE *fp;
3546 int t;
3547 CValue cval;
3549 fp = tcc_state->ppfp;
3550 if (!fp || !tcc_state->dflag)
3551 fp = stdout;
3553 fprintf(fp, "%s ", msg);
3554 while (str) {
3555 TOK_GET(&t, &str, &cval);
3556 if (!t)
3557 break;
3558 fprintf(fp,"%s", get_tok_str(t, &cval));
3560 fprintf(fp, "\n");
3563 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3565 int d = f->line_num - f->line_ref;
3566 if (s1->dflag & 4)
3567 return;
3568 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3569 if (level == 0 && f->line_ref && d) {
3570 d = 1;
3571 goto simple;
3573 } else if (level == 0 && f->line_ref && d < 8) {
3574 simple:
3575 while (d > 0)
3576 fputs("\n", s1->ppfp), --d;
3577 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3578 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3579 } else {
3580 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3581 level > 0 ? " 1" : level < 0 ? " 2" : "");
3583 f->line_ref = f->line_num;
3586 static void define_print(TCCState *s1, int v)
3588 FILE *fp;
3589 Sym *s;
3591 s = define_find(v);
3592 if (NULL == s || NULL == s->d)
3593 return;
3595 fp = s1->ppfp;
3596 fprintf(fp, "#define %s", get_tok_str(v, NULL));
3597 if (s->type.t == MACRO_FUNC) {
3598 Sym *a = s->next;
3599 fprintf(fp,"(");
3600 if (a)
3601 for (;;) {
3602 fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3603 if (!(a = a->next))
3604 break;
3605 fprintf(fp,",");
3607 fprintf(fp,")");
3609 tok_print("", s->d);
3612 static void pp_debug_defines(TCCState *s1)
3614 int v, t;
3615 const char *vs;
3616 FILE *fp;
3618 t = pp_debug_tok;
3619 if (t == 0)
3620 return;
3622 file->line_num--;
3623 pp_line(s1, file, 0);
3624 file->line_ref = ++file->line_num;
3626 fp = s1->ppfp;
3627 v = pp_debug_symv;
3628 vs = get_tok_str(v, NULL);
3629 if (t == TOK_DEFINE) {
3630 define_print(s1, v);
3631 } else if (t == TOK_UNDEF) {
3632 fprintf(fp, "#undef %s\n", vs);
3633 } else if (t == TOK_push_macro) {
3634 fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3635 } else if (t == TOK_pop_macro) {
3636 fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3638 pp_debug_tok = 0;
3641 static void pp_debug_builtins(TCCState *s1)
3643 int v;
3644 for (v = TOK_IDENT; v < tok_ident; ++v)
3645 define_print(s1, v);
3648 /* Add a space between tokens a and b to avoid unwanted textual pasting */
3649 static int pp_need_space(int a, int b)
3651 return 'E' == a ? '+' == b || '-' == b
3652 : '+' == a ? TOK_INC == b || '+' == b
3653 : '-' == a ? TOK_DEC == b || '-' == b
3654 : a >= TOK_IDENT ? b >= TOK_IDENT
3655 : 0;
3658 /* maybe hex like 0x1e */
3659 static int pp_check_he0xE(int t, const char *p)
3661 if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
3662 return 'E';
3663 return t;
3666 /* Preprocess the current file */
3667 ST_FUNC int tcc_preprocess(TCCState *s1)
3669 BufferedFile **iptr;
3670 int token_seen, spcs, level;
3671 const char *p;
3672 Sym *define_start;
3674 preprocess_init(s1);
3675 ch = file->buf_ptr[0];
3676 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3677 parse_flags = PARSE_FLAG_PREPROCESS
3678 | (parse_flags & PARSE_FLAG_ASM_FILE)
3679 | PARSE_FLAG_LINEFEED
3680 | PARSE_FLAG_SPACES
3681 | PARSE_FLAG_ACCEPT_STRAYS
3683 define_start = define_stack;
3685 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3686 capability to compile and run itself, provided all numbers are
3687 given as decimals. tcc -E -P10 will do. */
3688 if (s1->Pflag == 1 + 10)
3689 parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
3691 #ifdef PP_BENCH
3692 /* for PP benchmarks */
3693 do next(); while (tok != TOK_EOF); return 0;
3694 #endif
3696 if (s1->dflag & 1) {
3697 pp_debug_builtins(s1);
3698 s1->dflag &= ~1;
3701 token_seen = TOK_LINEFEED, spcs = 0;
3702 pp_line(s1, file, 0);
3704 for (;;) {
3705 iptr = s1->include_stack_ptr;
3706 next();
3707 if (tok == TOK_EOF)
3708 break;
3709 level = s1->include_stack_ptr - iptr;
3710 if (level) {
3711 if (level > 0)
3712 pp_line(s1, *iptr, 0);
3713 pp_line(s1, file, level);
3716 if (s1->dflag) {
3717 pp_debug_defines(s1);
3718 if (s1->dflag & 4)
3719 continue;
3722 if (token_seen == TOK_LINEFEED) {
3723 if (tok == ' ') {
3724 ++spcs;
3725 continue;
3727 if (tok == TOK_LINEFEED) {
3728 spcs = 0;
3729 continue;
3731 pp_line(s1, file, 0);
3732 } else if (tok == TOK_LINEFEED) {
3733 ++file->line_ref;
3734 } else {
3735 spcs = pp_need_space(token_seen, tok);
3738 while (spcs)
3739 fputs(" ", s1->ppfp), --spcs;
3740 fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
3741 token_seen = pp_check_he0xE(tok, p);;
3743 /* reset define stack, but keep -D and built-ins */
3744 free_defines(define_start);
3745 return 0;
3748 /* ------------------------------------------------------------------------- */