16 #include <libfirm/firm.h>
17 #include <libfirm/be.h>
21 #include "type_hash.h"
24 #include "adt/error.h"
27 #define PREPROCESSOR "cpp"
35 /* remap some names */
36 #define popen(cmd, mode) _popen(cmd, mode)
37 #define pclose(file) _pclose(file)
43 static const ir_settings_if_conv_t *if_conv_info = NULL;
44 static const backend_params *be_params = NULL;
46 static void initialize_firm(void)
49 firm_init_options(NULL, 0, NULL);
51 firm_parameter_t params;
52 memset(¶ms, 0, sizeof(params));
54 params.size = sizeof(params);
55 params.enable_statistics = 0;
56 params.initialize_local_func = uninitialized_local_var;
58 params.builtin_dbg = NULL;
60 /* initialize backend */
61 be_params = be_init();
62 ir_set_debug_retrieve(retrieve_dbg);
63 params.arch_op_settings = be_params->arch_op_settings;
64 if_conv_info = be_params->if_conv_info;
66 /* intialize firm itself */
68 dbg_init(NULL, NULL, dbg_snprint);
70 set_opt_constant_folding(1);
71 set_opt_unreachable_code(1);
72 set_opt_control_flow_straightening(1);
73 set_opt_control_flow_weak_simplification(1);
74 set_opt_control_flow_strong_simplification(1);
75 set_opt_dyn_meth_dispatch(1);
77 set_opt_precise_exc_context(0);
78 set_opt_strength_red(0);
79 set_opt_fragile_ops(0);
80 set_opt_optimize_class_casts(0);
81 set_opt_suppress_downcast_optimization(0);
82 set_opt_remove_confirm(1);
83 set_opt_scalar_replacement(1);
84 set_opt_ldst_only_null_ptr_exceptions(1);
85 set_opt_alias_analysis(1);
88 dump_keepalive_edges(1);
91 static void dump(ir_graph *irg, const char *suffix)
94 dump_ir_block_graph(irg, 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)
170 snprintf(buf, sizeof(buf), PREPROCESSOR " %s", in);
175 FILE* f = popen(buf, "r");
177 fprintf(stderr, "invoking preprocessor failed\n");
183 static void link(const char *in, const char *out)
187 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
191 int err = system(buf);
193 fprintf(stderr, "linker reported an error\n");
198 static void assemble(const char *in, const char *out)
202 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
206 int err = system(buf);
208 fprintf(stderr, "assembler reported an error\n");
213 static void create_firm_prog(translation_unit_t *unit)
215 translation_unit_to_firm(unit);
217 //dump_globals_as_text(dump_verbosity_max, "-globals");
219 int n_irgs = get_irp_n_irgs();
220 for(int i = 0; i < n_irgs; ++i) {
221 ir_graph *const irg = get_irp_irg(i);
225 lower_params_t params;
227 params.def_ptr_alignment = 4;
228 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
229 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
230 params.find_pointer_type = NULL;
231 params.ret_compound_in_regs = NULL;
232 lower_calls_with_compounds(¶ms);
235 for(int i = 0; i < n_irgs; ++i) {
236 ir_graph *const irg = get_irp_irg(i);
241 static void optimize(void)
244 ir_entity **keep_methods;
246 cgana(&arr_len, &keep_methods);
247 gc_irgs(arr_len, keep_methods);
250 optimize_funccalls(0);
252 lwrdw_param_t lwrdw_param = {
257 def_create_intrinsic_fkt,
260 if (be_params->arch_create_intrinsic_fkt) {
261 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
262 lwrdw_param.ctx = be_params->create_intrinsic_ctx;
265 for(int i = 0; i < get_irp_n_irgs(); ++i) {
266 ir_graph *irg = get_irp_irg(i);
268 optimize_graph_df(irg);
269 dump(irg, "-01-localopt");
271 dump(irg, "-02-place");
274 lower_dw_ops(&lwrdw_param);
276 optimize_graph_df(irg);
277 dump(irg, "-05-localopt");
283 void write_fluffy_decls(translation_unit_t *unit);
285 typedef enum compile_mode_t {
294 static void usage(const char *argv0)
296 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
299 int main(int argc, char **argv)
312 const char *input = NULL;
313 const char *outname = NULL;
314 compile_mode_t mode = CompileAssembleLink;
316 for(int i = 1; i < argc; ++i) {
317 const char *arg = argv[i];
318 if(strcmp(arg, "-o") == 0) {
325 } else if(strcmp(arg, "-c") == 0) {
326 mode = CompileAssemble;
327 } else if(strcmp(arg, "-S") == 0) {
329 } else if(strcmp(arg, "--lextest") == 0) {
331 } else if(strcmp(arg, "--print-ast") == 0) {
333 } else if(strcmp(arg, "--print-fluffy") == 0) {
335 } else if(strcmp(arg, "--dump") == 0) {
337 } else if(strcmp(arg, "-v") == 0) {
339 } else if(arg[0] == '-' && arg[1] == 'f') {
340 const char *opt = &arg[2];
354 } else if(arg[0] == '-' && arg[1] == 'b') {
355 const char *opt = &arg[2];
368 //firm_be_option(opt);
369 } else if(arg[0] == '-') {
373 strncmp(arg + 1, "std=", 4) == 0) {
374 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
381 fprintf(stderr, "Error: multiple input files specified\n");
391 fprintf(stderr, "%s: no input files\n", argv[0]);
395 if(mode == LexTest) {
400 FILE *const in = preprocess(input);
401 translation_unit_t *const unit = do_parsing(in, input);
406 if(mode == PrintAst) {
410 if(mode == PrintFluffy) {
411 ast_set_output(stdout);
412 write_fluffy_decls(unit);
416 const char *sname = NULL;
417 if(mode == Compile) {
421 get_output_name(outsname, sizeof(outsname), input, ".s");
425 create_firm_prog(unit);
429 if(mode == CompileAssemble) {
431 const char *oname = outname;
433 get_output_name(outoname, sizeof(outoname), input, ".o");
436 assemble(sname, oname);
438 assert(mode == CompileAssembleLink);
443 link(sname, outname);