2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode) _access(fname, mode)
48 #define mktemp(tmpl) _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode) _fdopen(fd, mode)
51 #define popen(cmd, mode) _popen(cmd, mode)
52 #define pclose(file) _pclose(file)
53 #define unlink(filename) _unlink(filename)
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
66 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_timing.h"
74 #include "driver/firm_machine.h"
75 #include "adt/error.h"
76 #include "adt/strutil.h"
77 #include "wrappergen/write_fluffy.h"
78 #include "wrappergen/write_jna.h"
87 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
89 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
98 #define ASSEMBLER "gcc -c -xassembler"
101 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
102 bool byte_order_big_endian = false;
103 bool strict_mode = false;
104 bool enable_main_collect2_hack = false;
105 bool freestanding = false;
106 unsigned architecture_modulo_shift = 0;
108 static bool char_is_signed = true;
109 static atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
111 static machine_triple_t *target_machine;
112 static const char *target_triple;
114 static struct obstack cppflags_obst;
115 static struct obstack ldflags_obst;
116 static struct obstack asflags_obst;
117 static char dep_target[1024];
118 static const char *outname;
119 static bool define_intmax_types;
120 static const char *input_encoding;
122 typedef enum lang_standard_t {
123 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
124 STANDARD_ANSI, /* c89 (for C) or c++98 (for C++) */
125 STANDARD_C89, /* ISO C90 (sic) */
126 STANDARD_C90, /* ISO C90 as modified in amendment 1 */
127 STANDARD_C99, /* ISO C99 */
128 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
129 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
130 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
131 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
134 static lang_standard_t standard;
136 typedef struct file_list_entry_t file_list_entry_t;
138 typedef enum filetype_t {
141 FILETYPE_PREPROCESSED_C,
143 FILETYPE_PREPROCESSED_CXX,
145 FILETYPE_PREPROCESSED_ASSEMBLER,
151 struct file_list_entry_t {
152 const char *name; /**< filename or NULL for stdin */
154 file_list_entry_t *next;
157 static file_list_entry_t *temp_files;
159 static void get_output_name(char *buf, size_t buflen, const char *inputname,
162 if (inputname == NULL)
165 char const *const last_slash = strrchr(inputname, '/');
166 char const *const filename =
167 last_slash != NULL ? last_slash + 1 : inputname;
168 char const *const last_dot = strrchr(filename, '.');
169 char const *const name_end =
170 last_dot != NULL ? last_dot : strchr(filename, '\0');
172 int const len = snprintf(buf, buflen, "%.*s%s",
173 (int)(name_end - filename), filename, newext);
175 if (len < 0 || buflen <= (size_t)len)
177 if (buflen <= (size_t)len)
179 panic("filename too long");
182 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
186 input_t *input = input_from_stream(in, input_encoding);
187 lexer_switch_input(input, input_name);
189 translation_unit_t *unit = finish_parsing();
195 static void lextest(FILE *in, const char *fname)
197 input_t *input = input_from_stream(in, input_encoding);
198 lexer_switch_input(input, fname);
201 lexer_next_preprocessing_token();
202 print_token(stdout, &lexer_token);
204 } while (lexer_token.kind != T_EOF);
208 static void add_flag(struct obstack *obst, const char *format, ...)
213 va_start(ap, format);
217 vsnprintf(buf, sizeof(buf), format, ap);
220 obstack_1grow(obst, ' ');
222 obstack_1grow(obst, '"');
223 obstack_grow(obst, buf, len);
224 obstack_1grow(obst, '"');
226 /* escape stuff... */
227 for (char *c = buf; *c != '\0'; ++c) {
245 obstack_1grow(obst, '\\');
248 obstack_1grow(obst, *c);
255 static const char *type_to_string(type_t *type)
257 assert(type->kind == TYPE_ATOMIC);
258 return get_atomic_kind_name(type->atomic.akind);
261 static FILE *preprocess(const char *fname, filetype_t filetype)
263 static const char *common_flags = NULL;
265 if (common_flags == NULL) {
266 obstack_1grow(&cppflags_obst, '\0');
267 const char *flags = obstack_finish(&cppflags_obst);
269 /* setup default defines */
270 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
271 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
272 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
273 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
275 add_flag(&cppflags_obst, "-U__VERSION__");
276 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
278 if (define_intmax_types) {
279 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
280 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
281 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
282 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
285 if (flags[0] != '\0') {
286 size_t len = strlen(flags);
287 obstack_1grow(&cppflags_obst, ' ');
288 obstack_grow(&cppflags_obst, flags, len);
290 obstack_1grow(&cppflags_obst, '\0');
291 common_flags = obstack_finish(&cppflags_obst);
294 assert(obstack_object_size(&cppflags_obst) == 0);
296 const char *preprocessor = getenv("CPARSER_PP");
297 if (preprocessor != NULL) {
298 obstack_printf(&cppflags_obst, "%s ", preprocessor);
300 if (target_triple != NULL)
301 obstack_printf(&cppflags_obst, "%s-", target_triple);
302 obstack_printf(&cppflags_obst, PREPROCESSOR);
307 add_flag(&cppflags_obst, "-std=c99");
310 add_flag(&cppflags_obst, "-std=c++98");
312 case FILETYPE_ASSEMBLER:
313 add_flag(&cppflags_obst, "-x");
314 add_flag(&cppflags_obst, "assembler-with-cpp");
319 obstack_printf(&cppflags_obst, "%s", common_flags);
321 /* handle dependency generation */
322 if (dep_target[0] != '\0') {
323 add_flag(&cppflags_obst, "-MF");
324 add_flag(&cppflags_obst, dep_target);
325 if (outname != NULL) {
326 add_flag(&cppflags_obst, "-MQ");
327 add_flag(&cppflags_obst, outname);
330 add_flag(&cppflags_obst, fname);
331 obstack_1grow(&cppflags_obst, '\0');
333 char *commandline = obstack_finish(&cppflags_obst);
337 FILE *f = popen(commandline, "r");
339 fprintf(stderr, "invoking preprocessor failed\n");
342 /* we do not really need that anymore */
343 obstack_free(&cppflags_obst, commandline);
348 static void assemble(const char *out, const char *in)
350 obstack_1grow(&asflags_obst, '\0');
351 const char *flags = obstack_finish(&asflags_obst);
353 const char *assembler = getenv("CPARSER_AS");
354 if (assembler != NULL) {
355 obstack_printf(&asflags_obst, "%s", assembler);
357 if (target_triple != NULL)
358 obstack_printf(&asflags_obst, "%s-", target_triple);
359 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
361 if (flags[0] != '\0')
362 obstack_printf(&asflags_obst, " %s", flags);
364 obstack_printf(&asflags_obst, " %s -o %s", in, out);
365 obstack_1grow(&asflags_obst, '\0');
367 char *commandline = obstack_finish(&asflags_obst);
371 int err = system(commandline);
372 if (err != EXIT_SUCCESS) {
373 fprintf(stderr, "assembler reported an error\n");
376 obstack_free(&asflags_obst, commandline);
379 static void print_file_name(const char *file)
381 add_flag(&ldflags_obst, "-print-file-name=%s", file);
383 obstack_1grow(&ldflags_obst, '\0');
384 const char *flags = obstack_finish(&ldflags_obst);
386 /* construct commandline */
387 const char *linker = getenv("CPARSER_LINK");
388 if (linker != NULL) {
389 obstack_printf(&ldflags_obst, "%s ", linker);
391 if (target_triple != NULL)
392 obstack_printf(&ldflags_obst, "%s-", target_triple);
393 obstack_printf(&ldflags_obst, "%s ", LINKER);
395 obstack_printf(&ldflags_obst, "%s ", linker);
396 obstack_printf(&ldflags_obst, "%s", flags);
397 obstack_1grow(&ldflags_obst, '\0');
399 char *commandline = obstack_finish(&ldflags_obst);
403 int err = system(commandline);
404 if (err != EXIT_SUCCESS) {
405 fprintf(stderr, "linker reported an error\n");
408 obstack_free(&ldflags_obst, commandline);
411 static const char *try_dir(const char *dir)
415 if (access(dir, R_OK | W_OK | X_OK) == 0)
420 static const char *get_tempdir(void)
422 static const char *tmpdir = NULL;
428 tmpdir = try_dir(getenv("TMPDIR"));
430 tmpdir = try_dir(getenv("TMP"));
432 tmpdir = try_dir(getenv("TEMP"));
436 tmpdir = try_dir(P_tmpdir);
440 tmpdir = try_dir("/var/tmp");
442 tmpdir = try_dir("/usr/tmp");
444 tmpdir = try_dir("/tmp");
453 /* cheap and nasty mkstemp replacement */
454 static int mkstemp(char *templ)
457 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
462 * an own version of tmpnam, which: writes in a buffer, emits no warnings
463 * during linking (like glibc/gnu ld do for tmpnam)...
465 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
467 const char *tempdir = get_tempdir();
469 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
471 int fd = mkstemp(buffer);
473 fprintf(stderr, "could not create temporary file: %s\n",
477 FILE *out = fdopen(fd, "w");
479 fprintf(stderr, "could not create temporary file FILE*\n");
483 file_list_entry_t *entry = xmalloc(sizeof(*entry));
484 memset(entry, 0, sizeof(*entry));
486 size_t name_len = strlen(buffer) + 1;
487 char *name = malloc(name_len);
488 memcpy(name, buffer, name_len);
491 entry->next = temp_files;
497 static void free_temp_files(void)
499 file_list_entry_t *entry = temp_files;
500 file_list_entry_t *next;
501 for ( ; entry != NULL; entry = next) {
505 free((char*) entry->name);
511 typedef enum compile_mode_t {
526 static void usage(const char *argv0)
528 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
531 static void print_cparser_version(void)
533 printf("cparser (%s) using libFirm (%u.%u",
534 cparser_REVISION, ir_get_version_major(),
535 ir_get_version_minor());
537 const char *revision = ir_get_version_revision();
538 if (revision[0] != 0) {
540 fputs(revision, stdout);
543 const char *build = ir_get_version_build();
546 fputs(build, stdout);
549 puts("This is free software; see the source for copying conditions. There is NO\n"
550 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
553 static void print_help_basic(const char *argv0)
557 put_help("--help", "Display this information");
558 put_help("--version", "Display compiler version");
559 put_help("--help-parser", "Display information about parser options");
560 put_help("--help-warnings", "Display information about warning options");
561 put_help("--help-codegen", "Display information about code-generation options");
562 put_help("--help-optimization", "Display information about optimization options");
563 put_help("--help-linker", "Display information about linker options");
564 put_help("--help-language-tools", "Display information about language tools options");
565 put_help("--help-debug", "Display information about compiler debugging options");
566 put_help("--help-firm", "Display information about direct firm options");
567 put_help("--help-all", "Display information about all options");
568 put_help("-c", "Compile and assemble but do not link");
569 put_help("-E", "Preprocess only");
570 put_help("-S", "Compile but do not assembler or link");
571 put_help("-o", "Specify output file");
572 put_help("-v", "Verbose output (show invocation of sub-processes)");
573 put_help("-x", "Force input language:");
574 put_choice("c", "C");
575 put_choice("c++", "C++");
576 put_choice("assembler", "Assembler (no preprocessing)");
577 put_choice("assembler-with-cpp", "Assembler with preprocessing");
578 put_choice("none", "Autodetection");
579 put_help("-pipe", "Ignored (gcc compatibility)");
582 static void print_help_preprocessor(void)
584 put_help("-nostdinc", "Do not search standard system include directories");
585 put_help("-trigraphs", "Support ISO C trigraphs");
586 put_help("-isystem", "");
587 put_help("-include", "");
588 put_help("-I PATH", "");
589 put_help("-D SYMBOL[=value]", "");
590 put_help("-U SYMBOL", "");
591 put_help("-Wp,OPTION", "Pass option directly to preprocessor");
594 put_help("-MMD", "");
602 static void print_help_parser(void)
604 put_help("-finput-charset=CHARSET", "Select encoding of input files");
605 put_help("-fmessage-length=LEN", "Ignored (gcc compatibility)");
606 put_help("-fshort-wchar", "Type \"wchar_t\" is unsigned short instead of int");
607 put_help("-fshow-column", "Show the column number in diagnostic messages");
608 put_help("-fsigned-char", "Type \"char\" is a signed type");
609 put_help("-funsigned-char", "Type \"char\" is an unsigned type");
610 put_help("--ms", "Enable msvc extensions");
611 put_help("--no-ms", "Disable msvc extensions");
612 put_help("--gcc", "Enable gcc extensions");
613 put_help("--no-gcc", "Disable gcc extensions");
614 put_help("-std=STANDARD", "Specify language standard:");
615 put_choice("c99", "ISO C99 standard");
616 put_choice("c89", "ISO C89 standard");
617 put_choice("c9x", "Deprecated");
618 put_choice("c++", "ISO C++ 98");
619 put_choice("c++98", "ISO C++ 98");
620 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
621 put_choice("gnu89", "ISO C89 + GNU extensions");
622 put_choice("gnu9x", "Deprecated");
623 put_choice("iso9899:1990", "ISO C89");
624 put_choice("iso9899:199409", "ISO C90");
625 put_choice("iso9899:1999", "ISO C99");
626 put_choice("iso9899:199x", "Deprecated");
627 put_help("-pedantic", "Ignored (gcc compatibility)");
628 put_help("-ansi", "Ignored (gcc compatibility)");
629 put_help("--strict", "Enable strict conformance checking");
632 static void print_help_warnings(void)
634 put_help("-f[no-]diagnostics-show-option", "Show the switch, which controls a warning, after each warning");
635 put_help("-w", "Disable all warnings");
636 put_help("-Wno-trigraphs", "Warn if input contains trigraphs");
637 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
638 put_help("-Wmissing-include-dirs", "Warn about missing user-specified include directories");
639 put_help("-Wendif-labels", "Warn about stray text after #elif and #endif");
640 put_help("-Winit-self", "Ignored (gcc compatibility)");
641 put_help("-Wformat-y2k", "Ignored (gcc compatibility)");
642 put_help("-Wformat-security", "Ignored (gcc compatibility)");
643 put_help("-Wold-style-declaration", "Ignored (gcc compatibility)");
644 put_help("-Wtype-limits", "Ignored (gcc compatibility)");
645 print_warning_opt_help();
648 static void print_help_optimization(void)
650 put_help("-O LEVEL", "Select optimization level (0-4)");
651 firm_option_help(put_help);
652 put_help("-fexpensive-optimizations","Ignored (gcc compatibility)");
655 static void print_help_codegeneration(void)
657 put_help("-g", "Generate debug information");
658 put_help("-pg", "Instrument code for gnu gprof");
659 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
660 put_help("-ffreestanding", "Compile in freestanding mode (see ISO C standard)");
661 put_help("-fhosted", "Compile in hosted (not freestanding) mode");
662 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
663 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
664 put_help("-ffp-precise", "Precise floating point model");
665 put_help("-ffp-fast", "Imprecise floating point model");
666 put_help("-ffp-strict", "Strict floating point model");
667 put_help("-pthread", "Use pthread threading library");
668 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
669 put_help("-mtriple=TARGET", "Alias for -mtarget (clang compatibility)");
670 put_help("-march=ARCH", "");
671 put_help("-mtune=ARCH", "");
672 put_help("-mcpu=CPU", "");
673 put_help("-mfpmath=", "");
674 put_help("-mpreferred-stack-boundary=", "");
675 put_help("-mrtd", "");
676 put_help("-mregparm=", "Not supported yet");
677 put_help("-msoft-float", "Not supported yet");
678 put_help("-m32", "Generate 32bit code");
679 put_help("-m64", "Generate 64bit code");
680 put_help("-fverbose-asm", "Ignored (gcc compatibility)");
681 put_help("-fjump-tables", "Ignored (gcc compatibility)");
682 put_help("-fcommon", "Ignored (gcc compatibility)");
683 put_help("-foptimize-sibling-calls", "Ignored (gcc compatibility)");
684 put_help("-falign-loops", "Ignored (gcc compatibility)");
685 put_help("-falign-jumps", "Ignored (gcc compatibility)");
686 put_help("-falign-functions", "Ignored (gcc compatibility)");
687 put_help("-fPIC", "Ignored (gcc compatibility)");
688 put_help("-ffast-math", "Same as -ffp-fast (gcc compatibility)");
690 puts("\tMost of these options can be used with a no- prefix to disable them");
691 puts("\te.g. -fno-omit-frame-pointer");
694 static void print_help_linker(void)
696 put_help("-l LIBRARY", "");
697 put_help("-L PATH", "");
698 put_help("-shared", "Produce a shared library");
699 put_help("-static", "Produce statically linked binary");
700 put_help("-Wl,OPTION", "Pass option directly to linker");
703 static void print_help_debug(void)
705 put_help("--lextest", "Preprocess and tokenize only");
706 put_help("--print-ast", "Preprocess, parse and print AST");
707 put_help("--print-implicit-cast", "");
708 put_help("--print-parenthesis", "");
709 put_help("--benchmark", "Preprocess and parse, produces no output");
710 put_help("--time", "Measure time of compiler passes");
711 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
712 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
715 static void print_help_language_tools(void)
717 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
718 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
719 put_help("--jna-limit filename", "");
720 put_help("--jna-libname name", "");
723 static void print_help_firm(void)
725 put_help("-bOPTION", "Directly pass option to libFirm backend");
726 int res = be_parse_arg("help");
733 HELP_BASIC = 1u << 0,
734 HELP_PREPROCESSOR = 1u << 1,
735 HELP_PARSER = 1u << 2,
736 HELP_WARNINGS = 1u << 3,
737 HELP_OPTIMIZATION = 1u << 4,
738 HELP_CODEGEN = 1u << 5,
739 HELP_LINKER = 1u << 6,
740 HELP_LANGUAGETOOLS = 1u << 7,
741 HELP_DEBUG = 1u << 8,
744 HELP_ALL = (unsigned)-1
747 static void print_help(const char *argv0, help_sections_t sections)
749 if (sections & HELP_BASIC) print_help_basic(argv0);
750 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
751 if (sections & HELP_PARSER) print_help_parser();
752 if (sections & HELP_WARNINGS) print_help_warnings();
753 if (sections & HELP_OPTIMIZATION) print_help_optimization();
754 if (sections & HELP_CODEGEN) print_help_codegeneration();
755 if (sections & HELP_LINKER) print_help_linker();
756 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
757 if (sections & HELP_DEBUG) print_help_debug();
758 if (sections & HELP_FIRM) print_help_firm();
761 static void set_be_option(const char *arg)
763 int res = be_parse_arg(arg);
768 static void copy_file(FILE *dest, FILE *input)
772 while (!feof(input) && !ferror(dest)) {
773 size_t read = fread(buf, 1, sizeof(buf), input);
774 if (fwrite(buf, 1, read, dest) != read) {
775 perror("could not write output");
780 static FILE *open_file(const char *filename)
782 if (streq(filename, "-")) {
786 FILE *in = fopen(filename, "r");
788 fprintf(stderr, "Could not open '%s': %s\n", filename,
796 static filetype_t get_filetype_from_string(const char *string)
798 if (streq(string, "c") || streq(string, "c-header"))
800 if (streq(string, "c++") || streq(string, "c++-header"))
802 if (streq(string, "assembler"))
803 return FILETYPE_PREPROCESSED_ASSEMBLER;
804 if (streq(string, "assembler-with-cpp"))
805 return FILETYPE_ASSEMBLER;
806 if (streq(string, "none"))
807 return FILETYPE_AUTODETECT;
809 return FILETYPE_UNKNOWN;
812 static bool is_windows_os(const char *os)
814 return strstr(os, "mingw") != NULL || streq(os, "win32");
817 static bool is_unixish_os(const char *os)
819 return strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
820 || streq(os, "solaris");
823 static bool is_darwin_os(const char *os)
825 return streq(os, "darwin");
828 static bool init_os_support(void)
830 const char *os = target_machine->operating_system;
831 wchar_atomic_kind = ATOMIC_TYPE_INT;
832 enable_main_collect2_hack = false;
833 define_intmax_types = false;
835 if (is_unixish_os(os)) {
836 set_create_ld_ident(create_name_linux_elf);
837 } else if (is_darwin_os(os)) {
838 set_create_ld_ident(create_name_macho);
839 define_intmax_types = true;
840 } else if (is_windows_os(os)) {
841 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
842 enable_main_collect2_hack = true;
843 set_create_ld_ident(create_name_win32);
851 static bool parse_target_triple(const char *arg)
853 machine_triple_t *triple = firm_parse_machine_triple(arg);
854 if (triple == NULL) {
855 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
858 target_machine = triple;
862 static unsigned decide_modulo_shift(unsigned type_size)
864 if (architecture_modulo_shift == 0)
866 if (type_size < architecture_modulo_shift)
867 return architecture_modulo_shift;
871 static bool is_ia32_cpu(const char *architecture)
873 return streq(architecture, "i386")
874 || streq(architecture, "i486")
875 || streq(architecture, "i586")
876 || streq(architecture, "i686")
877 || streq(architecture, "i786");
880 static const char *setup_isa_from_tripel(const machine_triple_t *machine)
882 const char *cpu = machine->cpu_type;
884 if (is_ia32_cpu(cpu)) {
886 } else if (streq(cpu, "x86_64")) {
888 } else if (streq(cpu, "sparc")) {
890 } else if (streq(cpu, "arm")) {
893 fprintf(stderr, "Unknown cpu '%s' in target-triple\n", cpu);
898 static const char *setup_target_machine(void)
900 if (!setup_firm_for_machine(target_machine))
903 const char *isa = setup_isa_from_tripel(target_machine);
914 * initialize cparser type properties based on a firm type
916 static void set_typeprops_type(atomic_type_properties_t* props, ir_type *type)
918 props->size = get_type_size_bytes(type);
919 props->alignment = get_type_alignment_bytes(type);
920 props->struct_alignment = props->alignment;
924 * Copy atomic type properties except the integer conversion rank
926 static void copy_typeprops(atomic_type_properties_t *dest,
927 const atomic_type_properties_t *src)
929 dest->size = src->size;
930 dest->alignment = src->alignment;
931 dest->struct_alignment = src->struct_alignment;
932 dest->flags = src->flags;
935 static void init_types_and_adjust(void)
937 const backend_params *be_params = be_get_backend_param();
938 unsigned machine_size = be_params->machine_size;
939 init_types(machine_size);
941 atomic_type_properties_t *props = atomic_type_properties;
943 /* adjust types as requested by target architecture */
944 ir_type *type_long_double = be_params->type_long_double;
945 if (type_long_double != NULL)
946 set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], 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_long_long != NULL)
954 set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_unsigned_long_long);
956 /* operating system ABI specifics */
957 const char *os = target_machine->operating_system;
958 if (is_darwin_os(os)) {
959 if (machine_size == 32) {
960 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
961 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
962 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
964 } else if (is_windows_os(os)) {
965 if (machine_size == 64) {
966 /* to ease porting of old c-code microsoft decided to use 32bits
968 props[ATOMIC_TYPE_LONG] = props[ATOMIC_TYPE_INT];
969 props[ATOMIC_TYPE_ULONG] = props[ATOMIC_TYPE_UINT];
972 /* on windows long double is not supported */
973 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
974 } else if (is_unixish_os(os)) {
975 if (is_ia32_cpu(target_machine->cpu_type)) {
976 /* System V has a broken alignment for double so we have to add
978 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
979 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
980 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
984 /* stuff decided after processing operating system specifics and
985 * commandline flags */
986 if (char_is_signed) {
987 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
989 props[ATOMIC_TYPE_CHAR].flags &= ~ATOMIC_TYPE_FLAG_SIGNED;
991 /* copy over wchar_t properties (including rank) */
992 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
994 /* initialize defaults for unsupported types */
995 if (type_long_long == NULL) {
996 copy_typeprops(&props[ATOMIC_TYPE_LONGLONG], &props[ATOMIC_TYPE_LONG]);
998 if (type_unsigned_long_long == NULL) {
999 copy_typeprops(&props[ATOMIC_TYPE_ULONGLONG],
1000 &props[ATOMIC_TYPE_ULONG]);
1002 if (type_long_double == NULL) {
1003 copy_typeprops(&props[ATOMIC_TYPE_LONG_DOUBLE],
1004 &props[ATOMIC_TYPE_DOUBLE]);
1007 /* initialize firm pointer modes */
1009 ir_mode_sort sort = irms_reference;
1010 unsigned bit_size = machine_size;
1012 ir_mode_arithmetic arithmetic = irma_twos_complement;
1013 unsigned modulo_shift = decide_modulo_shift(bit_size);
1015 snprintf(name, sizeof(name), "p%u", machine_size);
1016 ir_mode *ptr_mode = new_ir_mode(name, sort, bit_size, is_signed, arithmetic,
1019 if (machine_size == 16) {
1020 set_reference_mode_signed_eq(ptr_mode, mode_Hs);
1021 set_reference_mode_unsigned_eq(ptr_mode, mode_Hu);
1022 } else if (machine_size == 32) {
1023 set_reference_mode_signed_eq(ptr_mode, mode_Is);
1024 set_reference_mode_unsigned_eq(ptr_mode, mode_Iu);
1025 } else if (machine_size == 64) {
1026 set_reference_mode_signed_eq(ptr_mode, mode_Ls);
1027 set_reference_mode_unsigned_eq(ptr_mode, mode_Lu);
1029 panic("strange machine_size when determining pointer modes");
1032 /* Hmm, pointers should be machine size */
1033 set_modeP_data(ptr_mode);
1034 set_modeP_code(ptr_mode);
1036 byte_order_big_endian = be_params->byte_order_big_endian;
1037 if (be_params->modulo_shift_efficient) {
1038 architecture_modulo_shift = machine_size;
1040 architecture_modulo_shift = 0;
1044 int main(int argc, char **argv)
1048 const char *dumpfunction = NULL;
1049 const char *print_file_name_file = NULL;
1050 compile_mode_t mode = CompileAssembleLink;
1052 int result = EXIT_SUCCESS;
1053 char cpu_arch[16] = "ia32";
1054 file_list_entry_t *files = NULL;
1055 file_list_entry_t *last_file = NULL;
1056 bool construct_dep_target = false;
1057 bool do_timing = false;
1058 bool profile_generate = false;
1059 bool profile_use = false;
1060 struct obstack file_obst;
1062 atexit(free_temp_files);
1064 /* hack for now... */
1065 if (strstr(argv[0], "pptest") != NULL) {
1066 extern int pptest_main(int argc, char **argv);
1067 return pptest_main(argc, argv);
1070 obstack_init(&cppflags_obst);
1071 obstack_init(&ldflags_obst);
1072 obstack_init(&asflags_obst);
1073 obstack_init(&file_obst);
1075 #define GET_ARG_AFTER(def, args) \
1077 def = &arg[sizeof(args)-1]; \
1078 if (def[0] == '\0') { \
1081 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1082 argument_errors = true; \
1086 if (def[0] == '-' && def[1] != '\0') { \
1087 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1088 argument_errors = true; \
1094 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
1096 /* early options parsing (find out optimization level and OS) */
1097 for (int i = 1; i < argc; ++i) {
1098 const char *arg = argv[i];
1102 const char *option = &arg[1];
1103 if (option[0] == 'O') {
1104 sscanf(&option[1], "%d", &opt_level);
1108 const char *target = getenv("TARGET");
1110 parse_target_triple(target);
1111 if (target_machine == NULL) {
1112 target_machine = firm_get_host_machine();
1114 choose_optimization_pack(opt_level);
1115 setup_target_machine();
1117 /* parse rest of options */
1118 standard = STANDARD_DEFAULT;
1119 unsigned features_on = 0;
1120 unsigned features_off = 0;
1121 filetype_t forced_filetype = FILETYPE_AUTODETECT;
1122 help_sections_t help = HELP_NONE;
1123 bool argument_errors = false;
1124 for (int i = 1; i < argc; ++i) {
1125 const char *arg = argv[i];
1126 if (arg[0] == '-' && arg[1] != '\0') {
1128 const char *option = &arg[1];
1129 if (option[0] == 'o') {
1130 GET_ARG_AFTER(outname, "-o");
1131 } else if (option[0] == 'g') {
1132 set_be_option("debuginfo=stabs");
1133 set_be_option("omitfp=no");
1134 set_be_option("ia32-nooptcc=yes");
1135 } else if (SINGLE_OPTION('c')) {
1136 mode = CompileAssemble;
1137 } else if (SINGLE_OPTION('E')) {
1138 mode = PreprocessOnly;
1139 } else if (SINGLE_OPTION('S')) {
1141 } else if (option[0] == 'O') {
1143 } else if (option[0] == 'I') {
1145 GET_ARG_AFTER(opt, "-I");
1146 add_flag(&cppflags_obst, "-I%s", opt);
1147 } else if (option[0] == 'D') {
1149 GET_ARG_AFTER(opt, "-D");
1150 add_flag(&cppflags_obst, "-D%s", opt);
1151 } else if (option[0] == 'U') {
1153 GET_ARG_AFTER(opt, "-U");
1154 add_flag(&cppflags_obst, "-U%s", opt);
1155 } else if (option[0] == 'l') {
1157 GET_ARG_AFTER(opt, "-l");
1158 add_flag(&ldflags_obst, "-l%s", opt);
1159 } else if (option[0] == 'L') {
1161 GET_ARG_AFTER(opt, "-L");
1162 add_flag(&ldflags_obst, "-L%s", opt);
1163 } else if (SINGLE_OPTION('v')) {
1165 } else if (SINGLE_OPTION('w')) {
1166 disable_all_warnings();
1167 } else if (option[0] == 'x') {
1169 GET_ARG_AFTER(opt, "-x");
1170 forced_filetype = get_filetype_from_string(opt);
1171 if (forced_filetype == FILETYPE_UNKNOWN) {
1172 fprintf(stderr, "Unknown language '%s'\n", opt);
1173 argument_errors = true;
1175 } else if (streq(option, "M")) {
1176 mode = PreprocessOnly;
1177 add_flag(&cppflags_obst, "-M");
1178 } else if (streq(option, "MMD") ||
1179 streq(option, "MD")) {
1180 construct_dep_target = true;
1181 add_flag(&cppflags_obst, "-%s", option);
1182 } else if (streq(option, "MM") ||
1183 streq(option, "MP")) {
1184 add_flag(&cppflags_obst, "-%s", option);
1185 } else if (streq(option, "MT") ||
1186 streq(option, "MQ") ||
1187 streq(option, "MF")) {
1189 GET_ARG_AFTER(opt, "-MT");
1190 add_flag(&cppflags_obst, "-%s", option);
1191 add_flag(&cppflags_obst, "%s", opt);
1192 } else if (streq(option, "include")) {
1194 GET_ARG_AFTER(opt, "-include");
1195 add_flag(&cppflags_obst, "-include");
1196 add_flag(&cppflags_obst, "%s", opt);
1197 } else if (streq(option, "isystem")) {
1199 GET_ARG_AFTER(opt, "-isystem");
1200 add_flag(&cppflags_obst, "-isystem");
1201 add_flag(&cppflags_obst, "%s", opt);
1202 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1203 } else if (streq(option, "pthread")) {
1204 /* set flags for the preprocessor */
1205 add_flag(&cppflags_obst, "-D_REENTRANT");
1206 /* set flags for the linker */
1207 add_flag(&ldflags_obst, "-lpthread");
1209 } else if (streq(option, "nostdinc")
1210 || streq(option, "trigraphs")) {
1211 /* pass these through to the preprocessor */
1212 add_flag(&cppflags_obst, "%s", arg);
1213 } else if (streq(option, "pipe")) {
1214 /* here for gcc compatibility */
1215 } else if (streq(option, "static")) {
1216 add_flag(&ldflags_obst, "-static");
1217 } else if (streq(option, "shared")) {
1218 add_flag(&ldflags_obst, "-shared");
1219 } else if (option[0] == 'f') {
1220 char const *orig_opt;
1221 GET_ARG_AFTER(orig_opt, "-f");
1223 if (strstart(orig_opt, "input-charset=")) {
1224 char const* const encoding = strchr(orig_opt, '=') + 1;
1225 input_encoding = encoding;
1226 } else if (strstart(orig_opt, "align-loops=") ||
1227 strstart(orig_opt, "align-jumps=") ||
1228 strstart(orig_opt, "align-functions=")) {
1229 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1230 } else if (strstart(orig_opt, "visibility=")) {
1231 const char *val = strchr(orig_opt, '=')+1;
1232 elf_visibility_tag_t visibility
1233 = get_elf_visibility_from_string(val);
1234 if (visibility == ELF_VISIBILITY_ERROR) {
1235 fprintf(stderr, "invalid visibility '%s' specified\n",
1237 argument_errors = true;
1239 set_default_visibility(visibility);
1241 } else if (strstart(orig_opt, "message-length=")) {
1242 /* ignore: would only affect error message format */
1243 } else if (streq(orig_opt, "fast-math") ||
1244 streq(orig_opt, "fp-fast")) {
1245 firm_fp_model = fp_model_fast;
1246 } else if (streq(orig_opt, "fp-precise")) {
1247 firm_fp_model = fp_model_precise;
1248 } else if (streq(orig_opt, "fp-strict")) {
1249 firm_fp_model = fp_model_strict;
1250 } else if (streq(orig_opt, "help")) {
1251 fprintf(stderr, "warning: -fhelp is deprecated\n");
1252 help |= HELP_OPTIMIZATION;
1254 /* -f options which have an -fno- variant */
1255 char const *opt = orig_opt;
1256 bool truth_value = true;
1257 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1258 truth_value = false;
1262 if (streq(opt, "diagnostics-show-option")) {
1263 diagnostics_show_option = truth_value;
1264 } else if (streq(opt, "dollars-in-identifiers")) {
1265 allow_dollar_in_symbol = truth_value;
1266 } else if (streq(opt, "omit-frame-pointer")) {
1267 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1268 } else if (streq(opt, "short-wchar")) {
1269 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1271 } else if (streq(opt, "show-column")) {
1272 show_column = truth_value;
1273 } else if (streq(opt, "signed-char")) {
1274 char_is_signed = truth_value;
1275 } else if (streq(opt, "strength-reduce")) {
1276 /* does nothing, for gcc compatibility (even gcc does
1277 * nothing for this switch anymore) */
1278 } else if (streq(opt, "syntax-only")) {
1279 mode = truth_value ? ParseOnly : CompileAssembleLink;
1280 } else if (streq(opt, "unsigned-char")) {
1281 char_is_signed = !truth_value;
1282 } else if (streq(opt, "freestanding")) {
1283 freestanding = truth_value;
1284 } else if (streq(opt, "hosted")) {
1285 freestanding = !truth_value;
1286 } else if (streq(opt, "profile-generate")) {
1287 profile_generate = truth_value;
1288 } else if (streq(opt, "profile-use")) {
1289 profile_use = truth_value;
1290 } else if (!truth_value &&
1291 streq(opt, "asynchronous-unwind-tables")) {
1292 /* nothing todo, a gcc feature which we do not support
1293 * anyway was deactivated */
1294 } else if (streq(opt, "verbose-asm")) {
1295 /* ignore: we always print verbose assembler */
1296 } else if (streq(opt, "jump-tables") ||
1297 streq(opt, "expensive-optimizations") ||
1298 streq(opt, "common") ||
1299 streq(opt, "optimize-sibling-calls") ||
1300 streq(opt, "align-loops") ||
1301 streq(opt, "align-jumps") ||
1302 streq(opt, "align-functions") ||
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 (asme 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 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
1436 int res = be_parse_arg(arch_opt);
1438 argument_errors = true;
1440 long int value = strtol(opt, NULL, 10);
1442 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1443 argument_errors = true;
1444 } else if (value != 16 && value != 32 && value != 64) {
1445 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1446 argument_errors = true;
1448 unsigned machine_size = (unsigned)value;
1449 /* TODO: choose/change backend based on this */
1450 add_flag(&cppflags_obst, "-m%u", machine_size);
1451 add_flag(&asflags_obst, "-m%u", machine_size);
1452 add_flag(&ldflags_obst, "-m%u", machine_size);
1455 } else if (streq(option, "pg")) {
1456 set_be_option("gprof");
1457 add_flag(&ldflags_obst, "-pg");
1458 } else if (streq(option, "pedantic") ||
1459 streq(option, "ansi")) {
1460 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1461 } else if (strstart(option, "std=")) {
1462 const char *const o = &option[4];
1464 streq(o, "c++") ? STANDARD_CXX98 :
1465 streq(o, "c++98") ? STANDARD_CXX98 :
1466 streq(o, "c89") ? STANDARD_C89 :
1467 streq(o, "c99") ? STANDARD_C99 :
1468 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1469 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1470 streq(o, "gnu89") ? STANDARD_GNU89 :
1471 streq(o, "gnu99") ? STANDARD_GNU99 :
1472 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1473 streq(o, "iso9899:1990") ? STANDARD_C89 :
1474 streq(o, "iso9899:199409") ? STANDARD_C90 :
1475 streq(o, "iso9899:1999") ? STANDARD_C99 :
1476 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1477 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1478 } else if (streq(option, "version")) {
1479 print_cparser_version();
1480 } else if (strstart(option, "print-file-name=")) {
1481 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1482 } else if (option[0] == '-') {
1483 /* double dash option */
1485 if (streq(option, "gcc")) {
1486 features_on |= _GNUC;
1487 features_off &= ~_GNUC;
1488 } else if (streq(option, "no-gcc")) {
1489 features_on &= ~_GNUC;
1490 features_off |= _GNUC;
1491 } else if (streq(option, "ms")) {
1493 features_off &= ~_MS;
1494 } else if (streq(option, "no-ms")) {
1495 features_on &= ~_MS;
1496 features_off |= _MS;
1497 } else if (streq(option, "strict")) {
1499 } else if (streq(option, "lextest")) {
1501 } else if (streq(option, "benchmark")) {
1502 mode = BenchmarkParser;
1503 } else if (streq(option, "print-ast")) {
1505 } else if (streq(option, "print-implicit-cast")) {
1506 print_implicit_casts = true;
1507 } else if (streq(option, "print-parenthesis")) {
1508 print_parenthesis = true;
1509 } else if (streq(option, "print-fluffy")) {
1511 } else if (streq(option, "print-jna")) {
1513 } else if (streq(option, "jna-limit")) {
1516 fprintf(stderr, "error: "
1517 "expected argument after '--jna-limit'\n");
1518 argument_errors = true;
1521 jna_limit_output(argv[i]);
1522 } else if (streq(option, "jna-libname")) {
1525 fprintf(stderr, "error: "
1526 "expected argument after '--jna-libname'\n");
1527 argument_errors = true;
1530 jna_set_libname(argv[i]);
1531 } else if (streq(option, "time")) {
1533 } else if (streq(option, "version")) {
1534 print_cparser_version();
1535 return EXIT_SUCCESS;
1536 } else if (streq(option, "help")) {
1538 } else if (streq(option, "help-parser")) {
1539 help |= HELP_PARSER;
1540 } else if (streq(option, "help-warnings")) {
1541 help |= HELP_WARNINGS;
1542 } else if (streq(option, "help-codegen")) {
1543 help |= HELP_CODEGEN;
1544 } else if (streq(option, "help-linker")) {
1545 help |= HELP_LINKER;
1546 } else if (streq(option, "help-optimization")) {
1547 help |= HELP_OPTIMIZATION;
1548 } else if (streq(option, "help-language-tools")) {
1549 help |= HELP_LANGUAGETOOLS;
1550 } else if (streq(option, "help-debug")) {
1552 } else if (streq(option, "help-firm")) {
1554 } else if (streq(option, "help-all")) {
1556 } else if (streq(option, "dump-function")) {
1559 fprintf(stderr, "error: "
1560 "expected argument after '--dump-function'\n");
1561 argument_errors = true;
1564 dumpfunction = argv[i];
1566 } else if (streq(option, "export-ir")) {
1567 mode = CompileExportIR;
1569 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1570 argument_errors = true;
1573 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1574 argument_errors = true;
1577 filetype_t type = forced_filetype;
1578 if (type == FILETYPE_AUTODETECT) {
1579 if (streq(arg, "-")) {
1580 /* - implicitly means C source file */
1583 const char *suffix = strrchr(arg, '.');
1584 /* Ensure there is at least one char before the suffix */
1585 if (suffix != NULL && suffix != arg) {
1588 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1589 streq(suffix, "a") ? FILETYPE_OBJECT :
1590 streq(suffix, "c") ? FILETYPE_C :
1591 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1592 streq(suffix, "C") ? FILETYPE_CXX :
1593 streq(suffix, "cc") ? FILETYPE_CXX :
1594 streq(suffix, "cp") ? FILETYPE_CXX :
1595 streq(suffix, "cpp") ? FILETYPE_CXX :
1596 streq(suffix, "CPP") ? FILETYPE_CXX :
1597 streq(suffix, "cxx") ? FILETYPE_CXX :
1598 streq(suffix, "c++") ? FILETYPE_CXX :
1599 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1600 streq(suffix, "h") ? FILETYPE_C :
1601 streq(suffix, "ir") ? FILETYPE_IR :
1602 streq(suffix, "o") ? FILETYPE_OBJECT :
1603 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1604 streq(suffix, "so") ? FILETYPE_OBJECT :
1605 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1610 file_list_entry_t *entry
1611 = obstack_alloc(&file_obst, sizeof(entry[0]));
1612 memset(entry, 0, sizeof(entry[0]));
1616 if (last_file != NULL) {
1617 last_file->next = entry;
1625 if (help != HELP_NONE) {
1626 print_help(argv[0], help);
1627 return !argument_errors;
1630 if (print_file_name_file != NULL) {
1631 print_file_name(print_file_name_file);
1632 return EXIT_SUCCESS;
1634 if (files == NULL) {
1635 fprintf(stderr, "error: no input files specified\n");
1636 argument_errors = true;
1639 if (argument_errors) {
1641 return EXIT_FAILURE;
1644 /* apply some effects from switches */
1645 c_mode |= features_on;
1646 c_mode &= ~features_off;
1647 if (profile_generate) {
1648 add_flag(&ldflags_obst, "-lfirmprof");
1649 set_be_option("profilegenerate");
1652 set_be_option("profileuse");
1656 init_symbol_table();
1657 init_types_and_adjust();
1660 if (wchar_atomic_kind == ATOMIC_TYPE_INT)
1661 init_wchar_types(type_int);
1662 else if (wchar_atomic_kind == ATOMIC_TYPE_SHORT)
1663 init_wchar_types(type_short);
1665 panic("unexpected wchar type");
1675 if (construct_dep_target) {
1676 if (outname != 0 && strlen(outname) >= 2) {
1677 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1679 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1682 dep_target[0] = '\0';
1685 char outnamebuf[4096];
1686 if (outname == NULL) {
1687 const char *filename = files->name;
1690 case BenchmarkParser:
1695 case PreprocessOnly:
1700 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1701 outname = outnamebuf;
1703 case CompileAssemble:
1704 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1705 outname = outnamebuf;
1708 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1710 outname = outnamebuf;
1712 case CompileExportIR:
1713 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1714 outname = outnamebuf;
1716 case CompileAssembleLink:
1726 assert(outname != NULL);
1729 if (streq(outname, "-")) {
1732 out = fopen(outname, "w");
1734 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
1736 return EXIT_FAILURE;
1740 file_list_entry_t *file;
1741 bool already_constructed_firm = false;
1742 for (file = files; file != NULL; file = file->next) {
1743 char asm_tempfile[1024];
1744 const char *filename = file->name;
1745 filetype_t filetype = file->type;
1747 if (filetype == FILETYPE_OBJECT)
1751 if (mode == LexTest) {
1753 in = open_file(filename);
1754 lextest(in, filename);
1756 return EXIT_SUCCESS;
1759 FILE *preprocessed_in = NULL;
1760 filetype_t next_filetype = filetype;
1763 next_filetype = FILETYPE_PREPROCESSED_C;
1766 next_filetype = FILETYPE_PREPROCESSED_CXX;
1768 case FILETYPE_ASSEMBLER:
1769 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1772 /* no support for input on FILE* yet */
1774 panic("internal compiler error: in for preprocessor != NULL");
1776 preprocessed_in = preprocess(filename, filetype);
1777 if (mode == PreprocessOnly) {
1778 copy_file(out, preprocessed_in);
1779 int pp_result = pclose(preprocessed_in);
1781 /* remove output file in case of error */
1782 if (out != stdout && pp_result != EXIT_SUCCESS) {
1788 in = preprocessed_in;
1789 filetype = next_filetype;
1797 if (mode == Compile) {
1800 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1804 in = open_file(filename);
1806 /* preprocess and compile */
1807 if (filetype == FILETYPE_PREPROCESSED_C) {
1808 char const* invalid_mode;
1811 case STANDARD_C89: c_mode = _C89; break;
1812 /* TODO determine difference between these two */
1813 case STANDARD_C90: c_mode = _C89; break;
1814 case STANDARD_C99: c_mode = _C89 | _C99; break;
1815 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1819 "warning: command line option \"-std=%s\" is not valid for C\n",
1822 case STANDARD_DEFAULT:
1823 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1825 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1826 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1829 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1830 char const* invalid_mode;
1832 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1833 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1834 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1835 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1836 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1839 case STANDARD_CXX98: c_mode = _CXX; break;
1843 "warning: command line option \"-std=%s\" is not valid for C++\n",
1845 case STANDARD_DEFAULT:
1846 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1850 c_mode |= features_on;
1851 c_mode &= ~features_off;
1853 /* do the actual parsing */
1854 ir_timer_t *t_parsing = ir_timer_new();
1855 timer_register(t_parsing, "Frontend: Parsing");
1856 timer_push(t_parsing);
1858 translation_unit_t *const unit = do_parsing(in, filename);
1859 timer_pop(t_parsing);
1861 /* prints the AST even if errors occurred */
1862 if (mode == PrintAst) {
1867 if (error_count > 0) {
1868 /* parsing failed because of errors */
1869 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1871 result = EXIT_FAILURE;
1873 } else if (warning_count > 0) {
1874 fprintf(stderr, "%u warning(s)\n", warning_count);
1877 if (in == preprocessed_in) {
1878 int pp_result = pclose(preprocessed_in);
1879 if (pp_result != EXIT_SUCCESS) {
1880 /* remove output file */
1883 return EXIT_FAILURE;
1887 if (mode == BenchmarkParser) {
1889 } else if (mode == PrintFluffy) {
1890 write_fluffy_decls(out, unit);
1892 } else if (mode == PrintJna) {
1893 write_jna_decls(out, unit);
1897 /* build the firm graph */
1898 ir_timer_t *t_construct = ir_timer_new();
1899 timer_register(t_construct, "Frontend: Graph construction");
1900 timer_push(t_construct);
1901 if (already_constructed_firm) {
1902 panic("compiling multiple files/translation units not possible");
1904 translation_unit_to_firm(unit);
1905 already_constructed_firm = true;
1906 timer_pop(t_construct);
1909 if (mode == ParseOnly) {
1913 if (mode == CompileDump) {
1915 ident *id = new_id_from_str(dumpfunction);
1916 ir_graph *irg = NULL;
1917 int n_irgs = get_irp_n_irgs();
1918 for (int i = 0; i < n_irgs; ++i) {
1919 ir_graph *tirg = get_irp_irg(i);
1920 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1928 fprintf(stderr, "No graph for function '%s' found\n",
1930 return EXIT_FAILURE;
1933 dump_ir_graph_file(out, irg);
1935 return EXIT_SUCCESS;
1938 if (mode == CompileExportIR) {
1941 return EXIT_SUCCESS;
1944 gen_firm_finish(asm_out, filename);
1945 if (asm_out != out) {
1948 } else if (filetype == FILETYPE_IR) {
1950 ir_import(filename);
1952 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1953 copy_file(asm_out, in);
1954 if (in == preprocessed_in) {
1955 int pp_result = pclose(preprocessed_in);
1956 if (pp_result != EXIT_SUCCESS) {
1957 /* remove output in error case */
1963 if (asm_out != out) {
1968 if (mode == Compile)
1971 /* if we're here then we have preprocessed assembly */
1972 filename = asm_tempfile;
1973 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1976 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1978 const char *filename_o;
1979 if (mode == CompileAssemble) {
1981 filename_o = outname;
1983 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1988 assemble(filename_o, filename);
1990 size_t len = strlen(filename_o) + 1;
1991 filename = obstack_copy(&file_obst, filename_o, len);
1992 filetype = FILETYPE_OBJECT;
1995 /* ok we're done here, process next file */
1996 file->name = filename;
1997 file->type = filetype;
2000 if (result != EXIT_SUCCESS) {
2006 /* link program file */
2007 if (mode == CompileAssembleLink) {
2008 obstack_1grow(&ldflags_obst, '\0');
2009 const char *flags = obstack_finish(&ldflags_obst);
2011 /* construct commandline */
2012 const char *linker = getenv("CPARSER_LINK");
2013 if (linker != NULL) {
2014 obstack_printf(&file_obst, "%s ", linker);
2016 if (target_triple != NULL)
2017 obstack_printf(&file_obst, "%s-", target_triple);
2018 obstack_printf(&file_obst, "%s ", LINKER);
2021 for (file_list_entry_t *entry = files; entry != NULL;
2022 entry = entry->next) {
2023 if (entry->type != FILETYPE_OBJECT)
2026 add_flag(&file_obst, "%s", entry->name);
2029 add_flag(&file_obst, "-o");
2030 add_flag(&file_obst, outname);
2031 obstack_printf(&file_obst, "%s", flags);
2032 obstack_1grow(&file_obst, '\0');
2034 char *commandline = obstack_finish(&file_obst);
2039 int err = system(commandline);
2040 if (err != EXIT_SUCCESS) {
2041 fprintf(stderr, "linker reported an error\n");
2042 return EXIT_FAILURE;
2049 obstack_free(&cppflags_obst, NULL);
2050 obstack_free(&ldflags_obst, NULL);
2051 obstack_free(&asflags_obst, NULL);
2052 obstack_free(&file_obst, NULL);
2062 exit_symbol_table();
2063 return EXIT_SUCCESS;