X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=main.c;h=1dd4976e27a1b474c19fb071cb8c8bf1dc857ed8;hb=27f4ce3da8d78ff0e70afde834793e0ebd81a42a;hp=2b1146cb96df4a518d6fa7f7e6daef1909300646;hpb=faa3eadfc670c10f7cd5e5a2c61ea2192fabd090;p=cparser diff --git a/main.c b/main.c index 2b1146c..1dd4976 100644 --- a/main.c +++ b/main.c @@ -80,7 +80,11 @@ #include "warning.h" #ifndef PREPROCESSOR -#define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=__SIZE_TYPE__ -m32" +#ifdef __APPLE__ +#define PREPROCESSOR "gcc -E -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=unsigned\\ long -m32 -U__STRICT_ANSI__" +#else +#define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -U__SIZE_TYPE__ -D__SIZE_TYPE__=unsigned\\ long -m32 -U__STRICT_ANSI__" +#endif #endif #ifndef LINKER @@ -88,8 +92,12 @@ #endif #ifndef ASSEMBLER +#ifdef __APPLE__ +#define ASSEMBLER "gcc -c -xassembler" +#else #define ASSEMBLER "as --32" #endif +#endif /** The current c mode/dialect. */ unsigned int c_mode = _C89|_C99|_GNUC; @@ -106,18 +114,24 @@ bool strict_mode = false; /** use builtins for some libc functions */ bool use_builtins = false; +/** we have extern function with const attribute. */ +bool have_const_functions = false; + /* to switch on printing of implicit casts */ extern bool print_implicit_casts; /* to switch on printing of parenthesis to indicate operator precedence */ extern bool print_parenthesis; -static int verbose; -static struct obstack cppflags_obst, ldflags_obst; +static int verbose; +static struct obstack cppflags_obst, ldflags_obst; +static char dep_target[1024]; +static const char *outname; typedef struct file_list_entry_t file_list_entry_t; typedef enum filetype_t { + FILETYPE_AUTODETECT, FILETYPE_C, FILETYPE_PREPROCESSED_C, FILETYPE_ASSEMBLER, @@ -252,11 +266,25 @@ static void add_flag(struct obstack *obst, const char *format, ...) static FILE *preprocess(const char *fname) { - char buf[4096]; obstack_1grow(&cppflags_obst, '\0'); const char *flags = obstack_finish(&cppflags_obst); - snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname); + obstack_printf(&cppflags_obst, "%s", PREPROCESSOR); + if (dep_target[0] != '\0') { + add_flag(&cppflags_obst, "-MF"); + add_flag(&cppflags_obst, "%s", dep_target); + if (outname != NULL) { + add_flag(&cppflags_obst, "-MQ"); + add_flag(&cppflags_obst, "%s", outname); + } + } + if (flags[0] != '\0') { + obstack_printf(&cppflags_obst, " %s", flags); + } + add_flag(&cppflags_obst, "%s", fname); + + obstack_1grow(&cppflags_obst, '\0'); + const char *buf = obstack_finish(&cppflags_obst); if(verbose) { puts(buf); } @@ -266,6 +294,7 @@ static FILE *preprocess(const char *fname) fprintf(stderr, "invoking preprocessor failed\n"); exit(1); } + return f; } @@ -437,9 +466,23 @@ static void copy_file(FILE *dest, FILE *input) } } +static inline bool streq(char const* a, char const* b) +{ + return strcmp(a, b) == 0; +} + +static inline bool strstart(char const* str, char const* start) +{ + do { + if (*start == '\0') + return true; + } while (*str++ == *start++); + return false; +} + static FILE *open_file(const char *filename) { - if (strcmp(filename, "-") == 0) { + if (streq(filename, "-")) { return stdin; } @@ -453,18 +496,32 @@ static FILE *open_file(const char *filename) return in; } +static filetype_t get_filetype_from_string(const char *string) +{ + if (streq(string, "c") || streq(string, "c-header")) + return FILETYPE_C; + if (streq(string, "assembler")) + return FILETYPE_PREPROCESSED_ASSEMBLER; + if (streq(string, "assembler-with-cpp")) + return FILETYPE_ASSEMBLER; + if (streq(string, "none")) + return FILETYPE_AUTODETECT; + + return FILETYPE_UNKNOWN; +} + int main(int argc, char **argv) { initialize_firm(); - const char *outname = NULL; - const char *dumpfunction = NULL; - compile_mode_t mode = CompileAssembleLink; - int opt_level = 1; - int result = EXIT_SUCCESS; - char cpu_arch[16] = "ia32"; - file_list_entry_t *files = NULL; - file_list_entry_t *last_file = NULL; + const char *dumpfunction = NULL; + compile_mode_t mode = CompileAssembleLink; + int opt_level = 1; + int result = EXIT_SUCCESS; + char cpu_arch[16] = "ia32"; + file_list_entry_t *files = NULL; + file_list_entry_t *last_file = NULL; + bool construct_dep_target = false; struct obstack file_obst; obstack_init(&cppflags_obst); @@ -513,11 +570,11 @@ int main(int argc, char **argv) default: case 4: set_option("strict-aliasing"); + /* use_builtins = true; */ /* fallthrough */ case 3: set_option("cond-eval"); set_option("if-conv"); - use_builtins = true; /* fallthrough */ case 2: set_option("inline"); @@ -526,9 +583,15 @@ int main(int argc, char **argv) break; } +#ifdef __APPLE__ + /* Darwin expects the stack to be aligned to 16byte boundary */ + firm_be_option("ia32-stackalign=4"); +#endif + /* parse rest of options */ - bool help_displayed = false; - bool argument_errors = false; + filetype_t forced_filetype = FILETYPE_AUTODETECT; + bool help_displayed = false; + bool argument_errors = false; for(int i = 1; i < argc; ++i) { const char *arg = argv[i]; if(arg[0] == '-' && arg[1] != 0) { @@ -572,54 +635,84 @@ int main(int argc, char **argv) verbose = 1; } else if(SINGLE_OPTION('w')) { inhibit_all_warnings = true; - } else if(strcmp(option, "M") == 0) { + } else if(option[0] == 'x') { + const char *opt; + GET_ARG_AFTER(opt, "-x"); + forced_filetype = get_filetype_from_string(opt); + if (forced_filetype == FILETYPE_UNKNOWN) { + fprintf(stderr, "Unknown language '%s'\n", opt); + argument_errors = true; + } + } else if (streq(option, "M")) { mode = PreprocessOnly; add_flag(&cppflags_obst, "-M"); - } else if(strcmp(option, "MMD") == 0 - || strcmp(option, "MD") == 0 - || strcmp(option, "MM") == 0) { + } else if (streq(option, "MMD") || + streq(option, "MD")) { + construct_dep_target = true; + add_flag(&cppflags_obst, "-%s", option); + } else if(streq(option, "MM") || + streq(option, "MP")) { add_flag(&cppflags_obst, "-%s", option); - } else if(strcmp(option, "MT") == 0 - || strcmp(option, "MQ") == 0 - || strcmp(option, "MF") == 0) { + } else if (streq(option, "MT") || + streq(option, "MQ") || + streq(option, "MF")) { const char *opt; GET_ARG_AFTER(opt, "-MT"); add_flag(&cppflags_obst, "-%s", option); add_flag(&cppflags_obst, "%s", opt); - } else if(strcmp(option, "pipe") == 0) { + } else if (streq(option, "pipe")) { /* here for gcc compatibility */ - } else if(option[0] == 'f') { - const char *opt; - GET_ARG_AFTER(opt, "-f"); - - if(strcmp(opt, "syntax-only") == 0) { - mode = ParseOnly; - } else if(strcmp(opt, "omit-frame-pointer") == 0) { - set_be_option("omitfp"); - } else if(strcmp(opt, "no-omit-frame-pointer") == 0) { - set_be_option("omitfp=no"); - } else if(strcmp(opt, "strength-reduce") == 0) { - firm_option("strength-red"); - } else if(strcmp(opt, "fast-math") == 0 - || strcmp(opt, "unroll-loops") == 0 - || strcmp(opt, "expensive-optimizations") == 0 - || strcmp(opt, "no-common") == 0 - || strncmp(opt, "align-loops=", sizeof("align-loops=")-1) == 0 - || strncmp(opt, "align-jumps=", sizeof("align-jumps=")-1) == 0 - || strncmp(opt, "align-functions=", sizeof("align-functions=")-1) == 0) { - fprintf(stderr, "ignoring gcc option '-f %s'\n", opt); + } else if (option[0] == 'f') { + char const *orig_opt; + GET_ARG_AFTER(orig_opt, "-f"); + + if (strstart(orig_opt, "align-loops=") || + strstart(orig_opt, "align-jumps=") || + strstart(orig_opt, "align-functions=")) { + fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt); } else { - int res = firm_option(opt); - if (res == 0) { - fprintf(stderr, "error: unknown Firm option '-f %s'\n", - opt); - argument_errors = true; - continue; - } else if (res == -1) { - help_displayed = true; + char const *opt = orig_opt; + bool truth_value = true; + if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') { + truth_value = false; + opt += 3; + } + + if (streq(opt, "dollars-in-identifiers")) { + allow_dollar_in_symbol = truth_value; + } if (streq(opt, "builtins")) { + use_builtins = truth_value; + } else if (streq(opt, "short-wchar")) { + opt_short_wchar_t = truth_value; + } else if (streq(opt, "syntax-only")) { + mode = truth_value ? ParseOnly : CompileAssembleLink; + } else if (streq(opt, "omit-frame-pointer")) { + set_be_option(truth_value ? "omitfp" : "omitfp=no"); + } else if (streq(opt, "strength-reduce")) { + firm_option(truth_value ? "strength-red" : "no-strength-red"); + } else if (streq(opt, "fast-math") || + streq(opt, "jump-tables") || + streq(opt, "unroll-loops") || + streq(opt, "expensive-optimizations") || + streq(opt, "common") || + streq(opt, "PIC") || + streq(opt, "align-loops") || + streq(opt, "align-jumps") || + streq(opt, "align-functions")) { + fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt); + } else { + int res = firm_option(orig_opt); + if (res == 0) { + fprintf(stderr, "error: unknown Firm option '-f%s'\n", + orig_opt); + argument_errors = true; + continue; + } else if (res == -1) { + help_displayed = true; + } } } - } else if(option[0] == 'b') { + } else if (option[0] == 'b') { const char *opt; GET_ARG_AFTER(opt, "-b"); int res = firm_be_option(opt); @@ -629,19 +722,24 @@ int main(int argc, char **argv) argument_errors = true; } else if (res == -1) { help_displayed = true; - } else { - if (strncmp(opt, "isa=", 4) == 0) - strncpy(cpu_arch, opt, sizeof(cpu_arch)); + } else if (strstart(opt, "isa=")) { + strncpy(cpu_arch, opt, sizeof(cpu_arch)); + } + } else if (option[0] == 'W') { + if (strstart(option + 1, "l,")) // a gcc-style linker option + { + const char *opt; + GET_ARG_AFTER(opt, "-Wl,"); + add_flag(&ldflags_obst, "-Wl,%s", opt); } - } else if(option[0] == 'W') { - set_warning_opt(&option[1]); + else set_warning_opt(&option[1]); } else if(option[0] == 'm') { /* -m options */ const char *opt; char arch_opt[64]; GET_ARG_AFTER(opt, "-m"); - if(strncmp(opt, "arch=", 5) == 0) { + if (strstart(opt, "arch=")) { GET_ARG_AFTER(opt, "-march="); snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt); int res = firm_be_option(arch_opt); @@ -653,23 +751,23 @@ int main(int argc, char **argv) if (res == 0) argument_errors = true; } - } else if(strncmp(opt, "tune=", 5) == 0) { + } else if (strstart(opt, "tune=")) { GET_ARG_AFTER(opt, "-mtune="); snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt); int res = firm_be_option(arch_opt); if (res == 0) argument_errors = true; - } else if(strncmp(opt, "cpu=", 4) == 0) { + } else if (strstart(opt, "cpu=")) { GET_ARG_AFTER(opt, "-mcpu="); snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt); int res = firm_be_option(arch_opt); if (res == 0) argument_errors = true; - } else if(strncmp(opt, "fpmath=", 7) == 0) { + } else if (strstart(opt, "fpmath=")) { GET_ARG_AFTER(opt, "-mfpmath="); - if(strcmp(opt, "387") == 0) + if (streq(opt, "387")) opt = "x87"; - else if(strcmp(opt, "sse") == 0) + else if (streq(opt, "sse")) opt = "sse2"; else { fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n"); @@ -681,15 +779,15 @@ int main(int argc, char **argv) if (res == 0) argument_errors = true; } - } else if(strncmp(opt, "preferred-stack-boundary=", 25) == 0) { + } else if (strstart(opt, "preferred-stack-boundary=")) { GET_ARG_AFTER(opt, "-mpreferred-stack-boundary="); snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt); int res = firm_be_option(arch_opt); if (res == 0) argument_errors = true; - } else if(strcmp(opt, "omit-leaf-frame-pointer") == 0) { + } else if (streq(opt, "omit-leaf-frame-pointer")) { set_be_option("omitleaffp=1"); - } else if(strcmp(opt, "no-omit-leaf-frame-pointer") == 0) { + } else if (streq(opt, "no-omit-leaf-frame-pointer")) { set_be_option("omitleaffp=0"); } else { char *endptr; @@ -705,61 +803,62 @@ int main(int argc, char **argv) machine_size = (unsigned int)value; } } - } else if(strcmp(option, "pg") == 0) { + } else if (streq(option, "pg")) { set_be_option("gprof"); add_flag(&ldflags_obst, "-pg"); - } else if(strcmp(option, "pedantic") == 0) { + } else if (streq(option, "pedantic") || + streq(option, "ansi")) { fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg); - } else if(strcmp(option, "shared") == 0) { + } else if (streq(option, "shared")) { add_flag(&ldflags_obst, "-shared"); - } else if(strncmp(option, "std=", 4) == 0) { - if(strcmp(&option[4], "c99") == 0) { + } else if (strstart(option, "std=")) { + if (streq(&option[4], "c99")) { c_mode = _C89|_C99; - } else if(strcmp(&option[4], "c89") == 0) { + } else if (streq(&option[4], "c89")) { c_mode = _C89; - } else if(strcmp(&option[4], "gnu99") == 0) { + } else if (streq(&option[4], "gnu99")) { c_mode = _C89|_C99|_GNUC; - } else if(strcmp(&option[4], "microsoft") == 0) { + } else if (streq(&option[4], "microsoft")) { c_mode = _C89|_C99|_MS; } else fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg); - } else if(strcmp(option, "version") == 0) { + } else if (streq(option, "version")) { print_cparser_version(); } else if (option[0] == '-') { /* double dash option */ ++option; - if(strcmp(option, "gcc") == 0) { + if (streq(option, "gcc")) { c_mode |= _GNUC; - } else if(strcmp(option, "no-gcc") == 0) { + } else if (streq(option, "no-gcc")) { c_mode &= ~_GNUC; - } else if(strcmp(option, "ms") == 0) { + } else if (streq(option, "ms")) { c_mode |= _MS; - } else if(strcmp(option, "no-ms") == 0) { + } else if (streq(option, "no-ms")) { c_mode &= ~_MS; - } else if(strcmp(option, "signed-chars") == 0) { + } else if (streq(option, "signed-chars")) { char_is_signed = true; - } else if(strcmp(option, "unsigned-chars") == 0) { + } else if (streq(option, "unsigned-chars")) { char_is_signed = false; - } else if(strcmp(option, "strict") == 0) { + } else if (streq(option, "strict")) { strict_mode = true; - } else if(strcmp(option, "lextest") == 0) { + } else if (streq(option, "lextest")) { mode = LexTest; - } else if(strcmp(option, "benchmark") == 0) { + } else if (streq(option, "benchmark")) { mode = BenchmarkParser; - } else if(strcmp(option, "print-ast") == 0) { + } else if (streq(option, "print-ast")) { mode = PrintAst; - } else if(strcmp(option, "print-implicit-cast") == 0) { + } else if (streq(option, "print-implicit-cast")) { print_implicit_casts = true; - } else if(strcmp(option, "print-parenthesis") == 0) { + } else if (streq(option, "print-parenthesis")) { print_parenthesis = true; - } else if(strcmp(option, "print-fluffy") == 0) { + } else if (streq(option, "print-fluffy")) { mode = PrintFluffy; - } else if(strcmp(option, "print-caml") == 0) { + } else if (streq(option, "print-caml")) { mode = PrintCaml; - } else if(strcmp(option, "version") == 0) { + } else if (streq(option, "version")) { print_cparser_version(); exit(EXIT_SUCCESS); - } else if(strcmp(option, "dump-function") == 0) { + } else if (streq(option, "dump-function")) { ++i; if(i >= argc) { fprintf(stderr, "error: " @@ -778,39 +877,48 @@ int main(int argc, char **argv) argument_errors = true; } } else { - filetype_t type = FILETYPE_UNKNOWN; - size_t len = strlen(arg); + filetype_t type = forced_filetype; const char *filename = arg; - if (len < 2 && arg[0] == '-') { - /* - implicitely means C source file */ - type = FILETYPE_C; - filename = NULL; - } else if (len > 2 && arg[len-2] == '.') { - switch(arg[len-1]) { - case 'c': type = FILETYPE_C; break; - case 'h': type = FILETYPE_C; break; - case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break; - case 'S': type = FILETYPE_ASSEMBLER; break; - case 'o': type = FILETYPE_OBJECT; break; + if (type == FILETYPE_AUTODETECT) { + size_t const len = strlen(arg); + if (len < 2 && arg[0] == '-') { + /* - implicitly means C source file */ + type = FILETYPE_C; + filename = NULL; + } else if (len > 2 && arg[len - 2] == '.') { + switch (arg[len - 1]) { + case 'c': type = FILETYPE_C; break; + case 'h': type = FILETYPE_C; break; + case 's': type = FILETYPE_PREPROCESSED_ASSEMBLER; break; + case 'S': type = FILETYPE_ASSEMBLER; break; + + case 'a': + case 'o': type = FILETYPE_OBJECT; break; + } + } else if (len > 3 && arg[len - 3] == '.') { + if (streq(arg + len - 2, "so")) { + type = FILETYPE_OBJECT; + } + } + + if (type == FILETYPE_AUTODETECT) { + fprintf(stderr, "'%s': file format not recognized\n", arg); + continue; } } - if (type == FILETYPE_UNKNOWN) { - fprintf(stderr, "'%s': file format not recognized\n", arg); + file_list_entry_t *entry + = obstack_alloc(&file_obst, sizeof(entry[0])); + memset(entry, 0, sizeof(entry[0])); + entry->name = arg; + entry->type = type; + + if (last_file != NULL) { + last_file->next = entry; } else { - file_list_entry_t *entry - = obstack_alloc(&file_obst, sizeof(entry[0])); - memset(entry, 0, sizeof(entry[0])); - entry->name = arg; - entry->type = type; - - if (last_file != NULL) { - last_file->next = entry; - } else { - files = entry; - } - last_file = entry; + files = entry; } + last_file = entry; } } @@ -841,6 +949,16 @@ int main(int argc, char **argv) init_parser(); init_ast2firm(); + if (construct_dep_target) { + if (outname != 0 && strlen(outname) >= 2) { + get_output_name(dep_target, sizeof(dep_target), outname, ".d"); + } else { + get_output_name(dep_target, sizeof(dep_target), files->name, ".d"); + } + } else { + dep_target[0] = '\0'; + } + char outnamebuf[4096]; if (outname == NULL) { const char *filename = files->name; @@ -882,7 +1000,7 @@ int main(int argc, char **argv) assert(outname != NULL); FILE *out; - if(strcmp(outname, "-") == 0) { + if (streq(outname, "-")) { out = stdout; } else { out = fopen(outname, "w"); @@ -953,7 +1071,7 @@ int main(int argc, char **argv) if (in == preprocessed_in) { int pp_result = pclose(preprocessed_in); if (pp_result != EXIT_SUCCESS) { - return pp_result; + exit(EXIT_FAILURE); } } @@ -1016,7 +1134,7 @@ int main(int argc, char **argv) } gen_firm_finish(asm_out, filename, /*c_mode=*/1, - /*firm_const_exists=*/0); + have_const_functions); if (asm_out != out) { fclose(asm_out); } @@ -1029,6 +1147,9 @@ int main(int argc, char **argv) return pp_result; } } + if(asm_out != out) { + fclose(asm_out); + } } if (mode == Compile) @@ -1063,6 +1184,9 @@ int main(int argc, char **argv) file->type = filetype; } + if (result != EXIT_SUCCESS) + return result; + /* link program file */ if(mode == CompileAssembleLink) { obstack_1grow(&ldflags_obst, '\0');