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"
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 #if defined(_DEBUG) || defined(FIRM_DEBUG)
116 * Debug printf implementation.
118 * @param fmt printf style format parameter
120 void dbg_printf(const char *fmt, ...)
124 if (firm_dump.debug_print) {
130 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
132 static void initialize_firm(void)
136 dump_consts_local(1);
137 dump_keepalive_edges(1);
140 static void get_output_name(char *buf, size_t buflen, const char *inputname,
143 size_t last_dot = 0xffffffff;
146 if(inputname == NULL) {
147 snprintf(buf, buflen, "a%s", newext);
151 for(const char *c = inputname; *c != 0; ++c) {
156 if(last_dot == 0xffffffff)
159 if(last_dot >= buflen)
160 panic("filename too long");
161 memcpy(buf, inputname, last_dot);
163 size_t extlen = strlen(newext) + 1;
164 if(extlen + last_dot >= buflen)
165 panic("filename too long");
166 memcpy(buf+last_dot, newext, extlen);
169 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
171 lexer_open_stream(in, input);
172 translation_unit_t *unit = parse();
176 static void lextest(FILE *in, const char *fname)
178 lexer_open_stream(in, fname);
181 lexer_next_preprocessing_token();
182 print_token(stdout, &lexer_token);
184 } while(lexer_token.type != T_EOF);
187 static FILE* preprocess(FILE* in, const char *fname)
190 obstack_1grow(&cppflags_obst, '\0');
191 const char *flags = obstack_finish(&cppflags_obst);
194 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
196 /* read from stdin */
197 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
203 FILE* f = popen(buf, "r");
205 fprintf(stderr, "invoking preprocessor failed\n");
211 static void do_link(const char *out, const char *in)
214 obstack_1grow(&ldflags_obst, '\0');
215 const char *flags = obstack_finish(&ldflags_obst);
217 snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
221 int err = system(buf);
223 fprintf(stderr, "linker reported an error\n");
228 static void assemble(const char *out, const char *in)
232 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
237 int err = system(buf);
239 fprintf(stderr, "assembler reported an error\n");
244 static const char *try_dir(const char *dir)
248 if(access(dir, R_OK | W_OK | X_OK) == 0)
253 static const char *get_tempdir(void)
255 static const char *tmpdir = NULL;
261 tmpdir = try_dir(getenv("TMPDIR"));
263 tmpdir = try_dir(getenv("TMP"));
265 tmpdir = try_dir(getenv("TEMP"));
269 tmpdir = try_dir(P_tmpdir);
273 tmpdir = try_dir("/var/tmp");
275 tmpdir = try_dir("/usr/tmp");
277 tmpdir = try_dir("/tmp");
286 /* cheap and nasty mkstemp replacement */
287 static int mkstemp(char *templ)
290 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
295 * an own version of tmpnam, which: writes in a buffer, appends a user specified
296 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
298 static FILE *make_temp_file(char *buffer, size_t buflen,
299 const char *prefix, const char *suffix)
301 const char *tempdir = get_tempdir();
303 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
307 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
309 int fd = mkstemp(buffer);
311 fprintf(stderr, "couldn't create temporary file: %s\n",
315 FILE *out = fdopen(fd, "w");
317 fprintf(stderr, "couldn't create temporary file FILE*\n");
325 * Do the necessary lowering for compound parameters.
327 void lower_compound_params(void)
329 lower_params_t params;
331 params.def_ptr_alignment = 4;
332 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
333 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
334 params.find_pointer_type = NULL;
335 params.ret_compound_in_regs = NULL;
336 lower_calls_with_compounds(¶ms);
339 typedef enum compile_mode_t {
351 static void usage(const char *argv0)
353 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
356 int main(int argc, char **argv)
360 const char *input = NULL;
361 const char *outname = NULL;
362 const char *dumpfunction = NULL;
363 compile_mode_t mode = CompileAssembleLink;
365 int result = EXIT_SUCCESS;
367 obstack_init(&cppflags_obst);
368 obstack_init(&ldflags_obst);
370 #define GET_ARG_AFTER(def, args) \
371 def = &arg[sizeof(args)-1]; \
372 if(def[0] == '\0') { \
375 fprintf(stderr, "error: expected argument after '" args "'\n"); \
376 argument_errors = true; \
380 if(def[0] == '-' && def[1] != '\0') { \
381 fprintf(stderr, "error: expected argument after '" args "'\n"); \
382 argument_errors = true; \
387 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
389 /* early options parsing (find out optimisation level) */
390 for(int i = 1; i < argc; ++i) {
391 const char *arg = argv[i];
395 const char *option = &arg[1];
396 if(option[0] == 'O') {
397 sscanf(&option[1], "%d", &opt_level);
401 /* apply optimisation level */
404 firm_option("no-opt");
407 firm_option("no-inline");
411 firm_option("strict-aliasing");
414 firm_option("cond-eval");
415 firm_option("if-conv");
418 firm_option("inline");
419 firm_option("deconv");
420 firm_be_option("omitfp");
424 /* parse rest of options */
425 bool help_displayed = false;
426 bool argument_errors = false;
427 for(int i = 1; i < argc; ++i) {
428 const char *arg = argv[i];
431 const char *option = &arg[1];
432 if(option[0] == 'o') {
433 GET_ARG_AFTER(outname, "-o");
434 } else if(option[0] == 'g') {
435 firm_be_option("stabs=yes");
436 firm_be_option("omitfp=no");
437 firm_be_option("ia32-nooptcc=yes");
438 } else if(SINGLE_OPTION('c')) {
439 mode = CompileAssemble;
440 } else if(SINGLE_OPTION('S')) {
442 } else if(option[0] == 'O') {
444 } else if(option[0] == 'I') {
446 GET_ARG_AFTER(opt, "-I");
447 obstack_printf(&cppflags_obst, " -I%s", opt);
448 } else if(option[0] == 'D') {
450 GET_ARG_AFTER(opt, "-D");
451 obstack_printf(&cppflags_obst, " -D%s", opt);
452 } else if(option[0] == 'U') {
454 GET_ARG_AFTER(opt, "-U");
455 obstack_printf(&cppflags_obst, " -U%s", opt);
456 } else if(option[0] == 'l') {
458 GET_ARG_AFTER(opt, "-l");
459 obstack_printf(&ldflags_obst, " -l%s", opt);
460 } else if(option[0] == 'L') {
462 GET_ARG_AFTER(opt, "-L");
463 obstack_printf(&ldflags_obst, " -L%s", opt);
464 } else if(SINGLE_OPTION('v')) {
466 } else if(SINGLE_OPTION('w')) {
467 inhibit_all_warnings = true;
468 } else if(option[0] == 'f') {
470 GET_ARG_AFTER(opt, "-f");
472 if(strcmp(opt, "syntax-only") == 0) {
474 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
475 firm_be_option("omitfp");
476 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
477 firm_be_option("omitfp=no");
479 int res = firm_option(opt);
481 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
483 argument_errors = true;
485 } else if (res == -1) {
486 help_displayed = true;
489 } else if(option[0] == 'b') {
491 GET_ARG_AFTER(opt, "-b");
492 int res = firm_be_option(opt);
494 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
496 argument_errors = true;
497 } else if (res == -1) {
498 help_displayed = true;
500 } else if(option[0] == 'W') {
501 set_warning_opt(&option[1]);
502 } else if(option[0] == 'm') {
504 GET_ARG_AFTER(opt, "-m");
506 long int value = strtol(opt, &endptr, 10);
507 if (*endptr != '\0') {
508 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
509 argument_errors = true;
511 if (value != 16 && value != 32 && value != 64) {
512 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
513 argument_errors = true;
515 machine_size = (unsigned int)value;
517 } else if (option[0] == '\0') {
519 fprintf(stderr, "error: multiple input files specified\n");
520 argument_errors = true;
524 } else if(strcmp(option, "pedantic") == 0) {
525 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
526 } else if(strncmp(option, "std=", 4) == 0) {
527 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
528 } else if (option[0] == '-') {
529 /* double dash option */
531 if(strcmp(option, "gcc") == 0) {
533 } else if(strcmp(option, "no-gcc") == 0) {
535 } else if(strcmp(option, "ms") == 0) {
537 } else if(strcmp(option, "no-ms") == 0) {
539 } else if(strcmp(option, "signed-chars") == 0) {
540 char_is_signed = true;
541 } else if(strcmp(option, "unsigned-chars") == 0) {
542 char_is_signed = false;
543 } else if(strcmp(option, "strict") == 0) {
545 } else if(strcmp(option, "lextest") == 0) {
547 } else if(strcmp(option, "benchmark") == 0) {
548 mode = BenchmarkParser;
549 } else if(strcmp(option, "print-ast") == 0) {
551 } else if(strcmp(option, "print-implicit-cast") == 0) {
552 print_implicit_casts = true;
553 } else if(strcmp(option, "print-parenthesis") == 0) {
554 print_parenthesis = true;
555 } else if(strcmp(option, "print-fluffy") == 0) {
557 } else if(strcmp(option, "version") == 0) {
559 firm_get_version(&ver);
560 printf("cparser (%s) using libFirm (%u.%u",
561 cparser_REVISION, ver.major, ver.minor);
562 if(ver.revision[0] != 0) {
564 fputs(ver.revision, stdout);
566 if(ver.build[0] != 0) {
568 fputs(ver.build, stdout);
572 } else if(strcmp(option, "dump-function") == 0) {
575 fprintf(stderr, "error: "
576 "expected argument after '--dump-function'\n");
577 argument_errors = true;
580 dumpfunction = argv[i];
583 fprintf(stderr, "error: unknown argument '%s'\n", arg);
584 argument_errors = true;
587 fprintf(stderr, "error: unknown argument '%s'\n", arg);
588 argument_errors = true;
592 fprintf(stderr, "error: multiple input files specified\n");
593 argument_errors = true;
600 /* we do the lowering in ast2firm */
601 firm_opt.lower_bitfields = FALSE;
604 return !argument_errors;
606 if(argument_errors) {
623 char outnamebuf[4096];
624 if(outname == NULL) {
626 case BenchmarkParser:
636 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
637 outname = outnamebuf;
639 case CompileAssemble:
640 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
641 outname = outnamebuf;
644 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
646 outname = outnamebuf;
648 case CompileAssembleLink:
654 if(outname != NULL) {
655 if(strcmp(outname, "-") == 0) {
658 out = fopen(outname, "w");
660 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
669 fprintf(stderr, "%s: no input files\n", argv[0]);
671 } else if(strcmp(input, "-") == 0) {
675 in = fopen(input, "r");
677 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
682 if(mode == LexTest) {
688 FILE *preprocessed_in = preprocess(in, input);
689 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
690 int res = pclose(preprocessed_in);
695 if(error_count > 0) {
696 /* parsing failed because of errors */
697 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
698 result = EXIT_FAILURE;
699 } else if(warning_count > 0) {
700 fprintf(stderr, "%u warning(s)\n", warning_count);
703 if(mode == BenchmarkParser) {
707 /* prints the AST even if errors occurred */
708 if(mode == PrintAst) {
709 type_set_output(out);
715 /* cannot handle other modes with errors */
716 if(result != EXIT_SUCCESS)
719 if(mode == PrintFluffy) {
720 type_set_output(out);
722 write_fluffy_decls(out, unit);
725 translation_unit_to_firm(unit);
727 if(mode == ParseOnly) {
732 char asm_tempfile[1024];
733 if(mode == CompileDump) {
735 firm_be_opt.selection = BE_NONE;
736 } else if(mode == Compile) {
740 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
742 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
744 if(mode == CompileDump) {
746 ident *id = new_id_from_str(dumpfunction);
747 ir_graph *irg = NULL;
748 int n_irgs = get_irp_n_irgs();
749 for(int i = 0; i < n_irgs; ++i) {
750 ir_graph *tirg = get_irp_irg(i);
751 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
759 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
763 dump_ir_block_graph_file(irg, out);
770 /* assemble assembler and create object file */
771 char obj_tfile[1024];
772 if(mode == CompileAssemble || mode == CompileAssembleLink) {
773 const char *obj_outfile;
774 if(mode == CompileAssemble) {
776 obj_outfile = outname;
779 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
781 obj_outfile = obj_tfile;
784 assemble(obj_outfile, asm_tempfile);
787 /* link object file */
788 if(mode == CompileAssembleLink) {
789 do_link(outname, obj_tfile);
792 obstack_free(&cppflags_obst, NULL);
793 obstack_free(&ldflags_obst, NULL);