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>
49 #include "type_hash.h"
52 #include "lang_features.h"
53 #include "driver/firm_opt.h"
54 #include "driver/firm_cmdline.h"
55 #include "adt/error.h"
56 #include "write_fluffy.h"
60 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
64 #define LINKER "gcc -m32"
68 #define ASSEMBLER "as --32"
71 /** The current c mode/dialect. */
72 unsigned int c_mode = _C99|_GNUC;
74 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
75 unsigned int machine_size = 32;
77 /** true if the char type is signed. */
78 bool char_is_signed = true;
81 static struct obstack cppflags_obst;
83 #if defined(_DEBUG) || defined(FIRM_DEBUG)
85 * Debug printf implementation.
87 * @param fmt printf style format parameter
89 void dbg_printf(const char *fmt, ...)
93 if (firm_dump.debug_print) {
99 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
101 static void initialize_firm(void)
105 dump_consts_local(1);
106 dump_keepalive_edges(1);
109 static void get_output_name(char *buf, size_t buflen, const char *inputname,
112 size_t last_dot = 0xffffffff;
115 if(inputname == NULL) {
116 snprintf(buf, buflen, "a%s", newext);
120 for(const char *c = inputname; *c != 0; ++c) {
125 if(last_dot == 0xffffffff)
128 if(last_dot >= buflen)
129 panic("filename too long");
130 memcpy(buf, inputname, last_dot);
132 size_t extlen = strlen(newext) + 1;
133 if(extlen + last_dot >= buflen)
134 panic("filename too long");
135 memcpy(buf+last_dot, newext, extlen);
138 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
140 lexer_open_stream(in, input);
141 translation_unit_t *unit = parse();
145 static void lextest(FILE *in, const char *fname)
147 lexer_open_stream(in, fname);
150 lexer_next_preprocessing_token();
151 print_token(stdout, &lexer_token);
153 } while(lexer_token.type != T_EOF);
156 static FILE* preprocess(FILE* in, const char *fname)
159 const char *flags = obstack_finish(&cppflags_obst);
162 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
164 /* read from stdin */
165 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
171 FILE* f = popen(buf, "r");
173 fprintf(stderr, "invoking preprocessor failed\n");
179 static void do_link(const char *out, const char *in)
183 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
187 int err = system(buf);
189 fprintf(stderr, "linker reported an error\n");
194 static void assemble(const char *out, const char *in)
198 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
203 int err = system(buf);
205 fprintf(stderr, "assembler reported an error\n");
210 static const char *try_dir(const char *dir)
214 if(access(dir, R_OK | W_OK | X_OK) == 0)
219 static const char *get_tempdir(void)
221 static const char *tmpdir = NULL;
227 tmpdir = try_dir(getenv("TMPDIR"));
229 tmpdir = try_dir(getenv("TMP"));
231 tmpdir = try_dir(getenv("TEMP"));
235 tmpdir = try_dir(P_tmpdir);
239 tmpdir = try_dir("/var/tmp");
241 tmpdir = try_dir("/usr/tmp");
243 tmpdir = try_dir("/tmp");
252 /* cheap and nasty mkstemp replacement */
253 static int mkstemp(char *templ)
256 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
261 * an own version of tmpnam, which: writes in a buffer, appends a user specified
262 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
264 static FILE *make_temp_file(char *buffer, size_t buflen,
265 const char *prefix, const char *suffix)
267 const char *tempdir = get_tempdir();
269 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
273 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
275 int fd = mkstemp(buffer);
277 fprintf(stderr, "couldn't create temporary file: %s\n",
281 FILE *out = fdopen(fd, "w");
283 fprintf(stderr, "couldn't create temporary file FILE*\n");
291 * Do the necessary lowering for compound parameters.
293 void lower_compound_params(void)
295 lower_params_t params;
297 params.def_ptr_alignment = 4;
298 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
299 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
300 params.find_pointer_type = NULL;
301 params.ret_compound_in_regs = NULL;
302 lower_calls_with_compounds(¶ms);
305 typedef enum compile_mode_t {
316 static void usage(const char *argv0)
318 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
321 int main(int argc, char **argv)
325 const char *input = NULL;
326 const char *outname = NULL;
327 const char *dumpfunction = NULL;
328 compile_mode_t mode = CompileAssembleLink;
330 obstack_init(&cppflags_obst);
332 #define GET_ARG_AFTER(def, args) \
333 def = &arg[sizeof(args)-1]; \
334 if(def[0] == '\0') { \
337 fprintf(stderr, "error: expected argument after '" args "'\n"); \
338 argument_errors = true; \
342 if(def[0] == '-' && def[1] != '\0') { \
343 fprintf(stderr, "error: expected argument after '" args "'\n"); \
344 argument_errors = true; \
349 bool help_displayed = false;
350 bool argument_errors = false;
351 for(int i = 1; i < argc; ++i) {
352 const char *arg = argv[i];
353 if(strncmp(arg, "-o", 2) == 0) {
354 GET_ARG_AFTER(outname, "-o");
355 } else if(strcmp(arg, "-c") == 0) {
356 mode = CompileAssemble;
357 } else if(strcmp(arg, "-S") == 0) {
359 } else if(strcmp(arg, "--gcc") == 0) {
361 } else if(strcmp(arg, "--no-gcc") == 0) {
363 } else if(strcmp(arg, "--ms") == 0) {
365 } else if(strcmp(arg, "--signed-chars") == 0) {
366 char_is_signed = true;
367 } else if(strcmp(arg, "--unsigned-chars") == 0) {
368 char_is_signed = false;
369 } else if(strcmp(arg, "--no-ms") == 0) {
371 } else if(strcmp(arg, "--lextest") == 0) {
373 } else if(strcmp(arg, "--print-ast") == 0) {
375 } else if(strcmp(arg, "--print-fluffy") == 0) {
377 } else if(strcmp(arg, "--version") == 0) {
379 firm_get_version(&ver);
380 printf("cparser (%d.%d %s) using libFirm (%d.%d", 0, 1, cparser_REVISION, ver.major, ver.minor);
381 if(ver.revision[0] != 0) {
383 fputs(ver.revision, stdout);
385 if(ver.build[0] != 0) {
387 fputs(ver.build, stdout);
391 } else if(strcmp(arg, "-fsyntax-only") == 0) {
393 } else if(strncmp(arg, "-I", 2) == 0) {
395 GET_ARG_AFTER(opt, "-I");
396 obstack_printf(&cppflags_obst, " -I%s", opt);
397 } else if(strncmp(arg, "-D", 2) == 0) {
399 GET_ARG_AFTER(opt, "-D");
400 obstack_printf(&cppflags_obst, " -D%s", opt);
401 } else if(strcmp(arg, "--dump-function") == 0) {
404 fprintf(stderr, "error: "
405 "expected argument after '--dump-function'\n");
406 argument_errors = true;
409 dumpfunction = argv[i];
411 } else if(strcmp(arg, "-v") == 0) {
413 } else if(arg[0] == '-' && arg[1] == 'f') {
415 GET_ARG_AFTER(opt, "-f");
417 if(strcmp(opt, "omit-frame-pointer") == 0) {
418 firm_be_option("omitfp");
419 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
420 firm_be_option("omitfp=no");
422 int res = firm_option(opt);
424 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
426 argument_errors = true;
428 } else if (res == -1) {
429 help_displayed = true;
432 } else if(arg[0] == '-' && arg[1] == 'b') {
434 GET_ARG_AFTER(opt, "-b");
435 int res = firm_be_option(opt);
437 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
439 argument_errors = true;
440 } else if (res == -1) {
441 help_displayed = true;
443 } else if(arg[0] == '-' && arg[1] == 'm') {
445 GET_ARG_AFTER(opt, "-m");
447 long int value = strtol(opt, &endptr, 10);
448 if (*endptr != '\0') {
449 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
450 argument_errors = true;
452 if (value != 16 && value != 32 && value != 64) {
453 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
454 argument_errors = true;
456 machine_size = (unsigned int)value;
458 } else if(arg[0] == '-') {
459 if (arg[1] == '\0') {
461 fprintf(stderr, "error: multiple input files specified\n");
462 argument_errors = true;
466 } else if(strcmp(arg, "-pedantic") == 0) {
467 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
468 } else if(arg[1] == 'O' ||
471 strncmp(arg + 1, "std=", 4) == 0) {
472 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
474 fprintf(stderr, "error: unknown argument '%s'\n", arg);
475 argument_errors = true;
479 fprintf(stderr, "error: multiple input files specified\n");
480 argument_errors = true;
488 return !argument_errors;
490 if(argument_errors) {
506 char outnamebuf[4096];
507 if(outname == NULL) {
518 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
519 outname = outnamebuf;
521 case CompileAssemble:
522 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
523 outname = outnamebuf;
526 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
528 outname = outnamebuf;
530 case CompileAssembleLink:
536 if(outname != NULL) {
537 if(strcmp(outname, "-") == 0) {
540 out = fopen(outname, "w");
542 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
551 fprintf(stderr, "%s: no input files\n", argv[0]);
553 } else if(strcmp(input, "-") == 0) {
557 in = fopen(input, "r");
559 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
564 if(mode == LexTest) {
570 FILE *preprocessed_in = preprocess(in, input);
571 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
572 pclose(preprocessed_in);
576 if(mode == PrintAst) {
577 type_set_output(out);
582 if(mode == PrintFluffy) {
583 type_set_output(out);
585 write_fluffy_decls(out, unit);
588 translation_unit_to_firm(unit);
590 if(mode == ParseOnly) {
595 char asm_tempfile[1024];
596 if(mode == CompileDump) {
598 firm_be_opt.selection = BE_NONE;
599 } else if(mode == Compile) {
603 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
605 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
607 if(mode == CompileDump) {
609 ident *id = new_id_from_str(dumpfunction);
610 ir_graph *irg = NULL;
611 int n_irgs = get_irp_n_irgs();
612 for(int i = 0; i < n_irgs; ++i) {
613 ir_graph *tirg = get_irp_irg(i);
614 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
622 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
626 dump_ir_block_graph_file(irg, out);
633 /* assemble assembler and create object file */
634 char obj_tfile[1024];
635 if(mode == CompileAssemble || mode == CompileAssembleLink) {
636 const char *obj_outfile;
637 if(mode == CompileAssemble) {
639 obj_outfile = outname;
642 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
644 obj_outfile = obj_tfile;
647 assemble(obj_outfile, asm_tempfile);
650 /* link object file */
651 if(mode == CompileAssembleLink) {
652 do_link(outname, obj_tfile);
655 obstack_free(&cppflags_obst, NULL);