i386-gen: fix USE_EBX
[tinycc.git] / tccpp.c
blobc069a24b99e2b74c806970f3be000b68dcd73ce0
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 static TokenString *macro_stack;
58 static const char tcc_keywords[] =
59 #define DEF(id, str) str "\0"
60 #include "tcctok.h"
61 #undef DEF
64 /* WARNING: the content of this string encodes token numbers */
65 static const unsigned char tok_two_chars[] =
66 /* outdated -- gr
67 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
68 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
69 */{
70 '<','=', TOK_LE,
71 '>','=', TOK_GE,
72 '!','=', TOK_NE,
73 '&','&', TOK_LAND,
74 '|','|', TOK_LOR,
75 '+','+', TOK_INC,
76 '-','-', TOK_DEC,
77 '=','=', TOK_EQ,
78 '<','<', TOK_SHL,
79 '>','>', TOK_SAR,
80 '+','=', TOK_A_ADD,
81 '-','=', TOK_A_SUB,
82 '*','=', TOK_A_MUL,
83 '/','=', TOK_A_DIV,
84 '%','=', TOK_A_MOD,
85 '&','=', TOK_A_AND,
86 '^','=', TOK_A_XOR,
87 '|','=', TOK_A_OR,
88 '-','>', TOK_ARROW,
89 '.','.', 0xa8, // C++ token ?
90 '#','#', TOK_TWOSHARPS,
94 static void next_nomacro_spc(void);
96 ST_FUNC void skip(int c)
98 if (tok != c)
99 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
100 next();
103 ST_FUNC void expect(const char *msg)
105 tcc_error("%s expected", msg);
108 /* ------------------------------------------------------------------------- */
109 /* Custom allocator for tiny objects */
111 #define USE_TAL
113 #ifndef USE_TAL
114 #define tal_free(al, p) tcc_free(p)
115 #define tal_realloc(al, p, size) tcc_realloc(p, size)
116 #define tal_new(a,b,c)
117 #define tal_delete(a)
118 #else
119 #if !defined(MEM_DEBUG)
120 #define tal_free(al, p) tal_free_impl(al, p)
121 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
122 #define TAL_DEBUG_PARAMS
123 #else
124 #define TAL_DEBUG 1
125 //#define TAL_INFO 1 /* collect and dump allocators stats */
126 #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
127 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
128 #define TAL_DEBUG_PARAMS , const char *file, int line
129 #define TAL_DEBUG_FILE_LEN 15
130 #endif
132 #define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */
133 #define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */
134 #define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */
135 #define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */
136 #define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */
137 #define CSTR_TAL_LIMIT 1024
139 typedef struct TinyAlloc {
140 unsigned limit;
141 unsigned size;
142 uint8_t *buffer;
143 uint8_t *p;
144 unsigned nb_allocs;
145 struct TinyAlloc *next, *top;
146 #ifdef TAL_INFO
147 unsigned nb_peak;
148 unsigned nb_total;
149 unsigned nb_missed;
150 uint8_t *peak_p;
151 #endif
152 } TinyAlloc;
154 typedef struct tal_header_t {
155 unsigned size;
156 #ifdef TAL_DEBUG
157 int line_num; /* negative line_num used for double free check */
158 char file_name[TAL_DEBUG_FILE_LEN + 1];
159 #endif
160 } tal_header_t;
162 /* ------------------------------------------------------------------------- */
164 static TinyAlloc *tal_new(TinyAlloc **pal, unsigned limit, unsigned size)
166 TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
167 al->p = al->buffer = tcc_malloc(size);
168 al->limit = limit;
169 al->size = size;
170 if (pal) *pal = al;
171 return al;
174 static void tal_delete(TinyAlloc *al)
176 TinyAlloc *next;
178 tail_call:
179 if (!al)
180 return;
181 #ifdef TAL_INFO
182 fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
183 al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
184 (al->peak_p - al->buffer) * 100.0 / al->size);
185 #endif
186 #ifdef TAL_DEBUG
187 if (al->nb_allocs > 0) {
188 uint8_t *p;
189 fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",
190 al->nb_allocs, al->limit);
191 p = al->buffer;
192 while (p < al->p) {
193 tal_header_t *header = (tal_header_t *)p;
194 if (header->line_num > 0) {
195 fprintf(stderr, "%s:%d: chunk of %d bytes leaked\n",
196 header->file_name, header->line_num, header->size);
198 p += header->size + sizeof(tal_header_t);
200 #if MEM_DEBUG-0 == 2
201 exit(2);
202 #endif
204 #endif
205 next = al->next;
206 tcc_free(al->buffer);
207 tcc_free(al);
208 al = next;
209 goto tail_call;
212 static void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
214 if (!p)
215 return;
216 tail_call:
217 if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
218 #ifdef TAL_DEBUG
219 tal_header_t *header = (((tal_header_t *)p) - 1);
220 if (header->line_num < 0) {
221 fprintf(stderr, "%s:%d: TAL_DEBUG: double frees chunk from\n",
222 file, line);
223 fprintf(stderr, "%s:%d: %d bytes\n",
224 header->file_name, (int)-header->line_num, (int)header->size);
225 } else
226 header->line_num = -header->line_num;
227 #endif
228 al->nb_allocs--;
229 if (!al->nb_allocs)
230 al->p = al->buffer;
231 } else if (al->next) {
232 al = al->next;
233 goto tail_call;
235 else
236 tcc_free(p);
239 static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_PARAMS)
241 tal_header_t *header;
242 void *ret;
243 int is_own;
244 unsigned adj_size = (size + 3) & -4;
245 TinyAlloc *al = *pal;
247 tail_call:
248 is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
249 if ((!p || is_own) && size <= al->limit) {
250 if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) {
251 header = (tal_header_t *)al->p;
252 header->size = adj_size;
253 #ifdef TAL_DEBUG
254 { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
255 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
256 header->file_name[TAL_DEBUG_FILE_LEN] = 0;
257 header->line_num = line; }
258 #endif
259 ret = al->p + sizeof(tal_header_t);
260 al->p += adj_size + sizeof(tal_header_t);
261 if (is_own) {
262 header = (((tal_header_t *)p) - 1);
263 memcpy(ret, p, header->size);
264 #ifdef TAL_DEBUG
265 header->line_num = -header->line_num;
266 #endif
267 } else {
268 al->nb_allocs++;
270 #ifdef TAL_INFO
271 if (al->nb_peak < al->nb_allocs)
272 al->nb_peak = al->nb_allocs;
273 if (al->peak_p < al->p)
274 al->peak_p = al->p;
275 al->nb_total++;
276 #endif
277 return ret;
278 } else if (is_own) {
279 al->nb_allocs--;
280 ret = tal_realloc(*pal, 0, size);
281 header = (((tal_header_t *)p) - 1);
282 memcpy(ret, p, header->size);
283 #ifdef TAL_DEBUG
284 header->line_num = -header->line_num;
285 #endif
286 return ret;
288 if (al->next) {
289 al = al->next;
290 } else {
291 TinyAlloc *bottom = al, *next = al->top ? al->top : al;
293 al = tal_new(pal, next->limit, next->size * 2);
294 al->next = next;
295 bottom->top = al;
297 goto tail_call;
299 if (is_own) {
300 al->nb_allocs--;
301 ret = tcc_malloc(size);
302 header = (((tal_header_t *)p) - 1);
303 memcpy(ret, p, header->size);
304 #ifdef TAL_DEBUG
305 header->line_num = -header->line_num;
306 #endif
307 } else if (al->next) {
308 al = al->next;
309 goto tail_call;
310 } else
311 ret = tcc_realloc(p, size);
312 #ifdef TAL_INFO
313 al->nb_missed++;
314 #endif
315 return ret;
318 #endif /* USE_TAL */
320 /* ------------------------------------------------------------------------- */
321 /* CString handling */
322 static void cstr_realloc(CString *cstr, int new_size)
324 int size;
326 size = cstr->size_allocated;
327 if (size < 8)
328 size = 8; /* no need to allocate a too small first string */
329 while (size < new_size)
330 size = size * 2;
331 cstr->data = tal_realloc(cstr_alloc, cstr->data, size);
332 cstr->size_allocated = size;
335 /* add a byte */
336 ST_INLN void cstr_ccat(CString *cstr, int ch)
338 int size;
339 size = cstr->size + 1;
340 if (size > cstr->size_allocated)
341 cstr_realloc(cstr, size);
342 ((unsigned char *)cstr->data)[size - 1] = ch;
343 cstr->size = size;
346 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len)
348 int size;
349 if (len <= 0)
350 len = strlen(str) + 1 + len;
351 size = cstr->size + len;
352 if (size > cstr->size_allocated)
353 cstr_realloc(cstr, size);
354 memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
355 cstr->size = size;
358 /* add a wide char */
359 ST_FUNC void cstr_wccat(CString *cstr, int ch)
361 int size;
362 size = cstr->size + sizeof(nwchar_t);
363 if (size > cstr->size_allocated)
364 cstr_realloc(cstr, size);
365 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
366 cstr->size = size;
369 ST_FUNC void cstr_new(CString *cstr)
371 memset(cstr, 0, sizeof(CString));
374 /* free string and reset it to NULL */
375 ST_FUNC void cstr_free(CString *cstr)
377 tal_free(cstr_alloc, cstr->data);
378 cstr_new(cstr);
381 /* reset string to empty */
382 ST_FUNC void cstr_reset(CString *cstr)
384 cstr->size = 0;
387 /* XXX: unicode ? */
388 static void add_char(CString *cstr, int c)
390 if (c == '\'' || c == '\"' || c == '\\') {
391 /* XXX: could be more precise if char or string */
392 cstr_ccat(cstr, '\\');
394 if (c >= 32 && c <= 126) {
395 cstr_ccat(cstr, c);
396 } else {
397 cstr_ccat(cstr, '\\');
398 if (c == '\n') {
399 cstr_ccat(cstr, 'n');
400 } else {
401 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
402 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
403 cstr_ccat(cstr, '0' + (c & 7));
408 /* ------------------------------------------------------------------------- */
409 /* allocate a new token */
410 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
412 TokenSym *ts, **ptable;
413 int i;
415 if (tok_ident >= SYM_FIRST_ANOM)
416 tcc_error("memory full (symbols)");
418 /* expand token table if needed */
419 i = tok_ident - TOK_IDENT;
420 if ((i % TOK_ALLOC_INCR) == 0) {
421 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
422 table_ident = ptable;
425 ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len);
426 table_ident[i] = ts;
427 ts->tok = tok_ident++;
428 ts->sym_define = NULL;
429 ts->sym_label = NULL;
430 ts->sym_struct = NULL;
431 ts->sym_identifier = NULL;
432 ts->len = len;
433 ts->hash_next = NULL;
434 memcpy(ts->str, str, len);
435 ts->str[len] = '\0';
436 *pts = ts;
437 return ts;
440 #define TOK_HASH_INIT 1
441 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
444 /* find a token and add it if not found */
445 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
447 TokenSym *ts, **pts;
448 int i;
449 unsigned int h;
451 h = TOK_HASH_INIT;
452 for(i=0;i<len;i++)
453 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
454 h &= (TOK_HASH_SIZE - 1);
456 pts = &hash_ident[h];
457 for(;;) {
458 ts = *pts;
459 if (!ts)
460 break;
461 if (ts->len == len && !memcmp(ts->str, str, len))
462 return ts;
463 pts = &(ts->hash_next);
465 return tok_alloc_new(pts, str, len);
468 /* XXX: buffer overflow */
469 /* XXX: float tokens */
470 ST_FUNC const char *get_tok_str(int v, CValue *cv)
472 char *p;
473 int i, len;
475 cstr_reset(&cstr_buf);
476 p = cstr_buf.data;
478 switch(v) {
479 case TOK_CINT:
480 case TOK_CUINT:
481 case TOK_CLLONG:
482 case TOK_CULLONG:
483 /* XXX: not quite exact, but only useful for testing */
484 #ifdef _WIN32
485 sprintf(p, "%u", (unsigned)cv->i);
486 #else
487 sprintf(p, "%llu", (unsigned long long)cv->i);
488 #endif
489 break;
490 case TOK_LCHAR:
491 cstr_ccat(&cstr_buf, 'L');
492 case TOK_CCHAR:
493 cstr_ccat(&cstr_buf, '\'');
494 add_char(&cstr_buf, cv->i);
495 cstr_ccat(&cstr_buf, '\'');
496 cstr_ccat(&cstr_buf, '\0');
497 break;
498 case TOK_PPNUM:
499 case TOK_PPSTR:
500 return (char*)cv->str.data;
501 case TOK_LSTR:
502 cstr_ccat(&cstr_buf, 'L');
503 case TOK_STR:
504 cstr_ccat(&cstr_buf, '\"');
505 if (v == TOK_STR) {
506 len = cv->str.size - 1;
507 for(i=0;i<len;i++)
508 add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
509 } else {
510 len = (cv->str.size / sizeof(nwchar_t)) - 1;
511 for(i=0;i<len;i++)
512 add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
514 cstr_ccat(&cstr_buf, '\"');
515 cstr_ccat(&cstr_buf, '\0');
516 break;
518 case TOK_CFLOAT:
519 cstr_cat(&cstr_buf, "<float>", 0);
520 break;
521 case TOK_CDOUBLE:
522 cstr_cat(&cstr_buf, "<double>", 0);
523 break;
524 case TOK_CLDOUBLE:
525 cstr_cat(&cstr_buf, "<long double>", 0);
526 break;
527 case TOK_LINENUM:
528 cstr_cat(&cstr_buf, "<linenumber>", 0);
529 break;
531 /* above tokens have value, the ones below don't */
533 case TOK_LT:
534 v = '<';
535 goto addv;
536 case TOK_GT:
537 v = '>';
538 goto addv;
539 case TOK_DOTS:
540 return strcpy(p, "...");
541 case TOK_A_SHL:
542 return strcpy(p, "<<=");
543 case TOK_A_SAR:
544 return strcpy(p, ">>=");
545 default:
546 if (v < TOK_IDENT) {
547 /* search in two bytes table */
548 const unsigned char *q = tok_two_chars;
549 while (*q) {
550 if (q[2] == v) {
551 *p++ = q[0];
552 *p++ = q[1];
553 *p = '\0';
554 return cstr_buf.data;
556 q += 3;
558 if (v >= 127) {
559 sprintf(cstr_buf.data, "<%02x>", v);
560 return cstr_buf.data;
562 addv:
563 *p++ = v;
564 *p = '\0';
565 } else if (v < tok_ident) {
566 return table_ident[v - TOK_IDENT]->str;
567 } else if (v >= SYM_FIRST_ANOM) {
568 /* special name for anonymous symbol */
569 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
570 } else {
571 /* should never happen */
572 return NULL;
574 break;
576 return cstr_buf.data;
579 /* return the current character, handling end of block if necessary
580 (but not stray) */
581 ST_FUNC int handle_eob(void)
583 BufferedFile *bf = file;
584 int len;
586 /* only tries to read if really end of buffer */
587 if (bf->buf_ptr >= bf->buf_end) {
588 if (bf->fd != -1) {
589 #if defined(PARSE_DEBUG)
590 len = 1;
591 #else
592 len = IO_BUF_SIZE;
593 #endif
594 len = read(bf->fd, bf->buffer, len);
595 if (len < 0)
596 len = 0;
597 } else {
598 len = 0;
600 total_bytes += len;
601 bf->buf_ptr = bf->buffer;
602 bf->buf_end = bf->buffer + len;
603 *bf->buf_end = CH_EOB;
605 if (bf->buf_ptr < bf->buf_end) {
606 return bf->buf_ptr[0];
607 } else {
608 bf->buf_ptr = bf->buf_end;
609 return CH_EOF;
613 /* read next char from current input file and handle end of input buffer */
614 ST_INLN void inp(void)
616 ch = *(++(file->buf_ptr));
617 /* end of buffer/file handling */
618 if (ch == CH_EOB)
619 ch = handle_eob();
622 /* handle '\[\r]\n' */
623 static int handle_stray_noerror(void)
625 while (ch == '\\') {
626 inp();
627 if (ch == '\n') {
628 file->line_num++;
629 inp();
630 } else if (ch == '\r') {
631 inp();
632 if (ch != '\n')
633 goto fail;
634 file->line_num++;
635 inp();
636 } else {
637 fail:
638 return 1;
641 return 0;
644 static void handle_stray(void)
646 if (handle_stray_noerror())
647 tcc_error("stray '\\' in program");
650 /* skip the stray and handle the \\n case. Output an error if
651 incorrect char after the stray */
652 static int handle_stray1(uint8_t *p)
654 int c;
656 file->buf_ptr = p;
657 if (p >= file->buf_end) {
658 c = handle_eob();
659 if (c != '\\')
660 return c;
661 p = file->buf_ptr;
663 ch = *p;
664 if (handle_stray_noerror()) {
665 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
666 tcc_error("stray '\\' in program");
667 *--file->buf_ptr = '\\';
669 p = file->buf_ptr;
670 c = *p;
671 return c;
674 /* handle just the EOB case, but not stray */
675 #define PEEKC_EOB(c, p)\
677 p++;\
678 c = *p;\
679 if (c == '\\') {\
680 file->buf_ptr = p;\
681 c = handle_eob();\
682 p = file->buf_ptr;\
686 /* handle the complicated stray case */
687 #define PEEKC(c, p)\
689 p++;\
690 c = *p;\
691 if (c == '\\') {\
692 c = handle_stray1(p);\
693 p = file->buf_ptr;\
697 /* input with '\[\r]\n' handling. Note that this function cannot
698 handle other characters after '\', so you cannot call it inside
699 strings or comments */
700 ST_FUNC void minp(void)
702 inp();
703 if (ch == '\\')
704 handle_stray();
707 /* single line C++ comments */
708 static uint8_t *parse_line_comment(uint8_t *p)
710 int c;
712 p++;
713 for(;;) {
714 c = *p;
715 redo:
716 if (c == '\n' || c == CH_EOF) {
717 break;
718 } else if (c == '\\') {
719 file->buf_ptr = p;
720 c = handle_eob();
721 p = file->buf_ptr;
722 if (c == '\\') {
723 PEEKC_EOB(c, p);
724 if (c == '\n') {
725 file->line_num++;
726 PEEKC_EOB(c, p);
727 } else if (c == '\r') {
728 PEEKC_EOB(c, p);
729 if (c == '\n') {
730 file->line_num++;
731 PEEKC_EOB(c, p);
734 } else {
735 goto redo;
737 } else {
738 p++;
741 return p;
744 /* C comments */
745 ST_FUNC uint8_t *parse_comment(uint8_t *p)
747 int c;
749 p++;
750 for(;;) {
751 /* fast skip loop */
752 for(;;) {
753 c = *p;
754 if (c == '\n' || c == '*' || c == '\\')
755 break;
756 p++;
757 c = *p;
758 if (c == '\n' || c == '*' || c == '\\')
759 break;
760 p++;
762 /* now we can handle all the cases */
763 if (c == '\n') {
764 file->line_num++;
765 p++;
766 } else if (c == '*') {
767 p++;
768 for(;;) {
769 c = *p;
770 if (c == '*') {
771 p++;
772 } else if (c == '/') {
773 goto end_of_comment;
774 } else if (c == '\\') {
775 file->buf_ptr = p;
776 c = handle_eob();
777 p = file->buf_ptr;
778 if (c == CH_EOF)
779 tcc_error("unexpected end of file in comment");
780 if (c == '\\') {
781 /* skip '\[\r]\n', otherwise just skip the stray */
782 while (c == '\\') {
783 PEEKC_EOB(c, p);
784 if (c == '\n') {
785 file->line_num++;
786 PEEKC_EOB(c, p);
787 } else if (c == '\r') {
788 PEEKC_EOB(c, p);
789 if (c == '\n') {
790 file->line_num++;
791 PEEKC_EOB(c, p);
793 } else {
794 goto after_star;
798 } else {
799 break;
802 after_star: ;
803 } else {
804 /* stray, eob or eof */
805 file->buf_ptr = p;
806 c = handle_eob();
807 p = file->buf_ptr;
808 if (c == CH_EOF) {
809 tcc_error("unexpected end of file in comment");
810 } else if (c == '\\') {
811 p++;
815 end_of_comment:
816 p++;
817 return p;
820 ST_FUNC void set_idnum(int c, int val)
822 isidnum_table[c - CH_EOF] = val;
825 #define cinp minp
827 static inline void skip_spaces(void)
829 while (isidnum_table[ch - CH_EOF] & IS_SPC)
830 cinp();
833 static inline int check_space(int t, int *spc)
835 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
836 if (*spc)
837 return 1;
838 *spc = 1;
839 } else
840 *spc = 0;
841 return 0;
844 /* parse a string without interpreting escapes */
845 static uint8_t *parse_pp_string(uint8_t *p,
846 int sep, CString *str)
848 int c;
849 p++;
850 for(;;) {
851 c = *p;
852 if (c == sep) {
853 break;
854 } else if (c == '\\') {
855 file->buf_ptr = p;
856 c = handle_eob();
857 p = file->buf_ptr;
858 if (c == CH_EOF) {
859 unterminated_string:
860 /* XXX: indicate line number of start of string */
861 tcc_error("missing terminating %c character", sep);
862 } else if (c == '\\') {
863 /* escape : just skip \[\r]\n */
864 PEEKC_EOB(c, p);
865 if (c == '\n') {
866 file->line_num++;
867 p++;
868 } else if (c == '\r') {
869 PEEKC_EOB(c, p);
870 if (c != '\n')
871 expect("'\n' after '\r'");
872 file->line_num++;
873 p++;
874 } else if (c == CH_EOF) {
875 goto unterminated_string;
876 } else {
877 if (str) {
878 cstr_ccat(str, '\\');
879 cstr_ccat(str, c);
881 p++;
884 } else if (c == '\n') {
885 file->line_num++;
886 goto add_char;
887 } else if (c == '\r') {
888 PEEKC_EOB(c, p);
889 if (c != '\n') {
890 if (str)
891 cstr_ccat(str, '\r');
892 } else {
893 file->line_num++;
894 goto add_char;
896 } else {
897 add_char:
898 if (str)
899 cstr_ccat(str, c);
900 p++;
903 p++;
904 return p;
907 /* skip block of text until #else, #elif or #endif. skip also pairs of
908 #if/#endif */
909 static void preprocess_skip(void)
911 int a, start_of_line, c, in_warn_or_error;
912 uint8_t *p;
914 p = file->buf_ptr;
915 a = 0;
916 redo_start:
917 start_of_line = 1;
918 in_warn_or_error = 0;
919 for(;;) {
920 redo_no_start:
921 c = *p;
922 switch(c) {
923 case ' ':
924 case '\t':
925 case '\f':
926 case '\v':
927 case '\r':
928 p++;
929 goto redo_no_start;
930 case '\n':
931 file->line_num++;
932 p++;
933 goto redo_start;
934 case '\\':
935 file->buf_ptr = p;
936 c = handle_eob();
937 if (c == CH_EOF) {
938 expect("#endif");
939 } else if (c == '\\') {
940 ch = file->buf_ptr[0];
941 handle_stray_noerror();
943 p = file->buf_ptr;
944 goto redo_no_start;
945 /* skip strings */
946 case '\"':
947 case '\'':
948 if (in_warn_or_error)
949 goto _default;
950 p = parse_pp_string(p, c, NULL);
951 break;
952 /* skip comments */
953 case '/':
954 if (in_warn_or_error)
955 goto _default;
956 file->buf_ptr = p;
957 ch = *p;
958 minp();
959 p = file->buf_ptr;
960 if (ch == '*') {
961 p = parse_comment(p);
962 } else if (ch == '/') {
963 p = parse_line_comment(p);
965 break;
966 case '#':
967 p++;
968 if (start_of_line) {
969 file->buf_ptr = p;
970 next_nomacro();
971 p = file->buf_ptr;
972 if (a == 0 &&
973 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
974 goto the_end;
975 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
976 a++;
977 else if (tok == TOK_ENDIF)
978 a--;
979 else if( tok == TOK_ERROR || tok == TOK_WARNING)
980 in_warn_or_error = 1;
981 else if (tok == TOK_LINEFEED)
982 goto redo_start;
983 else if (parse_flags & PARSE_FLAG_ASM_FILE)
984 p = parse_line_comment(p - 1);
985 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
986 p = parse_line_comment(p - 1);
987 break;
988 _default:
989 default:
990 p++;
991 break;
993 start_of_line = 0;
995 the_end: ;
996 file->buf_ptr = p;
999 /* ParseState handling */
1001 /* XXX: currently, no include file info is stored. Thus, we cannot display
1002 accurate messages if the function or data definition spans multiple
1003 files */
1005 /* save current parse state in 's' */
1006 ST_FUNC void save_parse_state(ParseState *s)
1008 s->line_num = file->line_num;
1009 s->macro_ptr = macro_ptr;
1010 s->tok = tok;
1011 s->tokc = tokc;
1014 /* restore parse state from 's' */
1015 ST_FUNC void restore_parse_state(ParseState *s)
1017 file->line_num = s->line_num;
1018 macro_ptr = s->macro_ptr;
1019 tok = s->tok;
1020 tokc = s->tokc;
1023 /* return the number of additional 'ints' necessary to store the
1024 token */
1025 static inline int tok_size(const int *p)
1027 switch(*p) {
1028 /* 4 bytes */
1029 case TOK_CINT:
1030 case TOK_CUINT:
1031 case TOK_CCHAR:
1032 case TOK_LCHAR:
1033 case TOK_CFLOAT:
1034 case TOK_LINENUM:
1035 return 1 + 1;
1036 case TOK_STR:
1037 case TOK_LSTR:
1038 case TOK_PPNUM:
1039 case TOK_PPSTR:
1040 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
1041 case TOK_CDOUBLE:
1042 case TOK_CLLONG:
1043 case TOK_CULLONG:
1044 return 1 + 2;
1045 case TOK_CLDOUBLE:
1046 return 1 + LDOUBLE_SIZE / 4;
1047 default:
1048 return 1 + 0;
1052 /* token string handling */
1054 ST_INLN void tok_str_new(TokenString *s)
1056 s->str = NULL;
1057 s->len = 0;
1058 s->allocated_len = 0;
1059 s->last_line_num = -1;
1062 ST_FUNC TokenString *tok_str_alloc(void)
1064 TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
1065 tok_str_new(str);
1066 return str;
1069 ST_FUNC int *tok_str_dup(TokenString *s)
1071 int *str;
1073 str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
1074 memcpy(str, s->str, s->len * sizeof(int));
1075 return str;
1078 ST_FUNC void tok_str_free_str(int *str)
1080 tal_free(tokstr_alloc, str);
1083 ST_FUNC void tok_str_free(TokenString *str)
1085 tok_str_free_str(str->str);
1086 tal_free(tokstr_alloc, str);
1089 ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
1091 int *str, size;
1093 size = s->allocated_len;
1094 if (size < 16)
1095 size = 16;
1096 while (size < new_size)
1097 size = size * 2;
1098 if (size > s->allocated_len) {
1099 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
1100 s->allocated_len = size;
1101 s->str = str;
1103 return s->str;
1106 ST_FUNC void tok_str_add(TokenString *s, int t)
1108 int len, *str;
1110 len = s->len;
1111 str = s->str;
1112 if (len >= s->allocated_len)
1113 str = tok_str_realloc(s, len + 1);
1114 str[len++] = t;
1115 s->len = len;
1118 ST_FUNC void begin_macro(TokenString *str, int alloc)
1120 str->alloc = alloc;
1121 str->prev = macro_stack;
1122 str->prev_ptr = macro_ptr;
1123 macro_ptr = str->str;
1124 macro_stack = str;
1127 ST_FUNC void end_macro(void)
1129 TokenString *str = macro_stack;
1130 macro_stack = str->prev;
1131 macro_ptr = str->prev_ptr;
1132 if (str->alloc == 2) {
1133 str->alloc = 3; /* just mark as finished */
1134 } else {
1135 tok_str_free(str);
1139 static void tok_str_add2(TokenString *s, int t, CValue *cv)
1141 int len, *str;
1143 len = s->len;
1144 str = s->str;
1146 /* allocate space for worst case */
1147 if (len + TOK_MAX_SIZE >= s->allocated_len)
1148 str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
1149 str[len++] = t;
1150 switch(t) {
1151 case TOK_CINT:
1152 case TOK_CUINT:
1153 case TOK_CCHAR:
1154 case TOK_LCHAR:
1155 case TOK_CFLOAT:
1156 case TOK_LINENUM:
1157 str[len++] = cv->tab[0];
1158 break;
1159 case TOK_PPNUM:
1160 case TOK_PPSTR:
1161 case TOK_STR:
1162 case TOK_LSTR:
1164 /* Insert the string into the int array. */
1165 size_t nb_words =
1166 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
1167 if (len + nb_words >= s->allocated_len)
1168 str = tok_str_realloc(s, len + nb_words + 1);
1169 str[len] = cv->str.size;
1170 memcpy(&str[len + 1], cv->str.data, cv->str.size);
1171 len += nb_words;
1173 break;
1174 case TOK_CDOUBLE:
1175 case TOK_CLLONG:
1176 case TOK_CULLONG:
1177 #if LDOUBLE_SIZE == 8
1178 case TOK_CLDOUBLE:
1179 #endif
1180 str[len++] = cv->tab[0];
1181 str[len++] = cv->tab[1];
1182 break;
1183 #if LDOUBLE_SIZE == 12
1184 case TOK_CLDOUBLE:
1185 str[len++] = cv->tab[0];
1186 str[len++] = cv->tab[1];
1187 str[len++] = cv->tab[2];
1188 #elif LDOUBLE_SIZE == 16
1189 case TOK_CLDOUBLE:
1190 str[len++] = cv->tab[0];
1191 str[len++] = cv->tab[1];
1192 str[len++] = cv->tab[2];
1193 str[len++] = cv->tab[3];
1194 #elif LDOUBLE_SIZE != 8
1195 #error add long double size support
1196 #endif
1197 break;
1198 default:
1199 break;
1201 s->len = len;
1204 /* add the current parse token in token string 's' */
1205 ST_FUNC void tok_str_add_tok(TokenString *s)
1207 CValue cval;
1209 /* save line number info */
1210 if (file->line_num != s->last_line_num) {
1211 s->last_line_num = file->line_num;
1212 cval.i = s->last_line_num;
1213 tok_str_add2(s, TOK_LINENUM, &cval);
1215 tok_str_add2(s, tok, &tokc);
1218 /* get a token from an integer array and increment pointer
1219 accordingly. we code it as a macro to avoid pointer aliasing. */
1220 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
1222 const int *p = *pp;
1223 int n, *tab;
1225 tab = cv->tab;
1226 switch(*t = *p++) {
1227 case TOK_CINT:
1228 case TOK_CUINT:
1229 case TOK_CCHAR:
1230 case TOK_LCHAR:
1231 case TOK_CFLOAT:
1232 case TOK_LINENUM:
1233 tab[0] = *p++;
1234 break;
1235 case TOK_STR:
1236 case TOK_LSTR:
1237 case TOK_PPNUM:
1238 case TOK_PPSTR:
1239 cv->str.size = *p++;
1240 cv->str.data = p;
1241 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1242 break;
1243 case TOK_CDOUBLE:
1244 case TOK_CLLONG:
1245 case TOK_CULLONG:
1246 n = 2;
1247 goto copy;
1248 case TOK_CLDOUBLE:
1249 #if LDOUBLE_SIZE == 16
1250 n = 4;
1251 #elif LDOUBLE_SIZE == 12
1252 n = 3;
1253 #elif LDOUBLE_SIZE == 8
1254 n = 2;
1255 #else
1256 # error add long double size support
1257 #endif
1258 copy:
1260 *tab++ = *p++;
1261 while (--n);
1262 break;
1263 default:
1264 break;
1266 *pp = p;
1269 /* Calling this function is expensive, but it is not possible
1270 to read a token string backwards. */
1271 static int tok_last(const int *str0, const int *str1)
1273 const int *str = str0;
1274 int tok = 0;
1275 CValue cval;
1277 while (str < str1)
1278 TOK_GET(&tok, &str, &cval);
1279 return tok;
1282 static int macro_is_equal(const int *a, const int *b)
1284 CValue cv;
1285 int t;
1286 static CString localbuf;
1288 if (!a || !b)
1289 return 1;
1291 while (*a && *b) {
1292 /* first time preallocate static localbuf, next time only reset position to start */
1293 cstr_reset(&localbuf);
1294 TOK_GET(&t, &a, &cv);
1295 cstr_cat(&localbuf, get_tok_str(t, &cv), 0);
1296 TOK_GET(&t, &b, &cv);
1297 if (strcmp(localbuf.data, get_tok_str(t, &cv)))
1298 return 0;
1300 return !(*a || *b);
1303 /* defines handling */
1304 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1306 Sym *s, *o;
1308 o = define_find(v);
1309 s = sym_push2(&define_stack, v, macro_type, 0);
1310 s->d = str;
1311 s->next = first_arg;
1312 table_ident[v - TOK_IDENT]->sym_define = s;
1314 if (o && !macro_is_equal(o->d, s->d))
1315 tcc_warning("%s redefined", get_tok_str(v, NULL));
1318 /* undefined a define symbol. Its name is just set to zero */
1319 ST_FUNC void define_undef(Sym *s)
1321 int v = s->v;
1322 if (v >= TOK_IDENT && v < tok_ident)
1323 table_ident[v - TOK_IDENT]->sym_define = NULL;
1326 ST_INLN Sym *define_find(int v)
1328 v -= TOK_IDENT;
1329 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1330 return NULL;
1331 return table_ident[v]->sym_define;
1334 /* free define stack until top reaches 'b' */
1335 ST_FUNC void free_defines(Sym *b)
1337 while (define_stack != b) {
1338 Sym *top = define_stack;
1339 define_stack = top->prev;
1340 tok_str_free_str(top->d);
1341 define_undef(top);
1342 sym_free(top);
1345 /* restore remaining (-D or predefined) symbols */
1346 while (b) {
1347 int v = b->v;
1348 if (v >= TOK_IDENT && v < tok_ident) {
1349 Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
1350 if (!*d)
1351 *d = b;
1353 b = b->prev;
1357 /* label lookup */
1358 ST_FUNC Sym *label_find(int v)
1360 v -= TOK_IDENT;
1361 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1362 return NULL;
1363 return table_ident[v]->sym_label;
1366 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1368 Sym *s, **ps;
1369 s = sym_push2(ptop, v, 0, 0);
1370 s->r = flags;
1371 ps = &table_ident[v - TOK_IDENT]->sym_label;
1372 if (ptop == &global_label_stack) {
1373 /* modify the top most local identifier, so that
1374 sym_identifier will point to 's' when popped */
1375 while (*ps != NULL)
1376 ps = &(*ps)->prev_tok;
1378 s->prev_tok = *ps;
1379 *ps = s;
1380 return s;
1383 /* pop labels until element last is reached. Look if any labels are
1384 undefined. Define symbols if '&&label' was used. */
1385 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1387 Sym *s, *s1;
1388 for(s = *ptop; s != slast; s = s1) {
1389 s1 = s->prev;
1390 if (s->r == LABEL_DECLARED) {
1391 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1392 } else if (s->r == LABEL_FORWARD) {
1393 tcc_error("label '%s' used but not defined",
1394 get_tok_str(s->v, NULL));
1395 } else {
1396 if (s->c) {
1397 /* define corresponding symbol. A size of
1398 1 is put. */
1399 put_extern_sym(s, cur_text_section, s->jnext, 1);
1402 /* remove label */
1403 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1404 sym_free(s);
1406 *ptop = slast;
1409 /* eval an expression for #if/#elif */
1410 static int expr_preprocess(void)
1412 int c, t;
1413 TokenString *str;
1415 str = tok_str_alloc();
1416 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1417 next(); /* do macro subst */
1418 if (tok == TOK_DEFINED) {
1419 next_nomacro();
1420 t = tok;
1421 if (t == '(')
1422 next_nomacro();
1423 c = define_find(tok) != 0;
1424 if (t == '(')
1425 next_nomacro();
1426 tok = TOK_CINT;
1427 tokc.i = c;
1428 } else if (tok >= TOK_IDENT) {
1429 /* if undefined macro */
1430 tok = TOK_CINT;
1431 tokc.i = 0;
1433 tok_str_add_tok(str);
1435 tok_str_add(str, -1); /* simulate end of file */
1436 tok_str_add(str, 0);
1437 /* now evaluate C constant expression */
1438 begin_macro(str, 1);
1439 next();
1440 c = expr_const();
1441 end_macro();
1442 return c != 0;
1446 /* parse after #define */
1447 ST_FUNC void parse_define(void)
1449 Sym *s, *first, **ps;
1450 int v, t, varg, is_vaargs, spc;
1451 int saved_parse_flags = parse_flags;
1453 v = tok;
1454 if (v < TOK_IDENT)
1455 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1456 /* XXX: should check if same macro (ANSI) */
1457 first = NULL;
1458 t = MACRO_OBJ;
1459 /* We have to parse the whole define as if not in asm mode, in particular
1460 no line comment with '#' must be ignored. Also for function
1461 macros the argument list must be parsed without '.' being an ID
1462 character. */
1463 parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES);
1464 /* '(' must be just after macro definition for MACRO_FUNC */
1465 next_nomacro_spc();
1466 if (tok == '(') {
1467 set_idnum('.', 0);
1468 next_nomacro();
1469 ps = &first;
1470 if (tok != ')') for (;;) {
1471 varg = tok;
1472 next_nomacro();
1473 is_vaargs = 0;
1474 if (varg == TOK_DOTS) {
1475 varg = TOK___VA_ARGS__;
1476 is_vaargs = 1;
1477 } else if (tok == TOK_DOTS && gnu_ext) {
1478 is_vaargs = 1;
1479 next_nomacro();
1481 if (varg < TOK_IDENT)
1482 bad_list:
1483 tcc_error("bad macro parameter list");
1484 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1485 *ps = s;
1486 ps = &s->next;
1487 if (tok == ')')
1488 break;
1489 if (tok != ',' || is_vaargs)
1490 goto bad_list;
1491 next_nomacro();
1493 next_nomacro_spc();
1494 t = MACRO_FUNC;
1497 tokstr_buf.len = 0;
1498 spc = 2;
1499 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1500 /* The body of a macro definition should be parsed such that identifiers
1501 are parsed like the file mode determines (i.e. with '.' being an
1502 ID character in asm mode). But '#' should be retained instead of
1503 regarded as line comment leader, so still don't set ASM_FILE
1504 in parse_flags. */
1505 set_idnum('.', (saved_parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
1506 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1507 /* remove spaces around ## and after '#' */
1508 if (TOK_TWOSHARPS == tok) {
1509 if (2 == spc)
1510 goto bad_twosharp;
1511 if (1 == spc)
1512 --tokstr_buf.len;
1513 spc = 3;
1514 tok = TOK_PPJOIN;
1515 } else if ('#' == tok) {
1516 spc = 4;
1517 } else if (check_space(tok, &spc)) {
1518 goto skip;
1520 tok_str_add2(&tokstr_buf, tok, &tokc);
1521 skip:
1522 next_nomacro_spc();
1525 parse_flags = saved_parse_flags;
1526 if (spc == 1)
1527 --tokstr_buf.len; /* remove trailing space */
1528 tok_str_add(&tokstr_buf, 0);
1529 if (3 == spc)
1530 bad_twosharp:
1531 tcc_error("'##' cannot appear at either end of macro");
1532 define_push(v, t, tok_str_dup(&tokstr_buf), first);
1535 static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
1537 const unsigned char *s;
1538 unsigned int h;
1539 CachedInclude *e;
1540 int i;
1542 h = TOK_HASH_INIT;
1543 s = (unsigned char *) filename;
1544 while (*s) {
1545 #ifdef _WIN32
1546 h = TOK_HASH_FUNC(h, toup(*s));
1547 #else
1548 h = TOK_HASH_FUNC(h, *s);
1549 #endif
1550 s++;
1552 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1554 i = s1->cached_includes_hash[h];
1555 for(;;) {
1556 if (i == 0)
1557 break;
1558 e = s1->cached_includes[i - 1];
1559 if (0 == PATHCMP(e->filename, filename))
1560 return e;
1561 i = e->hash_next;
1563 if (!add)
1564 return NULL;
1566 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1567 strcpy(e->filename, filename);
1568 e->ifndef_macro = e->once = 0;
1569 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1570 /* add in hash table */
1571 e->hash_next = s1->cached_includes_hash[h];
1572 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1573 #ifdef INC_DEBUG
1574 printf("adding cached '%s'\n", filename);
1575 #endif
1576 return e;
1579 static void pragma_parse(TCCState *s1)
1581 next_nomacro();
1582 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1583 int t = tok, v;
1584 Sym *s;
1586 if (next(), tok != '(')
1587 goto pragma_err;
1588 if (next(), tok != TOK_STR)
1589 goto pragma_err;
1590 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1591 if (next(), tok != ')')
1592 goto pragma_err;
1593 if (t == TOK_push_macro) {
1594 while (NULL == (s = define_find(v)))
1595 define_push(v, 0, NULL, NULL);
1596 s->type.ref = s; /* set push boundary */
1597 } else {
1598 for (s = define_stack; s; s = s->prev)
1599 if (s->v == v && s->type.ref == s) {
1600 s->type.ref = NULL;
1601 break;
1604 if (s)
1605 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1606 else
1607 tcc_warning("unbalanced #pragma pop_macro");
1608 pp_debug_tok = t, pp_debug_symv = v;
1610 } else if (tok == TOK_once) {
1611 search_cached_include(s1, file->filename, 1)->once = pp_once;
1613 } else if (s1->ppfp) {
1614 /* tcc -E: keep pragmas below unchanged */
1615 unget_tok(' ');
1616 unget_tok(TOK_PRAGMA);
1617 unget_tok('#');
1618 unget_tok(TOK_LINEFEED);
1620 } else if (tok == TOK_pack) {
1621 /* This may be:
1622 #pragma pack(1) // set
1623 #pragma pack() // reset to default
1624 #pragma pack(push,1) // push & set
1625 #pragma pack(pop) // restore previous */
1626 next();
1627 skip('(');
1628 if (tok == TOK_ASM_pop) {
1629 next();
1630 if (s1->pack_stack_ptr <= s1->pack_stack) {
1631 stk_error:
1632 tcc_error("out of pack stack");
1634 s1->pack_stack_ptr--;
1635 } else {
1636 int val = 0;
1637 if (tok != ')') {
1638 if (tok == TOK_ASM_push) {
1639 next();
1640 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1641 goto stk_error;
1642 s1->pack_stack_ptr++;
1643 skip(',');
1645 if (tok != TOK_CINT)
1646 goto pragma_err;
1647 val = tokc.i;
1648 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1649 goto pragma_err;
1650 next();
1652 *s1->pack_stack_ptr = val;
1654 if (tok != ')')
1655 goto pragma_err;
1657 } else if (tok == TOK_comment) {
1658 char *file;
1659 next();
1660 skip('(');
1661 if (tok != TOK_lib)
1662 goto pragma_warn;
1663 next();
1664 skip(',');
1665 if (tok != TOK_STR)
1666 goto pragma_err;
1667 file = tcc_strdup((char *)tokc.str.data);
1668 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1669 next();
1670 if (tok != ')')
1671 goto pragma_err;
1672 } else {
1673 pragma_warn:
1674 if (s1->warn_unsupported)
1675 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1677 return;
1679 pragma_err:
1680 tcc_error("malformed #pragma directive");
1681 return;
1684 /* is_bof is true if first non space token at beginning of file */
1685 ST_FUNC void preprocess(int is_bof)
1687 TCCState *s1 = tcc_state;
1688 int i, c, n, saved_parse_flags;
1689 char buf[1024], *q;
1690 Sym *s;
1692 saved_parse_flags = parse_flags;
1693 parse_flags = PARSE_FLAG_PREPROCESS
1694 | PARSE_FLAG_TOK_NUM
1695 | PARSE_FLAG_TOK_STR
1696 | PARSE_FLAG_LINEFEED
1697 | (parse_flags & PARSE_FLAG_ASM_FILE)
1700 next_nomacro();
1701 redo:
1702 switch(tok) {
1703 case TOK_DEFINE:
1704 pp_debug_tok = tok;
1705 next_nomacro();
1706 pp_debug_symv = tok;
1707 parse_define();
1708 break;
1709 case TOK_UNDEF:
1710 pp_debug_tok = tok;
1711 next_nomacro();
1712 pp_debug_symv = tok;
1713 s = define_find(tok);
1714 /* undefine symbol by putting an invalid name */
1715 if (s)
1716 define_undef(s);
1717 break;
1718 case TOK_INCLUDE:
1719 case TOK_INCLUDE_NEXT:
1720 ch = file->buf_ptr[0];
1721 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1722 skip_spaces();
1723 if (ch == '<') {
1724 c = '>';
1725 goto read_name;
1726 } else if (ch == '\"') {
1727 c = ch;
1728 read_name:
1729 inp();
1730 q = buf;
1731 while (ch != c && ch != '\n' && ch != CH_EOF) {
1732 if ((q - buf) < sizeof(buf) - 1)
1733 *q++ = ch;
1734 if (ch == '\\') {
1735 if (handle_stray_noerror() == 0)
1736 --q;
1737 } else
1738 inp();
1740 *q = '\0';
1741 minp();
1742 #if 0
1743 /* eat all spaces and comments after include */
1744 /* XXX: slightly incorrect */
1745 while (ch1 != '\n' && ch1 != CH_EOF)
1746 inp();
1747 #endif
1748 } else {
1749 int len;
1750 /* computed #include : concatenate everything up to linefeed,
1751 the result must be one of the two accepted forms.
1752 Don't convert pp-tokens to tokens here. */
1753 parse_flags = (PARSE_FLAG_PREPROCESS
1754 | PARSE_FLAG_LINEFEED
1755 | (parse_flags & PARSE_FLAG_ASM_FILE));
1756 next();
1757 buf[0] = '\0';
1758 while (tok != TOK_LINEFEED) {
1759 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1760 next();
1762 len = strlen(buf);
1763 /* check syntax and remove '<>|""' */
1764 if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') &&
1765 (buf[0] != '<' || buf[len-1] != '>'))))
1766 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1767 c = buf[len-1];
1768 memmove(buf, buf + 1, len - 2);
1769 buf[len - 2] = '\0';
1772 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1773 tcc_error("#include recursion too deep");
1774 /* store current file in stack, but increment stack later below */
1775 *s1->include_stack_ptr = file;
1776 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1777 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1778 for (; i < n; ++i) {
1779 char buf1[sizeof file->filename];
1780 CachedInclude *e;
1781 const char *path;
1783 if (i == 0) {
1784 /* check absolute include path */
1785 if (!IS_ABSPATH(buf))
1786 continue;
1787 buf1[0] = 0;
1789 } else if (i == 1) {
1790 /* search in file's dir if "header.h" */
1791 if (c != '\"')
1792 continue;
1793 path = file->filename;
1794 pstrncpy(buf1, path, tcc_basename(path) - path);
1796 } else {
1797 /* search in all the include paths */
1798 int j = i - 2, k = j - s1->nb_include_paths;
1799 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1800 pstrcpy(buf1, sizeof(buf1), path);
1801 pstrcat(buf1, sizeof(buf1), "/");
1804 pstrcat(buf1, sizeof(buf1), buf);
1805 e = search_cached_include(s1, buf1, 0);
1806 if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
1807 /* no need to parse the include because the 'ifndef macro'
1808 is defined (or had #pragma once) */
1809 #ifdef INC_DEBUG
1810 printf("%s: skipping cached %s\n", file->filename, buf1);
1811 #endif
1812 goto include_done;
1815 if (tcc_open(s1, buf1) < 0)
1816 continue;
1818 file->include_next_index = i + 1;
1819 #ifdef INC_DEBUG
1820 printf("%s: including %s\n", file->prev->filename, file->filename);
1821 #endif
1822 /* update target deps */
1823 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1824 tcc_strdup(buf1));
1825 /* push current file in stack */
1826 ++s1->include_stack_ptr;
1827 /* add include file debug info */
1828 if (s1->do_debug)
1829 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1830 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1831 ch = file->buf_ptr[0];
1832 goto the_end;
1834 tcc_error("include file '%s' not found", buf);
1835 include_done:
1836 break;
1837 case TOK_IFNDEF:
1838 c = 1;
1839 goto do_ifdef;
1840 case TOK_IF:
1841 c = expr_preprocess();
1842 goto do_if;
1843 case TOK_IFDEF:
1844 c = 0;
1845 do_ifdef:
1846 next_nomacro();
1847 if (tok < TOK_IDENT)
1848 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1849 if (is_bof) {
1850 if (c) {
1851 #ifdef INC_DEBUG
1852 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1853 #endif
1854 file->ifndef_macro = tok;
1857 c = (define_find(tok) != 0) ^ c;
1858 do_if:
1859 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1860 tcc_error("memory full (ifdef)");
1861 *s1->ifdef_stack_ptr++ = c;
1862 goto test_skip;
1863 case TOK_ELSE:
1864 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1865 tcc_error("#else without matching #if");
1866 if (s1->ifdef_stack_ptr[-1] & 2)
1867 tcc_error("#else after #else");
1868 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1869 goto test_else;
1870 case TOK_ELIF:
1871 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1872 tcc_error("#elif without matching #if");
1873 c = s1->ifdef_stack_ptr[-1];
1874 if (c > 1)
1875 tcc_error("#elif after #else");
1876 /* last #if/#elif expression was true: we skip */
1877 if (c == 1) {
1878 c = 0;
1879 } else {
1880 c = expr_preprocess();
1881 s1->ifdef_stack_ptr[-1] = c;
1883 test_else:
1884 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1885 file->ifndef_macro = 0;
1886 test_skip:
1887 if (!(c & 1)) {
1888 preprocess_skip();
1889 is_bof = 0;
1890 goto redo;
1892 break;
1893 case TOK_ENDIF:
1894 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1895 tcc_error("#endif without matching #if");
1896 s1->ifdef_stack_ptr--;
1897 /* '#ifndef macro' was at the start of file. Now we check if
1898 an '#endif' is exactly at the end of file */
1899 if (file->ifndef_macro &&
1900 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1901 file->ifndef_macro_saved = file->ifndef_macro;
1902 /* need to set to zero to avoid false matches if another
1903 #ifndef at middle of file */
1904 file->ifndef_macro = 0;
1905 while (tok != TOK_LINEFEED)
1906 next_nomacro();
1907 tok_flags |= TOK_FLAG_ENDIF;
1908 goto the_end;
1910 break;
1911 case TOK_PPNUM:
1912 n = strtoul((char*)tokc.str.data, &q, 10);
1913 goto _line_num;
1914 case TOK_LINE:
1915 next();
1916 if (tok != TOK_CINT)
1917 _line_err:
1918 tcc_error("wrong #line format");
1919 n = tokc.i;
1920 _line_num:
1921 next();
1922 if (tok != TOK_LINEFEED) {
1923 if (tok == TOK_STR)
1924 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1925 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1926 break;
1927 else
1928 goto _line_err;
1929 --n;
1931 if (file->fd > 0)
1932 total_lines += file->line_num - n;
1933 file->line_num = n;
1934 if (s1->do_debug)
1935 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1936 break;
1937 case TOK_ERROR:
1938 case TOK_WARNING:
1939 c = tok;
1940 ch = file->buf_ptr[0];
1941 skip_spaces();
1942 q = buf;
1943 while (ch != '\n' && ch != CH_EOF) {
1944 if ((q - buf) < sizeof(buf) - 1)
1945 *q++ = ch;
1946 if (ch == '\\') {
1947 if (handle_stray_noerror() == 0)
1948 --q;
1949 } else
1950 inp();
1952 *q = '\0';
1953 if (c == TOK_ERROR)
1954 tcc_error("#error %s", buf);
1955 else
1956 tcc_warning("#warning %s", buf);
1957 break;
1958 case TOK_PRAGMA:
1959 pragma_parse(s1);
1960 break;
1961 case TOK_LINEFEED:
1962 goto the_end;
1963 default:
1964 /* ignore gas line comment in an 'S' file. */
1965 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1966 goto ignore;
1967 if (tok == '!' && is_bof)
1968 /* '!' is ignored at beginning to allow C scripts. */
1969 goto ignore;
1970 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1971 ignore:
1972 file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
1973 goto the_end;
1975 /* ignore other preprocess commands or #! for C scripts */
1976 while (tok != TOK_LINEFEED)
1977 next_nomacro();
1978 the_end:
1979 parse_flags = saved_parse_flags;
1982 /* evaluate escape codes in a string. */
1983 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1985 int c, n;
1986 const uint8_t *p;
1988 p = buf;
1989 for(;;) {
1990 c = *p;
1991 if (c == '\0')
1992 break;
1993 if (c == '\\') {
1994 p++;
1995 /* escape */
1996 c = *p;
1997 switch(c) {
1998 case '0': case '1': case '2': case '3':
1999 case '4': case '5': case '6': case '7':
2000 /* at most three octal digits */
2001 n = c - '0';
2002 p++;
2003 c = *p;
2004 if (isoct(c)) {
2005 n = n * 8 + c - '0';
2006 p++;
2007 c = *p;
2008 if (isoct(c)) {
2009 n = n * 8 + c - '0';
2010 p++;
2013 c = n;
2014 goto add_char_nonext;
2015 case 'x':
2016 case 'u':
2017 case 'U':
2018 p++;
2019 n = 0;
2020 for(;;) {
2021 c = *p;
2022 if (c >= 'a' && c <= 'f')
2023 c = c - 'a' + 10;
2024 else if (c >= 'A' && c <= 'F')
2025 c = c - 'A' + 10;
2026 else if (isnum(c))
2027 c = c - '0';
2028 else
2029 break;
2030 n = n * 16 + c;
2031 p++;
2033 c = n;
2034 goto add_char_nonext;
2035 case 'a':
2036 c = '\a';
2037 break;
2038 case 'b':
2039 c = '\b';
2040 break;
2041 case 'f':
2042 c = '\f';
2043 break;
2044 case 'n':
2045 c = '\n';
2046 break;
2047 case 'r':
2048 c = '\r';
2049 break;
2050 case 't':
2051 c = '\t';
2052 break;
2053 case 'v':
2054 c = '\v';
2055 break;
2056 case 'e':
2057 if (!gnu_ext)
2058 goto invalid_escape;
2059 c = 27;
2060 break;
2061 case '\'':
2062 case '\"':
2063 case '\\':
2064 case '?':
2065 break;
2066 default:
2067 invalid_escape:
2068 if (c >= '!' && c <= '~')
2069 tcc_warning("unknown escape sequence: \'\\%c\'", c);
2070 else
2071 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
2072 break;
2075 p++;
2076 add_char_nonext:
2077 if (!is_long)
2078 cstr_ccat(outstr, c);
2079 else
2080 cstr_wccat(outstr, c);
2082 /* add a trailing '\0' */
2083 if (!is_long)
2084 cstr_ccat(outstr, '\0');
2085 else
2086 cstr_wccat(outstr, '\0');
2089 static void parse_string(const char *s, int len)
2091 uint8_t buf[1000], *p = buf;
2092 int is_long, sep;
2094 if ((is_long = *s == 'L'))
2095 ++s, --len;
2096 sep = *s++;
2097 len -= 2;
2098 if (len >= sizeof buf)
2099 p = tcc_malloc(len + 1);
2100 memcpy(p, s, len);
2101 p[len] = 0;
2103 cstr_reset(&tokcstr);
2104 parse_escape_string(&tokcstr, p, is_long);
2105 if (p != buf)
2106 tcc_free(p);
2108 if (sep == '\'') {
2109 int char_size;
2110 /* XXX: make it portable */
2111 if (!is_long)
2112 char_size = 1;
2113 else
2114 char_size = sizeof(nwchar_t);
2115 if (tokcstr.size <= char_size)
2116 tcc_error("empty character constant");
2117 if (tokcstr.size > 2 * char_size)
2118 tcc_warning("multi-character character constant");
2119 if (!is_long) {
2120 tokc.i = *(int8_t *)tokcstr.data;
2121 tok = TOK_CCHAR;
2122 } else {
2123 tokc.i = *(nwchar_t *)tokcstr.data;
2124 tok = TOK_LCHAR;
2126 } else {
2127 tokc.str.size = tokcstr.size;
2128 tokc.str.data = tokcstr.data;
2129 if (!is_long)
2130 tok = TOK_STR;
2131 else
2132 tok = TOK_LSTR;
2136 /* we use 64 bit numbers */
2137 #define BN_SIZE 2
2139 /* bn = (bn << shift) | or_val */
2140 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2142 int i;
2143 unsigned int v;
2144 for(i=0;i<BN_SIZE;i++) {
2145 v = bn[i];
2146 bn[i] = (v << shift) | or_val;
2147 or_val = v >> (32 - shift);
2151 static void bn_zero(unsigned int *bn)
2153 int i;
2154 for(i=0;i<BN_SIZE;i++) {
2155 bn[i] = 0;
2159 /* parse number in null terminated string 'p' and return it in the
2160 current token */
2161 static void parse_number(const char *p)
2163 int b, t, shift, frac_bits, s, exp_val, ch;
2164 char *q;
2165 unsigned int bn[BN_SIZE];
2166 double d;
2168 /* number */
2169 q = token_buf;
2170 ch = *p++;
2171 t = ch;
2172 ch = *p++;
2173 *q++ = t;
2174 b = 10;
2175 if (t == '.') {
2176 goto float_frac_parse;
2177 } else if (t == '0') {
2178 if (ch == 'x' || ch == 'X') {
2179 q--;
2180 ch = *p++;
2181 b = 16;
2182 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2183 q--;
2184 ch = *p++;
2185 b = 2;
2188 /* parse all digits. cannot check octal numbers at this stage
2189 because of floating point constants */
2190 while (1) {
2191 if (ch >= 'a' && ch <= 'f')
2192 t = ch - 'a' + 10;
2193 else if (ch >= 'A' && ch <= 'F')
2194 t = ch - 'A' + 10;
2195 else if (isnum(ch))
2196 t = ch - '0';
2197 else
2198 break;
2199 if (t >= b)
2200 break;
2201 if (q >= token_buf + STRING_MAX_SIZE) {
2202 num_too_long:
2203 tcc_error("number too long");
2205 *q++ = ch;
2206 ch = *p++;
2208 if (ch == '.' ||
2209 ((ch == 'e' || ch == 'E') && b == 10) ||
2210 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2211 if (b != 10) {
2212 /* NOTE: strtox should support that for hexa numbers, but
2213 non ISOC99 libcs do not support it, so we prefer to do
2214 it by hand */
2215 /* hexadecimal or binary floats */
2216 /* XXX: handle overflows */
2217 *q = '\0';
2218 if (b == 16)
2219 shift = 4;
2220 else
2221 shift = 1;
2222 bn_zero(bn);
2223 q = token_buf;
2224 while (1) {
2225 t = *q++;
2226 if (t == '\0') {
2227 break;
2228 } else if (t >= 'a') {
2229 t = t - 'a' + 10;
2230 } else if (t >= 'A') {
2231 t = t - 'A' + 10;
2232 } else {
2233 t = t - '0';
2235 bn_lshift(bn, shift, t);
2237 frac_bits = 0;
2238 if (ch == '.') {
2239 ch = *p++;
2240 while (1) {
2241 t = ch;
2242 if (t >= 'a' && t <= 'f') {
2243 t = t - 'a' + 10;
2244 } else if (t >= 'A' && t <= 'F') {
2245 t = t - 'A' + 10;
2246 } else if (t >= '0' && t <= '9') {
2247 t = t - '0';
2248 } else {
2249 break;
2251 if (t >= b)
2252 tcc_error("invalid digit");
2253 bn_lshift(bn, shift, t);
2254 frac_bits += shift;
2255 ch = *p++;
2258 if (ch != 'p' && ch != 'P')
2259 expect("exponent");
2260 ch = *p++;
2261 s = 1;
2262 exp_val = 0;
2263 if (ch == '+') {
2264 ch = *p++;
2265 } else if (ch == '-') {
2266 s = -1;
2267 ch = *p++;
2269 if (ch < '0' || ch > '9')
2270 expect("exponent digits");
2271 while (ch >= '0' && ch <= '9') {
2272 exp_val = exp_val * 10 + ch - '0';
2273 ch = *p++;
2275 exp_val = exp_val * s;
2277 /* now we can generate the number */
2278 /* XXX: should patch directly float number */
2279 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2280 d = ldexp(d, exp_val - frac_bits);
2281 t = toup(ch);
2282 if (t == 'F') {
2283 ch = *p++;
2284 tok = TOK_CFLOAT;
2285 /* float : should handle overflow */
2286 tokc.f = (float)d;
2287 } else if (t == 'L') {
2288 ch = *p++;
2289 #ifdef TCC_TARGET_PE
2290 tok = TOK_CDOUBLE;
2291 tokc.d = d;
2292 #else
2293 tok = TOK_CLDOUBLE;
2294 /* XXX: not large enough */
2295 tokc.ld = (long double)d;
2296 #endif
2297 } else {
2298 tok = TOK_CDOUBLE;
2299 tokc.d = d;
2301 } else {
2302 /* decimal floats */
2303 if (ch == '.') {
2304 if (q >= token_buf + STRING_MAX_SIZE)
2305 goto num_too_long;
2306 *q++ = ch;
2307 ch = *p++;
2308 float_frac_parse:
2309 while (ch >= '0' && ch <= '9') {
2310 if (q >= token_buf + STRING_MAX_SIZE)
2311 goto num_too_long;
2312 *q++ = ch;
2313 ch = *p++;
2316 if (ch == 'e' || ch == 'E') {
2317 if (q >= token_buf + STRING_MAX_SIZE)
2318 goto num_too_long;
2319 *q++ = ch;
2320 ch = *p++;
2321 if (ch == '-' || ch == '+') {
2322 if (q >= token_buf + STRING_MAX_SIZE)
2323 goto num_too_long;
2324 *q++ = ch;
2325 ch = *p++;
2327 if (ch < '0' || ch > '9')
2328 expect("exponent digits");
2329 while (ch >= '0' && ch <= '9') {
2330 if (q >= token_buf + STRING_MAX_SIZE)
2331 goto num_too_long;
2332 *q++ = ch;
2333 ch = *p++;
2336 *q = '\0';
2337 t = toup(ch);
2338 errno = 0;
2339 if (t == 'F') {
2340 ch = *p++;
2341 tok = TOK_CFLOAT;
2342 tokc.f = strtof(token_buf, NULL);
2343 } else if (t == 'L') {
2344 ch = *p++;
2345 #ifdef TCC_TARGET_PE
2346 tok = TOK_CDOUBLE;
2347 tokc.d = strtod(token_buf, NULL);
2348 #else
2349 tok = TOK_CLDOUBLE;
2350 tokc.ld = strtold(token_buf, NULL);
2351 #endif
2352 } else {
2353 tok = TOK_CDOUBLE;
2354 tokc.d = strtod(token_buf, NULL);
2357 } else {
2358 unsigned long long n, n1;
2359 int lcount, ucount, must_64bit;
2360 const char *p1;
2362 /* integer number */
2363 *q = '\0';
2364 q = token_buf;
2365 if (b == 10 && *q == '0') {
2366 b = 8;
2367 q++;
2369 n = 0;
2370 while(1) {
2371 t = *q++;
2372 /* no need for checks except for base 10 / 8 errors */
2373 if (t == '\0')
2374 break;
2375 else if (t >= 'a')
2376 t = t - 'a' + 10;
2377 else if (t >= 'A')
2378 t = t - 'A' + 10;
2379 else
2380 t = t - '0';
2381 if (t >= b)
2382 tcc_error("invalid digit");
2383 n1 = n;
2384 n = n * b + t;
2385 /* detect overflow */
2386 /* XXX: this test is not reliable */
2387 if (n < n1)
2388 tcc_error("integer constant overflow");
2391 /* Determine the characteristics (unsigned and/or 64bit) the type of
2392 the constant must have according to the constant suffix(es) */
2393 lcount = ucount = must_64bit = 0;
2394 p1 = p;
2395 for(;;) {
2396 t = toup(ch);
2397 if (t == 'L') {
2398 if (lcount >= 2)
2399 tcc_error("three 'l's in integer constant");
2400 if (lcount && *(p - 1) != ch)
2401 tcc_error("incorrect integer suffix: %s", p1);
2402 lcount++;
2403 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2404 if (lcount == 2)
2405 #endif
2406 must_64bit = 1;
2407 ch = *p++;
2408 } else if (t == 'U') {
2409 if (ucount >= 1)
2410 tcc_error("two 'u's in integer constant");
2411 ucount++;
2412 ch = *p++;
2413 } else {
2414 break;
2418 /* Whether 64 bits are needed to hold the constant's value */
2419 if (n & 0xffffffff00000000LL || must_64bit) {
2420 tok = TOK_CLLONG;
2421 n1 = n >> 32;
2422 } else {
2423 tok = TOK_CINT;
2424 n1 = n;
2427 /* Whether type must be unsigned to hold the constant's value */
2428 if (ucount || ((n1 >> 31) && (b != 10))) {
2429 if (tok == TOK_CLLONG)
2430 tok = TOK_CULLONG;
2431 else
2432 tok = TOK_CUINT;
2433 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2434 } else if (n1 >> 31) {
2435 if (tok == TOK_CINT)
2436 tok = TOK_CLLONG;
2437 else
2438 tcc_error("integer constant overflow");
2441 tokc.i = n;
2443 if (ch)
2444 tcc_error("invalid number\n");
2448 #define PARSE2(c1, tok1, c2, tok2) \
2449 case c1: \
2450 PEEKC(c, p); \
2451 if (c == c2) { \
2452 p++; \
2453 tok = tok2; \
2454 } else { \
2455 tok = tok1; \
2457 break;
2459 /* return next token without macro substitution */
2460 static inline void next_nomacro1(void)
2462 int t, c, is_long, len;
2463 TokenSym *ts;
2464 uint8_t *p, *p1;
2465 unsigned int h;
2467 p = file->buf_ptr;
2468 redo_no_start:
2469 c = *p;
2470 switch(c) {
2471 case ' ':
2472 case '\t':
2473 tok = c;
2474 p++;
2475 if (parse_flags & PARSE_FLAG_SPACES)
2476 goto keep_tok_flags;
2477 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2478 ++p;
2479 goto redo_no_start;
2480 case '\f':
2481 case '\v':
2482 case '\r':
2483 p++;
2484 goto redo_no_start;
2485 case '\\':
2486 /* first look if it is in fact an end of buffer */
2487 c = handle_stray1(p);
2488 p = file->buf_ptr;
2489 if (c == '\\')
2490 goto parse_simple;
2491 if (c != CH_EOF)
2492 goto redo_no_start;
2494 TCCState *s1 = tcc_state;
2495 if ((parse_flags & PARSE_FLAG_LINEFEED)
2496 && !(tok_flags & TOK_FLAG_EOF)) {
2497 tok_flags |= TOK_FLAG_EOF;
2498 tok = TOK_LINEFEED;
2499 goto keep_tok_flags;
2500 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2501 tok = TOK_EOF;
2502 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2503 tcc_error("missing #endif");
2504 } else if (s1->include_stack_ptr == s1->include_stack) {
2505 /* no include left : end of file. */
2506 tok = TOK_EOF;
2507 } else {
2508 tok_flags &= ~TOK_FLAG_EOF;
2509 /* pop include file */
2511 /* test if previous '#endif' was after a #ifdef at
2512 start of file */
2513 if (tok_flags & TOK_FLAG_ENDIF) {
2514 #ifdef INC_DEBUG
2515 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2516 #endif
2517 search_cached_include(s1, file->filename, 1)
2518 ->ifndef_macro = file->ifndef_macro_saved;
2519 tok_flags &= ~TOK_FLAG_ENDIF;
2522 /* add end of include file debug info */
2523 if (tcc_state->do_debug) {
2524 put_stabd(N_EINCL, 0, 0);
2526 /* pop include stack */
2527 tcc_close();
2528 s1->include_stack_ptr--;
2529 p = file->buf_ptr;
2530 goto redo_no_start;
2533 break;
2535 case '\n':
2536 file->line_num++;
2537 tok_flags |= TOK_FLAG_BOL;
2538 p++;
2539 maybe_newline:
2540 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2541 goto redo_no_start;
2542 tok = TOK_LINEFEED;
2543 goto keep_tok_flags;
2545 case '#':
2546 /* XXX: simplify */
2547 PEEKC(c, p);
2548 if ((tok_flags & TOK_FLAG_BOL) &&
2549 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2550 file->buf_ptr = p;
2551 preprocess(tok_flags & TOK_FLAG_BOF);
2552 p = file->buf_ptr;
2553 goto maybe_newline;
2554 } else {
2555 if (c == '#') {
2556 p++;
2557 tok = TOK_TWOSHARPS;
2558 } else {
2559 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2560 p = parse_line_comment(p - 1);
2561 goto redo_no_start;
2562 } else {
2563 tok = '#';
2567 break;
2569 /* dollar is allowed to start identifiers when not parsing asm */
2570 case '$':
2571 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2572 || (parse_flags & PARSE_FLAG_ASM_FILE))
2573 goto parse_simple;
2575 case 'a': case 'b': case 'c': case 'd':
2576 case 'e': case 'f': case 'g': case 'h':
2577 case 'i': case 'j': case 'k': case 'l':
2578 case 'm': case 'n': case 'o': case 'p':
2579 case 'q': case 'r': case 's': case 't':
2580 case 'u': case 'v': case 'w': case 'x':
2581 case 'y': case 'z':
2582 case 'A': case 'B': case 'C': case 'D':
2583 case 'E': case 'F': case 'G': case 'H':
2584 case 'I': case 'J': case 'K':
2585 case 'M': case 'N': case 'O': case 'P':
2586 case 'Q': case 'R': case 'S': case 'T':
2587 case 'U': case 'V': case 'W': case 'X':
2588 case 'Y': case 'Z':
2589 case '_':
2590 parse_ident_fast:
2591 p1 = p;
2592 h = TOK_HASH_INIT;
2593 h = TOK_HASH_FUNC(h, c);
2594 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2595 h = TOK_HASH_FUNC(h, c);
2596 len = p - p1;
2597 if (c != '\\') {
2598 TokenSym **pts;
2600 /* fast case : no stray found, so we have the full token
2601 and we have already hashed it */
2602 h &= (TOK_HASH_SIZE - 1);
2603 pts = &hash_ident[h];
2604 for(;;) {
2605 ts = *pts;
2606 if (!ts)
2607 break;
2608 if (ts->len == len && !memcmp(ts->str, p1, len))
2609 goto token_found;
2610 pts = &(ts->hash_next);
2612 ts = tok_alloc_new(pts, (char *) p1, len);
2613 token_found: ;
2614 } else {
2615 /* slower case */
2616 cstr_reset(&tokcstr);
2617 cstr_cat(&tokcstr, p1, len);
2618 p--;
2619 PEEKC(c, p);
2620 parse_ident_slow:
2621 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2623 cstr_ccat(&tokcstr, c);
2624 PEEKC(c, p);
2626 ts = tok_alloc(tokcstr.data, tokcstr.size);
2628 tok = ts->tok;
2629 break;
2630 case 'L':
2631 t = p[1];
2632 if (t != '\\' && t != '\'' && t != '\"') {
2633 /* fast case */
2634 goto parse_ident_fast;
2635 } else {
2636 PEEKC(c, p);
2637 if (c == '\'' || c == '\"') {
2638 is_long = 1;
2639 goto str_const;
2640 } else {
2641 cstr_reset(&tokcstr);
2642 cstr_ccat(&tokcstr, 'L');
2643 goto parse_ident_slow;
2646 break;
2648 case '0': case '1': case '2': case '3':
2649 case '4': case '5': case '6': case '7':
2650 case '8': case '9':
2651 t = c;
2652 PEEKC(c, p);
2653 /* after the first digit, accept digits, alpha, '.' or sign if
2654 prefixed by 'eEpP' */
2655 parse_num:
2656 cstr_reset(&tokcstr);
2657 for(;;) {
2658 cstr_ccat(&tokcstr, t);
2659 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2660 || c == '.'
2661 || ((c == '+' || c == '-')
2662 && (((t == 'e' || t == 'E')
2663 && !(parse_flags & PARSE_FLAG_ASM_FILE
2664 /* 0xe+1 is 3 tokens in asm */
2665 && ((char*)tokcstr.data)[0] == '0'
2666 && toup(((char*)tokcstr.data)[1]) == 'X'))
2667 || t == 'p' || t == 'P'))))
2668 break;
2669 t = c;
2670 PEEKC(c, p);
2672 /* We add a trailing '\0' to ease parsing */
2673 cstr_ccat(&tokcstr, '\0');
2674 tokc.str.size = tokcstr.size;
2675 tokc.str.data = tokcstr.data;
2676 tok = TOK_PPNUM;
2677 break;
2679 case '.':
2680 /* special dot handling because it can also start a number */
2681 PEEKC(c, p);
2682 if (isnum(c)) {
2683 t = '.';
2684 goto parse_num;
2685 } else if ((isidnum_table['.' - CH_EOF] & IS_ID)
2686 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
2687 *--p = c = '.';
2688 goto parse_ident_fast;
2689 } else if (c == '.') {
2690 PEEKC(c, p);
2691 if (c == '.') {
2692 p++;
2693 tok = TOK_DOTS;
2694 } else {
2695 *--p = '.'; /* may underflow into file->unget[] */
2696 tok = '.';
2698 } else {
2699 tok = '.';
2701 break;
2702 case '\'':
2703 case '\"':
2704 is_long = 0;
2705 str_const:
2706 cstr_reset(&tokcstr);
2707 if (is_long)
2708 cstr_ccat(&tokcstr, 'L');
2709 cstr_ccat(&tokcstr, c);
2710 p = parse_pp_string(p, c, &tokcstr);
2711 cstr_ccat(&tokcstr, c);
2712 cstr_ccat(&tokcstr, '\0');
2713 tokc.str.size = tokcstr.size;
2714 tokc.str.data = tokcstr.data;
2715 tok = TOK_PPSTR;
2716 break;
2718 case '<':
2719 PEEKC(c, p);
2720 if (c == '=') {
2721 p++;
2722 tok = TOK_LE;
2723 } else if (c == '<') {
2724 PEEKC(c, p);
2725 if (c == '=') {
2726 p++;
2727 tok = TOK_A_SHL;
2728 } else {
2729 tok = TOK_SHL;
2731 } else {
2732 tok = TOK_LT;
2734 break;
2735 case '>':
2736 PEEKC(c, p);
2737 if (c == '=') {
2738 p++;
2739 tok = TOK_GE;
2740 } else if (c == '>') {
2741 PEEKC(c, p);
2742 if (c == '=') {
2743 p++;
2744 tok = TOK_A_SAR;
2745 } else {
2746 tok = TOK_SAR;
2748 } else {
2749 tok = TOK_GT;
2751 break;
2753 case '&':
2754 PEEKC(c, p);
2755 if (c == '&') {
2756 p++;
2757 tok = TOK_LAND;
2758 } else if (c == '=') {
2759 p++;
2760 tok = TOK_A_AND;
2761 } else {
2762 tok = '&';
2764 break;
2766 case '|':
2767 PEEKC(c, p);
2768 if (c == '|') {
2769 p++;
2770 tok = TOK_LOR;
2771 } else if (c == '=') {
2772 p++;
2773 tok = TOK_A_OR;
2774 } else {
2775 tok = '|';
2777 break;
2779 case '+':
2780 PEEKC(c, p);
2781 if (c == '+') {
2782 p++;
2783 tok = TOK_INC;
2784 } else if (c == '=') {
2785 p++;
2786 tok = TOK_A_ADD;
2787 } else {
2788 tok = '+';
2790 break;
2792 case '-':
2793 PEEKC(c, p);
2794 if (c == '-') {
2795 p++;
2796 tok = TOK_DEC;
2797 } else if (c == '=') {
2798 p++;
2799 tok = TOK_A_SUB;
2800 } else if (c == '>') {
2801 p++;
2802 tok = TOK_ARROW;
2803 } else {
2804 tok = '-';
2806 break;
2808 PARSE2('!', '!', '=', TOK_NE)
2809 PARSE2('=', '=', '=', TOK_EQ)
2810 PARSE2('*', '*', '=', TOK_A_MUL)
2811 PARSE2('%', '%', '=', TOK_A_MOD)
2812 PARSE2('^', '^', '=', TOK_A_XOR)
2814 /* comments or operator */
2815 case '/':
2816 PEEKC(c, p);
2817 if (c == '*') {
2818 p = parse_comment(p);
2819 /* comments replaced by a blank */
2820 tok = ' ';
2821 goto keep_tok_flags;
2822 } else if (c == '/') {
2823 p = parse_line_comment(p);
2824 tok = ' ';
2825 goto keep_tok_flags;
2826 } else if (c == '=') {
2827 p++;
2828 tok = TOK_A_DIV;
2829 } else {
2830 tok = '/';
2832 break;
2834 /* simple tokens */
2835 case '(':
2836 case ')':
2837 case '[':
2838 case ']':
2839 case '{':
2840 case '}':
2841 case ',':
2842 case ';':
2843 case ':':
2844 case '?':
2845 case '~':
2846 case '@': /* only used in assembler */
2847 parse_simple:
2848 tok = c;
2849 p++;
2850 break;
2851 default:
2852 if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
2853 goto parse_ident_fast;
2854 if (parse_flags & PARSE_FLAG_ASM_FILE)
2855 goto parse_simple;
2856 tcc_error("unrecognized character \\x%02x", c);
2857 break;
2859 tok_flags = 0;
2860 keep_tok_flags:
2861 file->buf_ptr = p;
2862 #if defined(PARSE_DEBUG)
2863 printf("token = %s\n", get_tok_str(tok, &tokc));
2864 #endif
2867 /* return next token without macro substitution. Can read input from
2868 macro_ptr buffer */
2869 static void next_nomacro_spc(void)
2871 if (macro_ptr) {
2872 redo:
2873 tok = *macro_ptr;
2874 if (tok) {
2875 TOK_GET(&tok, &macro_ptr, &tokc);
2876 if (tok == TOK_LINENUM) {
2877 file->line_num = tokc.i;
2878 goto redo;
2881 } else {
2882 next_nomacro1();
2886 ST_FUNC void next_nomacro(void)
2888 do {
2889 next_nomacro_spc();
2890 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2894 static void macro_subst(
2895 TokenString *tok_str,
2896 Sym **nested_list,
2897 const int *macro_str,
2898 int can_read_stream
2901 /* substitute arguments in replacement lists in macro_str by the values in
2902 args (field d) and return allocated string */
2903 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2905 int t, t0, t1, spc;
2906 const int *st;
2907 Sym *s;
2908 CValue cval;
2909 TokenString str;
2910 CString cstr;
2912 tok_str_new(&str);
2913 t0 = t1 = 0;
2914 while(1) {
2915 TOK_GET(&t, &macro_str, &cval);
2916 if (!t)
2917 break;
2918 if (t == '#') {
2919 /* stringize */
2920 TOK_GET(&t, &macro_str, &cval);
2921 if (!t)
2922 goto bad_stringy;
2923 s = sym_find2(args, t);
2924 if (s) {
2925 cstr_new(&cstr);
2926 cstr_ccat(&cstr, '\"');
2927 st = s->d;
2928 spc = 0;
2929 while (*st) {
2930 TOK_GET(&t, &st, &cval);
2931 if (t != TOK_PLCHLDR
2932 && t != TOK_NOSUBST
2933 && 0 == check_space(t, &spc)) {
2934 const char *s = get_tok_str(t, &cval);
2935 while (*s) {
2936 if (t == TOK_PPSTR && *s != '\'')
2937 add_char(&cstr, *s);
2938 else
2939 cstr_ccat(&cstr, *s);
2940 ++s;
2944 cstr.size -= spc;
2945 cstr_ccat(&cstr, '\"');
2946 cstr_ccat(&cstr, '\0');
2947 #ifdef PP_DEBUG
2948 printf("\nstringize: <%s>\n", (char *)cstr.data);
2949 #endif
2950 /* add string */
2951 cval.str.size = cstr.size;
2952 cval.str.data = cstr.data;
2953 tok_str_add2(&str, TOK_PPSTR, &cval);
2954 cstr_free(&cstr);
2955 } else {
2956 bad_stringy:
2957 expect("macro parameter after '#'");
2959 } else if (t >= TOK_IDENT) {
2960 s = sym_find2(args, t);
2961 if (s) {
2962 int l0 = str.len;
2963 st = s->d;
2964 /* if '##' is present before or after, no arg substitution */
2965 if (*macro_str == TOK_PPJOIN || t1 == TOK_PPJOIN) {
2966 /* special case for var arg macros : ## eats the ','
2967 if empty VA_ARGS variable. */
2968 if (t1 == TOK_PPJOIN && t0 == ',' && gnu_ext && s->type.t) {
2969 if (*st == 0) {
2970 /* suppress ',' '##' */
2971 str.len -= 2;
2972 } else {
2973 /* suppress '##' and add variable */
2974 str.len--;
2975 goto add_var;
2977 } else {
2978 for(;;) {
2979 int t1;
2980 TOK_GET(&t1, &st, &cval);
2981 if (!t1)
2982 break;
2983 tok_str_add2(&str, t1, &cval);
2987 } else {
2988 add_var:
2989 /* NOTE: the stream cannot be read when macro
2990 substituing an argument */
2991 macro_subst(&str, nested_list, st, 0);
2993 if (str.len == l0) /* exanded to empty string */
2994 tok_str_add(&str, TOK_PLCHLDR);
2995 } else {
2996 tok_str_add(&str, t);
2998 } else {
2999 tok_str_add2(&str, t, &cval);
3001 t0 = t1, t1 = t;
3003 tok_str_add(&str, 0);
3004 return str.str;
3007 static char const ab_month_name[12][4] =
3009 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3010 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3013 /* peek or read [ws_str == NULL] next token from function macro call,
3014 walking up macro levels up to the file if necessary */
3015 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
3017 int t;
3018 const int *p;
3019 Sym *sa;
3021 for (;;) {
3022 if (macro_ptr) {
3023 p = macro_ptr, t = *p;
3024 if (ws_str) {
3025 while (is_space(t) || TOK_LINEFEED == t)
3026 tok_str_add(ws_str, t), t = *++p;
3028 if (t == 0 && can_read_stream) {
3029 end_macro();
3030 /* also, end of scope for nested defined symbol */
3031 sa = *nested_list;
3032 while (sa && sa->v == 0)
3033 sa = sa->prev;
3034 if (sa)
3035 sa->v = 0;
3036 continue;
3038 } else {
3039 ch = handle_eob();
3040 if (ws_str) {
3041 while (is_space(ch) || ch == '\n' || ch == '/') {
3042 if (ch == '/') {
3043 int c;
3044 uint8_t *p = file->buf_ptr;
3045 PEEKC(c, p);
3046 if (c == '*') {
3047 p = parse_comment(p);
3048 file->buf_ptr = p - 1;
3049 } else if (c == '/') {
3050 p = parse_line_comment(p);
3051 file->buf_ptr = p - 1;
3052 } else
3053 break;
3054 ch = ' ';
3056 if (!(ch == '\f' || ch == '\v' || ch == '\r'))
3057 tok_str_add(ws_str, ch);
3058 cinp();
3061 t = ch;
3064 if (ws_str)
3065 return t;
3066 next_nomacro_spc();
3067 return tok;
3071 /* do macro substitution of current token with macro 's' and add
3072 result to (tok_str,tok_len). 'nested_list' is the list of all
3073 macros we got inside to avoid recursing. Return non zero if no
3074 substitution needs to be done */
3075 static int macro_subst_tok(
3076 TokenString *tok_str,
3077 Sym **nested_list,
3078 Sym *s,
3079 int can_read_stream)
3081 Sym *args, *sa, *sa1;
3082 int parlevel, *mstr, t, t1, spc;
3083 TokenString str;
3084 char *cstrval;
3085 CValue cval;
3086 CString cstr;
3087 char buf[32];
3089 /* if symbol is a macro, prepare substitution */
3090 /* special macros */
3091 if (tok == TOK___LINE__) {
3092 snprintf(buf, sizeof(buf), "%d", file->line_num);
3093 cstrval = buf;
3094 t1 = TOK_PPNUM;
3095 goto add_cstr1;
3096 } else if (tok == TOK___FILE__) {
3097 cstrval = file->filename;
3098 goto add_cstr;
3099 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3100 time_t ti;
3101 struct tm *tm;
3103 time(&ti);
3104 tm = localtime(&ti);
3105 if (tok == TOK___DATE__) {
3106 snprintf(buf, sizeof(buf), "%s %2d %d",
3107 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3108 } else {
3109 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3110 tm->tm_hour, tm->tm_min, tm->tm_sec);
3112 cstrval = buf;
3113 add_cstr:
3114 t1 = TOK_STR;
3115 add_cstr1:
3116 cstr_new(&cstr);
3117 cstr_cat(&cstr, cstrval, 0);
3118 cval.str.size = cstr.size;
3119 cval.str.data = cstr.data;
3120 tok_str_add2(tok_str, t1, &cval);
3121 cstr_free(&cstr);
3122 } else {
3123 int saved_parse_flags = parse_flags;
3125 mstr = s->d;
3126 if (s->type.t == MACRO_FUNC) {
3127 /* whitespace between macro name and argument list */
3128 TokenString ws_str;
3129 tok_str_new(&ws_str);
3131 spc = 0;
3132 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3133 | PARSE_FLAG_ACCEPT_STRAYS;
3135 /* get next token from argument stream */
3136 t = next_argstream(nested_list, can_read_stream, &ws_str);
3137 if (t != '(') {
3138 /* not a macro substitution after all, restore the
3139 * macro token plus all whitespace we've read.
3140 * whitespace is intentionally not merged to preserve
3141 * newlines. */
3142 parse_flags = saved_parse_flags;
3143 tok_str_add(tok_str, tok);
3144 if (parse_flags & PARSE_FLAG_SPACES) {
3145 int i;
3146 for (i = 0; i < ws_str.len; i++)
3147 tok_str_add(tok_str, ws_str.str[i]);
3149 tok_str_free_str(ws_str.str);
3150 return 0;
3151 } else {
3152 tok_str_free_str(ws_str.str);
3154 next_nomacro(); /* eat '(' */
3156 /* argument macro */
3157 args = NULL;
3158 sa = s->next;
3159 /* NOTE: empty args are allowed, except if no args */
3160 for(;;) {
3161 do {
3162 next_argstream(nested_list, can_read_stream, NULL);
3163 } while (is_space(tok) || TOK_LINEFEED == tok);
3164 empty_arg:
3165 /* handle '()' case */
3166 if (!args && !sa && tok == ')')
3167 break;
3168 if (!sa)
3169 tcc_error("macro '%s' used with too many args",
3170 get_tok_str(s->v, 0));
3171 tok_str_new(&str);
3172 parlevel = spc = 0;
3173 /* NOTE: non zero sa->t indicates VA_ARGS */
3174 while ((parlevel > 0 ||
3175 (tok != ')' &&
3176 (tok != ',' || sa->type.t)))) {
3177 if (tok == TOK_EOF || tok == 0)
3178 break;
3179 if (tok == '(')
3180 parlevel++;
3181 else if (tok == ')')
3182 parlevel--;
3183 if (tok == TOK_LINEFEED)
3184 tok = ' ';
3185 if (!check_space(tok, &spc))
3186 tok_str_add2(&str, tok, &tokc);
3187 next_argstream(nested_list, can_read_stream, NULL);
3189 if (parlevel)
3190 expect(")");
3191 str.len -= spc;
3192 tok_str_add(&str, 0);
3193 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3194 sa1->d = str.str;
3195 sa = sa->next;
3196 if (tok == ')') {
3197 /* special case for gcc var args: add an empty
3198 var arg argument if it is omitted */
3199 if (sa && sa->type.t && gnu_ext)
3200 goto empty_arg;
3201 break;
3203 if (tok != ',')
3204 expect(",");
3206 if (sa) {
3207 tcc_error("macro '%s' used with too few args",
3208 get_tok_str(s->v, 0));
3211 parse_flags = saved_parse_flags;
3213 /* now subst each arg */
3214 mstr = macro_arg_subst(nested_list, mstr, args);
3215 /* free memory */
3216 sa = args;
3217 while (sa) {
3218 sa1 = sa->prev;
3219 tok_str_free_str(sa->d);
3220 sym_free(sa);
3221 sa = sa1;
3225 sym_push2(nested_list, s->v, 0, 0);
3226 parse_flags = saved_parse_flags;
3227 macro_subst(tok_str, nested_list, mstr, can_read_stream | 2);
3229 /* pop nested defined symbol */
3230 sa1 = *nested_list;
3231 *nested_list = sa1->prev;
3232 sym_free(sa1);
3233 if (mstr != s->d)
3234 tok_str_free_str(mstr);
3236 return 0;
3239 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3241 CString cstr;
3242 int n, ret = 1;
3244 cstr_new(&cstr);
3245 if (t1 != TOK_PLCHLDR)
3246 cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3247 n = cstr.size;
3248 if (t2 != TOK_PLCHLDR)
3249 cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3250 cstr_ccat(&cstr, '\0');
3252 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3253 memcpy(file->buffer, cstr.data, cstr.size);
3254 for (;;) {
3255 next_nomacro1();
3256 if (0 == *file->buf_ptr)
3257 break;
3258 if (is_space(tok))
3259 continue;
3260 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
3261 " preprocessing token", n, cstr.data, (char*)cstr.data + n);
3262 ret = 0;
3263 break;
3265 tcc_close();
3266 //printf("paste <%s>\n", (char*)cstr.data);
3267 cstr_free(&cstr);
3268 return ret;
3271 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3272 return the resulting string (which must be freed). */
3273 static inline int *macro_twosharps(const int *ptr0)
3275 int t;
3276 CValue cval;
3277 TokenString macro_str1;
3278 int start_of_nosubsts = -1;
3279 const int *ptr;
3281 /* we search the first '##' */
3282 for (ptr = ptr0;;) {
3283 TOK_GET(&t, &ptr, &cval);
3284 if (t == TOK_PPJOIN)
3285 break;
3286 if (t == 0)
3287 return NULL;
3290 tok_str_new(&macro_str1);
3292 //tok_print(" $$$", ptr0);
3293 for (ptr = ptr0;;) {
3294 TOK_GET(&t, &ptr, &cval);
3295 if (t == 0)
3296 break;
3297 if (t == TOK_PPJOIN)
3298 continue;
3299 while (*ptr == TOK_PPJOIN) {
3300 int t1; CValue cv1;
3301 /* given 'a##b', remove nosubsts preceding 'a' */
3302 if (start_of_nosubsts >= 0)
3303 macro_str1.len = start_of_nosubsts;
3304 /* given 'a##b', remove nosubsts preceding 'b' */
3305 while ((t1 = *++ptr) == TOK_NOSUBST)
3307 if (t1 && t1 != TOK_PPJOIN) {
3308 TOK_GET(&t1, &ptr, &cv1);
3309 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3310 if (paste_tokens(t, &cval, t1, &cv1)) {
3311 t = tok, cval = tokc;
3312 } else {
3313 tok_str_add2(&macro_str1, t, &cval);
3314 t = t1, cval = cv1;
3319 if (t == TOK_NOSUBST) {
3320 if (start_of_nosubsts < 0)
3321 start_of_nosubsts = macro_str1.len;
3322 } else {
3323 start_of_nosubsts = -1;
3325 tok_str_add2(&macro_str1, t, &cval);
3327 tok_str_add(&macro_str1, 0);
3328 //tok_print(" ###", macro_str1.str);
3329 return macro_str1.str;
3332 /* do macro substitution of macro_str and add result to
3333 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3334 inside to avoid recursing. */
3335 static void macro_subst(
3336 TokenString *tok_str,
3337 Sym **nested_list,
3338 const int *macro_str,
3339 int can_read_stream
3342 Sym *s;
3343 const int *ptr;
3344 int t, spc, nosubst;
3345 CValue cval;
3346 int *macro_str1 = NULL;
3348 /* first scan for '##' operator handling */
3349 ptr = macro_str;
3350 spc = nosubst = 0;
3352 /* first scan for '##' operator handling */
3353 if (can_read_stream) {
3354 macro_str1 = macro_twosharps(ptr);
3355 if (macro_str1)
3356 ptr = macro_str1;
3359 while (1) {
3360 TOK_GET(&t, &ptr, &cval);
3361 if (t == 0)
3362 break;
3364 if (t >= TOK_IDENT && 0 == nosubst) {
3365 s = define_find(t);
3366 if (s == NULL)
3367 goto no_subst;
3369 /* if nested substitution, do nothing */
3370 if (sym_find2(*nested_list, t)) {
3371 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3372 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3373 goto no_subst;
3377 TokenString str;
3378 str.str = (int*)ptr;
3379 begin_macro(&str, 2);
3381 tok = t;
3382 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3384 if (str.alloc == 3) {
3385 /* already finished by reading function macro arguments */
3386 break;
3389 ptr = macro_ptr;
3390 end_macro ();
3393 spc = (tok_str->len &&
3394 is_space(tok_last(tok_str->str,
3395 tok_str->str + tok_str->len)));
3397 } else {
3399 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3400 tcc_error("stray '\\' in program");
3402 no_subst:
3403 if (!check_space(t, &spc))
3404 tok_str_add2(tok_str, t, &cval);
3405 nosubst = 0;
3406 if (t == TOK_NOSUBST)
3407 nosubst = 1;
3410 if (macro_str1)
3411 tok_str_free_str(macro_str1);
3415 /* return next token with macro substitution */
3416 ST_FUNC void next(void)
3418 redo:
3419 if (parse_flags & PARSE_FLAG_SPACES)
3420 next_nomacro_spc();
3421 else
3422 next_nomacro();
3424 if (macro_ptr) {
3425 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3426 /* discard preprocessor markers */
3427 goto redo;
3428 } else if (tok == 0) {
3429 /* end of macro or unget token string */
3430 end_macro();
3431 goto redo;
3433 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3434 Sym *s;
3435 /* if reading from file, try to substitute macros */
3436 s = define_find(tok);
3437 if (s) {
3438 Sym *nested_list = NULL;
3439 tokstr_buf.len = 0;
3440 macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
3441 tok_str_add(&tokstr_buf, 0);
3442 begin_macro(&tokstr_buf, 2);
3443 goto redo;
3446 /* convert preprocessor tokens into C tokens */
3447 if (tok == TOK_PPNUM) {
3448 if (parse_flags & PARSE_FLAG_TOK_NUM)
3449 parse_number((char *)tokc.str.data);
3450 } else if (tok == TOK_PPSTR) {
3451 if (parse_flags & PARSE_FLAG_TOK_STR)
3452 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3456 /* push back current token and set current token to 'last_tok'. Only
3457 identifier case handled for labels. */
3458 ST_INLN void unget_tok(int last_tok)
3461 TokenString *str = tok_str_alloc();
3462 tok_str_add2(str, tok, &tokc);
3463 tok_str_add(str, 0);
3464 begin_macro(str, 1);
3465 tok = last_tok;
3468 ST_FUNC void preprocess_start(TCCState *s1)
3470 char *buf;
3471 s1->include_stack_ptr = s1->include_stack;
3472 /* XXX: move that before to avoid having to initialize
3473 file->ifdef_stack_ptr ? */
3474 s1->ifdef_stack_ptr = s1->ifdef_stack;
3475 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3476 pp_once++;
3478 pvtop = vtop = vstack - 1;
3479 s1->pack_stack[0] = 0;
3480 s1->pack_stack_ptr = s1->pack_stack;
3482 set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
3483 set_idnum('.', (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
3484 buf = tcc_malloc(3 + strlen(file->filename));
3485 sprintf(buf, "\"%s\"", file->filename);
3486 tcc_undefine_symbol(s1, "__BASE_FILE__");
3487 tcc_define_symbol(s1, "__BASE_FILE__", buf);
3488 tcc_free(buf);
3489 if (s1->nb_cmd_include_files) {
3490 CString cstr;
3491 int i;
3492 cstr_new(&cstr);
3493 for (i = 0; i < s1->nb_cmd_include_files; i++) {
3494 cstr_cat(&cstr, "#include \"", -1);
3495 cstr_cat(&cstr, s1->cmd_include_files[i], -1);
3496 cstr_cat(&cstr, "\"\n", -1);
3498 *s1->include_stack_ptr++ = file;
3499 tcc_open_bf(s1, "<command line>", cstr.size);
3500 memcpy(file->buffer, cstr.data, cstr.size);
3501 cstr_free(&cstr);
3505 ST_FUNC void tccpp_new(TCCState *s)
3507 int i, c;
3508 const char *p, *r;
3510 /* might be used in error() before preprocess_start() */
3511 s->include_stack_ptr = s->include_stack;
3513 /* init isid table */
3514 for(i = CH_EOF; i<128; i++)
3515 set_idnum(i,
3516 is_space(i) ? IS_SPC
3517 : isid(i) ? IS_ID
3518 : isnum(i) ? IS_NUM
3519 : 0);
3521 for(i = 128; i<256; i++)
3522 set_idnum(i, IS_ID);
3524 /* init allocators */
3525 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
3526 tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
3527 tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
3529 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3530 cstr_new(&cstr_buf);
3531 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
3532 tok_str_new(&tokstr_buf);
3533 tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
3535 tok_ident = TOK_IDENT;
3536 p = tcc_keywords;
3537 while (*p) {
3538 r = p;
3539 for(;;) {
3540 c = *r++;
3541 if (c == '\0')
3542 break;
3544 tok_alloc(p, r - p - 1);
3545 p = r;
3549 ST_FUNC void tccpp_delete(TCCState *s)
3551 int i, n;
3553 /* free -D and compiler defines */
3554 free_defines(NULL);
3556 /* cleanup from error/setjmp */
3557 while (macro_stack)
3558 end_macro();
3559 macro_ptr = NULL;
3561 /* free tokens */
3562 n = tok_ident - TOK_IDENT;
3563 for(i = 0; i < n; i++)
3564 tal_free(toksym_alloc, table_ident[i]);
3565 tcc_free(table_ident);
3566 table_ident = NULL;
3568 /* free static buffers */
3569 cstr_free(&tokcstr);
3570 cstr_free(&cstr_buf);
3571 tok_str_free_str(tokstr_buf.str);
3573 /* free allocators */
3574 tal_delete(toksym_alloc);
3575 toksym_alloc = NULL;
3576 tal_delete(tokstr_alloc);
3577 tokstr_alloc = NULL;
3578 tal_delete(cstr_alloc);
3579 cstr_alloc = NULL;
3582 /* ------------------------------------------------------------------------- */
3583 /* tcc -E [-P[1]] [-dD} support */
3585 static void tok_print(const char *msg, const int *str)
3587 FILE *fp;
3588 int t;
3589 CValue cval;
3591 fp = tcc_state->ppfp;
3592 if (!fp || !tcc_state->dflag)
3593 fp = stdout;
3595 fprintf(fp, "%s ", msg);
3596 while (str) {
3597 TOK_GET(&t, &str, &cval);
3598 if (!t)
3599 break;
3600 fprintf(fp,"%s", get_tok_str(t, &cval));
3602 fprintf(fp, "\n");
3605 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3607 int d = f->line_num - f->line_ref;
3609 if (s1->dflag & 4)
3610 return;
3612 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3614 } else if (level == 0 && f->line_ref && d < 8) {
3615 while (d > 0)
3616 fputs("\n", s1->ppfp), --d;
3617 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3618 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3619 } else {
3620 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3621 level > 0 ? " 1" : level < 0 ? " 2" : "");
3623 f->line_ref = f->line_num;
3626 static void define_print(TCCState *s1, int v)
3628 FILE *fp;
3629 Sym *s;
3631 s = define_find(v);
3632 if (NULL == s || NULL == s->d)
3633 return;
3635 fp = s1->ppfp;
3636 fprintf(fp, "#define %s", get_tok_str(v, NULL));
3637 if (s->type.t == MACRO_FUNC) {
3638 Sym *a = s->next;
3639 fprintf(fp,"(");
3640 if (a)
3641 for (;;) {
3642 fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3643 if (!(a = a->next))
3644 break;
3645 fprintf(fp,",");
3647 fprintf(fp,")");
3649 tok_print("", s->d);
3652 static void pp_debug_defines(TCCState *s1)
3654 int v, t;
3655 const char *vs;
3656 FILE *fp;
3658 t = pp_debug_tok;
3659 if (t == 0)
3660 return;
3662 file->line_num--;
3663 pp_line(s1, file, 0);
3664 file->line_ref = ++file->line_num;
3666 fp = s1->ppfp;
3667 v = pp_debug_symv;
3668 vs = get_tok_str(v, NULL);
3669 if (t == TOK_DEFINE) {
3670 define_print(s1, v);
3671 } else if (t == TOK_UNDEF) {
3672 fprintf(fp, "#undef %s\n", vs);
3673 } else if (t == TOK_push_macro) {
3674 fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3675 } else if (t == TOK_pop_macro) {
3676 fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3678 pp_debug_tok = 0;
3681 static void pp_debug_builtins(TCCState *s1)
3683 int v;
3684 for (v = TOK_IDENT; v < tok_ident; ++v)
3685 define_print(s1, v);
3688 /* Add a space between tokens a and b to avoid unwanted textual pasting */
3689 static int pp_need_space(int a, int b)
3691 return 'E' == a ? '+' == b || '-' == b
3692 : '+' == a ? TOK_INC == b || '+' == b
3693 : '-' == a ? TOK_DEC == b || '-' == b
3694 : a >= TOK_IDENT ? b >= TOK_IDENT
3695 : 0;
3698 /* maybe hex like 0x1e */
3699 static int pp_check_he0xE(int t, const char *p)
3701 if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
3702 return 'E';
3703 return t;
3706 /* Preprocess the current file */
3707 ST_FUNC int tcc_preprocess(TCCState *s1)
3709 BufferedFile **iptr;
3710 int token_seen, spcs, level;
3711 const char *p;
3712 Sym *define_start;
3714 preprocess_start(s1);
3715 ch = file->buf_ptr[0];
3716 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3717 parse_flags = PARSE_FLAG_PREPROCESS
3718 | (parse_flags & PARSE_FLAG_ASM_FILE)
3719 | PARSE_FLAG_LINEFEED
3720 | PARSE_FLAG_SPACES
3721 | PARSE_FLAG_ACCEPT_STRAYS
3723 define_start = define_stack;
3725 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3726 capability to compile and run itself, provided all numbers are
3727 given as decimals. tcc -E -P10 will do. */
3728 if (s1->Pflag == 1 + 10)
3729 parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
3731 #ifdef PP_BENCH
3732 /* for PP benchmarks */
3733 do next(); while (tok != TOK_EOF);
3734 free_defines(define_start);
3735 return 0;
3736 #endif
3738 if (s1->dflag & 1) {
3739 pp_debug_builtins(s1);
3740 s1->dflag &= ~1;
3743 token_seen = TOK_LINEFEED, spcs = 0;
3744 pp_line(s1, file, 0);
3746 for (;;) {
3747 iptr = s1->include_stack_ptr;
3748 next();
3749 if (tok == TOK_EOF)
3750 break;
3751 level = s1->include_stack_ptr - iptr;
3752 if (level) {
3753 if (level > 0)
3754 pp_line(s1, *iptr, 0);
3755 pp_line(s1, file, level);
3758 if (s1->dflag) {
3759 pp_debug_defines(s1);
3760 if (s1->dflag & 4)
3761 continue;
3764 if (token_seen == TOK_LINEFEED) {
3765 if (tok == ' ') {
3766 ++spcs;
3767 continue;
3769 if (tok == TOK_LINEFEED) {
3770 spcs = 0;
3771 continue;
3773 pp_line(s1, file, 0);
3774 } else if (tok == TOK_LINEFEED) {
3775 ++file->line_ref;
3776 } else {
3777 spcs = pp_need_space(token_seen, tok);
3780 while (spcs)
3781 fputs(" ", s1->ppfp), --spcs;
3782 fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
3783 token_seen = pp_check_he0xE(tok, p);;
3785 /* reset define stack, but keep -D and built-ins */
3786 free_defines(define_start);
3787 return 0;
3790 /* ------------------------------------------------------------------------- */