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>
63 #include "preprocessor.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 | _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_C89, /* ISO C90 (sic) */
125 STANDARD_C89AMD1, /* 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 typedef struct file_list_entry_t file_list_entry_t;
135 typedef enum filetype_t {
138 FILETYPE_PREPROCESSED_C,
140 FILETYPE_PREPROCESSED_CXX,
142 FILETYPE_PREPROCESSED_ASSEMBLER,
148 struct file_list_entry_t {
149 const char *name; /**< filename or NULL for stdin */
151 file_list_entry_t *next;
154 static file_list_entry_t *temp_files;
156 static void get_output_name(char *buf, size_t buflen, const char *inputname,
159 if (inputname == NULL)
162 char const *const last_slash = strrchr(inputname, '/');
163 char const *const filename =
164 last_slash != NULL ? last_slash + 1 : inputname;
165 char const *const last_dot = strrchr(filename, '.');
166 char const *const name_end =
167 last_dot != NULL ? last_dot : strchr(filename, '\0');
169 int const len = snprintf(buf, buflen, "%.*s%s",
170 (int)(name_end - filename), filename, newext);
172 if (len < 0 || buflen <= (size_t)len)
174 if (buflen <= (size_t)len)
176 panic("filename too long");
179 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
183 switch_input(in, input_name);
185 translation_unit_t *unit = finish_parsing();
191 static void lextest(FILE *in, const char *fname)
193 switch_input(in, fname);
195 next_preprocessing_token();
196 print_token(stdout, &pp_token);
198 } while (pp_token.kind != T_EOF);
202 static void add_flag(struct obstack *obst, const char *format, ...)
207 va_start(ap, format);
211 vsnprintf(buf, sizeof(buf), format, ap);
214 obstack_1grow(obst, ' ');
216 obstack_1grow(obst, '"');
217 obstack_grow(obst, buf, len);
218 obstack_1grow(obst, '"');
220 /* escape stuff... */
221 for (char *c = buf; *c != '\0'; ++c) {
239 obstack_1grow(obst, '\\');
242 obstack_1grow(obst, *c);
249 static const char *type_to_string(type_t *type)
251 assert(type->kind == TYPE_ATOMIC);
252 return get_atomic_kind_name(type->atomic.akind);
255 static char const* str_lang_standard(lang_standard_t const standard)
258 case STANDARD_C89: return "c89";
259 case STANDARD_C89AMD1: return "iso9899:199409";
260 case STANDARD_C99: return "c99";
261 case STANDARD_GNU89: return "gnu89";
262 case STANDARD_GNU99: return "gnu99";
263 case STANDARD_CXX98: return "c++98";
264 case STANDARD_GNUXX98: return "gnu++98";
265 case STANDARD_DEFAULT: break;
267 panic("invalid standard");
270 static FILE *preprocess(const char *fname, filetype_t filetype,
271 lang_standard_t standard)
273 static const char *common_flags = NULL;
275 if (common_flags == NULL) {
276 obstack_1grow(&cppflags_obst, '\0');
277 const char *flags = obstack_finish(&cppflags_obst);
279 /* setup default defines */
280 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
281 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
282 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
283 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
285 add_flag(&cppflags_obst, "-U__VERSION__");
286 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
288 if (define_intmax_types) {
289 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
290 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
291 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
292 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
295 if (flags[0] != '\0') {
296 size_t len = strlen(flags);
297 obstack_1grow(&cppflags_obst, ' ');
298 obstack_grow(&cppflags_obst, flags, len);
300 obstack_1grow(&cppflags_obst, '\0');
301 common_flags = obstack_finish(&cppflags_obst);
304 assert(obstack_object_size(&cppflags_obst) == 0);
306 const char *preprocessor = getenv("CPARSER_PP");
307 if (preprocessor != NULL) {
308 obstack_printf(&cppflags_obst, "%s ", preprocessor);
310 if (target_triple != NULL)
311 obstack_printf(&cppflags_obst, "%s-", target_triple);
312 obstack_printf(&cppflags_obst, PREPROCESSOR);
317 case FILETYPE_C: lang = "c"; break;
318 case FILETYPE_CXX: lang = "c++"; break;
319 case FILETYPE_ASSEMBLER: lang = "assembler-with-cpp"; break;
320 default: lang = NULL; break;
323 add_flag(&cppflags_obst, "-x%s", lang);
325 add_flag(&cppflags_obst, "-std=%s", str_lang_standard(standard));
327 obstack_printf(&cppflags_obst, "%s", common_flags);
329 /* handle dependency generation */
330 if (dep_target[0] != '\0') {
331 add_flag(&cppflags_obst, "-MF");
332 add_flag(&cppflags_obst, dep_target);
333 if (outname != NULL) {
334 add_flag(&cppflags_obst, "-MQ");
335 add_flag(&cppflags_obst, outname);
338 add_flag(&cppflags_obst, fname);
339 obstack_1grow(&cppflags_obst, '\0');
341 char *commandline = obstack_finish(&cppflags_obst);
345 FILE *f = popen(commandline, "r");
347 fprintf(stderr, "invoking preprocessor failed\n");
350 /* we do not really need that anymore */
351 obstack_free(&cppflags_obst, commandline);
356 static void assemble(const char *out, const char *in)
358 obstack_1grow(&asflags_obst, '\0');
359 const char *flags = obstack_finish(&asflags_obst);
361 const char *assembler = getenv("CPARSER_AS");
362 if (assembler != NULL) {
363 obstack_printf(&asflags_obst, "%s", assembler);
365 if (target_triple != NULL)
366 obstack_printf(&asflags_obst, "%s-", target_triple);
367 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
369 if (flags[0] != '\0')
370 obstack_printf(&asflags_obst, " %s", flags);
372 obstack_printf(&asflags_obst, " %s -o %s", in, out);
373 obstack_1grow(&asflags_obst, '\0');
375 char *commandline = obstack_finish(&asflags_obst);
379 int err = system(commandline);
380 if (err != EXIT_SUCCESS) {
381 fprintf(stderr, "assembler reported an error\n");
384 obstack_free(&asflags_obst, commandline);
387 static void print_file_name(const char *file)
389 add_flag(&ldflags_obst, "-print-file-name=%s", file);
391 obstack_1grow(&ldflags_obst, '\0');
392 const char *flags = obstack_finish(&ldflags_obst);
394 /* construct commandline */
395 const char *linker = getenv("CPARSER_LINK");
396 if (linker != NULL) {
397 obstack_printf(&ldflags_obst, "%s ", linker);
399 if (target_triple != NULL)
400 obstack_printf(&ldflags_obst, "%s-", target_triple);
401 obstack_printf(&ldflags_obst, "%s ", LINKER);
403 obstack_printf(&ldflags_obst, "%s ", linker);
404 obstack_printf(&ldflags_obst, "%s", flags);
405 obstack_1grow(&ldflags_obst, '\0');
407 char *commandline = obstack_finish(&ldflags_obst);
411 int err = system(commandline);
412 if (err != EXIT_SUCCESS) {
413 fprintf(stderr, "linker reported an error\n");
416 obstack_free(&ldflags_obst, commandline);
419 static const char *try_dir(const char *dir)
423 if (access(dir, R_OK | W_OK | X_OK) == 0)
428 static const char *get_tempdir(void)
430 static const char *tmpdir = NULL;
436 tmpdir = try_dir(getenv("TMPDIR"));
438 tmpdir = try_dir(getenv("TMP"));
440 tmpdir = try_dir(getenv("TEMP"));
444 tmpdir = try_dir(P_tmpdir);
448 tmpdir = try_dir("/var/tmp");
450 tmpdir = try_dir("/usr/tmp");
452 tmpdir = try_dir("/tmp");
461 /* cheap and nasty mkstemp replacement */
462 static int mkstemp(char *templ)
465 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
470 * an own version of tmpnam, which: writes in a buffer, emits no warnings
471 * during linking (like glibc/gnu ld do for tmpnam)...
473 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
475 const char *tempdir = get_tempdir();
477 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
479 int fd = mkstemp(buffer);
481 fprintf(stderr, "could not create temporary file: %s\n",
485 FILE *out = fdopen(fd, "w");
487 fprintf(stderr, "could not create temporary file FILE*\n");
491 file_list_entry_t *entry = xmalloc(sizeof(*entry));
492 memset(entry, 0, sizeof(*entry));
494 size_t name_len = strlen(buffer) + 1;
495 char *name = malloc(name_len);
496 memcpy(name, buffer, name_len);
499 entry->next = temp_files;
505 static void free_temp_files(void)
507 file_list_entry_t *entry = temp_files;
508 file_list_entry_t *next;
509 for ( ; entry != NULL; entry = next) {
513 free((char*) entry->name);
519 typedef enum compile_mode_t {
534 static void usage(const char *argv0)
536 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
539 static void print_cparser_version(void)
541 printf("cparser (%s) using libFirm (%u.%u",
542 cparser_REVISION, ir_get_version_major(),
543 ir_get_version_minor());
545 const char *revision = ir_get_version_revision();
546 if (revision[0] != 0) {
548 fputs(revision, stdout);
551 const char *build = ir_get_version_build();
554 fputs(build, stdout);
557 puts("This is free software; see the source for copying conditions. There is NO\n"
558 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
561 static void print_cparser_version_short(void)
563 puts(cparser_REVISION);
566 static void print_help_basic(const char *argv0)
570 put_help("--help", "Display this information");
571 put_help("--version", "Display compiler version");
572 put_help("--help-parser", "Display information about parser options");
573 put_help("--help-warnings", "Display information about warning options");
574 put_help("--help-codegen", "Display information about code-generation options");
575 put_help("--help-optimization", "Display information about optimization options");
576 put_help("--help-linker", "Display information about linker options");
577 put_help("--help-language-tools", "Display information about language tools options");
578 put_help("--help-debug", "Display information about compiler debugging options");
579 put_help("--help-firm", "Display information about direct firm options");
580 put_help("--help-all", "Display information about all options");
581 put_help("-c", "Compile and assemble but do not link");
582 put_help("-E", "Preprocess only");
583 put_help("-S", "Compile but do not assembler or link");
584 put_help("-o", "Specify output file");
585 put_help("-v", "Verbose output (show invocation of sub-processes)");
586 put_help("-x", "Force input language:");
587 put_choice("c", "C");
588 put_choice("c++", "C++");
589 put_choice("assembler", "Assembler (no preprocessing)");
590 put_choice("assembler-with-cpp", "Assembler with preprocessing");
591 put_choice("none", "Autodetection");
592 put_help("-pipe", "Ignored (gcc compatibility)");
595 static void print_help_preprocessor(void)
597 put_help("-nostdinc", "Do not search standard system include directories");
598 put_help("-trigraphs", "Support ISO C trigraphs");
599 put_help("-isystem", "");
600 put_help("-include", "");
601 put_help("-I PATH", "");
602 put_help("-D SYMBOL[=value]", "");
603 put_help("-U SYMBOL", "");
604 put_help("-Wp,OPTION", "Pass option directly to preprocessor");
607 put_help("-MMD", "");
615 static void print_help_parser(void)
617 put_help("-finput-charset=CHARSET", "Select encoding of input files");
618 put_help("-fmessage-length=LEN", "Ignored (gcc compatibility)");
619 put_help("-fshort-wchar", "Type \"wchar_t\" is unsigned short instead of int");
620 put_help("-fshow-column", "Show the column number in diagnostic messages");
621 put_help("-fsigned-char", "Type \"char\" is a signed type");
622 put_help("-funsigned-char", "Type \"char\" is an unsigned type");
623 put_help("--ms", "Enable msvc extensions");
624 put_help("--no-ms", "Disable msvc extensions");
625 put_help("--gcc", "Enable gcc extensions");
626 put_help("--no-gcc", "Disable gcc extensions");
627 put_help("-std=STANDARD", "Specify language standard:");
628 put_choice("c99", "ISO C99 standard");
629 put_choice("c89", "ISO C89 standard");
630 put_choice("c90", "Same as -std=c89");
631 put_choice("c9x", "Deprecated");
632 put_choice("c++", "ISO C++ 98");
633 put_choice("c++98", "ISO C++ 98");
634 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
635 put_choice("gnu89", "ISO C89 + GNU extensions");
636 put_choice("gnu9x", "Deprecated");
637 put_choice("iso9899:1990", "ISO C89");
638 put_choice("iso9899:199409", "ISO C90");
639 put_choice("iso9899:1999", "ISO C99");
640 put_choice("iso9899:199x", "Deprecated");
641 put_help("-pedantic", "Ignored (gcc compatibility)");
642 put_help("-ansi", "Ignored (gcc compatibility)");
643 put_help("--strict", "Enable strict conformance checking");
646 static void print_help_warnings(void)
648 put_help("-f[no-]diagnostics-show-option", "Show the switch, which controls a warning, after each warning");
649 put_help("-w", "Disable all warnings");
650 put_help("-Wno-trigraphs", "Warn if input contains trigraphs");
651 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
652 put_help("-Wmissing-include-dirs", "Warn about missing user-specified include directories");
653 put_help("-Wendif-labels", "Warn about stray text after #elif and #endif");
654 put_help("-Winit-self", "Ignored (gcc compatibility)");
655 put_help("-Wformat-y2k", "Ignored (gcc compatibility)");
656 put_help("-Wformat-security", "Ignored (gcc compatibility)");
657 put_help("-Wold-style-declaration", "Ignored (gcc compatibility)");
658 put_help("-Wtype-limits", "Ignored (gcc compatibility)");
659 print_warning_opt_help();
662 static void print_help_optimization(void)
664 put_help("-O LEVEL", "Select optimization level (0-4)");
665 firm_option_help(put_help);
666 put_help("-fexpensive-optimizations","Ignored (gcc compatibility)");
669 static void print_help_codegeneration(void)
671 put_help("-g", "Generate debug information");
672 put_help("-pg", "Instrument code for gnu gprof");
673 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
674 put_help("-ffreestanding", "Compile in freestanding mode (see ISO C standard)");
675 put_help("-fhosted", "Compile in hosted (not freestanding) mode");
676 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
677 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
678 put_help("-ffp-precise", "Precise floating point model");
679 put_help("-ffp-fast", "Imprecise floating point model");
680 put_help("-ffp-strict", "Strict floating point model");
681 put_help("-pthread", "Use pthread threading library");
682 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
683 put_help("-mtriple=TARGET", "Alias for -mtarget (clang compatibility)");
684 put_help("-march=ARCH", "");
685 put_help("-mtune=ARCH", "");
686 put_help("-mcpu=CPU", "");
687 put_help("-mfpmath=", "");
688 put_help("-mpreferred-stack-boundary=", "");
689 put_help("-mrtd", "");
690 put_help("-mregparm=", "Not supported yet");
691 put_help("-msoft-float", "Not supported yet");
692 put_help("-m32", "Generate 32bit code");
693 put_help("-m64", "Generate 64bit code");
694 put_help("-fverbose-asm", "Ignored (gcc compatibility)");
695 put_help("-fjump-tables", "Ignored (gcc compatibility)");
696 put_help("-fcommon", "Ignored (gcc compatibility)");
697 put_help("-foptimize-sibling-calls", "Ignored (gcc compatibility)");
698 put_help("-falign-loops", "Ignored (gcc compatibility)");
699 put_help("-falign-jumps", "Ignored (gcc compatibility)");
700 put_help("-falign-functions", "Ignored (gcc compatibility)");
701 put_help("-fPIC", "Ignored (gcc compatibility)");
702 put_help("-ffast-math", "Same as -ffp-fast (gcc compatibility)");
704 puts("\tMost of these options can be used with a no- prefix to disable them");
705 puts("\te.g. -fno-omit-frame-pointer");
708 static void print_help_linker(void)
710 put_help("-l LIBRARY", "");
711 put_help("-L PATH", "");
712 put_help("-s", "Do not produce symbol table and relocation information");
713 put_help("-shared", "Produce a shared library");
714 put_help("-static", "Produce statically linked binary");
715 put_help("-Wl,OPTION", "Pass option directly to linker");
718 static void print_help_debug(void)
720 put_help("--lextest", "Preprocess and tokenize only");
721 put_help("--print-ast", "Preprocess, parse and print AST");
722 put_help("--print-implicit-cast", "");
723 put_help("--print-parenthesis", "");
724 put_help("--benchmark", "Preprocess and parse, produces no output");
725 put_help("--time", "Measure time of compiler passes");
726 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
727 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
730 static void print_help_language_tools(void)
732 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
733 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
734 put_help("--jna-limit filename", "");
735 put_help("--jna-libname name", "");
738 static void print_help_firm(void)
740 put_help("-bOPTION", "Directly pass option to libFirm backend");
741 int res = be_parse_arg("help");
749 HELP_PREPROCESSOR = 1 << 1,
750 HELP_PARSER = 1 << 2,
751 HELP_WARNINGS = 1 << 3,
752 HELP_OPTIMIZATION = 1 << 4,
753 HELP_CODEGEN = 1 << 5,
754 HELP_LINKER = 1 << 6,
755 HELP_LANGUAGETOOLS = 1 << 7,
762 static void print_help(const char *argv0, help_sections_t sections)
764 if (sections & HELP_BASIC) print_help_basic(argv0);
765 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
766 if (sections & HELP_PARSER) print_help_parser();
767 if (sections & HELP_WARNINGS) print_help_warnings();
768 if (sections & HELP_OPTIMIZATION) print_help_optimization();
769 if (sections & HELP_CODEGEN) print_help_codegeneration();
770 if (sections & HELP_LINKER) print_help_linker();
771 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
772 if (sections & HELP_DEBUG) print_help_debug();
773 if (sections & HELP_FIRM) print_help_firm();
776 static void set_be_option(const char *arg)
778 int res = be_parse_arg(arg);
783 static void copy_file(FILE *dest, FILE *input)
787 while (!feof(input) && !ferror(dest)) {
788 size_t read = fread(buf, 1, sizeof(buf), input);
789 if (fwrite(buf, 1, read, dest) != read) {
790 perror("could not write output");
795 static FILE *open_file(const char *filename)
797 if (streq(filename, "-")) {
801 FILE *in = fopen(filename, "r");
803 fprintf(stderr, "Could not open '%s': %s\n", filename,
811 static filetype_t get_filetype_from_string(const char *string)
813 if (streq(string, "c") || streq(string, "c-header"))
815 if (streq(string, "c++") || streq(string, "c++-header"))
817 if (streq(string, "assembler"))
818 return FILETYPE_PREPROCESSED_ASSEMBLER;
819 if (streq(string, "assembler-with-cpp"))
820 return FILETYPE_ASSEMBLER;
821 if (streq(string, "none"))
822 return FILETYPE_AUTODETECT;
824 return FILETYPE_UNKNOWN;
827 static bool init_os_support(void)
829 wchar_atomic_kind = ATOMIC_TYPE_INT;
830 enable_main_collect2_hack = false;
831 define_intmax_types = false;
833 if (firm_is_unixish_os(target_machine)) {
834 set_create_ld_ident(create_name_linux_elf);
835 } else if (firm_is_darwin_os(target_machine)) {
836 set_create_ld_ident(create_name_macho);
837 define_intmax_types = true;
838 } else if (firm_is_windows_os(target_machine)) {
839 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
840 enable_main_collect2_hack = true;
841 set_create_ld_ident(create_name_win32);
849 static bool parse_target_triple(const char *arg)
851 machine_triple_t *triple = firm_parse_machine_triple(arg);
852 if (triple == NULL) {
853 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
856 target_machine = triple;
860 static unsigned decide_modulo_shift(unsigned type_size)
862 if (architecture_modulo_shift == 0)
864 if (type_size < architecture_modulo_shift)
865 return architecture_modulo_shift;
869 static bool is_ia32_cpu(const char *architecture)
871 return streq(architecture, "i386")
872 || streq(architecture, "i486")
873 || streq(architecture, "i586")
874 || streq(architecture, "i686")
875 || streq(architecture, "i786");
878 static const char *setup_isa_from_tripel(const machine_triple_t *machine)
880 const char *cpu = machine->cpu_type;
882 if (is_ia32_cpu(cpu)) {
884 } else if (streq(cpu, "x86_64")) {
886 } else if (streq(cpu, "sparc")) {
888 } else if (streq(cpu, "arm")) {
891 fprintf(stderr, "Unknown cpu '%s' in target-triple\n", cpu);
896 static const char *setup_target_machine(void)
898 if (!setup_firm_for_machine(target_machine))
901 const char *isa = setup_isa_from_tripel(target_machine);
912 * initialize cparser type properties based on a firm type
914 static void set_typeprops_type(atomic_type_properties_t* props, ir_type *type)
916 props->size = get_type_size_bytes(type);
917 props->alignment = get_type_alignment_bytes(type);
918 props->struct_alignment = props->alignment;
922 * Copy atomic type properties except the integer conversion rank
924 static void copy_typeprops(atomic_type_properties_t *dest,
925 const atomic_type_properties_t *src)
927 dest->size = src->size;
928 dest->alignment = src->alignment;
929 dest->struct_alignment = src->struct_alignment;
930 dest->flags = src->flags;
933 static void init_types_and_adjust(void)
935 const backend_params *be_params = be_get_backend_param();
936 unsigned machine_size = be_params->machine_size;
937 init_types(machine_size);
939 atomic_type_properties_t *props = atomic_type_properties;
941 /* adjust types as requested by target architecture */
942 ir_type *type_long_double = be_params->type_long_double;
943 if (type_long_double != NULL) {
944 set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_long_double);
945 atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = get_type_mode(type_long_double);
948 ir_type *type_long_long = be_params->type_long_long;
949 if (type_long_long != NULL)
950 set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_long_long);
952 ir_type *type_unsigned_long_long = be_params->type_unsigned_long_long;
953 if (type_unsigned_long_long != NULL)
954 set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_unsigned_long_long);
956 /* operating system ABI specifics */
957 if (firm_is_darwin_os(target_machine)) {
958 if (machine_size == 32) {
959 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
960 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
961 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
962 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
963 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
964 props[ATOMIC_TYPE_LONG_DOUBLE].struct_alignment = 16;
966 } else if (firm_is_windows_os(target_machine)) {
967 if (machine_size == 64) {
968 /* to ease porting of old c-code microsoft decided to use 32bits
970 props[ATOMIC_TYPE_LONG] = props[ATOMIC_TYPE_INT];
971 props[ATOMIC_TYPE_ULONG] = props[ATOMIC_TYPE_UINT];
974 /* on windows long double is not supported */
975 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
976 } else if (firm_is_unixish_os(target_machine)) {
977 if (is_ia32_cpu(target_machine->cpu_type)) {
978 /* System V has a broken alignment for double so we have to add
980 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
981 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
982 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
986 /* stuff decided after processing operating system specifics and
987 * commandline flags */
988 if (char_is_signed) {
989 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
991 props[ATOMIC_TYPE_CHAR].flags &= ~ATOMIC_TYPE_FLAG_SIGNED;
993 /* copy over wchar_t properties (including rank) */
994 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
996 /* initialize defaults for unsupported types */
997 if (type_long_long == NULL) {
998 copy_typeprops(&props[ATOMIC_TYPE_LONGLONG], &props[ATOMIC_TYPE_LONG]);
1000 if (type_unsigned_long_long == NULL) {
1001 copy_typeprops(&props[ATOMIC_TYPE_ULONGLONG],
1002 &props[ATOMIC_TYPE_ULONG]);
1004 if (type_long_double == NULL) {
1005 copy_typeprops(&props[ATOMIC_TYPE_LONG_DOUBLE],
1006 &props[ATOMIC_TYPE_DOUBLE]);
1009 /* initialize firm pointer modes */
1011 unsigned bit_size = machine_size;
1012 unsigned modulo_shift = decide_modulo_shift(bit_size);
1014 snprintf(name, sizeof(name), "p%u", machine_size);
1015 ir_mode *ptr_mode = new_reference_mode(name, irma_twos_complement, bit_size, modulo_shift);
1017 if (machine_size == 16) {
1018 set_reference_mode_signed_eq(ptr_mode, mode_Hs);
1019 set_reference_mode_unsigned_eq(ptr_mode, mode_Hu);
1020 } else if (machine_size == 32) {
1021 set_reference_mode_signed_eq(ptr_mode, mode_Is);
1022 set_reference_mode_unsigned_eq(ptr_mode, mode_Iu);
1023 } else if (machine_size == 64) {
1024 set_reference_mode_signed_eq(ptr_mode, mode_Ls);
1025 set_reference_mode_unsigned_eq(ptr_mode, mode_Lu);
1027 panic("strange machine_size when determining pointer modes");
1030 /* Hmm, pointers should be machine size */
1031 set_modeP_data(ptr_mode);
1032 set_modeP_code(ptr_mode);
1034 byte_order_big_endian = be_params->byte_order_big_endian;
1035 if (be_params->modulo_shift_efficient) {
1036 architecture_modulo_shift = machine_size;
1038 architecture_modulo_shift = 0;
1042 int main(int argc, char **argv)
1044 const char *dumpfunction = NULL;
1045 const char *print_file_name_file = NULL;
1046 compile_mode_t mode = CompileAssembleLink;
1048 int result = EXIT_SUCCESS;
1049 char cpu_arch[16] = "ia32";
1050 file_list_entry_t *files = NULL;
1051 file_list_entry_t *last_file = NULL;
1052 bool construct_dep_target = false;
1053 bool do_timing = false;
1054 bool profile_generate = false;
1055 bool profile_use = false;
1056 struct obstack file_obst;
1058 atexit(free_temp_files);
1060 /* hack for now... */
1061 if (strstr(argv[0], "pptest") != NULL) {
1062 extern int pptest_main(int argc, char **argv);
1063 return pptest_main(argc, argv);
1066 obstack_init(&cppflags_obst);
1067 obstack_init(&ldflags_obst);
1068 obstack_init(&asflags_obst);
1069 obstack_init(&file_obst);
1071 #define GET_ARG_AFTER(def, args) \
1073 def = &arg[sizeof(args)-1]; \
1074 if (def[0] == '\0') { \
1077 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1078 argument_errors = true; \
1082 if (def[0] == '-' && def[1] != '\0') { \
1083 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1084 argument_errors = true; \
1090 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
1092 /* initialize this early because it has to parse options */
1095 /* early options parsing (find out optimization level and OS) */
1096 for (int i = 1; i < argc; ++i) {
1097 const char *arg = argv[i];
1101 const char *option = &arg[1];
1102 if (option[0] == 'O') {
1103 sscanf(&option[1], "%d", &opt_level);
1107 if (target_machine == NULL) {
1108 target_machine = firm_get_host_machine();
1110 choose_optimization_pack(opt_level);
1111 setup_target_machine();
1113 /* parse rest of options */
1114 lang_standard_t standard = STANDARD_DEFAULT;
1115 unsigned features_on = 0;
1116 unsigned features_off = 0;
1117 filetype_t forced_filetype = FILETYPE_AUTODETECT;
1118 help_sections_t help = HELP_NONE;
1119 bool argument_errors = false;
1120 for (int i = 1; i < argc; ++i) {
1121 const char *arg = argv[i];
1122 if (arg[0] == '-' && arg[1] != '\0') {
1124 const char *option = &arg[1];
1125 if (option[0] == 'o') {
1126 GET_ARG_AFTER(outname, "-o");
1127 } else if (option[0] == 'g') {
1128 /* TODO: parse -gX with 0<=x<=3... */
1129 set_be_option("debug=frameinfo");
1130 set_be_option("ia32-nooptcc=yes");
1131 } else if (SINGLE_OPTION('c')) {
1132 mode = CompileAssemble;
1133 } else if (SINGLE_OPTION('E')) {
1134 mode = PreprocessOnly;
1135 } else if (SINGLE_OPTION('s')) {
1136 add_flag(&ldflags_obst, "-s");
1137 } else if (SINGLE_OPTION('S')) {
1139 } else if (option[0] == 'O') {
1141 } else if (option[0] == 'I') {
1143 GET_ARG_AFTER(opt, "-I");
1144 add_flag(&cppflags_obst, "-I%s", opt);
1145 } else if (option[0] == 'D') {
1147 GET_ARG_AFTER(opt, "-D");
1148 add_flag(&cppflags_obst, "-D%s", opt);
1149 } else if (option[0] == 'U') {
1151 GET_ARG_AFTER(opt, "-U");
1152 add_flag(&cppflags_obst, "-U%s", opt);
1153 } else if (option[0] == 'l') {
1155 GET_ARG_AFTER(opt, "-l");
1156 add_flag(&ldflags_obst, "-l%s", opt);
1157 } else if (option[0] == 'L') {
1159 GET_ARG_AFTER(opt, "-L");
1160 add_flag(&ldflags_obst, "-L%s", opt);
1161 } else if (SINGLE_OPTION('v')) {
1163 } else if (SINGLE_OPTION('w')) {
1164 add_flag(&cppflags_obst, "-w");
1165 disable_all_warnings();
1166 } else if (option[0] == 'x') {
1168 GET_ARG_AFTER(opt, "-x");
1169 forced_filetype = get_filetype_from_string(opt);
1170 if (forced_filetype == FILETYPE_UNKNOWN) {
1171 fprintf(stderr, "Unknown language '%s'\n", opt);
1172 argument_errors = true;
1174 } else if (streq(option, "M")) {
1175 mode = PreprocessOnly;
1176 add_flag(&cppflags_obst, "-M");
1177 } else if (streq(option, "MMD") ||
1178 streq(option, "MD")) {
1179 construct_dep_target = true;
1180 add_flag(&cppflags_obst, "-%s", option);
1181 } else if (streq(option, "MM") ||
1182 streq(option, "MP")) {
1183 add_flag(&cppflags_obst, "-%s", option);
1184 } else if (streq(option, "MT") ||
1185 streq(option, "MQ") ||
1186 streq(option, "MF")) {
1188 GET_ARG_AFTER(opt, "-MT");
1189 add_flag(&cppflags_obst, "-%s", option);
1190 add_flag(&cppflags_obst, "%s", opt);
1191 } else if (streq(option, "include")) {
1193 GET_ARG_AFTER(opt, "-include");
1194 add_flag(&cppflags_obst, "-include");
1195 add_flag(&cppflags_obst, "%s", opt);
1196 } else if (streq(option, "isystem")) {
1198 GET_ARG_AFTER(opt, "-isystem");
1199 add_flag(&cppflags_obst, "-isystem");
1200 add_flag(&cppflags_obst, "%s", opt);
1201 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1202 } else if (streq(option, "pthread")) {
1203 /* set flags for the preprocessor */
1204 add_flag(&cppflags_obst, "-D_REENTRANT");
1205 /* set flags for the linker */
1206 add_flag(&ldflags_obst, "-lpthread");
1208 } else if (streq(option, "nostdinc")
1209 || streq(option, "trigraphs")) {
1210 /* pass these through to the preprocessor */
1211 add_flag(&cppflags_obst, "%s", arg);
1212 } else if (streq(option, "pipe")) {
1213 /* here for gcc compatibility */
1214 } else if (streq(option, "static")) {
1215 add_flag(&ldflags_obst, "-static");
1216 } else if (streq(option, "shared")) {
1217 add_flag(&ldflags_obst, "-shared");
1218 } else if (option[0] == 'f') {
1219 char const *orig_opt;
1220 GET_ARG_AFTER(orig_opt, "-f");
1222 if (strstart(orig_opt, "input-charset=")) {
1223 char const* const encoding = strchr(orig_opt, '=') + 1;
1224 input_encoding = encoding;
1225 } else if (strstart(orig_opt, "align-loops=") ||
1226 strstart(orig_opt, "align-jumps=") ||
1227 strstart(orig_opt, "align-functions=")) {
1228 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1229 } else if (strstart(orig_opt, "visibility=")) {
1230 const char *val = strchr(orig_opt, '=')+1;
1231 elf_visibility_tag_t visibility
1232 = get_elf_visibility_from_string(val);
1233 if (visibility == ELF_VISIBILITY_ERROR) {
1234 fprintf(stderr, "invalid visibility '%s' specified\n",
1236 argument_errors = true;
1238 set_default_visibility(visibility);
1240 } else if (strstart(orig_opt, "message-length=")) {
1241 /* ignore: would only affect error message format */
1242 } else if (streq(orig_opt, "fast-math") ||
1243 streq(orig_opt, "fp-fast")) {
1244 firm_fp_model = fp_model_fast;
1245 } else if (streq(orig_opt, "fp-precise")) {
1246 firm_fp_model = fp_model_precise;
1247 } else if (streq(orig_opt, "fp-strict")) {
1248 firm_fp_model = fp_model_strict;
1249 } else if (streq(orig_opt, "help")) {
1250 fprintf(stderr, "warning: -fhelp is deprecated\n");
1251 help |= HELP_OPTIMIZATION;
1253 /* -f options which have an -fno- variant */
1254 char const *opt = orig_opt;
1255 bool truth_value = true;
1256 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1257 truth_value = false;
1261 if (streq(opt, "diagnostics-show-option")) {
1262 diagnostics_show_option = truth_value;
1263 } else if (streq(opt, "dollars-in-identifiers")) {
1264 allow_dollar_in_symbol = truth_value;
1265 } else if (streq(opt, "omit-frame-pointer")) {
1266 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1267 } else if (streq(opt, "short-wchar")) {
1268 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1270 } else if (streq(opt, "show-column")) {
1271 show_column = truth_value;
1272 } else if (streq(opt, "signed-char")) {
1273 char_is_signed = truth_value;
1274 } else if (streq(opt, "strength-reduce")) {
1275 /* does nothing, for gcc compatibility (even gcc does
1276 * nothing for this switch anymore) */
1277 } else if (streq(opt, "syntax-only")) {
1278 mode = truth_value ? ParseOnly : CompileAssembleLink;
1279 } else if (streq(opt, "unsigned-char")) {
1280 char_is_signed = !truth_value;
1281 } else if (streq(opt, "freestanding")) {
1282 freestanding = truth_value;
1283 } else if (streq(opt, "hosted")) {
1284 freestanding = !truth_value;
1285 } else if (streq(opt, "profile-generate")) {
1286 profile_generate = truth_value;
1287 } else if (streq(opt, "profile-use")) {
1288 profile_use = truth_value;
1289 } else if (!truth_value &&
1290 streq(opt, "asynchronous-unwind-tables")) {
1291 /* nothing todo, a gcc feature which we do not support
1292 * anyway was deactivated */
1293 } else if (streq(opt, "verbose-asm")) {
1294 /* ignore: we always print verbose assembler */
1295 } else if (streq(opt, "jump-tables") ||
1296 streq(opt, "expensive-optimizations") ||
1297 streq(opt, "common") ||
1298 streq(opt, "optimize-sibling-calls") ||
1299 streq(opt, "align-loops") ||
1300 streq(opt, "align-jumps") ||
1301 streq(opt, "align-functions") ||
1302 streq(opt, "unroll-loops") ||
1303 streq(opt, "PIC") ||
1304 streq(opt, "stack-protector") ||
1305 streq(opt, "stack-protector-all")) {
1306 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1308 int res = firm_option(orig_opt);
1310 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1312 argument_errors = true;
1317 } else if (option[0] == 'b') {
1319 GET_ARG_AFTER(opt, "-b");
1321 if (streq(opt, "help")) {
1322 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1325 int res = be_parse_arg(opt);
1327 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1329 argument_errors = true;
1330 } else if (strstart(opt, "isa=")) {
1331 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1334 } else if (option[0] == 'W') {
1335 if (strstart(option + 1, "p,")) {
1336 // pass options directly to the preprocessor
1338 GET_ARG_AFTER(opt, "-Wp,");
1339 add_flag(&cppflags_obst, "-Wp,%s", opt);
1340 } else if (strstart(option + 1, "l,")) {
1341 // pass options directly to the linker
1343 GET_ARG_AFTER(opt, "-Wl,");
1344 add_flag(&ldflags_obst, "-Wl,%s", opt);
1345 } else if (streq(option + 1, "no-trigraphs")
1346 || streq(option + 1, "undef")
1347 || streq(option + 1, "missing-include-dirs")
1348 || streq(option + 1, "endif-labels")) {
1349 add_flag(&cppflags_obst, "%s", arg);
1350 } else if (streq(option+1, "init-self")) {
1351 /* ignored (same as gcc does) */
1352 } else if (streq(option+1, "format-y2k")
1353 || streq(option+1, "format-security")
1354 || streq(option+1, "old-style-declaration")
1355 || streq(option+1, "type-limits")) {
1356 /* ignore (gcc compatibility) */
1358 set_warning_opt(&option[1]);
1360 } else if (option[0] == 'm') {
1365 GET_ARG_AFTER(opt, "-m");
1366 if (strstart(opt, "target=")) {
1367 GET_ARG_AFTER(opt, "-mtarget=");
1368 if (!parse_target_triple(opt)) {
1369 argument_errors = true;
1371 const char *isa = setup_target_machine();
1372 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1373 target_triple = opt;
1375 } else if (strstart(opt, "triple=")) {
1376 GET_ARG_AFTER(opt, "-mtriple=");
1377 if (!parse_target_triple(opt)) {
1378 argument_errors = true;
1380 const char *isa = setup_target_machine();
1381 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1382 target_triple = opt;
1384 } else if (strstart(opt, "arch=")) {
1385 GET_ARG_AFTER(opt, "-march=");
1386 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1387 int res = be_parse_arg(arch_opt);
1388 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1389 res &= be_parse_arg(arch_opt);
1392 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1393 argument_errors = true;
1395 } else if (strstart(opt, "tune=")) {
1396 GET_ARG_AFTER(opt, "-mtune=");
1397 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1398 int res = be_parse_arg(arch_opt);
1400 argument_errors = true;
1401 } else if (strstart(opt, "cpu=")) {
1402 GET_ARG_AFTER(opt, "-mcpu=");
1403 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1404 int res = be_parse_arg(arch_opt);
1406 argument_errors = true;
1407 } else if (strstart(opt, "fpmath=")) {
1408 GET_ARG_AFTER(opt, "-mfpmath=");
1409 if (streq(opt, "387"))
1411 else if (streq(opt, "sse"))
1414 fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
1415 argument_errors = true;
1417 if (!argument_errors) {
1418 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1419 int res = be_parse_arg(arch_opt);
1421 argument_errors = true;
1423 } else if (strstart(opt, "preferred-stack-boundary=")) {
1424 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1425 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1426 int res = be_parse_arg(arch_opt);
1428 argument_errors = true;
1429 } else if (streq(opt, "rtd")) {
1430 default_calling_convention = CC_STDCALL;
1431 } else if (strstart(opt, "regparm=")) {
1432 fprintf(stderr, "error: regparm convention not supported yet\n");
1433 argument_errors = true;
1434 } else if (streq(opt, "soft-float")) {
1435 add_flag(&ldflags_obst, "-msoft-float");
1436 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
1437 int res = be_parse_arg(arch_opt);
1439 argument_errors = true;
1440 } else if (streq(opt, "sse2")) {
1441 /* ignore for now, our x86 backend always uses sse when
1442 * sse is requested */
1444 long int value = strtol(opt, NULL, 10);
1446 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1447 argument_errors = true;
1448 } else if (value != 16 && value != 32 && value != 64) {
1449 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1450 argument_errors = true;
1452 unsigned machine_size = (unsigned)value;
1453 /* TODO: choose/change backend based on this */
1454 add_flag(&cppflags_obst, "-m%u", machine_size);
1455 add_flag(&asflags_obst, "-m%u", machine_size);
1456 add_flag(&ldflags_obst, "-m%u", machine_size);
1459 } else if (streq(option, "pg")) {
1460 set_be_option("gprof");
1461 add_flag(&ldflags_obst, "-pg");
1462 } else if (streq(option, "ansi")) {
1463 add_flag(&cppflags_obst, "-ansi");
1464 } else if (streq(option, "pedantic")) {
1465 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1466 } else if (strstart(option, "std=")) {
1467 const char *const o = &option[4];
1469 streq(o, "c++") ? STANDARD_CXX98 :
1470 streq(o, "c++98") ? STANDARD_CXX98 :
1471 streq(o, "c89") ? STANDARD_C89 :
1472 streq(o, "c90") ? STANDARD_C89 :
1473 streq(o, "c99") ? STANDARD_C99 :
1474 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1475 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1476 streq(o, "gnu89") ? STANDARD_GNU89 :
1477 streq(o, "gnu99") ? STANDARD_GNU99 :
1478 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1479 streq(o, "iso9899:1990") ? STANDARD_C89 :
1480 streq(o, "iso9899:199409") ? STANDARD_C89AMD1 :
1481 streq(o, "iso9899:1999") ? STANDARD_C99 :
1482 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1483 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1484 } else if (streq(option, "version")) {
1485 print_cparser_version();
1486 return EXIT_SUCCESS;
1487 } else if (streq(option, "dumpversion")) {
1488 /* gcc compatibility option */
1489 print_cparser_version_short();
1490 return EXIT_SUCCESS;
1491 } else if (strstart(option, "print-file-name=")) {
1492 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1493 } else if (option[0] == '-') {
1494 /* double dash option */
1496 if (streq(option, "gcc")) {
1497 features_on |= _GNUC;
1498 features_off &= ~_GNUC;
1499 } else if (streq(option, "no-gcc")) {
1500 features_on &= ~_GNUC;
1501 features_off |= _GNUC;
1502 } else if (streq(option, "ms")) {
1504 features_off &= ~_MS;
1505 } else if (streq(option, "no-ms")) {
1506 features_on &= ~_MS;
1507 features_off |= _MS;
1508 } else if (streq(option, "strict")) {
1510 } else if (streq(option, "lextest")) {
1512 } else if (streq(option, "benchmark")) {
1513 mode = BenchmarkParser;
1514 } else if (streq(option, "print-ast")) {
1516 } else if (streq(option, "print-implicit-cast")) {
1517 print_implicit_casts = true;
1518 } else if (streq(option, "print-parenthesis")) {
1519 print_parenthesis = true;
1520 } else if (streq(option, "print-fluffy")) {
1522 } else if (streq(option, "print-jna")) {
1524 } else if (streq(option, "jna-limit")) {
1527 fprintf(stderr, "error: "
1528 "expected argument after '--jna-limit'\n");
1529 argument_errors = true;
1532 jna_limit_output(argv[i]);
1533 } else if (streq(option, "jna-libname")) {
1536 fprintf(stderr, "error: "
1537 "expected argument after '--jna-libname'\n");
1538 argument_errors = true;
1541 jna_set_libname(argv[i]);
1542 } else if (streq(option, "time")) {
1544 } else if (streq(option, "version")) {
1545 print_cparser_version();
1546 return EXIT_SUCCESS;
1547 } else if (streq(option, "help")) {
1549 } else if (streq(option, "help-parser")) {
1550 help |= HELP_PARSER;
1551 } else if (streq(option, "help-warnings")) {
1552 help |= HELP_WARNINGS;
1553 } else if (streq(option, "help-codegen")) {
1554 help |= HELP_CODEGEN;
1555 } else if (streq(option, "help-linker")) {
1556 help |= HELP_LINKER;
1557 } else if (streq(option, "help-optimization")) {
1558 help |= HELP_OPTIMIZATION;
1559 } else if (streq(option, "help-language-tools")) {
1560 help |= HELP_LANGUAGETOOLS;
1561 } else if (streq(option, "help-debug")) {
1563 } else if (streq(option, "help-firm")) {
1565 } else if (streq(option, "help-all")) {
1567 } else if (streq(option, "dump-function")) {
1570 fprintf(stderr, "error: "
1571 "expected argument after '--dump-function'\n");
1572 argument_errors = true;
1575 dumpfunction = argv[i];
1577 } else if (streq(option, "export-ir")) {
1578 mode = CompileExportIR;
1579 } else if (streq(option, "unroll-loops")) {
1580 /* ignore (gcc compatibility) */
1582 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1583 argument_errors = true;
1586 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1587 argument_errors = true;
1590 filetype_t type = forced_filetype;
1591 if (type == FILETYPE_AUTODETECT) {
1592 if (streq(arg, "-")) {
1593 /* - implicitly means C source file */
1596 const char *suffix = strrchr(arg, '.');
1597 /* Ensure there is at least one char before the suffix */
1598 if (suffix != NULL && suffix != arg) {
1601 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1602 streq(suffix, "a") ? FILETYPE_OBJECT :
1603 streq(suffix, "c") ? FILETYPE_C :
1604 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1605 streq(suffix, "C") ? FILETYPE_CXX :
1606 streq(suffix, "cc") ? FILETYPE_CXX :
1607 streq(suffix, "cp") ? FILETYPE_CXX :
1608 streq(suffix, "cpp") ? FILETYPE_CXX :
1609 streq(suffix, "CPP") ? FILETYPE_CXX :
1610 streq(suffix, "cxx") ? FILETYPE_CXX :
1611 streq(suffix, "c++") ? FILETYPE_CXX :
1612 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1613 streq(suffix, "h") ? FILETYPE_C :
1614 streq(suffix, "ir") ? FILETYPE_IR :
1615 streq(suffix, "o") ? FILETYPE_OBJECT :
1616 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1617 streq(suffix, "so") ? FILETYPE_OBJECT :
1618 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1623 file_list_entry_t *entry
1624 = obstack_alloc(&file_obst, sizeof(entry[0]));
1625 memset(entry, 0, sizeof(entry[0]));
1629 if (last_file != NULL) {
1630 last_file->next = entry;
1638 if (help != HELP_NONE) {
1639 print_help(argv[0], help);
1640 return !argument_errors;
1643 if (print_file_name_file != NULL) {
1644 print_file_name(print_file_name_file);
1645 return EXIT_SUCCESS;
1647 if (files == NULL) {
1648 fprintf(stderr, "error: no input files specified\n");
1649 argument_errors = true;
1652 if (argument_errors) {
1654 return EXIT_FAILURE;
1657 /* apply some effects from switches */
1658 c_mode |= features_on;
1659 c_mode &= ~features_off;
1660 if (profile_generate) {
1661 add_flag(&ldflags_obst, "-lfirmprof");
1662 set_be_option("profilegenerate");
1665 set_be_option("profileuse");
1668 init_symbol_table();
1669 init_types_and_adjust();
1672 if (wchar_atomic_kind == ATOMIC_TYPE_INT)
1673 init_wchar_types(type_int);
1674 else if (wchar_atomic_kind == ATOMIC_TYPE_USHORT)
1675 init_wchar_types(type_short);
1677 panic("unexpected wchar type");
1678 init_preprocessor();
1687 if (construct_dep_target) {
1688 if (outname != 0 && strlen(outname) >= 2) {
1689 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1691 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1694 dep_target[0] = '\0';
1697 char outnamebuf[4096];
1698 if (outname == NULL) {
1699 const char *filename = files->name;
1702 case BenchmarkParser:
1707 case PreprocessOnly:
1712 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1713 outname = outnamebuf;
1715 case CompileAssemble:
1716 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1717 outname = outnamebuf;
1720 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1722 outname = outnamebuf;
1724 case CompileExportIR:
1725 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1726 outname = outnamebuf;
1728 case CompileAssembleLink:
1738 assert(outname != NULL);
1741 if (streq(outname, "-")) {
1744 out = fopen(outname, "w");
1746 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
1748 return EXIT_FAILURE;
1752 file_list_entry_t *file;
1753 bool already_constructed_firm = false;
1754 for (file = files; file != NULL; file = file->next) {
1755 char asm_tempfile[1024];
1756 const char *filename = file->name;
1757 filetype_t filetype = file->type;
1758 lang_standard_t file_standard = standard;
1760 if (filetype == FILETYPE_OBJECT)
1762 if (file_standard == STANDARD_DEFAULT) {
1765 case FILETYPE_PREPROCESSED_C:
1766 file_standard = STANDARD_GNU99;
1769 case FILETYPE_PREPROCESSED_CXX:
1770 file_standard = STANDARD_GNUXX98;
1778 if (mode == LexTest) {
1780 in = open_file(filename);
1781 lextest(in, filename);
1783 return EXIT_SUCCESS;
1786 FILE *preprocessed_in = NULL;
1787 filetype_t next_filetype = filetype;
1790 next_filetype = FILETYPE_PREPROCESSED_C;
1793 next_filetype = FILETYPE_PREPROCESSED_CXX;
1795 case FILETYPE_ASSEMBLER:
1796 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1799 /* no support for input on FILE* yet */
1801 panic("internal compiler error: in for preprocessor != NULL");
1803 preprocessed_in = preprocess(filename, filetype,
1805 if (mode == PreprocessOnly) {
1806 copy_file(out, preprocessed_in);
1807 int pp_result = pclose(preprocessed_in);
1809 /* remove output file in case of error */
1810 if (out != stdout && pp_result != EXIT_SUCCESS) {
1816 in = preprocessed_in;
1817 filetype = next_filetype;
1825 if (mode == Compile) {
1828 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1832 in = open_file(filename);
1834 /* preprocess and compile */
1835 if (filetype == FILETYPE_PREPROCESSED_C) {
1836 switch (file_standard) {
1837 case STANDARD_C89: c_mode = _C89; break;
1838 /* TODO determine difference between these two */
1839 case STANDARD_C89AMD1: c_mode = _C89; break;
1840 case STANDARD_C99: c_mode = _C89 | _C99; break;
1841 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1843 case STANDARD_CXX98:
1844 case STANDARD_GNUXX98:
1845 case STANDARD_DEFAULT:
1846 fprintf(stderr, "warning: command line option \"-std=%s\" is not valid for C\n", str_lang_standard(file_standard));
1848 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1851 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1852 switch (file_standard) {
1853 case STANDARD_CXX98: c_mode = _CXX; break;
1856 case STANDARD_C89AMD1:
1858 case STANDARD_GNU89:
1859 case STANDARD_GNU99:
1860 case STANDARD_DEFAULT:
1861 fprintf(stderr, "warning: command line option \"-std=%s\" is not valid for C++\n", str_lang_standard(file_standard));
1863 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1867 c_mode |= features_on;
1868 c_mode &= ~features_off;
1870 /* do the actual parsing */
1871 ir_timer_t *t_parsing = ir_timer_new();
1872 timer_register(t_parsing, "Frontend: Parsing");
1873 timer_push(t_parsing);
1875 translation_unit_t *const unit = do_parsing(in, filename);
1876 timer_pop(t_parsing);
1878 /* prints the AST even if errors occurred */
1879 if (mode == PrintAst) {
1884 if (error_count > 0) {
1885 /* parsing failed because of errors */
1886 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1888 result = EXIT_FAILURE;
1890 } else if (warning_count > 0) {
1891 fprintf(stderr, "%u warning(s)\n", warning_count);
1894 if (in == preprocessed_in) {
1895 int pp_result = pclose(preprocessed_in);
1896 if (pp_result != EXIT_SUCCESS) {
1897 /* remove output file */
1900 return EXIT_FAILURE;
1904 if (mode == BenchmarkParser) {
1906 } else if (mode == PrintFluffy) {
1907 write_fluffy_decls(out, unit);
1909 } else if (mode == PrintJna) {
1910 write_jna_decls(out, unit);
1914 /* build the firm graph */
1915 ir_timer_t *t_construct = ir_timer_new();
1916 timer_register(t_construct, "Frontend: Graph construction");
1917 timer_push(t_construct);
1918 if (already_constructed_firm) {
1919 panic("compiling multiple files/translation units not possible");
1921 init_implicit_optimizations();
1922 translation_unit_to_firm(unit);
1923 already_constructed_firm = true;
1924 timer_pop(t_construct);
1927 if (mode == ParseOnly) {
1931 if (mode == CompileDump) {
1933 ident *id = new_id_from_str(dumpfunction);
1934 ir_graph *irg = NULL;
1935 int n_irgs = get_irp_n_irgs();
1936 for (int i = 0; i < n_irgs; ++i) {
1937 ir_graph *tirg = get_irp_irg(i);
1938 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1946 fprintf(stderr, "No graph for function '%s' found\n",
1948 return EXIT_FAILURE;
1951 dump_ir_graph_file(out, irg);
1953 return EXIT_SUCCESS;
1956 if (mode == CompileExportIR) {
1957 ir_export_file(out);
1958 if (ferror(out) != 0) {
1959 fprintf(stderr, "Error while writing to output\n");
1960 return EXIT_FAILURE;
1962 return EXIT_SUCCESS;
1965 generate_code(asm_out, filename);
1966 if (asm_out != out) {
1969 } else if (filetype == FILETYPE_IR) {
1971 int res = ir_import(filename);
1973 fprintf(stderr, "Firm-Program import failed\n");
1974 return EXIT_FAILURE;
1977 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1978 copy_file(asm_out, in);
1979 if (in == preprocessed_in) {
1980 int pp_result = pclose(preprocessed_in);
1981 if (pp_result != EXIT_SUCCESS) {
1982 /* remove output in error case */
1988 if (asm_out != out) {
1993 if (mode == Compile)
1996 /* if we're here then we have preprocessed assembly */
1997 filename = asm_tempfile;
1998 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
2001 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
2003 const char *filename_o;
2004 if (mode == CompileAssemble) {
2006 filename_o = outname;
2008 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
2013 assemble(filename_o, filename);
2015 size_t len = strlen(filename_o) + 1;
2016 filename = obstack_copy(&file_obst, filename_o, len);
2017 filetype = FILETYPE_OBJECT;
2020 /* ok we're done here, process next file */
2021 file->name = filename;
2022 file->type = filetype;
2025 if (result != EXIT_SUCCESS) {
2031 /* link program file */
2032 if (mode == CompileAssembleLink) {
2033 obstack_1grow(&ldflags_obst, '\0');
2034 const char *flags = obstack_finish(&ldflags_obst);
2036 /* construct commandline */
2037 const char *linker = getenv("CPARSER_LINK");
2038 if (linker != NULL) {
2039 obstack_printf(&file_obst, "%s ", linker);
2041 if (target_triple != NULL)
2042 obstack_printf(&file_obst, "%s-", target_triple);
2043 obstack_printf(&file_obst, "%s ", LINKER);
2046 for (file_list_entry_t *entry = files; entry != NULL;
2047 entry = entry->next) {
2048 if (entry->type != FILETYPE_OBJECT)
2051 add_flag(&file_obst, "%s", entry->name);
2054 add_flag(&file_obst, "-o");
2055 add_flag(&file_obst, outname);
2056 obstack_printf(&file_obst, "%s", flags);
2057 obstack_1grow(&file_obst, '\0');
2059 char *commandline = obstack_finish(&file_obst);
2064 int err = system(commandline);
2065 if (err != EXIT_SUCCESS) {
2066 fprintf(stderr, "linker reported an error\n");
2067 return EXIT_FAILURE;
2074 obstack_free(&cppflags_obst, NULL);
2075 obstack_free(&ldflags_obst, NULL);
2076 obstack_free(&asflags_obst, NULL);
2077 obstack_free(&file_obst, NULL);
2084 exit_preprocessor();
2088 exit_symbol_table();
2089 return EXIT_SUCCESS;