History

initial implementation of -E option

bellard committed 20 years ago

Commit 200b58d1 parent 6ca02992 files · +85 −11Browse files

Unified · Split

2 files
  1. libtcc.h +1 −0
  2. tcc.c +84 −11
libtcc.h+1−0
@@ -60,6 +60,7 @@ int tcc_compile_string(TCCState *s, const char *buf); · 59 unmodified lines
60#define TCC_OUTPUT_EXE 1 /* executable file */60#define TCC_OUTPUT_EXE 1 /* executable file */
61#define TCC_OUTPUT_DLL 2 /* dynamic library */61#define TCC_OUTPUT_DLL 2 /* dynamic library */
62#define TCC_OUTPUT_OBJ 3 /* object file */62#define TCC_OUTPUT_OBJ 3 /* object file */
63#define TCC_OUTPUT_PREPROCESS 4 /* preprocessed file (used internally) */
63int tcc_set_output_type(TCCState *s, int output_type);64int tcc_set_output_type(TCCState *s, int output_type);
64 65
65#define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */66#define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
tcc.c+84−11
@@ -504,6 +504,9 @@ struct TCCState { · 503 unmodified lines
504 /* pack stack */504 /* pack stack */
505 int pack_stack[PACK_STACK_SIZE];505 int pack_stack[PACK_STACK_SIZE];
506 int *pack_stack_ptr;506 int *pack_stack_ptr;
507
508 /* output file for preprocessing */
509 FILE *outfile;
507};510};
508 511
509/* The current value can be: */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#define AFF_PRINT_ERROR 0x0001 /* print error if file not found */843#define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
841#define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */844#define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
845#define AFF_PREPROCESS 0x0004 /* preprocess file */
842static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);846static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
843 847
844/* tcccoff.c */848/* tcccoff.c */
@@ -1675,6 +1679,8 @@ char *get_tok_str(int v, CValue *cv) · 830 unmodified lines
1675 case TOK_GT:1679 case TOK_GT:
1676 v = '>';1680 v = '>';
1677 goto addv;1681 goto addv;
1682 case TOK_DOTS:
1683 return strcpy(p, "...");
1678 case TOK_A_SHL:1684 case TOK_A_SHL:
1679 return strcpy(p, "<<=");1685 return strcpy(p, "<<=");
1680 case TOK_A_SAR:1686 case TOK_A_SAR:
@@ -9279,6 +9285,47 @@ static int tcc_compile(TCCState *s1) · 7598 unmodified lines
9279 return s1->nb_errors != 0 ? -1 : 0;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 */
9290static 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#ifdef LIBTCC9329#ifdef LIBTCC
9283int tcc_compile_string(TCCState *s, const char *str)9330int 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 goto fail1;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 /* C file assumed */9997 /* C file assumed */
9949 ret = tcc_compile(s1);9998 ret = tcc_compile(s1);
9950 } else 9999 } else
@@ -10264,7 +10313,7 @@ static int64_t getclock_us(void) · 313 unmodified lines
10264 10313
10265void help(void)10314void 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 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"10317 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10269 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"10318 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
10270 " [infile1 infile2...] [-run infile args...]\n"10319 " [infile1 infile2...] [-run infile args...]\n"
@@ -10280,6 +10329,7 @@ void help(void) · 9 unmodified lines
10280 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"10329 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10281 " -w disable all warnings\n"10330 " -w disable all warnings\n"
10282 "Preprocessor options:\n"10331 "Preprocessor options:\n"
10332 " -E preprocess only\n"
10283 " -Idir add include path 'dir'\n"10333 " -Idir add include path 'dir'\n"
10284 " -Dsym[=val] define 'sym' with value 'val'\n"10334 " -Dsym[=val] define 'sym' with value 'val'\n"
10285 " -Usym undefine 'sym'\n"10335 " -Usym undefine 'sym'\n"
@@ -10338,6 +10388,7 @@ enum { · 52 unmodified lines
10338 TCC_OPTION_v,10388 TCC_OPTION_v,
10339 TCC_OPTION_w,10389 TCC_OPTION_w,
10340 TCC_OPTION_pipe,10390 TCC_OPTION_pipe,
10391 TCC_OPTION_E,
10341};10392};
10342 10393
10343static const TCCOption tcc_options[] = {10394static const TCCOption tcc_options[] = {
@@ -10373,6 +10424,7 @@ static const TCCOption tcc_options[] = { · 29 unmodified lines
10373 { "v", TCC_OPTION_v, 0 },10424 { "v", TCC_OPTION_v, 0 },
10374 { "w", TCC_OPTION_w, 0 },10425 { "w", TCC_OPTION_w, 0 },
10375 { "pipe", TCC_OPTION_pipe, 0},10426 { "pipe", TCC_OPTION_pipe, 0},
10427 { "E", TCC_OPTION_E, 0},
10376 { NULL },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 break;10655 break;
10656 case TCC_OPTION_E:
10657 output_type = TCC_OUTPUT_PREPROCESS;
10658 break;
10604 default:10659 default:
10605 if (s->warn_unsupported) {10660 if (s->warn_unsupported) {
10606 unsupported_option:10661 unsupported_option:
@@ -10657,7 +10712,7 @@ int main(int argc, char **argv) · 50 unmodified lines
10657 printf("install: %s/\n", tcc_lib_path);10712 printf("install: %s/\n", tcc_lib_path);
10658 return 0;10713 return 0;
10659 }10714 }
10660 10715
10661 nb_objfiles = nb_files - nb_libraries;10716 nb_objfiles = nb_files - nb_libraries;
10662 10717
10663 /* if outfile provided without other options, we output an10718 /* if outfile provided without other options, we output an
@@ -10674,7 +10729,16 @@ int main(int argc, char **argv) · 10 unmodified lines
10674 error("cannot specify libraries with -c");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 if (!outfile) {10742 if (!outfile) {
10679 /* compute default outfile name */10743 /* compute default outfile name */
10680 pstrcpy(objfilename, sizeof(objfilename) - 1, 10744 pstrcpy(objfilename, sizeof(objfilename) - 1,
@@ -10709,13 +10773,18 @@ int main(int argc, char **argv) · 28 unmodified lines
10709 const char *filename;10773 const char *filename;
10710 10774
10711 filename = files[i];10775 filename = files[i];
10712 if (filename[0] == '-') {10776 if (output_type == TCC_OUTPUT_PREPROCESS) {
10713 if (tcc_add_library(s, filename + 2) < 0)10777 tcc_add_file_internal(s, filename,
10714 error("cannot find %s", filename);10778 AFF_PRINT_ERROR | AFF_PREPROCESS);
10715 } else {10779 } else {
10716 if (tcc_add_file(s, filename) < 0) {10780 if (filename[0] == '-') {
10717 ret = 1;10781 if (tcc_add_library(s, filename + 2) < 0)
10718 goto the_end;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 total_bytes / total_time / 1000000.0); 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 ret = tcc_run(s, argc - optind, argv + optind);10813 ret = tcc_run(s, argc - optind, argv + optind);
10741 } else10814 } else
10742#ifdef TCC_TARGET_PE10815#ifdef TCC_TARGET_PE

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t