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"
62 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
66 #define LINKER "gcc -m32"
70 #define ASSEMBLER "as --32"
73 /** The current c mode/dialect. */
74 unsigned int c_mode = _C89|_C99|_GNUC;
76 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
77 unsigned int machine_size = 32;
79 /** true if the char type is signed. */
80 bool char_is_signed = true;
82 /** true for strict language checking. */
83 bool strict_mode = false;
86 static struct obstack cppflags_obst;
88 #if defined(_DEBUG) || defined(FIRM_DEBUG)
90 * Debug printf implementation.
92 * @param fmt printf style format parameter
94 void dbg_printf(const char *fmt, ...)
98 if (firm_dump.debug_print) {
104 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
106 static void initialize_firm(void)
110 dump_consts_local(1);
111 dump_keepalive_edges(1);
114 static void get_output_name(char *buf, size_t buflen, const char *inputname,
117 size_t last_dot = 0xffffffff;
120 if(inputname == NULL) {
121 snprintf(buf, buflen, "a%s", newext);
125 for(const char *c = inputname; *c != 0; ++c) {
130 if(last_dot == 0xffffffff)
133 if(last_dot >= buflen)
134 panic("filename too long");
135 memcpy(buf, inputname, last_dot);
137 size_t extlen = strlen(newext) + 1;
138 if(extlen + last_dot >= buflen)
139 panic("filename too long");
140 memcpy(buf+last_dot, newext, extlen);
143 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
145 lexer_open_stream(in, input);
146 translation_unit_t *unit = parse();
150 static void lextest(FILE *in, const char *fname)
152 lexer_open_stream(in, fname);
155 lexer_next_preprocessing_token();
156 print_token(stdout, &lexer_token);
158 } while(lexer_token.type != T_EOF);
161 static FILE* preprocess(FILE* in, const char *fname)
164 const char *flags = obstack_finish(&cppflags_obst);
167 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
169 /* read from stdin */
170 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
176 FILE* f = popen(buf, "r");
178 fprintf(stderr, "invoking preprocessor failed\n");
184 static void do_link(const char *out, const char *in)
188 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
192 int err = system(buf);
194 fprintf(stderr, "linker reported an error\n");
199 static void assemble(const char *out, const char *in)
203 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
208 int err = system(buf);
210 fprintf(stderr, "assembler reported an error\n");
215 static const char *try_dir(const char *dir)
219 if(access(dir, R_OK | W_OK | X_OK) == 0)
224 static const char *get_tempdir(void)
226 static const char *tmpdir = NULL;
232 tmpdir = try_dir(getenv("TMPDIR"));
234 tmpdir = try_dir(getenv("TMP"));
236 tmpdir = try_dir(getenv("TEMP"));
240 tmpdir = try_dir(P_tmpdir);
244 tmpdir = try_dir("/var/tmp");
246 tmpdir = try_dir("/usr/tmp");
248 tmpdir = try_dir("/tmp");
257 /* cheap and nasty mkstemp replacement */
258 static int mkstemp(char *templ)
261 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
266 * an own version of tmpnam, which: writes in a buffer, appends a user specified
267 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
269 static FILE *make_temp_file(char *buffer, size_t buflen,
270 const char *prefix, const char *suffix)
272 const char *tempdir = get_tempdir();
274 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
278 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
280 int fd = mkstemp(buffer);
282 fprintf(stderr, "couldn't create temporary file: %s\n",
286 FILE *out = fdopen(fd, "w");
288 fprintf(stderr, "couldn't create temporary file FILE*\n");
296 * Do the necessary lowering for compound parameters.
298 void lower_compound_params(void)
300 lower_params_t params;
302 params.def_ptr_alignment = 4;
303 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
304 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
305 params.find_pointer_type = NULL;
306 params.ret_compound_in_regs = NULL;
307 lower_calls_with_compounds(¶ms);
310 typedef enum compile_mode_t {
321 static void usage(const char *argv0)
323 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
326 int main(int argc, char **argv)
330 const char *input = NULL;
331 const char *outname = NULL;
332 const char *dumpfunction = NULL;
333 compile_mode_t mode = CompileAssembleLink;
335 obstack_init(&cppflags_obst);
337 #define GET_ARG_AFTER(def, args) \
338 def = &arg[sizeof(args)-1]; \
339 if(def[0] == '\0') { \
342 fprintf(stderr, "error: expected argument after '" args "'\n"); \
343 argument_errors = true; \
347 if(def[0] == '-' && def[1] != '\0') { \
348 fprintf(stderr, "error: expected argument after '" args "'\n"); \
349 argument_errors = true; \
354 bool help_displayed = false;
355 bool argument_errors = false;
356 for(int i = 1; i < argc; ++i) {
357 const char *arg = argv[i];
358 if(strncmp(arg, "-o", 2) == 0) {
359 GET_ARG_AFTER(outname, "-o");
360 } else if(strcmp(arg, "-c") == 0) {
361 mode = CompileAssemble;
362 } else if(strcmp(arg, "-S") == 0) {
364 } else if(strcmp(arg, "--gcc") == 0) {
366 } else if(strcmp(arg, "--no-gcc") == 0) {
368 } else if(strcmp(arg, "--ms") == 0) {
370 } else if(strcmp(arg, "--signed-chars") == 0) {
371 char_is_signed = true;
372 } else if(strcmp(arg, "--unsigned-chars") == 0) {
373 char_is_signed = false;
374 } else if(strcmp(arg, "--strict") == 0) {
376 } else if(strcmp(arg, "--no-ms") == 0) {
378 } else if(strcmp(arg, "--lextest") == 0) {
380 } else if(strcmp(arg, "--print-ast") == 0) {
382 } else if(strcmp(arg, "--print-fluffy") == 0) {
384 } else if(strcmp(arg, "--version") == 0) {
386 firm_get_version(&ver);
387 printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor);
388 if(ver.revision[0] != 0) {
390 fputs(ver.revision, stdout);
392 if(ver.build[0] != 0) {
394 fputs(ver.build, stdout);
398 } else if(strcmp(arg, "-fsyntax-only") == 0) {
400 } else if(strncmp(arg, "-I", 2) == 0) {
402 GET_ARG_AFTER(opt, "-I");
403 obstack_printf(&cppflags_obst, " -I%s", opt);
404 } else if(strncmp(arg, "-D", 2) == 0) {
406 GET_ARG_AFTER(opt, "-D");
407 obstack_printf(&cppflags_obst, " -D%s", opt);
408 } else if(strncmp(arg, "-U", 2) == 0) {
410 GET_ARG_AFTER(opt, "-U");
411 obstack_printf(&cppflags_obst, " -U%s", opt);
412 } else if(strcmp(arg, "--dump-function") == 0) {
415 fprintf(stderr, "error: "
416 "expected argument after '--dump-function'\n");
417 argument_errors = true;
420 dumpfunction = argv[i];
422 } else if(strcmp(arg, "-v") == 0) {
424 } else if(strcmp(arg, "-w") == 0) {
425 inhibit_all_warnings = true;
426 } else if(arg[0] == '-' && arg[1] == 'f') {
428 GET_ARG_AFTER(opt, "-f");
430 if(strcmp(opt, "omit-frame-pointer") == 0) {
431 firm_be_option("omitfp");
432 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
433 firm_be_option("omitfp=no");
435 int res = firm_option(opt);
437 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
439 argument_errors = true;
441 } else if (res == -1) {
442 help_displayed = true;
445 } else if(arg[0] == '-' && arg[1] == 'b') {
447 GET_ARG_AFTER(opt, "-b");
448 int res = firm_be_option(opt);
450 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
452 argument_errors = true;
453 } else if (res == -1) {
454 help_displayed = true;
456 } else if(arg[0] == '-' && arg[1] == 'W') {
458 GET_ARG_AFTER(opt, "-W");
459 if (strcmp(opt, "error") == 0) {
460 warnings_are_errors = true;
461 } else if (strcmp(opt, "fatal-errors") == 0) {
464 fprintf(stderr, "warning: ignoring gcc option -W%s\n", opt);
466 } else if(arg[0] == '-' && arg[1] == 'm') {
468 GET_ARG_AFTER(opt, "-m");
470 long int value = strtol(opt, &endptr, 10);
471 if (*endptr != '\0') {
472 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
473 argument_errors = true;
475 if (value != 16 && value != 32 && value != 64) {
476 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
477 argument_errors = true;
479 machine_size = (unsigned int)value;
481 } else if(arg[0] == '-') {
482 if (arg[1] == '\0') {
484 fprintf(stderr, "error: multiple input files specified\n");
485 argument_errors = true;
489 } else if(strcmp(arg, "-pedantic") == 0) {
490 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
491 } else if(arg[1] == 'O' ||
493 strncmp(arg + 1, "std=", 4) == 0) {
494 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
496 fprintf(stderr, "error: unknown argument '%s'\n", arg);
497 argument_errors = true;
501 fprintf(stderr, "error: multiple input files specified\n");
502 argument_errors = true;
509 /* we do the lowering in ast2firm */
510 firm_opt.lower_bitfields = FALSE;
513 return !argument_errors;
515 if(argument_errors) {
532 char outnamebuf[4096];
533 if(outname == NULL) {
544 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
545 outname = outnamebuf;
547 case CompileAssemble:
548 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
549 outname = outnamebuf;
552 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
554 outname = outnamebuf;
556 case CompileAssembleLink:
562 if(outname != NULL) {
563 if(strcmp(outname, "-") == 0) {
566 out = fopen(outname, "w");
568 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
577 fprintf(stderr, "%s: no input files\n", argv[0]);
579 } else if(strcmp(input, "-") == 0) {
583 in = fopen(input, "r");
585 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
590 if(mode == LexTest) {
596 FILE *preprocessed_in = preprocess(in, input);
597 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
598 int result = pclose(preprocessed_in);
603 /* parsing failed because of errors */
604 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
607 if (warning_count > 0) {
608 fprintf(stderr, "%u warning(s)\n", warning_count);
611 if(mode == PrintAst) {
612 type_set_output(out);
617 if(mode == PrintFluffy) {
618 type_set_output(out);
620 write_fluffy_decls(out, unit);
623 translation_unit_to_firm(unit);
625 if(mode == ParseOnly) {
630 char asm_tempfile[1024];
631 if(mode == CompileDump) {
633 firm_be_opt.selection = BE_NONE;
634 } else if(mode == Compile) {
638 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
640 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
642 if(mode == CompileDump) {
644 ident *id = new_id_from_str(dumpfunction);
645 ir_graph *irg = NULL;
646 int n_irgs = get_irp_n_irgs();
647 for(int i = 0; i < n_irgs; ++i) {
648 ir_graph *tirg = get_irp_irg(i);
649 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
657 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
661 dump_ir_block_graph_file(irg, out);
668 /* assemble assembler and create object file */
669 char obj_tfile[1024];
670 if(mode == CompileAssemble || mode == CompileAssembleLink) {
671 const char *obj_outfile;
672 if(mode == CompileAssemble) {
674 obj_outfile = outname;
677 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
679 obj_outfile = obj_tfile;
682 assemble(obj_outfile, asm_tempfile);
685 /* link object file */
686 if(mode == CompileAssembleLink) {
687 do_link(outname, obj_tfile);
690 obstack_free(&cppflags_obst, NULL);