+ initialize_firm();
+
+ 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);
+ obstack_init(&ldflags_obst);
+ obstack_init(&file_obst);
+
+#define GET_ARG_AFTER(def, args) \
+ def = &arg[sizeof(args)-1]; \
+ if(def[0] == '\0') { \
+ ++i; \
+ if(i >= argc) { \
+ fprintf(stderr, "error: expected argument after '" args "'\n"); \
+ argument_errors = true; \
+ break; \
+ } \
+ def = argv[i]; \
+ if(def[0] == '-' && def[1] != '\0') { \
+ fprintf(stderr, "error: expected argument after '" args "'\n"); \
+ argument_errors = true; \
+ continue; \
+ } \
+ }
+
+#define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
+
+ /* early options parsing (find out optimisation level) */
+ for(int i = 1; i < argc; ++i) {
+ const char *arg = argv[i];
+ if(arg[0] != '-')
+ continue;
+
+ const char *option = &arg[1];
+ if(option[0] == 'O') {
+ sscanf(&option[1], "%d", &opt_level);
+ }
+ }
+
+ /* apply optimisation level */
+ switch(opt_level) {
+ case 0:
+ set_option("no-opt");
+ break;
+ case 1:
+ set_option("no-inline");
+ break;
+ default:
+ case 4:
+ set_option("strict-aliasing");
+ /* use_builtins = true; */
+ /* fallthrough */
+ case 3:
+ set_option("cond-eval");
+ set_option("if-conv");
+ /* fallthrough */
+ case 2:
+ set_option("inline");
+ set_option("deconv");
+ set_be_option("omitfp");
+ break;
+ }
+
+#ifdef __APPLE__
+ /* Darwin expects the stack to be aligned to 16byte boundary */
+ firm_be_option("ia32-stackalign=4");
+#endif
+
+ /* parse rest of options */
+ 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) {
+ /* an option */
+ const char *option = &arg[1];
+ if(option[0] == 'o') {
+ GET_ARG_AFTER(outname, "-o");
+ } else if(option[0] == 'g') {
+ set_be_option("debuginfo=stabs");
+ set_be_option("omitfp=no");
+ set_be_option("ia32-nooptcc=yes");
+ } else if(SINGLE_OPTION('c')) {
+ mode = CompileAssemble;
+ } else if(SINGLE_OPTION('E')) {
+ mode = PreprocessOnly;
+ } else if(SINGLE_OPTION('S')) {
+ mode = Compile;
+ } else if(option[0] == 'O') {
+ continue;
+ } else if(option[0] == 'I') {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-I");
+ add_flag(&cppflags_obst, "-I%s", opt);
+ } else if(option[0] == 'D') {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-D");
+ add_flag(&cppflags_obst, "-D%s", opt);
+ } else if(option[0] == 'U') {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-U");
+ add_flag(&cppflags_obst, "-U%s", opt);
+ } else if(option[0] == 'l') {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-l");
+ add_flag(&ldflags_obst, "-l%s", opt);
+ } else if(option[0] == 'L') {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-L");
+ add_flag(&ldflags_obst, "-L%s", opt);
+ } else if(SINGLE_OPTION('v')) {
+ verbose = 1;
+ } else if(SINGLE_OPTION('w')) {
+ inhibit_all_warnings = true;
+ } 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 (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 (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 (streq(option, "pipe")) {
+ /* here for gcc compatibility */
+ } 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 {
+ 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') {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-b");
+ int res = firm_be_option(opt);
+ if (res == 0) {
+ fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
+ opt);
+ argument_errors = true;
+ } else if (res == -1) {
+ help_displayed = true;
+ } 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 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 (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);
+ if (res == 0)
+ argument_errors = true;
+ else {
+ 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 (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 (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 (strstart(opt, "fpmath=")) {
+ GET_ARG_AFTER(opt, "-mfpmath=");
+ if (streq(opt, "387"))
+ opt = "x87";
+ else if (streq(opt, "sse"))
+ opt = "sse2";
+ else {
+ fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
+ argument_errors = true;
+ }
+ if(!argument_errors) {
+ snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
+ int res = firm_be_option(arch_opt);
+ if (res == 0)
+ argument_errors = true;
+ }
+ } 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 (streq(opt, "omit-leaf-frame-pointer")) {
+ set_be_option("omitleaffp=1");
+ } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
+ set_be_option("omitleaffp=0");
+ } else {
+ char *endptr;
+ long int value = strtol(opt, &endptr, 10);
+ if (*endptr != '\0') {
+ fprintf(stderr, "error: wrong option '-m %s'\n", opt);
+ argument_errors = true;
+ }
+ if (value != 16 && value != 32 && value != 64) {
+ fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
+ argument_errors = true;
+ } else {
+ machine_size = (unsigned int)value;
+ }
+ }
+ } else if (streq(option, "pg")) {
+ set_be_option("gprof");
+ add_flag(&ldflags_obst, "-pg");
+ } else if (streq(option, "pedantic") ||
+ streq(option, "ansi")) {
+ fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
+ } else if (streq(option, "shared")) {
+ add_flag(&ldflags_obst, "-shared");
+ } else if (strstart(option, "std=")) {
+ if (streq(&option[4], "c99")) {
+ c_mode = _C89|_C99;
+ } else if (streq(&option[4], "c89")) {
+ c_mode = _C89;
+ } else if (streq(&option[4], "gnu99")) {
+ c_mode = _C89|_C99|_GNUC;
+ } else if (streq(&option[4], "microsoft")) {
+ c_mode = _C89|_C99|_MS;
+ } else
+ fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
+ } else if (streq(option, "version")) {
+ print_cparser_version();
+ } else if (option[0] == '-') {
+ /* double dash option */
+ ++option;
+ if (streq(option, "gcc")) {
+ c_mode |= _GNUC;
+ } else if (streq(option, "no-gcc")) {
+ c_mode &= ~_GNUC;
+ } else if (streq(option, "ms")) {
+ c_mode |= _MS;
+ } else if (streq(option, "no-ms")) {
+ c_mode &= ~_MS;
+ } else if (streq(option, "signed-chars")) {
+ char_is_signed = true;
+ } else if (streq(option, "unsigned-chars")) {
+ char_is_signed = false;
+ } else if (streq(option, "strict")) {
+ strict_mode = true;
+ } else if (streq(option, "lextest")) {
+ mode = LexTest;
+ } else if (streq(option, "benchmark")) {
+ mode = BenchmarkParser;
+ } else if (streq(option, "print-ast")) {
+ mode = PrintAst;
+ } else if (streq(option, "print-implicit-cast")) {
+ print_implicit_casts = true;
+ } else if (streq(option, "print-parenthesis")) {
+ print_parenthesis = true;
+ } else if (streq(option, "print-fluffy")) {
+ mode = PrintFluffy;
+ } else if (streq(option, "print-caml")) {
+ mode = PrintCaml;
+ } else if (streq(option, "version")) {
+ print_cparser_version();
+ exit(EXIT_SUCCESS);
+ } else if (streq(option, "dump-function")) {
+ ++i;
+ if(i >= argc) {
+ fprintf(stderr, "error: "
+ "expected argument after '--dump-function'\n");
+ argument_errors = true;
+ break;
+ }
+ dumpfunction = argv[i];
+ mode = CompileDump;
+ } else {
+ fprintf(stderr, "error: unknown argument '%s'\n", arg);
+ argument_errors = true;
+ }
+ } else {
+ fprintf(stderr, "error: unknown argument '%s'\n", arg);
+ argument_errors = true;
+ }
+ } else {
+ filetype_t type = forced_filetype;
+ const char *filename = arg;
+ 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;
+ }
+ }
+
+ 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;
+ }
+ }
+
+ if (files == NULL) {
+ fprintf(stderr, "error: no input files specified\n");
+ argument_errors = true;
+ }
+
+ if (help_displayed) {
+ return !argument_errors;
+ }
+ if (argument_errors) {
+ usage(argv[0]);
+ return 1;
+ }
+
+ /* we do the lowering in ast2firm */
+ firm_opt.lower_bitfields = FALSE;
+
+ gen_firm_init();