15 #include <libfirm/firm.h>
16 #include <libfirm/be.h>
20 #include "type_hash.h"
23 #include "adt/error.h"
25 #define PREPROCESSOR "cpp"
30 static const ir_settings_if_conv_t *if_conv_info = NULL;
32 static void initialize_firm(void)
35 firm_init_options(NULL, 0, NULL);
37 const backend_params *be_params;
38 firm_parameter_t params;
39 memset(¶ms, 0, sizeof(params));
41 params.size = sizeof(params);
42 params.enable_statistics = 0;
43 params.initialize_local_func = uninitialized_local_var;
45 params.builtin_dbg = NULL;
47 /* initialize backend */
48 be_params = be_init();
49 be_set_debug_retrieve(retrieve_dbg);
50 params.arch_op_settings = be_params->arch_op_settings;
51 if_conv_info = be_params->if_conv_info;
53 /* intialize firm itself */
55 dbg_init(NULL, NULL, dbg_snprint);
57 set_opt_constant_folding(1);
58 set_opt_unreachable_code(1);
59 set_opt_control_flow_straightening(1);
60 set_opt_control_flow_weak_simplification(1);
61 set_opt_control_flow_strong_simplification(1);
62 set_opt_dead_node_elimination(1);
63 set_opt_reassociation(1);
65 set_opt_dyn_meth_dispatch(1);
67 set_opt_tail_recursion(1);
68 set_opt_dead_method_elimination(1);
69 set_opt_precise_exc_context(0);
70 set_opt_loop_unrolling(0);
71 set_opt_strength_red(0);
72 set_opt_redundant_loadstore(1);
73 set_opt_fragile_ops(0);
74 set_opt_function_call(1);
75 set_opt_optimize_class_casts(0);
76 set_opt_suppress_downcast_optimization(0);
77 set_opt_remove_confirm(1);
78 set_opt_scalar_replacement(1);
79 set_opt_ldst_only_null_ptr_exceptions(1);
80 set_opt_alias_analysis(1);
83 dump_keepalive_edges(1);
86 static void dump(ir_graph *irg, const char *suffix)
88 dump_ir_block_graph(irg, suffix);
91 static void get_output_name(char *buf, size_t buflen, const char *inputname,
94 size_t last_dot = 0xffffffff;
96 for(const char *c = inputname; *c != 0; ++c) {
101 if(last_dot == 0xffffffff)
104 if(last_dot >= buflen)
105 panic("filename too long");
106 memcpy(buf, inputname, last_dot);
108 size_t extlen = strlen(newext) + 1;
109 if(extlen + last_dot >= buflen)
110 panic("filename too long");
111 memcpy(buf+last_dot, newext, extlen);
114 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
116 lexer_open_stream(in, input);
117 translation_unit_t *unit = parse();
121 static void lextest(const char *fname)
123 FILE *in = fopen(fname, "r");
125 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
129 lexer_open_stream(in, fname);
132 lexer_next_preprocessing_token();
133 print_token(stdout, &lexer_token);
135 } while(lexer_token.type != T_EOF);
140 static void backend(const char *inputname, const char *outname)
142 FILE *out = fopen(outname, "w");
144 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
149 be_main(out, inputname);
154 static void emit(const char *input_name, const char *out_name)
156 backend(input_name, out_name);
159 static FILE* preprocess(const char *in)
163 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
167 FILE* f = popen(buf, "r");
169 fprintf(stderr, "invoking preprocessor failed\n");
175 static void link(const char *in, const char *out)
179 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
183 int err = system(buf);
185 fprintf(stderr, "linker reported an error\n");
190 static void assemble(const char *in, const char *out)
194 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
198 int err = system(buf);
200 fprintf(stderr, "assembler reported an error\n");
205 static void create_firm_prog(translation_unit_t *unit)
207 translation_unit_to_firm(unit);
209 //dump_globals_as_text(dump_verbosity_max, "-globals");
211 int n_irgs = get_irp_n_irgs();
212 for(int i = 0; i < n_irgs; ++i) {
213 ir_graph *const irg = get_irp_irg(i);
218 for(int i = 0; i < n_irgs; ++i) {
219 ir_graph *const irg = get_irp_irg(i);
224 static void optimize(void)
227 ir_entity **keep_methods;
229 cgana(&arr_len, &keep_methods);
230 gc_irgs(arr_len, keep_methods);
233 optimize_funccalls(0);
235 for(int i = 0; i < get_irp_n_irgs(); ++i) {
236 ir_graph *irg = get_irp_irg(i);
238 optimize_graph_df(irg);
239 dump(irg, "-localopt");
247 void write_fluffy_decls(translation_unit_t *unit);
249 typedef enum compile_mode_t {
258 static void usage(const char *argv0)
260 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
263 int main(int argc, char **argv)
276 const char *input = NULL;
277 const char *outname = NULL;
278 compile_mode_t mode = CompileAssembleLink;
280 for(int i = 1; i < argc; ++i) {
281 const char *arg = argv[i];
282 if(strcmp(arg, "-o") == 0) {
289 } else if(strcmp(arg, "-c") == 0) {
290 mode = CompileAssemble;
291 } else if(strcmp(arg, "-S") == 0) {
293 } else if(strcmp(arg, "--lextest") == 0) {
295 } else if(strcmp(arg, "--print-ast") == 0) {
297 } else if(strcmp(arg, "--print-fluffy") == 0) {
299 } else if(strcmp(arg, "-v") == 0) {
301 } else if(arg[0] == '-') {
306 fprintf(stderr, "Error: multiple input files specified\n");
316 fprintf(stderr, "%s: no input files\n", argv[0]);
320 if(mode == LexTest) {
325 FILE *const in = preprocess(input);
326 translation_unit_t *const unit = do_parsing(in, input);
331 if(mode == PrintAst) {
335 if(mode == PrintFluffy) {
336 ast_set_output(stdout);
337 write_fluffy_decls(unit);
341 const char *sname = NULL;
342 if(mode == Compile) {
346 get_output_name(outsname, sizeof(outsname), input, ".s");
350 create_firm_prog(unit);
354 if(mode == CompileAssemble) {
356 const char *oname = outname;
358 get_output_name(outoname, sizeof(outoname), input, ".o");
361 assemble(sname, oname);
363 assert(mode == CompileAssembleLink);
368 link(sname, outname);