bitfields: Implement MS compatible layout
[tinycc.git] / tccpp.c
blob6659812e476a6b90251f04f50dc5108134193a1f
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 size_t limit;
141 size_t size;
142 uint8_t *buffer;
143 uint8_t *p;
144 size_t nb_allocs;
145 struct TinyAlloc *next, *top;
146 #ifdef TAL_INFO
147 size_t nb_peak;
148 size_t nb_total;
149 size_t nb_missed;
150 uint8_t *peak_p;
151 #endif
152 } TinyAlloc;
154 typedef struct tal_header_t {
155 size_t 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 ST_FUNC TinyAlloc *tal_new(TinyAlloc **pal, size_t limit, size_t 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 ST_FUNC 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: mem leak %d chunks (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, " file %s, line %u: %u bytes\n",
196 header->file_name, header->line_num, header->size);
198 p += header->size + sizeof(tal_header_t);
201 #endif
202 next = al->next;
203 tcc_free(al->buffer);
204 tcc_free(al);
205 al = next;
206 goto tail_call;
209 ST_FUNC void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
211 if (!p)
212 return;
213 tail_call:
214 if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
215 #ifdef TAL_DEBUG
216 tal_header_t *header = (((tal_header_t *)p) - 1);
217 if (header->line_num < 0) {
218 fprintf(stderr, "TAL_DEBUG: file %s, line %u double frees chunk from\n",
219 file, line);
220 fprintf(stderr, " file %s, line %u: %u bytes\n",
221 header->file_name, -header->line_num, header->size);
222 } else
223 header->line_num = -header->line_num;
224 #endif
225 al->nb_allocs--;
226 if (!al->nb_allocs)
227 al->p = al->buffer;
228 } else if (al->next) {
229 al = al->next;
230 goto tail_call;
232 else
233 tcc_free(p);
236 ST_FUNC void *tal_realloc_impl(TinyAlloc **pal, void *p, size_t size TAL_DEBUG_PARAMS)
238 tal_header_t *header;
239 void *ret;
240 int is_own;
241 size_t adj_size = (size + 3) & -4;
242 TinyAlloc *al = *pal;
244 tail_call:
245 is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
246 if ((!p || is_own) && size <= al->limit) {
247 if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) {
248 header = (tal_header_t *)al->p;
249 header->size = adj_size;
250 #ifdef TAL_DEBUG
251 { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
252 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
253 header->file_name[TAL_DEBUG_FILE_LEN] = 0;
254 header->line_num = line; }
255 #endif
256 ret = al->p + sizeof(tal_header_t);
257 al->p += adj_size + sizeof(tal_header_t);
258 if (is_own) {
259 header = (((tal_header_t *)p) - 1);
260 memcpy(ret, p, header->size);
261 #ifdef TAL_DEBUG
262 header->line_num = -header->line_num;
263 #endif
264 } else {
265 al->nb_allocs++;
267 #ifdef TAL_INFO
268 if (al->nb_peak < al->nb_allocs)
269 al->nb_peak = al->nb_allocs;
270 if (al->peak_p < al->p)
271 al->peak_p = al->p;
272 al->nb_total++;
273 #endif
274 return ret;
275 } else if (is_own) {
276 al->nb_allocs--;
277 ret = tal_realloc(*pal, 0, size);
278 header = (((tal_header_t *)p) - 1);
279 memcpy(ret, p, header->size);
280 #ifdef TAL_DEBUG
281 header->line_num = -header->line_num;
282 #endif
283 return ret;
285 if (al->next) {
286 al = al->next;
287 } else {
288 TinyAlloc *bottom = al, *next = al->top ? al->top : al;
290 al = tal_new(pal, next->limit, next->size * 2);
291 al->next = next;
292 bottom->top = al;
294 goto tail_call;
296 if (is_own) {
297 al->nb_allocs--;
298 ret = tcc_malloc(size);
299 header = (((tal_header_t *)p) - 1);
300 memcpy(ret, p, header->size);
301 #ifdef TAL_DEBUG
302 header->line_num = -header->line_num;
303 #endif
304 } else if (al->next) {
305 al = al->next;
306 goto tail_call;
307 } else
308 ret = tcc_realloc(p, size);
309 #ifdef TAL_INFO
310 al->nb_missed++;
311 #endif
312 return ret;
315 #endif /* USE_TAL */
317 /* ------------------------------------------------------------------------- */
318 /* CString handling */
319 static void cstr_realloc(CString *cstr, int new_size)
321 int size;
323 size = cstr->size_allocated;
324 if (size < 8)
325 size = 8; /* no need to allocate a too small first string */
326 while (size < new_size)
327 size = size * 2;
328 cstr->data = tal_realloc(cstr_alloc, cstr->data, size);
329 cstr->size_allocated = size;
332 /* add a byte */
333 ST_INLN void cstr_ccat(CString *cstr, int ch)
335 int size;
336 size = cstr->size + 1;
337 if (size > cstr->size_allocated)
338 cstr_realloc(cstr, size);
339 ((unsigned char *)cstr->data)[size - 1] = ch;
340 cstr->size = size;
343 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len)
345 int size;
346 if (len <= 0)
347 len = strlen(str) + 1 + len;
348 size = cstr->size + len;
349 if (size > cstr->size_allocated)
350 cstr_realloc(cstr, size);
351 memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
352 cstr->size = size;
355 /* add a wide char */
356 ST_FUNC void cstr_wccat(CString *cstr, int ch)
358 int size;
359 size = cstr->size + sizeof(nwchar_t);
360 if (size > cstr->size_allocated)
361 cstr_realloc(cstr, size);
362 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
363 cstr->size = size;
366 ST_FUNC void cstr_new(CString *cstr)
368 memset(cstr, 0, sizeof(CString));
371 /* free string and reset it to NULL */
372 ST_FUNC void cstr_free(CString *cstr)
374 tal_free(cstr_alloc, cstr->data);
375 cstr_new(cstr);
378 /* reset string to empty */
379 ST_FUNC void cstr_reset(CString *cstr)
381 cstr->size = 0;
384 /* XXX: unicode ? */
385 static void add_char(CString *cstr, int c)
387 if (c == '\'' || c == '\"' || c == '\\') {
388 /* XXX: could be more precise if char or string */
389 cstr_ccat(cstr, '\\');
391 if (c >= 32 && c <= 126) {
392 cstr_ccat(cstr, c);
393 } else {
394 cstr_ccat(cstr, '\\');
395 if (c == '\n') {
396 cstr_ccat(cstr, 'n');
397 } else {
398 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
399 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
400 cstr_ccat(cstr, '0' + (c & 7));
405 /* ------------------------------------------------------------------------- */
406 /* allocate a new token */
407 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
409 TokenSym *ts, **ptable;
410 int i;
412 if (tok_ident >= SYM_FIRST_ANOM)
413 tcc_error("memory full (symbols)");
415 /* expand token table if needed */
416 i = tok_ident - TOK_IDENT;
417 if ((i % TOK_ALLOC_INCR) == 0) {
418 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
419 table_ident = ptable;
422 ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len);
423 table_ident[i] = ts;
424 ts->tok = tok_ident++;
425 ts->sym_define = NULL;
426 ts->sym_label = NULL;
427 ts->sym_struct = NULL;
428 ts->sym_identifier = NULL;
429 ts->len = len;
430 ts->hash_next = NULL;
431 memcpy(ts->str, str, len);
432 ts->str[len] = '\0';
433 *pts = ts;
434 return ts;
437 #define TOK_HASH_INIT 1
438 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
441 /* find a token and add it if not found */
442 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
444 TokenSym *ts, **pts;
445 int i;
446 unsigned int h;
448 h = TOK_HASH_INIT;
449 for(i=0;i<len;i++)
450 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
451 h &= (TOK_HASH_SIZE - 1);
453 pts = &hash_ident[h];
454 for(;;) {
455 ts = *pts;
456 if (!ts)
457 break;
458 if (ts->len == len && !memcmp(ts->str, str, len))
459 return ts;
460 pts = &(ts->hash_next);
462 return tok_alloc_new(pts, str, len);
465 /* XXX: buffer overflow */
466 /* XXX: float tokens */
467 ST_FUNC const char *get_tok_str(int v, CValue *cv)
469 char *p;
470 int i, len;
472 cstr_reset(&cstr_buf);
473 p = cstr_buf.data;
475 switch(v) {
476 case TOK_CINT:
477 case TOK_CUINT:
478 case TOK_CLLONG:
479 case TOK_CULLONG:
480 /* XXX: not quite exact, but only useful for testing */
481 #ifdef _WIN32
482 sprintf(p, "%u", (unsigned)cv->i);
483 #else
484 sprintf(p, "%llu", (unsigned long long)cv->i);
485 #endif
486 break;
487 case TOK_LCHAR:
488 cstr_ccat(&cstr_buf, 'L');
489 case TOK_CCHAR:
490 cstr_ccat(&cstr_buf, '\'');
491 add_char(&cstr_buf, cv->i);
492 cstr_ccat(&cstr_buf, '\'');
493 cstr_ccat(&cstr_buf, '\0');
494 break;
495 case TOK_PPNUM:
496 case TOK_PPSTR:
497 return (char*)cv->str.data;
498 case TOK_LSTR:
499 cstr_ccat(&cstr_buf, 'L');
500 case TOK_STR:
501 cstr_ccat(&cstr_buf, '\"');
502 if (v == TOK_STR) {
503 len = cv->str.size - 1;
504 for(i=0;i<len;i++)
505 add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
506 } else {
507 len = (cv->str.size / sizeof(nwchar_t)) - 1;
508 for(i=0;i<len;i++)
509 add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
511 cstr_ccat(&cstr_buf, '\"');
512 cstr_ccat(&cstr_buf, '\0');
513 break;
515 case TOK_CFLOAT:
516 cstr_cat(&cstr_buf, "<float>", 0);
517 break;
518 case TOK_CDOUBLE:
519 cstr_cat(&cstr_buf, "<double>", 0);
520 break;
521 case TOK_CLDOUBLE:
522 cstr_cat(&cstr_buf, "<long double>", 0);
523 break;
524 case TOK_LINENUM:
525 cstr_cat(&cstr_buf, "<linenumber>", 0);
526 break;
528 /* above tokens have value, the ones below don't */
530 case TOK_LT:
531 v = '<';
532 goto addv;
533 case TOK_GT:
534 v = '>';
535 goto addv;
536 case TOK_DOTS:
537 return strcpy(p, "...");
538 case TOK_A_SHL:
539 return strcpy(p, "<<=");
540 case TOK_A_SAR:
541 return strcpy(p, ">>=");
542 default:
543 if (v < TOK_IDENT) {
544 /* search in two bytes table */
545 const unsigned char *q = tok_two_chars;
546 while (*q) {
547 if (q[2] == v) {
548 *p++ = q[0];
549 *p++ = q[1];
550 *p = '\0';
551 return cstr_buf.data;
553 q += 3;
555 if (v >= 127) {
556 sprintf(cstr_buf.data, "<%02x>", v);
557 return cstr_buf.data;
559 addv:
560 *p++ = v;
561 *p = '\0';
562 } else if (v < tok_ident) {
563 return table_ident[v - TOK_IDENT]->str;
564 } else if (v >= SYM_FIRST_ANOM) {
565 /* special name for anonymous symbol */
566 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
567 } else {
568 /* should never happen */
569 return NULL;
571 break;
573 return cstr_buf.data;
576 /* return the current character, handling end of block if necessary
577 (but not stray) */
578 ST_FUNC int handle_eob(void)
580 BufferedFile *bf = file;
581 int len;
583 /* only tries to read if really end of buffer */
584 if (bf->buf_ptr >= bf->buf_end) {
585 if (bf->fd != -1) {
586 #if defined(PARSE_DEBUG)
587 len = 1;
588 #else
589 len = IO_BUF_SIZE;
590 #endif
591 len = read(bf->fd, bf->buffer, len);
592 if (len < 0)
593 len = 0;
594 } else {
595 len = 0;
597 total_bytes += len;
598 bf->buf_ptr = bf->buffer;
599 bf->buf_end = bf->buffer + len;
600 *bf->buf_end = CH_EOB;
602 if (bf->buf_ptr < bf->buf_end) {
603 return bf->buf_ptr[0];
604 } else {
605 bf->buf_ptr = bf->buf_end;
606 return CH_EOF;
610 /* read next char from current input file and handle end of input buffer */
611 ST_INLN void inp(void)
613 ch = *(++(file->buf_ptr));
614 /* end of buffer/file handling */
615 if (ch == CH_EOB)
616 ch = handle_eob();
619 /* handle '\[\r]\n' */
620 static int handle_stray_noerror(void)
622 while (ch == '\\') {
623 inp();
624 if (ch == '\n') {
625 file->line_num++;
626 inp();
627 } else if (ch == '\r') {
628 inp();
629 if (ch != '\n')
630 goto fail;
631 file->line_num++;
632 inp();
633 } else {
634 fail:
635 return 1;
638 return 0;
641 static void handle_stray(void)
643 if (handle_stray_noerror())
644 tcc_error("stray '\\' in program");
647 /* skip the stray and handle the \\n case. Output an error if
648 incorrect char after the stray */
649 static int handle_stray1(uint8_t *p)
651 int c;
653 file->buf_ptr = p;
654 if (p >= file->buf_end) {
655 c = handle_eob();
656 if (c != '\\')
657 return c;
658 p = file->buf_ptr;
660 ch = *p;
661 if (handle_stray_noerror()) {
662 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
663 tcc_error("stray '\\' in program");
664 *--file->buf_ptr = '\\';
666 p = file->buf_ptr;
667 c = *p;
668 return c;
671 /* handle just the EOB case, but not stray */
672 #define PEEKC_EOB(c, p)\
674 p++;\
675 c = *p;\
676 if (c == '\\') {\
677 file->buf_ptr = p;\
678 c = handle_eob();\
679 p = file->buf_ptr;\
683 /* handle the complicated stray case */
684 #define PEEKC(c, p)\
686 p++;\
687 c = *p;\
688 if (c == '\\') {\
689 c = handle_stray1(p);\
690 p = file->buf_ptr;\
694 /* input with '\[\r]\n' handling. Note that this function cannot
695 handle other characters after '\', so you cannot call it inside
696 strings or comments */
697 ST_FUNC void minp(void)
699 inp();
700 if (ch == '\\')
701 handle_stray();
704 /* single line C++ comments */
705 static uint8_t *parse_line_comment(uint8_t *p)
707 int c;
709 p++;
710 for(;;) {
711 c = *p;
712 redo:
713 if (c == '\n' || c == CH_EOF) {
714 break;
715 } else if (c == '\\') {
716 file->buf_ptr = p;
717 c = handle_eob();
718 p = file->buf_ptr;
719 if (c == '\\') {
720 PEEKC_EOB(c, p);
721 if (c == '\n') {
722 file->line_num++;
723 PEEKC_EOB(c, p);
724 } else if (c == '\r') {
725 PEEKC_EOB(c, p);
726 if (c == '\n') {
727 file->line_num++;
728 PEEKC_EOB(c, p);
731 } else {
732 goto redo;
734 } else {
735 p++;
738 return p;
741 /* C comments */
742 ST_FUNC uint8_t *parse_comment(uint8_t *p)
744 int c;
746 p++;
747 for(;;) {
748 /* fast skip loop */
749 for(;;) {
750 c = *p;
751 if (c == '\n' || c == '*' || c == '\\')
752 break;
753 p++;
754 c = *p;
755 if (c == '\n' || c == '*' || c == '\\')
756 break;
757 p++;
759 /* now we can handle all the cases */
760 if (c == '\n') {
761 file->line_num++;
762 p++;
763 } else if (c == '*') {
764 p++;
765 for(;;) {
766 c = *p;
767 if (c == '*') {
768 p++;
769 } else if (c == '/') {
770 goto end_of_comment;
771 } else if (c == '\\') {
772 file->buf_ptr = p;
773 c = handle_eob();
774 p = file->buf_ptr;
775 if (c == CH_EOF)
776 tcc_error("unexpected end of file in comment");
777 if (c == '\\') {
778 /* skip '\[\r]\n', otherwise just skip the stray */
779 while (c == '\\') {
780 PEEKC_EOB(c, p);
781 if (c == '\n') {
782 file->line_num++;
783 PEEKC_EOB(c, p);
784 } else if (c == '\r') {
785 PEEKC_EOB(c, p);
786 if (c == '\n') {
787 file->line_num++;
788 PEEKC_EOB(c, p);
790 } else {
791 goto after_star;
795 } else {
796 break;
799 after_star: ;
800 } else {
801 /* stray, eob or eof */
802 file->buf_ptr = p;
803 c = handle_eob();
804 p = file->buf_ptr;
805 if (c == CH_EOF) {
806 tcc_error("unexpected end of file in comment");
807 } else if (c == '\\') {
808 p++;
812 end_of_comment:
813 p++;
814 return p;
817 ST_FUNC void set_idnum(int c, int val)
819 isidnum_table[c - CH_EOF] = val;
822 #define cinp minp
824 static inline void skip_spaces(void)
826 while (isidnum_table[ch - CH_EOF] & IS_SPC)
827 cinp();
830 static inline int check_space(int t, int *spc)
832 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
833 if (*spc)
834 return 1;
835 *spc = 1;
836 } else
837 *spc = 0;
838 return 0;
841 /* parse a string without interpreting escapes */
842 static uint8_t *parse_pp_string(uint8_t *p,
843 int sep, CString *str)
845 int c;
846 p++;
847 for(;;) {
848 c = *p;
849 if (c == sep) {
850 break;
851 } else if (c == '\\') {
852 file->buf_ptr = p;
853 c = handle_eob();
854 p = file->buf_ptr;
855 if (c == CH_EOF) {
856 unterminated_string:
857 /* XXX: indicate line number of start of string */
858 tcc_error("missing terminating %c character", sep);
859 } else if (c == '\\') {
860 /* escape : just skip \[\r]\n */
861 PEEKC_EOB(c, p);
862 if (c == '\n') {
863 file->line_num++;
864 p++;
865 } else if (c == '\r') {
866 PEEKC_EOB(c, p);
867 if (c != '\n')
868 expect("'\n' after '\r'");
869 file->line_num++;
870 p++;
871 } else if (c == CH_EOF) {
872 goto unterminated_string;
873 } else {
874 if (str) {
875 cstr_ccat(str, '\\');
876 cstr_ccat(str, c);
878 p++;
881 } else if (c == '\n') {
882 file->line_num++;
883 goto add_char;
884 } else if (c == '\r') {
885 PEEKC_EOB(c, p);
886 if (c != '\n') {
887 if (str)
888 cstr_ccat(str, '\r');
889 } else {
890 file->line_num++;
891 goto add_char;
893 } else {
894 add_char:
895 if (str)
896 cstr_ccat(str, c);
897 p++;
900 p++;
901 return p;
904 /* skip block of text until #else, #elif or #endif. skip also pairs of
905 #if/#endif */
906 static void preprocess_skip(void)
908 int a, start_of_line, c, in_warn_or_error;
909 uint8_t *p;
911 p = file->buf_ptr;
912 a = 0;
913 redo_start:
914 start_of_line = 1;
915 in_warn_or_error = 0;
916 for(;;) {
917 redo_no_start:
918 c = *p;
919 switch(c) {
920 case ' ':
921 case '\t':
922 case '\f':
923 case '\v':
924 case '\r':
925 p++;
926 goto redo_no_start;
927 case '\n':
928 file->line_num++;
929 p++;
930 goto redo_start;
931 case '\\':
932 file->buf_ptr = p;
933 c = handle_eob();
934 if (c == CH_EOF) {
935 expect("#endif");
936 } else if (c == '\\') {
937 ch = file->buf_ptr[0];
938 handle_stray_noerror();
940 p = file->buf_ptr;
941 goto redo_no_start;
942 /* skip strings */
943 case '\"':
944 case '\'':
945 if (in_warn_or_error)
946 goto _default;
947 p = parse_pp_string(p, c, NULL);
948 break;
949 /* skip comments */
950 case '/':
951 if (in_warn_or_error)
952 goto _default;
953 file->buf_ptr = p;
954 ch = *p;
955 minp();
956 p = file->buf_ptr;
957 if (ch == '*') {
958 p = parse_comment(p);
959 } else if (ch == '/') {
960 p = parse_line_comment(p);
962 break;
963 case '#':
964 p++;
965 if (start_of_line) {
966 file->buf_ptr = p;
967 next_nomacro();
968 p = file->buf_ptr;
969 if (a == 0 &&
970 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
971 goto the_end;
972 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
973 a++;
974 else if (tok == TOK_ENDIF)
975 a--;
976 else if( tok == TOK_ERROR || tok == TOK_WARNING)
977 in_warn_or_error = 1;
978 else if (tok == TOK_LINEFEED)
979 goto redo_start;
980 else if (parse_flags & PARSE_FLAG_ASM_FILE)
981 p = parse_line_comment(p - 1);
982 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
983 p = parse_line_comment(p - 1);
984 break;
985 _default:
986 default:
987 p++;
988 break;
990 start_of_line = 0;
992 the_end: ;
993 file->buf_ptr = p;
996 /* ParseState handling */
998 /* XXX: currently, no include file info is stored. Thus, we cannot display
999 accurate messages if the function or data definition spans multiple
1000 files */
1002 /* save current parse state in 's' */
1003 ST_FUNC void save_parse_state(ParseState *s)
1005 s->line_num = file->line_num;
1006 s->macro_ptr = macro_ptr;
1007 s->tok = tok;
1008 s->tokc = tokc;
1011 /* restore parse state from 's' */
1012 ST_FUNC void restore_parse_state(ParseState *s)
1014 file->line_num = s->line_num;
1015 macro_ptr = s->macro_ptr;
1016 tok = s->tok;
1017 tokc = s->tokc;
1020 /* return the number of additional 'ints' necessary to store the
1021 token */
1022 static inline int tok_size(const int *p)
1024 switch(*p) {
1025 /* 4 bytes */
1026 case TOK_CINT:
1027 case TOK_CUINT:
1028 case TOK_CCHAR:
1029 case TOK_LCHAR:
1030 case TOK_CFLOAT:
1031 case TOK_LINENUM:
1032 return 1 + 1;
1033 case TOK_STR:
1034 case TOK_LSTR:
1035 case TOK_PPNUM:
1036 case TOK_PPSTR:
1037 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
1038 case TOK_CDOUBLE:
1039 case TOK_CLLONG:
1040 case TOK_CULLONG:
1041 return 1 + 2;
1042 case TOK_CLDOUBLE:
1043 return 1 + LDOUBLE_SIZE / 4;
1044 default:
1045 return 1 + 0;
1049 /* token string handling */
1051 ST_INLN void tok_str_new(TokenString *s)
1053 s->str = NULL;
1054 s->len = 0;
1055 s->allocated_len = 0;
1056 s->last_line_num = -1;
1059 ST_FUNC TokenString *tok_str_alloc(void)
1061 TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
1062 tok_str_new(str);
1063 return str;
1066 ST_FUNC int *tok_str_dup(TokenString *s)
1068 int *str;
1070 str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
1071 memcpy(str, s->str, s->len * sizeof(int));
1072 return str;
1075 ST_FUNC void tok_str_free_str(int *str)
1077 tal_free(tokstr_alloc, str);
1080 ST_FUNC void tok_str_free(TokenString *str)
1082 tok_str_free_str(str->str);
1083 tal_free(tokstr_alloc, str);
1086 ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
1088 int *str, size;
1090 size = s->allocated_len;
1091 if (size < 16)
1092 size = 16;
1093 while (size < new_size)
1094 size = size * 2;
1095 if (size > s->allocated_len) {
1096 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
1097 s->allocated_len = size;
1098 s->str = str;
1100 return s->str;
1103 ST_FUNC void tok_str_add(TokenString *s, int t)
1105 int len, *str;
1107 len = s->len;
1108 str = s->str;
1109 if (len >= s->allocated_len)
1110 str = tok_str_realloc(s, len + 1);
1111 str[len++] = t;
1112 s->len = len;
1115 ST_FUNC void begin_macro(TokenString *str, int alloc)
1117 str->alloc = alloc;
1118 str->prev = macro_stack;
1119 str->prev_ptr = macro_ptr;
1120 macro_ptr = str->str;
1121 macro_stack = str;
1124 ST_FUNC void end_macro(void)
1126 TokenString *str = macro_stack;
1127 macro_stack = str->prev;
1128 macro_ptr = str->prev_ptr;
1129 if (str->alloc == 2) {
1130 str->alloc = 3; /* just mark as finished */
1131 } else {
1132 tok_str_free(str);
1136 static void tok_str_add2(TokenString *s, int t, CValue *cv)
1138 int len, *str;
1140 len = s->len;
1141 str = s->str;
1143 /* allocate space for worst case */
1144 if (len + TOK_MAX_SIZE >= s->allocated_len)
1145 str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
1146 str[len++] = t;
1147 switch(t) {
1148 case TOK_CINT:
1149 case TOK_CUINT:
1150 case TOK_CCHAR:
1151 case TOK_LCHAR:
1152 case TOK_CFLOAT:
1153 case TOK_LINENUM:
1154 str[len++] = cv->tab[0];
1155 break;
1156 case TOK_PPNUM:
1157 case TOK_PPSTR:
1158 case TOK_STR:
1159 case TOK_LSTR:
1161 /* Insert the string into the int array. */
1162 size_t nb_words =
1163 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
1164 if (len + nb_words >= s->allocated_len)
1165 str = tok_str_realloc(s, len + nb_words + 1);
1166 str[len] = cv->str.size;
1167 memcpy(&str[len + 1], cv->str.data, cv->str.size);
1168 len += nb_words;
1170 break;
1171 case TOK_CDOUBLE:
1172 case TOK_CLLONG:
1173 case TOK_CULLONG:
1174 #if LDOUBLE_SIZE == 8
1175 case TOK_CLDOUBLE:
1176 #endif
1177 str[len++] = cv->tab[0];
1178 str[len++] = cv->tab[1];
1179 break;
1180 #if LDOUBLE_SIZE == 12
1181 case TOK_CLDOUBLE:
1182 str[len++] = cv->tab[0];
1183 str[len++] = cv->tab[1];
1184 str[len++] = cv->tab[2];
1185 #elif LDOUBLE_SIZE == 16
1186 case TOK_CLDOUBLE:
1187 str[len++] = cv->tab[0];
1188 str[len++] = cv->tab[1];
1189 str[len++] = cv->tab[2];
1190 str[len++] = cv->tab[3];
1191 #elif LDOUBLE_SIZE != 8
1192 #error add long double size support
1193 #endif
1194 break;
1195 default:
1196 break;
1198 s->len = len;
1201 /* add the current parse token in token string 's' */
1202 ST_FUNC void tok_str_add_tok(TokenString *s)
1204 CValue cval;
1206 /* save line number info */
1207 if (file->line_num != s->last_line_num) {
1208 s->last_line_num = file->line_num;
1209 cval.i = s->last_line_num;
1210 tok_str_add2(s, TOK_LINENUM, &cval);
1212 tok_str_add2(s, tok, &tokc);
1215 /* get a token from an integer array and increment pointer
1216 accordingly. we code it as a macro to avoid pointer aliasing. */
1217 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
1219 const int *p = *pp;
1220 int n, *tab;
1222 tab = cv->tab;
1223 switch(*t = *p++) {
1224 case TOK_CINT:
1225 case TOK_CUINT:
1226 case TOK_CCHAR:
1227 case TOK_LCHAR:
1228 case TOK_CFLOAT:
1229 case TOK_LINENUM:
1230 tab[0] = *p++;
1231 break;
1232 case TOK_STR:
1233 case TOK_LSTR:
1234 case TOK_PPNUM:
1235 case TOK_PPSTR:
1236 cv->str.size = *p++;
1237 cv->str.data = p;
1238 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1239 break;
1240 case TOK_CDOUBLE:
1241 case TOK_CLLONG:
1242 case TOK_CULLONG:
1243 n = 2;
1244 goto copy;
1245 case TOK_CLDOUBLE:
1246 #if LDOUBLE_SIZE == 16
1247 n = 4;
1248 #elif LDOUBLE_SIZE == 12
1249 n = 3;
1250 #elif LDOUBLE_SIZE == 8
1251 n = 2;
1252 #else
1253 # error add long double size support
1254 #endif
1255 copy:
1257 *tab++ = *p++;
1258 while (--n);
1259 break;
1260 default:
1261 break;
1263 *pp = p;
1266 /* Calling this function is expensive, but it is not possible
1267 to read a token string backwards. */
1268 static int tok_last(const int *str0, const int *str1)
1270 const int *str = str0;
1271 int tok = 0;
1272 CValue cval;
1274 while (str < str1)
1275 TOK_GET(&tok, &str, &cval);
1276 return tok;
1279 static int macro_is_equal(const int *a, const int *b)
1281 CValue cv;
1282 int t;
1283 static CString localbuf;
1285 if (!a || !b)
1286 return 1;
1288 while (*a && *b) {
1289 /* first time preallocate static localbuf, next time only reset position to start */
1290 cstr_reset(&localbuf);
1291 TOK_GET(&t, &a, &cv);
1292 cstr_cat(&localbuf, get_tok_str(t, &cv), 0);
1293 TOK_GET(&t, &b, &cv);
1294 if (strcmp(localbuf.data, get_tok_str(t, &cv)))
1295 return 0;
1297 return !(*a || *b);
1300 /* defines handling */
1301 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1303 Sym *s, *o;
1305 o = define_find(v);
1306 s = sym_push2(&define_stack, v, macro_type, 0);
1307 s->d = str;
1308 s->next = first_arg;
1309 table_ident[v - TOK_IDENT]->sym_define = s;
1311 if (o && !macro_is_equal(o->d, s->d))
1312 tcc_warning("%s redefined", get_tok_str(v, NULL));
1315 /* undefined a define symbol. Its name is just set to zero */
1316 ST_FUNC void define_undef(Sym *s)
1318 int v = s->v;
1319 if (v >= TOK_IDENT && v < tok_ident)
1320 table_ident[v - TOK_IDENT]->sym_define = NULL;
1323 ST_INLN Sym *define_find(int v)
1325 v -= TOK_IDENT;
1326 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1327 return NULL;
1328 return table_ident[v]->sym_define;
1331 /* free define stack until top reaches 'b' */
1332 ST_FUNC void free_defines(Sym *b)
1334 while (define_stack != b) {
1335 Sym *top = define_stack;
1336 define_stack = top->prev;
1337 tok_str_free_str(top->d);
1338 define_undef(top);
1339 sym_free(top);
1342 /* restore remaining (-D or predefined) symbols */
1343 while (b) {
1344 int v = b->v;
1345 if (v >= TOK_IDENT && v < tok_ident) {
1346 Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
1347 if (!*d)
1348 *d = b;
1350 b = b->prev;
1354 /* label lookup */
1355 ST_FUNC Sym *label_find(int v)
1357 v -= TOK_IDENT;
1358 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1359 return NULL;
1360 return table_ident[v]->sym_label;
1363 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1365 Sym *s, **ps;
1366 s = sym_push2(ptop, v, 0, 0);
1367 s->r = flags;
1368 ps = &table_ident[v - TOK_IDENT]->sym_label;
1369 if (ptop == &global_label_stack) {
1370 /* modify the top most local identifier, so that
1371 sym_identifier will point to 's' when popped */
1372 while (*ps != NULL)
1373 ps = &(*ps)->prev_tok;
1375 s->prev_tok = *ps;
1376 *ps = s;
1377 return s;
1380 /* pop labels until element last is reached. Look if any labels are
1381 undefined. Define symbols if '&&label' was used. */
1382 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1384 Sym *s, *s1;
1385 for(s = *ptop; s != slast; s = s1) {
1386 s1 = s->prev;
1387 if (s->r == LABEL_DECLARED) {
1388 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1389 } else if (s->r == LABEL_FORWARD) {
1390 tcc_error("label '%s' used but not defined",
1391 get_tok_str(s->v, NULL));
1392 } else {
1393 if (s->c) {
1394 /* define corresponding symbol. A size of
1395 1 is put. */
1396 put_extern_sym(s, cur_text_section, s->jnext, 1);
1399 /* remove label */
1400 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1401 sym_free(s);
1403 *ptop = slast;
1406 /* eval an expression for #if/#elif */
1407 static int expr_preprocess(void)
1409 int c, t;
1410 TokenString *str;
1412 str = tok_str_alloc();
1413 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1414 next(); /* do macro subst */
1415 if (tok == TOK_DEFINED) {
1416 next_nomacro();
1417 t = tok;
1418 if (t == '(')
1419 next_nomacro();
1420 c = define_find(tok) != 0;
1421 if (t == '(')
1422 next_nomacro();
1423 tok = TOK_CINT;
1424 tokc.i = c;
1425 } else if (tok >= TOK_IDENT) {
1426 /* if undefined macro */
1427 tok = TOK_CINT;
1428 tokc.i = 0;
1430 tok_str_add_tok(str);
1432 tok_str_add(str, -1); /* simulate end of file */
1433 tok_str_add(str, 0);
1434 /* now evaluate C constant expression */
1435 begin_macro(str, 1);
1436 next();
1437 c = expr_const();
1438 end_macro();
1439 return c != 0;
1443 /* parse after #define */
1444 ST_FUNC void parse_define(void)
1446 Sym *s, *first, **ps;
1447 int v, t, varg, is_vaargs, spc;
1448 int saved_parse_flags = parse_flags;
1450 v = tok;
1451 if (v < TOK_IDENT)
1452 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1453 /* XXX: should check if same macro (ANSI) */
1454 first = NULL;
1455 t = MACRO_OBJ;
1456 /* We have to parse the whole define as if not in asm mode, in particular
1457 no line comment with '#' must be ignored. Also for function
1458 macros the argument list must be parsed without '.' being an ID
1459 character. */
1460 parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES);
1461 /* '(' must be just after macro definition for MACRO_FUNC */
1462 next_nomacro_spc();
1463 if (tok == '(') {
1464 set_idnum('.', 0);
1465 next_nomacro();
1466 ps = &first;
1467 if (tok != ')') for (;;) {
1468 varg = tok;
1469 next_nomacro();
1470 is_vaargs = 0;
1471 if (varg == TOK_DOTS) {
1472 varg = TOK___VA_ARGS__;
1473 is_vaargs = 1;
1474 } else if (tok == TOK_DOTS && gnu_ext) {
1475 is_vaargs = 1;
1476 next_nomacro();
1478 if (varg < TOK_IDENT)
1479 bad_list:
1480 tcc_error("bad macro parameter list");
1481 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1482 *ps = s;
1483 ps = &s->next;
1484 if (tok == ')')
1485 break;
1486 if (tok != ',' || is_vaargs)
1487 goto bad_list;
1488 next_nomacro();
1490 next_nomacro_spc();
1491 t = MACRO_FUNC;
1494 tokstr_buf.len = 0;
1495 spc = 2;
1496 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1497 /* The body of a macro definition should be parsed such that identifiers
1498 are parsed like the file mode determines (i.e. with '.' being an
1499 ID character in asm mode). But '#' should be retained instead of
1500 regarded as line comment leader, so still don't set ASM_FILE
1501 in parse_flags. */
1502 set_idnum('.', (saved_parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
1503 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1504 /* remove spaces around ## and after '#' */
1505 if (TOK_TWOSHARPS == tok) {
1506 if (2 == spc)
1507 goto bad_twosharp;
1508 if (1 == spc)
1509 --tokstr_buf.len;
1510 spc = 3;
1511 tok = TOK_PPJOIN;
1512 } else if ('#' == tok) {
1513 spc = 4;
1514 } else if (check_space(tok, &spc)) {
1515 goto skip;
1517 tok_str_add2(&tokstr_buf, tok, &tokc);
1518 skip:
1519 next_nomacro_spc();
1522 parse_flags = saved_parse_flags;
1523 if (spc == 1)
1524 --tokstr_buf.len; /* remove trailing space */
1525 tok_str_add(&tokstr_buf, 0);
1526 if (3 == spc)
1527 bad_twosharp:
1528 tcc_error("'##' cannot appear at either end of macro");
1529 define_push(v, t, tok_str_dup(&tokstr_buf), first);
1532 static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
1534 const unsigned char *s;
1535 unsigned int h;
1536 CachedInclude *e;
1537 int i;
1539 h = TOK_HASH_INIT;
1540 s = (unsigned char *) filename;
1541 while (*s) {
1542 #ifdef _WIN32
1543 h = TOK_HASH_FUNC(h, toup(*s));
1544 #else
1545 h = TOK_HASH_FUNC(h, *s);
1546 #endif
1547 s++;
1549 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1551 i = s1->cached_includes_hash[h];
1552 for(;;) {
1553 if (i == 0)
1554 break;
1555 e = s1->cached_includes[i - 1];
1556 if (0 == PATHCMP(e->filename, filename))
1557 return e;
1558 i = e->hash_next;
1560 if (!add)
1561 return NULL;
1563 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1564 strcpy(e->filename, filename);
1565 e->ifndef_macro = e->once = 0;
1566 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1567 /* add in hash table */
1568 e->hash_next = s1->cached_includes_hash[h];
1569 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1570 #ifdef INC_DEBUG
1571 printf("adding cached '%s'\n", filename);
1572 #endif
1573 return e;
1576 static void pragma_parse(TCCState *s1)
1578 next_nomacro();
1579 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1580 int t = tok, v;
1581 Sym *s;
1583 if (next(), tok != '(')
1584 goto pragma_err;
1585 if (next(), tok != TOK_STR)
1586 goto pragma_err;
1587 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1588 if (next(), tok != ')')
1589 goto pragma_err;
1590 if (t == TOK_push_macro) {
1591 while (NULL == (s = define_find(v)))
1592 define_push(v, 0, NULL, NULL);
1593 s->type.ref = s; /* set push boundary */
1594 } else {
1595 for (s = define_stack; s; s = s->prev)
1596 if (s->v == v && s->type.ref == s) {
1597 s->type.ref = NULL;
1598 break;
1601 if (s)
1602 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1603 else
1604 tcc_warning("unbalanced #pragma pop_macro");
1605 pp_debug_tok = t, pp_debug_symv = v;
1607 } else if (tok == TOK_once) {
1608 search_cached_include(s1, file->filename, 1)->once = pp_once;
1610 } else if (s1->ppfp) {
1611 /* tcc -E: keep pragmas below unchanged */
1612 unget_tok(' ');
1613 unget_tok(TOK_PRAGMA);
1614 unget_tok('#');
1615 unget_tok(TOK_LINEFEED);
1617 } else if (tok == TOK_pack) {
1618 /* This may be:
1619 #pragma pack(1) // set
1620 #pragma pack() // reset to default
1621 #pragma pack(push,1) // push & set
1622 #pragma pack(pop) // restore previous */
1623 next();
1624 skip('(');
1625 if (tok == TOK_ASM_pop) {
1626 next();
1627 if (s1->pack_stack_ptr <= s1->pack_stack) {
1628 stk_error:
1629 tcc_error("out of pack stack");
1631 s1->pack_stack_ptr--;
1632 } else {
1633 int val = 0;
1634 if (tok != ')') {
1635 if (tok == TOK_ASM_push) {
1636 next();
1637 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1638 goto stk_error;
1639 s1->pack_stack_ptr++;
1640 skip(',');
1642 if (tok != TOK_CINT)
1643 goto pragma_err;
1644 val = tokc.i;
1645 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1646 goto pragma_err;
1647 next();
1649 *s1->pack_stack_ptr = val;
1651 if (tok != ')')
1652 goto pragma_err;
1654 } else if (tok == TOK_comment) {
1655 char *file;
1656 next();
1657 skip('(');
1658 if (tok != TOK_lib)
1659 goto pragma_warn;
1660 next();
1661 skip(',');
1662 if (tok != TOK_STR)
1663 goto pragma_err;
1664 file = tcc_strdup((char *)tokc.str.data);
1665 dynarray_add((void ***)&s1->pragma_libs, &s1->nb_pragma_libs, file);
1666 next();
1667 if (tok != ')')
1668 goto pragma_err;
1669 } else {
1670 pragma_warn:
1671 if (s1->warn_unsupported)
1672 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1674 return;
1676 pragma_err:
1677 tcc_error("malformed #pragma directive");
1678 return;
1681 /* is_bof is true if first non space token at beginning of file */
1682 ST_FUNC void preprocess(int is_bof)
1684 TCCState *s1 = tcc_state;
1685 int i, c, n, saved_parse_flags;
1686 char buf[1024], *q;
1687 Sym *s;
1689 saved_parse_flags = parse_flags;
1690 parse_flags = PARSE_FLAG_PREPROCESS
1691 | PARSE_FLAG_TOK_NUM
1692 | PARSE_FLAG_TOK_STR
1693 | PARSE_FLAG_LINEFEED
1694 | (parse_flags & PARSE_FLAG_ASM_FILE)
1697 next_nomacro();
1698 redo:
1699 switch(tok) {
1700 case TOK_DEFINE:
1701 pp_debug_tok = tok;
1702 next_nomacro();
1703 pp_debug_symv = tok;
1704 parse_define();
1705 break;
1706 case TOK_UNDEF:
1707 pp_debug_tok = tok;
1708 next_nomacro();
1709 pp_debug_symv = tok;
1710 s = define_find(tok);
1711 /* undefine symbol by putting an invalid name */
1712 if (s)
1713 define_undef(s);
1714 break;
1715 case TOK_INCLUDE:
1716 case TOK_INCLUDE_NEXT:
1717 ch = file->buf_ptr[0];
1718 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1719 skip_spaces();
1720 if (ch == '<') {
1721 c = '>';
1722 goto read_name;
1723 } else if (ch == '\"') {
1724 c = ch;
1725 read_name:
1726 inp();
1727 q = buf;
1728 while (ch != c && ch != '\n' && ch != CH_EOF) {
1729 if ((q - buf) < sizeof(buf) - 1)
1730 *q++ = ch;
1731 if (ch == '\\') {
1732 if (handle_stray_noerror() == 0)
1733 --q;
1734 } else
1735 inp();
1737 *q = '\0';
1738 minp();
1739 #if 0
1740 /* eat all spaces and comments after include */
1741 /* XXX: slightly incorrect */
1742 while (ch1 != '\n' && ch1 != CH_EOF)
1743 inp();
1744 #endif
1745 } else {
1746 int len;
1747 /* computed #include : concatenate everything up to linefeed,
1748 the result must be one of the two accepted forms.
1749 Don't convert pp-tokens to tokens here. */
1750 parse_flags = (PARSE_FLAG_PREPROCESS
1751 | PARSE_FLAG_LINEFEED
1752 | (parse_flags & PARSE_FLAG_ASM_FILE));
1753 next();
1754 buf[0] = '\0';
1755 while (tok != TOK_LINEFEED) {
1756 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1757 next();
1759 len = strlen(buf);
1760 /* check syntax and remove '<>|""' */
1761 if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') &&
1762 (buf[0] != '<' || buf[len-1] != '>'))))
1763 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1764 c = buf[len-1];
1765 memmove(buf, buf + 1, len - 2);
1766 buf[len - 2] = '\0';
1769 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1770 tcc_error("#include recursion too deep");
1771 /* store current file in stack, but increment stack later below */
1772 *s1->include_stack_ptr = file;
1773 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1774 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1775 for (; i < n; ++i) {
1776 char buf1[sizeof file->filename];
1777 CachedInclude *e;
1778 const char *path;
1780 if (i == 0) {
1781 /* check absolute include path */
1782 if (!IS_ABSPATH(buf))
1783 continue;
1784 buf1[0] = 0;
1786 } else if (i == 1) {
1787 /* search in file's dir if "header.h" */
1788 if (c != '\"')
1789 continue;
1790 path = file->filename;
1791 pstrncpy(buf1, path, tcc_basename(path) - path);
1793 } else {
1794 /* search in all the include paths */
1795 int j = i - 2, k = j - s1->nb_include_paths;
1796 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1797 pstrcpy(buf1, sizeof(buf1), path);
1798 pstrcat(buf1, sizeof(buf1), "/");
1801 pstrcat(buf1, sizeof(buf1), buf);
1802 e = search_cached_include(s1, buf1, 0);
1803 if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
1804 /* no need to parse the include because the 'ifndef macro'
1805 is defined (or had #pragma once) */
1806 #ifdef INC_DEBUG
1807 printf("%s: skipping cached %s\n", file->filename, buf1);
1808 #endif
1809 goto include_done;
1812 if (tcc_open(s1, buf1) < 0)
1813 continue;
1815 file->include_next_index = i + 1;
1816 #ifdef INC_DEBUG
1817 printf("%s: including %s\n", file->prev->filename, file->filename);
1818 #endif
1819 /* update target deps */
1820 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1821 tcc_strdup(buf1));
1822 /* push current file in stack */
1823 ++s1->include_stack_ptr;
1824 /* add include file debug info */
1825 if (s1->do_debug)
1826 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1827 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1828 ch = file->buf_ptr[0];
1829 goto the_end;
1831 tcc_error("include file '%s' not found", buf);
1832 include_done:
1833 break;
1834 case TOK_IFNDEF:
1835 c = 1;
1836 goto do_ifdef;
1837 case TOK_IF:
1838 c = expr_preprocess();
1839 goto do_if;
1840 case TOK_IFDEF:
1841 c = 0;
1842 do_ifdef:
1843 next_nomacro();
1844 if (tok < TOK_IDENT)
1845 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1846 if (is_bof) {
1847 if (c) {
1848 #ifdef INC_DEBUG
1849 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1850 #endif
1851 file->ifndef_macro = tok;
1854 c = (define_find(tok) != 0) ^ c;
1855 do_if:
1856 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1857 tcc_error("memory full (ifdef)");
1858 *s1->ifdef_stack_ptr++ = c;
1859 goto test_skip;
1860 case TOK_ELSE:
1861 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1862 tcc_error("#else without matching #if");
1863 if (s1->ifdef_stack_ptr[-1] & 2)
1864 tcc_error("#else after #else");
1865 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1866 goto test_else;
1867 case TOK_ELIF:
1868 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1869 tcc_error("#elif without matching #if");
1870 c = s1->ifdef_stack_ptr[-1];
1871 if (c > 1)
1872 tcc_error("#elif after #else");
1873 /* last #if/#elif expression was true: we skip */
1874 if (c == 1) {
1875 c = 0;
1876 } else {
1877 c = expr_preprocess();
1878 s1->ifdef_stack_ptr[-1] = c;
1880 test_else:
1881 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1882 file->ifndef_macro = 0;
1883 test_skip:
1884 if (!(c & 1)) {
1885 preprocess_skip();
1886 is_bof = 0;
1887 goto redo;
1889 break;
1890 case TOK_ENDIF:
1891 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1892 tcc_error("#endif without matching #if");
1893 s1->ifdef_stack_ptr--;
1894 /* '#ifndef macro' was at the start of file. Now we check if
1895 an '#endif' is exactly at the end of file */
1896 if (file->ifndef_macro &&
1897 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1898 file->ifndef_macro_saved = file->ifndef_macro;
1899 /* need to set to zero to avoid false matches if another
1900 #ifndef at middle of file */
1901 file->ifndef_macro = 0;
1902 while (tok != TOK_LINEFEED)
1903 next_nomacro();
1904 tok_flags |= TOK_FLAG_ENDIF;
1905 goto the_end;
1907 break;
1908 case TOK_PPNUM:
1909 n = strtoul((char*)tokc.str.data, &q, 10);
1910 goto _line_num;
1911 case TOK_LINE:
1912 next();
1913 if (tok != TOK_CINT)
1914 _line_err:
1915 tcc_error("wrong #line format");
1916 n = tokc.i;
1917 _line_num:
1918 next();
1919 if (tok != TOK_LINEFEED) {
1920 if (tok == TOK_STR)
1921 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1922 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1923 break;
1924 else
1925 goto _line_err;
1926 --n;
1928 if (file->fd > 0)
1929 total_lines += file->line_num - n;
1930 file->line_num = n;
1931 if (s1->do_debug)
1932 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1933 break;
1934 case TOK_ERROR:
1935 case TOK_WARNING:
1936 c = tok;
1937 ch = file->buf_ptr[0];
1938 skip_spaces();
1939 q = buf;
1940 while (ch != '\n' && ch != CH_EOF) {
1941 if ((q - buf) < sizeof(buf) - 1)
1942 *q++ = ch;
1943 if (ch == '\\') {
1944 if (handle_stray_noerror() == 0)
1945 --q;
1946 } else
1947 inp();
1949 *q = '\0';
1950 if (c == TOK_ERROR)
1951 tcc_error("#error %s", buf);
1952 else
1953 tcc_warning("#warning %s", buf);
1954 break;
1955 case TOK_PRAGMA:
1956 pragma_parse(s1);
1957 break;
1958 case TOK_LINEFEED:
1959 goto the_end;
1960 default:
1961 /* ignore gas line comment in an 'S' file. */
1962 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1963 goto ignore;
1964 if (tok == '!' && is_bof)
1965 /* '!' is ignored at beginning to allow C scripts. */
1966 goto ignore;
1967 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1968 ignore:
1969 file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
1970 goto the_end;
1972 /* ignore other preprocess commands or #! for C scripts */
1973 while (tok != TOK_LINEFEED)
1974 next_nomacro();
1975 the_end:
1976 parse_flags = saved_parse_flags;
1979 /* evaluate escape codes in a string. */
1980 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1982 int c, n;
1983 const uint8_t *p;
1985 p = buf;
1986 for(;;) {
1987 c = *p;
1988 if (c == '\0')
1989 break;
1990 if (c == '\\') {
1991 p++;
1992 /* escape */
1993 c = *p;
1994 switch(c) {
1995 case '0': case '1': case '2': case '3':
1996 case '4': case '5': case '6': case '7':
1997 /* at most three octal digits */
1998 n = c - '0';
1999 p++;
2000 c = *p;
2001 if (isoct(c)) {
2002 n = n * 8 + c - '0';
2003 p++;
2004 c = *p;
2005 if (isoct(c)) {
2006 n = n * 8 + c - '0';
2007 p++;
2010 c = n;
2011 goto add_char_nonext;
2012 case 'x':
2013 case 'u':
2014 case 'U':
2015 p++;
2016 n = 0;
2017 for(;;) {
2018 c = *p;
2019 if (c >= 'a' && c <= 'f')
2020 c = c - 'a' + 10;
2021 else if (c >= 'A' && c <= 'F')
2022 c = c - 'A' + 10;
2023 else if (isnum(c))
2024 c = c - '0';
2025 else
2026 break;
2027 n = n * 16 + c;
2028 p++;
2030 c = n;
2031 goto add_char_nonext;
2032 case 'a':
2033 c = '\a';
2034 break;
2035 case 'b':
2036 c = '\b';
2037 break;
2038 case 'f':
2039 c = '\f';
2040 break;
2041 case 'n':
2042 c = '\n';
2043 break;
2044 case 'r':
2045 c = '\r';
2046 break;
2047 case 't':
2048 c = '\t';
2049 break;
2050 case 'v':
2051 c = '\v';
2052 break;
2053 case 'e':
2054 if (!gnu_ext)
2055 goto invalid_escape;
2056 c = 27;
2057 break;
2058 case '\'':
2059 case '\"':
2060 case '\\':
2061 case '?':
2062 break;
2063 default:
2064 invalid_escape:
2065 if (c >= '!' && c <= '~')
2066 tcc_warning("unknown escape sequence: \'\\%c\'", c);
2067 else
2068 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
2069 break;
2072 p++;
2073 add_char_nonext:
2074 if (!is_long)
2075 cstr_ccat(outstr, c);
2076 else
2077 cstr_wccat(outstr, c);
2079 /* add a trailing '\0' */
2080 if (!is_long)
2081 cstr_ccat(outstr, '\0');
2082 else
2083 cstr_wccat(outstr, '\0');
2086 static void parse_string(const char *s, int len)
2088 uint8_t buf[1000], *p = buf;
2089 int is_long, sep;
2091 if ((is_long = *s == 'L'))
2092 ++s, --len;
2093 sep = *s++;
2094 len -= 2;
2095 if (len >= sizeof buf)
2096 p = tcc_malloc(len + 1);
2097 memcpy(p, s, len);
2098 p[len] = 0;
2100 cstr_reset(&tokcstr);
2101 parse_escape_string(&tokcstr, p, is_long);
2102 if (p != buf)
2103 tcc_free(p);
2105 if (sep == '\'') {
2106 int char_size;
2107 /* XXX: make it portable */
2108 if (!is_long)
2109 char_size = 1;
2110 else
2111 char_size = sizeof(nwchar_t);
2112 if (tokcstr.size <= char_size)
2113 tcc_error("empty character constant");
2114 if (tokcstr.size > 2 * char_size)
2115 tcc_warning("multi-character character constant");
2116 if (!is_long) {
2117 tokc.i = *(int8_t *)tokcstr.data;
2118 tok = TOK_CCHAR;
2119 } else {
2120 tokc.i = *(nwchar_t *)tokcstr.data;
2121 tok = TOK_LCHAR;
2123 } else {
2124 tokc.str.size = tokcstr.size;
2125 tokc.str.data = tokcstr.data;
2126 if (!is_long)
2127 tok = TOK_STR;
2128 else
2129 tok = TOK_LSTR;
2133 /* we use 64 bit numbers */
2134 #define BN_SIZE 2
2136 /* bn = (bn << shift) | or_val */
2137 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2139 int i;
2140 unsigned int v;
2141 for(i=0;i<BN_SIZE;i++) {
2142 v = bn[i];
2143 bn[i] = (v << shift) | or_val;
2144 or_val = v >> (32 - shift);
2148 static void bn_zero(unsigned int *bn)
2150 int i;
2151 for(i=0;i<BN_SIZE;i++) {
2152 bn[i] = 0;
2156 /* parse number in null terminated string 'p' and return it in the
2157 current token */
2158 static void parse_number(const char *p)
2160 int b, t, shift, frac_bits, s, exp_val, ch;
2161 char *q;
2162 unsigned int bn[BN_SIZE];
2163 double d;
2165 /* number */
2166 q = token_buf;
2167 ch = *p++;
2168 t = ch;
2169 ch = *p++;
2170 *q++ = t;
2171 b = 10;
2172 if (t == '.') {
2173 goto float_frac_parse;
2174 } else if (t == '0') {
2175 if (ch == 'x' || ch == 'X') {
2176 q--;
2177 ch = *p++;
2178 b = 16;
2179 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2180 q--;
2181 ch = *p++;
2182 b = 2;
2185 /* parse all digits. cannot check octal numbers at this stage
2186 because of floating point constants */
2187 while (1) {
2188 if (ch >= 'a' && ch <= 'f')
2189 t = ch - 'a' + 10;
2190 else if (ch >= 'A' && ch <= 'F')
2191 t = ch - 'A' + 10;
2192 else if (isnum(ch))
2193 t = ch - '0';
2194 else
2195 break;
2196 if (t >= b)
2197 break;
2198 if (q >= token_buf + STRING_MAX_SIZE) {
2199 num_too_long:
2200 tcc_error("number too long");
2202 *q++ = ch;
2203 ch = *p++;
2205 if (ch == '.' ||
2206 ((ch == 'e' || ch == 'E') && b == 10) ||
2207 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2208 if (b != 10) {
2209 /* NOTE: strtox should support that for hexa numbers, but
2210 non ISOC99 libcs do not support it, so we prefer to do
2211 it by hand */
2212 /* hexadecimal or binary floats */
2213 /* XXX: handle overflows */
2214 *q = '\0';
2215 if (b == 16)
2216 shift = 4;
2217 else
2218 shift = 1;
2219 bn_zero(bn);
2220 q = token_buf;
2221 while (1) {
2222 t = *q++;
2223 if (t == '\0') {
2224 break;
2225 } else if (t >= 'a') {
2226 t = t - 'a' + 10;
2227 } else if (t >= 'A') {
2228 t = t - 'A' + 10;
2229 } else {
2230 t = t - '0';
2232 bn_lshift(bn, shift, t);
2234 frac_bits = 0;
2235 if (ch == '.') {
2236 ch = *p++;
2237 while (1) {
2238 t = ch;
2239 if (t >= 'a' && t <= 'f') {
2240 t = t - 'a' + 10;
2241 } else if (t >= 'A' && t <= 'F') {
2242 t = t - 'A' + 10;
2243 } else if (t >= '0' && t <= '9') {
2244 t = t - '0';
2245 } else {
2246 break;
2248 if (t >= b)
2249 tcc_error("invalid digit");
2250 bn_lshift(bn, shift, t);
2251 frac_bits += shift;
2252 ch = *p++;
2255 if (ch != 'p' && ch != 'P')
2256 expect("exponent");
2257 ch = *p++;
2258 s = 1;
2259 exp_val = 0;
2260 if (ch == '+') {
2261 ch = *p++;
2262 } else if (ch == '-') {
2263 s = -1;
2264 ch = *p++;
2266 if (ch < '0' || ch > '9')
2267 expect("exponent digits");
2268 while (ch >= '0' && ch <= '9') {
2269 exp_val = exp_val * 10 + ch - '0';
2270 ch = *p++;
2272 exp_val = exp_val * s;
2274 /* now we can generate the number */
2275 /* XXX: should patch directly float number */
2276 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2277 d = ldexp(d, exp_val - frac_bits);
2278 t = toup(ch);
2279 if (t == 'F') {
2280 ch = *p++;
2281 tok = TOK_CFLOAT;
2282 /* float : should handle overflow */
2283 tokc.f = (float)d;
2284 } else if (t == 'L') {
2285 ch = *p++;
2286 #ifdef TCC_TARGET_PE
2287 tok = TOK_CDOUBLE;
2288 tokc.d = d;
2289 #else
2290 tok = TOK_CLDOUBLE;
2291 /* XXX: not large enough */
2292 tokc.ld = (long double)d;
2293 #endif
2294 } else {
2295 tok = TOK_CDOUBLE;
2296 tokc.d = d;
2298 } else {
2299 /* decimal floats */
2300 if (ch == '.') {
2301 if (q >= token_buf + STRING_MAX_SIZE)
2302 goto num_too_long;
2303 *q++ = ch;
2304 ch = *p++;
2305 float_frac_parse:
2306 while (ch >= '0' && ch <= '9') {
2307 if (q >= token_buf + STRING_MAX_SIZE)
2308 goto num_too_long;
2309 *q++ = ch;
2310 ch = *p++;
2313 if (ch == 'e' || ch == 'E') {
2314 if (q >= token_buf + STRING_MAX_SIZE)
2315 goto num_too_long;
2316 *q++ = ch;
2317 ch = *p++;
2318 if (ch == '-' || ch == '+') {
2319 if (q >= token_buf + STRING_MAX_SIZE)
2320 goto num_too_long;
2321 *q++ = ch;
2322 ch = *p++;
2324 if (ch < '0' || ch > '9')
2325 expect("exponent digits");
2326 while (ch >= '0' && ch <= '9') {
2327 if (q >= token_buf + STRING_MAX_SIZE)
2328 goto num_too_long;
2329 *q++ = ch;
2330 ch = *p++;
2333 *q = '\0';
2334 t = toup(ch);
2335 errno = 0;
2336 if (t == 'F') {
2337 ch = *p++;
2338 tok = TOK_CFLOAT;
2339 tokc.f = strtof(token_buf, NULL);
2340 } else if (t == 'L') {
2341 ch = *p++;
2342 #ifdef TCC_TARGET_PE
2343 tok = TOK_CDOUBLE;
2344 tokc.d = strtod(token_buf, NULL);
2345 #else
2346 tok = TOK_CLDOUBLE;
2347 tokc.ld = strtold(token_buf, NULL);
2348 #endif
2349 } else {
2350 tok = TOK_CDOUBLE;
2351 tokc.d = strtod(token_buf, NULL);
2354 } else {
2355 unsigned long long n, n1;
2356 int lcount, ucount, must_64bit;
2357 const char *p1;
2359 /* integer number */
2360 *q = '\0';
2361 q = token_buf;
2362 if (b == 10 && *q == '0') {
2363 b = 8;
2364 q++;
2366 n = 0;
2367 while(1) {
2368 t = *q++;
2369 /* no need for checks except for base 10 / 8 errors */
2370 if (t == '\0')
2371 break;
2372 else if (t >= 'a')
2373 t = t - 'a' + 10;
2374 else if (t >= 'A')
2375 t = t - 'A' + 10;
2376 else
2377 t = t - '0';
2378 if (t >= b)
2379 tcc_error("invalid digit");
2380 n1 = n;
2381 n = n * b + t;
2382 /* detect overflow */
2383 /* XXX: this test is not reliable */
2384 if (n < n1)
2385 tcc_error("integer constant overflow");
2388 /* Determine the characteristics (unsigned and/or 64bit) the type of
2389 the constant must have according to the constant suffix(es) */
2390 lcount = ucount = must_64bit = 0;
2391 p1 = p;
2392 for(;;) {
2393 t = toup(ch);
2394 if (t == 'L') {
2395 if (lcount >= 2)
2396 tcc_error("three 'l's in integer constant");
2397 if (lcount && *(p - 1) != ch)
2398 tcc_error("incorrect integer suffix: %s", p1);
2399 lcount++;
2400 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2401 if (lcount == 2)
2402 #endif
2403 must_64bit = 1;
2404 ch = *p++;
2405 } else if (t == 'U') {
2406 if (ucount >= 1)
2407 tcc_error("two 'u's in integer constant");
2408 ucount++;
2409 ch = *p++;
2410 } else {
2411 break;
2415 /* Whether 64 bits are needed to hold the constant's value */
2416 if (n & 0xffffffff00000000LL || must_64bit) {
2417 tok = TOK_CLLONG;
2418 n1 = n >> 32;
2419 } else {
2420 tok = TOK_CINT;
2421 n1 = n;
2424 /* Whether type must be unsigned to hold the constant's value */
2425 if (ucount || ((n1 >> 31) && (b != 10))) {
2426 if (tok == TOK_CLLONG)
2427 tok = TOK_CULLONG;
2428 else
2429 tok = TOK_CUINT;
2430 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2431 } else if (n1 >> 31) {
2432 if (tok == TOK_CINT)
2433 tok = TOK_CLLONG;
2434 else
2435 tcc_error("integer constant overflow");
2438 tokc.i = n;
2440 if (ch)
2441 tcc_error("invalid number\n");
2445 #define PARSE2(c1, tok1, c2, tok2) \
2446 case c1: \
2447 PEEKC(c, p); \
2448 if (c == c2) { \
2449 p++; \
2450 tok = tok2; \
2451 } else { \
2452 tok = tok1; \
2454 break;
2456 /* return next token without macro substitution */
2457 static inline void next_nomacro1(void)
2459 int t, c, is_long, len;
2460 TokenSym *ts;
2461 uint8_t *p, *p1;
2462 unsigned int h;
2464 p = file->buf_ptr;
2465 redo_no_start:
2466 c = *p;
2467 switch(c) {
2468 case ' ':
2469 case '\t':
2470 tok = c;
2471 p++;
2472 if (parse_flags & PARSE_FLAG_SPACES)
2473 goto keep_tok_flags;
2474 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2475 ++p;
2476 goto redo_no_start;
2477 case '\f':
2478 case '\v':
2479 case '\r':
2480 p++;
2481 goto redo_no_start;
2482 case '\\':
2483 /* first look if it is in fact an end of buffer */
2484 c = handle_stray1(p);
2485 p = file->buf_ptr;
2486 if (c == '\\')
2487 goto parse_simple;
2488 if (c != CH_EOF)
2489 goto redo_no_start;
2491 TCCState *s1 = tcc_state;
2492 if ((parse_flags & PARSE_FLAG_LINEFEED)
2493 && !(tok_flags & TOK_FLAG_EOF)) {
2494 tok_flags |= TOK_FLAG_EOF;
2495 tok = TOK_LINEFEED;
2496 goto keep_tok_flags;
2497 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2498 tok = TOK_EOF;
2499 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2500 tcc_error("missing #endif");
2501 } else if (s1->include_stack_ptr == s1->include_stack) {
2502 /* no include left : end of file. */
2503 tok = TOK_EOF;
2504 } else {
2505 tok_flags &= ~TOK_FLAG_EOF;
2506 /* pop include file */
2508 /* test if previous '#endif' was after a #ifdef at
2509 start of file */
2510 if (tok_flags & TOK_FLAG_ENDIF) {
2511 #ifdef INC_DEBUG
2512 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2513 #endif
2514 search_cached_include(s1, file->filename, 1)
2515 ->ifndef_macro = file->ifndef_macro_saved;
2516 tok_flags &= ~TOK_FLAG_ENDIF;
2519 /* add end of include file debug info */
2520 if (tcc_state->do_debug) {
2521 put_stabd(N_EINCL, 0, 0);
2523 /* pop include stack */
2524 tcc_close();
2525 s1->include_stack_ptr--;
2526 p = file->buf_ptr;
2527 goto redo_no_start;
2530 break;
2532 case '\n':
2533 file->line_num++;
2534 tok_flags |= TOK_FLAG_BOL;
2535 p++;
2536 maybe_newline:
2537 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2538 goto redo_no_start;
2539 tok = TOK_LINEFEED;
2540 goto keep_tok_flags;
2542 case '#':
2543 /* XXX: simplify */
2544 PEEKC(c, p);
2545 if ((tok_flags & TOK_FLAG_BOL) &&
2546 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2547 file->buf_ptr = p;
2548 preprocess(tok_flags & TOK_FLAG_BOF);
2549 p = file->buf_ptr;
2550 goto maybe_newline;
2551 } else {
2552 if (c == '#') {
2553 p++;
2554 tok = TOK_TWOSHARPS;
2555 } else {
2556 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2557 p = parse_line_comment(p - 1);
2558 goto redo_no_start;
2559 } else {
2560 tok = '#';
2564 break;
2566 /* dollar is allowed to start identifiers when not parsing asm */
2567 case '$':
2568 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2569 || (parse_flags & PARSE_FLAG_ASM_FILE))
2570 goto parse_simple;
2572 case 'a': case 'b': case 'c': case 'd':
2573 case 'e': case 'f': case 'g': case 'h':
2574 case 'i': case 'j': case 'k': case 'l':
2575 case 'm': case 'n': case 'o': case 'p':
2576 case 'q': case 'r': case 's': case 't':
2577 case 'u': case 'v': case 'w': case 'x':
2578 case 'y': case 'z':
2579 case 'A': case 'B': case 'C': case 'D':
2580 case 'E': case 'F': case 'G': case 'H':
2581 case 'I': case 'J': case 'K':
2582 case 'M': case 'N': case 'O': case 'P':
2583 case 'Q': case 'R': case 'S': case 'T':
2584 case 'U': case 'V': case 'W': case 'X':
2585 case 'Y': case 'Z':
2586 case '_':
2587 parse_ident_fast:
2588 p1 = p;
2589 h = TOK_HASH_INIT;
2590 h = TOK_HASH_FUNC(h, c);
2591 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2592 h = TOK_HASH_FUNC(h, c);
2593 len = p - p1;
2594 if (c != '\\') {
2595 TokenSym **pts;
2597 /* fast case : no stray found, so we have the full token
2598 and we have already hashed it */
2599 h &= (TOK_HASH_SIZE - 1);
2600 pts = &hash_ident[h];
2601 for(;;) {
2602 ts = *pts;
2603 if (!ts)
2604 break;
2605 if (ts->len == len && !memcmp(ts->str, p1, len))
2606 goto token_found;
2607 pts = &(ts->hash_next);
2609 ts = tok_alloc_new(pts, (char *) p1, len);
2610 token_found: ;
2611 } else {
2612 /* slower case */
2613 cstr_reset(&tokcstr);
2614 cstr_cat(&tokcstr, p1, len);
2615 p--;
2616 PEEKC(c, p);
2617 parse_ident_slow:
2618 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2620 cstr_ccat(&tokcstr, c);
2621 PEEKC(c, p);
2623 ts = tok_alloc(tokcstr.data, tokcstr.size);
2625 tok = ts->tok;
2626 break;
2627 case 'L':
2628 t = p[1];
2629 if (t != '\\' && t != '\'' && t != '\"') {
2630 /* fast case */
2631 goto parse_ident_fast;
2632 } else {
2633 PEEKC(c, p);
2634 if (c == '\'' || c == '\"') {
2635 is_long = 1;
2636 goto str_const;
2637 } else {
2638 cstr_reset(&tokcstr);
2639 cstr_ccat(&tokcstr, 'L');
2640 goto parse_ident_slow;
2643 break;
2645 case '0': case '1': case '2': case '3':
2646 case '4': case '5': case '6': case '7':
2647 case '8': case '9':
2648 t = c;
2649 PEEKC(c, p);
2650 /* after the first digit, accept digits, alpha, '.' or sign if
2651 prefixed by 'eEpP' */
2652 parse_num:
2653 cstr_reset(&tokcstr);
2654 for(;;) {
2655 cstr_ccat(&tokcstr, t);
2656 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2657 || c == '.'
2658 || ((c == '+' || c == '-')
2659 && (((t == 'e' || t == 'E')
2660 && !(parse_flags & PARSE_FLAG_ASM_FILE
2661 /* 0xe+1 is 3 tokens in asm */
2662 && ((char*)tokcstr.data)[0] == '0'
2663 && toup(((char*)tokcstr.data)[1]) == 'X'))
2664 || t == 'p' || t == 'P'))))
2665 break;
2666 t = c;
2667 PEEKC(c, p);
2669 /* We add a trailing '\0' to ease parsing */
2670 cstr_ccat(&tokcstr, '\0');
2671 tokc.str.size = tokcstr.size;
2672 tokc.str.data = tokcstr.data;
2673 tok = TOK_PPNUM;
2674 break;
2676 case '.':
2677 /* special dot handling because it can also start a number */
2678 PEEKC(c, p);
2679 if (isnum(c)) {
2680 t = '.';
2681 goto parse_num;
2682 } else if ((isidnum_table['.' - CH_EOF] & IS_ID)
2683 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
2684 *--p = c = '.';
2685 goto parse_ident_fast;
2686 } else if (c == '.') {
2687 PEEKC(c, p);
2688 if (c == '.') {
2689 p++;
2690 tok = TOK_DOTS;
2691 } else {
2692 *--p = '.'; /* may underflow into file->unget[] */
2693 tok = '.';
2695 } else {
2696 tok = '.';
2698 break;
2699 case '\'':
2700 case '\"':
2701 is_long = 0;
2702 str_const:
2703 cstr_reset(&tokcstr);
2704 if (is_long)
2705 cstr_ccat(&tokcstr, 'L');
2706 cstr_ccat(&tokcstr, c);
2707 p = parse_pp_string(p, c, &tokcstr);
2708 cstr_ccat(&tokcstr, c);
2709 cstr_ccat(&tokcstr, '\0');
2710 tokc.str.size = tokcstr.size;
2711 tokc.str.data = tokcstr.data;
2712 tok = TOK_PPSTR;
2713 break;
2715 case '<':
2716 PEEKC(c, p);
2717 if (c == '=') {
2718 p++;
2719 tok = TOK_LE;
2720 } else if (c == '<') {
2721 PEEKC(c, p);
2722 if (c == '=') {
2723 p++;
2724 tok = TOK_A_SHL;
2725 } else {
2726 tok = TOK_SHL;
2728 } else {
2729 tok = TOK_LT;
2731 break;
2732 case '>':
2733 PEEKC(c, p);
2734 if (c == '=') {
2735 p++;
2736 tok = TOK_GE;
2737 } else if (c == '>') {
2738 PEEKC(c, p);
2739 if (c == '=') {
2740 p++;
2741 tok = TOK_A_SAR;
2742 } else {
2743 tok = TOK_SAR;
2745 } else {
2746 tok = TOK_GT;
2748 break;
2750 case '&':
2751 PEEKC(c, p);
2752 if (c == '&') {
2753 p++;
2754 tok = TOK_LAND;
2755 } else if (c == '=') {
2756 p++;
2757 tok = TOK_A_AND;
2758 } else {
2759 tok = '&';
2761 break;
2763 case '|':
2764 PEEKC(c, p);
2765 if (c == '|') {
2766 p++;
2767 tok = TOK_LOR;
2768 } else if (c == '=') {
2769 p++;
2770 tok = TOK_A_OR;
2771 } else {
2772 tok = '|';
2774 break;
2776 case '+':
2777 PEEKC(c, p);
2778 if (c == '+') {
2779 p++;
2780 tok = TOK_INC;
2781 } else if (c == '=') {
2782 p++;
2783 tok = TOK_A_ADD;
2784 } else {
2785 tok = '+';
2787 break;
2789 case '-':
2790 PEEKC(c, p);
2791 if (c == '-') {
2792 p++;
2793 tok = TOK_DEC;
2794 } else if (c == '=') {
2795 p++;
2796 tok = TOK_A_SUB;
2797 } else if (c == '>') {
2798 p++;
2799 tok = TOK_ARROW;
2800 } else {
2801 tok = '-';
2803 break;
2805 PARSE2('!', '!', '=', TOK_NE)
2806 PARSE2('=', '=', '=', TOK_EQ)
2807 PARSE2('*', '*', '=', TOK_A_MUL)
2808 PARSE2('%', '%', '=', TOK_A_MOD)
2809 PARSE2('^', '^', '=', TOK_A_XOR)
2811 /* comments or operator */
2812 case '/':
2813 PEEKC(c, p);
2814 if (c == '*') {
2815 p = parse_comment(p);
2816 /* comments replaced by a blank */
2817 tok = ' ';
2818 goto keep_tok_flags;
2819 } else if (c == '/') {
2820 p = parse_line_comment(p);
2821 tok = ' ';
2822 goto keep_tok_flags;
2823 } else if (c == '=') {
2824 p++;
2825 tok = TOK_A_DIV;
2826 } else {
2827 tok = '/';
2829 break;
2831 /* simple tokens */
2832 case '(':
2833 case ')':
2834 case '[':
2835 case ']':
2836 case '{':
2837 case '}':
2838 case ',':
2839 case ';':
2840 case ':':
2841 case '?':
2842 case '~':
2843 case '@': /* only used in assembler */
2844 parse_simple:
2845 tok = c;
2846 p++;
2847 break;
2848 default:
2849 if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
2850 goto parse_ident_fast;
2851 if (parse_flags & PARSE_FLAG_ASM_FILE)
2852 goto parse_simple;
2853 tcc_error("unrecognized character \\x%02x", c);
2854 break;
2856 tok_flags = 0;
2857 keep_tok_flags:
2858 file->buf_ptr = p;
2859 #if defined(PARSE_DEBUG)
2860 printf("token = %s\n", get_tok_str(tok, &tokc));
2861 #endif
2864 /* return next token without macro substitution. Can read input from
2865 macro_ptr buffer */
2866 static void next_nomacro_spc(void)
2868 if (macro_ptr) {
2869 redo:
2870 tok = *macro_ptr;
2871 if (tok) {
2872 TOK_GET(&tok, &macro_ptr, &tokc);
2873 if (tok == TOK_LINENUM) {
2874 file->line_num = tokc.i;
2875 goto redo;
2878 } else {
2879 next_nomacro1();
2883 ST_FUNC void next_nomacro(void)
2885 do {
2886 next_nomacro_spc();
2887 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2891 static void macro_subst(
2892 TokenString *tok_str,
2893 Sym **nested_list,
2894 const int *macro_str,
2895 int can_read_stream
2898 /* substitute arguments in replacement lists in macro_str by the values in
2899 args (field d) and return allocated string */
2900 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2902 int t, t0, t1, spc;
2903 const int *st;
2904 Sym *s;
2905 CValue cval;
2906 TokenString str;
2907 CString cstr;
2909 tok_str_new(&str);
2910 t0 = t1 = 0;
2911 while(1) {
2912 TOK_GET(&t, &macro_str, &cval);
2913 if (!t)
2914 break;
2915 if (t == '#') {
2916 /* stringize */
2917 TOK_GET(&t, &macro_str, &cval);
2918 if (!t)
2919 goto bad_stringy;
2920 s = sym_find2(args, t);
2921 if (s) {
2922 cstr_new(&cstr);
2923 cstr_ccat(&cstr, '\"');
2924 st = s->d;
2925 spc = 0;
2926 while (*st) {
2927 TOK_GET(&t, &st, &cval);
2928 if (t != TOK_PLCHLDR
2929 && t != TOK_NOSUBST
2930 && 0 == check_space(t, &spc)) {
2931 const char *s = get_tok_str(t, &cval);
2932 while (*s) {
2933 if (t == TOK_PPSTR && *s != '\'')
2934 add_char(&cstr, *s);
2935 else
2936 cstr_ccat(&cstr, *s);
2937 ++s;
2941 cstr.size -= spc;
2942 cstr_ccat(&cstr, '\"');
2943 cstr_ccat(&cstr, '\0');
2944 #ifdef PP_DEBUG
2945 printf("\nstringize: <%s>\n", (char *)cstr.data);
2946 #endif
2947 /* add string */
2948 cval.str.size = cstr.size;
2949 cval.str.data = cstr.data;
2950 tok_str_add2(&str, TOK_PPSTR, &cval);
2951 cstr_free(&cstr);
2952 } else {
2953 bad_stringy:
2954 expect("macro parameter after '#'");
2956 } else if (t >= TOK_IDENT) {
2957 s = sym_find2(args, t);
2958 if (s) {
2959 int l0 = str.len;
2960 st = s->d;
2961 /* if '##' is present before or after, no arg substitution */
2962 if (*macro_str == TOK_PPJOIN || t1 == TOK_PPJOIN) {
2963 /* special case for var arg macros : ## eats the ','
2964 if empty VA_ARGS variable. */
2965 if (t1 == TOK_PPJOIN && t0 == ',' && gnu_ext && s->type.t) {
2966 if (*st == 0) {
2967 /* suppress ',' '##' */
2968 str.len -= 2;
2969 } else {
2970 /* suppress '##' and add variable */
2971 str.len--;
2972 goto add_var;
2974 } else {
2975 for(;;) {
2976 int t1;
2977 TOK_GET(&t1, &st, &cval);
2978 if (!t1)
2979 break;
2980 tok_str_add2(&str, t1, &cval);
2984 } else {
2985 add_var:
2986 /* NOTE: the stream cannot be read when macro
2987 substituing an argument */
2988 macro_subst(&str, nested_list, st, 0);
2990 if (str.len == l0) /* exanded to empty string */
2991 tok_str_add(&str, TOK_PLCHLDR);
2992 } else {
2993 tok_str_add(&str, t);
2995 } else {
2996 tok_str_add2(&str, t, &cval);
2998 t0 = t1, t1 = t;
3000 tok_str_add(&str, 0);
3001 return str.str;
3004 static char const ab_month_name[12][4] =
3006 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3007 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3010 /* peek or read [ws_str == NULL] next token from function macro call,
3011 walking up macro levels up to the file if necessary */
3012 static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *ws_str)
3014 int t;
3015 const int *p;
3016 Sym *sa;
3018 for (;;) {
3019 if (macro_ptr) {
3020 p = macro_ptr, t = *p;
3021 if (ws_str) {
3022 while (is_space(t) || TOK_LINEFEED == t)
3023 tok_str_add(ws_str, t), t = *++p;
3025 if (t == 0 && can_read_stream) {
3026 end_macro();
3027 /* also, end of scope for nested defined symbol */
3028 sa = *nested_list;
3029 while (sa && sa->v == 0)
3030 sa = sa->prev;
3031 if (sa)
3032 sa->v = 0;
3033 continue;
3035 } else {
3036 ch = handle_eob();
3037 if (ws_str) {
3038 while (is_space(ch) || ch == '\n' || ch == '/') {
3039 if (ch == '/') {
3040 int c;
3041 uint8_t *p = file->buf_ptr;
3042 PEEKC(c, p);
3043 if (c == '*') {
3044 p = parse_comment(p);
3045 file->buf_ptr = p - 1;
3046 } else if (c == '/') {
3047 p = parse_line_comment(p);
3048 file->buf_ptr = p - 1;
3049 } else
3050 break;
3051 ch = ' ';
3053 if (!(ch == '\f' || ch == '\v' || ch == '\r'))
3054 tok_str_add(ws_str, ch);
3055 cinp();
3058 t = ch;
3061 if (ws_str)
3062 return t;
3063 next_nomacro_spc();
3064 return tok;
3068 /* do macro substitution of current token with macro 's' and add
3069 result to (tok_str,tok_len). 'nested_list' is the list of all
3070 macros we got inside to avoid recursing. Return non zero if no
3071 substitution needs to be done */
3072 static int macro_subst_tok(
3073 TokenString *tok_str,
3074 Sym **nested_list,
3075 Sym *s,
3076 int can_read_stream)
3078 Sym *args, *sa, *sa1;
3079 int parlevel, *mstr, t, t1, spc;
3080 TokenString str;
3081 char *cstrval;
3082 CValue cval;
3083 CString cstr;
3084 char buf[32];
3086 /* if symbol is a macro, prepare substitution */
3087 /* special macros */
3088 if (tok == TOK___LINE__) {
3089 snprintf(buf, sizeof(buf), "%d", file->line_num);
3090 cstrval = buf;
3091 t1 = TOK_PPNUM;
3092 goto add_cstr1;
3093 } else if (tok == TOK___FILE__) {
3094 cstrval = file->filename;
3095 goto add_cstr;
3096 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3097 time_t ti;
3098 struct tm *tm;
3100 time(&ti);
3101 tm = localtime(&ti);
3102 if (tok == TOK___DATE__) {
3103 snprintf(buf, sizeof(buf), "%s %2d %d",
3104 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3105 } else {
3106 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3107 tm->tm_hour, tm->tm_min, tm->tm_sec);
3109 cstrval = buf;
3110 add_cstr:
3111 t1 = TOK_STR;
3112 add_cstr1:
3113 cstr_new(&cstr);
3114 cstr_cat(&cstr, cstrval, 0);
3115 cval.str.size = cstr.size;
3116 cval.str.data = cstr.data;
3117 tok_str_add2(tok_str, t1, &cval);
3118 cstr_free(&cstr);
3119 } else {
3120 int saved_parse_flags = parse_flags;
3122 mstr = s->d;
3123 if (s->type.t == MACRO_FUNC) {
3124 /* whitespace between macro name and argument list */
3125 TokenString ws_str;
3126 tok_str_new(&ws_str);
3128 spc = 0;
3129 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3130 | PARSE_FLAG_ACCEPT_STRAYS;
3132 /* get next token from argument stream */
3133 t = next_argstream(nested_list, can_read_stream, &ws_str);
3134 if (t != '(') {
3135 /* not a macro substitution after all, restore the
3136 * macro token plus all whitespace we've read.
3137 * whitespace is intentionally not merged to preserve
3138 * newlines. */
3139 parse_flags = saved_parse_flags;
3140 tok_str_add(tok_str, tok);
3141 if (parse_flags & PARSE_FLAG_SPACES) {
3142 int i;
3143 for (i = 0; i < ws_str.len; i++)
3144 tok_str_add(tok_str, ws_str.str[i]);
3146 tok_str_free_str(ws_str.str);
3147 return 0;
3148 } else {
3149 tok_str_free_str(ws_str.str);
3151 next_nomacro(); /* eat '(' */
3153 /* argument macro */
3154 args = NULL;
3155 sa = s->next;
3156 /* NOTE: empty args are allowed, except if no args */
3157 for(;;) {
3158 do {
3159 next_argstream(nested_list, can_read_stream, NULL);
3160 } while (is_space(tok) || TOK_LINEFEED == tok);
3161 empty_arg:
3162 /* handle '()' case */
3163 if (!args && !sa && tok == ')')
3164 break;
3165 if (!sa)
3166 tcc_error("macro '%s' used with too many args",
3167 get_tok_str(s->v, 0));
3168 tok_str_new(&str);
3169 parlevel = spc = 0;
3170 /* NOTE: non zero sa->t indicates VA_ARGS */
3171 while ((parlevel > 0 ||
3172 (tok != ')' &&
3173 (tok != ',' || sa->type.t)))) {
3174 if (tok == TOK_EOF || tok == 0)
3175 break;
3176 if (tok == '(')
3177 parlevel++;
3178 else if (tok == ')')
3179 parlevel--;
3180 if (tok == TOK_LINEFEED)
3181 tok = ' ';
3182 if (!check_space(tok, &spc))
3183 tok_str_add2(&str, tok, &tokc);
3184 next_argstream(nested_list, can_read_stream, NULL);
3186 if (parlevel)
3187 expect(")");
3188 str.len -= spc;
3189 tok_str_add(&str, 0);
3190 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3191 sa1->d = str.str;
3192 sa = sa->next;
3193 if (tok == ')') {
3194 /* special case for gcc var args: add an empty
3195 var arg argument if it is omitted */
3196 if (sa && sa->type.t && gnu_ext)
3197 goto empty_arg;
3198 break;
3200 if (tok != ',')
3201 expect(",");
3203 if (sa) {
3204 tcc_error("macro '%s' used with too few args",
3205 get_tok_str(s->v, 0));
3208 parse_flags = saved_parse_flags;
3210 /* now subst each arg */
3211 mstr = macro_arg_subst(nested_list, mstr, args);
3212 /* free memory */
3213 sa = args;
3214 while (sa) {
3215 sa1 = sa->prev;
3216 tok_str_free_str(sa->d);
3217 sym_free(sa);
3218 sa = sa1;
3222 sym_push2(nested_list, s->v, 0, 0);
3223 parse_flags = saved_parse_flags;
3224 macro_subst(tok_str, nested_list, mstr, can_read_stream | 2);
3226 /* pop nested defined symbol */
3227 sa1 = *nested_list;
3228 *nested_list = sa1->prev;
3229 sym_free(sa1);
3230 if (mstr != s->d)
3231 tok_str_free_str(mstr);
3233 return 0;
3236 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3238 CString cstr;
3239 int n, ret = 1;
3241 cstr_new(&cstr);
3242 if (t1 != TOK_PLCHLDR)
3243 cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3244 n = cstr.size;
3245 if (t2 != TOK_PLCHLDR)
3246 cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3247 cstr_ccat(&cstr, '\0');
3249 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3250 memcpy(file->buffer, cstr.data, cstr.size);
3251 for (;;) {
3252 next_nomacro1();
3253 if (0 == *file->buf_ptr)
3254 break;
3255 if (is_space(tok))
3256 continue;
3257 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
3258 " preprocessing token", n, cstr.data, (char*)cstr.data + n);
3259 ret = 0;
3260 break;
3262 tcc_close();
3263 //printf("paste <%s>\n", (char*)cstr.data);
3264 cstr_free(&cstr);
3265 return ret;
3268 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3269 return the resulting string (which must be freed). */
3270 static inline int *macro_twosharps(const int *ptr0)
3272 int t;
3273 CValue cval;
3274 TokenString macro_str1;
3275 int start_of_nosubsts = -1;
3276 const int *ptr;
3278 /* we search the first '##' */
3279 for (ptr = ptr0;;) {
3280 TOK_GET(&t, &ptr, &cval);
3281 if (t == TOK_PPJOIN)
3282 break;
3283 if (t == 0)
3284 return NULL;
3287 tok_str_new(&macro_str1);
3289 //tok_print(" $$$", ptr0);
3290 for (ptr = ptr0;;) {
3291 TOK_GET(&t, &ptr, &cval);
3292 if (t == 0)
3293 break;
3294 if (t == TOK_PPJOIN)
3295 continue;
3296 while (*ptr == TOK_PPJOIN) {
3297 int t1; CValue cv1;
3298 /* given 'a##b', remove nosubsts preceding 'a' */
3299 if (start_of_nosubsts >= 0)
3300 macro_str1.len = start_of_nosubsts;
3301 /* given 'a##b', remove nosubsts preceding 'b' */
3302 while ((t1 = *++ptr) == TOK_NOSUBST)
3304 if (t1 && t1 != TOK_PPJOIN) {
3305 TOK_GET(&t1, &ptr, &cv1);
3306 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3307 if (paste_tokens(t, &cval, t1, &cv1)) {
3308 t = tok, cval = tokc;
3309 } else {
3310 tok_str_add2(&macro_str1, t, &cval);
3311 t = t1, cval = cv1;
3316 if (t == TOK_NOSUBST) {
3317 if (start_of_nosubsts < 0)
3318 start_of_nosubsts = macro_str1.len;
3319 } else {
3320 start_of_nosubsts = -1;
3322 tok_str_add2(&macro_str1, t, &cval);
3324 tok_str_add(&macro_str1, 0);
3325 //tok_print(" ###", macro_str1.str);
3326 return macro_str1.str;
3329 /* do macro substitution of macro_str and add result to
3330 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3331 inside to avoid recursing. */
3332 static void macro_subst(
3333 TokenString *tok_str,
3334 Sym **nested_list,
3335 const int *macro_str,
3336 int can_read_stream
3339 Sym *s;
3340 const int *ptr;
3341 int t, spc, nosubst;
3342 CValue cval;
3343 int *macro_str1 = NULL;
3345 /* first scan for '##' operator handling */
3346 ptr = macro_str;
3347 spc = nosubst = 0;
3349 /* first scan for '##' operator handling */
3350 if (can_read_stream) {
3351 macro_str1 = macro_twosharps(ptr);
3352 if (macro_str1)
3353 ptr = macro_str1;
3356 while (1) {
3357 TOK_GET(&t, &ptr, &cval);
3358 if (t == 0)
3359 break;
3361 if (t >= TOK_IDENT && 0 == nosubst) {
3362 s = define_find(t);
3363 if (s == NULL)
3364 goto no_subst;
3366 /* if nested substitution, do nothing */
3367 if (sym_find2(*nested_list, t)) {
3368 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3369 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3370 goto no_subst;
3374 TokenString str;
3375 str.str = (int*)ptr;
3376 begin_macro(&str, 2);
3378 tok = t;
3379 macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3381 if (str.alloc == 3) {
3382 /* already finished by reading function macro arguments */
3383 break;
3386 ptr = macro_ptr;
3387 end_macro ();
3390 spc = (tok_str->len &&
3391 is_space(tok_last(tok_str->str,
3392 tok_str->str + tok_str->len)));
3394 } else {
3396 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3397 tcc_error("stray '\\' in program");
3399 no_subst:
3400 if (!check_space(t, &spc))
3401 tok_str_add2(tok_str, t, &cval);
3402 nosubst = 0;
3403 if (t == TOK_NOSUBST)
3404 nosubst = 1;
3407 if (macro_str1)
3408 tok_str_free_str(macro_str1);
3412 /* return next token with macro substitution */
3413 ST_FUNC void next(void)
3415 redo:
3416 if (parse_flags & PARSE_FLAG_SPACES)
3417 next_nomacro_spc();
3418 else
3419 next_nomacro();
3421 if (macro_ptr) {
3422 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3423 /* discard preprocessor markers */
3424 goto redo;
3425 } else if (tok == 0) {
3426 /* end of macro or unget token string */
3427 end_macro();
3428 goto redo;
3430 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3431 Sym *s;
3432 /* if reading from file, try to substitute macros */
3433 s = define_find(tok);
3434 if (s) {
3435 Sym *nested_list = NULL;
3436 tokstr_buf.len = 0;
3437 macro_subst_tok(&tokstr_buf, &nested_list, s, 1);
3438 tok_str_add(&tokstr_buf, 0);
3439 begin_macro(&tokstr_buf, 2);
3440 goto redo;
3443 /* convert preprocessor tokens into C tokens */
3444 if (tok == TOK_PPNUM) {
3445 if (parse_flags & PARSE_FLAG_TOK_NUM)
3446 parse_number((char *)tokc.str.data);
3447 } else if (tok == TOK_PPSTR) {
3448 if (parse_flags & PARSE_FLAG_TOK_STR)
3449 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3453 /* push back current token and set current token to 'last_tok'. Only
3454 identifier case handled for labels. */
3455 ST_INLN void unget_tok(int last_tok)
3458 TokenString *str = tok_str_alloc();
3459 tok_str_add2(str, tok, &tokc);
3460 tok_str_add(str, 0);
3461 begin_macro(str, 1);
3462 tok = last_tok;
3465 ST_FUNC void preprocess_start(TCCState *s1)
3467 char *buf;
3468 s1->include_stack_ptr = s1->include_stack;
3469 /* XXX: move that before to avoid having to initialize
3470 file->ifdef_stack_ptr ? */
3471 s1->ifdef_stack_ptr = s1->ifdef_stack;
3472 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3473 pp_once++;
3475 pvtop = vtop = vstack - 1;
3476 s1->pack_stack[0] = 0;
3477 s1->pack_stack_ptr = s1->pack_stack;
3479 set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
3480 set_idnum('.', (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0);
3481 buf = tcc_malloc(3 + strlen(file->filename));
3482 sprintf(buf, "\"%s\"", file->filename);
3483 tcc_undefine_symbol(s1, "__BASE_FILE__");
3484 tcc_define_symbol(s1, "__BASE_FILE__", buf);
3485 tcc_free(buf);
3486 if (s1->nb_cmd_include_files) {
3487 CString cstr;
3488 int i;
3489 cstr_new(&cstr);
3490 for (i = 0; i < s1->nb_cmd_include_files; i++) {
3491 cstr_cat(&cstr, "#include \"", -1);
3492 cstr_cat(&cstr, s1->cmd_include_files[i], -1);
3493 cstr_cat(&cstr, "\"\n", -1);
3495 *s1->include_stack_ptr++ = file;
3496 tcc_open_bf(s1, "<command line>", cstr.size);
3497 memcpy(file->buffer, cstr.data, cstr.size);
3498 cstr_free(&cstr);
3502 ST_FUNC void tccpp_new(TCCState *s)
3504 int i, c;
3505 const char *p, *r;
3507 /* might be used in error() before preprocess_start() */
3508 s->include_stack_ptr = s->include_stack;
3510 /* init isid table */
3511 for(i = CH_EOF; i<128; i++)
3512 set_idnum(i,
3513 is_space(i) ? IS_SPC
3514 : isid(i) ? IS_ID
3515 : isnum(i) ? IS_NUM
3516 : 0);
3518 for(i = 128; i<256; i++)
3519 set_idnum(i, IS_ID);
3521 /* init allocators */
3522 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
3523 tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
3524 tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
3526 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3527 cstr_new(&cstr_buf);
3528 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
3529 tok_str_new(&tokstr_buf);
3530 tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
3532 tok_ident = TOK_IDENT;
3533 p = tcc_keywords;
3534 while (*p) {
3535 r = p;
3536 for(;;) {
3537 c = *r++;
3538 if (c == '\0')
3539 break;
3541 tok_alloc(p, r - p - 1);
3542 p = r;
3546 ST_FUNC void tccpp_delete(TCCState *s)
3548 int i, n;
3550 /* free -D and compiler defines */
3551 free_defines(NULL);
3553 /* cleanup from error/setjmp */
3554 while (macro_stack)
3555 end_macro();
3556 macro_ptr = NULL;
3558 /* free tokens */
3559 n = tok_ident - TOK_IDENT;
3560 for(i = 0; i < n; i++)
3561 tal_free(toksym_alloc, table_ident[i]);
3562 tcc_free(table_ident);
3563 table_ident = NULL;
3565 /* free static buffers */
3566 cstr_free(&tokcstr);
3567 cstr_free(&cstr_buf);
3568 tok_str_free_str(tokstr_buf.str);
3570 /* free allocators */
3571 tal_delete(toksym_alloc);
3572 toksym_alloc = NULL;
3573 tal_delete(tokstr_alloc);
3574 tokstr_alloc = NULL;
3575 tal_delete(cstr_alloc);
3576 cstr_alloc = NULL;
3579 /* ------------------------------------------------------------------------- */
3580 /* tcc -E [-P[1]] [-dD} support */
3582 static void tok_print(const char *msg, const int *str)
3584 FILE *fp;
3585 int t;
3586 CValue cval;
3588 fp = tcc_state->ppfp;
3589 if (!fp || !tcc_state->dflag)
3590 fp = stdout;
3592 fprintf(fp, "%s ", msg);
3593 while (str) {
3594 TOK_GET(&t, &str, &cval);
3595 if (!t)
3596 break;
3597 fprintf(fp,"%s", get_tok_str(t, &cval));
3599 fprintf(fp, "\n");
3602 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3604 int d = f->line_num - f->line_ref;
3606 if (s1->dflag & 4)
3607 return;
3609 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3611 } else if (level == 0 && f->line_ref && d < 8) {
3612 while (d > 0)
3613 fputs("\n", s1->ppfp), --d;
3614 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3615 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3616 } else {
3617 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3618 level > 0 ? " 1" : level < 0 ? " 2" : "");
3620 f->line_ref = f->line_num;
3623 static void define_print(TCCState *s1, int v)
3625 FILE *fp;
3626 Sym *s;
3628 s = define_find(v);
3629 if (NULL == s || NULL == s->d)
3630 return;
3632 fp = s1->ppfp;
3633 fprintf(fp, "#define %s", get_tok_str(v, NULL));
3634 if (s->type.t == MACRO_FUNC) {
3635 Sym *a = s->next;
3636 fprintf(fp,"(");
3637 if (a)
3638 for (;;) {
3639 fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3640 if (!(a = a->next))
3641 break;
3642 fprintf(fp,",");
3644 fprintf(fp,")");
3646 tok_print("", s->d);
3649 static void pp_debug_defines(TCCState *s1)
3651 int v, t;
3652 const char *vs;
3653 FILE *fp;
3655 t = pp_debug_tok;
3656 if (t == 0)
3657 return;
3659 file->line_num--;
3660 pp_line(s1, file, 0);
3661 file->line_ref = ++file->line_num;
3663 fp = s1->ppfp;
3664 v = pp_debug_symv;
3665 vs = get_tok_str(v, NULL);
3666 if (t == TOK_DEFINE) {
3667 define_print(s1, v);
3668 } else if (t == TOK_UNDEF) {
3669 fprintf(fp, "#undef %s\n", vs);
3670 } else if (t == TOK_push_macro) {
3671 fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3672 } else if (t == TOK_pop_macro) {
3673 fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3675 pp_debug_tok = 0;
3678 static void pp_debug_builtins(TCCState *s1)
3680 int v;
3681 for (v = TOK_IDENT; v < tok_ident; ++v)
3682 define_print(s1, v);
3685 /* Add a space between tokens a and b to avoid unwanted textual pasting */
3686 static int pp_need_space(int a, int b)
3688 return 'E' == a ? '+' == b || '-' == b
3689 : '+' == a ? TOK_INC == b || '+' == b
3690 : '-' == a ? TOK_DEC == b || '-' == b
3691 : a >= TOK_IDENT ? b >= TOK_IDENT
3692 : 0;
3695 /* maybe hex like 0x1e */
3696 static int pp_check_he0xE(int t, const char *p)
3698 if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
3699 return 'E';
3700 return t;
3703 /* Preprocess the current file */
3704 ST_FUNC int tcc_preprocess(TCCState *s1)
3706 BufferedFile **iptr;
3707 int token_seen, spcs, level;
3708 const char *p;
3709 Sym *define_start;
3711 preprocess_start(s1);
3712 ch = file->buf_ptr[0];
3713 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3714 parse_flags = PARSE_FLAG_PREPROCESS
3715 | (parse_flags & PARSE_FLAG_ASM_FILE)
3716 | PARSE_FLAG_LINEFEED
3717 | PARSE_FLAG_SPACES
3718 | PARSE_FLAG_ACCEPT_STRAYS
3720 define_start = define_stack;
3722 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3723 capability to compile and run itself, provided all numbers are
3724 given as decimals. tcc -E -P10 will do. */
3725 if (s1->Pflag == 1 + 10)
3726 parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
3728 #ifdef PP_BENCH
3729 /* for PP benchmarks */
3730 do next(); while (tok != TOK_EOF);
3731 free_defines(define_start);
3732 return 0;
3733 #endif
3735 if (s1->dflag & 1) {
3736 pp_debug_builtins(s1);
3737 s1->dflag &= ~1;
3740 token_seen = TOK_LINEFEED, spcs = 0;
3741 pp_line(s1, file, 0);
3743 for (;;) {
3744 iptr = s1->include_stack_ptr;
3745 next();
3746 if (tok == TOK_EOF)
3747 break;
3748 level = s1->include_stack_ptr - iptr;
3749 if (level) {
3750 if (level > 0)
3751 pp_line(s1, *iptr, 0);
3752 pp_line(s1, file, level);
3755 if (s1->dflag) {
3756 pp_debug_defines(s1);
3757 if (s1->dflag & 4)
3758 continue;
3761 if (token_seen == TOK_LINEFEED) {
3762 if (tok == ' ') {
3763 ++spcs;
3764 continue;
3766 if (tok == TOK_LINEFEED) {
3767 spcs = 0;
3768 continue;
3770 pp_line(s1, file, 0);
3771 } else if (tok == TOK_LINEFEED) {
3772 ++file->line_ref;
3773 } else {
3774 spcs = pp_need_space(token_seen, tok);
3777 while (spcs)
3778 fputs(" ", s1->ppfp), --spcs;
3779 fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
3780 token_seen = pp_check_he0xE(tok, p);;
3782 /* reset define stack, but keep -D and built-ins */
3783 free_defines(define_start);
3784 return 0;
3787 /* ------------------------------------------------------------------------- */