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__"
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_push(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_pop(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 (machine_size == 32) {
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 (machine_size == 64) {
998 /* to ease porting of old c-code microsoft decided to use 32bits
1000 props[ATOMIC_TYPE_LONG] = props[ATOMIC_TYPE_INT];
1001 props[ATOMIC_TYPE_ULONG] = props[ATOMIC_TYPE_UINT];
1004 /* on windows long double is not supported */
1005 props[ATOMIC_TYPE_LONG_DOUBLE] = props[ATOMIC_TYPE_DOUBLE];
1006 } else if (firm_is_unixish_os(target_machine)) {
1007 if (is_ia32_cpu(target_machine->cpu_type)) {
1008 /* System V has a broken alignment for double so we have to add
1010 props[ATOMIC_TYPE_DOUBLE].struct_alignment = 4;
1011 props[ATOMIC_TYPE_LONGLONG].struct_alignment = 4;
1012 props[ATOMIC_TYPE_ULONGLONG].struct_alignment = 4;
1016 /* stuff decided after processing operating system specifics and
1017 * commandline flags */
1018 if (char_is_signed) {
1019 props[ATOMIC_TYPE_CHAR].flags |= ATOMIC_TYPE_FLAG_SIGNED;
1021 props[ATOMIC_TYPE_CHAR].flags &= ~ATOMIC_TYPE_FLAG_SIGNED;
1023 /* copy over wchar_t properties (including rank) */
1024 props[ATOMIC_TYPE_WCHAR_T] = props[wchar_atomic_kind];
1026 /* initialize defaults for unsupported types */
1027 if (type_long_long == NULL) {
1028 copy_typeprops(&props[ATOMIC_TYPE_LONGLONG], &props[ATOMIC_TYPE_LONG]);
1030 if (type_unsigned_long_long == NULL) {
1031 copy_typeprops(&props[ATOMIC_TYPE_ULONGLONG],
1032 &props[ATOMIC_TYPE_ULONG]);
1034 if (type_long_double == NULL) {
1035 copy_typeprops(&props[ATOMIC_TYPE_LONG_DOUBLE],
1036 &props[ATOMIC_TYPE_DOUBLE]);
1039 /* initialize firm pointer modes */
1041 unsigned bit_size = machine_size;
1042 unsigned modulo_shift = decide_modulo_shift(bit_size);
1044 snprintf(name, sizeof(name), "p%u", machine_size);
1045 ir_mode *ptr_mode = new_reference_mode(name, irma_twos_complement, bit_size, modulo_shift);
1047 if (machine_size == 16) {
1048 set_reference_mode_signed_eq(ptr_mode, mode_Hs);
1049 set_reference_mode_unsigned_eq(ptr_mode, mode_Hu);
1050 } else if (machine_size == 32) {
1051 set_reference_mode_signed_eq(ptr_mode, mode_Is);
1052 set_reference_mode_unsigned_eq(ptr_mode, mode_Iu);
1053 } else if (machine_size == 64) {
1054 set_reference_mode_signed_eq(ptr_mode, mode_Ls);
1055 set_reference_mode_unsigned_eq(ptr_mode, mode_Lu);
1057 panic("strange machine_size when determining pointer modes");
1060 /* Hmm, pointers should be machine size */
1061 set_modeP_data(ptr_mode);
1062 set_modeP_code(ptr_mode);
1064 byte_order_big_endian = be_params->byte_order_big_endian;
1065 if (be_params->modulo_shift_efficient) {
1066 architecture_modulo_shift = machine_size;
1068 architecture_modulo_shift = 0;
1072 static void setup_cmode(const compilation_unit_t *unit)
1074 compilation_unit_type_t type = unit->type;
1075 lang_standard_t standard = unit->standard;
1076 if (type == COMPILATION_UNIT_PREPROCESSED_C || type == COMPILATION_UNIT_C) {
1078 case STANDARD_C89: c_mode = _C89; break;
1079 /* TODO determine difference between these two */
1080 case STANDARD_C89AMD1: c_mode = _C89; break;
1081 case STANDARD_C99: c_mode = _C89 | _C99; break;
1082 case STANDARD_C11: c_mode = _C89 | _C99 | _C11; break;
1083 case STANDARD_GNU89: c_mode = _C89 | _GNUC; break;
1084 case STANDARD_GNU11: c_mode = _C89 | _C99 | _C11 | _GNUC; break;
1087 case STANDARD_CXX98:
1088 case STANDARD_GNUXX98:
1089 case STANDARD_DEFAULT:
1090 fprintf(stderr, "warning: command line option \"-std=%s\" is not valid for C\n", str_lang_standard(standard));
1092 case STANDARD_GNU99: c_mode = _C89 | _C99 | _GNUC; break;
1094 } else if (type == COMPILATION_UNIT_PREPROCESSED_CXX
1095 || type == COMPILATION_UNIT_CXX) {
1097 case STANDARD_CXX98: c_mode = _CXX; break;
1101 case STANDARD_C89AMD1:
1104 case STANDARD_GNU89:
1105 case STANDARD_GNU99:
1106 case STANDARD_GNU11:
1107 case STANDARD_DEFAULT:
1108 fprintf(stderr, "warning: command line option \"-std=%s\" is not valid for C++\n", str_lang_standard(standard));
1110 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1114 c_mode |= features_on;
1115 c_mode &= ~features_off;
1118 static void determine_unit_standard(compilation_unit_t *unit,
1119 lang_standard_t standard)
1121 unit->standard = standard;
1124 switch (unit->type) {
1125 case COMPILATION_UNIT_C:
1126 case COMPILATION_UNIT_PREPROCESSED_C:
1127 unit->standard = STANDARD_C89;
1129 case COMPILATION_UNIT_CXX:
1130 case COMPILATION_UNIT_PREPROCESSED_CXX:
1131 unit->standard = STANDARD_CXX98;
1138 case STANDARD_DEFAULT:
1139 switch (unit->type) {
1140 case COMPILATION_UNIT_C:
1141 case COMPILATION_UNIT_PREPROCESSED_C:
1142 unit->standard = STANDARD_GNU99;
1144 case COMPILATION_UNIT_CXX:
1145 case COMPILATION_UNIT_PREPROCESSED_CXX:
1146 unit->standard = STANDARD_GNUXX98;
1158 static bool output_preprocessor_tokens(compilation_unit_t *unit, FILE *out)
1160 /* just here for gcc compatibility */
1161 fprintf(out, "# 1 \"%s\"\n", unit->name);
1162 fprintf(out, "# 1 \"<built-in>\"\n");
1163 fprintf(out, "# 1 \"<command-line>\"\n");
1165 set_preprocessor_output(out);
1166 switch_pp_input(unit->input, unit->name, NULL, false);
1169 next_preprocessing_token();
1170 if (pp_token.kind == T_EOF)
1176 check_unclosed_conditionals();
1178 print_error_summary();
1179 set_preprocessor_output(NULL);
1181 if (unit->type == COMPILATION_UNIT_C) {
1182 unit->type = COMPILATION_UNIT_PREPROCESSED_C;
1183 } else if (unit->type == COMPILATION_UNIT_CXX) {
1184 unit->type = COMPILATION_UNIT_PREPROCESSED_CXX;
1186 bool res = close_input(unit);
1187 return res && error_count == 0;
1190 static bool open_input(compilation_unit_t *unit)
1192 /* input already available as FILE? */
1193 if (unit->input != NULL)
1196 const char *const inputname = unit->name;
1197 unit->input_is_pipe = false;
1198 if (streq(inputname, "-")) {
1199 unit->input = stdin;
1201 unit->input = fopen(inputname, "r");
1202 if (unit->input == NULL) {
1203 fprintf(stderr, "Could not open '%s': %s\n", inputname,
1211 static int compilation_loop(compile_mode_t mode, compilation_unit_t *units,
1212 lang_standard_t standard, FILE *out)
1214 int result = EXIT_SUCCESS;
1215 bool already_constructed_firm = false;
1216 for (compilation_unit_t *unit = units; unit != NULL; unit = unit->next) {
1217 const char *const inputname = unit->name;
1219 determine_unit_standard(unit, standard);
1223 switch (unit->type) {
1224 case COMPILATION_UNIT_IR: {
1225 bool res = open_input(unit);
1227 result = EXIT_FAILURE;
1230 res = !ir_import_file(unit->input, unit->name);
1232 fprintf(stderr, "Import of firm graph from '%s' failed\n",
1234 result = EXIT_FAILURE;
1237 unit->type = COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION;
1240 case COMPILATION_UNIT_ASSEMBLER:
1241 panic("TODO: preprocess for assembler");
1242 case COMPILATION_UNIT_C:
1243 case COMPILATION_UNIT_CXX:
1244 if (external_preprocessor != NULL) {
1245 bool res = run_external_preprocessor(unit);
1247 result = EXIT_FAILURE;
1254 case COMPILATION_UNIT_PREPROCESSED_C:
1255 case COMPILATION_UNIT_PREPROCESSED_CXX: {
1256 bool res = open_input(unit);
1258 result = EXIT_FAILURE;
1263 if (mode == PreprocessOnly) {
1264 bool res = output_preprocessor_tokens(unit, out);
1266 result = EXIT_FAILURE;
1272 /* do the actual parsing */
1276 case COMPILATION_UNIT_AST:
1277 /* prints the AST even if errors occurred */
1278 if (mode == PrintAst) {
1280 print_ast(unit->ast);
1282 if (unit->parse_errors) {
1283 result = EXIT_FAILURE;
1287 if (mode == BenchmarkParser) {
1289 } else if (mode == PrintFluffy) {
1290 write_fluffy_decls(out, unit->ast);
1292 } else if (mode == PrintJna) {
1293 write_jna_decls(out, unit->ast);
1295 } else if (mode == PrintCompoundSizes) {
1296 write_compoundsizes(out, unit->ast);
1300 /* build the firm graph */
1301 ir_timer_t *t_construct = ir_timer_new();
1302 timer_register(t_construct, "Frontend: Graph construction");
1303 timer_push(t_construct);
1304 if (already_constructed_firm) {
1305 panic("compiling multiple files/translation units not possible");
1307 init_implicit_optimizations();
1308 translation_unit_to_firm(unit->ast);
1309 already_constructed_firm = true;
1310 timer_pop(t_construct);
1311 unit->type = COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION;
1314 case COMPILATION_UNIT_INTERMEDIATE_REPRESENTATION:
1315 if (mode == ParseOnly)
1318 if (mode == CompileDump) {
1320 ident *id = new_id_from_str(dumpfunction);
1321 ir_graph *irg = NULL;
1322 int n_irgs = get_irp_n_irgs();
1323 for (int i = 0; i < n_irgs; ++i) {
1324 ir_graph *tirg = get_irp_irg(i);
1325 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
1333 fprintf(stderr, "No graph for function '%s' found\n",
1335 return EXIT_FAILURE;
1338 dump_ir_graph_file(out, irg);
1340 return EXIT_SUCCESS;
1343 if (mode == CompileExportIR) {
1344 ir_export_file(out);
1345 if (ferror(out) != 0) {
1346 fprintf(stderr, "Error while writing to output\n");
1347 return EXIT_FAILURE;
1349 return EXIT_SUCCESS;
1353 if (mode == Compile) {
1356 asm_out = make_temp_file("ccs", &unit->name);
1358 generate_code(asm_out, inputname);
1359 if (asm_out != out) {
1362 unit->type = COMPILATION_UNIT_PREPROCESSED_ASSEMBLER;
1364 case COMPILATION_UNIT_PREPROCESSED_ASSEMBLER:
1365 if (mode != CompileAssemble && mode != CompileAssembleLink)
1369 const char *input = unit->name;
1370 if (mode == CompileAssemble) {
1372 unit->name = outname;
1374 FILE *tempf = make_temp_file("cco", &unit->name);
1379 assemble(unit->name, input);
1381 unit->type = COMPILATION_UNIT_OBJECT;
1383 case COMPILATION_UNIT_UNKNOWN:
1384 case COMPILATION_UNIT_AUTODETECT:
1385 case COMPILATION_UNIT_OBJECT:
1392 static int link_program(compilation_unit_t *units)
1394 obstack_1grow(&ldflags_obst, '\0');
1395 const char *flags = obstack_finish(&ldflags_obst);
1397 /* construct commandline */
1398 const char *linker = getenv("CPARSER_LINK");
1399 if (linker != NULL) {
1400 obstack_printf(&file_obst, "%s ", linker);
1402 if (target_triple != NULL)
1403 obstack_printf(&file_obst, "%s-", target_triple);
1404 obstack_printf(&file_obst, "%s ", LINKER);
1407 for (compilation_unit_t *unit = units; unit != NULL; unit = unit->next) {
1408 if (unit->type != COMPILATION_UNIT_OBJECT)
1411 add_flag(&file_obst, "%s", unit->name);
1414 add_flag(&file_obst, "-o");
1415 add_flag(&file_obst, outname);
1416 obstack_printf(&file_obst, "%s", flags);
1417 obstack_1grow(&file_obst, '\0');
1419 char *commandline = obstack_finish(&file_obst);
1424 int err = system(commandline);
1425 if (err != EXIT_SUCCESS) {
1426 fprintf(stderr, "linker reported an error\n");
1427 return EXIT_FAILURE;
1429 return EXIT_SUCCESS;
1432 int main(int argc, char **argv)
1434 const char *print_file_name_file = NULL;
1435 compile_mode_t mode = CompileAssembleLink;
1437 char cpu_arch[16] = "ia32";
1438 compilation_unit_t *units = NULL;
1439 compilation_unit_t *last_unit = NULL;
1440 bool construct_dep_target = false;
1441 bool do_timing = false;
1442 bool profile_generate = false;
1443 bool profile_use = false;
1445 /* hack for now... */
1446 if (strstr(argv[0], "pptest") != NULL) {
1447 extern int pptest_main(int argc, char **argv);
1448 return pptest_main(argc, argv);
1451 temp_files = NEW_ARR_F(char*, 0);
1452 atexit(free_temp_files);
1454 obstack_init(&cppflags_obst);
1455 obstack_init(&ldflags_obst);
1456 obstack_init(&asflags_obst);
1457 obstack_init(&file_obst);
1458 init_include_paths();
1460 #define GET_ARG_AFTER(def, args) \
1462 def = &arg[sizeof(args)-1]; \
1463 if (def[0] == '\0') { \
1466 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1467 argument_errors = true; \
1471 if (def[0] == '-' && def[1] != '\0') { \
1472 fprintf(stderr, "error: expected argument after '" args "'\n"); \
1473 argument_errors = true; \
1479 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
1481 /* initialize this early because it has to parse options */
1484 /* early options parsing (find out optimization level and OS) */
1485 for (int i = 1; i < argc; ++i) {
1486 const char *arg = argv[i];
1490 const char *option = &arg[1];
1491 if (option[0] == 'O') {
1492 sscanf(&option[1], "%d", &opt_level);
1496 if (target_machine == NULL) {
1497 target_machine = firm_get_host_machine();
1499 choose_optimization_pack(opt_level);
1500 setup_target_machine();
1502 /* parse rest of options */
1503 lang_standard_t standard = STANDARD_DEFAULT;
1504 compilation_unit_type_t forced_unittype = COMPILATION_UNIT_AUTODETECT;
1505 help_sections_t help = HELP_NONE;
1506 bool argument_errors = false;
1507 for (int i = 1; i < argc; ++i) {
1508 const char *arg = argv[i];
1509 if (arg[0] == '-' && arg[1] != '\0') {
1511 const char *option = &arg[1];
1512 if (option[0] == 'o') {
1513 GET_ARG_AFTER(outname, "-o");
1514 } else if (option[0] == 'g') {
1515 /* TODO: parse -gX with 0<=x<=3... */
1516 set_be_option("debug=frameinfo");
1517 set_be_option("ia32-nooptcc=yes");
1518 } else if (SINGLE_OPTION('c')) {
1519 mode = CompileAssemble;
1520 } else if (SINGLE_OPTION('E')) {
1521 mode = PreprocessOnly;
1522 } else if (SINGLE_OPTION('s')) {
1523 add_flag(&ldflags_obst, "-s");
1524 } else if (SINGLE_OPTION('S')) {
1526 } else if (option[0] == 'O') {
1528 } else if (option[0] == 'I') {
1530 GET_ARG_AFTER(opt, "-I");
1531 add_flag(&cppflags_obst, "-I%s", opt);
1532 append_include_path(&bracket_searchpath, opt);
1533 } else if (option[0] == 'D') {
1535 GET_ARG_AFTER(opt, "-D");
1536 add_flag(&cppflags_obst, "-D%s", opt);
1537 } else if (option[0] == 'U') {
1539 GET_ARG_AFTER(opt, "-U");
1540 add_flag(&cppflags_obst, "-U%s", opt);
1541 } else if (option[0] == 'l') {
1543 GET_ARG_AFTER(opt, "-l");
1544 add_flag(&ldflags_obst, "-l%s", opt);
1545 } else if (option[0] == 'L') {
1547 GET_ARG_AFTER(opt, "-L");
1548 add_flag(&ldflags_obst, "-L%s", opt);
1549 } else if (SINGLE_OPTION('v')) {
1551 } else if (SINGLE_OPTION('w')) {
1552 add_flag(&cppflags_obst, "-w");
1553 disable_all_warnings();
1554 } else if (option[0] == 'x') {
1556 GET_ARG_AFTER(opt, "-x");
1557 forced_unittype = get_unit_type_from_string(opt);
1558 if (forced_unittype == COMPILATION_UNIT_UNKNOWN) {
1559 fprintf(stderr, "Unknown language '%s'\n", opt);
1560 argument_errors = true;
1562 } else if (streq(option, "M")) {
1563 mode = PreprocessOnly;
1564 add_flag(&cppflags_obst, "-M");
1565 } else if (streq(option, "MMD") ||
1566 streq(option, "MD")) {
1567 construct_dep_target = true;
1568 add_flag(&cppflags_obst, "-%s", option);
1569 } else if (streq(option, "MM") ||
1570 streq(option, "MP")) {
1571 add_flag(&cppflags_obst, "-%s", option);
1572 } else if (streq(option, "MT") ||
1573 streq(option, "MQ") ||
1574 streq(option, "MF")) {
1576 GET_ARG_AFTER(opt, "-MT");
1577 add_flag(&cppflags_obst, "-%s", option);
1578 add_flag(&cppflags_obst, "%s", opt);
1579 } else if (streq(option, "include")) {
1581 GET_ARG_AFTER(opt, "-include");
1582 add_flag(&cppflags_obst, "-include");
1583 add_flag(&cppflags_obst, "%s", opt);
1584 } else if (streq(option, "idirafter")) {
1586 GET_ARG_AFTER(opt, "-idirafter");
1587 add_flag(&cppflags_obst, "-idirafter");
1588 add_flag(&cppflags_obst, "%s", opt);
1589 append_include_path(&after_searchpath, opt);
1590 } else if (streq(option, "isystem")) {
1592 GET_ARG_AFTER(opt, "-isystem");
1593 add_flag(&cppflags_obst, "-isystem");
1594 add_flag(&cppflags_obst, "%s", opt);
1595 append_include_path(&system_searchpath, opt);
1596 } else if (streq(option, "iquote")) {
1598 GET_ARG_AFTER(opt, "-iquote");
1599 add_flag(&cppflags_obst, "-iquote");
1600 add_flag(&cppflags_obst, "%s", opt);
1601 append_include_path("e_searchpath, opt);
1602 } else if (streq(option, "pthread")) {
1603 /* set flags for the preprocessor */
1604 add_flag(&cppflags_obst, "-D_REENTRANT");
1605 /* set flags for the linker */
1606 add_flag(&ldflags_obst, "-lpthread");
1607 } else if (streq(option, "nostdinc")
1608 || streq(option, "trigraphs")) {
1609 /* pass these through to the preprocessor */
1610 add_flag(&cppflags_obst, "%s", arg);
1611 } else if (streq(option, "pipe")) {
1612 /* here for gcc compatibility */
1613 } else if (streq(option, "static")) {
1614 add_flag(&ldflags_obst, "-static");
1615 } else if (streq(option, "shared")) {
1616 add_flag(&ldflags_obst, "-shared");
1617 } else if (option[0] == 'f') {
1618 char const *orig_opt;
1619 GET_ARG_AFTER(orig_opt, "-f");
1621 if (strstart(orig_opt, "input-charset=")) {
1622 char const* const encoding = strchr(orig_opt, '=') + 1;
1623 input_encoding = encoding;
1624 } else if (strstart(orig_opt, "align-loops=") ||
1625 strstart(orig_opt, "align-jumps=") ||
1626 strstart(orig_opt, "align-functions=")) {
1627 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1628 } else if (strstart(orig_opt, "visibility=")) {
1629 const char *val = strchr(orig_opt, '=')+1;
1630 elf_visibility_tag_t visibility
1631 = get_elf_visibility_from_string(val);
1632 if (visibility == ELF_VISIBILITY_ERROR) {
1633 fprintf(stderr, "invalid visibility '%s' specified\n",
1635 argument_errors = true;
1637 set_default_visibility(visibility);
1639 } else if (strstart(orig_opt, "message-length=")) {
1640 /* ignore: would only affect error message format */
1641 } else if (streq(orig_opt, "fast-math") ||
1642 streq(orig_opt, "fp-fast")) {
1643 firm_fp_model = fp_model_fast;
1644 } else if (streq(orig_opt, "fp-precise")) {
1645 firm_fp_model = fp_model_precise;
1646 } else if (streq(orig_opt, "fp-strict")) {
1647 firm_fp_model = fp_model_strict;
1648 } else if (streq(orig_opt, "help")) {
1649 fprintf(stderr, "warning: -fhelp is deprecated\n");
1650 help |= HELP_OPTIMIZATION;
1652 /* -f options which have an -fno- variant */
1653 char const *opt = orig_opt;
1654 bool truth_value = true;
1655 if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
1656 truth_value = false;
1660 if (streq(opt, "diagnostics-show-option")) {
1661 diagnostics_show_option = truth_value;
1662 } else if (streq(opt, "dollars-in-identifiers")) {
1663 allow_dollar_in_symbol = truth_value;
1664 } else if (streq(opt, "omit-frame-pointer")) {
1665 set_be_option(truth_value ? "omitfp" : "omitfp=no");
1666 } else if (streq(opt, "short-wchar")) {
1667 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
1669 } else if (streq(opt, "show-column")) {
1670 show_column = truth_value;
1671 } else if (streq(opt, "signed-char")) {
1672 char_is_signed = truth_value;
1673 } else if (streq(opt, "strength-reduce")) {
1674 /* does nothing, for gcc compatibility (even gcc does
1675 * nothing for this switch anymore) */
1676 } else if (streq(opt, "syntax-only")) {
1677 mode = truth_value ? ParseOnly : CompileAssembleLink;
1678 } else if (streq(opt, "unsigned-char")) {
1679 char_is_signed = !truth_value;
1680 } else if (streq(opt, "freestanding")) {
1681 freestanding = truth_value;
1682 } else if (streq(opt, "hosted")) {
1683 freestanding = !truth_value;
1684 } else if (streq(opt, "profile-generate")) {
1685 profile_generate = truth_value;
1686 } else if (streq(opt, "profile-use")) {
1687 profile_use = truth_value;
1688 } else if (!truth_value &&
1689 streq(opt, "asynchronous-unwind-tables")) {
1690 /* nothing todo, a gcc feature which we do not support
1691 * anyway was deactivated */
1692 } else if (streq(opt, "verbose-asm")) {
1693 /* ignore: we always print verbose assembler */
1694 } else if (streq(opt, "jump-tables") ||
1695 streq(opt, "expensive-optimizations") ||
1696 streq(opt, "common") ||
1697 streq(opt, "optimize-sibling-calls") ||
1698 streq(opt, "align-loops") ||
1699 streq(opt, "align-jumps") ||
1700 streq(opt, "align-functions") ||
1701 streq(opt, "unroll-loops") ||
1702 streq(opt, "PIC") ||
1703 streq(opt, "stack-protector") ||
1704 streq(opt, "stack-protector-all")) {
1705 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
1707 int res = firm_option(orig_opt);
1709 fprintf(stderr, "error: unknown Firm option '-f%s'\n",
1711 argument_errors = true;
1716 } else if (option[0] == 'b') {
1718 GET_ARG_AFTER(opt, "-b");
1720 if (streq(opt, "help")) {
1721 fprintf(stderr, "warning: -bhelp is deprecated (use --help-firm)\n");
1724 int res = be_parse_arg(opt);
1726 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
1728 argument_errors = true;
1729 } else if (strstart(opt, "isa=")) {
1730 strncpy(cpu_arch, opt, sizeof(cpu_arch));
1733 } else if (option[0] == 'W') {
1734 if (strstart(option + 1, "p,")) {
1735 // pass options directly to the preprocessor
1737 GET_ARG_AFTER(opt, "-Wp,");
1738 add_flag(&cppflags_obst, "-Wp,%s", opt);
1739 } else if (strstart(option + 1, "l,")) {
1740 // pass options directly to the linker
1742 GET_ARG_AFTER(opt, "-Wl,");
1743 add_flag(&ldflags_obst, "-Wl,%s", opt);
1744 } else if (streq(option + 1, "no-trigraphs")
1745 || streq(option + 1, "undef")
1746 || streq(option + 1, "missing-include-dirs")
1747 || streq(option + 1, "endif-labels")) {
1748 add_flag(&cppflags_obst, "%s", arg);
1749 } else if (streq(option+1, "init-self")) {
1750 /* ignored (same as gcc does) */
1751 } else if (streq(option+1, "format-y2k")
1752 || streq(option+1, "format-security")
1753 || streq(option+1, "old-style-declaration")
1754 || streq(option+1, "type-limits")) {
1755 /* ignore (gcc compatibility) */
1757 set_warning_opt(&option[1]);
1759 } else if (option[0] == 'm') {
1764 GET_ARG_AFTER(opt, "-m");
1765 if (strstart(opt, "target=")) {
1766 GET_ARG_AFTER(opt, "-mtarget=");
1767 if (!parse_target_triple(opt)) {
1768 argument_errors = true;
1770 const char *isa = setup_target_machine();
1771 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1772 target_triple = opt;
1774 } else if (strstart(opt, "triple=")) {
1775 GET_ARG_AFTER(opt, "-mtriple=");
1776 if (!parse_target_triple(opt)) {
1777 argument_errors = true;
1779 const char *isa = setup_target_machine();
1780 strncpy(cpu_arch, isa, sizeof(cpu_arch));
1781 target_triple = opt;
1783 } else if (strstart(opt, "arch=")) {
1784 GET_ARG_AFTER(opt, "-march=");
1785 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1786 int res = be_parse_arg(arch_opt);
1787 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1788 res &= be_parse_arg(arch_opt);
1791 fprintf(stderr, "Unknown architecture '%s'\n", arch_opt);
1792 argument_errors = true;
1794 } else if (strstart(opt, "tune=")) {
1795 GET_ARG_AFTER(opt, "-mtune=");
1796 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
1797 int res = be_parse_arg(arch_opt);
1799 argument_errors = true;
1800 } else if (strstart(opt, "cpu=")) {
1801 GET_ARG_AFTER(opt, "-mcpu=");
1802 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
1803 int res = be_parse_arg(arch_opt);
1805 argument_errors = true;
1806 } else if (strstart(opt, "fpmath=")) {
1807 GET_ARG_AFTER(opt, "-mfpmath=");
1808 if (streq(opt, "387"))
1810 else if (streq(opt, "sse"))
1813 fprintf(stderr, "error: option -mfpmath supports only 387 or sse\n");
1814 argument_errors = true;
1816 if (!argument_errors) {
1817 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
1818 int res = be_parse_arg(arch_opt);
1820 argument_errors = true;
1822 } else if (strstart(opt, "preferred-stack-boundary=")) {
1823 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
1824 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
1825 int res = be_parse_arg(arch_opt);
1827 argument_errors = true;
1828 } else if (streq(opt, "rtd")) {
1829 default_calling_convention = CC_STDCALL;
1830 } else if (strstart(opt, "regparm=")) {
1831 fprintf(stderr, "error: regparm convention not supported yet\n");
1832 argument_errors = true;
1833 } else if (streq(opt, "soft-float")) {
1834 add_flag(&ldflags_obst, "-msoft-float");
1835 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=softfloat", cpu_arch);
1836 int res = be_parse_arg(arch_opt);
1838 argument_errors = true;
1839 } else if (streq(opt, "sse2")) {
1840 /* ignore for now, our x86 backend always uses sse when
1841 * sse is requested */
1843 long int value = strtol(opt, NULL, 10);
1845 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
1846 argument_errors = true;
1847 } else if (value != 16 && value != 32 && value != 64) {
1848 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
1849 argument_errors = true;
1851 unsigned machine_size = (unsigned)value;
1852 /* TODO: choose/change backend based on this */
1853 add_flag(&cppflags_obst, "-m%u", machine_size);
1854 add_flag(&asflags_obst, "-m%u", machine_size);
1855 add_flag(&ldflags_obst, "-m%u", machine_size);
1858 } else if (streq(option, "pg")) {
1859 set_be_option("gprof");
1860 add_flag(&ldflags_obst, "-pg");
1861 } else if (streq(option, "ansi")) {
1862 standard = STANDARD_ANSI;
1863 } else if (streq(option, "pedantic")) {
1864 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
1865 } else if (strstart(option, "std=")) {
1866 const char *const o = &option[4];
1868 streq(o, "c++") ? STANDARD_CXX98 :
1869 streq(o, "c++98") ? STANDARD_CXX98 :
1870 streq(o, "c11") ? STANDARD_C11 :
1871 streq(o, "c1x") ? STANDARD_C11 : // deprecated
1872 streq(o, "c89") ? STANDARD_C89 :
1873 streq(o, "c90") ? STANDARD_C89 :
1874 streq(o, "c99") ? STANDARD_C99 :
1875 streq(o, "c9x") ? STANDARD_C99 : // deprecated
1876 streq(o, "gnu++98") ? STANDARD_GNUXX98 :
1877 streq(o, "gnu11") ? STANDARD_GNU11 :
1878 streq(o, "gnu1x") ? STANDARD_GNU11 : // deprecated
1879 streq(o, "gnu89") ? STANDARD_GNU89 :
1880 streq(o, "gnu99") ? STANDARD_GNU99 :
1881 streq(o, "gnu9x") ? STANDARD_GNU99 : // deprecated
1882 streq(o, "iso9899:1990") ? STANDARD_C89 :
1883 streq(o, "iso9899:199409") ? STANDARD_C89AMD1 :
1884 streq(o, "iso9899:1999") ? STANDARD_C99 :
1885 streq(o, "iso9899:199x") ? STANDARD_C99 : // deprecated
1886 streq(o, "iso9899:2011") ? STANDARD_C11 :
1887 (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
1888 } else if (streq(option, "version")) {
1889 print_cparser_version();
1890 return EXIT_SUCCESS;
1891 } else if (streq(option, "dumpversion")) {
1892 /* gcc compatibility option */
1893 print_cparser_version_short();
1894 return EXIT_SUCCESS;
1895 } else if (strstart(option, "print-file-name=")) {
1896 GET_ARG_AFTER(print_file_name_file, "-print-file-name=");
1897 } else if (option[0] == '-') {
1898 /* double dash option */
1900 if (streq(option, "gcc")) {
1901 features_on |= _GNUC;
1902 features_off &= ~_GNUC;
1903 } else if (streq(option, "no-gcc")) {
1904 features_on &= ~_GNUC;
1905 features_off |= _GNUC;
1906 } else if (streq(option, "ms")) {
1908 features_off &= ~_MS;
1909 } else if (streq(option, "no-ms")) {
1910 features_on &= ~_MS;
1911 features_off |= _MS;
1912 } else if (streq(option, "strict")) {
1914 } else if (streq(option, "benchmark")) {
1915 mode = BenchmarkParser;
1916 } else if (streq(option, "print-ast")) {
1918 } else if (streq(option, "print-implicit-cast")) {
1919 print_implicit_casts = true;
1920 } else if (streq(option, "print-parenthesis")) {
1921 print_parenthesis = true;
1922 } else if (streq(option, "print-fluffy")) {
1924 } else if (streq(option, "print-compound-sizes")) {
1925 mode = PrintCompoundSizes;
1926 } else if (streq(option, "print-jna")) {
1928 } else if (streq(option, "jna-limit")) {
1931 fprintf(stderr, "error: "
1932 "expected argument after '--jna-limit'\n");
1933 argument_errors = true;
1936 jna_limit_output(argv[i]);
1937 } else if (streq(option, "jna-libname")) {
1940 fprintf(stderr, "error: "
1941 "expected argument after '--jna-libname'\n");
1942 argument_errors = true;
1945 jna_set_libname(argv[i]);
1946 } else if (streq(option, "external-pp")) {
1947 if (i+1 < argc && argv[i+1][0] != '-') {
1949 external_preprocessor = argv[i+1];
1951 external_preprocessor = PREPROCESSOR;
1953 } else if (streq(option, "no-external-pp")) {
1954 external_preprocessor = NULL;
1955 } else if (streq(option, "time")) {
1957 } else if (streq(option, "version")) {
1958 print_cparser_version();
1959 return EXIT_SUCCESS;
1960 } else if (streq(option, "help")) {
1962 } else if (streq(option, "help-parser")) {
1963 help |= HELP_PARSER;
1964 } else if (streq(option, "help-warnings")) {
1965 help |= HELP_WARNINGS;
1966 } else if (streq(option, "help-codegen")) {
1967 help |= HELP_CODEGEN;
1968 } else if (streq(option, "help-linker")) {
1969 help |= HELP_LINKER;
1970 } else if (streq(option, "help-optimization")) {
1971 help |= HELP_OPTIMIZATION;
1972 } else if (streq(option, "help-language-tools")) {
1973 help |= HELP_LANGUAGETOOLS;
1974 } else if (streq(option, "help-debug")) {
1976 } else if (streq(option, "help-firm")) {
1978 } else if (streq(option, "help-all")) {
1980 } else if (streq(option, "dump-function")) {
1983 fprintf(stderr, "error: "
1984 "expected argument after '--dump-function'\n");
1985 argument_errors = true;
1988 dumpfunction = argv[i];
1990 } else if (streq(option, "export-ir")) {
1991 mode = CompileExportIR;
1992 } else if (streq(option, "unroll-loops")) {
1993 /* ignore (gcc compatibility) */
1995 fprintf(stderr, "error: unknown argument '%s'\n", arg);
1996 argument_errors = true;
1999 fprintf(stderr, "error: unknown argument '%s'\n", arg);
2000 argument_errors = true;
2003 compilation_unit_type_t type = forced_unittype;
2004 if (type == COMPILATION_UNIT_AUTODETECT) {
2005 if (streq(arg, "-")) {
2006 /* - implicitly means C source file */
2007 type = COMPILATION_UNIT_C;
2009 const char *suffix = strrchr(arg, '.');
2010 /* Ensure there is at least one char before the suffix */
2011 if (suffix != NULL && suffix != arg) {
2014 streq(suffix, "S") ? COMPILATION_UNIT_ASSEMBLER :
2015 streq(suffix, "a") ? COMPILATION_UNIT_OBJECT :
2016 streq(suffix, "c") ? COMPILATION_UNIT_C :
2017 streq(suffix, "i") ? COMPILATION_UNIT_PREPROCESSED_C :
2018 streq(suffix, "C") ? COMPILATION_UNIT_CXX :
2019 streq(suffix, "cc") ? COMPILATION_UNIT_CXX :
2020 streq(suffix, "cp") ? COMPILATION_UNIT_CXX :
2021 streq(suffix, "cpp") ? COMPILATION_UNIT_CXX :
2022 streq(suffix, "CPP") ? COMPILATION_UNIT_CXX :
2023 streq(suffix, "cxx") ? COMPILATION_UNIT_CXX :
2024 streq(suffix, "c++") ? COMPILATION_UNIT_CXX :
2025 streq(suffix, "ii") ? COMPILATION_UNIT_PREPROCESSED_CXX :
2026 streq(suffix, "h") ? COMPILATION_UNIT_C :
2027 streq(suffix, "ir") ? COMPILATION_UNIT_IR :
2028 streq(suffix, "o") ? COMPILATION_UNIT_OBJECT :
2029 streq(suffix, "s") ? COMPILATION_UNIT_PREPROCESSED_ASSEMBLER :
2030 streq(suffix, "so") ? COMPILATION_UNIT_OBJECT :
2031 COMPILATION_UNIT_OBJECT; /* gcc behavior: unknown file extension means object file */
2036 compilation_unit_t *entry = OALLOCZ(&file_obst, compilation_unit_t);
2040 if (last_unit != NULL) {
2041 last_unit->next = entry;
2049 if (help != HELP_NONE) {
2050 print_help(argv[0], help);
2051 return !argument_errors;
2054 if (print_file_name_file != NULL) {
2055 print_file_name(print_file_name_file);
2056 return EXIT_SUCCESS;
2058 if (units == NULL) {
2059 fprintf(stderr, "error: no input files specified\n");
2060 argument_errors = true;
2063 if (argument_errors) {
2065 return EXIT_FAILURE;
2068 /* apply some effects from switches */
2069 c_mode |= features_on;
2070 c_mode &= ~features_off;
2071 if (profile_generate) {
2072 add_flag(&ldflags_obst, "-lfirmprof");
2073 set_be_option("profilegenerate");
2076 set_be_option("profileuse");
2079 init_symbol_table();
2080 init_types_and_adjust();
2083 if (c_mode & _CXX) {
2084 init_wchar_types(ATOMIC_TYPE_WCHAR_T);
2086 init_wchar_types(wchar_atomic_kind);
2088 init_preprocessor();
2097 if (construct_dep_target) {
2098 if (outname != 0 && strlen(outname) >= 2) {
2099 get_output_name(dep_target, sizeof(dep_target), outname, ".d");
2101 get_output_name(dep_target, sizeof(dep_target), units->name, ".d");
2104 dep_target[0] = '\0';
2107 char outnamebuf[4096];
2108 if (outname == NULL) {
2109 const char *filename = units->name;
2112 case BenchmarkParser:
2116 case PrintCompoundSizes:
2117 case PreprocessOnly:
2122 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
2123 outname = outnamebuf;
2125 case CompileAssemble:
2126 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
2127 outname = outnamebuf;
2130 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
2132 outname = outnamebuf;
2134 case CompileExportIR:
2135 get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".ir");
2136 outname = outnamebuf;
2138 case CompileAssembleLink:
2139 if (firm_is_windows_os(target_machine)) {
2148 assert(outname != NULL);
2151 if (streq(outname, "-")) {
2154 out = fopen(outname, "w");
2156 fprintf(stderr, "Could not open '%s' for writing: %s\n", outname,
2158 return EXIT_FAILURE;
2162 int result = compilation_loop(mode, units, standard, out);
2163 if (result != EXIT_SUCCESS) {
2169 /* link program file */
2170 if (mode == CompileAssembleLink) {
2171 int result = link_program(units);
2172 if (result != EXIT_SUCCESS) {
2183 obstack_free(&cppflags_obst, NULL);
2184 obstack_free(&ldflags_obst, NULL);
2185 obstack_free(&asflags_obst, NULL);
2186 obstack_free(&file_obst, NULL);
2193 exit_preprocessor();
2197 exit_symbol_table();
2198 return EXIT_SUCCESS;