2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode) _access(fname, mode)
48 #define mktemp(tmpl) _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode) _fdopen(fd, mode)
51 #define popen(cmd, mode) _popen(cmd, mode)
52 #define pclose(file) _pclose(file)
53 #define unlink(filename) _unlink(filename)
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
66 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_timing.h"
74 #include "driver/firm_machine.h"
75 #include "adt/error.h"
76 #include "adt/strutil.h"
77 #include "wrappergen/write_fluffy.h"
78 #include "wrappergen/write_jna.h"
87 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__"
89 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
98 #define ASSEMBLER "gcc -c -xassembler"
101 unsigned int c_mode = _C89 | _ANSI | _C99 | _GNUC;
102 unsigned int machine_size = 32;
103 bool byte_order_big_endian = false;
104 bool char_is_signed = true;
105 bool strict_mode = false;
106 atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
107 unsigned long_double_size = 0;
108 bool enable_main_collect2_hack = false;
109 bool freestanding = false;
111 static machine_triple_t *target_machine;
112 static const char *target_triple;
114 static bool use_builtins;
115 static struct obstack cppflags_obst;
116 static struct obstack ldflags_obst;
117 static struct obstack asflags_obst;
118 static char dep_target[1024];
119 static const char *outname;
120 static bool define_intmax_types;
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 #include "gen_builtins.h"
184 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
189 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
193 lexer_open_stream(in, input_name);
196 translation_unit_t *unit = finish_parsing();
200 static void lextest(FILE *in, const char *fname)
202 lexer_open_stream(in, fname);
205 lexer_next_preprocessing_token();
206 print_token(stdout, &lexer_token);
208 } while (lexer_token.type != T_EOF);
211 static void add_flag(struct obstack *obst, const char *format, ...)
216 va_start(ap, format);
220 vsnprintf(buf, sizeof(buf), format, ap);
223 obstack_1grow(obst, ' ');
225 obstack_1grow(obst, '"');
226 obstack_grow(obst, buf, len);
227 obstack_1grow(obst, '"');
229 /* escape stuff... */
230 for (char *c = buf; *c != '\0'; ++c) {
248 obstack_1grow(obst, '\\');
251 obstack_1grow(obst, *c);
258 static const char *type_to_string(type_t *type)
260 assert(type->kind == TYPE_ATOMIC);
261 return get_atomic_kind_name(type->atomic.akind);
264 static FILE *preprocess(const char *fname, filetype_t filetype)
266 static const char *common_flags = NULL;
268 if (common_flags == NULL) {
269 obstack_1grow(&cppflags_obst, '\0');
270 const char *flags = obstack_finish(&cppflags_obst);
272 /* setup default defines */
273 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
274 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
275 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
276 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
278 add_flag(&cppflags_obst, "-U__VERSION__");
279 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
281 if (define_intmax_types) {
282 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
283 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
284 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
285 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
288 if (flags[0] != '\0') {
289 size_t len = strlen(flags);
290 obstack_1grow(&cppflags_obst, ' ');
291 obstack_grow(&cppflags_obst, flags, len);
293 obstack_1grow(&cppflags_obst, '\0');
294 common_flags = obstack_finish(&cppflags_obst);
297 assert(obstack_object_size(&cppflags_obst) == 0);
299 const char *preprocessor = getenv("CPARSER_PP");
300 if (preprocessor != NULL) {
301 obstack_printf(&cppflags_obst, "%s ", preprocessor);
303 if (target_triple != NULL)
304 obstack_printf(&cppflags_obst, "%s-", target_triple);
305 obstack_printf(&cppflags_obst, PREPROCESSOR);
310 add_flag(&cppflags_obst, "-std=c99");
313 add_flag(&cppflags_obst, "-std=c++98");
315 case FILETYPE_ASSEMBLER:
316 add_flag(&cppflags_obst, "-x");
317 add_flag(&cppflags_obst, "assembler-with-cpp");
322 obstack_printf(&cppflags_obst, "%s", common_flags);
324 /* handle dependency generation */
325 if (dep_target[0] != '\0') {
326 add_flag(&cppflags_obst, "-MF");
327 add_flag(&cppflags_obst, dep_target);
328 if (outname != NULL) {
329 add_flag(&cppflags_obst, "-MQ");
330 add_flag(&cppflags_obst, outname);
333 add_flag(&cppflags_obst, fname);
334 obstack_1grow(&cppflags_obst, '\0');
336 char *commandline = obstack_finish(&cppflags_obst);
340 FILE *f = popen(commandline, "r");
342 fprintf(stderr, "invoking preprocessor failed\n");
345 /* we do not really need that anymore */
346 obstack_free(&cppflags_obst, commandline);
351 static void assemble(const char *out, const char *in)
353 obstack_1grow(&asflags_obst, '\0');
354 const char *flags = obstack_finish(&asflags_obst);
356 const char *assembler = getenv("CPARSER_AS");
357 if (assembler != NULL) {
358 obstack_printf(&asflags_obst, "%s", assembler);
360 if (target_triple != NULL)
361 obstack_printf(&asflags_obst, "%s-", target_triple);
362 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
364 if (flags[0] != '\0')
365 obstack_printf(&asflags_obst, " %s", flags);
367 obstack_printf(&asflags_obst, " %s -o %s", in, out);
368 obstack_1grow(&asflags_obst, '\0');
370 char *commandline = obstack_finish(&asflags_obst);
374 int err = system(commandline);
375 if (err != EXIT_SUCCESS) {
376 fprintf(stderr, "assembler reported an error\n");
379 obstack_free(&asflags_obst, commandline);
382 static void print_file_name(const char *file)
384 add_flag(&ldflags_obst, "-print-file-name=%s", file);
386 obstack_1grow(&ldflags_obst, '\0');
387 const char *flags = obstack_finish(&ldflags_obst);
389 /* construct commandline */
390 const char *linker = getenv("CPARSER_LINK");
391 if (linker != NULL) {
392 obstack_printf(&ldflags_obst, "%s ", linker);
394 if (target_triple != NULL)
395 obstack_printf(&ldflags_obst, "%s-", target_triple);
396 obstack_printf(&ldflags_obst, "%s ", LINKER);
398 obstack_printf(&ldflags_obst, "%s ", linker);
399 obstack_printf(&ldflags_obst, "%s", flags);
400 obstack_1grow(&ldflags_obst, '\0');
402 char *commandline = obstack_finish(&ldflags_obst);
406 int err = system(commandline);
407 if (err != EXIT_SUCCESS) {
408 fprintf(stderr, "linker reported an error\n");
411 obstack_free(&ldflags_obst, commandline);
414 static const char *try_dir(const char *dir)
418 if (access(dir, R_OK | W_OK | X_OK) == 0)
423 static const char *get_tempdir(void)
425 static const char *tmpdir = NULL;
431 tmpdir = try_dir(getenv("TMPDIR"));
433 tmpdir = try_dir(getenv("TMP"));
435 tmpdir = try_dir(getenv("TEMP"));
439 tmpdir = try_dir(P_tmpdir);
443 tmpdir = try_dir("/var/tmp");
445 tmpdir = try_dir("/usr/tmp");
447 tmpdir = try_dir("/tmp");
456 /* cheap and nasty mkstemp replacement */
457 static int mkstemp(char *templ)
460 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
465 * an own version of tmpnam, which: writes in a buffer, emits no warnings
466 * during linking (like glibc/gnu ld do for tmpnam)...
468 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
470 const char *tempdir = get_tempdir();
472 snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
474 int fd = mkstemp(buffer);
476 fprintf(stderr, "could not create temporary file: %s\n",
480 FILE *out = fdopen(fd, "w");
482 fprintf(stderr, "could not create temporary file FILE*\n");
486 file_list_entry_t *entry = xmalloc(sizeof(*entry));
487 memset(entry, 0, sizeof(*entry));
489 size_t name_len = strlen(buffer) + 1;
490 char *name = malloc(name_len);
491 memcpy(name, buffer, name_len);
494 entry->next = temp_files;
500 static void free_temp_files(void)
502 file_list_entry_t *entry = temp_files;
503 file_list_entry_t *next;
504 for ( ; entry != NULL; entry = next) {
508 free((char*) entry->name);
514 typedef enum compile_mode_t {
529 static void usage(const char *argv0)
531 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
534 static void print_cparser_version(void)
536 printf("cparser (%s) using libFirm (%u.%u",
537 cparser_REVISION, ir_get_version_major(),
538 ir_get_version_minor());
540 const char *revision = ir_get_version_revision();
541 if (revision[0] != 0) {
543 fputs(revision, stdout);
546 const char *build = ir_get_version_build();
549 fputs(build, stdout);
552 puts("This is free software; see the source for copying conditions. There is NO\n"
553 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
556 static void print_help_basic(const char *argv0)
560 put_help("--help", "Display this information");
561 put_help("--version", "Display compiler version");
562 put_help("--help-parser", "Display information about parser options");
563 put_help("--help-warnings", "Display information about warning options");
564 put_help("--help-codegen", "Display information about code-generation options");
565 put_help("--help-optimization", "Display information about optimization options");
566 put_help("--help-linker", "Display information about linker options");
567 put_help("--help-language-tools", "Display information about language tools options");
568 put_help("--help-debug", "Display information about compiler debugging options");
569 put_help("--help-firm", "Display information about direct firm options");
570 put_help("--help-all", "Display information about all options");
571 put_help("-c", "Compile and assemble but do not link");
572 put_help("-E", "Preprocess only");
573 put_help("-S", "Compile but do not assembler or link");
574 put_help("-o", "Specify output file");
575 put_help("-v", "Verbose output (show invocation of sub-processes)");
576 put_help("-x", "Force input language:");
577 put_choice("c", "C");
578 put_choice("c++", "C++");
579 put_choice("assembler", "Assembler (no preprocessing)");
580 put_choice("assembler-with-cpp", "Assembler with preprocessing");
581 put_choice("none", "Autodetection");
582 put_help("-pipe", "Ignored (gcc compatibility)");
585 static void print_help_preprocessor(void)
587 put_help("-nostdinc", "Do not search standard system include directories");
588 put_help("-trigraphs", "Support ISO C trigraphs");
589 put_help("-isystem", "");
590 put_help("-include", "");
591 put_help("-I PATH", "");
592 put_help("-D SYMBOL[=value]", "");
593 put_help("-U SYMBOL", "");
594 put_help("-Wp,OPTION", "Pass option directly to preprocessor");
597 put_help("-MMD", "");
605 static void print_help_parser(void)
607 put_help("-finput-charset=CHARSET", "Select encoding of input files");
608 put_help("-fmessage-length=LEN", "Ignored (gcc compatibility)");
609 put_help("-fshort-wchar", "Type \"wchar_t\" is unsigned short instead of int");
610 put_help("-fshow-column", "Show the column number in diagnostic messages");
611 put_help("-fsigned-char", "Type \"char\" is a signed type");
612 put_help("-funsigned-char", "Type \"char\" is an unsigned type");
613 put_help("--ms", "Enable msvc extensions");
614 put_help("--no-ms", "Disable msvc extensions");
615 put_help("--gcc", "Enable gcc extensions");
616 put_help("--no-gcc", "Disable gcc extensions");
617 put_help("-std=STANDARD", "Specify language standard:");
618 put_choice("c99", "ISO C99 standard");
619 put_choice("c89", "ISO C89 standard");
620 put_choice("c9x", "Deprecated");
621 put_choice("c++", "ISO C++ 98");
622 put_choice("c++98", "ISO C++ 98");
623 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
624 put_choice("gnu89", "ISO C89 + GNU extensions");
625 put_choice("gnu9x", "Deprecated");
626 put_choice("iso9899:1990", "ISO C89");
627 put_choice("iso9899:199409", "ISO C90");
628 put_choice("iso9899:1999", "ISO C99");
629 put_choice("iso9899:199x", "Deprecated");
630 put_help("-pedantic", "Ignored (gcc compatibility)");
631 put_help("-ansi", "Ignored (gcc compatibility)");
632 put_help("--strict", "Enable strict conformance checking");
635 static void print_help_warnings(void)
637 put_help("-f[no-]diagnostics-show-option", "Show the switch, which controls a warning, after each warning");
638 put_help("-w", "Disable all warnings");
639 put_help("-Wno-trigraphs", "Warn if input contains trigraphs");
640 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
641 put_help("-Winit-self", "Ignored (gcc compatibility)");
642 print_warning_opt_help();
645 static void print_help_optimization(void)
647 put_help("-O LEVEL", "Select optimization level (0-4)");
648 firm_option_help(put_help);
649 put_help("-fexpensive-optimizations","Ignored (gcc compatibility)");
652 static void print_help_codegeneration(void)
654 put_help("-g", "Generate debug information");
655 put_help("-pg", "Instrument code for gnu gprof");
656 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
657 put_help("-ffreestanding", "Compile in freestanding mode (see ISO C standard)");
658 put_help("-fhosted", "Compile in hosted (not freestanding) mode");
659 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
660 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
661 put_help("-ffp-precise", "Precise floating point model");
662 put_help("-ffp-fast", "Imprecise floating point model");
663 put_help("-ffp-strict", "Strict floating point model");
664 put_help("-pthread", "Use pthread threading library");
665 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
666 put_help("-mtriple=TARGET", "Alias for -mtarget (clang compatibility)");
667 put_help("-march=ARCH", "");
668 put_help("-mtune=ARCH", "");
669 put_help("-mcpu=CPU", "");
670 put_help("-mfpmath=", "");
671 put_help("-mpreferred-stack-boundary=", "");
672 put_help("-mrtd", "");
673 put_help("-mregparm=", "Not supported yet");
674 put_help("-msoft-float", "Not supported yet");
675 put_help("-m32", "Generate 32bit code");
676 put_help("-m64", "Generate 64bit code");
677 put_help("-fverbose-asm", "Ignored (gcc compatibility)");
678 put_help("-fjump-tables", "Ignored (gcc compatibility)");
679 put_help("-fcommon", "Ignored (gcc compatibility)");
680 put_help("-foptimize-sibling-calls", "Ignored (gcc compatibility)");
681 put_help("-falign-loops", "Ignored (gcc compatibility)");
682 put_help("-falign-jumps", "Ignored (gcc compatibility)");
683 put_help("-falign-functions", "Ignored (gcc compatibility)");
684 put_help("-fPIC", "Ignored (gcc compatibility)");
685 put_help("-ffast-math", "Same as -ffp-fast (gcc compatibility)");
687 puts("\tMost of these options can be used with a no- prefix to disable them");
688 puts("\te.g. -fno-omit-frame-pointer");
691 static void print_help_linker(void)
693 put_help("-l LIBRARY", "");
694 put_help("-L PATH", "");
695 put_help("-shared", "Produce a shared library");
696 put_help("-static", "Produce statically linked binary");
697 put_help("-Wl,OPTION", "Pass option directly to linker");
700 static void print_help_debug(void)
702 put_help("--lextest", "Preprocess and tokenize only");
703 put_help("--print-ast", "Preprocess, parse and print AST");
704 put_help("--print-implicit-cast", "");
705 put_help("--print-parenthesis", "");
706 put_help("--benchmark", "Preprocess and parse, produces no output");
707 put_help("--time", "Measure time of compiler passes");
708 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
709 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
712 static void print_help_language_tools(void)
714 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
715 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
716 put_help("--jna-limit filename", "");
717 put_help("--jna-libname name", "");
720 static void print_help_firm(void)
722 put_help("-bOPTION", "Directly pass option to libFirm backend");
723 int res = be_parse_arg("help");
730 HELP_BASIC = 1u << 0,
731 HELP_PREPROCESSOR = 1u << 1,
732 HELP_PARSER = 1u << 2,
733 HELP_WARNINGS = 1u << 3,
734 HELP_OPTIMIZATION = 1u << 4,
735 HELP_CODEGEN = 1u << 5,
736 HELP_LINKER = 1u << 6,
737 HELP_LANGUAGETOOLS = 1u << 7,
738 HELP_DEBUG = 1u << 8,
741 HELP_ALL = (unsigned)-1
744 static void print_help(const char *argv0, help_sections_t sections)
746 if (sections & HELP_BASIC) print_help_basic(argv0);
747 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
748 if (sections & HELP_PARSER) print_help_parser();
749 if (sections & HELP_WARNINGS) print_help_warnings();
750 if (sections & HELP_OPTIMIZATION) print_help_optimization();
751 if (sections & HELP_CODEGEN) print_help_codegeneration();
752 if (sections & HELP_LINKER) print_help_linker();
753 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
754 if (sections & HELP_DEBUG) print_help_debug();
755 if (sections & HELP_FIRM) print_help_firm();
758 static void set_be_option(const char *arg)
760 int res = be_parse_arg(arg);
765 static void copy_file(FILE *dest, FILE *input)
769 while (!feof(input) && !ferror(dest)) {
770 size_t read = fread(buf, 1, sizeof(buf), input);
771 if (fwrite(buf, 1, read, dest) != read) {
772 perror("could not write output");
777 static FILE *open_file(const char *filename)
779 if (streq(filename, "-")) {
783 FILE *in = fopen(filename, "r");
785 fprintf(stderr, "Could not open '%s': %s\n", filename,
793 static filetype_t get_filetype_from_string(const char *string)
795 if (streq(string, "c") || streq(string, "c-header"))
797 if (streq(string, "c++") || streq(string, "c++-header"))
799 if (streq(string, "assembler"))
800 return FILETYPE_PREPROCESSED_ASSEMBLER;
801 if (streq(string, "assembler-with-cpp"))
802 return FILETYPE_ASSEMBLER;
803 if (streq(string, "none"))
804 return FILETYPE_AUTODETECT;
806 return FILETYPE_UNKNOWN;
809 static bool init_os_support(void)
811 const char *os = target_machine->operating_system;
812 wchar_atomic_kind = ATOMIC_TYPE_INT;
813 enable_main_collect2_hack = false;
814 define_intmax_types = false;
816 if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
817 || streq(os, "solaris")) {
818 set_create_ld_ident(create_name_linux_elf);
819 } else if (streq(os, "darwin")) {
820 long_double_size = 16;
821 set_create_ld_ident(create_name_macho);
822 define_intmax_types = true;
823 } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
824 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
825 enable_main_collect2_hack = true;
826 set_create_ld_ident(create_name_win32);
834 static bool parse_target_triple(const char *arg)
836 machine_triple_t *triple = firm_parse_machine_triple(arg);
837 if (triple == NULL) {
838 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
841 target_machine = triple;
845 static void setup_target_machine(void)
847 if (!setup_firm_for_machine(target_machine))
850 const backend_params *be_params = be_get_backend_param();
851 if (be_params->long_double_size % 8 != 0) {
852 fprintf(stderr, "firm-target long double size is not a multiple of 8, cannot handle this\n");
856 byte_order_big_endian = be_params->byte_order_big_endian;
857 machine_size = be_params->machine_size;
858 long_double_size = be_params->long_double_size / 8;
863 int main(int argc, char **argv)
867 const char *dumpfunction = NULL;
868 const char *print_file_name_file = NULL;
869 compile_mode_t mode = CompileAssembleLink;
871 int result = EXIT_SUCCESS;
872 char cpu_arch[16] = "ia32";
873 file_list_entry_t *files = NULL;
874 file_list_entry_t *last_file = NULL;
875 bool construct_dep_target = false;
876 bool do_timing = false;
877 bool profile_generate = false;
878 bool profile_use = false;
879 struct obstack file_obst;
881 atexit(free_temp_files);
883 /* hack for now... */
884 if (strstr(argv[0], "pptest") != NULL) {
885 extern int pptest_main(int argc, char **argv);
886 return pptest_main(argc, argv);
889 obstack_init(&cppflags_obst);
890 obstack_init(&ldflags_obst);
891 obstack_init(&asflags_obst);
892 obstack_init(&file_obst);
894 #define GET_ARG_AFTER(def, args) \
896 def = &arg[sizeof(args)-1]; \
897 if (def[0] == '\0') { \
900 fprintf(stderr, "error: expected argument after '" args "'\n"); \
901 argument_errors = true; \
905 if (def[0] == '-' && def[1] != '\0') { \
906 fprintf(stderr, "error: expected argument after '" args "'\n"); \
907 argument_errors = true; \
913 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
915 /* early options parsing (find out optimization level and OS) */
916 for (int i = 1; i < argc; ++i) {
917 const char *arg = argv[i];
921 const char *option = &arg[1];
922 if (option[0] == 'O') {
923 sscanf(&option[1], "%d", &opt_level);
927 const char *target = getenv("TARGET");
929 parse_target_triple(target);
930 if (target_machine == NULL) {
931 target_machine = firm_get_host_machine();
933 choose_optimization_pack(opt_level);
934 setup_target_machine();
936 /* parse rest of options */
937 standard = STANDARD_DEFAULT;
938 unsigned features_on = 0;
939 unsigned features_off = 0;
940 filetype_t forced_filetype = FILETYPE_AUTODETECT;
941 help_sections_t help = HELP_NONE;
942 bool argument_errors = false;
943 for (int i = 1; i < argc; ++i) {
944 const char *arg = argv[i];
945 if (arg[0] == '-' && arg[1] != '\0') {
947 const char *option = &arg[1];
948 if (option[0] == 'o') {
949 GET_ARG_AFTER(outname, "-o");
950 } else if (option[0] == 'g') {
951 set_be_option("debuginfo=stabs");
952 set_be_option("omitfp=no");
953 set_be_option("ia32-nooptcc=yes");
954 } else if (SINGLE_OPTION('c')) {
955 mode = CompileAssemble;
956 } else if (SINGLE_OPTION('E')) {
957 mode = PreprocessOnly;
958 } else if (SINGLE_OPTION('S')) {
960 } else if (option[0] == 'O') {
962 } else if (option[0] == 'I') {
964 GET_ARG_AFTER(opt, "-I");
965 add_flag(&cppflags_obst, "-I%s", opt);
966 } else if (option[0] == 'D') {
968 GET_ARG_AFTER(opt, "-D");
969 add_flag(&cppflags_obst, "-D%s", opt);
970 } else if (option[0] == 'U') {
972 GET_ARG_AFTER(opt, "-U");
973 add_flag(&cppflags_obst, "-U%s", opt);
974 } else if (option[0] == 'l') {
976 GET_ARG_AFTER(opt, "-l");
977 add_flag(&ldflags_obst, "-l%s", opt);
978 } else if (option[0] == 'L') {
980 GET_ARG_AFTER(opt, "-L");
981 add_flag(&ldflags_obst, "-L%s", opt);
982 } else if (SINGLE_OPTION('v')) {
984 } else if (SINGLE_OPTION('w')) {
985 disable_all_warnings();
986 } else if (option[0] == 'x') {
988 GET_ARG_AFTER(opt, "-x");
989 forced_filetype = get_filetype_from_string(opt);
990 if (forced_filetype == FILETYPE_UNKNOWN) {
991 fprintf(stderr, "Unknown language '%s'\n", opt);
992 argument_errors = true;
994 } else if (streq(option, "M")) {
995 mode = PreprocessOnly;
996 add_flag(&cppflags_obst, "-M");
997 } else if (streq(option, "MMD") ||
998 streq(option, "MD")) {
999 construct_dep_target = true;
1000 add_flag(&cppflags_obst, "-%s", option);
1001 } else if (streq(option, "MM") ||
1002 streq(option, "MP")) {
1003 add_flag(&cppflags_obst, "-%s", option);
1004 } else if (streq(option, "MT") ||
1005 streq(option, "MQ") ||
1006 streq(option, "MF")) {
1008 GET_ARG_AFTER(opt, "-MT");
1009 add_flag(&cppflags_obst, "-%s", option);
1010 add_flag(&cppflags_obst, "%s", opt);
1011 } else if (streq(option, "include")) {
1013 GET_ARG_AFTER(opt, "-include");
1014 add_flag(&cppflags_obst, "-include");
1015 add_flag(&cppflags_obst, "%s", opt);
1016 } else if (streq(option, "isystem")) {
1018 GET_ARG_AFTER(opt, "-isystem");
1019 add_flag(&cppflags_obst, "-isystem");
1020 add_flag(&cppflags_obst, "%s", opt);
1021 #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
1022 } else if (streq(option, "pthread")) {
1023 /* set flags for the preprocessor */
1024 add_flag(&cppflags_obst, "-D_REENTRANT");
1025 /* set flags for the linker */
1026 add_flag(&ldflags_obst, "-lpthread");
1028 } else if (streq(option, "nostdinc")
1029 || streq(option, "trigraphs")) {
1030 /* pass these through to the preprocessor */
1031 add_flag(&cppflags_obst, "%s", arg);
1032 } else if (streq(option, "pipe")) {
1033 /* here for gcc compatibility */
1034 } else if (streq(option, "static")) {
1035 add_flag(&ldflags_obst, "-static");
1036 } else if (streq(option, "shared")) {
1037 add_flag(&ldflags_obst, "-shared");
1038 } else if (option[0] == 'f') {
1039 char const *orig_opt;
1040 GET_ARG_AFTER(orig_opt, "-f");
1042 if (strstart(orig_opt, "input-charset=")) {
1043 char const* const encoding = strchr(orig_opt, '=') + 1;
1044 select_input_encoding(encoding);
1045 } else if (strstart(orig_opt, "align-loops=") ||
1046 strstart(orig_opt, "align-jumps=") ||
1047 strstart(orig_opt, "align-functions=")) {
1048 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1049 } else if (strstart(orig_opt, "visibility=")) {
1050 const char *val = strchr(orig_opt, '=')+1;
1051 elf_visibility_tag_t visibility
1052 = get_elf_visibility_from_string(val);
1053 if (visibility == ELF_VISIBILITY_ERROR) {
1054 fprintf(stderr, "invalid visibility '%s' specified\n",
1056 argument_errors = true;
1058 set_default_visibility(visibility);
1060 } else if (strstart(orig_opt, "message-length=")) {
1061 /* ignore: would only affect error message format */
1062 } else if (streq(orig_opt, "fast-math") ||
1063 streq(orig_opt, "fp-fast")) {
1064 firm_fp_model = fp_model_fast;
1065 } else if (streq(orig_opt, "fp-precise")) {
1066 firm_fp_model = fp_model_precise;
1067 } else if (streq(orig_opt, "fp-strict")) {
1068 firm_fp_model = fp_model_strict;
1069 } else if (streq(orig_opt, "help")) {
1070 fprintf(stderr, "warning: -fhelp is deprecated\n");
1071 help |= HELP_OPTIMIZATION;
1073 /* -f options which have an -fno- variant */
1074 char const *opt = orig_opt;
1075 bool truth_value = true;
1076 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1077 truth_value = false;
1081 if (streq(opt, "builtins")) {
1082 use_builtins = truth_value;
1083 } else if (streq(opt, "diagnostics-show-option")) {
1084 diagnostics_show_option = truth_value;
1085 } else if (streq(opt, "dollars-in-identifiers")) {
1086 allow_dollar_in_symbol = truth_value;
1087 } else if (streq(opt, "omit-frame-pointer")) {
1088 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1089 } else if (streq(opt, "short-wchar")) {
1090 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1092 } else if (streq(opt, "show-column")) {
1093 show_column = truth_value;
1094 } else if (streq(opt, "signed-char")) {
1095 char_is_signed = truth_value;
1096 } else if (streq(opt, "strength-reduce")) {
1097 /* does nothing, for gcc compatibility (even gcc does
1098 * nothing for this switch anymore) */
1099 } else if (streq(opt, "syntax-only")) {
1100 mode = truth_value ? ParseOnly : CompileAssembleLink;
1101 } else if (streq(opt, "unsigned-char")) {
1102 char_is_signed = !truth_value;
1103 } else if (streq(opt, "freestanding")) {
1104 freestanding = truth_value;
1105 } else if (streq(opt, "hosted")) {
1106 freestanding = !truth_value;
1107 } else if (streq(opt, "profile-generate")) {
1108 profile_generate = truth_value;
1109 } else if (streq(opt, "profile-use")) {
1110 profile_use = truth_value;
1111 } else if (!truth_value &&
1112 streq(opt, "asynchronous-unwind-tables")) {
1113 /* nothing todo, a gcc feature which we do not support
1114 * anyway was deactivated */
1115 } else if (streq(opt, "verbose-asm")) {
1116 /* ignore: we always print verbose assembler */
1117 } else if (streq(opt, "jump-tables") ||
1118 streq(opt, "expensive-optimizations") ||
1119 streq(opt, "common") ||
1120 streq(opt, "optimize-sibling-calls") ||
1121 streq(opt, "align-loops") ||
1122 streq(opt, "align-jumps") ||
1123 streq(opt, "align-functions") ||
1124 streq(opt, "PIC")) {
1125 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1127 int res = firm_option(orig_opt);
1129 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1131 argument_errors = true;
1136 } else if (option[0] == 'b') {
1138 GET_ARG_AFTER(opt, "-b");
1140 if (streq(opt, "help")) {
1141 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1144 int res = be_parse_arg(opt);
1146 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1148 argument_errors = true;
1149 } else if (strstart(opt, "isa=")) {
1150 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1153 } else if (option[0] == 'W') {
1154 if (strstart(option + 1, "p,")) {
1155 // pass options directly to the preprocessor
1157 GET_ARG_AFTER(opt, "-Wp,");
1158 add_flag(&cppflags_obst, "-Wp,%s", opt);
1159 } else if (strstart(option + 1, "l,")) {
1160 // pass options directly to the linker
1162 GET_ARG_AFTER(opt, "-Wl,");
1163 add_flag(&ldflags_obst, "-Wl,%s", opt);
1164 } else if (streq(option + 1, "no-trigraphs")
1165 || streq(option + 1, "undef")) {
1166 add_flag(&cppflags_obst, "%s", arg);
1167 } else if (streq(option+1, "init-self")) {
1168 /* ignored (gcc compatibility) */
1170 set_warning_opt(&option[1]);
1172 } else if (option[0] == 'm') {
1177 GET_ARG_AFTER(opt, "-m");
1178 if (strstart(opt, "target=")) {
1179 GET_ARG_AFTER(opt, "-mtarget=");
1180 if (!parse_target_triple(opt)) {
1181 argument_errors = true;
1183 setup_target_machine();
1184 target_triple = opt;
1186 } else if (strstart(opt, "triple=")) {
1187 GET_ARG_AFTER(opt, "-mtriple=");
1188 if (!parse_target_triple(opt)) {
1189 argument_errors = true;
1191 setup_target_machine();
1192 target_triple = opt;
1194 } else if (strstart(opt, "arch=")) {
1195 GET_ARG_AFTER(opt, "-march=");
1196 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1197 int res = be_parse_arg(arch_opt);
1198 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1199 res &= be_parse_arg(arch_opt);
1202 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1203 argument_errors = true;
1205 } else if (strstart(opt, "tune=")) {
1206 GET_ARG_AFTER(opt, "-mtune=");
1207 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1208 int res = be_parse_arg(arch_opt);
1210 argument_errors = true;
1211 } else if (strstart(opt, "cpu=")) {
1212 GET_ARG_AFTER(opt, "-mcpu=");
1213 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1214 int res = be_parse_arg(arch_opt);
1216 argument_errors = true;
1217 } else if (strstart(opt, "fpmath=")) {
1218 GET_ARG_AFTER(opt, "-mfpmath=");
1219 if (streq(opt, "387"))
1221 else if (streq(opt, "sse"))
1224 fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
1225 argument_errors = true;
1227 if (!argument_errors) {
1228 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1229 int res = be_parse_arg(arch_opt);
1231 argument_errors = true;
1233 } else if (strstart(opt, "preferred-stack-boundary=")) {
1234 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1235 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1236 int res = be_parse_arg(arch_opt);
1238 argument_errors = true;
1239 } else if (streq(opt, "rtd")) {
1240 default_calling_convention = CC_STDCALL;
1241 } else if (strstart(opt, "regparm=")) {
1242 fprintf(stderr, "error: regparm convention not supported yet\n");
1243 argument_errors = true;
1244 } else if (streq(opt, "soft-float")) {
1245 fprintf(stderr, "error: software floatingpoint not supported yet\n");
1246 argument_errors = true;
1248 long int value = strtol(opt, NULL, 10);
1250 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1251 argument_errors = true;
1252 } else if (value != 16 && value != 32 && value != 64) {
1253 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1254 argument_errors = true;
1256 add_flag(&cppflags_obst, "-m%u", machine_size);
1257 add_flag(&asflags_obst, "-m%u", machine_size);
1258 add_flag(&ldflags_obst, "-m%u", machine_size);
1259 /* TODO: choose/change backend based on this */
1262 } else if (streq(option, "pg")) {
1263 set_be_option("gprof");
1264 add_flag(&ldflags_obst, "-pg");
1265 } else if (streq(option, "pedantic") ||
1266 streq(option, "ansi")) {
1267 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1268 } else if (strstart(option, "std=")) {
1269 const char *const o = &option[4];
1271 streq(o, "c++") ? STANDARD_CXX98 :
1272 streq(o, "c++98") ? STANDARD_CXX98 :
1273 streq(o, "c89") ? STANDARD_C89 :
1274 streq(o, "c99") ? STANDARD_C99 :
1275 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1276 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1277 streq(o, "gnu89") ? STANDARD_GNU89 :
1278 streq(o, "gnu99") ? STANDARD_GNU99 :
1279 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1280 streq(o, "iso9899:1990") ? STANDARD_C89 :
1281 streq(o, "iso9899:199409") ? STANDARD_C90 :
1282 streq(o, "iso9899:1999") ? STANDARD_C99 :
1283 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1284 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1285 } else if (streq(option, "version")) {
1286 print_cparser_version();
1287 } else if (strstart(option, "print-file-name=")) {
1288 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1289 } else if (option[0] == '-') {
1290 /* double dash option */
1292 if (streq(option, "gcc")) {
1293 features_on |= _GNUC;
1294 features_off &= ~_GNUC;
1295 } else if (streq(option, "no-gcc")) {
1296 features_on &= ~_GNUC;
1297 features_off |= _GNUC;
1298 } else if (streq(option, "ms")) {
1300 features_off &= ~_MS;
1301 } else if (streq(option, "no-ms")) {
1302 features_on &= ~_MS;
1303 features_off |= _MS;
1304 } else if (streq(option, "strict")) {
1306 } else if (streq(option, "lextest")) {
1308 } else if (streq(option, "benchmark")) {
1309 mode = BenchmarkParser;
1310 } else if (streq(option, "print-ast")) {
1312 } else if (streq(option, "print-implicit-cast")) {
1313 print_implicit_casts = true;
1314 } else if (streq(option, "print-parenthesis")) {
1315 print_parenthesis = true;
1316 } else if (streq(option, "print-fluffy")) {
1318 } else if (streq(option, "print-jna")) {
1320 } else if (streq(option, "jna-limit")) {
1323 fprintf(stderr, "error: "
1324 "expected argument after '--jna-limit'\n");
1325 argument_errors = true;
1328 jna_limit_output(argv[i]);
1329 } else if (streq(option, "jna-libname")) {
1332 fprintf(stderr, "error: "
1333 "expected argument after '--jna-libname'\n");
1334 argument_errors = true;
1337 jna_set_libname(argv[i]);
1338 } else if (streq(option, "time")) {
1340 } else if (streq(option, "version")) {
1341 print_cparser_version();
1342 return EXIT_SUCCESS;
1343 } else if (streq(option, "help")) {
1345 } else if (streq(option, "help-parser")) {
1346 help |= HELP_PARSER;
1347 } else if (streq(option, "help-warnings")) {
1348 help |= HELP_WARNINGS;
1349 } else if (streq(option, "help-codegen")) {
1350 help |= HELP_CODEGEN;
1351 } else if (streq(option, "help-linker")) {
1352 help |= HELP_LINKER;
1353 } else if (streq(option, "help-optimization")) {
1354 help |= HELP_OPTIMIZATION;
1355 } else if (streq(option, "help-language-tools")) {
1356 help |= HELP_LANGUAGETOOLS;
1357 } else if (streq(option, "help-debug")) {
1359 } else if (streq(option, "help-firm")) {
1361 } else if (streq(option, "help-all")) {
1363 } else if (streq(option, "dump-function")) {
1366 fprintf(stderr, "error: "
1367 "expected argument after '--dump-function'\n");
1368 argument_errors = true;
1371 dumpfunction = argv[i];
1373 } else if (streq(option, "export-ir")) {
1374 mode = CompileExportIR;
1376 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1377 argument_errors = true;
1380 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1381 argument_errors = true;
1384 filetype_t type = forced_filetype;
1385 if (type == FILETYPE_AUTODETECT) {
1386 if (streq(arg, "-")) {
1387 /* - implicitly means C source file */
1390 const char *suffix = strrchr(arg, '.');
1391 /* Ensure there is at least one char before the suffix */
1392 if (suffix != NULL && suffix != arg) {
1395 streq(suffix, "S") ? FILETYPE_ASSEMBLER :
1396 streq(suffix, "a") ? FILETYPE_OBJECT :
1397 streq(suffix, "c") ? FILETYPE_C :
1398 streq(suffix, "i") ? FILETYPE_PREPROCESSED_C :
1399 streq(suffix, "C") ? FILETYPE_CXX :
1400 streq(suffix, "cc") ? FILETYPE_CXX :
1401 streq(suffix, "cp") ? FILETYPE_CXX :
1402 streq(suffix, "cpp") ? FILETYPE_CXX :
1403 streq(suffix, "CPP") ? FILETYPE_CXX :
1404 streq(suffix, "cxx") ? FILETYPE_CXX :
1405 streq(suffix, "c++") ? FILETYPE_CXX :
1406 streq(suffix, "ii") ? FILETYPE_PREPROCESSED_CXX :
1407 streq(suffix, "h") ? FILETYPE_C :
1408 streq(suffix, "ir") ? FILETYPE_IR :
1409 streq(suffix, "o") ? FILETYPE_OBJECT :
1410 streq(suffix, "s") ? FILETYPE_PREPROCESSED_ASSEMBLER :
1411 streq(suffix, "so") ? FILETYPE_OBJECT :
1412 FILETYPE_OBJECT; /* gcc behavior: unknown file extension means object file */
1417 file_list_entry_t *entry
1418 = obstack_alloc(&file_obst, sizeof(entry[0]));
1419 memset(entry, 0, sizeof(entry[0]));
1423 if (last_file != NULL) {
1424 last_file->next = entry;
1432 if (help != HELP_NONE) {
1433 print_help(argv[0], help);
1434 return !argument_errors;
1437 if (print_file_name_file != NULL) {
1438 print_file_name(print_file_name_file);
1439 return EXIT_SUCCESS;
1441 if (files == NULL) {
1442 fprintf(stderr, "error: no input files specified\n");
1443 argument_errors = true;
1446 if (argument_errors) {
1448 return EXIT_FAILURE;
1451 /* apply some effects from switches */
1452 c_mode |= features_on;
1453 c_mode &= ~features_off;
1454 if (profile_generate) {
1455 add_flag(&ldflags_obst, "-lfirmprof");
1456 set_be_option("profilegenerate");
1459 set_be_option("profileuse");
1463 init_symbol_table();
1476 if (construct_dep_target) {
1477 if (outname != 0 && strlen(outname) >= 2) {
1478 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1480 get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1483 dep_target[0] = '\0';
1486 char outnamebuf[4096];
1487 if (outname == NULL) {
1488 const char *filename = files->name;
1491 case BenchmarkParser:
1496 case PreprocessOnly:
1501 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1502 outname = outnamebuf;
1504 case CompileAssemble:
1505 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1506 outname = outnamebuf;
1509 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1511 outname = outnamebuf;
1513 case CompileExportIR:
1514 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
1515 outname = outnamebuf;
1517 case CompileAssembleLink:
1527 assert(outname != NULL);
1530 if (streq(outname, "-")) {
1533 out = fopen(outname, "w");
1535 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
1537 return EXIT_FAILURE;
1541 file_list_entry_t *file;
1542 bool already_constructed_firm = false;
1543 for (file = files; file != NULL; file = file->next) {
1544 char asm_tempfile[1024];
1545 const char *filename = file->name;
1546 filetype_t filetype = file->type;
1548 if (filetype == FILETYPE_OBJECT)
1552 if (mode == LexTest) {
1554 in = open_file(filename);
1555 lextest(in, filename);
1557 return EXIT_SUCCESS;
1560 FILE *preprocessed_in = NULL;
1561 filetype_t next_filetype = filetype;
1564 next_filetype = FILETYPE_PREPROCESSED_C;
1567 next_filetype = FILETYPE_PREPROCESSED_CXX;
1569 case FILETYPE_ASSEMBLER:
1570 next_filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1573 /* no support for input on FILE* yet */
1575 panic("internal compiler error: in for preprocessor != NULL");
1577 preprocessed_in = preprocess(filename, filetype);
1578 if (mode == PreprocessOnly) {
1579 copy_file(out, preprocessed_in);
1580 int pp_result = pclose(preprocessed_in);
1582 /* remove output file in case of error */
1583 if (out != stdout && pp_result != EXIT_SUCCESS) {
1589 in = preprocessed_in;
1590 filetype = next_filetype;
1598 if (mode == Compile) {
1601 asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1605 in = open_file(filename);
1607 /* preprocess and compile */
1608 if (filetype == FILETYPE_PREPROCESSED_C) {
1609 char const* invalid_mode;
1612 case STANDARD_C89: c_mode = _C89; break;
1613 /* TODO determine difference between these two */
1614 case STANDARD_C90: c_mode = _C89; break;
1615 case STANDARD_C99: c_mode = _C89 | _C99; break;
1616 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1620 "warning: command line option \"-std=%s\" is not valid for C\n",
1623 case STANDARD_DEFAULT:
1624 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1626 case STANDARD_CXX98: invalid_mode = "c++98"; goto default_c_warn;
1627 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1630 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1631 char const* invalid_mode;
1633 case STANDARD_C89: invalid_mode = "c89"; goto default_cxx_warn;
1634 case STANDARD_C90: invalid_mode = "c90"; goto default_cxx_warn;
1635 case STANDARD_C99: invalid_mode = "c99"; goto default_cxx_warn;
1636 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1637 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1640 case STANDARD_CXX98: c_mode = _CXX; break;
1644 "warning: command line option \"-std=%s\" is not valid for C++\n",
1646 case STANDARD_DEFAULT:
1647 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1651 c_mode |= features_on;
1652 c_mode &= ~features_off;
1654 /* do the actual parsing */
1655 ir_timer_t *t_parsing = ir_timer_new();
1656 timer_register(t_parsing, "Frontend: Parsing");
1657 timer_push(t_parsing);
1659 translation_unit_t *const unit = do_parsing(in, filename);
1660 timer_pop(t_parsing);
1662 /* prints the AST even if errors occurred */
1663 if (mode == PrintAst) {
1668 if (error_count > 0) {
1669 /* parsing failed because of errors */
1670 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1672 result = EXIT_FAILURE;
1674 } else if (warning_count > 0) {
1675 fprintf(stderr, "%u warning(s)\n", warning_count);
1678 if (in == preprocessed_in) {
1679 int pp_result = pclose(preprocessed_in);
1680 if (pp_result != EXIT_SUCCESS) {
1681 /* remove output file */
1684 return EXIT_FAILURE;
1688 if (mode == BenchmarkParser) {
1690 } else if (mode == PrintFluffy) {
1691 write_fluffy_decls(out, unit);
1693 } else if (mode == PrintJna) {
1694 write_jna_decls(out, unit);
1698 /* build the firm graph */
1699 ir_timer_t *t_construct = ir_timer_new();
1700 timer_register(t_construct, "Frontend: Graph construction");
1701 timer_push(t_construct);
1702 if (already_constructed_firm) {
1703 panic("compiling multiple files/translation units not possible");
1705 translation_unit_to_firm(unit);
1706 already_constructed_firm = true;
1707 timer_pop(t_construct);
1710 if (mode == ParseOnly) {
1714 if (mode == CompileDump) {
1716 ident *id = new_id_from_str(dumpfunction);
1717 ir_graph *irg = NULL;
1718 int n_irgs = get_irp_n_irgs();
1719 for (int i = 0; i < n_irgs; ++i) {
1720 ir_graph *tirg = get_irp_irg(i);
1721 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1729 fprintf(stderr, "No graph for function '%s' found\n",
1731 return EXIT_FAILURE;
1734 dump_ir_graph_file(out, irg);
1736 return EXIT_SUCCESS;
1739 if (mode == CompileExportIR) {
1742 return EXIT_SUCCESS;
1745 gen_firm_finish(asm_out, filename);
1746 if (asm_out != out) {
1749 } else if (filetype == FILETYPE_IR) {
1751 ir_import(filename);
1753 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1754 copy_file(asm_out, in);
1755 if (in == preprocessed_in) {
1756 int pp_result = pclose(preprocessed_in);
1757 if (pp_result != EXIT_SUCCESS) {
1758 /* remove output in error case */
1764 if (asm_out != out) {
1769 if (mode == Compile)
1772 /* if we're here then we have preprocessed assembly */
1773 filename = asm_tempfile;
1774 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1777 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1779 const char *filename_o;
1780 if (mode == CompileAssemble) {
1782 filename_o = outname;
1784 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1789 assemble(filename_o, filename);
1791 size_t len = strlen(filename_o) + 1;
1792 filename = obstack_copy(&file_obst, filename_o, len);
1793 filetype = FILETYPE_OBJECT;
1796 /* ok we're done here, process next file */
1797 file->name = filename;
1798 file->type = filetype;
1801 if (result != EXIT_SUCCESS) {
1807 /* link program file */
1808 if (mode == CompileAssembleLink) {
1809 obstack_1grow(&ldflags_obst, '\0');
1810 const char *flags = obstack_finish(&ldflags_obst);
1812 /* construct commandline */
1813 const char *linker = getenv("CPARSER_LINK");
1814 if (linker != NULL) {
1815 obstack_printf(&file_obst, "%s ", linker);
1817 if (target_triple != NULL)
1818 obstack_printf(&file_obst, "%s-", target_triple);
1819 obstack_printf(&file_obst, "%s ", LINKER);
1822 for (file_list_entry_t *entry = files; entry != NULL;
1823 entry = entry->next) {
1824 if (entry->type != FILETYPE_OBJECT)
1827 add_flag(&file_obst, "%s", entry->name);
1830 add_flag(&file_obst, "-o");
1831 add_flag(&file_obst, outname);
1832 obstack_printf(&file_obst, "%s", flags);
1833 obstack_1grow(&file_obst, '\0');
1835 char *commandline = obstack_finish(&file_obst);
1840 int err = system(commandline);
1841 if (err != EXIT_SUCCESS) {
1842 fprintf(stderr, "linker reported an error\n");
1843 return EXIT_FAILURE;
1850 obstack_free(&cppflags_obst, NULL);
1851 obstack_free(&ldflags_obst, NULL);
1852 obstack_free(&asflags_obst, NULL);
1853 obstack_free(&file_obst, NULL);
1863 exit_symbol_table();
1864 return EXIT_SUCCESS;