2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode) _access(fname, mode)
48 #define mktemp(tmpl) _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode) _fdopen(fd, mode)
51 #define popen(cmd, mode) _popen(cmd, mode)
52 #define pclose(file) _pclose(file)
53 #define unlink(filename) _unlink(filename)
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
66 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_cmdline.h"
74 #include "driver/firm_timing.h"
75 #include "driver/firm_machine.h"
76 #include "adt/error.h"
77 #include "adt/strutil.h"
78 #include "wrappergen/write_fluffy.h"
79 #include "wrappergen/write_jna.h"
88 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
90 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
99 #define ASSEMBLER "gcc -c -xassembler"
102 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
103 unsigned int machine_size = 32;
104 bool byte_order_big_endian = false;
105 bool char_is_signed = true;
106 bool strict_mode = false;
107 bool use_builtins = false;
108 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
109 unsigned force_long_double_size = 0;
110 bool enable_main_collect2_hack = false;
111 bool freestanding = false;
113 static machine_triple_t *target_machine;
114 static const char *target_triple;
116 static struct obstack cppflags_obst;
117 static struct obstack ldflags_obst;
118 static struct obstack asflags_obst;
119 static char dep_target[1024];
120 static const char *outname;
121 static bool define_intmax_types;
123 typedef enum lang_standard_t {
124 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
125 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
126 STANDARD_C89, /* ISO C90 (sic) */
127 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
128 STANDARD_C99, /* ISO C99 */
129 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
130 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
131 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
132 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
135 static lang_standard_t standard;
137 typedef struct file_list_entry_t file_list_entry_t;
139 typedef enum filetype_t {
142 FILETYPE_PREPROCESSED_C,
144 FILETYPE_PREPROCESSED_CXX,
146 FILETYPE_PREPROCESSED_ASSEMBLER,
152 struct file_list_entry_t {
153 const char *name; /**< filename or NULL for stdin */
155 file_list_entry_t *next;
158 static file_list_entry_t *temp_files;
160 static void get_output_name(char *buf, size_t buflen, const char *inputname,
163 if (inputname == NULL)
166 char const *const last_slash = strrchr(inputname, '/');
167 char const *const filename =
168 last_slash != NULL ? last_slash + 1 : inputname;
169 char const *const last_dot = strrchr(filename, '.');
170 char const *const name_end =
171 last_dot != NULL ? last_dot : strchr(filename, '\0');
173 int const len = snprintf(buf, buflen, "%.*s%s",
174 (int)(name_end - filename), filename, newext);
176 if (len < 0 || buflen <= (size_t)len)
178 if (buflen <= (size_t)len)
180 panic("filename too long");
183 #include "gen_builtins.h"
185 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
190 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
194 lexer_open_stream(in, input_name);
197 translation_unit_t *unit = finish_parsing();
201 static void lextest(FILE *in, const char *fname)
203 lexer_open_stream(in, fname);
206 lexer_next_preprocessing_token();
207 print_token(stdout, &lexer_token);
209 } while (lexer_token.type != T_EOF);
212 static void add_flag(struct obstack *obst, const char *format, ...)
217 va_start(ap, format);
221 vsnprintf(buf, sizeof(buf), format, ap);
224 obstack_1grow(obst, ' ');
226 obstack_1grow(obst, '"');
227 obstack_grow(obst, buf, len);
228 obstack_1grow(obst, '"');
230 /* escape stuff... */
231 for (char *c = buf; *c != '\0'; ++c) {
249 obstack_1grow(obst, '\\');
252 obstack_1grow(obst, *c);
259 static const char *type_to_string(type_t *type)
261 assert(type->kind == TYPE_ATOMIC);
262 return get_atomic_kind_name(type->atomic.akind);
265 static FILE *preprocess(const char *fname, filetype_t filetype)
267 static const char *common_flags = NULL;
269 if (common_flags == NULL) {
270 obstack_1grow(&cppflags_obst, '\0');
271 const char *flags = obstack_finish(&cppflags_obst);
273 /* setup default defines */
274 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
275 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
276 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
277 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
279 add_flag(&cppflags_obst, "-U__VERSION__");
280 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
282 if (define_intmax_types) {
283 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
284 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
285 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
286 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
289 if (flags[0] != '\0') {
290 size_t len = strlen(flags);
291 obstack_1grow(&cppflags_obst, ' ');
292 obstack_grow(&cppflags_obst, flags, len);
294 obstack_1grow(&cppflags_obst, '\0');
295 common_flags = obstack_finish(&cppflags_obst);
298 assert(obstack_object_size(&cppflags_obst) == 0);
300 const char *preprocessor = getenv("CPARSER_PP");
301 if (preprocessor != NULL) {
302 obstack_printf(&cppflags_obst, "%s ", preprocessor);
304 if (target_triple != NULL)
305 obstack_printf(&cppflags_obst, "%s-", target_triple);
306 obstack_printf(&cppflags_obst, PREPROCESSOR);
311 add_flag(&cppflags_obst, "-std=c99");
314 add_flag(&cppflags_obst, "-std=c++98");
316 case FILETYPE_ASSEMBLER:
317 add_flag(&cppflags_obst, "-x");
318 add_flag(&cppflags_obst, "assembler-with-cpp");
323 obstack_printf(&cppflags_obst, "%s", common_flags);
325 /* handle dependency generation */
326 if (dep_target[0] != '\0') {
327 add_flag(&cppflags_obst, "-MF");
328 add_flag(&cppflags_obst, dep_target);
329 if (outname != NULL) {
330 add_flag(&cppflags_obst, "-MQ");
331 add_flag(&cppflags_obst, outname);
334 add_flag(&cppflags_obst, fname);
335 obstack_1grow(&cppflags_obst, '\0');
337 char *commandline = obstack_finish(&cppflags_obst);
341 FILE *f = popen(commandline, "r");
343 fprintf(stderr, "invoking preprocessor failed\n");
346 /* we don't really need that anymore */
347 obstack_free(&cppflags_obst, commandline);
352 static void assemble(const char *out, const char *in)
354 obstack_1grow(&asflags_obst, '\0');
355 const char *flags = obstack_finish(&asflags_obst);
357 const char *assembler = getenv("CPARSER_AS");
358 if (assembler != NULL) {
359 obstack_printf(&asflags_obst, "%s", assembler);
361 if (target_triple != NULL)
362 obstack_printf(&asflags_obst, "%s-", target_triple);
363 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
365 if (flags[0] != '\0')
366 obstack_printf(&asflags_obst, " %s", flags);
368 obstack_printf(&asflags_obst, " %s -o %s", in, out);
369 obstack_1grow(&asflags_obst, '\0');
371 char *commandline = obstack_finish(&asflags_obst);
375 int err = system(commandline);
376 if (err != EXIT_SUCCESS) {
377 fprintf(stderr, "assembler reported an error\n");
380 obstack_free(&asflags_obst, commandline);
383 static void print_file_name(const char *file)
385 add_flag(&ldflags_obst, "-print-file-name=%s", file);
387 obstack_1grow(&ldflags_obst, '\0');
388 const char *flags = obstack_finish(&ldflags_obst);
390 /* construct commandline */
391 const char *linker = getenv("CPARSER_LINK");
392 if (linker != NULL) {
393 obstack_printf(&ldflags_obst, "%s ", linker);
395 if (target_triple != NULL)
396 obstack_printf(&ldflags_obst, "%s-", target_triple);
397 obstack_printf(&ldflags_obst, "%s ", LINKER);
399 obstack_printf(&ldflags_obst, "%s ", linker);
400 obstack_printf(&ldflags_obst, "%s", flags);
401 obstack_1grow(&ldflags_obst, '\0');
403 char *commandline = obstack_finish(&ldflags_obst);
407 int err = system(commandline);
408 if (err != EXIT_SUCCESS) {
409 fprintf(stderr, "linker reported an error\n");
412 obstack_free(&ldflags_obst, commandline);
415 static const char *try_dir(const char *dir)
419 if (access(dir, R_OK | W_OK | X_OK) == 0)
424 static const char *get_tempdir(void)
426 static const char *tmpdir = NULL;
432 tmpdir = try_dir(getenv("TMPDIR"));
434 tmpdir = try_dir(getenv("TMP"));
436 tmpdir = try_dir(getenv("TEMP"));
440 tmpdir = try_dir(P_tmpdir);
444 tmpdir = try_dir("/var/tmp");
446 tmpdir = try_dir("/usr/tmp");
448 tmpdir = try_dir("/tmp");
457 /* cheap and nasty mkstemp replacement */
458 static int mkstemp(char *templ)
461 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
466 * an own version of tmpnam, which: writes in a buffer, emits no warnings
467 * during linking (like glibc/gnu ld do for tmpnam)...
469 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
471 const char *tempdir = get_tempdir();
473 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
475 int fd = mkstemp(buffer);
477 fprintf(stderr, "couldn't create temporary file: %s\n",
481 FILE *out = fdopen(fd, "w");
483 fprintf(stderr, "couldn't create temporary file FILE*\n");
487 file_list_entry_t *entry = xmalloc(sizeof(*entry));
488 memset(entry, 0, sizeof(*entry));
490 size_t name_len = strlen(buffer) + 1;
491 char *name = malloc(name_len);
492 memcpy(name, buffer, name_len);
495 entry->next = temp_files;
501 static void free_temp_files(void)
503 file_list_entry_t *entry = temp_files;
504 file_list_entry_t *next;
505 for ( ; entry != NULL; entry = next) {
509 free((char*) entry->name);
515 typedef enum compile_mode_t {
530 static void usage(const char *argv0)
532 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
535 static void print_cparser_version(void)
537 printf("cparser (%s) using libFirm (%u.%u",
538 cparser_REVISION, ir_get_version_major(),
539 ir_get_version_minor());
541 const char *revision = ir_get_version_revision();
542 if (revision[0] != 0) {
544 fputs(revision, stdout);
547 const char *build = ir_get_version_build();
550 fputs(build, stdout);
553 puts("This is free software; see the source for copying conditions. There is NO\n"
554 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
557 static void print_help_basic(const char *argv0)
561 put_help("--help", "Display this information");
562 put_help("--version", "Display compiler version");
563 put_help("--help-parser", "Display information about parser options");
564 put_help("--help-warnings", "Display information about warning options");
565 put_help("--help-codegen", "Display information about code-generation options");
566 put_help("--help-linker", "Display information about linker options");
567 put_help("--help-language-tools", "Display information about language tools options");
568 put_help("--help-debug", "Display information about compiler debugging options");
569 put_help("--help-firm", "Display information about direct firm options");
570 put_help("--help-all", "Display information about all options");
571 put_help("-c", "Compile and assemble but do not link");
572 put_help("-E", "Preprocess only");
573 put_help("-S", "Compile but do not assembler or link");
574 put_help("-o", "Specify output file");
575 put_help("-v", "Verbose output (show invocation of sub-processes)");
576 put_help("-x", "Force input language:");
577 put_choice("c", "C");
578 put_choice("c++", "C++");
579 put_choice("assembler", "Assembler (no preprocessing)");
580 put_choice("assembler-with-cpp", "Assembler with preprocessing");
581 put_choice("none", "Autodetection");
582 put_help("-pipe", "ignored (gcc compatibility)");
585 static void print_help_preprocessor(void)
587 put_help("-nostdinc", "Do not search standard system include directories");
588 put_help("-trigraphs", "Support ISO C trigraphs");
589 put_help("-isystem", "");
590 put_help("-include", "");
591 put_help("-I PATH", "");
592 put_help("-D SYMBOL[=value]", "");
593 put_help("-U SYMBOL", "");
594 put_help("-Wp,OPTION", "pass option directly to preprocessor");
597 put_help("-MMD", "");
605 static void print_help_parser(void)
607 put_help("-finput-charset=CHARSET", "Select encoding of input files");
608 put_help("-fmessage-length=LEN", "ignored (gcc compatibility)");
609 put_help("-fshort-wchar", "wchar_t is unsigned short instead of int");
610 put_help("-fshow-column", "show the column number in diagnostic messages");
611 put_help("-funsigned-char", "char is an unsigned type");
612 put_help("--ms", "Enable msvc extensions");
613 put_help("--no-ms", "Disable msvc extensions");
614 put_help("--gcc", "Enable gcc extensions");
615 put_help("--no-gcc", "Disable gcc extensions");
616 put_help("-std=STANDARD", "Specify language standard:");
617 put_choice("c99", "ISO C99 standard");
618 put_choice("c89", "ISO C89 standard");
619 put_choice("c9x", "deprecated");
620 put_choice("c++", "ISO C++ 98");
621 put_choice("c++98", "ISO C++ 98");
622 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
623 put_choice("gnu89", "ISO C89 + GNU extensions");
624 put_choice("gnu9x", "deprecated");
625 put_choice("iso9899:1990", "ISO C89");
626 put_choice("iso9899:199409", "ISO C90");
627 put_choice("iso9899:1999", "ISO C99");
628 put_choice("iso9899:199x", "deprecated");
629 put_help("-pedantic", "ignored (gcc compatibility)");
630 put_help("-ansi", "ignored (gcc compatibility)");
631 put_help("--strict", "Enable strict conformance checking");
634 static void print_help_warnings(void)
636 put_help("-w", "disable all warnings");
637 put_help("-W", "ignored (gcc compatibility)");
638 put_help("-Wno-trigraphs", "warn if input contains trigraphs");
639 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
640 print_warning_opt_help();
643 static void print_help_optimisation(void)
645 put_help("-O LEVEL", "select optimisation level (0-4)");
646 put_help("-fexpensive-optimizations","ignored (gcc compatibility)");
649 static void print_help_codegeneration(void)
651 put_help("-g", "Generate debug information");
652 put_help("-pg", "Instrument code for gnu gprof");
653 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
654 put_help("-ffreestanding", "compile in freestanding mode (see ISO C standard)");
655 put_help("-fhosted", "compile in hosted (not freestanding) mode");
656 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
657 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
658 put_help("-pthread", "Use pthread threading library");
659 put_help("-fverbose-asm", "ignored (gcc compatibility)");
660 put_help("-ffast-math", "ignored (gcc compatibility)");
661 put_help("-fjump-tables", "ignored (gcc compatibility)");
662 put_help("-fcommon", "ignored (gcc compatibility)");
663 put_help("-foptimize-sibling-calls", "ignored (gcc compatibility)");
664 put_help("-falign-loops", "ignored (gcc compatibility)");
665 put_help("-falign-jumps", "ignored (gcc compatibility)");
666 put_help("-falign-functions", "ignored (gcc compatibility)");
667 put_help("-fPIC", "ignored (gcc compatibility)");
668 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
669 put_help("-mtriple=TARGET", "alias for -mtarget (clang compatibility)");
670 put_help("-march=ARCH", "");
671 put_help("-mtune=ARCH", "");
672 put_help("-mcpu=CPU", "");
673 put_help("-mfpmath=", "");
674 put_help("-mpreferred-stack-boundary=", "");
675 put_help("-mrtd", "");
676 put_help("-mregparm=", "not supported yet");
677 put_help("-msoft-float", "not supported yet");
678 put_help("-m32", "generate 32bit code");
679 put_help("-m64", "generate 64bit code");
681 puts("\tMost of these options can be used with a no- prefix to disable them");
682 puts("\ti.e. -fno-signed-char");
685 static void print_help_linker(void)
687 put_help("-l LIBRARY", "");
688 put_help("-L PATH", "");
689 put_help("-shared", "Produce a shared library");
690 put_help("-static", "Produce statically linked binary");
691 put_help("-Wl,OPTION", "pass option directly to linker");
694 static void print_help_debug(void)
696 put_help("--lextest", "Preprocess and tokenize only");
697 put_help("--print-ast", "Preprocess, parse and print AST");
698 put_help("--print-implicit-cast", "");
699 put_help("--print-parenthesis", "");
700 put_help("--benchmark", "Preprocess and parse, produces no output");
701 put_help("--time", "Measure time of compiler passes");
702 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
703 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
706 static void print_help_language_tools(void)
708 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
709 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
710 put_help("--jna-limit filename", "");
711 put_help("--jna-libname name", "");
714 static void print_help_firm(void)
716 put_help("-bOPTION", "directly pass option to libFirm backend");
717 int res = be_parse_arg("help");
724 HELP_BASIC = 1u << 0,
725 HELP_PREPROCESSOR = 1u << 1,
726 HELP_PARSER = 1u << 2,
727 HELP_WARNINGS = 1u << 3,
728 HELP_OPTIMISATION = 1u << 4,
729 HELP_CODEGEN = 1u << 5,
730 HELP_LINKER = 1u << 6,
731 HELP_LANGUAGETOOLS = 1u << 7,
732 HELP_DEBUG = 1u << 8,
735 HELP_ALL = (unsigned)-1
738 static void print_help(const char *argv0, help_sections_t sections)
740 if (sections & HELP_BASIC) print_help_basic(argv0);
741 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
742 if (sections & HELP_PARSER) print_help_parser();
743 if (sections & HELP_WARNINGS) print_help_warnings();
744 if (sections & HELP_OPTIMISATION) print_help_optimisation();
745 if (sections & HELP_CODEGEN) print_help_codegeneration();
746 if (sections & HELP_LINKER) print_help_linker();
747 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
748 if (sections & HELP_DEBUG) print_help_debug();
749 if (sections & HELP_FIRM) print_help_firm();
752 static void set_be_option(const char *arg)
754 int res = be_parse_arg(arg);
759 static void copy_file(FILE *dest, FILE *input)
763 while (!feof(input) && !ferror(dest)) {
764 size_t read = fread(buf, 1, sizeof(buf), input);
765 if (fwrite(buf, 1, read, dest) != read) {
766 perror("couldn't write output");
771 static FILE *open_file(const char *filename)
773 if (streq(filename, "-")) {
777 FILE *in = fopen(filename, "r");
779 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
787 static filetype_t get_filetype_from_string(const char *string)
789 if (streq(string, "c") || streq(string, "c-header"))
791 if (streq(string, "c++") || streq(string, "c++-header"))
793 if (streq(string, "assembler"))
794 return FILETYPE_PREPROCESSED_ASSEMBLER;
795 if (streq(string, "assembler-with-cpp"))
796 return FILETYPE_ASSEMBLER;
797 if (streq(string, "none"))
798 return FILETYPE_AUTODETECT;
800 return FILETYPE_UNKNOWN;
803 static bool init_os_support(void)
805 const char *os = target_machine->operating_system;
806 wchar_atomic_kind = ATOMIC_TYPE_INT;
807 force_long_double_size = 0;
808 enable_main_collect2_hack = false;
809 define_intmax_types = false;
811 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
812 || streq(os, "solaris")) {
813 set_create_ld_ident(create_name_linux_elf);
814 } else if (streq(os, "darwin")) {
815 force_long_double_size = 16;
816 set_create_ld_ident(create_name_macho);
817 define_intmax_types = true;
818 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
819 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
820 enable_main_collect2_hack = true;
821 set_create_ld_ident(create_name_win32);
829 static bool parse_target_triple(const char *arg)
831 machine_triple_t *triple = firm_parse_machine_triple(arg);
832 if (triple == NULL) {
833 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
836 target_machine = triple;
840 static void setup_target_machine(void)
842 if (!setup_firm_for_machine(target_machine))
847 int main(int argc, char **argv)
851 const char *dumpfunction = NULL;
852 const char *print_file_name_file = NULL;
853 compile_mode_t mode = CompileAssembleLink;
855 int result = EXIT_SUCCESS;
856 char cpu_arch[16] = "ia32";
857 file_list_entry_t *files = NULL;
858 file_list_entry_t *last_file = NULL;
859 bool construct_dep_target = false;
860 bool do_timing = false;
861 bool profile_generate = false;
862 bool profile_use = false;
863 struct obstack file_obst;
865 atexit(free_temp_files);
867 /* hack for now... */
868 if (strstr(argv[0], "pptest") != NULL) {
869 extern int pptest_main(int argc, char **argv);
870 return pptest_main(argc, argv);
873 obstack_init(&cppflags_obst);
874 obstack_init(&ldflags_obst);
875 obstack_init(&asflags_obst);
876 obstack_init(&file_obst);
878 #define GET_ARG_AFTER(def, args) \
879 def = &arg[sizeof(args)-1]; \
880 if (def[0] == '\0') { \
883 fprintf(stderr, "error: expected argument after '" args "'\n"); \
884 argument_errors = true; \
888 if (def[0] == '-' && def[1] != '\0') { \
889 fprintf(stderr, "error: expected argument after '" args "'\n"); \
890 argument_errors = true; \
895 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
897 /* early options parsing (find out optimisation level and OS) */
898 for (int i = 1; i < argc; ++i) {
899 const char *arg = argv[i];
903 const char *option = &arg[1];
904 if (option[0] == 'O') {
905 sscanf(&option[1], "%d", &opt_level);
909 const char *target = getenv("TARGET");
911 parse_target_triple(target);
912 if (target_machine == NULL) {
913 target_machine = firm_get_host_machine();
915 choose_optimization_pack(opt_level);
916 setup_target_machine();
918 /* parse rest of options */
919 standard = STANDARD_DEFAULT;
920 unsigned features_on = 0;
921 unsigned features_off = 0;
922 filetype_t forced_filetype = FILETYPE_AUTODETECT;
923 help_sections_t help = HELP_NONE;
924 bool argument_errors = false;
925 for (int i = 1; i < argc; ++i) {
926 const char *arg = argv[i];
927 if (arg[0] == '-' && arg[1] != '\0') {
929 const char *option = &arg[1];
930 if (option[0] == 'o') {
931 GET_ARG_AFTER(outname, "-o");
932 } else if (option[0] == 'g') {
933 set_be_option("debuginfo=stabs");
934 set_be_option("omitfp=no");
935 set_be_option("ia32-nooptcc=yes");
936 } else if (SINGLE_OPTION('c')) {
937 mode = CompileAssemble;
938 } else if (SINGLE_OPTION('E')) {
939 mode = PreprocessOnly;
940 } else if (SINGLE_OPTION('S')) {
942 } else if (option[0] == 'O') {
944 } else if (option[0] == 'I') {
946 GET_ARG_AFTER(opt, "-I");
947 add_flag(&cppflags_obst, "-I%s", opt);
948 } else if (option[0] == 'D') {
950 GET_ARG_AFTER(opt, "-D");
951 add_flag(&cppflags_obst, "-D%s", opt);
952 } else if (option[0] == 'U') {
954 GET_ARG_AFTER(opt, "-U");
955 add_flag(&cppflags_obst, "-U%s", opt);
956 } else if (option[0] == 'l') {
958 GET_ARG_AFTER(opt, "-l");
959 add_flag(&ldflags_obst, "-l%s", opt);
960 } else if (option[0] == 'L') {
962 GET_ARG_AFTER(opt, "-L");
963 add_flag(&ldflags_obst, "-L%s", opt);
964 } else if (SINGLE_OPTION('v')) {
966 } else if (SINGLE_OPTION('w')) {
967 memset(&warning, 0, sizeof(warning));
968 } else if (option[0] == 'x') {
970 GET_ARG_AFTER(opt, "-x");
971 forced_filetype = get_filetype_from_string(opt);
972 if (forced_filetype == FILETYPE_UNKNOWN) {
973 fprintf(stderr, "Unknown language '%s'\n", opt);
974 argument_errors = true;
976 } else if (streq(option, "M")) {
977 mode = PreprocessOnly;
978 add_flag(&cppflags_obst, "-M");
979 } else if (streq(option, "MMD") ||
980 streq(option, "MD")) {
981 construct_dep_target = true;
982 add_flag(&cppflags_obst, "-%s", option);
983 } else if (streq(option, "MM") ||
984 streq(option, "MP")) {
985 add_flag(&cppflags_obst, "-%s", option);
986 } else if (streq(option, "MT") ||
987 streq(option, "MQ") ||
988 streq(option, "MF")) {
990 GET_ARG_AFTER(opt, "-MT");
991 add_flag(&cppflags_obst, "-%s", option);
992 add_flag(&cppflags_obst, "%s", opt);
993 } else if (streq(option, "include")) {
995 GET_ARG_AFTER(opt, "-include");
996 add_flag(&cppflags_obst, "-include");
997 add_flag(&cppflags_obst, "%s", opt);
998 } else if (streq(option, "isystem")) {
1000 GET_ARG_AFTER(opt, "-isystem");
1001 add_flag(&cppflags_obst, "-isystem");
1002 add_flag(&cppflags_obst, "%s", opt);
1003 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1004 } else if (streq(option, "pthread")) {
1005 /* set flags for the preprocessor */
1006 add_flag(&cppflags_obst, "-D_REENTRANT");
1007 /* set flags for the linker */
1008 add_flag(&ldflags_obst, "-lpthread");
1010 } else if (streq(option, "nostdinc")
1011 || streq(option, "trigraphs")) {
1012 /* pass these through to the preprocessor */
1013 add_flag(&cppflags_obst, "%s", arg);
1014 } else if (streq(option, "pipe")) {
1015 /* here for gcc compatibility */
1016 } else if (streq(option, "static")) {
1017 add_flag(&ldflags_obst, "-static");
1018 } else if (streq(option, "shared")) {
1019 add_flag(&ldflags_obst, "-shared");
1020 } else if (option[0] == 'f') {
1021 char const *orig_opt;
1022 GET_ARG_AFTER(orig_opt, "-f");
1024 if (strstart(orig_opt, "input-charset=")) {
1025 char const* const encoding = strchr(orig_opt, '=') + 1;
1026 select_input_encoding(encoding);
1027 } else if (strstart(orig_opt, "align-loops=") ||
1028 strstart(orig_opt, "align-jumps=") ||
1029 strstart(orig_opt, "align-functions=")) {
1030 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1031 } else if (strstart(orig_opt, "visibility=")) {
1032 const char *arg = strchr(orig_opt, '=')+1;
1033 elf_visibility_tag_t visibility
1034 = get_elf_visibility_from_string(arg);
1035 if (visibility == ELF_VISIBILITY_ERROR) {
1036 fprintf(stderr, "invalid visibility '%s' specified\n",
1038 argument_errors = true;
1040 set_default_visibility(visibility);
1042 } else if (strstart(orig_opt, "message-length=")) {
1043 /* ignore: would only affect error message format */
1045 /* -f options which have an -fno- variant */
1046 char const *opt = orig_opt;
1047 bool truth_value = true;
1048 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1049 truth_value = false;
1053 if (streq(opt, "builtins")) {
1054 use_builtins = truth_value;
1055 } else if (streq(opt, "dollars-in-identifiers")) {
1056 allow_dollar_in_symbol = truth_value;
1057 } else if (streq(opt, "omit-frame-pointer")) {
1058 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1059 } else if (streq(opt, "short-wchar")) {
1060 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1062 } else if (streq(opt, "show-column")) {
1063 show_column = truth_value;
1064 } else if (streq(opt, "signed-char")) {
1065 char_is_signed = truth_value;
1066 } else if (streq(opt, "strength-reduce")) {
1067 firm_option(truth_value ? "strength-red" : "no-strength-red");
1068 } else if (streq(opt, "syntax-only")) {
1069 mode = truth_value ? ParseOnly : CompileAssembleLink;
1070 } else if (streq(opt, "unsigned-char")) {
1071 char_is_signed = !truth_value;
1072 } else if (streq(opt, "freestanding")) {
1073 freestanding = truth_value;
1074 } else if (streq(opt, "hosted")) {
1075 freestanding = !truth_value;
1076 } else if (streq(opt, "profile-generate")) {
1077 profile_generate = truth_value;
1078 } else if (streq(opt, "profile-use")) {
1079 profile_use = truth_value;
1080 } else if (!truth_value &&
1081 streq(opt, "asynchronous-unwind-tables")) {
1082 /* nothing todo, a gcc feature which we don't support
1083 * anyway was deactivated */
1084 } else if (streq(orig_opt, "verbose-asm")) {
1085 /* ignore: we always print verbose assembler */
1086 } else if (streq(opt, "fast-math") ||
1087 streq(opt, "jump-tables") ||
1088 streq(opt, "expensive-optimizations") ||
1089 streq(opt, "common") ||
1090 streq(opt, "optimize-sibling-calls") ||
1091 streq(opt, "align-loops") ||
1092 streq(opt, "align-jumps") ||
1093 streq(opt, "align-functions") ||
1094 streq(opt, "PIC")) {
1095 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1096 } else if (streq(opt, "help")) {
1097 fprintf(stderr, "warning: -fhelp is deprecated\n");
1100 int res = firm_option(orig_opt);
1102 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1104 argument_errors = true;
1109 } else if (option[0] == 'b') {
1111 GET_ARG_AFTER(opt, "-b");
1113 if (streq(opt, "help")) {
1114 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1117 int res = be_parse_arg(opt);
1119 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1121 argument_errors = true;
1122 } else if (strstart(opt, "isa=")) {
1123 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1126 } else if (option[0] == 'W') {
1127 if (option[1] == '\0') {
1128 /* ignore -W, our defaults are already quite verbose */
1129 } else if (strstart(option + 1, "p,")) {
1130 // pass options directly to the preprocessor
1132 GET_ARG_AFTER(opt, "-Wp,");
1133 add_flag(&cppflags_obst, "-Wp,%s", opt);
1134 } else if (strstart(option + 1, "l,")) {
1135 // pass options directly to the linker
1137 GET_ARG_AFTER(opt, "-Wl,");
1138 add_flag(&ldflags_obst, "-Wl,%s", opt);
1139 } else if (streq(option + 1, "no-trigraphs")
1140 || streq(option + 1, "undef")) {
1141 add_flag(&cppflags_obst, "%s", arg);
1143 set_warning_opt(&option[1]);
1145 } else if (option[0] == 'm') {
1150 GET_ARG_AFTER(opt, "-m");
1151 if (strstart(opt, "target=")) {
1152 GET_ARG_AFTER(opt, "-mtarget=");
1153 if (!parse_target_triple(opt)) {
1154 argument_errors = true;
1156 setup_target_machine();
1157 target_triple = opt;
1159 } else if (strstart(opt, "triple=")) {
1160 GET_ARG_AFTER(opt, "-mtriple=");
1161 if (!parse_target_triple(opt)) {
1162 argument_errors = true;
1164 setup_target_machine();
1165 target_triple = opt;
1167 } else if (strstart(opt, "arch=")) {
1168 GET_ARG_AFTER(opt, "-march=");
1169 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1170 int res = be_parse_arg(arch_opt);
1172 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1173 argument_errors = true;
1175 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1176 int res = be_parse_arg(arch_opt);
1178 argument_errors = true;
1180 } else if (strstart(opt, "tune=")) {
1181 GET_ARG_AFTER(opt, "-mtune=");
1182 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1183 int res = be_parse_arg(arch_opt);
1185 argument_errors = true;
1186 } else if (strstart(opt, "cpu=")) {
1187 GET_ARG_AFTER(opt, "-mcpu=");
1188 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1189 int res = be_parse_arg(arch_opt);
1191 argument_errors = true;
1192 } else if (strstart(opt, "fpmath=")) {
1193 GET_ARG_AFTER(opt, "-mfpmath=");
1194 if (streq(opt, "387"))
1196 else if (streq(opt, "sse"))
1199 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1200 argument_errors = true;
1202 if (!argument_errors) {
1203 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1204 int res = be_parse_arg(arch_opt);
1206 argument_errors = true;
1208 } else if (strstart(opt, "preferred-stack-boundary=")) {
1209 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1210 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1211 int res = be_parse_arg(arch_opt);
1213 argument_errors = true;
1214 } else if (streq(opt, "rtd")) {
1215 default_calling_convention = CC_STDCALL;
1216 } else if (strstart(opt, "regparm=")) {
1217 fprintf(stderr, "error: regparm convention not supported yet\n");
1218 argument_errors = true;
1219 } else if (streq(opt, "soft-float")) {
1220 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1221 argument_errors = true;
1223 long int value = strtol(opt, NULL, 10);
1225 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1226 argument_errors = true;
1227 } else if (value != 16 && value != 32 && value != 64) {
1228 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1229 argument_errors = true;
1231 machine_size = (unsigned int)value;
1232 add_flag(&cppflags_obst, "-m%u", machine_size);
1233 add_flag(&asflags_obst, "-m%u", machine_size);
1234 add_flag(&ldflags_obst, "-m%u", machine_size);
1237 } else if (streq(option, "pg")) {
1238 set_be_option("gprof");
1239 add_flag(&ldflags_obst, "-pg");
1240 } else if (streq(option, "pedantic") ||
1241 streq(option, "ansi")) {
1242 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1243 } else if (strstart(option, "std=")) {
1244 const char *const o = &option[4];
1246 streq(o, "c++") ? STANDARD_CXX98 :
1247 streq(o, "c++98") ? STANDARD_CXX98 :
1248 streq(o, "c89") ? STANDARD_C89 :
1249 streq(o, "c99") ? STANDARD_C99 :
1250 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1251 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1252 streq(o, "gnu89") ? STANDARD_GNU89 :
1253 streq(o, "gnu99") ? STANDARD_GNU99 :
1254 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1255 streq(o, "iso9899:1990") ? STANDARD_C89 :
1256 streq(o, "iso9899:199409") ? STANDARD_C90 :
1257 streq(o, "iso9899:1999") ? STANDARD_C99 :
1258 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1259 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1260 } else if (streq(option, "version")) {
1261 print_cparser_version();
1262 } else if (strstart(option, "print-file-name=")) {
1263 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1264 } else if (option[0] == '-') {
1265 /* double dash option */
1267 if (streq(option, "gcc")) {
1268 features_on |= _GNUC;
1269 features_off &= ~_GNUC;
1270 } else if (streq(option, "no-gcc")) {
1271 features_on &= ~_GNUC;
1272 features_off |= _GNUC;
1273 } else if (streq(option, "ms")) {
1275 features_off &= ~_MS;
1276 } else if (streq(option, "no-ms")) {
1277 features_on &= ~_MS;
1278 features_off |= _MS;
1279 } else if (streq(option, "strict")) {
1281 } else if (streq(option, "lextest")) {
1283 } else if (streq(option, "benchmark")) {
1284 mode = BenchmarkParser;
1285 } else if (streq(option, "print-ast")) {
1287 } else if (streq(option, "print-implicit-cast")) {
1288 print_implicit_casts = true;
1289 } else if (streq(option, "print-parenthesis")) {
1290 print_parenthesis = true;
1291 } else if (streq(option, "print-fluffy")) {
1293 } else if (streq(option, "print-jna")) {
1295 } else if (streq(option, "jna-limit")) {
1298 fprintf(stderr, "error: "
1299 "expected argument after '--jna-limit'\n");
1300 argument_errors = true;
1303 jna_limit_output(argv[i]);
1304 } else if (streq(option, "jna-libname")) {
1307 fprintf(stderr, "error: "
1308 "expected argument after '--jna-libname'\n");
1309 argument_errors = true;
1312 jna_set_libname(argv[i]);
1313 } else if (streq(option, "time")) {
1315 } else if (streq(option, "version")) {
1316 print_cparser_version();
1317 return EXIT_SUCCESS;
1318 } else if (streq(option, "help")) {
1320 } else if (streq(option, "help-parser")) {
1321 help |= HELP_PARSER;
1322 } else if (streq(option, "help-warnings")) {
1323 help |= HELP_WARNINGS;
1324 } else if (streq(option, "help-codegen")) {
1325 help |= HELP_CODEGEN;
1326 } else if (streq(option, "help-linker")) {
1327 help |= HELP_LINKER;
1328 } else if (streq(option, "help-language-tools")) {
1329 help |= HELP_LANGUAGETOOLS;
1330 } else if (streq(option, "help-debug")) {
1332 } else if (streq(option, "help-firm")) {
1334 } else if (streq(option, "help-all")) {
1336 } else if (streq(option, "dump-function")) {
1339 fprintf(stderr, "error: "
1340 "expected argument after '--dump-function'\n");
1341 argument_errors = true;
1344 dumpfunction = argv[i];
1346 } else if (streq(option, "export-ir")) {
1347 mode = CompileExportIR;
1349 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1350 argument_errors = true;
1353 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1354 argument_errors = true;
1357 filetype_t type = forced_filetype;
1358 if (type == FILETYPE_AUTODETECT) {
1359 if (streq(arg, "-")) {
1360 /* - implicitly means C source file */
1363 const char *suffix = strrchr(arg, '.');
1364 /* Ensure there is at least one char before the suffix */
1365 if (suffix != NULL && suffix != arg) {
1368 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1369 streq(suffix, "a") ? FILETYPE_OBJECT :
1370 streq(suffix, "c") ? FILETYPE_C :
1371 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1372 streq(suffix, "C") ? FILETYPE_CXX :
1373 streq(suffix, "cc") ? FILETYPE_CXX :
1374 streq(suffix, "cp") ? FILETYPE_CXX :
1375 streq(suffix, "cpp") ? FILETYPE_CXX :
1376 streq(suffix, "CPP") ? FILETYPE_CXX :
1377 streq(suffix, "cxx") ? FILETYPE_CXX :
1378 streq(suffix, "c++") ? FILETYPE_CXX :
1379 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1380 streq(suffix, "h") ? FILETYPE_C :
1381 streq(suffix, "ir") ? FILETYPE_IR :
1382 streq(suffix, "o") ? FILETYPE_OBJECT :
1383 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1384 streq(suffix, "so") ? FILETYPE_OBJECT :
1385 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1390 file_list_entry_t *entry
1391 = obstack_alloc(&file_obst, sizeof(entry[0]));
1392 memset(entry, 0, sizeof(entry[0]));
1396 if (last_file != NULL) {
1397 last_file->next = entry;
1405 if (help != HELP_NONE) {
1406 print_help(argv[0], help);
1407 return !argument_errors;
1410 if (print_file_name_file != NULL) {
1411 print_file_name(print_file_name_file);
1412 return EXIT_SUCCESS;
1414 if (files == NULL) {
1415 fprintf(stderr, "error: no input files specified\n");
1416 argument_errors = true;
1419 if (argument_errors) {
1421 return EXIT_FAILURE;
1424 /* apply some effects from switches */
1425 c_mode |= features_on;
1426 c_mode &= ~features_off;
1427 if (profile_generate) {
1428 add_flag(&ldflags_obst, "-lfirmprof");
1429 set_be_option("profilegenerate");
1432 set_be_option("profileuse");
1436 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1437 init_symbol_table();
1450 if (construct_dep_target) {
1451 if (outname != 0 && strlen(outname) >= 2) {
1452 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1454 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1457 dep_target[0] = '\0';
1460 char outnamebuf[4096];
1461 if (outname == NULL) {
1462 const char *filename = files->name;
1465 case BenchmarkParser:
1470 case PreprocessOnly:
1475 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1476 outname = outnamebuf;
1478 case CompileAssemble:
1479 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1480 outname = outnamebuf;
1483 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1485 outname = outnamebuf;
1487 case CompileExportIR:
1488 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1489 outname = outnamebuf;
1491 case CompileAssembleLink:
1501 assert(outname != NULL);
1504 if (streq(outname, "-")) {
1507 out = fopen(outname, "w");
1509 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1511 return EXIT_FAILURE;
1515 file_list_entry_t *file;
1516 bool already_constructed_firm = false;
1517 for (file = files; file != NULL; file = file->next) {
1518 char asm_tempfile[1024];
1519 const char *filename = file->name;
1520 filetype_t filetype = file->type;
1522 if (filetype == FILETYPE_OBJECT)
1526 if (mode == LexTest) {
1528 in = open_file(filename);
1529 lextest(in, filename);
1531 return EXIT_SUCCESS;
1534 FILE *preprocessed_in = NULL;
1535 filetype_t next_filetype = filetype;
1538 next_filetype = FILETYPE_PREPROCESSED_C;
1541 next_filetype = FILETYPE_PREPROCESSED_CXX;
1543 case FILETYPE_ASSEMBLER:
1544 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1547 /* no support for input on FILE* yet */
1549 panic("internal compiler error: in for preprocessor != NULL");
1551 preprocessed_in = preprocess(filename, filetype);
1552 if (mode == PreprocessOnly) {
1553 copy_file(out, preprocessed_in);
1554 int result = pclose(preprocessed_in);
1556 /* remove output file in case of error */
1557 if (out != stdout && result != EXIT_SUCCESS) {
1563 in = preprocessed_in;
1564 filetype = next_filetype;
1572 if (mode == Compile) {
1575 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1579 in = open_file(filename);
1581 /* preprocess and compile */
1582 if (filetype == FILETYPE_PREPROCESSED_C) {
1583 char const* invalid_mode;
1586 case STANDARD_C89: c_mode = _C89; break;
1587 /* TODO determine difference between these two */
1588 case STANDARD_C90: c_mode = _C89; break;
1589 case STANDARD_C99: c_mode = _C89 | _C99; break;
1590 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1594 "warning: command line option \"-std=%s\" is not valid for C\n",
1597 case STANDARD_DEFAULT:
1598 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1600 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1601 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1604 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1605 char const* invalid_mode;
1607 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1608 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1609 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1610 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1611 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1614 case STANDARD_CXX98: c_mode = _CXX; break;
1618 "warning: command line option \"-std=%s\" is not valid for C++\n",
1620 case STANDARD_DEFAULT:
1621 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1625 c_mode |= features_on;
1626 c_mode &= ~features_off;
1628 /* do the actual parsing */
1629 ir_timer_t *t_parsing = ir_timer_new();
1630 timer_register(t_parsing, "Frontend: Parsing");
1631 timer_push(t_parsing);
1633 translation_unit_t *const unit = do_parsing(in, filename);
1634 timer_pop(t_parsing);
1636 /* prints the AST even if errors occurred */
1637 if (mode == PrintAst) {
1642 if (error_count > 0) {
1643 /* parsing failed because of errors */
1644 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1646 result = EXIT_FAILURE;
1648 } else if (warning_count > 0) {
1649 fprintf(stderr, "%u warning(s)\n", warning_count);
1652 if (in == preprocessed_in) {
1653 int pp_result = pclose(preprocessed_in);
1654 if (pp_result != EXIT_SUCCESS) {
1655 /* remove output file */
1658 return EXIT_FAILURE;
1662 if (mode == BenchmarkParser) {
1664 } else if (mode == PrintFluffy) {
1665 write_fluffy_decls(out, unit);
1667 } else if (mode == PrintJna) {
1668 write_jna_decls(out, unit);
1672 /* build the firm graph */
1673 ir_timer_t *t_construct = ir_timer_new();
1674 timer_register(t_construct, "Frontend: Graph construction");
1675 timer_push(t_construct);
1676 if (already_constructed_firm) {
1677 panic("compiling multiple files/translation units not possible");
1679 translation_unit_to_firm(unit);
1680 already_constructed_firm = true;
1681 timer_pop(t_construct);
1684 if (mode == ParseOnly) {
1688 if (mode == CompileDump) {
1690 ident *id = new_id_from_str(dumpfunction);
1691 ir_graph *irg = NULL;
1692 int n_irgs = get_irp_n_irgs();
1693 for (int i = 0; i < n_irgs; ++i) {
1694 ir_graph *tirg = get_irp_irg(i);
1695 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1703 fprintf(stderr, "No graph for function '%s' found\n",
1705 return EXIT_FAILURE;
1708 dump_ir_graph_file(out, irg);
1710 return EXIT_SUCCESS;
1713 if (mode == CompileExportIR) {
1716 return EXIT_SUCCESS;
1719 gen_firm_finish(asm_out, filename);
1720 if (asm_out != out) {
1723 } else if (filetype == FILETYPE_IR) {
1725 ir_import(filename);
1727 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1728 copy_file(asm_out, in);
1729 if (in == preprocessed_in) {
1730 int pp_result = pclose(preprocessed_in);
1731 if (pp_result != EXIT_SUCCESS) {
1732 /* remove output in error case */
1738 if (asm_out != out) {
1743 if (mode == Compile)
1746 /* if we're here then we have preprocessed assembly */
1747 filename = asm_tempfile;
1748 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1751 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1753 const char *filename_o;
1754 if (mode == CompileAssemble) {
1756 filename_o = outname;
1758 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1763 assemble(filename_o, filename);
1765 size_t len = strlen(filename_o) + 1;
1766 filename = obstack_copy(&file_obst, filename_o, len);
1767 filetype = FILETYPE_OBJECT;
1770 /* ok we're done here, process next file */
1771 file->name = filename;
1772 file->type = filetype;
1775 if (result != EXIT_SUCCESS) {
1781 /* link program file */
1782 if (mode == CompileAssembleLink) {
1783 obstack_1grow(&ldflags_obst, '\0');
1784 const char *flags = obstack_finish(&ldflags_obst);
1786 /* construct commandline */
1787 const char *linker = getenv("CPARSER_LINK");
1788 if (linker != NULL) {
1789 obstack_printf(&file_obst, "%s ", linker);
1791 if (target_triple != NULL)
1792 obstack_printf(&file_obst, "%s-", target_triple);
1793 obstack_printf(&file_obst, "%s ", LINKER);
1796 for (file_list_entry_t *entry = files; entry != NULL;
1797 entry = entry->next) {
1798 if (entry->type != FILETYPE_OBJECT)
1801 add_flag(&file_obst, "%s", entry->name);
1804 add_flag(&file_obst, "-o");
1805 add_flag(&file_obst, outname);
1806 obstack_printf(&file_obst, "%s", flags);
1807 obstack_1grow(&file_obst, '\0');
1809 char *commandline = obstack_finish(&file_obst);
1814 int err = system(commandline);
1815 if (err != EXIT_SUCCESS) {
1816 fprintf(stderr, "linker reported an error\n");
1817 return EXIT_FAILURE;
1824 obstack_free(&cppflags_obst, NULL);
1825 obstack_free(&ldflags_obst, NULL);
1826 obstack_free(&asflags_obst, NULL);
1827 obstack_free(&file_obst, NULL);
1837 exit_symbol_table();
1838 return EXIT_SUCCESS;