15 #include <libfirm/firm.h>
16 #include <libfirm/be.h>
20 #include "type_hash.h"
23 #include "adt/error.h"
26 #define PREPROCESSOR "cpp"
34 /* remap some names */
35 #define popen(cmd, mode) _popen(cmd, mode)
36 #define pclose(file) _pclose(file)
41 static const ir_settings_if_conv_t *if_conv_info = NULL;
42 static const backend_params *be_params = NULL;
44 static void initialize_firm(void)
47 firm_init_options(NULL, 0, NULL);
49 firm_parameter_t params;
50 memset(¶ms, 0, sizeof(params));
52 params.size = sizeof(params);
53 params.enable_statistics = 0;
54 params.initialize_local_func = uninitialized_local_var;
56 params.builtin_dbg = NULL;
58 /* initialize backend */
59 be_params = be_init();
60 ir_set_debug_retrieve(retrieve_dbg);
61 params.arch_op_settings = be_params->arch_op_settings;
62 if_conv_info = be_params->if_conv_info;
64 /* intialize firm itself */
66 dbg_init(NULL, NULL, dbg_snprint);
68 set_opt_constant_folding(1);
69 set_opt_unreachable_code(1);
70 set_opt_control_flow_straightening(1);
71 set_opt_control_flow_weak_simplification(1);
72 set_opt_control_flow_strong_simplification(1);
73 set_opt_dyn_meth_dispatch(1);
75 set_opt_precise_exc_context(0);
76 set_opt_strength_red(0);
77 set_opt_fragile_ops(0);
78 set_opt_optimize_class_casts(0);
79 set_opt_suppress_downcast_optimization(0);
80 set_opt_remove_confirm(1);
81 set_opt_scalar_replacement(1);
82 set_opt_ldst_only_null_ptr_exceptions(1);
83 set_opt_alias_analysis(1);
86 dump_keepalive_edges(1);
89 static void dump(ir_graph *irg, const char *suffix)
92 dump_ir_block_graph(irg, suffix);
94 (void)irg, (void)suffix;
98 static void get_output_name(char *buf, size_t buflen, const char *inputname,
101 size_t last_dot = 0xffffffff;
103 for(const char *c = inputname; *c != 0; ++c) {
108 if(last_dot == 0xffffffff)
111 if(last_dot >= buflen)
112 panic("filename too long");
113 memcpy(buf, inputname, last_dot);
115 size_t extlen = strlen(newext) + 1;
116 if(extlen + last_dot >= buflen)
117 panic("filename too long");
118 memcpy(buf+last_dot, newext, extlen);
121 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
123 lexer_open_stream(in, input);
124 translation_unit_t *unit = parse();
128 static void lextest(const char *fname)
130 FILE *in = fopen(fname, "r");
132 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
136 lexer_open_stream(in, fname);
139 lexer_next_preprocessing_token();
140 print_token(stdout, &lexer_token);
142 } while(lexer_token.type != T_EOF);
147 static void backend(const char *inputname, const char *outname)
149 FILE *out = fopen(outname, "w");
151 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
156 be_main(out, inputname);
161 static void emit(const char *input_name, const char *out_name)
163 backend(input_name, out_name);
166 static FILE* preprocess(const char *in)
171 snprintf(buf, sizeof(buf), PREPROCESSOR " %s",in);
173 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
179 FILE* f = popen(buf, "r");
181 fprintf(stderr, "invoking preprocessor failed\n");
187 static void link(const char *in, const char *out)
191 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
195 int err = system(buf);
197 fprintf(stderr, "linker reported an error\n");
202 static void assemble(const char *in, const char *out)
206 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
210 int err = system(buf);
212 fprintf(stderr, "assembler reported an error\n");
217 static void create_firm_prog(translation_unit_t *unit)
219 translation_unit_to_firm(unit);
221 //dump_globals_as_text(dump_verbosity_max, "-globals");
223 int n_irgs = get_irp_n_irgs();
224 for(int i = 0; i < n_irgs; ++i) {
225 ir_graph *const irg = get_irp_irg(i);
230 for(int i = 0; i < n_irgs; ++i) {
231 ir_graph *const irg = get_irp_irg(i);
236 static void optimize(void)
239 ir_entity **keep_methods;
241 cgana(&arr_len, &keep_methods);
242 gc_irgs(arr_len, keep_methods);
245 optimize_funccalls(0);
247 lwrdw_param_t lwrdw_param = {
252 def_create_intrinsic_fkt,
255 if (be_params->arch_create_intrinsic_fkt) {
256 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
257 lwrdw_param.ctx = be_params->create_intrinsic_ctx;
260 for(int i = 0; i < get_irp_n_irgs(); ++i) {
261 ir_graph *irg = get_irp_irg(i);
263 optimize_graph_df(irg);
264 dump(irg, "-01-localopt");
266 dump(irg, "-02-place");
269 lower_dw_ops(&lwrdw_param);
271 optimize_graph_df(irg);
272 dump(irg, "-05-localopt");
278 void write_fluffy_decls(translation_unit_t *unit);
280 typedef enum compile_mode_t {
289 static void usage(const char *argv0)
291 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
294 int main(int argc, char **argv)
307 const char *input = NULL;
308 const char *outname = NULL;
309 compile_mode_t mode = CompileAssembleLink;
311 for(int i = 1; i < argc; ++i) {
312 const char *arg = argv[i];
313 if(strcmp(arg, "-o") == 0) {
320 } else if(strcmp(arg, "-c") == 0) {
321 mode = CompileAssemble;
322 } else if(strcmp(arg, "-S") == 0) {
324 } else if(strcmp(arg, "--lextest") == 0) {
326 } else if(strcmp(arg, "--print-ast") == 0) {
328 } else if(strcmp(arg, "--print-fluffy") == 0) {
330 } else if(strcmp(arg, "-v") == 0) {
332 } else if(arg[0] == '-') {
336 strncmp(arg + 1, "std=", 4) == 0) {
337 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
344 fprintf(stderr, "Error: multiple input files specified\n");
354 fprintf(stderr, "%s: no input files\n", argv[0]);
358 if(mode == LexTest) {
363 FILE *const in = preprocess(input);
364 translation_unit_t *const unit = do_parsing(in, input);
369 if(mode == PrintAst) {
373 if(mode == PrintFluffy) {
374 ast_set_output(stdout);
375 write_fluffy_decls(unit);
379 const char *sname = NULL;
380 if(mode == Compile) {
384 get_output_name(outsname, sizeof(outsname), input, ".s");
388 create_firm_prog(unit);
392 if(mode == CompileAssemble) {
394 const char *oname = outname;
396 get_output_name(outoname, sizeof(outoname), input, ".o");
399 assemble(sname, oname);
401 assert(mode == CompileAssembleLink);
406 link(sname, outname);