8 #include <libfirm/firm.h>
9 #include <libfirm/be.h>
13 #include "type_hash.h"
16 #include "adt/error.h"
18 static const ir_settings_if_conv_t *if_conv_info = NULL;
20 static void initialize_firm(void)
23 firm_init_options(NULL, 0, NULL);
25 const backend_params *be_params;
26 firm_parameter_t params;
27 memset(¶ms, 0, sizeof(params));
29 params.size = sizeof(params);
30 params.enable_statistics = 0;
31 params.initialize_local_func = uninitialized_local_var;
33 params.builtin_dbg = NULL;
35 /* initialize backend */
36 be_params = be_init();
37 be_set_debug_retrieve(retrieve_dbg);
38 params.arch_op_settings = be_params->arch_op_settings;
39 if_conv_info = be_params->if_conv_info;
41 /* intialize firm itself */
43 dbg_init(NULL, NULL, dbg_snprint);
45 set_opt_constant_folding(1);
46 set_opt_unreachable_code(1);
47 set_opt_control_flow_straightening(1);
48 set_opt_control_flow_weak_simplification(1);
49 set_opt_control_flow_strong_simplification(1);
50 set_opt_dead_node_elimination(1);
51 set_opt_reassociation(1);
53 set_opt_dyn_meth_dispatch(1);
55 set_opt_tail_recursion(1);
56 set_opt_dead_method_elimination(1);
57 set_opt_precise_exc_context(0);
58 set_opt_loop_unrolling(0);
59 set_opt_strength_red(0);
60 set_opt_redundant_loadstore(1);
61 set_opt_fragile_ops(0);
62 set_opt_function_call(1);
63 set_opt_optimize_class_casts(0);
64 set_opt_suppress_downcast_optimization(0);
65 set_opt_remove_confirm(1);
66 set_opt_scalar_replacement(1);
67 set_opt_ldst_only_null_ptr_exceptions(1);
68 set_opt_alias_analysis(1);
73 static void get_output_name(char *buf, size_t buflen, const char *inputname,
76 size_t last_dot = 0xffffffff;
78 for(const char *c = inputname; *c != 0; ++c) {
83 if(last_dot == 0xffffffff)
86 if(last_dot >= buflen)
87 panic("filename too long");
88 memcpy(buf, inputname, last_dot);
90 size_t extlen = strlen(newext) + 1;
91 if(extlen + last_dot >= buflen)
92 panic("filename too long");
93 memcpy(buf+last_dot, newext, extlen);
96 static translation_unit_t *do_parsing(const char *fname)
98 FILE *in = fopen(fname, "r");
100 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
104 lexer_open_stream(in, fname);
106 translation_unit_t *unit = parse();
113 static void lextest(const char *fname)
115 FILE *in = fopen(fname, "r");
117 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
121 lexer_open_stream(in, fname);
124 lexer_next_preprocessing_token();
125 print_token(stdout, &lexer_token);
127 } while(lexer_token.type != T_EOF);
132 static void backend(const char *inputname, const char *outname)
134 FILE *out = fopen(outname, "w");
136 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
141 be_main(out, inputname);
146 static void emit(const char *input_name)
150 get_output_name(outfname, sizeof(outfname), input_name, ".s");
151 backend(input_name, outfname);
154 static void create_firm_prog(translation_unit_t *unit)
156 translation_unit_to_firm(unit);
159 void write_fluffy_decls(translation_unit_t *unit);
161 int main(int argc, char **argv)
174 if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
179 if(argc > 2 && strcmp(argv[1], "--print-ast") == 0) {
180 translation_unit_t *unit = do_parsing(argv[2]);
181 ast_set_output(stdout);
186 if(argc > 2 && strcmp(argv[1], "--print-fluffy") == 0) {
187 translation_unit_t *unit = do_parsing(argv[2]);
188 ast_set_output(stdout);
189 write_fluffy_decls(unit);
193 for(int i = 1; i < argc; ++i) {
194 translation_unit_t *unit = do_parsing(argv[i]);
195 create_firm_prog(unit);