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
34 /* no eXecute on Win32 */
39 #define O_RDWR _O_RDWR
40 #define O_CREAT _O_CREAT
41 #define O_EXCL _O_EXCL
42 #define O_BINARY _O_BINARY
44 /* remap some names, we are not in the POSIX world */
45 #define access(fname, mode) _access(fname, mode)
46 #define mktemp(tmpl) _mktemp(tmpl)
47 #define open(fname, oflag, mode) _open(fname, oflag, mode)
48 #define fdopen(fd, mode) _fdopen(fd, mode)
49 #define popen(cmd, mode) _popen(cmd, mode)
50 #define pclose(file) _pclose(file)
61 #include <libfirm/firm.h>
62 #include <libfirm/be.h>
67 #include "type_hash.h"
70 #include "diagnostic.h"
71 #include "lang_features.h"
72 #include "driver/firm_opt.h"
73 #include "driver/firm_cmdline.h"
74 #include "adt/error.h"
75 #include "write_fluffy.h"
80 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
84 #define LINKER "gcc -m32"
88 #define ASSEMBLER "as --32"
91 /** The current c mode/dialect. */
92 unsigned int c_mode = _C89|_C99|_GNUC;
94 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
95 unsigned int machine_size = 32;
97 /** true if the char type is signed. */
98 bool char_is_signed = true;
100 /** true for strict language checking. */
101 bool strict_mode = false;
103 /* to switch on printing of implicit casts */
104 extern bool print_implicit_casts;
106 /* to switch on printing of srenthesis to indicate operator precedence */
107 extern bool print_parenthesis;
110 static struct obstack cppflags_obst, ldflags_obst;
112 #if defined(_DEBUG) || defined(FIRM_DEBUG)
114 * Debug printf implementation.
116 * @param fmt printf style format parameter
118 void dbg_printf(const char *fmt, ...)
122 if (firm_dump.debug_print) {
128 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
130 static void initialize_firm(void)
134 dump_consts_local(1);
135 dump_keepalive_edges(1);
138 static void get_output_name(char *buf, size_t buflen, const char *inputname,
141 size_t last_dot = 0xffffffff;
144 if(inputname == NULL) {
145 snprintf(buf, buflen, "a%s", newext);
149 for(const char *c = inputname; *c != 0; ++c) {
154 if(last_dot == 0xffffffff)
157 if(last_dot >= buflen)
158 panic("filename too long");
159 memcpy(buf, inputname, last_dot);
161 size_t extlen = strlen(newext) + 1;
162 if(extlen + last_dot >= buflen)
163 panic("filename too long");
164 memcpy(buf+last_dot, newext, extlen);
167 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
169 lexer_open_stream(in, input);
170 translation_unit_t *unit = parse();
174 static void lextest(FILE *in, const char *fname)
176 lexer_open_stream(in, fname);
179 lexer_next_preprocessing_token();
180 print_token(stdout, &lexer_token);
182 } while(lexer_token.type != T_EOF);
185 static FILE* preprocess(FILE* in, const char *fname)
188 obstack_1grow(&cppflags_obst, '\0');
189 const char *flags = obstack_finish(&cppflags_obst);
192 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
194 /* read from stdin */
195 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
201 FILE* f = popen(buf, "r");
203 fprintf(stderr, "invoking preprocessor failed\n");
209 static void do_link(const char *out, const char *in)
212 obstack_1grow(&ldflags_obst, '\0');
213 const char *flags = obstack_finish(&ldflags_obst);
215 snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
219 int err = system(buf);
221 fprintf(stderr, "linker reported an error\n");
226 static void assemble(const char *out, const char *in)
230 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
235 int err = system(buf);
237 fprintf(stderr, "assembler reported an error\n");
242 static const char *try_dir(const char *dir)
246 if(access(dir, R_OK | W_OK | X_OK) == 0)
251 static const char *get_tempdir(void)
253 static const char *tmpdir = NULL;
259 tmpdir = try_dir(getenv("TMPDIR"));
261 tmpdir = try_dir(getenv("TMP"));
263 tmpdir = try_dir(getenv("TEMP"));
267 tmpdir = try_dir(P_tmpdir);
271 tmpdir = try_dir("/var/tmp");
273 tmpdir = try_dir("/usr/tmp");
275 tmpdir = try_dir("/tmp");
284 /* cheap and nasty mkstemp replacement */
285 static int mkstemp(char *templ)
288 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
293 * an own version of tmpnam, which: writes in a buffer, appends a user specified
294 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
296 static FILE *make_temp_file(char *buffer, size_t buflen,
297 const char *prefix, const char *suffix)
299 const char *tempdir = get_tempdir();
301 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
305 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
307 int fd = mkstemp(buffer);
309 fprintf(stderr, "couldn't create temporary file: %s\n",
313 FILE *out = fdopen(fd, "w");
315 fprintf(stderr, "couldn't create temporary file FILE*\n");
323 * Do the necessary lowering for compound parameters.
325 void lower_compound_params(void)
327 lower_params_t params;
329 params.def_ptr_alignment = 4;
330 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
331 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
332 params.find_pointer_type = NULL;
333 params.ret_compound_in_regs = NULL;
334 lower_calls_with_compounds(¶ms);
337 typedef enum compile_mode_t {
349 static void usage(const char *argv0)
351 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
354 int main(int argc, char **argv)
358 const char *input = NULL;
359 const char *outname = NULL;
360 const char *dumpfunction = NULL;
361 compile_mode_t mode = CompileAssembleLink;
364 obstack_init(&cppflags_obst);
365 obstack_init(&ldflags_obst);
367 #define GET_ARG_AFTER(def, args) \
368 def = &arg[sizeof(args)-1]; \
369 if(def[0] == '\0') { \
372 fprintf(stderr, "error: expected argument after '" args "'\n"); \
373 argument_errors = true; \
377 if(def[0] == '-' && def[1] != '\0') { \
378 fprintf(stderr, "error: expected argument after '" args "'\n"); \
379 argument_errors = true; \
384 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
386 /* early options parsing (find out optimisation level) */
387 for(int i = 1; i < argc; ++i) {
388 const char *arg = argv[i];
392 const char *option = &arg[1];
393 if(option[0] == 'O') {
394 sscanf(&option[1], "%d", &opt_level);
398 /* apply optimisation level */
401 firm_option("no-opt");
404 firm_option("no-inline");
408 firm_option("strict-aliasing");
411 firm_option("cond-eval");
412 firm_option("if-conv");
415 firm_option("inline");
416 firm_option("no-strength-red");
417 firm_option("deconv");
418 firm_be_option("omitfp");
422 /* parse rest of options */
423 bool help_displayed = false;
424 bool argument_errors = false;
425 for(int i = 1; i < argc; ++i) {
426 const char *arg = argv[i];
429 const char *option = &arg[1];
430 if(option[0] == 'o') {
431 GET_ARG_AFTER(outname, "-o");
432 } else if(option[0] == 'g') {
433 firm_be_option("stabs=yes");
434 firm_be_option("omitfp=no");
435 firm_be_option("ia32-nooptcc=yes");
436 } else if(SINGLE_OPTION('c')) {
437 mode = CompileAssemble;
438 } else if(SINGLE_OPTION('S')) {
440 } else if(option[0] == 'O') {
442 } else if(option[0] == 'I') {
444 GET_ARG_AFTER(opt, "-I");
445 obstack_printf(&cppflags_obst, " -I%s", opt);
446 } else if(option[0] == 'D') {
448 GET_ARG_AFTER(opt, "-D");
449 obstack_printf(&cppflags_obst, " -D%s", opt);
450 } else if(option[0] == 'U') {
452 GET_ARG_AFTER(opt, "-U");
453 obstack_printf(&cppflags_obst, " -U%s", opt);
454 } else if(option[0] == 'l') {
456 GET_ARG_AFTER(opt, "-l");
457 obstack_printf(&ldflags_obst, " -l%s", opt);
458 } else if(option[0] == 'L') {
460 GET_ARG_AFTER(opt, "-L");
461 obstack_printf(&ldflags_obst, " -L%s", opt);
462 } else if(SINGLE_OPTION('v')) {
464 } else if(SINGLE_OPTION('w')) {
465 inhibit_all_warnings = true;
466 } else if(option[0] == 'f') {
468 GET_ARG_AFTER(opt, "-f");
470 if(strcmp(opt, "syntax-only") == 0) {
472 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
473 firm_be_option("omitfp");
474 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
475 firm_be_option("omitfp=no");
477 int res = firm_option(opt);
479 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
481 argument_errors = true;
483 } else if (res == -1) {
484 help_displayed = true;
487 } else if(option[0] == 'b') {
489 GET_ARG_AFTER(opt, "-b");
490 int res = firm_be_option(opt);
492 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
494 argument_errors = true;
495 } else if (res == -1) {
496 help_displayed = true;
498 } else if(option[0] == 'W') {
499 set_warning_opt(&option[1]);
500 } else if(option[0] == 'm') {
502 GET_ARG_AFTER(opt, "-m");
504 long int value = strtol(opt, &endptr, 10);
505 if (*endptr != '\0') {
506 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
507 argument_errors = true;
509 if (value != 16 && value != 32 && value != 64) {
510 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
511 argument_errors = true;
513 machine_size = (unsigned int)value;
515 } else if (option[0] == '\0') {
517 fprintf(stderr, "error: multiple input files specified\n");
518 argument_errors = true;
522 } else if(strcmp(option, "pedantic") == 0) {
523 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
524 } else if(strncmp(option, "std=", 4) == 0) {
525 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
526 } else if (option[0] == '-') {
527 /* double dash option */
529 if(strcmp(option, "gcc") == 0) {
531 } else if(strcmp(option, "no-gcc") == 0) {
533 } else if(strcmp(option, "ms") == 0) {
535 } else if(strcmp(option, "no-ms") == 0) {
537 } else if(strcmp(option, "signed-chars") == 0) {
538 char_is_signed = true;
539 } else if(strcmp(option, "unsigned-chars") == 0) {
540 char_is_signed = false;
541 } else if(strcmp(option, "strict") == 0) {
543 } else if(strcmp(option, "lextest") == 0) {
545 } else if(strcmp(option, "benchmark") == 0) {
546 mode = BenchmarkParser;
547 } else if(strcmp(option, "print-ast") == 0) {
549 } else if(strcmp(option, "print-implicit-cast") == 0) {
550 print_implicit_casts = true;
551 } else if(strcmp(option, "print-parenthesis") == 0) {
552 print_parenthesis = true;
553 } else if(strcmp(option, "print-fluffy") == 0) {
555 } else if(strcmp(option, "version") == 0) {
557 firm_get_version(&ver);
558 printf("cparser (%s) using libFirm (%u.%u",
559 cparser_REVISION, ver.major, ver.minor);
560 if(ver.revision[0] != 0) {
562 fputs(ver.revision, stdout);
564 if(ver.build[0] != 0) {
566 fputs(ver.build, stdout);
570 } else if(strcmp(option, "dump-function") == 0) {
573 fprintf(stderr, "error: "
574 "expected argument after '--dump-function'\n");
575 argument_errors = true;
578 dumpfunction = argv[i];
581 fprintf(stderr, "error: unknown argument '%s'\n", arg);
582 argument_errors = true;
585 fprintf(stderr, "error: unknown argument '%s'\n", arg);
586 argument_errors = true;
590 fprintf(stderr, "error: multiple input files specified\n");
591 argument_errors = true;
598 /* we do the lowering in ast2firm */
599 firm_opt.lower_bitfields = FALSE;
602 return !argument_errors;
604 if(argument_errors) {
621 char outnamebuf[4096];
622 if(outname == NULL) {
624 case BenchmarkParser:
634 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
635 outname = outnamebuf;
637 case CompileAssemble:
638 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
639 outname = outnamebuf;
642 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
644 outname = outnamebuf;
646 case CompileAssembleLink:
652 if(outname != NULL) {
653 if(strcmp(outname, "-") == 0) {
656 out = fopen(outname, "w");
658 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
667 fprintf(stderr, "%s: no input files\n", argv[0]);
669 } else if(strcmp(input, "-") == 0) {
673 in = fopen(input, "r");
675 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
680 if(mode == LexTest) {
686 FILE *preprocessed_in = preprocess(in, input);
687 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
688 int result = pclose(preprocessed_in);
693 /* parsing failed because of errors */
694 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
697 if (warning_count > 0) {
698 fprintf(stderr, "%u warning(s)\n", warning_count);
701 if(mode == BenchmarkParser) {
705 if(mode == PrintAst) {
706 type_set_output(out);
711 if(mode == PrintFluffy) {
712 type_set_output(out);
714 write_fluffy_decls(out, unit);
717 translation_unit_to_firm(unit);
719 if(mode == ParseOnly) {
724 char asm_tempfile[1024];
725 if(mode == CompileDump) {
727 firm_be_opt.selection = BE_NONE;
728 } else if(mode == Compile) {
732 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
734 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
736 if(mode == CompileDump) {
738 ident *id = new_id_from_str(dumpfunction);
739 ir_graph *irg = NULL;
740 int n_irgs = get_irp_n_irgs();
741 for(int i = 0; i < n_irgs; ++i) {
742 ir_graph *tirg = get_irp_irg(i);
743 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
751 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
755 dump_ir_block_graph_file(irg, out);
762 /* assemble assembler and create object file */
763 char obj_tfile[1024];
764 if(mode == CompileAssemble || mode == CompileAssembleLink) {
765 const char *obj_outfile;
766 if(mode == CompileAssemble) {
768 obj_outfile = outname;
771 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
773 obj_outfile = obj_tfile;
776 assemble(obj_outfile, asm_tempfile);
779 /* link object file */
780 if(mode == CompileAssembleLink) {
781 do_link(outname, obj_tfile);
784 obstack_free(&cppflags_obst, NULL);
785 obstack_free(&ldflags_obst, NULL);