8 #include <libfirm/firm.h>
9 #include <libfirm/be.h>
13 #include "type_hash.h"
16 #include "adt/error.h"
22 static const ir_settings_if_conv_t *if_conv_info = NULL;
24 static void initialize_firm(void)
27 firm_init_options(NULL, 0, NULL);
29 const backend_params *be_params;
30 firm_parameter_t params;
31 memset(¶ms, 0, sizeof(params));
33 params.size = sizeof(params);
34 params.enable_statistics = 0;
35 params.initialize_local_func = uninitialized_local_var;
37 params.builtin_dbg = NULL;
39 /* initialize backend */
40 be_params = be_init();
41 be_set_debug_retrieve(retrieve_dbg);
42 params.arch_op_settings = be_params->arch_op_settings;
43 if_conv_info = be_params->if_conv_info;
45 /* intialize firm itself */
47 dbg_init(NULL, NULL, dbg_snprint);
49 set_opt_constant_folding(1);
50 set_opt_unreachable_code(1);
51 set_opt_control_flow_straightening(1);
52 set_opt_control_flow_weak_simplification(1);
53 set_opt_control_flow_strong_simplification(1);
54 set_opt_dead_node_elimination(1);
55 set_opt_reassociation(1);
57 set_opt_dyn_meth_dispatch(1);
59 set_opt_tail_recursion(1);
60 set_opt_dead_method_elimination(1);
61 set_opt_precise_exc_context(0);
62 set_opt_loop_unrolling(0);
63 set_opt_strength_red(0);
64 set_opt_redundant_loadstore(1);
65 set_opt_fragile_ops(0);
66 set_opt_function_call(1);
67 set_opt_optimize_class_casts(0);
68 set_opt_suppress_downcast_optimization(0);
69 set_opt_remove_confirm(1);
70 set_opt_scalar_replacement(1);
71 set_opt_ldst_only_null_ptr_exceptions(1);
72 set_opt_alias_analysis(1);
77 static void dump(const char *suffix)
79 dump_ir_block_graph(current_ir_graph, suffix);
82 static void get_output_name(char *buf, size_t buflen, const char *inputname,
85 size_t last_dot = 0xffffffff;
87 for(const char *c = inputname; *c != 0; ++c) {
92 if(last_dot == 0xffffffff)
95 if(last_dot >= buflen)
96 panic("filename too long");
97 memcpy(buf, inputname, last_dot);
99 size_t extlen = strlen(newext) + 1;
100 if(extlen + last_dot >= buflen)
101 panic("filename too long");
102 memcpy(buf+last_dot, newext, extlen);
105 static translation_unit_t *do_parsing(const char *fname)
107 FILE *in = fopen(fname, "r");
109 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
113 lexer_open_stream(in, fname);
115 translation_unit_t *unit = parse();
122 static void lextest(const char *fname)
124 FILE *in = fopen(fname, "r");
126 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
130 lexer_open_stream(in, fname);
133 lexer_next_preprocessing_token();
134 print_token(stdout, &lexer_token);
136 } while(lexer_token.type != T_EOF);
141 static void backend(const char *inputname, const char *outname)
143 FILE *out = fopen(outname, "w");
145 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
150 be_main(out, inputname);
155 static void emit(const char *input_name, const char *out_name)
157 backend(input_name, out_name);
160 static void link(const char *in, const char *out)
164 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
168 int err = system(buf);
170 fprintf(stderr, "linker reported an error\n");
177 static void create_firm_prog(translation_unit_t *unit)
179 translation_unit_to_firm(unit);
181 int n_irgs = get_irp_n_irgs();
182 for(int i = 0; i < n_irgs; ++i) {
183 current_ir_graph = get_irp_irg(i);
188 void write_fluffy_decls(translation_unit_t *unit);
190 int main(int argc, char **argv)
203 if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
208 if(argc > 2 && strcmp(argv[1], "--print-ast") == 0) {
209 translation_unit_t *unit = do_parsing(argv[2]);
210 ast_set_output(stdout);
215 if(argc > 2 && strcmp(argv[1], "--print-fluffy") == 0) {
216 translation_unit_t *unit = do_parsing(argv[2]);
217 ast_set_output(stdout);
218 write_fluffy_decls(unit);
222 for(int i = 1; i < argc; ++i) {
223 const char *input = argv[i];
226 get_output_name(outfname, sizeof(outfname), input, ".s");
228 translation_unit_t *unit = do_parsing(input);
229 create_firm_prog(unit);
230 emit(input, outfname);
231 link(outfname, "a.out");