17 /* no eXecute on Win32 */
22 #define O_RDWR _O_RDWR
23 #define O_CREAT _O_CREAT
24 #define O_EXCL _O_EXCL
25 #define O_BINARY _O_BINARY
27 /* remap some names, we are not in the POSIX world */
28 #define access(fname, mode) _access(fname, mode)
29 #define mktemp(tmpl) _mktemp(tmpl)
30 #define open(fname, oflag, mode) _open(fname, oflag, mode)
31 #define fdopen(fd, mode) _fdopen(fd, mode)
32 #define popen(cmd, mode) _popen(cmd, mode)
33 #define pclose(file) _pclose(file)
44 #include <libfirm/firm.h>
45 #include <libfirm/be.h>
50 #include "type_hash.h"
53 #include "diagnostic.h"
54 #include "lang_features.h"
55 #include "driver/firm_opt.h"
56 #include "driver/firm_cmdline.h"
57 #include "adt/error.h"
58 #include "write_fluffy.h"
63 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
67 #define LINKER "gcc -m32"
71 #define ASSEMBLER "as --32"
74 /** The current c mode/dialect. */
75 unsigned int c_mode = _C89|_C99|_GNUC;
77 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
78 unsigned int machine_size = 32;
80 /** true if the char type is signed. */
81 bool char_is_signed = true;
83 /** true for strict language checking. */
84 bool strict_mode = false;
86 /* to switch on printing of implicit casts */
87 extern bool print_implicit_casts;
89 /* to switch on printing of srenthesis to indicate operator precedence */
90 extern bool print_parenthesis;
93 static struct obstack cppflags_obst, ldflags_obst;
95 #if defined(_DEBUG) || defined(FIRM_DEBUG)
97 * Debug printf implementation.
99 * @param fmt printf style format parameter
101 void dbg_printf(const char *fmt, ...)
105 if (firm_dump.debug_print) {
111 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
113 static void initialize_firm(void)
117 dump_consts_local(1);
118 dump_keepalive_edges(1);
121 static void get_output_name(char *buf, size_t buflen, const char *inputname,
124 size_t last_dot = 0xffffffff;
127 if(inputname == NULL) {
128 snprintf(buf, buflen, "a%s", newext);
132 for(const char *c = inputname; *c != 0; ++c) {
137 if(last_dot == 0xffffffff)
140 if(last_dot >= buflen)
141 panic("filename too long");
142 memcpy(buf, inputname, last_dot);
144 size_t extlen = strlen(newext) + 1;
145 if(extlen + last_dot >= buflen)
146 panic("filename too long");
147 memcpy(buf+last_dot, newext, extlen);
150 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
152 lexer_open_stream(in, input);
153 translation_unit_t *unit = parse();
157 static void lextest(FILE *in, const char *fname)
159 lexer_open_stream(in, fname);
162 lexer_next_preprocessing_token();
163 print_token(stdout, &lexer_token);
165 } while(lexer_token.type != T_EOF);
168 static FILE* preprocess(FILE* in, const char *fname)
171 obstack_1grow(&cppflags_obst, '\0');
172 const char *flags = obstack_finish(&cppflags_obst);
175 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
177 /* read from stdin */
178 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
184 FILE* f = popen(buf, "r");
186 fprintf(stderr, "invoking preprocessor failed\n");
192 static void do_link(const char *out, const char *in)
195 obstack_1grow(&ldflags_obst, '\0');
196 const char *flags = obstack_finish(&ldflags_obst);
198 snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
202 int err = system(buf);
204 fprintf(stderr, "linker reported an error\n");
209 static void assemble(const char *out, const char *in)
213 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
218 int err = system(buf);
220 fprintf(stderr, "assembler reported an error\n");
225 static const char *try_dir(const char *dir)
229 if(access(dir, R_OK | W_OK | X_OK) == 0)
234 static const char *get_tempdir(void)
236 static const char *tmpdir = NULL;
242 tmpdir = try_dir(getenv("TMPDIR"));
244 tmpdir = try_dir(getenv("TMP"));
246 tmpdir = try_dir(getenv("TEMP"));
250 tmpdir = try_dir(P_tmpdir);
254 tmpdir = try_dir("/var/tmp");
256 tmpdir = try_dir("/usr/tmp");
258 tmpdir = try_dir("/tmp");
267 /* cheap and nasty mkstemp replacement */
268 static int mkstemp(char *templ)
271 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
276 * an own version of tmpnam, which: writes in a buffer, appends a user specified
277 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
279 static FILE *make_temp_file(char *buffer, size_t buflen,
280 const char *prefix, const char *suffix)
282 const char *tempdir = get_tempdir();
284 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
288 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
290 int fd = mkstemp(buffer);
292 fprintf(stderr, "couldn't create temporary file: %s\n",
296 FILE *out = fdopen(fd, "w");
298 fprintf(stderr, "couldn't create temporary file FILE*\n");
306 * Do the necessary lowering for compound parameters.
308 void lower_compound_params(void)
310 lower_params_t params;
312 params.def_ptr_alignment = 4;
313 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
314 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
315 params.find_pointer_type = NULL;
316 params.ret_compound_in_regs = NULL;
317 lower_calls_with_compounds(¶ms);
320 typedef enum compile_mode_t {
332 static void usage(const char *argv0)
334 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
337 int main(int argc, char **argv)
341 const char *input = NULL;
342 const char *outname = NULL;
343 const char *dumpfunction = NULL;
344 compile_mode_t mode = CompileAssembleLink;
347 obstack_init(&cppflags_obst);
348 obstack_init(&ldflags_obst);
350 #define GET_ARG_AFTER(def, args) \
351 def = &arg[sizeof(args)-1]; \
352 if(def[0] == '\0') { \
355 fprintf(stderr, "error: expected argument after '" args "'\n"); \
356 argument_errors = true; \
360 if(def[0] == '-' && def[1] != '\0') { \
361 fprintf(stderr, "error: expected argument after '" args "'\n"); \
362 argument_errors = true; \
367 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
369 /* early options parsing (find out optimisation level) */
370 for(int i = 1; i < argc; ++i) {
371 const char *arg = argv[i];
375 const char *option = &arg[1];
376 if(option[0] == 'O') {
377 sscanf(&option[1], "%d", &opt_level);
381 /* apply optimisation level */
384 firm_option("no-opt");
387 firm_option("no-inline");
391 firm_option("strict-aliasing");
394 firm_option("cond-eval");
395 firm_option("if-conv");
398 firm_option("inline");
399 firm_option("no-strength-red");
400 firm_option("deconv");
401 firm_be_option("omitfp");
405 /* parse rest of options */
406 bool help_displayed = false;
407 bool argument_errors = false;
408 for(int i = 1; i < argc; ++i) {
409 const char *arg = argv[i];
412 const char *option = &arg[1];
413 if(option[0] == 'o') {
414 GET_ARG_AFTER(outname, "-o");
415 } else if(SINGLE_OPTION('c')) {
416 mode = CompileAssemble;
417 } else if(SINGLE_OPTION('S')) {
419 } else if(option[0] == 'O') {
421 } else if(option[0] == 'I') {
423 GET_ARG_AFTER(opt, "-I");
424 obstack_printf(&cppflags_obst, " -I%s", opt);
425 } else if(option[0] == 'D') {
427 GET_ARG_AFTER(opt, "-D");
428 obstack_printf(&cppflags_obst, " -D%s", opt);
429 } else if(option[0] == 'U') {
431 GET_ARG_AFTER(opt, "-U");
432 obstack_printf(&cppflags_obst, " -U%s", opt);
433 } else if(option[0] == 'l') {
435 GET_ARG_AFTER(opt, "-l");
436 obstack_printf(&ldflags_obst, " -l%s", opt);
437 } else if(option[0] == 'L') {
439 GET_ARG_AFTER(opt, "-L");
440 obstack_printf(&ldflags_obst, " -L%s", opt);
441 } else if(SINGLE_OPTION('v')) {
443 } else if(SINGLE_OPTION('w')) {
444 inhibit_all_warnings = true;
445 } else if(option[0] == 'f') {
447 GET_ARG_AFTER(opt, "-f");
449 if(strcmp(opt, "syntax-only") == 0) {
451 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
452 firm_be_option("omitfp");
453 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
454 firm_be_option("omitfp=no");
456 int res = firm_option(opt);
458 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
460 argument_errors = true;
462 } else if (res == -1) {
463 help_displayed = true;
466 } else if(option[0] == 'b') {
468 GET_ARG_AFTER(opt, "-b");
469 int res = firm_be_option(opt);
471 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
473 argument_errors = true;
474 } else if (res == -1) {
475 help_displayed = true;
477 } else if(option[0] == 'W') {
478 set_warning_opt(&option[1]);
479 } else if(option[0] == 'm') {
481 GET_ARG_AFTER(opt, "-m");
483 long int value = strtol(opt, &endptr, 10);
484 if (*endptr != '\0') {
485 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
486 argument_errors = true;
488 if (value != 16 && value != 32 && value != 64) {
489 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
490 argument_errors = true;
492 machine_size = (unsigned int)value;
494 } else if (option[0] == '\0') {
496 fprintf(stderr, "error: multiple input files specified\n");
497 argument_errors = true;
501 } else if(strcmp(option, "pedantic") == 0) {
502 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
503 } else if(option[0] == 'g' || strncmp(option, "std=", 4) == 0) {
504 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
505 } else if (option[0] == '-') {
506 /* double dash option */
508 if(strcmp(option, "gcc") == 0) {
510 } else if(strcmp(option, "no-gcc") == 0) {
512 } else if(strcmp(option, "ms") == 0) {
514 } else if(strcmp(option, "no-ms") == 0) {
516 } else if(strcmp(option, "signed-chars") == 0) {
517 char_is_signed = true;
518 } else if(strcmp(option, "unsigned-chars") == 0) {
519 char_is_signed = false;
520 } else if(strcmp(option, "strict") == 0) {
522 } else if(strcmp(option, "lextest") == 0) {
524 } else if(strcmp(option, "benchmark") == 0) {
525 mode = BenchmarkParser;
526 } else if(strcmp(option, "print-ast") == 0) {
528 } else if(strcmp(option, "print-implicit-cast") == 0) {
529 print_implicit_casts = true;
530 } else if(strcmp(option, "print-parenthesis") == 0) {
531 print_parenthesis = true;
532 } else if(strcmp(option, "print-fluffy") == 0) {
534 } else if(strcmp(option, "version") == 0) {
536 firm_get_version(&ver);
537 printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor);
538 if(ver.revision[0] != 0) {
540 fputs(ver.revision, stdout);
542 if(ver.build[0] != 0) {
544 fputs(ver.build, stdout);
548 } else if(strcmp(option, "dump-function") == 0) {
551 fprintf(stderr, "error: "
552 "expected argument after '--dump-function'\n");
553 argument_errors = true;
556 dumpfunction = argv[i];
559 fprintf(stderr, "error: unknown argument '%s'\n", arg);
560 argument_errors = true;
563 fprintf(stderr, "error: unknown argument '%s'\n", arg);
564 argument_errors = true;
568 fprintf(stderr, "error: multiple input files specified\n");
569 argument_errors = true;
576 /* we do the lowering in ast2firm */
577 firm_opt.lower_bitfields = FALSE;
580 return !argument_errors;
582 if(argument_errors) {
599 char outnamebuf[4096];
600 if(outname == NULL) {
602 case BenchmarkParser:
612 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
613 outname = outnamebuf;
615 case CompileAssemble:
616 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
617 outname = outnamebuf;
620 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
622 outname = outnamebuf;
624 case CompileAssembleLink:
630 if(outname != NULL) {
631 if(strcmp(outname, "-") == 0) {
634 out = fopen(outname, "w");
636 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
645 fprintf(stderr, "%s: no input files\n", argv[0]);
647 } else if(strcmp(input, "-") == 0) {
651 in = fopen(input, "r");
653 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
658 if(mode == LexTest) {
664 FILE *preprocessed_in = preprocess(in, input);
665 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
666 int result = pclose(preprocessed_in);
671 /* parsing failed because of errors */
672 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
675 if (warning_count > 0) {
676 fprintf(stderr, "%u warning(s)\n", warning_count);
679 if(mode == BenchmarkParser) {
683 if(mode == PrintAst) {
684 type_set_output(out);
689 if(mode == PrintFluffy) {
690 type_set_output(out);
692 write_fluffy_decls(out, unit);
695 translation_unit_to_firm(unit);
697 if(mode == ParseOnly) {
702 char asm_tempfile[1024];
703 if(mode == CompileDump) {
705 firm_be_opt.selection = BE_NONE;
706 } else if(mode == Compile) {
710 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
712 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
714 if(mode == CompileDump) {
716 ident *id = new_id_from_str(dumpfunction);
717 ir_graph *irg = NULL;
718 int n_irgs = get_irp_n_irgs();
719 for(int i = 0; i < n_irgs; ++i) {
720 ir_graph *tirg = get_irp_irg(i);
721 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
729 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
733 dump_ir_block_graph_file(irg, out);
740 /* assemble assembler and create object file */
741 char obj_tfile[1024];
742 if(mode == CompileAssemble || mode == CompileAssembleLink) {
743 const char *obj_outfile;
744 if(mode == CompileAssemble) {
746 obj_outfile = outname;
749 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
751 obj_outfile = obj_tfile;
754 assemble(obj_outfile, asm_tempfile);
757 /* link object file */
758 if(mode == CompileAssembleLink) {
759 do_link(outname, obj_tfile);
762 obstack_free(&cppflags_obst, NULL);
763 obstack_free(&ldflags_obst, NULL);