16 #include <libfirm/firm.h>
17 #include <libfirm/be.h>
21 #include "type_hash.h"
24 #include "adt/error.h"
25 #include "write_fluffy.h"
28 #define PREPROCESSOR "cpp"
36 /* remap some names */
37 #define popen(cmd, mode) _popen(cmd, mode)
38 #define pclose(file) _pclose(file)
44 static const ir_settings_if_conv_t *if_conv_info = NULL;
45 static const backend_params *be_params = NULL;
47 static void initialize_firm(void)
50 firm_init_options(NULL, 0, NULL);
52 firm_parameter_t params;
53 memset(¶ms, 0, sizeof(params));
55 params.size = sizeof(params);
56 params.enable_statistics = 0;
57 params.initialize_local_func = uninitialized_local_var;
59 params.builtin_dbg = NULL;
61 /* initialize backend */
62 be_params = be_init();
63 ir_set_debug_retrieve(retrieve_dbg);
64 params.arch_op_settings = be_params->arch_op_settings;
65 if_conv_info = be_params->if_conv_info;
67 (void) if_conv_info; /* avoid unused warning */
69 /* intialize firm itself */
71 dbg_init(NULL, NULL, dbg_snprint);
73 set_opt_constant_folding(1);
74 set_opt_unreachable_code(1);
75 set_opt_control_flow_straightening(1);
76 set_opt_control_flow_weak_simplification(1);
77 set_opt_control_flow_strong_simplification(1);
78 set_opt_dyn_meth_dispatch(1);
80 set_opt_precise_exc_context(0);
81 set_opt_strength_red(0);
82 set_opt_fragile_ops(0);
83 set_opt_optimize_class_casts(0);
84 set_opt_suppress_downcast_optimization(0);
85 set_opt_remove_confirm(1);
86 set_opt_scalar_replacement(1);
87 set_opt_ldst_only_null_ptr_exceptions(1);
88 set_opt_alias_analysis(1);
91 dump_keepalive_edges(1);
94 static void dump(ir_graph *irg, const char *suffix)
97 dump_ir_block_graph(irg, suffix);
101 static void get_output_name(char *buf, size_t buflen, const char *inputname,
104 size_t last_dot = 0xffffffff;
106 for(const char *c = inputname; *c != 0; ++c) {
111 if(last_dot == 0xffffffff)
114 if(last_dot >= buflen)
115 panic("filename too long");
116 memcpy(buf, inputname, last_dot);
118 size_t extlen = strlen(newext) + 1;
119 if(extlen + last_dot >= buflen)
120 panic("filename too long");
121 memcpy(buf+last_dot, newext, extlen);
124 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
126 lexer_open_stream(in, input);
127 translation_unit_t *unit = parse();
131 static void lextest(const char *fname)
133 FILE *in = fopen(fname, "r");
135 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
139 lexer_open_stream(in, fname);
142 lexer_next_preprocessing_token();
143 print_token(stdout, &lexer_token);
145 } while(lexer_token.type != T_EOF);
150 static void backend(const char *inputname, const char *outname)
152 FILE *out = fopen(outname, "w");
154 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
159 be_main(out, inputname);
164 static void emit(const char *input_name, const char *out_name)
166 backend(input_name, out_name);
169 static FILE* preprocess(const char *in)
173 snprintf(buf, sizeof(buf), PREPROCESSOR " %s", in);
178 FILE* f = popen(buf, "r");
180 fprintf(stderr, "invoking preprocessor failed\n");
186 static void link(const char *in, const char *out)
190 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
194 int err = system(buf);
196 fprintf(stderr, "linker reported an error\n");
201 static void assemble(const char *in, const char *out)
205 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
209 int err = system(buf);
211 fprintf(stderr, "assembler reported an error\n");
216 static void create_firm_prog(translation_unit_t *unit)
218 translation_unit_to_firm(unit);
220 //dump_globals_as_text(dump_verbosity_max, "-globals");
222 int n_irgs = get_irp_n_irgs();
223 for(int i = 0; i < n_irgs; ++i) {
224 ir_graph *const irg = get_irp_irg(i);
228 lower_params_t params;
230 params.def_ptr_alignment = 4;
231 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
232 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
233 params.find_pointer_type = NULL;
234 params.ret_compound_in_regs = NULL;
235 lower_calls_with_compounds(¶ms);
238 for(int i = 0; i < n_irgs; ++i) {
239 ir_graph *const irg = get_irp_irg(i);
244 static void optimize(void)
247 ir_entity **keep_methods;
249 cgana(&arr_len, &keep_methods);
250 gc_irgs(arr_len, keep_methods);
253 optimize_funccalls(0);
255 lwrdw_param_t lwrdw_param = {
260 def_create_intrinsic_fkt,
263 if (be_params->arch_create_intrinsic_fkt) {
264 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
265 lwrdw_param.ctx = be_params->create_intrinsic_ctx;
268 for(int i = 0; i < get_irp_n_irgs(); ++i) {
269 ir_graph *irg = get_irp_irg(i);
271 optimize_graph_df(irg);
272 dump(irg, "-01-localopt");
274 dump(irg, "-02-place");
277 lower_dw_ops(&lwrdw_param);
279 optimize_graph_df(irg);
280 dump(irg, "-05-localopt");
286 typedef enum compile_mode_t {
295 static void usage(const char *argv0)
297 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
300 int main(int argc, char **argv)
313 const char *input = NULL;
314 const char *outname = NULL;
315 compile_mode_t mode = CompileAssembleLink;
317 for(int i = 1; i < argc; ++i) {
318 const char *arg = argv[i];
319 if(strcmp(arg, "-o") == 0) {
326 } else if(strcmp(arg, "-c") == 0) {
327 mode = CompileAssemble;
328 } else if(strcmp(arg, "-S") == 0) {
330 } else if(strcmp(arg, "--lextest") == 0) {
332 } else if(strcmp(arg, "--print-ast") == 0) {
334 } else if(strcmp(arg, "--print-fluffy") == 0) {
336 } else if(strcmp(arg, "--dump") == 0) {
338 } else if(strcmp(arg, "-v") == 0) {
340 } else if(arg[0] == '-' && arg[1] == 'f') {
341 const char *opt = &arg[2];
355 } else if(arg[0] == '-' && arg[1] == 'b') {
356 const char *opt = &arg[2];
369 //firm_be_option(opt);
370 } else if(arg[0] == '-') {
374 strncmp(arg + 1, "std=", 4) == 0) {
375 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
382 fprintf(stderr, "Error: multiple input files specified\n");
392 fprintf(stderr, "%s: no input files\n", argv[0]);
396 if(mode == LexTest) {
401 FILE *const in = preprocess(input);
402 translation_unit_t *const unit = do_parsing(in, input);
407 if(mode == PrintAst) {
411 if(mode == PrintFluffy) {
412 ast_set_output(stdout);
413 write_fluffy_decls(unit);
417 const char *sname = NULL;
418 if(mode == Compile) {
422 get_output_name(outsname, sizeof(outsname), input, ".s");
426 create_firm_prog(unit);
430 if(mode == CompileAssemble) {
432 const char *oname = outname;
434 get_output_name(outoname, sizeof(outoname), input, ".o");
437 assemble(sname, oname);
439 assert(mode == CompileAssembleLink);
444 link(sname, outname);