2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 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)
63 #include <libfirm/firm.h>
64 #include <libfirm/be.h>
69 #include "type_hash.h"
72 #include "diagnostic.h"
73 #include "lang_features.h"
74 #include "driver/firm_opt.h"
75 #include "driver/firm_cmdline.h"
76 #include "adt/error.h"
77 #include "write_fluffy.h"
82 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int -m32"
86 #define LINKER "gcc -m32"
90 #define ASSEMBLER "as --32"
93 /** The current c mode/dialect. */
94 unsigned int c_mode = _C89|_C99|_GNUC;
96 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
97 unsigned int machine_size = 32;
99 /** true if the char type is signed. */
100 bool char_is_signed = true;
102 /** true for strict language checking. */
103 bool strict_mode = false;
105 /* to switch on printing of implicit casts */
106 extern bool print_implicit_casts;
108 /* to switch on printing of parenthesis to indicate operator precedence */
109 extern bool print_parenthesis;
112 static struct obstack cppflags_obst, ldflags_obst;
114 typedef struct file_list_entry_t file_list_entry_t;
116 struct file_list_entry_t {
117 const char *filename;
118 file_list_entry_t *next;
121 #if defined(_DEBUG) || defined(FIRM_DEBUG)
123 * Debug printf implementation.
125 * @param fmt printf style format parameter
127 void dbg_printf(const char *fmt, ...)
131 if (firm_dump.debug_print) {
137 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
139 static void initialize_firm(void)
143 dump_consts_local(1);
144 dump_keepalive_edges(1);
147 static void get_output_name(char *buf, size_t buflen, const char *inputname,
150 size_t last_dot = 0xffffffff;
153 if(inputname == NULL) {
154 snprintf(buf, buflen, "a%s", newext);
158 for(const char *c = inputname; *c != 0; ++c) {
163 if(last_dot == 0xffffffff)
166 if(last_dot >= buflen)
167 panic("filename too long");
168 memcpy(buf, inputname, last_dot);
170 size_t extlen = strlen(newext) + 1;
171 if(extlen + last_dot >= buflen)
172 panic("filename too long");
173 memcpy(buf+last_dot, newext, extlen);
176 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
178 lexer_open_stream(in, input_name);
179 translation_unit_t *unit = parse();
183 static void lextest(FILE *in, const char *fname)
185 lexer_open_stream(in, fname);
188 lexer_next_preprocessing_token();
189 print_token(stdout, &lexer_token);
191 } while(lexer_token.type != T_EOF);
194 static FILE *preprocess(FILE *in, const char *fname, bool to_stdout)
197 obstack_1grow(&cppflags_obst, '\0');
198 const char *flags = obstack_finish(&cppflags_obst);
201 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
203 /* read from stdin */
204 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
211 int res = system(buf);
214 FILE *f = popen(buf, "r");
216 fprintf(stderr, "invoking preprocessor failed\n");
223 static void do_link(const char *out, const char *in)
226 obstack_1grow(&ldflags_obst, '\0');
227 const char *flags = obstack_finish(&ldflags_obst);
229 snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
233 int err = system(buf);
235 fprintf(stderr, "linker reported an error\n");
240 static void assemble(const char *out, const char *in)
244 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
249 int err = system(buf);
251 fprintf(stderr, "assembler reported an error\n");
256 static const char *try_dir(const char *dir)
260 if(access(dir, R_OK | W_OK | X_OK) == 0)
265 static const char *get_tempdir(void)
267 static const char *tmpdir = NULL;
273 tmpdir = try_dir(getenv("TMPDIR"));
275 tmpdir = try_dir(getenv("TMP"));
277 tmpdir = try_dir(getenv("TEMP"));
281 tmpdir = try_dir(P_tmpdir);
285 tmpdir = try_dir("/var/tmp");
287 tmpdir = try_dir("/usr/tmp");
289 tmpdir = try_dir("/tmp");
298 /* cheap and nasty mkstemp replacement */
299 static int mkstemp(char *templ)
302 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
307 * an own version of tmpnam, which: writes in a buffer, appends a user specified
308 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
310 static FILE *make_temp_file(char *buffer, size_t buflen,
311 const char *prefix, const char *suffix)
313 const char *tempdir = get_tempdir();
315 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
319 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
321 int fd = mkstemp(buffer);
323 fprintf(stderr, "couldn't create temporary file: %s\n",
327 FILE *out = fdopen(fd, "w");
329 fprintf(stderr, "couldn't create temporary file FILE*\n");
337 * Do the necessary lowering for compound parameters.
339 void lower_compound_params(void)
341 lower_params_t params;
343 params.def_ptr_alignment = 4;
344 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
345 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
346 params.find_pointer_type = NULL;
347 params.ret_compound_in_regs = NULL;
348 lower_calls_with_compounds(¶ms);
351 typedef enum compile_mode_t {
365 static void usage(const char *argv0)
367 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
370 static void print_cparser_version(void) {
372 firm_get_version(&ver);
374 printf("cparser (%s) using libFirm (%u.%u",
375 cparser_REVISION, ver.major, ver.minor);
376 if(ver.revision[0] != 0) {
378 fputs(ver.revision, stdout);
380 if(ver.build[0] != 0) {
382 fputs(ver.build, stdout);
387 int main(int argc, char **argv)
391 const char *input = NULL;
392 const char *outname = NULL;
393 const char *dumpfunction = NULL;
394 compile_mode_t mode = CompileAssembleLink;
396 int result = EXIT_SUCCESS;
397 char cpu_arch[16] = "ia32";
398 file_list_entry_t *c_files = NULL;
399 file_list_entry_t *s_files = NULL;
400 file_list_entry_t *o_files = NULL;
401 struct obstack file_obst;
403 obstack_init(&cppflags_obst);
404 obstack_init(&ldflags_obst);
405 obstack_init(&file_obst);
407 #define GET_ARG_AFTER(def, args) \
408 def = &arg[sizeof(args)-1]; \
409 if(def[0] == '\0') { \
412 fprintf(stderr, "error: expected argument after '" args "'\n"); \
413 argument_errors = true; \
417 if(def[0] == '-' && def[1] != '\0') { \
418 fprintf(stderr, "error: expected argument after '" args "'\n"); \
419 argument_errors = true; \
424 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
426 /* early options parsing (find out optimisation level) */
427 for(int i = 1; i < argc; ++i) {
428 const char *arg = argv[i];
432 const char *option = &arg[1];
433 if(option[0] == 'O') {
434 sscanf(&option[1], "%d", &opt_level);
438 /* apply optimisation level */
441 firm_option("no-opt");
444 firm_option("no-inline");
448 firm_option("strict-aliasing");
451 firm_option("cond-eval");
452 firm_option("if-conv");
455 firm_option("inline");
456 firm_option("deconv");
457 firm_be_option("omitfp");
461 /* parse rest of options */
462 bool help_displayed = false;
463 bool argument_errors = false;
464 for(int i = 1; i < argc; ++i) {
465 const char *arg = argv[i];
468 const char *option = &arg[1];
469 if(option[0] == 'o') {
470 GET_ARG_AFTER(outname, "-o");
471 } else if(option[0] == 'g') {
472 firm_be_option("stabs=yes");
473 firm_be_option("omitfp=no");
474 firm_be_option("ia32-nooptcc=yes");
475 } else if(SINGLE_OPTION('c')) {
476 mode = CompileAssemble;
477 } else if(SINGLE_OPTION('E')) {
478 mode = PreprocessOnly;
479 } else if(SINGLE_OPTION('S')) {
481 } else if(option[0] == 'O') {
483 } else if(option[0] == 'I') {
485 GET_ARG_AFTER(opt, "-I");
486 obstack_printf(&cppflags_obst, " -I%s", opt);
487 } else if(option[0] == 'D') {
489 GET_ARG_AFTER(opt, "-D");
490 obstack_printf(&cppflags_obst, " -D%s", opt);
491 } else if(option[0] == 'U') {
493 GET_ARG_AFTER(opt, "-U");
494 obstack_printf(&cppflags_obst, " -U%s", opt);
495 } else if(option[0] == 'l') {
497 GET_ARG_AFTER(opt, "-l");
498 obstack_printf(&ldflags_obst, " -l%s", opt);
499 } else if(option[0] == 'L') {
501 GET_ARG_AFTER(opt, "-L");
502 obstack_printf(&ldflags_obst, " -L%s", opt);
503 } else if(SINGLE_OPTION('v')) {
505 } else if(SINGLE_OPTION('w')) {
506 inhibit_all_warnings = true;
507 } else if(option[0] == 'f') {
509 GET_ARG_AFTER(opt, "-f");
511 if(strcmp(opt, "syntax-only") == 0) {
513 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
514 firm_be_option("omitfp");
515 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
516 firm_be_option("omitfp=no");
518 int res = firm_option(opt);
520 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
522 argument_errors = true;
524 } else if (res == -1) {
525 help_displayed = true;
528 } else if(option[0] == 'b') {
530 GET_ARG_AFTER(opt, "-b");
531 int res = firm_be_option(opt);
533 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
535 argument_errors = true;
536 } else if (res == -1) {
537 help_displayed = true;
539 if (strncmp(opt, "isa=", 4) == 0)
540 strncpy(cpu_arch, opt, sizeof(cpu_arch));
542 } else if(option[0] == 'W') {
543 set_warning_opt(&option[1]);
544 } else if(option[0] == 'm') {
549 GET_ARG_AFTER(opt, "-m");
550 if(strncmp(opt, "arch=", 5) == 0) {
551 GET_ARG_AFTER(opt, "-march=");
552 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
553 int res = firm_be_option(arch_opt);
555 argument_errors = true;
557 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
558 int res = firm_be_option(arch_opt);
560 argument_errors = true;
562 } else if(strncmp(opt, "tune=", 5) == 0) {
563 GET_ARG_AFTER(opt, "-mtune=");
564 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
565 int res = firm_be_option(arch_opt);
567 argument_errors = true;
568 } else if(strncmp(opt, "cpu=", 4) == 0) {
569 GET_ARG_AFTER(opt, "-mcpu=");
570 snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
571 int res = firm_be_option(arch_opt);
573 argument_errors = true;
574 } else if(strncmp(opt, "fpmath=", 7) == 0) {
575 GET_ARG_AFTER(opt, "-mfpmath=");
576 if(strcmp(opt, "387") == 0)
578 else if(strcmp(opt, "sse") == 0)
581 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
582 argument_errors = true;
584 if(!argument_errors) {
585 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
586 int res = firm_be_option(arch_opt);
588 argument_errors = true;
590 } else if(strncmp(opt, "preferred-stack-boundary=", 25) == 0) {
591 GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
592 snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
593 int res = firm_be_option(arch_opt);
595 argument_errors = true;
596 } else if(strcmp(opt, "omit-leaf-frame-pointer") == 0) {
597 int res = firm_be_option("omitleaffp=1");
599 argument_errors = true;
600 } else if(strcmp(opt, "no-omit-leaf-frame-pointer") == 0) {
601 int res = firm_be_option("omitleaffp=0");
603 argument_errors = true;
606 long int value = strtol(opt, &endptr, 10);
607 if (*endptr != '\0') {
608 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
609 argument_errors = true;
611 if (value != 16 && value != 32 && value != 64) {
612 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
613 argument_errors = true;
615 machine_size = (unsigned int)value;
618 } else if (option[0] == '\0') {
620 fprintf(stderr, "error: multiple input files specified\n");
621 argument_errors = true;
625 } else if(strcmp(option, "pg") == 0) {
626 firm_be_option("-b gprof");
627 obstack_printf(&ldflags_obst, " -pg");
628 } else if(strcmp(option, "pedantic") == 0) {
629 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
630 } else if(strncmp(option, "std=", 4) == 0) {
631 if(strcmp(&option[4], "c99") == 0) {
633 } else if(strcmp(&option[4], "c89") == 0) {
635 } else if(strcmp(&option[4], "gnu99") == 0) {
636 c_mode = _C89|_C99|_GNUC;
637 } else if(strcmp(&option[4], "microsoft") == 0) {
638 c_mode = _C89|_C99|_MS;
640 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
641 } else if(strcmp(option, "version") == 0) {
642 print_cparser_version();
643 } else if (option[0] == '-') {
644 /* double dash option */
646 if(strcmp(option, "gcc") == 0) {
648 } else if(strcmp(option, "no-gcc") == 0) {
650 } else if(strcmp(option, "ms") == 0) {
652 } else if(strcmp(option, "no-ms") == 0) {
654 } else if(strcmp(option, "signed-chars") == 0) {
655 char_is_signed = true;
656 } else if(strcmp(option, "unsigned-chars") == 0) {
657 char_is_signed = false;
658 } else if(strcmp(option, "strict") == 0) {
660 } else if(strcmp(option, "lextest") == 0) {
662 } else if(strcmp(option, "benchmark") == 0) {
663 mode = BenchmarkParser;
664 } else if(strcmp(option, "print-ast") == 0) {
666 } else if(strcmp(option, "print-implicit-cast") == 0) {
667 print_implicit_casts = true;
668 } else if(strcmp(option, "print-parenthesis") == 0) {
669 print_parenthesis = true;
670 } else if(strcmp(option, "print-fluffy") == 0) {
672 } else if(strcmp(option, "version") == 0) {
673 print_cparser_version();
675 } else if(strcmp(option, "dump-function") == 0) {
678 fprintf(stderr, "error: "
679 "expected argument after '--dump-function'\n");
680 argument_errors = true;
683 dumpfunction = argv[i];
686 fprintf(stderr, "error: unknown argument '%s'\n", arg);
687 argument_errors = true;
690 fprintf(stderr, "error: unknown argument '%s'\n", arg);
691 argument_errors = true;
695 size_t len = strlen(arg);
697 fprintf(stderr, "'%s': file format not recognized\n", input);
701 file_list_entry_t *entry
702 = obstack_alloc(&file_obst, sizeof(entry[0]));
703 entry->filename = arg;
704 if (strcmp(arg+len-2, ".c") == 0) {
705 entry->next = c_files;
707 } else if (strcmp(arg+len-2, ".s") == 0) {
708 entry->next = s_files;
710 } else if (strcmp(arg+len-2, ".o") == 0) {
711 entry->next = o_files;
714 fprintf(stderr, "'%s': file format not recognized\n", input);
719 if (c_files == NULL && s_files == NULL && o_files != NULL) {
721 } else if (c_files != NULL && c_files->next == NULL) {
722 input = c_files->filename;
724 fprintf(stderr, "error: multiple input files specified\n");
725 argument_errors = true;
728 /* we do the lowering in ast2firm */
729 firm_opt.lower_bitfields = FALSE;
731 if (help_displayed) {
732 return !argument_errors;
734 if (argument_errors) {
751 char outnamebuf[4096];
752 if(outname == NULL) {
754 case BenchmarkParser:
766 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
767 outname = outnamebuf;
769 case CompileAssemble:
770 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
771 outname = outnamebuf;
774 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
776 outname = outnamebuf;
779 case CompileAssembleLink:
781 /* Windows compiler typically derive the output name from
782 the first source file */
783 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".exe");
784 outname = outnamebuf;
793 obstack_1grow(&ldflags_obst, '\0');
794 const char *flags = obstack_finish(&ldflags_obst);
796 obstack_printf(&file_obst, "%s", LINKER);
798 for (file_list_entry_t *entry = o_files; entry != NULL;
799 entry = entry->next) {
800 obstack_printf(&file_obst, " %s", entry->filename);
803 obstack_printf(&file_obst, " -o %s %s", outname, flags);
805 char *buf = obstack_finish(&file_obst);
810 int err = system(buf);
812 fprintf(stderr, "linker reported an error\n");
818 if(outname != NULL) {
819 if(strcmp(outname, "-") == 0) {
822 out = fopen(outname, "w");
824 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
833 fprintf(stderr, "%s: no input files\n", argv[0]);
835 } else if(strcmp(input, "-") == 0) {
839 in = fopen(input, "r");
841 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
846 if(mode == LexTest) {
852 FILE *preprocessed_in = preprocess(in, input, mode == PreprocessOnly);
853 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
854 int res = pclose(preprocessed_in);
859 if(error_count > 0) {
860 /* parsing failed because of errors */
861 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
862 result = EXIT_FAILURE;
863 } else if(warning_count > 0) {
864 fprintf(stderr, "%u warning(s)\n", warning_count);
867 if(mode == BenchmarkParser) {
871 /* prints the AST even if errors occurred */
872 if(mode == PrintAst) {
873 type_set_output(out);
879 /* cannot handle other modes with errors */
880 if(result != EXIT_SUCCESS)
883 if(mode == PrintFluffy) {
884 type_set_output(out);
886 write_fluffy_decls(out, unit);
889 translation_unit_to_firm(unit);
891 if(mode == ParseOnly) {
896 char asm_tempfile[1024];
897 if(mode == CompileDump) {
899 firm_be_opt.selection = BE_NONE;
900 } else if(mode == Compile) {
904 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
906 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
908 if(mode == CompileDump) {
910 ident *id = new_id_from_str(dumpfunction);
911 ir_graph *irg = NULL;
912 int n_irgs = get_irp_n_irgs();
913 for(int i = 0; i < n_irgs; ++i) {
914 ir_graph *tirg = get_irp_irg(i);
915 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
923 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
927 dump_ir_block_graph_file(irg, out);
934 /* assemble assembler and create object file */
935 char obj_tfile[1024];
936 if(mode == CompileAssemble || mode == CompileAssembleLink) {
937 const char *obj_outfile;
938 if(mode == CompileAssemble) {
940 obj_outfile = outname;
943 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
945 obj_outfile = obj_tfile;
948 assemble(obj_outfile, asm_tempfile);
951 /* link object file */
952 if(mode == CompileAssembleLink) {
953 do_link(outname, obj_tfile);
956 obstack_free(&cppflags_obst, NULL);
957 obstack_free(&ldflags_obst, NULL);
958 obstack_free(&file_obst, NULL);