fix a preprocessor for .S
[tinycc.git] / tccpp.c
blob4d12be16e660f12376439497183ab1a7c14daf31
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 ST_DATA int tok_flags;
27 ST_DATA int parse_flags;
29 ST_DATA struct BufferedFile *file;
30 ST_DATA int ch, tok;
31 ST_DATA CValue tokc;
32 ST_DATA const int *macro_ptr;
33 ST_DATA CString tokcstr; /* current parsed string, if any */
35 /* display benchmark infos */
36 ST_DATA int total_lines;
37 ST_DATA int total_bytes;
38 ST_DATA int tok_ident;
39 ST_DATA TokenSym **table_ident;
41 /* ------------------------------------------------------------------------- */
43 static int *macro_ptr_allocated;
44 static const int *unget_saved_macro_ptr;
45 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
46 static int unget_buffer_enabled;
47 static TokenSym *hash_ident[TOK_HASH_SIZE];
48 static char token_buf[STRING_MAX_SIZE + 1];
49 /* true if isid(c) || isnum(c) */
50 static unsigned char isidnum_table[256-CH_EOF];
52 static const char tcc_keywords[] =
53 #define DEF(id, str) str "\0"
54 #include "tcctok.h"
55 #undef DEF
58 /* WARNING: the content of this string encodes token numbers */
59 static const unsigned char tok_two_chars[] =
60 /* outdated -- gr
61 "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
62 "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
63 */{
64 '<','=', TOK_LE,
65 '>','=', TOK_GE,
66 '!','=', TOK_NE,
67 '&','&', TOK_LAND,
68 '|','|', TOK_LOR,
69 '+','+', TOK_INC,
70 '-','-', TOK_DEC,
71 '=','=', TOK_EQ,
72 '<','<', TOK_SHL,
73 '>','>', TOK_SAR,
74 '+','=', TOK_A_ADD,
75 '-','=', TOK_A_SUB,
76 '*','=', TOK_A_MUL,
77 '/','=', TOK_A_DIV,
78 '%','=', TOK_A_MOD,
79 '&','=', TOK_A_AND,
80 '^','=', TOK_A_XOR,
81 '|','=', TOK_A_OR,
82 '-','>', TOK_ARROW,
83 '.','.', 0xa8, // C++ token ?
84 '#','#', TOK_TWOSHARPS,
88 struct macro_level {
89 struct macro_level *prev;
90 const int *p;
93 static void next_nomacro_spc(void);
94 static void macro_subst(
95 TokenString *tok_str,
96 Sym **nested_list,
97 const int *macro_str,
98 struct macro_level **can_read_stream
101 ST_FUNC void skip(int c)
103 if (tok != c)
104 tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
105 next();
108 ST_FUNC void expect(const char *msg)
110 tcc_error("%s expected", msg);
113 /* ------------------------------------------------------------------------- */
114 /* CString handling */
115 static void cstr_realloc(CString *cstr, int new_size)
117 int size;
118 void *data;
120 size = cstr->size_allocated;
121 if (size == 0)
122 size = 8; /* no need to allocate a too small first string */
123 while (size < new_size)
124 size = size * 2;
125 data = tcc_realloc(cstr->data_allocated, size);
126 cstr->data_allocated = data;
127 cstr->size_allocated = size;
128 cstr->data = data;
131 /* add a byte */
132 ST_FUNC void cstr_ccat(CString *cstr, int ch)
134 int size;
135 size = cstr->size + 1;
136 if (size > cstr->size_allocated)
137 cstr_realloc(cstr, size);
138 ((unsigned char *)cstr->data)[size - 1] = ch;
139 cstr->size = size;
142 ST_FUNC void cstr_cat(CString *cstr, const char *str)
144 int c;
145 for(;;) {
146 c = *str;
147 if (c == '\0')
148 break;
149 cstr_ccat(cstr, c);
150 str++;
154 /* add a wide char */
155 ST_FUNC void cstr_wccat(CString *cstr, int ch)
157 int size;
158 size = cstr->size + sizeof(nwchar_t);
159 if (size > cstr->size_allocated)
160 cstr_realloc(cstr, size);
161 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
162 cstr->size = size;
165 ST_FUNC void cstr_new(CString *cstr)
167 memset(cstr, 0, sizeof(CString));
170 /* free string and reset it to NULL */
171 ST_FUNC void cstr_free(CString *cstr)
173 tcc_free(cstr->data_allocated);
174 cstr_new(cstr);
177 /* reset string to empty */
178 ST_FUNC void cstr_reset(CString *cstr)
180 cstr->size = 0;
183 /* XXX: unicode ? */
184 static void add_char(CString *cstr, int c)
186 if (c == '\'' || c == '\"' || c == '\\') {
187 /* XXX: could be more precise if char or string */
188 cstr_ccat(cstr, '\\');
190 if (c >= 32 && c <= 126) {
191 cstr_ccat(cstr, c);
192 } else {
193 cstr_ccat(cstr, '\\');
194 if (c == '\n') {
195 cstr_ccat(cstr, 'n');
196 } else {
197 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
198 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
199 cstr_ccat(cstr, '0' + (c & 7));
204 /* ------------------------------------------------------------------------- */
205 /* allocate a new token */
206 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
208 TokenSym *ts, **ptable;
209 int i;
211 if (tok_ident >= SYM_FIRST_ANOM)
212 tcc_error("memory full (symbols)");
214 /* expand token table if needed */
215 i = tok_ident - TOK_IDENT;
216 if ((i % TOK_ALLOC_INCR) == 0) {
217 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
218 table_ident = ptable;
221 ts = tcc_malloc(sizeof(TokenSym) + len);
222 table_ident[i] = ts;
223 ts->tok = tok_ident++;
224 ts->sym_define = NULL;
225 ts->sym_label = NULL;
226 ts->sym_struct = NULL;
227 ts->sym_identifier = NULL;
228 ts->len = len;
229 ts->hash_next = NULL;
230 memcpy(ts->str, str, len);
231 ts->str[len] = '\0';
232 *pts = ts;
233 return ts;
236 #define TOK_HASH_INIT 1
237 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
239 /* find a token and add it if not found */
240 ST_FUNC TokenSym *tok_alloc(const char *str, int len)
242 TokenSym *ts, **pts;
243 int i;
244 unsigned int h;
246 h = TOK_HASH_INIT;
247 for(i=0;i<len;i++)
248 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
249 h &= (TOK_HASH_SIZE - 1);
251 pts = &hash_ident[h];
252 for(;;) {
253 ts = *pts;
254 if (!ts)
255 break;
256 if (ts->len == len && !memcmp(ts->str, str, len))
257 return ts;
258 pts = &(ts->hash_next);
260 return tok_alloc_new(pts, str, len);
263 /* XXX: buffer overflow */
264 /* XXX: float tokens */
265 ST_FUNC char *get_tok_str(int v, CValue *cv)
267 static char buf[STRING_MAX_SIZE + 1];
268 static CString cstr_buf;
269 CString *cstr;
270 char *p;
271 int i, len;
273 /* NOTE: to go faster, we give a fixed buffer for small strings */
274 cstr_reset(&cstr_buf);
275 cstr_buf.data = buf;
276 cstr_buf.size_allocated = sizeof(buf);
277 p = buf;
279 /* just an explanation, should never happen:
280 if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL)
281 tcc_error("internal error: get_tok_str"); */
283 switch(v) {
284 case TOK_CINT:
285 case TOK_CUINT:
286 /* XXX: not quite exact, but only useful for testing */
287 sprintf(p, "%u", cv->ui);
288 break;
289 case TOK_CLLONG:
290 case TOK_CULLONG:
291 /* XXX: not quite exact, but only useful for testing */
292 #ifdef _WIN32
293 sprintf(p, "%u", (unsigned)cv->ull);
294 #else
295 sprintf(p, "%llu", cv->ull);
296 #endif
297 break;
298 case TOK_LCHAR:
299 cstr_ccat(&cstr_buf, 'L');
300 case TOK_CCHAR:
301 cstr_ccat(&cstr_buf, '\'');
302 add_char(&cstr_buf, cv->i);
303 cstr_ccat(&cstr_buf, '\'');
304 cstr_ccat(&cstr_buf, '\0');
305 break;
306 case TOK_PPNUM:
307 cstr = cv->cstr;
308 len = cstr->size - 1;
309 for(i=0;i<len;i++)
310 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
311 cstr_ccat(&cstr_buf, '\0');
312 break;
313 case TOK_LSTR:
314 cstr_ccat(&cstr_buf, 'L');
315 case TOK_STR:
316 cstr = cv->cstr;
317 cstr_ccat(&cstr_buf, '\"');
318 if (v == TOK_STR) {
319 len = cstr->size - 1;
320 for(i=0;i<len;i++)
321 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
322 } else {
323 len = (cstr->size / sizeof(nwchar_t)) - 1;
324 for(i=0;i<len;i++)
325 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
327 cstr_ccat(&cstr_buf, '\"');
328 cstr_ccat(&cstr_buf, '\0');
329 break;
331 case TOK_CFLOAT:
332 case TOK_CDOUBLE:
333 case TOK_CLDOUBLE:
334 case TOK_LINENUM:
335 return NULL; /* should not happen */
337 /* above tokens have value, the ones below don't */
339 case TOK_LT:
340 v = '<';
341 goto addv;
342 case TOK_GT:
343 v = '>';
344 goto addv;
345 case TOK_DOTS:
346 return strcpy(p, "...");
347 case TOK_A_SHL:
348 return strcpy(p, "<<=");
349 case TOK_A_SAR:
350 return strcpy(p, ">>=");
351 default:
352 if (v < TOK_IDENT) {
353 /* search in two bytes table */
354 const unsigned char *q = tok_two_chars;
355 while (*q) {
356 if (q[2] == v) {
357 *p++ = q[0];
358 *p++ = q[1];
359 *p = '\0';
360 return buf;
362 q += 3;
364 addv:
365 *p++ = v;
366 *p = '\0';
367 } else if (v < tok_ident) {
368 return table_ident[v - TOK_IDENT]->str;
369 } else if (v >= SYM_FIRST_ANOM) {
370 /* special name for anonymous symbol */
371 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
372 } else {
373 /* should never happen */
374 return NULL;
376 break;
378 return cstr_buf.data;
381 /* fill input buffer and peek next char */
382 static int tcc_peekc_slow(BufferedFile *bf)
384 int len;
385 /* only tries to read if really end of buffer */
386 if (bf->buf_ptr >= bf->buf_end) {
387 if (bf->fd != -1) {
388 #if defined(PARSE_DEBUG)
389 len = 8;
390 #else
391 len = IO_BUF_SIZE;
392 #endif
393 len = read(bf->fd, bf->buffer, len);
394 if (len < 0)
395 len = 0;
396 } else {
397 len = 0;
399 total_bytes += len;
400 bf->buf_ptr = bf->buffer;
401 bf->buf_end = bf->buffer + len;
402 *bf->buf_end = CH_EOB;
404 if (bf->buf_ptr < bf->buf_end) {
405 return bf->buf_ptr[0];
406 } else {
407 bf->buf_ptr = bf->buf_end;
408 return CH_EOF;
412 /* return the current character, handling end of block if necessary
413 (but not stray) */
414 ST_FUNC int handle_eob(void)
416 return tcc_peekc_slow(file);
419 /* read next char from current input file and handle end of input buffer */
420 ST_INLN void inp(void)
422 ch = *(++(file->buf_ptr));
423 /* end of buffer/file handling */
424 if (ch == CH_EOB)
425 ch = handle_eob();
428 /* handle '\[\r]\n' */
429 static int handle_stray_noerror(void)
431 while (ch == '\\') {
432 inp();
433 if (ch == '\n') {
434 file->line_num++;
435 inp();
436 } else if (ch == '\r') {
437 inp();
438 if (ch != '\n')
439 goto fail;
440 file->line_num++;
441 inp();
442 } else {
443 fail:
444 return 1;
447 return 0;
450 static void handle_stray(void)
452 if (handle_stray_noerror())
453 tcc_error("stray '\\' in program");
456 /* skip the stray and handle the \\n case. Output an error if
457 incorrect char after the stray */
458 static int handle_stray1(uint8_t *p)
460 int c;
462 if (p >= file->buf_end) {
463 file->buf_ptr = p;
464 c = handle_eob();
465 p = file->buf_ptr;
466 if (c == '\\')
467 goto parse_stray;
468 } else {
469 parse_stray:
470 file->buf_ptr = p;
471 ch = *p;
472 handle_stray();
473 p = file->buf_ptr;
474 c = *p;
476 return c;
479 /* handle just the EOB case, but not stray */
480 #define PEEKC_EOB(c, p)\
482 p++;\
483 c = *p;\
484 if (c == '\\') {\
485 file->buf_ptr = p;\
486 c = handle_eob();\
487 p = file->buf_ptr;\
491 /* handle the complicated stray case */
492 #define PEEKC(c, p)\
494 p++;\
495 c = *p;\
496 if (c == '\\') {\
497 c = handle_stray1(p);\
498 p = file->buf_ptr;\
502 /* input with '\[\r]\n' handling. Note that this function cannot
503 handle other characters after '\', so you cannot call it inside
504 strings or comments */
505 ST_FUNC void minp(void)
507 inp();
508 if (ch == '\\')
509 handle_stray();
513 /* single line C++ comments */
514 static uint8_t *parse_line_comment(uint8_t *p)
516 int c;
518 p++;
519 for(;;) {
520 c = *p;
521 redo:
522 if (c == '\n' || c == CH_EOF) {
523 break;
524 } else if (c == '\\') {
525 file->buf_ptr = p;
526 c = handle_eob();
527 p = file->buf_ptr;
528 if (c == '\\') {
529 PEEKC_EOB(c, p);
530 if (c == '\n') {
531 file->line_num++;
532 PEEKC_EOB(c, p);
533 } else if (c == '\r') {
534 PEEKC_EOB(c, p);
535 if (c == '\n') {
536 file->line_num++;
537 PEEKC_EOB(c, p);
540 } else {
541 goto redo;
543 } else {
544 p++;
547 return p;
550 /* C comments */
551 ST_FUNC uint8_t *parse_comment(uint8_t *p)
553 int c;
555 p++;
556 for(;;) {
557 /* fast skip loop */
558 for(;;) {
559 c = *p;
560 if (c == '\n' || c == '*' || c == '\\')
561 break;
562 p++;
563 c = *p;
564 if (c == '\n' || c == '*' || c == '\\')
565 break;
566 p++;
568 /* now we can handle all the cases */
569 if (c == '\n') {
570 file->line_num++;
571 p++;
572 } else if (c == '*') {
573 p++;
574 for(;;) {
575 c = *p;
576 if (c == '*') {
577 p++;
578 } else if (c == '/') {
579 goto end_of_comment;
580 } else if (c == '\\') {
581 file->buf_ptr = p;
582 c = handle_eob();
583 p = file->buf_ptr;
584 if (c == '\\') {
585 /* skip '\[\r]\n', otherwise just skip the stray */
586 while (c == '\\') {
587 PEEKC_EOB(c, p);
588 if (c == '\n') {
589 file->line_num++;
590 PEEKC_EOB(c, p);
591 } else if (c == '\r') {
592 PEEKC_EOB(c, p);
593 if (c == '\n') {
594 file->line_num++;
595 PEEKC_EOB(c, p);
597 } else {
598 goto after_star;
602 } else {
603 break;
606 after_star: ;
607 } else {
608 /* stray, eob or eof */
609 file->buf_ptr = p;
610 c = handle_eob();
611 p = file->buf_ptr;
612 if (c == CH_EOF) {
613 tcc_error("unexpected end of file in comment");
614 } else if (c == '\\') {
615 p++;
619 end_of_comment:
620 p++;
621 return p;
624 #define cinp minp
626 static inline void skip_spaces(void)
628 while (is_space(ch))
629 cinp();
632 static inline int check_space(int t, int *spc)
634 if (is_space(t)) {
635 if (*spc)
636 return 1;
637 *spc = 1;
638 } else
639 *spc = 0;
640 return 0;
643 /* parse a string without interpreting escapes */
644 static uint8_t *parse_pp_string(uint8_t *p,
645 int sep, CString *str)
647 int c;
648 p++;
649 for(;;) {
650 c = *p;
651 if (c == sep) {
652 break;
653 } else if (c == '\\') {
654 file->buf_ptr = p;
655 c = handle_eob();
656 p = file->buf_ptr;
657 if (c == CH_EOF) {
658 unterminated_string:
659 /* XXX: indicate line number of start of string */
660 tcc_error("missing terminating %c character", sep);
661 } else if (c == '\\') {
662 /* escape : just skip \[\r]\n */
663 PEEKC_EOB(c, p);
664 if (c == '\n') {
665 file->line_num++;
666 p++;
667 } else if (c == '\r') {
668 PEEKC_EOB(c, p);
669 if (c != '\n')
670 expect("'\n' after '\r'");
671 file->line_num++;
672 p++;
673 } else if (c == CH_EOF) {
674 goto unterminated_string;
675 } else {
676 if (str) {
677 cstr_ccat(str, '\\');
678 cstr_ccat(str, c);
680 p++;
683 } else if (c == '\n') {
684 file->line_num++;
685 goto add_char;
686 } else if (c == '\r') {
687 PEEKC_EOB(c, p);
688 if (c != '\n') {
689 if (str)
690 cstr_ccat(str, '\r');
691 } else {
692 file->line_num++;
693 goto add_char;
695 } else {
696 add_char:
697 if (str)
698 cstr_ccat(str, c);
699 p++;
702 p++;
703 return p;
706 /* skip block of text until #else, #elif or #endif. skip also pairs of
707 #if/#endif */
708 static void preprocess_skip(void)
710 int a, start_of_line, c, in_warn_or_error;
711 uint8_t *p;
713 p = file->buf_ptr;
714 a = 0;
715 redo_start:
716 start_of_line = 1;
717 in_warn_or_error = 0;
718 for(;;) {
719 redo_no_start:
720 c = *p;
721 switch(c) {
722 case ' ':
723 case '\t':
724 case '\f':
725 case '\v':
726 case '\r':
727 p++;
728 goto redo_no_start;
729 case '\n':
730 file->line_num++;
731 p++;
732 goto redo_start;
733 case '\\':
734 file->buf_ptr = p;
735 c = handle_eob();
736 if (c == CH_EOF) {
737 expect("#endif");
738 } else if (c == '\\') {
739 ch = file->buf_ptr[0];
740 handle_stray_noerror();
742 p = file->buf_ptr;
743 goto redo_no_start;
744 /* skip strings */
745 case '\"':
746 case '\'':
747 if (in_warn_or_error)
748 goto _default;
749 p = parse_pp_string(p, c, NULL);
750 break;
751 /* skip comments */
752 case '/':
753 if (in_warn_or_error)
754 goto _default;
755 file->buf_ptr = p;
756 ch = *p;
757 minp();
758 p = file->buf_ptr;
759 if (ch == '*') {
760 p = parse_comment(p);
761 } else if (ch == '/') {
762 p = parse_line_comment(p);
764 break;
765 case '#':
766 p++;
767 if (start_of_line) {
768 file->buf_ptr = p;
769 next_nomacro();
770 p = file->buf_ptr;
771 if (a == 0 &&
772 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
773 goto the_end;
774 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
775 a++;
776 else if (tok == TOK_ENDIF)
777 a--;
778 else if( tok == TOK_ERROR || tok == TOK_WARNING)
779 in_warn_or_error = 1;
780 else if (tok == TOK_LINEFEED)
781 goto redo_start;
782 else if (parse_flags & PARSE_FLAG_ASM_COMMENTS)
783 p = parse_line_comment(p);
785 break;
786 _default:
787 default:
788 p++;
789 break;
791 start_of_line = 0;
793 the_end: ;
794 file->buf_ptr = p;
797 /* ParseState handling */
799 /* XXX: currently, no include file info is stored. Thus, we cannot display
800 accurate messages if the function or data definition spans multiple
801 files */
803 /* save current parse state in 's' */
804 ST_FUNC void save_parse_state(ParseState *s)
806 s->line_num = file->line_num;
807 s->macro_ptr = macro_ptr;
808 s->tok = tok;
809 s->tokc = tokc;
812 /* restore parse state from 's' */
813 ST_FUNC void restore_parse_state(ParseState *s)
815 file->line_num = s->line_num;
816 macro_ptr = s->macro_ptr;
817 tok = s->tok;
818 tokc = s->tokc;
821 /* return the number of additional 'ints' necessary to store the
822 token */
823 static inline int tok_ext_size(int t)
825 switch(t) {
826 /* 4 bytes */
827 case TOK_CINT:
828 case TOK_CUINT:
829 case TOK_CCHAR:
830 case TOK_LCHAR:
831 case TOK_CFLOAT:
832 case TOK_LINENUM:
833 return 1;
834 case TOK_STR:
835 case TOK_LSTR:
836 case TOK_PPNUM:
837 tcc_error("unsupported token");
838 return 1;
839 case TOK_CDOUBLE:
840 case TOK_CLLONG:
841 case TOK_CULLONG:
842 return 2;
843 case TOK_CLDOUBLE:
844 return LDOUBLE_SIZE / 4;
845 default:
846 return 0;
850 /* token string handling */
852 ST_INLN void tok_str_new(TokenString *s)
854 s->str = NULL;
855 s->len = 0;
856 s->allocated_len = 0;
857 s->last_line_num = -1;
860 ST_FUNC void tok_str_free(int *str)
862 tcc_free(str);
865 static int *tok_str_realloc(TokenString *s)
867 int *str, len;
869 if (s->allocated_len == 0) {
870 len = 8;
871 } else {
872 len = s->allocated_len * 2;
874 str = tcc_realloc(s->str, len * sizeof(int));
875 s->allocated_len = len;
876 s->str = str;
877 return str;
880 ST_FUNC void tok_str_add(TokenString *s, int t)
882 int len, *str;
884 len = s->len;
885 str = s->str;
886 if (len >= s->allocated_len)
887 str = tok_str_realloc(s);
888 str[len++] = t;
889 s->len = len;
892 static void tok_str_add2(TokenString *s, int t, CValue *cv)
894 int len, *str;
896 len = s->len;
897 str = s->str;
899 /* allocate space for worst case */
900 if (len + TOK_MAX_SIZE > s->allocated_len)
901 str = tok_str_realloc(s);
902 str[len++] = t;
903 switch(t) {
904 case TOK_CINT:
905 case TOK_CUINT:
906 case TOK_CCHAR:
907 case TOK_LCHAR:
908 case TOK_CFLOAT:
909 case TOK_LINENUM:
910 str[len++] = cv->tab[0];
911 break;
912 case TOK_PPNUM:
913 case TOK_STR:
914 case TOK_LSTR:
916 int nb_words;
917 CString *cstr;
919 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
920 while ((len + nb_words) > s->allocated_len)
921 str = tok_str_realloc(s);
922 cstr = (CString *)(str + len);
923 cstr->data = NULL;
924 cstr->size = cv->cstr->size;
925 cstr->data_allocated = NULL;
926 cstr->size_allocated = cstr->size;
927 memcpy((char *)cstr + sizeof(CString),
928 cv->cstr->data, cstr->size);
929 len += nb_words;
931 break;
932 case TOK_CDOUBLE:
933 case TOK_CLLONG:
934 case TOK_CULLONG:
935 #if LDOUBLE_SIZE == 8
936 case TOK_CLDOUBLE:
937 #endif
938 str[len++] = cv->tab[0];
939 str[len++] = cv->tab[1];
940 break;
941 #if LDOUBLE_SIZE == 12
942 case TOK_CLDOUBLE:
943 str[len++] = cv->tab[0];
944 str[len++] = cv->tab[1];
945 str[len++] = cv->tab[2];
946 #elif LDOUBLE_SIZE == 16
947 case TOK_CLDOUBLE:
948 str[len++] = cv->tab[0];
949 str[len++] = cv->tab[1];
950 str[len++] = cv->tab[2];
951 str[len++] = cv->tab[3];
952 #elif LDOUBLE_SIZE != 8
953 #error add long double size support
954 #endif
955 break;
956 default:
957 break;
959 s->len = len;
962 /* add the current parse token in token string 's' */
963 ST_FUNC void tok_str_add_tok(TokenString *s)
965 CValue cval;
967 /* save line number info */
968 if (file->line_num != s->last_line_num) {
969 s->last_line_num = file->line_num;
970 cval.i = s->last_line_num;
971 tok_str_add2(s, TOK_LINENUM, &cval);
973 tok_str_add2(s, tok, &tokc);
976 /* get a token from an integer array and increment pointer
977 accordingly. we code it as a macro to avoid pointer aliasing. */
978 static inline void TOK_GET(int *t, const int **pp, CValue *cv)
980 const int *p = *pp;
981 int n, *tab;
983 tab = cv->tab;
984 switch(*t = *p++) {
985 case TOK_CINT:
986 case TOK_CUINT:
987 case TOK_CCHAR:
988 case TOK_LCHAR:
989 case TOK_CFLOAT:
990 case TOK_LINENUM:
991 tab[0] = *p++;
992 break;
993 case TOK_STR:
994 case TOK_LSTR:
995 case TOK_PPNUM:
996 cv->cstr = (CString *)p;
997 cv->cstr->data = (char *)p + sizeof(CString);
998 p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
999 break;
1000 case TOK_CDOUBLE:
1001 case TOK_CLLONG:
1002 case TOK_CULLONG:
1003 n = 2;
1004 goto copy;
1005 case TOK_CLDOUBLE:
1006 #if LDOUBLE_SIZE == 16
1007 n = 4;
1008 #elif LDOUBLE_SIZE == 12
1009 n = 3;
1010 #elif LDOUBLE_SIZE == 8
1011 n = 2;
1012 #else
1013 # error add long double size support
1014 #endif
1015 copy:
1017 *tab++ = *p++;
1018 while (--n);
1019 break;
1020 default:
1021 break;
1023 *pp = p;
1026 static int macro_is_equal(const int *a, const int *b)
1028 char buf[STRING_MAX_SIZE + 1];
1029 CValue cv;
1030 int t;
1031 while (*a && *b) {
1032 TOK_GET(&t, &a, &cv);
1033 pstrcpy(buf, sizeof buf, get_tok_str(t, &cv));
1034 TOK_GET(&t, &b, &cv);
1035 if (strcmp(buf, get_tok_str(t, &cv)))
1036 return 0;
1038 return !(*a || *b);
1041 static void define_print(Sym *s, int is_undef)
1043 int c, t;
1044 CValue cval;
1045 const int *str;
1046 Sym *arg;
1048 if (tcc_state->dflag == 0 || !s || !tcc_state->ppfp)
1049 return;
1051 if (file) {
1052 c = file->line_num - file->line_ref - 1;
1053 if (c > 0) {
1054 while (c--)
1055 fputs("\n", tcc_state->ppfp);
1056 file->line_ref = file->line_num;
1060 if (is_undef) {
1061 fprintf(tcc_state->ppfp, "// #undef %s\n", get_tok_str(s->v, NULL));
1062 return;
1065 fprintf(tcc_state->ppfp, "// #define %s", get_tok_str(s->v, NULL));
1066 arg = s->next;
1067 if (arg) {
1068 char *sep = "(";
1069 while (arg) {
1070 fprintf(tcc_state->ppfp, "%s%s", sep, get_tok_str(arg->v & ~SYM_FIELD, NULL));
1071 sep = ",";
1072 arg = arg->next;
1074 fprintf(tcc_state->ppfp, ")");
1077 str = s->d;
1078 if (str)
1079 fprintf(tcc_state->ppfp, " ");
1081 while (str) {
1082 TOK_GET(&t, &str, &cval);
1083 if (!t)
1084 break;
1085 fprintf(tcc_state->ppfp, "%s", get_tok_str(t, &cval));
1087 fprintf(tcc_state->ppfp, "\n");
1090 /* defines handling */
1091 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
1093 Sym *s;
1095 s = define_find(v);
1096 if (s && !macro_is_equal(s->d, str))
1097 tcc_warning("%s redefined", get_tok_str(v, NULL));
1099 s = sym_push2(&define_stack, v, macro_type, 0);
1100 s->d = str;
1101 s->next = first_arg;
1102 table_ident[v - TOK_IDENT]->sym_define = s;
1103 define_print(s, 0);
1106 /* undefined a define symbol. Its name is just set to zero */
1107 ST_FUNC void define_undef(Sym *s)
1109 int v;
1110 v = s->v;
1111 if (v >= TOK_IDENT && v < tok_ident) {
1112 define_print(s, 1);
1113 table_ident[v - TOK_IDENT]->sym_define = NULL;
1115 s->v = 0;
1118 ST_INLN Sym *define_find(int v)
1120 v -= TOK_IDENT;
1121 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1122 return NULL;
1123 return table_ident[v]->sym_define;
1126 /* free define stack until top reaches 'b' */
1127 ST_FUNC void free_defines(Sym *b)
1129 Sym *top, *top1;
1130 int v;
1132 top = define_stack;
1133 while (top != b) {
1134 top1 = top->prev;
1135 /* do not free args or predefined defines */
1136 if (top->d)
1137 tok_str_free(top->d);
1138 v = top->v;
1139 if (v >= TOK_IDENT && v < tok_ident)
1140 table_ident[v - TOK_IDENT]->sym_define = NULL;
1141 sym_free(top);
1142 top = top1;
1144 define_stack = b;
1147 ST_FUNC void print_defines(void)
1149 Sym *top, *s;
1150 int v;
1152 top = define_stack;
1153 while (top) {
1154 v = top->v;
1155 if (v >= TOK_IDENT && v < tok_ident) {
1156 s = table_ident[v - TOK_IDENT]->sym_define;
1157 define_print(s, 0);
1159 top = top->prev;
1163 /* label lookup */
1164 ST_FUNC Sym *label_find(int v)
1166 v -= TOK_IDENT;
1167 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1168 return NULL;
1169 return table_ident[v]->sym_label;
1172 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
1174 Sym *s, **ps;
1175 s = sym_push2(ptop, v, 0, 0);
1176 s->r = flags;
1177 ps = &table_ident[v - TOK_IDENT]->sym_label;
1178 if (ptop == &global_label_stack) {
1179 /* modify the top most local identifier, so that
1180 sym_identifier will point to 's' when popped */
1181 while (*ps != NULL)
1182 ps = &(*ps)->prev_tok;
1184 s->prev_tok = *ps;
1185 *ps = s;
1186 return s;
1189 /* pop labels until element last is reached. Look if any labels are
1190 undefined. Define symbols if '&&label' was used. */
1191 ST_FUNC void label_pop(Sym **ptop, Sym *slast)
1193 Sym *s, *s1;
1194 for(s = *ptop; s != slast; s = s1) {
1195 s1 = s->prev;
1196 if (s->r == LABEL_DECLARED) {
1197 tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
1198 } else if (s->r == LABEL_FORWARD) {
1199 tcc_error("label '%s' used but not defined",
1200 get_tok_str(s->v, NULL));
1201 } else {
1202 if (s->c) {
1203 /* define corresponding symbol. A size of
1204 1 is put. */
1205 put_extern_sym(s, cur_text_section, s->jnext, 1);
1208 /* remove label */
1209 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
1210 sym_free(s);
1212 *ptop = slast;
1215 /* eval an expression for #if/#elif */
1216 static int expr_preprocess(void)
1218 int c, t;
1219 TokenString str;
1221 tok_str_new(&str);
1222 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1223 next(); /* do macro subst */
1224 if (tok == TOK_DEFINED) {
1225 next_nomacro();
1226 t = tok;
1227 if (t == '(')
1228 next_nomacro();
1229 c = define_find(tok) != 0;
1230 if (t == '(')
1231 next_nomacro();
1232 tok = TOK_CINT;
1233 tokc.i = c;
1234 } else if (tok >= TOK_IDENT) {
1235 /* if undefined macro */
1236 tok = TOK_CINT;
1237 tokc.i = 0;
1239 tok_str_add_tok(&str);
1241 tok_str_add(&str, -1); /* simulate end of file */
1242 tok_str_add(&str, 0);
1243 /* now evaluate C constant expression */
1244 macro_ptr = str.str;
1245 next();
1246 c = expr_const();
1247 macro_ptr = NULL;
1248 tok_str_free(str.str);
1249 return c != 0;
1252 /* parse after #define */
1253 ST_FUNC void parse_define(void)
1255 Sym *s, *first, **ps;
1256 int v, t, varg, is_vaargs, spc, ptok, macro_list_start;
1257 TokenString str;
1259 v = tok;
1260 if (v < TOK_IDENT)
1261 tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
1262 /* XXX: should check if same macro (ANSI) */
1263 first = NULL;
1264 t = MACRO_OBJ;
1265 /* '(' must be just after macro definition for MACRO_FUNC */
1266 next_nomacro_spc();
1267 if (tok == '(') {
1268 next_nomacro();
1269 ps = &first;
1270 while (tok != ')') {
1271 varg = tok;
1272 next_nomacro();
1273 is_vaargs = 0;
1274 if (varg == TOK_DOTS) {
1275 varg = TOK___VA_ARGS__;
1276 is_vaargs = 1;
1277 } else if (tok == TOK_DOTS && gnu_ext) {
1278 is_vaargs = 1;
1279 next_nomacro();
1281 if (varg < TOK_IDENT)
1282 tcc_error( "\'%s\' may not appear in parameter list", get_tok_str(varg, NULL));
1283 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1284 *ps = s;
1285 ps = &s->next;
1286 if (tok != ',')
1287 continue;
1288 next_nomacro();
1290 next_nomacro_spc();
1291 t = MACRO_FUNC;
1293 tok_str_new(&str);
1294 spc = 2;
1295 /* EOF testing necessary for '-D' handling */
1296 ptok = 0;
1297 macro_list_start = 1;
1298 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
1299 if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS)
1300 tcc_error("'##' invalid at start of macro");
1301 ptok = tok;
1302 /* remove spaces around ## and after '#' */
1303 if (TOK_TWOSHARPS == tok) {
1304 if (1 == spc)
1305 --str.len;
1306 spc = 2;
1307 } else if ('#' == tok) {
1308 spc = 2;
1309 } else if (check_space(tok, &spc)) {
1310 goto skip;
1312 tok_str_add2(&str, tok, &tokc);
1313 skip:
1314 next_nomacro_spc();
1315 macro_list_start = 0;
1317 if (ptok == TOK_TWOSHARPS)
1318 tcc_error("'##' invalid at end of macro");
1319 if (spc == 1)
1320 --str.len; /* remove trailing space */
1321 tok_str_add(&str, 0);
1322 define_push(v, t, str.str, first);
1325 static inline int hash_cached_include(const char *filename)
1327 const unsigned char *s;
1328 unsigned int h;
1330 h = TOK_HASH_INIT;
1331 s = (unsigned char *) filename;
1332 while (*s) {
1333 h = TOK_HASH_FUNC(h, *s);
1334 s++;
1336 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
1337 return h;
1340 static CachedInclude *search_cached_include(TCCState *s1, const char *filename)
1342 CachedInclude *e;
1343 int i, h;
1344 h = hash_cached_include(filename);
1345 i = s1->cached_includes_hash[h];
1346 for(;;) {
1347 if (i == 0)
1348 break;
1349 e = s1->cached_includes[i - 1];
1350 if (0 == PATHCMP(e->filename, filename))
1351 return e;
1352 i = e->hash_next;
1354 return NULL;
1357 static inline void add_cached_include(TCCState *s1, const char *filename, int ifndef_macro)
1359 CachedInclude *e;
1360 int h;
1362 if (search_cached_include(s1, filename))
1363 return;
1364 #ifdef INC_DEBUG
1365 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
1366 #endif
1367 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
1368 strcpy(e->filename, filename);
1369 e->ifndef_macro = ifndef_macro;
1370 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
1371 /* add in hash table */
1372 h = hash_cached_include(filename);
1373 e->hash_next = s1->cached_includes_hash[h];
1374 s1->cached_includes_hash[h] = s1->nb_cached_includes;
1377 static void pragma_parse(TCCState *s1)
1379 int val;
1381 next();
1382 if (tok == TOK_pack) {
1384 This may be:
1385 #pragma pack(1) // set
1386 #pragma pack() // reset to default
1387 #pragma pack(push,1) // push & set
1388 #pragma pack(pop) // restore previous
1390 next();
1391 skip('(');
1392 if (tok == TOK_ASM_pop) {
1393 next();
1394 if (s1->pack_stack_ptr <= s1->pack_stack) {
1395 stk_error:
1396 tcc_error("out of pack stack");
1398 s1->pack_stack_ptr--;
1399 } else {
1400 val = 0;
1401 if (tok != ')') {
1402 if (tok == TOK_ASM_push) {
1403 next();
1404 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
1405 goto stk_error;
1406 s1->pack_stack_ptr++;
1407 skip(',');
1409 if (tok != TOK_CINT) {
1410 pack_error:
1411 tcc_error("invalid pack pragma");
1413 val = tokc.i;
1414 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
1415 goto pack_error;
1416 next();
1418 *s1->pack_stack_ptr = val;
1419 skip(')');
1424 /* is_bof is true if first non space token at beginning of file */
1425 ST_FUNC void preprocess(int is_bof)
1427 TCCState *s1 = tcc_state;
1428 int i, c, n, saved_parse_flags;
1429 char buf[1024], *q;
1430 Sym *s;
1432 saved_parse_flags = parse_flags;
1433 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1434 PARSE_FLAG_LINEFEED;
1435 parse_flags |= (saved_parse_flags & PARSE_FLAG_ASM_COMMENTS);
1436 next_nomacro();
1437 redo:
1438 switch(tok) {
1439 case TOK_DEFINE:
1440 next_nomacro();
1441 parse_define();
1442 break;
1443 case TOK_UNDEF:
1444 next_nomacro();
1445 s = define_find(tok);
1446 /* undefine symbol by putting an invalid name */
1447 if (s)
1448 define_undef(s);
1449 break;
1450 case TOK_INCLUDE:
1451 case TOK_INCLUDE_NEXT:
1452 ch = file->buf_ptr[0];
1453 /* XXX: incorrect if comments : use next_nomacro with a special mode */
1454 skip_spaces();
1455 if (ch == '<') {
1456 c = '>';
1457 goto read_name;
1458 } else if (ch == '\"') {
1459 c = ch;
1460 read_name:
1461 inp();
1462 q = buf;
1463 while (ch != c && ch != '\n' && ch != CH_EOF) {
1464 if ((q - buf) < sizeof(buf) - 1)
1465 *q++ = ch;
1466 if (ch == '\\') {
1467 if (handle_stray_noerror() == 0)
1468 --q;
1469 } else
1470 inp();
1472 *q = '\0';
1473 minp();
1474 #if 0
1475 /* eat all spaces and comments after include */
1476 /* XXX: slightly incorrect */
1477 while (ch1 != '\n' && ch1 != CH_EOF)
1478 inp();
1479 #endif
1480 } else {
1481 /* computed #include : either we have only strings or
1482 we have anything enclosed in '<>' */
1483 next();
1484 buf[0] = '\0';
1485 if (tok == TOK_STR) {
1486 while (tok != TOK_LINEFEED) {
1487 if (tok != TOK_STR) {
1488 include_syntax:
1489 tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1491 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
1492 next();
1494 c = '\"';
1495 } else {
1496 int len;
1497 while (tok != TOK_LINEFEED) {
1498 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1499 next();
1501 len = strlen(buf);
1502 /* check syntax and remove '<>' */
1503 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1504 goto include_syntax;
1505 memmove(buf, buf + 1, len - 2);
1506 buf[len - 2] = '\0';
1507 c = '>';
1511 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1512 tcc_error("#include recursion too deep");
1513 /* store current file in stack, but increment stack later below */
1514 *s1->include_stack_ptr = file;
1516 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1517 for (i = -2; i < n; ++i) {
1518 char buf1[sizeof file->filename];
1519 CachedInclude *e;
1520 BufferedFile **f;
1521 const char *path;
1523 if (i == -2) {
1524 /* check absolute include path */
1525 if (!IS_ABSPATH(buf))
1526 continue;
1527 buf1[0] = 0;
1528 i = n; /* force end loop */
1530 } else if (i == -1) {
1531 /* search in current dir if "header.h" */
1532 if (c != '\"')
1533 continue;
1534 path = file->filename;
1535 pstrncpy(buf1, path, tcc_basename(path) - path);
1537 } else {
1538 /* search in all the include paths */
1539 if (i < s1->nb_include_paths)
1540 path = s1->include_paths[i];
1541 else
1542 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1543 pstrcpy(buf1, sizeof(buf1), path);
1544 pstrcat(buf1, sizeof(buf1), "/");
1547 pstrcat(buf1, sizeof(buf1), buf);
1549 if (tok == TOK_INCLUDE_NEXT)
1550 for (f = s1->include_stack_ptr; f >= s1->include_stack; --f)
1551 if (0 == PATHCMP((*f)->filename, buf1)) {
1552 #ifdef INC_DEBUG
1553 printf("%s: #include_next skipping %s\n", file->filename, buf1);
1554 #endif
1555 goto include_trynext;
1558 e = search_cached_include(s1, buf1);
1559 if (e && define_find(e->ifndef_macro)) {
1560 /* no need to parse the include because the 'ifndef macro'
1561 is defined */
1562 #ifdef INC_DEBUG
1563 printf("%s: skipping cached %s\n", file->filename, buf1);
1564 #endif
1565 goto include_done;
1568 if (tcc_open(s1, buf1) < 0)
1569 include_trynext:
1570 continue;
1572 #ifdef INC_DEBUG
1573 printf("%s: including %s\n", file->prev->filename, file->filename);
1574 #endif
1575 /* update target deps */
1576 dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1577 tcc_strdup(buf1));
1578 /* push current file in stack */
1579 ++s1->include_stack_ptr;
1580 /* add include file debug info */
1581 if (s1->do_debug)
1582 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1583 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
1584 ch = file->buf_ptr[0];
1585 goto the_end;
1587 tcc_error("include file '%s' not found", buf);
1588 include_done:
1589 break;
1590 case TOK_IFNDEF:
1591 c = 1;
1592 goto do_ifdef;
1593 case TOK_IF:
1594 c = expr_preprocess();
1595 goto do_if;
1596 case TOK_IFDEF:
1597 c = 0;
1598 do_ifdef:
1599 next_nomacro();
1600 if (tok < TOK_IDENT)
1601 tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
1602 if (is_bof) {
1603 if (c) {
1604 #ifdef INC_DEBUG
1605 printf("#ifndef %s\n", get_tok_str(tok, NULL));
1606 #endif
1607 file->ifndef_macro = tok;
1610 c = (define_find(tok) != 0) ^ c;
1611 do_if:
1612 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
1613 tcc_error("memory full (ifdef)");
1614 *s1->ifdef_stack_ptr++ = c;
1615 goto test_skip;
1616 case TOK_ELSE:
1617 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1618 tcc_error("#else without matching #if");
1619 if (s1->ifdef_stack_ptr[-1] & 2)
1620 tcc_error("#else after #else");
1621 c = (s1->ifdef_stack_ptr[-1] ^= 3);
1622 goto test_else;
1623 case TOK_ELIF:
1624 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
1625 tcc_error("#elif without matching #if");
1626 c = s1->ifdef_stack_ptr[-1];
1627 if (c > 1)
1628 tcc_error("#elif after #else");
1629 /* last #if/#elif expression was true: we skip */
1630 if (c == 1)
1631 goto skip;
1632 c = expr_preprocess();
1633 s1->ifdef_stack_ptr[-1] = c;
1634 test_else:
1635 if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
1636 file->ifndef_macro = 0;
1637 test_skip:
1638 if (!(c & 1)) {
1639 skip:
1640 preprocess_skip();
1641 is_bof = 0;
1642 goto redo;
1644 break;
1645 case TOK_ENDIF:
1646 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
1647 tcc_error("#endif without matching #if");
1648 s1->ifdef_stack_ptr--;
1649 /* '#ifndef macro' was at the start of file. Now we check if
1650 an '#endif' is exactly at the end of file */
1651 if (file->ifndef_macro &&
1652 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
1653 file->ifndef_macro_saved = file->ifndef_macro;
1654 /* need to set to zero to avoid false matches if another
1655 #ifndef at middle of file */
1656 file->ifndef_macro = 0;
1657 while (tok != TOK_LINEFEED)
1658 next_nomacro();
1659 tok_flags |= TOK_FLAG_ENDIF;
1660 goto the_end;
1662 break;
1663 case TOK_LINE:
1664 next();
1665 if (tok != TOK_CINT)
1666 tcc_error("A #line format is wrong");
1667 case TOK_PPNUM:
1668 if (tok != TOK_CINT) {
1669 char *p = tokc.cstr->data;
1670 tokc.i = strtoul(p, (char **)&p, 10);
1672 i = file->line_num;
1673 file->line_num = tokc.i - 1;
1674 next();
1675 if (tok != TOK_LINEFEED) {
1676 if (tok != TOK_STR) {
1677 if ((parse_flags & PARSE_FLAG_ASM_COMMENTS) == 0) {
1678 file->line_num = i;
1679 tcc_error("#line format is wrong");
1681 break;
1683 pstrcpy(file->filename, sizeof(file->filename),
1684 (char *)tokc.cstr->data);
1686 if (s1->do_debug)
1687 put_stabs(file->filename, N_BINCL, 0, 0, 0);
1688 break;
1689 case TOK_ERROR:
1690 case TOK_WARNING:
1691 c = tok;
1692 ch = file->buf_ptr[0];
1693 skip_spaces();
1694 q = buf;
1695 while (ch != '\n' && ch != CH_EOF) {
1696 if ((q - buf) < sizeof(buf) - 1)
1697 *q++ = ch;
1698 if (ch == '\\') {
1699 if (handle_stray_noerror() == 0)
1700 --q;
1701 } else
1702 inp();
1704 *q = '\0';
1705 if (c == TOK_ERROR)
1706 tcc_error("#error %s", buf);
1707 else
1708 tcc_warning("#warning %s", buf);
1709 break;
1710 case TOK_PRAGMA:
1711 pragma_parse(s1);
1712 break;
1713 default:
1714 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_PPNUM) {
1715 /* '!' is ignored to allow C scripts. numbers are ignored
1716 to emulate cpp behaviour */
1717 } else {
1718 if (!(parse_flags & PARSE_FLAG_ASM_COMMENTS))
1719 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
1720 else {
1721 /* this is a gas line comment in an 'S' file. */
1722 file->buf_ptr = parse_line_comment(file->buf_ptr);
1723 goto the_end;
1726 break;
1728 /* ignore other preprocess commands or #! for C scripts */
1729 while (tok != TOK_LINEFEED)
1730 next_nomacro();
1731 the_end:
1732 parse_flags = saved_parse_flags;
1735 /* evaluate escape codes in a string. */
1736 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
1738 int c, n;
1739 const uint8_t *p;
1741 p = buf;
1742 for(;;) {
1743 c = *p;
1744 if (c == '\0')
1745 break;
1746 if (c == '\\') {
1747 p++;
1748 /* escape */
1749 c = *p;
1750 switch(c) {
1751 case '0': case '1': case '2': case '3':
1752 case '4': case '5': case '6': case '7':
1753 /* at most three octal digits */
1754 n = c - '0';
1755 p++;
1756 c = *p;
1757 if (isoct(c)) {
1758 n = n * 8 + c - '0';
1759 p++;
1760 c = *p;
1761 if (isoct(c)) {
1762 n = n * 8 + c - '0';
1763 p++;
1766 c = n;
1767 goto add_char_nonext;
1768 case 'x':
1769 case 'u':
1770 case 'U':
1771 p++;
1772 n = 0;
1773 for(;;) {
1774 c = *p;
1775 if (c >= 'a' && c <= 'f')
1776 c = c - 'a' + 10;
1777 else if (c >= 'A' && c <= 'F')
1778 c = c - 'A' + 10;
1779 else if (isnum(c))
1780 c = c - '0';
1781 else
1782 break;
1783 n = n * 16 + c;
1784 p++;
1786 c = n;
1787 goto add_char_nonext;
1788 case 'a':
1789 c = '\a';
1790 break;
1791 case 'b':
1792 c = '\b';
1793 break;
1794 case 'f':
1795 c = '\f';
1796 break;
1797 case 'n':
1798 c = '\n';
1799 break;
1800 case 'r':
1801 c = '\r';
1802 break;
1803 case 't':
1804 c = '\t';
1805 break;
1806 case 'v':
1807 c = '\v';
1808 break;
1809 case 'e':
1810 if (!gnu_ext)
1811 goto invalid_escape;
1812 c = 27;
1813 break;
1814 case '\'':
1815 case '\"':
1816 case '\\':
1817 case '?':
1818 break;
1819 default:
1820 invalid_escape:
1821 if (c >= '!' && c <= '~')
1822 tcc_warning("unknown escape sequence: \'\\%c\'", c);
1823 else
1824 tcc_warning("unknown escape sequence: \'\\x%x\'", c);
1825 break;
1828 p++;
1829 add_char_nonext:
1830 if (!is_long)
1831 cstr_ccat(outstr, c);
1832 else
1833 cstr_wccat(outstr, c);
1835 /* add a trailing '\0' */
1836 if (!is_long)
1837 cstr_ccat(outstr, '\0');
1838 else
1839 cstr_wccat(outstr, '\0');
1842 /* we use 64 bit numbers */
1843 #define BN_SIZE 2
1845 /* bn = (bn << shift) | or_val */
1846 static void bn_lshift(unsigned int *bn, int shift, int or_val)
1848 int i;
1849 unsigned int v;
1850 for(i=0;i<BN_SIZE;i++) {
1851 v = bn[i];
1852 bn[i] = (v << shift) | or_val;
1853 or_val = v >> (32 - shift);
1857 static void bn_zero(unsigned int *bn)
1859 int i;
1860 for(i=0;i<BN_SIZE;i++) {
1861 bn[i] = 0;
1865 /* parse number in null terminated string 'p' and return it in the
1866 current token */
1867 static void parse_number(const char *p)
1869 int b, t, shift, frac_bits, s, exp_val, ch;
1870 char *q;
1871 unsigned int bn[BN_SIZE];
1872 double d;
1874 /* number */
1875 q = token_buf;
1876 ch = *p++;
1877 t = ch;
1878 ch = *p++;
1879 *q++ = t;
1880 b = 10;
1881 if (t == '.') {
1882 goto float_frac_parse;
1883 } else if (t == '0') {
1884 if (ch == 'x' || ch == 'X') {
1885 q--;
1886 ch = *p++;
1887 b = 16;
1888 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
1889 q--;
1890 ch = *p++;
1891 b = 2;
1894 /* parse all digits. cannot check octal numbers at this stage
1895 because of floating point constants */
1896 while (1) {
1897 if (ch >= 'a' && ch <= 'f')
1898 t = ch - 'a' + 10;
1899 else if (ch >= 'A' && ch <= 'F')
1900 t = ch - 'A' + 10;
1901 else if (isnum(ch))
1902 t = ch - '0';
1903 else
1904 break;
1905 if (t >= b)
1906 break;
1907 if (q >= token_buf + STRING_MAX_SIZE) {
1908 num_too_long:
1909 tcc_error("number too long");
1911 *q++ = ch;
1912 ch = *p++;
1914 if (ch == '.' ||
1915 ((ch == 'e' || ch == 'E') && b == 10) ||
1916 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
1917 if (b != 10) {
1918 /* NOTE: strtox should support that for hexa numbers, but
1919 non ISOC99 libcs do not support it, so we prefer to do
1920 it by hand */
1921 /* hexadecimal or binary floats */
1922 /* XXX: handle overflows */
1923 *q = '\0';
1924 if (b == 16)
1925 shift = 4;
1926 else
1927 shift = 1;
1928 bn_zero(bn);
1929 q = token_buf;
1930 while (1) {
1931 t = *q++;
1932 if (t == '\0') {
1933 break;
1934 } else if (t >= 'a') {
1935 t = t - 'a' + 10;
1936 } else if (t >= 'A') {
1937 t = t - 'A' + 10;
1938 } else {
1939 t = t - '0';
1941 bn_lshift(bn, shift, t);
1943 frac_bits = 0;
1944 if (ch == '.') {
1945 ch = *p++;
1946 while (1) {
1947 t = ch;
1948 if (t >= 'a' && t <= 'f') {
1949 t = t - 'a' + 10;
1950 } else if (t >= 'A' && t <= 'F') {
1951 t = t - 'A' + 10;
1952 } else if (t >= '0' && t <= '9') {
1953 t = t - '0';
1954 } else {
1955 break;
1957 if (t >= b)
1958 tcc_error("invalid digit");
1959 bn_lshift(bn, shift, t);
1960 frac_bits += shift;
1961 ch = *p++;
1964 if (ch != 'p' && ch != 'P')
1965 expect("exponent");
1966 ch = *p++;
1967 s = 1;
1968 exp_val = 0;
1969 if (ch == '+') {
1970 ch = *p++;
1971 } else if (ch == '-') {
1972 s = -1;
1973 ch = *p++;
1975 if (ch < '0' || ch > '9')
1976 expect("exponent digits");
1977 while (ch >= '0' && ch <= '9') {
1978 exp_val = exp_val * 10 + ch - '0';
1979 ch = *p++;
1981 exp_val = exp_val * s;
1983 /* now we can generate the number */
1984 /* XXX: should patch directly float number */
1985 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
1986 d = ldexp(d, exp_val - frac_bits);
1987 t = toup(ch);
1988 if (t == 'F') {
1989 ch = *p++;
1990 tok = TOK_CFLOAT;
1991 /* float : should handle overflow */
1992 tokc.f = (float)d;
1993 } else if (t == 'L') {
1994 ch = *p++;
1995 #ifdef TCC_TARGET_PE
1996 tok = TOK_CDOUBLE;
1997 tokc.d = d;
1998 #else
1999 tok = TOK_CLDOUBLE;
2000 /* XXX: not large enough */
2001 tokc.ld = (long double)d;
2002 #endif
2003 } else {
2004 tok = TOK_CDOUBLE;
2005 tokc.d = d;
2007 } else {
2008 /* decimal floats */
2009 if (ch == '.') {
2010 if (q >= token_buf + STRING_MAX_SIZE)
2011 goto num_too_long;
2012 *q++ = ch;
2013 ch = *p++;
2014 float_frac_parse:
2015 while (ch >= '0' && ch <= '9') {
2016 if (q >= token_buf + STRING_MAX_SIZE)
2017 goto num_too_long;
2018 *q++ = ch;
2019 ch = *p++;
2022 if (ch == 'e' || ch == 'E') {
2023 if (q >= token_buf + STRING_MAX_SIZE)
2024 goto num_too_long;
2025 *q++ = ch;
2026 ch = *p++;
2027 if (ch == '-' || ch == '+') {
2028 if (q >= token_buf + STRING_MAX_SIZE)
2029 goto num_too_long;
2030 *q++ = ch;
2031 ch = *p++;
2033 if (ch < '0' || ch > '9')
2034 expect("exponent digits");
2035 while (ch >= '0' && ch <= '9') {
2036 if (q >= token_buf + STRING_MAX_SIZE)
2037 goto num_too_long;
2038 *q++ = ch;
2039 ch = *p++;
2042 *q = '\0';
2043 t = toup(ch);
2044 errno = 0;
2045 if (t == 'F') {
2046 ch = *p++;
2047 tok = TOK_CFLOAT;
2048 tokc.f = strtof(token_buf, NULL);
2049 } else if (t == 'L') {
2050 ch = *p++;
2051 #ifdef TCC_TARGET_PE
2052 tok = TOK_CDOUBLE;
2053 tokc.d = strtod(token_buf, NULL);
2054 #else
2055 tok = TOK_CLDOUBLE;
2056 tokc.ld = strtold(token_buf, NULL);
2057 #endif
2058 } else {
2059 tok = TOK_CDOUBLE;
2060 tokc.d = strtod(token_buf, NULL);
2063 } else {
2064 unsigned long long n, n1;
2065 int lcount, ucount, must_64bit;
2066 const char *p1;
2068 /* integer number */
2069 *q = '\0';
2070 q = token_buf;
2071 if (b == 10 && *q == '0') {
2072 b = 8;
2073 q++;
2075 n = 0;
2076 while(1) {
2077 t = *q++;
2078 /* no need for checks except for base 10 / 8 errors */
2079 if (t == '\0')
2080 break;
2081 else if (t >= 'a')
2082 t = t - 'a' + 10;
2083 else if (t >= 'A')
2084 t = t - 'A' + 10;
2085 else
2086 t = t - '0';
2087 if (t >= b)
2088 tcc_error("invalid digit");
2089 n1 = n;
2090 n = n * b + t;
2091 /* detect overflow */
2092 /* XXX: this test is not reliable */
2093 if (n < n1)
2094 tcc_error("integer constant overflow");
2097 /* Determine the characteristics (unsigned and/or 64bit) the type of
2098 the constant must have according to the constant suffix(es) */
2099 lcount = ucount = must_64bit = 0;
2100 p1 = p;
2101 for(;;) {
2102 t = toup(ch);
2103 if (t == 'L') {
2104 if (lcount >= 2)
2105 tcc_error("three 'l's in integer constant");
2106 if (lcount && *(p - 1) != ch)
2107 tcc_error("incorrect integer suffix: %s", p1);
2108 lcount++;
2109 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2110 if (lcount == 2)
2111 #endif
2112 must_64bit = 1;
2113 ch = *p++;
2114 } else if (t == 'U') {
2115 if (ucount >= 1)
2116 tcc_error("two 'u's in integer constant");
2117 ucount++;
2118 ch = *p++;
2119 } else {
2120 break;
2124 /* Whether 64 bits are needed to hold the constant's value */
2125 if (n & 0xffffffff00000000LL || must_64bit) {
2126 tok = TOK_CLLONG;
2127 n1 = n >> 32;
2128 } else {
2129 tok = TOK_CINT;
2130 n1 = n;
2133 /* Whether type must be unsigned to hold the constant's value */
2134 if (ucount || ((n1 >> 31) && (b != 10))) {
2135 if (tok == TOK_CLLONG)
2136 tok = TOK_CULLONG;
2137 else
2138 tok = TOK_CUINT;
2139 /* If decimal and no unsigned suffix, bump to 64 bits or throw error */
2140 } else if (n1 >> 31) {
2141 if (tok == TOK_CINT)
2142 tok = TOK_CLLONG;
2143 else
2144 tcc_error("integer constant overflow");
2147 if (tok == TOK_CINT || tok == TOK_CUINT)
2148 tokc.ui = n;
2149 else
2150 tokc.ull = n;
2152 if (ch)
2153 tcc_error("invalid number\n");
2157 #define PARSE2(c1, tok1, c2, tok2) \
2158 case c1: \
2159 PEEKC(c, p); \
2160 if (c == c2) { \
2161 p++; \
2162 tok = tok2; \
2163 } else { \
2164 tok = tok1; \
2166 break;
2168 /* return next token without macro substitution */
2169 static inline void next_nomacro1(void)
2171 int t, c, is_long;
2172 TokenSym *ts;
2173 uint8_t *p, *p1;
2174 unsigned int h;
2176 p = file->buf_ptr;
2177 redo_no_start:
2178 c = *p;
2179 switch(c) {
2180 case ' ':
2181 case '\t':
2182 tok = c;
2183 p++;
2184 goto keep_tok_flags;
2185 case '\f':
2186 case '\v':
2187 case '\r':
2188 p++;
2189 goto redo_no_start;
2190 case '\\':
2191 /* first look if it is in fact an end of buffer */
2192 if (p >= file->buf_end) {
2193 file->buf_ptr = p;
2194 handle_eob();
2195 p = file->buf_ptr;
2196 if (p >= file->buf_end)
2197 goto parse_eof;
2198 else
2199 goto redo_no_start;
2200 } else {
2201 file->buf_ptr = p;
2202 ch = *p;
2203 handle_stray();
2204 p = file->buf_ptr;
2205 goto redo_no_start;
2207 parse_eof:
2209 TCCState *s1 = tcc_state;
2210 if ((parse_flags & PARSE_FLAG_LINEFEED)
2211 && !(tok_flags & TOK_FLAG_EOF)) {
2212 tok_flags |= TOK_FLAG_EOF;
2213 tok = TOK_LINEFEED;
2214 goto keep_tok_flags;
2215 } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
2216 tok = TOK_EOF;
2217 } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
2218 tcc_error("missing #endif");
2219 } else if (s1->include_stack_ptr == s1->include_stack) {
2220 /* no include left : end of file. */
2221 tok = TOK_EOF;
2222 } else {
2223 tok_flags &= ~TOK_FLAG_EOF;
2224 /* pop include file */
2226 /* test if previous '#endif' was after a #ifdef at
2227 start of file */
2228 if (tok_flags & TOK_FLAG_ENDIF) {
2229 #ifdef INC_DEBUG
2230 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
2231 #endif
2232 add_cached_include(s1, file->filename, file->ifndef_macro_saved);
2233 tok_flags &= ~TOK_FLAG_ENDIF;
2236 /* add end of include file debug info */
2237 if (tcc_state->do_debug) {
2238 put_stabd(N_EINCL, 0, 0);
2240 /* pop include stack */
2241 tcc_close();
2242 s1->include_stack_ptr--;
2243 p = file->buf_ptr;
2244 goto redo_no_start;
2247 break;
2249 case '\n':
2250 file->line_num++;
2251 tok_flags |= TOK_FLAG_BOL;
2252 p++;
2253 maybe_newline:
2254 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
2255 goto redo_no_start;
2256 tok = TOK_LINEFEED;
2257 goto keep_tok_flags;
2259 case '#':
2260 /* XXX: simplify */
2261 PEEKC(c, p);
2262 if ((tok_flags & TOK_FLAG_BOL) &&
2263 (parse_flags & PARSE_FLAG_PREPROCESS)) {
2264 file->buf_ptr = p;
2265 preprocess(tok_flags & TOK_FLAG_BOF);
2266 p = file->buf_ptr;
2267 goto maybe_newline;
2268 } else {
2269 if (c == '#') {
2270 p++;
2271 tok = TOK_TWOSHARPS;
2272 } else {
2273 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
2274 p = parse_line_comment(p - 1);
2275 goto redo_no_start;
2276 } else {
2277 tok = '#';
2281 break;
2283 case 'a': case 'b': case 'c': case 'd':
2284 case 'e': case 'f': case 'g': case 'h':
2285 case 'i': case 'j': case 'k': case 'l':
2286 case 'm': case 'n': case 'o': case 'p':
2287 case 'q': case 'r': case 's': case 't':
2288 case 'u': case 'v': case 'w': case 'x':
2289 case 'y': case 'z':
2290 case 'A': case 'B': case 'C': case 'D':
2291 case 'E': case 'F': case 'G': case 'H':
2292 case 'I': case 'J': case 'K':
2293 case 'M': case 'N': case 'O': case 'P':
2294 case 'Q': case 'R': case 'S': case 'T':
2295 case 'U': case 'V': case 'W': case 'X':
2296 case 'Y': case 'Z':
2297 case '_':
2298 parse_ident_fast:
2299 p1 = p;
2300 h = TOK_HASH_INIT;
2301 h = TOK_HASH_FUNC(h, c);
2302 p++;
2303 for(;;) {
2304 c = *p;
2305 if (!isidnum_table[c-CH_EOF])
2306 break;
2307 h = TOK_HASH_FUNC(h, c);
2308 p++;
2310 if (c != '\\') {
2311 TokenSym **pts;
2312 int len;
2314 /* fast case : no stray found, so we have the full token
2315 and we have already hashed it */
2316 len = p - p1;
2317 h &= (TOK_HASH_SIZE - 1);
2318 pts = &hash_ident[h];
2319 for(;;) {
2320 ts = *pts;
2321 if (!ts)
2322 break;
2323 if (ts->len == len && !memcmp(ts->str, p1, len))
2324 goto token_found;
2325 pts = &(ts->hash_next);
2327 ts = tok_alloc_new(pts, (char *) p1, len);
2328 token_found: ;
2329 } else {
2330 /* slower case */
2331 cstr_reset(&tokcstr);
2333 while (p1 < p) {
2334 cstr_ccat(&tokcstr, *p1);
2335 p1++;
2337 p--;
2338 PEEKC(c, p);
2339 parse_ident_slow:
2340 while (isidnum_table[c-CH_EOF]) {
2341 cstr_ccat(&tokcstr, c);
2342 PEEKC(c, p);
2344 ts = tok_alloc(tokcstr.data, tokcstr.size);
2346 tok = ts->tok;
2347 break;
2348 case 'L':
2349 t = p[1];
2350 if (t != '\\' && t != '\'' && t != '\"') {
2351 /* fast case */
2352 goto parse_ident_fast;
2353 } else {
2354 PEEKC(c, p);
2355 if (c == '\'' || c == '\"') {
2356 is_long = 1;
2357 goto str_const;
2358 } else {
2359 cstr_reset(&tokcstr);
2360 cstr_ccat(&tokcstr, 'L');
2361 goto parse_ident_slow;
2364 break;
2365 case '0': case '1': case '2': case '3':
2366 case '4': case '5': case '6': case '7':
2367 case '8': case '9':
2369 cstr_reset(&tokcstr);
2370 /* after the first digit, accept digits, alpha, '.' or sign if
2371 prefixed by 'eEpP' */
2372 parse_num:
2373 for(;;) {
2374 t = c;
2375 cstr_ccat(&tokcstr, c);
2376 PEEKC(c, p);
2377 if (!(isnum(c) || isid(c) || c == '.' ||
2378 ((c == '+' || c == '-') &&
2379 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
2380 break;
2382 /* We add a trailing '\0' to ease parsing */
2383 cstr_ccat(&tokcstr, '\0');
2384 tokc.cstr = &tokcstr;
2385 tok = TOK_PPNUM;
2386 break;
2387 case '.':
2388 /* special dot handling because it can also start a number */
2389 PEEKC(c, p);
2390 if (isnum(c)) {
2391 cstr_reset(&tokcstr);
2392 cstr_ccat(&tokcstr, '.');
2393 goto parse_num;
2394 } else if (c == '.') {
2395 PEEKC(c, p);
2396 if (c != '.')
2397 expect("'.'");
2398 PEEKC(c, p);
2399 tok = TOK_DOTS;
2400 } else {
2401 tok = '.';
2403 break;
2404 case '\'':
2405 case '\"':
2406 is_long = 0;
2407 str_const:
2409 CString str;
2410 int sep;
2412 sep = c;
2414 /* parse the string */
2415 cstr_new(&str);
2416 p = parse_pp_string(p, sep, &str);
2417 cstr_ccat(&str, '\0');
2419 /* eval the escape (should be done as TOK_PPNUM) */
2420 cstr_reset(&tokcstr);
2421 parse_escape_string(&tokcstr, str.data, is_long);
2422 cstr_free(&str);
2424 if (sep == '\'') {
2425 int char_size;
2426 /* XXX: make it portable */
2427 if (!is_long)
2428 char_size = 1;
2429 else
2430 char_size = sizeof(nwchar_t);
2431 if (tokcstr.size <= char_size)
2432 tcc_error("empty character constant");
2433 if (tokcstr.size > 2 * char_size)
2434 tcc_warning("multi-character character constant");
2435 if (!is_long) {
2436 tokc.i = *(int8_t *)tokcstr.data;
2437 tok = TOK_CCHAR;
2438 } else {
2439 tokc.i = *(nwchar_t *)tokcstr.data;
2440 tok = TOK_LCHAR;
2442 } else {
2443 tokc.cstr = &tokcstr;
2444 if (!is_long)
2445 tok = TOK_STR;
2446 else
2447 tok = TOK_LSTR;
2450 break;
2452 case '<':
2453 PEEKC(c, p);
2454 if (c == '=') {
2455 p++;
2456 tok = TOK_LE;
2457 } else if (c == '<') {
2458 PEEKC(c, p);
2459 if (c == '=') {
2460 p++;
2461 tok = TOK_A_SHL;
2462 } else {
2463 tok = TOK_SHL;
2465 } else {
2466 tok = TOK_LT;
2468 break;
2470 case '>':
2471 PEEKC(c, p);
2472 if (c == '=') {
2473 p++;
2474 tok = TOK_GE;
2475 } else if (c == '>') {
2476 PEEKC(c, p);
2477 if (c == '=') {
2478 p++;
2479 tok = TOK_A_SAR;
2480 } else {
2481 tok = TOK_SAR;
2483 } else {
2484 tok = TOK_GT;
2486 break;
2488 case '&':
2489 PEEKC(c, p);
2490 if (c == '&') {
2491 p++;
2492 tok = TOK_LAND;
2493 } else if (c == '=') {
2494 p++;
2495 tok = TOK_A_AND;
2496 } else {
2497 tok = '&';
2499 break;
2501 case '|':
2502 PEEKC(c, p);
2503 if (c == '|') {
2504 p++;
2505 tok = TOK_LOR;
2506 } else if (c == '=') {
2507 p++;
2508 tok = TOK_A_OR;
2509 } else {
2510 tok = '|';
2512 break;
2514 case '+':
2515 PEEKC(c, p);
2516 if (c == '+') {
2517 p++;
2518 tok = TOK_INC;
2519 } else if (c == '=') {
2520 p++;
2521 tok = TOK_A_ADD;
2522 } else {
2523 tok = '+';
2525 break;
2527 case '-':
2528 PEEKC(c, p);
2529 if (c == '-') {
2530 p++;
2531 tok = TOK_DEC;
2532 } else if (c == '=') {
2533 p++;
2534 tok = TOK_A_SUB;
2535 } else if (c == '>') {
2536 p++;
2537 tok = TOK_ARROW;
2538 } else {
2539 tok = '-';
2541 break;
2543 PARSE2('!', '!', '=', TOK_NE)
2544 PARSE2('=', '=', '=', TOK_EQ)
2545 PARSE2('*', '*', '=', TOK_A_MUL)
2546 PARSE2('%', '%', '=', TOK_A_MOD)
2547 PARSE2('^', '^', '=', TOK_A_XOR)
2549 /* comments or operator */
2550 case '/':
2551 PEEKC(c, p);
2552 if (c == '*') {
2553 p = parse_comment(p);
2554 /* comments replaced by a blank */
2555 tok = ' ';
2556 goto keep_tok_flags;
2557 } else if (c == '/') {
2558 p = parse_line_comment(p);
2559 tok = ' ';
2560 goto keep_tok_flags;
2561 } else if (c == '=') {
2562 p++;
2563 tok = TOK_A_DIV;
2564 } else {
2565 tok = '/';
2567 break;
2569 /* simple tokens */
2570 case '(':
2571 case ')':
2572 case '[':
2573 case ']':
2574 case '{':
2575 case '}':
2576 case ',':
2577 case ';':
2578 case ':':
2579 case '?':
2580 case '~':
2581 case '$': /* only used in assembler */
2582 case '@': /* dito */
2583 tok = c;
2584 p++;
2585 break;
2586 default:
2587 tcc_error("unrecognized character \\x%02x", c);
2588 break;
2590 tok_flags = 0;
2591 keep_tok_flags:
2592 file->buf_ptr = p;
2593 #if defined(PARSE_DEBUG)
2594 printf("token = %s\n", get_tok_str(tok, &tokc));
2595 #endif
2598 /* return next token without macro substitution. Can read input from
2599 macro_ptr buffer */
2600 static void next_nomacro_spc(void)
2602 if (macro_ptr) {
2603 redo:
2604 tok = *macro_ptr;
2605 if (tok) {
2606 TOK_GET(&tok, &macro_ptr, &tokc);
2607 if (tok == TOK_LINENUM) {
2608 file->line_num = tokc.i;
2609 goto redo;
2612 } else {
2613 next_nomacro1();
2617 ST_FUNC void next_nomacro(void)
2619 do {
2620 next_nomacro_spc();
2621 } while (is_space(tok));
2624 /* substitute arguments in replacement lists in macro_str by the values in
2625 args (field d) and return allocated string */
2626 static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
2628 int last_tok, t, spc;
2629 const int *st;
2630 Sym *s;
2631 CValue cval;
2632 TokenString str;
2633 CString cstr;
2635 tok_str_new(&str);
2636 last_tok = 0;
2637 while(1) {
2638 TOK_GET(&t, &macro_str, &cval);
2639 if (!t)
2640 break;
2641 if (t == '#') {
2642 /* stringize */
2643 TOK_GET(&t, &macro_str, &cval);
2644 if (!t)
2645 break;
2646 s = sym_find2(args, t);
2647 if (s) {
2648 cstr_new(&cstr);
2649 st = s->d;
2650 spc = 0;
2651 while (*st) {
2652 TOK_GET(&t, &st, &cval);
2653 if (!check_space(t, &spc))
2654 cstr_cat(&cstr, get_tok_str(t, &cval));
2656 cstr.size -= spc;
2657 cstr_ccat(&cstr, '\0');
2658 #ifdef PP_DEBUG
2659 printf("stringize: %s\n", (char *)cstr.data);
2660 #endif
2661 /* add string */
2662 cval.cstr = &cstr;
2663 tok_str_add2(&str, TOK_STR, &cval);
2664 cstr_free(&cstr);
2665 } else {
2666 tok_str_add2(&str, t, &cval);
2668 } else if (t >= TOK_IDENT) {
2669 s = sym_find2(args, t);
2670 if (s) {
2671 st = s->d;
2672 /* if '##' is present before or after, no arg substitution */
2673 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
2674 /* special case for var arg macros : ## eats the
2675 ',' if empty VA_ARGS variable. */
2676 /* XXX: test of the ',' is not 100%
2677 reliable. should fix it to avoid security
2678 problems */
2679 if (gnu_ext && s->type.t &&
2680 last_tok == TOK_TWOSHARPS &&
2681 str.len >= 2 && str.str[str.len - 2] == ',') {
2682 if (*st == TOK_PLCHLDR) {
2683 /* suppress ',' '##' */
2684 str.len -= 2;
2685 } else {
2686 /* suppress '##' and add variable */
2687 str.len--;
2688 goto add_var;
2690 } else {
2691 int t1;
2692 add_var:
2693 for(;;) {
2694 TOK_GET(&t1, &st, &cval);
2695 if (!t1)
2696 break;
2697 tok_str_add2(&str, t1, &cval);
2700 } else if (*st != TOK_PLCHLDR) {
2701 /* NOTE: the stream cannot be read when macro
2702 substituing an argument */
2703 macro_subst(&str, nested_list, st, NULL);
2705 } else {
2706 tok_str_add(&str, t);
2708 } else {
2709 tok_str_add2(&str, t, &cval);
2711 last_tok = t;
2713 tok_str_add(&str, 0);
2714 return str.str;
2717 static char const ab_month_name[12][4] =
2719 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2720 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2723 /* do macro substitution of current token with macro 's' and add
2724 result to (tok_str,tok_len). 'nested_list' is the list of all
2725 macros we got inside to avoid recursing. Return non zero if no
2726 substitution needs to be done */
2727 static int macro_subst_tok(TokenString *tok_str,
2728 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
2730 Sym *args, *sa, *sa1;
2731 int mstr_allocated, parlevel, *mstr, t, t1, spc;
2732 const int *p;
2733 TokenString str;
2734 char *cstrval;
2735 CValue cval;
2736 CString cstr;
2737 char buf[32];
2739 /* if symbol is a macro, prepare substitution */
2740 /* special macros */
2741 if (tok == TOK___LINE__) {
2742 snprintf(buf, sizeof(buf), "%d", file->line_num);
2743 cstrval = buf;
2744 t1 = TOK_PPNUM;
2745 goto add_cstr1;
2746 } else if (tok == TOK___FILE__) {
2747 cstrval = file->filename;
2748 goto add_cstr;
2749 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
2750 time_t ti;
2751 struct tm *tm;
2753 time(&ti);
2754 tm = localtime(&ti);
2755 if (tok == TOK___DATE__) {
2756 snprintf(buf, sizeof(buf), "%s %2d %d",
2757 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
2758 } else {
2759 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
2760 tm->tm_hour, tm->tm_min, tm->tm_sec);
2762 cstrval = buf;
2763 add_cstr:
2764 t1 = TOK_STR;
2765 add_cstr1:
2766 cstr_new(&cstr);
2767 cstr_cat(&cstr, cstrval);
2768 cstr_ccat(&cstr, '\0');
2769 cval.cstr = &cstr;
2770 tok_str_add2(tok_str, t1, &cval);
2771 cstr_free(&cstr);
2772 } else {
2773 mstr = s->d;
2774 mstr_allocated = 0;
2775 if (s->type.t == MACRO_FUNC) {
2776 /* NOTE: we do not use next_nomacro to avoid eating the
2777 next token. XXX: find better solution */
2778 redo:
2779 if (macro_ptr) {
2780 p = macro_ptr;
2781 while (is_space(t = *p) || TOK_LINEFEED == t)
2782 ++p;
2783 if (t == 0 && can_read_stream) {
2784 /* end of macro stream: we must look at the token
2785 after in the file */
2786 struct macro_level *ml = *can_read_stream;
2787 macro_ptr = NULL;
2788 if (ml)
2790 macro_ptr = ml->p;
2791 ml->p = NULL;
2792 *can_read_stream = ml -> prev;
2794 /* also, end of scope for nested defined symbol */
2795 (*nested_list)->v = -1;
2796 goto redo;
2798 } else {
2799 ch = file->buf_ptr[0];
2800 while (is_space(ch) || ch == '\n' || ch == '/')
2802 if (ch == '/')
2804 int c;
2805 uint8_t *p = file->buf_ptr;
2806 PEEKC(c, p);
2807 if (c == '*') {
2808 p = parse_comment(p);
2809 file->buf_ptr = p - 1;
2810 } else if (c == '/') {
2811 p = parse_line_comment(p);
2812 file->buf_ptr = p - 1;
2813 } else
2814 break;
2816 cinp();
2818 t = ch;
2820 if (t != '(') /* no macro subst */
2821 return -1;
2823 /* argument macro */
2824 next_nomacro();
2825 next_nomacro();
2826 args = NULL;
2827 sa = s->next;
2828 /* NOTE: empty args are allowed, except if no args */
2829 for(;;) {
2830 /* handle '()' case */
2831 if (!args && !sa && tok == ')')
2832 break;
2833 if (!sa)
2834 tcc_error("macro '%s' used with too many args",
2835 get_tok_str(s->v, 0));
2836 tok_str_new(&str);
2837 parlevel = spc = 0;
2838 /* NOTE: non zero sa->t indicates VA_ARGS */
2839 while ((parlevel > 0 ||
2840 (tok != ')' &&
2841 (tok != ',' || sa->type.t))) &&
2842 tok != -1) {
2843 if (tok == '(')
2844 parlevel++;
2845 else if (tok == ')')
2846 parlevel--;
2847 if (tok == TOK_LINEFEED)
2848 tok = ' ';
2849 if (!check_space(tok, &spc))
2850 tok_str_add2(&str, tok, &tokc);
2851 next_nomacro_spc();
2853 if (!str.len)
2854 tok_str_add(&str, TOK_PLCHLDR);
2855 str.len -= spc;
2856 tok_str_add(&str, 0);
2857 sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
2858 sa1->d = str.str;
2859 sa = sa->next;
2860 if (tok == ')') {
2861 /* special case for gcc var args: add an empty
2862 var arg argument if it is omitted */
2863 if (sa && sa->type.t && gnu_ext)
2864 continue;
2865 else
2866 break;
2868 if (tok != ',')
2869 expect(",");
2870 next_nomacro();
2872 if (sa) {
2873 tcc_error("macro '%s' used with too few args",
2874 get_tok_str(s->v, 0));
2877 /* now subst each arg */
2878 mstr = macro_arg_subst(nested_list, mstr, args);
2879 /* free memory */
2880 sa = args;
2881 while (sa) {
2882 sa1 = sa->prev;
2883 tok_str_free(sa->d);
2884 sym_free(sa);
2885 sa = sa1;
2887 mstr_allocated = 1;
2889 sym_push2(nested_list, s->v, 0, 0);
2890 macro_subst(tok_str, nested_list, mstr, can_read_stream);
2891 /* pop nested defined symbol */
2892 sa1 = *nested_list;
2893 *nested_list = sa1->prev;
2894 sym_free(sa1);
2895 if (mstr_allocated)
2896 tok_str_free(mstr);
2898 return 0;
2901 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
2902 return the resulting string (which must be freed). */
2903 static inline int *macro_twosharps(const int *macro_str)
2905 const int *ptr;
2906 int t;
2907 TokenString macro_str1;
2908 CString cstr;
2909 int n, start_of_nosubsts;
2911 /* we search the first '##' */
2912 for(ptr = macro_str;;) {
2913 CValue cval;
2914 TOK_GET(&t, &ptr, &cval);
2915 if (t == TOK_TWOSHARPS)
2916 break;
2917 /* nothing more to do if end of string */
2918 if (t == 0)
2919 return NULL;
2922 /* we saw '##', so we need more processing to handle it */
2923 start_of_nosubsts = -1;
2924 tok_str_new(&macro_str1);
2925 for(ptr = macro_str;;) {
2926 TOK_GET(&tok, &ptr, &tokc);
2927 if (tok == 0)
2928 break;
2929 if (tok == TOK_TWOSHARPS)
2930 continue;
2931 if (tok == TOK_NOSUBST && start_of_nosubsts < 0)
2932 start_of_nosubsts = macro_str1.len;
2933 while (*ptr == TOK_TWOSHARPS) {
2934 /* given 'a##b', remove nosubsts preceding 'a' */
2935 if (start_of_nosubsts >= 0)
2936 macro_str1.len = start_of_nosubsts;
2937 /* given 'a##b', skip '##' */
2938 t = *++ptr;
2939 /* given 'a##b', remove nosubsts preceding 'b' */
2940 while (t == TOK_NOSUBST)
2941 t = *++ptr;
2942 if (t && t != TOK_TWOSHARPS) {
2943 CValue cval;
2944 TOK_GET(&t, &ptr, &cval);
2945 /* We concatenate the two tokens */
2946 cstr_new(&cstr);
2947 if (tok != TOK_PLCHLDR)
2948 cstr_cat(&cstr, get_tok_str(tok, &tokc));
2949 n = cstr.size;
2950 if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR)
2951 cstr_cat(&cstr, get_tok_str(t, &cval));
2952 cstr_ccat(&cstr, '\0');
2954 tcc_open_bf(tcc_state, ":paste:", cstr.size);
2955 memcpy(file->buffer, cstr.data, cstr.size);
2956 for (;;) {
2957 next_nomacro1();
2958 if (0 == *file->buf_ptr)
2959 break;
2960 tok_str_add2(&macro_str1, tok, &tokc);
2961 tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid preprocessing token",
2962 n, cstr.data, (char*)cstr.data + n);
2964 tcc_close();
2965 cstr_free(&cstr);
2968 if (tok != TOK_NOSUBST) {
2969 tok_str_add2(&macro_str1, tok, &tokc);
2970 tok = ' ';
2971 start_of_nosubsts = -1;
2973 tok_str_add2(&macro_str1, tok, &tokc);
2975 tok_str_add(&macro_str1, 0);
2976 return macro_str1.str;
2980 /* do macro substitution of macro_str and add result to
2981 (tok_str,tok_len). 'nested_list' is the list of all macros we got
2982 inside to avoid recursing. */
2983 static void macro_subst(TokenString *tok_str, Sym **nested_list,
2984 const int *macro_str, struct macro_level ** can_read_stream)
2986 Sym *s;
2987 int *macro_str1;
2988 const int *ptr;
2989 int t, ret, spc;
2990 CValue cval;
2991 struct macro_level ml;
2992 int force_blank;
2994 /* first scan for '##' operator handling */
2995 ptr = macro_str;
2996 macro_str1 = macro_twosharps(ptr);
2998 if (macro_str1)
2999 ptr = macro_str1;
3000 spc = 0;
3001 force_blank = 0;
3003 while (1) {
3004 /* NOTE: ptr == NULL can only happen if tokens are read from
3005 file stream due to a macro function call */
3006 if (ptr == NULL)
3007 break;
3008 TOK_GET(&t, &ptr, &cval);
3009 if (t == 0)
3010 break;
3011 if (t == TOK_NOSUBST) {
3012 /* following token has already been subst'd. just copy it on */
3013 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3014 TOK_GET(&t, &ptr, &cval);
3015 goto no_subst;
3017 s = define_find(t);
3018 if (s != NULL) {
3019 /* if nested substitution, do nothing */
3020 if (sym_find2(*nested_list, t)) {
3021 /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
3022 tok_str_add2(tok_str, TOK_NOSUBST, NULL);
3023 goto no_subst;
3025 ml.p = macro_ptr;
3026 if (can_read_stream)
3027 ml.prev = *can_read_stream, *can_read_stream = &ml;
3028 macro_ptr = (int *)ptr;
3029 tok = t;
3030 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
3031 ptr = (int *)macro_ptr;
3032 macro_ptr = ml.p;
3033 if (can_read_stream && *can_read_stream == &ml)
3034 *can_read_stream = ml.prev;
3035 if (ret != 0)
3036 goto no_subst;
3037 if (parse_flags & PARSE_FLAG_SPACES)
3038 force_blank = 1;
3039 } else {
3040 no_subst:
3041 if (force_blank) {
3042 tok_str_add(tok_str, ' ');
3043 spc = 1;
3044 force_blank = 0;
3046 if (!check_space(t, &spc))
3047 tok_str_add2(tok_str, t, &cval);
3050 if (macro_str1)
3051 tok_str_free(macro_str1);
3054 /* return next token with macro substitution */
3055 ST_FUNC void next(void)
3057 Sym *nested_list, *s;
3058 TokenString str;
3059 struct macro_level *ml;
3061 redo:
3062 if (parse_flags & PARSE_FLAG_SPACES)
3063 next_nomacro_spc();
3064 else
3065 next_nomacro();
3066 if (!macro_ptr) {
3067 /* if not reading from macro substituted string, then try
3068 to substitute macros */
3069 if (tok >= TOK_IDENT &&
3070 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3071 s = define_find(tok);
3072 if (s) {
3073 /* we have a macro: we try to substitute */
3074 tok_str_new(&str);
3075 nested_list = NULL;
3076 ml = NULL;
3077 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
3078 /* substitution done, NOTE: maybe empty */
3079 tok_str_add(&str, 0);
3080 macro_ptr = str.str;
3081 macro_ptr_allocated = str.str;
3082 goto redo;
3086 } else {
3087 if (tok == 0) {
3088 /* end of macro or end of unget buffer */
3089 if (unget_buffer_enabled) {
3090 macro_ptr = unget_saved_macro_ptr;
3091 unget_buffer_enabled = 0;
3092 } else {
3093 /* end of macro string: free it */
3094 tok_str_free(macro_ptr_allocated);
3095 macro_ptr_allocated = NULL;
3096 macro_ptr = NULL;
3098 goto redo;
3099 } else if (tok == TOK_NOSUBST) {
3100 /* discard preprocessor's nosubst markers */
3101 goto redo;
3105 /* convert preprocessor tokens into C tokens */
3106 if (tok == TOK_PPNUM &&
3107 (parse_flags & PARSE_FLAG_TOK_NUM)) {
3108 parse_number((char *)tokc.cstr->data);
3112 /* push back current token and set current token to 'last_tok'. Only
3113 identifier case handled for labels. */
3114 ST_INLN void unget_tok(int last_tok)
3116 int i, n;
3117 int *q;
3118 if (unget_buffer_enabled)
3120 /* assert(macro_ptr == unget_saved_buffer + 1);
3121 assert(*macro_ptr == 0); */
3123 else
3125 unget_saved_macro_ptr = macro_ptr;
3126 unget_buffer_enabled = 1;
3128 q = unget_saved_buffer;
3129 macro_ptr = q;
3130 *q++ = tok;
3131 n = tok_ext_size(tok) - 1;
3132 for(i=0;i<n;i++)
3133 *q++ = tokc.tab[i];
3134 *q = 0; /* end of token string */
3135 tok = last_tok;
3139 /* better than nothing, but needs extension to handle '-E' option
3140 correctly too */
3141 ST_FUNC void preprocess_init(TCCState *s1)
3143 s1->include_stack_ptr = s1->include_stack;
3144 /* XXX: move that before to avoid having to initialize
3145 file->ifdef_stack_ptr ? */
3146 s1->ifdef_stack_ptr = s1->ifdef_stack;
3147 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
3149 vtop = vstack - 1;
3150 s1->pack_stack[0] = 0;
3151 s1->pack_stack_ptr = s1->pack_stack;
3154 ST_FUNC void preprocess_new(void)
3156 int i, c;
3157 const char *p, *r;
3159 /* init isid table */
3160 for(i=CH_EOF;i<256;i++)
3161 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
3163 /* add all tokens */
3164 if (table_ident) {
3165 tcc_free (table_ident);
3166 table_ident = NULL;
3168 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
3170 tok_ident = TOK_IDENT;
3171 p = tcc_keywords;
3172 while (*p) {
3173 r = p;
3174 for(;;) {
3175 c = *r++;
3176 if (c == '\0')
3177 break;
3179 tok_alloc(p, r - p - 1);
3180 p = r;
3184 static void line_macro_output(BufferedFile *f, const char *s, TCCState *s1)
3186 switch (s1->Pflag) {
3187 case LINE_MACRO_OUTPUT_FORMAT_STD:
3188 /* "tcc -E -P1" case */
3189 fprintf(s1->ppfp, "# line %d \"%s\"\n", f->line_num, f->filename);
3190 break;
3192 case LINE_MACRO_OUTPUT_FORMAT_NONE:
3193 /* "tcc -E -P" case: don't output a line directive */
3194 break;
3196 case LINE_MACRO_OUTPUT_FORMAT_GCC:
3197 default:
3198 /* "tcc -E" case: a gcc standard by default */
3199 fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, s);
3200 break;
3204 /* Preprocess the current file */
3205 ST_FUNC int tcc_preprocess(TCCState *s1)
3207 BufferedFile *file_ref, **iptr, **iptr_new;
3208 int token_seen, d;
3209 const char *s;
3211 preprocess_init(s1);
3212 ch = file->buf_ptr[0];
3213 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
3214 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
3215 PARSE_FLAG_LINEFEED | PARSE_FLAG_SPACES;
3216 token_seen = 0;
3217 file->line_ref = 0;
3218 file_ref = NULL;
3219 iptr = s1->include_stack_ptr;
3221 for (;;) {
3222 next();
3223 if (tok == TOK_EOF) {
3224 break;
3225 } else if (file != file_ref) {
3226 if (file_ref)
3227 line_macro_output(file_ref, "", s1);
3228 goto print_line;
3229 } else if (tok == TOK_LINEFEED) {
3230 if (!token_seen)
3231 continue;
3232 file->line_ref++;
3233 token_seen = 0;
3234 } else if (!token_seen) {
3235 d = file->line_num - file->line_ref;
3236 if (file != file_ref || d >= 8) {
3237 print_line:
3238 s = "";
3239 if (tcc_state->Pflag == LINE_MACRO_OUTPUT_FORMAT_GCC) {
3240 iptr_new = s1->include_stack_ptr;
3241 s = iptr_new > iptr ? " 1"
3242 : iptr_new < iptr ? " 2"
3243 : iptr_new > s1->include_stack ? " 3"
3244 : ""
3247 line_macro_output(file, s, s1);
3248 } else {
3249 while (d > 0)
3250 fputs("\n", s1->ppfp), --d;
3252 file->line_ref = (file_ref = file)->line_num;
3253 token_seen = tok != TOK_LINEFEED;
3254 if (!token_seen)
3255 continue;
3257 fputs(get_tok_str(tok, &tokc), s1->ppfp);
3259 return 0;