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 bool byte_order_big_endian = false;
103 bool strict_mode = false;
104 bool enable_main_collect2_hack = false;
105 bool freestanding = false;
106 unsigned architecture_modulo_shift = 0;
108 static bool char_is_signed = true;
109 static atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
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;
120 static const char *input_encoding;
122 typedef enum lang_standard_t {
123 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
124 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
125 STANDARD_C89, /* ISO C90 (sic) */
126 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
127 STANDARD_C99, /* ISO C99 */
128 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
129 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
130 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
131 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
134 static lang_standard_t standard;
136 typedef struct file_list_entry_t file_list_entry_t;
138 typedef enum filetype_t {
141 FILETYPE_PREPROCESSED_C,
143 FILETYPE_PREPROCESSED_CXX,
145 FILETYPE_PREPROCESSED_ASSEMBLER,
151 struct file_list_entry_t {
152 const char *name; /**< filename or NULL for stdin */
154 file_list_entry_t *next;
157 static file_list_entry_t *temp_files;
159 static void get_output_name(char *buf, size_t buflen, const char *inputname,
162 if (inputname == NULL)
165 char const *const last_slash = strrchr(inputname, '/');
166 char const *const filename =
167 last_slash != NULL ? last_slash + 1 : inputname;
168 char const *const last_dot = strrchr(filename, '.');
169 char const *const name_end =
170 last_dot != NULL ? last_dot : strchr(filename, '\0');
172 int const len = snprintf(buf, buflen, "%.*s%s",
173 (int)(name_end - filename), filename, newext);
175 if (len < 0 || buflen <= (size_t)len)
177 if (buflen <= (size_t)len)
179 panic("filename too long");
182 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
186 input_t *input = input_from_stream(in, input_encoding);
187 lexer_switch_input(input, input_name);
189 translation_unit_t *unit = finish_parsing();
195 static void lextest(FILE *in, const char *fname)
197 input_t *input = input_from_stream(in, input_encoding);
198 lexer_switch_input(input, fname);
201 lexer_next_preprocessing_token();
202 print_token(stdout, &lexer_token);
204 } while (lexer_token.kind != T_EOF);
208 static void add_flag(struct obstack *obst, const char *format, ...)
213 va_start(ap, format);
217 vsnprintf(buf, sizeof(buf), format, ap);
220 obstack_1grow(obst, ' ');
222 obstack_1grow(obst, '"');
223 obstack_grow(obst, buf, len);
224 obstack_1grow(obst, '"');
226 /* escape stuff... */
227 for (char *c = buf; *c != '\0'; ++c) {
245 obstack_1grow(obst, '\\');
248 obstack_1grow(obst, *c);
255 static const char *type_to_string(type_t *type)
257 assert(type->kind == TYPE_ATOMIC);
258 return get_atomic_kind_name(type->atomic.akind);
261 static FILE *preprocess(const char *fname, filetype_t filetype)
263 static const char *common_flags = NULL;
265 if (common_flags == NULL) {
266 obstack_1grow(&cppflags_obst, '\0');
267 const char *flags = obstack_finish(&cppflags_obst);
269 /* setup default defines */
270 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
271 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
272 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
273 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
275 add_flag(&cppflags_obst, "-U__VERSION__");
276 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
278 if (define_intmax_types) {
279 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
280 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
281 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
282 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
285 if (flags[0] != '\0') {
286 size_t len = strlen(flags);
287 obstack_1grow(&cppflags_obst, ' ');
288 obstack_grow(&cppflags_obst, flags, len);
290 obstack_1grow(&cppflags_obst, '\0');
291 common_flags = obstack_finish(&cppflags_obst);
294 assert(obstack_object_size(&cppflags_obst) == 0);
296 const char *preprocessor = getenv("CPARSER_PP");
297 if (preprocessor != NULL) {
298 obstack_printf(&cppflags_obst, "%s ", preprocessor);
300 if (target_triple != NULL)
301 obstack_printf(&cppflags_obst, "%s-", target_triple);
302 obstack_printf(&cppflags_obst, PREPROCESSOR);
307 add_flag(&cppflags_obst, "-std=c99");
310 add_flag(&cppflags_obst, "-std=c++98");
312 case FILETYPE_ASSEMBLER:
313 add_flag(&cppflags_obst, "-x");
314 add_flag(&cppflags_obst, "assembler-with-cpp");
319 obstack_printf(&cppflags_obst, "%s", common_flags);
321 /* handle dependency generation */
322 if (dep_target[0] != '\0') {
323 add_flag(&cppflags_obst, "-MF");
324 add_flag(&cppflags_obst, dep_target);
325 if (outname != NULL) {
326 add_flag(&cppflags_obst, "-MQ");
327 add_flag(&cppflags_obst, outname);
330 add_flag(&cppflags_obst, fname);
331 obstack_1grow(&cppflags_obst, '\0');
333 char *commandline = obstack_finish(&cppflags_obst);
337 FILE *f = popen(commandline, "r");
339 fprintf(stderr, "invoking preprocessor failed\n");
342 /* we do not really need that anymore */
343 obstack_free(&cppflags_obst, commandline);
348 static void assemble(const char *out, const char *in)
350 obstack_1grow(&asflags_obst, '\0');
351 const char *flags = obstack_finish(&asflags_obst);
353 const char *assembler = getenv("CPARSER_AS");
354 if (assembler != NULL) {
355 obstack_printf(&asflags_obst, "%s", assembler);
357 if (target_triple != NULL)
358 obstack_printf(&asflags_obst, "%s-", target_triple);
359 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
361 if (flags[0] != '\0')
362 obstack_printf(&asflags_obst, " %s", flags);
364 obstack_printf(&asflags_obst, " %s -o %s", in, out);
365 obstack_1grow(&asflags_obst, '\0');
367 char *commandline = obstack_finish(&asflags_obst);
371 int err = system(commandline);
372 if (err != EXIT_SUCCESS) {
373 fprintf(stderr, "assembler reported an error\n");
376 obstack_free(&asflags_obst, commandline);
379 static void print_file_name(const char *file)
381 add_flag(&ldflags_obst, "-print-file-name=%s", file);
383 obstack_1grow(&ldflags_obst, '\0');
384 const char *flags = obstack_finish(&ldflags_obst);
386 /* construct commandline */
387 const char *linker = getenv("CPARSER_LINK");
388 if (linker != NULL) {
389 obstack_printf(&ldflags_obst, "%s ", linker);
391 if (target_triple != NULL)
392 obstack_printf(&ldflags_obst, "%s-", target_triple);
393 obstack_printf(&ldflags_obst, "%s ", LINKER);
395 obstack_printf(&ldflags_obst, "%s ", linker);
396 obstack_printf(&ldflags_obst, "%s", flags);
397 obstack_1grow(&ldflags_obst, '\0');
399 char *commandline = obstack_finish(&ldflags_obst);
403 int err = system(commandline);
404 if (err != EXIT_SUCCESS) {
405 fprintf(stderr, "linker reported an error\n");
408 obstack_free(&ldflags_obst, commandline);
411 static const char *try_dir(const char *dir)
415 if (access(dir, R_OK | W_OK | X_OK) == 0)
420 static const char *get_tempdir(void)
422 static const char *tmpdir = NULL;
428 tmpdir = try_dir(getenv("TMPDIR"));
430 tmpdir = try_dir(getenv("TMP"));
432 tmpdir = try_dir(getenv("TEMP"));
436 tmpdir = try_dir(P_tmpdir);
440 tmpdir = try_dir("/var/tmp");
442 tmpdir = try_dir("/usr/tmp");
444 tmpdir = try_dir("/tmp");
453 /* cheap and nasty mkstemp replacement */
454 static int mkstemp(char *templ)
457 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
462 * an own version of tmpnam, which: writes in a buffer, emits no warnings
463 * during linking (like glibc/gnu ld do for tmpnam)...
465 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
467 const char *tempdir = get_tempdir();
469 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
471 int fd = mkstemp(buffer);
473 fprintf(stderr, "could not create temporary file: %s\n",
477 FILE *out = fdopen(fd, "w");
479 fprintf(stderr, "could not create temporary file FILE*\n");
483 file_list_entry_t *entry = xmalloc(sizeof(*entry));
484 memset(entry, 0, sizeof(*entry));
486 size_t name_len = strlen(buffer) + 1;
487 char *name = malloc(name_len);
488 memcpy(name, buffer, name_len);
491 entry->next = temp_files;
497 static void free_temp_files(void)
499 file_list_entry_t *entry = temp_files;
500 file_list_entry_t *next;
501 for ( ; entry != NULL; entry = next) {
505 free((char*) entry->name);
511 typedef enum compile_mode_t {
526 static void usage(const char *argv0)
528 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
531 static void print_cparser_version(void)
533 printf("cparser (%s) using libFirm (%u.%u",
534 cparser_REVISION, ir_get_version_major(),
535 ir_get_version_minor());
537 const char *revision = ir_get_version_revision();
538 if (revision[0] != 0) {
540 fputs(revision, stdout);
543 const char *build = ir_get_version_build();
546 fputs(build, stdout);
549 puts("This is free software; see the source for copying conditions. There is NO\n"
550 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
553 static void print_cparser_version_short(void)
555 puts(cparser_REVISION);
558 static void print_help_basic(const char *argv0)
562 put_help("--help", "Display this information");
563 put_help("--version", "Display compiler version");
564 put_help("--help-parser", "Display information about parser options");
565 put_help("--help-warnings", "Display information about warning options");
566 put_help("--help-codegen", "Display information about code-generation options");
567 put_help("--help-optimization", "Display information about optimization options");
568 put_help("--help-linker", "Display information about linker options");
569 put_help("--help-language-tools", "Display information about language tools options");
570 put_help("--help-debug", "Display information about compiler debugging options");
571 put_help("--help-firm", "Display information about direct firm options");
572 put_help("--help-all", "Display information about all options");
573 put_help("-c", "Compile and assemble but do not link");
574 put_help("-E", "Preprocess only");
575 put_help("-S", "Compile but do not assembler or link");
576 put_help("-o", "Specify output file");
577 put_help("-v", "Verbose output (show invocation of sub-processes)");
578 put_help("-x", "Force input language:");
579 put_choice("c", "C");
580 put_choice("c++", "C++");
581 put_choice("assembler", "Assembler (no preprocessing)");
582 put_choice("assembler-with-cpp", "Assembler with preprocessing");
583 put_choice("none", "Autodetection");
584 put_help("-pipe", "Ignored (gcc compatibility)");
587 static void print_help_preprocessor(void)
589 put_help("-nostdinc", "Do not search standard system include directories");
590 put_help("-trigraphs", "Support ISO C trigraphs");
591 put_help("-isystem", "");
592 put_help("-include", "");
593 put_help("-I PATH", "");
594 put_help("-D SYMBOL[=value]", "");
595 put_help("-U SYMBOL", "");
596 put_help("-Wp,OPTION", "Pass option directly to preprocessor");
599 put_help("-MMD", "");
607 static void print_help_parser(void)
609 put_help("-finput-charset=CHARSET", "Select encoding of input files");
610 put_help("-fmessage-length=LEN", "Ignored (gcc compatibility)");
611 put_help("-fshort-wchar", "Type \"wchar_t\" is unsigned short instead of int");
612 put_help("-fshow-column", "Show the column number in diagnostic messages");
613 put_help("-fsigned-char", "Type \"char\" is a signed type");
614 put_help("-funsigned-char", "Type \"char\" is an unsigned type");
615 put_help("--ms", "Enable msvc extensions");
616 put_help("--no-ms", "Disable msvc extensions");
617 put_help("--gcc", "Enable gcc extensions");
618 put_help("--no-gcc", "Disable gcc extensions");
619 put_help("-std=STANDARD", "Specify language standard:");
620 put_choice("c99", "ISO C99 standard");
621 put_choice("c89", "ISO C89 standard");
622 put_choice("c9x", "Deprecated");
623 put_choice("c++", "ISO C++ 98");
624 put_choice("c++98", "ISO C++ 98");
625 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
626 put_choice("gnu89", "ISO C89 + GNU extensions");
627 put_choice("gnu9x", "Deprecated");
628 put_choice("iso9899:1990", "ISO C89");
629 put_choice("iso9899:199409", "ISO C90");
630 put_choice("iso9899:1999", "ISO C99");
631 put_choice("iso9899:199x", "Deprecated");
632 put_help("-pedantic", "Ignored (gcc compatibility)");
633 put_help("-ansi", "Ignored (gcc compatibility)");
634 put_help("--strict", "Enable strict conformance checking");
637 static void print_help_warnings(void)
639 put_help("-f[no-]diagnostics-show-option", "Show the switch, which controls a warning, after each warning");
640 put_help("-w", "Disable all warnings");
641 put_help("-Wno-trigraphs", "Warn if input contains trigraphs");
642 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
643 put_help("-Wmissing-include-dirs", "Warn about missing user-specified include directories");
644 put_help("-Wendif-labels", "Warn about stray text after #elif and #endif");
645 put_help("-Winit-self", "Ignored (gcc compatibility)");
646 put_help("-Wformat-y2k", "Ignored (gcc compatibility)");
647 put_help("-Wformat-security", "Ignored (gcc compatibility)");
648 put_help("-Wold-style-declaration", "Ignored (gcc compatibility)");
649 put_help("-Wtype-limits", "Ignored (gcc compatibility)");
650 print_warning_opt_help();
653 static void print_help_optimization(void)
655 put_help("-O LEVEL", "Select optimization level (0-4)");
656 firm_option_help(put_help);
657 put_help("-fexpensive-optimizations","Ignored (gcc compatibility)");
660 static void print_help_codegeneration(void)
662 put_help("-g", "Generate debug information");
663 put_help("-pg", "Instrument code for gnu gprof");
664 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
665 put_help("-ffreestanding", "Compile in freestanding mode (see ISO C standard)");
666 put_help("-fhosted", "Compile in hosted (not freestanding) mode");
667 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
668 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
669 put_help("-ffp-precise", "Precise floating point model");
670 put_help("-ffp-fast", "Imprecise floating point model");
671 put_help("-ffp-strict", "Strict floating point model");
672 put_help("-pthread", "Use pthread threading library");
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");
685 put_help("-fverbose-asm", "Ignored (gcc compatibility)");
686 put_help("-fjump-tables", "Ignored (gcc compatibility)");
687 put_help("-fcommon", "Ignored (gcc compatibility)");
688 put_help("-foptimize-sibling-calls", "Ignored (gcc compatibility)");
689 put_help("-falign-loops", "Ignored (gcc compatibility)");
690 put_help("-falign-jumps", "Ignored (gcc compatibility)");
691 put_help("-falign-functions", "Ignored (gcc compatibility)");
692 put_help("-fPIC", "Ignored (gcc compatibility)");
693 put_help("-ffast-math", "Same as -ffp-fast (gcc compatibility)");
695 puts("\tMost of these options can be used with a no- prefix to disable them");
696 puts("\te.g. -fno-omit-frame-pointer");
699 static void print_help_linker(void)
701 put_help("-l LIBRARY", "");
702 put_help("-L PATH", "");
703 put_help("-s", "Do not produce symbol table and relocation information");
704 put_help("-shared", "Produce a shared library");
705 put_help("-static", "Produce statically linked binary");
706 put_help("-Wl,OPTION", "Pass option directly to linker");
709 static void print_help_debug(void)
711 put_help("--lextest", "Preprocess and tokenize only");
712 put_help("--print-ast", "Preprocess, parse and print AST");
713 put_help("--print-implicit-cast", "");
714 put_help("--print-parenthesis", "");
715 put_help("--benchmark", "Preprocess and parse, produces no output");
716 put_help("--time", "Measure time of compiler passes");
717 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
718 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
721 static void print_help_language_tools(void)
723 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
724 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
725 put_help("--jna-limit filename", "");
726 put_help("--jna-libname name", "");
729 static void print_help_firm(void)
731 put_help("-bOPTION", "Directly pass option to libFirm backend");
732 int res = be_parse_arg("help");
740 HELP_PREPROCESSOR = 1 << 1,
741 HELP_PARSER = 1 << 2,
742 HELP_WARNINGS = 1 << 3,
743 HELP_OPTIMIZATION = 1 << 4,
744 HELP_CODEGEN = 1 << 5,
745 HELP_LINKER = 1 << 6,
746 HELP_LANGUAGETOOLS = 1 << 7,
753 static void print_help(const char *argv0, help_sections_t sections)
755 if (sections & HELP_BASIC) print_help_basic(argv0);
756 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
757 if (sections & HELP_PARSER) print_help_parser();
758 if (sections & HELP_WARNINGS) print_help_warnings();
759 if (sections & HELP_OPTIMIZATION) print_help_optimization();
760 if (sections & HELP_CODEGEN) print_help_codegeneration();
761 if (sections & HELP_LINKER) print_help_linker();
762 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
763 if (sections & HELP_DEBUG) print_help_debug();
764 if (sections & HELP_FIRM) print_help_firm();
767 static void set_be_option(const char *arg)
769 int res = be_parse_arg(arg);
774 static void copy_file(FILE *dest, FILE *input)
778 while (!feof(input) && !ferror(dest)) {
779 size_t read = fread(buf, 1, sizeof(buf), input);
780 if (fwrite(buf, 1, read, dest) != read) {
781 perror("could not write output");
786 static FILE *open_file(const char *filename)
788 if (streq(filename, "-")) {
792 FILE *in = fopen(filename, "r");
794 fprintf(stderr, "Could not open '%s': %s\n", filename,
802 static filetype_t get_filetype_from_string(const char *string)
804 if (streq(string, "c") || streq(string, "c-header"))
806 if (streq(string, "c++") || streq(string, "c++-header"))
808 if (streq(string, "assembler"))
809 return FILETYPE_PREPROCESSED_ASSEMBLER;
810 if (streq(string, "assembler-with-cpp"))
811 return FILETYPE_ASSEMBLER;
812 if (streq(string, "none"))
813 return FILETYPE_AUTODETECT;
815 return FILETYPE_UNKNOWN;
818 static bool is_windows_os(const char *os)
820 return strstr(os, "mingw") != NULL || streq(os, "win32");
823 static bool is_unixish_os(const char *os)
825 return strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
826 || streq(os, "solaris");
829 static bool is_darwin_os(const char *os)
831 return streq(os, "darwin");
834 static bool init_os_support(void)
836 const char *os = target_machine->operating_system;
837 wchar_atomic_kind = ATOMIC_TYPE_INT;
838 enable_main_collect2_hack = false;
839 define_intmax_types = false;
841 if (is_unixish_os(os)) {
842 set_create_ld_ident(create_name_linux_elf);
843 } else if (is_darwin_os(os)) {
844 set_create_ld_ident(create_name_macho);
845 define_intmax_types = true;
846 } else if (is_windows_os(os)) {
847 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
848 enable_main_collect2_hack = true;
849 set_create_ld_ident(create_name_win32);
857 static bool parse_target_triple(const char *arg)
859 machine_triple_t *triple = firm_parse_machine_triple(arg);
860 if (triple == NULL) {
861 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
864 target_machine = triple;
868 static unsigned decide_modulo_shift(unsigned type_size)
870 if (architecture_modulo_shift == 0)
872 if (type_size < architecture_modulo_shift)
873 return architecture_modulo_shift;
877 static bool is_ia32_cpu(const char *architecture)
879 return streq(architecture, "i386")
880 || streq(architecture, "i486")
881 || streq(architecture, "i586")
882 || streq(architecture, "i686")
883 || streq(architecture, "i786");
886 static const char *setup_isa_from_tripel(const machine_triple_t *machine)
888 const char *cpu = machine->cpu_type;
890 if (is_ia32_cpu(cpu)) {
892 } else if (streq(cpu, "x86_64")) {
894 } else if (streq(cpu, "sparc")) {
896 } else if (streq(cpu, "arm")) {
899 fprintf(stderr, "Unknown cpu '%s' in target-triple\n", cpu);
904 static const char *setup_target_machine(void)
906 if (!setup_firm_for_machine(target_machine))
909 const char *isa = setup_isa_from_tripel(target_machine);
920 * initialize cparser type properties based on a firm type
922 static void set_typeprops_type(atomic_type_properties_t* props, ir_type *type)
924 props->size = get_type_size_bytes(type);
925 props->alignment = get_type_alignment_bytes(type);
926 props->struct_alignment = props->alignment;
930 * Copy atomic type properties except the integer conversion rank
932 static void copy_typeprops(atomic_type_properties_t *dest,
933 const atomic_type_properties_t *src)
935 dest->size = src->size;
936 dest->alignment = src->alignment;
937 dest->struct_alignment = src->struct_alignment;
938 dest->flags = src->flags;
941 static void init_types_and_adjust(void)
943 const backend_params *be_params = be_get_backend_param();
944 unsigned machine_size = be_params->machine_size;
945 init_types(machine_size);
947 atomic_type_properties_t *props = atomic_type_properties;
949 /* adjust types as requested by target architecture */
950 ir_type *type_long_double = be_params->type_long_double;
951 if (type_long_double != NULL) {
952 set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_long_double);
953 atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = get_type_mode(type_long_double);
956 ir_type *type_long_long = be_params->type_long_long;
957 if (type_long_long != NULL)
958 set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_long_long);
960 ir_type *type_unsigned_long_long = be_params->type_unsigned_long_long;
961 if (type_unsigned_long_long != NULL)
962 set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_unsigned_long_long);
964 /* operating system ABI specifics */
965 const char *os = target_machine->operating_system;
966 if (is_darwin_os(os)) {
967 if (machine_size == 32) {
968 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
969 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
970 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
971 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
972 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
973 props[ATOMIC_TYPE_LONG_DOUBLE].struct_alignment = 16;
975 } else if (is_windows_os(os)) {
976 if (machine_size == 64) {
977 /* to ease porting of old c-code microsoft decided to use 32bits
979 props[ATOMIC_TYPE_LONG] = props[ATOMIC_TYPE_INT];
980 props[ATOMIC_TYPE_ULONG] = props[ATOMIC_TYPE_UINT];
983 /* on windows long double is not supported */
984 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
985 } else if (is_unixish_os(os)) {
986 if (is_ia32_cpu(target_machine->cpu_type)) {
987 /* System V has a broken alignment for double so we have to add
989 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
990 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
991 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
995 /* stuff decided after processing operating system specifics and
996 * commandline flags */
997 if (char_is_signed) {
998 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
1000 props[ATOMIC_TYPE_CHAR].flags &= ~ATOMIC_TYPE_FLAG_SIGNED;
1002 /* copy over wchar_t properties (including rank) */
1003 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
1005 /* initialize defaults for unsupported types */
1006 if (type_long_long == NULL) {
1007 copy_typeprops(&props[ATOMIC_TYPE_LONGLONG], &props[ATOMIC_TYPE_LONG]);
1009 if (type_unsigned_long_long == NULL) {
1010 copy_typeprops(&props[ATOMIC_TYPE_ULONGLONG],
1011 &props[ATOMIC_TYPE_ULONG]);
1013 if (type_long_double == NULL) {
1014 copy_typeprops(&props[ATOMIC_TYPE_LONG_DOUBLE],
1015 &props[ATOMIC_TYPE_DOUBLE]);
1018 /* initialize firm pointer modes */
1020 unsigned bit_size = machine_size;
1021 unsigned modulo_shift = decide_modulo_shift(bit_size);
1023 snprintf(name, sizeof(name), "p%u", machine_size);
1024 ir_mode *ptr_mode = new_reference_mode(name, irma_twos_complement, bit_size, modulo_shift);
1026 if (machine_size == 16) {
1027 set_reference_mode_signed_eq(ptr_mode, mode_Hs);
1028 set_reference_mode_unsigned_eq(ptr_mode, mode_Hu);
1029 } else if (machine_size == 32) {
1030 set_reference_mode_signed_eq(ptr_mode, mode_Is);
1031 set_reference_mode_unsigned_eq(ptr_mode, mode_Iu);
1032 } else if (machine_size == 64) {
1033 set_reference_mode_signed_eq(ptr_mode, mode_Ls);
1034 set_reference_mode_unsigned_eq(ptr_mode, mode_Lu);
1036 panic("strange machine_size when determining pointer modes");
1039 /* Hmm, pointers should be machine size */
1040 set_modeP_data(ptr_mode);
1041 set_modeP_code(ptr_mode);
1043 byte_order_big_endian = be_params->byte_order_big_endian;
1044 if (be_params->modulo_shift_efficient) {
1045 architecture_modulo_shift = machine_size;
1047 architecture_modulo_shift = 0;
1051 int main(int argc, char **argv)
1053 const char *dumpfunction = NULL;
1054 const char *print_file_name_file = NULL;
1055 compile_mode_t mode = CompileAssembleLink;
1057 int result = EXIT_SUCCESS;
1058 char cpu_arch[16] = "ia32";
1059 file_list_entry_t *files = NULL;
1060 file_list_entry_t *last_file = NULL;
1061 bool construct_dep_target = false;
1062 bool do_timing = false;
1063 bool profile_generate = false;
1064 bool profile_use = false;
1065 struct obstack file_obst;
1067 atexit(free_temp_files);
1069 /* hack for now... */
1070 if (strstr(argv[0], "pptest") != NULL) {
1071 extern int pptest_main(int argc, char **argv);
1072 return pptest_main(argc, argv);
1075 obstack_init(&cppflags_obst);
1076 obstack_init(&ldflags_obst);
1077 obstack_init(&asflags_obst);
1078 obstack_init(&file_obst);
1080 #define GET_ARG_AFTER(def, args) \
1082 def = &arg[sizeof(args)-1]; \
1083 if (def[0] == '\0') { \
1086 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1087 argument_errors = true; \
1091 if (def[0] == '-' && def[1] != '\0') { \
1092 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1093 argument_errors = true; \
1099 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
1101 /* initialize this early because it has to parse options */
1104 /* early options parsing (find out optimization level and OS) */
1105 for (int i = 1; i < argc; ++i) {
1106 const char *arg = argv[i];
1110 const char *option = &arg[1];
1111 if (option[0] == 'O') {
1112 sscanf(&option[1], "%d", &opt_level);
1116 const char *target = getenv("TARGET");
1118 parse_target_triple(target);
1119 if (target_machine == NULL) {
1120 target_machine = firm_get_host_machine();
1122 choose_optimization_pack(opt_level);
1123 setup_target_machine();
1125 /* parse rest of options */
1126 standard = STANDARD_DEFAULT;
1127 unsigned features_on = 0;
1128 unsigned features_off = 0;
1129 filetype_t forced_filetype = FILETYPE_AUTODETECT;
1130 help_sections_t help = HELP_NONE;
1131 bool argument_errors = false;
1132 for (int i = 1; i < argc; ++i) {
1133 const char *arg = argv[i];
1134 if (arg[0] == '-' && arg[1] != '\0') {
1136 const char *option = &arg[1];
1137 if (option[0] == 'o') {
1138 GET_ARG_AFTER(outname, "-o");
1139 } else if (option[0] == 'g') {
1140 set_be_option("debuginfo=stabs");
1141 set_be_option("omitfp=no");
1142 set_be_option("ia32-nooptcc=yes");
1143 } else if (SINGLE_OPTION('c')) {
1144 mode = CompileAssemble;
1145 } else if (SINGLE_OPTION('E')) {
1146 mode = PreprocessOnly;
1147 } else if (SINGLE_OPTION('s')) {
1148 add_flag(&ldflags_obst, "-s");
1149 } else if (SINGLE_OPTION('S')) {
1151 } else if (option[0] == 'O') {
1153 } else if (option[0] == 'I') {
1155 GET_ARG_AFTER(opt, "-I");
1156 add_flag(&cppflags_obst, "-I%s", opt);
1157 } else if (option[0] == 'D') {
1159 GET_ARG_AFTER(opt, "-D");
1160 add_flag(&cppflags_obst, "-D%s", opt);
1161 } else if (option[0] == 'U') {
1163 GET_ARG_AFTER(opt, "-U");
1164 add_flag(&cppflags_obst, "-U%s", opt);
1165 } else if (option[0] == 'l') {
1167 GET_ARG_AFTER(opt, "-l");
1168 add_flag(&ldflags_obst, "-l%s", opt);
1169 } else if (option[0] == 'L') {
1171 GET_ARG_AFTER(opt, "-L");
1172 add_flag(&ldflags_obst, "-L%s", opt);
1173 } else if (SINGLE_OPTION('v')) {
1175 } else if (SINGLE_OPTION('w')) {
1176 add_flag(&cppflags_obst, "-w");
1177 disable_all_warnings();
1178 } else if (option[0] == 'x') {
1180 GET_ARG_AFTER(opt, "-x");
1181 forced_filetype = get_filetype_from_string(opt);
1182 if (forced_filetype == FILETYPE_UNKNOWN) {
1183 fprintf(stderr, "Unknown language '%s'\n", opt);
1184 argument_errors = true;
1186 } else if (streq(option, "M")) {
1187 mode = PreprocessOnly;
1188 add_flag(&cppflags_obst, "-M");
1189 } else if (streq(option, "MMD") ||
1190 streq(option, "MD")) {
1191 construct_dep_target = true;
1192 add_flag(&cppflags_obst, "-%s", option);
1193 } else if (streq(option, "MM") ||
1194 streq(option, "MP")) {
1195 add_flag(&cppflags_obst, "-%s", option);
1196 } else if (streq(option, "MT") ||
1197 streq(option, "MQ") ||
1198 streq(option, "MF")) {
1200 GET_ARG_AFTER(opt, "-MT");
1201 add_flag(&cppflags_obst, "-%s", option);
1202 add_flag(&cppflags_obst, "%s", opt);
1203 } else if (streq(option, "include")) {
1205 GET_ARG_AFTER(opt, "-include");
1206 add_flag(&cppflags_obst, "-include");
1207 add_flag(&cppflags_obst, "%s", opt);
1208 } else if (streq(option, "isystem")) {
1210 GET_ARG_AFTER(opt, "-isystem");
1211 add_flag(&cppflags_obst, "-isystem");
1212 add_flag(&cppflags_obst, "%s", opt);
1213 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1214 } else if (streq(option, "pthread")) {
1215 /* set flags for the preprocessor */
1216 add_flag(&cppflags_obst, "-D_REENTRANT");
1217 /* set flags for the linker */
1218 add_flag(&ldflags_obst, "-lpthread");
1220 } else if (streq(option, "nostdinc")
1221 || streq(option, "trigraphs")) {
1222 /* pass these through to the preprocessor */
1223 add_flag(&cppflags_obst, "%s", arg);
1224 } else if (streq(option, "pipe")) {
1225 /* here for gcc compatibility */
1226 } else if (streq(option, "static")) {
1227 add_flag(&ldflags_obst, "-static");
1228 } else if (streq(option, "shared")) {
1229 add_flag(&ldflags_obst, "-shared");
1230 } else if (option[0] == 'f') {
1231 char const *orig_opt;
1232 GET_ARG_AFTER(orig_opt, "-f");
1234 if (strstart(orig_opt, "input-charset=")) {
1235 char const* const encoding = strchr(orig_opt, '=') + 1;
1236 input_encoding = encoding;
1237 } else if (strstart(orig_opt, "align-loops=") ||
1238 strstart(orig_opt, "align-jumps=") ||
1239 strstart(orig_opt, "align-functions=")) {
1240 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1241 } else if (strstart(orig_opt, "visibility=")) {
1242 const char *val = strchr(orig_opt, '=')+1;
1243 elf_visibility_tag_t visibility
1244 = get_elf_visibility_from_string(val);
1245 if (visibility == ELF_VISIBILITY_ERROR) {
1246 fprintf(stderr, "invalid visibility '%s' specified\n",
1248 argument_errors = true;
1250 set_default_visibility(visibility);
1252 } else if (strstart(orig_opt, "message-length=")) {
1253 /* ignore: would only affect error message format */
1254 } else if (streq(orig_opt, "fast-math") ||
1255 streq(orig_opt, "fp-fast")) {
1256 firm_fp_model = fp_model_fast;
1257 } else if (streq(orig_opt, "fp-precise")) {
1258 firm_fp_model = fp_model_precise;
1259 } else if (streq(orig_opt, "fp-strict")) {
1260 firm_fp_model = fp_model_strict;
1261 } else if (streq(orig_opt, "help")) {
1262 fprintf(stderr, "warning: -fhelp is deprecated\n");
1263 help |= HELP_OPTIMIZATION;
1265 /* -f options which have an -fno- variant */
1266 char const *opt = orig_opt;
1267 bool truth_value = true;
1268 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1269 truth_value = false;
1273 if (streq(opt, "diagnostics-show-option")) {
1274 diagnostics_show_option = truth_value;
1275 } else if (streq(opt, "dollars-in-identifiers")) {
1276 allow_dollar_in_symbol = truth_value;
1277 } else if (streq(opt, "omit-frame-pointer")) {
1278 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1279 } else if (streq(opt, "short-wchar")) {
1280 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1282 } else if (streq(opt, "show-column")) {
1283 show_column = truth_value;
1284 } else if (streq(opt, "signed-char")) {
1285 char_is_signed = truth_value;
1286 } else if (streq(opt, "strength-reduce")) {
1287 /* does nothing, for gcc compatibility (even gcc does
1288 * nothing for this switch anymore) */
1289 } else if (streq(opt, "syntax-only")) {
1290 mode = truth_value ? ParseOnly : CompileAssembleLink;
1291 } else if (streq(opt, "unsigned-char")) {
1292 char_is_signed = !truth_value;
1293 } else if (streq(opt, "freestanding")) {
1294 freestanding = truth_value;
1295 } else if (streq(opt, "hosted")) {
1296 freestanding = !truth_value;
1297 } else if (streq(opt, "profile-generate")) {
1298 profile_generate = truth_value;
1299 } else if (streq(opt, "profile-use")) {
1300 profile_use = truth_value;
1301 } else if (!truth_value &&
1302 streq(opt, "asynchronous-unwind-tables")) {
1303 /* nothing todo, a gcc feature which we do not support
1304 * anyway was deactivated */
1305 } else if (streq(opt, "verbose-asm")) {
1306 /* ignore: we always print verbose assembler */
1307 } else if (streq(opt, "jump-tables") ||
1308 streq(opt, "expensive-optimizations") ||
1309 streq(opt, "common") ||
1310 streq(opt, "optimize-sibling-calls") ||
1311 streq(opt, "align-loops") ||
1312 streq(opt, "align-jumps") ||
1313 streq(opt, "align-functions") ||
1314 streq(opt, "unroll-loops") ||
1315 streq(opt, "PIC") ||
1316 streq(opt, "stack-protector") ||
1317 streq(opt, "stack-protector-all")) {
1318 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1320 int res = firm_option(orig_opt);
1322 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1324 argument_errors = true;
1329 } else if (option[0] == 'b') {
1331 GET_ARG_AFTER(opt, "-b");
1333 if (streq(opt, "help")) {
1334 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1337 int res = be_parse_arg(opt);
1339 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1341 argument_errors = true;
1342 } else if (strstart(opt, "isa=")) {
1343 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1346 } else if (option[0] == 'W') {
1347 if (strstart(option + 1, "p,")) {
1348 // pass options directly to the preprocessor
1350 GET_ARG_AFTER(opt, "-Wp,");
1351 add_flag(&cppflags_obst, "-Wp,%s", opt);
1352 } else if (strstart(option + 1, "l,")) {
1353 // pass options directly to the linker
1355 GET_ARG_AFTER(opt, "-Wl,");
1356 add_flag(&ldflags_obst, "-Wl,%s", opt);
1357 } else if (streq(option + 1, "no-trigraphs")
1358 || streq(option + 1, "undef")
1359 || streq(option + 1, "missing-include-dirs")
1360 || streq(option + 1, "endif-labels")) {
1361 add_flag(&cppflags_obst, "%s", arg);
1362 } else if (streq(option+1, "init-self")) {
1363 /* ignored (same as gcc does) */
1364 } else if (streq(option+1, "format-y2k")
1365 || streq(option+1, "format-security")
1366 || streq(option+1, "old-style-declaration")
1367 || streq(option+1, "type-limits")) {
1368 /* ignore (gcc compatibility) */
1370 set_warning_opt(&option[1]);
1372 } else if (option[0] == 'm') {
1377 GET_ARG_AFTER(opt, "-m");
1378 if (strstart(opt, "target=")) {
1379 GET_ARG_AFTER(opt, "-mtarget=");
1380 if (!parse_target_triple(opt)) {
1381 argument_errors = true;
1383 const char *isa = setup_target_machine();
1384 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1385 target_triple = opt;
1387 } else if (strstart(opt, "triple=")) {
1388 GET_ARG_AFTER(opt, "-mtriple=");
1389 if (!parse_target_triple(opt)) {
1390 argument_errors = true;
1392 const char *isa = setup_target_machine();
1393 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1394 target_triple = opt;
1396 } else if (strstart(opt, "arch=")) {
1397 GET_ARG_AFTER(opt, "-march=");
1398 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1399 int res = be_parse_arg(arch_opt);
1400 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1401 res &= be_parse_arg(arch_opt);
1404 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1405 argument_errors = true;
1407 } else if (strstart(opt, "tune=")) {
1408 GET_ARG_AFTER(opt, "-mtune=");
1409 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1410 int res = be_parse_arg(arch_opt);
1412 argument_errors = true;
1413 } else if (strstart(opt, "cpu=")) {
1414 GET_ARG_AFTER(opt, "-mcpu=");
1415 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1416 int res = be_parse_arg(arch_opt);
1418 argument_errors = true;
1419 } else if (strstart(opt, "fpmath=")) {
1420 GET_ARG_AFTER(opt, "-mfpmath=");
1421 if (streq(opt, "387"))
1423 else if (streq(opt, "sse"))
1426 fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
1427 argument_errors = true;
1429 if (!argument_errors) {
1430 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1431 int res = be_parse_arg(arch_opt);
1433 argument_errors = true;
1435 } else if (strstart(opt, "preferred-stack-boundary=")) {
1436 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1437 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1438 int res = be_parse_arg(arch_opt);
1440 argument_errors = true;
1441 } else if (streq(opt, "rtd")) {
1442 default_calling_convention = CC_STDCALL;
1443 } else if (strstart(opt, "regparm=")) {
1444 fprintf(stderr, "error: regparm convention not supported yet\n");
1445 argument_errors = true;
1446 } else if (streq(opt, "soft-float")) {
1447 add_flag(&ldflags_obst, "-msoft-float");
1448 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
1449 int res = be_parse_arg(arch_opt);
1451 argument_errors = true;
1453 long int value = strtol(opt, NULL, 10);
1455 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1456 argument_errors = true;
1457 } else if (value != 16 && value != 32 && value != 64) {
1458 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1459 argument_errors = true;
1461 unsigned machine_size = (unsigned)value;
1462 /* TODO: choose/change backend based on this */
1463 add_flag(&cppflags_obst, "-m%u", machine_size);
1464 add_flag(&asflags_obst, "-m%u", machine_size);
1465 add_flag(&ldflags_obst, "-m%u", machine_size);
1468 } else if (streq(option, "pg")) {
1469 set_be_option("gprof");
1470 add_flag(&ldflags_obst, "-pg");
1471 } else if (streq(option, "ansi")) {
1472 add_flag(&cppflags_obst, "-ansi");
1473 } else if (streq(option, "pedantic")) {
1474 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1475 } else if (strstart(option, "std=")) {
1476 const char *const o = &option[4];
1478 streq(o, "c++") ? STANDARD_CXX98 :
1479 streq(o, "c++98") ? STANDARD_CXX98 :
1480 streq(o, "c89") ? STANDARD_C89 :
1481 streq(o, "c99") ? STANDARD_C99 :
1482 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1483 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1484 streq(o, "gnu89") ? STANDARD_GNU89 :
1485 streq(o, "gnu99") ? STANDARD_GNU99 :
1486 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1487 streq(o, "iso9899:1990") ? STANDARD_C89 :
1488 streq(o, "iso9899:199409") ? STANDARD_C90 :
1489 streq(o, "iso9899:1999") ? STANDARD_C99 :
1490 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1491 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1492 } else if (streq(option, "version")) {
1493 print_cparser_version();
1494 return EXIT_SUCCESS;
1495 } else if (streq(option, "dumpversion")) {
1496 /* gcc compatibility option */
1497 print_cparser_version_short();
1498 return EXIT_SUCCESS;
1499 } else if (strstart(option, "print-file-name=")) {
1500 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1501 } else if (option[0] == '-') {
1502 /* double dash option */
1504 if (streq(option, "gcc")) {
1505 features_on |= _GNUC;
1506 features_off &= ~_GNUC;
1507 } else if (streq(option, "no-gcc")) {
1508 features_on &= ~_GNUC;
1509 features_off |= _GNUC;
1510 } else if (streq(option, "ms")) {
1512 features_off &= ~_MS;
1513 } else if (streq(option, "no-ms")) {
1514 features_on &= ~_MS;
1515 features_off |= _MS;
1516 } else if (streq(option, "strict")) {
1518 } else if (streq(option, "lextest")) {
1520 } else if (streq(option, "benchmark")) {
1521 mode = BenchmarkParser;
1522 } else if (streq(option, "print-ast")) {
1524 } else if (streq(option, "print-implicit-cast")) {
1525 print_implicit_casts = true;
1526 } else if (streq(option, "print-parenthesis")) {
1527 print_parenthesis = true;
1528 } else if (streq(option, "print-fluffy")) {
1530 } else if (streq(option, "print-jna")) {
1532 } else if (streq(option, "jna-limit")) {
1535 fprintf(stderr, "error: "
1536 "expected argument after '--jna-limit'\n");
1537 argument_errors = true;
1540 jna_limit_output(argv[i]);
1541 } else if (streq(option, "jna-libname")) {
1544 fprintf(stderr, "error: "
1545 "expected argument after '--jna-libname'\n");
1546 argument_errors = true;
1549 jna_set_libname(argv[i]);
1550 } else if (streq(option, "time")) {
1552 } else if (streq(option, "version")) {
1553 print_cparser_version();
1554 return EXIT_SUCCESS;
1555 } else if (streq(option, "help")) {
1557 } else if (streq(option, "help-parser")) {
1558 help |= HELP_PARSER;
1559 } else if (streq(option, "help-warnings")) {
1560 help |= HELP_WARNINGS;
1561 } else if (streq(option, "help-codegen")) {
1562 help |= HELP_CODEGEN;
1563 } else if (streq(option, "help-linker")) {
1564 help |= HELP_LINKER;
1565 } else if (streq(option, "help-optimization")) {
1566 help |= HELP_OPTIMIZATION;
1567 } else if (streq(option, "help-language-tools")) {
1568 help |= HELP_LANGUAGETOOLS;
1569 } else if (streq(option, "help-debug")) {
1571 } else if (streq(option, "help-firm")) {
1573 } else if (streq(option, "help-all")) {
1575 } else if (streq(option, "dump-function")) {
1578 fprintf(stderr, "error: "
1579 "expected argument after '--dump-function'\n");
1580 argument_errors = true;
1583 dumpfunction = argv[i];
1585 } else if (streq(option, "export-ir")) {
1586 mode = CompileExportIR;
1587 } else if (streq(option, "unroll-loops")) {
1588 /* ignore (gcc compatibility) */
1590 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1591 argument_errors = true;
1594 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1595 argument_errors = true;
1598 filetype_t type = forced_filetype;
1599 if (type == FILETYPE_AUTODETECT) {
1600 if (streq(arg, "-")) {
1601 /* - implicitly means C source file */
1604 const char *suffix = strrchr(arg, '.');
1605 /* Ensure there is at least one char before the suffix */
1606 if (suffix != NULL && suffix != arg) {
1609 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1610 streq(suffix, "a") ? FILETYPE_OBJECT :
1611 streq(suffix, "c") ? FILETYPE_C :
1612 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1613 streq(suffix, "C") ? FILETYPE_CXX :
1614 streq(suffix, "cc") ? FILETYPE_CXX :
1615 streq(suffix, "cp") ? FILETYPE_CXX :
1616 streq(suffix, "cpp") ? FILETYPE_CXX :
1617 streq(suffix, "CPP") ? FILETYPE_CXX :
1618 streq(suffix, "cxx") ? FILETYPE_CXX :
1619 streq(suffix, "c++") ? FILETYPE_CXX :
1620 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1621 streq(suffix, "h") ? FILETYPE_C :
1622 streq(suffix, "ir") ? FILETYPE_IR :
1623 streq(suffix, "o") ? FILETYPE_OBJECT :
1624 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1625 streq(suffix, "so") ? FILETYPE_OBJECT :
1626 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1631 file_list_entry_t *entry
1632 = obstack_alloc(&file_obst, sizeof(entry[0]));
1633 memset(entry, 0, sizeof(entry[0]));
1637 if (last_file != NULL) {
1638 last_file->next = entry;
1646 if (help != HELP_NONE) {
1647 print_help(argv[0], help);
1648 return !argument_errors;
1651 if (print_file_name_file != NULL) {
1652 print_file_name(print_file_name_file);
1653 return EXIT_SUCCESS;
1655 if (files == NULL) {
1656 fprintf(stderr, "error: no input files specified\n");
1657 argument_errors = true;
1660 if (argument_errors) {
1662 return EXIT_FAILURE;
1665 /* apply some effects from switches */
1666 c_mode |= features_on;
1667 c_mode &= ~features_off;
1668 if (profile_generate) {
1669 add_flag(&ldflags_obst, "-lfirmprof");
1670 set_be_option("profilegenerate");
1673 set_be_option("profileuse");
1676 init_symbol_table();
1677 init_types_and_adjust();
1680 if (wchar_atomic_kind == ATOMIC_TYPE_INT)
1681 init_wchar_types(type_int);
1682 else if (wchar_atomic_kind == ATOMIC_TYPE_USHORT)
1683 init_wchar_types(type_short);
1685 panic("unexpected wchar type");
1695 if (construct_dep_target) {
1696 if (outname != 0 && strlen(outname) >= 2) {
1697 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1699 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1702 dep_target[0] = '\0';
1705 char outnamebuf[4096];
1706 if (outname == NULL) {
1707 const char *filename = files->name;
1710 case BenchmarkParser:
1715 case PreprocessOnly:
1720 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1721 outname = outnamebuf;
1723 case CompileAssemble:
1724 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1725 outname = outnamebuf;
1728 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1730 outname = outnamebuf;
1732 case CompileExportIR:
1733 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1734 outname = outnamebuf;
1736 case CompileAssembleLink:
1746 assert(outname != NULL);
1749 if (streq(outname, "-")) {
1752 out = fopen(outname, "w");
1754 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
1756 return EXIT_FAILURE;
1760 file_list_entry_t *file;
1761 bool already_constructed_firm = false;
1762 for (file = files; file != NULL; file = file->next) {
1763 char asm_tempfile[1024];
1764 const char *filename = file->name;
1765 filetype_t filetype = file->type;
1767 if (filetype == FILETYPE_OBJECT)
1771 if (mode == LexTest) {
1773 in = open_file(filename);
1774 lextest(in, filename);
1776 return EXIT_SUCCESS;
1779 FILE *preprocessed_in = NULL;
1780 filetype_t next_filetype = filetype;
1783 next_filetype = FILETYPE_PREPROCESSED_C;
1786 next_filetype = FILETYPE_PREPROCESSED_CXX;
1788 case FILETYPE_ASSEMBLER:
1789 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1792 /* no support for input on FILE* yet */
1794 panic("internal compiler error: in for preprocessor != NULL");
1796 preprocessed_in = preprocess(filename, filetype);
1797 if (mode == PreprocessOnly) {
1798 copy_file(out, preprocessed_in);
1799 int pp_result = pclose(preprocessed_in);
1801 /* remove output file in case of error */
1802 if (out != stdout && pp_result != EXIT_SUCCESS) {
1808 in = preprocessed_in;
1809 filetype = next_filetype;
1817 if (mode == Compile) {
1820 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1824 in = open_file(filename);
1826 /* preprocess and compile */
1827 if (filetype == FILETYPE_PREPROCESSED_C) {
1828 char const* invalid_mode;
1831 case STANDARD_C89: c_mode = _C89; break;
1832 /* TODO determine difference between these two */
1833 case STANDARD_C90: c_mode = _C89; break;
1834 case STANDARD_C99: c_mode = _C89 | _C99; break;
1835 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1839 "warning: command line option \"-std=%s\" is not valid for C\n",
1842 case STANDARD_DEFAULT:
1843 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1845 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1846 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1849 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1850 char const* invalid_mode;
1852 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1853 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1854 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1855 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1856 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1859 case STANDARD_CXX98: c_mode = _CXX; break;
1863 "warning: command line option \"-std=%s\" is not valid for C++\n",
1865 case STANDARD_DEFAULT:
1866 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1870 c_mode |= features_on;
1871 c_mode &= ~features_off;
1873 /* do the actual parsing */
1874 ir_timer_t *t_parsing = ir_timer_new();
1875 timer_register(t_parsing, "Frontend: Parsing");
1876 timer_push(t_parsing);
1878 translation_unit_t *const unit = do_parsing(in, filename);
1879 timer_pop(t_parsing);
1881 /* prints the AST even if errors occurred */
1882 if (mode == PrintAst) {
1887 if (error_count > 0) {
1888 /* parsing failed because of errors */
1889 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1891 result = EXIT_FAILURE;
1893 } else if (warning_count > 0) {
1894 fprintf(stderr, "%u warning(s)\n", warning_count);
1897 if (in == preprocessed_in) {
1898 int pp_result = pclose(preprocessed_in);
1899 if (pp_result != EXIT_SUCCESS) {
1900 /* remove output file */
1903 return EXIT_FAILURE;
1907 if (mode == BenchmarkParser) {
1909 } else if (mode == PrintFluffy) {
1910 write_fluffy_decls(out, unit);
1912 } else if (mode == PrintJna) {
1913 write_jna_decls(out, unit);
1917 /* build the firm graph */
1918 ir_timer_t *t_construct = ir_timer_new();
1919 timer_register(t_construct, "Frontend: Graph construction");
1920 timer_push(t_construct);
1921 if (already_constructed_firm) {
1922 panic("compiling multiple files/translation units not possible");
1924 translation_unit_to_firm(unit);
1925 already_constructed_firm = true;
1926 timer_pop(t_construct);
1929 if (mode == ParseOnly) {
1933 if (mode == CompileDump) {
1935 ident *id = new_id_from_str(dumpfunction);
1936 ir_graph *irg = NULL;
1937 int n_irgs = get_irp_n_irgs();
1938 for (int i = 0; i < n_irgs; ++i) {
1939 ir_graph *tirg = get_irp_irg(i);
1940 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1948 fprintf(stderr, "No graph for function '%s' found\n",
1950 return EXIT_FAILURE;
1953 dump_ir_graph_file(out, irg);
1955 return EXIT_SUCCESS;
1958 if (mode == CompileExportIR) {
1959 ir_export_file(out);
1960 if (ferror(out) != 0) {
1961 fprintf(stderr, "Error while writing to output\n");
1962 return EXIT_FAILURE;
1964 return EXIT_SUCCESS;
1967 generate_code(asm_out, filename);
1968 if (asm_out != out) {
1971 } else if (filetype == FILETYPE_IR) {
1973 int res = ir_import(filename);
1975 fprintf(stderr, "Firm-Program import failed\n");
1976 return EXIT_FAILURE;
1979 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1980 copy_file(asm_out, in);
1981 if (in == preprocessed_in) {
1982 int pp_result = pclose(preprocessed_in);
1983 if (pp_result != EXIT_SUCCESS) {
1984 /* remove output in error case */
1990 if (asm_out != out) {
1995 if (mode == Compile)
1998 /* if we're here then we have preprocessed assembly */
1999 filename = asm_tempfile;
2000 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
2003 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
2005 const char *filename_o;
2006 if (mode == CompileAssemble) {
2008 filename_o = outname;
2010 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
2015 assemble(filename_o, filename);
2017 size_t len = strlen(filename_o) + 1;
2018 filename = obstack_copy(&file_obst, filename_o, len);
2019 filetype = FILETYPE_OBJECT;
2022 /* ok we're done here, process next file */
2023 file->name = filename;
2024 file->type = filetype;
2027 if (result != EXIT_SUCCESS) {
2033 /* link program file */
2034 if (mode == CompileAssembleLink) {
2035 obstack_1grow(&ldflags_obst, '\0');
2036 const char *flags = obstack_finish(&ldflags_obst);
2038 /* construct commandline */
2039 const char *linker = getenv("CPARSER_LINK");
2040 if (linker != NULL) {
2041 obstack_printf(&file_obst, "%s ", linker);
2043 if (target_triple != NULL)
2044 obstack_printf(&file_obst, "%s-", target_triple);
2045 obstack_printf(&file_obst, "%s ", LINKER);
2048 for (file_list_entry_t *entry = files; entry != NULL;
2049 entry = entry->next) {
2050 if (entry->type != FILETYPE_OBJECT)
2053 add_flag(&file_obst, "%s", entry->name);
2056 add_flag(&file_obst, "-o");
2057 add_flag(&file_obst, outname);
2058 obstack_printf(&file_obst, "%s", flags);
2059 obstack_1grow(&file_obst, '\0');
2061 char *commandline = obstack_finish(&file_obst);
2066 int err = system(commandline);
2067 if (err != EXIT_SUCCESS) {
2068 fprintf(stderr, "linker reported an error\n");
2069 return EXIT_FAILURE;
2076 obstack_free(&cppflags_obst, NULL);
2077 obstack_free(&ldflags_obst, NULL);
2078 obstack_free(&asflags_obst, NULL);
2079 obstack_free(&file_obst, NULL);
2090 exit_symbol_table();
2091 return EXIT_SUCCESS;