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 {
330 static void usage(const char *argv0)
332 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
335 int main(int argc, char **argv)
339 const char *input = NULL;
340 const char *outname = NULL;
341 const char *dumpfunction = NULL;
342 compile_mode_t mode = CompileAssembleLink;
344 obstack_init(&cppflags_obst);
345 obstack_init(&ldflags_obst);
347 #define GET_ARG_AFTER(def, args) \
348 def = &arg[sizeof(args)-1]; \
349 if(def[0] == '\0') { \
352 fprintf(stderr, "error: expected argument after '" args "'\n"); \
353 argument_errors = true; \
357 if(def[0] == '-' && def[1] != '\0') { \
358 fprintf(stderr, "error: expected argument after '" args "'\n"); \
359 argument_errors = true; \
364 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
366 bool help_displayed = false;
367 bool argument_errors = false;
368 for(int i = 1; i < argc; ++i) {
369 const char *arg = argv[i];
372 const char *option = &arg[1];
373 if(option[0] == 'o') {
374 GET_ARG_AFTER(outname, "-o");
375 } else if(SINGLE_OPTION('c')) {
376 mode = CompileAssemble;
377 } else if(SINGLE_OPTION('S')) {
379 } else if(option[0] == 'I') {
381 GET_ARG_AFTER(opt, "-I");
382 obstack_printf(&cppflags_obst, " -I%s", opt);
383 } else if(option[0] == 'D') {
385 GET_ARG_AFTER(opt, "-D");
386 obstack_printf(&cppflags_obst, " -D%s", opt);
387 } else if(option[0] == 'U') {
389 GET_ARG_AFTER(opt, "-U");
390 obstack_printf(&cppflags_obst, " -U%s", opt);
391 } else if(option[0] == 'l') {
393 GET_ARG_AFTER(opt, "-l");
394 obstack_printf(&ldflags_obst, " -l%s", opt);
395 } else if(option[0] == 'L') {
397 GET_ARG_AFTER(opt, "-L");
398 obstack_printf(&ldflags_obst, " -L%s", opt);
399 } else if(SINGLE_OPTION('v')) {
401 } else if(SINGLE_OPTION('w')) {
402 inhibit_all_warnings = true;
403 } else if(option[0] == 'f') {
405 GET_ARG_AFTER(opt, "-f");
407 if(strcmp(opt, "syntax-only") == 0) {
409 } else if(strcmp(opt, "omit-frame-pointer") == 0) {
410 firm_be_option("omitfp");
411 } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
412 firm_be_option("omitfp=no");
414 int res = firm_option(opt);
416 fprintf(stderr, "error: unknown Firm option '-f %s'\n",
418 argument_errors = true;
420 } else if (res == -1) {
421 help_displayed = true;
424 } else if(option[0] == 'b') {
426 GET_ARG_AFTER(opt, "-b");
427 int res = firm_be_option(opt);
429 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
431 argument_errors = true;
432 } else if (res == -1) {
433 help_displayed = true;
435 } else if(option[0] == 'W') {
436 set_warning_opt(&option[1]);
437 } else if(option[0] == 'm') {
439 GET_ARG_AFTER(opt, "-m");
441 long int value = strtol(opt, &endptr, 10);
442 if (*endptr != '\0') {
443 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
444 argument_errors = true;
446 if (value != 16 && value != 32 && value != 64) {
447 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
448 argument_errors = true;
450 machine_size = (unsigned int)value;
452 } else if (option[0] == '\0') {
454 fprintf(stderr, "error: multiple input files specified\n");
455 argument_errors = true;
459 } else if(strcmp(option, "pedantic") == 0) {
460 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
461 } else if(option[0] == 'O' ||
463 strncmp(option, "std=", 4) == 0) {
464 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
465 } else if (option[0] == '-') {
466 /* double dash option */
468 if(strcmp(option, "gcc") == 0) {
470 } else if(strcmp(option, "no-gcc") == 0) {
472 } else if(strcmp(option, "ms") == 0) {
474 } else if(strcmp(option, "no-ms") == 0) {
476 } else if(strcmp(option, "signed-chars") == 0) {
477 char_is_signed = true;
478 } else if(strcmp(option, "unsigned-chars") == 0) {
479 char_is_signed = false;
480 } else if(strcmp(option, "strict") == 0) {
482 } else if(strcmp(option, "lextest") == 0) {
484 } else if(strcmp(option, "benchmark") == 0) {
485 mode = BenchmarkParser;
486 } else if(strcmp(option, "print-ast") == 0) {
488 } else if(strcmp(option, "print-implicit-cast") == 0) {
489 print_implicit_casts = true;
490 } else if(strcmp(option, "print-parenthesis") == 0) {
491 print_parenthesis = true;
492 } else if(strcmp(option, "print-fluffy") == 0) {
494 } else if(strcmp(option, "version") == 0) {
496 firm_get_version(&ver);
497 printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor);
498 if(ver.revision[0] != 0) {
500 fputs(ver.revision, stdout);
502 if(ver.build[0] != 0) {
504 fputs(ver.build, stdout);
508 } else if(strcmp(option, "dump-function") == 0) {
511 fprintf(stderr, "error: "
512 "expected argument after '--dump-function'\n");
513 argument_errors = true;
516 dumpfunction = argv[i];
519 fprintf(stderr, "error: unknown argument '%s'\n", arg);
520 argument_errors = true;
523 fprintf(stderr, "error: unknown argument '%s'\n", arg);
524 argument_errors = true;
528 fprintf(stderr, "error: multiple input files specified\n");
529 argument_errors = true;
536 /* we do the lowering in ast2firm */
537 firm_opt.lower_bitfields = FALSE;
540 return !argument_errors;
542 if(argument_errors) {
559 char outnamebuf[4096];
560 if(outname == NULL) {
562 case BenchmarkParser:
572 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
573 outname = outnamebuf;
575 case CompileAssemble:
576 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
577 outname = outnamebuf;
580 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
582 outname = outnamebuf;
584 case CompileAssembleLink:
590 if(outname != NULL) {
591 if(strcmp(outname, "-") == 0) {
594 out = fopen(outname, "w");
596 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
605 fprintf(stderr, "%s: no input files\n", argv[0]);
607 } else if(strcmp(input, "-") == 0) {
611 in = fopen(input, "r");
613 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
618 if(mode == LexTest) {
624 FILE *preprocessed_in = preprocess(in, input);
625 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
626 int result = pclose(preprocessed_in);
631 /* parsing failed because of errors */
632 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
635 if (warning_count > 0) {
636 fprintf(stderr, "%u warning(s)\n", warning_count);
639 if(mode == BenchmarkParser) {
643 if(mode == PrintAst) {
644 type_set_output(out);
649 if(mode == PrintFluffy) {
650 type_set_output(out);
652 write_fluffy_decls(out, unit);
655 translation_unit_to_firm(unit);
657 if(mode == ParseOnly) {
662 char asm_tempfile[1024];
663 if(mode == CompileDump) {
665 firm_be_opt.selection = BE_NONE;
666 } else if(mode == Compile) {
670 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
672 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
674 if(mode == CompileDump) {
676 ident *id = new_id_from_str(dumpfunction);
677 ir_graph *irg = NULL;
678 int n_irgs = get_irp_n_irgs();
679 for(int i = 0; i < n_irgs; ++i) {
680 ir_graph *tirg = get_irp_irg(i);
681 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
689 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
693 dump_ir_block_graph_file(irg, out);
700 /* assemble assembler and create object file */
701 char obj_tfile[1024];
702 if(mode == CompileAssemble || mode == CompileAssembleLink) {
703 const char *obj_outfile;
704 if(mode == CompileAssemble) {
706 obj_outfile = outname;
709 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
711 obj_outfile = obj_tfile;
714 assemble(obj_outfile, asm_tempfile);
717 /* link object file */
718 if(mode == CompileAssembleLink) {
719 do_link(outname, obj_tfile);
722 obstack_free(&cppflags_obst, NULL);
723 obstack_free(&ldflags_obst, NULL);