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_timing.h"
74 #include "driver/firm_machine.h"
75 #include "adt/error.h"
76 #include "adt/strutil.h"
77 #include "wrappergen/write_fluffy.h"
78 #include "wrappergen/write_jna.h"
87 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
89 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
98 #define ASSEMBLER "gcc -c -xassembler"
101 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
102 unsigned int machine_size = 32;
103 bool byte_order_big_endian = false;
104 bool char_is_signed = true;
105 bool strict_mode = false;
106 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
107 unsigned long_double_size = 0;
108 bool enable_main_collect2_hack = false;
109 bool freestanding = false;
111 static machine_triple_t *target_machine;
112 static const char *target_triple;
114 static struct obstack cppflags_obst;
115 static struct obstack ldflags_obst;
116 static struct obstack asflags_obst;
117 static char dep_target[1024];
118 static const char *outname;
119 static bool define_intmax_types;
121 typedef enum lang_standard_t {
122 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
123 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
124 STANDARD_C89, /* ISO C90 (sic) */
125 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
126 STANDARD_C99, /* ISO C99 */
127 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
128 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
129 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
130 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
133 static lang_standard_t standard;
135 typedef struct file_list_entry_t file_list_entry_t;
137 typedef enum filetype_t {
140 FILETYPE_PREPROCESSED_C,
142 FILETYPE_PREPROCESSED_CXX,
144 FILETYPE_PREPROCESSED_ASSEMBLER,
150 struct file_list_entry_t {
151 const char *name; /**< filename or NULL for stdin */
153 file_list_entry_t *next;
156 static file_list_entry_t *temp_files;
158 static void get_output_name(char *buf, size_t buflen, const char *inputname,
161 if (inputname == NULL)
164 char const *const last_slash = strrchr(inputname, '/');
165 char const *const filename =
166 last_slash != NULL ? last_slash + 1 : inputname;
167 char const *const last_dot = strrchr(filename, '.');
168 char const *const name_end =
169 last_dot != NULL ? last_dot : strchr(filename, '\0');
171 int const len = snprintf(buf, buflen, "%.*s%s",
172 (int)(name_end - filename), filename, newext);
174 if (len < 0 || buflen <= (size_t)len)
176 if (buflen <= (size_t)len)
178 panic("filename too long");
181 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
185 lexer_open_stream(in, input_name);
188 translation_unit_t *unit = finish_parsing();
192 static void lextest(FILE *in, const char *fname)
194 lexer_open_stream(in, fname);
197 lexer_next_preprocessing_token();
198 print_token(stdout, &lexer_token);
200 } while (lexer_token.type != T_EOF);
203 static void add_flag(struct obstack *obst, const char *format, ...)
208 va_start(ap, format);
212 vsnprintf(buf, sizeof(buf), format, ap);
215 obstack_1grow(obst, ' ');
217 obstack_1grow(obst, '"');
218 obstack_grow(obst, buf, len);
219 obstack_1grow(obst, '"');
221 /* escape stuff... */
222 for (char *c = buf; *c != '\0'; ++c) {
240 obstack_1grow(obst, '\\');
243 obstack_1grow(obst, *c);
250 static const char *type_to_string(type_t *type)
252 assert(type->kind == TYPE_ATOMIC);
253 return get_atomic_kind_name(type->atomic.akind);
256 static FILE *preprocess(const char *fname, filetype_t filetype)
258 static const char *common_flags = NULL;
260 if (common_flags == NULL) {
261 obstack_1grow(&cppflags_obst, '\0');
262 const char *flags = obstack_finish(&cppflags_obst);
264 /* setup default defines */
265 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
266 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
267 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
268 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
270 add_flag(&cppflags_obst, "-U__VERSION__");
271 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
273 if (define_intmax_types) {
274 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
275 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
276 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
277 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
280 if (flags[0] != '\0') {
281 size_t len = strlen(flags);
282 obstack_1grow(&cppflags_obst, ' ');
283 obstack_grow(&cppflags_obst, flags, len);
285 obstack_1grow(&cppflags_obst, '\0');
286 common_flags = obstack_finish(&cppflags_obst);
289 assert(obstack_object_size(&cppflags_obst) == 0);
291 const char *preprocessor = getenv("CPARSER_PP");
292 if (preprocessor != NULL) {
293 obstack_printf(&cppflags_obst, "%s ", preprocessor);
295 if (target_triple != NULL)
296 obstack_printf(&cppflags_obst, "%s-", target_triple);
297 obstack_printf(&cppflags_obst, PREPROCESSOR);
302 add_flag(&cppflags_obst, "-std=c99");
305 add_flag(&cppflags_obst, "-std=c++98");
307 case FILETYPE_ASSEMBLER:
308 add_flag(&cppflags_obst, "-x");
309 add_flag(&cppflags_obst, "assembler-with-cpp");
314 obstack_printf(&cppflags_obst, "%s", common_flags);
316 /* handle dependency generation */
317 if (dep_target[0] != '\0') {
318 add_flag(&cppflags_obst, "-MF");
319 add_flag(&cppflags_obst, dep_target);
320 if (outname != NULL) {
321 add_flag(&cppflags_obst, "-MQ");
322 add_flag(&cppflags_obst, outname);
325 add_flag(&cppflags_obst, fname);
326 obstack_1grow(&cppflags_obst, '\0');
328 char *commandline = obstack_finish(&cppflags_obst);
332 FILE *f = popen(commandline, "r");
334 fprintf(stderr, "invoking preprocessor failed\n");
337 /* we do not really need that anymore */
338 obstack_free(&cppflags_obst, commandline);
343 static void assemble(const char *out, const char *in)
345 obstack_1grow(&asflags_obst, '\0');
346 const char *flags = obstack_finish(&asflags_obst);
348 const char *assembler = getenv("CPARSER_AS");
349 if (assembler != NULL) {
350 obstack_printf(&asflags_obst, "%s", assembler);
352 if (target_triple != NULL)
353 obstack_printf(&asflags_obst, "%s-", target_triple);
354 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
356 if (flags[0] != '\0')
357 obstack_printf(&asflags_obst, " %s", flags);
359 obstack_printf(&asflags_obst, " %s -o %s", in, out);
360 obstack_1grow(&asflags_obst, '\0');
362 char *commandline = obstack_finish(&asflags_obst);
366 int err = system(commandline);
367 if (err != EXIT_SUCCESS) {
368 fprintf(stderr, "assembler reported an error\n");
371 obstack_free(&asflags_obst, commandline);
374 static void print_file_name(const char *file)
376 add_flag(&ldflags_obst, "-print-file-name=%s", file);
378 obstack_1grow(&ldflags_obst, '\0');
379 const char *flags = obstack_finish(&ldflags_obst);
381 /* construct commandline */
382 const char *linker = getenv("CPARSER_LINK");
383 if (linker != NULL) {
384 obstack_printf(&ldflags_obst, "%s ", linker);
386 if (target_triple != NULL)
387 obstack_printf(&ldflags_obst, "%s-", target_triple);
388 obstack_printf(&ldflags_obst, "%s ", LINKER);
390 obstack_printf(&ldflags_obst, "%s ", linker);
391 obstack_printf(&ldflags_obst, "%s", flags);
392 obstack_1grow(&ldflags_obst, '\0');
394 char *commandline = obstack_finish(&ldflags_obst);
398 int err = system(commandline);
399 if (err != EXIT_SUCCESS) {
400 fprintf(stderr, "linker reported an error\n");
403 obstack_free(&ldflags_obst, commandline);
406 static const char *try_dir(const char *dir)
410 if (access(dir, R_OK | W_OK | X_OK) == 0)
415 static const char *get_tempdir(void)
417 static const char *tmpdir = NULL;
423 tmpdir = try_dir(getenv("TMPDIR"));
425 tmpdir = try_dir(getenv("TMP"));
427 tmpdir = try_dir(getenv("TEMP"));
431 tmpdir = try_dir(P_tmpdir);
435 tmpdir = try_dir("/var/tmp");
437 tmpdir = try_dir("/usr/tmp");
439 tmpdir = try_dir("/tmp");
448 /* cheap and nasty mkstemp replacement */
449 static int mkstemp(char *templ)
452 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
457 * an own version of tmpnam, which: writes in a buffer, emits no warnings
458 * during linking (like glibc/gnu ld do for tmpnam)...
460 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
462 const char *tempdir = get_tempdir();
464 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
466 int fd = mkstemp(buffer);
468 fprintf(stderr, "could not create temporary file: %s\n",
472 FILE *out = fdopen(fd, "w");
474 fprintf(stderr, "could not create temporary file FILE*\n");
478 file_list_entry_t *entry = xmalloc(sizeof(*entry));
479 memset(entry, 0, sizeof(*entry));
481 size_t name_len = strlen(buffer) + 1;
482 char *name = malloc(name_len);
483 memcpy(name, buffer, name_len);
486 entry->next = temp_files;
492 static void free_temp_files(void)
494 file_list_entry_t *entry = temp_files;
495 file_list_entry_t *next;
496 for ( ; entry != NULL; entry = next) {
500 free((char*) entry->name);
506 typedef enum compile_mode_t {
521 static void usage(const char *argv0)
523 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
526 static void print_cparser_version(void)
528 printf("cparser (%s) using libFirm (%u.%u",
529 cparser_REVISION, ir_get_version_major(),
530 ir_get_version_minor());
532 const char *revision = ir_get_version_revision();
533 if (revision[0] != 0) {
535 fputs(revision, stdout);
538 const char *build = ir_get_version_build();
541 fputs(build, stdout);
544 puts("This is free software; see the source for copying conditions. There is NO\n"
545 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
548 static void print_help_basic(const char *argv0)
552 put_help("--help", "Display this information");
553 put_help("--version", "Display compiler version");
554 put_help("--help-parser", "Display information about parser options");
555 put_help("--help-warnings", "Display information about warning options");
556 put_help("--help-codegen", "Display information about code-generation options");
557 put_help("--help-optimization", "Display information about optimization options");
558 put_help("--help-linker", "Display information about linker options");
559 put_help("--help-language-tools", "Display information about language tools options");
560 put_help("--help-debug", "Display information about compiler debugging options");
561 put_help("--help-firm", "Display information about direct firm options");
562 put_help("--help-all", "Display information about all options");
563 put_help("-c", "Compile and assemble but do not link");
564 put_help("-E", "Preprocess only");
565 put_help("-S", "Compile but do not assembler or link");
566 put_help("-o", "Specify output file");
567 put_help("-v", "Verbose output (show invocation of sub-processes)");
568 put_help("-x", "Force input language:");
569 put_choice("c", "C");
570 put_choice("c++", "C++");
571 put_choice("assembler", "Assembler (no preprocessing)");
572 put_choice("assembler-with-cpp", "Assembler with preprocessing");
573 put_choice("none", "Autodetection");
574 put_help("-pipe", "Ignored (gcc compatibility)");
577 static void print_help_preprocessor(void)
579 put_help("-nostdinc", "Do not search standard system include directories");
580 put_help("-trigraphs", "Support ISO C trigraphs");
581 put_help("-isystem", "");
582 put_help("-include", "");
583 put_help("-I PATH", "");
584 put_help("-D SYMBOL[=value]", "");
585 put_help("-U SYMBOL", "");
586 put_help("-Wp,OPTION", "Pass option directly to preprocessor");
589 put_help("-MMD", "");
597 static void print_help_parser(void)
599 put_help("-finput-charset=CHARSET", "Select encoding of input files");
600 put_help("-fmessage-length=LEN", "Ignored (gcc compatibility)");
601 put_help("-fshort-wchar", "Type \"wchar_t\" is unsigned short instead of int");
602 put_help("-fshow-column", "Show the column number in diagnostic messages");
603 put_help("-fsigned-char", "Type \"char\" is a signed type");
604 put_help("-funsigned-char", "Type \"char\" is an unsigned type");
605 put_help("--ms", "Enable msvc extensions");
606 put_help("--no-ms", "Disable msvc extensions");
607 put_help("--gcc", "Enable gcc extensions");
608 put_help("--no-gcc", "Disable gcc extensions");
609 put_help("-std=STANDARD", "Specify language standard:");
610 put_choice("c99", "ISO C99 standard");
611 put_choice("c89", "ISO C89 standard");
612 put_choice("c9x", "Deprecated");
613 put_choice("c++", "ISO C++ 98");
614 put_choice("c++98", "ISO C++ 98");
615 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
616 put_choice("gnu89", "ISO C89 + GNU extensions");
617 put_choice("gnu9x", "Deprecated");
618 put_choice("iso9899:1990", "ISO C89");
619 put_choice("iso9899:199409", "ISO C90");
620 put_choice("iso9899:1999", "ISO C99");
621 put_choice("iso9899:199x", "Deprecated");
622 put_help("-pedantic", "Ignored (gcc compatibility)");
623 put_help("-ansi", "Ignored (gcc compatibility)");
624 put_help("--strict", "Enable strict conformance checking");
627 static void print_help_warnings(void)
629 put_help("-f[no-]diagnostics-show-option", "Show the switch, which controls a warning, after each warning");
630 put_help("-w", "Disable all warnings");
631 put_help("-Wno-trigraphs", "Warn if input contains trigraphs");
632 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
633 put_help("-Winit-self", "Ignored (gcc compatibility)");
634 print_warning_opt_help();
637 static void print_help_optimization(void)
639 put_help("-O LEVEL", "Select optimization level (0-4)");
640 firm_option_help(put_help);
641 put_help("-fexpensive-optimizations","Ignored (gcc compatibility)");
644 static void print_help_codegeneration(void)
646 put_help("-g", "Generate debug information");
647 put_help("-pg", "Instrument code for gnu gprof");
648 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
649 put_help("-ffreestanding", "Compile in freestanding mode (see ISO C standard)");
650 put_help("-fhosted", "Compile in hosted (not freestanding) mode");
651 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
652 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
653 put_help("-ffp-precise", "Precise floating point model");
654 put_help("-ffp-fast", "Imprecise floating point model");
655 put_help("-ffp-strict", "Strict floating point model");
656 put_help("-pthread", "Use pthread threading library");
657 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
658 put_help("-mtriple=TARGET", "Alias for -mtarget (clang compatibility)");
659 put_help("-march=ARCH", "");
660 put_help("-mtune=ARCH", "");
661 put_help("-mcpu=CPU", "");
662 put_help("-mfpmath=", "");
663 put_help("-mpreferred-stack-boundary=", "");
664 put_help("-mrtd", "");
665 put_help("-mregparm=", "Not supported yet");
666 put_help("-msoft-float", "Not supported yet");
667 put_help("-m32", "Generate 32bit code");
668 put_help("-m64", "Generate 64bit code");
669 put_help("-fverbose-asm", "Ignored (gcc compatibility)");
670 put_help("-fjump-tables", "Ignored (gcc compatibility)");
671 put_help("-fcommon", "Ignored (gcc compatibility)");
672 put_help("-foptimize-sibling-calls", "Ignored (gcc compatibility)");
673 put_help("-falign-loops", "Ignored (gcc compatibility)");
674 put_help("-falign-jumps", "Ignored (gcc compatibility)");
675 put_help("-falign-functions", "Ignored (gcc compatibility)");
676 put_help("-fPIC", "Ignored (gcc compatibility)");
677 put_help("-ffast-math", "Same as -ffp-fast (gcc compatibility)");
679 puts("\tMost of these options can be used with a no- prefix to disable them");
680 puts("\te.g. -fno-omit-frame-pointer");
683 static void print_help_linker(void)
685 put_help("-l LIBRARY", "");
686 put_help("-L PATH", "");
687 put_help("-shared", "Produce a shared library");
688 put_help("-static", "Produce statically linked binary");
689 put_help("-Wl,OPTION", "Pass option directly to linker");
692 static void print_help_debug(void)
694 put_help("--lextest", "Preprocess and tokenize only");
695 put_help("--print-ast", "Preprocess, parse and print AST");
696 put_help("--print-implicit-cast", "");
697 put_help("--print-parenthesis", "");
698 put_help("--benchmark", "Preprocess and parse, produces no output");
699 put_help("--time", "Measure time of compiler passes");
700 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
701 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
704 static void print_help_language_tools(void)
706 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
707 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
708 put_help("--jna-limit filename", "");
709 put_help("--jna-libname name", "");
712 static void print_help_firm(void)
714 put_help("-bOPTION", "Directly pass option to libFirm backend");
715 int res = be_parse_arg("help");
722 HELP_BASIC = 1u << 0,
723 HELP_PREPROCESSOR = 1u << 1,
724 HELP_PARSER = 1u << 2,
725 HELP_WARNINGS = 1u << 3,
726 HELP_OPTIMIZATION = 1u << 4,
727 HELP_CODEGEN = 1u << 5,
728 HELP_LINKER = 1u << 6,
729 HELP_LANGUAGETOOLS = 1u << 7,
730 HELP_DEBUG = 1u << 8,
733 HELP_ALL = (unsigned)-1
736 static void print_help(const char *argv0, help_sections_t sections)
738 if (sections & HELP_BASIC) print_help_basic(argv0);
739 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
740 if (sections & HELP_PARSER) print_help_parser();
741 if (sections & HELP_WARNINGS) print_help_warnings();
742 if (sections & HELP_OPTIMIZATION) print_help_optimization();
743 if (sections & HELP_CODEGEN) print_help_codegeneration();
744 if (sections & HELP_LINKER) print_help_linker();
745 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
746 if (sections & HELP_DEBUG) print_help_debug();
747 if (sections & HELP_FIRM) print_help_firm();
750 static void set_be_option(const char *arg)
752 int res = be_parse_arg(arg);
757 static void copy_file(FILE *dest, FILE *input)
761 while (!feof(input) && !ferror(dest)) {
762 size_t read = fread(buf, 1, sizeof(buf), input);
763 if (fwrite(buf, 1, read, dest) != read) {
764 perror("could not write output");
769 static FILE *open_file(const char *filename)
771 if (streq(filename, "-")) {
775 FILE *in = fopen(filename, "r");
777 fprintf(stderr, "Could not open '%s': %s\n", filename,
785 static filetype_t get_filetype_from_string(const char *string)
787 if (streq(string, "c") || streq(string, "c-header"))
789 if (streq(string, "c++") || streq(string, "c++-header"))
791 if (streq(string, "assembler"))
792 return FILETYPE_PREPROCESSED_ASSEMBLER;
793 if (streq(string, "assembler-with-cpp"))
794 return FILETYPE_ASSEMBLER;
795 if (streq(string, "none"))
796 return FILETYPE_AUTODETECT;
798 return FILETYPE_UNKNOWN;
801 static bool init_os_support(void)
803 const char *os = target_machine->operating_system;
804 wchar_atomic_kind = ATOMIC_TYPE_INT;
805 enable_main_collect2_hack = false;
806 define_intmax_types = false;
808 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
809 || streq(os, "solaris")) {
810 set_create_ld_ident(create_name_linux_elf);
811 } else if (streq(os, "darwin")) {
812 long_double_size = 16;
813 set_create_ld_ident(create_name_macho);
814 define_intmax_types = true;
815 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
816 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
817 enable_main_collect2_hack = true;
818 set_create_ld_ident(create_name_win32);
826 static bool parse_target_triple(const char *arg)
828 machine_triple_t *triple = firm_parse_machine_triple(arg);
829 if (triple == NULL) {
830 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
833 target_machine = triple;
837 static void setup_target_machine(void)
839 if (!setup_firm_for_machine(target_machine))
842 const backend_params *be_params = be_get_backend_param();
843 if (be_params->long_double_size % 8 != 0) {
844 fprintf(stderr, "firm-target long double size is not a multiple of 8, cannot handle this\n");
848 byte_order_big_endian = be_params->byte_order_big_endian;
849 machine_size = be_params->machine_size;
850 long_double_size = be_params->long_double_size / 8;
855 int main(int argc, char **argv)
859 const char *dumpfunction = NULL;
860 const char *print_file_name_file = NULL;
861 compile_mode_t mode = CompileAssembleLink;
863 int result = EXIT_SUCCESS;
864 char cpu_arch[16] = "ia32";
865 file_list_entry_t *files = NULL;
866 file_list_entry_t *last_file = NULL;
867 bool construct_dep_target = false;
868 bool do_timing = false;
869 bool profile_generate = false;
870 bool profile_use = false;
871 struct obstack file_obst;
873 atexit(free_temp_files);
875 /* hack for now... */
876 if (strstr(argv[0], "pptest") != NULL) {
877 extern int pptest_main(int argc, char **argv);
878 return pptest_main(argc, argv);
881 obstack_init(&cppflags_obst);
882 obstack_init(&ldflags_obst);
883 obstack_init(&asflags_obst);
884 obstack_init(&file_obst);
886 #define GET_ARG_AFTER(def, args) \
888 def = &arg[sizeof(args)-1]; \
889 if (def[0] == '\0') { \
892 fprintf(stderr, "error: expected argument after '" args "'\n"); \
893 argument_errors = true; \
897 if (def[0] == '-' && def[1] != '\0') { \
898 fprintf(stderr, "error: expected argument after '" args "'\n"); \
899 argument_errors = true; \
905 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
907 /* early options parsing (find out optimization level and OS) */
908 for (int i = 1; i < argc; ++i) {
909 const char *arg = argv[i];
913 const char *option = &arg[1];
914 if (option[0] == 'O') {
915 sscanf(&option[1], "%d", &opt_level);
919 const char *target = getenv("TARGET");
921 parse_target_triple(target);
922 if (target_machine == NULL) {
923 target_machine = firm_get_host_machine();
925 choose_optimization_pack(opt_level);
926 setup_target_machine();
928 /* parse rest of options */
929 standard = STANDARD_DEFAULT;
930 unsigned features_on = 0;
931 unsigned features_off = 0;
932 filetype_t forced_filetype = FILETYPE_AUTODETECT;
933 help_sections_t help = HELP_NONE;
934 bool argument_errors = false;
935 for (int i = 1; i < argc; ++i) {
936 const char *arg = argv[i];
937 if (arg[0] == '-' && arg[1] != '\0') {
939 const char *option = &arg[1];
940 if (option[0] == 'o') {
941 GET_ARG_AFTER(outname, "-o");
942 } else if (option[0] == 'g') {
943 set_be_option("debuginfo=stabs");
944 set_be_option("omitfp=no");
945 set_be_option("ia32-nooptcc=yes");
946 } else if (SINGLE_OPTION('c')) {
947 mode = CompileAssemble;
948 } else if (SINGLE_OPTION('E')) {
949 mode = PreprocessOnly;
950 } else if (SINGLE_OPTION('S')) {
952 } else if (option[0] == 'O') {
954 } else if (option[0] == 'I') {
956 GET_ARG_AFTER(opt, "-I");
957 add_flag(&cppflags_obst, "-I%s", opt);
958 } else if (option[0] == 'D') {
960 GET_ARG_AFTER(opt, "-D");
961 add_flag(&cppflags_obst, "-D%s", opt);
962 } else if (option[0] == 'U') {
964 GET_ARG_AFTER(opt, "-U");
965 add_flag(&cppflags_obst, "-U%s", opt);
966 } else if (option[0] == 'l') {
968 GET_ARG_AFTER(opt, "-l");
969 add_flag(&ldflags_obst, "-l%s", opt);
970 } else if (option[0] == 'L') {
972 GET_ARG_AFTER(opt, "-L");
973 add_flag(&ldflags_obst, "-L%s", opt);
974 } else if (SINGLE_OPTION('v')) {
976 } else if (SINGLE_OPTION('w')) {
977 disable_all_warnings();
978 } else if (option[0] == 'x') {
980 GET_ARG_AFTER(opt, "-x");
981 forced_filetype = get_filetype_from_string(opt);
982 if (forced_filetype == FILETYPE_UNKNOWN) {
983 fprintf(stderr, "Unknown language '%s'\n", opt);
984 argument_errors = true;
986 } else if (streq(option, "M")) {
987 mode = PreprocessOnly;
988 add_flag(&cppflags_obst, "-M");
989 } else if (streq(option, "MMD") ||
990 streq(option, "MD")) {
991 construct_dep_target = true;
992 add_flag(&cppflags_obst, "-%s", option);
993 } else if (streq(option, "MM") ||
994 streq(option, "MP")) {
995 add_flag(&cppflags_obst, "-%s", option);
996 } else if (streq(option, "MT") ||
997 streq(option, "MQ") ||
998 streq(option, "MF")) {
1000 GET_ARG_AFTER(opt, "-MT");
1001 add_flag(&cppflags_obst, "-%s", option);
1002 add_flag(&cppflags_obst, "%s", opt);
1003 } else if (streq(option, "include")) {
1005 GET_ARG_AFTER(opt, "-include");
1006 add_flag(&cppflags_obst, "-include");
1007 add_flag(&cppflags_obst, "%s", opt);
1008 } else if (streq(option, "isystem")) {
1010 GET_ARG_AFTER(opt, "-isystem");
1011 add_flag(&cppflags_obst, "-isystem");
1012 add_flag(&cppflags_obst, "%s", opt);
1013 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1014 } else if (streq(option, "pthread")) {
1015 /* set flags for the preprocessor */
1016 add_flag(&cppflags_obst, "-D_REENTRANT");
1017 /* set flags for the linker */
1018 add_flag(&ldflags_obst, "-lpthread");
1020 } else if (streq(option, "nostdinc")
1021 || streq(option, "trigraphs")) {
1022 /* pass these through to the preprocessor */
1023 add_flag(&cppflags_obst, "%s", arg);
1024 } else if (streq(option, "pipe")) {
1025 /* here for gcc compatibility */
1026 } else if (streq(option, "static")) {
1027 add_flag(&ldflags_obst, "-static");
1028 } else if (streq(option, "shared")) {
1029 add_flag(&ldflags_obst, "-shared");
1030 } else if (option[0] == 'f') {
1031 char const *orig_opt;
1032 GET_ARG_AFTER(orig_opt, "-f");
1034 if (strstart(orig_opt, "input-charset=")) {
1035 char const* const encoding = strchr(orig_opt, '=') + 1;
1036 select_input_encoding(encoding);
1037 } else if (strstart(orig_opt, "align-loops=") ||
1038 strstart(orig_opt, "align-jumps=") ||
1039 strstart(orig_opt, "align-functions=")) {
1040 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1041 } else if (strstart(orig_opt, "visibility=")) {
1042 const char *val = strchr(orig_opt, '=')+1;
1043 elf_visibility_tag_t visibility
1044 = get_elf_visibility_from_string(val);
1045 if (visibility == ELF_VISIBILITY_ERROR) {
1046 fprintf(stderr, "invalid visibility '%s' specified\n",
1048 argument_errors = true;
1050 set_default_visibility(visibility);
1052 } else if (strstart(orig_opt, "message-length=")) {
1053 /* ignore: would only affect error message format */
1054 } else if (streq(orig_opt, "fast-math") ||
1055 streq(orig_opt, "fp-fast")) {
1056 firm_fp_model = fp_model_fast;
1057 } else if (streq(orig_opt, "fp-precise")) {
1058 firm_fp_model = fp_model_precise;
1059 } else if (streq(orig_opt, "fp-strict")) {
1060 firm_fp_model = fp_model_strict;
1061 } else if (streq(orig_opt, "help")) {
1062 fprintf(stderr, "warning: -fhelp is deprecated\n");
1063 help |= HELP_OPTIMIZATION;
1065 /* -f options which have an -fno- variant */
1066 char const *opt = orig_opt;
1067 bool truth_value = true;
1068 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1069 truth_value = false;
1073 if (streq(opt, "diagnostics-show-option")) {
1074 diagnostics_show_option = truth_value;
1075 } else if (streq(opt, "dollars-in-identifiers")) {
1076 allow_dollar_in_symbol = truth_value;
1077 } else if (streq(opt, "omit-frame-pointer")) {
1078 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1079 } else if (streq(opt, "short-wchar")) {
1080 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1082 } else if (streq(opt, "show-column")) {
1083 show_column = truth_value;
1084 } else if (streq(opt, "signed-char")) {
1085 char_is_signed = truth_value;
1086 } else if (streq(opt, "strength-reduce")) {
1087 /* does nothing, for gcc compatibility (even gcc does
1088 * nothing for this switch anymore) */
1089 } else if (streq(opt, "syntax-only")) {
1090 mode = truth_value ? ParseOnly : CompileAssembleLink;
1091 } else if (streq(opt, "unsigned-char")) {
1092 char_is_signed = !truth_value;
1093 } else if (streq(opt, "freestanding")) {
1094 freestanding = truth_value;
1095 } else if (streq(opt, "hosted")) {
1096 freestanding = !truth_value;
1097 } else if (streq(opt, "profile-generate")) {
1098 profile_generate = truth_value;
1099 } else if (streq(opt, "profile-use")) {
1100 profile_use = truth_value;
1101 } else if (!truth_value &&
1102 streq(opt, "asynchronous-unwind-tables")) {
1103 /* nothing todo, a gcc feature which we do not support
1104 * anyway was deactivated */
1105 } else if (streq(opt, "verbose-asm")) {
1106 /* ignore: we always print verbose assembler */
1107 } else if (streq(opt, "jump-tables") ||
1108 streq(opt, "expensive-optimizations") ||
1109 streq(opt, "common") ||
1110 streq(opt, "optimize-sibling-calls") ||
1111 streq(opt, "align-loops") ||
1112 streq(opt, "align-jumps") ||
1113 streq(opt, "align-functions") ||
1114 streq(opt, "PIC")) {
1115 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1117 int res = firm_option(orig_opt);
1119 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1121 argument_errors = true;
1126 } else if (option[0] == 'b') {
1128 GET_ARG_AFTER(opt, "-b");
1130 if (streq(opt, "help")) {
1131 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1134 int res = be_parse_arg(opt);
1136 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1138 argument_errors = true;
1139 } else if (strstart(opt, "isa=")) {
1140 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1143 } else if (option[0] == 'W') {
1144 if (strstart(option + 1, "p,")) {
1145 // pass options directly to the preprocessor
1147 GET_ARG_AFTER(opt, "-Wp,");
1148 add_flag(&cppflags_obst, "-Wp,%s", opt);
1149 } else if (strstart(option + 1, "l,")) {
1150 // pass options directly to the linker
1152 GET_ARG_AFTER(opt, "-Wl,");
1153 add_flag(&ldflags_obst, "-Wl,%s", opt);
1154 } else if (streq(option + 1, "no-trigraphs")
1155 || streq(option + 1, "undef")) {
1156 add_flag(&cppflags_obst, "%s", arg);
1157 } else if (streq(option+1, "init-self")) {
1158 /* ignored (gcc compatibility) */
1160 set_warning_opt(&option[1]);
1162 } else if (option[0] == 'm') {
1167 GET_ARG_AFTER(opt, "-m");
1168 if (strstart(opt, "target=")) {
1169 GET_ARG_AFTER(opt, "-mtarget=");
1170 if (!parse_target_triple(opt)) {
1171 argument_errors = true;
1173 setup_target_machine();
1174 target_triple = opt;
1176 } else if (strstart(opt, "triple=")) {
1177 GET_ARG_AFTER(opt, "-mtriple=");
1178 if (!parse_target_triple(opt)) {
1179 argument_errors = true;
1181 setup_target_machine();
1182 target_triple = opt;
1184 } else if (strstart(opt, "arch=")) {
1185 GET_ARG_AFTER(opt, "-march=");
1186 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1187 int res = be_parse_arg(arch_opt);
1188 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1189 res &= be_parse_arg(arch_opt);
1192 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1193 argument_errors = true;
1195 } else if (strstart(opt, "tune=")) {
1196 GET_ARG_AFTER(opt, "-mtune=");
1197 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1198 int res = be_parse_arg(arch_opt);
1200 argument_errors = true;
1201 } else if (strstart(opt, "cpu=")) {
1202 GET_ARG_AFTER(opt, "-mcpu=");
1203 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1204 int res = be_parse_arg(arch_opt);
1206 argument_errors = true;
1207 } else if (strstart(opt, "fpmath=")) {
1208 GET_ARG_AFTER(opt, "-mfpmath=");
1209 if (streq(opt, "387"))
1211 else if (streq(opt, "sse"))
1214 fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
1215 argument_errors = true;
1217 if (!argument_errors) {
1218 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1219 int res = be_parse_arg(arch_opt);
1221 argument_errors = true;
1223 } else if (strstart(opt, "preferred-stack-boundary=")) {
1224 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1225 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1226 int res = be_parse_arg(arch_opt);
1228 argument_errors = true;
1229 } else if (streq(opt, "rtd")) {
1230 default_calling_convention = CC_STDCALL;
1231 } else if (strstart(opt, "regparm=")) {
1232 fprintf(stderr, "error: regparm convention not supported yet\n");
1233 argument_errors = true;
1234 } else if (streq(opt, "soft-float")) {
1235 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1236 argument_errors = true;
1238 long int value = strtol(opt, NULL, 10);
1240 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1241 argument_errors = true;
1242 } else if (value != 16 && value != 32 && value != 64) {
1243 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1244 argument_errors = true;
1246 add_flag(&cppflags_obst, "-m%u", machine_size);
1247 add_flag(&asflags_obst, "-m%u", machine_size);
1248 add_flag(&ldflags_obst, "-m%u", machine_size);
1249 /* TODO: choose/change backend based on this */
1252 } else if (streq(option, "pg")) {
1253 set_be_option("gprof");
1254 add_flag(&ldflags_obst, "-pg");
1255 } else if (streq(option, "pedantic") ||
1256 streq(option, "ansi")) {
1257 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1258 } else if (strstart(option, "std=")) {
1259 const char *const o = &option[4];
1261 streq(o, "c++") ? STANDARD_CXX98 :
1262 streq(o, "c++98") ? STANDARD_CXX98 :
1263 streq(o, "c89") ? STANDARD_C89 :
1264 streq(o, "c99") ? STANDARD_C99 :
1265 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1266 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1267 streq(o, "gnu89") ? STANDARD_GNU89 :
1268 streq(o, "gnu99") ? STANDARD_GNU99 :
1269 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1270 streq(o, "iso9899:1990") ? STANDARD_C89 :
1271 streq(o, "iso9899:199409") ? STANDARD_C90 :
1272 streq(o, "iso9899:1999") ? STANDARD_C99 :
1273 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1274 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1275 } else if (streq(option, "version")) {
1276 print_cparser_version();
1277 } else if (strstart(option, "print-file-name=")) {
1278 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1279 } else if (option[0] == '-') {
1280 /* double dash option */
1282 if (streq(option, "gcc")) {
1283 features_on |= _GNUC;
1284 features_off &= ~_GNUC;
1285 } else if (streq(option, "no-gcc")) {
1286 features_on &= ~_GNUC;
1287 features_off |= _GNUC;
1288 } else if (streq(option, "ms")) {
1290 features_off &= ~_MS;
1291 } else if (streq(option, "no-ms")) {
1292 features_on &= ~_MS;
1293 features_off |= _MS;
1294 } else if (streq(option, "strict")) {
1296 } else if (streq(option, "lextest")) {
1298 } else if (streq(option, "benchmark")) {
1299 mode = BenchmarkParser;
1300 } else if (streq(option, "print-ast")) {
1302 } else if (streq(option, "print-implicit-cast")) {
1303 print_implicit_casts = true;
1304 } else if (streq(option, "print-parenthesis")) {
1305 print_parenthesis = true;
1306 } else if (streq(option, "print-fluffy")) {
1308 } else if (streq(option, "print-jna")) {
1310 } else if (streq(option, "jna-limit")) {
1313 fprintf(stderr, "error: "
1314 "expected argument after '--jna-limit'\n");
1315 argument_errors = true;
1318 jna_limit_output(argv[i]);
1319 } else if (streq(option, "jna-libname")) {
1322 fprintf(stderr, "error: "
1323 "expected argument after '--jna-libname'\n");
1324 argument_errors = true;
1327 jna_set_libname(argv[i]);
1328 } else if (streq(option, "time")) {
1330 } else if (streq(option, "version")) {
1331 print_cparser_version();
1332 return EXIT_SUCCESS;
1333 } else if (streq(option, "help")) {
1335 } else if (streq(option, "help-parser")) {
1336 help |= HELP_PARSER;
1337 } else if (streq(option, "help-warnings")) {
1338 help |= HELP_WARNINGS;
1339 } else if (streq(option, "help-codegen")) {
1340 help |= HELP_CODEGEN;
1341 } else if (streq(option, "help-linker")) {
1342 help |= HELP_LINKER;
1343 } else if (streq(option, "help-optimization")) {
1344 help |= HELP_OPTIMIZATION;
1345 } else if (streq(option, "help-language-tools")) {
1346 help |= HELP_LANGUAGETOOLS;
1347 } else if (streq(option, "help-debug")) {
1349 } else if (streq(option, "help-firm")) {
1351 } else if (streq(option, "help-all")) {
1353 } else if (streq(option, "dump-function")) {
1356 fprintf(stderr, "error: "
1357 "expected argument after '--dump-function'\n");
1358 argument_errors = true;
1361 dumpfunction = argv[i];
1363 } else if (streq(option, "export-ir")) {
1364 mode = CompileExportIR;
1366 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1367 argument_errors = true;
1370 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1371 argument_errors = true;
1374 filetype_t type = forced_filetype;
1375 if (type == FILETYPE_AUTODETECT) {
1376 if (streq(arg, "-")) {
1377 /* - implicitly means C source file */
1380 const char *suffix = strrchr(arg, '.');
1381 /* Ensure there is at least one char before the suffix */
1382 if (suffix != NULL && suffix != arg) {
1385 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1386 streq(suffix, "a") ? FILETYPE_OBJECT :
1387 streq(suffix, "c") ? FILETYPE_C :
1388 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1389 streq(suffix, "C") ? FILETYPE_CXX :
1390 streq(suffix, "cc") ? FILETYPE_CXX :
1391 streq(suffix, "cp") ? FILETYPE_CXX :
1392 streq(suffix, "cpp") ? FILETYPE_CXX :
1393 streq(suffix, "CPP") ? FILETYPE_CXX :
1394 streq(suffix, "cxx") ? FILETYPE_CXX :
1395 streq(suffix, "c++") ? FILETYPE_CXX :
1396 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1397 streq(suffix, "h") ? FILETYPE_C :
1398 streq(suffix, "ir") ? FILETYPE_IR :
1399 streq(suffix, "o") ? FILETYPE_OBJECT :
1400 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1401 streq(suffix, "so") ? FILETYPE_OBJECT :
1402 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1407 file_list_entry_t *entry
1408 = obstack_alloc(&file_obst, sizeof(entry[0]));
1409 memset(entry, 0, sizeof(entry[0]));
1413 if (last_file != NULL) {
1414 last_file->next = entry;
1422 if (help != HELP_NONE) {
1423 print_help(argv[0], help);
1424 return !argument_errors;
1427 if (print_file_name_file != NULL) {
1428 print_file_name(print_file_name_file);
1429 return EXIT_SUCCESS;
1431 if (files == NULL) {
1432 fprintf(stderr, "error: no input files specified\n");
1433 argument_errors = true;
1436 if (argument_errors) {
1438 return EXIT_FAILURE;
1441 /* apply some effects from switches */
1442 c_mode |= features_on;
1443 c_mode &= ~features_off;
1444 if (profile_generate) {
1445 add_flag(&ldflags_obst, "-lfirmprof");
1446 set_be_option("profilegenerate");
1449 set_be_option("profileuse");
1453 init_symbol_table();
1466 if (construct_dep_target) {
1467 if (outname != 0 && strlen(outname) >= 2) {
1468 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1470 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1473 dep_target[0] = '\0';
1476 char outnamebuf[4096];
1477 if (outname == NULL) {
1478 const char *filename = files->name;
1481 case BenchmarkParser:
1486 case PreprocessOnly:
1491 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1492 outname = outnamebuf;
1494 case CompileAssemble:
1495 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1496 outname = outnamebuf;
1499 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1501 outname = outnamebuf;
1503 case CompileExportIR:
1504 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1505 outname = outnamebuf;
1507 case CompileAssembleLink:
1517 assert(outname != NULL);
1520 if (streq(outname, "-")) {
1523 out = fopen(outname, "w");
1525 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
1527 return EXIT_FAILURE;
1531 file_list_entry_t *file;
1532 bool already_constructed_firm = false;
1533 for (file = files; file != NULL; file = file->next) {
1534 char asm_tempfile[1024];
1535 const char *filename = file->name;
1536 filetype_t filetype = file->type;
1538 if (filetype == FILETYPE_OBJECT)
1542 if (mode == LexTest) {
1544 in = open_file(filename);
1545 lextest(in, filename);
1547 return EXIT_SUCCESS;
1550 FILE *preprocessed_in = NULL;
1551 filetype_t next_filetype = filetype;
1554 next_filetype = FILETYPE_PREPROCESSED_C;
1557 next_filetype = FILETYPE_PREPROCESSED_CXX;
1559 case FILETYPE_ASSEMBLER:
1560 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1563 /* no support for input on FILE* yet */
1565 panic("internal compiler error: in for preprocessor != NULL");
1567 preprocessed_in = preprocess(filename, filetype);
1568 if (mode == PreprocessOnly) {
1569 copy_file(out, preprocessed_in);
1570 int pp_result = pclose(preprocessed_in);
1572 /* remove output file in case of error */
1573 if (out != stdout && pp_result != EXIT_SUCCESS) {
1579 in = preprocessed_in;
1580 filetype = next_filetype;
1588 if (mode == Compile) {
1591 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1595 in = open_file(filename);
1597 /* preprocess and compile */
1598 if (filetype == FILETYPE_PREPROCESSED_C) {
1599 char const* invalid_mode;
1602 case STANDARD_C89: c_mode = _C89; break;
1603 /* TODO determine difference between these two */
1604 case STANDARD_C90: c_mode = _C89; break;
1605 case STANDARD_C99: c_mode = _C89 | _C99; break;
1606 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1610 "warning: command line option \"-std=%s\" is not valid for C\n",
1613 case STANDARD_DEFAULT:
1614 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1616 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1617 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1620 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1621 char const* invalid_mode;
1623 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1624 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1625 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1626 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1627 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1630 case STANDARD_CXX98: c_mode = _CXX; break;
1634 "warning: command line option \"-std=%s\" is not valid for C++\n",
1636 case STANDARD_DEFAULT:
1637 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1641 c_mode |= features_on;
1642 c_mode &= ~features_off;
1644 /* do the actual parsing */
1645 ir_timer_t *t_parsing = ir_timer_new();
1646 timer_register(t_parsing, "Frontend: Parsing");
1647 timer_push(t_parsing);
1649 translation_unit_t *const unit = do_parsing(in, filename);
1650 timer_pop(t_parsing);
1652 /* prints the AST even if errors occurred */
1653 if (mode == PrintAst) {
1658 if (error_count > 0) {
1659 /* parsing failed because of errors */
1660 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1662 result = EXIT_FAILURE;
1664 } else if (warning_count > 0) {
1665 fprintf(stderr, "%u warning(s)\n", warning_count);
1668 if (in == preprocessed_in) {
1669 int pp_result = pclose(preprocessed_in);
1670 if (pp_result != EXIT_SUCCESS) {
1671 /* remove output file */
1674 return EXIT_FAILURE;
1678 if (mode == BenchmarkParser) {
1680 } else if (mode == PrintFluffy) {
1681 write_fluffy_decls(out, unit);
1683 } else if (mode == PrintJna) {
1684 write_jna_decls(out, unit);
1688 /* build the firm graph */
1689 ir_timer_t *t_construct = ir_timer_new();
1690 timer_register(t_construct, "Frontend: Graph construction");
1691 timer_push(t_construct);
1692 if (already_constructed_firm) {
1693 panic("compiling multiple files/translation units not possible");
1695 translation_unit_to_firm(unit);
1696 already_constructed_firm = true;
1697 timer_pop(t_construct);
1700 if (mode == ParseOnly) {
1704 if (mode == CompileDump) {
1706 ident *id = new_id_from_str(dumpfunction);
1707 ir_graph *irg = NULL;
1708 int n_irgs = get_irp_n_irgs();
1709 for (int i = 0; i < n_irgs; ++i) {
1710 ir_graph *tirg = get_irp_irg(i);
1711 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1719 fprintf(stderr, "No graph for function '%s' found\n",
1721 return EXIT_FAILURE;
1724 dump_ir_graph_file(out, irg);
1726 return EXIT_SUCCESS;
1729 if (mode == CompileExportIR) {
1732 return EXIT_SUCCESS;
1735 gen_firm_finish(asm_out, filename);
1736 if (asm_out != out) {
1739 } else if (filetype == FILETYPE_IR) {
1741 ir_import(filename);
1743 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1744 copy_file(asm_out, in);
1745 if (in == preprocessed_in) {
1746 int pp_result = pclose(preprocessed_in);
1747 if (pp_result != EXIT_SUCCESS) {
1748 /* remove output in error case */
1754 if (asm_out != out) {
1759 if (mode == Compile)
1762 /* if we're here then we have preprocessed assembly */
1763 filename = asm_tempfile;
1764 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1767 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1769 const char *filename_o;
1770 if (mode == CompileAssemble) {
1772 filename_o = outname;
1774 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1779 assemble(filename_o, filename);
1781 size_t len = strlen(filename_o) + 1;
1782 filename = obstack_copy(&file_obst, filename_o, len);
1783 filetype = FILETYPE_OBJECT;
1786 /* ok we're done here, process next file */
1787 file->name = filename;
1788 file->type = filetype;
1791 if (result != EXIT_SUCCESS) {
1797 /* link program file */
1798 if (mode == CompileAssembleLink) {
1799 obstack_1grow(&ldflags_obst, '\0');
1800 const char *flags = obstack_finish(&ldflags_obst);
1802 /* construct commandline */
1803 const char *linker = getenv("CPARSER_LINK");
1804 if (linker != NULL) {
1805 obstack_printf(&file_obst, "%s ", linker);
1807 if (target_triple != NULL)
1808 obstack_printf(&file_obst, "%s-", target_triple);
1809 obstack_printf(&file_obst, "%s ", LINKER);
1812 for (file_list_entry_t *entry = files; entry != NULL;
1813 entry = entry->next) {
1814 if (entry->type != FILETYPE_OBJECT)
1817 add_flag(&file_obst, "%s", entry->name);
1820 add_flag(&file_obst, "-o");
1821 add_flag(&file_obst, outname);
1822 obstack_printf(&file_obst, "%s", flags);
1823 obstack_1grow(&file_obst, '\0');
1825 char *commandline = obstack_finish(&file_obst);
1830 int err = system(commandline);
1831 if (err != EXIT_SUCCESS) {
1832 fprintf(stderr, "linker reported an error\n");
1833 return EXIT_FAILURE;
1840 obstack_free(&cppflags_obst, NULL);
1841 obstack_free(&ldflags_obst, NULL);
1842 obstack_free(&asflags_obst, NULL);
1843 obstack_free(&file_obst, NULL);
1853 exit_symbol_table();
1854 return EXIT_SUCCESS;