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 /* to switch on printing of implicit casts */
114 extern bool print_implicit_casts;
116 /* to switch on printing of parenthesis to indicate operator precedence */
117 extern bool print_parenthesis;
119 static machine_triple_t *target_machine;
120 static const char *target_triple;
122 static struct obstack cppflags_obst;
123 static struct obstack ldflags_obst;
124 static struct obstack asflags_obst;
125 static char dep_target[1024];
126 static const char *outname;
127 static bool define_intmax_types;
129 typedef enum lang_standard_t {
130 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
131 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
132 STANDARD_C89, /* ISO C90 (sic) */
133 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
134 STANDARD_C99, /* ISO C99 */
135 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
136 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
137 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
138 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
141 static lang_standard_t standard;
143 typedef struct file_list_entry_t file_list_entry_t;
145 typedef enum filetype_t {
148 FILETYPE_PREPROCESSED_C,
150 FILETYPE_PREPROCESSED_CXX,
152 FILETYPE_PREPROCESSED_ASSEMBLER,
158 struct file_list_entry_t {
159 const char *name; /**< filename or NULL for stdin */
161 file_list_entry_t *next;
164 static file_list_entry_t *temp_files;
166 static void get_output_name(char *buf, size_t buflen, const char *inputname,
169 if (inputname == NULL)
172 char const *const last_slash = strrchr(inputname, '/');
173 char const *const filename =
174 last_slash != NULL ? last_slash + 1 : inputname;
175 char const *const last_dot = strrchr(filename, '.');
176 char const *const name_end =
177 last_dot != NULL ? last_dot : strchr(filename, '\0');
179 int const len = snprintf(buf, buflen, "%.*s%s",
180 (int)(name_end - filename), filename, newext);
182 if (len < 0 || buflen <= (size_t)len)
184 if (buflen <= (size_t)len)
186 panic("filename too long");
189 #include "gen_builtins.h"
191 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
196 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
200 lexer_open_stream(in, input_name);
203 translation_unit_t *unit = finish_parsing();
207 static void lextest(FILE *in, const char *fname)
209 lexer_open_stream(in, fname);
212 lexer_next_preprocessing_token();
213 print_token(stdout, &lexer_token);
215 } while (lexer_token.type != T_EOF);
218 static void add_flag(struct obstack *obst, const char *format, ...)
223 va_start(ap, format);
227 vsnprintf(buf, sizeof(buf), format, ap);
230 obstack_1grow(obst, ' ');
232 obstack_1grow(obst, '"');
233 obstack_grow(obst, buf, len);
234 obstack_1grow(obst, '"');
236 /* escape stuff... */
237 for (char *c = buf; *c != '\0'; ++c) {
255 obstack_1grow(obst, '\\');
258 obstack_1grow(obst, *c);
265 static const char *type_to_string(type_t *type)
267 assert(type->kind == TYPE_ATOMIC);
268 return get_atomic_kind_name(type->atomic.akind);
271 static FILE *preprocess(const char *fname, filetype_t filetype)
273 static const char *common_flags = NULL;
275 if (common_flags == NULL) {
276 obstack_1grow(&cppflags_obst, '\0');
277 const char *flags = obstack_finish(&cppflags_obst);
279 /* setup default defines */
280 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
281 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
282 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
283 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
285 add_flag(&cppflags_obst, "-U__VERSION__");
286 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
288 if (define_intmax_types) {
289 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
290 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
291 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
292 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
295 if (flags[0] != '\0') {
296 size_t len = strlen(flags);
297 obstack_1grow(&cppflags_obst, ' ');
298 obstack_grow(&cppflags_obst, flags, len);
300 obstack_1grow(&cppflags_obst, '\0');
301 common_flags = obstack_finish(&cppflags_obst);
304 assert(obstack_object_size(&cppflags_obst) == 0);
306 const char *preprocessor = getenv("CPARSER_PP");
307 if (preprocessor != NULL) {
308 obstack_printf(&cppflags_obst, "%s ", preprocessor);
310 if (target_triple != NULL)
311 obstack_printf(&cppflags_obst, "%s-", target_triple);
312 obstack_printf(&cppflags_obst, PREPROCESSOR);
317 add_flag(&cppflags_obst, "-std=c99");
320 add_flag(&cppflags_obst, "-std=c++98");
322 case FILETYPE_ASSEMBLER:
323 add_flag(&cppflags_obst, "-x");
324 add_flag(&cppflags_obst, "assembler-with-cpp");
329 obstack_printf(&cppflags_obst, "%s", common_flags);
331 /* handle dependency generation */
332 if (dep_target[0] != '\0') {
333 add_flag(&cppflags_obst, "-MF");
334 add_flag(&cppflags_obst, dep_target);
335 if (outname != NULL) {
336 add_flag(&cppflags_obst, "-MQ");
337 add_flag(&cppflags_obst, outname);
340 add_flag(&cppflags_obst, fname);
341 obstack_1grow(&cppflags_obst, '\0');
343 char *commandline = obstack_finish(&cppflags_obst);
347 FILE *f = popen(commandline, "r");
349 fprintf(stderr, "invoking preprocessor failed\n");
352 /* we don't really need that anymore */
353 obstack_free(&cppflags_obst, commandline);
358 static void assemble(const char *out, const char *in)
360 obstack_1grow(&asflags_obst, '\0');
361 const char *flags = obstack_finish(&asflags_obst);
363 const char *assembler = getenv("CPARSER_AS");
364 if (assembler != NULL) {
365 obstack_printf(&asflags_obst, "%s", assembler);
367 if (target_triple != NULL)
368 obstack_printf(&asflags_obst, "%s-", target_triple);
369 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
371 if (flags[0] != '\0')
372 obstack_printf(&asflags_obst, " %s", flags);
374 obstack_printf(&asflags_obst, " %s -o %s", in, out);
375 obstack_1grow(&asflags_obst, '\0');
377 char *commandline = obstack_finish(&asflags_obst);
381 int err = system(commandline);
382 if (err != EXIT_SUCCESS) {
383 fprintf(stderr, "assembler reported an error\n");
386 obstack_free(&asflags_obst, commandline);
389 static void print_file_name(const char *file)
391 add_flag(&ldflags_obst, "-print-file-name=%s", file);
393 obstack_1grow(&ldflags_obst, '\0');
394 const char *flags = obstack_finish(&ldflags_obst);
396 /* construct commandline */
397 const char *linker = getenv("CPARSER_LINK");
398 if (linker != NULL) {
399 obstack_printf(&ldflags_obst, "%s ", linker);
401 if (target_triple != NULL)
402 obstack_printf(&ldflags_obst, "%s-", target_triple);
403 obstack_printf(&ldflags_obst, "%s ", LINKER);
405 obstack_printf(&ldflags_obst, "%s ", linker);
406 obstack_printf(&ldflags_obst, "%s", flags);
407 obstack_1grow(&ldflags_obst, '\0');
409 char *commandline = obstack_finish(&ldflags_obst);
413 int err = system(commandline);
414 if (err != EXIT_SUCCESS) {
415 fprintf(stderr, "linker reported an error\n");
418 obstack_free(&ldflags_obst, commandline);
421 static const char *try_dir(const char *dir)
425 if (access(dir, R_OK | W_OK | X_OK) == 0)
430 static const char *get_tempdir(void)
432 static const char *tmpdir = NULL;
438 tmpdir = try_dir(getenv("TMPDIR"));
440 tmpdir = try_dir(getenv("TMP"));
442 tmpdir = try_dir(getenv("TEMP"));
446 tmpdir = try_dir(P_tmpdir);
450 tmpdir = try_dir("/var/tmp");
452 tmpdir = try_dir("/usr/tmp");
454 tmpdir = try_dir("/tmp");
463 /* cheap and nasty mkstemp replacement */
464 static int mkstemp(char *templ)
467 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
472 * an own version of tmpnam, which: writes in a buffer, emits no warnings
473 * during linking (like glibc/gnu ld do for tmpnam)...
475 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
477 const char *tempdir = get_tempdir();
479 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
481 int fd = mkstemp(buffer);
483 fprintf(stderr, "couldn't create temporary file: %s\n",
487 FILE *out = fdopen(fd, "w");
489 fprintf(stderr, "couldn't create temporary file FILE*\n");
493 file_list_entry_t *entry = xmalloc(sizeof(*entry));
494 memset(entry, 0, sizeof(*entry));
496 size_t name_len = strlen(buffer) + 1;
497 char *name = malloc(name_len);
498 memcpy(name, buffer, name_len);
501 entry->next = temp_files;
507 static void free_temp_files(void)
509 file_list_entry_t *entry = temp_files;
510 file_list_entry_t *next;
511 for ( ; entry != NULL; entry = next) {
515 free((char*) entry->name);
521 typedef enum compile_mode_t {
536 static void usage(const char *argv0)
538 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
541 static void print_cparser_version(void)
543 printf("cparser (%s) using libFirm (%u.%u",
544 cparser_REVISION, ir_get_version_major(),
545 ir_get_version_minor());
547 const char *revision = ir_get_version_revision();
548 if (revision[0] != 0) {
550 fputs(revision, stdout);
553 const char *build = ir_get_version_build();
556 fputs(build, stdout);
559 puts("This is free software; see the source for copying conditions. There is NO\n"
560 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
563 static void print_help_basic(const char *argv0)
567 put_help("--help", "Display this information");
568 put_help("--version", "Display compiler version");
569 put_help("--help-parser", "Display information about parser options");
570 put_help("--help-warnings", "Display information about warning options");
571 put_help("--help-codegen", "Display information about code-generation options");
572 put_help("--help-linker", "Display information about linker options");
573 put_help("--help-language-tools", "Display information about language tools options");
574 put_help("--help-debug", "Display information about compiler debugging options");
575 put_help("--help-firm", "Display information about direct firm options");
576 put_help("--help-all", "Display information about all options");
577 put_help("-c", "Compile and assemble but do not link");
578 put_help("-E", "Preprocess only");
579 put_help("-S", "Compile but do not assembler or link");
580 put_help("-o", "Specify output file");
581 put_help("-v", "Verbose output (show invocation of sub-processes)");
582 put_help("-x", "Force input language:");
583 put_choice("c", "C");
584 put_choice("c++", "C++");
585 put_choice("assembler", "Assembler (no preprocessing)");
586 put_choice("assembler-with-cpp", "Assembler with preprocessing");
587 put_choice("none", "Autodetection");
588 put_help("-pipe", "ignored (gcc compatibility)");
591 static void print_help_preprocessor(void)
593 put_help("-nostdinc", "Do not search standard system include directories");
594 put_help("-trigraphs", "Support ISO C trigraphs");
595 put_help("-isystem", "");
596 put_help("-include", "");
597 put_help("-I PATH", "");
598 put_help("-D SYMBOL[=value]", "");
599 put_help("-U SYMBOL", "");
600 put_help("-Wp,OPTION", "pass option directly to preprocessor");
603 put_help("-MMD", "");
611 static void print_help_parser(void)
613 put_help("-finput-charset=CHARSET", "Select encoding of input files");
614 put_help("-fmessage-length=LEN", "ignored (gcc compatibility)");
615 put_help("-fshort-wchar", "wchar_t is unsigned short instead of int");
616 put_help("-funsigned-char", "char is an unsigned type");
617 put_help("--ms", "Enable msvc extensions");
618 put_help("--no-ms", "Disable msvc extensions");
619 put_help("--gcc", "Enable gcc extensions");
620 put_help("--no-gcc", "Disable gcc extensions");
621 put_help("-std=STANDARD", "Specify language standard:");
622 put_choice("c99", "ISO C99 standard");
623 put_choice("c89", "ISO C89 standard");
624 put_choice("c9x", "deprecated");
625 put_choice("c++", "ISO C++ 98");
626 put_choice("c++98", "ISO C++ 98");
627 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
628 put_choice("gnu89", "ISO C89 + GNU extensions");
629 put_choice("gnu9x", "deprecated");
630 put_choice("iso9899:1990", "ISO C89");
631 put_choice("iso9899:199409", "ISO C90");
632 put_choice("iso9899:1999", "ISO C99");
633 put_choice("iso9899:199x", "deprecated");
634 put_help("-pedantic", "ignored (gcc compatibility)");
635 put_help("-ansi", "ignored (gcc compatibility)");
636 put_help("--strict", "Enable strict conformance checking");
639 static void print_help_warnings(void)
641 put_help("-w", "disable all warnings");
642 put_help("-W", "ignored (gcc compatibility)");
643 put_help("-Wno-trigraphs", "warn if input contains trigraphs");
644 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
645 print_warning_opt_help();
648 static void print_help_optimisation(void)
650 put_help("-O LEVEL", "select optimisation level (0-4)");
651 put_help("-fexpensive-optimizations","ignored (gcc compatibility)");
654 static void print_help_codegeneration(void)
656 put_help("-g", "Generate debug information");
657 put_help("-pg", "Instrument code for gnu gprof");
658 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
659 put_help("-ffreestanding", "compile in freestanding mode (see ISO C standard)");
660 put_help("-fhosted", "compile in hosted (not freestanding) mode");
661 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
662 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
663 put_help("-pthread", "Use pthread threading library");
664 put_help("-fverbose-asm", "ignored (gcc compatibility)");
665 put_help("-ffast-math", "ignored (gcc compatibility)");
666 put_help("-fjump-tables", "ignored (gcc compatibility)");
667 put_help("-fcommon", "ignored (gcc compatibility)");
668 put_help("-foptimize-sibling-calls", "ignored (gcc compatibility)");
669 put_help("-falign-loops", "ignored (gcc compatibility)");
670 put_help("-falign-jumps", "ignored (gcc compatibility)");
671 put_help("-falign-functions", "ignored (gcc compatibility)");
672 put_help("-fPIC", "ignored (gcc compatibility)");
673 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
674 put_help("-mtriple=TARGET", "alias for -mtarget (clang compatibility)");
675 put_help("-march=ARCH", "");
676 put_help("-mtune=ARCH", "");
677 put_help("-mcpu=CPU", "");
678 put_help("-mfpmath=", "");
679 put_help("-mpreferred-stack-boundary=", "");
680 put_help("-mrtd", "");
681 put_help("-mregparm=", "not supported yet");
682 put_help("-msoft-float", "not supported yet");
683 put_help("-m32", "generate 32bit code");
684 put_help("-m64", "generate 64bit code");
686 puts("\tMost of these options can be used with a no- prefix to disable them");
687 puts("\ti.e. -fno-signed-char");
690 static void print_help_linker(void)
692 put_help("-l LIBRARY", "");
693 put_help("-L PATH", "");
694 put_help("-shared", "Produce a shared library");
695 put_help("-static", "Produce statically linked binary");
696 put_help("-Wl,OPTION", "pass option directly to linker");
699 static void print_help_debug(void)
701 put_help("--lextest", "Preprocess and tokenize only");
702 put_help("--print-ast", "Preprocess, parse and print AST");
703 put_help("--print-implicit-cast", "");
704 put_help("--print-parenthesis", "");
705 put_help("--benchmark", "Preprocess and parse, produces no output");
706 put_help("--time", "Measure time of compiler passes");
707 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
708 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
711 static void print_help_language_tools(void)
713 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
714 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
715 put_help("--jna-limit filename", "");
716 put_help("--jna-libname name", "");
719 static void print_help_firm(void)
721 put_help("-bOPTION", "directly pass option to libFirm backend");
722 int res = be_parse_arg("help");
729 HELP_BASIC = 1u << 0,
730 HELP_PREPROCESSOR = 1u << 1,
731 HELP_PARSER = 1u << 2,
732 HELP_WARNINGS = 1u << 3,
733 HELP_OPTIMISATION = 1u << 4,
734 HELP_CODEGEN = 1u << 5,
735 HELP_LINKER = 1u << 6,
736 HELP_LANGUAGETOOLS = 1u << 7,
737 HELP_DEBUG = 1u << 8,
740 HELP_ALL = (unsigned)-1
743 static void print_help(const char *argv0, help_sections_t sections)
745 if (sections & HELP_BASIC) print_help_basic(argv0);
746 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
747 if (sections & HELP_PARSER) print_help_parser();
748 if (sections & HELP_WARNINGS) print_help_warnings();
749 if (sections & HELP_OPTIMISATION) print_help_optimisation();
750 if (sections & HELP_CODEGEN) print_help_codegeneration();
751 if (sections & HELP_LINKER) print_help_linker();
752 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
753 if (sections & HELP_DEBUG) print_help_debug();
754 if (sections & HELP_FIRM) print_help_firm();
757 static void set_be_option(const char *arg)
759 int res = be_parse_arg(arg);
764 static void copy_file(FILE *dest, FILE *input)
768 while (!feof(input) && !ferror(dest)) {
769 size_t read = fread(buf, 1, sizeof(buf), input);
770 if (fwrite(buf, 1, read, dest) != read) {
771 perror("couldn't write output");
776 static FILE *open_file(const char *filename)
778 if (streq(filename, "-")) {
782 FILE *in = fopen(filename, "r");
784 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
792 static filetype_t get_filetype_from_string(const char *string)
794 if (streq(string, "c") || streq(string, "c-header"))
796 if (streq(string, "c++") || streq(string, "c++-header"))
798 if (streq(string, "assembler"))
799 return FILETYPE_PREPROCESSED_ASSEMBLER;
800 if (streq(string, "assembler-with-cpp"))
801 return FILETYPE_ASSEMBLER;
802 if (streq(string, "none"))
803 return FILETYPE_AUTODETECT;
805 return FILETYPE_UNKNOWN;
808 static bool init_os_support(void)
810 const char *os = target_machine->operating_system;
811 wchar_atomic_kind = ATOMIC_TYPE_INT;
812 force_long_double_size = 0;
813 enable_main_collect2_hack = false;
814 define_intmax_types = false;
816 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
817 || streq(os, "solaris")) {
818 set_create_ld_ident(create_name_linux_elf);
819 } else if (streq(os, "darwin")) {
820 force_long_double_size = 16;
821 set_create_ld_ident(create_name_macho);
822 define_intmax_types = true;
823 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
824 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
825 enable_main_collect2_hack = true;
826 set_create_ld_ident(create_name_win32);
834 static bool parse_target_triple(const char *arg)
836 machine_triple_t *triple = firm_parse_machine_triple(arg);
837 if (triple == NULL) {
838 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
841 target_machine = triple;
845 static void setup_target_machine(void)
847 if (!setup_firm_for_machine(target_machine))
852 int main(int argc, char **argv)
856 const char *dumpfunction = NULL;
857 const char *print_file_name_file = NULL;
858 compile_mode_t mode = CompileAssembleLink;
860 int result = EXIT_SUCCESS;
861 char cpu_arch[16] = "ia32";
862 file_list_entry_t *files = NULL;
863 file_list_entry_t *last_file = NULL;
864 bool construct_dep_target = false;
865 bool do_timing = false;
866 bool profile_generate = false;
867 bool profile_use = false;
868 struct obstack file_obst;
870 atexit(free_temp_files);
872 /* hack for now... */
873 if (strstr(argv[0], "pptest") != NULL) {
874 extern int pptest_main(int argc, char **argv);
875 return pptest_main(argc, argv);
878 obstack_init(&cppflags_obst);
879 obstack_init(&ldflags_obst);
880 obstack_init(&asflags_obst);
881 obstack_init(&file_obst);
883 #define GET_ARG_AFTER(def, args) \
884 def = &arg[sizeof(args)-1]; \
885 if (def[0] == '\0') { \
888 fprintf(stderr, "error: expected argument after '" args "'\n"); \
889 argument_errors = true; \
893 if (def[0] == '-' && def[1] != '\0') { \
894 fprintf(stderr, "error: expected argument after '" args "'\n"); \
895 argument_errors = true; \
900 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
902 /* early options parsing (find out optimisation level and OS) */
903 for (int i = 1; i < argc; ++i) {
904 const char *arg = argv[i];
908 const char *option = &arg[1];
909 if (option[0] == 'O') {
910 sscanf(&option[1], "%d", &opt_level);
914 const char *target = getenv("TARGET");
916 parse_target_triple(target);
917 if (target_machine == NULL) {
918 target_machine = firm_get_host_machine();
920 choose_optimization_pack(opt_level);
921 setup_target_machine();
923 /* parse rest of options */
924 standard = STANDARD_DEFAULT;
925 unsigned features_on = 0;
926 unsigned features_off = 0;
927 filetype_t forced_filetype = FILETYPE_AUTODETECT;
928 help_sections_t help = HELP_NONE;
929 bool argument_errors = false;
930 for (int i = 1; i < argc; ++i) {
931 const char *arg = argv[i];
932 if (arg[0] == '-' && arg[1] != '\0') {
934 const char *option = &arg[1];
935 if (option[0] == 'o') {
936 GET_ARG_AFTER(outname, "-o");
937 } else if (option[0] == 'g') {
938 set_be_option("debuginfo=stabs");
939 set_be_option("omitfp=no");
940 set_be_option("ia32-nooptcc=yes");
941 } else if (SINGLE_OPTION('c')) {
942 mode = CompileAssemble;
943 } else if (SINGLE_OPTION('E')) {
944 mode = PreprocessOnly;
945 } else if (SINGLE_OPTION('S')) {
947 } else if (option[0] == 'O') {
949 } else if (option[0] == 'I') {
951 GET_ARG_AFTER(opt, "-I");
952 add_flag(&cppflags_obst, "-I%s", opt);
953 } else if (option[0] == 'D') {
955 GET_ARG_AFTER(opt, "-D");
956 add_flag(&cppflags_obst, "-D%s", opt);
957 } else if (option[0] == 'U') {
959 GET_ARG_AFTER(opt, "-U");
960 add_flag(&cppflags_obst, "-U%s", opt);
961 } else if (option[0] == 'l') {
963 GET_ARG_AFTER(opt, "-l");
964 add_flag(&ldflags_obst, "-l%s", opt);
965 } else if (option[0] == 'L') {
967 GET_ARG_AFTER(opt, "-L");
968 add_flag(&ldflags_obst, "-L%s", opt);
969 } else if (SINGLE_OPTION('v')) {
971 } else if (SINGLE_OPTION('w')) {
972 memset(&warning, 0, sizeof(warning));
973 } else if (option[0] == 'x') {
975 GET_ARG_AFTER(opt, "-x");
976 forced_filetype = get_filetype_from_string(opt);
977 if (forced_filetype == FILETYPE_UNKNOWN) {
978 fprintf(stderr, "Unknown language '%s'\n", opt);
979 argument_errors = true;
981 } else if (streq(option, "M")) {
982 mode = PreprocessOnly;
983 add_flag(&cppflags_obst, "-M");
984 } else if (streq(option, "MMD") ||
985 streq(option, "MD")) {
986 construct_dep_target = true;
987 add_flag(&cppflags_obst, "-%s", option);
988 } else if (streq(option, "MM") ||
989 streq(option, "MP")) {
990 add_flag(&cppflags_obst, "-%s", option);
991 } else if (streq(option, "MT") ||
992 streq(option, "MQ") ||
993 streq(option, "MF")) {
995 GET_ARG_AFTER(opt, "-MT");
996 add_flag(&cppflags_obst, "-%s", option);
997 add_flag(&cppflags_obst, "%s", opt);
998 } else if (streq(option, "include")) {
1000 GET_ARG_AFTER(opt, "-include");
1001 add_flag(&cppflags_obst, "-include");
1002 add_flag(&cppflags_obst, "%s", opt);
1003 } else if (streq(option, "isystem")) {
1005 GET_ARG_AFTER(opt, "-isystem");
1006 add_flag(&cppflags_obst, "-isystem");
1007 add_flag(&cppflags_obst, "%s", opt);
1008 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1009 } else if (streq(option, "pthread")) {
1010 /* set flags for the preprocessor */
1011 add_flag(&cppflags_obst, "-D_REENTRANT");
1012 /* set flags for the linker */
1013 add_flag(&ldflags_obst, "-lpthread");
1015 } else if (streq(option, "nostdinc")
1016 || streq(option, "trigraphs")) {
1017 /* pass these through to the preprocessor */
1018 add_flag(&cppflags_obst, "%s", arg);
1019 } else if (streq(option, "pipe")) {
1020 /* here for gcc compatibility */
1021 } else if (streq(option, "static")) {
1022 add_flag(&ldflags_obst, "-static");
1023 } else if (streq(option, "shared")) {
1024 add_flag(&ldflags_obst, "-shared");
1025 } else if (option[0] == 'f') {
1026 char const *orig_opt;
1027 GET_ARG_AFTER(orig_opt, "-f");
1029 if (strstart(orig_opt, "input-charset=")) {
1030 char const* const encoding = strchr(orig_opt, '=') + 1;
1031 select_input_encoding(encoding);
1032 } else if (strstart(orig_opt, "align-loops=") ||
1033 strstart(orig_opt, "align-jumps=") ||
1034 strstart(orig_opt, "visibility=") ||
1035 strstart(orig_opt, "align-functions=")) {
1036 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1037 } else if (strstart(orig_opt, "message-length=")) {
1038 /* ignore: would only affect error message format */
1040 /* -f options which have an -fno- variant */
1041 char const *opt = orig_opt;
1042 bool truth_value = true;
1043 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1044 truth_value = false;
1048 if (streq(opt, "builtins")) {
1049 use_builtins = truth_value;
1050 } else if (streq(opt, "dollars-in-identifiers")) {
1051 allow_dollar_in_symbol = truth_value;
1052 } else if (streq(opt, "omit-frame-pointer")) {
1053 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1054 } else if (streq(opt, "short-wchar")) {
1055 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1057 } else if (streq(opt, "signed-char")) {
1058 char_is_signed = truth_value;
1059 } else if (streq(opt, "strength-reduce")) {
1060 firm_option(truth_value ? "strength-red" : "no-strength-red");
1061 } else if (streq(opt, "syntax-only")) {
1062 mode = truth_value ? ParseOnly : CompileAssembleLink;
1063 } else if (streq(opt, "unsigned-char")) {
1064 char_is_signed = !truth_value;
1065 } else if (streq(opt, "freestanding")) {
1066 freestanding = truth_value;
1067 } else if (streq(opt, "hosted")) {
1068 freestanding = !truth_value;
1069 } else if (streq(opt, "profile-generate")) {
1070 profile_generate = truth_value;
1071 } else if (streq(opt, "profile-use")) {
1072 profile_use = truth_value;
1073 } else if (!truth_value &&
1074 streq(opt, "asynchronous-unwind-tables")) {
1075 /* nothing todo, a gcc feature which we don't support
1076 * anyway was deactivated */
1077 } else if (streq(orig_opt, "verbose-asm")) {
1078 /* ignore: we always print verbose assembler */
1079 } else if (streq(opt, "fast-math") ||
1080 streq(opt, "jump-tables") ||
1081 streq(opt, "expensive-optimizations") ||
1082 streq(opt, "common") ||
1083 streq(opt, "optimize-sibling-calls") ||
1084 streq(opt, "align-loops") ||
1085 streq(opt, "align-jumps") ||
1086 streq(opt, "align-functions") ||
1087 streq(opt, "PIC")) {
1088 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1089 } else if (streq(opt, "help")) {
1090 fprintf(stderr, "warning: -fhelp is deprecated\n");
1093 int res = firm_option(orig_opt);
1095 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1097 argument_errors = true;
1102 } else if (option[0] == 'b') {
1104 GET_ARG_AFTER(opt, "-b");
1106 if (streq(opt, "help")) {
1107 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1110 int res = be_parse_arg(opt);
1112 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1114 argument_errors = true;
1115 } else if (strstart(opt, "isa=")) {
1116 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1119 } else if (option[0] == 'W') {
1120 if (option[1] == '\0') {
1121 /* ignore -W, our defaults are already quite verbose */
1122 } else if (strstart(option + 1, "p,")) {
1123 // pass options directly to the preprocessor
1125 GET_ARG_AFTER(opt, "-Wp,");
1126 add_flag(&cppflags_obst, "-Wp,%s", opt);
1127 } else if (strstart(option + 1, "l,")) {
1128 // pass options directly to the linker
1130 GET_ARG_AFTER(opt, "-Wl,");
1131 add_flag(&ldflags_obst, "-Wl,%s", opt);
1132 } else if (streq(option + 1, "no-trigraphs")
1133 || streq(option + 1, "undef")) {
1134 add_flag(&cppflags_obst, "%s", arg);
1136 set_warning_opt(&option[1]);
1138 } else if (option[0] == 'm') {
1143 GET_ARG_AFTER(opt, "-m");
1144 if (strstart(opt, "target=")) {
1145 GET_ARG_AFTER(opt, "-mtarget=");
1146 if (!parse_target_triple(opt)) {
1147 argument_errors = true;
1149 setup_target_machine();
1150 target_triple = opt;
1152 } else if (strstart(opt, "triple=")) {
1153 GET_ARG_AFTER(opt, "-mtriple=");
1154 if (!parse_target_triple(opt)) {
1155 argument_errors = true;
1157 setup_target_machine();
1158 target_triple = opt;
1160 } else if (strstart(opt, "arch=")) {
1161 GET_ARG_AFTER(opt, "-march=");
1162 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1163 int res = be_parse_arg(arch_opt);
1165 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1166 argument_errors = true;
1168 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1169 int res = be_parse_arg(arch_opt);
1171 argument_errors = true;
1173 } else if (strstart(opt, "tune=")) {
1174 GET_ARG_AFTER(opt, "-mtune=");
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;
1179 } else if (strstart(opt, "cpu=")) {
1180 GET_ARG_AFTER(opt, "-mcpu=");
1181 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1182 int res = be_parse_arg(arch_opt);
1184 argument_errors = true;
1185 } else if (strstart(opt, "fpmath=")) {
1186 GET_ARG_AFTER(opt, "-mfpmath=");
1187 if (streq(opt, "387"))
1189 else if (streq(opt, "sse"))
1192 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
1193 argument_errors = true;
1195 if (!argument_errors) {
1196 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1197 int res = be_parse_arg(arch_opt);
1199 argument_errors = true;
1201 } else if (strstart(opt, "preferred-stack-boundary=")) {
1202 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1203 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1204 int res = be_parse_arg(arch_opt);
1206 argument_errors = true;
1207 } else if (streq(opt, "rtd")) {
1208 default_calling_convention = CC_STDCALL;
1209 } else if (strstart(opt, "regparm=")) {
1210 fprintf(stderr, "error: regparm convention not supported yet\n");
1211 argument_errors = true;
1212 } else if (streq(opt, "soft-float")) {
1213 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1214 argument_errors = true;
1216 long int value = strtol(opt, NULL, 10);
1218 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1219 argument_errors = true;
1220 } else if (value != 16 && value != 32 && value != 64) {
1221 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1222 argument_errors = true;
1224 machine_size = (unsigned int)value;
1225 add_flag(&asflags_obst, "-m%u", machine_size);
1226 add_flag(&ldflags_obst, "-m%u", machine_size);
1229 } else if (streq(option, "pg")) {
1230 set_be_option("gprof");
1231 add_flag(&ldflags_obst, "-pg");
1232 } else if (streq(option, "pedantic") ||
1233 streq(option, "ansi")) {
1234 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1235 } else if (strstart(option, "std=")) {
1236 const char *const o = &option[4];
1238 streq(o, "c++") ? STANDARD_CXX98 :
1239 streq(o, "c++98") ? STANDARD_CXX98 :
1240 streq(o, "c89") ? STANDARD_C89 :
1241 streq(o, "c99") ? STANDARD_C99 :
1242 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1243 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1244 streq(o, "gnu89") ? STANDARD_GNU89 :
1245 streq(o, "gnu99") ? STANDARD_GNU99 :
1246 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1247 streq(o, "iso9899:1990") ? STANDARD_C89 :
1248 streq(o, "iso9899:199409") ? STANDARD_C90 :
1249 streq(o, "iso9899:1999") ? STANDARD_C99 :
1250 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1251 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1252 } else if (streq(option, "version")) {
1253 print_cparser_version();
1254 } else if (strstart(option, "print-file-name=")) {
1255 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1256 } else if (option[0] == '-') {
1257 /* double dash option */
1259 if (streq(option, "gcc")) {
1260 features_on |= _GNUC;
1261 features_off &= ~_GNUC;
1262 } else if (streq(option, "no-gcc")) {
1263 features_on &= ~_GNUC;
1264 features_off |= _GNUC;
1265 } else if (streq(option, "ms")) {
1267 features_off &= ~_MS;
1268 } else if (streq(option, "no-ms")) {
1269 features_on &= ~_MS;
1270 features_off |= _MS;
1271 } else if (streq(option, "strict")) {
1273 } else if (streq(option, "lextest")) {
1275 } else if (streq(option, "benchmark")) {
1276 mode = BenchmarkParser;
1277 } else if (streq(option, "print-ast")) {
1279 } else if (streq(option, "print-implicit-cast")) {
1280 print_implicit_casts = true;
1281 } else if (streq(option, "print-parenthesis")) {
1282 print_parenthesis = true;
1283 } else if (streq(option, "print-fluffy")) {
1285 } else if (streq(option, "print-jna")) {
1287 } else if (streq(option, "jna-limit")) {
1290 fprintf(stderr, "error: "
1291 "expected argument after '--jna-limit'\n");
1292 argument_errors = true;
1295 jna_limit_output(argv[i]);
1296 } else if (streq(option, "jna-libname")) {
1299 fprintf(stderr, "error: "
1300 "expected argument after '--jna-libname'\n");
1301 argument_errors = true;
1304 jna_set_libname(argv[i]);
1305 } else if (streq(option, "time")) {
1307 } else if (streq(option, "version")) {
1308 print_cparser_version();
1309 return EXIT_SUCCESS;
1310 } else if (streq(option, "help")) {
1312 } else if (streq(option, "help-parser")) {
1313 help |= HELP_PARSER;
1314 } else if (streq(option, "help-warnings")) {
1315 help |= HELP_WARNINGS;
1316 } else if (streq(option, "help-codegen")) {
1317 help |= HELP_CODEGEN;
1318 } else if (streq(option, "help-linker")) {
1319 help |= HELP_LINKER;
1320 } else if (streq(option, "help-language-tools")) {
1321 help |= HELP_LANGUAGETOOLS;
1322 } else if (streq(option, "help-debug")) {
1324 } else if (streq(option, "help-firm")) {
1326 } else if (streq(option, "help-all")) {
1328 } else if (streq(option, "dump-function")) {
1331 fprintf(stderr, "error: "
1332 "expected argument after '--dump-function'\n");
1333 argument_errors = true;
1336 dumpfunction = argv[i];
1338 } else if (streq(option, "export-ir")) {
1339 mode = CompileExportIR;
1341 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1342 argument_errors = true;
1345 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1346 argument_errors = true;
1349 filetype_t type = forced_filetype;
1350 if (type == FILETYPE_AUTODETECT) {
1351 if (streq(arg, "-")) {
1352 /* - implicitly means C source file */
1355 const char *suffix = strrchr(arg, '.');
1356 /* Ensure there is at least one char before the suffix */
1357 if (suffix != NULL && suffix != arg) {
1360 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1361 streq(suffix, "a") ? FILETYPE_OBJECT :
1362 streq(suffix, "c") ? FILETYPE_C :
1363 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1364 streq(suffix, "C") ? FILETYPE_CXX :
1365 streq(suffix, "cc") ? FILETYPE_CXX :
1366 streq(suffix, "cp") ? FILETYPE_CXX :
1367 streq(suffix, "cpp") ? FILETYPE_CXX :
1368 streq(suffix, "CPP") ? FILETYPE_CXX :
1369 streq(suffix, "cxx") ? FILETYPE_CXX :
1370 streq(suffix, "c++") ? FILETYPE_CXX :
1371 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1372 streq(suffix, "h") ? FILETYPE_C :
1373 streq(suffix, "ir") ? FILETYPE_IR :
1374 streq(suffix, "o") ? FILETYPE_OBJECT :
1375 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1376 streq(suffix, "so") ? FILETYPE_OBJECT :
1377 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1382 file_list_entry_t *entry
1383 = obstack_alloc(&file_obst, sizeof(entry[0]));
1384 memset(entry, 0, sizeof(entry[0]));
1388 if (last_file != NULL) {
1389 last_file->next = entry;
1397 if (help != HELP_NONE) {
1398 print_help(argv[0], help);
1399 return !argument_errors;
1402 if (print_file_name_file != NULL) {
1403 print_file_name(print_file_name_file);
1404 return EXIT_SUCCESS;
1406 if (files == NULL) {
1407 fprintf(stderr, "error: no input files specified\n");
1408 argument_errors = true;
1411 if (argument_errors) {
1413 return EXIT_FAILURE;
1416 /* apply some effects from switches */
1417 c_mode |= features_on;
1418 c_mode &= ~features_off;
1419 if (profile_generate) {
1420 add_flag(&ldflags_obst, "-lfirmprof");
1421 set_be_option("profilegenerate");
1424 set_be_option("profileuse");
1428 byte_order_big_endian = be_get_backend_param()->byte_order_big_endian;
1429 init_symbol_table();
1442 if (construct_dep_target) {
1443 if (outname != 0 && strlen(outname) >= 2) {
1444 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1446 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1449 dep_target[0] = '\0';
1452 char outnamebuf[4096];
1453 if (outname == NULL) {
1454 const char *filename = files->name;
1457 case BenchmarkParser:
1462 case PreprocessOnly:
1467 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1468 outname = outnamebuf;
1470 case CompileAssemble:
1471 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1472 outname = outnamebuf;
1475 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1477 outname = outnamebuf;
1479 case CompileExportIR:
1480 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1481 outname = outnamebuf;
1483 case CompileAssembleLink:
1493 assert(outname != NULL);
1496 if (streq(outname, "-")) {
1499 out = fopen(outname, "w");
1501 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1503 return EXIT_FAILURE;
1507 file_list_entry_t *file;
1508 bool already_constructed_firm = false;
1509 for (file = files; file != NULL; file = file->next) {
1510 char asm_tempfile[1024];
1511 const char *filename = file->name;
1512 filetype_t filetype = file->type;
1514 if (filetype == FILETYPE_OBJECT)
1518 if (mode == LexTest) {
1520 in = open_file(filename);
1521 lextest(in, filename);
1523 return EXIT_SUCCESS;
1526 FILE *preprocessed_in = NULL;
1527 filetype_t next_filetype = filetype;
1530 next_filetype = FILETYPE_PREPROCESSED_C;
1533 next_filetype = FILETYPE_PREPROCESSED_CXX;
1535 case FILETYPE_ASSEMBLER:
1536 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1539 /* no support for input on FILE* yet */
1541 panic("internal compiler error: in for preprocessor != NULL");
1543 preprocessed_in = preprocess(filename, filetype);
1544 if (mode == PreprocessOnly) {
1545 copy_file(out, preprocessed_in);
1546 int result = pclose(preprocessed_in);
1548 /* remove output file in case of error */
1549 if (out != stdout && result != EXIT_SUCCESS) {
1555 in = preprocessed_in;
1556 filetype = next_filetype;
1564 if (mode == Compile) {
1567 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1571 in = open_file(filename);
1573 /* preprocess and compile */
1574 if (filetype == FILETYPE_PREPROCESSED_C) {
1575 char const* invalid_mode;
1578 case STANDARD_C89: c_mode = _C89; break;
1579 /* TODO determine difference between these two */
1580 case STANDARD_C90: c_mode = _C89; break;
1581 case STANDARD_C99: c_mode = _C89 | _C99; break;
1582 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1586 "warning: command line option \"-std=%s\" is not valid for C\n",
1589 case STANDARD_DEFAULT:
1590 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1592 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1593 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1596 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1597 char const* invalid_mode;
1599 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1600 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1601 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1602 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1603 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1606 case STANDARD_CXX98: c_mode = _CXX; break;
1610 "warning: command line option \"-std=%s\" is not valid for C++\n",
1612 case STANDARD_DEFAULT:
1613 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1617 c_mode |= features_on;
1618 c_mode &= ~features_off;
1620 /* do the actual parsing */
1621 ir_timer_t *t_parsing = ir_timer_new();
1622 timer_register(t_parsing, "Frontend: Parsing");
1623 timer_push(t_parsing);
1625 translation_unit_t *const unit = do_parsing(in, filename);
1626 timer_pop(t_parsing);
1628 /* prints the AST even if errors occurred */
1629 if (mode == PrintAst) {
1634 if (error_count > 0) {
1635 /* parsing failed because of errors */
1636 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1638 result = EXIT_FAILURE;
1640 } else if (warning_count > 0) {
1641 fprintf(stderr, "%u warning(s)\n", warning_count);
1644 if (in == preprocessed_in) {
1645 int pp_result = pclose(preprocessed_in);
1646 if (pp_result != EXIT_SUCCESS) {
1647 /* remove output file */
1650 return EXIT_FAILURE;
1654 if (mode == BenchmarkParser) {
1656 } else if (mode == PrintFluffy) {
1657 write_fluffy_decls(out, unit);
1659 } else if (mode == PrintJna) {
1660 write_jna_decls(out, unit);
1664 /* build the firm graph */
1665 ir_timer_t *t_construct = ir_timer_new();
1666 timer_register(t_construct, "Frontend: Graph construction");
1667 timer_push(t_construct);
1668 if (already_constructed_firm) {
1669 panic("compiling multiple files/translation units not possible");
1671 translation_unit_to_firm(unit);
1672 already_constructed_firm = true;
1673 timer_pop(t_construct);
1676 if (mode == ParseOnly) {
1680 if (mode == CompileDump) {
1682 ident *id = new_id_from_str(dumpfunction);
1683 ir_graph *irg = NULL;
1684 int n_irgs = get_irp_n_irgs();
1685 for (int i = 0; i < n_irgs; ++i) {
1686 ir_graph *tirg = get_irp_irg(i);
1687 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1695 fprintf(stderr, "No graph for function '%s' found\n",
1697 return EXIT_FAILURE;
1700 dump_ir_graph_file(out, irg);
1702 return EXIT_SUCCESS;
1705 if (mode == CompileExportIR) {
1708 return EXIT_SUCCESS;
1711 gen_firm_finish(asm_out, filename);
1712 if (asm_out != out) {
1715 } else if (filetype == FILETYPE_IR) {
1717 ir_import(filename);
1719 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1720 copy_file(asm_out, in);
1721 if (in == preprocessed_in) {
1722 int pp_result = pclose(preprocessed_in);
1723 if (pp_result != EXIT_SUCCESS) {
1724 /* remove output in error case */
1730 if (asm_out != out) {
1735 if (mode == Compile)
1738 /* if we're here then we have preprocessed assembly */
1739 filename = asm_tempfile;
1740 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1743 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1745 const char *filename_o;
1746 if (mode == CompileAssemble) {
1748 filename_o = outname;
1750 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1755 assemble(filename_o, filename);
1757 size_t len = strlen(filename_o) + 1;
1758 filename = obstack_copy(&file_obst, filename_o, len);
1759 filetype = FILETYPE_OBJECT;
1762 /* ok we're done here, process next file */
1763 file->name = filename;
1764 file->type = filetype;
1767 if (result != EXIT_SUCCESS) {
1773 /* link program file */
1774 if (mode == CompileAssembleLink) {
1775 obstack_1grow(&ldflags_obst, '\0');
1776 const char *flags = obstack_finish(&ldflags_obst);
1778 /* construct commandline */
1779 const char *linker = getenv("CPARSER_LINK");
1780 if (linker != NULL) {
1781 obstack_printf(&file_obst, "%s ", linker);
1783 if (target_triple != NULL)
1784 obstack_printf(&file_obst, "%s-", target_triple);
1785 obstack_printf(&file_obst, "%s ", LINKER);
1788 for (file_list_entry_t *entry = files; entry != NULL;
1789 entry = entry->next) {
1790 if (entry->type != FILETYPE_OBJECT)
1793 add_flag(&file_obst, "%s", entry->name);
1796 add_flag(&file_obst, "-o");
1797 add_flag(&file_obst, outname);
1798 obstack_printf(&file_obst, "%s", flags);
1799 obstack_1grow(&file_obst, '\0');
1801 char *commandline = obstack_finish(&file_obst);
1806 int err = system(commandline);
1807 if (err != EXIT_SUCCESS) {
1808 fprintf(stderr, "linker reported an error\n");
1809 return EXIT_FAILURE;
1816 obstack_free(&cppflags_obst, NULL);
1817 obstack_free(&ldflags_obst, NULL);
1818 obstack_free(&asflags_obst, NULL);
1819 obstack_free(&file_obst, NULL);
1829 exit_symbol_table();
1830 return EXIT_SUCCESS;