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;
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)
189 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
193 int err = system(buf);
195 fprintf(stderr, "linker reported an error\n");
200 static void assemble(const char *out, const char *in)
204 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
209 int err = system(buf);
211 fprintf(stderr, "assembler reported an error\n");
216 static const char *try_dir(const char *dir)
220 if(access(dir, R_OK | W_OK | X_OK) == 0)
225 static const char *get_tempdir(void)
227 static const char *tmpdir = NULL;
233 tmpdir = try_dir(getenv("TMPDIR"));
235 tmpdir = try_dir(getenv("TMP"));
237 tmpdir = try_dir(getenv("TEMP"));
241 tmpdir = try_dir(P_tmpdir);
245 tmpdir = try_dir("/var/tmp");
247 tmpdir = try_dir("/usr/tmp");
249 tmpdir = try_dir("/tmp");
258 /* cheap and nasty mkstemp replacement */
259 static int mkstemp(char *templ)
262 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
267 * an own version of tmpnam, which: writes in a buffer, appends a user specified
268 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
270 static FILE *make_temp_file(char *buffer, size_t buflen,
271 const char *prefix, const char *suffix)
273 const char *tempdir = get_tempdir();
275 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
279 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
281 int fd = mkstemp(buffer);
283 fprintf(stderr, "couldn't create temporary file: %s\n",
287 FILE *out = fdopen(fd, "w");
289 fprintf(stderr, "couldn't create temporary file FILE*\n");
297 * Do the necessary lowering for compound parameters.
299 void lower_compound_params(void)
301 lower_params_t params;
303 params.def_ptr_alignment = 4;
304 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
305 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
306 params.find_pointer_type = NULL;
307 params.ret_compound_in_regs = NULL;
308 lower_calls_with_compounds(¶ms);
311 typedef enum compile_mode_t {
322 static void usage(const char *argv0)
324 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
327 int main(int argc, char **argv)
331 const char *input = NULL;
332 const char *outname = NULL;
333 const char *dumpfunction = NULL;
334 compile_mode_t mode = CompileAssembleLink;
336 obstack_init(&cppflags_obst);
338 #define GET_ARG_AFTER(def, args) \
339 def = &arg[sizeof(args)-1]; \
340 if(def[0] == '\0') { \
343 fprintf(stderr, "error: expected argument after '" args "'\n"); \
344 argument_errors = true; \
348 if(def[0] == '-' && def[1] != '\0') { \
349 fprintf(stderr, "error: expected argument after '" args "'\n"); \
350 argument_errors = true; \
355 bool help_displayed = false;
356 bool argument_errors = false;
357 for(int i = 1; i < argc; ++i) {
358 const char *arg = argv[i];
359 if(strncmp(arg, "-o", 2) == 0) {
360 GET_ARG_AFTER(outname, "-o");
361 } else if(strcmp(arg, "-c") == 0) {
362 mode = CompileAssemble;
363 } else if(strcmp(arg, "-S") == 0) {
365 } else if(strcmp(arg, "--gcc") == 0) {
367 } else if(strcmp(arg, "--no-gcc") == 0) {
369 } else if(strcmp(arg, "--ms") == 0) {
371 } else if(strcmp(arg, "--signed-chars") == 0) {
372 char_is_signed = true;
373 } else if(strcmp(arg, "--unsigned-chars") == 0) {
374 char_is_signed = false;
375 } else if(strcmp(arg, "--strict") == 0) {
377 } else if(strcmp(arg, "--no-ms") == 0) {
379 } else if(strcmp(arg, "--lextest") == 0) {
381 } else if(strcmp(arg, "--print-ast") == 0) {
383 } else if(strcmp(arg, "--print-fluffy") == 0) {
385 } else if(strcmp(arg, "--version") == 0) {
387 firm_get_version(&ver);
388 printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor);
389 if(ver.revision[0] != 0) {
391 fputs(ver.revision, stdout);
393 if(ver.build[0] != 0) {
395 fputs(ver.build, stdout);
399 } else if(strcmp(arg, "-fsyntax-only") == 0) {
401 } else if(strncmp(arg, "-I", 2) == 0) {
403 GET_ARG_AFTER(opt, "-I");
404 obstack_printf(&cppflags_obst, " -I%s", opt);
405 } else if(strncmp(arg, "-D", 2) == 0) {
407 GET_ARG_AFTER(opt, "-D");
408 obstack_printf(&cppflags_obst, " -D%s", opt);
409 } else if(strncmp(arg, "-U", 2) == 0) {
411 GET_ARG_AFTER(opt, "-U");
412 obstack_printf(&cppflags_obst, " -U%s", opt);
413 } else if(strcmp(arg, "--dump-function") == 0) {
416 fprintf(stderr, "error: "
417 "expected argument after '--dump-function'\n");
418 argument_errors = true;
421 dumpfunction = argv[i];
423 } else if(strcmp(arg, "-v") == 0) {
425 } else if(strcmp(arg, "-w") == 0) {
426 inhibit_all_warnings = true;
427 } else if(arg[0] == '-' && arg[1] == 'f') {
429 GET_ARG_AFTER(opt, "-f");
431 if(strcmp(opt, "omit-frame-pointer") == 0) {
432 firm_be_option("omitfp");
433 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
434 firm_be_option("omitfp=no");
436 int res = firm_option(opt);
438 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
440 argument_errors = true;
442 } else if (res == -1) {
443 help_displayed = true;
446 } else if(arg[0] == '-' && arg[1] == 'b') {
448 GET_ARG_AFTER(opt, "-b");
449 int res = firm_be_option(opt);
451 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
453 argument_errors = true;
454 } else if (res == -1) {
455 help_displayed = true;
457 } else if(arg[0] == '-' && arg[1] == 'W') {
458 set_warning_opt(&arg[2]);
459 } else if(arg[0] == '-' && arg[1] == 'm') {
461 GET_ARG_AFTER(opt, "-m");
463 long int value = strtol(opt, &endptr, 10);
464 if (*endptr != '\0') {
465 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
466 argument_errors = true;
468 if (value != 16 && value != 32 && value != 64) {
469 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
470 argument_errors = true;
472 machine_size = (unsigned int)value;
474 } else if(arg[0] == '-') {
475 if (arg[1] == '\0') {
477 fprintf(stderr, "error: multiple input files specified\n");
478 argument_errors = true;
482 } else if(strcmp(arg, "-pedantic") == 0) {
483 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
484 } else if(arg[1] == 'O' ||
486 strncmp(arg + 1, "std=", 4) == 0) {
487 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
489 fprintf(stderr, "error: unknown argument '%s'\n", arg);
490 argument_errors = true;
494 fprintf(stderr, "error: multiple input files specified\n");
495 argument_errors = true;
502 /* we do the lowering in ast2firm */
503 firm_opt.lower_bitfields = FALSE;
506 return !argument_errors;
508 if(argument_errors) {
525 char outnamebuf[4096];
526 if(outname == NULL) {
537 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
538 outname = outnamebuf;
540 case CompileAssemble:
541 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
542 outname = outnamebuf;
545 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
547 outname = outnamebuf;
549 case CompileAssembleLink:
555 if(outname != NULL) {
556 if(strcmp(outname, "-") == 0) {
559 out = fopen(outname, "w");
561 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
570 fprintf(stderr, "%s: no input files\n", argv[0]);
572 } else if(strcmp(input, "-") == 0) {
576 in = fopen(input, "r");
578 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
583 if(mode == LexTest) {
589 FILE *preprocessed_in = preprocess(in, input);
590 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
591 int result = pclose(preprocessed_in);
596 /* parsing failed because of errors */
597 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
600 if (warning_count > 0) {
601 fprintf(stderr, "%u warning(s)\n", warning_count);
604 if(mode == PrintAst) {
605 type_set_output(out);
610 if(mode == PrintFluffy) {
611 type_set_output(out);
613 write_fluffy_decls(out, unit);
616 translation_unit_to_firm(unit);
618 if(mode == ParseOnly) {
623 char asm_tempfile[1024];
624 if(mode == CompileDump) {
626 firm_be_opt.selection = BE_NONE;
627 } else if(mode == Compile) {
631 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
633 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
635 if(mode == CompileDump) {
637 ident *id = new_id_from_str(dumpfunction);
638 ir_graph *irg = NULL;
639 int n_irgs = get_irp_n_irgs();
640 for(int i = 0; i < n_irgs; ++i) {
641 ir_graph *tirg = get_irp_irg(i);
642 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
650 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
654 dump_ir_block_graph_file(irg, out);
661 /* assemble assembler and create object file */
662 char obj_tfile[1024];
663 if(mode == CompileAssemble || mode == CompileAssembleLink) {
664 const char *obj_outfile;
665 if(mode == CompileAssemble) {
667 obj_outfile = outname;
670 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
672 obj_outfile = obj_tfile;
675 assemble(obj_outfile, asm_tempfile);
678 /* link object file */
679 if(mode == CompileAssembleLink) {
680 do_link(outname, obj_tfile);
683 obstack_free(&cppflags_obst, NULL);