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 const char *flags = obstack_finish(&cppflags_obst);
174 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
176 /* read from stdin */
177 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
183 FILE* f = popen(buf, "r");
185 fprintf(stderr, "invoking preprocessor failed\n");
191 static void do_link(const char *out, const char *in)
194 const char *flags = obstack_finish(&ldflags_obst);
196 snprintf(buf, sizeof(buf), LINKER " %s -o %s %s", flags, out, in);
200 int err = system(buf);
202 fprintf(stderr, "linker reported an error\n");
207 static void assemble(const char *out, const char *in)
211 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
216 int err = system(buf);
218 fprintf(stderr, "assembler reported an error\n");
223 static const char *try_dir(const char *dir)
227 if(access(dir, R_OK | W_OK | X_OK) == 0)
232 static const char *get_tempdir(void)
234 static const char *tmpdir = NULL;
240 tmpdir = try_dir(getenv("TMPDIR"));
242 tmpdir = try_dir(getenv("TMP"));
244 tmpdir = try_dir(getenv("TEMP"));
248 tmpdir = try_dir(P_tmpdir);
252 tmpdir = try_dir("/var/tmp");
254 tmpdir = try_dir("/usr/tmp");
256 tmpdir = try_dir("/tmp");
265 /* cheap and nasty mkstemp replacement */
266 static int mkstemp(char *templ)
269 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
274 * an own version of tmpnam, which: writes in a buffer, appends a user specified
275 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
277 static FILE *make_temp_file(char *buffer, size_t buflen,
278 const char *prefix, const char *suffix)
280 const char *tempdir = get_tempdir();
282 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
286 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
288 int fd = mkstemp(buffer);
290 fprintf(stderr, "couldn't create temporary file: %s\n",
294 FILE *out = fdopen(fd, "w");
296 fprintf(stderr, "couldn't create temporary file FILE*\n");
304 * Do the necessary lowering for compound parameters.
306 void lower_compound_params(void)
308 lower_params_t params;
310 params.def_ptr_alignment = 4;
311 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
312 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
313 params.find_pointer_type = NULL;
314 params.ret_compound_in_regs = NULL;
315 lower_calls_with_compounds(¶ms);
318 typedef enum compile_mode_t {
329 static void usage(const char *argv0)
331 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
334 int main(int argc, char **argv)
338 const char *input = NULL;
339 const char *outname = NULL;
340 const char *dumpfunction = NULL;
341 compile_mode_t mode = CompileAssembleLink;
343 obstack_init(&cppflags_obst);
344 obstack_init(&ldflags_obst);
346 #define GET_ARG_AFTER(def, args) \
347 def = &arg[sizeof(args)-1]; \
348 if(def[0] == '\0') { \
351 fprintf(stderr, "error: expected argument after '" args "'\n"); \
352 argument_errors = true; \
356 if(def[0] == '-' && def[1] != '\0') { \
357 fprintf(stderr, "error: expected argument after '" args "'\n"); \
358 argument_errors = true; \
363 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
365 bool help_displayed = false;
366 bool argument_errors = false;
367 for(int i = 1; i < argc; ++i) {
368 const char *arg = argv[i];
371 const char *option = &arg[1];
372 if(option[0] == 'o') {
373 GET_ARG_AFTER(outname, "-o");
374 } else if(SINGLE_OPTION('c')) {
375 mode = CompileAssemble;
376 } else if(SINGLE_OPTION('S')) {
378 } else if(option[0] == 'I') {
380 GET_ARG_AFTER(opt, "-I");
381 obstack_printf(&cppflags_obst, " -I%s", opt);
382 } else if(option[0] == 'D') {
384 GET_ARG_AFTER(opt, "-D");
385 obstack_printf(&cppflags_obst, " -D%s", opt);
386 } else if(option[0] == 'U') {
388 GET_ARG_AFTER(opt, "-U");
389 obstack_printf(&cppflags_obst, " -U%s", opt);
390 } else if(option[0] == 'l') {
392 GET_ARG_AFTER(opt, "-l");
393 obstack_printf(&ldflags_obst, " -l%s", opt);
394 } else if(option[0] == 'L') {
396 GET_ARG_AFTER(opt, "-L");
397 obstack_printf(&ldflags_obst, " -L%s", opt);
398 } else if(SINGLE_OPTION('v')) {
400 } else if(SINGLE_OPTION('w')) {
401 inhibit_all_warnings = true;
402 } else if(option[0] == 'f') {
404 GET_ARG_AFTER(opt, "-f");
406 if(strcmp(opt, "syntax-only") == 0) {
408 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
409 firm_be_option("omitfp");
410 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
411 firm_be_option("omitfp=no");
413 int res = firm_option(opt);
415 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
417 argument_errors = true;
419 } else if (res == -1) {
420 help_displayed = true;
423 } else if(option[0] == 'b') {
425 GET_ARG_AFTER(opt, "-b");
426 int res = firm_be_option(opt);
428 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
430 argument_errors = true;
431 } else if (res == -1) {
432 help_displayed = true;
434 } else if(option[0] == 'W') {
435 set_warning_opt(&option[1]);
436 } else if(option[0] == 'm') {
438 GET_ARG_AFTER(opt, "-m");
440 long int value = strtol(opt, &endptr, 10);
441 if (*endptr != '\0') {
442 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
443 argument_errors = true;
445 if (value != 16 && value != 32 && value != 64) {
446 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
447 argument_errors = true;
449 machine_size = (unsigned int)value;
451 } else if (option[0] == '\0') {
453 fprintf(stderr, "error: multiple input files specified\n");
454 argument_errors = true;
458 } else if(strcmp(option, "pedantic") == 0) {
459 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
460 } else if(option[0] == 'O' ||
462 strncmp(option, "std=", 4) == 0) {
463 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
464 } else if (option[0] == '-') {
465 /* double dash option */
467 if(strcmp(option, "gcc") == 0) {
469 } else if(strcmp(option, "no-gcc") == 0) {
471 } else if(strcmp(option, "ms") == 0) {
473 } else if(strcmp(option, "no-ms") == 0) {
475 } else if(strcmp(option, "signed-chars") == 0) {
476 char_is_signed = true;
477 } else if(strcmp(option, "unsigned-chars") == 0) {
478 char_is_signed = false;
479 } else if(strcmp(option, "strict") == 0) {
481 } else if(strcmp(option, "lextest") == 0) {
483 } else if(strcmp(option, "print-ast") == 0) {
485 } else if(strcmp(option, "print-implicit-cast") == 0) {
486 print_implicit_casts = true;
487 } else if(strcmp(option, "print-parenthesis") == 0) {
488 print_parenthesis = true;
489 } else if(strcmp(option, "print-fluffy") == 0) {
491 } else if(strcmp(option, "version") == 0) {
493 firm_get_version(&ver);
494 printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor);
495 if(ver.revision[0] != 0) {
497 fputs(ver.revision, stdout);
499 if(ver.build[0] != 0) {
501 fputs(ver.build, stdout);
505 } else if(strcmp(option, "dump-function") == 0) {
508 fprintf(stderr, "error: "
509 "expected argument after '--dump-function'\n");
510 argument_errors = true;
513 dumpfunction = argv[i];
516 fprintf(stderr, "error: unknown argument '%s'\n", arg);
517 argument_errors = true;
520 fprintf(stderr, "error: unknown argument '%s'\n", arg);
521 argument_errors = true;
525 fprintf(stderr, "error: multiple input files specified\n");
526 argument_errors = true;
533 /* we do the lowering in ast2firm */
534 firm_opt.lower_bitfields = FALSE;
537 return !argument_errors;
539 if(argument_errors) {
556 char outnamebuf[4096];
557 if(outname == NULL) {
568 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
569 outname = outnamebuf;
571 case CompileAssemble:
572 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
573 outname = outnamebuf;
576 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
578 outname = outnamebuf;
580 case CompileAssembleLink:
586 if(outname != NULL) {
587 if(strcmp(outname, "-") == 0) {
590 out = fopen(outname, "w");
592 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
601 fprintf(stderr, "%s: no input files\n", argv[0]);
603 } else if(strcmp(input, "-") == 0) {
607 in = fopen(input, "r");
609 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
614 if(mode == LexTest) {
620 FILE *preprocessed_in = preprocess(in, input);
621 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
622 int result = pclose(preprocessed_in);
627 /* parsing failed because of errors */
628 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
631 if (warning_count > 0) {
632 fprintf(stderr, "%u warning(s)\n", warning_count);
635 if(mode == PrintAst) {
636 type_set_output(out);
641 if(mode == PrintFluffy) {
642 type_set_output(out);
644 write_fluffy_decls(out, unit);
647 translation_unit_to_firm(unit);
649 if(mode == ParseOnly) {
654 char asm_tempfile[1024];
655 if(mode == CompileDump) {
657 firm_be_opt.selection = BE_NONE;
658 } else if(mode == Compile) {
662 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
664 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
666 if(mode == CompileDump) {
668 ident *id = new_id_from_str(dumpfunction);
669 ir_graph *irg = NULL;
670 int n_irgs = get_irp_n_irgs();
671 for(int i = 0; i < n_irgs; ++i) {
672 ir_graph *tirg = get_irp_irg(i);
673 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
681 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
685 dump_ir_block_graph_file(irg, out);
692 /* assemble assembler and create object file */
693 char obj_tfile[1024];
694 if(mode == CompileAssemble || mode == CompileAssembleLink) {
695 const char *obj_outfile;
696 if(mode == CompileAssemble) {
698 obj_outfile = outname;
701 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
703 obj_outfile = obj_tfile;
706 assemble(obj_outfile, asm_tempfile);
709 /* link object file */
710 if(mode == CompileAssembleLink) {
711 do_link(outname, obj_tfile);
714 obstack_free(&cppflags_obst, NULL);
715 obstack_free(&ldflags_obst, NULL);