nocode, noreturn
[tinycc.git] / tccpp.c
blob17f6a6093939a2bcee5259cae8d3a492346e46a9
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 '.','.', TOK_TWODOTS,
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 >= 0) {
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 return 1 + 1;
1021 case TOK_STR:
1022 case TOK_LSTR:
1023 case TOK_PPNUM:
1024 case TOK_PPSTR:
1025 return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
1026 case TOK_CLONG:
1027 case TOK_CULONG:
1028 return 1 + LONG_SIZE / 4;
1029 case TOK_CDOUBLE:
1030 case TOK_CLLONG:
1031 case TOK_CULLONG:
1032 return 1 + 2;
1033 case TOK_CLDOUBLE:
1034 return 1 + LDOUBLE_SIZE / 4;
1035 default:
1036 return 1 + 0;
1039 #endif
1041 /* token string handling */
1042 ST_INLN void tok_str_new(TokenString *s)
1044 s->str = NULL;
1045 s->len = s->lastlen = 0;
1046 s->allocated_len = 0;
1047 s->last_line_num = -1;
1050 ST_FUNC TokenString *tok_str_alloc(void)
1052 TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
1053 tok_str_new(str);
1054 return str;
1057 ST_FUNC int *tok_str_dup(TokenString *s)
1059 int *str;
1061 str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
1062 memcpy(str, s->str, s->len * sizeof(int));
1063 return str;
1066 ST_FUNC void tok_str_free_str(int *str)
1068 tal_free(tokstr_alloc, str);
1071 ST_FUNC void tok_str_free(TokenString *str)
1073 tok_str_free_str(str->str);
1074 tal_free(tokstr_alloc, str);
1077 ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
1079 int *str, size;
1081 size = s->allocated_len;
1082 if (size < 16)
1083 size = 16;
1084 while (size < new_size)
1085 size = size * 2;
1086 if (size > s->allocated_len) {
1087 str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
1088 s->allocated_len = size;
1089 s->str = str;
1091 return s->str;
1094 ST_FUNC void tok_str_add(TokenString *s, int t)
1096 int len, *str;
1098 len = s->len;
1099 str = s->str;
1100 if (len >= s->allocated_len)
1101 str = tok_str_realloc(s, len + 1);
1102 str[len++] = t;
1103 s->len = len;
1106 ST_FUNC void begin_macro(TokenString *str, int alloc)
1108 str->alloc = alloc;
1109 str->prev = macro_stack;
1110 str->prev_ptr = macro_ptr;
1111 str->save_line_num = file->line_num;
1112 macro_ptr = str->str;
1113 macro_stack = str;
1116 ST_FUNC void end_macro(void)
1118 TokenString *str = macro_stack;
1119 macro_stack = str->prev;
1120 macro_ptr = str->prev_ptr;
1121 file->line_num = str->save_line_num;
1122 if (str->alloc == 2) {
1123 str->alloc = 3; /* just mark as finished */
1124 } else {
1125 tok_str_free(str);
1129 static void tok_str_add2(TokenString *s, int t, CValue *cv)
1131 int len, *str;
1133 len = s->lastlen = s->len;
1134 str = s->str;
1136 /* allocate space for worst case */
1137 if (len + TOK_MAX_SIZE >= s->allocated_len)
1138 str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
1139 str[len++] = t;
1140 switch(t) {
1141 case TOK_CINT:
1142 case TOK_CUINT:
1143 case TOK_CCHAR:
1144 case TOK_LCHAR:
1145 case TOK_CFLOAT:
1146 case TOK_LINENUM:
1147 #if LONG_SIZE == 4
1148 case TOK_CLONG:
1149 case TOK_CULONG:
1150 #endif
1151 str[len++] = cv->tab[0];
1152 break;
1153 case TOK_PPNUM:
1154 case TOK_PPSTR:
1155 case TOK_STR:
1156 case TOK_LSTR:
1158 /* Insert the string into the int array. */
1159 size_t nb_words =
1160 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
1161 if (len + nb_words >= s->allocated_len)
1162 str = tok_str_realloc(s, len + nb_words + 1);
1163 str[len] = cv->str.size;
1164 memcpy(&str[len + 1], cv->str.data, cv->str.size);
1165 len += nb_words;
1167 break;
1168 case TOK_CDOUBLE:
1169 case TOK_CLLONG:
1170 case TOK_CULLONG:
1171 #if LONG_SIZE == 8
1172 case TOK_CLONG:
1173 case TOK_CULONG:
1174 #endif
1175 #if LDOUBLE_SIZE == 8
1176 case TOK_CLDOUBLE:
1177 #endif
1178 str[len++] = cv->tab[0];
1179 str[len++] = cv->tab[1];
1180 break;
1181 #if LDOUBLE_SIZE == 12
1182 case TOK_CLDOUBLE:
1183 str[len++] = cv->tab[0];
1184 str[len++] = cv->tab[1];
1185 str[len++] = cv->tab[2];
1186 #elif LDOUBLE_SIZE == 16
1187 case TOK_CLDOUBLE:
1188 str[len++] = cv->tab[0];
1189 str[len++] = cv->tab[1];
1190 str[len++] = cv->tab[2];
1191 str[len++] = cv->tab[3];
1192 #elif LDOUBLE_SIZE != 8
1193 #error add long double size support
1194 #endif
1195 break;
1196 default:
1197 break;
1199 s->len = len;
1202 /* add the current parse token in token string 's' */
1203 ST_FUNC void tok_str_add_tok(TokenString *s)
1205 CValue cval;
1207 /* save line number info */
1208 if (file->line_num != s->last_line_num) {
1209 s->last_line_num = file->line_num;
1210 cval.i = s->last_line_num;
1211 tok_str_add2(s, TOK_LINENUM, &cval);
1213 tok_str_add2(s, tok, &tokc);
1216 /* get a token from an integer array and increment pointer. */
1217 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
1219 const int *p = *pp;
1220 int n, *tab;
1222 tab = cv->tab;
1223 switch(*t = *p++) {
1224 #if LONG_SIZE == 4
1225 case TOK_CLONG:
1226 #endif
1227 case TOK_CINT:
1228 case TOK_CCHAR:
1229 case TOK_LCHAR:
1230 case TOK_LINENUM:
1231 cv->i = *p++;
1232 break;
1233 #if LONG_SIZE == 4
1234 case TOK_CULONG:
1235 #endif
1236 case TOK_CUINT:
1237 cv->i = (unsigned)*p++;
1238 break;
1239 case TOK_CFLOAT:
1240 tab[0] = *p++;
1241 break;
1242 case TOK_STR:
1243 case TOK_LSTR:
1244 case TOK_PPNUM:
1245 case TOK_PPSTR:
1246 cv->str.size = *p++;
1247 cv->str.data = p;
1248 p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
1249 break;
1250 case TOK_CDOUBLE:
1251 case TOK_CLLONG:
1252 case TOK_CULLONG:
1253 #if LONG_SIZE == 8
1254 case TOK_CLONG:
1255 case TOK_CULONG:
1256 #endif
1257 n = 2;
1258 goto copy;
1259 case TOK_CLDOUBLE:
1260 #if LDOUBLE_SIZE == 16
1261 n = 4;
1262 #elif LDOUBLE_SIZE == 12
1263 n = 3;
1264 #elif LDOUBLE_SIZE == 8
1265 n = 2;
1266 #else
1267 # error add long double size support
1268 #endif
1269 copy:
1271 *tab++ = *p++;
1272 while (--n);
1273 break;
1274 default:
1275 break;
1277 *pp = p;
1280 static int macro_is_equal(const int *a, const int *b)
1282 CValue cv;
1283 int t;
1285 if (!a || !b)
1286 return 1;
1288 while (*a && *b) {
1289 /* first time preallocate macro_equal_buf, next time only reset position to start */
1290 cstr_reset(&macro_equal_buf);
1291 TOK_GET(&t, &a, &cv);
1292 cstr_cat(&macro_equal_buf, get_tok_str(t, &cv), 0);
1293 TOK_GET(&t, &b, &cv);
1294 if (strcmp(macro_equal_buf.data, get_tok_str(t, &cv)))
1295 return 0;
1297 return !(*a || *b);
1300 /* defines handling */
1301 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1303 Sym *s, *o;
1305 o = define_find(v);
1306 s = sym_push2(&define_stack, v, macro_type, 0);
1307 s->d = str;
1308 s->next = first_arg;
1309 table_ident[v - TOK_IDENT]->sym_define = s;
1311 if (o && !macro_is_equal(o->d, s->d))
1312 tcc_warning("%s redefined", get_tok_str(v, NULL));
1315 /* undefined a define symbol. Its name is just set to zero */
1316 ST_FUNC void define_undef(Sym *s)
1318 int v = s->v;
1319 if (v >= TOK_IDENT && v < tok_ident)
1320 table_ident[v - TOK_IDENT]->sym_define = NULL;
1323 ST_INLN Sym *define_find(int v)
1325 v -= TOK_IDENT;
1326 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1327 return NULL;
1328 return table_ident[v]->sym_define;
1331 /* free define stack until top reaches 'b' */
1332 ST_FUNC void free_defines(Sym *b)
1334 while (define_stack != b) {
1335 Sym *top = define_stack;
1336 define_stack = top->prev;
1337 tok_str_free_str(top->d);
1338 define_undef(top);
1339 sym_free(top);
1342 /* restore remaining (-D or predefined) symbols if they were
1343 #undef'd in the file */
1344 while (b) {
1345 int v = b->v;
1346 if (v >= TOK_IDENT && v < tok_ident) {
1347 Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
1348 if (!*d)
1349 *d = b;
1351 b = b->prev;
1355 /* label lookup */
1356 ST_FUNC Sym *label_find(int v)
1358 v -= TOK_IDENT;
1359 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1360 return NULL;
1361 return table_ident[v]->sym_label;
1364 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1366 Sym *s, **ps;
1367 s = sym_push2(ptop, v, 0, 0);
1368 s->r = flags;
1369 ps = &table_ident[v - TOK_IDENT]->sym_label;
1370 if (ptop == &global_label_stack) {
1371 /* modify the top most local identifier, so that
1372 sym_identifier will point to 's' when popped */
1373 while (*ps != NULL)
1374 ps = &(*ps)->prev_tok;
1376 s->prev_tok = *ps;
1377 *ps = s;
1378 return s;
1381 /* pop labels until element last is reached. Look if any labels are
1382 undefined. Define symbols if '&&label' was used. */
1383 ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep)
1385 Sym *s, *s1;
1386 for(s = *ptop; s != slast; s = s1) {
1387 s1 = s->prev;
1388 if (s->r == LABEL_DECLARED) {
1389 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1390 } else if (s->r == LABEL_FORWARD) {
1391 tcc_error("label '%s' used but not defined",
1392 get_tok_str(s->v, NULL));
1393 } else {
1394 if (s->c) {
1395 /* define corresponding symbol. A size of
1396 1 is put. */
1397 put_extern_sym(s, cur_text_section, s->jnext, 1);
1400 /* remove label */
1401 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1402 if (!keep)
1403 sym_free(s);
1405 if (!keep)
1406 *ptop = slast;
1409 /* fake the nth "#if defined test_..." for tcc -dt -run */
1410 static void maybe_run_test(TCCState *s)
1412 const char *p;
1413 if (s->include_stack_ptr != s->include_stack)
1414 return;
1415 p = get_tok_str(tok, NULL);
1416 if (0 != memcmp(p, "test_", 5))
1417 return;
1418 if (0 != --s->run_test)
1419 return;
1420 fprintf(s->ppfp, "\n[%s]\n" + !(s->dflag & 32), p), fflush(s->ppfp);
1421 define_push(tok, MACRO_OBJ, NULL, NULL);
1424 /* eval an expression for #if/#elif */
1425 static int expr_preprocess(void)
1427 int c, t;
1428 TokenString *str;
1430 str = tok_str_alloc();
1431 pp_expr = 1;
1432 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1433 next(); /* do macro subst */
1434 if (tok == TOK_DEFINED) {
1435 next_nomacro();
1436 t = tok;
1437 if (t == '(')
1438 next_nomacro();
1439 if (tok < TOK_IDENT)
1440 expect("identifier");
1441 if (tcc_state->run_test)
1442 maybe_run_test(tcc_state);
1443 c = define_find(tok) != 0;
1444 if (t == '(') {
1445 next_nomacro();
1446 if (tok != ')')
1447 expect("')'");
1449 tok = TOK_CINT;
1450 tokc.i = c;
1451 } else if (tok >= TOK_IDENT) {
1452 /* if undefined macro */
1453 tok = TOK_CINT;
1454 tokc.i = 0;
1456 tok_str_add_tok(str);
1458 pp_expr = 0;
1459 tok_str_add(str, -1); /* simulate end of file */
1460 tok_str_add(str, 0);
1461 /* now evaluate C constant expression */
1462 begin_macro(str, 1);
1463 next();
1464 c = expr_const();
1465 end_macro();
1466 return c != 0;
1470 /* parse after #define */
1471 ST_FUNC void parse_define(void)
1473 Sym *s, *first, **ps;
1474 int v, t, varg, is_vaargs, spc;
1475 int saved_parse_flags = parse_flags;
1477 v = tok;
1478 if (v < TOK_IDENT || v == TOK_DEFINED)
1479 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1480 /* XXX: should check if same macro (ANSI) */
1481 first = NULL;
1482 t = MACRO_OBJ;
1483 /* We have to parse the whole define as if not in asm mode, in particular
1484 no line comment with '#' must be ignored. Also for function
1485 macros the argument list must be parsed without '.' being an ID
1486 character. */
1487 parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES);
1488 /* '(' must be just after macro definition for MACRO_FUNC */
1489 next_nomacro_spc();
1490 if (tok == '(') {
1491 int dotid = set_idnum('.', 0);
1492 next_nomacro();
1493 ps = &first;
1494 if (tok != ')') for (;;) {
1495 varg = tok;
1496 next_nomacro();
1497 is_vaargs = 0;
1498 if (varg == TOK_DOTS) {
1499 varg = TOK___VA_ARGS__;
1500 is_vaargs = 1;
1501 } else if (tok == TOK_DOTS && gnu_ext) {
1502 is_vaargs = 1;
1503 next_nomacro();
1505 if (varg < TOK_IDENT)
1506 bad_list:
1507 tcc_error("bad macro parameter list");
1508 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1509 *ps = s;
1510 ps = &s->next;
1511 if (tok == ')')
1512 break;
1513 if (tok != ',' || is_vaargs)
1514 goto bad_list;
1515 next_nomacro();
1517 next_nomacro_spc();
1518 t = MACRO_FUNC;
1519 set_idnum('.', dotid);
1522 tokstr_buf.len = 0;
1523 spc = 2;
1524 parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
1525 /* The body of a macro definition should be parsed such that identifiers
1526 are parsed like the file mode determines (i.e. with '.' being an
1527 ID character in asm mode). But '#' should be retained instead of
1528 regarded as line comment leader, so still don't set ASM_FILE
1529 in parse_flags. */
1530 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1531 /* remove spaces around ## and after '#' */
1532 if (TOK_TWOSHARPS == tok) {
1533 if (2 == spc)
1534 goto bad_twosharp;
1535 if (1 == spc)
1536 --tokstr_buf.len;
1537 spc = 3;
1538 tok = TOK_PPJOIN;
1539 } else if ('#' == tok) {
1540 spc = 4;
1541 } else if (check_space(tok, &spc)) {
1542 goto skip;
1544 tok_str_add2(&tokstr_buf, tok, &tokc);
1545 skip:
1546 next_nomacro_spc();
1549 parse_flags = saved_parse_flags;
1550 if (spc == 1)
1551 --tokstr_buf.len; /* remove trailing space */
1552 tok_str_add(&tokstr_buf, 0);
1553 if (3 == spc)
1554 bad_twosharp:
1555 tcc_error("'##' cannot appear at either end of macro");
1556 define_push(v, t, tok_str_dup(&tokstr_buf), first);
1559 static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
1561 const unsigned char *s;
1562 unsigned int h;
1563 CachedInclude *e;
1564 int i;
1566 h = TOK_HASH_INIT;
1567 s = (unsigned char *) filename;
1568 while (*s) {
1569 #ifdef _WIN32
1570 h = TOK_HASH_FUNC(h, toup(*s));
1571 #else
1572 h = TOK_HASH_FUNC(h, *s);
1573 #endif
1574 s++;
1576 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1578 i = s1->cached_includes_hash[h];
1579 for(;;) {
1580 if (i == 0)
1581 break;
1582 e = s1->cached_includes[i - 1];
1583 if (0 == PATHCMP(e->filename, filename))
1584 return e;
1585 i = e->hash_next;
1587 if (!add)
1588 return NULL;
1590 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1591 strcpy(e->filename, filename);
1592 e->ifndef_macro = e->once = 0;
1593 dynarray_add(&s1->cached_includes, &s1->nb_cached_includes, e);
1594 /* add in hash table */
1595 e->hash_next = s1->cached_includes_hash[h];
1596 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1597 #ifdef INC_DEBUG
1598 printf("adding cached '%s'\n", filename);
1599 #endif
1600 return e;
1603 static void pragma_parse(TCCState *s1)
1605 next_nomacro();
1606 if (tok == TOK_push_macro || tok == TOK_pop_macro) {
1607 int t = tok, v;
1608 Sym *s;
1610 if (next(), tok != '(')
1611 goto pragma_err;
1612 if (next(), tok != TOK_STR)
1613 goto pragma_err;
1614 v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
1615 if (next(), tok != ')')
1616 goto pragma_err;
1617 if (t == TOK_push_macro) {
1618 while (NULL == (s = define_find(v)))
1619 define_push(v, 0, NULL, NULL);
1620 s->type.ref = s; /* set push boundary */
1621 } else {
1622 for (s = define_stack; s; s = s->prev)
1623 if (s->v == v && s->type.ref == s) {
1624 s->type.ref = NULL;
1625 break;
1628 if (s)
1629 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
1630 else
1631 tcc_warning("unbalanced #pragma pop_macro");
1632 pp_debug_tok = t, pp_debug_symv = v;
1634 } else if (tok == TOK_once) {
1635 search_cached_include(s1, file->filename, 1)->once = pp_once;
1637 } else if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
1638 /* tcc -E: keep pragmas below unchanged */
1639 unget_tok(' ');
1640 unget_tok(TOK_PRAGMA);
1641 unget_tok('#');
1642 unget_tok(TOK_LINEFEED);
1644 } else if (tok == TOK_pack) {
1645 /* This may be:
1646 #pragma pack(1) // set
1647 #pragma pack() // reset to default
1648 #pragma pack(push,1) // push & set
1649 #pragma pack(pop) // restore previous */
1650 next();
1651 skip('(');
1652 if (tok == TOK_ASM_pop) {
1653 next();
1654 if (s1->pack_stack_ptr <= s1->pack_stack) {
1655 stk_error:
1656 tcc_error("out of pack stack");
1658 s1->pack_stack_ptr--;
1659 } else {
1660 int val = 0;
1661 if (tok != ')') {
1662 if (tok == TOK_ASM_push) {
1663 next();
1664 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1665 goto stk_error;
1666 s1->pack_stack_ptr++;
1667 skip(',');
1669 if (tok != TOK_CINT)
1670 goto pragma_err;
1671 val = tokc.i;
1672 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1673 goto pragma_err;
1674 next();
1676 *s1->pack_stack_ptr = val;
1678 if (tok != ')')
1679 goto pragma_err;
1681 } else if (tok == TOK_comment) {
1682 char *p; int t;
1683 next();
1684 skip('(');
1685 t = tok;
1686 next();
1687 skip(',');
1688 if (tok != TOK_STR)
1689 goto pragma_err;
1690 p = tcc_strdup((char *)tokc.str.data);
1691 next();
1692 if (tok != ')')
1693 goto pragma_err;
1694 if (t == TOK_lib) {
1695 dynarray_add(&s1->pragma_libs, &s1->nb_pragma_libs, p);
1696 } else {
1697 if (t == TOK_option)
1698 tcc_set_options(s1, p);
1699 tcc_free(p);
1702 } else if (s1->warn_unsupported) {
1703 tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
1705 return;
1707 pragma_err:
1708 tcc_error("malformed #pragma directive");
1709 return;
1712 /* is_bof is true if first non space token at beginning of file */
1713 ST_FUNC void preprocess(int is_bof)
1715 TCCState *s1 = tcc_state;
1716 int i, c, n, saved_parse_flags;
1717 char buf[1024], *q;
1718 Sym *s;
1720 saved_parse_flags = parse_flags;
1721 parse_flags = PARSE_FLAG_PREPROCESS
1722 | PARSE_FLAG_TOK_NUM
1723 | PARSE_FLAG_TOK_STR
1724 | PARSE_FLAG_LINEFEED
1725 | (parse_flags & PARSE_FLAG_ASM_FILE)
1728 next_nomacro();
1729 redo:
1730 switch(tok) {
1731 case TOK_DEFINE:
1732 pp_debug_tok = tok;
1733 next_nomacro();
1734 pp_debug_symv = tok;
1735 parse_define();
1736 break;
1737 case TOK_UNDEF:
1738 pp_debug_tok = tok;
1739 next_nomacro();
1740 pp_debug_symv = tok;
1741 s = define_find(tok);
1742 /* undefine symbol by putting an invalid name */
1743 if (s)
1744 define_undef(s);
1745 break;
1746 case TOK_INCLUDE:
1747 case TOK_INCLUDE_NEXT:
1748 ch = file->buf_ptr[0];
1749 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1750 skip_spaces();
1751 if (ch == '<') {
1752 c = '>';
1753 goto read_name;
1754 } else if (ch == '\"') {
1755 c = ch;
1756 read_name:
1757 inp();
1758 q = buf;
1759 while (ch != c && ch != '\n' && ch != CH_EOF) {
1760 if ((q - buf) < sizeof(buf) - 1)
1761 *q++ = ch;
1762 if (ch == '\\') {
1763 if (handle_stray_noerror() == 0)
1764 --q;
1765 } else
1766 inp();
1768 *q = '\0';
1769 minp();
1770 #if 0
1771 /* eat all spaces and comments after include */
1772 /* XXX: slightly incorrect */
1773 while (ch1 != '\n' && ch1 != CH_EOF)
1774 inp();
1775 #endif
1776 } else {
1777 int len;
1778 /* computed #include : concatenate everything up to linefeed,
1779 the result must be one of the two accepted forms.
1780 Don't convert pp-tokens to tokens here. */
1781 parse_flags = (PARSE_FLAG_PREPROCESS
1782 | PARSE_FLAG_LINEFEED
1783 | (parse_flags & PARSE_FLAG_ASM_FILE));
1784 next();
1785 buf[0] = '\0';
1786 while (tok != TOK_LINEFEED) {
1787 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1788 next();
1790 len = strlen(buf);
1791 /* check syntax and remove '<>|""' */
1792 if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') &&
1793 (buf[0] != '<' || buf[len-1] != '>'))))
1794 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1795 c = buf[len-1];
1796 memmove(buf, buf + 1, len - 2);
1797 buf[len - 2] = '\0';
1800 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1801 tcc_error("#include recursion too deep");
1802 /* store current file in stack, but increment stack later below */
1803 *s1->include_stack_ptr = file;
1804 i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
1805 n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
1806 for (; i < n; ++i) {
1807 char buf1[sizeof file->filename];
1808 CachedInclude *e;
1809 const char *path;
1811 if (i == 0) {
1812 /* check absolute include path */
1813 if (!IS_ABSPATH(buf))
1814 continue;
1815 buf1[0] = 0;
1817 } else if (i == 1) {
1818 /* search in file's dir if "header.h" */
1819 if (c != '\"')
1820 continue;
1821 /* https://savannah.nongnu.org/bugs/index.php?50847 */
1822 path = file->true_filename;
1823 pstrncpy(buf1, path, tcc_basename(path) - path);
1825 } else {
1826 /* search in all the include paths */
1827 int j = i - 2, k = j - s1->nb_include_paths;
1828 path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
1829 pstrcpy(buf1, sizeof(buf1), path);
1830 pstrcat(buf1, sizeof(buf1), "/");
1833 pstrcat(buf1, sizeof(buf1), buf);
1834 e = search_cached_include(s1, buf1, 0);
1835 if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
1836 /* no need to parse the include because the 'ifndef macro'
1837 is defined (or had #pragma once) */
1838 #ifdef INC_DEBUG
1839 printf("%s: skipping cached %s\n", file->filename, buf1);
1840 #endif
1841 goto include_done;
1844 if (tcc_open(s1, buf1) < 0)
1845 continue;
1847 file->include_next_index = i + 1;
1848 #ifdef INC_DEBUG
1849 printf("%s: including %s\n", file->prev->filename, file->filename);
1850 #endif
1851 /* update target deps */
1852 dynarray_add(&s1->target_deps, &s1->nb_target_deps,
1853 tcc_strdup(buf1));
1854 /* push current file in stack */
1855 ++s1->include_stack_ptr;
1856 /* add include file debug info */
1857 if (s1->do_debug)
1858 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1859 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1860 ch = file->buf_ptr[0];
1861 goto the_end;
1863 tcc_error("include file '%s' not found", buf);
1864 include_done:
1865 break;
1866 case TOK_IFNDEF:
1867 c = 1;
1868 goto do_ifdef;
1869 case TOK_IF:
1870 c = expr_preprocess();
1871 goto do_if;
1872 case TOK_IFDEF:
1873 c = 0;
1874 do_ifdef:
1875 next_nomacro();
1876 if (tok < TOK_IDENT)
1877 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1878 if (is_bof) {
1879 if (c) {
1880 #ifdef INC_DEBUG
1881 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1882 #endif
1883 file->ifndef_macro = tok;
1886 c = (define_find(tok) != 0) ^ c;
1887 do_if:
1888 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1889 tcc_error("memory full (ifdef)");
1890 *s1->ifdef_stack_ptr++ = c;
1891 goto test_skip;
1892 case TOK_ELSE:
1893 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1894 tcc_error("#else without matching #if");
1895 if (s1->ifdef_stack_ptr[-1] & 2)
1896 tcc_error("#else after #else");
1897 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1898 goto test_else;
1899 case TOK_ELIF:
1900 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1901 tcc_error("#elif without matching #if");
1902 c = s1->ifdef_stack_ptr[-1];
1903 if (c > 1)
1904 tcc_error("#elif after #else");
1905 /* last #if/#elif expression was true: we skip */
1906 if (c == 1) {
1907 c = 0;
1908 } else {
1909 c = expr_preprocess();
1910 s1->ifdef_stack_ptr[-1] = c;
1912 test_else:
1913 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1914 file->ifndef_macro = 0;
1915 test_skip:
1916 if (!(c & 1)) {
1917 preprocess_skip();
1918 is_bof = 0;
1919 goto redo;
1921 break;
1922 case TOK_ENDIF:
1923 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1924 tcc_error("#endif without matching #if");
1925 s1->ifdef_stack_ptr--;
1926 /* '#ifndef macro' was at the start of file. Now we check if
1927 an '#endif' is exactly at the end of file */
1928 if (file->ifndef_macro &&
1929 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1930 file->ifndef_macro_saved = file->ifndef_macro;
1931 /* need to set to zero to avoid false matches if another
1932 #ifndef at middle of file */
1933 file->ifndef_macro = 0;
1934 while (tok != TOK_LINEFEED)
1935 next_nomacro();
1936 tok_flags |= TOK_FLAG_ENDIF;
1937 goto the_end;
1939 break;
1940 case TOK_PPNUM:
1941 n = strtoul((char*)tokc.str.data, &q, 10);
1942 goto _line_num;
1943 case TOK_LINE:
1944 next();
1945 if (tok != TOK_CINT)
1946 _line_err:
1947 tcc_error("wrong #line format");
1948 n = tokc.i;
1949 _line_num:
1950 next();
1951 if (tok != TOK_LINEFEED) {
1952 if (tok == TOK_STR) {
1953 if (file->true_filename == file->filename)
1954 file->true_filename = tcc_strdup(file->filename);
1955 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
1956 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
1957 break;
1958 else
1959 goto _line_err;
1960 --n;
1962 if (file->fd > 0)
1963 total_lines += file->line_num - n;
1964 file->line_num = n;
1965 if (s1->do_debug)
1966 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1967 break;
1968 case TOK_ERROR:
1969 case TOK_WARNING:
1970 c = tok;
1971 ch = file->buf_ptr[0];
1972 skip_spaces();
1973 q = buf;
1974 while (ch != '\n' && ch != CH_EOF) {
1975 if ((q - buf) < sizeof(buf) - 1)
1976 *q++ = ch;
1977 if (ch == '\\') {
1978 if (handle_stray_noerror() == 0)
1979 --q;
1980 } else
1981 inp();
1983 *q = '\0';
1984 if (c == TOK_ERROR)
1985 tcc_error("#error %s", buf);
1986 else
1987 tcc_warning("#warning %s", buf);
1988 break;
1989 case TOK_PRAGMA:
1990 pragma_parse(s1);
1991 break;
1992 case TOK_LINEFEED:
1993 goto the_end;
1994 default:
1995 /* ignore gas line comment in an 'S' file. */
1996 if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
1997 goto ignore;
1998 if (tok == '!' && is_bof)
1999 /* '!' is ignored at beginning to allow C scripts. */
2000 goto ignore;
2001 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
2002 ignore:
2003 file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
2004 goto the_end;
2006 /* ignore other preprocess commands or #! for C scripts */
2007 while (tok != TOK_LINEFEED)
2008 next_nomacro();
2009 the_end:
2010 parse_flags = saved_parse_flags;
2013 /* evaluate escape codes in a string. */
2014 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2016 int c, n;
2017 const uint8_t *p;
2019 p = buf;
2020 for(;;) {
2021 c = *p;
2022 if (c == '\0')
2023 break;
2024 if (c == '\\') {
2025 p++;
2026 /* escape */
2027 c = *p;
2028 switch(c) {
2029 case '0': case '1': case '2': case '3':
2030 case '4': case '5': case '6': case '7':
2031 /* at most three octal digits */
2032 n = c - '0';
2033 p++;
2034 c = *p;
2035 if (isoct(c)) {
2036 n = n * 8 + c - '0';
2037 p++;
2038 c = *p;
2039 if (isoct(c)) {
2040 n = n * 8 + c - '0';
2041 p++;
2044 c = n;
2045 goto add_char_nonext;
2046 case 'x':
2047 case 'u':
2048 case 'U':
2049 p++;
2050 n = 0;
2051 for(;;) {
2052 c = *p;
2053 if (c >= 'a' && c <= 'f')
2054 c = c - 'a' + 10;
2055 else if (c >= 'A' && c <= 'F')
2056 c = c - 'A' + 10;
2057 else if (isnum(c))
2058 c = c - '0';
2059 else
2060 break;
2061 n = n * 16 + c;
2062 p++;
2064 c = n;
2065 goto add_char_nonext;
2066 case 'a':
2067 c = '\a';
2068 break;
2069 case 'b':
2070 c = '\b';
2071 break;
2072 case 'f':
2073 c = '\f';
2074 break;
2075 case 'n':
2076 c = '\n';
2077 break;
2078 case 'r':
2079 c = '\r';
2080 break;
2081 case 't':
2082 c = '\t';
2083 break;
2084 case 'v':
2085 c = '\v';
2086 break;
2087 case 'e':
2088 if (!gnu_ext)
2089 goto invalid_escape;
2090 c = 27;
2091 break;
2092 case '\'':
2093 case '\"':
2094 case '\\':
2095 case '?':
2096 break;
2097 default:
2098 invalid_escape:
2099 if (c >= '!' && c <= '~')
2100 tcc_warning("unknown escape sequence: \'\\%c\'", c);
2101 else
2102 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
2103 break;
2105 } else if (is_long && c >= 0x80) {
2106 /* assume we are processing UTF-8 sequence */
2107 /* reference: The Unicode Standard, Version 10.0, ch3.9 */
2109 int cont; /* count of continuation bytes */
2110 int skip; /* how many bytes should skip when error occurred */
2111 int i;
2113 /* decode leading byte */
2114 if (c < 0xC2) {
2115 skip = 1; goto invalid_utf8_sequence;
2116 } else if (c <= 0xDF) {
2117 cont = 1; n = c & 0x1f;
2118 } else if (c <= 0xEF) {
2119 cont = 2; n = c & 0xf;
2120 } else if (c <= 0xF4) {
2121 cont = 3; n = c & 0x7;
2122 } else {
2123 skip = 1; goto invalid_utf8_sequence;
2126 /* decode continuation bytes */
2127 for (i = 1; i <= cont; i++) {
2128 int l = 0x80, h = 0xBF;
2130 /* adjust limit for second byte */
2131 if (i == 1) {
2132 switch (c) {
2133 case 0xE0: l = 0xA0; break;
2134 case 0xED: h = 0x9F; break;
2135 case 0xF0: l = 0x90; break;
2136 case 0xF4: h = 0x8F; break;
2140 if (p[i] < l || p[i] > h) {
2141 skip = i; goto invalid_utf8_sequence;
2144 n = (n << 6) | (p[i] & 0x3f);
2147 /* advance pointer */
2148 p += 1 + cont;
2149 c = n;
2150 goto add_char_nonext;
2152 /* error handling */
2153 invalid_utf8_sequence:
2154 tcc_warning("ill-formed UTF-8 subsequence starting with: \'\\x%x\'", c);
2155 c = 0xFFFD;
2156 p += skip;
2157 goto add_char_nonext;
2160 p++;
2161 add_char_nonext:
2162 if (!is_long)
2163 cstr_ccat(outstr, c);
2164 else {
2165 #ifdef TCC_TARGET_PE
2166 /* store as UTF-16 */
2167 if (c < 0x10000) {
2168 cstr_wccat(outstr, c);
2169 } else {
2170 c -= 0x10000;
2171 cstr_wccat(outstr, (c >> 10) + 0xD800);
2172 cstr_wccat(outstr, (c & 0x3FF) + 0xDC00);
2174 #else
2175 cstr_wccat(outstr, c);
2176 #endif
2179 /* add a trailing '\0' */
2180 if (!is_long)
2181 cstr_ccat(outstr, '\0');
2182 else
2183 cstr_wccat(outstr, '\0');
2186 static void parse_string(const char *s, int len)
2188 uint8_t buf[1000], *p = buf;
2189 int is_long, sep;
2191 if ((is_long = *s == 'L'))
2192 ++s, --len;
2193 sep = *s++;
2194 len -= 2;
2195 if (len >= sizeof buf)
2196 p = tcc_malloc(len + 1);
2197 memcpy(p, s, len);
2198 p[len] = 0;
2200 cstr_reset(&tokcstr);
2201 parse_escape_string(&tokcstr, p, is_long);
2202 if (p != buf)
2203 tcc_free(p);
2205 if (sep == '\'') {
2206 int char_size, i, n, c;
2207 /* XXX: make it portable */
2208 if (!is_long)
2209 tok = TOK_CCHAR, char_size = 1;
2210 else
2211 tok = TOK_LCHAR, char_size = sizeof(nwchar_t);
2212 n = tokcstr.size / char_size - 1;
2213 if (n < 1)
2214 tcc_error("empty character constant");
2215 if (n > 1)
2216 tcc_warning("multi-character character constant");
2217 for (c = i = 0; i < n; ++i) {
2218 if (is_long)
2219 c = ((nwchar_t *)tokcstr.data)[i];
2220 else
2221 c = (c << 8) | ((char *)tokcstr.data)[i];
2223 tokc.i = c;
2224 } else {
2225 tokc.str.size = tokcstr.size;
2226 tokc.str.data = tokcstr.data;
2227 if (!is_long)
2228 tok = TOK_STR;
2229 else
2230 tok = TOK_LSTR;
2234 /* we use 64 bit numbers */
2235 #define BN_SIZE 2
2237 /* bn = (bn << shift) | or_val */
2238 static void bn_lshift(unsigned int *bn, int shift, int or_val)
2240 int i;
2241 unsigned int v;
2242 for(i=0;i<BN_SIZE;i++) {
2243 v = bn[i];
2244 bn[i] = (v << shift) | or_val;
2245 or_val = v >> (32 - shift);
2249 static void bn_zero(unsigned int *bn)
2251 int i;
2252 for(i=0;i<BN_SIZE;i++) {
2253 bn[i] = 0;
2257 /* parse number in null terminated string 'p' and return it in the
2258 current token */
2259 static void parse_number(const char *p)
2261 int b, t, shift, frac_bits, s, exp_val, ch;
2262 char *q;
2263 unsigned int bn[BN_SIZE];
2264 double d;
2266 /* number */
2267 q = token_buf;
2268 ch = *p++;
2269 t = ch;
2270 ch = *p++;
2271 *q++ = t;
2272 b = 10;
2273 if (t == '.') {
2274 goto float_frac_parse;
2275 } else if (t == '0') {
2276 if (ch == 'x' || ch == 'X') {
2277 q--;
2278 ch = *p++;
2279 b = 16;
2280 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2281 q--;
2282 ch = *p++;
2283 b = 2;
2286 /* parse all digits. cannot check octal numbers at this stage
2287 because of floating point constants */
2288 while (1) {
2289 if (ch >= 'a' && ch <= 'f')
2290 t = ch - 'a' + 10;
2291 else if (ch >= 'A' && ch <= 'F')
2292 t = ch - 'A' + 10;
2293 else if (isnum(ch))
2294 t = ch - '0';
2295 else
2296 break;
2297 if (t >= b)
2298 break;
2299 if (q >= token_buf + STRING_MAX_SIZE) {
2300 num_too_long:
2301 tcc_error("number too long");
2303 *q++ = ch;
2304 ch = *p++;
2306 if (ch == '.' ||
2307 ((ch == 'e' || ch == 'E') && b == 10) ||
2308 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2309 if (b != 10) {
2310 /* NOTE: strtox should support that for hexa numbers, but
2311 non ISOC99 libcs do not support it, so we prefer to do
2312 it by hand */
2313 /* hexadecimal or binary floats */
2314 /* XXX: handle overflows */
2315 *q = '\0';
2316 if (b == 16)
2317 shift = 4;
2318 else
2319 shift = 1;
2320 bn_zero(bn);
2321 q = token_buf;
2322 while (1) {
2323 t = *q++;
2324 if (t == '\0') {
2325 break;
2326 } else if (t >= 'a') {
2327 t = t - 'a' + 10;
2328 } else if (t >= 'A') {
2329 t = t - 'A' + 10;
2330 } else {
2331 t = t - '0';
2333 bn_lshift(bn, shift, t);
2335 frac_bits = 0;
2336 if (ch == '.') {
2337 ch = *p++;
2338 while (1) {
2339 t = ch;
2340 if (t >= 'a' && t <= 'f') {
2341 t = t - 'a' + 10;
2342 } else if (t >= 'A' && t <= 'F') {
2343 t = t - 'A' + 10;
2344 } else if (t >= '0' && t <= '9') {
2345 t = t - '0';
2346 } else {
2347 break;
2349 if (t >= b)
2350 tcc_error("invalid digit");
2351 bn_lshift(bn, shift, t);
2352 frac_bits += shift;
2353 ch = *p++;
2356 if (ch != 'p' && ch != 'P')
2357 expect("exponent");
2358 ch = *p++;
2359 s = 1;
2360 exp_val = 0;
2361 if (ch == '+') {
2362 ch = *p++;
2363 } else if (ch == '-') {
2364 s = -1;
2365 ch = *p++;
2367 if (ch < '0' || ch > '9')
2368 expect("exponent digits");
2369 while (ch >= '0' && ch <= '9') {
2370 exp_val = exp_val * 10 + ch - '0';
2371 ch = *p++;
2373 exp_val = exp_val * s;
2375 /* now we can generate the number */
2376 /* XXX: should patch directly float number */
2377 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2378 d = ldexp(d, exp_val - frac_bits);
2379 t = toup(ch);
2380 if (t == 'F') {
2381 ch = *p++;
2382 tok = TOK_CFLOAT;
2383 /* float : should handle overflow */
2384 tokc.f = (float)d;
2385 } else if (t == 'L') {
2386 ch = *p++;
2387 #ifdef TCC_TARGET_PE
2388 tok = TOK_CDOUBLE;
2389 tokc.d = d;
2390 #else
2391 tok = TOK_CLDOUBLE;
2392 /* XXX: not large enough */
2393 tokc.ld = (long double)d;
2394 #endif
2395 } else {
2396 tok = TOK_CDOUBLE;
2397 tokc.d = d;
2399 } else {
2400 /* decimal floats */
2401 if (ch == '.') {
2402 if (q >= token_buf + STRING_MAX_SIZE)
2403 goto num_too_long;
2404 *q++ = ch;
2405 ch = *p++;
2406 float_frac_parse:
2407 while (ch >= '0' && ch <= '9') {
2408 if (q >= token_buf + STRING_MAX_SIZE)
2409 goto num_too_long;
2410 *q++ = ch;
2411 ch = *p++;
2414 if (ch == 'e' || ch == 'E') {
2415 if (q >= token_buf + STRING_MAX_SIZE)
2416 goto num_too_long;
2417 *q++ = ch;
2418 ch = *p++;
2419 if (ch == '-' || ch == '+') {
2420 if (q >= token_buf + STRING_MAX_SIZE)
2421 goto num_too_long;
2422 *q++ = ch;
2423 ch = *p++;
2425 if (ch < '0' || ch > '9')
2426 expect("exponent digits");
2427 while (ch >= '0' && ch <= '9') {
2428 if (q >= token_buf + STRING_MAX_SIZE)
2429 goto num_too_long;
2430 *q++ = ch;
2431 ch = *p++;
2434 *q = '\0';
2435 t = toup(ch);
2436 errno = 0;
2437 if (t == 'F') {
2438 ch = *p++;
2439 tok = TOK_CFLOAT;
2440 tokc.f = strtof(token_buf, NULL);
2441 } else if (t == 'L') {
2442 ch = *p++;
2443 #ifdef TCC_TARGET_PE
2444 tok = TOK_CDOUBLE;
2445 tokc.d = strtod(token_buf, NULL);
2446 #else
2447 tok = TOK_CLDOUBLE;
2448 tokc.ld = strtold(token_buf, NULL);
2449 #endif
2450 } else {
2451 tok = TOK_CDOUBLE;
2452 tokc.d = strtod(token_buf, NULL);
2455 } else {
2456 unsigned long long n, n1;
2457 int lcount, ucount, ov = 0;
2458 const char *p1;
2460 /* integer number */
2461 *q = '\0';
2462 q = token_buf;
2463 if (b == 10 && *q == '0') {
2464 b = 8;
2465 q++;
2467 n = 0;
2468 while(1) {
2469 t = *q++;
2470 /* no need for checks except for base 10 / 8 errors */
2471 if (t == '\0')
2472 break;
2473 else if (t >= 'a')
2474 t = t - 'a' + 10;
2475 else if (t >= 'A')
2476 t = t - 'A' + 10;
2477 else
2478 t = t - '0';
2479 if (t >= b)
2480 tcc_error("invalid digit");
2481 n1 = n;
2482 n = n * b + t;
2483 /* detect overflow */
2484 if (n1 >= 0x1000000000000000ULL && n / b != n1)
2485 ov = 1;
2488 /* Determine the characteristics (unsigned and/or 64bit) the type of
2489 the constant must have according to the constant suffix(es) */
2490 lcount = ucount = 0;
2491 p1 = p;
2492 for(;;) {
2493 t = toup(ch);
2494 if (t == 'L') {
2495 if (lcount >= 2)
2496 tcc_error("three 'l's in integer constant");
2497 if (lcount && *(p - 1) != ch)
2498 tcc_error("incorrect integer suffix: %s", p1);
2499 lcount++;
2500 ch = *p++;
2501 } else if (t == 'U') {
2502 if (ucount >= 1)
2503 tcc_error("two 'u's in integer constant");
2504 ucount++;
2505 ch = *p++;
2506 } else {
2507 break;
2511 /* Determine if it needs 64 bits and/or unsigned in order to fit */
2512 if (ucount == 0 && b == 10) {
2513 if (lcount <= (LONG_SIZE == 4)) {
2514 if (n >= 0x80000000U)
2515 lcount = (LONG_SIZE == 4) + 1;
2517 if (n >= 0x8000000000000000ULL)
2518 ov = 1, ucount = 1;
2519 } else {
2520 if (lcount <= (LONG_SIZE == 4)) {
2521 if (n >= 0x100000000ULL)
2522 lcount = (LONG_SIZE == 4) + 1;
2523 else if (n >= 0x80000000U)
2524 ucount = 1;
2526 if (n >= 0x8000000000000000ULL)
2527 ucount = 1;
2530 if (ov)
2531 tcc_warning("integer constant overflow");
2533 tok = TOK_CINT;
2534 if (lcount) {
2535 tok = TOK_CLONG;
2536 if (lcount == 2)
2537 tok = TOK_CLLONG;
2539 if (ucount)
2540 ++tok; /* TOK_CU... */
2541 tokc.i = n;
2543 if (ch)
2544 tcc_error("invalid number\n");
2548 #define PARSE2(c1, tok1, c2, tok2) \
2549 case c1: \
2550 PEEKC(c, p); \
2551 if (c == c2) { \
2552 p++; \
2553 tok = tok2; \
2554 } else { \
2555 tok = tok1; \
2557 break;
2559 /* return next token without macro substitution */
2560 static inline void next_nomacro1(void)
2562 int t, c, is_long, len;
2563 TokenSym *ts;
2564 uint8_t *p, *p1;
2565 unsigned int h;
2567 p = file->buf_ptr;
2568 redo_no_start:
2569 c = *p;
2570 switch(c) {
2571 case ' ':
2572 case '\t':
2573 tok = c;
2574 p++;
2575 if (parse_flags & PARSE_FLAG_SPACES)
2576 goto keep_tok_flags;
2577 while (isidnum_table[*p - CH_EOF] & IS_SPC)
2578 ++p;
2579 goto redo_no_start;
2580 case '\f':
2581 case '\v':
2582 case '\r':
2583 p++;
2584 goto redo_no_start;
2585 case '\\':
2586 /* first look if it is in fact an end of buffer */
2587 c = handle_stray1(p);
2588 p = file->buf_ptr;
2589 if (c == '\\')
2590 goto parse_simple;
2591 if (c != CH_EOF)
2592 goto redo_no_start;
2594 TCCState *s1 = tcc_state;
2595 if ((parse_flags & PARSE_FLAG_LINEFEED)
2596 && !(tok_flags & TOK_FLAG_EOF)) {
2597 tok_flags |= TOK_FLAG_EOF;
2598 tok = TOK_LINEFEED;
2599 goto keep_tok_flags;
2600 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2601 tok = TOK_EOF;
2602 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2603 tcc_error("missing #endif");
2604 } else if (s1->include_stack_ptr == s1->include_stack) {
2605 /* no include left : end of file. */
2606 tok = TOK_EOF;
2607 } else {
2608 tok_flags &= ~TOK_FLAG_EOF;
2609 /* pop include file */
2611 /* test if previous '#endif' was after a #ifdef at
2612 start of file */
2613 if (tok_flags & TOK_FLAG_ENDIF) {
2614 #ifdef INC_DEBUG
2615 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2616 #endif
2617 search_cached_include(s1, file->filename, 1)
2618 ->ifndef_macro = file->ifndef_macro_saved;
2619 tok_flags &= ~TOK_FLAG_ENDIF;
2622 /* add end of include file debug info */
2623 if (tcc_state->do_debug) {
2624 put_stabd(N_EINCL, 0, 0);
2626 /* pop include stack */
2627 tcc_close();
2628 s1->include_stack_ptr--;
2629 p = file->buf_ptr;
2630 if (p == file->buffer)
2631 tok_flags = TOK_FLAG_BOF|TOK_FLAG_BOL;
2632 goto redo_no_start;
2635 break;
2637 case '\n':
2638 file->line_num++;
2639 tok_flags |= TOK_FLAG_BOL;
2640 p++;
2641 maybe_newline:
2642 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2643 goto redo_no_start;
2644 tok = TOK_LINEFEED;
2645 goto keep_tok_flags;
2647 case '#':
2648 /* XXX: simplify */
2649 PEEKC(c, p);
2650 if ((tok_flags & TOK_FLAG_BOL) &&
2651 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2652 file->buf_ptr = p;
2653 preprocess(tok_flags & TOK_FLAG_BOF);
2654 p = file->buf_ptr;
2655 goto maybe_newline;
2656 } else {
2657 if (c == '#') {
2658 p++;
2659 tok = TOK_TWOSHARPS;
2660 } else {
2661 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2662 p = parse_line_comment(p - 1);
2663 goto redo_no_start;
2664 } else {
2665 tok = '#';
2669 break;
2671 /* dollar is allowed to start identifiers when not parsing asm */
2672 case '$':
2673 if (!(isidnum_table[c - CH_EOF] & IS_ID)
2674 || (parse_flags & PARSE_FLAG_ASM_FILE))
2675 goto parse_simple;
2677 case 'a': case 'b': case 'c': case 'd':
2678 case 'e': case 'f': case 'g': case 'h':
2679 case 'i': case 'j': case 'k': case 'l':
2680 case 'm': case 'n': case 'o': case 'p':
2681 case 'q': case 'r': case 's': case 't':
2682 case 'u': case 'v': case 'w': case 'x':
2683 case 'y': case 'z':
2684 case 'A': case 'B': case 'C': case 'D':
2685 case 'E': case 'F': case 'G': case 'H':
2686 case 'I': case 'J': case 'K':
2687 case 'M': case 'N': case 'O': case 'P':
2688 case 'Q': case 'R': case 'S': case 'T':
2689 case 'U': case 'V': case 'W': case 'X':
2690 case 'Y': case 'Z':
2691 case '_':
2692 parse_ident_fast:
2693 p1 = p;
2694 h = TOK_HASH_INIT;
2695 h = TOK_HASH_FUNC(h, c);
2696 while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2697 h = TOK_HASH_FUNC(h, c);
2698 len = p - p1;
2699 if (c != '\\') {
2700 TokenSym **pts;
2702 /* fast case : no stray found, so we have the full token
2703 and we have already hashed it */
2704 h &= (TOK_HASH_SIZE - 1);
2705 pts = &hash_ident[h];
2706 for(;;) {
2707 ts = *pts;
2708 if (!ts)
2709 break;
2710 if (ts->len == len && !memcmp(ts->str, p1, len))
2711 goto token_found;
2712 pts = &(ts->hash_next);
2714 ts = tok_alloc_new(pts, (char *) p1, len);
2715 token_found: ;
2716 } else {
2717 /* slower case */
2718 cstr_reset(&tokcstr);
2719 cstr_cat(&tokcstr, (char *) p1, len);
2720 p--;
2721 PEEKC(c, p);
2722 parse_ident_slow:
2723 while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2725 cstr_ccat(&tokcstr, c);
2726 PEEKC(c, p);
2728 ts = tok_alloc(tokcstr.data, tokcstr.size);
2730 tok = ts->tok;
2731 break;
2732 case 'L':
2733 t = p[1];
2734 if (t != '\\' && t != '\'' && t != '\"') {
2735 /* fast case */
2736 goto parse_ident_fast;
2737 } else {
2738 PEEKC(c, p);
2739 if (c == '\'' || c == '\"') {
2740 is_long = 1;
2741 goto str_const;
2742 } else {
2743 cstr_reset(&tokcstr);
2744 cstr_ccat(&tokcstr, 'L');
2745 goto parse_ident_slow;
2748 break;
2750 case '0': case '1': case '2': case '3':
2751 case '4': case '5': case '6': case '7':
2752 case '8': case '9':
2753 t = c;
2754 PEEKC(c, p);
2755 /* after the first digit, accept digits, alpha, '.' or sign if
2756 prefixed by 'eEpP' */
2757 parse_num:
2758 cstr_reset(&tokcstr);
2759 for(;;) {
2760 cstr_ccat(&tokcstr, t);
2761 if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
2762 || c == '.'
2763 || ((c == '+' || c == '-')
2764 && (((t == 'e' || t == 'E')
2765 && !(parse_flags & PARSE_FLAG_ASM_FILE
2766 /* 0xe+1 is 3 tokens in asm */
2767 && ((char*)tokcstr.data)[0] == '0'
2768 && toup(((char*)tokcstr.data)[1]) == 'X'))
2769 || t == 'p' || t == 'P'))))
2770 break;
2771 t = c;
2772 PEEKC(c, p);
2774 /* We add a trailing '\0' to ease parsing */
2775 cstr_ccat(&tokcstr, '\0');
2776 tokc.str.size = tokcstr.size;
2777 tokc.str.data = tokcstr.data;
2778 tok = TOK_PPNUM;
2779 break;
2781 case '.':
2782 /* special dot handling because it can also start a number */
2783 PEEKC(c, p);
2784 if (isnum(c)) {
2785 t = '.';
2786 goto parse_num;
2787 } else if ((isidnum_table['.' - CH_EOF] & IS_ID)
2788 && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
2789 *--p = c = '.';
2790 goto parse_ident_fast;
2791 } else if (c == '.') {
2792 PEEKC(c, p);
2793 if (c == '.') {
2794 p++;
2795 tok = TOK_DOTS;
2796 } else {
2797 *--p = '.'; /* may underflow into file->unget[] */
2798 tok = '.';
2800 } else {
2801 tok = '.';
2803 break;
2804 case '\'':
2805 case '\"':
2806 is_long = 0;
2807 str_const:
2808 cstr_reset(&tokcstr);
2809 if (is_long)
2810 cstr_ccat(&tokcstr, 'L');
2811 cstr_ccat(&tokcstr, c);
2812 p = parse_pp_string(p, c, &tokcstr);
2813 cstr_ccat(&tokcstr, c);
2814 cstr_ccat(&tokcstr, '\0');
2815 tokc.str.size = tokcstr.size;
2816 tokc.str.data = tokcstr.data;
2817 tok = TOK_PPSTR;
2818 break;
2820 case '<':
2821 PEEKC(c, p);
2822 if (c == '=') {
2823 p++;
2824 tok = TOK_LE;
2825 } else if (c == '<') {
2826 PEEKC(c, p);
2827 if (c == '=') {
2828 p++;
2829 tok = TOK_A_SHL;
2830 } else {
2831 tok = TOK_SHL;
2833 } else {
2834 tok = TOK_LT;
2836 break;
2837 case '>':
2838 PEEKC(c, p);
2839 if (c == '=') {
2840 p++;
2841 tok = TOK_GE;
2842 } else if (c == '>') {
2843 PEEKC(c, p);
2844 if (c == '=') {
2845 p++;
2846 tok = TOK_A_SAR;
2847 } else {
2848 tok = TOK_SAR;
2850 } else {
2851 tok = TOK_GT;
2853 break;
2855 case '&':
2856 PEEKC(c, p);
2857 if (c == '&') {
2858 p++;
2859 tok = TOK_LAND;
2860 } else if (c == '=') {
2861 p++;
2862 tok = TOK_A_AND;
2863 } else {
2864 tok = '&';
2866 break;
2868 case '|':
2869 PEEKC(c, p);
2870 if (c == '|') {
2871 p++;
2872 tok = TOK_LOR;
2873 } else if (c == '=') {
2874 p++;
2875 tok = TOK_A_OR;
2876 } else {
2877 tok = '|';
2879 break;
2881 case '+':
2882 PEEKC(c, p);
2883 if (c == '+') {
2884 p++;
2885 tok = TOK_INC;
2886 } else if (c == '=') {
2887 p++;
2888 tok = TOK_A_ADD;
2889 } else {
2890 tok = '+';
2892 break;
2894 case '-':
2895 PEEKC(c, p);
2896 if (c == '-') {
2897 p++;
2898 tok = TOK_DEC;
2899 } else if (c == '=') {
2900 p++;
2901 tok = TOK_A_SUB;
2902 } else if (c == '>') {
2903 p++;
2904 tok = TOK_ARROW;
2905 } else {
2906 tok = '-';
2908 break;
2910 PARSE2('!', '!', '=', TOK_NE)
2911 PARSE2('=', '=', '=', TOK_EQ)
2912 PARSE2('*', '*', '=', TOK_A_MUL)
2913 PARSE2('%', '%', '=', TOK_A_MOD)
2914 PARSE2('^', '^', '=', TOK_A_XOR)
2916 /* comments or operator */
2917 case '/':
2918 PEEKC(c, p);
2919 if (c == '*') {
2920 p = parse_comment(p);
2921 /* comments replaced by a blank */
2922 tok = ' ';
2923 goto keep_tok_flags;
2924 } else if (c == '/') {
2925 p = parse_line_comment(p);
2926 tok = ' ';
2927 goto keep_tok_flags;
2928 } else if (c == '=') {
2929 p++;
2930 tok = TOK_A_DIV;
2931 } else {
2932 tok = '/';
2934 break;
2936 /* simple tokens */
2937 case '(':
2938 case ')':
2939 case '[':
2940 case ']':
2941 case '{':
2942 case '}':
2943 case ',':
2944 case ';':
2945 case ':':
2946 case '?':
2947 case '~':
2948 case '@': /* only used in assembler */
2949 parse_simple:
2950 tok = c;
2951 p++;
2952 break;
2953 default:
2954 if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
2955 goto parse_ident_fast;
2956 if (parse_flags & PARSE_FLAG_ASM_FILE)
2957 goto parse_simple;
2958 tcc_error("unrecognized character \\x%02x", c);
2959 break;
2961 tok_flags = 0;
2962 keep_tok_flags:
2963 file->buf_ptr = p;
2964 #if defined(PARSE_DEBUG)
2965 printf("token = %d %s\n", tok, get_tok_str(tok, &tokc));
2966 #endif
2969 /* return next token without macro substitution. Can read input from
2970 macro_ptr buffer */
2971 static void next_nomacro_spc(void)
2973 if (macro_ptr) {
2974 redo:
2975 tok = *macro_ptr;
2976 if (tok) {
2977 TOK_GET(&tok, &macro_ptr, &tokc);
2978 if (tok == TOK_LINENUM) {
2979 file->line_num = tokc.i;
2980 goto redo;
2983 } else {
2984 next_nomacro1();
2986 //printf("token = %s\n", get_tok_str(tok, &tokc));
2989 ST_FUNC void next_nomacro(void)
2991 do {
2992 next_nomacro_spc();
2993 } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
2997 static void macro_subst(
2998 TokenString *tok_str,
2999 Sym **nested_list,
3000 const int *macro_str
3003 /* substitute arguments in replacement lists in macro_str by the values in
3004 args (field d) and return allocated string */
3005 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
3007 int t, t0, t1, spc;
3008 const int *st;
3009 Sym *s;
3010 CValue cval;
3011 TokenString str;
3012 CString cstr;
3014 tok_str_new(&str);
3015 t0 = t1 = 0;
3016 while(1) {
3017 TOK_GET(&t, &macro_str, &cval);
3018 if (!t)
3019 break;
3020 if (t == '#') {
3021 /* stringize */
3022 TOK_GET(&t, &macro_str, &cval);
3023 if (!t)
3024 goto bad_stringy;
3025 s = sym_find2(args, t);
3026 if (s) {
3027 cstr_new(&cstr);
3028 cstr_ccat(&cstr, '\"');
3029 st = s->d;
3030 spc = 0;
3031 while (*st >= 0) {
3032 TOK_GET(&t, &st, &cval);
3033 if (t != TOK_PLCHLDR
3034 && t != TOK_NOSUBST
3035 && 0 == check_space(t, &spc)) {
3036 const char *s = get_tok_str(t, &cval);
3037 while (*s) {
3038 if (t == TOK_PPSTR && *s != '\'')
3039 add_char(&cstr, *s);
3040 else
3041 cstr_ccat(&cstr, *s);
3042 ++s;
3046 cstr.size -= spc;
3047 cstr_ccat(&cstr, '\"');
3048 cstr_ccat(&cstr, '\0');
3049 #ifdef PP_DEBUG
3050 printf("\nstringize: <%s>\n", (char *)cstr.data);
3051 #endif
3052 /* add string */
3053 cval.str.size = cstr.size;
3054 cval.str.data = cstr.data;
3055 tok_str_add2(&str, TOK_PPSTR, &cval);
3056 cstr_free(&cstr);
3057 } else {
3058 bad_stringy:
3059 expect("macro parameter after '#'");
3061 } else if (t >= TOK_IDENT) {
3062 s = sym_find2(args, t);
3063 if (s) {
3064 int l0 = str.len;
3065 st = s->d;
3066 /* if '##' is present before or after, no arg substitution */
3067 if (*macro_str == TOK_PPJOIN || t1 == TOK_PPJOIN) {
3068 /* special case for var arg macros : ## eats the ','
3069 if empty VA_ARGS variable. */
3070 if (t1 == TOK_PPJOIN && t0 == ',' && gnu_ext && s->type.t) {
3071 if (*st <= 0) {
3072 /* suppress ',' '##' */
3073 str.len -= 2;
3074 } else {
3075 /* suppress '##' and add variable */
3076 str.len--;
3077 goto add_var;
3080 } else {
3081 add_var:
3082 if (!s->next) {
3083 /* Expand arguments tokens and store them. In most
3084 cases we could also re-expand each argument if
3085 used multiple times, but not if the argument
3086 contains the __COUNTER__ macro. */
3087 TokenString str2;
3088 sym_push2(&s->next, s->v, s->type.t, 0);
3089 tok_str_new(&str2);
3090 macro_subst(&str2, nested_list, st);
3091 tok_str_add(&str2, 0);
3092 s->next->d = str2.str;
3094 st = s->next->d;
3096 for(;;) {
3097 int t2;
3098 TOK_GET(&t2, &st, &cval);
3099 if (t2 <= 0)
3100 break;
3101 tok_str_add2(&str, t2, &cval);
3103 if (str.len == l0) /* expanded to empty string */
3104 tok_str_add(&str, TOK_PLCHLDR);
3105 } else {
3106 tok_str_add(&str, t);
3108 } else {
3109 tok_str_add2(&str, t, &cval);
3111 t0 = t1, t1 = t;
3113 tok_str_add(&str, 0);
3114 return str.str;
3117 static char const ab_month_name[12][4] =
3119 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3120 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3123 static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
3125 CString cstr;
3126 int n, ret = 1;
3128 cstr_new(&cstr);
3129 if (t1 != TOK_PLCHLDR)
3130 cstr_cat(&cstr, get_tok_str(t1, v1), -1);
3131 n = cstr.size;
3132 if (t2 != TOK_PLCHLDR)
3133 cstr_cat(&cstr, get_tok_str(t2, v2), -1);
3134 cstr_ccat(&cstr, '\0');
3136 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3137 memcpy(file->buffer, cstr.data, cstr.size);
3138 tok_flags = 0;
3139 for (;;) {
3140 next_nomacro1();
3141 if (0 == *file->buf_ptr)
3142 break;
3143 if (is_space(tok))
3144 continue;
3145 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
3146 " preprocessing token", n, cstr.data, (char*)cstr.data + n);
3147 ret = 0;
3148 break;
3150 tcc_close();
3151 //printf("paste <%s>\n", (char*)cstr.data);
3152 cstr_free(&cstr);
3153 return ret;
3156 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3157 return the resulting string (which must be freed). */
3158 static inline int *macro_twosharps(const int *ptr0)
3160 int t;
3161 CValue cval;
3162 TokenString macro_str1;
3163 int start_of_nosubsts = -1;
3164 const int *ptr;
3166 /* we search the first '##' */
3167 for (ptr = ptr0;;) {
3168 TOK_GET(&t, &ptr, &cval);
3169 if (t == TOK_PPJOIN)
3170 break;
3171 if (t == 0)
3172 return NULL;
3175 tok_str_new(&macro_str1);
3177 //tok_print(" $$$", ptr0);
3178 for (ptr = ptr0;;) {
3179 TOK_GET(&t, &ptr, &cval);
3180 if (t == 0)
3181 break;
3182 if (t == TOK_PPJOIN)
3183 continue;
3184 while (*ptr == TOK_PPJOIN) {
3185 int t1; CValue cv1;
3186 /* given 'a##b', remove nosubsts preceding 'a' */
3187 if (start_of_nosubsts >= 0)
3188 macro_str1.len = start_of_nosubsts;
3189 /* given 'a##b', remove nosubsts preceding 'b' */
3190 while ((t1 = *++ptr) == TOK_NOSUBST)
3192 if (t1 && t1 != TOK_PPJOIN) {
3193 TOK_GET(&t1, &ptr, &cv1);
3194 if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
3195 if (paste_tokens(t, &cval, t1, &cv1)) {
3196 t = tok, cval = tokc;
3197 } else {
3198 tok_str_add2(&macro_str1, t, &cval);
3199 t = t1, cval = cv1;
3204 if (t == TOK_NOSUBST) {
3205 if (start_of_nosubsts < 0)
3206 start_of_nosubsts = macro_str1.len;
3207 } else {
3208 start_of_nosubsts = -1;
3210 tok_str_add2(&macro_str1, t, &cval);
3212 tok_str_add(&macro_str1, 0);
3213 //tok_print(" ###", macro_str1.str);
3214 return macro_str1.str;
3217 /* peek or read [ws_str == NULL] next token from function macro call,
3218 walking up macro levels up to the file if necessary */
3219 static int next_argstream(Sym **nested_list, TokenString *ws_str)
3221 int t;
3222 const int *p;
3223 Sym *sa;
3225 for (;;) {
3226 if (macro_ptr) {
3227 p = macro_ptr, t = *p;
3228 if (ws_str) {
3229 while (is_space(t) || TOK_LINEFEED == t || TOK_PLCHLDR == t)
3230 tok_str_add(ws_str, t), t = *++p;
3232 if (t == 0) {
3233 end_macro();
3234 /* also, end of scope for nested defined symbol */
3235 sa = *nested_list;
3236 while (sa && sa->v == 0)
3237 sa = sa->prev;
3238 if (sa)
3239 sa->v = 0;
3240 continue;
3242 } else {
3243 ch = handle_eob();
3244 if (ws_str) {
3245 while (is_space(ch) || ch == '\n' || ch == '/') {
3246 if (ch == '/') {
3247 int c;
3248 uint8_t *p = file->buf_ptr;
3249 PEEKC(c, p);
3250 if (c == '*') {
3251 p = parse_comment(p);
3252 file->buf_ptr = p - 1;
3253 } else if (c == '/') {
3254 p = parse_line_comment(p);
3255 file->buf_ptr = p - 1;
3256 } else
3257 break;
3258 ch = ' ';
3260 if (ch == '\n')
3261 file->line_num++;
3262 if (!(ch == '\f' || ch == '\v' || ch == '\r'))
3263 tok_str_add(ws_str, ch);
3264 cinp();
3267 t = ch;
3270 if (ws_str)
3271 return t;
3272 next_nomacro_spc();
3273 return tok;
3277 /* do macro substitution of current token with macro 's' and add
3278 result to (tok_str,tok_len). 'nested_list' is the list of all
3279 macros we got inside to avoid recursing. Return non zero if no
3280 substitution needs to be done */
3281 static int macro_subst_tok(
3282 TokenString *tok_str,
3283 Sym **nested_list,
3284 Sym *s)
3286 Sym *args, *sa, *sa1;
3287 int parlevel, t, t1, spc;
3288 TokenString str;
3289 char *cstrval;
3290 CValue cval;
3291 CString cstr;
3292 char buf[32];
3294 /* if symbol is a macro, prepare substitution */
3295 /* special macros */
3296 if (tok == TOK___LINE__ || tok == TOK___COUNTER__) {
3297 t = tok == TOK___LINE__ ? file->line_num : pp_counter++;
3298 snprintf(buf, sizeof(buf), "%d", t);
3299 cstrval = buf;
3300 t1 = TOK_PPNUM;
3301 goto add_cstr1;
3302 } else if (tok == TOK___FILE__) {
3303 cstrval = file->filename;
3304 goto add_cstr;
3305 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3306 time_t ti;
3307 struct tm *tm;
3309 time(&ti);
3310 tm = localtime(&ti);
3311 if (tok == TOK___DATE__) {
3312 snprintf(buf, sizeof(buf), "%s %2d %d",
3313 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3314 } else {
3315 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3316 tm->tm_hour, tm->tm_min, tm->tm_sec);
3318 cstrval = buf;
3319 add_cstr:
3320 t1 = TOK_STR;
3321 add_cstr1:
3322 cstr_new(&cstr);
3323 cstr_cat(&cstr, cstrval, 0);
3324 cval.str.size = cstr.size;
3325 cval.str.data = cstr.data;
3326 tok_str_add2(tok_str, t1, &cval);
3327 cstr_free(&cstr);
3328 } else if (s->d) {
3329 int saved_parse_flags = parse_flags;
3330 int *joined_str = NULL;
3331 int *mstr = s->d;
3333 if (s->type.t == MACRO_FUNC) {
3334 /* whitespace between macro name and argument list */
3335 TokenString ws_str;
3336 tok_str_new(&ws_str);
3338 spc = 0;
3339 parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
3340 | PARSE_FLAG_ACCEPT_STRAYS;
3342 /* get next token from argument stream */
3343 t = next_argstream(nested_list, &ws_str);
3344 if (t != '(') {
3345 /* not a macro substitution after all, restore the
3346 * macro token plus all whitespace we've read.
3347 * whitespace is intentionally not merged to preserve
3348 * newlines. */
3349 parse_flags = saved_parse_flags;
3350 tok_str_add(tok_str, tok);
3351 if (parse_flags & PARSE_FLAG_SPACES) {
3352 int i;
3353 for (i = 0; i < ws_str.len; i++)
3354 tok_str_add(tok_str, ws_str.str[i]);
3356 tok_str_free_str(ws_str.str);
3357 return 0;
3358 } else {
3359 tok_str_free_str(ws_str.str);
3361 do {
3362 next_nomacro(); /* eat '(' */
3363 } while (tok == TOK_PLCHLDR);
3365 /* argument macro */
3366 args = NULL;
3367 sa = s->next;
3368 /* NOTE: empty args are allowed, except if no args */
3369 for(;;) {
3370 do {
3371 next_argstream(nested_list, NULL);
3372 } while (is_space(tok) || TOK_LINEFEED == tok);
3373 empty_arg:
3374 /* handle '()' case */
3375 if (!args && !sa && tok == ')')
3376 break;
3377 if (!sa)
3378 tcc_error("macro '%s' used with too many args",
3379 get_tok_str(s->v, 0));
3380 tok_str_new(&str);
3381 parlevel = spc = 0;
3382 /* NOTE: non zero sa->t indicates VA_ARGS */
3383 while ((parlevel > 0 ||
3384 (tok != ')' &&
3385 (tok != ',' || sa->type.t)))) {
3386 if (tok == TOK_EOF || tok == 0)
3387 break;
3388 if (tok == '(')
3389 parlevel++;
3390 else if (tok == ')')
3391 parlevel--;
3392 if (tok == TOK_LINEFEED)
3393 tok = ' ';
3394 if (!check_space(tok, &spc))
3395 tok_str_add2(&str, tok, &tokc);
3396 next_argstream(nested_list, NULL);
3398 if (parlevel)
3399 expect(")");
3400 str.len -= spc;
3401 tok_str_add(&str, -1);
3402 tok_str_add(&str, 0);
3403 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
3404 sa1->d = str.str;
3405 sa = sa->next;
3406 if (tok == ')') {
3407 /* special case for gcc var args: add an empty
3408 var arg argument if it is omitted */
3409 if (sa && sa->type.t && gnu_ext)
3410 goto empty_arg;
3411 break;
3413 if (tok != ',')
3414 expect(",");
3416 if (sa) {
3417 tcc_error("macro '%s' used with too few args",
3418 get_tok_str(s->v, 0));
3421 parse_flags = saved_parse_flags;
3423 /* now subst each arg */
3424 mstr = macro_arg_subst(nested_list, mstr, args);
3425 /* free memory */
3426 sa = args;
3427 while (sa) {
3428 sa1 = sa->prev;
3429 tok_str_free_str(sa->d);
3430 if (sa->next) {
3431 tok_str_free_str(sa->next->d);
3432 sym_free(sa->next);
3434 sym_free(sa);
3435 sa = sa1;
3439 sym_push2(nested_list, s->v, 0, 0);
3440 parse_flags = saved_parse_flags;
3441 joined_str = macro_twosharps(mstr);
3442 macro_subst(tok_str, nested_list, joined_str ? joined_str : mstr);
3444 /* pop nested defined symbol */
3445 sa1 = *nested_list;
3446 *nested_list = sa1->prev;
3447 sym_free(sa1);
3448 if (joined_str)
3449 tok_str_free_str(joined_str);
3450 if (mstr != s->d)
3451 tok_str_free_str(mstr);
3453 return 0;
3456 /* do macro substitution of macro_str and add result to
3457 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3458 inside to avoid recursing. */
3459 static void macro_subst(
3460 TokenString *tok_str,
3461 Sym **nested_list,
3462 const int *macro_str
3465 Sym *s;
3466 int t, spc, nosubst;
3467 CValue cval;
3469 spc = nosubst = 0;
3471 while (1) {
3472 TOK_GET(&t, &macro_str, &cval);
3473 if (t <= 0)
3474 break;
3476 if (t >= TOK_IDENT && 0 == nosubst) {
3477 s = define_find(t);
3478 if (s == NULL)
3479 goto no_subst;
3481 /* if nested substitution, do nothing */
3482 if (sym_find2(*nested_list, t)) {
3483 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3484 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3485 goto no_subst;
3489 TokenString str;
3490 str.str = (int*)macro_str;
3491 begin_macro(&str, 2);
3493 tok = t;
3494 macro_subst_tok(tok_str, nested_list, s);
3496 if (str.alloc == 3) {
3497 /* already finished by reading function macro arguments */
3498 break;
3501 macro_str = macro_ptr;
3502 end_macro ();
3504 if (tok_str->len)
3505 spc = is_space(t = tok_str->str[tok_str->lastlen]);
3506 } else {
3507 if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
3508 tcc_error("stray '\\' in program");
3509 no_subst:
3510 if (!check_space(t, &spc))
3511 tok_str_add2(tok_str, t, &cval);
3513 if (nosubst) {
3514 if (nosubst > 1 && (spc || (++nosubst == 3 && t == '(')))
3515 continue;
3516 nosubst = 0;
3518 if (t == TOK_NOSUBST)
3519 nosubst = 1;
3521 /* GCC supports 'defined' as result of a macro substitution */
3522 if (t == TOK_DEFINED && pp_expr)
3523 nosubst = 2;
3527 /* return next token with macro substitution */
3528 ST_FUNC void next(void)
3530 /* generate line number info */
3531 if (tcc_state->do_debug)
3532 tcc_debug_line(tcc_state);
3533 redo:
3534 if (parse_flags & PARSE_FLAG_SPACES)
3535 next_nomacro_spc();
3536 else
3537 next_nomacro();
3539 if (macro_ptr) {
3540 if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
3541 /* discard preprocessor markers */
3542 goto redo;
3543 } else if (tok == 0) {
3544 /* end of macro or unget token string */
3545 end_macro();
3546 goto redo;
3548 } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
3549 Sym *s;
3550 /* if reading from file, try to substitute macros */
3551 s = define_find(tok);
3552 if (s) {
3553 Sym *nested_list = NULL;
3554 tokstr_buf.len = 0;
3555 macro_subst_tok(&tokstr_buf, &nested_list, s);
3556 tok_str_add(&tokstr_buf, 0);
3557 begin_macro(&tokstr_buf, 2);
3558 goto redo;
3561 /* convert preprocessor tokens into C tokens */
3562 if (tok == TOK_PPNUM) {
3563 if (parse_flags & PARSE_FLAG_TOK_NUM)
3564 parse_number((char *)tokc.str.data);
3565 } else if (tok == TOK_PPSTR) {
3566 if (parse_flags & PARSE_FLAG_TOK_STR)
3567 parse_string((char *)tokc.str.data, tokc.str.size - 1);
3571 /* push back current token and set current token to 'last_tok'. Only
3572 identifier case handled for labels. */
3573 ST_INLN void unget_tok(int last_tok)
3576 TokenString *str = tok_str_alloc();
3577 tok_str_add2(str, tok, &tokc);
3578 tok_str_add(str, 0);
3579 begin_macro(str, 1);
3580 tok = last_tok;
3583 ST_FUNC void preprocess_start(TCCState *s1, int is_asm)
3585 CString cstr;
3586 int i;
3588 s1->include_stack_ptr = s1->include_stack;
3589 s1->ifdef_stack_ptr = s1->ifdef_stack;
3590 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3591 pp_expr = 0;
3592 pp_counter = 0;
3593 pp_debug_tok = pp_debug_symv = 0;
3594 pp_once++;
3595 pvtop = vtop = vstack - 1;
3596 s1->pack_stack[0] = 0;
3597 s1->pack_stack_ptr = s1->pack_stack;
3599 set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
3600 set_idnum('.', is_asm ? IS_ID : 0);
3602 cstr_new(&cstr);
3603 cstr_cat(&cstr, "\"", -1);
3604 cstr_cat(&cstr, file->filename, -1);
3605 cstr_cat(&cstr, "\"", 0);
3606 tcc_define_symbol(s1, "__BASE_FILE__", cstr.data);
3608 cstr_reset(&cstr);
3609 for (i = 0; i < s1->nb_cmd_include_files; i++) {
3610 cstr_cat(&cstr, "#include \"", -1);
3611 cstr_cat(&cstr, s1->cmd_include_files[i], -1);
3612 cstr_cat(&cstr, "\"\n", -1);
3614 if (cstr.size) {
3615 *s1->include_stack_ptr++ = file;
3616 tcc_open_bf(s1, "<command line>", cstr.size);
3617 memcpy(file->buffer, cstr.data, cstr.size);
3619 cstr_free(&cstr);
3621 if (is_asm)
3622 tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
3624 parse_flags = is_asm ? PARSE_FLAG_ASM_FILE : 0;
3625 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3628 /* cleanup from error/setjmp */
3629 ST_FUNC void preprocess_end(TCCState *s1)
3631 /* Normally macro_stack is NULL here, except if an
3632 error was thrown; then it can point to allocated storage
3633 or to some stack variables, but those are unwound via
3634 setjmp already, so can't be accessed. Only two choices:
3635 either we leak memory or we access invalid memory. The
3636 former is the better choice. */
3637 macro_stack = NULL;
3638 macro_ptr = NULL;
3641 ST_FUNC void tccpp_new(TCCState *s)
3643 int i, c;
3644 const char *p, *r;
3646 /* might be used in error() before preprocess_start() */
3647 s->include_stack_ptr = s->include_stack;
3648 s->ppfp = stdout;
3650 /* init isid table */
3651 for(i = CH_EOF; i<128; i++)
3652 set_idnum(i,
3653 is_space(i) ? IS_SPC
3654 : isid(i) ? IS_ID
3655 : isnum(i) ? IS_NUM
3656 : 0);
3658 for(i = 128; i<256; i++)
3659 set_idnum(i, IS_ID);
3661 /* init allocators */
3662 tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
3663 tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
3664 tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
3666 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3667 cstr_new(&cstr_buf);
3668 cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
3669 tok_str_new(&tokstr_buf);
3670 tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
3672 tok_ident = TOK_IDENT;
3673 p = tcc_keywords;
3674 while (*p) {
3675 r = p;
3676 for(;;) {
3677 c = *r++;
3678 if (c == '\0')
3679 break;
3681 tok_alloc(p, r - p - 1);
3682 p = r;
3686 ST_FUNC void tccpp_delete(TCCState *s)
3688 int i, n;
3690 /* free -D and compiler defines */
3691 free_defines(NULL);
3693 /* free tokens */
3694 n = tok_ident - TOK_IDENT;
3695 for(i = 0; i < n; i++)
3696 tal_free(toksym_alloc, table_ident[i]);
3697 tcc_free(table_ident);
3698 table_ident = NULL;
3700 /* free static buffers */
3701 cstr_free(&tokcstr);
3702 cstr_free(&cstr_buf);
3703 cstr_free(&macro_equal_buf);
3704 tok_str_free_str(tokstr_buf.str);
3706 /* free allocators */
3707 tal_delete(toksym_alloc);
3708 toksym_alloc = NULL;
3709 tal_delete(tokstr_alloc);
3710 tokstr_alloc = NULL;
3711 tal_delete(cstr_alloc);
3712 cstr_alloc = NULL;
3715 /* ------------------------------------------------------------------------- */
3716 /* tcc -E [-P[1]] [-dD} support */
3718 static void tok_print(const char *msg, const int *str)
3720 FILE *fp;
3721 int t, s = 0;
3722 CValue cval;
3724 fp = tcc_state->ppfp;
3725 fprintf(fp, "%s", msg);
3726 while (str) {
3727 TOK_GET(&t, &str, &cval);
3728 if (!t)
3729 break;
3730 fprintf(fp, " %s" + s, get_tok_str(t, &cval)), s = 1;
3732 fprintf(fp, "\n");
3735 static void pp_line(TCCState *s1, BufferedFile *f, int level)
3737 int d = f->line_num - f->line_ref;
3739 if (s1->dflag & 4)
3740 return;
3742 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3744 } else if (level == 0 && f->line_ref && d < 8) {
3745 while (d > 0)
3746 fputs("\n", s1->ppfp), --d;
3747 } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3748 fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3749 } else {
3750 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3751 level > 0 ? " 1" : level < 0 ? " 2" : "");
3753 f->line_ref = f->line_num;
3756 static void define_print(TCCState *s1, int v)
3758 FILE *fp;
3759 Sym *s;
3761 s = define_find(v);
3762 if (NULL == s || NULL == s->d)
3763 return;
3765 fp = s1->ppfp;
3766 fprintf(fp, "#define %s", get_tok_str(v, NULL));
3767 if (s->type.t == MACRO_FUNC) {
3768 Sym *a = s->next;
3769 fprintf(fp,"(");
3770 if (a)
3771 for (;;) {
3772 fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3773 if (!(a = a->next))
3774 break;
3775 fprintf(fp,",");
3777 fprintf(fp,")");
3779 tok_print("", s->d);
3782 static void pp_debug_defines(TCCState *s1)
3784 int v, t;
3785 const char *vs;
3786 FILE *fp;
3788 t = pp_debug_tok;
3789 if (t == 0)
3790 return;
3792 file->line_num--;
3793 pp_line(s1, file, 0);
3794 file->line_ref = ++file->line_num;
3796 fp = s1->ppfp;
3797 v = pp_debug_symv;
3798 vs = get_tok_str(v, NULL);
3799 if (t == TOK_DEFINE) {
3800 define_print(s1, v);
3801 } else if (t == TOK_UNDEF) {
3802 fprintf(fp, "#undef %s\n", vs);
3803 } else if (t == TOK_push_macro) {
3804 fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3805 } else if (t == TOK_pop_macro) {
3806 fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3808 pp_debug_tok = 0;
3811 static void pp_debug_builtins(TCCState *s1)
3813 int v;
3814 for (v = TOK_IDENT; v < tok_ident; ++v)
3815 define_print(s1, v);
3818 /* Add a space between tokens a and b to avoid unwanted textual pasting */
3819 static int pp_need_space(int a, int b)
3821 return 'E' == a ? '+' == b || '-' == b
3822 : '+' == a ? TOK_INC == b || '+' == b
3823 : '-' == a ? TOK_DEC == b || '-' == b
3824 : a >= TOK_IDENT ? b >= TOK_IDENT
3825 : a == TOK_PPNUM ? b >= TOK_IDENT
3826 : 0;
3829 /* maybe hex like 0x1e */
3830 static int pp_check_he0xE(int t, const char *p)
3832 if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
3833 return 'E';
3834 return t;
3837 /* Preprocess the current file */
3838 ST_FUNC int tcc_preprocess(TCCState *s1)
3840 BufferedFile **iptr;
3841 int token_seen, spcs, level;
3842 const char *p;
3843 char white[400];
3845 parse_flags = PARSE_FLAG_PREPROCESS
3846 | (parse_flags & PARSE_FLAG_ASM_FILE)
3847 | PARSE_FLAG_LINEFEED
3848 | PARSE_FLAG_SPACES
3849 | PARSE_FLAG_ACCEPT_STRAYS
3851 /* Credits to Fabrice Bellard's initial revision to demonstrate its
3852 capability to compile and run itself, provided all numbers are
3853 given as decimals. tcc -E -P10 will do. */
3854 if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_P10)
3855 parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
3857 #ifdef PP_BENCH
3858 /* for PP benchmarks */
3859 do next(); while (tok != TOK_EOF);
3860 return 0;
3861 #endif
3863 if (s1->dflag & 1) {
3864 pp_debug_builtins(s1);
3865 s1->dflag &= ~1;
3868 token_seen = TOK_LINEFEED, spcs = 0;
3869 pp_line(s1, file, 0);
3870 for (;;) {
3871 iptr = s1->include_stack_ptr;
3872 next();
3873 if (tok == TOK_EOF)
3874 break;
3876 level = s1->include_stack_ptr - iptr;
3877 if (level) {
3878 if (level > 0)
3879 pp_line(s1, *iptr, 0);
3880 pp_line(s1, file, level);
3882 if (s1->dflag & 7) {
3883 pp_debug_defines(s1);
3884 if (s1->dflag & 4)
3885 continue;
3888 if (is_space(tok)) {
3889 if (spcs < sizeof white - 1)
3890 white[spcs++] = tok;
3891 continue;
3892 } else if (tok == TOK_LINEFEED) {
3893 spcs = 0;
3894 if (token_seen == TOK_LINEFEED)
3895 continue;
3896 ++file->line_ref;
3897 } else if (token_seen == TOK_LINEFEED) {
3898 pp_line(s1, file, 0);
3899 } else if (spcs == 0 && pp_need_space(token_seen, tok)) {
3900 white[spcs++] = ' ';
3903 white[spcs] = 0, fputs(white, s1->ppfp), spcs = 0;
3904 fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
3905 token_seen = pp_check_he0xE(tok, p);
3907 return 0;
3910 /* ------------------------------------------------------------------------- */