Win: Enable use "*.def + *.c" files as library instead of *.a by "-l" option
[tinycc.git] / tccpp.c
blob053fd575f29045db6d6d29121d18f7adb3ada22a
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 /* additional informations about token */
28 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
29 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
30 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
31 #define TOK_FLAG_EOF 0x0008 /* end of file */
33 ST_DATA int parse_flags;
34 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
35 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
36 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
37 token. line feed is also
38 returned at eof */
39 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
40 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
42 ST_DATA struct BufferedFile *file;
43 ST_DATA int ch, tok;
44 ST_DATA CValue tokc;
45 ST_DATA const int *macro_ptr;
46 ST_DATA CString tokcstr; /* current parsed string, if any */
48 /* display benchmark infos */
49 ST_DATA int total_lines;
50 ST_DATA int total_bytes;
51 ST_DATA int tok_ident;
52 ST_DATA TokenSym **table_ident;
54 /* ------------------------------------------------------------------------- */
56 static int *macro_ptr_allocated;
57 static const int *unget_saved_macro_ptr;
58 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
59 static int unget_buffer_enabled;
60 static TokenSym *hash_ident[TOK_HASH_SIZE];
61 static char token_buf[STRING_MAX_SIZE + 1];
62 /* true if isid(c) || isnum(c) */
63 static unsigned char isidnum_table[256-CH_EOF];
64 static int token_seen;
66 static const char tcc_keywords[] =
67 #define DEF(id, str) str "\0"
68 #include "tcctok.h"
69 #undef DEF
72 /* WARNING: the content of this string encodes token numbers */
73 static const unsigned char tok_two_chars[] =
74 /* outdated -- gr
75 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
76 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
77 */{
78 '<','=', TOK_LE,
79 '>','=', TOK_GE,
80 '!','=', TOK_NE,
81 '&','&', TOK_LAND,
82 '|','|', TOK_LOR,
83 '+','+', TOK_INC,
84 '-','-', TOK_DEC,
85 '=','=', TOK_EQ,
86 '<','<', TOK_SHL,
87 '>','>', TOK_SAR,
88 '+','=', TOK_A_ADD,
89 '-','=', TOK_A_SUB,
90 '*','=', TOK_A_MUL,
91 '/','=', TOK_A_DIV,
92 '%','=', TOK_A_MOD,
93 '&','=', TOK_A_AND,
94 '^','=', TOK_A_XOR,
95 '|','=', TOK_A_OR,
96 '-','>', TOK_ARROW,
97 '.','.', 0xa8, // C++ token ?
98 '#','#', TOK_TWOSHARPS,
102 struct macro_level {
103 struct macro_level *prev;
104 const int *p;
107 static void next_nomacro_spc(void);
108 static void macro_subst(
109 TokenString *tok_str,
110 Sym **nested_list,
111 const int *macro_str,
112 struct macro_level **can_read_stream
115 ST_FUNC void skip(int c)
117 if (tok != c)
118 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
119 next();
122 ST_FUNC void expect(const char *msg)
124 tcc_error("%s expected", msg);
127 /* ------------------------------------------------------------------------- */
128 /* CString handling */
129 static void cstr_realloc(CString *cstr, int new_size)
131 int size;
132 void *data;
134 size = cstr->size_allocated;
135 if (size == 0)
136 size = 8; /* no need to allocate a too small first string */
137 while (size < new_size)
138 size = size * 2;
139 data = tcc_realloc(cstr->data_allocated, size);
140 cstr->data_allocated = data;
141 cstr->size_allocated = size;
142 cstr->data = data;
145 /* add a byte */
146 ST_FUNC void cstr_ccat(CString *cstr, int ch)
148 int size;
149 size = cstr->size + 1;
150 if (size > cstr->size_allocated)
151 cstr_realloc(cstr, size);
152 ((unsigned char *)cstr->data)[size - 1] = ch;
153 cstr->size = size;
156 ST_FUNC void cstr_cat(CString *cstr, const char *str)
158 int c;
159 for(;;) {
160 c = *str;
161 if (c == '\0')
162 break;
163 cstr_ccat(cstr, c);
164 str++;
168 /* add a wide char */
169 ST_FUNC void cstr_wccat(CString *cstr, int ch)
171 int size;
172 size = cstr->size + sizeof(nwchar_t);
173 if (size > cstr->size_allocated)
174 cstr_realloc(cstr, size);
175 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
176 cstr->size = size;
179 ST_FUNC void cstr_new(CString *cstr)
181 memset(cstr, 0, sizeof(CString));
184 /* free string and reset it to NULL */
185 ST_FUNC void cstr_free(CString *cstr)
187 tcc_free(cstr->data_allocated);
188 cstr_new(cstr);
191 /* reset string to empty */
192 ST_FUNC void cstr_reset(CString *cstr)
194 cstr->size = 0;
197 /* XXX: unicode ? */
198 static void add_char(CString *cstr, int c)
200 if (c == '\'' || c == '\"' || c == '\\') {
201 /* XXX: could be more precise if char or string */
202 cstr_ccat(cstr, '\\');
204 if (c >= 32 && c <= 126) {
205 cstr_ccat(cstr, c);
206 } else {
207 cstr_ccat(cstr, '\\');
208 if (c == '\n') {
209 cstr_ccat(cstr, 'n');
210 } else {
211 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
212 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
213 cstr_ccat(cstr, '0' + (c & 7));
218 /* ------------------------------------------------------------------------- */
219 /* allocate a new token */
220 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
222 TokenSym *ts, **ptable;
223 int i;
225 if (tok_ident >= SYM_FIRST_ANOM)
226 tcc_error("memory full (symbols)");
228 /* expand token table if needed */
229 i = tok_ident - TOK_IDENT;
230 if ((i % TOK_ALLOC_INCR) == 0) {
231 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
232 table_ident = ptable;
235 ts = tcc_malloc(sizeof(TokenSym) + len);
236 table_ident[i] = ts;
237 ts->tok = tok_ident++;
238 ts->sym_define.data = tcc_malloc(sizeof(Sym*));
239 ts->sym_define.off = 0;
240 ts->sym_define.data[0] = NULL;
241 ts->sym_define.size = 1;
242 ts->sym_label = NULL;
243 ts->sym_struct = NULL;
244 ts->sym_identifier = NULL;
245 ts->len = len;
246 ts->hash_next = NULL;
247 memcpy(ts->str, str, len);
248 ts->str[len] = '\0';
249 *pts = ts;
250 return ts;
253 #define TOK_HASH_INIT 1
254 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
256 /* find a token and add it if not found */
257 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
259 TokenSym *ts, **pts;
260 int i;
261 unsigned int h;
263 h = TOK_HASH_INIT;
264 for(i=0;i<len;i++)
265 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
266 h &= (TOK_HASH_SIZE - 1);
268 pts = &hash_ident[h];
269 for(;;) {
270 ts = *pts;
271 if (!ts)
272 break;
273 if (ts->len == len && !memcmp(ts->str, str, len))
274 return ts;
275 pts = &(ts->hash_next);
277 return tok_alloc_new(pts, str, len);
280 /* XXX: buffer overflow */
281 /* XXX: float tokens */
282 ST_FUNC char *get_tok_str(int v, CValue *cv)
284 static char buf[STRING_MAX_SIZE + 1];
285 static CString cstr_buf;
286 CString *cstr;
287 char *p;
288 int i, len;
290 /* NOTE: to go faster, we give a fixed buffer for small strings */
291 cstr_reset(&cstr_buf);
292 cstr_buf.data = buf;
293 cstr_buf.size_allocated = sizeof(buf);
294 p = buf;
296 /* just an explanation, should never happen:
297 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
298 tcc_error("internal error: get_tok_str"); */
300 switch(v) {
301 case TOK_CINT:
302 case TOK_CUINT:
303 /* XXX: not quite exact, but only useful for testing */
304 sprintf(p, "%u", cv->ui);
305 break;
306 case TOK_CLLONG:
307 case TOK_CULLONG:
308 /* XXX: not quite exact, but only useful for testing */
309 #ifdef _WIN32
310 sprintf(p, "%u", (unsigned)cv->ull);
311 #else
312 sprintf(p, "%llu", cv->ull);
313 #endif
314 break;
315 case TOK_LCHAR:
316 cstr_ccat(&cstr_buf, 'L');
317 case TOK_CCHAR:
318 cstr_ccat(&cstr_buf, '\'');
319 add_char(&cstr_buf, cv->i);
320 cstr_ccat(&cstr_buf, '\'');
321 cstr_ccat(&cstr_buf, '\0');
322 break;
323 case TOK_PPNUM:
324 cstr = cv->cstr;
325 len = cstr->size - 1;
326 for(i=0;i<len;i++)
327 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
328 cstr_ccat(&cstr_buf, '\0');
329 break;
330 case TOK_LSTR:
331 cstr_ccat(&cstr_buf, 'L');
332 case TOK_STR:
333 cstr = cv->cstr;
334 cstr_ccat(&cstr_buf, '\"');
335 if (v == TOK_STR) {
336 len = cstr->size - 1;
337 for(i=0;i<len;i++)
338 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
339 } else {
340 len = (cstr->size / sizeof(nwchar_t)) - 1;
341 for(i=0;i<len;i++)
342 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
344 cstr_ccat(&cstr_buf, '\"');
345 cstr_ccat(&cstr_buf, '\0');
346 break;
348 case TOK_CFLOAT:
349 case TOK_CDOUBLE:
350 case TOK_CLDOUBLE:
351 case TOK_LINENUM:
352 return NULL; /* should not happen */
354 /* above tokens have value, the ones below don't */
356 case TOK_LT:
357 v = '<';
358 goto addv;
359 case TOK_GT:
360 v = '>';
361 goto addv;
362 case TOK_DOTS:
363 return strcpy(p, "...");
364 case TOK_A_SHL:
365 return strcpy(p, "<<=");
366 case TOK_A_SAR:
367 return strcpy(p, ">>=");
368 default:
369 if (v < TOK_IDENT) {
370 /* search in two bytes table */
371 const unsigned char *q = tok_two_chars;
372 while (*q) {
373 if (q[2] == v) {
374 *p++ = q[0];
375 *p++ = q[1];
376 *p = '\0';
377 return buf;
379 q += 3;
381 addv:
382 *p++ = v;
383 *p = '\0';
384 } else if (v < tok_ident) {
385 return table_ident[v - TOK_IDENT]->str;
386 } else if (v >= SYM_FIRST_ANOM) {
387 /* special name for anonymous symbol */
388 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
389 } else {
390 /* should never happen */
391 return NULL;
393 break;
395 return cstr_buf.data;
398 /* fill input buffer and peek next char */
399 static int tcc_peekc_slow(BufferedFile *bf)
401 int len;
402 /* only tries to read if really end of buffer */
403 if (bf->buf_ptr >= bf->buf_end) {
404 if (bf->fd != -1) {
405 #if defined(PARSE_DEBUG)
406 len = 8;
407 #else
408 len = IO_BUF_SIZE;
409 #endif
410 len = read(bf->fd, bf->buffer, len);
411 if (len < 0)
412 len = 0;
413 } else {
414 len = 0;
416 total_bytes += len;
417 bf->buf_ptr = bf->buffer;
418 bf->buf_end = bf->buffer + len;
419 *bf->buf_end = CH_EOB;
421 if (bf->buf_ptr < bf->buf_end) {
422 return bf->buf_ptr[0];
423 } else {
424 bf->buf_ptr = bf->buf_end;
425 return CH_EOF;
429 /* return the current character, handling end of block if necessary
430 (but not stray) */
431 ST_FUNC int handle_eob(void)
433 return tcc_peekc_slow(file);
436 /* read next char from current input file and handle end of input buffer */
437 ST_INLN void inp(void)
439 ch = *(++(file->buf_ptr));
440 /* end of buffer/file handling */
441 if (ch == CH_EOB)
442 ch = handle_eob();
445 /* handle '\[\r]\n' */
446 static int handle_stray_noerror(void)
448 while (ch == '\\') {
449 inp();
450 if (ch == '\n') {
451 file->line_num++;
452 inp();
453 } else if (ch == '\r') {
454 inp();
455 if (ch != '\n')
456 goto fail;
457 file->line_num++;
458 inp();
459 } else {
460 fail:
461 return 1;
464 return 0;
467 static void handle_stray(void)
469 if (handle_stray_noerror())
470 tcc_error("stray '\\' in program");
473 /* skip the stray and handle the \\n case. Output an error if
474 incorrect char after the stray */
475 static int handle_stray1(uint8_t *p)
477 int c;
479 if (p >= file->buf_end) {
480 file->buf_ptr = p;
481 c = handle_eob();
482 p = file->buf_ptr;
483 if (c == '\\')
484 goto parse_stray;
485 } else {
486 parse_stray:
487 file->buf_ptr = p;
488 ch = *p;
489 handle_stray();
490 p = file->buf_ptr;
491 c = *p;
493 return c;
496 /* handle just the EOB case, but not stray */
497 #define PEEKC_EOB(c, p)\
499 p++;\
500 c = *p;\
501 if (c == '\\') {\
502 file->buf_ptr = p;\
503 c = handle_eob();\
504 p = file->buf_ptr;\
508 /* handle the complicated stray case */
509 #define PEEKC(c, p)\
511 p++;\
512 c = *p;\
513 if (c == '\\') {\
514 c = handle_stray1(p);\
515 p = file->buf_ptr;\
519 /* input with '\[\r]\n' handling. Note that this function cannot
520 handle other characters after '\', so you cannot call it inside
521 strings or comments */
522 ST_FUNC void minp(void)
524 inp();
525 if (ch == '\\')
526 handle_stray();
530 /* single line C++ comments */
531 static uint8_t *parse_line_comment(uint8_t *p)
533 int c;
535 p++;
536 for(;;) {
537 c = *p;
538 redo:
539 if (c == '\n' || c == CH_EOF) {
540 break;
541 } else if (c == '\\') {
542 file->buf_ptr = p;
543 c = handle_eob();
544 p = file->buf_ptr;
545 if (c == '\\') {
546 PEEKC_EOB(c, p);
547 if (c == '\n') {
548 file->line_num++;
549 PEEKC_EOB(c, p);
550 } else if (c == '\r') {
551 PEEKC_EOB(c, p);
552 if (c == '\n') {
553 file->line_num++;
554 PEEKC_EOB(c, p);
557 } else {
558 goto redo;
560 } else {
561 p++;
564 return p;
567 /* C comments */
568 ST_FUNC uint8_t *parse_comment(uint8_t *p)
570 int c;
572 p++;
573 for(;;) {
574 /* fast skip loop */
575 for(;;) {
576 c = *p;
577 if (c == '\n' || c == '*' || c == '\\')
578 break;
579 p++;
580 c = *p;
581 if (c == '\n' || c == '*' || c == '\\')
582 break;
583 p++;
585 /* now we can handle all the cases */
586 if (c == '\n') {
587 file->line_num++;
588 p++;
589 } else if (c == '*') {
590 p++;
591 for(;;) {
592 c = *p;
593 if (c == '*') {
594 p++;
595 } else if (c == '/') {
596 goto end_of_comment;
597 } else if (c == '\\') {
598 file->buf_ptr = p;
599 c = handle_eob();
600 p = file->buf_ptr;
601 if (c == '\\') {
602 /* skip '\[\r]\n', otherwise just skip the stray */
603 while (c == '\\') {
604 PEEKC_EOB(c, p);
605 if (c == '\n') {
606 file->line_num++;
607 PEEKC_EOB(c, p);
608 } else if (c == '\r') {
609 PEEKC_EOB(c, p);
610 if (c == '\n') {
611 file->line_num++;
612 PEEKC_EOB(c, p);
614 } else {
615 goto after_star;
619 } else {
620 break;
623 after_star: ;
624 } else {
625 /* stray, eob or eof */
626 file->buf_ptr = p;
627 c = handle_eob();
628 p = file->buf_ptr;
629 if (c == CH_EOF) {
630 tcc_error("unexpected end of file in comment");
631 } else if (c == '\\') {
632 p++;
636 end_of_comment:
637 p++;
638 return p;
641 #define cinp minp
643 static inline void skip_spaces(void)
645 while (is_space(ch))
646 cinp();
649 static inline int check_space(int t, int *spc)
651 if (is_space(t)) {
652 if (*spc)
653 return 1;
654 *spc = 1;
655 } else
656 *spc = 0;
657 return 0;
660 /* parse a string without interpreting escapes */
661 static uint8_t *parse_pp_string(uint8_t *p,
662 int sep, CString *str)
664 int c;
665 p++;
666 for(;;) {
667 c = *p;
668 if (c == sep) {
669 break;
670 } else if (c == '\\') {
671 file->buf_ptr = p;
672 c = handle_eob();
673 p = file->buf_ptr;
674 if (c == CH_EOF) {
675 unterminated_string:
676 /* XXX: indicate line number of start of string */
677 tcc_error("missing terminating %c character", sep);
678 } else if (c == '\\') {
679 /* escape : just skip \[\r]\n */
680 PEEKC_EOB(c, p);
681 if (c == '\n') {
682 file->line_num++;
683 p++;
684 } else if (c == '\r') {
685 PEEKC_EOB(c, p);
686 if (c != '\n')
687 expect("'\n' after '\r'");
688 file->line_num++;
689 p++;
690 } else if (c == CH_EOF) {
691 goto unterminated_string;
692 } else {
693 if (str) {
694 cstr_ccat(str, '\\');
695 cstr_ccat(str, c);
697 p++;
700 } else if (c == '\n') {
701 file->line_num++;
702 goto add_char;
703 } else if (c == '\r') {
704 PEEKC_EOB(c, p);
705 if (c != '\n') {
706 if (str)
707 cstr_ccat(str, '\r');
708 } else {
709 file->line_num++;
710 goto add_char;
712 } else {
713 add_char:
714 if (str)
715 cstr_ccat(str, c);
716 p++;
719 p++;
720 return p;
723 /* skip block of text until #else, #elif or #endif. skip also pairs of
724 #if/#endif */
725 static void preprocess_skip(void)
727 int a, start_of_line, c, in_warn_or_error;
728 uint8_t *p;
730 p = file->buf_ptr;
731 a = 0;
732 redo_start:
733 start_of_line = 1;
734 in_warn_or_error = 0;
735 for(;;) {
736 redo_no_start:
737 c = *p;
738 switch(c) {
739 case ' ':
740 case '\t':
741 case '\f':
742 case '\v':
743 case '\r':
744 p++;
745 goto redo_no_start;
746 case '\n':
747 file->line_num++;
748 p++;
749 goto redo_start;
750 case '\\':
751 file->buf_ptr = p;
752 c = handle_eob();
753 if (c == CH_EOF) {
754 expect("#endif");
755 } else if (c == '\\') {
756 ch = file->buf_ptr[0];
757 handle_stray_noerror();
759 p = file->buf_ptr;
760 goto redo_no_start;
761 /* skip strings */
762 case '\"':
763 case '\'':
764 if (in_warn_or_error)
765 goto _default;
766 p = parse_pp_string(p, c, NULL);
767 break;
768 /* skip comments */
769 case '/':
770 if (in_warn_or_error)
771 goto _default;
772 file->buf_ptr = p;
773 ch = *p;
774 minp();
775 p = file->buf_ptr;
776 if (ch == '*') {
777 p = parse_comment(p);
778 } else if (ch == '/') {
779 p = parse_line_comment(p);
781 break;
782 case '#':
783 p++;
784 if (start_of_line) {
785 file->buf_ptr = p;
786 next_nomacro();
787 p = file->buf_ptr;
788 if (a == 0 &&
789 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
790 goto the_end;
791 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
792 a++;
793 else if (tok == TOK_ENDIF)
794 a--;
795 else if( tok == TOK_ERROR || tok == TOK_WARNING)
796 in_warn_or_error = 1;
797 else if (tok == TOK_LINEFEED)
798 goto redo_start;
800 break;
801 _default:
802 default:
803 p++;
804 break;
806 start_of_line = 0;
808 the_end: ;
809 file->buf_ptr = p;
812 /* ParseState handling */
814 /* XXX: currently, no include file info is stored. Thus, we cannot display
815 accurate messages if the function or data definition spans multiple
816 files */
818 /* save current parse state in 's' */
819 ST_FUNC void save_parse_state(ParseState *s)
821 s->line_num = file->line_num;
822 s->macro_ptr = macro_ptr;
823 s->tok = tok;
824 s->tokc = tokc;
827 /* restore parse state from 's' */
828 ST_FUNC void restore_parse_state(ParseState *s)
830 file->line_num = s->line_num;
831 macro_ptr = s->macro_ptr;
832 tok = s->tok;
833 tokc = s->tokc;
836 /* return the number of additional 'ints' necessary to store the
837 token */
838 static inline int tok_ext_size(int t)
840 switch(t) {
841 /* 4 bytes */
842 case TOK_CINT:
843 case TOK_CUINT:
844 case TOK_CCHAR:
845 case TOK_LCHAR:
846 case TOK_CFLOAT:
847 case TOK_LINENUM:
848 return 1;
849 case TOK_STR:
850 case TOK_LSTR:
851 case TOK_PPNUM:
852 tcc_error("unsupported token");
853 return 1;
854 case TOK_CDOUBLE:
855 case TOK_CLLONG:
856 case TOK_CULLONG:
857 return 2;
858 case TOK_CLDOUBLE:
859 return LDOUBLE_SIZE / 4;
860 default:
861 return 0;
865 /* token string handling */
867 ST_INLN void tok_str_new(TokenString *s)
869 s->str = NULL;
870 s->len = 0;
871 s->allocated_len = 0;
872 s->last_line_num = -1;
875 ST_FUNC void tok_str_free(int *str)
877 tcc_free(str);
880 static int *tok_str_realloc(TokenString *s)
882 int *str, len;
884 if (s->allocated_len == 0) {
885 len = 8;
886 } else {
887 len = s->allocated_len * 2;
889 str = tcc_realloc(s->str, len * sizeof(int));
890 s->allocated_len = len;
891 s->str = str;
892 return str;
895 ST_FUNC void tok_str_add(TokenString *s, int t)
897 int len, *str;
899 len = s->len;
900 str = s->str;
901 if (len >= s->allocated_len)
902 str = tok_str_realloc(s);
903 str[len++] = t;
904 s->len = len;
907 static void tok_str_add2(TokenString *s, int t, CValue *cv)
909 int len, *str;
911 len = s->len;
912 str = s->str;
914 /* allocate space for worst case */
915 if (len + TOK_MAX_SIZE > s->allocated_len)
916 str = tok_str_realloc(s);
917 str[len++] = t;
918 switch(t) {
919 case TOK_CINT:
920 case TOK_CUINT:
921 case TOK_CCHAR:
922 case TOK_LCHAR:
923 case TOK_CFLOAT:
924 case TOK_LINENUM:
925 str[len++] = cv->tab[0];
926 break;
927 case TOK_PPNUM:
928 case TOK_STR:
929 case TOK_LSTR:
931 int nb_words;
932 CString *cstr;
934 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
935 while ((len + nb_words) > s->allocated_len)
936 str = tok_str_realloc(s);
937 cstr = (CString *)(str + len);
938 cstr->data = NULL;
939 cstr->size = cv->cstr->size;
940 cstr->data_allocated = NULL;
941 cstr->size_allocated = cstr->size;
942 memcpy((char *)cstr + sizeof(CString),
943 cv->cstr->data, cstr->size);
944 len += nb_words;
946 break;
947 case TOK_CDOUBLE:
948 case TOK_CLLONG:
949 case TOK_CULLONG:
950 #if LDOUBLE_SIZE == 8
951 case TOK_CLDOUBLE:
952 #endif
953 str[len++] = cv->tab[0];
954 str[len++] = cv->tab[1];
955 break;
956 #if LDOUBLE_SIZE == 12
957 case TOK_CLDOUBLE:
958 str[len++] = cv->tab[0];
959 str[len++] = cv->tab[1];
960 str[len++] = cv->tab[2];
961 #elif LDOUBLE_SIZE == 16
962 case TOK_CLDOUBLE:
963 str[len++] = cv->tab[0];
964 str[len++] = cv->tab[1];
965 str[len++] = cv->tab[2];
966 str[len++] = cv->tab[3];
967 #elif LDOUBLE_SIZE != 8
968 #error add long double size support
969 #endif
970 break;
971 default:
972 break;
974 s->len = len;
977 /* add the current parse token in token string 's' */
978 ST_FUNC void tok_str_add_tok(TokenString *s)
980 CValue cval;
982 /* save line number info */
983 if (file->line_num != s->last_line_num) {
984 s->last_line_num = file->line_num;
985 cval.i = s->last_line_num;
986 tok_str_add2(s, TOK_LINENUM, &cval);
988 tok_str_add2(s, tok, &tokc);
991 /* get a token from an integer array and increment pointer
992 accordingly. we code it as a macro to avoid pointer aliasing. */
993 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
995 const int *p = *pp;
996 int n, *tab;
998 tab = cv->tab;
999 switch(*t = *p++) {
1000 case TOK_CINT:
1001 case TOK_CUINT:
1002 case TOK_CCHAR:
1003 case TOK_LCHAR:
1004 case TOK_CFLOAT:
1005 case TOK_LINENUM:
1006 tab[0] = *p++;
1007 break;
1008 case TOK_STR:
1009 case TOK_LSTR:
1010 case TOK_PPNUM:
1011 cv->cstr = (CString *)p;
1012 cv->cstr->data = (char *)p + sizeof(CString);
1013 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1014 break;
1015 case TOK_CDOUBLE:
1016 case TOK_CLLONG:
1017 case TOK_CULLONG:
1018 n = 2;
1019 goto copy;
1020 case TOK_CLDOUBLE:
1021 #if LDOUBLE_SIZE == 16
1022 n = 4;
1023 #elif LDOUBLE_SIZE == 12
1024 n = 3;
1025 #elif LDOUBLE_SIZE == 8
1026 n = 2;
1027 #else
1028 # error add long double size support
1029 #endif
1030 copy:
1032 *tab++ = *p++;
1033 while (--n);
1034 break;
1035 default:
1036 break;
1038 *pp = p;
1041 static int macro_is_equal(const int *a, const int *b)
1043 char buf[STRING_MAX_SIZE + 1];
1044 CValue cv;
1045 int t;
1046 while (*a && *b) {
1047 TOK_GET(&t, &a, &cv);
1048 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1049 TOK_GET(&t, &b, &cv);
1050 if (strcmp(buf, get_tok_str(t, &cv)))
1051 return 0;
1053 return !(*a || *b);
1056 /* defines handling */
1057 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1059 Sym *s;
1060 CSym *def;
1061 s = define_find(v);
1062 if (s && !macro_is_equal(s->d, str))
1063 tcc_warning("%s redefined", get_tok_str(v, NULL));
1064 s = sym_push2(&define_stack, v, macro_type, 0);
1065 s->d = str;
1066 s->next = first_arg;
1067 def = &table_ident[v - TOK_IDENT]->sym_define;
1068 def->data[def->off] = s;
1071 /* undefined a define symbol. Its name is just set to zero */
1072 ST_FUNC void define_undef(Sym *s)
1074 int v;
1075 CSym *def;
1076 v = s->v - TOK_IDENT;
1077 if ((unsigned)v < (unsigned)(tok_ident - TOK_IDENT)){
1078 def = &table_ident[v]->sym_define;
1079 def->data[def->off] = NULL;
1083 ST_INLN Sym *define_find(int v)
1085 CSym *def;
1086 v -= TOK_IDENT;
1087 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1088 return NULL;
1089 def = &table_ident[v]->sym_define;
1090 return def->data[def->off];
1093 /* free define stack until top reaches 'b' */
1094 ST_FUNC void free_defines(Sym *b)
1096 Sym *top, *tmp;
1097 int v;
1098 CSym *def;
1100 top = define_stack;
1101 while (top != b) {
1102 tmp = top->prev;
1103 /* do not free args or predefined defines */
1104 if (top->d)
1105 tok_str_free(top->d);
1106 v = top->v - TOK_IDENT;
1107 if ((unsigned)v < (unsigned)(tok_ident - TOK_IDENT)){
1108 def = &table_ident[v]->sym_define;
1109 if(def->off)
1110 def->off = 0;
1111 if(def->data[0])
1112 def->data[0] = NULL;
1114 sym_free(top);
1115 top = tmp;
1117 define_stack = b;
1120 /* label lookup */
1121 ST_FUNC Sym *label_find(int v)
1123 v -= TOK_IDENT;
1124 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1125 return NULL;
1126 return table_ident[v]->sym_label;
1129 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1131 Sym *s, **ps;
1132 s = sym_push2(ptop, v, 0, 0);
1133 s->r = flags;
1134 ps = &table_ident[v - TOK_IDENT]->sym_label;
1135 if (ptop == &global_label_stack) {
1136 /* modify the top most local identifier, so that
1137 sym_identifier will point to 's' when popped */
1138 while (*ps != NULL)
1139 ps = &(*ps)->prev_tok;
1141 s->prev_tok = *ps;
1142 *ps = s;
1143 return s;
1146 /* pop labels until element last is reached. Look if any labels are
1147 undefined. Define symbols if '&&label' was used. */
1148 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1150 Sym *s, *s1;
1151 for(s = *ptop; s != slast; s = s1) {
1152 s1 = s->prev;
1153 if (s->r == LABEL_DECLARED) {
1154 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1155 } else if (s->r == LABEL_FORWARD) {
1156 tcc_error("label '%s' used but not defined",
1157 get_tok_str(s->v, NULL));
1158 } else {
1159 if (s->c) {
1160 /* define corresponding symbol. A size of
1161 1 is put. */
1162 put_extern_sym(s, cur_text_section, s->jnext, 1);
1165 /* remove label */
1166 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1167 sym_free(s);
1169 *ptop = slast;
1172 /* eval an expression for #if/#elif */
1173 static int expr_preprocess(void)
1175 int c, t;
1176 TokenString str;
1178 tok_str_new(&str);
1179 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1180 next(); /* do macro subst */
1181 if (tok == TOK_DEFINED) {
1182 next_nomacro();
1183 t = tok;
1184 if (t == '(')
1185 next_nomacro();
1186 c = define_find(tok) != 0;
1187 if (t == '(')
1188 next_nomacro();
1189 tok = TOK_CINT;
1190 tokc.i = c;
1191 } else if (tok >= TOK_IDENT) {
1192 /* if undefined macro */
1193 tok = TOK_CINT;
1194 tokc.i = 0;
1196 tok_str_add_tok(&str);
1198 tok_str_add(&str, -1); /* simulate end of file */
1199 tok_str_add(&str, 0);
1200 /* now evaluate C constant expression */
1201 macro_ptr = str.str;
1202 next();
1203 c = expr_const();
1204 macro_ptr = NULL;
1205 tok_str_free(str.str);
1206 return c != 0;
1209 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1210 static void tok_print(int *str)
1212 int t;
1213 CValue cval;
1215 printf("<");
1216 while (1) {
1217 TOK_GET(&t, &str, &cval);
1218 if (!t)
1219 break;
1220 printf("%s", get_tok_str(t, &cval));
1222 printf(">\n");
1224 #endif
1226 /* parse after #define */
1227 ST_FUNC void parse_define(void)
1229 Sym *s, *first, **ps;
1230 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1231 TokenString str;
1233 v = tok;
1234 if (v < TOK_IDENT)
1235 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1236 /* XXX: should check if same macro (ANSI) */
1237 first = NULL;
1238 t = MACRO_OBJ;
1239 /* '(' must be just after macro definition for MACRO_FUNC */
1240 next_nomacro_spc();
1241 if (tok == '(') {
1242 next_nomacro();
1243 ps = &first;
1244 while (tok != ')') {
1245 varg = tok;
1246 next_nomacro();
1247 is_vaargs = 0;
1248 if (varg == TOK_DOTS) {
1249 varg = TOK___VA_ARGS__;
1250 is_vaargs = 1;
1251 } else if (tok == TOK_DOTS && gnu_ext) {
1252 is_vaargs = 1;
1253 next_nomacro();
1255 if (varg < TOK_IDENT)
1256 tcc_error("badly punctuated parameter list");
1257 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1258 *ps = s;
1259 ps = &s->next;
1260 if (tok != ',')
1261 break;
1262 next_nomacro();
1264 if (tok == ')')
1265 next_nomacro_spc();
1266 t = MACRO_FUNC;
1268 tok_str_new(&str);
1269 spc = 2;
1270 /* EOF testing necessary for '-D' handling */
1271 ptok = 0;
1272 macro_list_start = 1;
1273 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1274 if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1275 tcc_error("'##' invalid at start of macro");
1276 ptok = tok;
1277 /* remove spaces around ## and after '#' */
1278 if (TOK_TWOSHARPS == tok) {
1279 if (1 == spc)
1280 --str.len;
1281 spc = 2;
1282 } else if ('#' == tok) {
1283 spc = 2;
1284 } else if (check_space(tok, &spc)) {
1285 goto skip;
1287 tok_str_add2(&str, tok, &tokc);
1288 skip:
1289 next_nomacro_spc();
1290 macro_list_start = 0;
1292 if (ptok == TOK_TWOSHARPS)
1293 tcc_error("'##' invalid at end of macro");
1294 if (spc == 1)
1295 --str.len; /* remove trailing space */
1296 tok_str_add(&str, 0);
1297 #ifdef PP_DEBUG
1298 printf("define %s %d: ", get_tok_str(v, NULL), t);
1299 tok_print(str.str);
1300 #endif
1301 define_push(v, t, str.str, first);
1304 static inline int hash_cached_include(const char *filename)
1306 const unsigned char *s;
1307 unsigned int h;
1309 h = TOK_HASH_INIT;
1310 s = (unsigned char *) filename;
1311 while (*s) {
1312 h = TOK_HASH_FUNC(h, *s);
1313 s++;
1315 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1316 return h;
1319 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1321 CachedInclude *e;
1322 int i, h;
1323 h = hash_cached_include(filename);
1324 i = s1->cached_includes_hash[h];
1325 for(;;) {
1326 if (i == 0)
1327 break;
1328 e = s1->cached_includes[i - 1];
1329 if (0 == PATHCMP(e->filename, filename))
1330 return e;
1331 i = e->hash_next;
1333 return NULL;
1336 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1338 CachedInclude *e;
1339 int h;
1341 if (search_cached_include(s1, filename))
1342 return;
1343 #ifdef INC_DEBUG
1344 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1345 #endif
1346 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1347 strcpy(e->filename, filename);
1348 e->ifndef_macro = ifndef_macro;
1349 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1350 /* add in hash table */
1351 h = hash_cached_include(filename);
1352 e->hash_next = s1->cached_includes_hash[h];
1353 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1356 /* is_bof is true if first non space token at beginning of file */
1357 ST_FUNC void preprocess(int is_bof)
1359 TCCState *s1 = tcc_state;
1360 int i, c, n, saved_parse_flags;
1361 uint8_t buf[1024], *p;
1362 Sym *s;
1364 saved_parse_flags = parse_flags;
1365 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_LINEFEED;
1366 next_nomacro();
1367 redo:
1368 switch(tok) {
1369 case TOK_DEFINE:
1370 next_nomacro();
1371 parse_define();
1372 break;
1373 case TOK_UNDEF:
1374 next_nomacro();
1375 s = define_find(tok);
1376 /* undefine symbol by putting an invalid name */
1377 if (s)
1378 define_undef(s);
1379 break;
1380 case TOK_INCLUDE:
1381 case TOK_INCLUDE_NEXT:
1382 ch = file->buf_ptr[0];
1383 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1384 skip_spaces();
1385 if (ch == '<') {
1386 c = '>';
1387 goto read_name;
1388 } else if (ch == '\"') {
1389 c = ch;
1390 read_name:
1391 inp();
1392 p = buf;
1393 while (ch != c && ch != '\n' && ch != CH_EOF) {
1394 if ((p - buf) < sizeof(buf) - 1)
1395 *p++ = ch;
1396 if (ch == '\\') {
1397 if (handle_stray_noerror() == 0)
1398 --p;
1399 } else
1400 inp();
1402 if (ch != c)
1403 goto include_syntax;
1404 *p = '\0';
1405 minp();
1406 #if 0
1407 /* eat all spaces and comments after include */
1408 /* XXX: slightly incorrect */
1409 while (ch1 != '\n' && ch1 != CH_EOF)
1410 inp();
1411 #endif
1412 } else {
1413 /* computed #include : either we have only strings or
1414 we have anything enclosed in '<>' */
1415 next();
1416 buf[0] = '\0';
1417 if (tok == TOK_STR) {
1418 while (tok != TOK_LINEFEED) {
1419 if (tok != TOK_STR) {
1420 include_syntax:
1421 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1423 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1424 next();
1426 c = '\"';
1427 } else {
1428 int len;
1429 while (tok != TOK_LINEFEED) {
1430 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1431 next();
1433 len = strlen(buf);
1434 /* check syntax and remove '<>' */
1435 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1436 goto include_syntax;
1437 memmove(buf, buf + 1, len - 2);
1438 buf[len - 2] = '\0';
1439 c = '>';
1442 if(!buf[0])
1443 tcc_error(" empty filename in #include");
1445 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1446 tcc_error("#include recursion too deep");
1447 /* store current file in stack, but increment stack later below */
1448 *s1->include_stack_ptr = file;
1450 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1451 for (i = -2; i < n; ++i) {
1452 char buf1[sizeof file->filename];
1453 CachedInclude *e;
1454 BufferedFile **f;
1455 const char *path;
1457 if (i == -2) {
1458 /* check absolute include path */
1459 if (!IS_ABSPATH(buf))
1460 continue;
1461 buf1[0] = 0;
1462 i = n; /* force end loop */
1464 } else if (i == -1) {
1465 /* search in current dir if "header.h" */
1466 if (c != '\"')
1467 continue;
1468 path = file->filename;
1469 pstrncpy(buf1, path, tcc_basename(path) - path);
1471 } else {
1472 /* search in all the include paths */
1473 if (i < s1->nb_include_paths)
1474 path = s1->include_paths[i];
1475 else
1476 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1477 pstrcpy(buf1, sizeof(buf1), path);
1478 pstrcat(buf1, sizeof(buf1), "/");
1481 pstrcat(buf1, sizeof(buf1), buf);
1483 if (tok == TOK_INCLUDE_NEXT)
1484 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1485 if (0 == PATHCMP((*f)->filename, buf1)) {
1486 #ifdef INC_DEBUG
1487 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1488 #endif
1489 goto include_trynext;
1492 e = search_cached_include(s1, buf1);
1493 if (e && define_find(e->ifndef_macro)) {
1494 /* no need to parse the include because the 'ifndef macro'
1495 is defined */
1496 #ifdef INC_DEBUG
1497 printf("%s: skipping cached %s\n", file->filename, buf1);
1498 #endif
1499 goto include_done;
1502 if (tcc_open(s1, buf1) < 0)
1503 include_trynext:
1504 continue;
1506 #ifdef INC_DEBUG
1507 printf("%s: including %s\n", file->prev->filename, file->filename);
1508 #endif
1509 /* update target deps */
1510 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps, tcc_strdup(buf1));
1511 /* push current file in stack */
1512 ++s1->include_stack_ptr;
1513 /* add include file debug info */
1514 if (s1->do_debug)
1515 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1516 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1517 ch = file->buf_ptr[0];
1518 goto the_end;
1520 tcc_error("include file '%s' not found", buf);
1521 include_done:
1522 break;
1523 case TOK_IFNDEF:
1524 c = 1;
1525 goto do_ifdef;
1526 case TOK_IF:
1527 c = expr_preprocess();
1528 goto do_if;
1529 case TOK_IFDEF:
1530 c = 0;
1531 do_ifdef:
1532 next_nomacro();
1533 if (tok < TOK_IDENT)
1534 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1535 if (is_bof) {
1536 if (c) {
1537 #ifdef INC_DEBUG
1538 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1539 #endif
1540 file->ifndef_macro = tok;
1543 c = !!define_find(tok) ^ c;
1544 do_if:
1545 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1546 tcc_error("memory full (ifdef)");
1547 *s1->ifdef_stack_ptr++ = c;
1548 goto test_skip;
1549 case TOK_ELSE:
1550 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1551 tcc_error("#else without matching #if");
1552 if (s1->ifdef_stack_ptr[-1] & 2)
1553 tcc_error("#else after #else");
1554 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1555 goto test_else;
1556 case TOK_ELIF:
1557 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1558 tcc_error("#elif without matching #if");
1559 c = s1->ifdef_stack_ptr[-1];
1560 if (c > 1)
1561 tcc_error("#elif after #else");
1562 /* last #if/#elif expression was true: we skip */
1563 if (c == 1)
1564 goto skip;
1565 c = expr_preprocess();
1566 s1->ifdef_stack_ptr[-1] = c;
1567 test_else:
1568 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1569 file->ifndef_macro = 0;
1570 test_skip:
1571 if (!(c & 1)) {
1572 skip:
1573 preprocess_skip();
1574 is_bof = 0;
1575 goto redo;
1577 break;
1578 case TOK_ENDIF:
1579 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1580 tcc_error("#endif without matching #if");
1581 s1->ifdef_stack_ptr--;
1582 /* '#ifndef macro' was at the start of file. Now we check if
1583 an '#endif' is exactly at the end of file */
1584 if (file->ifndef_macro &&
1585 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1586 file->ifndef_macro_saved = file->ifndef_macro;
1587 /* need to set to zero to avoid false matches if another
1588 #ifndef at middle of file */
1589 file->ifndef_macro = 0;
1590 tok_flags |= TOK_FLAG_ENDIF;
1592 next_nomacro();
1593 if (tok != TOK_LINEFEED)
1594 tcc_warning("Ignoring: %s", get_tok_str(tok, &tokc));
1595 break;
1596 case TOK_LINE:
1597 next();
1598 if (tok != TOK_CINT)
1599 tcc_error("#line");
1600 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1601 next();
1602 if (tok != TOK_LINEFEED) {
1603 if (tok != TOK_STR)
1604 tcc_error("#line");
1605 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.cstr->data);
1607 break;
1608 case TOK_ERROR:
1609 case TOK_WARNING:
1610 c = tok;
1611 ch = file->buf_ptr[0];
1612 skip_spaces();
1613 p = buf;
1614 while (ch != '\n' && ch != CH_EOF) {
1615 if ((p - buf) < sizeof(buf) - 1)
1616 *p++ = ch;
1617 if (ch == '\\') {
1618 if (handle_stray_noerror() == 0)
1619 --p;
1620 } else
1621 inp();
1623 *p = '\0';
1624 if (c == TOK_ERROR)
1625 tcc_error("#error %s", buf);
1626 else
1627 tcc_warning("#warning %s", buf);
1628 break;
1629 case TOK_PRAGMA:
1630 next();
1631 if (tok == TOK_pack && s1->output_type != TCC_OUTPUT_PREPROCESS) {
1633 This may be:
1634 #pragma pack(1) // set
1635 #pragma pack() // reset to default
1636 #pragma pack(push,1) // push & set
1637 #pragma pack(pop) // restore previous
1639 next();
1640 skip('(');
1641 if (tok == TOK_ASM_pop) {
1642 next();
1643 if (s1->pack_stack_ptr <= s1->pack_stack) {
1644 stk_error:
1645 tcc_error("out of pack stack");
1647 s1->pack_stack_ptr--;
1648 } else {
1649 int val = 0;
1650 if (tok != ')') {
1651 if (tok == TOK_ASM_push) {
1652 next();
1653 s1->pack_stack_ptr++;
1654 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE)
1655 goto stk_error;
1656 skip(',');
1658 if (tok != TOK_CINT) {
1659 pack_error:
1660 tcc_error("invalid pack pragma");
1662 val = tokc.i;
1663 if (val < 1 || val > 16)
1664 goto pack_error;
1665 if (val < 1 || val > 16)
1666 tcc_error("Value must be greater than 1 is less than or equal to 16");
1667 if ((val & (val - 1)) != 0)
1668 tcc_error("Value must be a power of 2 curtain");
1669 next();
1671 *s1->pack_stack_ptr = val;
1672 skip(')');
1674 }else if (tok == TOK_PUSH_MACRO || tok == TOK_POP_MACRO) {
1675 TokenSym *ts;
1676 CSym *def;
1677 uint8_t *p1;
1678 int len, t;
1679 t = tok;
1680 ch = file->buf_ptr[0];
1681 skip_spaces();
1682 if (ch != '(')
1683 goto macro_xxx_syntax;
1684 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1685 inp();
1686 skip_spaces();
1687 if (ch == '\"'){
1688 inp();
1689 p = buf;
1690 while (ch != '\"' && ch != '\n' && ch != CH_EOF) {
1691 if ((p - buf) < sizeof(buf) - 1)
1692 *p++ = ch;
1693 if (ch == CH_EOB) {
1694 --p;
1695 handle_stray();
1696 }else
1697 inp();
1699 if(ch != '\"')
1700 goto macro_xxx_syntax;
1701 *p = '\0';
1702 minp();
1703 next();
1704 }else{
1705 /* computed #pragma macro_xxx for #define xxx */
1706 next();
1707 buf[0] = '\0';
1708 while (tok != ')') {
1709 if (tok != TOK_STR) {
1710 macro_xxx_syntax:
1711 tcc_error("'macro_xxx' expects (\"NAME\")");
1713 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1714 next();
1717 skip (')');
1718 if(!buf[0])
1719 tcc_error(" empty string in #pragma");
1720 /* find TokenSym */
1721 p = buf;
1722 while (is_space(*p))
1723 p++;
1724 p1 = p;
1725 for(;;){
1726 if (!isidnum_table[p[0] - CH_EOF])
1727 break;
1728 ++p;
1730 len = p - p1;
1731 while (is_space(*p))
1732 p++;
1733 if(!p) //'\0'
1734 tcc_error("unrecognized string: %s", buf);
1735 ts = tok_alloc(p1, len);
1736 if(ts){
1737 def = &ts->sym_define;
1738 if(t == TOK_PUSH_MACRO){
1739 void *tmp = def->data[def->off];
1740 def->off++;
1741 if(def->off >= def->size){
1742 int size = def->size;
1743 size *= 2;
1744 if (size > MACRO_STACK_SIZE)
1745 tcc_error("stack full");
1746 def->data = tcc_realloc(def->data, size*sizeof(Sym*));
1747 def->size = size;
1749 def->data[def->off] = tmp;
1750 }else{
1751 if(def->off){
1752 --def->off;
1756 }else if(s1->output_type == TCC_OUTPUT_PREPROCESS){
1757 fputs("#pragma ", s1->ppfp);
1758 while (tok != TOK_LINEFEED){
1759 fputs(get_tok_str(tok, &tokc), s1->ppfp);
1760 next();
1762 token_seen = 0;//printf '\n'
1763 goto the_end;
1765 break;
1766 default:
1767 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1768 /* '!' is ignored to allow C scripts. numbers are ignored
1769 to emulate cpp behaviour */
1770 } else {
1771 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1772 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1773 else {
1774 /* this is a gas line comment in an 'S' file. */
1775 file->buf_ptr = parse_line_comment(file->buf_ptr);
1776 goto the_end;
1779 break;
1781 /* ignore other preprocess commands or #! for C scripts */
1782 while (tok != TOK_LINEFEED)
1783 next_nomacro();
1784 the_end:
1785 parse_flags = saved_parse_flags;
1788 /* evaluate escape codes in a string. */
1789 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1791 int c, n;
1792 const uint8_t *p;
1794 p = buf;
1795 for(;;) {
1796 c = *p;
1797 if (c == '\0')
1798 break;
1799 if (c == '\\') {
1800 p++;
1801 /* escape */
1802 c = *p;
1803 switch(c) {
1804 case '0': case '1': case '2': case '3':
1805 case '4': case '5': case '6': case '7':
1806 /* at most three octal digits */
1807 n = c - '0';
1808 p++;
1809 c = *p;
1810 if (isoct(c)) {
1811 n = n * 8 + c - '0';
1812 p++;
1813 c = *p;
1814 if (isoct(c)) {
1815 n = n * 8 + c - '0';
1816 p++;
1819 c = n;
1820 goto add_char_nonext;
1821 case 'x':
1822 case 'u':
1823 case 'U':
1824 p++;
1825 n = 0;
1826 for(;;) {
1827 c = *p;
1828 if (c >= 'a' && c <= 'f')
1829 c = c - 'a' + 10;
1830 else if (c >= 'A' && c <= 'F')
1831 c = c - 'A' + 10;
1832 else if (isnum(c))
1833 c = c - '0';
1834 else
1835 break;
1836 n = n * 16 + c;
1837 p++;
1839 c = n;
1840 goto add_char_nonext;
1841 case 'a':
1842 c = '\a';
1843 break;
1844 case 'b':
1845 c = '\b';
1846 break;
1847 case 'f':
1848 c = '\f';
1849 break;
1850 case 'n':
1851 c = '\n';
1852 break;
1853 case 'r':
1854 c = '\r';
1855 break;
1856 case 't':
1857 c = '\t';
1858 break;
1859 case 'v':
1860 c = '\v';
1861 break;
1862 case 'e':
1863 if (!gnu_ext)
1864 goto invalid_escape;
1865 c = 27;
1866 break;
1867 case '\'':
1868 case '\"':
1869 case '\\':
1870 case '?':
1871 break;
1872 default:
1873 invalid_escape:
1874 if (c >= '!' && c <= '~')
1875 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1876 else
1877 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1878 break;
1881 p++;
1882 add_char_nonext:
1883 if (!is_long)
1884 cstr_ccat(outstr, c);
1885 else
1886 cstr_wccat(outstr, c);
1888 /* add a trailing '\0' */
1889 if (!is_long)
1890 cstr_ccat(outstr, '\0');
1891 else
1892 cstr_wccat(outstr, '\0');
1895 /* we use 64 bit numbers */
1896 #define BN_SIZE 2
1898 /* bn = (bn << shift) | or_val */
1899 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1901 int i;
1902 unsigned int v;
1903 for(i=0;i<BN_SIZE;i++) {
1904 v = bn[i];
1905 bn[i] = (v << shift) | or_val;
1906 or_val = v >> (32 - shift);
1910 static void bn_zero(unsigned int *bn)
1912 int i;
1913 for(i=0;i<BN_SIZE;i++) {
1914 bn[i] = 0;
1918 /* parse number in null terminated string 'p' and return it in the
1919 current token */
1920 static void parse_number(const char *p)
1922 int b, t, shift, frac_bits, s, exp_val, ch;
1923 char *q;
1924 unsigned int bn[BN_SIZE];
1925 double d;
1927 /* number */
1928 q = token_buf;
1929 ch = *p++;
1930 t = ch;
1931 ch = *p++;
1932 *q++ = t;
1933 b = 10;
1934 if (t == '.') {
1935 goto float_frac_parse;
1936 } else if (t == '0') {
1937 if (ch == 'x' || ch == 'X') {
1938 q--;
1939 ch = *p++;
1940 b = 16;
1941 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1942 q--;
1943 ch = *p++;
1944 b = 2;
1947 /* parse all digits. cannot check octal numbers at this stage
1948 because of floating point constants */
1949 while (1) {
1950 if (ch >= 'a' && ch <= 'f')
1951 t = ch - 'a' + 10;
1952 else if (ch >= 'A' && ch <= 'F')
1953 t = ch - 'A' + 10;
1954 else if (isnum(ch))
1955 t = ch - '0';
1956 else
1957 break;
1958 if (t >= b)
1959 break;
1960 if (q >= token_buf + STRING_MAX_SIZE) {
1961 num_too_long:
1962 tcc_error("number too long");
1964 *q++ = ch;
1965 ch = *p++;
1967 if (ch == '.' ||
1968 ((ch == 'e' || ch == 'E') && b == 10) ||
1969 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1970 if (b != 10) {
1971 /* NOTE: strtox should support that for hexa numbers, but
1972 non ISOC99 libcs do not support it, so we prefer to do
1973 it by hand */
1974 /* hexadecimal or binary floats */
1975 /* XXX: handle overflows */
1976 *q = '\0';
1977 if (b == 16)
1978 shift = 4;
1979 else
1980 shift = 2;
1981 bn_zero(bn);
1982 q = token_buf;
1983 while (1) {
1984 t = *q++;
1985 if (t == '\0') {
1986 break;
1987 } else if (t >= 'a') {
1988 t = t - 'a' + 10;
1989 } else if (t >= 'A') {
1990 t = t - 'A' + 10;
1991 } else {
1992 t = t - '0';
1994 bn_lshift(bn, shift, t);
1996 frac_bits = 0;
1997 if (ch == '.') {
1998 ch = *p++;
1999 while (1) {
2000 t = ch;
2001 if (t >= 'a' && t <= 'f') {
2002 t = t - 'a' + 10;
2003 } else if (t >= 'A' && t <= 'F') {
2004 t = t - 'A' + 10;
2005 } else if (t >= '0' && t <= '9') {
2006 t = t - '0';
2007 } else {
2008 break;
2010 if (t >= b)
2011 tcc_error("invalid digit");
2012 bn_lshift(bn, shift, t);
2013 frac_bits += shift;
2014 ch = *p++;
2017 if (ch != 'p' && ch != 'P')
2018 expect("exponent");
2019 ch = *p++;
2020 s = 1;
2021 exp_val = 0;
2022 if (ch == '+') {
2023 ch = *p++;
2024 } else if (ch == '-') {
2025 s = -1;
2026 ch = *p++;
2028 if (ch < '0' || ch > '9')
2029 expect("exponent digits");
2030 while (ch >= '0' && ch <= '9') {
2031 exp_val = exp_val * 10 + ch - '0';
2032 ch = *p++;
2034 exp_val = exp_val * s;
2036 /* now we can generate the number */
2037 /* XXX: should patch directly float number */
2038 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2039 d = ldexp(d, exp_val - frac_bits);
2040 t = toup(ch);
2041 if (t == 'F') {
2042 ch = *p++;
2043 tok = TOK_CFLOAT;
2044 /* float : should handle overflow */
2045 tokc.f = (float)d;
2046 } else if (t == 'L') {
2047 ch = *p++;
2048 #ifdef TCC_TARGET_PE
2049 tok = TOK_CDOUBLE;
2050 tokc.d = d;
2051 #else
2052 tok = TOK_CLDOUBLE;
2053 /* XXX: not large enough */
2054 tokc.ld = (long double)d;
2055 #endif
2056 } else {
2057 tok = TOK_CDOUBLE;
2058 tokc.d = d;
2060 } else {
2061 /* decimal floats */
2062 if (ch == '.') {
2063 if (q >= token_buf + STRING_MAX_SIZE)
2064 goto num_too_long;
2065 *q++ = ch;
2066 ch = *p++;
2067 float_frac_parse:
2068 while (ch >= '0' && ch <= '9') {
2069 if (q >= token_buf + STRING_MAX_SIZE)
2070 goto num_too_long;
2071 *q++ = ch;
2072 ch = *p++;
2075 if (ch == 'e' || ch == 'E') {
2076 if (q >= token_buf + STRING_MAX_SIZE)
2077 goto num_too_long;
2078 *q++ = ch;
2079 ch = *p++;
2080 if (ch == '-' || ch == '+') {
2081 if (q >= token_buf + STRING_MAX_SIZE)
2082 goto num_too_long;
2083 *q++ = ch;
2084 ch = *p++;
2086 if (ch < '0' || ch > '9')
2087 expect("exponent digits");
2088 while (ch >= '0' && ch <= '9') {
2089 if (q >= token_buf + STRING_MAX_SIZE)
2090 goto num_too_long;
2091 *q++ = ch;
2092 ch = *p++;
2095 *q = '\0';
2096 t = toup(ch);
2097 errno = 0;
2098 if (t == 'F') {
2099 ch = *p++;
2100 tok = TOK_CFLOAT;
2101 tokc.f = strtof(token_buf, NULL);
2102 } else if (t == 'L') {
2103 ch = *p++;
2104 #ifdef TCC_TARGET_PE
2105 tok = TOK_CDOUBLE;
2106 tokc.d = strtod(token_buf, NULL);
2107 #else
2108 tok = TOK_CLDOUBLE;
2109 tokc.ld = strtold(token_buf, NULL);
2110 #endif
2111 } else {
2112 tok = TOK_CDOUBLE;
2113 tokc.d = strtod(token_buf, NULL);
2116 } else {
2117 unsigned long long n, n1;
2118 int lcount, ucount;
2120 /* integer number */
2121 *q = '\0';
2122 q = token_buf;
2123 if (b == 10 && *q == '0') {
2124 b = 8;
2125 q++;
2127 n = 0;
2128 while(1) {
2129 t = *q++;
2130 /* no need for checks except for base 10 / 8 errors */
2131 if (t == '\0') {
2132 break;
2133 } else if (t >= 'a') {
2134 t = t - 'a' + 10;
2135 } else if (t >= 'A') {
2136 t = t - 'A' + 10;
2137 } else {
2138 t = t - '0';
2139 if (t >= b)
2140 tcc_error("invalid digit");
2142 n1 = n;
2143 n = n * b + t;
2144 /* detect overflow */
2145 /* XXX: this test is not reliable */
2146 if (n < n1)
2147 tcc_error("integer constant overflow");
2150 /* XXX: not exactly ANSI compliant */
2151 if ((n & 0xffffffff00000000LL) != 0) {
2152 if ((n >> 63) != 0)
2153 tok = TOK_CULLONG;
2154 else
2155 tok = TOK_CLLONG;
2156 } else if (n > 0x7fffffff) {
2157 tok = TOK_CUINT;
2158 } else {
2159 tok = TOK_CINT;
2161 lcount = 0;
2162 ucount = 0;
2163 for(;;) {
2164 t = toup(ch);
2165 if (t == 'L') {
2166 if (lcount >= 2)
2167 tcc_error("three 'l's in integer constant");
2168 lcount++;
2169 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2170 if (lcount == 2) {
2171 #endif
2172 if (tok == TOK_CINT)
2173 tok = TOK_CLLONG;
2174 else if (tok == TOK_CUINT)
2175 tok = TOK_CULLONG;
2176 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2178 #endif
2179 ch = *p++;
2180 } else if (t == 'U') {
2181 if (ucount >= 1)
2182 tcc_error("two 'u's in integer constant");
2183 ucount++;
2184 if (tok == TOK_CINT)
2185 tok = TOK_CUINT;
2186 else if (tok == TOK_CLLONG)
2187 tok = TOK_CULLONG;
2188 ch = *p++;
2189 } else {
2190 break;
2193 if (tok == TOK_CINT || tok == TOK_CUINT)
2194 tokc.ui = n;
2195 else
2196 tokc.ull = n;
2198 if (ch)
2199 tcc_error("invalid number\n");
2203 #define PARSE2(c1, tok1, c2, tok2) \
2204 case c1: \
2205 PEEKC(c, p); \
2206 if (c == c2) { \
2207 p++; \
2208 tok = tok2; \
2209 } else { \
2210 tok = tok1; \
2212 break;
2214 /* return next token without macro substitution */
2215 static inline void next_nomacro1(void)
2217 int t, c, is_long;
2218 TokenSym *ts;
2219 uint8_t *p, *p1;
2220 unsigned int h;
2222 p = file->buf_ptr;
2223 redo_no_start:
2224 c = *p;
2225 switch(c) {
2226 case ' ':
2227 case '\t':
2228 tok = c;
2229 p++;
2230 goto keep_tok_flags;
2231 case '\f':
2232 case '\v':
2233 case '\r':
2234 p++;
2235 goto redo_no_start;
2236 case '\\':
2237 /* first look if it is in fact an end of buffer */
2238 if (p >= file->buf_end) {
2239 file->buf_ptr = p;
2240 handle_eob();
2241 p = file->buf_ptr;
2242 if (p >= file->buf_end)
2243 goto parse_eof;
2244 else
2245 goto redo_no_start;
2246 } else {
2247 file->buf_ptr = p;
2248 ch = *p;
2249 handle_stray();
2250 p = file->buf_ptr;
2251 goto redo_no_start;
2253 parse_eof:
2255 TCCState *s1 = tcc_state;
2256 if ((parse_flags & PARSE_FLAG_LINEFEED)
2257 && !(tok_flags & TOK_FLAG_EOF)) {
2258 tok_flags |= TOK_FLAG_EOF;
2259 tok = TOK_LINEFEED;
2260 goto keep_tok_flags;
2261 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2262 tok = TOK_EOF;
2263 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2264 tcc_error("missing #endif");
2265 } else if (s1->include_stack_ptr == s1->include_stack) {
2266 /* no include left : end of file. */
2267 tok = TOK_EOF;
2268 } else {
2269 tok_flags &= ~TOK_FLAG_EOF;
2270 /* pop include file */
2272 /* test if previous '#endif' was after a #ifdef at
2273 start of file */
2274 if (tok_flags & TOK_FLAG_ENDIF) {
2275 #ifdef INC_DEBUG
2276 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2277 #endif
2278 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2279 tok_flags &= ~TOK_FLAG_ENDIF;
2282 /* add end of include file debug info */
2283 if (tcc_state->do_debug) {
2284 put_stabd(N_EINCL, 0, 0);
2286 /* pop include stack */
2287 tcc_close();
2288 s1->include_stack_ptr--;
2289 p = file->buf_ptr;
2290 goto redo_no_start;
2293 break;
2295 case '\n':
2296 file->line_num++;
2297 tok_flags |= TOK_FLAG_BOL;
2298 p++;
2299 maybe_newline:
2300 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2301 goto redo_no_start;
2302 tok = TOK_LINEFEED;
2303 goto keep_tok_flags;
2305 case '#':
2306 /* XXX: simplify */
2307 PEEKC(c, p);
2308 if ((tok_flags & TOK_FLAG_BOL) &&
2309 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2310 file->buf_ptr = p;
2311 preprocess(tok_flags & TOK_FLAG_BOF);
2312 p = file->buf_ptr;
2313 goto maybe_newline;
2314 } else {
2315 if (c == '#') {
2316 p++;
2317 tok = TOK_TWOSHARPS;
2318 } else {
2319 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2320 p = parse_line_comment(p - 1);
2321 goto redo_no_start;
2322 } else {
2323 tok = '#';
2327 break;
2329 case 'a': case 'b': case 'c': case 'd':
2330 case 'e': case 'f': case 'g': case 'h':
2331 case 'i': case 'j': case 'k': case 'l':
2332 case 'm': case 'n': case 'o': case 'p':
2333 case 'q': case 'r': case 's': case 't':
2334 case 'u': case 'v': case 'w': case 'x':
2335 case 'y': case 'z':
2336 case 'A': case 'B': case 'C': case 'D':
2337 case 'E': case 'F': case 'G': case 'H':
2338 case 'I': case 'J': case 'K':
2339 case 'M': case 'N': case 'O': case 'P':
2340 case 'Q': case 'R': case 'S': case 'T':
2341 case 'U': case 'V': case 'W': case 'X':
2342 case 'Y': case 'Z':
2343 case '_':
2344 parse_ident_fast:
2345 p1 = p;
2346 h = TOK_HASH_INIT;
2347 h = TOK_HASH_FUNC(h, c);
2348 p++;
2349 for(;;) {
2350 c = *p;
2351 if (!isidnum_table[c-CH_EOF])
2352 break;
2353 h = TOK_HASH_FUNC(h, c);
2354 p++;
2356 if (c != '\\') {
2357 TokenSym **pts;
2358 int len;
2360 /* fast case : no stray found, so we have the full token
2361 and we have already hashed it */
2362 len = p - p1;
2363 h &= (TOK_HASH_SIZE - 1);
2364 pts = &hash_ident[h];
2365 for(;;) {
2366 ts = *pts;
2367 if (!ts)
2368 break;
2369 if (ts->len == len && !memcmp(ts->str, p1, len))
2370 goto token_found;
2371 pts = &(ts->hash_next);
2373 ts = tok_alloc_new(pts, (char *) p1, len);
2374 token_found: ;
2375 } else {
2376 /* slower case */
2377 cstr_reset(&tokcstr);
2379 while (p1 < p) {
2380 cstr_ccat(&tokcstr, *p1);
2381 p1++;
2383 p--;
2384 PEEKC(c, p);
2385 parse_ident_slow:
2386 while (isidnum_table[c-CH_EOF]) {
2387 cstr_ccat(&tokcstr, c);
2388 PEEKC(c, p);
2390 ts = tok_alloc(tokcstr.data, tokcstr.size);
2392 tok = ts->tok;
2393 break;
2394 case 'L':
2395 t = p[1];
2396 if (t != '\\' && t != '\'' && t != '\"') {
2397 /* fast case */
2398 goto parse_ident_fast;
2399 } else {
2400 PEEKC(c, p);
2401 if (c == '\'' || c == '\"') {
2402 is_long = 1;
2403 goto str_const;
2404 } else {
2405 cstr_reset(&tokcstr);
2406 cstr_ccat(&tokcstr, 'L');
2407 goto parse_ident_slow;
2410 break;
2411 case '0': case '1': case '2': case '3':
2412 case '4': case '5': case '6': case '7':
2413 case '8': case '9':
2415 cstr_reset(&tokcstr);
2416 /* after the first digit, accept digits, alpha, '.' or sign if
2417 prefixed by 'eEpP' */
2418 parse_num:
2419 for(;;) {
2420 t = c;
2421 cstr_ccat(&tokcstr, c);
2422 PEEKC(c, p);
2423 if (!(isnum(c) || isid(c) || c == '.' ||
2424 ((c == '+' || c == '-') &&
2425 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2426 break;
2428 /* We add a trailing '\0' to ease parsing */
2429 cstr_ccat(&tokcstr, '\0');
2430 tokc.cstr = &tokcstr;
2431 tok = TOK_PPNUM;
2432 break;
2433 case '.':
2434 /* special dot handling because it can also start a number */
2435 PEEKC(c, p);
2436 if (isnum(c)) {
2437 cstr_reset(&tokcstr);
2438 cstr_ccat(&tokcstr, '.');
2439 goto parse_num;
2440 } else if (c == '.') {
2441 PEEKC(c, p);
2442 if (c != '.')
2443 expect("'.'");
2444 PEEKC(c, p);
2445 tok = TOK_DOTS;
2446 } else {
2447 tok = '.';
2449 break;
2450 case '\'':
2451 case '\"':
2452 is_long = 0;
2453 str_const:
2455 CString str;
2456 int sep;
2458 sep = c;
2460 /* parse the string */
2461 cstr_new(&str);
2462 p = parse_pp_string(p, sep, &str);
2463 cstr_ccat(&str, '\0');
2465 /* eval the escape (should be done as TOK_PPNUM) */
2466 cstr_reset(&tokcstr);
2467 parse_escape_string(&tokcstr, str.data, is_long);
2468 cstr_free(&str);
2470 if (sep == '\'') {
2471 int char_size;
2472 /* XXX: make it portable */
2473 if (!is_long)
2474 char_size = 1;
2475 else
2476 char_size = sizeof(nwchar_t);
2477 if (tokcstr.size <= char_size)
2478 tcc_error("empty character constant");
2479 if (tokcstr.size > 2 * char_size)
2480 tcc_warning("multi-character character constant");
2481 if (!is_long) {
2482 tokc.i = *(int8_t *)tokcstr.data;
2483 tok = TOK_CCHAR;
2484 } else {
2485 tokc.i = *(nwchar_t *)tokcstr.data;
2486 tok = TOK_LCHAR;
2488 } else {
2489 tokc.cstr = &tokcstr;
2490 if (!is_long)
2491 tok = TOK_STR;
2492 else
2493 tok = TOK_LSTR;
2496 break;
2498 case '<':
2499 PEEKC(c, p);
2500 if (c == '=') {
2501 p++;
2502 tok = TOK_LE;
2503 } else if (c == '<') {
2504 PEEKC(c, p);
2505 if (c == '=') {
2506 p++;
2507 tok = TOK_A_SHL;
2508 } else {
2509 tok = TOK_SHL;
2511 } else {
2512 tok = TOK_LT;
2514 break;
2516 case '>':
2517 PEEKC(c, p);
2518 if (c == '=') {
2519 p++;
2520 tok = TOK_GE;
2521 } else if (c == '>') {
2522 PEEKC(c, p);
2523 if (c == '=') {
2524 p++;
2525 tok = TOK_A_SAR;
2526 } else {
2527 tok = TOK_SAR;
2529 } else {
2530 tok = TOK_GT;
2532 break;
2534 case '&':
2535 PEEKC(c, p);
2536 if (c == '&') {
2537 p++;
2538 tok = TOK_LAND;
2539 } else if (c == '=') {
2540 p++;
2541 tok = TOK_A_AND;
2542 } else {
2543 tok = '&';
2545 break;
2547 case '|':
2548 PEEKC(c, p);
2549 if (c == '|') {
2550 p++;
2551 tok = TOK_LOR;
2552 } else if (c == '=') {
2553 p++;
2554 tok = TOK_A_OR;
2555 } else {
2556 tok = '|';
2558 break;
2560 case '+':
2561 PEEKC(c, p);
2562 if (c == '+') {
2563 p++;
2564 tok = TOK_INC;
2565 } else if (c == '=') {
2566 p++;
2567 tok = TOK_A_ADD;
2568 } else {
2569 tok = '+';
2571 break;
2573 case '-':
2574 PEEKC(c, p);
2575 if (c == '-') {
2576 p++;
2577 tok = TOK_DEC;
2578 } else if (c == '=') {
2579 p++;
2580 tok = TOK_A_SUB;
2581 } else if (c == '>') {
2582 p++;
2583 tok = TOK_ARROW;
2584 } else {
2585 tok = '-';
2587 break;
2589 PARSE2('!', '!', '=', TOK_NE)
2590 PARSE2('=', '=', '=', TOK_EQ)
2591 PARSE2('*', '*', '=', TOK_A_MUL)
2592 PARSE2('%', '%', '=', TOK_A_MOD)
2593 PARSE2('^', '^', '=', TOK_A_XOR)
2595 /* comments or operator */
2596 case '/':
2597 PEEKC(c, p);
2598 if (c == '*') {
2599 p = parse_comment(p);
2600 /* comments replaced by a blank */
2601 tok = ' ';
2602 goto keep_tok_flags;
2603 } else if (c == '/') {
2604 p = parse_line_comment(p);
2605 tok = ' ';
2606 goto keep_tok_flags;
2607 } else if (c == '=') {
2608 p++;
2609 tok = TOK_A_DIV;
2610 } else {
2611 tok = '/';
2613 break;
2615 /* simple tokens */
2616 case '(':
2617 case ')':
2618 case '[':
2619 case ']':
2620 case '{':
2621 case '}':
2622 case ',':
2623 case ';':
2624 case ':':
2625 case '?':
2626 case '~':
2627 case '$': /* only used in assembler */
2628 case '@': /* dito */
2629 tok = c;
2630 p++;
2631 break;
2632 default:
2633 tcc_error("unrecognized character \\x%02x", c);
2634 break;
2636 tok_flags = 0;
2637 keep_tok_flags:
2638 file->buf_ptr = p;
2639 #if defined(PARSE_DEBUG)
2640 printf("token = %s\n", get_tok_str(tok, &tokc));
2641 #endif
2644 /* return next token without macro substitution. Can read input from
2645 macro_ptr buffer */
2646 static void next_nomacro_spc(void)
2648 if (macro_ptr) {
2649 redo:
2650 tok = *macro_ptr;
2651 if (tok) {
2652 TOK_GET(&tok, &macro_ptr, &tokc);
2653 if (tok == TOK_LINENUM) {
2654 file->line_num = tokc.i;
2655 goto redo;
2658 } else {
2659 next_nomacro1();
2663 ST_FUNC void next_nomacro(void)
2665 do {
2666 next_nomacro_spc();
2667 } while (is_space(tok));
2670 /* substitute arguments in replacement lists in macro_str by the values in
2671 args (field d) and return allocated string */
2672 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2674 int last_tok, t, spc;
2675 const int *st;
2676 Sym *s;
2677 CValue cval;
2678 TokenString str;
2679 CString cstr;
2681 tok_str_new(&str);
2682 last_tok = 0;
2683 while(1) {
2684 TOK_GET(&t, &macro_str, &cval);
2685 if (!t)
2686 break;
2687 if (t == '#') {
2688 /* stringize */
2689 TOK_GET(&t, &macro_str, &cval);
2690 if (!t)
2691 break;
2692 s = sym_find2(args, t);
2693 if (s) {
2694 cstr_new(&cstr);
2695 st = s->d;
2696 spc = 0;
2697 while (*st) {
2698 TOK_GET(&t, &st, &cval);
2699 if (!check_space(t, &spc))
2700 cstr_cat(&cstr, get_tok_str(t, &cval));
2702 cstr.size -= spc;
2703 cstr_ccat(&cstr, '\0');
2704 #ifdef PP_DEBUG
2705 printf("stringize: %s\n", (char *)cstr.data);
2706 #endif
2707 /* add string */
2708 cval.cstr = &cstr;
2709 tok_str_add2(&str, TOK_STR, &cval);
2710 cstr_free(&cstr);
2711 } else {
2712 tok_str_add2(&str, t, &cval);
2714 } else if (t >= TOK_IDENT) {
2715 s = sym_find2(args, t);
2716 if (s) {
2717 st = s->d;
2718 /* if '##' is present before or after, no arg substitution */
2719 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2720 /* special case for var arg macros : ## eats the
2721 ',' if empty VA_ARGS variable. */
2722 /* XXX: test of the ',' is not 100%
2723 reliable. should fix it to avoid security
2724 problems */
2725 if (gnu_ext && s->type.t &&
2726 last_tok == TOK_TWOSHARPS &&
2727 str.len >= 2 && str.str[str.len - 2] == ',') {
2728 if (*st == TOK_PLCHLDR) {
2729 /* suppress ',' '##' */
2730 str.len -= 2;
2731 } else {
2732 /* suppress '##' and add variable */
2733 str.len--;
2734 goto add_var;
2736 } else {
2737 int t1;
2738 add_var:
2739 for(;;) {
2740 TOK_GET(&t1, &st, &cval);
2741 if (!t1)
2742 break;
2743 tok_str_add2(&str, t1, &cval);
2746 } else {
2747 /* NOTE: the stream cannot be read when macro
2748 substituing an argument */
2749 macro_subst(&str, nested_list, st, NULL);
2751 } else {
2752 tok_str_add(&str, t);
2754 } else {
2755 tok_str_add2(&str, t, &cval);
2757 last_tok = t;
2759 tok_str_add(&str, 0);
2760 return str.str;
2763 static char const ab_month_name[12][4] =
2765 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2766 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2769 /* do macro substitution of current token with macro 's' and add
2770 result to (tok_str,tok_len). 'nested_list' is the list of all
2771 macros we got inside to avoid recursing. Return non zero if no
2772 substitution needs to be done */
2773 static int macro_subst_tok(TokenString *tok_str,
2774 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2776 Sym *args, *sa, *sa1;
2777 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2778 const int *p;
2779 TokenString str;
2780 char *cstrval;
2781 CValue cval;
2782 CString cstr;
2783 char buf[32];
2785 /* if symbol is a macro, prepare substitution */
2786 /* special macros */
2787 if (tok == TOK___LINE__) {
2788 snprintf(buf, sizeof(buf), "%d", file->line_num);
2789 cstrval = buf;
2790 t1 = TOK_PPNUM;
2791 goto add_cstr1;
2792 } else if (tok == TOK___FILE__) {
2793 cstrval = file->filename;
2794 goto add_cstr;
2795 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2796 time_t ti;
2797 struct tm *tm;
2799 time(&ti);
2800 tm = localtime(&ti);
2801 if (tok == TOK___DATE__) {
2802 snprintf(buf, sizeof(buf), "%s %2d %d",
2803 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2804 } else {
2805 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2806 tm->tm_hour, tm->tm_min, tm->tm_sec);
2808 cstrval = buf;
2809 add_cstr:
2810 t1 = TOK_STR;
2811 add_cstr1:
2812 cstr_new(&cstr);
2813 cstr_cat(&cstr, cstrval);
2814 cstr_ccat(&cstr, '\0');
2815 cval.cstr = &cstr;
2816 tok_str_add2(tok_str, t1, &cval);
2817 cstr_free(&cstr);
2818 } else {
2819 mstr = s->d;
2820 mstr_allocated = 0;
2821 if (s->type.t == MACRO_FUNC) {
2822 /* NOTE: we do not use next_nomacro to avoid eating the
2823 next token. XXX: find better solution */
2824 redo:
2825 if (macro_ptr) {
2826 p = macro_ptr;
2827 while (is_space(t = *p) || TOK_LINEFEED == t)
2828 ++p;
2829 if (t == 0 && can_read_stream) {
2830 /* end of macro stream: we must look at the token
2831 after in the file */
2832 struct macro_level *ml = *can_read_stream;
2833 macro_ptr = NULL;
2834 if (ml)
2836 macro_ptr = ml->p;
2837 ml->p = NULL;
2838 *can_read_stream = ml -> prev;
2840 /* also, end of scope for nested defined symbol */
2841 (*nested_list)->v = -1;
2842 goto redo;
2844 } else {
2845 ch = file->buf_ptr[0];
2846 while (is_space(ch) || ch == '\n' || ch == '/')
2848 if (ch == '/')
2850 int c;
2851 uint8_t *p = file->buf_ptr;
2852 PEEKC(c, p);
2853 if (c == '*') {
2854 p = parse_comment(p);
2855 file->buf_ptr = p - 1;
2856 } else if (c == '/') {
2857 p = parse_line_comment(p);
2858 file->buf_ptr = p - 1;
2859 } else
2860 break;
2862 cinp();
2864 t = ch;
2866 if (t != '(') /* no macro subst */
2867 return -1;
2869 /* argument macro */
2870 next_nomacro();
2871 next_nomacro();
2872 args = NULL;
2873 sa = s->next;
2874 /* NOTE: empty args are allowed, except if no args */
2875 for(;;) {
2876 /* handle '()' case */
2877 if (!args && !sa && tok == ')')
2878 break;
2879 if (!sa)
2880 tcc_error("macro '%s' used with too many args",
2881 get_tok_str(s->v, 0));
2882 tok_str_new(&str);
2883 parlevel = spc = 0;
2884 /* NOTE: non zero sa->t indicates VA_ARGS */
2885 while ((parlevel > 0 ||
2886 (tok != ')' &&
2887 (tok != ',' || sa->type.t))) &&
2888 tok != -1) {
2889 if (tok == '(')
2890 parlevel++;
2891 else if (tok == ')')
2892 parlevel--;
2893 if (tok == TOK_LINEFEED)
2894 tok = ' ';
2895 if (!check_space(tok, &spc))
2896 tok_str_add2(&str, tok, &tokc);
2897 next_nomacro_spc();
2899 if (!str.len)
2900 tok_str_add(&str, TOK_PLCHLDR);
2901 str.len -= spc;
2902 tok_str_add(&str, 0);
2903 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2904 sa1->d = str.str;
2905 sa = sa->next;
2906 if (tok == ')') {
2907 /* special case for gcc var args: add an empty
2908 var arg argument if it is omitted */
2909 if (sa && sa->type.t && gnu_ext)
2910 continue;
2911 else
2912 break;
2914 if (tok != ',')
2915 expect(",");
2916 next_nomacro();
2918 if (sa) {
2919 tcc_error("macro '%s' used with too few args",
2920 get_tok_str(s->v, 0));
2923 /* now subst each arg */
2924 mstr = macro_arg_subst(nested_list, mstr, args);
2925 /* free memory */
2926 sa = args;
2927 while (sa) {
2928 sa1 = sa->prev;
2929 tok_str_free(sa->d);
2930 sym_free(sa);
2931 sa = sa1;
2933 mstr_allocated = 1;
2935 sym_push2(nested_list, s->v, 0, 0);
2936 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2937 /* pop nested defined symbol */
2938 sa1 = *nested_list;
2939 *nested_list = sa1->prev;
2940 sym_free(sa1);
2941 if (mstr_allocated)
2942 tok_str_free(mstr);
2944 return 0;
2947 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2948 return the resulting string (which must be freed). */
2949 static inline int *macro_twosharps(const int *macro_str)
2951 const int *ptr;
2952 int t;
2953 TokenString macro_str1;
2954 CString cstr;
2955 int n, start_of_nosubsts;
2957 /* we search the first '##' */
2958 for(ptr = macro_str;;) {
2959 CValue cval;
2960 TOK_GET(&t, &ptr, &cval);
2961 if (t == TOK_TWOSHARPS)
2962 break;
2963 /* nothing more to do if end of string */
2964 if (t == 0)
2965 return NULL;
2968 /* we saw '##', so we need more processing to handle it */
2969 start_of_nosubsts = -1;
2970 tok_str_new(&macro_str1);
2971 for(ptr = macro_str;;) {
2972 TOK_GET(&tok, &ptr, &tokc);
2973 if (tok == 0)
2974 break;
2975 if (tok == TOK_TWOSHARPS)
2976 continue;
2977 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2978 start_of_nosubsts = macro_str1.len;
2979 while (*ptr == TOK_TWOSHARPS) {
2980 /* given 'a##b', remove nosubsts preceding 'a' */
2981 if (start_of_nosubsts >= 0)
2982 macro_str1.len = start_of_nosubsts;
2983 /* given 'a##b', skip '##' */
2984 t = *++ptr;
2985 /* given 'a##b', remove nosubsts preceding 'b' */
2986 while (t == TOK_NOSUBST)
2987 t = *++ptr;
2988 if (t && t != TOK_TWOSHARPS) {
2989 CValue cval;
2990 TOK_GET(&t, &ptr, &cval);
2991 /* We concatenate the two tokens */
2992 cstr_new(&cstr);
2993 if (tok != TOK_PLCHLDR)
2994 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2995 n = cstr.size;
2996 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
2997 cstr_cat(&cstr, get_tok_str(t, &cval));
2998 cstr_ccat(&cstr, '\0');
3000 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3001 memcpy(file->buffer, cstr.data, cstr.size);
3002 for (;;) {
3003 next_nomacro1();
3004 if (0 == *file->buf_ptr)
3005 break;
3006 tok_str_add2(&macro_str1, tok, &tokc);
3007 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
3008 n, cstr.data, (char*)cstr.data + n);
3010 tcc_close();
3011 cstr_free(&cstr);
3014 if (tok != TOK_NOSUBST) {
3015 tok_str_add2(&macro_str1, tok, &tokc);
3016 tok = ' ';
3017 start_of_nosubsts = -1;
3019 tok_str_add2(&macro_str1, tok, &tokc);
3021 tok_str_add(&macro_str1, 0);
3022 return macro_str1.str;
3026 /* do macro substitution of macro_str and add result to
3027 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3028 inside to avoid recursing. */
3029 static void macro_subst(TokenString *tok_str, Sym **nested_list,
3030 const int *macro_str, struct macro_level ** can_read_stream)
3032 Sym *s;
3033 int *macro_str1;
3034 const int *ptr;
3035 int t, ret, spc;
3036 CValue cval;
3037 struct macro_level ml;
3038 int force_blank;
3040 /* first scan for '##' operator handling */
3041 ptr = macro_str;
3042 macro_str1 = macro_twosharps(ptr);
3044 if (macro_str1)
3045 ptr = macro_str1;
3046 spc = 0;
3047 force_blank = 0;
3049 while (1) {
3050 /* NOTE: ptr == NULL can only happen if tokens are read from
3051 file stream due to a macro function call */
3052 if (ptr == NULL)
3053 break;
3054 TOK_GET(&t, &ptr, &cval);
3055 if (t == 0)
3056 break;
3057 if (t == TOK_NOSUBST) {
3058 /* following token has already been subst'd. just copy it on */
3059 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3060 TOK_GET(&t, &ptr, &cval);
3061 goto no_subst;
3063 s = define_find(t);
3064 if (s != NULL) {
3065 /* if nested substitution, do nothing */
3066 if (sym_find2(*nested_list, t)) {
3067 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3068 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3069 goto no_subst;
3071 ml.p = macro_ptr;
3072 if (can_read_stream)
3073 ml.prev = *can_read_stream, *can_read_stream = &ml;
3074 macro_ptr = (int *)ptr;
3075 tok = t;
3076 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3077 ptr = (int *)macro_ptr;
3078 macro_ptr = ml.p;
3079 if (can_read_stream && *can_read_stream == &ml)
3080 *can_read_stream = ml.prev;
3081 if (ret != 0)
3082 goto no_subst;
3083 if (parse_flags & PARSE_FLAG_SPACES)
3084 force_blank = 1;
3085 } else {
3086 no_subst:
3087 if (force_blank) {
3088 tok_str_add(tok_str, ' ');
3089 spc = 1;
3090 force_blank = 0;
3092 if (!check_space(t, &spc))
3093 tok_str_add2(tok_str, t, &cval);
3096 if (macro_str1)
3097 tok_str_free(macro_str1);
3100 /* return next token with macro substitution */
3101 ST_FUNC void next(void)
3103 Sym *nested_list, *s;
3104 TokenString str;
3105 struct macro_level *ml;
3107 redo:
3108 if (parse_flags & PARSE_FLAG_SPACES)
3109 next_nomacro_spc();
3110 else
3111 next_nomacro();
3112 if (!macro_ptr) {
3113 /* if not reading from macro substituted string, then try
3114 to substitute macros */
3115 if (tok >= TOK_IDENT &&
3116 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3117 s = define_find(tok);
3118 if (s) {
3119 /* we have a macro: we try to substitute */
3120 tok_str_new(&str);
3121 nested_list = NULL;
3122 ml = NULL;
3123 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3124 /* substitution done, NOTE: maybe empty */
3125 tok_str_add(&str, 0);
3126 macro_ptr = str.str;
3127 macro_ptr_allocated = str.str;
3128 goto redo;
3132 } else {
3133 if (tok == 0) {
3134 /* end of macro or end of unget buffer */
3135 if (unget_buffer_enabled) {
3136 macro_ptr = unget_saved_macro_ptr;
3137 unget_buffer_enabled = 0;
3138 } else {
3139 /* end of macro string: free it */
3140 tok_str_free(macro_ptr_allocated);
3141 macro_ptr_allocated = NULL;
3142 macro_ptr = NULL;
3144 goto redo;
3145 } else if (tok == TOK_NOSUBST) {
3146 /* discard preprocessor's nosubst markers */
3147 goto redo;
3151 /* convert preprocessor tokens into C tokens */
3152 if (tok == TOK_PPNUM &&
3153 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3154 parse_number((char *)tokc.cstr->data);
3158 /* push back current token and set current token to 'last_tok'. Only
3159 identifier case handled for labels. */
3160 ST_INLN void unget_tok(int last_tok)
3162 int i, n;
3163 int *q;
3164 if (unget_buffer_enabled)
3166 /* assert(macro_ptr == unget_saved_buffer + 1);
3167 assert(*macro_ptr == 0); */
3169 else
3171 unget_saved_macro_ptr = macro_ptr;
3172 unget_buffer_enabled = 1;
3174 q = unget_saved_buffer;
3175 macro_ptr = q;
3176 *q++ = tok;
3177 n = tok_ext_size(tok) - 1;
3178 for(i=0;i<n;i++)
3179 *q++ = tokc.tab[i];
3180 *q = 0; /* end of token string */
3181 tok = last_tok;
3185 /* better than nothing, but needs extension to handle '-E' option
3186 correctly too */
3187 ST_FUNC void preprocess_init(TCCState *s1)
3189 s1->include_stack_ptr = s1->include_stack;
3190 /* XXX: move that before to avoid having to initialize
3191 file->ifdef_stack_ptr ? */
3192 s1->ifdef_stack_ptr = s1->ifdef_stack;
3193 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3195 vtop = vstack - 1;
3196 s1->pack_stack[0] = 0;
3197 s1->pack_stack_ptr = s1->pack_stack;
3200 ST_FUNC void preprocess_new(void)
3202 int i, c;
3203 const char *p, *r;
3205 /* init isid table */
3206 for(i=CH_EOF;i<256;i++)
3207 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3209 /* add all tokens */
3210 table_ident = NULL;
3211 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3213 tok_ident = TOK_IDENT;
3214 p = tcc_keywords;
3215 while (*p) {
3216 r = p;
3217 for(;;) {
3218 c = *r++;
3219 if (c == '\0')
3220 break;
3222 tok_alloc(p, r - p - 1);
3223 p = r;
3227 /* Preprocess the current file */
3228 ST_FUNC int tcc_preprocess(TCCState *s1)
3230 Sym *define_start;
3232 BufferedFile *file_ref, **iptr, **iptr_new;
3233 int line_ref, d;
3234 const char *s;
3236 preprocess_init(s1);
3237 define_start = define_stack;
3238 ch = file->buf_ptr[0];
3239 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3240 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3241 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3242 line_ref = 0;
3243 file_ref = NULL;
3244 iptr = s1->include_stack_ptr;
3245 tok = TOK_LINEFEED; /* print line */
3246 goto print_line;
3247 for (;;) {
3248 next();
3249 if (tok == TOK_EOF) {
3250 break;
3251 } else if (file != file_ref) {
3252 goto print_line;
3253 } else if (tok == TOK_LINEFEED) {
3254 if (token_seen)
3255 continue;
3256 ++line_ref;
3257 token_seen = 1;
3258 } else if (token_seen) {
3259 d = file->line_num - line_ref;
3260 if (file != file_ref || d < 0 || d >= 8) {
3261 print_line:
3262 iptr_new = s1->include_stack_ptr;
3263 s = iptr_new > iptr ? " 1"
3264 : iptr_new < iptr ? " 2"
3265 : iptr_new > s1->include_stack ? " 3"
3266 : "";
3267 iptr = iptr_new;
3268 fprintf(s1->ppfp, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
3269 } else {
3270 while (d)
3271 fputs("\n", s1->ppfp), --d;
3273 line_ref = (file_ref = file)->line_num;
3274 token_seen = tok == TOK_LINEFEED;
3275 if (token_seen)
3276 continue;
3278 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3280 free_defines(define_start);
3281 return 0;