gen_addrpc32: absolute ptr needs *ABS* relocation
[tinycc.git] / tccpp.c
blobe43cb233b0f49f899184e0a0afae6a43ffc73f4d
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 CString macro_equal_buf;
47 static TokenString tokstr_buf;
48 static unsigned char isidnum_table[256 - CH_EOF];
49 static int pp_debug_tok, pp_debug_symv;
50 static int pp_once;
51 static int pp_expr;
52 static int pp_counter;
53 static void tok_print(const char *msg, const int *str);
55 static struct TinyAlloc *toksym_alloc;
56 static struct TinyAlloc *tokstr_alloc;
57 static struct TinyAlloc *cstr_alloc;
59 static TokenString *macro_stack;
61 static const char tcc_keywords[] =
62 #define DEF(id, str) str "\0"
63 #include "tcctok.h"
64 #undef DEF
67 /* WARNING: the content of this string encodes token numbers */
68 static const unsigned char tok_two_chars[] =
69 /* outdated -- gr
70 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
71 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
72 */{
73 '<','=', TOK_LE,
74 '>','=', TOK_GE,
75 '!','=', TOK_NE,
76 '&','&', TOK_LAND,
77 '|','|', TOK_LOR,
78 '+','+', TOK_INC,
79 '-','-', TOK_DEC,
80 '=','=', TOK_EQ,
81 '<','<', TOK_SHL,
82 '>','>', TOK_SAR,
83 '+','=', TOK_A_ADD,
84 '-','=', TOK_A_SUB,
85 '*','=', TOK_A_MUL,
86 '/','=', TOK_A_DIV,
87 '%','=', TOK_A_MOD,
88 '&','=', TOK_A_AND,
89 '^','=', TOK_A_XOR,
90 '|','=', TOK_A_OR,
91 '-','>', TOK_ARROW,
92 '.','.', 0xa8, // C++ token ?
93 '#','#', TOK_TWOSHARPS,
97 static void next_nomacro_spc(void);
99 ST_FUNC void skip(int c)
101 if (tok != c)
102 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
103 next();
106 ST_FUNC void expect(const char *msg)
108 tcc_error("%s expected", msg);
111 /* ------------------------------------------------------------------------- */
112 /* Custom allocator for tiny objects */
114 #define USE_TAL
116 #ifndef USE_TAL
117 #define tal_free(al, p) tcc_free(p)
118 #define tal_realloc(al, p, size) tcc_realloc(p, size)
119 #define tal_new(a,b,c)
120 #define tal_delete(a)
121 #else
122 #if !defined(MEM_DEBUG)
123 #define tal_free(al, p) tal_free_impl(al, p)
124 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
125 #define TAL_DEBUG_PARAMS
126 #else
127 #define TAL_DEBUG 1
128 //#define TAL_INFO 1 /* collect and dump allocators stats */
129 #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
130 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
131 #define TAL_DEBUG_PARAMS , const char *file, int line
132 #define TAL_DEBUG_FILE_LEN 40
133 #endif
135 #define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */
136 #define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */
137 #define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */
138 #define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */
139 #define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */
140 #define CSTR_TAL_LIMIT 1024
142 typedef struct TinyAlloc {
143 unsigned limit;
144 unsigned size;
145 uint8_t *buffer;
146 uint8_t *p;
147 unsigned nb_allocs;
148 struct TinyAlloc *next, *top;
149 #ifdef TAL_INFO
150 unsigned nb_peak;
151 unsigned nb_total;
152 unsigned nb_missed;
153 uint8_t *peak_p;
154 #endif
155 } TinyAlloc;
157 typedef struct tal_header_t {
158 unsigned size;
159 #ifdef TAL_DEBUG
160 int line_num; /* negative line_num used for double free check */
161 char file_name[TAL_DEBUG_FILE_LEN + 1];
162 #endif
163 } tal_header_t;
165 /* ------------------------------------------------------------------------- */
167 static TinyAlloc *tal_new(TinyAlloc **pal, unsigned limit, unsigned size)
169 TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
170 al->p = al->buffer = tcc_malloc(size);
171 al->limit = limit;
172 al->size = size;
173 if (pal) *pal = al;
174 return al;
177 static void tal_delete(TinyAlloc *al)
179 TinyAlloc *next;
181 tail_call:
182 if (!al)
183 return;
184 #ifdef TAL_INFO
185 fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
186 al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
187 (al->peak_p - al->buffer) * 100.0 / al->size);
188 #endif
189 #ifdef TAL_DEBUG
190 if (al->nb_allocs > 0) {
191 uint8_t *p;
192 fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",
193 al->nb_allocs, al->limit);
194 p = al->buffer;
195 while (p < al->p) {
196 tal_header_t *header = (tal_header_t *)p;
197 if (header->line_num > 0) {
198 fprintf(stderr, "%s:%d: chunk of %d bytes leaked\n",
199 header->file_name, header->line_num, header->size);
201 p += header->size + sizeof(tal_header_t);
203 #if MEM_DEBUG-0 == 2
204 exit(2);
205 #endif
207 #endif
208 next = al->next;
209 tcc_free(al->buffer);
210 tcc_free(al);
211 al = next;
212 goto tail_call;
215 static void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
217 if (!p)
218 return;
219 tail_call:
220 if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
221 #ifdef TAL_DEBUG
222 tal_header_t *header = (((tal_header_t *)p) - 1);
223 if (header->line_num < 0) {
224 fprintf(stderr, "%s:%d: TAL_DEBUG: double frees chunk from\n",
225 file, line);
226 fprintf(stderr, "%s:%d: %d bytes\n",
227 header->file_name, (int)-header->line_num, (int)header->size);
228 } else
229 header->line_num = -header->line_num;
230 #endif
231 al->nb_allocs--;
232 if (!al->nb_allocs)
233 al->p = al->buffer;
234 } else if (al->next) {
235 al = al->next;
236 goto tail_call;
238 else
239 tcc_free(p);
242 static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_PARAMS)
244 tal_header_t *header;
245 void *ret;
246 int is_own;
247 unsigned adj_size = (size + 3) & -4;
248 TinyAlloc *al = *pal;
250 tail_call:
251 is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
252 if ((!p || is_own) && size <= al->limit) {
253 if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) {
254 header = (tal_header_t *)al->p;
255 header->size = adj_size;
256 #ifdef TAL_DEBUG
257 { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
258 strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
259 header->file_name[TAL_DEBUG_FILE_LEN] = 0;
260 header->line_num = line; }
261 #endif
262 ret = al->p + sizeof(tal_header_t);
263 al->p += adj_size + sizeof(tal_header_t);
264 if (is_own) {
265 header = (((tal_header_t *)p) - 1);
266 memcpy(ret, p, header->size);
267 #ifdef TAL_DEBUG
268 header->line_num = -header->line_num;
269 #endif
270 } else {
271 al->nb_allocs++;
273 #ifdef TAL_INFO
274 if (al->nb_peak < al->nb_allocs)
275 al->nb_peak = al->nb_allocs;
276 if (al->peak_p < al->p)
277 al->peak_p = al->p;
278 al->nb_total++;
279 #endif
280 return ret;
281 } else if (is_own) {
282 al->nb_allocs--;
283 ret = tal_realloc(*pal, 0, size);
284 header = (((tal_header_t *)p) - 1);
285 memcpy(ret, p, header->size);
286 #ifdef TAL_DEBUG
287 header->line_num = -header->line_num;
288 #endif
289 return ret;
291 if (al->next) {
292 al = al->next;
293 } else {
294 TinyAlloc *bottom = al, *next = al->top ? al->top : al;
296 al = tal_new(pal, next->limit, next->size * 2);
297 al->next = next;
298 bottom->top = al;
300 goto tail_call;
302 if (is_own) {
303 al->nb_allocs--;
304 ret = tcc_malloc(size);
305 header = (((tal_header_t *)p) - 1);
306 memcpy(ret, p, header->size);
307 #ifdef TAL_DEBUG
308 header->line_num = -header->line_num;
309 #endif
310 } else if (al->next) {
311 al = al->next;
312 goto tail_call;
313 } else
314 ret = tcc_realloc(p, size);
315 #ifdef TAL_INFO
316 al->nb_missed++;
317 #endif
318 return ret;
321 #endif /* USE_TAL */
323 /* ------------------------------------------------------------------------- */
324 /* CString handling */
325 static void cstr_realloc(CString *cstr, int new_size)
327 int size;
329 size = cstr->size_allocated;
330 if (size < 8)
331 size = 8; /* no need to allocate a too small first string */
332 while (size < new_size)
333 size = size * 2;
334 cstr->data = tal_realloc(cstr_alloc, cstr->data, size);
335 cstr->size_allocated = size;
338 /* add a byte */
339 ST_INLN void cstr_ccat(CString *cstr, int ch)
341 int size;
342 size = cstr->size + 1;
343 if (size > cstr->size_allocated)
344 cstr_realloc(cstr, size);
345 ((unsigned char *)cstr->data)[size - 1] = ch;
346 cstr->size = size;
349 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len)
351 int size;
352 if (len <= 0)
353 len = strlen(str) + 1 + len;
354 size = cstr->size + len;
355 if (size > cstr->size_allocated)
356 cstr_realloc(cstr, size);
357 memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
358 cstr->size = size;
361 /* add a wide char */
362 ST_FUNC void cstr_wccat(CString *cstr, int ch)
364 int size;
365 size = cstr->size + sizeof(nwchar_t);
366 if (size > cstr->size_allocated)
367 cstr_realloc(cstr, size);
368 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
369 cstr->size = size;
372 ST_FUNC void cstr_new(CString *cstr)
374 memset(cstr, 0, sizeof(CString));
377 /* free string and reset it to NULL */
378 ST_FUNC void cstr_free(CString *cstr)
380 tal_free(cstr_alloc, cstr->data);
381 cstr_new(cstr);
384 /* reset string to empty */
385 ST_FUNC void cstr_reset(CString *cstr)
387 cstr->size = 0;
390 /* XXX: unicode ? */
391 static void add_char(CString *cstr, int c)
393 if (c == '\'' || c == '\"' || c == '\\') {
394 /* XXX: could be more precise if char or string */
395 cstr_ccat(cstr, '\\');
397 if (c >= 32 && c <= 126) {
398 cstr_ccat(cstr, c);
399 } else {
400 cstr_ccat(cstr, '\\');
401 if (c == '\n') {
402 cstr_ccat(cstr, 'n');
403 } else {
404 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
405 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
406 cstr_ccat(cstr, '0' + (c & 7));
411 /* ------------------------------------------------------------------------- */
412 /* allocate a new token */
413 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
415 TokenSym *ts, **ptable;
416 int i;
418 if (tok_ident >= SYM_FIRST_ANOM)
419 tcc_error("memory full (symbols)");
421 /* expand token table if needed */
422 i = tok_ident - TOK_IDENT;
423 if ((i % TOK_ALLOC_INCR) == 0) {
424 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
425 table_ident = ptable;
428 ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len);
429 table_ident[i] = ts;
430 ts->tok = tok_ident++;
431 ts->sym_define = NULL;
432 ts->sym_label = NULL;
433 ts->sym_struct = NULL;
434 ts->sym_identifier = NULL;
435 ts->len = len;
436 ts->hash_next = NULL;
437 memcpy(ts->str, str, len);
438 ts->str[len] = '\0';
439 *pts = ts;
440 return ts;
443 #define TOK_HASH_INIT 1
444 #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
447 /* find a token and add it if not found */
448 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
450 TokenSym *ts, **pts;
451 int i;
452 unsigned int h;
454 h = TOK_HASH_INIT;
455 for(i=0;i<len;i++)
456 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
457 h &= (TOK_HASH_SIZE - 1);
459 pts = &hash_ident[h];
460 for(;;) {
461 ts = *pts;
462 if (!ts)
463 break;
464 if (ts->len == len && !memcmp(ts->str, str, len))
465 return ts;
466 pts = &(ts->hash_next);
468 return tok_alloc_new(pts, str, len);
471 /* XXX: buffer overflow */
472 /* XXX: float tokens */
473 ST_FUNC const char *get_tok_str(int v, CValue *cv)
475 char *p;
476 int i, len;
478 cstr_reset(&cstr_buf);
479 p = cstr_buf.data;
481 switch(v) {
482 case TOK_CINT:
483 case TOK_CUINT:
484 case TOK_CLONG:
485 case TOK_CULONG:
486 case TOK_CLLONG:
487 case TOK_CULLONG:
488 /* XXX: not quite exact, but only useful for testing */
489 #ifdef _WIN32
490 sprintf(p, "%u", (unsigned)cv->i);
491 #else
492 sprintf(p, "%llu", (unsigned long long)cv->i);
493 #endif
494 break;
495 case TOK_LCHAR:
496 cstr_ccat(&cstr_buf, 'L');
497 case TOK_CCHAR:
498 cstr_ccat(&cstr_buf, '\'');
499 add_char(&cstr_buf, cv->i);
500 cstr_ccat(&cstr_buf, '\'');
501 cstr_ccat(&cstr_buf, '\0');
502 break;
503 case TOK_PPNUM:
504 case TOK_PPSTR:
505 return (char*)cv->str.data;
506 case TOK_LSTR:
507 cstr_ccat(&cstr_buf, 'L');
508 case TOK_STR:
509 cstr_ccat(&cstr_buf, '\"');
510 if (v == TOK_STR) {
511 len = cv->str.size - 1;
512 for(i=0;i<len;i++)
513 add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
514 } else {
515 len = (cv->str.size / sizeof(nwchar_t)) - 1;
516 for(i=0;i<len;i++)
517 add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
519 cstr_ccat(&cstr_buf, '\"');
520 cstr_ccat(&cstr_buf, '\0');
521 break;
523 case TOK_CFLOAT:
524 cstr_cat(&cstr_buf, "<float>", 0);
525 break;
526 case TOK_CDOUBLE:
527 cstr_cat(&cstr_buf, "<double>", 0);
528 break;
529 case TOK_CLDOUBLE:
530 cstr_cat(&cstr_buf, "<long double>", 0);
531 break;
532 case TOK_LINENUM:
533 cstr_cat(&cstr_buf, "<linenumber>", 0);
534 break;
536 /* above tokens have value, the ones below don't */
537 case TOK_LT:
538 v = '<';
539 goto addv;
540 case TOK_GT:
541 v = '>';
542 goto addv;
543 case TOK_DOTS:
544 return strcpy(p, "...");
545 case TOK_A_SHL:
546 return strcpy(p, "<<=");
547 case TOK_A_SAR:
548 return strcpy(p, ">>=");
549 case TOK_EOF:
550 return strcpy(p, "<eof>");
551 default:
552 if (v < TOK_IDENT) {
553 /* search in two bytes table */
554 const unsigned char *q = tok_two_chars;
555 while (*q) {
556 if (q[2] == v) {
557 *p++ = q[0];
558 *p++ = q[1];
559 *p = '\0';
560 return cstr_buf.data;
562 q += 3;
564 if (v >= 127) {
565 sprintf(cstr_buf.data, "<%02x>", v);
566 return cstr_buf.data;
568 addv:
569 *p++ = v;
570 *p = '\0';
571 } else if (v < tok_ident) {
572 return table_ident[v - TOK_IDENT]->str;
573 } else if (v >= SYM_FIRST_ANOM) {
574 /* special name for anonymous symbol */
575 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
576 } else {
577 /* should never happen */
578 return NULL;
580 break;
582 return cstr_buf.data;
585 /* return the current character, handling end of block if necessary
586 (but not stray) */
587 ST_FUNC int handle_eob(void)
589 BufferedFile *bf = file;
590 int len;
592 /* only tries to read if really end of buffer */
593 if (bf->buf_ptr >= bf->buf_end) {
594 if (bf->fd != -1) {
595 #if defined(PARSE_DEBUG)
596 len = 1;
597 #else
598 len = IO_BUF_SIZE;
599 #endif
600 len = read(bf->fd, bf->buffer, len);
601 if (len < 0)
602 len = 0;
603 } else {
604 len = 0;
606 total_bytes += len;
607 bf->buf_ptr = bf->buffer;
608 bf->buf_end = bf->buffer + len;
609 *bf->buf_end = CH_EOB;
611 if (bf->buf_ptr < bf->buf_end) {
612 return bf->buf_ptr[0];
613 } else {
614 bf->buf_ptr = bf->buf_end;
615 return CH_EOF;
619 /* read next char from current input file and handle end of input buffer */
620 ST_INLN void inp(void)
622 ch = *(++(file->buf_ptr));
623 /* end of buffer/file handling */
624 if (ch == CH_EOB)
625 ch = handle_eob();
628 /* handle '\[\r]\n' */
629 static int handle_stray_noerror(void)
631 while (ch == '\\') {
632 inp();
633 if (ch == '\n') {
634 file->line_num++;
635 inp();
636 } else if (ch == '\r') {
637 inp();
638 if (ch != '\n')
639 goto fail;
640 file->line_num++;
641 inp();
642 } else {
643 fail:
644 return 1;
647 return 0;
650 static void handle_stray(void)
652 if (handle_stray_noerror())
653 tcc_error("stray '\\' in program");
656 /* skip the stray and handle the \\n case. Output an error if
657 incorrect char after the stray */
658 static int handle_stray1(uint8_t *p)
660 int c;
662 file->buf_ptr = p;
663 if (p >= file->buf_end) {
664 c = handle_eob();
665 if (c != '\\')
666 return c;
667 p = file->buf_ptr;
669 ch = *p;
670 if (handle_stray_noerror()) {
671 if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
672 tcc_error("stray '\\' in program");
673 *--file->buf_ptr = '\\';
675 p = file->buf_ptr;
676 c = *p;
677 return c;
680 /* handle just the EOB case, but not stray */
681 #define PEEKC_EOB(c, p)\
683 p++;\
684 c = *p;\
685 if (c == '\\') {\
686 file->buf_ptr = p;\
687 c = handle_eob();\
688 p = file->buf_ptr;\
692 /* handle the complicated stray case */
693 #define PEEKC(c, p)\
695 p++;\
696 c = *p;\
697 if (c == '\\') {\
698 c = handle_stray1(p);\
699 p = file->buf_ptr;\
703 /* input with '\[\r]\n' handling. Note that this function cannot
704 handle other characters after '\', so you cannot call it inside
705 strings or comments */
706 ST_FUNC void minp(void)
708 inp();
709 if (ch == '\\')
710 handle_stray();
713 /* single line C++ comments */
714 static uint8_t *parse_line_comment(uint8_t *p)
716 int c;
718 p++;
719 for(;;) {
720 c = *p;
721 redo:
722 if (c == '\n' || c == CH_EOF) {
723 break;
724 } else if (c == '\\') {
725 file->buf_ptr = p;
726 c = handle_eob();
727 p = file->buf_ptr;
728 if (c == '\\') {
729 PEEKC_EOB(c, p);
730 if (c == '\n') {
731 file->line_num++;
732 PEEKC_EOB(c, p);
733 } else if (c == '\r') {
734 PEEKC_EOB(c, p);
735 if (c == '\n') {
736 file->line_num++;
737 PEEKC_EOB(c, p);
740 } else {
741 goto redo;
743 } else {
744 p++;
747 return p;
750 /* C comments */
751 ST_FUNC uint8_t *parse_comment(uint8_t *p)
753 int c;
755 p++;
756 for(;;) {
757 /* fast skip loop */
758 for(;;) {
759 c = *p;
760 if (c == '\n' || c == '*' || c == '\\')
761 break;
762 p++;
763 c = *p;
764 if (c == '\n' || c == '*' || c == '\\')
765 break;
766 p++;
768 /* now we can handle all the cases */
769 if (c == '\n') {
770 file->line_num++;
771 p++;
772 } else if (c == '*') {
773 p++;
774 for(;;) {
775 c = *p;
776 if (c == '*') {
777 p++;
778 } else if (c == '/') {
779 goto end_of_comment;
780 } else if (c == '\\') {
781 file->buf_ptr = p;
782 c = handle_eob();
783 p = file->buf_ptr;
784 if (c == CH_EOF)
785 tcc_error("unexpected end of file in comment");
786 if (c == '\\') {
787 /* skip '\[\r]\n', otherwise just skip the stray */
788 while (c == '\\') {
789 PEEKC_EOB(c, p);
790 if (c == '\n') {
791 file->line_num++;
792 PEEKC_EOB(c, p);
793 } else if (c == '\r') {
794 PEEKC_EOB(c, p);
795 if (c == '\n') {
796 file->line_num++;
797 PEEKC_EOB(c, p);
799 } else {
800 goto after_star;
804 } else {
805 break;
808 after_star: ;
809 } else {
810 /* stray, eob or eof */
811 file->buf_ptr = p;
812 c = handle_eob();
813 p = file->buf_ptr;
814 if (c == CH_EOF) {
815 tcc_error("unexpected end of file in comment");
816 } else if (c == '\\') {
817 p++;
821 end_of_comment:
822 p++;
823 return p;
826 ST_FUNC int set_idnum(int c, int val)
828 int prev = isidnum_table[c - CH_EOF];
829 isidnum_table[c - CH_EOF] = val;
830 return prev;
833 #define cinp minp
835 static inline void skip_spaces(void)
837 while (isidnum_table[ch - CH_EOF] & IS_SPC)
838 cinp();
841 static inline int check_space(int t, int *spc)
843 if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
844 if (*spc)
845 return 1;
846 *spc = 1;
847 } else
848 *spc = 0;
849 return 0;
852 /* parse a string without interpreting escapes */
853 static uint8_t *parse_pp_string(uint8_t *p,
854 int sep, CString *str)
856 int c;
857 p++;
858 for(;;) {
859 c = *p;
860 if (c == sep) {
861 break;
862 } else if (c == '\\') {
863 file->buf_ptr = p;
864 c = handle_eob();
865 p = file->buf_ptr;
866 if (c == CH_EOF) {
867 unterminated_string:
868 /* XXX: indicate line number of start of string */
869 tcc_error("missing terminating %c character", sep);
870 } else if (c == '\\') {
871 /* escape : just skip \[\r]\n */
872 PEEKC_EOB(c, p);
873 if (c == '\n') {
874 file->line_num++;
875 p++;
876 } else if (c == '\r') {
877 PEEKC_EOB(c, p);
878 if (c != '\n')
879 expect("'\n' after '\r'");
880 file->line_num++;
881 p++;
882 } else if (c == CH_EOF) {
883 goto unterminated_string;
884 } else {
885 if (str) {
886 cstr_ccat(str, '\\');
887 cstr_ccat(str, c);
889 p++;
892 } else if (c == '\n') {
893 file->line_num++;
894 goto add_char;
895 } else if (c == '\r') {
896 PEEKC_EOB(c, p);
897 if (c != '\n') {
898 if (str)
899 cstr_ccat(str, '\r');
900 } else {
901 file->line_num++;
902 goto add_char;
904 } else {
905 add_char:
906 if (str)
907 cstr_ccat(str, c);
908 p++;
911 p++;
912 return p;
915 /* skip block of text until #else, #elif or #endif. skip also pairs of
916 #if/#endif */
917 static void preprocess_skip(void)
919 int a, start_of_line, c, in_warn_or_error;
920 uint8_t *p;
922 p = file->buf_ptr;
923 a = 0;
924 redo_start:
925 start_of_line = 1;
926 in_warn_or_error = 0;
927 for(;;) {
928 redo_no_start:
929 c = *p;
930 switch(c) {
931 case ' ':
932 case '\t':
933 case '\f':
934 case '\v':
935 case '\r':
936 p++;
937 goto redo_no_start;
938 case '\n':
939 file->line_num++;
940 p++;
941 goto redo_start;
942 case '\\':
943 file->buf_ptr = p;
944 c = handle_eob();
945 if (c == CH_EOF) {
946 expect("#endif");
947 } else if (c == '\\') {
948 ch = file->buf_ptr[0];
949 handle_stray_noerror();
951 p = file->buf_ptr;
952 goto redo_no_start;
953 /* skip strings */
954 case '\"':
955 case '\'':
956 if (in_warn_or_error)
957 goto _default;
958 p = parse_pp_string(p, c, NULL);
959 break;
960 /* skip comments */
961 case '/':
962 if (in_warn_or_error)
963 goto _default;
964 file->buf_ptr = p;
965 ch = *p;
966 minp();
967 p = file->buf_ptr;
968 if (ch == '*') {
969 p = parse_comment(p);
970 } else if (ch == '/') {
971 p = parse_line_comment(p);
973 break;
974 case '#':
975 p++;
976 if (start_of_line) {
977 file->buf_ptr = p;
978 next_nomacro();
979 p = file->buf_ptr;
980 if (a == 0 &&
981 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
982 goto the_end;
983 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
984 a++;
985 else if (tok == TOK_ENDIF)
986 a--;
987 else if( tok == TOK_ERROR || tok == TOK_WARNING)
988 in_warn_or_error = 1;
989 else if (tok == TOK_LINEFEED)
990 goto redo_start;
991 else if (parse_flags & PARSE_FLAG_ASM_FILE)
992 p = parse_line_comment(p - 1);
993 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
994 p = parse_line_comment(p - 1);
995 break;
996 _default:
997 default:
998 p++;
999 break;
1001 start_of_line = 0;
1003 the_end: ;
1004 file->buf_ptr = p;
1007 #if 0
1008 /* return the number of additional 'ints' necessary to store the
1009 token */
1010 static inline int tok_size(const int *p)
1012 switch(*p) {
1013 /* 4 bytes */
1014 case TOK_CINT:
1015 case TOK_CUINT:
1016 case TOK_CCHAR:
1017 case TOK_LCHAR:
1018 case TOK_CFLOAT:
1019 case TOK_LINENUM:
1020 #ifndef TCC_LONG_ARE_64_BIT
1021 case TOK_CLONG;
1022 case TOK_CULONG;
1023 #endif
1024 return 1 + 1;
1025 case TOK_STR:
1026 case TOK_LSTR:
1027 case TOK_PPNUM:
1028 case TOK_PPSTR:
1029 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
1030 case TOK_CDOUBLE:
1031 case TOK_CLLONG:
1032 case TOK_CULLONG:
1033 #ifdef TCC_LONG_ARE_64_BIT
1034 case TOK_CLONG;
1035 case TOK_CULONG;
1036 #endif
1037 return 1 + 2;
1038 case TOK_CLDOUBLE:
1039 return 1 + LDOUBLE_SIZE / 4;
1040 default:
1041 return 1 + 0;
1044 #endif
1046 /* token string handling */
1047 ST_INLN void tok_str_new(TokenString *s)
1049 s->str = NULL;
1050 s->len = s->lastlen = 0;
1051 s->allocated_len = 0;
1052 s->last_line_num = -1;
1055 ST_FUNC TokenString *tok_str_alloc(void)
1057 TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
1058 tok_str_new(str);
1059 return str;
1062 ST_FUNC int *tok_str_dup(TokenString *s)
1064 int *str;
1066 str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
1067 memcpy(str, s->str, s->len * sizeof(int));
1068 return str;
1071 ST_FUNC void tok_str_free_str(int *str)
1073 tal_free(tokstr_alloc, str);
1076 ST_FUNC void tok_str_free(TokenString *str)
1078 tok_str_free_str(str->str);
1079 tal_free(tokstr_alloc, str);
1082 ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
1084 int *str, size;
1086 size = s->allocated_len;
1087 if (size < 16)
1088 size = 16;
1089 while (size < new_size)
1090 size = size * 2;
1091 if (size > s->allocated_len) {
1092 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
1093 s->allocated_len = size;
1094 s->str = str;
1096 return s->str;
1099 ST_FUNC void tok_str_add(TokenString *s, int t)
1101 int len, *str;
1103 len = s->len;
1104 str = s->str;
1105 if (len >= s->allocated_len)
1106 str = tok_str_realloc(s, len + 1);
1107 str[len++] = t;
1108 s->len = len;
1111 ST_FUNC void begin_macro(TokenString *str, int alloc)
1113 str->alloc = alloc;
1114 str->prev = macro_stack;
1115 str->prev_ptr = macro_ptr;
1116 str->save_line_num = file->line_num;
1117 macro_ptr = str->str;
1118 macro_stack = str;
1121 ST_FUNC void end_macro(void)
1123 TokenString *str = macro_stack;
1124 macro_stack = str->prev;
1125 macro_ptr = str->prev_ptr;
1126 file->line_num = str->save_line_num;
1127 if (str->alloc == 2) {
1128 str->alloc = 3; /* just mark as finished */
1129 } else {
1130 tok_str_free(str);
1134 static void tok_str_add2(TokenString *s, int t, CValue *cv)
1136 int len, *str;
1138 len = s->lastlen = s->len;
1139 str = s->str;
1141 /* allocate space for worst case */
1142 if (len + TOK_MAX_SIZE >= s->allocated_len)
1143 str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
1144 str[len++] = t;
1145 switch(t) {
1146 case TOK_CINT:
1147 case TOK_CUINT:
1148 case TOK_CCHAR:
1149 case TOK_LCHAR:
1150 case TOK_CFLOAT:
1151 case TOK_LINENUM:
1152 #ifndef TCC_LONG_ARE_64_BIT
1153 case TOK_CLONG:
1154 case TOK_CULONG:
1155 #endif
1156 str[len++] = cv->tab[0];
1157 break;
1158 case TOK_PPNUM:
1159 case TOK_PPSTR:
1160 case TOK_STR:
1161 case TOK_LSTR:
1163 /* Insert the string into the int array. */
1164 size_t nb_words =
1165 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
1166 if (len + nb_words >= s->allocated_len)
1167 str = tok_str_realloc(s, len + nb_words + 1);
1168 str[len] = cv->str.size;
1169 memcpy(&str[len + 1], cv->str.data, cv->str.size);
1170 len += nb_words;
1172 break;
1173 case TOK_CDOUBLE:
1174 case TOK_CLLONG:
1175 case TOK_CULLONG:
1176 #ifdef TCC_LONG_ARE_64_BIT
1177 case TOK_CLONG:
1178 case TOK_CULONG:
1179 #endif
1180 #if LDOUBLE_SIZE == 8
1181 case TOK_CLDOUBLE:
1182 #endif
1183 str[len++] = cv->tab[0];
1184 str[len++] = cv->tab[1];
1185 break;
1186 #if LDOUBLE_SIZE == 12
1187 case TOK_CLDOUBLE:
1188 str[len++] = cv->tab[0];
1189 str[len++] = cv->tab[1];
1190 str[len++] = cv->tab[2];
1191 #elif LDOUBLE_SIZE == 16
1192 case TOK_CLDOUBLE:
1193 str[len++] = cv->tab[0];
1194 str[len++] = cv->tab[1];
1195 str[len++] = cv->tab[2];
1196 str[len++] = cv->tab[3];
1197 #elif LDOUBLE_SIZE != 8
1198 #error add long double size support
1199 #endif
1200 break;
1201 default:
1202 break;
1204 s->len = len;
1207 /* add the current parse token in token string 's' */
1208 ST_FUNC void tok_str_add_tok(TokenString *s)
1210 CValue cval;
1212 /* save line number info */
1213 if (file->line_num != s->last_line_num) {
1214 s->last_line_num = file->line_num;
1215 cval.i = s->last_line_num;
1216 tok_str_add2(s, TOK_LINENUM, &cval);
1218 tok_str_add2(s, tok, &tokc);
1221 /* get a token from an integer array and increment pointer
1222 accordingly. we code it as a macro to avoid pointer aliasing. */
1223 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
1225 const int *p = *pp;
1226 int n, *tab;
1228 tab = cv->tab;
1229 switch(*t = *p++) {
1230 case TOK_CINT:
1231 case TOK_CUINT:
1232 case TOK_CCHAR:
1233 case TOK_LCHAR:
1234 case TOK_LINENUM:
1235 #ifndef TCC_LONG_ARE_64_BIT
1236 case TOK_CLONG:
1237 case TOK_CULONG:
1238 #endif
1239 tab[0] = *p++;
1240 cv->i = (*t == TOK_CUINT) ? (unsigned)cv->i : (int)cv->i;
1241 break;
1242 case TOK_CFLOAT:
1243 tab[0] = *p++;
1244 break;
1245 case TOK_STR:
1246 case TOK_LSTR:
1247 case TOK_PPNUM:
1248 case TOK_PPSTR:
1249 cv->str.size = *p++;
1250 cv->str.data = p;
1251 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1252 break;
1253 case TOK_CDOUBLE:
1254 case TOK_CLLONG:
1255 case TOK_CULLONG:
1256 #ifdef TCC_LONG_ARE_64_BIT
1257 case TOK_CLONG:
1258 case TOK_CULONG:
1259 #endif
1260 n = 2;
1261 goto copy;
1262 case TOK_CLDOUBLE:
1263 #if LDOUBLE_SIZE == 16
1264 n = 4;
1265 #elif LDOUBLE_SIZE == 12
1266 n = 3;
1267 #elif LDOUBLE_SIZE == 8
1268 n = 2;
1269 #else
1270 # error add long double size support
1271 #endif
1272 copy:
1274 *tab++ = *p++;
1275 while (--n);
1276 break;
1277 default:
1278 break;
1280 *pp = p;
1283 static int macro_is_equal(const int *a, const int *b)
1285 CValue cv;
1286 int t;
1288 if (!a || !b)
1289 return 1;
1291 while (*a && *b) {
1292 /* first time preallocate macro_equal_buf, next time only reset position to start */
1293 cstr_reset(&macro_equal_buf);
1294 TOK_GET(&t, &a, &cv);
1295 cstr_cat(&macro_equal_buf, get_tok_str(t, &cv), 0);
1296 TOK_GET(&t, &b, &cv);
1297 if (strcmp(macro_equal_buf.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 if they were
1346 #undef'd in the file */
1347 while (b) {
1348 int v = b->v;
1349 if (v >= TOK_IDENT && v < tok_ident) {
1350 Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
1351 if (!*d)
1352 *d = b;
1354 b = b->prev;
1358 /* label lookup */
1359 ST_FUNC Sym *label_find(int v)
1361 v -= TOK_IDENT;
1362 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1363 return NULL;
1364 return table_ident[v]->sym_label;
1367 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1369 Sym *s, **ps;
1370 s = sym_push2(ptop, v, 0, 0);
1371 s->r = flags;
1372 ps = &table_ident[v - TOK_IDENT]->sym_label;
1373 if (ptop == &global_label_stack) {
1374 /* modify the top most local identifier, so that
1375 sym_identifier will point to 's' when popped */
1376 while (*ps != NULL)
1377 ps = &(*ps)->prev_tok;
1379 s->prev_tok = *ps;
1380 *ps = s;
1381 return s;
1384 /* pop labels until element last is reached. Look if any labels are
1385 undefined. Define symbols if '&&label' was used. */
1386 ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep)
1388 Sym *s, *s1;
1389 for(s = *ptop; s != slast; s = s1) {
1390 s1 = s->prev;
1391 if (s->r == LABEL_DECLARED) {
1392 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1393 } else if (s->r == LABEL_FORWARD) {
1394 tcc_error("label '%s' used but not defined",
1395 get_tok_str(s->v, NULL));
1396 } else {
1397 if (s->c) {
1398 /* define corresponding symbol. A size of
1399 1 is put. */
1400 put_extern_sym(s, cur_text_section, s->jnext, 1);
1403 /* remove label */
1404 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1405 if (!keep)
1406 sym_free(s);
1408 if (!keep)
1409 *ptop = slast;
1412 /* fake the nth "#if defined test_..." for tcc -dt -run */
1413 static void maybe_run_test(TCCState *s)
1415 const char *p;
1416 if (s->include_stack_ptr != s->include_stack)
1417 return;
1418 p = get_tok_str(tok, NULL);
1419 if (0 != memcmp(p, "test_", 5))
1420 return;
1421 if (0 != --s->run_test)
1422 return;
1423 fprintf(s->ppfp, "\n[%s]\n" + !(s->dflag & 32), p), fflush(s->ppfp);
1424 define_push(tok, MACRO_OBJ, NULL, NULL);
1427 /* eval an expression for #if/#elif */
1428 static int expr_preprocess(void)
1430 int c, t;
1431 TokenString *str;
1433 str = tok_str_alloc();
1434 pp_expr = 1;
1435 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1436 next(); /* do macro subst */
1437 if (tok == TOK_DEFINED) {
1438 next_nomacro();
1439 t = tok;
1440 if (t == '(')
1441 next_nomacro();
1442 if (tok < TOK_IDENT)
1443 expect("identifier");
1444 if (tcc_state->run_test)
1445 maybe_run_test(tcc_state);
1446 c = define_find(tok) != 0;
1447 if (t == '(') {
1448 next_nomacro();
1449 if (tok != ')')
1450 expect("')'");
1452 tok = TOK_CINT;
1453 tokc.i = c;
1454 } else if (tok >= TOK_IDENT) {
1455 /* if undefined macro */
1456 tok = TOK_CINT;
1457 tokc.i = 0;
1459 tok_str_add_tok(str);
1461 pp_expr = 0;
1462 tok_str_add(str, -1); /* simulate end of file */
1463 tok_str_add(str, 0);
1464 /* now evaluate C constant expression */
1465 begin_macro(str, 1);
1466 next();
1467 c = expr_const();
1468 end_macro();
1469 return c != 0;
1473 /* parse after #define */
1474 ST_FUNC void parse_define(void)
1476 Sym *s, *first, **ps;
1477 int v, t, varg, is_vaargs, spc;
1478 int saved_parse_flags = parse_flags;
1480 v = tok;
1481 if (v < TOK_IDENT || v == TOK_DEFINED)
1482 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1483 /* XXX: should check if same macro (ANSI) */
1484 first = NULL;
1485 t = MACRO_OBJ;
1486 /* We have to parse the whole define as if not in asm mode, in particular
1487 no line comment with '#' must be ignored. Also for function
1488 macros the argument list must be parsed without '.' being an ID
1489 character. */
1490 parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES);
1491 /* '(' must be just after macro definition for MACRO_FUNC */
1492 next_nomacro_spc();
1493 if (tok == '(') {
1494 int dotid = set_idnum('.', 0);
1495 next_nomacro();
1496 ps = &first;
1497 if (tok != ')') for (;;) {
1498 varg = tok;
1499 next_nomacro();
1500 is_vaargs = 0;
1501 if (varg == TOK_DOTS) {
1502 varg = TOK___VA_ARGS__;
1503 is_vaargs = 1;
1504 } else if (tok == TOK_DOTS && gnu_ext) {
1505 is_vaargs = 1;
1506 next_nomacro();
1508 if (varg < TOK_IDENT)
1509 bad_list:
1510 tcc_error("bad macro parameter list");
1511 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1512 *ps = s;
1513 ps = &s->next;
1514 if (tok == ')')
1515 break;
1516 if (tok != ',' || is_vaargs)
1517 goto bad_list;
1518 next_nomacro();
1520 next_nomacro_spc();
1521 t = MACRO_FUNC;
1522 set_idnum('.', dotid);
1525 tokstr_buf.len = 0;
1526 spc = 2;
1527 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1528 /* The body of a macro definition should be parsed such that identifiers
1529 are parsed like the file mode determines (i.e. with '.' being an
1530 ID character in asm mode). But '#' should be retained instead of
1531 regarded as line comment leader, so still don't set ASM_FILE
1532 in parse_flags. */
1533 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1534 /* remove spaces around ## and after '#' */
1535 if (TOK_TWOSHARPS == tok) {
1536 if (2 == spc)
1537 goto bad_twosharp;
1538 if (1 == spc)
1539 --tokstr_buf.len;
1540 spc = 3;
1541 tok = TOK_PPJOIN;
1542 } else if ('#' == tok) {
1543 spc = 4;
1544 } else if (check_space(tok, &spc)) {
1545 goto skip;
1547 tok_str_add2(&tokstr_buf, tok, &tokc);
1548 skip:
1549 next_nomacro_spc();
1552 parse_flags = saved_parse_flags;
1553 if (spc == 1)
1554 --tokstr_buf.len; /* remove trailing space */
1555 tok_str_add(&tokstr_buf, 0);
1556 if (3 == spc)
1557 bad_twosharp:
1558 tcc_error("'##' cannot appear at either end of macro");
1559 define_push(v, t, tok_str_dup(&tokstr_buf), first);
1562 static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
1564 const unsigned char *s;
1565 unsigned int h;
1566 CachedInclude *e;
1567 int i;
1569 h = TOK_HASH_INIT;
1570 s = (unsigned char *) filename;
1571 while (*s) {
1572 #ifdef _WIN32
1573 h = TOK_HASH_FUNC(h, toup(*s));
1574 #else
1575 h = TOK_HASH_FUNC(h, *s);
1576 #endif
1577 s++;
1579 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1581 i = s1->cached_includes_hash[h];
1582 for(;;) {
1583 if (i == 0)
1584 break;
1585 e = s1->cached_includes[i - 1];
1586 if (0 == PATHCMP(e->filename, filename))
1587 return e;
1588 i = e->hash_next;
1590 if (!add)
1591 return NULL;
1593 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1594 strcpy(e->filename, filename);
1595 e->ifndef_macro = e->once = 0;
1596 dynarray_add(&s1->cached_includes, &s1->nb_cached_includes, e);
1597 /* add in hash table */
1598 e->hash_next = s1->cached_includes_hash[h];
1599 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1600 #ifdef INC_DEBUG
1601 printf("adding cached '%s'\n", filename);
1602 #endif
1603 return e;
1606 static void pragma_parse(TCCState *s1)
1608 next_nomacro();
1609 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1610 int t = tok, v;
1611 Sym *s;
1613 if (next(), tok != '(')
1614 goto pragma_err;
1615 if (next(), tok != TOK_STR)
1616 goto pragma_err;
1617 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1618 if (next(), tok != ')')
1619 goto pragma_err;
1620 if (t == TOK_push_macro) {
1621 while (NULL == (s = define_find(v)))
1622 define_push(v, 0, NULL, NULL);
1623 s->type.ref = s; /* set push boundary */
1624 } else {
1625 for (s = define_stack; s; s = s->prev)
1626 if (s->v == v && s->type.ref == s) {
1627 s->type.ref = NULL;
1628 break;
1631 if (s)
1632 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1633 else
1634 tcc_warning("unbalanced #pragma pop_macro");
1635 pp_debug_tok = t, pp_debug_symv = v;
1637 } else if (tok == TOK_once) {
1638 search_cached_include(s1, file->filename, 1)->once = pp_once;
1640 } else if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
1641 /* tcc -E: keep pragmas below unchanged */
1642 unget_tok(' ');
1643 unget_tok(TOK_PRAGMA);
1644 unget_tok('#');
1645 unget_tok(TOK_LINEFEED);
1647 } else if (tok == TOK_pack) {
1648 /* This may be:
1649 #pragma pack(1) // set
1650 #pragma pack() // reset to default
1651 #pragma pack(push,1) // push & set
1652 #pragma pack(pop) // restore previous */
1653 next();
1654 skip('(');
1655 if (tok == TOK_ASM_pop) {
1656 next();
1657 if (s1->pack_stack_ptr <= s1->pack_stack) {
1658 stk_error:
1659 tcc_error("out of pack stack");
1661 s1->pack_stack_ptr--;
1662 } else {
1663 int val = 0;
1664 if (tok != ')') {
1665 if (tok == TOK_ASM_push) {
1666 next();
1667 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1668 goto stk_error;
1669 s1->pack_stack_ptr++;
1670 skip(',');
1672 if (tok != TOK_CINT)
1673 goto pragma_err;
1674 val = tokc.i;
1675 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1676 goto pragma_err;
1677 next();
1679 *s1->pack_stack_ptr = val;
1681 if (tok != ')')
1682 goto pragma_err;
1684 } else if (tok == TOK_comment) {
1685 char *p; int t;
1686 next();
1687 skip('(');
1688 t = tok;
1689 next();
1690 skip(',');
1691 if (tok != TOK_STR)
1692 goto pragma_err;
1693 p = tcc_strdup((char *)tokc.str.data);
1694 next();
1695 if (tok != ')')
1696 goto pragma_err;
1697 if (t == TOK_lib) {
1698 dynarray_add(&s1->pragma_libs, &s1->nb_pragma_libs, p);
1699 } else {
1700 if (t == TOK_option)
1701 tcc_set_options(s1, p);
1702 tcc_free(p);
1705 } else if (s1->warn_unsupported) {
1706 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1708 return;
1710 pragma_err:
1711 tcc_error("malformed #pragma directive");
1712 return;
1715 /* is_bof is true if first non space token at beginning of file */
1716 ST_FUNC void preprocess(int is_bof)
1718 TCCState *s1 = tcc_state;
1719 int i, c, n, saved_parse_flags;
1720 char buf[1024], *q;
1721 Sym *s;
1723 saved_parse_flags = parse_flags;
1724 parse_flags = PARSE_FLAG_PREPROCESS
1725 | PARSE_FLAG_TOK_NUM
1726 | PARSE_FLAG_TOK_STR
1727 | PARSE_FLAG_LINEFEED
1728 | (parse_flags & PARSE_FLAG_ASM_FILE)
1731 next_nomacro();
1732 redo:
1733 switch(tok) {
1734 case TOK_DEFINE:
1735 pp_debug_tok = tok;
1736 next_nomacro();
1737 pp_debug_symv = tok;
1738 parse_define();
1739 break;
1740 case TOK_UNDEF:
1741 pp_debug_tok = tok;
1742 next_nomacro();
1743 pp_debug_symv = tok;
1744 s = define_find(tok);
1745 /* undefine symbol by putting an invalid name */
1746 if (s)
1747 define_undef(s);
1748 break;
1749 case TOK_INCLUDE:
1750 case TOK_INCLUDE_NEXT:
1751 ch = file->buf_ptr[0];
1752 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1753 skip_spaces();
1754 if (ch == '<') {
1755 c = '>';
1756 goto read_name;
1757 } else if (ch == '\"') {
1758 c = ch;
1759 read_name:
1760 inp();
1761 q = buf;
1762 while (ch != c && ch != '\n' && ch != CH_EOF) {
1763 if ((q - buf) < sizeof(buf) - 1)
1764 *q++ = ch;
1765 if (ch == '\\') {
1766 if (handle_stray_noerror() == 0)
1767 --q;
1768 } else
1769 inp();
1771 *q = '\0';
1772 minp();
1773 #if 0
1774 /* eat all spaces and comments after include */
1775 /* XXX: slightly incorrect */
1776 while (ch1 != '\n' && ch1 != CH_EOF)
1777 inp();
1778 #endif
1779 } else {
1780 int len;
1781 /* computed #include : concatenate everything up to linefeed,
1782 the result must be one of the two accepted forms.
1783 Don't convert pp-tokens to tokens here. */
1784 parse_flags = (PARSE_FLAG_PREPROCESS
1785 | PARSE_FLAG_LINEFEED
1786 | (parse_flags & PARSE_FLAG_ASM_FILE));
1787 next();
1788 buf[0] = '\0';
1789 while (tok != TOK_LINEFEED) {
1790 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1791 next();
1793 len = strlen(buf);
1794 /* check syntax and remove '<>|""' */
1795 if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') &&
1796 (buf[0] != '<' || buf[len-1] != '>'))))
1797 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1798 c = buf[len-1];
1799 memmove(buf, buf + 1, len - 2);
1800 buf[len - 2] = '\0';
1803 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1804 tcc_error("#include recursion too deep");
1805 /* store current file in stack, but increment stack later below */
1806 *s1->include_stack_ptr = file;
1807 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1808 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1809 for (; i < n; ++i) {
1810 char buf1[sizeof file->filename];
1811 CachedInclude *e;
1812 const char *path;
1814 if (i == 0) {
1815 /* check absolute include path */
1816 if (!IS_ABSPATH(buf))
1817 continue;
1818 buf1[0] = 0;
1820 } else if (i == 1) {
1821 /* search in file's dir if "header.h" */
1822 if (c != '\"')
1823 continue;
1824 /* https://savannah.nongnu.org/bugs/index.php?50847 */
1825 path = file->true_filename;
1826 pstrncpy(buf1, path, tcc_basename(path) - path);
1828 } else {
1829 /* search in all the include paths */
1830 int j = i - 2, k = j - s1->nb_include_paths;
1831 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1832 pstrcpy(buf1, sizeof(buf1), path);
1833 pstrcat(buf1, sizeof(buf1), "/");
1836 pstrcat(buf1, sizeof(buf1), buf);
1837 e = search_cached_include(s1, buf1, 0);
1838 if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
1839 /* no need to parse the include because the 'ifndef macro'
1840 is defined (or had #pragma once) */
1841 #ifdef INC_DEBUG
1842 printf("%s: skipping cached %s\n", file->filename, buf1);
1843 #endif
1844 goto include_done;
1847 if (tcc_open(s1, buf1) < 0)
1848 continue;
1850 file->include_next_index = i + 1;
1851 #ifdef INC_DEBUG
1852 printf("%s: including %s\n", file->prev->filename, file->filename);
1853 #endif
1854 /* update target deps */
1855 dynarray_add(&s1->target_deps, &s1->nb_target_deps,
1856 tcc_strdup(buf1));
1857 /* push current file in stack */
1858 ++s1->include_stack_ptr;
1859 /* add include file debug info */
1860 if (s1->do_debug)
1861 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1862 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1863 ch = file->buf_ptr[0];
1864 goto the_end;
1866 tcc_error("include file '%s' not found", buf);
1867 include_done:
1868 break;
1869 case TOK_IFNDEF:
1870 c = 1;
1871 goto do_ifdef;
1872 case TOK_IF:
1873 c = expr_preprocess();
1874 goto do_if;
1875 case TOK_IFDEF:
1876 c = 0;
1877 do_ifdef:
1878 next_nomacro();
1879 if (tok < TOK_IDENT)
1880 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1881 if (is_bof) {
1882 if (c) {
1883 #ifdef INC_DEBUG
1884 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1885 #endif
1886 file->ifndef_macro = tok;
1889 c = (define_find(tok) != 0) ^ c;
1890 do_if:
1891 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1892 tcc_error("memory full (ifdef)");
1893 *s1->ifdef_stack_ptr++ = c;
1894 goto test_skip;
1895 case TOK_ELSE:
1896 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1897 tcc_error("#else without matching #if");
1898 if (s1->ifdef_stack_ptr[-1] & 2)
1899 tcc_error("#else after #else");
1900 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1901 goto test_else;
1902 case TOK_ELIF:
1903 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1904 tcc_error("#elif without matching #if");
1905 c = s1->ifdef_stack_ptr[-1];
1906 if (c > 1)
1907 tcc_error("#elif after #else");
1908 /* last #if/#elif expression was true: we skip */
1909 if (c == 1) {
1910 c = 0;
1911 } else {
1912 c = expr_preprocess();
1913 s1->ifdef_stack_ptr[-1] = c;
1915 test_else:
1916 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1917 file->ifndef_macro = 0;
1918 test_skip:
1919 if (!(c & 1)) {
1920 preprocess_skip();
1921 is_bof = 0;
1922 goto redo;
1924 break;
1925 case TOK_ENDIF:
1926 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1927 tcc_error("#endif without matching #if");
1928 s1->ifdef_stack_ptr--;
1929 /* '#ifndef macro' was at the start of file. Now we check if
1930 an '#endif' is exactly at the end of file */
1931 if (file->ifndef_macro &&
1932 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1933 file->ifndef_macro_saved = file->ifndef_macro;
1934 /* need to set to zero to avoid false matches if another
1935 #ifndef at middle of file */
1936 file->ifndef_macro = 0;
1937 while (tok != TOK_LINEFEED)
1938 next_nomacro();
1939 tok_flags |= TOK_FLAG_ENDIF;
1940 goto the_end;
1942 break;
1943 case TOK_PPNUM:
1944 n = strtoul((char*)tokc.str.data, &q, 10);
1945 goto _line_num;
1946 case TOK_LINE:
1947 next();
1948 if (tok != TOK_CINT)
1949 _line_err:
1950 tcc_error("wrong #line format");
1951 n = tokc.i;
1952 _line_num:
1953 next();
1954 if (tok != TOK_LINEFEED) {
1955 if (tok == TOK_STR) {
1956 if (file->true_filename == file->filename)
1957 file->true_filename = tcc_strdup(file->filename);
1958 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1959 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
1960 break;
1961 else
1962 goto _line_err;
1963 --n;
1965 if (file->fd > 0)
1966 total_lines += file->line_num - n;
1967 file->line_num = n;
1968 if (s1->do_debug)
1969 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1970 break;
1971 case TOK_ERROR:
1972 case TOK_WARNING:
1973 c = tok;
1974 ch = file->buf_ptr[0];
1975 skip_spaces();
1976 q = buf;
1977 while (ch != '\n' && ch != CH_EOF) {
1978 if ((q - buf) < sizeof(buf) - 1)
1979 *q++ = ch;
1980 if (ch == '\\') {
1981 if (handle_stray_noerror() == 0)
1982 --q;
1983 } else
1984 inp();
1986 *q = '\0';
1987 if (c == TOK_ERROR)
1988 tcc_error("#error %s", buf);
1989 else
1990 tcc_warning("#warning %s", buf);
1991 break;
1992 case TOK_PRAGMA:
1993 pragma_parse(s1);
1994 break;
1995 case TOK_LINEFEED:
1996 goto the_end;
1997 default:
1998 /* ignore gas line comment in an 'S' file. */
1999 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
2000 goto ignore;
2001 if (tok == '!' && is_bof)
2002 /* '!' is ignored at beginning to allow C scripts. */
2003 goto ignore;
2004 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
2005 ignore:
2006 file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
2007 goto the_end;
2009 /* ignore other preprocess commands or #! for C scripts */
2010 while (tok != TOK_LINEFEED)
2011 next_nomacro();
2012 the_end:
2013 parse_flags = saved_parse_flags;
2016 /* evaluate escape codes in a string. */
2017 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2019 int c, n;
2020 const uint8_t *p;
2022 p = buf;
2023 for(;;) {
2024 c = *p;
2025 if (c == '\0')
2026 break;
2027 if (c == '\\') {
2028 p++;
2029 /* escape */
2030 c = *p;
2031 switch(c) {
2032 case '0': case '1': case '2': case '3':
2033 case '4': case '5': case '6': case '7':
2034 /* at most three octal digits */
2035 n = c - '0';
2036 p++;
2037 c = *p;
2038 if (isoct(c)) {
2039 n = n * 8 + c - '0';
2040 p++;
2041 c = *p;
2042 if (isoct(c)) {
2043 n = n * 8 + c - '0';
2044 p++;
2047 c = n;
2048 goto add_char_nonext;
2049 case 'x':
2050 case 'u':
2051 case 'U':
2052 p++;
2053 n = 0;
2054 for(;;) {
2055 c = *p;
2056 if (c >= 'a' && c <= 'f')
2057 c = c - 'a' + 10;
2058 else if (c >= 'A' && c <= 'F')
2059 c = c - 'A' + 10;
2060 else if (isnum(c))
2061 c = c - '0';
2062 else
2063 break;
2064 n = n * 16 + c;
2065 p++;
2067 c = n;
2068 goto add_char_nonext;
2069 case 'a':
2070 c = '\a';
2071 break;
2072 case 'b':
2073 c = '\b';
2074 break;
2075 case 'f':
2076 c = '\f';
2077 break;
2078 case 'n':
2079 c = '\n';
2080 break;
2081 case 'r':
2082 c = '\r';
2083 break;
2084 case 't':
2085 c = '\t';
2086 break;
2087 case 'v':
2088 c = '\v';
2089 break;
2090 case 'e':
2091 if (!gnu_ext)
2092 goto invalid_escape;
2093 c = 27;
2094 break;
2095 case '\'':
2096 case '\"':
2097 case '\\':
2098 case '?':
2099 break;
2100 default:
2101 invalid_escape:
2102 if (c >= '!' && c <= '~')
2103 tcc_warning("unknown escape sequence: \'\\%c\'", c);
2104 else
2105 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
2106 break;
2108 } else if (is_long && c >= 0x80) {
2109 /* assume we are processing UTF-8 sequence */
2110 /* reference: The Unicode Standard, Version 10.0, ch3.9 */
2112 int cont; /* count of continuation bytes */
2113 int skip; /* how many bytes should skip when error occured */
2114 int i;
2116 /* decode leading byte */
2117 if (c < 0xC2) {
2118 skip = 1; goto invalid_utf8_sequence;
2119 } else if (c <= 0xDF) {
2120 cont = 1; n = c & 0x1f;
2121 } else if (c <= 0xEF) {
2122 cont = 2; n = c & 0xf;
2123 } else if (c <= 0xF4) {
2124 cont = 3; n = c & 0x7;
2125 } else {
2126 skip = 1; goto invalid_utf8_sequence;
2129 /* decode continuation bytes */
2130 for (i = 1; i <= cont; i++) {
2131 int l = 0x80, h = 0xBF;
2133 /* adjust limit for second byte */
2134 if (i == 1) {
2135 switch (c) {
2136 case 0xE0: l = 0xA0; break;
2137 case 0xED: h = 0x9F; break;
2138 case 0xF0: l = 0x90; break;
2139 case 0xF4: h = 0x8F; break;
2143 if (p[i] < l || p[i] > h) {
2144 skip = i; goto invalid_utf8_sequence;
2147 n = (n << 6) | (p[i] & 0x3f);
2150 /* advance pointer */
2151 p += 1 + cont;
2152 c = n;
2153 goto add_char_nonext;
2155 /* error handling */
2156 invalid_utf8_sequence:
2157 tcc_warning("ill-formed UTF-8 subsequence starting with: \'\\x%x\'", c);
2158 c = 0xFFFD;
2159 p += skip;
2160 goto add_char_nonext;
2163 p++;
2164 add_char_nonext:
2165 if (!is_long)
2166 cstr_ccat(outstr, c);
2167 else {
2168 #ifdef TCC_TARGET_PE
2169 /* store as UTF-16 */
2170 if (c < 0x10000) {
2171 cstr_wccat(outstr, c);
2172 } else {
2173 c -= 0x10000;
2174 cstr_wccat(outstr, (c >> 10) + 0xD800);
2175 cstr_wccat(outstr, (c & 0x3FF) + 0xDC00);
2177 #else
2178 cstr_wccat(outstr, c);
2179 #endif
2182 /* add a trailing '\0' */
2183 if (!is_long)
2184 cstr_ccat(outstr, '\0');
2185 else
2186 cstr_wccat(outstr, '\0');
2189 static void parse_string(const char *s, int len)
2191 uint8_t buf[1000], *p = buf;
2192 int is_long, sep;
2194 if ((is_long = *s == 'L'))
2195 ++s, --len;
2196 sep = *s++;
2197 len -= 2;
2198 if (len >= sizeof buf)
2199 p = tcc_malloc(len + 1);
2200 memcpy(p, s, len);
2201 p[len] = 0;
2203 cstr_reset(&tokcstr);
2204 parse_escape_string(&tokcstr, p, is_long);
2205 if (p != buf)
2206 tcc_free(p);
2208 if (sep == '\'') {
2209 int char_size;
2210 /* XXX: make it portable */
2211 if (!is_long)
2212 char_size = 1;
2213 else
2214 char_size = sizeof(nwchar_t);
2215 if (tokcstr.size <= char_size)
2216 tcc_error("empty character constant");
2217 if (tokcstr.size > 2 * char_size)
2218 tcc_warning("multi-character character constant");
2219 if (!is_long) {
2220 tokc.i = *(int8_t *)tokcstr.data;
2221 tok = TOK_CCHAR;
2222 } else {
2223 tokc.i = *(nwchar_t *)tokcstr.data;
2224 tok = TOK_LCHAR;
2226 } else {
2227 tokc.str.size = tokcstr.size;
2228 tokc.str.data = tokcstr.data;
2229 if (!is_long)
2230 tok = TOK_STR;
2231 else
2232 tok = TOK_LSTR;
2236 /* we use 64 bit numbers */
2237 #define BN_SIZE 2
2239 /* bn = (bn << shift) | or_val */
2240 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2242 int i;
2243 unsigned int v;
2244 for(i=0;i<BN_SIZE;i++) {
2245 v = bn[i];
2246 bn[i] = (v << shift) | or_val;
2247 or_val = v >> (32 - shift);
2251 static void bn_zero(unsigned int *bn)
2253 int i;
2254 for(i=0;i<BN_SIZE;i++) {
2255 bn[i] = 0;
2259 /* parse number in null terminated string 'p' and return it in the
2260 current token */
2261 static void parse_number(const char *p)
2263 int b, t, shift, frac_bits, s, exp_val, ch;
2264 char *q;
2265 unsigned int bn[BN_SIZE];
2266 double d;
2268 /* number */
2269 q = token_buf;
2270 ch = *p++;
2271 t = ch;
2272 ch = *p++;
2273 *q++ = t;
2274 b = 10;
2275 if (t == '.') {
2276 goto float_frac_parse;
2277 } else if (t == '0') {
2278 if (ch == 'x' || ch == 'X') {
2279 q--;
2280 ch = *p++;
2281 b = 16;
2282 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2283 q--;
2284 ch = *p++;
2285 b = 2;
2288 /* parse all digits. cannot check octal numbers at this stage
2289 because of floating point constants */
2290 while (1) {
2291 if (ch >= 'a' && ch <= 'f')
2292 t = ch - 'a' + 10;
2293 else if (ch >= 'A' && ch <= 'F')
2294 t = ch - 'A' + 10;
2295 else if (isnum(ch))
2296 t = ch - '0';
2297 else
2298 break;
2299 if (t >= b)
2300 break;
2301 if (q >= token_buf + STRING_MAX_SIZE) {
2302 num_too_long:
2303 tcc_error("number too long");
2305 *q++ = ch;
2306 ch = *p++;
2308 if (ch == '.' ||
2309 ((ch == 'e' || ch == 'E') && b == 10) ||
2310 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2311 if (b != 10) {
2312 /* NOTE: strtox should support that for hexa numbers, but
2313 non ISOC99 libcs do not support it, so we prefer to do
2314 it by hand */
2315 /* hexadecimal or binary floats */
2316 /* XXX: handle overflows */
2317 *q = '\0';
2318 if (b == 16)
2319 shift = 4;
2320 else
2321 shift = 1;
2322 bn_zero(bn);
2323 q = token_buf;
2324 while (1) {
2325 t = *q++;
2326 if (t == '\0') {
2327 break;
2328 } else if (t >= 'a') {
2329 t = t - 'a' + 10;
2330 } else if (t >= 'A') {
2331 t = t - 'A' + 10;
2332 } else {
2333 t = t - '0';
2335 bn_lshift(bn, shift, t);
2337 frac_bits = 0;
2338 if (ch == '.') {
2339 ch = *p++;
2340 while (1) {
2341 t = ch;
2342 if (t >= 'a' && t <= 'f') {
2343 t = t - 'a' + 10;
2344 } else if (t >= 'A' && t <= 'F') {
2345 t = t - 'A' + 10;
2346 } else if (t >= '0' && t <= '9') {
2347 t = t - '0';
2348 } else {
2349 break;
2351 if (t >= b)
2352 tcc_error("invalid digit");
2353 bn_lshift(bn, shift, t);
2354 frac_bits += shift;
2355 ch = *p++;
2358 if (ch != 'p' && ch != 'P')
2359 expect("exponent");
2360 ch = *p++;
2361 s = 1;
2362 exp_val = 0;
2363 if (ch == '+') {
2364 ch = *p++;
2365 } else if (ch == '-') {
2366 s = -1;
2367 ch = *p++;
2369 if (ch < '0' || ch > '9')
2370 expect("exponent digits");
2371 while (ch >= '0' && ch <= '9') {
2372 exp_val = exp_val * 10 + ch - '0';
2373 ch = *p++;
2375 exp_val = exp_val * s;
2377 /* now we can generate the number */
2378 /* XXX: should patch directly float number */
2379 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2380 d = ldexp(d, exp_val - frac_bits);
2381 t = toup(ch);
2382 if (t == 'F') {
2383 ch = *p++;
2384 tok = TOK_CFLOAT;
2385 /* float : should handle overflow */
2386 tokc.f = (float)d;
2387 } else if (t == 'L') {
2388 ch = *p++;
2389 #ifdef TCC_TARGET_PE
2390 tok = TOK_CDOUBLE;
2391 tokc.d = d;
2392 #else
2393 tok = TOK_CLDOUBLE;
2394 /* XXX: not large enough */
2395 tokc.ld = (long double)d;
2396 #endif
2397 } else {
2398 tok = TOK_CDOUBLE;
2399 tokc.d = d;
2401 } else {
2402 /* decimal floats */
2403 if (ch == '.') {
2404 if (q >= token_buf + STRING_MAX_SIZE)
2405 goto num_too_long;
2406 *q++ = ch;
2407 ch = *p++;
2408 float_frac_parse:
2409 while (ch >= '0' && ch <= '9') {
2410 if (q >= token_buf + STRING_MAX_SIZE)
2411 goto num_too_long;
2412 *q++ = ch;
2413 ch = *p++;
2416 if (ch == 'e' || ch == 'E') {
2417 if (q >= token_buf + STRING_MAX_SIZE)
2418 goto num_too_long;
2419 *q++ = ch;
2420 ch = *p++;
2421 if (ch == '-' || ch == '+') {
2422 if (q >= token_buf + STRING_MAX_SIZE)
2423 goto num_too_long;
2424 *q++ = ch;
2425 ch = *p++;
2427 if (ch < '0' || ch > '9')
2428 expect("exponent digits");
2429 while (ch >= '0' && ch <= '9') {
2430 if (q >= token_buf + STRING_MAX_SIZE)
2431 goto num_too_long;
2432 *q++ = ch;
2433 ch = *p++;
2436 *q = '\0';
2437 t = toup(ch);
2438 errno = 0;
2439 if (t == 'F') {
2440 ch = *p++;
2441 tok = TOK_CFLOAT;
2442 tokc.f = strtof(token_buf, NULL);
2443 } else if (t == 'L') {
2444 ch = *p++;
2445 #ifdef TCC_TARGET_PE
2446 tok = TOK_CDOUBLE;
2447 tokc.d = strtod(token_buf, NULL);
2448 #else
2449 tok = TOK_CLDOUBLE;
2450 tokc.ld = strtold(token_buf, NULL);
2451 #endif
2452 } else {
2453 tok = TOK_CDOUBLE;
2454 tokc.d = strtod(token_buf, NULL);
2457 } else {
2458 unsigned long long n, n1;
2459 int lcount, ucount, must_64bit;
2460 const char *p1;
2462 /* integer number */
2463 *q = '\0';
2464 q = token_buf;
2465 if (b == 10 && *q == '0') {
2466 b = 8;
2467 q++;
2469 n = 0;
2470 while(1) {
2471 t = *q++;
2472 /* no need for checks except for base 10 / 8 errors */
2473 if (t == '\0')
2474 break;
2475 else if (t >= 'a')
2476 t = t - 'a' + 10;
2477 else if (t >= 'A')
2478 t = t - 'A' + 10;
2479 else
2480 t = t - '0';
2481 if (t >= b)
2482 tcc_error("invalid digit");
2483 n1 = n;
2484 n = n * b + t;
2485 /* detect overflow */
2486 /* XXX: this test is not reliable */
2487 if (n < n1)
2488 tcc_error("integer constant overflow");
2491 /* Determine the characteristics (unsigned and/or 64bit) the type of
2492 the constant must have according to the constant suffix(es) */
2493 lcount = ucount = must_64bit = 0;
2494 p1 = p;
2495 for(;;) {
2496 t = toup(ch);
2497 if (t == 'L') {
2498 if (lcount >= 2)
2499 tcc_error("three 'l's in integer constant");
2500 if (lcount && *(p - 1) != ch)
2501 tcc_error("incorrect integer suffix: %s", p1);
2502 lcount++;
2503 if (lcount == 2)
2504 must_64bit = 1;
2505 ch = *p++;
2506 } else if (t == 'U') {
2507 if (ucount >= 1)
2508 tcc_error("two 'u's in integer constant");
2509 ucount++;
2510 ch = *p++;
2511 } else {
2512 break;
2516 /* Whether 64 bits are needed to hold the constant's value */
2517 if (n & 0xffffffff00000000LL || must_64bit) {
2518 tok = TOK_CLLONG;
2519 n1 = n >> 32;
2520 } else if (lcount) {
2521 #ifdef TCC_LONG_ARE_64_BIT
2522 n1 = n >> 32;
2523 #else
2524 n1 = n;
2525 #endif
2526 tok = TOK_CLONG;
2527 } else {
2528 tok = TOK_CINT;
2529 n1 = n;
2532 /* Whether type must be unsigned to hold the constant's value */
2533 if (ucount || ((n1 >> 31) && (b != 10))) {
2534 if (tok == TOK_CLLONG)
2535 tok = TOK_CULLONG;
2536 else if (tok == TOK_CLONG)
2537 tok = TOK_CULONG;
2538 else
2539 tok = TOK_CUINT;
2540 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2541 } else if (n1 >> 31) {
2542 if (tok == TOK_CINT)
2543 tok = TOK_CLLONG;
2544 else
2545 tcc_error("integer constant overflow");
2548 tokc.i = n;
2550 if (ch)
2551 tcc_error("invalid number\n");
2555 #define PARSE2(c1, tok1, c2, tok2) \
2556 case c1: \
2557 PEEKC(c, p); \
2558 if (c == c2) { \
2559 p++; \
2560 tok = tok2; \
2561 } else { \
2562 tok = tok1; \
2564 break;
2566 /* return next token without macro substitution */
2567 static inline void next_nomacro1(void)
2569 int t, c, is_long, len;
2570 TokenSym *ts;
2571 uint8_t *p, *p1;
2572 unsigned int h;
2574 p = file->buf_ptr;
2575 redo_no_start:
2576 c = *p;
2577 switch(c) {
2578 case ' ':
2579 case '\t':
2580 tok = c;
2581 p++;
2582 if (parse_flags & PARSE_FLAG_SPACES)
2583 goto keep_tok_flags;
2584 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2585 ++p;
2586 goto redo_no_start;
2587 case '\f':
2588 case '\v':
2589 case '\r':
2590 p++;
2591 goto redo_no_start;
2592 case '\\':
2593 /* first look if it is in fact an end of buffer */
2594 c = handle_stray1(p);
2595 p = file->buf_ptr;
2596 if (c == '\\')
2597 goto parse_simple;
2598 if (c != CH_EOF)
2599 goto redo_no_start;
2601 TCCState *s1 = tcc_state;
2602 if ((parse_flags & PARSE_FLAG_LINEFEED)
2603 && !(tok_flags & TOK_FLAG_EOF)) {
2604 tok_flags |= TOK_FLAG_EOF;
2605 tok = TOK_LINEFEED;
2606 goto keep_tok_flags;
2607 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2608 tok = TOK_EOF;
2609 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2610 tcc_error("missing #endif");
2611 } else if (s1->include_stack_ptr == s1->include_stack) {
2612 /* no include left : end of file. */
2613 tok = TOK_EOF;
2614 } else {
2615 tok_flags &= ~TOK_FLAG_EOF;
2616 /* pop include file */
2618 /* test if previous '#endif' was after a #ifdef at
2619 start of file */
2620 if (tok_flags & TOK_FLAG_ENDIF) {
2621 #ifdef INC_DEBUG
2622 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2623 #endif
2624 search_cached_include(s1, file->filename, 1)
2625 ->ifndef_macro = file->ifndef_macro_saved;
2626 tok_flags &= ~TOK_FLAG_ENDIF;
2629 /* add end of include file debug info */
2630 if (tcc_state->do_debug) {
2631 put_stabd(N_EINCL, 0, 0);
2633 /* pop include stack */
2634 tcc_close();
2635 s1->include_stack_ptr--;
2636 p = file->buf_ptr;
2637 goto redo_no_start;
2640 break;
2642 case '\n':
2643 file->line_num++;
2644 tok_flags |= TOK_FLAG_BOL;
2645 p++;
2646 maybe_newline:
2647 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2648 goto redo_no_start;
2649 tok = TOK_LINEFEED;
2650 goto keep_tok_flags;
2652 case '#':
2653 /* XXX: simplify */
2654 PEEKC(c, p);
2655 if ((tok_flags & TOK_FLAG_BOL) &&
2656 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2657 file->buf_ptr = p;
2658 preprocess(tok_flags & TOK_FLAG_BOF);
2659 p = file->buf_ptr;
2660 goto maybe_newline;
2661 } else {
2662 if (c == '#') {
2663 p++;
2664 tok = TOK_TWOSHARPS;
2665 } else {
2666 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2667 p = parse_line_comment(p - 1);
2668 goto redo_no_start;
2669 } else {
2670 tok = '#';
2674 break;
2676 /* dollar is allowed to start identifiers when not parsing asm */
2677 case '$':
2678 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2679 || (parse_flags & PARSE_FLAG_ASM_FILE))
2680 goto parse_simple;
2682 case 'a': case 'b': case 'c': case 'd':
2683 case 'e': case 'f': case 'g': case 'h':
2684 case 'i': case 'j': case 'k': case 'l':
2685 case 'm': case 'n': case 'o': case 'p':
2686 case 'q': case 'r': case 's': case 't':
2687 case 'u': case 'v': case 'w': case 'x':
2688 case 'y': case 'z':
2689 case 'A': case 'B': case 'C': case 'D':
2690 case 'E': case 'F': case 'G': case 'H':
2691 case 'I': case 'J': case 'K':
2692 case 'M': case 'N': case 'O': case 'P':
2693 case 'Q': case 'R': case 'S': case 'T':
2694 case 'U': case 'V': case 'W': case 'X':
2695 case 'Y': case 'Z':
2696 case '_':
2697 parse_ident_fast:
2698 p1 = p;
2699 h = TOK_HASH_INIT;
2700 h = TOK_HASH_FUNC(h, c);
2701 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2702 h = TOK_HASH_FUNC(h, c);
2703 len = p - p1;
2704 if (c != '\\') {
2705 TokenSym **pts;
2707 /* fast case : no stray found, so we have the full token
2708 and we have already hashed it */
2709 h &= (TOK_HASH_SIZE - 1);
2710 pts = &hash_ident[h];
2711 for(;;) {
2712 ts = *pts;
2713 if (!ts)
2714 break;
2715 if (ts->len == len && !memcmp(ts->str, p1, len))
2716 goto token_found;
2717 pts = &(ts->hash_next);
2719 ts = tok_alloc_new(pts, (char *) p1, len);
2720 token_found: ;
2721 } else {
2722 /* slower case */
2723 cstr_reset(&tokcstr);
2724 cstr_cat(&tokcstr, (char *) p1, len);
2725 p--;
2726 PEEKC(c, p);
2727 parse_ident_slow:
2728 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2730 cstr_ccat(&tokcstr, c);
2731 PEEKC(c, p);
2733 ts = tok_alloc(tokcstr.data, tokcstr.size);
2735 tok = ts->tok;
2736 break;
2737 case 'L':
2738 t = p[1];
2739 if (t != '\\' && t != '\'' && t != '\"') {
2740 /* fast case */
2741 goto parse_ident_fast;
2742 } else {
2743 PEEKC(c, p);
2744 if (c == '\'' || c == '\"') {
2745 is_long = 1;
2746 goto str_const;
2747 } else {
2748 cstr_reset(&tokcstr);
2749 cstr_ccat(&tokcstr, 'L');
2750 goto parse_ident_slow;
2753 break;
2755 case '0': case '1': case '2': case '3':
2756 case '4': case '5': case '6': case '7':
2757 case '8': case '9':
2758 t = c;
2759 PEEKC(c, p);
2760 /* after the first digit, accept digits, alpha, '.' or sign if
2761 prefixed by 'eEpP' */
2762 parse_num:
2763 cstr_reset(&tokcstr);
2764 for(;;) {
2765 cstr_ccat(&tokcstr, t);
2766 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2767 || c == '.'
2768 || ((c == '+' || c == '-')
2769 && (((t == 'e' || t == 'E')
2770 && !(parse_flags & PARSE_FLAG_ASM_FILE
2771 /* 0xe+1 is 3 tokens in asm */
2772 && ((char*)tokcstr.data)[0] == '0'
2773 && toup(((char*)tokcstr.data)[1]) == 'X'))
2774 || t == 'p' || t == 'P'))))
2775 break;
2776 t = c;
2777 PEEKC(c, p);
2779 /* We add a trailing '\0' to ease parsing */
2780 cstr_ccat(&tokcstr, '\0');
2781 tokc.str.size = tokcstr.size;
2782 tokc.str.data = tokcstr.data;
2783 tok = TOK_PPNUM;
2784 break;
2786 case '.':
2787 /* special dot handling because it can also start a number */
2788 PEEKC(c, p);
2789 if (isnum(c)) {
2790 t = '.';
2791 goto parse_num;
2792 } else if ((isidnum_table['.' - CH_EOF] & IS_ID)
2793 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
2794 *--p = c = '.';
2795 goto parse_ident_fast;
2796 } else if (c == '.') {
2797 PEEKC(c, p);
2798 if (c == '.') {
2799 p++;
2800 tok = TOK_DOTS;
2801 } else {
2802 *--p = '.'; /* may underflow into file->unget[] */
2803 tok = '.';
2805 } else {
2806 tok = '.';
2808 break;
2809 case '\'':
2810 case '\"':
2811 is_long = 0;
2812 str_const:
2813 cstr_reset(&tokcstr);
2814 if (is_long)
2815 cstr_ccat(&tokcstr, 'L');
2816 cstr_ccat(&tokcstr, c);
2817 p = parse_pp_string(p, c, &tokcstr);
2818 cstr_ccat(&tokcstr, c);
2819 cstr_ccat(&tokcstr, '\0');
2820 tokc.str.size = tokcstr.size;
2821 tokc.str.data = tokcstr.data;
2822 tok = TOK_PPSTR;
2823 break;
2825 case '<':
2826 PEEKC(c, p);
2827 if (c == '=') {
2828 p++;
2829 tok = TOK_LE;
2830 } else if (c == '<') {
2831 PEEKC(c, p);
2832 if (c == '=') {
2833 p++;
2834 tok = TOK_A_SHL;
2835 } else {
2836 tok = TOK_SHL;
2838 } else {
2839 tok = TOK_LT;
2841 break;
2842 case '>':
2843 PEEKC(c, p);
2844 if (c == '=') {
2845 p++;
2846 tok = TOK_GE;
2847 } else if (c == '>') {
2848 PEEKC(c, p);
2849 if (c == '=') {
2850 p++;
2851 tok = TOK_A_SAR;
2852 } else {
2853 tok = TOK_SAR;
2855 } else {
2856 tok = TOK_GT;
2858 break;
2860 case '&':
2861 PEEKC(c, p);
2862 if (c == '&') {
2863 p++;
2864 tok = TOK_LAND;
2865 } else if (c == '=') {
2866 p++;
2867 tok = TOK_A_AND;
2868 } else {
2869 tok = '&';
2871 break;
2873 case '|':
2874 PEEKC(c, p);
2875 if (c == '|') {
2876 p++;
2877 tok = TOK_LOR;
2878 } else if (c == '=') {
2879 p++;
2880 tok = TOK_A_OR;
2881 } else {
2882 tok = '|';
2884 break;
2886 case '+':
2887 PEEKC(c, p);
2888 if (c == '+') {
2889 p++;
2890 tok = TOK_INC;
2891 } else if (c == '=') {
2892 p++;
2893 tok = TOK_A_ADD;
2894 } else {
2895 tok = '+';
2897 break;
2899 case '-':
2900 PEEKC(c, p);
2901 if (c == '-') {
2902 p++;
2903 tok = TOK_DEC;
2904 } else if (c == '=') {
2905 p++;
2906 tok = TOK_A_SUB;
2907 } else if (c == '>') {
2908 p++;
2909 tok = TOK_ARROW;
2910 } else {
2911 tok = '-';
2913 break;
2915 PARSE2('!', '!', '=', TOK_NE)
2916 PARSE2('=', '=', '=', TOK_EQ)
2917 PARSE2('*', '*', '=', TOK_A_MUL)
2918 PARSE2('%', '%', '=', TOK_A_MOD)
2919 PARSE2('^', '^', '=', TOK_A_XOR)
2921 /* comments or operator */
2922 case '/':
2923 PEEKC(c, p);
2924 if (c == '*') {
2925 p = parse_comment(p);
2926 /* comments replaced by a blank */
2927 tok = ' ';
2928 goto keep_tok_flags;
2929 } else if (c == '/') {
2930 p = parse_line_comment(p);
2931 tok = ' ';
2932 goto keep_tok_flags;
2933 } else if (c == '=') {
2934 p++;
2935 tok = TOK_A_DIV;
2936 } else {
2937 tok = '/';
2939 break;
2941 /* simple tokens */
2942 case '(':
2943 case ')':
2944 case '[':
2945 case ']':
2946 case '{':
2947 case '}':
2948 case ',':
2949 case ';':
2950 case ':':
2951 case '?':
2952 case '~':
2953 case '@': /* only used in assembler */
2954 parse_simple:
2955 tok = c;
2956 p++;
2957 break;
2958 default:
2959 if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
2960 goto parse_ident_fast;
2961 if (parse_flags & PARSE_FLAG_ASM_FILE)
2962 goto parse_simple;
2963 tcc_error("unrecognized character \\x%02x", c);
2964 break;
2966 tok_flags = 0;
2967 keep_tok_flags:
2968 file->buf_ptr = p;
2969 #if defined(PARSE_DEBUG)
2970 printf("token = %s\n", get_tok_str(tok, &tokc));
2971 #endif
2974 /* return next token without macro substitution. Can read input from
2975 macro_ptr buffer */
2976 static void next_nomacro_spc(void)
2978 if (macro_ptr) {
2979 redo:
2980 tok = *macro_ptr;
2981 if (tok) {
2982 TOK_GET(&tok, &macro_ptr, &tokc);
2983 if (tok == TOK_LINENUM) {
2984 file->line_num = tokc.i;
2985 goto redo;
2988 } else {
2989 next_nomacro1();
2991 //printf("token = %s\n", get_tok_str(tok, &tokc));
2994 ST_FUNC void next_nomacro(void)
2996 do {
2997 next_nomacro_spc();
2998 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
3002 static void macro_subst(
3003 TokenString *tok_str,
3004 Sym **nested_list,
3005 const int *macro_str
3008 /* substitute arguments in replacement lists in macro_str by the values in
3009 args (field d) and return allocated string */
3010 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
3012 int t, t0, t1, spc;
3013 const int *st;
3014 Sym *s;
3015 CValue cval;
3016 TokenString str;
3017 CString cstr;
3019 tok_str_new(&str);
3020 t0 = t1 = 0;
3021 while(1) {
3022 TOK_GET(&t, &macro_str, &cval);
3023 if (!t)
3024 break;
3025 if (t == '#') {
3026 /* stringize */
3027 TOK_GET(&t, &macro_str, &cval);
3028 if (!t)
3029 goto bad_stringy;
3030 s = sym_find2(args, t);
3031 if (s) {
3032 cstr_new(&cstr);
3033 cstr_ccat(&cstr, '\"');
3034 st = s->d;
3035 spc = 0;
3036 while (*st >= 0) {
3037 TOK_GET(&t, &st, &cval);
3038 if (t != TOK_PLCHLDR
3039 && t != TOK_NOSUBST
3040 && 0 == check_space(t, &spc)) {
3041 const char *s = get_tok_str(t, &cval);
3042 while (*s) {
3043 if (t == TOK_PPSTR && *s != '\'')
3044 add_char(&cstr, *s);
3045 else
3046 cstr_ccat(&cstr, *s);
3047 ++s;
3051 cstr.size -= spc;
3052 cstr_ccat(&cstr, '\"');
3053 cstr_ccat(&cstr, '\0');
3054 #ifdef PP_DEBUG
3055 printf("\nstringize: <%s>\n", (char *)cstr.data);
3056 #endif
3057 /* add string */
3058 cval.str.size = cstr.size;
3059 cval.str.data = cstr.data;
3060 tok_str_add2(&str, TOK_PPSTR, &cval);
3061 cstr_free(&cstr);
3062 } else {
3063 bad_stringy:
3064 expect("macro parameter after '#'");
3066 } else if (t >= TOK_IDENT) {
3067 s = sym_find2(args, t);
3068 if (s) {
3069 int l0 = str.len;
3070 st = s->d;
3071 /* if '##' is present before or after, no arg substitution */
3072 if (*macro_str == TOK_PPJOIN || t1 == TOK_PPJOIN) {
3073 /* special case for var arg macros : ## eats the ','
3074 if empty VA_ARGS variable. */
3075 if (t1 == TOK_PPJOIN && t0 == ',' && gnu_ext && s->type.t) {
3076 if (*st <= 0) {
3077 /* suppress ',' '##' */
3078 str.len -= 2;
3079 } else {
3080 /* suppress '##' and add variable */
3081 str.len--;
3082 goto add_var;
3085 } else {
3086 add_var:
3087 if (!s->next) {
3088 /* Expand arguments tokens and store them. In most
3089 cases we could also re-expand each argument if
3090 used multiple times, but not if the argument
3091 contains the __COUNTER__ macro. */
3092 TokenString str2;
3093 sym_push2(&s->next, s->v, s->type.t, 0);
3094 tok_str_new(&str2);
3095 macro_subst(&str2, nested_list, st);
3096 tok_str_add(&str2, 0);
3097 s->next->d = str2.str;
3099 st = s->next->d;
3101 for(;;) {
3102 int t2;
3103 TOK_GET(&t2, &st, &cval);
3104 if (t2 <= 0)
3105 break;
3106 tok_str_add2(&str, t2, &cval);
3108 if (str.len == l0) /* expanded to empty string */
3109 tok_str_add(&str, TOK_PLCHLDR);
3110 } else {
3111 tok_str_add(&str, t);
3113 } else {
3114 tok_str_add2(&str, t, &cval);
3116 t0 = t1, t1 = t;
3118 tok_str_add(&str, 0);
3119 return str.str;
3122 static char const ab_month_name[12][4] =
3124 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3125 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3128 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3130 CString cstr;
3131 int n, ret = 1;
3133 cstr_new(&cstr);
3134 if (t1 != TOK_PLCHLDR)
3135 cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3136 n = cstr.size;
3137 if (t2 != TOK_PLCHLDR)
3138 cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3139 cstr_ccat(&cstr, '\0');
3141 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3142 memcpy(file->buffer, cstr.data, cstr.size);
3143 tok_flags = 0;
3144 for (;;) {
3145 next_nomacro1();
3146 if (0 == *file->buf_ptr)
3147 break;
3148 if (is_space(tok))
3149 continue;
3150 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
3151 " preprocessing token", n, cstr.data, (char*)cstr.data + n);
3152 ret = 0;
3153 break;
3155 tcc_close();
3156 //printf("paste <%s>\n", (char*)cstr.data);
3157 cstr_free(&cstr);
3158 return ret;
3161 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3162 return the resulting string (which must be freed). */
3163 static inline int *macro_twosharps(const int *ptr0)
3165 int t;
3166 CValue cval;
3167 TokenString macro_str1;
3168 int start_of_nosubsts = -1;
3169 const int *ptr;
3171 /* we search the first '##' */
3172 for (ptr = ptr0;;) {
3173 TOK_GET(&t, &ptr, &cval);
3174 if (t == TOK_PPJOIN)
3175 break;
3176 if (t == 0)
3177 return NULL;
3180 tok_str_new(&macro_str1);
3182 //tok_print(" $$$", ptr0);
3183 for (ptr = ptr0;;) {
3184 TOK_GET(&t, &ptr, &cval);
3185 if (t == 0)
3186 break;
3187 if (t == TOK_PPJOIN)
3188 continue;
3189 while (*ptr == TOK_PPJOIN) {
3190 int t1; CValue cv1;
3191 /* given 'a##b', remove nosubsts preceding 'a' */
3192 if (start_of_nosubsts >= 0)
3193 macro_str1.len = start_of_nosubsts;
3194 /* given 'a##b', remove nosubsts preceding 'b' */
3195 while ((t1 = *++ptr) == TOK_NOSUBST)
3197 if (t1 && t1 != TOK_PPJOIN) {
3198 TOK_GET(&t1, &ptr, &cv1);
3199 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3200 if (paste_tokens(t, &cval, t1, &cv1)) {
3201 t = tok, cval = tokc;
3202 } else {
3203 tok_str_add2(&macro_str1, t, &cval);
3204 t = t1, cval = cv1;
3209 if (t == TOK_NOSUBST) {
3210 if (start_of_nosubsts < 0)
3211 start_of_nosubsts = macro_str1.len;
3212 } else {
3213 start_of_nosubsts = -1;
3215 tok_str_add2(&macro_str1, t, &cval);
3217 tok_str_add(&macro_str1, 0);
3218 //tok_print(" ###", macro_str1.str);
3219 return macro_str1.str;
3222 /* peek or read [ws_str == NULL] next token from function macro call,
3223 walking up macro levels up to the file if necessary */
3224 static int next_argstream(Sym **nested_list, TokenString *ws_str)
3226 int t;
3227 const int *p;
3228 Sym *sa;
3230 for (;;) {
3231 if (macro_ptr) {
3232 p = macro_ptr, t = *p;
3233 if (ws_str) {
3234 while (is_space(t) || TOK_LINEFEED == t || TOK_PLCHLDR == t)
3235 tok_str_add(ws_str, t), t = *++p;
3237 if (t == 0) {
3238 end_macro();
3239 /* also, end of scope for nested defined symbol */
3240 sa = *nested_list;
3241 while (sa && sa->v == 0)
3242 sa = sa->prev;
3243 if (sa)
3244 sa->v = 0;
3245 continue;
3247 } else {
3248 ch = handle_eob();
3249 if (ws_str) {
3250 while (is_space(ch) || ch == '\n' || ch == '/') {
3251 if (ch == '/') {
3252 int c;
3253 uint8_t *p = file->buf_ptr;
3254 PEEKC(c, p);
3255 if (c == '*') {
3256 p = parse_comment(p);
3257 file->buf_ptr = p - 1;
3258 } else if (c == '/') {
3259 p = parse_line_comment(p);
3260 file->buf_ptr = p - 1;
3261 } else
3262 break;
3263 ch = ' ';
3265 if (ch == '\n')
3266 file->line_num++;
3267 if (!(ch == '\f' || ch == '\v' || ch == '\r'))
3268 tok_str_add(ws_str, ch);
3269 cinp();
3272 t = ch;
3275 if (ws_str)
3276 return t;
3277 next_nomacro_spc();
3278 return tok;
3282 /* do macro substitution of current token with macro 's' and add
3283 result to (tok_str,tok_len). 'nested_list' is the list of all
3284 macros we got inside to avoid recursing. Return non zero if no
3285 substitution needs to be done */
3286 static int macro_subst_tok(
3287 TokenString *tok_str,
3288 Sym **nested_list,
3289 Sym *s)
3291 Sym *args, *sa, *sa1;
3292 int parlevel, t, t1, spc;
3293 TokenString str;
3294 char *cstrval;
3295 CValue cval;
3296 CString cstr;
3297 char buf[32];
3299 /* if symbol is a macro, prepare substitution */
3300 /* special macros */
3301 if (tok == TOK___LINE__ || tok == TOK___COUNTER__) {
3302 t = tok == TOK___LINE__ ? file->line_num : pp_counter++;
3303 snprintf(buf, sizeof(buf), "%d", t);
3304 cstrval = buf;
3305 t1 = TOK_PPNUM;
3306 goto add_cstr1;
3307 } else if (tok == TOK___FILE__) {
3308 cstrval = file->filename;
3309 goto add_cstr;
3310 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3311 time_t ti;
3312 struct tm *tm;
3314 time(&ti);
3315 tm = localtime(&ti);
3316 if (tok == TOK___DATE__) {
3317 snprintf(buf, sizeof(buf), "%s %2d %d",
3318 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3319 } else {
3320 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3321 tm->tm_hour, tm->tm_min, tm->tm_sec);
3323 cstrval = buf;
3324 add_cstr:
3325 t1 = TOK_STR;
3326 add_cstr1:
3327 cstr_new(&cstr);
3328 cstr_cat(&cstr, cstrval, 0);
3329 cval.str.size = cstr.size;
3330 cval.str.data = cstr.data;
3331 tok_str_add2(tok_str, t1, &cval);
3332 cstr_free(&cstr);
3333 } else if (s->d) {
3334 int saved_parse_flags = parse_flags;
3335 int *joined_str = NULL;
3336 int *mstr = s->d;
3338 if (s->type.t == MACRO_FUNC) {
3339 /* whitespace between macro name and argument list */
3340 TokenString ws_str;
3341 tok_str_new(&ws_str);
3343 spc = 0;
3344 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3345 | PARSE_FLAG_ACCEPT_STRAYS;
3347 /* get next token from argument stream */
3348 t = next_argstream(nested_list, &ws_str);
3349 if (t != '(') {
3350 /* not a macro substitution after all, restore the
3351 * macro token plus all whitespace we've read.
3352 * whitespace is intentionally not merged to preserve
3353 * newlines. */
3354 parse_flags = saved_parse_flags;
3355 tok_str_add(tok_str, tok);
3356 if (parse_flags & PARSE_FLAG_SPACES) {
3357 int i;
3358 for (i = 0; i < ws_str.len; i++)
3359 tok_str_add(tok_str, ws_str.str[i]);
3361 tok_str_free_str(ws_str.str);
3362 return 0;
3363 } else {
3364 tok_str_free_str(ws_str.str);
3366 do {
3367 next_nomacro(); /* eat '(' */
3368 } while (tok == TOK_PLCHLDR);
3370 /* argument macro */
3371 args = NULL;
3372 sa = s->next;
3373 /* NOTE: empty args are allowed, except if no args */
3374 for(;;) {
3375 do {
3376 next_argstream(nested_list, NULL);
3377 } while (is_space(tok) || TOK_LINEFEED == tok);
3378 empty_arg:
3379 /* handle '()' case */
3380 if (!args && !sa && tok == ')')
3381 break;
3382 if (!sa)
3383 tcc_error("macro '%s' used with too many args",
3384 get_tok_str(s->v, 0));
3385 tok_str_new(&str);
3386 parlevel = spc = 0;
3387 /* NOTE: non zero sa->t indicates VA_ARGS */
3388 while ((parlevel > 0 ||
3389 (tok != ')' &&
3390 (tok != ',' || sa->type.t)))) {
3391 if (tok == TOK_EOF || tok == 0)
3392 break;
3393 if (tok == '(')
3394 parlevel++;
3395 else if (tok == ')')
3396 parlevel--;
3397 if (tok == TOK_LINEFEED)
3398 tok = ' ';
3399 if (!check_space(tok, &spc))
3400 tok_str_add2(&str, tok, &tokc);
3401 next_argstream(nested_list, NULL);
3403 if (parlevel)
3404 expect(")");
3405 str.len -= spc;
3406 tok_str_add(&str, -1);
3407 tok_str_add(&str, 0);
3408 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3409 sa1->d = str.str;
3410 sa = sa->next;
3411 if (tok == ')') {
3412 /* special case for gcc var args: add an empty
3413 var arg argument if it is omitted */
3414 if (sa && sa->type.t && gnu_ext)
3415 goto empty_arg;
3416 break;
3418 if (tok != ',')
3419 expect(",");
3421 if (sa) {
3422 tcc_error("macro '%s' used with too few args",
3423 get_tok_str(s->v, 0));
3426 parse_flags = saved_parse_flags;
3428 /* now subst each arg */
3429 mstr = macro_arg_subst(nested_list, mstr, args);
3430 /* free memory */
3431 sa = args;
3432 while (sa) {
3433 sa1 = sa->prev;
3434 tok_str_free_str(sa->d);
3435 if (sa->next) {
3436 tok_str_free_str(sa->next->d);
3437 sym_free(sa->next);
3439 sym_free(sa);
3440 sa = sa1;
3444 sym_push2(nested_list, s->v, 0, 0);
3445 parse_flags = saved_parse_flags;
3446 joined_str = macro_twosharps(mstr);
3447 macro_subst(tok_str, nested_list, joined_str ? joined_str : mstr);
3449 /* pop nested defined symbol */
3450 sa1 = *nested_list;
3451 *nested_list = sa1->prev;
3452 sym_free(sa1);
3453 if (joined_str)
3454 tok_str_free_str(joined_str);
3455 if (mstr != s->d)
3456 tok_str_free_str(mstr);
3458 return 0;
3461 /* do macro substitution of macro_str and add result to
3462 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3463 inside to avoid recursing. */
3464 static void macro_subst(
3465 TokenString *tok_str,
3466 Sym **nested_list,
3467 const int *macro_str
3470 Sym *s;
3471 int t, spc, nosubst;
3472 CValue cval;
3474 spc = nosubst = 0;
3476 while (1) {
3477 TOK_GET(&t, &macro_str, &cval);
3478 if (t <= 0)
3479 break;
3481 if (t >= TOK_IDENT && 0 == nosubst) {
3482 s = define_find(t);
3483 if (s == NULL)
3484 goto no_subst;
3486 /* if nested substitution, do nothing */
3487 if (sym_find2(*nested_list, t)) {
3488 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3489 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3490 goto no_subst;
3494 TokenString str;
3495 str.str = (int*)macro_str;
3496 begin_macro(&str, 2);
3498 tok = t;
3499 macro_subst_tok(tok_str, nested_list, s);
3501 if (str.alloc == 3) {
3502 /* already finished by reading function macro arguments */
3503 break;
3506 macro_str = macro_ptr;
3507 end_macro ();
3509 if (tok_str->len)
3510 spc = is_space(t = tok_str->str[tok_str->lastlen]);
3511 } else {
3512 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3513 tcc_error("stray '\\' in program");
3514 no_subst:
3515 if (!check_space(t, &spc))
3516 tok_str_add2(tok_str, t, &cval);
3518 if (nosubst) {
3519 if (nosubst > 1 && (spc || (++nosubst == 3 && t == '(')))
3520 continue;
3521 nosubst = 0;
3523 if (t == TOK_NOSUBST)
3524 nosubst = 1;
3526 /* GCC supports 'defined' as result of a macto substitution */
3527 if (t == TOK_DEFINED && pp_expr)
3528 nosubst = 2;
3532 /* return next token with macro substitution */
3533 ST_FUNC void next(void)
3535 redo:
3536 if (parse_flags & PARSE_FLAG_SPACES)
3537 next_nomacro_spc();
3538 else
3539 next_nomacro();
3541 if (macro_ptr) {
3542 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3543 /* discard preprocessor markers */
3544 goto redo;
3545 } else if (tok == 0) {
3546 /* end of macro or unget token string */
3547 end_macro();
3548 goto redo;
3550 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3551 Sym *s;
3552 /* if reading from file, try to substitute macros */
3553 s = define_find(tok);
3554 if (s) {
3555 Sym *nested_list = NULL;
3556 tokstr_buf.len = 0;
3557 macro_subst_tok(&tokstr_buf, &nested_list, s);
3558 tok_str_add(&tokstr_buf, 0);
3559 begin_macro(&tokstr_buf, 2);
3560 goto redo;
3563 /* convert preprocessor tokens into C tokens */
3564 if (tok == TOK_PPNUM) {
3565 if (parse_flags & PARSE_FLAG_TOK_NUM)
3566 parse_number((char *)tokc.str.data);
3567 } else if (tok == TOK_PPSTR) {
3568 if (parse_flags & PARSE_FLAG_TOK_STR)
3569 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3573 /* push back current token and set current token to 'last_tok'. Only
3574 identifier case handled for labels. */
3575 ST_INLN void unget_tok(int last_tok)
3578 TokenString *str = tok_str_alloc();
3579 tok_str_add2(str, tok, &tokc);
3580 tok_str_add(str, 0);
3581 begin_macro(str, 1);
3582 tok = last_tok;
3585 ST_FUNC void preprocess_start(TCCState *s1, int is_asm)
3587 char *buf;
3589 s1->include_stack_ptr = s1->include_stack;
3590 s1->ifdef_stack_ptr = s1->ifdef_stack;
3591 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3592 pp_expr = 0;
3593 pp_counter = 0;
3594 pp_debug_tok = pp_debug_symv = 0;
3595 pp_once++;
3596 pvtop = vtop = vstack - 1;
3597 s1->pack_stack[0] = 0;
3598 s1->pack_stack_ptr = s1->pack_stack;
3600 set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
3601 set_idnum('.', is_asm ? IS_ID : 0);
3603 buf = tcc_malloc(3 + strlen(file->filename));
3604 sprintf(buf, "\"%s\"", file->filename);
3605 tcc_define_symbol(s1, "__BASE_FILE__", buf);
3606 tcc_free(buf);
3608 if (s1->nb_cmd_include_files) {
3609 CString cstr;
3610 int i;
3611 cstr_new(&cstr);
3612 for (i = 0; i < s1->nb_cmd_include_files; i++) {
3613 cstr_cat(&cstr, "#include \"", -1);
3614 cstr_cat(&cstr, s1->cmd_include_files[i], -1);
3615 cstr_cat(&cstr, "\"\n", -1);
3617 *s1->include_stack_ptr++ = file;
3618 tcc_open_bf(s1, "<command line>", cstr.size);
3619 memcpy(file->buffer, cstr.data, cstr.size);
3620 cstr_free(&cstr);
3623 if (is_asm)
3624 tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
3626 parse_flags = is_asm ? PARSE_FLAG_ASM_FILE : 0;
3627 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3630 /* cleanup from error/setjmp */
3631 ST_FUNC void preprocess_end(TCCState *s1)
3633 while (macro_stack)
3634 end_macro();
3635 macro_ptr = NULL;
3638 ST_FUNC void tccpp_new(TCCState *s)
3640 int i, c;
3641 const char *p, *r;
3643 /* might be used in error() before preprocess_start() */
3644 s->include_stack_ptr = s->include_stack;
3645 s->ppfp = stdout;
3647 /* init isid table */
3648 for(i = CH_EOF; i<128; i++)
3649 set_idnum(i,
3650 is_space(i) ? IS_SPC
3651 : isid(i) ? IS_ID
3652 : isnum(i) ? IS_NUM
3653 : 0);
3655 for(i = 128; i<256; i++)
3656 set_idnum(i, IS_ID);
3658 /* init allocators */
3659 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
3660 tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
3661 tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
3663 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3664 cstr_new(&cstr_buf);
3665 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
3666 tok_str_new(&tokstr_buf);
3667 tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
3669 tok_ident = TOK_IDENT;
3670 p = tcc_keywords;
3671 while (*p) {
3672 r = p;
3673 for(;;) {
3674 c = *r++;
3675 if (c == '\0')
3676 break;
3678 tok_alloc(p, r - p - 1);
3679 p = r;
3683 ST_FUNC void tccpp_delete(TCCState *s)
3685 int i, n;
3687 /* free -D and compiler defines */
3688 free_defines(NULL);
3690 /* free tokens */
3691 n = tok_ident - TOK_IDENT;
3692 for(i = 0; i < n; i++)
3693 tal_free(toksym_alloc, table_ident[i]);
3694 tcc_free(table_ident);
3695 table_ident = NULL;
3697 /* free static buffers */
3698 cstr_free(&tokcstr);
3699 cstr_free(&cstr_buf);
3700 cstr_free(&macro_equal_buf);
3701 tok_str_free_str(tokstr_buf.str);
3703 /* free allocators */
3704 tal_delete(toksym_alloc);
3705 toksym_alloc = NULL;
3706 tal_delete(tokstr_alloc);
3707 tokstr_alloc = NULL;
3708 tal_delete(cstr_alloc);
3709 cstr_alloc = NULL;
3712 /* ------------------------------------------------------------------------- */
3713 /* tcc -E [-P[1]] [-dD} support */
3715 static void tok_print(const char *msg, const int *str)
3717 FILE *fp;
3718 int t, s = 0;
3719 CValue cval;
3721 fp = tcc_state->ppfp;
3722 fprintf(fp, "%s", msg);
3723 while (str) {
3724 TOK_GET(&t, &str, &cval);
3725 if (!t)
3726 break;
3727 fprintf(fp, " %s" + s, get_tok_str(t, &cval)), s = 1;
3729 fprintf(fp, "\n");
3732 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3734 int d = f->line_num - f->line_ref;
3736 if (s1->dflag & 4)
3737 return;
3739 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3741 } else if (level == 0 && f->line_ref && d < 8) {
3742 while (d > 0)
3743 fputs("\n", s1->ppfp), --d;
3744 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3745 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3746 } else {
3747 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3748 level > 0 ? " 1" : level < 0 ? " 2" : "");
3750 f->line_ref = f->line_num;
3753 static void define_print(TCCState *s1, int v)
3755 FILE *fp;
3756 Sym *s;
3758 s = define_find(v);
3759 if (NULL == s || NULL == s->d)
3760 return;
3762 fp = s1->ppfp;
3763 fprintf(fp, "#define %s", get_tok_str(v, NULL));
3764 if (s->type.t == MACRO_FUNC) {
3765 Sym *a = s->next;
3766 fprintf(fp,"(");
3767 if (a)
3768 for (;;) {
3769 fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3770 if (!(a = a->next))
3771 break;
3772 fprintf(fp,",");
3774 fprintf(fp,")");
3776 tok_print("", s->d);
3779 static void pp_debug_defines(TCCState *s1)
3781 int v, t;
3782 const char *vs;
3783 FILE *fp;
3785 t = pp_debug_tok;
3786 if (t == 0)
3787 return;
3789 file->line_num--;
3790 pp_line(s1, file, 0);
3791 file->line_ref = ++file->line_num;
3793 fp = s1->ppfp;
3794 v = pp_debug_symv;
3795 vs = get_tok_str(v, NULL);
3796 if (t == TOK_DEFINE) {
3797 define_print(s1, v);
3798 } else if (t == TOK_UNDEF) {
3799 fprintf(fp, "#undef %s\n", vs);
3800 } else if (t == TOK_push_macro) {
3801 fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3802 } else if (t == TOK_pop_macro) {
3803 fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3805 pp_debug_tok = 0;
3808 static void pp_debug_builtins(TCCState *s1)
3810 int v;
3811 for (v = TOK_IDENT; v < tok_ident; ++v)
3812 define_print(s1, v);
3815 /* Add a space between tokens a and b to avoid unwanted textual pasting */
3816 static int pp_need_space(int a, int b)
3818 return 'E' == a ? '+' == b || '-' == b
3819 : '+' == a ? TOK_INC == b || '+' == b
3820 : '-' == a ? TOK_DEC == b || '-' == b
3821 : a >= TOK_IDENT ? b >= TOK_IDENT
3822 : a == TOK_PPNUM ? b >= TOK_IDENT
3823 : 0;
3826 /* maybe hex like 0x1e */
3827 static int pp_check_he0xE(int t, const char *p)
3829 if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
3830 return 'E';
3831 return t;
3834 /* Preprocess the current file */
3835 ST_FUNC int tcc_preprocess(TCCState *s1)
3837 BufferedFile **iptr;
3838 int token_seen, spcs, level;
3839 const char *p;
3840 char white[400];
3842 parse_flags = PARSE_FLAG_PREPROCESS
3843 | (parse_flags & PARSE_FLAG_ASM_FILE)
3844 | PARSE_FLAG_LINEFEED
3845 | PARSE_FLAG_SPACES
3846 | PARSE_FLAG_ACCEPT_STRAYS
3848 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3849 capability to compile and run itself, provided all numbers are
3850 given as decimals. tcc -E -P10 will do. */
3851 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_P10)
3852 parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
3854 #ifdef PP_BENCH
3855 /* for PP benchmarks */
3856 do next(); while (tok != TOK_EOF);
3857 return 0;
3858 #endif
3860 if (s1->dflag & 1) {
3861 pp_debug_builtins(s1);
3862 s1->dflag &= ~1;
3865 token_seen = TOK_LINEFEED, spcs = 0;
3866 pp_line(s1, file, 0);
3867 for (;;) {
3868 iptr = s1->include_stack_ptr;
3869 next();
3870 if (tok == TOK_EOF)
3871 break;
3873 level = s1->include_stack_ptr - iptr;
3874 if (level) {
3875 if (level > 0)
3876 pp_line(s1, *iptr, 0);
3877 pp_line(s1, file, level);
3879 if (s1->dflag & 7) {
3880 pp_debug_defines(s1);
3881 if (s1->dflag & 4)
3882 continue;
3885 if (is_space(tok)) {
3886 if (spcs < sizeof white - 1)
3887 white[spcs++] = tok;
3888 continue;
3889 } else if (tok == TOK_LINEFEED) {
3890 spcs = 0;
3891 if (token_seen == TOK_LINEFEED)
3892 continue;
3893 ++file->line_ref;
3894 } else if (token_seen == TOK_LINEFEED) {
3895 pp_line(s1, file, 0);
3896 } else if (spcs == 0 && pp_need_space(token_seen, tok)) {
3897 white[spcs++] = ' ';
3900 white[spcs] = 0, fputs(white, s1->ppfp), spcs = 0;
3901 fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
3902 token_seen = pp_check_he0xE(tok, p);
3904 return 0;
3907 /* ------------------------------------------------------------------------- */