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;
87 static struct obstack cppflags_obst, ldflags_obst;
89 #if defined(_DEBUG) || defined(FIRM_DEBUG)
91 * Debug printf implementation.
93 * @param fmt printf style format parameter
95 void dbg_printf(const char *fmt, ...)
99 if (firm_dump.debug_print) {
105 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
107 static void initialize_firm(void)
111 dump_consts_local(1);
112 dump_keepalive_edges(1);
115 static void get_output_name(char *buf, size_t buflen, const char *inputname,
118 size_t last_dot = 0xffffffff;
121 if(inputname == NULL) {
122 snprintf(buf, buflen, "a%s", newext);
126 for(const char *c = inputname; *c != 0; ++c) {
131 if(last_dot == 0xffffffff)
134 if(last_dot >= buflen)
135 panic("filename too long");
136 memcpy(buf, inputname, last_dot);
138 size_t extlen = strlen(newext) + 1;
139 if(extlen + last_dot >= buflen)
140 panic("filename too long");
141 memcpy(buf+last_dot, newext, extlen);
144 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
146 lexer_open_stream(in, input);
147 translation_unit_t *unit = parse();
151 static void lextest(FILE *in, const char *fname)
153 lexer_open_stream(in, fname);
156 lexer_next_preprocessing_token();
157 print_token(stdout, &lexer_token);
159 } while(lexer_token.type != T_EOF);
162 static FILE* preprocess(FILE* in, const char *fname)
165 const char *flags = obstack_finish(&cppflags_obst);
168 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
170 /* read from stdin */
171 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
177 FILE* f = popen(buf, "r");
179 fprintf(stderr, "invoking preprocessor failed\n");
185 static void do_link(const char *out, const char *in)
188 const char *flags = obstack_finish(&ldflags_obst);
190 snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
194 int err = system(buf);
196 fprintf(stderr, "linker reported an error\n");
201 static void assemble(const char *out, const char *in)
205 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
210 int err = system(buf);
212 fprintf(stderr, "assembler reported an error\n");
217 static const char *try_dir(const char *dir)
221 if(access(dir, R_OK | W_OK | X_OK) == 0)
226 static const char *get_tempdir(void)
228 static const char *tmpdir = NULL;
234 tmpdir = try_dir(getenv("TMPDIR"));
236 tmpdir = try_dir(getenv("TMP"));
238 tmpdir = try_dir(getenv("TEMP"));
242 tmpdir = try_dir(P_tmpdir);
246 tmpdir = try_dir("/var/tmp");
248 tmpdir = try_dir("/usr/tmp");
250 tmpdir = try_dir("/tmp");
259 /* cheap and nasty mkstemp replacement */
260 static int mkstemp(char *templ)
263 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
268 * an own version of tmpnam, which: writes in a buffer, appends a user specified
269 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
271 static FILE *make_temp_file(char *buffer, size_t buflen,
272 const char *prefix, const char *suffix)
274 const char *tempdir = get_tempdir();
276 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
280 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
282 int fd = mkstemp(buffer);
284 fprintf(stderr, "couldn't create temporary file: %s\n",
288 FILE *out = fdopen(fd, "w");
290 fprintf(stderr, "couldn't create temporary file FILE*\n");
298 * Do the necessary lowering for compound parameters.
300 void lower_compound_params(void)
302 lower_params_t params;
304 params.def_ptr_alignment = 4;
305 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
306 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
307 params.find_pointer_type = NULL;
308 params.ret_compound_in_regs = NULL;
309 lower_calls_with_compounds(¶ms);
312 typedef enum compile_mode_t {
323 static void usage(const char *argv0)
325 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
328 int main(int argc, char **argv)
332 const char *input = NULL;
333 const char *outname = NULL;
334 const char *dumpfunction = NULL;
335 compile_mode_t mode = CompileAssembleLink;
337 obstack_init(&cppflags_obst);
338 obstack_init(&ldflags_obst);
340 #define GET_ARG_AFTER(def, args) \
341 def = &arg[sizeof(args)-1]; \
342 if(def[0] == '\0') { \
345 fprintf(stderr, "error: expected argument after '" args "'\n"); \
346 argument_errors = true; \
350 if(def[0] == '-' && def[1] != '\0') { \
351 fprintf(stderr, "error: expected argument after '" args "'\n"); \
352 argument_errors = true; \
357 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
359 bool help_displayed = false;
360 bool argument_errors = false;
361 for(int i = 1; i < argc; ++i) {
362 const char *arg = argv[i];
365 const char *option = &arg[1];
366 if(option[0] == 'o') {
367 GET_ARG_AFTER(outname, "-o");
368 } else if(SINGLE_OPTION('c')) {
369 mode = CompileAssemble;
370 } else if(SINGLE_OPTION('S')) {
372 } else if(option[0] == 'I') {
374 GET_ARG_AFTER(opt, "-I");
375 obstack_printf(&cppflags_obst, " -I%s", opt);
376 } else if(option[0] == 'D') {
378 GET_ARG_AFTER(opt, "-D");
379 obstack_printf(&cppflags_obst, " -D%s", opt);
380 } else if(option[0] == 'U') {
382 GET_ARG_AFTER(opt, "-U");
383 obstack_printf(&cppflags_obst, " -U%s", opt);
384 } else if(option[0] == 'l') {
386 GET_ARG_AFTER(opt, "-l");
387 obstack_printf(&ldflags_obst, " -l%s", opt);
388 } else if(option[0] == 'L') {
390 GET_ARG_AFTER(opt, "-L");
391 obstack_printf(&ldflags_obst, " -L%s", opt);
392 } else if(SINGLE_OPTION('v')) {
394 } else if(SINGLE_OPTION('w')) {
395 inhibit_all_warnings = true;
396 } else if(option[0] == 'f') {
398 GET_ARG_AFTER(opt, "-f");
400 if(strcmp(opt, "syntax-only") == 0) {
402 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
403 firm_be_option("omitfp");
404 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
405 firm_be_option("omitfp=no");
407 int res = firm_option(opt);
409 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
411 argument_errors = true;
413 } else if (res == -1) {
414 help_displayed = true;
417 } else if(option[0] == 'b') {
419 GET_ARG_AFTER(opt, "-b");
420 int res = firm_be_option(opt);
422 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
424 argument_errors = true;
425 } else if (res == -1) {
426 help_displayed = true;
428 } else if(option[0] == 'W') {
429 set_warning_opt(&option[1]);
430 } else if(option[0] == 'm') {
432 GET_ARG_AFTER(opt, "-m");
434 long int value = strtol(opt, &endptr, 10);
435 if (*endptr != '\0') {
436 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
437 argument_errors = true;
439 if (value != 16 && value != 32 && value != 64) {
440 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
441 argument_errors = true;
443 machine_size = (unsigned int)value;
445 } else if (option[0] == '\0') {
447 fprintf(stderr, "error: multiple input files specified\n");
448 argument_errors = true;
452 } else if(strcmp(option, "pedantic") == 0) {
453 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
454 } else if(option[0] == 'O' ||
456 strncmp(option, "std=", 4) == 0) {
457 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
458 } else if (option[0] == '-') {
459 /* double dash option */
461 if(strcmp(option, "gcc") == 0) {
463 } else if(strcmp(option, "no-gcc") == 0) {
465 } else if(strcmp(option, "ms") == 0) {
467 } else if(strcmp(option, "no-ms") == 0) {
469 } else if(strcmp(option, "signed-chars") == 0) {
470 char_is_signed = true;
471 } else if(strcmp(option, "unsigned-chars") == 0) {
472 char_is_signed = false;
473 } else if(strcmp(option, "strict") == 0) {
475 } else if(strcmp(option, "lextest") == 0) {
477 } else if(strcmp(option, "print-ast") == 0) {
479 } else if(strcmp(option, "print-fluffy") == 0) {
481 } else if(strcmp(option, "version") == 0) {
483 firm_get_version(&ver);
484 printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor);
485 if(ver.revision[0] != 0) {
487 fputs(ver.revision, stdout);
489 if(ver.build[0] != 0) {
491 fputs(ver.build, stdout);
495 } else if(strcmp(option, "dump-function") == 0) {
498 fprintf(stderr, "error: "
499 "expected argument after '--dump-function'\n");
500 argument_errors = true;
503 dumpfunction = argv[i];
506 fprintf(stderr, "error: unknown argument '%s'\n", arg);
507 argument_errors = true;
510 fprintf(stderr, "error: unknown argument '%s'\n", arg);
511 argument_errors = true;
515 fprintf(stderr, "error: multiple input files specified\n");
516 argument_errors = true;
523 /* we do the lowering in ast2firm */
524 firm_opt.lower_bitfields = FALSE;
527 return !argument_errors;
529 if(argument_errors) {
546 char outnamebuf[4096];
547 if(outname == NULL) {
558 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
559 outname = outnamebuf;
561 case CompileAssemble:
562 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
563 outname = outnamebuf;
566 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
568 outname = outnamebuf;
570 case CompileAssembleLink:
576 if(outname != NULL) {
577 if(strcmp(outname, "-") == 0) {
580 out = fopen(outname, "w");
582 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
591 fprintf(stderr, "%s: no input files\n", argv[0]);
593 } else if(strcmp(input, "-") == 0) {
597 in = fopen(input, "r");
599 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
604 if(mode == LexTest) {
610 FILE *preprocessed_in = preprocess(in, input);
611 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
612 int result = pclose(preprocessed_in);
617 /* parsing failed because of errors */
618 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
621 if (warning_count > 0) {
622 fprintf(stderr, "%u warning(s)\n", warning_count);
625 if(mode == PrintAst) {
626 type_set_output(out);
631 if(mode == PrintFluffy) {
632 type_set_output(out);
634 write_fluffy_decls(out, unit);
637 translation_unit_to_firm(unit);
639 if(mode == ParseOnly) {
644 char asm_tempfile[1024];
645 if(mode == CompileDump) {
647 firm_be_opt.selection = BE_NONE;
648 } else if(mode == Compile) {
652 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
654 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
656 if(mode == CompileDump) {
658 ident *id = new_id_from_str(dumpfunction);
659 ir_graph *irg = NULL;
660 int n_irgs = get_irp_n_irgs();
661 for(int i = 0; i < n_irgs; ++i) {
662 ir_graph *tirg = get_irp_irg(i);
663 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
671 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
675 dump_ir_block_graph_file(irg, out);
682 /* assemble assembler and create object file */
683 char obj_tfile[1024];
684 if(mode == CompileAssemble || mode == CompileAssembleLink) {
685 const char *obj_outfile;
686 if(mode == CompileAssemble) {
688 obj_outfile = outname;
691 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
693 obj_outfile = obj_tfile;
696 assemble(obj_outfile, asm_tempfile);
699 /* link object file */
700 if(mode == CompileAssembleLink) {
701 do_link(outname, obj_tfile);
704 obstack_free(&cppflags_obst, NULL);
705 obstack_free(&ldflags_obst, NULL);