| @@ -504,6 +504,9 @@ struct TCCState { · 503 unmodified lines |
| 504 | 504 | /* pack stack */ |
| 505 | 505 | int pack_stack[PACK_STACK_SIZE]; |
| 506 | 506 | int *pack_stack_ptr; |
| 507 | + |
| 508 | + /* output file for preprocessing */ |
| 509 | + FILE *outfile; |
| 507 | 510 | }; |
| 508 | 511 | |
| 509 | 512 | /* The current value can be: */ |
| @@ -839,6 +842,7 @@ static int tcc_add_dll(TCCState *s, const char *filename, int flags); · 329 unmodified lines |
| 839 | 842 | |
| 840 | 843 | #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */ |
| 841 | 844 | #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */ |
| 845 | +#define AFF_PREPROCESS 0x0004 /* preprocess file */ |
| 842 | 846 | static int tcc_add_file_internal(TCCState *s, const char *filename, int flags); |
| 843 | 847 | |
| 844 | 848 | /* tcccoff.c */ |
| @@ -1675,6 +1679,8 @@ char *get_tok_str(int v, CValue *cv) · 830 unmodified lines |
| 1675 | 1679 | case TOK_GT: |
| 1676 | 1680 | v = '>'; |
| 1677 | 1681 | goto addv; |
| 1682 | + case TOK_DOTS: |
| 1683 | + return strcpy(p, "..."); |
| 1678 | 1684 | case TOK_A_SHL: |
| 1679 | 1685 | return strcpy(p, "<<="); |
| 1680 | 1686 | case TOK_A_SAR: |
| @@ -9279,6 +9285,47 @@ static int tcc_compile(TCCState *s1) · 7598 unmodified lines |
| 9279 | 9285 | return s1->nb_errors != 0 ? -1 : 0; |
| 9280 | 9286 | } |
| 9281 | 9287 | |
| 9288 | +/* Preprocess the current file */ |
| 9289 | +/* XXX: add line and file infos, add options to preserve spaces */ |
| 9290 | +static int tcc_preprocess(TCCState *s1) |
| 9291 | +{ |
| 9292 | + Sym *define_start; |
| 9293 | + int last_is_space; |
| 9294 | + |
| 9295 | + preprocess_init(s1); |
| 9296 | + |
| 9297 | + define_start = define_stack; |
| 9298 | + |
| 9299 | + ch = file->buf_ptr[0]; |
| 9300 | + tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; |
| 9301 | + parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS | |
| 9302 | + PARSE_FLAG_LINEFEED; |
| 9303 | + last_is_space = 1; |
| 9304 | + next(); |
| 9305 | + for(;;) { |
| 9306 | + if (tok == TOK_EOF) |
| 9307 | + break; |
| 9308 | + if (!last_is_space) { |
| 9309 | + fputc(' ', s1->outfile); |
| 9310 | + } |
| 9311 | + fputs(get_tok_str(tok, &tokc), s1->outfile); |
| 9312 | + if (tok == TOK_LINEFEED) { |
| 9313 | + last_is_space = 1; |
| 9314 | + /* XXX: suppress that hack */ |
| 9315 | + parse_flags &= ~PARSE_FLAG_LINEFEED; |
| 9316 | + next(); |
| 9317 | + parse_flags |= PARSE_FLAG_LINEFEED; |
| 9318 | + } else { |
| 9319 | + last_is_space = 0; |
| 9320 | + next(); |
| 9321 | + } |
| 9322 | + } |
| 9323 | + |
| 9324 | + free_defines(define_start); |
| 9325 | + |
| 9326 | + return 0; |
| 9327 | +} |
| 9328 | + |
| 9282 | 9329 | #ifdef LIBTCC |
| 9283 | 9330 | int tcc_compile_string(TCCState *s, const char *str) |
| 9284 | 9331 | { |
| @@ -9944,7 +9991,9 @@ static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 659 unmodified lines |
| 9944 | 9991 | goto fail1; |
| 9945 | 9992 | } |
| 9946 | 9993 | |
| 9947 | | − if (!ext || !strcmp(ext, "c")) { |
| 9994 | + if (flags & AFF_PREPROCESS) { |
| 9995 | + ret = tcc_preprocess(s1); |
| 9996 | + } else if (!ext || !strcmp(ext, "c")) { |
| 9948 | 9997 | /* C file assumed */ |
| 9949 | 9998 | ret = tcc_compile(s1); |
| 9950 | 9999 | } else |
| @@ -10264,7 +10313,7 @@ static int64_t getclock_us(void) · 313 unmodified lines |
| 10264 | 10313 | |
| 10265 | 10314 | void help(void) |
| 10266 | 10315 | { |
| 10267 | | − printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2005 Fabrice Bellard\n" |
| 10316 | + printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n" |
| 10268 | 10317 | "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" |
| 10269 | 10318 | " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n" |
| 10270 | 10319 | " [infile1 infile2...] [-run infile args...]\n" |
| @@ -10280,6 +10329,7 @@ void help(void) · 9 unmodified lines |
| 10280 | 10329 | " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n" |
| 10281 | 10330 | " -w disable all warnings\n" |
| 10282 | 10331 | "Preprocessor options:\n" |
| 10332 | + " -E preprocess only\n" |
| 10283 | 10333 | " -Idir add include path 'dir'\n" |
| 10284 | 10334 | " -Dsym[=val] define 'sym' with value 'val'\n" |
| 10285 | 10335 | " -Usym undefine 'sym'\n" |
| @@ -10338,6 +10388,7 @@ enum { · 52 unmodified lines |
| 10338 | 10388 | TCC_OPTION_v, |
| 10339 | 10389 | TCC_OPTION_w, |
| 10340 | 10390 | TCC_OPTION_pipe, |
| 10391 | + TCC_OPTION_E, |
| 10341 | 10392 | }; |
| 10342 | 10393 | |
| 10343 | 10394 | static const TCCOption tcc_options[] = { |
| @@ -10373,6 +10424,7 @@ static const TCCOption tcc_options[] = { · 29 unmodified lines |
| 10373 | 10424 | { "v", TCC_OPTION_v, 0 }, |
| 10374 | 10425 | { "w", TCC_OPTION_w, 0 }, |
| 10375 | 10426 | { "pipe", TCC_OPTION_pipe, 0}, |
| 10427 | + { "E", TCC_OPTION_E, 0}, |
| 10376 | 10428 | { NULL }, |
| 10377 | 10429 | }; |
| 10378 | 10430 | |
| @@ -10601,6 +10653,9 @@ int parse_args(TCCState *s, int argc, char **argv) · 222 unmodified lines |
| 10601 | 10653 | } |
| 10602 | 10654 | } |
| 10603 | 10655 | break; |
| 10656 | + case TCC_OPTION_E: |
| 10657 | + output_type = TCC_OUTPUT_PREPROCESS; |
| 10658 | + break; |
| 10604 | 10659 | default: |
| 10605 | 10660 | if (s->warn_unsupported) { |
| 10606 | 10661 | unsupported_option: |
| @@ -10657,7 +10712,7 @@ int main(int argc, char **argv) · 50 unmodified lines |
| 10657 | 10712 | printf("install: %s/\n", tcc_lib_path); |
| 10658 | 10713 | return 0; |
| 10659 | 10714 | } |
| 10660 | | − |
| 10715 | + |
| 10661 | 10716 | nb_objfiles = nb_files - nb_libraries; |
| 10662 | 10717 | |
| 10663 | 10718 | /* if outfile provided without other options, we output an |
| @@ -10674,7 +10729,16 @@ int main(int argc, char **argv) · 10 unmodified lines |
| 10674 | 10729 | error("cannot specify libraries with -c"); |
| 10675 | 10730 | } |
| 10676 | 10731 | |
| 10677 | | − if (output_type != TCC_OUTPUT_MEMORY) { |
| 10732 | + |
| 10733 | + if (output_type == TCC_OUTPUT_PREPROCESS) { |
| 10734 | + if (!outfile) { |
| 10735 | + s->outfile = stdout; |
| 10736 | + } else { |
| 10737 | + s->outfile = fopen(outfile, "wb"); |
| 10738 | + if (!s->outfile) |
| 10739 | + error("could not open '%s", outfile); |
| 10740 | + } |
| 10741 | + } else if (output_type != TCC_OUTPUT_MEMORY) { |
| 10678 | 10742 | if (!outfile) { |
| 10679 | 10743 | /* compute default outfile name */ |
| 10680 | 10744 | pstrcpy(objfilename, sizeof(objfilename) - 1, |
| @@ -10709,13 +10773,18 @@ int main(int argc, char **argv) · 28 unmodified lines |
| 10709 | 10773 | const char *filename; |
| 10710 | 10774 | |
| 10711 | 10775 | filename = files[i]; |
| 10712 | | − if (filename[0] == '-') { |
| 10713 | | − if (tcc_add_library(s, filename + 2) < 0) |
| 10714 | | − error("cannot find %s", filename); |
| 10776 | + if (output_type == TCC_OUTPUT_PREPROCESS) { |
| 10777 | + tcc_add_file_internal(s, filename, |
| 10778 | + AFF_PRINT_ERROR | AFF_PREPROCESS); |
| 10715 | 10779 | } else { |
| 10716 | | − if (tcc_add_file(s, filename) < 0) { |
| 10717 | | − ret = 1; |
| 10718 | | − goto the_end; |
| 10780 | + if (filename[0] == '-') { |
| 10781 | + if (tcc_add_library(s, filename + 2) < 0) |
| 10782 | + error("cannot find %s", filename); |
| 10783 | + } else { |
| 10784 | + if (tcc_add_file(s, filename) < 0) { |
| 10785 | + ret = 1; |
| 10786 | + goto the_end; |
| 10787 | + } |
| 10719 | 10788 | } |
| 10720 | 10789 | } |
| 10721 | 10790 | } |
| @@ -10736,7 +10805,11 @@ int main(int argc, char **argv) · 14 unmodified lines |
| 10736 | 10805 | total_bytes / total_time / 1000000.0); |
| 10737 | 10806 | } |
| 10738 | 10807 | |
| 10739 | | − if (s->output_type == TCC_OUTPUT_MEMORY) { |
| 10808 | + if (s->output_type == TCC_OUTPUT_PREPROCESS) { |
| 10809 | + if (outfile) |
| 10810 | + fclose(s->outfile); |
| 10811 | + ret = 0; |
| 10812 | + } else if (s->output_type == TCC_OUTPUT_MEMORY) { |
| 10740 | 10813 | ret = tcc_run(s, argc - optind, argv + optind); |
| 10741 | 10814 | } else |
| 10742 | 10815 | #ifdef TCC_TARGET_PE |