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 "lang_features.h"
54 #include "driver/firm_opt.h"
55 #include "driver/firm_cmdline.h"
56 #include "adt/error.h"
57 #include "write_fluffy.h"
61 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
65 #define LINKER "gcc -m32"
69 #define ASSEMBLER "as --32"
72 /** The current c mode/dialect. */
73 unsigned int c_mode = _C89|_C99|_GNUC;
75 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
76 unsigned int machine_size = 32;
78 /** true if the char type is signed. */
79 bool char_is_signed = true;
82 static struct obstack cppflags_obst;
84 #if defined(_DEBUG) || defined(FIRM_DEBUG)
86 * Debug printf implementation.
88 * @param fmt printf style format parameter
90 void dbg_printf(const char *fmt, ...)
94 if (firm_dump.debug_print) {
100 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
102 static void initialize_firm(void)
106 dump_consts_local(1);
107 dump_keepalive_edges(1);
110 static void get_output_name(char *buf, size_t buflen, const char *inputname,
113 size_t last_dot = 0xffffffff;
116 if(inputname == NULL) {
117 snprintf(buf, buflen, "a%s", newext);
121 for(const char *c = inputname; *c != 0; ++c) {
126 if(last_dot == 0xffffffff)
129 if(last_dot >= buflen)
130 panic("filename too long");
131 memcpy(buf, inputname, last_dot);
133 size_t extlen = strlen(newext) + 1;
134 if(extlen + last_dot >= buflen)
135 panic("filename too long");
136 memcpy(buf+last_dot, newext, extlen);
139 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
141 lexer_open_stream(in, input);
142 translation_unit_t *unit = parse();
146 static void lextest(FILE *in, const char *fname)
148 lexer_open_stream(in, fname);
151 lexer_next_preprocessing_token();
152 print_token(stdout, &lexer_token);
154 } while(lexer_token.type != T_EOF);
157 static FILE* preprocess(FILE* in, const char *fname)
160 const char *flags = obstack_finish(&cppflags_obst);
163 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
165 /* read from stdin */
166 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
172 FILE* f = popen(buf, "r");
174 fprintf(stderr, "invoking preprocessor failed\n");
180 static void do_link(const char *out, const char *in)
184 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
188 int err = system(buf);
190 fprintf(stderr, "linker reported an error\n");
195 static void assemble(const char *out, const char *in)
199 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
204 int err = system(buf);
206 fprintf(stderr, "assembler reported an error\n");
211 static const char *try_dir(const char *dir)
215 if(access(dir, R_OK | W_OK | X_OK) == 0)
220 static const char *get_tempdir(void)
222 static const char *tmpdir = NULL;
228 tmpdir = try_dir(getenv("TMPDIR"));
230 tmpdir = try_dir(getenv("TMP"));
232 tmpdir = try_dir(getenv("TEMP"));
236 tmpdir = try_dir(P_tmpdir);
240 tmpdir = try_dir("/var/tmp");
242 tmpdir = try_dir("/usr/tmp");
244 tmpdir = try_dir("/tmp");
253 /* cheap and nasty mkstemp replacement */
254 static int mkstemp(char *templ)
257 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
262 * an own version of tmpnam, which: writes in a buffer, appends a user specified
263 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
265 static FILE *make_temp_file(char *buffer, size_t buflen,
266 const char *prefix, const char *suffix)
268 const char *tempdir = get_tempdir();
270 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
274 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
276 int fd = mkstemp(buffer);
278 fprintf(stderr, "couldn't create temporary file: %s\n",
282 FILE *out = fdopen(fd, "w");
284 fprintf(stderr, "couldn't create temporary file FILE*\n");
292 * Do the necessary lowering for compound parameters.
294 void lower_compound_params(void)
296 lower_params_t params;
298 params.def_ptr_alignment = 4;
299 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
300 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
301 params.find_pointer_type = NULL;
302 params.ret_compound_in_regs = NULL;
303 lower_calls_with_compounds(¶ms);
306 typedef enum compile_mode_t {
317 static void usage(const char *argv0)
319 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
322 int main(int argc, char **argv)
326 const char *input = NULL;
327 const char *outname = NULL;
328 const char *dumpfunction = NULL;
329 compile_mode_t mode = CompileAssembleLink;
331 obstack_init(&cppflags_obst);
333 #define GET_ARG_AFTER(def, args) \
334 def = &arg[sizeof(args)-1]; \
335 if(def[0] == '\0') { \
338 fprintf(stderr, "error: expected argument after '" args "'\n"); \
339 argument_errors = true; \
343 if(def[0] == '-' && def[1] != '\0') { \
344 fprintf(stderr, "error: expected argument after '" args "'\n"); \
345 argument_errors = true; \
350 bool help_displayed = false;
351 bool argument_errors = false;
352 for(int i = 1; i < argc; ++i) {
353 const char *arg = argv[i];
354 if(strncmp(arg, "-o", 2) == 0) {
355 GET_ARG_AFTER(outname, "-o");
356 } else if(strcmp(arg, "-c") == 0) {
357 mode = CompileAssemble;
358 } else if(strcmp(arg, "-S") == 0) {
360 } else if(strcmp(arg, "--gcc") == 0) {
362 } else if(strcmp(arg, "--no-gcc") == 0) {
364 } else if(strcmp(arg, "--ms") == 0) {
366 } else if(strcmp(arg, "--signed-chars") == 0) {
367 char_is_signed = true;
368 } else if(strcmp(arg, "--unsigned-chars") == 0) {
369 char_is_signed = false;
370 } else if(strcmp(arg, "--no-ms") == 0) {
372 } else if(strcmp(arg, "--lextest") == 0) {
374 } else if(strcmp(arg, "--print-ast") == 0) {
376 } else if(strcmp(arg, "--print-fluffy") == 0) {
378 } else if(strcmp(arg, "--version") == 0) {
380 firm_get_version(&ver);
381 printf("cparser (%d.%d %s) using libFirm (%d.%d", 0, 1, cparser_REVISION, ver.major, ver.minor);
382 if(ver.revision[0] != 0) {
384 fputs(ver.revision, stdout);
386 if(ver.build[0] != 0) {
388 fputs(ver.build, stdout);
392 } else if(strcmp(arg, "-fsyntax-only") == 0) {
394 } else if(strncmp(arg, "-I", 2) == 0) {
396 GET_ARG_AFTER(opt, "-I");
397 obstack_printf(&cppflags_obst, " -I%s", opt);
398 } else if(strncmp(arg, "-D", 2) == 0) {
400 GET_ARG_AFTER(opt, "-D");
401 obstack_printf(&cppflags_obst, " -D%s", opt);
402 } else if(strcmp(arg, "--dump-function") == 0) {
405 fprintf(stderr, "error: "
406 "expected argument after '--dump-function'\n");
407 argument_errors = true;
410 dumpfunction = argv[i];
412 } else if(strcmp(arg, "-v") == 0) {
414 } else if(arg[0] == '-' && arg[1] == 'f') {
416 GET_ARG_AFTER(opt, "-f");
418 if(strcmp(opt, "omit-frame-pointer") == 0) {
419 firm_be_option("omitfp");
420 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
421 firm_be_option("omitfp=no");
423 int res = firm_option(opt);
425 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
427 argument_errors = true;
429 } else if (res == -1) {
430 help_displayed = true;
433 } else if(arg[0] == '-' && arg[1] == 'b') {
435 GET_ARG_AFTER(opt, "-b");
436 int res = firm_be_option(opt);
438 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
440 argument_errors = true;
441 } else if (res == -1) {
442 help_displayed = true;
444 } else if(arg[0] == '-' && arg[1] == 'm') {
446 GET_ARG_AFTER(opt, "-m");
448 long int value = strtol(opt, &endptr, 10);
449 if (*endptr != '\0') {
450 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
451 argument_errors = true;
453 if (value != 16 && value != 32 && value != 64) {
454 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
455 argument_errors = true;
457 machine_size = (unsigned int)value;
459 } else if(arg[0] == '-') {
460 if (arg[1] == '\0') {
462 fprintf(stderr, "error: multiple input files specified\n");
463 argument_errors = true;
467 } else if(strcmp(arg, "-pedantic") == 0) {
468 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
469 } else if(arg[1] == 'O' ||
472 strncmp(arg + 1, "std=", 4) == 0) {
473 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
475 fprintf(stderr, "error: unknown argument '%s'\n", arg);
476 argument_errors = true;
480 fprintf(stderr, "error: multiple input files specified\n");
481 argument_errors = true;
489 return !argument_errors;
491 if(argument_errors) {
508 char outnamebuf[4096];
509 if(outname == NULL) {
520 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
521 outname = outnamebuf;
523 case CompileAssemble:
524 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
525 outname = outnamebuf;
528 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
530 outname = outnamebuf;
532 case CompileAssembleLink:
538 if(outname != NULL) {
539 if(strcmp(outname, "-") == 0) {
542 out = fopen(outname, "w");
544 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
553 fprintf(stderr, "%s: no input files\n", argv[0]);
555 } else if(strcmp(input, "-") == 0) {
559 in = fopen(input, "r");
561 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
566 if(mode == LexTest) {
572 FILE *preprocessed_in = preprocess(in, input);
573 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
574 pclose(preprocessed_in);
578 if(mode == PrintAst) {
579 type_set_output(out);
584 if(mode == PrintFluffy) {
585 type_set_output(out);
587 write_fluffy_decls(out, unit);
590 translation_unit_to_firm(unit);
592 if(mode == ParseOnly) {
597 char asm_tempfile[1024];
598 if(mode == CompileDump) {
600 firm_be_opt.selection = BE_NONE;
601 } else if(mode == Compile) {
605 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
607 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
609 if(mode == CompileDump) {
611 ident *id = new_id_from_str(dumpfunction);
612 ir_graph *irg = NULL;
613 int n_irgs = get_irp_n_irgs();
614 for(int i = 0; i < n_irgs; ++i) {
615 ir_graph *tirg = get_irp_irg(i);
616 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
624 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
628 dump_ir_block_graph_file(irg, out);
635 /* assemble assembler and create object file */
636 char obj_tfile[1024];
637 if(mode == CompileAssemble || mode == CompileAssembleLink) {
638 const char *obj_outfile;
639 if(mode == CompileAssemble) {
641 obj_outfile = outname;
644 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
646 obj_outfile = obj_tfile;
649 assemble(obj_outfile, asm_tempfile);
652 /* link object file */
653 if(mode == CompileAssembleLink) {
654 do_link(outname, obj_tfile);
657 obstack_free(&cppflags_obst, NULL);