1 macro_push and macro_pop work I made a mistake, no matter the definition does not...
[tinycc.git] / tccpp.c
blobac1717bd1a7ecd15321e03564fd3273f54866794
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];
65 static const char tcc_keywords[] =
66 #define DEF(id, str) str "\0"
67 #include "tcctok.h"
68 #undef DEF
71 /* WARNING: the content of this string encodes token numbers */
72 static const unsigned char tok_two_chars[] =
73 /* outdated -- gr
74 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
75 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
76 */{
77 '<','=', TOK_LE,
78 '>','=', TOK_GE,
79 '!','=', TOK_NE,
80 '&','&', TOK_LAND,
81 '|','|', TOK_LOR,
82 '+','+', TOK_INC,
83 '-','-', TOK_DEC,
84 '=','=', TOK_EQ,
85 '<','<', TOK_SHL,
86 '>','>', TOK_SAR,
87 '+','=', TOK_A_ADD,
88 '-','=', TOK_A_SUB,
89 '*','=', TOK_A_MUL,
90 '/','=', TOK_A_DIV,
91 '%','=', TOK_A_MOD,
92 '&','=', TOK_A_AND,
93 '^','=', TOK_A_XOR,
94 '|','=', TOK_A_OR,
95 '-','>', TOK_ARROW,
96 '.','.', 0xa8, // C++ token ?
97 '#','#', TOK_TWOSHARPS,
101 struct macro_level {
102 struct macro_level *prev;
103 const int *p;
106 static void next_nomacro_spc(void);
107 static void macro_subst(
108 TokenString *tok_str,
109 Sym **nested_list,
110 const int *macro_str,
111 struct macro_level **can_read_stream
114 ST_FUNC void skip(int c)
116 if (tok != c)
117 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
118 next();
121 ST_FUNC void expect(const char *msg)
123 tcc_error("%s expected", msg);
126 /* ------------------------------------------------------------------------- */
127 /* CString handling */
128 static void cstr_realloc(CString *cstr, int new_size)
130 int size;
131 void *data;
133 size = cstr->size_allocated;
134 if (size == 0)
135 size = 8; /* no need to allocate a too small first string */
136 while (size < new_size)
137 size = size * 2;
138 data = tcc_realloc(cstr->data_allocated, size);
139 cstr->data_allocated = data;
140 cstr->size_allocated = size;
141 cstr->data = data;
144 /* add a byte */
145 ST_FUNC void cstr_ccat(CString *cstr, int ch)
147 int size;
148 size = cstr->size + 1;
149 if (size > cstr->size_allocated)
150 cstr_realloc(cstr, size);
151 ((unsigned char *)cstr->data)[size - 1] = ch;
152 cstr->size = size;
155 ST_FUNC void cstr_cat(CString *cstr, const char *str)
157 int c;
158 for(;;) {
159 c = *str;
160 if (c == '\0')
161 break;
162 cstr_ccat(cstr, c);
163 str++;
167 /* add a wide char */
168 ST_FUNC void cstr_wccat(CString *cstr, int ch)
170 int size;
171 size = cstr->size + sizeof(nwchar_t);
172 if (size > cstr->size_allocated)
173 cstr_realloc(cstr, size);
174 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
175 cstr->size = size;
178 ST_FUNC void cstr_new(CString *cstr)
180 memset(cstr, 0, sizeof(CString));
183 /* free string and reset it to NULL */
184 ST_FUNC void cstr_free(CString *cstr)
186 tcc_free(cstr->data_allocated);
187 cstr_new(cstr);
190 /* reset string to empty */
191 ST_FUNC void cstr_reset(CString *cstr)
193 cstr->size = 0;
196 /* XXX: unicode ? */
197 static void add_char(CString *cstr, int c)
199 if (c == '\'' || c == '\"' || c == '\\') {
200 /* XXX: could be more precise if char or string */
201 cstr_ccat(cstr, '\\');
203 if (c >= 32 && c <= 126) {
204 cstr_ccat(cstr, c);
205 } else {
206 cstr_ccat(cstr, '\\');
207 if (c == '\n') {
208 cstr_ccat(cstr, 'n');
209 } else {
210 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
211 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
212 cstr_ccat(cstr, '0' + (c & 7));
217 /* ------------------------------------------------------------------------- */
218 /* allocate a new token */
219 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
221 TokenSym *ts, **ptable;
222 int i;
224 if (tok_ident >= SYM_FIRST_ANOM)
225 tcc_error("memory full (symbols)");
227 /* expand token table if needed */
228 i = tok_ident - TOK_IDENT;
229 if ((i % TOK_ALLOC_INCR) == 0) {
230 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
231 table_ident = ptable;
234 ts = tcc_malloc(sizeof(TokenSym) + len);
235 table_ident[i] = ts;
236 ts->tok = tok_ident++;
237 ts->sym_define.data = tcc_malloc(sizeof(Sym*));
238 ts->sym_define.off = 0;
239 ts->sym_define.data[0] = NULL;
240 ts->sym_define.size = 1;
241 ts->sym_label = NULL;
242 ts->sym_struct = NULL;
243 ts->sym_identifier = NULL;
244 ts->len = len;
245 ts->hash_next = NULL;
246 memcpy(ts->str, str, len);
247 ts->str[len] = '\0';
248 *pts = ts;
249 return ts;
252 #define TOK_HASH_INIT 1
253 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
255 /* find a token and add it if not found */
256 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
258 TokenSym *ts, **pts;
259 int i;
260 unsigned int h;
262 h = TOK_HASH_INIT;
263 for(i=0;i<len;i++)
264 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
265 h &= (TOK_HASH_SIZE - 1);
267 pts = &hash_ident[h];
268 for(;;) {
269 ts = *pts;
270 if (!ts)
271 break;
272 if (ts->len == len && !memcmp(ts->str, str, len))
273 return ts;
274 pts = &(ts->hash_next);
276 return tok_alloc_new(pts, str, len);
279 /* XXX: buffer overflow */
280 /* XXX: float tokens */
281 ST_FUNC char *get_tok_str(int v, CValue *cv)
283 static char buf[STRING_MAX_SIZE + 1];
284 static CString cstr_buf;
285 CString *cstr;
286 char *p;
287 int i, len;
289 /* NOTE: to go faster, we give a fixed buffer for small strings */
290 cstr_reset(&cstr_buf);
291 cstr_buf.data = buf;
292 cstr_buf.size_allocated = sizeof(buf);
293 p = buf;
295 /* just an explanation, should never happen:
296 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
297 tcc_error("internal error: get_tok_str"); */
299 switch(v) {
300 case TOK_CINT:
301 case TOK_CUINT:
302 /* XXX: not quite exact, but only useful for testing */
303 sprintf(p, "%u", cv->ui);
304 break;
305 case TOK_CLLONG:
306 case TOK_CULLONG:
307 /* XXX: not quite exact, but only useful for testing */
308 #ifdef _WIN32
309 sprintf(p, "%u", (unsigned)cv->ull);
310 #else
311 sprintf(p, "%llu", cv->ull);
312 #endif
313 break;
314 case TOK_LCHAR:
315 cstr_ccat(&cstr_buf, 'L');
316 case TOK_CCHAR:
317 cstr_ccat(&cstr_buf, '\'');
318 add_char(&cstr_buf, cv->i);
319 cstr_ccat(&cstr_buf, '\'');
320 cstr_ccat(&cstr_buf, '\0');
321 break;
322 case TOK_PPNUM:
323 cstr = cv->cstr;
324 len = cstr->size - 1;
325 for(i=0;i<len;i++)
326 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
327 cstr_ccat(&cstr_buf, '\0');
328 break;
329 case TOK_LSTR:
330 cstr_ccat(&cstr_buf, 'L');
331 case TOK_STR:
332 cstr = cv->cstr;
333 cstr_ccat(&cstr_buf, '\"');
334 if (v == TOK_STR) {
335 len = cstr->size - 1;
336 for(i=0;i<len;i++)
337 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
338 } else {
339 len = (cstr->size / sizeof(nwchar_t)) - 1;
340 for(i=0;i<len;i++)
341 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
343 cstr_ccat(&cstr_buf, '\"');
344 cstr_ccat(&cstr_buf, '\0');
345 break;
347 case TOK_CFLOAT:
348 case TOK_CDOUBLE:
349 case TOK_CLDOUBLE:
350 case TOK_LINENUM:
351 return NULL; /* should not happen */
353 /* above tokens have value, the ones below don't */
355 case TOK_LT:
356 v = '<';
357 goto addv;
358 case TOK_GT:
359 v = '>';
360 goto addv;
361 case TOK_DOTS:
362 return strcpy(p, "...");
363 case TOK_A_SHL:
364 return strcpy(p, "<<=");
365 case TOK_A_SAR:
366 return strcpy(p, ">>=");
367 default:
368 if (v < TOK_IDENT) {
369 /* search in two bytes table */
370 const unsigned char *q = tok_two_chars;
371 while (*q) {
372 if (q[2] == v) {
373 *p++ = q[0];
374 *p++ = q[1];
375 *p = '\0';
376 return buf;
378 q += 3;
380 addv:
381 *p++ = v;
382 *p = '\0';
383 } else if (v < tok_ident) {
384 return table_ident[v - TOK_IDENT]->str;
385 } else if (v >= SYM_FIRST_ANOM) {
386 /* special name for anonymous symbol */
387 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
388 } else {
389 /* should never happen */
390 return NULL;
392 break;
394 return cstr_buf.data;
397 /* fill input buffer and peek next char */
398 static int tcc_peekc_slow(BufferedFile *bf)
400 int len;
401 /* only tries to read if really end of buffer */
402 if (bf->buf_ptr >= bf->buf_end) {
403 if (bf->fd != -1) {
404 #if defined(PARSE_DEBUG)
405 len = 8;
406 #else
407 len = IO_BUF_SIZE;
408 #endif
409 len = read(bf->fd, bf->buffer, len);
410 if (len < 0)
411 len = 0;
412 } else {
413 len = 0;
415 total_bytes += len;
416 bf->buf_ptr = bf->buffer;
417 bf->buf_end = bf->buffer + len;
418 *bf->buf_end = CH_EOB;
420 if (bf->buf_ptr < bf->buf_end) {
421 return bf->buf_ptr[0];
422 } else {
423 bf->buf_ptr = bf->buf_end;
424 return CH_EOF;
428 /* return the current character, handling end of block if necessary
429 (but not stray) */
430 ST_FUNC int handle_eob(void)
432 return tcc_peekc_slow(file);
435 /* read next char from current input file and handle end of input buffer */
436 ST_INLN void inp(void)
438 ch = *(++(file->buf_ptr));
439 /* end of buffer/file handling */
440 if (ch == CH_EOB)
441 ch = handle_eob();
444 /* handle '\[\r]\n' */
445 static int handle_stray_noerror(void)
447 while (ch == '\\') {
448 inp();
449 if (ch == '\n') {
450 file->line_num++;
451 inp();
452 } else if (ch == '\r') {
453 inp();
454 if (ch != '\n')
455 goto fail;
456 file->line_num++;
457 inp();
458 } else {
459 fail:
460 return 1;
463 return 0;
466 static void handle_stray(void)
468 if (handle_stray_noerror())
469 tcc_error("stray '\\' in program");
472 /* skip the stray and handle the \\n case. Output an error if
473 incorrect char after the stray */
474 static int handle_stray1(uint8_t *p)
476 int c;
478 if (p >= file->buf_end) {
479 file->buf_ptr = p;
480 c = handle_eob();
481 p = file->buf_ptr;
482 if (c == '\\')
483 goto parse_stray;
484 } else {
485 parse_stray:
486 file->buf_ptr = p;
487 ch = *p;
488 handle_stray();
489 p = file->buf_ptr;
490 c = *p;
492 return c;
495 /* handle just the EOB case, but not stray */
496 #define PEEKC_EOB(c, p)\
498 p++;\
499 c = *p;\
500 if (c == '\\') {\
501 file->buf_ptr = p;\
502 c = handle_eob();\
503 p = file->buf_ptr;\
507 /* handle the complicated stray case */
508 #define PEEKC(c, p)\
510 p++;\
511 c = *p;\
512 if (c == '\\') {\
513 c = handle_stray1(p);\
514 p = file->buf_ptr;\
518 /* input with '\[\r]\n' handling. Note that this function cannot
519 handle other characters after '\', so you cannot call it inside
520 strings or comments */
521 ST_FUNC void minp(void)
523 inp();
524 if (ch == '\\')
525 handle_stray();
529 /* single line C++ comments */
530 static uint8_t *parse_line_comment(uint8_t *p)
532 int c;
534 p++;
535 for(;;) {
536 c = *p;
537 redo:
538 if (c == '\n' || c == CH_EOF) {
539 break;
540 } else if (c == '\\') {
541 file->buf_ptr = p;
542 c = handle_eob();
543 p = file->buf_ptr;
544 if (c == '\\') {
545 PEEKC_EOB(c, p);
546 if (c == '\n') {
547 file->line_num++;
548 PEEKC_EOB(c, p);
549 } else if (c == '\r') {
550 PEEKC_EOB(c, p);
551 if (c == '\n') {
552 file->line_num++;
553 PEEKC_EOB(c, p);
556 } else {
557 goto redo;
559 } else {
560 p++;
563 return p;
566 /* C comments */
567 ST_FUNC uint8_t *parse_comment(uint8_t *p)
569 int c;
571 p++;
572 for(;;) {
573 /* fast skip loop */
574 for(;;) {
575 c = *p;
576 if (c == '\n' || c == '*' || c == '\\')
577 break;
578 p++;
579 c = *p;
580 if (c == '\n' || c == '*' || c == '\\')
581 break;
582 p++;
584 /* now we can handle all the cases */
585 if (c == '\n') {
586 file->line_num++;
587 p++;
588 } else if (c == '*') {
589 p++;
590 for(;;) {
591 c = *p;
592 if (c == '*') {
593 p++;
594 } else if (c == '/') {
595 goto end_of_comment;
596 } else if (c == '\\') {
597 file->buf_ptr = p;
598 c = handle_eob();
599 p = file->buf_ptr;
600 if (c == '\\') {
601 /* skip '\[\r]\n', otherwise just skip the stray */
602 while (c == '\\') {
603 PEEKC_EOB(c, p);
604 if (c == '\n') {
605 file->line_num++;
606 PEEKC_EOB(c, p);
607 } else if (c == '\r') {
608 PEEKC_EOB(c, p);
609 if (c == '\n') {
610 file->line_num++;
611 PEEKC_EOB(c, p);
613 } else {
614 goto after_star;
618 } else {
619 break;
622 after_star: ;
623 } else {
624 /* stray, eob or eof */
625 file->buf_ptr = p;
626 c = handle_eob();
627 p = file->buf_ptr;
628 if (c == CH_EOF) {
629 tcc_error("unexpected end of file in comment");
630 } else if (c == '\\') {
631 p++;
635 end_of_comment:
636 p++;
637 return p;
640 #define cinp minp
642 static inline void skip_spaces(void)
644 while (is_space(ch))
645 cinp();
648 static inline int check_space(int t, int *spc)
650 if (is_space(t)) {
651 if (*spc)
652 return 1;
653 *spc = 1;
654 } else
655 *spc = 0;
656 return 0;
659 /* parse a string without interpreting escapes */
660 static uint8_t *parse_pp_string(uint8_t *p,
661 int sep, CString *str)
663 int c;
664 p++;
665 for(;;) {
666 c = *p;
667 if (c == sep) {
668 break;
669 } else if (c == '\\') {
670 file->buf_ptr = p;
671 c = handle_eob();
672 p = file->buf_ptr;
673 if (c == CH_EOF) {
674 unterminated_string:
675 /* XXX: indicate line number of start of string */
676 tcc_error("missing terminating %c character", sep);
677 } else if (c == '\\') {
678 /* escape : just skip \[\r]\n */
679 PEEKC_EOB(c, p);
680 if (c == '\n') {
681 file->line_num++;
682 p++;
683 } else if (c == '\r') {
684 PEEKC_EOB(c, p);
685 if (c != '\n')
686 expect("'\n' after '\r'");
687 file->line_num++;
688 p++;
689 } else if (c == CH_EOF) {
690 goto unterminated_string;
691 } else {
692 if (str) {
693 cstr_ccat(str, '\\');
694 cstr_ccat(str, c);
696 p++;
699 } else if (c == '\n') {
700 file->line_num++;
701 goto add_char;
702 } else if (c == '\r') {
703 PEEKC_EOB(c, p);
704 if (c != '\n') {
705 if (str)
706 cstr_ccat(str, '\r');
707 } else {
708 file->line_num++;
709 goto add_char;
711 } else {
712 add_char:
713 if (str)
714 cstr_ccat(str, c);
715 p++;
718 p++;
719 return p;
722 /* skip block of text until #else, #elif or #endif. skip also pairs of
723 #if/#endif */
724 static void preprocess_skip(void)
726 int a, start_of_line, c, in_warn_or_error;
727 uint8_t *p;
729 p = file->buf_ptr;
730 a = 0;
731 redo_start:
732 start_of_line = 1;
733 in_warn_or_error = 0;
734 for(;;) {
735 redo_no_start:
736 c = *p;
737 switch(c) {
738 case ' ':
739 case '\t':
740 case '\f':
741 case '\v':
742 case '\r':
743 p++;
744 goto redo_no_start;
745 case '\n':
746 file->line_num++;
747 p++;
748 goto redo_start;
749 case '\\':
750 file->buf_ptr = p;
751 c = handle_eob();
752 if (c == CH_EOF) {
753 expect("#endif");
754 } else if (c == '\\') {
755 ch = file->buf_ptr[0];
756 handle_stray_noerror();
758 p = file->buf_ptr;
759 goto redo_no_start;
760 /* skip strings */
761 case '\"':
762 case '\'':
763 if (in_warn_or_error)
764 goto _default;
765 p = parse_pp_string(p, c, NULL);
766 break;
767 /* skip comments */
768 case '/':
769 if (in_warn_or_error)
770 goto _default;
771 file->buf_ptr = p;
772 ch = *p;
773 minp();
774 p = file->buf_ptr;
775 if (ch == '*') {
776 p = parse_comment(p);
777 } else if (ch == '/') {
778 p = parse_line_comment(p);
780 break;
781 case '#':
782 p++;
783 if (start_of_line) {
784 file->buf_ptr = p;
785 next_nomacro();
786 p = file->buf_ptr;
787 if (a == 0 &&
788 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
789 goto the_end;
790 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
791 a++;
792 else if (tok == TOK_ENDIF)
793 a--;
794 else if( tok == TOK_ERROR || tok == TOK_WARNING)
795 in_warn_or_error = 1;
796 else if (tok == TOK_LINEFEED)
797 goto redo_start;
799 break;
800 _default:
801 default:
802 p++;
803 break;
805 start_of_line = 0;
807 the_end: ;
808 file->buf_ptr = p;
811 /* ParseState handling */
813 /* XXX: currently, no include file info is stored. Thus, we cannot display
814 accurate messages if the function or data definition spans multiple
815 files */
817 /* save current parse state in 's' */
818 ST_FUNC void save_parse_state(ParseState *s)
820 s->line_num = file->line_num;
821 s->macro_ptr = macro_ptr;
822 s->tok = tok;
823 s->tokc = tokc;
826 /* restore parse state from 's' */
827 ST_FUNC void restore_parse_state(ParseState *s)
829 file->line_num = s->line_num;
830 macro_ptr = s->macro_ptr;
831 tok = s->tok;
832 tokc = s->tokc;
835 /* return the number of additional 'ints' necessary to store the
836 token */
837 static inline int tok_ext_size(int t)
839 switch(t) {
840 /* 4 bytes */
841 case TOK_CINT:
842 case TOK_CUINT:
843 case TOK_CCHAR:
844 case TOK_LCHAR:
845 case TOK_CFLOAT:
846 case TOK_LINENUM:
847 return 1;
848 case TOK_STR:
849 case TOK_LSTR:
850 case TOK_PPNUM:
851 tcc_error("unsupported token");
852 return 1;
853 case TOK_CDOUBLE:
854 case TOK_CLLONG:
855 case TOK_CULLONG:
856 return 2;
857 case TOK_CLDOUBLE:
858 return LDOUBLE_SIZE / 4;
859 default:
860 return 0;
864 /* token string handling */
866 ST_INLN void tok_str_new(TokenString *s)
868 s->str = NULL;
869 s->len = 0;
870 s->allocated_len = 0;
871 s->last_line_num = -1;
874 ST_FUNC void tok_str_free(int *str)
876 tcc_free(str);
879 static int *tok_str_realloc(TokenString *s)
881 int *str, len;
883 if (s->allocated_len == 0) {
884 len = 8;
885 } else {
886 len = s->allocated_len * 2;
888 str = tcc_realloc(s->str, len * sizeof(int));
889 s->allocated_len = len;
890 s->str = str;
891 return str;
894 ST_FUNC void tok_str_add(TokenString *s, int t)
896 int len, *str;
898 len = s->len;
899 str = s->str;
900 if (len >= s->allocated_len)
901 str = tok_str_realloc(s);
902 str[len++] = t;
903 s->len = len;
906 static void tok_str_add2(TokenString *s, int t, CValue *cv)
908 int len, *str;
910 len = s->len;
911 str = s->str;
913 /* allocate space for worst case */
914 if (len + TOK_MAX_SIZE > s->allocated_len)
915 str = tok_str_realloc(s);
916 str[len++] = t;
917 switch(t) {
918 case TOK_CINT:
919 case TOK_CUINT:
920 case TOK_CCHAR:
921 case TOK_LCHAR:
922 case TOK_CFLOAT:
923 case TOK_LINENUM:
924 str[len++] = cv->tab[0];
925 break;
926 case TOK_PPNUM:
927 case TOK_STR:
928 case TOK_LSTR:
930 int nb_words;
931 CString *cstr;
933 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
934 while ((len + nb_words) > s->allocated_len)
935 str = tok_str_realloc(s);
936 cstr = (CString *)(str + len);
937 cstr->data = NULL;
938 cstr->size = cv->cstr->size;
939 cstr->data_allocated = NULL;
940 cstr->size_allocated = cstr->size;
941 memcpy((char *)cstr + sizeof(CString),
942 cv->cstr->data, cstr->size);
943 len += nb_words;
945 break;
946 case TOK_CDOUBLE:
947 case TOK_CLLONG:
948 case TOK_CULLONG:
949 #if LDOUBLE_SIZE == 8
950 case TOK_CLDOUBLE:
951 #endif
952 str[len++] = cv->tab[0];
953 str[len++] = cv->tab[1];
954 break;
955 #if LDOUBLE_SIZE == 12
956 case TOK_CLDOUBLE:
957 str[len++] = cv->tab[0];
958 str[len++] = cv->tab[1];
959 str[len++] = cv->tab[2];
960 #elif LDOUBLE_SIZE == 16
961 case TOK_CLDOUBLE:
962 str[len++] = cv->tab[0];
963 str[len++] = cv->tab[1];
964 str[len++] = cv->tab[2];
965 str[len++] = cv->tab[3];
966 #elif LDOUBLE_SIZE != 8
967 #error add long double size support
968 #endif
969 break;
970 default:
971 break;
973 s->len = len;
976 /* add the current parse token in token string 's' */
977 ST_FUNC void tok_str_add_tok(TokenString *s)
979 CValue cval;
981 /* save line number info */
982 if (file->line_num != s->last_line_num) {
983 s->last_line_num = file->line_num;
984 cval.i = s->last_line_num;
985 tok_str_add2(s, TOK_LINENUM, &cval);
987 tok_str_add2(s, tok, &tokc);
990 /* get a token from an integer array and increment pointer
991 accordingly. we code it as a macro to avoid pointer aliasing. */
992 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
994 const int *p = *pp;
995 int n, *tab;
997 tab = cv->tab;
998 switch(*t = *p++) {
999 case TOK_CINT:
1000 case TOK_CUINT:
1001 case TOK_CCHAR:
1002 case TOK_LCHAR:
1003 case TOK_CFLOAT:
1004 case TOK_LINENUM:
1005 tab[0] = *p++;
1006 break;
1007 case TOK_STR:
1008 case TOK_LSTR:
1009 case TOK_PPNUM:
1010 cv->cstr = (CString *)p;
1011 cv->cstr->data = (char *)p + sizeof(CString);
1012 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
1013 break;
1014 case TOK_CDOUBLE:
1015 case TOK_CLLONG:
1016 case TOK_CULLONG:
1017 n = 2;
1018 goto copy;
1019 case TOK_CLDOUBLE:
1020 #if LDOUBLE_SIZE == 16
1021 n = 4;
1022 #elif LDOUBLE_SIZE == 12
1023 n = 3;
1024 #elif LDOUBLE_SIZE == 8
1025 n = 2;
1026 #else
1027 # error add long double size support
1028 #endif
1029 copy:
1031 *tab++ = *p++;
1032 while (--n);
1033 break;
1034 default:
1035 break;
1037 *pp = p;
1040 static int macro_is_equal(const int *a, const int *b)
1042 char buf[STRING_MAX_SIZE + 1];
1043 CValue cv;
1044 int t;
1045 while (*a && *b) {
1046 TOK_GET(&t, &a, &cv);
1047 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1048 TOK_GET(&t, &b, &cv);
1049 if (strcmp(buf, get_tok_str(t, &cv)))
1050 return 0;
1052 return !(*a || *b);
1055 /* defines handling */
1056 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1058 Sym *s;
1059 CSym *def;
1060 s = define_find(v);
1061 if (s && !macro_is_equal(s->d, str))
1062 tcc_warning("%s redefined", get_tok_str(v, NULL));
1063 s = sym_push2(&define_stack, v, macro_type, 0);
1064 s->d = str;
1065 s->next = first_arg;
1066 def = &table_ident[v - TOK_IDENT]->sym_define;
1067 def->data[def->off] = s;
1070 /* undefined a define symbol. Its name is just set to zero */
1071 ST_FUNC void define_undef(Sym *s)
1073 int v;
1074 CSym *def;
1075 v = s->v - TOK_IDENT;
1076 if ((unsigned)v < (unsigned)(tok_ident - TOK_IDENT)){
1077 def = &table_ident[v]->sym_define;
1078 def->data[def->off] = NULL;
1082 ST_INLN Sym *define_find(int v)
1084 CSym *def;
1085 v -= TOK_IDENT;
1086 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1087 return NULL;
1088 def = &table_ident[v]->sym_define;
1089 return def->data[def->off];
1092 /* free define stack until top reaches 'b' */
1093 ST_FUNC void free_defines(Sym *b)
1095 Sym *top, *tmp;
1096 int v;
1097 CSym *def;
1099 top = define_stack;
1100 while (top != b) {
1101 tmp = top->prev;
1102 /* do not free args or predefined defines */
1103 if (top->d)
1104 tok_str_free(top->d);
1105 v = top->v - TOK_IDENT;
1106 if ((unsigned)v < (unsigned)(tok_ident - TOK_IDENT)){
1107 def = &table_ident[v]->sym_define;
1108 if(def->off)
1109 def->off = 0;
1110 if(def->data[0])
1111 def->data[0] = NULL;
1113 sym_free(top);
1114 top = tmp;
1116 define_stack = b;
1119 /* label lookup */
1120 ST_FUNC Sym *label_find(int v)
1122 v -= TOK_IDENT;
1123 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1124 return NULL;
1125 return table_ident[v]->sym_label;
1128 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1130 Sym *s, **ps;
1131 s = sym_push2(ptop, v, 0, 0);
1132 s->r = flags;
1133 ps = &table_ident[v - TOK_IDENT]->sym_label;
1134 if (ptop == &global_label_stack) {
1135 /* modify the top most local identifier, so that
1136 sym_identifier will point to 's' when popped */
1137 while (*ps != NULL)
1138 ps = &(*ps)->prev_tok;
1140 s->prev_tok = *ps;
1141 *ps = s;
1142 return s;
1145 /* pop labels until element last is reached. Look if any labels are
1146 undefined. Define symbols if '&&label' was used. */
1147 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1149 Sym *s, *s1;
1150 for(s = *ptop; s != slast; s = s1) {
1151 s1 = s->prev;
1152 if (s->r == LABEL_DECLARED) {
1153 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1154 } else if (s->r == LABEL_FORWARD) {
1155 tcc_error("label '%s' used but not defined",
1156 get_tok_str(s->v, NULL));
1157 } else {
1158 if (s->c) {
1159 /* define corresponding symbol. A size of
1160 1 is put. */
1161 put_extern_sym(s, cur_text_section, s->jnext, 1);
1164 /* remove label */
1165 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1166 sym_free(s);
1168 *ptop = slast;
1171 /* eval an expression for #if/#elif */
1172 static int expr_preprocess(void)
1174 int c, t;
1175 TokenString str;
1177 tok_str_new(&str);
1178 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1179 next(); /* do macro subst */
1180 if (tok == TOK_DEFINED) {
1181 next_nomacro();
1182 t = tok;
1183 if (t == '(')
1184 next_nomacro();
1185 c = define_find(tok) != 0;
1186 if (t == '(')
1187 next_nomacro();
1188 tok = TOK_CINT;
1189 tokc.i = c;
1190 } else if (tok >= TOK_IDENT) {
1191 /* if undefined macro */
1192 tok = TOK_CINT;
1193 tokc.i = 0;
1195 tok_str_add_tok(&str);
1197 tok_str_add(&str, -1); /* simulate end of file */
1198 tok_str_add(&str, 0);
1199 /* now evaluate C constant expression */
1200 macro_ptr = str.str;
1201 next();
1202 c = expr_const();
1203 macro_ptr = NULL;
1204 tok_str_free(str.str);
1205 return c != 0;
1208 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1209 static void tok_print(int *str)
1211 int t;
1212 CValue cval;
1214 printf("<");
1215 while (1) {
1216 TOK_GET(&t, &str, &cval);
1217 if (!t)
1218 break;
1219 printf("%s", get_tok_str(t, &cval));
1221 printf(">\n");
1223 #endif
1225 /* parse after #define */
1226 ST_FUNC void parse_define(void)
1228 Sym *s, *first, **ps;
1229 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1230 TokenString str;
1232 v = tok;
1233 if (v < TOK_IDENT)
1234 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1235 /* XXX: should check if same macro (ANSI) */
1236 first = NULL;
1237 t = MACRO_OBJ;
1238 /* '(' must be just after macro definition for MACRO_FUNC */
1239 next_nomacro_spc();
1240 if (tok == '(') {
1241 next_nomacro();
1242 ps = &first;
1243 while (tok != ')') {
1244 varg = tok;
1245 next_nomacro();
1246 is_vaargs = 0;
1247 if (varg == TOK_DOTS) {
1248 varg = TOK___VA_ARGS__;
1249 is_vaargs = 1;
1250 } else if (tok == TOK_DOTS && gnu_ext) {
1251 is_vaargs = 1;
1252 next_nomacro();
1254 if (varg < TOK_IDENT)
1255 tcc_error("badly punctuated parameter list");
1256 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1257 *ps = s;
1258 ps = &s->next;
1259 if (tok != ',')
1260 break;
1261 next_nomacro();
1263 if (tok == ')')
1264 next_nomacro_spc();
1265 t = MACRO_FUNC;
1267 tok_str_new(&str);
1268 spc = 2;
1269 /* EOF testing necessary for '-D' handling */
1270 ptok = 0;
1271 macro_list_start = 1;
1272 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1273 if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1274 tcc_error("'##' invalid at start of macro");
1275 ptok = tok;
1276 /* remove spaces around ## and after '#' */
1277 if (TOK_TWOSHARPS == tok) {
1278 if (1 == spc)
1279 --str.len;
1280 spc = 2;
1281 } else if ('#' == tok) {
1282 spc = 2;
1283 } else if (check_space(tok, &spc)) {
1284 goto skip;
1286 tok_str_add2(&str, tok, &tokc);
1287 skip:
1288 next_nomacro_spc();
1289 macro_list_start = 0;
1291 if (ptok == TOK_TWOSHARPS)
1292 tcc_error("'##' invalid at end of macro");
1293 if (spc == 1)
1294 --str.len; /* remove trailing space */
1295 tok_str_add(&str, 0);
1296 #ifdef PP_DEBUG
1297 printf("define %s %d: ", get_tok_str(v, NULL), t);
1298 tok_print(str.str);
1299 #endif
1300 define_push(v, t, str.str, first);
1303 static inline int hash_cached_include(const char *filename)
1305 const unsigned char *s;
1306 unsigned int h;
1308 h = TOK_HASH_INIT;
1309 s = (unsigned char *) filename;
1310 while (*s) {
1311 h = TOK_HASH_FUNC(h, *s);
1312 s++;
1314 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1315 return h;
1318 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1320 CachedInclude *e;
1321 int i, h;
1322 h = hash_cached_include(filename);
1323 i = s1->cached_includes_hash[h];
1324 for(;;) {
1325 if (i == 0)
1326 break;
1327 e = s1->cached_includes[i - 1];
1328 if (0 == PATHCMP(e->filename, filename))
1329 return e;
1330 i = e->hash_next;
1332 return NULL;
1335 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1337 CachedInclude *e;
1338 int h;
1340 if (search_cached_include(s1, filename))
1341 return;
1342 #ifdef INC_DEBUG
1343 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1344 #endif
1345 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1346 strcpy(e->filename, filename);
1347 e->ifndef_macro = ifndef_macro;
1348 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1349 /* add in hash table */
1350 h = hash_cached_include(filename);
1351 e->hash_next = s1->cached_includes_hash[h];
1352 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1355 /* is_bof is true if first non space token at beginning of file */
1356 ST_FUNC void preprocess(int is_bof)
1358 TCCState *s1 = tcc_state;
1359 int i, c, n, saved_parse_flags;
1360 uint8_t buf[1024], *p;
1361 Sym *s;
1363 saved_parse_flags = parse_flags;
1364 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_LINEFEED;
1365 next_nomacro();
1366 redo:
1367 switch(tok) {
1368 case TOK_DEFINE:
1369 next_nomacro();
1370 parse_define();
1371 break;
1372 case TOK_UNDEF:
1373 next_nomacro();
1374 s = define_find(tok);
1375 /* undefine symbol by putting an invalid name */
1376 if (s)
1377 define_undef(s);
1378 break;
1379 case TOK_INCLUDE:
1380 case TOK_INCLUDE_NEXT:
1381 ch = file->buf_ptr[0];
1382 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1383 skip_spaces();
1384 if (ch == '<') {
1385 c = '>';
1386 goto read_name;
1387 } else if (ch == '\"') {
1388 c = ch;
1389 read_name:
1390 inp();
1391 p = buf;
1392 while (ch != c && ch != '\n' && ch != CH_EOF) {
1393 if ((p - buf) < sizeof(buf) - 1)
1394 *p++ = ch;
1395 if (ch == '\\') {
1396 if (handle_stray_noerror() == 0)
1397 --p;
1398 } else
1399 inp();
1401 if (ch != c)
1402 goto include_syntax;
1403 *p = '\0';
1404 minp();
1405 #if 0
1406 /* eat all spaces and comments after include */
1407 /* XXX: slightly incorrect */
1408 while (ch1 != '\n' && ch1 != CH_EOF)
1409 inp();
1410 #endif
1411 } else {
1412 /* computed #include : either we have only strings or
1413 we have anything enclosed in '<>' */
1414 next();
1415 buf[0] = '\0';
1416 if (tok == TOK_STR) {
1417 while (tok != TOK_LINEFEED) {
1418 if (tok != TOK_STR) {
1419 include_syntax:
1420 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1422 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1423 next();
1425 c = '\"';
1426 } else {
1427 int len;
1428 while (tok != TOK_LINEFEED) {
1429 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1430 next();
1432 len = strlen(buf);
1433 /* check syntax and remove '<>' */
1434 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1435 goto include_syntax;
1436 memmove(buf, buf + 1, len - 2);
1437 buf[len - 2] = '\0';
1438 c = '>';
1441 if(!buf[0])
1442 tcc_error(" empty filename in #include");
1444 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1445 tcc_error("#include recursion too deep");
1446 /* store current file in stack, but increment stack later below */
1447 *s1->include_stack_ptr = file;
1449 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1450 for (i = -2; i < n; ++i) {
1451 char buf1[sizeof file->filename];
1452 CachedInclude *e;
1453 BufferedFile **f;
1454 const char *path;
1456 if (i == -2) {
1457 /* check absolute include path */
1458 if (!IS_ABSPATH(buf))
1459 continue;
1460 buf1[0] = 0;
1461 i = n; /* force end loop */
1463 } else if (i == -1) {
1464 /* search in current dir if "header.h" */
1465 if (c != '\"')
1466 continue;
1467 path = file->filename;
1468 pstrncpy(buf1, path, tcc_basename(path) - path);
1470 } else {
1471 /* search in all the include paths */
1472 if (i < s1->nb_include_paths)
1473 path = s1->include_paths[i];
1474 else
1475 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1476 pstrcpy(buf1, sizeof(buf1), path);
1477 pstrcat(buf1, sizeof(buf1), "/");
1480 pstrcat(buf1, sizeof(buf1), buf);
1482 if (tok == TOK_INCLUDE_NEXT)
1483 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1484 if (0 == PATHCMP((*f)->filename, buf1)) {
1485 #ifdef INC_DEBUG
1486 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1487 #endif
1488 goto include_trynext;
1491 e = search_cached_include(s1, buf1);
1492 if (e && define_find(e->ifndef_macro)) {
1493 /* no need to parse the include because the 'ifndef macro'
1494 is defined */
1495 #ifdef INC_DEBUG
1496 printf("%s: skipping cached %s\n", file->filename, buf1);
1497 #endif
1498 goto include_done;
1501 if (tcc_open(s1, buf1) < 0)
1502 include_trynext:
1503 continue;
1505 #ifdef INC_DEBUG
1506 printf("%s: including %s\n", file->prev->filename, file->filename);
1507 #endif
1508 /* update target deps */
1509 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps, tcc_strdup(buf1));
1510 /* push current file in stack */
1511 ++s1->include_stack_ptr;
1512 /* add include file debug info */
1513 if (s1->do_debug)
1514 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1515 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1516 ch = file->buf_ptr[0];
1517 goto the_end;
1519 tcc_error("include file '%s' not found", buf);
1520 include_done:
1521 break;
1522 case TOK_IFNDEF:
1523 c = 1;
1524 goto do_ifdef;
1525 case TOK_IF:
1526 c = expr_preprocess();
1527 goto do_if;
1528 case TOK_IFDEF:
1529 c = 0;
1530 do_ifdef:
1531 next_nomacro();
1532 if (tok < TOK_IDENT)
1533 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1534 if (is_bof) {
1535 if (c) {
1536 #ifdef INC_DEBUG
1537 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1538 #endif
1539 file->ifndef_macro = tok;
1542 c = !!define_find(tok) ^ c;
1543 do_if:
1544 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1545 tcc_error("memory full (ifdef)");
1546 *s1->ifdef_stack_ptr++ = c;
1547 goto test_skip;
1548 case TOK_ELSE:
1549 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1550 tcc_error("#else without matching #if");
1551 if (s1->ifdef_stack_ptr[-1] & 2)
1552 tcc_error("#else after #else");
1553 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1554 goto test_else;
1555 case TOK_ELIF:
1556 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1557 tcc_error("#elif without matching #if");
1558 c = s1->ifdef_stack_ptr[-1];
1559 if (c > 1)
1560 tcc_error("#elif after #else");
1561 /* last #if/#elif expression was true: we skip */
1562 if (c == 1)
1563 goto skip;
1564 c = expr_preprocess();
1565 s1->ifdef_stack_ptr[-1] = c;
1566 test_else:
1567 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1568 file->ifndef_macro = 0;
1569 test_skip:
1570 if (!(c & 1)) {
1571 skip:
1572 preprocess_skip();
1573 is_bof = 0;
1574 goto redo;
1576 break;
1577 case TOK_ENDIF:
1578 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1579 tcc_error("#endif without matching #if");
1580 s1->ifdef_stack_ptr--;
1581 /* '#ifndef macro' was at the start of file. Now we check if
1582 an '#endif' is exactly at the end of file */
1583 if (file->ifndef_macro &&
1584 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1585 file->ifndef_macro_saved = file->ifndef_macro;
1586 /* need to set to zero to avoid false matches if another
1587 #ifndef at middle of file */
1588 file->ifndef_macro = 0;
1589 tok_flags |= TOK_FLAG_ENDIF;
1591 next_nomacro();
1592 if (tok != TOK_LINEFEED)
1593 tcc_warning("Ignoring: %s", get_tok_str(tok, &tokc));
1594 break;
1595 case TOK_LINE:
1596 next();
1597 if (tok != TOK_CINT)
1598 tcc_error("#line");
1599 file->line_num = tokc.i - 1; /* the line number will be incremented after */
1600 next();
1601 if (tok != TOK_LINEFEED) {
1602 if (tok != TOK_STR)
1603 tcc_error("#line");
1604 pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.cstr->data);
1606 break;
1607 case TOK_ERROR:
1608 case TOK_WARNING:
1609 c = tok;
1610 ch = file->buf_ptr[0];
1611 skip_spaces();
1612 p = buf;
1613 while (ch != '\n' && ch != CH_EOF) {
1614 if ((p - buf) < sizeof(buf) - 1)
1615 *p++ = ch;
1616 if (ch == '\\') {
1617 if (handle_stray_noerror() == 0)
1618 --p;
1619 } else
1620 inp();
1622 *p = '\0';
1623 if (c == TOK_ERROR)
1624 tcc_error("#error %s", buf);
1625 else
1626 tcc_warning("#warning %s", buf);
1627 break;
1628 case TOK_PRAGMA:
1629 next();
1630 if (tok == TOK_pack && s1->output_type != TCC_OUTPUT_PREPROCESS) {
1632 This may be:
1633 #pragma pack(1) // set
1634 #pragma pack() // reset to default
1635 #pragma pack(push,1) // push & set
1636 #pragma pack(pop) // restore previous
1638 next();
1639 skip('(');
1640 if (tok == TOK_ASM_pop) {
1641 next();
1642 if (s1->pack_stack_ptr <= s1->pack_stack) {
1643 stk_error:
1644 tcc_error("out of pack stack");
1646 s1->pack_stack_ptr--;
1647 } else {
1648 int val = 0;
1649 if (tok != ')') {
1650 if (tok == TOK_ASM_push) {
1651 next();
1652 s1->pack_stack_ptr++;
1653 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE)
1654 goto stk_error;
1655 skip(',');
1657 if (tok != TOK_CINT) {
1658 pack_error:
1659 tcc_error("invalid pack pragma");
1661 val = tokc.i;
1662 if (val < 1 || val > 16)
1663 goto pack_error;
1664 if (val < 1 || val > 16)
1665 tcc_error("Value must be greater than 1 is less than or equal to 16");
1666 if ((val & (val - 1)) != 0)
1667 tcc_error("Value must be a power of 2 curtain");
1668 next();
1670 *s1->pack_stack_ptr = val;
1671 skip(')');
1673 }else if (tok == TOK_PUSH_MACRO || tok == TOK_POP_MACRO) {
1674 TokenSym *ts;
1675 CSym *def;
1676 uint8_t *p1;
1677 int len, t;
1678 t = tok;
1679 ch = file->buf_ptr[0];
1680 skip_spaces();
1681 if (ch != '(')
1682 goto macro_xxx_syntax;
1683 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1684 inp();
1685 skip_spaces();
1686 if (ch == '\"'){
1687 inp();
1688 p = buf;
1689 while (ch != '\"' && ch != '\n' && ch != CH_EOF) {
1690 if ((p - buf) < sizeof(buf) - 1)
1691 *p++ = ch;
1692 if (ch == CH_EOB) {
1693 --p;
1694 handle_stray();
1695 }else
1696 inp();
1698 if(ch != '\"')
1699 goto macro_xxx_syntax;
1700 *p = '\0';
1701 minp();
1702 next();
1703 }else{
1704 /* computed #pragma macro_xxx for #define xxx */
1705 next();
1706 buf[0] = '\0';
1707 while (tok != ')') {
1708 if (tok != TOK_STR) {
1709 macro_xxx_syntax:
1710 tcc_error("'macro_xxx' expects (\"NAME\")");
1712 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1713 next();
1716 skip (')');
1717 if(!buf[0])
1718 tcc_error(" empty string in #pragma");
1719 /* find TokenSym */
1720 p = buf;
1721 while (is_space(*p))
1722 p++;
1723 p1 = p;
1724 for(;;){
1725 if (!isidnum_table[p[0] - CH_EOF])
1726 break;
1727 ++p;
1729 len = p - p1;
1730 while (is_space(*p))
1731 p++;
1732 if(!p) //'\0'
1733 tcc_error("unrecognized string: %s", buf);
1734 ts = tok_alloc(p1, len);
1735 if(ts){
1736 def = &ts->sym_define;
1737 if(t == TOK_PUSH_MACRO){
1738 void *tmp = def->data[def->off];
1739 def->off++;
1740 if(def->off >= def->size){
1741 int size = def->size;
1742 size *= 2;
1743 if (size > MACRO_STACK_SIZE)
1744 tcc_error("stack full");
1745 def->data = tcc_realloc(def->data, size*sizeof(Sym*));
1746 def->size = size;
1748 def->data[def->off] = tmp;
1749 }else{
1750 if(def->off){
1751 --def->off;
1755 }else if(s1->output_type == TCC_OUTPUT_PREPROCESS){
1756 fputs("#pragma ", s1->ppfp);
1757 while (tok != TOK_LINEFEED){
1758 fputs(get_tok_str(tok, &tokc), s1->ppfp);
1759 next();
1761 fputs("\n", s1->ppfp);
1762 goto the_end;
1764 break;
1765 default:
1766 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1767 /* '!' is ignored to allow C scripts. numbers are ignored
1768 to emulate cpp behaviour */
1769 } else {
1770 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1771 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1772 else {
1773 /* this is a gas line comment in an 'S' file. */
1774 file->buf_ptr = parse_line_comment(file->buf_ptr);
1775 goto the_end;
1778 break;
1780 /* ignore other preprocess commands or #! for C scripts */
1781 while (tok != TOK_LINEFEED)
1782 next_nomacro();
1783 the_end:
1784 parse_flags = saved_parse_flags;
1787 /* evaluate escape codes in a string. */
1788 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1790 int c, n;
1791 const uint8_t *p;
1793 p = buf;
1794 for(;;) {
1795 c = *p;
1796 if (c == '\0')
1797 break;
1798 if (c == '\\') {
1799 p++;
1800 /* escape */
1801 c = *p;
1802 switch(c) {
1803 case '0': case '1': case '2': case '3':
1804 case '4': case '5': case '6': case '7':
1805 /* at most three octal digits */
1806 n = c - '0';
1807 p++;
1808 c = *p;
1809 if (isoct(c)) {
1810 n = n * 8 + c - '0';
1811 p++;
1812 c = *p;
1813 if (isoct(c)) {
1814 n = n * 8 + c - '0';
1815 p++;
1818 c = n;
1819 goto add_char_nonext;
1820 case 'x':
1821 case 'u':
1822 case 'U':
1823 p++;
1824 n = 0;
1825 for(;;) {
1826 c = *p;
1827 if (c >= 'a' && c <= 'f')
1828 c = c - 'a' + 10;
1829 else if (c >= 'A' && c <= 'F')
1830 c = c - 'A' + 10;
1831 else if (isnum(c))
1832 c = c - '0';
1833 else
1834 break;
1835 n = n * 16 + c;
1836 p++;
1838 c = n;
1839 goto add_char_nonext;
1840 case 'a':
1841 c = '\a';
1842 break;
1843 case 'b':
1844 c = '\b';
1845 break;
1846 case 'f':
1847 c = '\f';
1848 break;
1849 case 'n':
1850 c = '\n';
1851 break;
1852 case 'r':
1853 c = '\r';
1854 break;
1855 case 't':
1856 c = '\t';
1857 break;
1858 case 'v':
1859 c = '\v';
1860 break;
1861 case 'e':
1862 if (!gnu_ext)
1863 goto invalid_escape;
1864 c = 27;
1865 break;
1866 case '\'':
1867 case '\"':
1868 case '\\':
1869 case '?':
1870 break;
1871 default:
1872 invalid_escape:
1873 if (c >= '!' && c <= '~')
1874 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1875 else
1876 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1877 break;
1880 p++;
1881 add_char_nonext:
1882 if (!is_long)
1883 cstr_ccat(outstr, c);
1884 else
1885 cstr_wccat(outstr, c);
1887 /* add a trailing '\0' */
1888 if (!is_long)
1889 cstr_ccat(outstr, '\0');
1890 else
1891 cstr_wccat(outstr, '\0');
1894 /* we use 64 bit numbers */
1895 #define BN_SIZE 2
1897 /* bn = (bn << shift) | or_val */
1898 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1900 int i;
1901 unsigned int v;
1902 for(i=0;i<BN_SIZE;i++) {
1903 v = bn[i];
1904 bn[i] = (v << shift) | or_val;
1905 or_val = v >> (32 - shift);
1909 static void bn_zero(unsigned int *bn)
1911 int i;
1912 for(i=0;i<BN_SIZE;i++) {
1913 bn[i] = 0;
1917 /* parse number in null terminated string 'p' and return it in the
1918 current token */
1919 static void parse_number(const char *p)
1921 int b, t, shift, frac_bits, s, exp_val, ch;
1922 char *q;
1923 unsigned int bn[BN_SIZE];
1924 double d;
1926 /* number */
1927 q = token_buf;
1928 ch = *p++;
1929 t = ch;
1930 ch = *p++;
1931 *q++ = t;
1932 b = 10;
1933 if (t == '.') {
1934 goto float_frac_parse;
1935 } else if (t == '0') {
1936 if (ch == 'x' || ch == 'X') {
1937 q--;
1938 ch = *p++;
1939 b = 16;
1940 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1941 q--;
1942 ch = *p++;
1943 b = 2;
1946 /* parse all digits. cannot check octal numbers at this stage
1947 because of floating point constants */
1948 while (1) {
1949 if (ch >= 'a' && ch <= 'f')
1950 t = ch - 'a' + 10;
1951 else if (ch >= 'A' && ch <= 'F')
1952 t = ch - 'A' + 10;
1953 else if (isnum(ch))
1954 t = ch - '0';
1955 else
1956 break;
1957 if (t >= b)
1958 break;
1959 if (q >= token_buf + STRING_MAX_SIZE) {
1960 num_too_long:
1961 tcc_error("number too long");
1963 *q++ = ch;
1964 ch = *p++;
1966 if (ch == '.' ||
1967 ((ch == 'e' || ch == 'E') && b == 10) ||
1968 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1969 if (b != 10) {
1970 /* NOTE: strtox should support that for hexa numbers, but
1971 non ISOC99 libcs do not support it, so we prefer to do
1972 it by hand */
1973 /* hexadecimal or binary floats */
1974 /* XXX: handle overflows */
1975 *q = '\0';
1976 if (b == 16)
1977 shift = 4;
1978 else
1979 shift = 2;
1980 bn_zero(bn);
1981 q = token_buf;
1982 while (1) {
1983 t = *q++;
1984 if (t == '\0') {
1985 break;
1986 } else if (t >= 'a') {
1987 t = t - 'a' + 10;
1988 } else if (t >= 'A') {
1989 t = t - 'A' + 10;
1990 } else {
1991 t = t - '0';
1993 bn_lshift(bn, shift, t);
1995 frac_bits = 0;
1996 if (ch == '.') {
1997 ch = *p++;
1998 while (1) {
1999 t = ch;
2000 if (t >= 'a' && t <= 'f') {
2001 t = t - 'a' + 10;
2002 } else if (t >= 'A' && t <= 'F') {
2003 t = t - 'A' + 10;
2004 } else if (t >= '0' && t <= '9') {
2005 t = t - '0';
2006 } else {
2007 break;
2009 if (t >= b)
2010 tcc_error("invalid digit");
2011 bn_lshift(bn, shift, t);
2012 frac_bits += shift;
2013 ch = *p++;
2016 if (ch != 'p' && ch != 'P')
2017 expect("exponent");
2018 ch = *p++;
2019 s = 1;
2020 exp_val = 0;
2021 if (ch == '+') {
2022 ch = *p++;
2023 } else if (ch == '-') {
2024 s = -1;
2025 ch = *p++;
2027 if (ch < '0' || ch > '9')
2028 expect("exponent digits");
2029 while (ch >= '0' && ch <= '9') {
2030 exp_val = exp_val * 10 + ch - '0';
2031 ch = *p++;
2033 exp_val = exp_val * s;
2035 /* now we can generate the number */
2036 /* XXX: should patch directly float number */
2037 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2038 d = ldexp(d, exp_val - frac_bits);
2039 t = toup(ch);
2040 if (t == 'F') {
2041 ch = *p++;
2042 tok = TOK_CFLOAT;
2043 /* float : should handle overflow */
2044 tokc.f = (float)d;
2045 } else if (t == 'L') {
2046 ch = *p++;
2047 #ifdef TCC_TARGET_PE
2048 tok = TOK_CDOUBLE;
2049 tokc.d = d;
2050 #else
2051 tok = TOK_CLDOUBLE;
2052 /* XXX: not large enough */
2053 tokc.ld = (long double)d;
2054 #endif
2055 } else {
2056 tok = TOK_CDOUBLE;
2057 tokc.d = d;
2059 } else {
2060 /* decimal floats */
2061 if (ch == '.') {
2062 if (q >= token_buf + STRING_MAX_SIZE)
2063 goto num_too_long;
2064 *q++ = ch;
2065 ch = *p++;
2066 float_frac_parse:
2067 while (ch >= '0' && ch <= '9') {
2068 if (q >= token_buf + STRING_MAX_SIZE)
2069 goto num_too_long;
2070 *q++ = ch;
2071 ch = *p++;
2074 if (ch == 'e' || ch == 'E') {
2075 if (q >= token_buf + STRING_MAX_SIZE)
2076 goto num_too_long;
2077 *q++ = ch;
2078 ch = *p++;
2079 if (ch == '-' || ch == '+') {
2080 if (q >= token_buf + STRING_MAX_SIZE)
2081 goto num_too_long;
2082 *q++ = ch;
2083 ch = *p++;
2085 if (ch < '0' || ch > '9')
2086 expect("exponent digits");
2087 while (ch >= '0' && ch <= '9') {
2088 if (q >= token_buf + STRING_MAX_SIZE)
2089 goto num_too_long;
2090 *q++ = ch;
2091 ch = *p++;
2094 *q = '\0';
2095 t = toup(ch);
2096 errno = 0;
2097 if (t == 'F') {
2098 ch = *p++;
2099 tok = TOK_CFLOAT;
2100 tokc.f = strtof(token_buf, NULL);
2101 } else if (t == 'L') {
2102 ch = *p++;
2103 #ifdef TCC_TARGET_PE
2104 tok = TOK_CDOUBLE;
2105 tokc.d = strtod(token_buf, NULL);
2106 #else
2107 tok = TOK_CLDOUBLE;
2108 tokc.ld = strtold(token_buf, NULL);
2109 #endif
2110 } else {
2111 tok = TOK_CDOUBLE;
2112 tokc.d = strtod(token_buf, NULL);
2115 } else {
2116 unsigned long long n, n1;
2117 int lcount, ucount;
2119 /* integer number */
2120 *q = '\0';
2121 q = token_buf;
2122 if (b == 10 && *q == '0') {
2123 b = 8;
2124 q++;
2126 n = 0;
2127 while(1) {
2128 t = *q++;
2129 /* no need for checks except for base 10 / 8 errors */
2130 if (t == '\0') {
2131 break;
2132 } else if (t >= 'a') {
2133 t = t - 'a' + 10;
2134 } else if (t >= 'A') {
2135 t = t - 'A' + 10;
2136 } else {
2137 t = t - '0';
2138 if (t >= b)
2139 tcc_error("invalid digit");
2141 n1 = n;
2142 n = n * b + t;
2143 /* detect overflow */
2144 /* XXX: this test is not reliable */
2145 if (n < n1)
2146 tcc_error("integer constant overflow");
2149 /* XXX: not exactly ANSI compliant */
2150 if ((n & 0xffffffff00000000LL) != 0) {
2151 if ((n >> 63) != 0)
2152 tok = TOK_CULLONG;
2153 else
2154 tok = TOK_CLLONG;
2155 } else if (n > 0x7fffffff) {
2156 tok = TOK_CUINT;
2157 } else {
2158 tok = TOK_CINT;
2160 lcount = 0;
2161 ucount = 0;
2162 for(;;) {
2163 t = toup(ch);
2164 if (t == 'L') {
2165 if (lcount >= 2)
2166 tcc_error("three 'l's in integer constant");
2167 lcount++;
2168 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2169 if (lcount == 2) {
2170 #endif
2171 if (tok == TOK_CINT)
2172 tok = TOK_CLLONG;
2173 else if (tok == TOK_CUINT)
2174 tok = TOK_CULLONG;
2175 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2177 #endif
2178 ch = *p++;
2179 } else if (t == 'U') {
2180 if (ucount >= 1)
2181 tcc_error("two 'u's in integer constant");
2182 ucount++;
2183 if (tok == TOK_CINT)
2184 tok = TOK_CUINT;
2185 else if (tok == TOK_CLLONG)
2186 tok = TOK_CULLONG;
2187 ch = *p++;
2188 } else {
2189 break;
2192 if (tok == TOK_CINT || tok == TOK_CUINT)
2193 tokc.ui = n;
2194 else
2195 tokc.ull = n;
2197 if (ch)
2198 tcc_error("invalid number\n");
2202 #define PARSE2(c1, tok1, c2, tok2) \
2203 case c1: \
2204 PEEKC(c, p); \
2205 if (c == c2) { \
2206 p++; \
2207 tok = tok2; \
2208 } else { \
2209 tok = tok1; \
2211 break;
2213 /* return next token without macro substitution */
2214 static inline void next_nomacro1(void)
2216 int t, c, is_long;
2217 TokenSym *ts;
2218 uint8_t *p, *p1;
2219 unsigned int h;
2221 p = file->buf_ptr;
2222 redo_no_start:
2223 c = *p;
2224 switch(c) {
2225 case ' ':
2226 case '\t':
2227 tok = c;
2228 p++;
2229 goto keep_tok_flags;
2230 case '\f':
2231 case '\v':
2232 case '\r':
2233 p++;
2234 goto redo_no_start;
2235 case '\\':
2236 /* first look if it is in fact an end of buffer */
2237 if (p >= file->buf_end) {
2238 file->buf_ptr = p;
2239 handle_eob();
2240 p = file->buf_ptr;
2241 if (p >= file->buf_end)
2242 goto parse_eof;
2243 else
2244 goto redo_no_start;
2245 } else {
2246 file->buf_ptr = p;
2247 ch = *p;
2248 handle_stray();
2249 p = file->buf_ptr;
2250 goto redo_no_start;
2252 parse_eof:
2254 TCCState *s1 = tcc_state;
2255 if ((parse_flags & PARSE_FLAG_LINEFEED)
2256 && !(tok_flags & TOK_FLAG_EOF)) {
2257 tok_flags |= TOK_FLAG_EOF;
2258 tok = TOK_LINEFEED;
2259 goto keep_tok_flags;
2260 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2261 tok = TOK_EOF;
2262 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2263 tcc_error("missing #endif");
2264 } else if (s1->include_stack_ptr == s1->include_stack) {
2265 /* no include left : end of file. */
2266 tok = TOK_EOF;
2267 } else {
2268 tok_flags &= ~TOK_FLAG_EOF;
2269 /* pop include file */
2271 /* test if previous '#endif' was after a #ifdef at
2272 start of file */
2273 if (tok_flags & TOK_FLAG_ENDIF) {
2274 #ifdef INC_DEBUG
2275 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2276 #endif
2277 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2278 tok_flags &= ~TOK_FLAG_ENDIF;
2281 /* add end of include file debug info */
2282 if (tcc_state->do_debug) {
2283 put_stabd(N_EINCL, 0, 0);
2285 /* pop include stack */
2286 tcc_close();
2287 s1->include_stack_ptr--;
2288 p = file->buf_ptr;
2289 goto redo_no_start;
2292 break;
2294 case '\n':
2295 file->line_num++;
2296 tok_flags |= TOK_FLAG_BOL;
2297 p++;
2298 maybe_newline:
2299 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2300 goto redo_no_start;
2301 tok = TOK_LINEFEED;
2302 goto keep_tok_flags;
2304 case '#':
2305 /* XXX: simplify */
2306 PEEKC(c, p);
2307 if ((tok_flags & TOK_FLAG_BOL) &&
2308 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2309 file->buf_ptr = p;
2310 preprocess(tok_flags & TOK_FLAG_BOF);
2311 p = file->buf_ptr;
2312 goto maybe_newline;
2313 } else {
2314 if (c == '#') {
2315 p++;
2316 tok = TOK_TWOSHARPS;
2317 } else {
2318 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2319 p = parse_line_comment(p - 1);
2320 goto redo_no_start;
2321 } else {
2322 tok = '#';
2326 break;
2328 case 'a': case 'b': case 'c': case 'd':
2329 case 'e': case 'f': case 'g': case 'h':
2330 case 'i': case 'j': case 'k': case 'l':
2331 case 'm': case 'n': case 'o': case 'p':
2332 case 'q': case 'r': case 's': case 't':
2333 case 'u': case 'v': case 'w': case 'x':
2334 case 'y': case 'z':
2335 case 'A': case 'B': case 'C': case 'D':
2336 case 'E': case 'F': case 'G': case 'H':
2337 case 'I': case 'J': case 'K':
2338 case 'M': case 'N': case 'O': case 'P':
2339 case 'Q': case 'R': case 'S': case 'T':
2340 case 'U': case 'V': case 'W': case 'X':
2341 case 'Y': case 'Z':
2342 case '_':
2343 parse_ident_fast:
2344 p1 = p;
2345 h = TOK_HASH_INIT;
2346 h = TOK_HASH_FUNC(h, c);
2347 p++;
2348 for(;;) {
2349 c = *p;
2350 if (!isidnum_table[c-CH_EOF])
2351 break;
2352 h = TOK_HASH_FUNC(h, c);
2353 p++;
2355 if (c != '\\') {
2356 TokenSym **pts;
2357 int len;
2359 /* fast case : no stray found, so we have the full token
2360 and we have already hashed it */
2361 len = p - p1;
2362 h &= (TOK_HASH_SIZE - 1);
2363 pts = &hash_ident[h];
2364 for(;;) {
2365 ts = *pts;
2366 if (!ts)
2367 break;
2368 if (ts->len == len && !memcmp(ts->str, p1, len))
2369 goto token_found;
2370 pts = &(ts->hash_next);
2372 ts = tok_alloc_new(pts, (char *) p1, len);
2373 token_found: ;
2374 } else {
2375 /* slower case */
2376 cstr_reset(&tokcstr);
2378 while (p1 < p) {
2379 cstr_ccat(&tokcstr, *p1);
2380 p1++;
2382 p--;
2383 PEEKC(c, p);
2384 parse_ident_slow:
2385 while (isidnum_table[c-CH_EOF]) {
2386 cstr_ccat(&tokcstr, c);
2387 PEEKC(c, p);
2389 ts = tok_alloc(tokcstr.data, tokcstr.size);
2391 tok = ts->tok;
2392 break;
2393 case 'L':
2394 t = p[1];
2395 if (t != '\\' && t != '\'' && t != '\"') {
2396 /* fast case */
2397 goto parse_ident_fast;
2398 } else {
2399 PEEKC(c, p);
2400 if (c == '\'' || c == '\"') {
2401 is_long = 1;
2402 goto str_const;
2403 } else {
2404 cstr_reset(&tokcstr);
2405 cstr_ccat(&tokcstr, 'L');
2406 goto parse_ident_slow;
2409 break;
2410 case '0': case '1': case '2': case '3':
2411 case '4': case '5': case '6': case '7':
2412 case '8': case '9':
2414 cstr_reset(&tokcstr);
2415 /* after the first digit, accept digits, alpha, '.' or sign if
2416 prefixed by 'eEpP' */
2417 parse_num:
2418 for(;;) {
2419 t = c;
2420 cstr_ccat(&tokcstr, c);
2421 PEEKC(c, p);
2422 if (!(isnum(c) || isid(c) || c == '.' ||
2423 ((c == '+' || c == '-') &&
2424 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2425 break;
2427 /* We add a trailing '\0' to ease parsing */
2428 cstr_ccat(&tokcstr, '\0');
2429 tokc.cstr = &tokcstr;
2430 tok = TOK_PPNUM;
2431 break;
2432 case '.':
2433 /* special dot handling because it can also start a number */
2434 PEEKC(c, p);
2435 if (isnum(c)) {
2436 cstr_reset(&tokcstr);
2437 cstr_ccat(&tokcstr, '.');
2438 goto parse_num;
2439 } else if (c == '.') {
2440 PEEKC(c, p);
2441 if (c != '.')
2442 expect("'.'");
2443 PEEKC(c, p);
2444 tok = TOK_DOTS;
2445 } else {
2446 tok = '.';
2448 break;
2449 case '\'':
2450 case '\"':
2451 is_long = 0;
2452 str_const:
2454 CString str;
2455 int sep;
2457 sep = c;
2459 /* parse the string */
2460 cstr_new(&str);
2461 p = parse_pp_string(p, sep, &str);
2462 cstr_ccat(&str, '\0');
2464 /* eval the escape (should be done as TOK_PPNUM) */
2465 cstr_reset(&tokcstr);
2466 parse_escape_string(&tokcstr, str.data, is_long);
2467 cstr_free(&str);
2469 if (sep == '\'') {
2470 int char_size;
2471 /* XXX: make it portable */
2472 if (!is_long)
2473 char_size = 1;
2474 else
2475 char_size = sizeof(nwchar_t);
2476 if (tokcstr.size <= char_size)
2477 tcc_error("empty character constant");
2478 if (tokcstr.size > 2 * char_size)
2479 tcc_warning("multi-character character constant");
2480 if (!is_long) {
2481 tokc.i = *(int8_t *)tokcstr.data;
2482 tok = TOK_CCHAR;
2483 } else {
2484 tokc.i = *(nwchar_t *)tokcstr.data;
2485 tok = TOK_LCHAR;
2487 } else {
2488 tokc.cstr = &tokcstr;
2489 if (!is_long)
2490 tok = TOK_STR;
2491 else
2492 tok = TOK_LSTR;
2495 break;
2497 case '<':
2498 PEEKC(c, p);
2499 if (c == '=') {
2500 p++;
2501 tok = TOK_LE;
2502 } else if (c == '<') {
2503 PEEKC(c, p);
2504 if (c == '=') {
2505 p++;
2506 tok = TOK_A_SHL;
2507 } else {
2508 tok = TOK_SHL;
2510 } else {
2511 tok = TOK_LT;
2513 break;
2515 case '>':
2516 PEEKC(c, p);
2517 if (c == '=') {
2518 p++;
2519 tok = TOK_GE;
2520 } else if (c == '>') {
2521 PEEKC(c, p);
2522 if (c == '=') {
2523 p++;
2524 tok = TOK_A_SAR;
2525 } else {
2526 tok = TOK_SAR;
2528 } else {
2529 tok = TOK_GT;
2531 break;
2533 case '&':
2534 PEEKC(c, p);
2535 if (c == '&') {
2536 p++;
2537 tok = TOK_LAND;
2538 } else if (c == '=') {
2539 p++;
2540 tok = TOK_A_AND;
2541 } else {
2542 tok = '&';
2544 break;
2546 case '|':
2547 PEEKC(c, p);
2548 if (c == '|') {
2549 p++;
2550 tok = TOK_LOR;
2551 } else if (c == '=') {
2552 p++;
2553 tok = TOK_A_OR;
2554 } else {
2555 tok = '|';
2557 break;
2559 case '+':
2560 PEEKC(c, p);
2561 if (c == '+') {
2562 p++;
2563 tok = TOK_INC;
2564 } else if (c == '=') {
2565 p++;
2566 tok = TOK_A_ADD;
2567 } else {
2568 tok = '+';
2570 break;
2572 case '-':
2573 PEEKC(c, p);
2574 if (c == '-') {
2575 p++;
2576 tok = TOK_DEC;
2577 } else if (c == '=') {
2578 p++;
2579 tok = TOK_A_SUB;
2580 } else if (c == '>') {
2581 p++;
2582 tok = TOK_ARROW;
2583 } else {
2584 tok = '-';
2586 break;
2588 PARSE2('!', '!', '=', TOK_NE)
2589 PARSE2('=', '=', '=', TOK_EQ)
2590 PARSE2('*', '*', '=', TOK_A_MUL)
2591 PARSE2('%', '%', '=', TOK_A_MOD)
2592 PARSE2('^', '^', '=', TOK_A_XOR)
2594 /* comments or operator */
2595 case '/':
2596 PEEKC(c, p);
2597 if (c == '*') {
2598 p = parse_comment(p);
2599 /* comments replaced by a blank */
2600 tok = ' ';
2601 goto keep_tok_flags;
2602 } else if (c == '/') {
2603 p = parse_line_comment(p);
2604 tok = ' ';
2605 goto keep_tok_flags;
2606 } else if (c == '=') {
2607 p++;
2608 tok = TOK_A_DIV;
2609 } else {
2610 tok = '/';
2612 break;
2614 /* simple tokens */
2615 case '(':
2616 case ')':
2617 case '[':
2618 case ']':
2619 case '{':
2620 case '}':
2621 case ',':
2622 case ';':
2623 case ':':
2624 case '?':
2625 case '~':
2626 case '$': /* only used in assembler */
2627 case '@': /* dito */
2628 tok = c;
2629 p++;
2630 break;
2631 default:
2632 tcc_error("unrecognized character \\x%02x", c);
2633 break;
2635 tok_flags = 0;
2636 keep_tok_flags:
2637 file->buf_ptr = p;
2638 #if defined(PARSE_DEBUG)
2639 printf("token = %s\n", get_tok_str(tok, &tokc));
2640 #endif
2643 /* return next token without macro substitution. Can read input from
2644 macro_ptr buffer */
2645 static void next_nomacro_spc(void)
2647 if (macro_ptr) {
2648 redo:
2649 tok = *macro_ptr;
2650 if (tok) {
2651 TOK_GET(&tok, &macro_ptr, &tokc);
2652 if (tok == TOK_LINENUM) {
2653 file->line_num = tokc.i;
2654 goto redo;
2657 } else {
2658 next_nomacro1();
2662 ST_FUNC void next_nomacro(void)
2664 do {
2665 next_nomacro_spc();
2666 } while (is_space(tok));
2669 /* substitute arguments in replacement lists in macro_str by the values in
2670 args (field d) and return allocated string */
2671 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2673 int last_tok, t, spc;
2674 const int *st;
2675 Sym *s;
2676 CValue cval;
2677 TokenString str;
2678 CString cstr;
2680 tok_str_new(&str);
2681 last_tok = 0;
2682 while(1) {
2683 TOK_GET(&t, &macro_str, &cval);
2684 if (!t)
2685 break;
2686 if (t == '#') {
2687 /* stringize */
2688 TOK_GET(&t, &macro_str, &cval);
2689 if (!t)
2690 break;
2691 s = sym_find2(args, t);
2692 if (s) {
2693 cstr_new(&cstr);
2694 st = s->d;
2695 spc = 0;
2696 while (*st) {
2697 TOK_GET(&t, &st, &cval);
2698 if (!check_space(t, &spc))
2699 cstr_cat(&cstr, get_tok_str(t, &cval));
2701 cstr.size -= spc;
2702 cstr_ccat(&cstr, '\0');
2703 #ifdef PP_DEBUG
2704 printf("stringize: %s\n", (char *)cstr.data);
2705 #endif
2706 /* add string */
2707 cval.cstr = &cstr;
2708 tok_str_add2(&str, TOK_STR, &cval);
2709 cstr_free(&cstr);
2710 } else {
2711 tok_str_add2(&str, t, &cval);
2713 } else if (t >= TOK_IDENT) {
2714 s = sym_find2(args, t);
2715 if (s) {
2716 st = s->d;
2717 /* if '##' is present before or after, no arg substitution */
2718 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2719 /* special case for var arg macros : ## eats the
2720 ',' if empty VA_ARGS variable. */
2721 /* XXX: test of the ',' is not 100%
2722 reliable. should fix it to avoid security
2723 problems */
2724 if (gnu_ext && s->type.t &&
2725 last_tok == TOK_TWOSHARPS &&
2726 str.len >= 2 && str.str[str.len - 2] == ',') {
2727 if (*st == TOK_PLCHLDR) {
2728 /* suppress ',' '##' */
2729 str.len -= 2;
2730 } else {
2731 /* suppress '##' and add variable */
2732 str.len--;
2733 goto add_var;
2735 } else {
2736 int t1;
2737 add_var:
2738 for(;;) {
2739 TOK_GET(&t1, &st, &cval);
2740 if (!t1)
2741 break;
2742 tok_str_add2(&str, t1, &cval);
2745 } else {
2746 /* NOTE: the stream cannot be read when macro
2747 substituing an argument */
2748 macro_subst(&str, nested_list, st, NULL);
2750 } else {
2751 tok_str_add(&str, t);
2753 } else {
2754 tok_str_add2(&str, t, &cval);
2756 last_tok = t;
2758 tok_str_add(&str, 0);
2759 return str.str;
2762 static char const ab_month_name[12][4] =
2764 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2765 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2768 /* do macro substitution of current token with macro 's' and add
2769 result to (tok_str,tok_len). 'nested_list' is the list of all
2770 macros we got inside to avoid recursing. Return non zero if no
2771 substitution needs to be done */
2772 static int macro_subst_tok(TokenString *tok_str,
2773 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2775 Sym *args, *sa, *sa1;
2776 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2777 const int *p;
2778 TokenString str;
2779 char *cstrval;
2780 CValue cval;
2781 CString cstr;
2782 char buf[32];
2784 /* if symbol is a macro, prepare substitution */
2785 /* special macros */
2786 if (tok == TOK___LINE__) {
2787 snprintf(buf, sizeof(buf), "%d", file->line_num);
2788 cstrval = buf;
2789 t1 = TOK_PPNUM;
2790 goto add_cstr1;
2791 } else if (tok == TOK___FILE__) {
2792 cstrval = file->filename;
2793 goto add_cstr;
2794 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2795 time_t ti;
2796 struct tm *tm;
2798 time(&ti);
2799 tm = localtime(&ti);
2800 if (tok == TOK___DATE__) {
2801 snprintf(buf, sizeof(buf), "%s %2d %d",
2802 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2803 } else {
2804 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2805 tm->tm_hour, tm->tm_min, tm->tm_sec);
2807 cstrval = buf;
2808 add_cstr:
2809 t1 = TOK_STR;
2810 add_cstr1:
2811 cstr_new(&cstr);
2812 cstr_cat(&cstr, cstrval);
2813 cstr_ccat(&cstr, '\0');
2814 cval.cstr = &cstr;
2815 tok_str_add2(tok_str, t1, &cval);
2816 cstr_free(&cstr);
2817 } else {
2818 mstr = s->d;
2819 mstr_allocated = 0;
2820 if (s->type.t == MACRO_FUNC) {
2821 /* NOTE: we do not use next_nomacro to avoid eating the
2822 next token. XXX: find better solution */
2823 redo:
2824 if (macro_ptr) {
2825 p = macro_ptr;
2826 while (is_space(t = *p) || TOK_LINEFEED == t)
2827 ++p;
2828 if (t == 0 && can_read_stream) {
2829 /* end of macro stream: we must look at the token
2830 after in the file */
2831 struct macro_level *ml = *can_read_stream;
2832 macro_ptr = NULL;
2833 if (ml)
2835 macro_ptr = ml->p;
2836 ml->p = NULL;
2837 *can_read_stream = ml -> prev;
2839 /* also, end of scope for nested defined symbol */
2840 (*nested_list)->v = -1;
2841 goto redo;
2843 } else {
2844 ch = file->buf_ptr[0];
2845 while (is_space(ch) || ch == '\n' || ch == '/')
2847 if (ch == '/')
2849 int c;
2850 uint8_t *p = file->buf_ptr;
2851 PEEKC(c, p);
2852 if (c == '*') {
2853 p = parse_comment(p);
2854 file->buf_ptr = p - 1;
2855 } else if (c == '/') {
2856 p = parse_line_comment(p);
2857 file->buf_ptr = p - 1;
2858 } else
2859 break;
2861 cinp();
2863 t = ch;
2865 if (t != '(') /* no macro subst */
2866 return -1;
2868 /* argument macro */
2869 next_nomacro();
2870 next_nomacro();
2871 args = NULL;
2872 sa = s->next;
2873 /* NOTE: empty args are allowed, except if no args */
2874 for(;;) {
2875 /* handle '()' case */
2876 if (!args && !sa && tok == ')')
2877 break;
2878 if (!sa)
2879 tcc_error("macro '%s' used with too many args",
2880 get_tok_str(s->v, 0));
2881 tok_str_new(&str);
2882 parlevel = spc = 0;
2883 /* NOTE: non zero sa->t indicates VA_ARGS */
2884 while ((parlevel > 0 ||
2885 (tok != ')' &&
2886 (tok != ',' || sa->type.t))) &&
2887 tok != -1) {
2888 if (tok == '(')
2889 parlevel++;
2890 else if (tok == ')')
2891 parlevel--;
2892 if (tok == TOK_LINEFEED)
2893 tok = ' ';
2894 if (!check_space(tok, &spc))
2895 tok_str_add2(&str, tok, &tokc);
2896 next_nomacro_spc();
2898 if (!str.len)
2899 tok_str_add(&str, TOK_PLCHLDR);
2900 str.len -= spc;
2901 tok_str_add(&str, 0);
2902 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2903 sa1->d = str.str;
2904 sa = sa->next;
2905 if (tok == ')') {
2906 /* special case for gcc var args: add an empty
2907 var arg argument if it is omitted */
2908 if (sa && sa->type.t && gnu_ext)
2909 continue;
2910 else
2911 break;
2913 if (tok != ',')
2914 expect(",");
2915 next_nomacro();
2917 if (sa) {
2918 tcc_error("macro '%s' used with too few args",
2919 get_tok_str(s->v, 0));
2922 /* now subst each arg */
2923 mstr = macro_arg_subst(nested_list, mstr, args);
2924 /* free memory */
2925 sa = args;
2926 while (sa) {
2927 sa1 = sa->prev;
2928 tok_str_free(sa->d);
2929 sym_free(sa);
2930 sa = sa1;
2932 mstr_allocated = 1;
2934 sym_push2(nested_list, s->v, 0, 0);
2935 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2936 /* pop nested defined symbol */
2937 sa1 = *nested_list;
2938 *nested_list = sa1->prev;
2939 sym_free(sa1);
2940 if (mstr_allocated)
2941 tok_str_free(mstr);
2943 return 0;
2946 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2947 return the resulting string (which must be freed). */
2948 static inline int *macro_twosharps(const int *macro_str)
2950 const int *ptr;
2951 int t;
2952 TokenString macro_str1;
2953 CString cstr;
2954 int n, start_of_nosubsts;
2956 /* we search the first '##' */
2957 for(ptr = macro_str;;) {
2958 CValue cval;
2959 TOK_GET(&t, &ptr, &cval);
2960 if (t == TOK_TWOSHARPS)
2961 break;
2962 /* nothing more to do if end of string */
2963 if (t == 0)
2964 return NULL;
2967 /* we saw '##', so we need more processing to handle it */
2968 start_of_nosubsts = -1;
2969 tok_str_new(&macro_str1);
2970 for(ptr = macro_str;;) {
2971 TOK_GET(&tok, &ptr, &tokc);
2972 if (tok == 0)
2973 break;
2974 if (tok == TOK_TWOSHARPS)
2975 continue;
2976 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2977 start_of_nosubsts = macro_str1.len;
2978 while (*ptr == TOK_TWOSHARPS) {
2979 /* given 'a##b', remove nosubsts preceding 'a' */
2980 if (start_of_nosubsts >= 0)
2981 macro_str1.len = start_of_nosubsts;
2982 /* given 'a##b', skip '##' */
2983 t = *++ptr;
2984 /* given 'a##b', remove nosubsts preceding 'b' */
2985 while (t == TOK_NOSUBST)
2986 t = *++ptr;
2987 if (t && t != TOK_TWOSHARPS) {
2988 CValue cval;
2989 TOK_GET(&t, &ptr, &cval);
2990 /* We concatenate the two tokens */
2991 cstr_new(&cstr);
2992 if (tok != TOK_PLCHLDR)
2993 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2994 n = cstr.size;
2995 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
2996 cstr_cat(&cstr, get_tok_str(t, &cval));
2997 cstr_ccat(&cstr, '\0');
2999 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3000 memcpy(file->buffer, cstr.data, cstr.size);
3001 for (;;) {
3002 next_nomacro1();
3003 if (0 == *file->buf_ptr)
3004 break;
3005 tok_str_add2(&macro_str1, tok, &tokc);
3006 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
3007 n, cstr.data, (char*)cstr.data + n);
3009 tcc_close();
3010 cstr_free(&cstr);
3013 if (tok != TOK_NOSUBST) {
3014 tok_str_add2(&macro_str1, tok, &tokc);
3015 tok = ' ';
3016 start_of_nosubsts = -1;
3018 tok_str_add2(&macro_str1, tok, &tokc);
3020 tok_str_add(&macro_str1, 0);
3021 return macro_str1.str;
3025 /* do macro substitution of macro_str and add result to
3026 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3027 inside to avoid recursing. */
3028 static void macro_subst(TokenString *tok_str, Sym **nested_list,
3029 const int *macro_str, struct macro_level ** can_read_stream)
3031 Sym *s;
3032 int *macro_str1;
3033 const int *ptr;
3034 int t, ret, spc;
3035 CValue cval;
3036 struct macro_level ml;
3037 int force_blank;
3039 /* first scan for '##' operator handling */
3040 ptr = macro_str;
3041 macro_str1 = macro_twosharps(ptr);
3043 if (macro_str1)
3044 ptr = macro_str1;
3045 spc = 0;
3046 force_blank = 0;
3048 while (1) {
3049 /* NOTE: ptr == NULL can only happen if tokens are read from
3050 file stream due to a macro function call */
3051 if (ptr == NULL)
3052 break;
3053 TOK_GET(&t, &ptr, &cval);
3054 if (t == 0)
3055 break;
3056 if (t == TOK_NOSUBST) {
3057 /* following token has already been subst'd. just copy it on */
3058 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3059 TOK_GET(&t, &ptr, &cval);
3060 goto no_subst;
3062 s = define_find(t);
3063 if (s != NULL) {
3064 /* if nested substitution, do nothing */
3065 if (sym_find2(*nested_list, t)) {
3066 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3067 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3068 goto no_subst;
3070 ml.p = macro_ptr;
3071 if (can_read_stream)
3072 ml.prev = *can_read_stream, *can_read_stream = &ml;
3073 macro_ptr = (int *)ptr;
3074 tok = t;
3075 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3076 ptr = (int *)macro_ptr;
3077 macro_ptr = ml.p;
3078 if (can_read_stream && *can_read_stream == &ml)
3079 *can_read_stream = ml.prev;
3080 if (ret != 0)
3081 goto no_subst;
3082 if (parse_flags & PARSE_FLAG_SPACES)
3083 force_blank = 1;
3084 } else {
3085 no_subst:
3086 if (force_blank) {
3087 tok_str_add(tok_str, ' ');
3088 spc = 1;
3089 force_blank = 0;
3091 if (!check_space(t, &spc))
3092 tok_str_add2(tok_str, t, &cval);
3095 if (macro_str1)
3096 tok_str_free(macro_str1);
3099 /* return next token with macro substitution */
3100 ST_FUNC void next(void)
3102 Sym *nested_list, *s;
3103 TokenString str;
3104 struct macro_level *ml;
3106 redo:
3107 if (parse_flags & PARSE_FLAG_SPACES)
3108 next_nomacro_spc();
3109 else
3110 next_nomacro();
3111 if (!macro_ptr) {
3112 /* if not reading from macro substituted string, then try
3113 to substitute macros */
3114 if (tok >= TOK_IDENT &&
3115 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3116 s = define_find(tok);
3117 if (s) {
3118 /* we have a macro: we try to substitute */
3119 tok_str_new(&str);
3120 nested_list = NULL;
3121 ml = NULL;
3122 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3123 /* substitution done, NOTE: maybe empty */
3124 tok_str_add(&str, 0);
3125 macro_ptr = str.str;
3126 macro_ptr_allocated = str.str;
3127 goto redo;
3131 } else {
3132 if (tok == 0) {
3133 /* end of macro or end of unget buffer */
3134 if (unget_buffer_enabled) {
3135 macro_ptr = unget_saved_macro_ptr;
3136 unget_buffer_enabled = 0;
3137 } else {
3138 /* end of macro string: free it */
3139 tok_str_free(macro_ptr_allocated);
3140 macro_ptr_allocated = NULL;
3141 macro_ptr = NULL;
3143 goto redo;
3144 } else if (tok == TOK_NOSUBST) {
3145 /* discard preprocessor's nosubst markers */
3146 goto redo;
3150 /* convert preprocessor tokens into C tokens */
3151 if (tok == TOK_PPNUM &&
3152 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3153 parse_number((char *)tokc.cstr->data);
3157 /* push back current token and set current token to 'last_tok'. Only
3158 identifier case handled for labels. */
3159 ST_INLN void unget_tok(int last_tok)
3161 int i, n;
3162 int *q;
3163 if (unget_buffer_enabled)
3165 /* assert(macro_ptr == unget_saved_buffer + 1);
3166 assert(*macro_ptr == 0); */
3168 else
3170 unget_saved_macro_ptr = macro_ptr;
3171 unget_buffer_enabled = 1;
3173 q = unget_saved_buffer;
3174 macro_ptr = q;
3175 *q++ = tok;
3176 n = tok_ext_size(tok) - 1;
3177 for(i=0;i<n;i++)
3178 *q++ = tokc.tab[i];
3179 *q = 0; /* end of token string */
3180 tok = last_tok;
3184 /* better than nothing, but needs extension to handle '-E' option
3185 correctly too */
3186 ST_FUNC void preprocess_init(TCCState *s1)
3188 s1->include_stack_ptr = s1->include_stack;
3189 /* XXX: move that before to avoid having to initialize
3190 file->ifdef_stack_ptr ? */
3191 s1->ifdef_stack_ptr = s1->ifdef_stack;
3192 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3194 vtop = vstack - 1;
3195 s1->pack_stack[0] = 0;
3196 s1->pack_stack_ptr = s1->pack_stack;
3199 ST_FUNC void preprocess_new(void)
3201 int i, c;
3202 const char *p, *r;
3204 /* init isid table */
3205 for(i=CH_EOF;i<256;i++)
3206 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3208 /* add all tokens */
3209 table_ident = NULL;
3210 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3212 tok_ident = TOK_IDENT;
3213 p = tcc_keywords;
3214 while (*p) {
3215 r = p;
3216 for(;;) {
3217 c = *r++;
3218 if (c == '\0')
3219 break;
3221 tok_alloc(p, r - p - 1);
3222 p = r;
3226 /* Preprocess the current file */
3227 ST_FUNC int tcc_preprocess(TCCState *s1)
3229 Sym *define_start;
3231 BufferedFile *file_ref, **iptr, **iptr_new;
3232 int token_seen, line_ref, d;
3233 const char *s;
3235 preprocess_init(s1);
3236 define_start = define_stack;
3237 ch = file->buf_ptr[0];
3238 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3239 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3240 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3241 token_seen = 0;
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;