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;
121 typedef enum lang_standard_t {
122 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
123 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
124 STANDARD_C89, /* ISO C90 (sic) */
125 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
126 STANDARD_C99, /* ISO C99 */
127 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
128 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
129 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
130 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
133 static lang_standard_t standard;
135 typedef struct file_list_entry_t file_list_entry_t;
137 typedef enum filetype_t {
140 FILETYPE_PREPROCESSED_C,
142 FILETYPE_PREPROCESSED_CXX,
144 FILETYPE_PREPROCESSED_ASSEMBLER,
150 struct file_list_entry_t {
151 const char *name; /**< filename or NULL for stdin */
153 file_list_entry_t *next;
156 static file_list_entry_t *temp_files;
158 static void get_output_name(char *buf, size_t buflen, const char *inputname,
161 if (inputname == NULL)
164 char const *const last_slash = strrchr(inputname, '/');
165 char const *const filename =
166 last_slash != NULL ? last_slash + 1 : inputname;
167 char const *const last_dot = strrchr(filename, '.');
168 char const *const name_end =
169 last_dot != NULL ? last_dot : strchr(filename, '\0');
171 int const len = snprintf(buf, buflen, "%.*s%s",
172 (int)(name_end - filename), filename, newext);
174 if (len < 0 || buflen <= (size_t)len)
176 if (buflen <= (size_t)len)
178 panic("filename too long");
181 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
185 lexer_open_stream(in, input_name);
188 translation_unit_t *unit = finish_parsing();
192 static void lextest(FILE *in, const char *fname)
194 lexer_open_stream(in, fname);
197 lexer_next_preprocessing_token();
198 print_token(stdout, &lexer_token);
200 } while (lexer_token.kind != T_EOF);
203 static void add_flag(struct obstack *obst, const char *format, ...)
208 va_start(ap, format);
212 vsnprintf(buf, sizeof(buf), format, ap);
215 obstack_1grow(obst, ' ');
217 obstack_1grow(obst, '"');
218 obstack_grow(obst, buf, len);
219 obstack_1grow(obst, '"');
221 /* escape stuff... */
222 for (char *c = buf; *c != '\0'; ++c) {
240 obstack_1grow(obst, '\\');
243 obstack_1grow(obst, *c);
250 static const char *type_to_string(type_t *type)
252 assert(type->kind == TYPE_ATOMIC);
253 return get_atomic_kind_name(type->atomic.akind);
256 static FILE *preprocess(const char *fname, filetype_t filetype)
258 static const char *common_flags = NULL;
260 if (common_flags == NULL) {
261 obstack_1grow(&cppflags_obst, '\0');
262 const char *flags = obstack_finish(&cppflags_obst);
264 /* setup default defines */
265 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
266 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
267 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
268 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
270 add_flag(&cppflags_obst, "-U__VERSION__");
271 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
273 if (define_intmax_types) {
274 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
275 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
276 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
277 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
280 if (flags[0] != '\0') {
281 size_t len = strlen(flags);
282 obstack_1grow(&cppflags_obst, ' ');
283 obstack_grow(&cppflags_obst, flags, len);
285 obstack_1grow(&cppflags_obst, '\0');
286 common_flags = obstack_finish(&cppflags_obst);
289 assert(obstack_object_size(&cppflags_obst) == 0);
291 const char *preprocessor = getenv("CPARSER_PP");
292 if (preprocessor != NULL) {
293 obstack_printf(&cppflags_obst, "%s ", preprocessor);
295 if (target_triple != NULL)
296 obstack_printf(&cppflags_obst, "%s-", target_triple);
297 obstack_printf(&cppflags_obst, PREPROCESSOR);
302 add_flag(&cppflags_obst, "-std=c99");
305 add_flag(&cppflags_obst, "-std=c++98");
307 case FILETYPE_ASSEMBLER:
308 add_flag(&cppflags_obst, "-x");
309 add_flag(&cppflags_obst, "assembler-with-cpp");
314 obstack_printf(&cppflags_obst, "%s", common_flags);
316 /* handle dependency generation */
317 if (dep_target[0] != '\0') {
318 add_flag(&cppflags_obst, "-MF");
319 add_flag(&cppflags_obst, dep_target);
320 if (outname != NULL) {
321 add_flag(&cppflags_obst, "-MQ");
322 add_flag(&cppflags_obst, outname);
325 add_flag(&cppflags_obst, fname);
326 obstack_1grow(&cppflags_obst, '\0');
328 char *commandline = obstack_finish(&cppflags_obst);
332 FILE *f = popen(commandline, "r");
334 fprintf(stderr, "invoking preprocessor failed\n");
337 /* we do not really need that anymore */
338 obstack_free(&cppflags_obst, commandline);
343 static void assemble(const char *out, const char *in)
345 obstack_1grow(&asflags_obst, '\0');
346 const char *flags = obstack_finish(&asflags_obst);
348 const char *assembler = getenv("CPARSER_AS");
349 if (assembler != NULL) {
350 obstack_printf(&asflags_obst, "%s", assembler);
352 if (target_triple != NULL)
353 obstack_printf(&asflags_obst, "%s-", target_triple);
354 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
356 if (flags[0] != '\0')
357 obstack_printf(&asflags_obst, " %s", flags);
359 obstack_printf(&asflags_obst, " %s -o %s", in, out);
360 obstack_1grow(&asflags_obst, '\0');
362 char *commandline = obstack_finish(&asflags_obst);
366 int err = system(commandline);
367 if (err != EXIT_SUCCESS) {
368 fprintf(stderr, "assembler reported an error\n");
371 obstack_free(&asflags_obst, commandline);
374 static void print_file_name(const char *file)
376 add_flag(&ldflags_obst, "-print-file-name=%s", file);
378 obstack_1grow(&ldflags_obst, '\0');
379 const char *flags = obstack_finish(&ldflags_obst);
381 /* construct commandline */
382 const char *linker = getenv("CPARSER_LINK");
383 if (linker != NULL) {
384 obstack_printf(&ldflags_obst, "%s ", linker);
386 if (target_triple != NULL)
387 obstack_printf(&ldflags_obst, "%s-", target_triple);
388 obstack_printf(&ldflags_obst, "%s ", LINKER);
390 obstack_printf(&ldflags_obst, "%s ", linker);
391 obstack_printf(&ldflags_obst, "%s", flags);
392 obstack_1grow(&ldflags_obst, '\0');
394 char *commandline = obstack_finish(&ldflags_obst);
398 int err = system(commandline);
399 if (err != EXIT_SUCCESS) {
400 fprintf(stderr, "linker reported an error\n");
403 obstack_free(&ldflags_obst, commandline);
406 static const char *try_dir(const char *dir)
410 if (access(dir, R_OK | W_OK | X_OK) == 0)
415 static const char *get_tempdir(void)
417 static const char *tmpdir = NULL;
423 tmpdir = try_dir(getenv("TMPDIR"));
425 tmpdir = try_dir(getenv("TMP"));
427 tmpdir = try_dir(getenv("TEMP"));
431 tmpdir = try_dir(P_tmpdir);
435 tmpdir = try_dir("/var/tmp");
437 tmpdir = try_dir("/usr/tmp");
439 tmpdir = try_dir("/tmp");
448 /* cheap and nasty mkstemp replacement */
449 static int mkstemp(char *templ)
452 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
457 * an own version of tmpnam, which: writes in a buffer, emits no warnings
458 * during linking (like glibc/gnu ld do for tmpnam)...
460 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
462 const char *tempdir = get_tempdir();
464 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
466 int fd = mkstemp(buffer);
468 fprintf(stderr, "could not create temporary file: %s\n",
472 FILE *out = fdopen(fd, "w");
474 fprintf(stderr, "could not create temporary file FILE*\n");
478 file_list_entry_t *entry = xmalloc(sizeof(*entry));
479 memset(entry, 0, sizeof(*entry));
481 size_t name_len = strlen(buffer) + 1;
482 char *name = malloc(name_len);
483 memcpy(name, buffer, name_len);
486 entry->next = temp_files;
492 static void free_temp_files(void)
494 file_list_entry_t *entry = temp_files;
495 file_list_entry_t *next;
496 for ( ; entry != NULL; entry = next) {
500 free((char*) entry->name);
506 typedef enum compile_mode_t {
521 static void usage(const char *argv0)
523 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
526 static void print_cparser_version(void)
528 printf("cparser (%s) using libFirm (%u.%u",
529 cparser_REVISION, ir_get_version_major(),
530 ir_get_version_minor());
532 const char *revision = ir_get_version_revision();
533 if (revision[0] != 0) {
535 fputs(revision, stdout);
538 const char *build = ir_get_version_build();
541 fputs(build, stdout);
544 puts("This is free software; see the source for copying conditions. There is NO\n"
545 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
548 static void print_help_basic(const char *argv0)
552 put_help("--help", "Display this information");
553 put_help("--version", "Display compiler version");
554 put_help("--help-parser", "Display information about parser options");
555 put_help("--help-warnings", "Display information about warning options");
556 put_help("--help-codegen", "Display information about code-generation options");
557 put_help("--help-optimization", "Display information about optimization options");
558 put_help("--help-linker", "Display information about linker options");
559 put_help("--help-language-tools", "Display information about language tools options");
560 put_help("--help-debug", "Display information about compiler debugging options");
561 put_help("--help-firm", "Display information about direct firm options");
562 put_help("--help-all", "Display information about all options");
563 put_help("-c", "Compile and assemble but do not link");
564 put_help("-E", "Preprocess only");
565 put_help("-S", "Compile but do not assembler or link");
566 put_help("-o", "Specify output file");
567 put_help("-v", "Verbose output (show invocation of sub-processes)");
568 put_help("-x", "Force input language:");
569 put_choice("c", "C");
570 put_choice("c++", "C++");
571 put_choice("assembler", "Assembler (no preprocessing)");
572 put_choice("assembler-with-cpp", "Assembler with preprocessing");
573 put_choice("none", "Autodetection");
574 put_help("-pipe", "Ignored (gcc compatibility)");
577 static void print_help_preprocessor(void)
579 put_help("-nostdinc", "Do not search standard system include directories");
580 put_help("-trigraphs", "Support ISO C trigraphs");
581 put_help("-isystem", "");
582 put_help("-include", "");
583 put_help("-I PATH", "");
584 put_help("-D SYMBOL[=value]", "");
585 put_help("-U SYMBOL", "");
586 put_help("-Wp,OPTION", "Pass option directly to preprocessor");
589 put_help("-MMD", "");
597 static void print_help_parser(void)
599 put_help("-finput-charset=CHARSET", "Select encoding of input files");
600 put_help("-fmessage-length=LEN", "Ignored (gcc compatibility)");
601 put_help("-fshort-wchar", "Type \"wchar_t\" is unsigned short instead of int");
602 put_help("-fshow-column", "Show the column number in diagnostic messages");
603 put_help("-fsigned-char", "Type \"char\" is a signed type");
604 put_help("-funsigned-char", "Type \"char\" is an unsigned type");
605 put_help("--ms", "Enable msvc extensions");
606 put_help("--no-ms", "Disable msvc extensions");
607 put_help("--gcc", "Enable gcc extensions");
608 put_help("--no-gcc", "Disable gcc extensions");
609 put_help("-std=STANDARD", "Specify language standard:");
610 put_choice("c99", "ISO C99 standard");
611 put_choice("c89", "ISO C89 standard");
612 put_choice("c9x", "Deprecated");
613 put_choice("c++", "ISO C++ 98");
614 put_choice("c++98", "ISO C++ 98");
615 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
616 put_choice("gnu89", "ISO C89 + GNU extensions");
617 put_choice("gnu9x", "Deprecated");
618 put_choice("iso9899:1990", "ISO C89");
619 put_choice("iso9899:199409", "ISO C90");
620 put_choice("iso9899:1999", "ISO C99");
621 put_choice("iso9899:199x", "Deprecated");
622 put_help("-pedantic", "Ignored (gcc compatibility)");
623 put_help("-ansi", "Ignored (gcc compatibility)");
624 put_help("--strict", "Enable strict conformance checking");
627 static void print_help_warnings(void)
629 put_help("-f[no-]diagnostics-show-option", "Show the switch, which controls a warning, after each warning");
630 put_help("-w", "Disable all warnings");
631 put_help("-Wno-trigraphs", "Warn if input contains trigraphs");
632 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
633 put_help("-Winit-self", "Ignored (gcc compatibility)");
634 print_warning_opt_help();
637 static void print_help_optimization(void)
639 put_help("-O LEVEL", "Select optimization level (0-4)");
640 firm_option_help(put_help);
641 put_help("-fexpensive-optimizations","Ignored (gcc compatibility)");
644 static void print_help_codegeneration(void)
646 put_help("-g", "Generate debug information");
647 put_help("-pg", "Instrument code for gnu gprof");
648 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
649 put_help("-ffreestanding", "Compile in freestanding mode (see ISO C standard)");
650 put_help("-fhosted", "Compile in hosted (not freestanding) mode");
651 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
652 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
653 put_help("-ffp-precise", "Precise floating point model");
654 put_help("-ffp-fast", "Imprecise floating point model");
655 put_help("-ffp-strict", "Strict floating point model");
656 put_help("-pthread", "Use pthread threading library");
657 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
658 put_help("-mtriple=TARGET", "Alias for -mtarget (clang compatibility)");
659 put_help("-march=ARCH", "");
660 put_help("-mtune=ARCH", "");
661 put_help("-mcpu=CPU", "");
662 put_help("-mfpmath=", "");
663 put_help("-mpreferred-stack-boundary=", "");
664 put_help("-mrtd", "");
665 put_help("-mregparm=", "Not supported yet");
666 put_help("-msoft-float", "Not supported yet");
667 put_help("-m32", "Generate 32bit code");
668 put_help("-m64", "Generate 64bit code");
669 put_help("-fverbose-asm", "Ignored (gcc compatibility)");
670 put_help("-fjump-tables", "Ignored (gcc compatibility)");
671 put_help("-fcommon", "Ignored (gcc compatibility)");
672 put_help("-foptimize-sibling-calls", "Ignored (gcc compatibility)");
673 put_help("-falign-loops", "Ignored (gcc compatibility)");
674 put_help("-falign-jumps", "Ignored (gcc compatibility)");
675 put_help("-falign-functions", "Ignored (gcc compatibility)");
676 put_help("-fPIC", "Ignored (gcc compatibility)");
677 put_help("-ffast-math", "Same as -ffp-fast (gcc compatibility)");
679 puts("\tMost of these options can be used with a no- prefix to disable them");
680 puts("\te.g. -fno-omit-frame-pointer");
683 static void print_help_linker(void)
685 put_help("-l LIBRARY", "");
686 put_help("-L PATH", "");
687 put_help("-shared", "Produce a shared library");
688 put_help("-static", "Produce statically linked binary");
689 put_help("-Wl,OPTION", "Pass option directly to linker");
692 static void print_help_debug(void)
694 put_help("--lextest", "Preprocess and tokenize only");
695 put_help("--print-ast", "Preprocess, parse and print AST");
696 put_help("--print-implicit-cast", "");
697 put_help("--print-parenthesis", "");
698 put_help("--benchmark", "Preprocess and parse, produces no output");
699 put_help("--time", "Measure time of compiler passes");
700 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
701 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
704 static void print_help_language_tools(void)
706 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
707 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
708 put_help("--jna-limit filename", "");
709 put_help("--jna-libname name", "");
712 static void print_help_firm(void)
714 put_help("-bOPTION", "Directly pass option to libFirm backend");
715 int res = be_parse_arg("help");
722 HELP_BASIC = 1u << 0,
723 HELP_PREPROCESSOR = 1u << 1,
724 HELP_PARSER = 1u << 2,
725 HELP_WARNINGS = 1u << 3,
726 HELP_OPTIMIZATION = 1u << 4,
727 HELP_CODEGEN = 1u << 5,
728 HELP_LINKER = 1u << 6,
729 HELP_LANGUAGETOOLS = 1u << 7,
730 HELP_DEBUG = 1u << 8,
733 HELP_ALL = (unsigned)-1
736 static void print_help(const char *argv0, help_sections_t sections)
738 if (sections & HELP_BASIC) print_help_basic(argv0);
739 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
740 if (sections & HELP_PARSER) print_help_parser();
741 if (sections & HELP_WARNINGS) print_help_warnings();
742 if (sections & HELP_OPTIMIZATION) print_help_optimization();
743 if (sections & HELP_CODEGEN) print_help_codegeneration();
744 if (sections & HELP_LINKER) print_help_linker();
745 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
746 if (sections & HELP_DEBUG) print_help_debug();
747 if (sections & HELP_FIRM) print_help_firm();
750 static void set_be_option(const char *arg)
752 int res = be_parse_arg(arg);
757 static void copy_file(FILE *dest, FILE *input)
761 while (!feof(input) && !ferror(dest)) {
762 size_t read = fread(buf, 1, sizeof(buf), input);
763 if (fwrite(buf, 1, read, dest) != read) {
764 perror("could not write output");
769 static FILE *open_file(const char *filename)
771 if (streq(filename, "-")) {
775 FILE *in = fopen(filename, "r");
777 fprintf(stderr, "Could not open '%s': %s\n", filename,
785 static filetype_t get_filetype_from_string(const char *string)
787 if (streq(string, "c") || streq(string, "c-header"))
789 if (streq(string, "c++") || streq(string, "c++-header"))
791 if (streq(string, "assembler"))
792 return FILETYPE_PREPROCESSED_ASSEMBLER;
793 if (streq(string, "assembler-with-cpp"))
794 return FILETYPE_ASSEMBLER;
795 if (streq(string, "none"))
796 return FILETYPE_AUTODETECT;
798 return FILETYPE_UNKNOWN;
801 static bool is_windows_os(const char *os)
803 return strstr(os, "mingw") != NULL || streq(os, "win32");
806 static bool is_unixish_os(const char *os)
808 return strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
809 || streq(os, "solaris");
812 static bool is_darwin_os(const char *os)
814 return streq(os, "darwin");
817 static bool init_os_support(void)
819 const char *os = target_machine->operating_system;
820 wchar_atomic_kind = ATOMIC_TYPE_INT;
821 enable_main_collect2_hack = false;
822 define_intmax_types = false;
824 if (is_unixish_os(os)) {
825 set_create_ld_ident(create_name_linux_elf);
826 } else if (is_darwin_os(os)) {
827 set_create_ld_ident(create_name_macho);
828 define_intmax_types = true;
829 } else if (is_windows_os(os)) {
830 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
831 enable_main_collect2_hack = true;
832 set_create_ld_ident(create_name_win32);
840 static bool parse_target_triple(const char *arg)
842 machine_triple_t *triple = firm_parse_machine_triple(arg);
843 if (triple == NULL) {
844 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
847 target_machine = triple;
851 static unsigned decide_modulo_shift(unsigned type_size)
853 if (architecture_modulo_shift == 0)
855 if (type_size < architecture_modulo_shift)
856 return architecture_modulo_shift;
860 static void setup_target_machine(void)
862 if (!setup_firm_for_machine(target_machine))
869 * initialize cparser type properties based on a firm type
871 static void set_typeprops_type(atomic_type_properties_t* props, ir_type *type)
873 props->size = get_type_size_bytes(type);
874 props->alignment = get_type_alignment_bytes(type);
875 props->struct_alignment = props->alignment;
878 static bool is_ia32_cpu(const char *architecture)
880 return streq(architecture, "i386")
881 || streq(architecture, "i486")
882 || streq(architecture, "i586")
883 || streq(architecture, "i686")
884 || streq(architecture, "i786");
887 static void init_types_and_adjust(void)
889 const backend_params *be_params = be_get_backend_param();
890 unsigned machine_size = be_params->machine_size;
891 init_types(machine_size);
893 atomic_type_properties_t *props = atomic_type_properties;
895 /* adjust types as requested by target architecture */
896 ir_type *type_long_double = be_params->type_long_double;
897 if (type_long_double != NULL)
898 set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_long_double);
900 ir_type *type_long_long = be_params->type_long_long;
901 if (type_long_long != NULL)
902 set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_long_long);
904 ir_type *type_unsigned_long_long = be_params->type_unsigned_long_long;
905 if (type_long_long != NULL)
906 set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_unsigned_long_long);
908 /* operating system ABI specifics */
909 const char *os = target_machine->operating_system;
910 if (is_darwin_os(os)) {
911 if (machine_size == 32) {
912 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
913 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
914 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
916 } else if (is_windows_os(os)) {
917 if (machine_size == 64) {
918 /* to ease porting of old c-code microsoft decided to use 32bits
920 props[ATOMIC_TYPE_LONG] = props[ATOMIC_TYPE_INT];
921 props[ATOMIC_TYPE_ULONG] = props[ATOMIC_TYPE_UINT];
924 /* on windows long double is not supported */
925 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
926 } else if (is_unixish_os(os)) {
927 if (is_ia32_cpu(target_machine->cpu_type)) {
928 /* System V has a broken alignment for double so we have to add
930 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
931 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
932 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
936 /* stuff decided after processing operating system specifics and
937 * commandline flags */
938 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
939 if (char_is_signed) {
940 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
942 props[ATOMIC_TYPE_CHAR].flags &= ~ATOMIC_TYPE_FLAG_SIGNED;
945 /* initialize defaults for unsupported types */
946 if (type_long_long == NULL) {
947 props[ATOMIC_TYPE_LONGLONG] = props[ATOMIC_TYPE_LONG];
949 if (type_long_double == NULL) {
950 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
952 if (type_unsigned_long_long == NULL) {
953 props[ATOMIC_TYPE_ULONGLONG] = props[ATOMIC_TYPE_ULONG];
956 /* initialize firm pointer modes */
958 ir_mode_sort sort = irms_reference;
959 unsigned bit_size = machine_size;
961 ir_mode_arithmetic arithmetic = irma_twos_complement;
962 unsigned modulo_shift = decide_modulo_shift(bit_size);
964 snprintf(name, sizeof(name), "p%u", machine_size);
965 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
968 if (machine_size == 16) {
969 set_reference_mode_signed_eq(ptr_mode, mode_Hs);
970 set_reference_mode_unsigned_eq(ptr_mode, mode_Hu);
971 } else if (machine_size == 32) {
972 set_reference_mode_signed_eq(ptr_mode, mode_Is);
973 set_reference_mode_unsigned_eq(ptr_mode, mode_Iu);
974 } else if (machine_size == 64) {
975 set_reference_mode_signed_eq(ptr_mode, mode_Ls);
976 set_reference_mode_unsigned_eq(ptr_mode, mode_Lu);
978 panic("strange machine_size when determining pointer modes");
981 /* Hmm, pointers should be machine size */
982 set_modeP_data(ptr_mode);
983 set_modeP_code(ptr_mode);
985 byte_order_big_endian = be_params->byte_order_big_endian;
986 if (be_params->modulo_shift_efficient) {
987 architecture_modulo_shift = machine_size;
989 architecture_modulo_shift = 0;
993 int main(int argc, char **argv)
997 const char *dumpfunction = NULL;
998 const char *print_file_name_file = NULL;
999 compile_mode_t mode = CompileAssembleLink;
1001 int result = EXIT_SUCCESS;
1002 char cpu_arch[16] = "ia32";
1003 file_list_entry_t *files = NULL;
1004 file_list_entry_t *last_file = NULL;
1005 bool construct_dep_target = false;
1006 bool do_timing = false;
1007 bool profile_generate = false;
1008 bool profile_use = false;
1009 struct obstack file_obst;
1011 atexit(free_temp_files);
1013 /* hack for now... */
1014 if (strstr(argv[0], "pptest") != NULL) {
1015 extern int pptest_main(int argc, char **argv);
1016 return pptest_main(argc, argv);
1019 obstack_init(&cppflags_obst);
1020 obstack_init(&ldflags_obst);
1021 obstack_init(&asflags_obst);
1022 obstack_init(&file_obst);
1024 #define GET_ARG_AFTER(def, args) \
1026 def = &arg[sizeof(args)-1]; \
1027 if (def[0] == '\0') { \
1030 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1031 argument_errors = true; \
1035 if (def[0] == '-' && def[1] != '\0') { \
1036 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1037 argument_errors = true; \
1043 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
1045 /* early options parsing (find out optimization level and OS) */
1046 for (int i = 1; i < argc; ++i) {
1047 const char *arg = argv[i];
1051 const char *option = &arg[1];
1052 if (option[0] == 'O') {
1053 sscanf(&option[1], "%d", &opt_level);
1057 const char *target = getenv("TARGET");
1059 parse_target_triple(target);
1060 if (target_machine == NULL) {
1061 target_machine = firm_get_host_machine();
1063 choose_optimization_pack(opt_level);
1064 setup_target_machine();
1066 /* parse rest of options */
1067 standard = STANDARD_DEFAULT;
1068 unsigned features_on = 0;
1069 unsigned features_off = 0;
1070 filetype_t forced_filetype = FILETYPE_AUTODETECT;
1071 help_sections_t help = HELP_NONE;
1072 bool argument_errors = false;
1073 for (int i = 1; i < argc; ++i) {
1074 const char *arg = argv[i];
1075 if (arg[0] == '-' && arg[1] != '\0') {
1077 const char *option = &arg[1];
1078 if (option[0] == 'o') {
1079 GET_ARG_AFTER(outname, "-o");
1080 } else if (option[0] == 'g') {
1081 set_be_option("debuginfo=stabs");
1082 set_be_option("omitfp=no");
1083 set_be_option("ia32-nooptcc=yes");
1084 } else if (SINGLE_OPTION('c')) {
1085 mode = CompileAssemble;
1086 } else if (SINGLE_OPTION('E')) {
1087 mode = PreprocessOnly;
1088 } else if (SINGLE_OPTION('S')) {
1090 } else if (option[0] == 'O') {
1092 } else if (option[0] == 'I') {
1094 GET_ARG_AFTER(opt, "-I");
1095 add_flag(&cppflags_obst, "-I%s", opt);
1096 } else if (option[0] == 'D') {
1098 GET_ARG_AFTER(opt, "-D");
1099 add_flag(&cppflags_obst, "-D%s", opt);
1100 } else if (option[0] == 'U') {
1102 GET_ARG_AFTER(opt, "-U");
1103 add_flag(&cppflags_obst, "-U%s", opt);
1104 } else if (option[0] == 'l') {
1106 GET_ARG_AFTER(opt, "-l");
1107 add_flag(&ldflags_obst, "-l%s", opt);
1108 } else if (option[0] == 'L') {
1110 GET_ARG_AFTER(opt, "-L");
1111 add_flag(&ldflags_obst, "-L%s", opt);
1112 } else if (SINGLE_OPTION('v')) {
1114 } else if (SINGLE_OPTION('w')) {
1115 disable_all_warnings();
1116 } else if (option[0] == 'x') {
1118 GET_ARG_AFTER(opt, "-x");
1119 forced_filetype = get_filetype_from_string(opt);
1120 if (forced_filetype == FILETYPE_UNKNOWN) {
1121 fprintf(stderr, "Unknown language '%s'\n", opt);
1122 argument_errors = true;
1124 } else if (streq(option, "M")) {
1125 mode = PreprocessOnly;
1126 add_flag(&cppflags_obst, "-M");
1127 } else if (streq(option, "MMD") ||
1128 streq(option, "MD")) {
1129 construct_dep_target = true;
1130 add_flag(&cppflags_obst, "-%s", option);
1131 } else if (streq(option, "MM") ||
1132 streq(option, "MP")) {
1133 add_flag(&cppflags_obst, "-%s", option);
1134 } else if (streq(option, "MT") ||
1135 streq(option, "MQ") ||
1136 streq(option, "MF")) {
1138 GET_ARG_AFTER(opt, "-MT");
1139 add_flag(&cppflags_obst, "-%s", option);
1140 add_flag(&cppflags_obst, "%s", opt);
1141 } else if (streq(option, "include")) {
1143 GET_ARG_AFTER(opt, "-include");
1144 add_flag(&cppflags_obst, "-include");
1145 add_flag(&cppflags_obst, "%s", opt);
1146 } else if (streq(option, "isystem")) {
1148 GET_ARG_AFTER(opt, "-isystem");
1149 add_flag(&cppflags_obst, "-isystem");
1150 add_flag(&cppflags_obst, "%s", opt);
1151 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1152 } else if (streq(option, "pthread")) {
1153 /* set flags for the preprocessor */
1154 add_flag(&cppflags_obst, "-D_REENTRANT");
1155 /* set flags for the linker */
1156 add_flag(&ldflags_obst, "-lpthread");
1158 } else if (streq(option, "nostdinc")
1159 || streq(option, "trigraphs")) {
1160 /* pass these through to the preprocessor */
1161 add_flag(&cppflags_obst, "%s", arg);
1162 } else if (streq(option, "pipe")) {
1163 /* here for gcc compatibility */
1164 } else if (streq(option, "static")) {
1165 add_flag(&ldflags_obst, "-static");
1166 } else if (streq(option, "shared")) {
1167 add_flag(&ldflags_obst, "-shared");
1168 } else if (option[0] == 'f') {
1169 char const *orig_opt;
1170 GET_ARG_AFTER(orig_opt, "-f");
1172 if (strstart(orig_opt, "input-charset=")) {
1173 char const* const encoding = strchr(orig_opt, '=') + 1;
1174 select_input_encoding(encoding);
1175 } else if (strstart(orig_opt, "align-loops=") ||
1176 strstart(orig_opt, "align-jumps=") ||
1177 strstart(orig_opt, "align-functions=")) {
1178 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1179 } else if (strstart(orig_opt, "visibility=")) {
1180 const char *val = strchr(orig_opt, '=')+1;
1181 elf_visibility_tag_t visibility
1182 = get_elf_visibility_from_string(val);
1183 if (visibility == ELF_VISIBILITY_ERROR) {
1184 fprintf(stderr, "invalid visibility '%s' specified\n",
1186 argument_errors = true;
1188 set_default_visibility(visibility);
1190 } else if (strstart(orig_opt, "message-length=")) {
1191 /* ignore: would only affect error message format */
1192 } else if (streq(orig_opt, "fast-math") ||
1193 streq(orig_opt, "fp-fast")) {
1194 firm_fp_model = fp_model_fast;
1195 } else if (streq(orig_opt, "fp-precise")) {
1196 firm_fp_model = fp_model_precise;
1197 } else if (streq(orig_opt, "fp-strict")) {
1198 firm_fp_model = fp_model_strict;
1199 } else if (streq(orig_opt, "help")) {
1200 fprintf(stderr, "warning: -fhelp is deprecated\n");
1201 help |= HELP_OPTIMIZATION;
1203 /* -f options which have an -fno- variant */
1204 char const *opt = orig_opt;
1205 bool truth_value = true;
1206 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1207 truth_value = false;
1211 if (streq(opt, "diagnostics-show-option")) {
1212 diagnostics_show_option = truth_value;
1213 } else if (streq(opt, "dollars-in-identifiers")) {
1214 allow_dollar_in_symbol = truth_value;
1215 } else if (streq(opt, "omit-frame-pointer")) {
1216 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1217 } else if (streq(opt, "short-wchar")) {
1218 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1220 } else if (streq(opt, "show-column")) {
1221 show_column = truth_value;
1222 } else if (streq(opt, "signed-char")) {
1223 char_is_signed = truth_value;
1224 } else if (streq(opt, "strength-reduce")) {
1225 /* does nothing, for gcc compatibility (even gcc does
1226 * nothing for this switch anymore) */
1227 } else if (streq(opt, "syntax-only")) {
1228 mode = truth_value ? ParseOnly : CompileAssembleLink;
1229 } else if (streq(opt, "unsigned-char")) {
1230 char_is_signed = !truth_value;
1231 } else if (streq(opt, "freestanding")) {
1232 freestanding = truth_value;
1233 } else if (streq(opt, "hosted")) {
1234 freestanding = !truth_value;
1235 } else if (streq(opt, "profile-generate")) {
1236 profile_generate = truth_value;
1237 } else if (streq(opt, "profile-use")) {
1238 profile_use = truth_value;
1239 } else if (!truth_value &&
1240 streq(opt, "asynchronous-unwind-tables")) {
1241 /* nothing todo, a gcc feature which we do not support
1242 * anyway was deactivated */
1243 } else if (streq(opt, "verbose-asm")) {
1244 /* ignore: we always print verbose assembler */
1245 } else if (streq(opt, "jump-tables") ||
1246 streq(opt, "expensive-optimizations") ||
1247 streq(opt, "common") ||
1248 streq(opt, "optimize-sibling-calls") ||
1249 streq(opt, "align-loops") ||
1250 streq(opt, "align-jumps") ||
1251 streq(opt, "align-functions") ||
1252 streq(opt, "PIC")) {
1253 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1255 int res = firm_option(orig_opt);
1257 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1259 argument_errors = true;
1264 } else if (option[0] == 'b') {
1266 GET_ARG_AFTER(opt, "-b");
1268 if (streq(opt, "help")) {
1269 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1272 int res = be_parse_arg(opt);
1274 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1276 argument_errors = true;
1277 } else if (strstart(opt, "isa=")) {
1278 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1281 } else if (option[0] == 'W') {
1282 if (strstart(option + 1, "p,")) {
1283 // pass options directly to the preprocessor
1285 GET_ARG_AFTER(opt, "-Wp,");
1286 add_flag(&cppflags_obst, "-Wp,%s", opt);
1287 } else if (strstart(option + 1, "l,")) {
1288 // pass options directly to the linker
1290 GET_ARG_AFTER(opt, "-Wl,");
1291 add_flag(&ldflags_obst, "-Wl,%s", opt);
1292 } else if (streq(option + 1, "no-trigraphs")
1293 || streq(option + 1, "undef")) {
1294 add_flag(&cppflags_obst, "%s", arg);
1295 } else if (streq(option+1, "init-self")) {
1296 /* ignored (gcc compatibility) */
1298 set_warning_opt(&option[1]);
1300 } else if (option[0] == 'm') {
1305 GET_ARG_AFTER(opt, "-m");
1306 if (strstart(opt, "target=")) {
1307 GET_ARG_AFTER(opt, "-mtarget=");
1308 if (!parse_target_triple(opt)) {
1309 argument_errors = true;
1311 setup_target_machine();
1312 target_triple = opt;
1314 } else if (strstart(opt, "triple=")) {
1315 GET_ARG_AFTER(opt, "-mtriple=");
1316 if (!parse_target_triple(opt)) {
1317 argument_errors = true;
1319 setup_target_machine();
1320 target_triple = opt;
1322 } else if (strstart(opt, "arch=")) {
1323 GET_ARG_AFTER(opt, "-march=");
1324 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1325 int res = be_parse_arg(arch_opt);
1326 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1327 res &= be_parse_arg(arch_opt);
1330 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1331 argument_errors = true;
1333 } else if (strstart(opt, "tune=")) {
1334 GET_ARG_AFTER(opt, "-mtune=");
1335 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1336 int res = be_parse_arg(arch_opt);
1338 argument_errors = true;
1339 } else if (strstart(opt, "cpu=")) {
1340 GET_ARG_AFTER(opt, "-mcpu=");
1341 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1342 int res = be_parse_arg(arch_opt);
1344 argument_errors = true;
1345 } else if (strstart(opt, "fpmath=")) {
1346 GET_ARG_AFTER(opt, "-mfpmath=");
1347 if (streq(opt, "387"))
1349 else if (streq(opt, "sse"))
1352 fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
1353 argument_errors = true;
1355 if (!argument_errors) {
1356 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1357 int res = be_parse_arg(arch_opt);
1359 argument_errors = true;
1361 } else if (strstart(opt, "preferred-stack-boundary=")) {
1362 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1363 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1364 int res = be_parse_arg(arch_opt);
1366 argument_errors = true;
1367 } else if (streq(opt, "rtd")) {
1368 default_calling_convention = CC_STDCALL;
1369 } else if (strstart(opt, "regparm=")) {
1370 fprintf(stderr, "error: regparm convention not supported yet\n");
1371 argument_errors = true;
1372 } else if (streq(opt, "soft-float")) {
1373 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1374 argument_errors = true;
1376 long int value = strtol(opt, NULL, 10);
1378 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1379 argument_errors = true;
1380 } else if (value != 16 && value != 32 && value != 64) {
1381 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1382 argument_errors = true;
1384 unsigned machine_size = (unsigned)value;
1385 /* TODO: choose/change backend based on this */
1386 add_flag(&cppflags_obst, "-m%u", machine_size);
1387 add_flag(&asflags_obst, "-m%u", machine_size);
1388 add_flag(&ldflags_obst, "-m%u", machine_size);
1391 } else if (streq(option, "pg")) {
1392 set_be_option("gprof");
1393 add_flag(&ldflags_obst, "-pg");
1394 } else if (streq(option, "pedantic") ||
1395 streq(option, "ansi")) {
1396 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1397 } else if (strstart(option, "std=")) {
1398 const char *const o = &option[4];
1400 streq(o, "c++") ? STANDARD_CXX98 :
1401 streq(o, "c++98") ? STANDARD_CXX98 :
1402 streq(o, "c89") ? STANDARD_C89 :
1403 streq(o, "c99") ? STANDARD_C99 :
1404 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1405 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1406 streq(o, "gnu89") ? STANDARD_GNU89 :
1407 streq(o, "gnu99") ? STANDARD_GNU99 :
1408 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1409 streq(o, "iso9899:1990") ? STANDARD_C89 :
1410 streq(o, "iso9899:199409") ? STANDARD_C90 :
1411 streq(o, "iso9899:1999") ? STANDARD_C99 :
1412 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1413 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1414 } else if (streq(option, "version")) {
1415 print_cparser_version();
1416 } else if (strstart(option, "print-file-name=")) {
1417 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1418 } else if (option[0] == '-') {
1419 /* double dash option */
1421 if (streq(option, "gcc")) {
1422 features_on |= _GNUC;
1423 features_off &= ~_GNUC;
1424 } else if (streq(option, "no-gcc")) {
1425 features_on &= ~_GNUC;
1426 features_off |= _GNUC;
1427 } else if (streq(option, "ms")) {
1429 features_off &= ~_MS;
1430 } else if (streq(option, "no-ms")) {
1431 features_on &= ~_MS;
1432 features_off |= _MS;
1433 } else if (streq(option, "strict")) {
1435 } else if (streq(option, "lextest")) {
1437 } else if (streq(option, "benchmark")) {
1438 mode = BenchmarkParser;
1439 } else if (streq(option, "print-ast")) {
1441 } else if (streq(option, "print-implicit-cast")) {
1442 print_implicit_casts = true;
1443 } else if (streq(option, "print-parenthesis")) {
1444 print_parenthesis = true;
1445 } else if (streq(option, "print-fluffy")) {
1447 } else if (streq(option, "print-jna")) {
1449 } else if (streq(option, "jna-limit")) {
1452 fprintf(stderr, "error: "
1453 "expected argument after '--jna-limit'\n");
1454 argument_errors = true;
1457 jna_limit_output(argv[i]);
1458 } else if (streq(option, "jna-libname")) {
1461 fprintf(stderr, "error: "
1462 "expected argument after '--jna-libname'\n");
1463 argument_errors = true;
1466 jna_set_libname(argv[i]);
1467 } else if (streq(option, "time")) {
1469 } else if (streq(option, "version")) {
1470 print_cparser_version();
1471 return EXIT_SUCCESS;
1472 } else if (streq(option, "help")) {
1474 } else if (streq(option, "help-parser")) {
1475 help |= HELP_PARSER;
1476 } else if (streq(option, "help-warnings")) {
1477 help |= HELP_WARNINGS;
1478 } else if (streq(option, "help-codegen")) {
1479 help |= HELP_CODEGEN;
1480 } else if (streq(option, "help-linker")) {
1481 help |= HELP_LINKER;
1482 } else if (streq(option, "help-optimization")) {
1483 help |= HELP_OPTIMIZATION;
1484 } else if (streq(option, "help-language-tools")) {
1485 help |= HELP_LANGUAGETOOLS;
1486 } else if (streq(option, "help-debug")) {
1488 } else if (streq(option, "help-firm")) {
1490 } else if (streq(option, "help-all")) {
1492 } else if (streq(option, "dump-function")) {
1495 fprintf(stderr, "error: "
1496 "expected argument after '--dump-function'\n");
1497 argument_errors = true;
1500 dumpfunction = argv[i];
1502 } else if (streq(option, "export-ir")) {
1503 mode = CompileExportIR;
1505 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1506 argument_errors = true;
1509 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1510 argument_errors = true;
1513 filetype_t type = forced_filetype;
1514 if (type == FILETYPE_AUTODETECT) {
1515 if (streq(arg, "-")) {
1516 /* - implicitly means C source file */
1519 const char *suffix = strrchr(arg, '.');
1520 /* Ensure there is at least one char before the suffix */
1521 if (suffix != NULL && suffix != arg) {
1524 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1525 streq(suffix, "a") ? FILETYPE_OBJECT :
1526 streq(suffix, "c") ? FILETYPE_C :
1527 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1528 streq(suffix, "C") ? FILETYPE_CXX :
1529 streq(suffix, "cc") ? FILETYPE_CXX :
1530 streq(suffix, "cp") ? FILETYPE_CXX :
1531 streq(suffix, "cpp") ? FILETYPE_CXX :
1532 streq(suffix, "CPP") ? FILETYPE_CXX :
1533 streq(suffix, "cxx") ? FILETYPE_CXX :
1534 streq(suffix, "c++") ? FILETYPE_CXX :
1535 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1536 streq(suffix, "h") ? FILETYPE_C :
1537 streq(suffix, "ir") ? FILETYPE_IR :
1538 streq(suffix, "o") ? FILETYPE_OBJECT :
1539 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1540 streq(suffix, "so") ? FILETYPE_OBJECT :
1541 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1546 file_list_entry_t *entry
1547 = obstack_alloc(&file_obst, sizeof(entry[0]));
1548 memset(entry, 0, sizeof(entry[0]));
1552 if (last_file != NULL) {
1553 last_file->next = entry;
1561 if (help != HELP_NONE) {
1562 print_help(argv[0], help);
1563 return !argument_errors;
1566 if (print_file_name_file != NULL) {
1567 print_file_name(print_file_name_file);
1568 return EXIT_SUCCESS;
1570 if (files == NULL) {
1571 fprintf(stderr, "error: no input files specified\n");
1572 argument_errors = true;
1575 if (argument_errors) {
1577 return EXIT_FAILURE;
1580 /* apply some effects from switches */
1581 c_mode |= features_on;
1582 c_mode &= ~features_off;
1583 if (profile_generate) {
1584 add_flag(&ldflags_obst, "-lfirmprof");
1585 set_be_option("profilegenerate");
1588 set_be_option("profileuse");
1592 init_symbol_table();
1593 init_types_and_adjust();
1596 if (wchar_atomic_kind == ATOMIC_TYPE_INT)
1597 init_wchar_types(type_int);
1598 else if (wchar_atomic_kind == ATOMIC_TYPE_SHORT)
1599 init_wchar_types(type_short);
1601 panic("unexpected wchar type");
1611 if (construct_dep_target) {
1612 if (outname != 0 && strlen(outname) >= 2) {
1613 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1615 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1618 dep_target[0] = '\0';
1621 char outnamebuf[4096];
1622 if (outname == NULL) {
1623 const char *filename = files->name;
1626 case BenchmarkParser:
1631 case PreprocessOnly:
1636 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1637 outname = outnamebuf;
1639 case CompileAssemble:
1640 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1641 outname = outnamebuf;
1644 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1646 outname = outnamebuf;
1648 case CompileExportIR:
1649 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1650 outname = outnamebuf;
1652 case CompileAssembleLink:
1662 assert(outname != NULL);
1665 if (streq(outname, "-")) {
1668 out = fopen(outname, "w");
1670 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
1672 return EXIT_FAILURE;
1676 file_list_entry_t *file;
1677 bool already_constructed_firm = false;
1678 for (file = files; file != NULL; file = file->next) {
1679 char asm_tempfile[1024];
1680 const char *filename = file->name;
1681 filetype_t filetype = file->type;
1683 if (filetype == FILETYPE_OBJECT)
1687 if (mode == LexTest) {
1689 in = open_file(filename);
1690 lextest(in, filename);
1692 return EXIT_SUCCESS;
1695 FILE *preprocessed_in = NULL;
1696 filetype_t next_filetype = filetype;
1699 next_filetype = FILETYPE_PREPROCESSED_C;
1702 next_filetype = FILETYPE_PREPROCESSED_CXX;
1704 case FILETYPE_ASSEMBLER:
1705 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1708 /* no support for input on FILE* yet */
1710 panic("internal compiler error: in for preprocessor != NULL");
1712 preprocessed_in = preprocess(filename, filetype);
1713 if (mode == PreprocessOnly) {
1714 copy_file(out, preprocessed_in);
1715 int pp_result = pclose(preprocessed_in);
1717 /* remove output file in case of error */
1718 if (out != stdout && pp_result != EXIT_SUCCESS) {
1724 in = preprocessed_in;
1725 filetype = next_filetype;
1733 if (mode == Compile) {
1736 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1740 in = open_file(filename);
1742 /* preprocess and compile */
1743 if (filetype == FILETYPE_PREPROCESSED_C) {
1744 char const* invalid_mode;
1747 case STANDARD_C89: c_mode = _C89; break;
1748 /* TODO determine difference between these two */
1749 case STANDARD_C90: c_mode = _C89; break;
1750 case STANDARD_C99: c_mode = _C89 | _C99; break;
1751 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1755 "warning: command line option \"-std=%s\" is not valid for C\n",
1758 case STANDARD_DEFAULT:
1759 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1761 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1762 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1765 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1766 char const* invalid_mode;
1768 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1769 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1770 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1771 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1772 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1775 case STANDARD_CXX98: c_mode = _CXX; break;
1779 "warning: command line option \"-std=%s\" is not valid for C++\n",
1781 case STANDARD_DEFAULT:
1782 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1786 c_mode |= features_on;
1787 c_mode &= ~features_off;
1789 /* do the actual parsing */
1790 ir_timer_t *t_parsing = ir_timer_new();
1791 timer_register(t_parsing, "Frontend: Parsing");
1792 timer_push(t_parsing);
1794 translation_unit_t *const unit = do_parsing(in, filename);
1795 timer_pop(t_parsing);
1797 /* prints the AST even if errors occurred */
1798 if (mode == PrintAst) {
1803 if (error_count > 0) {
1804 /* parsing failed because of errors */
1805 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1807 result = EXIT_FAILURE;
1809 } else if (warning_count > 0) {
1810 fprintf(stderr, "%u warning(s)\n", warning_count);
1813 if (in == preprocessed_in) {
1814 int pp_result = pclose(preprocessed_in);
1815 if (pp_result != EXIT_SUCCESS) {
1816 /* remove output file */
1819 return EXIT_FAILURE;
1823 if (mode == BenchmarkParser) {
1825 } else if (mode == PrintFluffy) {
1826 write_fluffy_decls(out, unit);
1828 } else if (mode == PrintJna) {
1829 write_jna_decls(out, unit);
1833 /* build the firm graph */
1834 ir_timer_t *t_construct = ir_timer_new();
1835 timer_register(t_construct, "Frontend: Graph construction");
1836 timer_push(t_construct);
1837 if (already_constructed_firm) {
1838 panic("compiling multiple files/translation units not possible");
1840 translation_unit_to_firm(unit);
1841 already_constructed_firm = true;
1842 timer_pop(t_construct);
1845 if (mode == ParseOnly) {
1849 if (mode == CompileDump) {
1851 ident *id = new_id_from_str(dumpfunction);
1852 ir_graph *irg = NULL;
1853 int n_irgs = get_irp_n_irgs();
1854 for (int i = 0; i < n_irgs; ++i) {
1855 ir_graph *tirg = get_irp_irg(i);
1856 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1864 fprintf(stderr, "No graph for function '%s' found\n",
1866 return EXIT_FAILURE;
1869 dump_ir_graph_file(out, irg);
1871 return EXIT_SUCCESS;
1874 if (mode == CompileExportIR) {
1877 return EXIT_SUCCESS;
1880 gen_firm_finish(asm_out, filename);
1881 if (asm_out != out) {
1884 } else if (filetype == FILETYPE_IR) {
1886 ir_import(filename);
1888 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1889 copy_file(asm_out, in);
1890 if (in == preprocessed_in) {
1891 int pp_result = pclose(preprocessed_in);
1892 if (pp_result != EXIT_SUCCESS) {
1893 /* remove output in error case */
1899 if (asm_out != out) {
1904 if (mode == Compile)
1907 /* if we're here then we have preprocessed assembly */
1908 filename = asm_tempfile;
1909 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1912 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1914 const char *filename_o;
1915 if (mode == CompileAssemble) {
1917 filename_o = outname;
1919 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1924 assemble(filename_o, filename);
1926 size_t len = strlen(filename_o) + 1;
1927 filename = obstack_copy(&file_obst, filename_o, len);
1928 filetype = FILETYPE_OBJECT;
1931 /* ok we're done here, process next file */
1932 file->name = filename;
1933 file->type = filetype;
1936 if (result != EXIT_SUCCESS) {
1942 /* link program file */
1943 if (mode == CompileAssembleLink) {
1944 obstack_1grow(&ldflags_obst, '\0');
1945 const char *flags = obstack_finish(&ldflags_obst);
1947 /* construct commandline */
1948 const char *linker = getenv("CPARSER_LINK");
1949 if (linker != NULL) {
1950 obstack_printf(&file_obst, "%s ", linker);
1952 if (target_triple != NULL)
1953 obstack_printf(&file_obst, "%s-", target_triple);
1954 obstack_printf(&file_obst, "%s ", LINKER);
1957 for (file_list_entry_t *entry = files; entry != NULL;
1958 entry = entry->next) {
1959 if (entry->type != FILETYPE_OBJECT)
1962 add_flag(&file_obst, "%s", entry->name);
1965 add_flag(&file_obst, "-o");
1966 add_flag(&file_obst, outname);
1967 obstack_printf(&file_obst, "%s", flags);
1968 obstack_1grow(&file_obst, '\0');
1970 char *commandline = obstack_finish(&file_obst);
1975 int err = system(commandline);
1976 if (err != EXIT_SUCCESS) {
1977 fprintf(stderr, "linker reported an error\n");
1978 return EXIT_FAILURE;
1985 obstack_free(&cppflags_obst, NULL);
1986 obstack_free(&ldflags_obst, NULL);
1987 obstack_free(&asflags_obst, NULL);
1988 obstack_free(&file_obst, NULL);
1998 exit_symbol_table();
1999 return EXIT_SUCCESS;