17 #include <libfirm/firm.h>
18 #include <libfirm/be.h>
22 #include "type_hash.h"
25 #include "adt/error.h"
26 #include "write_fluffy.h"
29 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
37 #define ASSEMBLER "as"
41 /* remap some names */
42 #define popen(cmd, mode) _popen(cmd, mode)
43 #define pclose(file) _pclose(file)
49 static const ir_settings_if_conv_t *if_conv_info = NULL;
50 static const backend_params *be_params = NULL;
52 static void initialize_firm(void)
55 firm_init_options(NULL, 0, NULL);
57 firm_parameter_t params;
58 memset(¶ms, 0, sizeof(params));
60 params.size = sizeof(params);
61 params.enable_statistics = 0;
62 params.initialize_local_func = uninitialized_local_var;
64 params.builtin_dbg = NULL;
66 /* initialize backend */
67 be_params = be_init();
68 be_set_debug_retrieve(retrieve_dbg);
69 params.arch_op_settings = be_params->arch_op_settings;
70 if_conv_info = be_params->if_conv_info;
72 (void) if_conv_info; /* avoid unused warning */
74 /* intialize firm itself */
76 dbg_init(NULL, NULL, dbg_snprint);
78 set_opt_constant_folding(1);
79 set_opt_unreachable_code(1);
80 set_opt_control_flow_straightening(1);
81 set_opt_control_flow_weak_simplification(1);
82 set_opt_control_flow_strong_simplification(1);
83 set_opt_dyn_meth_dispatch(1);
85 set_opt_precise_exc_context(0);
86 set_opt_strength_red(0);
87 set_opt_fragile_ops(0);
88 set_opt_optimize_class_casts(0);
89 set_opt_suppress_downcast_optimization(0);
90 set_opt_remove_confirm(1);
91 set_opt_scalar_replacement(1);
92 set_opt_ldst_only_null_ptr_exceptions(1);
93 set_opt_alias_analysis(1);
96 dump_keepalive_edges(1);
99 static void dump(ir_graph *irg, const char *suffix)
102 dump_ir_block_graph(irg, suffix);
106 static void get_output_name(char *buf, size_t buflen, const char *inputname,
109 size_t last_dot = 0xffffffff;
112 if(inputname == NULL) {
113 snprintf(buf, buflen, "a%s", newext);
117 for(const char *c = inputname; *c != 0; ++c) {
122 if(last_dot == 0xffffffff)
125 if(last_dot >= buflen)
126 panic("filename too long");
127 memcpy(buf, inputname, last_dot);
129 size_t extlen = strlen(newext) + 1;
130 if(extlen + last_dot >= buflen)
131 panic("filename too long");
132 memcpy(buf+last_dot, newext, extlen);
135 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
137 lexer_open_stream(in, input);
138 translation_unit_t *unit = parse();
142 static void lextest(FILE *in, const char *fname)
144 lexer_open_stream(in, fname);
147 lexer_next_preprocessing_token();
148 print_token(stdout, &lexer_token);
150 } while(lexer_token.type != T_EOF);
153 static void emit(FILE *out, const char *input_name)
155 be_main(out, input_name);
158 static FILE* preprocess(FILE* in, const char *fname)
163 snprintf(buf, sizeof(buf), PREPROCESSOR " %s", fname);
165 /* read from stdin */
166 snprintf(buf, sizeof(buf), PREPROCESSOR " -");
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 * an own version of tmpnam, which: writes in a buffer, appends a user specified
254 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
256 static FILE *make_temp_file(char *buffer, size_t buflen,
257 const char *prefix, const char *suffix)
259 const char *tempdir = get_tempdir();
261 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
265 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
267 int fd = mkstemp(buffer);
269 fprintf(stderr, "couldn't create temporary file: %s\n",
273 FILE *out = fdopen(fd, "w");
275 fprintf(stderr, "couldn't create temporary file FILE*\n");
282 static void create_firm_prog(translation_unit_t *unit)
284 translation_unit_to_firm(unit);
286 //dump_globals_as_text(dump_verbosity_max, "-globals");
288 int n_irgs = get_irp_n_irgs();
289 for(int i = 0; i < n_irgs; ++i) {
290 ir_graph *const irg = get_irp_irg(i);
294 lower_params_t params;
296 params.def_ptr_alignment = 4;
297 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
298 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
299 params.find_pointer_type = NULL;
300 params.ret_compound_in_regs = NULL;
301 lower_calls_with_compounds(¶ms);
305 for(int i = 0; i < n_irgs; ++i) {
306 ir_graph *const irg = get_irp_irg(i);
311 static void optimize(void)
314 ir_entity **keep_methods;
316 cgana(&arr_len, &keep_methods);
317 gc_irgs(arr_len, keep_methods);
320 optimize_funccalls(0);
322 lwrdw_param_t lwrdw_param = {
327 def_create_intrinsic_fkt,
330 if (be_params->arch_create_intrinsic_fkt) {
331 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
332 lwrdw_param.ctx = be_params->create_intrinsic_ctx;
335 for(int i = 0; i < get_irp_n_irgs(); ++i) {
336 ir_graph *irg = get_irp_irg(i);
338 optimize_graph_df(irg);
339 dump(irg, "-01-localopt");
341 dump(irg, "-02-place");
344 lower_dw_ops(&lwrdw_param);
346 optimize_graph_df(irg);
347 dump(irg, "-05-localopt");
353 typedef enum compile_mode_t {
363 static void usage(const char *argv0)
365 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
368 int main(int argc, char **argv)
381 const char *input = NULL;
382 const char *outname = NULL;
383 const char *dumpfunction = NULL;
384 compile_mode_t mode = CompileAssembleLink;
386 for(int i = 1; i < argc; ++i) {
387 const char *arg = argv[i];
388 if(strcmp(arg, "-o") == 0) {
395 } else if(strcmp(arg, "-c") == 0) {
396 mode = CompileAssemble;
397 } else if(strcmp(arg, "-S") == 0) {
399 } else if(strcmp(arg, "--lextest") == 0) {
401 } else if(strcmp(arg, "--print-ast") == 0) {
403 } else if(strcmp(arg, "--print-fluffy") == 0) {
405 } else if(strcmp(arg, "--dump") == 0) {
407 } else if(strcmp(arg, "--dump-function") == 0) {
413 dumpfunction = argv[i];
415 } else if(strcmp(arg, "-v") == 0) {
417 } else if(arg[0] == '-' && arg[1] == 'f') {
418 const char *opt = &arg[2];
432 } else if(arg[0] == '-' && arg[1] == 'b') {
433 const char *opt = &arg[2];
447 } else if(arg[0] == '-') {
448 if (arg[1] == '\0') {
450 } else if (arg[1] == 'D' ||
455 strncmp(arg + 1, "std=", 4) == 0) {
456 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
463 fprintf(stderr, "Error: multiple input files specified\n");
473 char outnamebuf[4096];
474 if(outname == NULL) {
481 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
482 outname = outnamebuf;
484 case CompileAssemble:
485 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
486 outname = outnamebuf;
489 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
491 outname = outnamebuf;
493 case CompileAssembleLink:
497 assert(outname != NULL);
500 if(strcmp(outname, "-") == 0) {
503 out = fopen(outname, "w");
505 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
513 fprintf(stderr, "%s: no input files\n", argv[0]);
515 } else if(strcmp(input, "-") == 0) {
519 in = fopen(input, "r");
521 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
526 if(mode == LexTest) {
532 FILE *preprocessed_in = preprocess(in, input);
533 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
534 pclose(preprocessed_in);
538 if(mode == PrintAst) {
543 if(mode == PrintFluffy) {
544 write_fluffy_decls(out, unit);
547 create_firm_prog(unit);
550 if(mode == CompileDump) {
552 ident *id = new_id_from_str(dumpfunction);
553 ir_graph *irg = NULL;
554 int n_irgs = get_irp_n_irgs();
555 for(int i = 0; i < n_irgs; ++i) {
556 ir_graph *tirg = get_irp_irg(i);
557 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
565 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
569 dump_ir_block_graph_file(irg, out);
574 /* generate code and emit assembler */
576 char asm_tempfile[1024];
577 if(mode == Compile) {
581 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
583 emit(asm_out, input);
586 /* assemble assembler and create object file */
587 char obj_tfile[1024];
588 if(mode == CompileAssemble || mode == CompileAssembleLink) {
589 const char *obj_outfile;
590 if(mode == CompileAssemble) {
592 obj_outfile = outname;
595 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
597 obj_outfile = obj_tfile;
600 assemble(obj_outfile, asm_tempfile);
603 /* link object file */
604 if(mode == CompileAssembleLink) {
605 do_link(outname, obj_tfile);