add push_macro test again
[tinycc.git] / tccpp.c
blob4aa8b952a09cf6635b24aaa2a25aacefd4658a49
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 && parse_flags & PARSE_FLAG_PACK) {
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 if(tmp){
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;
1751 }else{
1752 if(def->off){
1753 --def->off;
1754 }else{
1755 tcc_warning("stack empty");
1759 }else{
1760 fputs("#pragma ", s1->ppfp);
1761 while (tok != TOK_LINEFEED){
1762 fputs(get_tok_str(tok, &tokc), s1->ppfp);
1763 next();
1765 goto the_end;
1767 break;
1768 default:
1769 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1770 /* '!' is ignored to allow C scripts. numbers are ignored
1771 to emulate cpp behaviour */
1772 } else {
1773 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
1774 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1775 else {
1776 /* this is a gas line comment in an 'S' file. */
1777 file->buf_ptr = parse_line_comment(file->buf_ptr);
1778 goto the_end;
1781 break;
1783 /* ignore other preprocess commands or #! for C scripts */
1784 while (tok != TOK_LINEFEED)
1785 next_nomacro();
1786 the_end:
1787 parse_flags = saved_parse_flags;
1790 /* evaluate escape codes in a string. */
1791 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1793 int c, n;
1794 const uint8_t *p;
1796 p = buf;
1797 for(;;) {
1798 c = *p;
1799 if (c == '\0')
1800 break;
1801 if (c == '\\') {
1802 p++;
1803 /* escape */
1804 c = *p;
1805 switch(c) {
1806 case '0': case '1': case '2': case '3':
1807 case '4': case '5': case '6': case '7':
1808 /* at most three octal digits */
1809 n = c - '0';
1810 p++;
1811 c = *p;
1812 if (isoct(c)) {
1813 n = n * 8 + c - '0';
1814 p++;
1815 c = *p;
1816 if (isoct(c)) {
1817 n = n * 8 + c - '0';
1818 p++;
1821 c = n;
1822 goto add_char_nonext;
1823 case 'x':
1824 case 'u':
1825 case 'U':
1826 p++;
1827 n = 0;
1828 for(;;) {
1829 c = *p;
1830 if (c >= 'a' && c <= 'f')
1831 c = c - 'a' + 10;
1832 else if (c >= 'A' && c <= 'F')
1833 c = c - 'A' + 10;
1834 else if (isnum(c))
1835 c = c - '0';
1836 else
1837 break;
1838 n = n * 16 + c;
1839 p++;
1841 c = n;
1842 goto add_char_nonext;
1843 case 'a':
1844 c = '\a';
1845 break;
1846 case 'b':
1847 c = '\b';
1848 break;
1849 case 'f':
1850 c = '\f';
1851 break;
1852 case 'n':
1853 c = '\n';
1854 break;
1855 case 'r':
1856 c = '\r';
1857 break;
1858 case 't':
1859 c = '\t';
1860 break;
1861 case 'v':
1862 c = '\v';
1863 break;
1864 case 'e':
1865 if (!gnu_ext)
1866 goto invalid_escape;
1867 c = 27;
1868 break;
1869 case '\'':
1870 case '\"':
1871 case '\\':
1872 case '?':
1873 break;
1874 default:
1875 invalid_escape:
1876 if (c >= '!' && c <= '~')
1877 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1878 else
1879 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1880 break;
1883 p++;
1884 add_char_nonext:
1885 if (!is_long)
1886 cstr_ccat(outstr, c);
1887 else
1888 cstr_wccat(outstr, c);
1890 /* add a trailing '\0' */
1891 if (!is_long)
1892 cstr_ccat(outstr, '\0');
1893 else
1894 cstr_wccat(outstr, '\0');
1897 /* we use 64 bit numbers */
1898 #define BN_SIZE 2
1900 /* bn = (bn << shift) | or_val */
1901 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1903 int i;
1904 unsigned int v;
1905 for(i=0;i<BN_SIZE;i++) {
1906 v = bn[i];
1907 bn[i] = (v << shift) | or_val;
1908 or_val = v >> (32 - shift);
1912 static void bn_zero(unsigned int *bn)
1914 int i;
1915 for(i=0;i<BN_SIZE;i++) {
1916 bn[i] = 0;
1920 /* parse number in null terminated string 'p' and return it in the
1921 current token */
1922 static void parse_number(const char *p)
1924 int b, t, shift, frac_bits, s, exp_val, ch;
1925 char *q;
1926 unsigned int bn[BN_SIZE];
1927 double d;
1929 /* number */
1930 q = token_buf;
1931 ch = *p++;
1932 t = ch;
1933 ch = *p++;
1934 *q++ = t;
1935 b = 10;
1936 if (t == '.') {
1937 goto float_frac_parse;
1938 } else if (t == '0') {
1939 if (ch == 'x' || ch == 'X') {
1940 q--;
1941 ch = *p++;
1942 b = 16;
1943 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1944 q--;
1945 ch = *p++;
1946 b = 2;
1949 /* parse all digits. cannot check octal numbers at this stage
1950 because of floating point constants */
1951 while (1) {
1952 if (ch >= 'a' && ch <= 'f')
1953 t = ch - 'a' + 10;
1954 else if (ch >= 'A' && ch <= 'F')
1955 t = ch - 'A' + 10;
1956 else if (isnum(ch))
1957 t = ch - '0';
1958 else
1959 break;
1960 if (t >= b)
1961 break;
1962 if (q >= token_buf + STRING_MAX_SIZE) {
1963 num_too_long:
1964 tcc_error("number too long");
1966 *q++ = ch;
1967 ch = *p++;
1969 if (ch == '.' ||
1970 ((ch == 'e' || ch == 'E') && b == 10) ||
1971 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1972 if (b != 10) {
1973 /* NOTE: strtox should support that for hexa numbers, but
1974 non ISOC99 libcs do not support it, so we prefer to do
1975 it by hand */
1976 /* hexadecimal or binary floats */
1977 /* XXX: handle overflows */
1978 *q = '\0';
1979 if (b == 16)
1980 shift = 4;
1981 else
1982 shift = 2;
1983 bn_zero(bn);
1984 q = token_buf;
1985 while (1) {
1986 t = *q++;
1987 if (t == '\0') {
1988 break;
1989 } else if (t >= 'a') {
1990 t = t - 'a' + 10;
1991 } else if (t >= 'A') {
1992 t = t - 'A' + 10;
1993 } else {
1994 t = t - '0';
1996 bn_lshift(bn, shift, t);
1998 frac_bits = 0;
1999 if (ch == '.') {
2000 ch = *p++;
2001 while (1) {
2002 t = ch;
2003 if (t >= 'a' && t <= 'f') {
2004 t = t - 'a' + 10;
2005 } else if (t >= 'A' && t <= 'F') {
2006 t = t - 'A' + 10;
2007 } else if (t >= '0' && t <= '9') {
2008 t = t - '0';
2009 } else {
2010 break;
2012 if (t >= b)
2013 tcc_error("invalid digit");
2014 bn_lshift(bn, shift, t);
2015 frac_bits += shift;
2016 ch = *p++;
2019 if (ch != 'p' && ch != 'P')
2020 expect("exponent");
2021 ch = *p++;
2022 s = 1;
2023 exp_val = 0;
2024 if (ch == '+') {
2025 ch = *p++;
2026 } else if (ch == '-') {
2027 s = -1;
2028 ch = *p++;
2030 if (ch < '0' || ch > '9')
2031 expect("exponent digits");
2032 while (ch >= '0' && ch <= '9') {
2033 exp_val = exp_val * 10 + ch - '0';
2034 ch = *p++;
2036 exp_val = exp_val * s;
2038 /* now we can generate the number */
2039 /* XXX: should patch directly float number */
2040 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
2041 d = ldexp(d, exp_val - frac_bits);
2042 t = toup(ch);
2043 if (t == 'F') {
2044 ch = *p++;
2045 tok = TOK_CFLOAT;
2046 /* float : should handle overflow */
2047 tokc.f = (float)d;
2048 } else if (t == 'L') {
2049 ch = *p++;
2050 #ifdef TCC_TARGET_PE
2051 tok = TOK_CDOUBLE;
2052 tokc.d = d;
2053 #else
2054 tok = TOK_CLDOUBLE;
2055 /* XXX: not large enough */
2056 tokc.ld = (long double)d;
2057 #endif
2058 } else {
2059 tok = TOK_CDOUBLE;
2060 tokc.d = d;
2062 } else {
2063 /* decimal floats */
2064 if (ch == '.') {
2065 if (q >= token_buf + STRING_MAX_SIZE)
2066 goto num_too_long;
2067 *q++ = ch;
2068 ch = *p++;
2069 float_frac_parse:
2070 while (ch >= '0' && ch <= '9') {
2071 if (q >= token_buf + STRING_MAX_SIZE)
2072 goto num_too_long;
2073 *q++ = ch;
2074 ch = *p++;
2077 if (ch == 'e' || ch == 'E') {
2078 if (q >= token_buf + STRING_MAX_SIZE)
2079 goto num_too_long;
2080 *q++ = ch;
2081 ch = *p++;
2082 if (ch == '-' || ch == '+') {
2083 if (q >= token_buf + STRING_MAX_SIZE)
2084 goto num_too_long;
2085 *q++ = ch;
2086 ch = *p++;
2088 if (ch < '0' || ch > '9')
2089 expect("exponent digits");
2090 while (ch >= '0' && ch <= '9') {
2091 if (q >= token_buf + STRING_MAX_SIZE)
2092 goto num_too_long;
2093 *q++ = ch;
2094 ch = *p++;
2097 *q = '\0';
2098 t = toup(ch);
2099 errno = 0;
2100 if (t == 'F') {
2101 ch = *p++;
2102 tok = TOK_CFLOAT;
2103 tokc.f = strtof(token_buf, NULL);
2104 } else if (t == 'L') {
2105 ch = *p++;
2106 #ifdef TCC_TARGET_PE
2107 tok = TOK_CDOUBLE;
2108 tokc.d = strtod(token_buf, NULL);
2109 #else
2110 tok = TOK_CLDOUBLE;
2111 tokc.ld = strtold(token_buf, NULL);
2112 #endif
2113 } else {
2114 tok = TOK_CDOUBLE;
2115 tokc.d = strtod(token_buf, NULL);
2118 } else {
2119 unsigned long long n, n1;
2120 int lcount, ucount;
2122 /* integer number */
2123 *q = '\0';
2124 q = token_buf;
2125 if (b == 10 && *q == '0') {
2126 b = 8;
2127 q++;
2129 n = 0;
2130 while(1) {
2131 t = *q++;
2132 /* no need for checks except for base 10 / 8 errors */
2133 if (t == '\0') {
2134 break;
2135 } else if (t >= 'a') {
2136 t = t - 'a' + 10;
2137 } else if (t >= 'A') {
2138 t = t - 'A' + 10;
2139 } else {
2140 t = t - '0';
2141 if (t >= b)
2142 tcc_error("invalid digit");
2144 n1 = n;
2145 n = n * b + t;
2146 /* detect overflow */
2147 /* XXX: this test is not reliable */
2148 if (n < n1)
2149 tcc_error("integer constant overflow");
2152 /* XXX: not exactly ANSI compliant */
2153 if ((n & 0xffffffff00000000LL) != 0) {
2154 if ((n >> 63) != 0)
2155 tok = TOK_CULLONG;
2156 else
2157 tok = TOK_CLLONG;
2158 } else if (n > 0x7fffffff) {
2159 tok = TOK_CUINT;
2160 } else {
2161 tok = TOK_CINT;
2163 lcount = 0;
2164 ucount = 0;
2165 for(;;) {
2166 t = toup(ch);
2167 if (t == 'L') {
2168 if (lcount >= 2)
2169 tcc_error("three 'l's in integer constant");
2170 lcount++;
2171 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2172 if (lcount == 2) {
2173 #endif
2174 if (tok == TOK_CINT)
2175 tok = TOK_CLLONG;
2176 else if (tok == TOK_CUINT)
2177 tok = TOK_CULLONG;
2178 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2180 #endif
2181 ch = *p++;
2182 } else if (t == 'U') {
2183 if (ucount >= 1)
2184 tcc_error("two 'u's in integer constant");
2185 ucount++;
2186 if (tok == TOK_CINT)
2187 tok = TOK_CUINT;
2188 else if (tok == TOK_CLLONG)
2189 tok = TOK_CULLONG;
2190 ch = *p++;
2191 } else {
2192 break;
2195 if (tok == TOK_CINT || tok == TOK_CUINT)
2196 tokc.ui = n;
2197 else
2198 tokc.ull = n;
2200 if (ch)
2201 tcc_error("invalid number\n");
2205 #define PARSE2(c1, tok1, c2, tok2) \
2206 case c1: \
2207 PEEKC(c, p); \
2208 if (c == c2) { \
2209 p++; \
2210 tok = tok2; \
2211 } else { \
2212 tok = tok1; \
2214 break;
2216 /* return next token without macro substitution */
2217 static inline void next_nomacro1(void)
2219 int t, c, is_long;
2220 TokenSym *ts;
2221 uint8_t *p, *p1;
2222 unsigned int h;
2224 p = file->buf_ptr;
2225 redo_no_start:
2226 c = *p;
2227 switch(c) {
2228 case ' ':
2229 case '\t':
2230 tok = c;
2231 p++;
2232 goto keep_tok_flags;
2233 case '\f':
2234 case '\v':
2235 case '\r':
2236 p++;
2237 goto redo_no_start;
2238 case '\\':
2239 /* first look if it is in fact an end of buffer */
2240 if (p >= file->buf_end) {
2241 file->buf_ptr = p;
2242 handle_eob();
2243 p = file->buf_ptr;
2244 if (p >= file->buf_end)
2245 goto parse_eof;
2246 else
2247 goto redo_no_start;
2248 } else {
2249 file->buf_ptr = p;
2250 ch = *p;
2251 handle_stray();
2252 p = file->buf_ptr;
2253 goto redo_no_start;
2255 parse_eof:
2257 TCCState *s1 = tcc_state;
2258 if ((parse_flags & PARSE_FLAG_LINEFEED)
2259 && !(tok_flags & TOK_FLAG_EOF)) {
2260 tok_flags |= TOK_FLAG_EOF;
2261 tok = TOK_LINEFEED;
2262 goto keep_tok_flags;
2263 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2264 tok = TOK_EOF;
2265 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2266 tcc_error("missing #endif");
2267 } else if (s1->include_stack_ptr == s1->include_stack) {
2268 /* no include left : end of file. */
2269 tok = TOK_EOF;
2270 } else {
2271 tok_flags &= ~TOK_FLAG_EOF;
2272 /* pop include file */
2274 /* test if previous '#endif' was after a #ifdef at
2275 start of file */
2276 if (tok_flags & TOK_FLAG_ENDIF) {
2277 #ifdef INC_DEBUG
2278 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2279 #endif
2280 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2281 tok_flags &= ~TOK_FLAG_ENDIF;
2284 /* add end of include file debug info */
2285 if (tcc_state->do_debug) {
2286 put_stabd(N_EINCL, 0, 0);
2288 /* pop include stack */
2289 tcc_close();
2290 s1->include_stack_ptr--;
2291 p = file->buf_ptr;
2292 goto redo_no_start;
2295 break;
2297 case '\n':
2298 file->line_num++;
2299 tok_flags |= TOK_FLAG_BOL;
2300 p++;
2301 maybe_newline:
2302 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2303 goto redo_no_start;
2304 tok = TOK_LINEFEED;
2305 goto keep_tok_flags;
2307 case '#':
2308 /* XXX: simplify */
2309 PEEKC(c, p);
2310 if ((tok_flags & TOK_FLAG_BOL) &&
2311 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2312 file->buf_ptr = p;
2313 preprocess(tok_flags & TOK_FLAG_BOF);
2314 p = file->buf_ptr;
2315 goto maybe_newline;
2316 } else {
2317 if (c == '#') {
2318 p++;
2319 tok = TOK_TWOSHARPS;
2320 } else {
2321 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2322 p = parse_line_comment(p - 1);
2323 goto redo_no_start;
2324 } else {
2325 tok = '#';
2329 break;
2331 case 'a': case 'b': case 'c': case 'd':
2332 case 'e': case 'f': case 'g': case 'h':
2333 case 'i': case 'j': case 'k': case 'l':
2334 case 'm': case 'n': case 'o': case 'p':
2335 case 'q': case 'r': case 's': case 't':
2336 case 'u': case 'v': case 'w': case 'x':
2337 case 'y': case 'z':
2338 case 'A': case 'B': case 'C': case 'D':
2339 case 'E': case 'F': case 'G': case 'H':
2340 case 'I': case 'J': case 'K':
2341 case 'M': case 'N': case 'O': case 'P':
2342 case 'Q': case 'R': case 'S': case 'T':
2343 case 'U': case 'V': case 'W': case 'X':
2344 case 'Y': case 'Z':
2345 case '_':
2346 parse_ident_fast:
2347 p1 = p;
2348 h = TOK_HASH_INIT;
2349 h = TOK_HASH_FUNC(h, c);
2350 p++;
2351 for(;;) {
2352 c = *p;
2353 if (!isidnum_table[c-CH_EOF])
2354 break;
2355 h = TOK_HASH_FUNC(h, c);
2356 p++;
2358 if (c != '\\') {
2359 TokenSym **pts;
2360 int len;
2362 /* fast case : no stray found, so we have the full token
2363 and we have already hashed it */
2364 len = p - p1;
2365 h &= (TOK_HASH_SIZE - 1);
2366 pts = &hash_ident[h];
2367 for(;;) {
2368 ts = *pts;
2369 if (!ts)
2370 break;
2371 if (ts->len == len && !memcmp(ts->str, p1, len))
2372 goto token_found;
2373 pts = &(ts->hash_next);
2375 ts = tok_alloc_new(pts, (char *) p1, len);
2376 token_found: ;
2377 } else {
2378 /* slower case */
2379 cstr_reset(&tokcstr);
2381 while (p1 < p) {
2382 cstr_ccat(&tokcstr, *p1);
2383 p1++;
2385 p--;
2386 PEEKC(c, p);
2387 parse_ident_slow:
2388 while (isidnum_table[c-CH_EOF]) {
2389 cstr_ccat(&tokcstr, c);
2390 PEEKC(c, p);
2392 ts = tok_alloc(tokcstr.data, tokcstr.size);
2394 tok = ts->tok;
2395 break;
2396 case 'L':
2397 t = p[1];
2398 if (t != '\\' && t != '\'' && t != '\"') {
2399 /* fast case */
2400 goto parse_ident_fast;
2401 } else {
2402 PEEKC(c, p);
2403 if (c == '\'' || c == '\"') {
2404 is_long = 1;
2405 goto str_const;
2406 } else {
2407 cstr_reset(&tokcstr);
2408 cstr_ccat(&tokcstr, 'L');
2409 goto parse_ident_slow;
2412 break;
2413 case '0': case '1': case '2': case '3':
2414 case '4': case '5': case '6': case '7':
2415 case '8': case '9':
2417 cstr_reset(&tokcstr);
2418 /* after the first digit, accept digits, alpha, '.' or sign if
2419 prefixed by 'eEpP' */
2420 parse_num:
2421 for(;;) {
2422 t = c;
2423 cstr_ccat(&tokcstr, c);
2424 PEEKC(c, p);
2425 if (!(isnum(c) || isid(c) || c == '.' ||
2426 ((c == '+' || c == '-') &&
2427 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2428 break;
2430 /* We add a trailing '\0' to ease parsing */
2431 cstr_ccat(&tokcstr, '\0');
2432 tokc.cstr = &tokcstr;
2433 tok = TOK_PPNUM;
2434 break;
2435 case '.':
2436 /* special dot handling because it can also start a number */
2437 PEEKC(c, p);
2438 if (isnum(c)) {
2439 cstr_reset(&tokcstr);
2440 cstr_ccat(&tokcstr, '.');
2441 goto parse_num;
2442 } else if (c == '.') {
2443 PEEKC(c, p);
2444 if (c != '.')
2445 expect("'.'");
2446 PEEKC(c, p);
2447 tok = TOK_DOTS;
2448 } else {
2449 tok = '.';
2451 break;
2452 case '\'':
2453 case '\"':
2454 is_long = 0;
2455 str_const:
2457 CString str;
2458 int sep;
2460 sep = c;
2462 /* parse the string */
2463 cstr_new(&str);
2464 p = parse_pp_string(p, sep, &str);
2465 cstr_ccat(&str, '\0');
2467 /* eval the escape (should be done as TOK_PPNUM) */
2468 cstr_reset(&tokcstr);
2469 parse_escape_string(&tokcstr, str.data, is_long);
2470 cstr_free(&str);
2472 if (sep == '\'') {
2473 int char_size;
2474 /* XXX: make it portable */
2475 if (!is_long)
2476 char_size = 1;
2477 else
2478 char_size = sizeof(nwchar_t);
2479 if (tokcstr.size <= char_size)
2480 tcc_error("empty character constant");
2481 if (tokcstr.size > 2 * char_size)
2482 tcc_warning("multi-character character constant");
2483 if (!is_long) {
2484 tokc.i = *(int8_t *)tokcstr.data;
2485 tok = TOK_CCHAR;
2486 } else {
2487 tokc.i = *(nwchar_t *)tokcstr.data;
2488 tok = TOK_LCHAR;
2490 } else {
2491 tokc.cstr = &tokcstr;
2492 if (!is_long)
2493 tok = TOK_STR;
2494 else
2495 tok = TOK_LSTR;
2498 break;
2500 case '<':
2501 PEEKC(c, p);
2502 if (c == '=') {
2503 p++;
2504 tok = TOK_LE;
2505 } else if (c == '<') {
2506 PEEKC(c, p);
2507 if (c == '=') {
2508 p++;
2509 tok = TOK_A_SHL;
2510 } else {
2511 tok = TOK_SHL;
2513 } else {
2514 tok = TOK_LT;
2516 break;
2518 case '>':
2519 PEEKC(c, p);
2520 if (c == '=') {
2521 p++;
2522 tok = TOK_GE;
2523 } else if (c == '>') {
2524 PEEKC(c, p);
2525 if (c == '=') {
2526 p++;
2527 tok = TOK_A_SAR;
2528 } else {
2529 tok = TOK_SAR;
2531 } else {
2532 tok = TOK_GT;
2534 break;
2536 case '&':
2537 PEEKC(c, p);
2538 if (c == '&') {
2539 p++;
2540 tok = TOK_LAND;
2541 } else if (c == '=') {
2542 p++;
2543 tok = TOK_A_AND;
2544 } else {
2545 tok = '&';
2547 break;
2549 case '|':
2550 PEEKC(c, p);
2551 if (c == '|') {
2552 p++;
2553 tok = TOK_LOR;
2554 } else if (c == '=') {
2555 p++;
2556 tok = TOK_A_OR;
2557 } else {
2558 tok = '|';
2560 break;
2562 case '+':
2563 PEEKC(c, p);
2564 if (c == '+') {
2565 p++;
2566 tok = TOK_INC;
2567 } else if (c == '=') {
2568 p++;
2569 tok = TOK_A_ADD;
2570 } else {
2571 tok = '+';
2573 break;
2575 case '-':
2576 PEEKC(c, p);
2577 if (c == '-') {
2578 p++;
2579 tok = TOK_DEC;
2580 } else if (c == '=') {
2581 p++;
2582 tok = TOK_A_SUB;
2583 } else if (c == '>') {
2584 p++;
2585 tok = TOK_ARROW;
2586 } else {
2587 tok = '-';
2589 break;
2591 PARSE2('!', '!', '=', TOK_NE)
2592 PARSE2('=', '=', '=', TOK_EQ)
2593 PARSE2('*', '*', '=', TOK_A_MUL)
2594 PARSE2('%', '%', '=', TOK_A_MOD)
2595 PARSE2('^', '^', '=', TOK_A_XOR)
2597 /* comments or operator */
2598 case '/':
2599 PEEKC(c, p);
2600 if (c == '*') {
2601 p = parse_comment(p);
2602 /* comments replaced by a blank */
2603 tok = ' ';
2604 goto keep_tok_flags;
2605 } else if (c == '/') {
2606 p = parse_line_comment(p);
2607 tok = ' ';
2608 goto keep_tok_flags;
2609 } else if (c == '=') {
2610 p++;
2611 tok = TOK_A_DIV;
2612 } else {
2613 tok = '/';
2615 break;
2617 /* simple tokens */
2618 case '(':
2619 case ')':
2620 case '[':
2621 case ']':
2622 case '{':
2623 case '}':
2624 case ',':
2625 case ';':
2626 case ':':
2627 case '?':
2628 case '~':
2629 case '$': /* only used in assembler */
2630 case '@': /* dito */
2631 tok = c;
2632 p++;
2633 break;
2634 default:
2635 tcc_error("unrecognized character \\x%02x", c);
2636 break;
2638 tok_flags = 0;
2639 keep_tok_flags:
2640 file->buf_ptr = p;
2641 #if defined(PARSE_DEBUG)
2642 printf("token = %s\n", get_tok_str(tok, &tokc));
2643 #endif
2646 /* return next token without macro substitution. Can read input from
2647 macro_ptr buffer */
2648 static void next_nomacro_spc(void)
2650 if (macro_ptr) {
2651 redo:
2652 tok = *macro_ptr;
2653 if (tok) {
2654 TOK_GET(&tok, &macro_ptr, &tokc);
2655 if (tok == TOK_LINENUM) {
2656 file->line_num = tokc.i;
2657 goto redo;
2660 } else {
2661 next_nomacro1();
2665 ST_FUNC void next_nomacro(void)
2667 do {
2668 next_nomacro_spc();
2669 } while (is_space(tok));
2672 /* substitute arguments in replacement lists in macro_str by the values in
2673 args (field d) and return allocated string */
2674 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2676 int last_tok, t, spc;
2677 const int *st;
2678 Sym *s;
2679 CValue cval;
2680 TokenString str;
2681 CString cstr;
2683 tok_str_new(&str);
2684 last_tok = 0;
2685 while(1) {
2686 TOK_GET(&t, &macro_str, &cval);
2687 if (!t)
2688 break;
2689 if (t == '#') {
2690 /* stringize */
2691 TOK_GET(&t, &macro_str, &cval);
2692 if (!t)
2693 break;
2694 s = sym_find2(args, t);
2695 if (s) {
2696 cstr_new(&cstr);
2697 st = s->d;
2698 spc = 0;
2699 while (*st) {
2700 TOK_GET(&t, &st, &cval);
2701 if (!check_space(t, &spc))
2702 cstr_cat(&cstr, get_tok_str(t, &cval));
2704 cstr.size -= spc;
2705 cstr_ccat(&cstr, '\0');
2706 #ifdef PP_DEBUG
2707 printf("stringize: %s\n", (char *)cstr.data);
2708 #endif
2709 /* add string */
2710 cval.cstr = &cstr;
2711 tok_str_add2(&str, TOK_STR, &cval);
2712 cstr_free(&cstr);
2713 } else {
2714 tok_str_add2(&str, t, &cval);
2716 } else if (t >= TOK_IDENT) {
2717 s = sym_find2(args, t);
2718 if (s) {
2719 st = s->d;
2720 /* if '##' is present before or after, no arg substitution */
2721 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2722 /* special case for var arg macros : ## eats the
2723 ',' if empty VA_ARGS variable. */
2724 /* XXX: test of the ',' is not 100%
2725 reliable. should fix it to avoid security
2726 problems */
2727 if (gnu_ext && s->type.t &&
2728 last_tok == TOK_TWOSHARPS &&
2729 str.len >= 2 && str.str[str.len - 2] == ',') {
2730 if (*st == TOK_PLCHLDR) {
2731 /* suppress ',' '##' */
2732 str.len -= 2;
2733 } else {
2734 /* suppress '##' and add variable */
2735 str.len--;
2736 goto add_var;
2738 } else {
2739 int t1;
2740 add_var:
2741 for(;;) {
2742 TOK_GET(&t1, &st, &cval);
2743 if (!t1)
2744 break;
2745 tok_str_add2(&str, t1, &cval);
2748 } else {
2749 /* NOTE: the stream cannot be read when macro
2750 substituing an argument */
2751 macro_subst(&str, nested_list, st, NULL);
2753 } else {
2754 tok_str_add(&str, t);
2756 } else {
2757 tok_str_add2(&str, t, &cval);
2759 last_tok = t;
2761 tok_str_add(&str, 0);
2762 return str.str;
2765 static char const ab_month_name[12][4] =
2767 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2768 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2771 /* do macro substitution of current token with macro 's' and add
2772 result to (tok_str,tok_len). 'nested_list' is the list of all
2773 macros we got inside to avoid recursing. Return non zero if no
2774 substitution needs to be done */
2775 static int macro_subst_tok(TokenString *tok_str,
2776 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2778 Sym *args, *sa, *sa1;
2779 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2780 const int *p;
2781 TokenString str;
2782 char *cstrval;
2783 CValue cval;
2784 CString cstr;
2785 char buf[32];
2787 /* if symbol is a macro, prepare substitution */
2788 /* special macros */
2789 if (tok == TOK___LINE__) {
2790 snprintf(buf, sizeof(buf), "%d", file->line_num);
2791 cstrval = buf;
2792 t1 = TOK_PPNUM;
2793 goto add_cstr1;
2794 } else if (tok == TOK___FILE__) {
2795 cstrval = file->filename;
2796 goto add_cstr;
2797 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2798 time_t ti;
2799 struct tm *tm;
2801 time(&ti);
2802 tm = localtime(&ti);
2803 if (tok == TOK___DATE__) {
2804 snprintf(buf, sizeof(buf), "%s %2d %d",
2805 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2806 } else {
2807 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2808 tm->tm_hour, tm->tm_min, tm->tm_sec);
2810 cstrval = buf;
2811 add_cstr:
2812 t1 = TOK_STR;
2813 add_cstr1:
2814 cstr_new(&cstr);
2815 cstr_cat(&cstr, cstrval);
2816 cstr_ccat(&cstr, '\0');
2817 cval.cstr = &cstr;
2818 tok_str_add2(tok_str, t1, &cval);
2819 cstr_free(&cstr);
2820 } else {
2821 mstr = s->d;
2822 mstr_allocated = 0;
2823 if (s->type.t == MACRO_FUNC) {
2824 /* NOTE: we do not use next_nomacro to avoid eating the
2825 next token. XXX: find better solution */
2826 redo:
2827 if (macro_ptr) {
2828 p = macro_ptr;
2829 while (is_space(t = *p) || TOK_LINEFEED == t)
2830 ++p;
2831 if (t == 0 && can_read_stream) {
2832 /* end of macro stream: we must look at the token
2833 after in the file */
2834 struct macro_level *ml = *can_read_stream;
2835 macro_ptr = NULL;
2836 if (ml)
2838 macro_ptr = ml->p;
2839 ml->p = NULL;
2840 *can_read_stream = ml -> prev;
2842 /* also, end of scope for nested defined symbol */
2843 (*nested_list)->v = -1;
2844 goto redo;
2846 } else {
2847 ch = file->buf_ptr[0];
2848 while (is_space(ch) || ch == '\n' || ch == '/')
2850 if (ch == '/')
2852 int c;
2853 uint8_t *p = file->buf_ptr;
2854 PEEKC(c, p);
2855 if (c == '*') {
2856 p = parse_comment(p);
2857 file->buf_ptr = p - 1;
2858 } else if (c == '/') {
2859 p = parse_line_comment(p);
2860 file->buf_ptr = p - 1;
2861 } else
2862 break;
2864 cinp();
2866 t = ch;
2868 if (t != '(') /* no macro subst */
2869 return -1;
2871 /* argument macro */
2872 next_nomacro();
2873 next_nomacro();
2874 args = NULL;
2875 sa = s->next;
2876 /* NOTE: empty args are allowed, except if no args */
2877 for(;;) {
2878 /* handle '()' case */
2879 if (!args && !sa && tok == ')')
2880 break;
2881 if (!sa)
2882 tcc_error("macro '%s' used with too many args",
2883 get_tok_str(s->v, 0));
2884 tok_str_new(&str);
2885 parlevel = spc = 0;
2886 /* NOTE: non zero sa->t indicates VA_ARGS */
2887 while ((parlevel > 0 ||
2888 (tok != ')' &&
2889 (tok != ',' || sa->type.t))) &&
2890 tok != -1) {
2891 if (tok == '(')
2892 parlevel++;
2893 else if (tok == ')')
2894 parlevel--;
2895 if (tok == TOK_LINEFEED)
2896 tok = ' ';
2897 if (!check_space(tok, &spc))
2898 tok_str_add2(&str, tok, &tokc);
2899 next_nomacro_spc();
2901 if (!str.len)
2902 tok_str_add(&str, TOK_PLCHLDR);
2903 str.len -= spc;
2904 tok_str_add(&str, 0);
2905 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2906 sa1->d = str.str;
2907 sa = sa->next;
2908 if (tok == ')') {
2909 /* special case for gcc var args: add an empty
2910 var arg argument if it is omitted */
2911 if (sa && sa->type.t && gnu_ext)
2912 continue;
2913 else
2914 break;
2916 if (tok != ',')
2917 expect(",");
2918 next_nomacro();
2920 if (sa) {
2921 tcc_error("macro '%s' used with too few args",
2922 get_tok_str(s->v, 0));
2925 /* now subst each arg */
2926 mstr = macro_arg_subst(nested_list, mstr, args);
2927 /* free memory */
2928 sa = args;
2929 while (sa) {
2930 sa1 = sa->prev;
2931 tok_str_free(sa->d);
2932 sym_free(sa);
2933 sa = sa1;
2935 mstr_allocated = 1;
2937 sym_push2(nested_list, s->v, 0, 0);
2938 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2939 /* pop nested defined symbol */
2940 sa1 = *nested_list;
2941 *nested_list = sa1->prev;
2942 sym_free(sa1);
2943 if (mstr_allocated)
2944 tok_str_free(mstr);
2946 return 0;
2949 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2950 return the resulting string (which must be freed). */
2951 static inline int *macro_twosharps(const int *macro_str)
2953 const int *ptr;
2954 int t;
2955 TokenString macro_str1;
2956 CString cstr;
2957 int n, start_of_nosubsts;
2959 /* we search the first '##' */
2960 for(ptr = macro_str;;) {
2961 CValue cval;
2962 TOK_GET(&t, &ptr, &cval);
2963 if (t == TOK_TWOSHARPS)
2964 break;
2965 /* nothing more to do if end of string */
2966 if (t == 0)
2967 return NULL;
2970 /* we saw '##', so we need more processing to handle it */
2971 start_of_nosubsts = -1;
2972 tok_str_new(&macro_str1);
2973 for(ptr = macro_str;;) {
2974 TOK_GET(&tok, &ptr, &tokc);
2975 if (tok == 0)
2976 break;
2977 if (tok == TOK_TWOSHARPS)
2978 continue;
2979 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2980 start_of_nosubsts = macro_str1.len;
2981 while (*ptr == TOK_TWOSHARPS) {
2982 /* given 'a##b', remove nosubsts preceding 'a' */
2983 if (start_of_nosubsts >= 0)
2984 macro_str1.len = start_of_nosubsts;
2985 /* given 'a##b', skip '##' */
2986 t = *++ptr;
2987 /* given 'a##b', remove nosubsts preceding 'b' */
2988 while (t == TOK_NOSUBST)
2989 t = *++ptr;
2990 if (t && t != TOK_TWOSHARPS) {
2991 CValue cval;
2992 TOK_GET(&t, &ptr, &cval);
2993 /* We concatenate the two tokens */
2994 cstr_new(&cstr);
2995 if (tok != TOK_PLCHLDR)
2996 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2997 n = cstr.size;
2998 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
2999 cstr_cat(&cstr, get_tok_str(t, &cval));
3000 cstr_ccat(&cstr, '\0');
3002 tcc_open_bf(tcc_state, ":paste:", cstr.size);
3003 memcpy(file->buffer, cstr.data, cstr.size);
3004 for (;;) {
3005 next_nomacro1();
3006 if (0 == *file->buf_ptr)
3007 break;
3008 tok_str_add2(&macro_str1, tok, &tokc);
3009 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
3010 n, cstr.data, (char*)cstr.data + n);
3012 tcc_close();
3013 cstr_free(&cstr);
3016 if (tok != TOK_NOSUBST) {
3017 tok_str_add2(&macro_str1, tok, &tokc);
3018 tok = ' ';
3019 start_of_nosubsts = -1;
3021 tok_str_add2(&macro_str1, tok, &tokc);
3023 tok_str_add(&macro_str1, 0);
3024 return macro_str1.str;
3028 /* do macro substitution of macro_str and add result to
3029 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3030 inside to avoid recursing. */
3031 static void macro_subst(TokenString *tok_str, Sym **nested_list,
3032 const int *macro_str, struct macro_level ** can_read_stream)
3034 Sym *s;
3035 int *macro_str1;
3036 const int *ptr;
3037 int t, ret, spc;
3038 CValue cval;
3039 struct macro_level ml;
3040 int force_blank;
3042 /* first scan for '##' operator handling */
3043 ptr = macro_str;
3044 macro_str1 = macro_twosharps(ptr);
3046 if (macro_str1)
3047 ptr = macro_str1;
3048 spc = 0;
3049 force_blank = 0;
3051 while (1) {
3052 /* NOTE: ptr == NULL can only happen if tokens are read from
3053 file stream due to a macro function call */
3054 if (ptr == NULL)
3055 break;
3056 TOK_GET(&t, &ptr, &cval);
3057 if (t == 0)
3058 break;
3059 if (t == TOK_NOSUBST) {
3060 /* following token has already been subst'd. just copy it on */
3061 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3062 TOK_GET(&t, &ptr, &cval);
3063 goto no_subst;
3065 s = define_find(t);
3066 if (s != NULL) {
3067 /* if nested substitution, do nothing */
3068 if (sym_find2(*nested_list, t)) {
3069 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3070 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3071 goto no_subst;
3073 ml.p = macro_ptr;
3074 if (can_read_stream)
3075 ml.prev = *can_read_stream, *can_read_stream = &ml;
3076 macro_ptr = (int *)ptr;
3077 tok = t;
3078 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3079 ptr = (int *)macro_ptr;
3080 macro_ptr = ml.p;
3081 if (can_read_stream && *can_read_stream == &ml)
3082 *can_read_stream = ml.prev;
3083 if (ret != 0)
3084 goto no_subst;
3085 if (parse_flags & PARSE_FLAG_SPACES)
3086 force_blank = 1;
3087 } else {
3088 no_subst:
3089 if (force_blank) {
3090 tok_str_add(tok_str, ' ');
3091 spc = 1;
3092 force_blank = 0;
3094 if (!check_space(t, &spc))
3095 tok_str_add2(tok_str, t, &cval);
3098 if (macro_str1)
3099 tok_str_free(macro_str1);
3102 /* return next token with macro substitution */
3103 ST_FUNC void next(void)
3105 Sym *nested_list, *s;
3106 TokenString str;
3107 struct macro_level *ml;
3109 redo:
3110 if (parse_flags & PARSE_FLAG_SPACES)
3111 next_nomacro_spc();
3112 else
3113 next_nomacro();
3114 if (!macro_ptr) {
3115 /* if not reading from macro substituted string, then try
3116 to substitute macros */
3117 if (tok >= TOK_IDENT &&
3118 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3119 s = define_find(tok);
3120 if (s) {
3121 /* we have a macro: we try to substitute */
3122 tok_str_new(&str);
3123 nested_list = NULL;
3124 ml = NULL;
3125 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3126 /* substitution done, NOTE: maybe empty */
3127 tok_str_add(&str, 0);
3128 macro_ptr = str.str;
3129 macro_ptr_allocated = str.str;
3130 goto redo;
3134 } else {
3135 if (tok == 0) {
3136 /* end of macro or end of unget buffer */
3137 if (unget_buffer_enabled) {
3138 macro_ptr = unget_saved_macro_ptr;
3139 unget_buffer_enabled = 0;
3140 } else {
3141 /* end of macro string: free it */
3142 tok_str_free(macro_ptr_allocated);
3143 macro_ptr_allocated = NULL;
3144 macro_ptr = NULL;
3146 goto redo;
3147 } else if (tok == TOK_NOSUBST) {
3148 /* discard preprocessor's nosubst markers */
3149 goto redo;
3153 /* convert preprocessor tokens into C tokens */
3154 if (tok == TOK_PPNUM &&
3155 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3156 parse_number((char *)tokc.cstr->data);
3160 /* push back current token and set current token to 'last_tok'. Only
3161 identifier case handled for labels. */
3162 ST_INLN void unget_tok(int last_tok)
3164 int i, n;
3165 int *q;
3166 if (unget_buffer_enabled)
3168 /* assert(macro_ptr == unget_saved_buffer + 1);
3169 assert(*macro_ptr == 0); */
3171 else
3173 unget_saved_macro_ptr = macro_ptr;
3174 unget_buffer_enabled = 1;
3176 q = unget_saved_buffer;
3177 macro_ptr = q;
3178 *q++ = tok;
3179 n = tok_ext_size(tok) - 1;
3180 for(i=0;i<n;i++)
3181 *q++ = tokc.tab[i];
3182 *q = 0; /* end of token string */
3183 tok = last_tok;
3187 /* better than nothing, but needs extension to handle '-E' option
3188 correctly too */
3189 ST_FUNC void preprocess_init(TCCState *s1)
3191 s1->include_stack_ptr = s1->include_stack;
3192 /* XXX: move that before to avoid having to initialize
3193 file->ifdef_stack_ptr ? */
3194 s1->ifdef_stack_ptr = s1->ifdef_stack;
3195 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3197 vtop = vstack - 1;
3198 s1->pack_stack[0] = 0;
3199 s1->pack_stack_ptr = s1->pack_stack;
3202 ST_FUNC void preprocess_new(void)
3204 int i, c;
3205 const char *p, *r;
3207 /* init isid table */
3208 for(i=CH_EOF;i<256;i++)
3209 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3211 /* add all tokens */
3212 table_ident = NULL;
3213 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3215 tok_ident = TOK_IDENT;
3216 p = tcc_keywords;
3217 while (*p) {
3218 r = p;
3219 for(;;) {
3220 c = *r++;
3221 if (c == '\0')
3222 break;
3224 tok_alloc(p, r - p - 1);
3225 p = r;
3229 /* Preprocess the current file */
3230 ST_FUNC int tcc_preprocess(TCCState *s1)
3232 Sym *define_start;
3234 BufferedFile *file_ref, **iptr, **iptr_new;
3235 int token_seen, line_ref, d;
3236 const char *s;
3238 preprocess_init(s1);
3239 define_start = define_stack;
3240 ch = file->buf_ptr[0];
3241 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3242 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3243 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3244 token_seen = 0;
3245 line_ref = 0;
3246 file_ref = NULL;
3247 iptr = s1->include_stack_ptr;
3248 tok = TOK_LINEFEED; /* print line */
3249 goto print_line;
3250 for (;;) {
3251 next();
3252 if (tok == TOK_EOF) {
3253 break;
3254 } else if (file != file_ref) {
3255 goto print_line;
3256 } else if (tok == TOK_LINEFEED) {
3257 if (token_seen)
3258 continue;
3259 ++line_ref;
3260 token_seen = 1;
3261 } else if (token_seen) {
3262 d = file->line_num - line_ref;
3263 if (file != file_ref || d < 0 || d >= 8) {
3264 print_line:
3265 iptr_new = s1->include_stack_ptr;
3266 s = iptr_new > iptr ? " 1"
3267 : iptr_new < iptr ? " 2"
3268 : iptr_new > s1->include_stack ? " 3"
3269 : "";
3270 iptr = iptr_new;
3271 fprintf(s1->ppfp, "# %d \"%s\"%s\n", file->line_num, file->filename, s);
3272 } else {
3273 while (d)
3274 fputs("\n", s1->ppfp), --d;
3276 line_ref = (file_ref = file)->line_num;
3277 token_seen = tok == TOK_LINEFEED;
3278 if (token_seen)
3279 continue;
3281 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3283 free_defines(define_start);
3284 return 0;