2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
36 /* no eXecute on Win32 */
41 #define O_RDWR _O_RDWR
42 #define O_CREAT _O_CREAT
43 #define O_EXCL _O_EXCL
44 #define O_BINARY _O_BINARY
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode) _access(fname, mode)
48 #define mktemp(tmpl) _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode) _fdopen(fd, mode)
51 #define popen(cmd, mode) _popen(cmd, mode)
52 #define pclose(file) _pclose(file)
53 #define unlink(filename) _unlink(filename)
60 #include <libfirm/firm.h>
61 #include <libfirm/be.h>
63 #include "preprocessor.h"
66 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_timing.h"
74 #include "driver/firm_machine.h"
75 #include "adt/error.h"
76 #include "adt/strutil.h"
77 #include "adt/array.h"
78 #include "wrappergen/write_fluffy.h"
79 #include "wrappergen/write_jna.h"
80 #include "wrappergen/write_compoundsizes.h"
89 #define PREPROCESSOR "gcc -E -U__STRICT_ANSI__ -U__BLOCKS__"
91 #define PREPROCESSOR "cpp -U__STRICT_ANSI__"
100 #define ASSEMBLER "gcc -c -xassembler"
103 unsigned int c_mode = _C89 | _C99 | _GNUC;
104 bool byte_order_big_endian = false;
105 bool strict_mode = false;
106 bool enable_main_collect2_hack = false;
107 bool freestanding = false;
108 unsigned architecture_modulo_shift = 0;
110 static bool char_is_signed = true;
111 static atomic_type_kind_t wchar_atomic_kind = ATOMIC_TYPE_INT;
112 static unsigned features_on = 0;
113 static unsigned features_off = 0;
114 static const char *dumpfunction = NULL;
115 static struct obstack file_obst;
116 static const char *external_preprocessor = PREPROCESSOR;
118 static machine_triple_t *target_machine;
119 static const char *target_triple;
121 static struct obstack cppflags_obst;
122 static struct obstack ldflags_obst;
123 static struct obstack asflags_obst;
124 static char dep_target[1024];
125 static const char *outname;
126 static bool define_intmax_types;
127 static const char *input_encoding;
129 typedef enum lang_standard_t {
130 STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
131 STANDARD_ANSI, /* ISO C90 (for C) or ISO C++ 1998 (for C++) */
132 STANDARD_C89, /* ISO C90 (sic) */
133 STANDARD_C89AMD1, /* ISO C90 as modified in amendment 1 */
134 STANDARD_C99, /* ISO C99 */
135 STANDARD_C11, /* ISO C11 */
136 STANDARD_GNU89, /* ISO C90 plus GNU extensions (including some C99) */
137 STANDARD_GNU99, /* ISO C99 plus GNU extensions */
138 STANDARD_GNU11, /* ISO C11 plus GNU extensions */
139 STANDARD_CXX98, /* ISO C++ 1998 plus amendments */
140 STANDARD_GNUXX98 /* ISO C++ 1998 plus amendments and GNU extensions */
143 typedef enum compilation_unit_type_t {
144 COMPILATION_UNIT_AUTODETECT,
146 COMPILATION_UNIT_PREPROCESSED_C,
147 COMPILATION_UNIT_CXX,
148 COMPILATION_UNIT_PREPROCESSED_CXX,
149 COMPILATION_UNIT_AST,
150 COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION,
151 COMPILATION_UNIT_ASSEMBLER,
152 COMPILATION_UNIT_PREPROCESSED_ASSEMBLER,
153 COMPILATION_UNIT_OBJECT,
155 COMPILATION_UNIT_UNKNOWN
156 } compilation_unit_type_t;
158 typedef struct compilation_unit_t compilation_unit_t;
159 struct compilation_unit_t {
160 const char *name; /**< filename or "-" for stdin */
161 FILE *input; /**< input (NULL if not opened yet) */
163 compilation_unit_type_t type;
164 lang_standard_t standard;
165 translation_unit_t *ast;
167 compilation_unit_t *next;
170 static char **temp_files;
172 static void get_output_name(char *buf, size_t buflen, const char *inputname,
175 if (inputname == NULL)
178 char const *const last_slash = strrchr(inputname, '/');
179 char const *const filename =
180 last_slash != NULL ? last_slash + 1 : inputname;
181 char const *const last_dot = strrchr(filename, '.');
182 char const *const name_end =
183 last_dot != NULL ? last_dot : strchr(filename, '\0');
185 int const len = snprintf(buf, buflen, "%.*s%s",
186 (int)(name_end - filename), filename, newext);
188 if (len < 0 || buflen <= (size_t)len)
190 if (buflen <= (size_t)len)
192 panic("filename too long");
195 static bool close_input(compilation_unit_t *unit)
199 if (unit->input == stdin) {
201 } else if (unit->input_is_pipe) {
202 res = pclose(unit->input) == EXIT_SUCCESS;
212 static void print_error_summary(void)
214 if (error_count > 0) {
215 /* parsing failed because of errors */
216 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
218 } else if (warning_count > 0) {
219 fprintf(stderr, "%u warning(s)\n", warning_count);
223 static void do_parsing(compilation_unit_t *unit)
225 ir_timer_t *t_parsing = ir_timer_new();
226 timer_register(t_parsing, "Frontend: Parsing");
227 timer_start(t_parsing);
231 switch_pp_input(unit->input, unit->name, NULL, false);
233 unit->ast = finish_parsing();
234 check_unclosed_conditionals();
236 bool res = close_input(unit);
238 print_error_summary();
240 unit->type = COMPILATION_UNIT_AST;
241 unit->parse_errors = error_count > 0 || !res;
242 timer_stop(t_parsing);
245 static void add_flag(struct obstack *obst, const char *format, ...)
250 va_start(ap, format);
254 vsnprintf(buf, sizeof(buf), format, ap);
257 obstack_1grow(obst, ' ');
259 obstack_1grow(obst, '"');
260 obstack_grow(obst, buf, len);
261 obstack_1grow(obst, '"');
263 /* escape stuff... */
264 for (char *c = buf; *c != '\0'; ++c) {
282 obstack_1grow(obst, '\\');
285 obstack_1grow(obst, *c);
292 static const char *type_to_string(type_t *type)
294 assert(type->kind == TYPE_ATOMIC);
295 return get_atomic_kind_name(type->atomic.akind);
298 static char const* str_lang_standard(lang_standard_t const standard)
301 case STANDARD_C89: return "c89";
302 case STANDARD_C89AMD1: return "iso9899:199409";
303 case STANDARD_C99: return "c99";
304 case STANDARD_C11: return "c11";
305 case STANDARD_GNU89: return "gnu89";
306 case STANDARD_GNU99: return "gnu99";
307 case STANDARD_GNU11: return "gnu11";
308 case STANDARD_CXX98: return "c++98";
309 case STANDARD_GNUXX98: return "gnu++98";
310 case STANDARD_ANSI: break;
311 case STANDARD_DEFAULT: break;
313 panic("invalid standard");
316 static bool run_external_preprocessor(compilation_unit_t *unit)
318 static const char *common_flags = NULL;
320 if (common_flags == NULL) {
321 obstack_1grow(&cppflags_obst, '\0');
322 const char *flags = obstack_finish(&cppflags_obst);
324 /* setup default defines */
325 add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
326 add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s", type_to_string(type_wchar_t));
327 add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
328 add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
330 add_flag(&cppflags_obst, "-U__VERSION__");
331 add_flag(&cppflags_obst, "-D__VERSION__=\"%s\"", cparser_REVISION);
333 if (define_intmax_types) {
334 add_flag(&cppflags_obst, "-U__INTMAX_TYPE__");
335 add_flag(&cppflags_obst, "-D__INTMAX_TYPE__=%s", type_to_string(type_intmax_t));
336 add_flag(&cppflags_obst, "-U__UINTMAX_TYPE__");
337 add_flag(&cppflags_obst, "-D__UINTMAX_TYPE__=%s", type_to_string(type_uintmax_t));
340 if (flags[0] != '\0') {
341 size_t len = strlen(flags);
342 obstack_1grow(&cppflags_obst, ' ');
343 obstack_grow(&cppflags_obst, flags, len);
345 obstack_1grow(&cppflags_obst, '\0');
346 common_flags = obstack_finish(&cppflags_obst);
349 assert(obstack_object_size(&cppflags_obst) == 0);
351 const char *preprocessor = getenv("CPARSER_PP");
352 if (preprocessor != NULL) {
353 obstack_printf(&cppflags_obst, "%s ", preprocessor);
355 if (target_triple != NULL)
356 obstack_printf(&cppflags_obst, "%s-", target_triple);
357 obstack_printf(&cppflags_obst, "%s", external_preprocessor);
361 switch (unit->type) {
362 case COMPILATION_UNIT_C: lang = "c"; break;
363 case COMPILATION_UNIT_CXX: lang = "c++"; break;
364 case COMPILATION_UNIT_ASSEMBLER: lang = "assembler-with-cpp"; break;
365 default: lang = NULL; break;
368 add_flag(&cppflags_obst, "-x%s", lang);
370 add_flag(&cppflags_obst, "-std=%s", str_lang_standard(unit->standard));
372 obstack_printf(&cppflags_obst, "%s", common_flags);
374 /* handle dependency generation */
375 if (dep_target[0] != '\0') {
376 add_flag(&cppflags_obst, "-MF");
377 add_flag(&cppflags_obst, dep_target);
378 if (outname != NULL) {
379 add_flag(&cppflags_obst, "-MQ");
380 add_flag(&cppflags_obst, outname);
383 assert(unit->input == NULL);
384 add_flag(&cppflags_obst, unit->name);
385 obstack_1grow(&cppflags_obst, '\0');
387 char *commandline = obstack_finish(&cppflags_obst);
391 FILE *f = popen(commandline, "r");
393 fprintf(stderr, "invoking preprocessor failed\n");
396 /* we do not really need that anymore */
397 obstack_free(&cppflags_obst, commandline);
400 unit->input_is_pipe = true;
401 switch (unit->type) {
402 case COMPILATION_UNIT_ASSEMBLER:
403 unit->type = COMPILATION_UNIT_PREPROCESSED_ASSEMBLER;
405 case COMPILATION_UNIT_C:
406 unit->type = COMPILATION_UNIT_PREPROCESSED_C;
408 case COMPILATION_UNIT_CXX:
409 unit->type = COMPILATION_UNIT_PREPROCESSED_CXX;
412 unit->type = COMPILATION_UNIT_UNKNOWN;
419 static void assemble(const char *out, const char *in)
421 obstack_1grow(&asflags_obst, '\0');
422 const char *flags = obstack_finish(&asflags_obst);
424 const char *assembler = getenv("CPARSER_AS");
425 if (assembler != NULL) {
426 obstack_printf(&asflags_obst, "%s", assembler);
428 if (target_triple != NULL)
429 obstack_printf(&asflags_obst, "%s-", target_triple);
430 obstack_printf(&asflags_obst, "%s", ASSEMBLER);
432 if (flags[0] != '\0')
433 obstack_printf(&asflags_obst, " %s", flags);
435 obstack_printf(&asflags_obst, " %s -o %s", in, out);
436 obstack_1grow(&asflags_obst, '\0');
438 char *commandline = obstack_finish(&asflags_obst);
442 int err = system(commandline);
443 if (err != EXIT_SUCCESS) {
444 fprintf(stderr, "assembler reported an error\n");
447 obstack_free(&asflags_obst, commandline);
450 static void print_file_name(const char *file)
452 add_flag(&ldflags_obst, "-print-file-name=%s", file);
454 obstack_1grow(&ldflags_obst, '\0');
455 const char *flags = obstack_finish(&ldflags_obst);
457 /* construct commandline */
458 const char *linker = getenv("CPARSER_LINK");
459 if (linker != NULL) {
460 obstack_printf(&ldflags_obst, "%s ", linker);
462 if (target_triple != NULL)
463 obstack_printf(&ldflags_obst, "%s-", target_triple);
464 obstack_printf(&ldflags_obst, "%s ", LINKER);
466 obstack_printf(&ldflags_obst, "%s ", linker);
467 obstack_printf(&ldflags_obst, "%s", flags);
468 obstack_1grow(&ldflags_obst, '\0');
470 char *commandline = obstack_finish(&ldflags_obst);
474 int err = system(commandline);
475 if (err != EXIT_SUCCESS) {
476 fprintf(stderr, "linker reported an error\n");
479 obstack_free(&ldflags_obst, commandline);
482 static const char *try_dir(const char *dir)
486 if (access(dir, R_OK | W_OK | X_OK) == 0)
491 static const char *get_tempdir(void)
493 static const char *tmpdir = NULL;
499 tmpdir = try_dir(getenv("TMPDIR"));
501 tmpdir = try_dir(getenv("TMP"));
503 tmpdir = try_dir(getenv("TEMP"));
507 tmpdir = try_dir(P_tmpdir);
511 tmpdir = try_dir("/var/tmp");
513 tmpdir = try_dir("/usr/tmp");
515 tmpdir = try_dir("/tmp");
524 /* cheap and nasty mkstemp replacement */
525 static int mkstemp(char *templ)
528 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
533 * custom version of tmpnam, which: writes to an obstack, emits no warnings
534 * during linking (like glibc/gnu ld do for tmpnam)...
536 static FILE *make_temp_file(const char *prefix, const char **name_result)
538 const char *tempdir = get_tempdir();
539 assert(obstack_object_size(&file_obst) == 0);
540 obstack_printf(&file_obst, "%s/%sXXXXXX", tempdir, prefix);
541 obstack_1grow(&file_obst, '\0');
543 char *name = obstack_finish(&file_obst);
544 int fd = mkstemp(name);
546 fprintf(stderr, "could not create temporary file: %s\n",
550 FILE *out = fdopen(fd, "w");
552 fprintf(stderr, "could not open temporary file as FILE*\n");
556 ARR_APP1(char*, temp_files, name);
561 static void free_temp_files(void)
563 if (temp_files == NULL)
566 size_t n_temp_files = ARR_LEN(temp_files);
568 for (i = 0; i < n_temp_files; ++i) {
569 char *file = temp_files[i];
572 DEL_ARR_F(temp_files);
576 typedef enum compile_mode_t {
591 static void usage(const char *argv0)
593 fprintf(stderr, "Usage %s [options] input [-o output]\n", argv0);
596 static void print_cparser_version(void)
598 printf("cparser (%s) using libFirm (%u.%u",
599 cparser_REVISION, ir_get_version_major(),
600 ir_get_version_minor());
602 const char *revision = ir_get_version_revision();
603 if (revision[0] != 0) {
605 fputs(revision, stdout);
608 const char *build = ir_get_version_build();
611 fputs(build, stdout);
614 puts("This is free software; see the source for copying conditions. There is NO\n"
615 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
618 static void print_cparser_version_short(void)
620 puts(cparser_REVISION);
623 static void print_help_basic(const char *argv0)
627 put_help("--help", "Display this information");
628 put_help("--version", "Display compiler version");
629 put_help("--help-parser", "Display information about parser options");
630 put_help("--help-warnings", "Display information about warning options");
631 put_help("--help-codegen", "Display information about code-generation options");
632 put_help("--help-optimization", "Display information about optimization options");
633 put_help("--help-linker", "Display information about linker options");
634 put_help("--help-language-tools", "Display information about language tools options");
635 put_help("--help-debug", "Display information about compiler debugging options");
636 put_help("--help-firm", "Display information about direct firm options");
637 put_help("--help-all", "Display information about all options");
638 put_help("-c", "Compile and assemble but do not link");
639 put_help("-E", "Preprocess only");
640 put_help("-S", "Compile but do not assembler or link");
641 put_help("-o", "Specify output file");
642 put_help("-v", "Verbose output (show invocation of sub-processes)");
643 put_help("-x", "Force input language:");
644 put_choice("c", "C");
645 put_choice("c++", "C++");
646 put_choice("assembler", "Assembler (no preprocessing)");
647 put_choice("assembler-with-cpp", "Assembler with preprocessing");
648 put_choice("none", "Autodetection");
649 put_help("-pipe", "Ignored (gcc compatibility)");
652 static void print_help_preprocessor(void)
654 put_help("-nostdinc", "Do not search standard system include directories");
655 put_help("-trigraphs", "Support ISO C trigraphs");
656 put_help("-isystem", "");
657 put_help("-include", "");
658 put_help("-I PATH", "");
659 put_help("-D SYMBOL[=value]", "");
660 put_help("-U SYMBOL", "");
661 put_help("-Wp,OPTION", "Pass option directly to preprocessor");
664 put_help("-MMD", "");
672 static void print_help_parser(void)
674 put_help("-finput-charset=CHARSET", "Select encoding of input files");
675 put_help("-fmessage-length=LEN", "Ignored (gcc compatibility)");
676 put_help("-fshort-wchar", "Type \"wchar_t\" is unsigned short instead of int");
677 put_help("-fshow-column", "Show the column number in diagnostic messages");
678 put_help("-fsigned-char", "Type \"char\" is a signed type");
679 put_help("-funsigned-char", "Type \"char\" is an unsigned type");
680 put_help("--ms", "Enable msvc extensions");
681 put_help("--no-ms", "Disable msvc extensions");
682 put_help("--gcc", "Enable gcc extensions");
683 put_help("--no-gcc", "Disable gcc extensions");
684 put_help("-std=STANDARD", "Specify language standard:");
685 put_choice("c99", "ISO C99 standard");
686 put_choice("c89", "ISO C89 standard");
687 put_choice("c90", "Same as -std=c89");
688 put_choice("c11", "ISO C11 standard");
689 put_choice("c9x", "Deprecated");
690 put_choice("c++", "ISO C++ 98");
691 put_choice("c++98", "ISO C++ 98");
692 put_choice("gnu99", "ISO C99 + GNU extensions (default)");
693 put_choice("gnu89", "ISO C89 + GNU extensions");
694 put_choice("gnu11", "ISO C11 + GNU extensions");
695 put_choice("gnu9x", "Deprecated");
696 put_choice("iso9899:1990", "ISO C89");
697 put_choice("iso9899:199409", "ISO C90");
698 put_choice("iso9899:1999", "ISO C99");
699 put_choice("iso9899:199x", "Deprecated");
700 put_help("-pedantic", "Ignored (gcc compatibility)");
701 put_help("-ansi", "-std=c90 (for C) or -std=c++98 (for C++)");
702 put_help("--strict", "Enable strict conformance checking");
705 static void print_help_warnings(void)
707 put_help("-f[no-]diagnostics-show-option", "Show the switch, which controls a warning, after each warning");
708 put_help("-w", "Disable all warnings");
709 put_help("-Wno-trigraphs", "Warn if input contains trigraphs");
710 put_help("-Wundef", "Warn if an undefined macro is used in an #if");
711 put_help("-Wmissing-include-dirs", "Warn about missing user-specified include directories");
712 put_help("-Wendif-labels", "Warn about stray text after #elif and #endif");
713 put_help("-Winit-self", "Ignored (gcc compatibility)");
714 put_help("-Wformat-y2k", "Ignored (gcc compatibility)");
715 put_help("-Wformat-security", "Ignored (gcc compatibility)");
716 put_help("-Wold-style-declaration", "Ignored (gcc compatibility)");
717 put_help("-Wtype-limits", "Ignored (gcc compatibility)");
718 print_warning_opt_help();
721 static void print_help_optimization(void)
723 put_help("-O LEVEL", "Select optimization level (0-4)");
724 firm_option_help(put_help);
725 put_help("-fexpensive-optimizations","Ignored (gcc compatibility)");
728 static void print_help_codegeneration(void)
730 put_help("-g", "Generate debug information");
731 put_help("-pg", "Instrument code for gnu gprof");
732 put_help("-fomit-frame-pointer", "Produce code without frame pointer where possible");
733 put_help("-ffreestanding", "Compile in freestanding mode (see ISO C standard)");
734 put_help("-fhosted", "Compile in hosted (not freestanding) mode");
735 put_help("-fprofile-generate", "Generate instrumented code to collect profile information");
736 put_help("-fprofile-use", "Use profile information generated by instrumented binaries");
737 put_help("-ffp-precise", "Precise floating point model");
738 put_help("-ffp-fast", "Imprecise floating point model");
739 put_help("-ffp-strict", "Strict floating point model");
740 put_help("-pthread", "Use pthread threading library");
741 put_help("-mtarget=TARGET", "Specify target architecture as CPU-manufacturer-OS triple");
742 put_help("-mtriple=TARGET", "Alias for -mtarget (clang compatibility)");
743 put_help("-march=ARCH", "");
744 put_help("-mtune=ARCH", "");
745 put_help("-mcpu=CPU", "");
746 put_help("-mfpmath=", "");
747 put_help("-mpreferred-stack-boundary=", "");
748 put_help("-mrtd", "");
749 put_help("-mregparm=", "Not supported yet");
750 put_help("-msoft-float", "Not supported yet");
751 put_help("-m32", "Generate 32bit code");
752 put_help("-m64", "Generate 64bit code");
753 put_help("-fverbose-asm", "Ignored (gcc compatibility)");
754 put_help("-fjump-tables", "Ignored (gcc compatibility)");
755 put_help("-fcommon", "Ignored (gcc compatibility)");
756 put_help("-foptimize-sibling-calls", "Ignored (gcc compatibility)");
757 put_help("-falign-loops", "Ignored (gcc compatibility)");
758 put_help("-falign-jumps", "Ignored (gcc compatibility)");
759 put_help("-falign-functions", "Ignored (gcc compatibility)");
760 put_help("-fPIC", "Ignored (gcc compatibility)");
761 put_help("-ffast-math", "Same as -ffp-fast (gcc compatibility)");
763 puts("\tMost of these options can be used with a no- prefix to disable them");
764 puts("\te.g. -fno-omit-frame-pointer");
767 static void print_help_linker(void)
769 put_help("-l LIBRARY", "");
770 put_help("-L PATH", "");
771 put_help("-s", "Do not produce symbol table and relocation information");
772 put_help("-shared", "Produce a shared library");
773 put_help("-static", "Produce statically linked binary");
774 put_help("-Wl,OPTION", "Pass option directly to linker");
777 static void print_help_debug(void)
779 put_help("--print-ast", "Preprocess, parse and print AST");
780 put_help("--print-implicit-cast", "");
781 put_help("--print-parenthesis", "");
782 put_help("--benchmark", "Preprocess and parse, produces no output");
783 put_help("--time", "Measure time of compiler passes");
784 put_help("--dump-function func", "Preprocess, parse and output vcg graph of func");
785 put_help("--export-ir", "Preprocess, parse and output compiler intermediate representation");
788 static void print_help_language_tools(void)
790 put_help("--print-fluffy", "Preprocess, parse and generate declarations for the fluffy language");
791 put_help("--print-jna", "Preprocess, parse and generate declarations for JNA");
792 put_help("--jna-limit filename", "");
793 put_help("--jna-libname name", "");
796 static void print_help_firm(void)
798 put_help("-bOPTION", "Directly pass option to libFirm backend");
799 int res = be_parse_arg("help");
807 HELP_PREPROCESSOR = 1 << 1,
808 HELP_PARSER = 1 << 2,
809 HELP_WARNINGS = 1 << 3,
810 HELP_OPTIMIZATION = 1 << 4,
811 HELP_CODEGEN = 1 << 5,
812 HELP_LINKER = 1 << 6,
813 HELP_LANGUAGETOOLS = 1 << 7,
820 static void print_help(const char *argv0, help_sections_t sections)
822 if (sections & HELP_BASIC) print_help_basic(argv0);
823 if (sections & HELP_PREPROCESSOR) print_help_preprocessor();
824 if (sections & HELP_PARSER) print_help_parser();
825 if (sections & HELP_WARNINGS) print_help_warnings();
826 if (sections & HELP_OPTIMIZATION) print_help_optimization();
827 if (sections & HELP_CODEGEN) print_help_codegeneration();
828 if (sections & HELP_LINKER) print_help_linker();
829 if (sections & HELP_LANGUAGETOOLS) print_help_language_tools();
830 if (sections & HELP_DEBUG) print_help_debug();
831 if (sections & HELP_FIRM) print_help_firm();
834 static void set_be_option(const char *arg)
836 int res = be_parse_arg(arg);
841 static compilation_unit_type_t get_unit_type_from_string(const char *string)
843 if (streq(string, "c") || streq(string, "c-header"))
844 return COMPILATION_UNIT_C;
845 if (streq(string, "c++") || streq(string, "c++-header"))
846 return COMPILATION_UNIT_CXX;
847 if (streq(string, "assembler"))
848 return COMPILATION_UNIT_PREPROCESSED_ASSEMBLER;
849 if (streq(string, "assembler-with-cpp"))
850 return COMPILATION_UNIT_ASSEMBLER;
851 if (streq(string, "none"))
852 return COMPILATION_UNIT_AUTODETECT;
854 return COMPILATION_UNIT_UNKNOWN;
857 static bool init_os_support(void)
859 wchar_atomic_kind = ATOMIC_TYPE_INT;
860 enable_main_collect2_hack = false;
861 define_intmax_types = false;
863 if (firm_is_unixish_os(target_machine)) {
864 set_create_ld_ident(create_name_linux_elf);
865 } else if (firm_is_darwin_os(target_machine)) {
866 set_create_ld_ident(create_name_macho);
867 define_intmax_types = true;
868 } else if (firm_is_windows_os(target_machine)) {
869 wchar_atomic_kind = ATOMIC_TYPE_USHORT;
870 enable_main_collect2_hack = true;
871 set_create_ld_ident(create_name_win32);
879 static bool parse_target_triple(const char *arg)
881 machine_triple_t *triple = firm_parse_machine_triple(arg);
882 if (triple == NULL) {
883 fprintf(stderr, "Target-triple is not in the form 'cpu_type-manufacturer-operating_system'\n");
886 target_machine = triple;
890 static unsigned decide_modulo_shift(unsigned type_size)
892 if (architecture_modulo_shift == 0)
894 if (type_size < architecture_modulo_shift)
895 return architecture_modulo_shift;
899 static bool is_ia32_cpu(const char *architecture)
901 return streq(architecture, "i386")
902 || streq(architecture, "i486")
903 || streq(architecture, "i586")
904 || streq(architecture, "i686")
905 || streq(architecture, "i786");
908 static const char *setup_isa_from_tripel(const machine_triple_t *machine)
910 const char *cpu = machine->cpu_type;
912 if (is_ia32_cpu(cpu)) {
914 } else if (streq(cpu, "x86_64")) {
916 } else if (streq(cpu, "sparc")) {
918 } else if (streq(cpu, "arm")) {
921 fprintf(stderr, "Unknown cpu '%s' in target-triple\n", cpu);
926 static const char *setup_target_machine(void)
928 if (!setup_firm_for_machine(target_machine))
931 const char *isa = setup_isa_from_tripel(target_machine);
942 * initialize cparser type properties based on a firm type
944 static void set_typeprops_type(atomic_type_properties_t* props, ir_type *type)
946 props->size = get_type_size_bytes(type);
947 props->alignment = get_type_alignment_bytes(type);
948 props->struct_alignment = props->alignment;
952 * Copy atomic type properties except the integer conversion rank
954 static void copy_typeprops(atomic_type_properties_t *dest,
955 const atomic_type_properties_t *src)
957 dest->size = src->size;
958 dest->alignment = src->alignment;
959 dest->struct_alignment = src->struct_alignment;
960 dest->flags = src->flags;
963 static void init_types_and_adjust(void)
965 const backend_params *be_params = be_get_backend_param();
966 unsigned machine_size = be_params->machine_size;
967 init_types(machine_size);
969 atomic_type_properties_t *props = atomic_type_properties;
971 /* adjust types as requested by target architecture */
972 ir_type *type_long_double = be_params->type_long_double;
973 if (type_long_double != NULL) {
974 set_typeprops_type(&props[ATOMIC_TYPE_LONG_DOUBLE], type_long_double);
975 atomic_modes[ATOMIC_TYPE_LONG_DOUBLE] = get_type_mode(type_long_double);
978 ir_type *type_long_long = be_params->type_long_long;
979 if (type_long_long != NULL)
980 set_typeprops_type(&props[ATOMIC_TYPE_LONGLONG], type_long_long);
982 ir_type *type_unsigned_long_long = be_params->type_unsigned_long_long;
983 if (type_unsigned_long_long != NULL)
984 set_typeprops_type(&props[ATOMIC_TYPE_ULONGLONG], type_unsigned_long_long);
986 /* operating system ABI specifics */
987 if (firm_is_darwin_os(target_machine)) {
988 if (is_ia32_cpu(target_machine->cpu_type)) {
989 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
990 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
991 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
992 props[ATOMIC_TYPE_LONG_DOUBLE].size = 16;
993 props[ATOMIC_TYPE_LONG_DOUBLE].alignment = 16;
994 props[ATOMIC_TYPE_LONG_DOUBLE].struct_alignment = 16;
996 } else if (firm_is_windows_os(target_machine)) {
997 if (is_ia32_cpu(target_machine->cpu_type)) {
998 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 8;
999 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 8;
1000 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 8;
1001 } else if (machine_size == 64) {
1002 /* to ease porting of old c-code microsoft decided to use 32bits
1004 props[ATOMIC_TYPE_LONG] = props[ATOMIC_TYPE_INT];
1005 props[ATOMIC_TYPE_ULONG] = props[ATOMIC_TYPE_UINT];
1008 /* on windows long double is not supported */
1009 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
1010 } else if (firm_is_unixish_os(target_machine)) {
1011 if (is_ia32_cpu(target_machine->cpu_type)) {
1012 /* System V has a broken alignment for double so we have to add
1014 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
1015 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
1016 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
1020 /* stuff decided after processing operating system specifics and
1021 * commandline flags */
1022 if (char_is_signed) {
1023 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
1025 props[ATOMIC_TYPE_CHAR].flags &= ~ATOMIC_TYPE_FLAG_SIGNED;
1027 /* copy over wchar_t properties (including rank) */
1028 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
1030 /* initialize defaults for unsupported types */
1031 if (type_long_long == NULL) {
1032 copy_typeprops(&props[ATOMIC_TYPE_LONGLONG], &props[ATOMIC_TYPE_LONG]);
1034 if (type_unsigned_long_long == NULL) {
1035 copy_typeprops(&props[ATOMIC_TYPE_ULONGLONG],
1036 &props[ATOMIC_TYPE_ULONG]);
1038 if (type_long_double == NULL) {
1039 copy_typeprops(&props[ATOMIC_TYPE_LONG_DOUBLE],
1040 &props[ATOMIC_TYPE_DOUBLE]);
1043 /* initialize firm pointer modes */
1045 unsigned bit_size = machine_size;
1046 unsigned modulo_shift = decide_modulo_shift(bit_size);
1048 snprintf(name, sizeof(name), "p%u", machine_size);
1049 ir_mode *ptr_mode = new_reference_mode(name, irma_twos_complement, bit_size, modulo_shift);
1051 if (machine_size == 16) {
1052 set_reference_mode_signed_eq(ptr_mode, mode_Hs);
1053 set_reference_mode_unsigned_eq(ptr_mode, mode_Hu);
1054 } else if (machine_size == 32) {
1055 set_reference_mode_signed_eq(ptr_mode, mode_Is);
1056 set_reference_mode_unsigned_eq(ptr_mode, mode_Iu);
1057 } else if (machine_size == 64) {
1058 set_reference_mode_signed_eq(ptr_mode, mode_Ls);
1059 set_reference_mode_unsigned_eq(ptr_mode, mode_Lu);
1061 panic("strange machine_size when determining pointer modes");
1064 /* Hmm, pointers should be machine size */
1065 set_modeP_data(ptr_mode);
1066 set_modeP_code(ptr_mode);
1068 byte_order_big_endian = be_params->byte_order_big_endian;
1069 if (be_params->modulo_shift_efficient) {
1070 architecture_modulo_shift = machine_size;
1072 architecture_modulo_shift = 0;
1076 static void setup_cmode(const compilation_unit_t *unit)
1078 compilation_unit_type_t type = unit->type;
1079 lang_standard_t standard = unit->standard;
1080 if (type == COMPILATION_UNIT_PREPROCESSED_C || type == COMPILATION_UNIT_C) {
1082 case STANDARD_C89: c_mode = _C89; break;
1083 /* TODO determine difference between these two */
1084 case STANDARD_C89AMD1: c_mode = _C89; break;
1085 case STANDARD_C99: c_mode = _C89 | _C99; break;
1086 case STANDARD_C11: c_mode = _C89 | _C99 | _C11; break;
1087 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1088 case STANDARD_GNU11: c_mode = _C89 | _C99 | _C11 | _GNUC; break;
1091 case STANDARD_CXX98:
1092 case STANDARD_GNUXX98:
1093 case STANDARD_DEFAULT:
1094 fprintf(stderr, "warning: command line option \"-std=%s\" is not valid for C\n", str_lang_standard(standard));
1096 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1098 } else if (type == COMPILATION_UNIT_PREPROCESSED_CXX
1099 || type == COMPILATION_UNIT_CXX) {
1101 case STANDARD_CXX98: c_mode = _CXX; break;
1105 case STANDARD_C89AMD1:
1108 case STANDARD_GNU89:
1109 case STANDARD_GNU99:
1110 case STANDARD_GNU11:
1111 case STANDARD_DEFAULT:
1112 fprintf(stderr, "warning: command line option \"-std=%s\" is not valid for C++\n", str_lang_standard(standard));
1114 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1118 c_mode |= features_on;
1119 c_mode &= ~features_off;
1122 static void determine_unit_standard(compilation_unit_t *unit,
1123 lang_standard_t standard)
1125 unit->standard = standard;
1128 switch (unit->type) {
1129 case COMPILATION_UNIT_C:
1130 case COMPILATION_UNIT_PREPROCESSED_C:
1131 unit->standard = STANDARD_C89;
1133 case COMPILATION_UNIT_CXX:
1134 case COMPILATION_UNIT_PREPROCESSED_CXX:
1135 unit->standard = STANDARD_CXX98;
1142 case STANDARD_DEFAULT:
1143 switch (unit->type) {
1144 case COMPILATION_UNIT_C:
1145 case COMPILATION_UNIT_PREPROCESSED_C:
1146 unit->standard = STANDARD_GNU99;
1148 case COMPILATION_UNIT_CXX:
1149 case COMPILATION_UNIT_PREPROCESSED_CXX:
1150 unit->standard = STANDARD_GNUXX98;
1162 static bool output_preprocessor_tokens(compilation_unit_t *unit, FILE *out)
1164 /* just here for gcc compatibility */
1165 fprintf(out, "# 1 \"%s\"\n", unit->name);
1166 fprintf(out, "# 1 \"<built-in>\"\n");
1167 fprintf(out, "# 1 \"<command-line>\"\n");
1169 set_preprocessor_output(out);
1170 switch_pp_input(unit->input, unit->name, NULL, false);
1173 next_preprocessing_token();
1174 if (pp_token.kind == T_EOF)
1180 check_unclosed_conditionals();
1182 print_error_summary();
1183 set_preprocessor_output(NULL);
1185 if (unit->type == COMPILATION_UNIT_C) {
1186 unit->type = COMPILATION_UNIT_PREPROCESSED_C;
1187 } else if (unit->type == COMPILATION_UNIT_CXX) {
1188 unit->type = COMPILATION_UNIT_PREPROCESSED_CXX;
1190 bool res = close_input(unit);
1191 return res && error_count == 0;
1194 static bool open_input(compilation_unit_t *unit)
1196 /* input already available as FILE? */
1197 if (unit->input != NULL)
1200 const char *const inputname = unit->name;
1201 unit->input_is_pipe = false;
1202 if (streq(inputname, "-")) {
1203 unit->input = stdin;
1205 unit->input = fopen(inputname, "r");
1206 if (unit->input == NULL) {
1207 fprintf(stderr, "Could not open '%s': %s\n", inputname,
1215 static int compilation_loop(compile_mode_t mode, compilation_unit_t *units,
1216 lang_standard_t standard, FILE *out)
1218 int result = EXIT_SUCCESS;
1219 bool already_constructed_firm = false;
1220 for (compilation_unit_t *unit = units; unit != NULL; unit = unit->next) {
1221 const char *const inputname = unit->name;
1223 determine_unit_standard(unit, standard);
1227 switch (unit->type) {
1228 case COMPILATION_UNIT_IR: {
1229 bool res = open_input(unit);
1231 result = EXIT_FAILURE;
1234 res = !ir_import_file(unit->input, unit->name);
1236 fprintf(stderr, "Import of firm graph from '%s' failed\n",
1238 result = EXIT_FAILURE;
1241 unit->type = COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION;
1244 case COMPILATION_UNIT_ASSEMBLER:
1245 panic("TODO: preprocess for assembler");
1246 case COMPILATION_UNIT_C:
1247 case COMPILATION_UNIT_CXX:
1248 if (external_preprocessor != NULL) {
1249 bool res = run_external_preprocessor(unit);
1251 result = EXIT_FAILURE;
1258 case COMPILATION_UNIT_PREPROCESSED_C:
1259 case COMPILATION_UNIT_PREPROCESSED_CXX: {
1260 bool res = open_input(unit);
1262 result = EXIT_FAILURE;
1267 if (mode == PreprocessOnly) {
1268 bool res = output_preprocessor_tokens(unit, out);
1270 result = EXIT_FAILURE;
1276 /* do the actual parsing */
1280 case COMPILATION_UNIT_AST:
1281 /* prints the AST even if errors occurred */
1282 if (mode == PrintAst) {
1284 print_ast(unit->ast);
1286 if (unit->parse_errors) {
1287 result = EXIT_FAILURE;
1291 if (mode == BenchmarkParser) {
1293 } else if (mode == PrintFluffy) {
1294 write_fluffy_decls(out, unit->ast);
1296 } else if (mode == PrintJna) {
1297 write_jna_decls(out, unit->ast);
1299 } else if (mode == PrintCompoundSizes) {
1300 write_compoundsizes(out, unit->ast);
1304 /* build the firm graph */
1305 ir_timer_t *t_construct = ir_timer_new();
1306 timer_register(t_construct, "Frontend: Graph construction");
1307 timer_start(t_construct);
1308 if (already_constructed_firm) {
1309 panic("compiling multiple files/translation units not possible");
1311 init_implicit_optimizations();
1312 translation_unit_to_firm(unit->ast);
1313 already_constructed_firm = true;
1314 timer_stop(t_construct);
1315 unit->type = COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION;
1318 case COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION:
1319 if (mode == ParseOnly)
1322 if (mode == CompileDump) {
1324 ident *id = new_id_from_str(dumpfunction);
1325 ir_graph *irg = NULL;
1326 int n_irgs = get_irp_n_irgs();
1327 for (int i = 0; i < n_irgs; ++i) {
1328 ir_graph *tirg = get_irp_irg(i);
1329 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1337 fprintf(stderr, "No graph for function '%s' found\n",
1339 return EXIT_FAILURE;
1342 dump_ir_graph_file(out, irg);
1344 return EXIT_SUCCESS;
1347 if (mode == CompileExportIR) {
1348 ir_export_file(out);
1349 if (ferror(out) != 0) {
1350 fprintf(stderr, "Error while writing to output\n");
1351 return EXIT_FAILURE;
1353 return EXIT_SUCCESS;
1357 if (mode == Compile) {
1360 asm_out = make_temp_file("ccs", &unit->name);
1362 ir_timer_t *t_opt_codegen = ir_timer_new();
1363 timer_register(t_opt_codegen, "Optimization and Codegeneration");
1364 timer_start(t_opt_codegen);
1365 generate_code(asm_out, inputname);
1366 timer_stop(t_opt_codegen);
1367 if (asm_out != out) {
1370 unit->type = COMPILATION_UNIT_PREPROCESSED_ASSEMBLER;
1372 case COMPILATION_UNIT_PREPROCESSED_ASSEMBLER:
1373 if (mode != CompileAssemble && mode != CompileAssembleLink)
1377 const char *input = unit->name;
1378 if (mode == CompileAssemble) {
1380 unit->name = outname;
1382 FILE *tempf = make_temp_file("cco", &unit->name);
1387 assemble(unit->name, input);
1389 unit->type = COMPILATION_UNIT_OBJECT;
1391 case COMPILATION_UNIT_UNKNOWN:
1392 case COMPILATION_UNIT_AUTODETECT:
1393 case COMPILATION_UNIT_OBJECT:
1400 static int link_program(compilation_unit_t *units)
1402 obstack_1grow(&ldflags_obst, '\0');
1403 const char *flags = obstack_finish(&ldflags_obst);
1405 /* construct commandline */
1406 const char *linker = getenv("CPARSER_LINK");
1407 if (linker != NULL) {
1408 obstack_printf(&file_obst, "%s ", linker);
1410 if (target_triple != NULL)
1411 obstack_printf(&file_obst, "%s-", target_triple);
1412 obstack_printf(&file_obst, "%s ", LINKER);
1415 for (compilation_unit_t *unit = units; unit != NULL; unit = unit->next) {
1416 if (unit->type != COMPILATION_UNIT_OBJECT)
1419 add_flag(&file_obst, "%s", unit->name);
1422 add_flag(&file_obst, "-o");
1423 add_flag(&file_obst, outname);
1424 obstack_printf(&file_obst, "%s", flags);
1425 obstack_1grow(&file_obst, '\0');
1427 char *commandline = obstack_finish(&file_obst);
1432 int err = system(commandline);
1433 if (err != EXIT_SUCCESS) {
1434 fprintf(stderr, "linker reported an error\n");
1435 return EXIT_FAILURE;
1437 return EXIT_SUCCESS;
1440 int main(int argc, char **argv)
1442 const char *print_file_name_file = NULL;
1443 compile_mode_t mode = CompileAssembleLink;
1445 char cpu_arch[16] = "ia32";
1446 compilation_unit_t *units = NULL;
1447 compilation_unit_t *last_unit = NULL;
1448 bool construct_dep_target = false;
1449 bool do_timing = false;
1450 bool profile_generate = false;
1451 bool profile_use = false;
1453 /* hack for now... */
1454 if (strstr(argv[0], "pptest") != NULL) {
1455 extern int pptest_main(int argc, char **argv);
1456 return pptest_main(argc, argv);
1459 temp_files = NEW_ARR_F(char*, 0);
1460 atexit(free_temp_files);
1462 obstack_init(&cppflags_obst);
1463 obstack_init(&ldflags_obst);
1464 obstack_init(&asflags_obst);
1465 obstack_init(&file_obst);
1466 init_include_paths();
1468 #define GET_ARG_AFTER(def, args) \
1470 def = &arg[sizeof(args)-1]; \
1471 if (def[0] == '\0') { \
1474 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1475 argument_errors = true; \
1479 if (def[0] == '-' && def[1] != '\0') { \
1480 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1481 argument_errors = true; \
1487 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
1489 /* initialize this early because it has to parse options */
1492 /* early options parsing (find out optimization level and OS) */
1493 for (int i = 1; i < argc; ++i) {
1494 const char *arg = argv[i];
1498 const char *option = &arg[1];
1499 if (option[0] == 'O') {
1500 sscanf(&option[1], "%d", &opt_level);
1504 if (target_machine == NULL) {
1505 target_machine = firm_get_host_machine();
1507 choose_optimization_pack(opt_level);
1508 setup_target_machine();
1510 /* parse rest of options */
1511 lang_standard_t standard = STANDARD_DEFAULT;
1512 compilation_unit_type_t forced_unittype = COMPILATION_UNIT_AUTODETECT;
1513 help_sections_t help = HELP_NONE;
1514 bool argument_errors = false;
1515 for (int i = 1; i < argc; ++i) {
1516 const char *arg = argv[i];
1517 if (arg[0] == '-' && arg[1] != '\0') {
1519 const char *option = &arg[1];
1520 if (option[0] == 'o') {
1521 GET_ARG_AFTER(outname, "-o");
1522 } else if (option[0] == 'g') {
1523 /* TODO: parse -gX with 0<=x<=3... */
1524 set_be_option("debug=frameinfo");
1525 set_be_option("ia32-nooptcc=yes");
1526 } else if (SINGLE_OPTION('c')) {
1527 mode = CompileAssemble;
1528 } else if (SINGLE_OPTION('E')) {
1529 mode = PreprocessOnly;
1530 } else if (SINGLE_OPTION('s')) {
1531 add_flag(&ldflags_obst, "-s");
1532 } else if (SINGLE_OPTION('S')) {
1534 } else if (option[0] == 'O') {
1536 } else if (option[0] == 'I') {
1538 GET_ARG_AFTER(opt, "-I");
1539 add_flag(&cppflags_obst, "-I%s", opt);
1540 append_include_path(&bracket_searchpath, opt);
1541 } else if (option[0] == 'D') {
1543 GET_ARG_AFTER(opt, "-D");
1544 add_flag(&cppflags_obst, "-D%s", opt);
1545 } else if (option[0] == 'U') {
1547 GET_ARG_AFTER(opt, "-U");
1548 add_flag(&cppflags_obst, "-U%s", opt);
1549 } else if (option[0] == 'l') {
1551 GET_ARG_AFTER(opt, "-l");
1552 add_flag(&ldflags_obst, "-l%s", opt);
1553 } else if (option[0] == 'L') {
1555 GET_ARG_AFTER(opt, "-L");
1556 add_flag(&ldflags_obst, "-L%s", opt);
1557 } else if (SINGLE_OPTION('v')) {
1559 } else if (SINGLE_OPTION('w')) {
1560 add_flag(&cppflags_obst, "-w");
1561 disable_all_warnings();
1562 } else if (option[0] == 'x') {
1564 GET_ARG_AFTER(opt, "-x");
1565 forced_unittype = get_unit_type_from_string(opt);
1566 if (forced_unittype == COMPILATION_UNIT_UNKNOWN) {
1567 fprintf(stderr, "Unknown language '%s'\n", opt);
1568 argument_errors = true;
1570 } else if (streq(option, "M")) {
1571 mode = PreprocessOnly;
1572 add_flag(&cppflags_obst, "-M");
1573 } else if (streq(option, "MMD") ||
1574 streq(option, "MD")) {
1575 construct_dep_target = true;
1576 add_flag(&cppflags_obst, "-%s", option);
1577 } else if (streq(option, "MM") ||
1578 streq(option, "MP")) {
1579 add_flag(&cppflags_obst, "-%s", option);
1580 } else if (streq(option, "MT") ||
1581 streq(option, "MQ") ||
1582 streq(option, "MF")) {
1584 GET_ARG_AFTER(opt, "-MT");
1585 add_flag(&cppflags_obst, "-%s", option);
1586 add_flag(&cppflags_obst, "%s", opt);
1587 } else if (streq(option, "include")) {
1589 GET_ARG_AFTER(opt, "-include");
1590 add_flag(&cppflags_obst, "-include");
1591 add_flag(&cppflags_obst, "%s", opt);
1592 } else if (streq(option, "idirafter")) {
1594 GET_ARG_AFTER(opt, "-idirafter");
1595 add_flag(&cppflags_obst, "-idirafter");
1596 add_flag(&cppflags_obst, "%s", opt);
1597 append_include_path(&after_searchpath, opt);
1598 } else if (streq(option, "isystem")) {
1600 GET_ARG_AFTER(opt, "-isystem");
1601 add_flag(&cppflags_obst, "-isystem");
1602 add_flag(&cppflags_obst, "%s", opt);
1603 append_include_path(&system_searchpath, opt);
1604 } else if (streq(option, "iquote")) {
1606 GET_ARG_AFTER(opt, "-iquote");
1607 add_flag(&cppflags_obst, "-iquote");
1608 add_flag(&cppflags_obst, "%s", opt);
1609 append_include_path("e_searchpath, opt);
1610 } else if (streq(option, "pthread")) {
1611 /* set flags for the preprocessor */
1612 add_flag(&cppflags_obst, "-D_REENTRANT");
1613 /* set flags for the linker */
1614 add_flag(&ldflags_obst, "-lpthread");
1615 } else if (streq(option, "nostdinc")
1616 || streq(option, "trigraphs")) {
1617 /* pass these through to the preprocessor */
1618 add_flag(&cppflags_obst, "%s", arg);
1619 } else if (streq(option, "pipe")) {
1620 /* here for gcc compatibility */
1621 } else if (streq(option, "static")) {
1622 add_flag(&ldflags_obst, "-static");
1623 } else if (streq(option, "shared")) {
1624 add_flag(&ldflags_obst, "-shared");
1625 } else if (option[0] == 'f') {
1626 char const *orig_opt;
1627 GET_ARG_AFTER(orig_opt, "-f");
1629 if (strstart(orig_opt, "input-charset=")) {
1630 char const* const encoding = strchr(orig_opt, '=') + 1;
1631 input_encoding = encoding;
1632 } else if (strstart(orig_opt, "align-loops=") ||
1633 strstart(orig_opt, "align-jumps=") ||
1634 strstart(orig_opt, "align-functions=")) {
1635 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1636 } else if (strstart(orig_opt, "visibility=")) {
1637 const char *val = strchr(orig_opt, '=')+1;
1638 elf_visibility_tag_t visibility
1639 = get_elf_visibility_from_string(val);
1640 if (visibility == ELF_VISIBILITY_ERROR) {
1641 fprintf(stderr, "invalid visibility '%s' specified\n",
1643 argument_errors = true;
1645 set_default_visibility(visibility);
1647 } else if (strstart(orig_opt, "message-length=")) {
1648 /* ignore: would only affect error message format */
1649 } else if (streq(orig_opt, "fast-math") ||
1650 streq(orig_opt, "fp-fast")) {
1651 firm_fp_model = fp_model_fast;
1652 } else if (streq(orig_opt, "fp-precise")) {
1653 firm_fp_model = fp_model_precise;
1654 } else if (streq(orig_opt, "fp-strict")) {
1655 firm_fp_model = fp_model_strict;
1656 } else if (streq(orig_opt, "help")) {
1657 fprintf(stderr, "warning: -fhelp is deprecated\n");
1658 help |= HELP_OPTIMIZATION;
1660 /* -f options which have an -fno- variant */
1661 char const *opt = orig_opt;
1662 bool truth_value = true;
1663 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1664 truth_value = false;
1668 if (streq(opt, "diagnostics-show-option")) {
1669 diagnostics_show_option = truth_value;
1670 } else if (streq(opt, "dollars-in-identifiers")) {
1671 allow_dollar_in_symbol = truth_value;
1672 } else if (streq(opt, "omit-frame-pointer")) {
1673 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1674 } else if (streq(opt, "short-wchar")) {
1675 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1677 } else if (streq(opt, "show-column")) {
1678 show_column = truth_value;
1679 } else if (streq(opt, "signed-char")) {
1680 char_is_signed = truth_value;
1681 } else if (streq(opt, "strength-reduce")) {
1682 /* does nothing, for gcc compatibility (even gcc does
1683 * nothing for this switch anymore) */
1684 } else if (streq(opt, "syntax-only")) {
1685 mode = truth_value ? ParseOnly : CompileAssembleLink;
1686 } else if (streq(opt, "unsigned-char")) {
1687 char_is_signed = !truth_value;
1688 } else if (streq(opt, "freestanding")) {
1689 freestanding = truth_value;
1690 } else if (streq(opt, "hosted")) {
1691 freestanding = !truth_value;
1692 } else if (streq(opt, "profile-generate")) {
1693 profile_generate = truth_value;
1694 } else if (streq(opt, "profile-use")) {
1695 profile_use = truth_value;
1696 } else if (!truth_value &&
1697 streq(opt, "asynchronous-unwind-tables")) {
1698 /* nothing todo, a gcc feature which we do not support
1699 * anyway was deactivated */
1700 } else if (streq(opt, "verbose-asm")) {
1701 /* ignore: we always print verbose assembler */
1702 } else if (streq(opt, "jump-tables") ||
1703 streq(opt, "expensive-optimizations") ||
1704 streq(opt, "common") ||
1705 streq(opt, "optimize-sibling-calls") ||
1706 streq(opt, "align-loops") ||
1707 streq(opt, "align-jumps") ||
1708 streq(opt, "align-functions") ||
1709 streq(opt, "PIC") ||
1710 streq(opt, "stack-protector") ||
1711 streq(opt, "stack-protector-all")) {
1712 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1714 int res = firm_option(orig_opt);
1716 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1718 argument_errors = true;
1723 } else if (option[0] == 'b') {
1725 GET_ARG_AFTER(opt, "-b");
1727 if (streq(opt, "help")) {
1728 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1731 int res = be_parse_arg(opt);
1733 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1735 argument_errors = true;
1736 } else if (strstart(opt, "isa=")) {
1737 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1740 } else if (option[0] == 'W') {
1741 if (strstart(option + 1, "p,")) {
1742 // pass options directly to the preprocessor
1744 GET_ARG_AFTER(opt, "-Wp,");
1745 add_flag(&cppflags_obst, "-Wp,%s", opt);
1746 } else if (strstart(option + 1, "l,")) {
1747 // pass options directly to the linker
1749 GET_ARG_AFTER(opt, "-Wl,");
1750 add_flag(&ldflags_obst, "-Wl,%s", opt);
1751 } else if (streq(option + 1, "no-trigraphs")
1752 || streq(option + 1, "undef")
1753 || streq(option + 1, "missing-include-dirs")
1754 || streq(option + 1, "endif-labels")) {
1755 add_flag(&cppflags_obst, "%s", arg);
1756 } else if (streq(option+1, "init-self")) {
1757 /* ignored (same as gcc does) */
1758 } else if (streq(option+1, "format-y2k")
1759 || streq(option+1, "format-security")
1760 || streq(option+1, "old-style-declaration")
1761 || streq(option+1, "type-limits")) {
1762 /* ignore (gcc compatibility) */
1764 set_warning_opt(&option[1]);
1766 } else if (option[0] == 'm') {
1771 GET_ARG_AFTER(opt, "-m");
1772 if (strstart(opt, "target=")) {
1773 GET_ARG_AFTER(opt, "-mtarget=");
1774 if (!parse_target_triple(opt)) {
1775 argument_errors = true;
1777 const char *isa = setup_target_machine();
1778 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1779 target_triple = opt;
1781 } else if (strstart(opt, "triple=")) {
1782 GET_ARG_AFTER(opt, "-mtriple=");
1783 if (!parse_target_triple(opt)) {
1784 argument_errors = true;
1786 const char *isa = setup_target_machine();
1787 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1788 target_triple = opt;
1790 } else if (strstart(opt, "arch=")) {
1791 GET_ARG_AFTER(opt, "-march=");
1792 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1793 int res = be_parse_arg(arch_opt);
1794 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1795 res &= be_parse_arg(arch_opt);
1798 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1799 argument_errors = true;
1801 } else if (strstart(opt, "tune=")) {
1802 GET_ARG_AFTER(opt, "-mtune=");
1803 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1804 int res = be_parse_arg(arch_opt);
1806 argument_errors = true;
1807 } else if (strstart(opt, "cpu=")) {
1808 GET_ARG_AFTER(opt, "-mcpu=");
1809 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1810 int res = be_parse_arg(arch_opt);
1812 argument_errors = true;
1813 } else if (strstart(opt, "fpmath=")) {
1814 GET_ARG_AFTER(opt, "-mfpmath=");
1815 if (streq(opt, "387"))
1817 else if (streq(opt, "sse"))
1820 fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
1821 argument_errors = true;
1823 if (!argument_errors) {
1824 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1825 int res = be_parse_arg(arch_opt);
1827 argument_errors = true;
1829 } else if (strstart(opt, "preferred-stack-boundary=")) {
1830 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1831 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1832 int res = be_parse_arg(arch_opt);
1834 argument_errors = true;
1835 } else if (streq(opt, "rtd")) {
1836 default_calling_convention = CC_STDCALL;
1837 } else if (strstart(opt, "regparm=")) {
1838 fprintf(stderr, "error: regparm convention not supported yet\n");
1839 argument_errors = true;
1840 } else if (streq(opt, "soft-float")) {
1841 add_flag(&ldflags_obst, "-msoft-float");
1842 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
1843 int res = be_parse_arg(arch_opt);
1845 argument_errors = true;
1846 } else if (streq(opt, "sse2")) {
1847 /* ignore for now, our x86 backend always uses sse when
1848 * sse is requested */
1850 long int value = strtol(opt, NULL, 10);
1852 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1853 argument_errors = true;
1854 } else if (value != 16 && value != 32 && value != 64) {
1855 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1856 argument_errors = true;
1858 unsigned machine_size = (unsigned)value;
1859 /* TODO: choose/change backend based on this */
1860 add_flag(&cppflags_obst, "-m%u", machine_size);
1861 add_flag(&asflags_obst, "-m%u", machine_size);
1862 add_flag(&ldflags_obst, "-m%u", machine_size);
1865 } else if (streq(option, "pg")) {
1866 set_be_option("gprof");
1867 add_flag(&ldflags_obst, "-pg");
1868 } else if (streq(option, "ansi")) {
1869 standard = STANDARD_ANSI;
1870 } else if (streq(option, "pedantic")) {
1871 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1872 } else if (strstart(option, "std=")) {
1873 const char *const o = &option[4];
1875 streq(o, "c++") ? STANDARD_CXX98 :
1876 streq(o, "c++98") ? STANDARD_CXX98 :
1877 streq(o, "c11") ? STANDARD_C11 :
1878 streq(o, "c1x") ? STANDARD_C11 : // deprecated
1879 streq(o, "c89") ? STANDARD_C89 :
1880 streq(o, "c90") ? STANDARD_C89 :
1881 streq(o, "c99") ? STANDARD_C99 :
1882 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1883 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1884 streq(o, "gnu11") ? STANDARD_GNU11 :
1885 streq(o, "gnu1x") ? STANDARD_GNU11 : // deprecated
1886 streq(o, "gnu89") ? STANDARD_GNU89 :
1887 streq(o, "gnu99") ? STANDARD_GNU99 :
1888 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1889 streq(o, "iso9899:1990") ? STANDARD_C89 :
1890 streq(o, "iso9899:199409") ? STANDARD_C89AMD1 :
1891 streq(o, "iso9899:1999") ? STANDARD_C99 :
1892 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1893 streq(o, "iso9899:2011") ? STANDARD_C11 :
1894 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1895 } else if (streq(option, "version")) {
1896 print_cparser_version();
1897 return EXIT_SUCCESS;
1898 } else if (streq(option, "dumpversion")) {
1899 /* gcc compatibility option */
1900 print_cparser_version_short();
1901 return EXIT_SUCCESS;
1902 } else if (strstart(option, "print-file-name=")) {
1903 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1904 } else if (option[0] == '-') {
1905 /* double dash option */
1907 if (streq(option, "gcc")) {
1908 features_on |= _GNUC;
1909 features_off &= ~_GNUC;
1910 } else if (streq(option, "no-gcc")) {
1911 features_on &= ~_GNUC;
1912 features_off |= _GNUC;
1913 } else if (streq(option, "ms")) {
1915 features_off &= ~_MS;
1916 } else if (streq(option, "no-ms")) {
1917 features_on &= ~_MS;
1918 features_off |= _MS;
1919 } else if (streq(option, "strict")) {
1921 } else if (streq(option, "benchmark")) {
1922 mode = BenchmarkParser;
1923 } else if (streq(option, "print-ast")) {
1925 } else if (streq(option, "print-implicit-cast")) {
1926 print_implicit_casts = true;
1927 } else if (streq(option, "print-parenthesis")) {
1928 print_parenthesis = true;
1929 } else if (streq(option, "print-fluffy")) {
1931 } else if (streq(option, "print-compound-sizes")) {
1932 mode = PrintCompoundSizes;
1933 } else if (streq(option, "print-jna")) {
1935 } else if (streq(option, "jna-limit")) {
1938 fprintf(stderr, "error: "
1939 "expected argument after '--jna-limit'\n");
1940 argument_errors = true;
1943 jna_limit_output(argv[i]);
1944 } else if (streq(option, "jna-libname")) {
1947 fprintf(stderr, "error: "
1948 "expected argument after '--jna-libname'\n");
1949 argument_errors = true;
1952 jna_set_libname(argv[i]);
1953 } else if (streq(option, "external-pp")) {
1954 if (i+1 < argc && argv[i+1][0] != '-') {
1956 external_preprocessor = argv[i+1];
1958 external_preprocessor = PREPROCESSOR;
1960 } else if (streq(option, "no-external-pp")) {
1961 external_preprocessor = NULL;
1962 } else if (streq(option, "time")) {
1964 } else if (streq(option, "version")) {
1965 print_cparser_version();
1966 return EXIT_SUCCESS;
1967 } else if (streq(option, "help")) {
1969 } else if (streq(option, "help-parser")) {
1970 help |= HELP_PARSER;
1971 } else if (streq(option, "help-warnings")) {
1972 help |= HELP_WARNINGS;
1973 } else if (streq(option, "help-codegen")) {
1974 help |= HELP_CODEGEN;
1975 } else if (streq(option, "help-linker")) {
1976 help |= HELP_LINKER;
1977 } else if (streq(option, "help-optimization")) {
1978 help |= HELP_OPTIMIZATION;
1979 } else if (streq(option, "help-language-tools")) {
1980 help |= HELP_LANGUAGETOOLS;
1981 } else if (streq(option, "help-debug")) {
1983 } else if (streq(option, "help-firm")) {
1985 } else if (streq(option, "help-all")) {
1987 } else if (streq(option, "dump-function")) {
1990 fprintf(stderr, "error: "
1991 "expected argument after '--dump-function'\n");
1992 argument_errors = true;
1995 dumpfunction = argv[i];
1997 } else if (streq(option, "export-ir")) {
1998 mode = CompileExportIR;
1999 } else if (streq(option, "unroll-loops")) {
2000 /* ignore (gcc compatibility) */
2002 fprintf(stderr, "error: unknown argument '%s'\n", arg);
2003 argument_errors = true;
2006 fprintf(stderr, "error: unknown argument '%s'\n", arg);
2007 argument_errors = true;
2010 compilation_unit_type_t type = forced_unittype;
2011 if (type == COMPILATION_UNIT_AUTODETECT) {
2012 if (streq(arg, "-")) {
2013 /* - implicitly means C source file */
2014 type = COMPILATION_UNIT_C;
2016 const char *suffix = strrchr(arg, '.');
2017 /* Ensure there is at least one char before the suffix */
2018 if (suffix != NULL && suffix != arg) {
2021 streq(suffix, "S") ? COMPILATION_UNIT_ASSEMBLER :
2022 streq(suffix, "a") ? COMPILATION_UNIT_OBJECT :
2023 streq(suffix, "c") ? COMPILATION_UNIT_C :
2024 streq(suffix, "i") ? COMPILATION_UNIT_PREPROCESSED_C :
2025 streq(suffix, "C") ? COMPILATION_UNIT_CXX :
2026 streq(suffix, "cc") ? COMPILATION_UNIT_CXX :
2027 streq(suffix, "cp") ? COMPILATION_UNIT_CXX :
2028 streq(suffix, "cpp") ? COMPILATION_UNIT_CXX :
2029 streq(suffix, "CPP") ? COMPILATION_UNIT_CXX :
2030 streq(suffix, "cxx") ? COMPILATION_UNIT_CXX :
2031 streq(suffix, "c++") ? COMPILATION_UNIT_CXX :
2032 streq(suffix, "ii") ? COMPILATION_UNIT_PREPROCESSED_CXX :
2033 streq(suffix, "h") ? COMPILATION_UNIT_C :
2034 streq(suffix, "ir") ? COMPILATION_UNIT_IR :
2035 streq(suffix, "o") ? COMPILATION_UNIT_OBJECT :
2036 streq(suffix, "s") ? COMPILATION_UNIT_PREPROCESSED_ASSEMBLER :
2037 streq(suffix, "so") ? COMPILATION_UNIT_OBJECT :
2038 COMPILATION_UNIT_OBJECT; /* gcc behavior: unknown file extension means object file */
2043 compilation_unit_t *entry = OALLOCZ(&file_obst, compilation_unit_t);
2047 if (last_unit != NULL) {
2048 last_unit->next = entry;
2056 if (help != HELP_NONE) {
2057 print_help(argv[0], help);
2058 return !argument_errors;
2061 if (print_file_name_file != NULL) {
2062 print_file_name(print_file_name_file);
2063 return EXIT_SUCCESS;
2065 if (units == NULL) {
2066 fprintf(stderr, "error: no input files specified\n");
2067 argument_errors = true;
2070 if (argument_errors) {
2072 return EXIT_FAILURE;
2075 /* apply some effects from switches */
2076 c_mode |= features_on;
2077 c_mode &= ~features_off;
2078 if (profile_generate) {
2079 add_flag(&ldflags_obst, "-lfirmprof");
2080 set_be_option("profilegenerate");
2083 set_be_option("profileuse");
2086 init_symbol_table();
2087 init_types_and_adjust();
2090 if (c_mode & _CXX) {
2091 init_wchar_types(ATOMIC_TYPE_WCHAR_T);
2093 init_wchar_types(wchar_atomic_kind);
2095 init_preprocessor();
2104 if (construct_dep_target) {
2105 if (outname != 0 && strlen(outname) >= 2) {
2106 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
2108 get_output_name(dep_target, sizeof(dep_target), units->name, ".d");
2111 dep_target[0] = '\0';
2114 char outnamebuf[4096];
2115 if (outname == NULL) {
2116 const char *filename = units->name;
2119 case BenchmarkParser:
2123 case PrintCompoundSizes:
2124 case PreprocessOnly:
2129 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
2130 outname = outnamebuf;
2132 case CompileAssemble:
2133 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
2134 outname = outnamebuf;
2137 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
2139 outname = outnamebuf;
2141 case CompileExportIR:
2142 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
2143 outname = outnamebuf;
2145 case CompileAssembleLink:
2146 if (firm_is_windows_os(target_machine)) {
2155 assert(outname != NULL);
2158 if (streq(outname, "-")) {
2161 out = fopen(outname, "w");
2163 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
2165 return EXIT_FAILURE;
2169 int result = compilation_loop(mode, units, standard, out);
2170 if (result != EXIT_SUCCESS) {
2176 /* link program file */
2177 if (mode == CompileAssembleLink) {
2178 int result = link_program(units);
2179 if (result != EXIT_SUCCESS) {
2190 obstack_free(&cppflags_obst, NULL);
2191 obstack_free(&ldflags_obst, NULL);
2192 obstack_free(&asflags_obst, NULL);
2193 obstack_free(&file_obst, NULL);
2200 exit_preprocessor();
2204 exit_symbol_table();
2205 return EXIT_SUCCESS;