+ if (arg[0] != '-')
+ continue;
+
+ const char *option = &arg[1];
+ if (option[0] == 'O') {
+ sscanf(&option[1], "%d", &opt_level);
+ }
+ }
+
+ const char *target = getenv("TARGET");
+ if (target != NULL)
+ parse_target_triple(target);
+ if (target_machine == NULL) {
+ target_machine = firm_get_host_machine();
+ }
+ choose_optimization_pack(opt_level);
+ setup_target_machine();
+
+ /* parse rest of options */
+ standard = STANDARD_DEFAULT;
+ unsigned features_on = 0;
+ unsigned features_off = 0;
+ filetype_t forced_filetype = FILETYPE_AUTODETECT;
+ help_sections_t help = HELP_NONE;
+ 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')) {
+ add_flag(&ldflags_obst, "-s");
+ } 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')) {
+ add_flag(&cppflags_obst, "-w");
+ disable_all_warnings();
+ } 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, "include")) {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-include");
+ add_flag(&cppflags_obst, "-include");
+ add_flag(&cppflags_obst, "%s", opt);
+ } else if (streq(option, "isystem")) {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-isystem");
+ add_flag(&cppflags_obst, "-isystem");
+ add_flag(&cppflags_obst, "%s", opt);
+#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
+ } else if (streq(option, "pthread")) {
+ /* set flags for the preprocessor */
+ add_flag(&cppflags_obst, "-D_REENTRANT");
+ /* set flags for the linker */
+ add_flag(&ldflags_obst, "-lpthread");
+#endif
+ } else if (streq(option, "nostdinc")
+ || streq(option, "trigraphs")) {
+ /* pass these through to the preprocessor */
+ add_flag(&cppflags_obst, "%s", arg);
+ } else if (streq(option, "pipe")) {
+ /* here for gcc compatibility */
+ } else if (streq(option, "static")) {
+ add_flag(&ldflags_obst, "-static");
+ } else if (streq(option, "shared")) {
+ add_flag(&ldflags_obst, "-shared");
+ } else if (option[0] == 'f') {
+ char const *orig_opt;
+ GET_ARG_AFTER(orig_opt, "-f");
+
+ if (strstart(orig_opt, "input-charset=")) {
+ char const* const encoding = strchr(orig_opt, '=') + 1;
+ input_encoding = encoding;
+ } else 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 if (strstart(orig_opt, "visibility=")) {
+ const char *val = strchr(orig_opt, '=')+1;
+ elf_visibility_tag_t visibility
+ = get_elf_visibility_from_string(val);
+ if (visibility == ELF_VISIBILITY_ERROR) {
+ fprintf(stderr, "invalid visibility '%s' specified\n",
+ val);
+ argument_errors = true;
+ } else {
+ set_default_visibility(visibility);
+ }
+ } else if (strstart(orig_opt, "message-length=")) {
+ /* ignore: would only affect error message format */
+ } else if (streq(orig_opt, "fast-math") ||
+ streq(orig_opt, "fp-fast")) {
+ firm_fp_model = fp_model_fast;
+ } else if (streq(orig_opt, "fp-precise")) {
+ firm_fp_model = fp_model_precise;
+ } else if (streq(orig_opt, "fp-strict")) {
+ firm_fp_model = fp_model_strict;
+ } else if (streq(orig_opt, "help")) {
+ fprintf(stderr, "warning: -fhelp is deprecated\n");
+ help |= HELP_OPTIMIZATION;
+ } else {
+ /* -f options which have an -fno- variant */
+ 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, "diagnostics-show-option")) {
+ diagnostics_show_option = truth_value;
+ } else if (streq(opt, "dollars-in-identifiers")) {
+ allow_dollar_in_symbol = truth_value;
+ } else if (streq(opt, "omit-frame-pointer")) {
+ set_be_option(truth_value ? "omitfp" : "omitfp=no");
+ } else if (streq(opt, "short-wchar")) {
+ wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
+ : ATOMIC_TYPE_INT;
+ } else if (streq(opt, "show-column")) {
+ show_column = truth_value;
+ } else if (streq(opt, "signed-char")) {
+ char_is_signed = truth_value;
+ } else if (streq(opt, "strength-reduce")) {
+ /* does nothing, for gcc compatibility (even gcc does
+ * nothing for this switch anymore) */
+ } else if (streq(opt, "syntax-only")) {
+ mode = truth_value ? ParseOnly : CompileAssembleLink;
+ } else if (streq(opt, "unsigned-char")) {
+ char_is_signed = !truth_value;
+ } else if (streq(opt, "freestanding")) {
+ freestanding = truth_value;
+ } else if (streq(opt, "hosted")) {
+ freestanding = !truth_value;
+ } else if (streq(opt, "profile-generate")) {
+ profile_generate = truth_value;
+ } else if (streq(opt, "profile-use")) {
+ profile_use = truth_value;
+ } else if (!truth_value &&
+ streq(opt, "asynchronous-unwind-tables")) {
+ /* nothing todo, a gcc feature which we do not support
+ * anyway was deactivated */
+ } else if (streq(opt, "verbose-asm")) {
+ /* ignore: we always print verbose assembler */
+ } else if (streq(opt, "jump-tables") ||
+ streq(opt, "expensive-optimizations") ||
+ streq(opt, "common") ||
+ streq(opt, "optimize-sibling-calls") ||
+ streq(opt, "align-loops") ||
+ streq(opt, "align-jumps") ||
+ streq(opt, "align-functions") ||
+ streq(opt, "PIC") ||
+ streq(opt, "stack-protector") ||
+ streq(opt, "stack-protector-all")) {
+ 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 (option[0] == 'b') {
+ const char *opt;
+ GET_ARG_AFTER(opt, "-b");
+
+ if (streq(opt, "help")) {
+ fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
+ help |= HELP_FIRM;
+ } else {
+ int res = be_parse_arg(opt);
+ if (res == 0) {
+ fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
+ opt);
+ argument_errors = true;
+ } else if (strstart(opt, "isa=")) {
+ strncpy(cpu_arch, opt, sizeof(cpu_arch));
+ }
+ }
+ } else if (option[0] == 'W') {
+ if (strstart(option + 1, "p,")) {
+ // pass options directly to the preprocessor
+ const char *opt;
+ GET_ARG_AFTER(opt, "-Wp,");
+ add_flag(&cppflags_obst, "-Wp,%s", opt);
+ } else if (strstart(option + 1, "l,")) {
+ // pass options directly to the linker
+ const char *opt;
+ GET_ARG_AFTER(opt, "-Wl,");
+ add_flag(&ldflags_obst, "-Wl,%s", opt);
+ } else if (streq(option + 1, "no-trigraphs")
+ || streq(option + 1, "undef")
+ || streq(option + 1, "missing-include-dirs")
+ || streq(option + 1, "endif-labels")) {
+ add_flag(&cppflags_obst, "%s", arg);
+ } else if (streq(option+1, "init-self")) {
+ /* ignored (same as gcc does) */
+ } else if (streq(option+1, "format-y2k")
+ || streq(option+1, "format-security")
+ || streq(option+1, "old-style-declaration")
+ || streq(option+1, "type-limits")) {
+ /* ignore (gcc compatibility) */
+ } 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, "target=")) {
+ GET_ARG_AFTER(opt, "-mtarget=");
+ if (!parse_target_triple(opt)) {
+ argument_errors = true;
+ } else {
+ const char *isa = setup_target_machine();
+ strncpy(cpu_arch, isa, sizeof(cpu_arch));
+ target_triple = opt;
+ }
+ } else if (strstart(opt, "triple=")) {
+ GET_ARG_AFTER(opt, "-mtriple=");
+ if (!parse_target_triple(opt)) {
+ argument_errors = true;
+ } else {
+ const char *isa = setup_target_machine();
+ strncpy(cpu_arch, isa, sizeof(cpu_arch));
+ target_triple = opt;
+ }
+ } else if (strstart(opt, "arch=")) {
+ GET_ARG_AFTER(opt, "-march=");
+ snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
+ int res = be_parse_arg(arch_opt);
+ snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
+ res &= be_parse_arg(arch_opt);
+
+ if (res == 0) {
+ fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
+ 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 = be_parse_arg(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 = be_parse_arg(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 -mfpmath 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 = be_parse_arg(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 = be_parse_arg(arch_opt);
+ if (res == 0)
+ argument_errors = true;
+ } else if (streq(opt, "rtd")) {
+ default_calling_convention = CC_STDCALL;
+ } else if (strstart(opt, "regparm=")) {
+ fprintf(stderr, "error: regparm convention not supported yet\n");
+ argument_errors = true;
+ } else if (streq(opt, "soft-float")) {
+ add_flag(&ldflags_obst, "-msoft-float");
+ snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
+ int res = be_parse_arg(arch_opt);
+ if (res == 0)
+ argument_errors = true;
+ } else {
+ long int value = strtol(opt, NULL, 10);
+ if (value == 0) {
+ fprintf(stderr, "error: wrong option '-m %s'\n", opt);
+ argument_errors = true;
+ } else if (value != 16 && value != 32 && value != 64) {
+ fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
+ argument_errors = true;
+ } else {
+ unsigned machine_size = (unsigned)value;
+ /* TODO: choose/change backend based on this */
+ add_flag(&cppflags_obst, "-m%u", machine_size);
+ add_flag(&asflags_obst, "-m%u", machine_size);
+ add_flag(&ldflags_obst, "-m%u", machine_size);
+ }
+ }
+ } 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 (strstart(option, "std=")) {
+ const char *const o = &option[4];
+ standard =
+ streq(o, "c++") ? STANDARD_CXX98 :
+ streq(o, "c++98") ? STANDARD_CXX98 :
+ streq(o, "c89") ? STANDARD_C89 :
+ streq(o, "c99") ? STANDARD_C99 :
+ streq(o, "c9x") ? STANDARD_C99 : // deprecated
+ streq(o, "gnu++98") ? STANDARD_GNUXX98 :
+ streq(o, "gnu89") ? STANDARD_GNU89 :
+ streq(o, "gnu99") ? STANDARD_GNU99 :
+ streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
+ streq(o, "iso9899:1990") ? STANDARD_C89 :
+ streq(o, "iso9899:199409") ? STANDARD_C90 :
+ streq(o, "iso9899:1999") ? STANDARD_C99 :
+ streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
+ (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
+ } else if (streq(option, "version")) {
+ print_cparser_version();
+ } else if (strstart(option, "print-file-name=")) {
+ GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
+ } else if (option[0] == '-') {
+ /* double dash option */
+ ++option;
+ if (streq(option, "gcc")) {
+ features_on |= _GNUC;
+ features_off &= ~_GNUC;
+ } else if (streq(option, "no-gcc")) {
+ features_on &= ~_GNUC;
+ features_off |= _GNUC;
+ } else if (streq(option, "ms")) {
+ features_on |= _MS;
+ features_off &= ~_MS;
+ } else if (streq(option, "no-ms")) {
+ features_on &= ~_MS;
+ features_off |= _MS;
+ } 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-jna")) {
+ mode = PrintJna;
+ } else if (streq(option, "jna-limit")) {
+ ++i;
+ if (i >= argc) {
+ fprintf(stderr, "error: "
+ "expected argument after '--jna-limit'\n");
+ argument_errors = true;
+ break;
+ }
+ jna_limit_output(argv[i]);
+ } else if (streq(option, "jna-libname")) {
+ ++i;
+ if (i >= argc) {
+ fprintf(stderr, "error: "
+ "expected argument after '--jna-libname'\n");
+ argument_errors = true;
+ break;
+ }
+ jna_set_libname(argv[i]);
+ } else if (streq(option, "time")) {
+ do_timing = true;
+ } else if (streq(option, "version")) {
+ print_cparser_version();
+ return EXIT_SUCCESS;
+ } else if (streq(option, "help")) {
+ help |= HELP_BASIC;
+ } else if (streq(option, "help-parser")) {
+ help |= HELP_PARSER;
+ } else if (streq(option, "help-warnings")) {
+ help |= HELP_WARNINGS;
+ } else if (streq(option, "help-codegen")) {
+ help |= HELP_CODEGEN;
+ } else if (streq(option, "help-linker")) {
+ help |= HELP_LINKER;
+ } else if (streq(option, "help-optimization")) {
+ help |= HELP_OPTIMIZATION;
+ } else if (streq(option, "help-language-tools")) {
+ help |= HELP_LANGUAGETOOLS;
+ } else if (streq(option, "help-debug")) {
+ help |= HELP_DEBUG;
+ } else if (streq(option, "help-firm")) {
+ help |= HELP_FIRM;
+ } else if (streq(option, "help-all")) {
+ help |= HELP_ALL;
+ } 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 if (streq(option, "export-ir")) {
+ mode = CompileExportIR;
+ } else {
+ fprintf(stderr, "error: unknown argument '%s'\n", arg);
+ argument_errors = true;
+ }
+ } else {
+ fprintf(stderr, "error: unknown argument '%s'\n", arg);
+ argument_errors = true;