12 #include <libfirm/firm.h>
13 #include <libfirm/be.h>
17 #include "type_hash.h"
20 #include "adt/error.h"
26 static const ir_settings_if_conv_t *if_conv_info = NULL;
28 static void initialize_firm(void)
31 firm_init_options(NULL, 0, NULL);
33 const backend_params *be_params;
34 firm_parameter_t params;
35 memset(¶ms, 0, sizeof(params));
37 params.size = sizeof(params);
38 params.enable_statistics = 0;
39 params.initialize_local_func = uninitialized_local_var;
41 params.builtin_dbg = NULL;
43 /* initialize backend */
44 be_params = be_init();
45 be_set_debug_retrieve(retrieve_dbg);
46 params.arch_op_settings = be_params->arch_op_settings;
47 if_conv_info = be_params->if_conv_info;
49 /* intialize firm itself */
51 dbg_init(NULL, NULL, dbg_snprint);
53 set_opt_constant_folding(1);
54 set_opt_unreachable_code(1);
55 set_opt_control_flow_straightening(1);
56 set_opt_control_flow_weak_simplification(1);
57 set_opt_control_flow_strong_simplification(1);
58 set_opt_dead_node_elimination(1);
59 set_opt_reassociation(1);
61 set_opt_dyn_meth_dispatch(1);
63 set_opt_tail_recursion(1);
64 set_opt_dead_method_elimination(1);
65 set_opt_precise_exc_context(0);
66 set_opt_loop_unrolling(0);
67 set_opt_strength_red(0);
68 set_opt_redundant_loadstore(1);
69 set_opt_fragile_ops(0);
70 set_opt_function_call(1);
71 set_opt_optimize_class_casts(0);
72 set_opt_suppress_downcast_optimization(0);
73 set_opt_remove_confirm(1);
74 set_opt_scalar_replacement(1);
75 set_opt_ldst_only_null_ptr_exceptions(1);
76 set_opt_alias_analysis(1);
79 dump_keepalive_edges(1);
82 static void dump(const char *suffix)
84 dump_ir_block_graph(current_ir_graph, suffix);
87 static void get_output_name(char *buf, size_t buflen, const char *inputname,
90 size_t last_dot = 0xffffffff;
92 for(const char *c = inputname; *c != 0; ++c) {
97 if(last_dot == 0xffffffff)
100 if(last_dot >= buflen)
101 panic("filename too long");
102 memcpy(buf, inputname, last_dot);
104 size_t extlen = strlen(newext) + 1;
105 if(extlen + last_dot >= buflen)
106 panic("filename too long");
107 memcpy(buf+last_dot, newext, extlen);
110 static translation_unit_t *do_parsing(const char *fname)
112 FILE *in = fopen(fname, "r");
114 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
118 lexer_open_stream(in, fname);
120 translation_unit_t *unit = parse();
127 static void lextest(const char *fname)
129 FILE *in = fopen(fname, "r");
131 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
135 lexer_open_stream(in, fname);
138 lexer_next_preprocessing_token();
139 print_token(stdout, &lexer_token);
141 } while(lexer_token.type != T_EOF);
146 static void backend(const char *inputname, const char *outname)
148 FILE *out = fopen(outname, "w");
150 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
155 be_main(out, inputname);
160 static void emit(const char *input_name, const char *out_name)
162 backend(input_name, out_name);
165 static void link(const char *in, const char *out)
169 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
173 int err = system(buf);
175 fprintf(stderr, "linker reported an error\n");
182 static void create_firm_prog(translation_unit_t *unit)
184 translation_unit_to_firm(unit);
186 int n_irgs = get_irp_n_irgs();
187 for(int i = 0; i < n_irgs; ++i) {
188 current_ir_graph = get_irp_irg(i);
193 void write_fluffy_decls(translation_unit_t *unit);
195 int main(int argc, char **argv)
208 if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
213 if(argc > 2 && strcmp(argv[1], "--print-ast") == 0) {
214 translation_unit_t *unit = do_parsing(argv[2]);
215 ast_set_output(stdout);
220 if(argc > 2 && strcmp(argv[1], "--print-fluffy") == 0) {
221 translation_unit_t *unit = do_parsing(argv[2]);
222 ast_set_output(stdout);
223 write_fluffy_decls(unit);
227 for(int i = 1; i < argc; ++i) {
228 const char *input = argv[i];
231 get_output_name(outfname, sizeof(outfname), input, ".s");
233 translation_unit_t *unit = do_parsing(input);
237 create_firm_prog(unit);
238 emit(input, outfname);
239 link(outfname, "a.out");