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;
43 static void initialize_firm(void)
46 firm_init_options(NULL, 0, NULL);
48 const backend_params *be_params;
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 be_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 const backend_params *const be_params = be_init();
248 create_intrinsic_fkt *const arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
249 void *const create_intrinsic_ctx = be_params->create_intrinsic_ctx;
250 lwrdw_param_t lwrdw_param = {
255 def_create_intrinsic_fkt,
258 if (arch_create_intrinsic) {
259 lwrdw_param.create_intrinsic = arch_create_intrinsic;
260 lwrdw_param.ctx = create_intrinsic_ctx;
263 for(int i = 0; i < get_irp_n_irgs(); ++i) {
264 ir_graph *irg = get_irp_irg(i);
266 optimize_graph_df(irg);
267 dump(irg, "-01-localopt");
269 dump(irg, "-02-place");
272 lower_dw_ops(&lwrdw_param);
274 optimize_graph_df(irg);
275 dump(irg, "-05-localopt");
281 void write_fluffy_decls(translation_unit_t *unit);
283 typedef enum compile_mode_t {
292 static void usage(const char *argv0)
294 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
297 int main(int argc, char **argv)
310 const char *input = NULL;
311 const char *outname = NULL;
312 compile_mode_t mode = CompileAssembleLink;
314 for(int i = 1; i < argc; ++i) {
315 const char *arg = argv[i];
316 if(strcmp(arg, "-o") == 0) {
323 } else if(strcmp(arg, "-c") == 0) {
324 mode = CompileAssemble;
325 } else if(strcmp(arg, "-S") == 0) {
327 } else if(strcmp(arg, "--lextest") == 0) {
329 } else if(strcmp(arg, "--print-ast") == 0) {
331 } else if(strcmp(arg, "--print-fluffy") == 0) {
333 } else if(strcmp(arg, "-v") == 0) {
335 } else if(arg[0] == '-') {
339 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
346 fprintf(stderr, "Error: multiple input files specified\n");
356 fprintf(stderr, "%s: no input files\n", argv[0]);
360 if(mode == LexTest) {
365 FILE *const in = preprocess(input);
366 translation_unit_t *const unit = do_parsing(in, input);
371 if(mode == PrintAst) {
375 if(mode == PrintFluffy) {
376 ast_set_output(stdout);
377 write_fluffy_decls(unit);
381 const char *sname = NULL;
382 if(mode == Compile) {
386 get_output_name(outsname, sizeof(outsname), input, ".s");
390 create_firm_prog(unit);
394 if(mode == CompileAssemble) {
396 const char *oname = outname;
398 get_output_name(outoname, sizeof(outoname), input, ".o");
401 assemble(sname, oname);
403 assert(mode == CompileAssembleLink);
408 link(sname, outname);