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)
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);
226 for(int i = 0; i < n_irgs; ++i) {
227 ir_graph *const irg = get_irp_irg(i);
232 static void optimize(void)
235 ir_entity **keep_methods;
237 cgana(&arr_len, &keep_methods);
238 gc_irgs(arr_len, keep_methods);
241 optimize_funccalls(0);
243 lwrdw_param_t lwrdw_param = {
248 def_create_intrinsic_fkt,
251 if (be_params->arch_create_intrinsic_fkt) {
252 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
253 lwrdw_param.ctx = be_params->create_intrinsic_ctx;
256 for(int i = 0; i < get_irp_n_irgs(); ++i) {
257 ir_graph *irg = get_irp_irg(i);
259 optimize_graph_df(irg);
260 dump(irg, "-01-localopt");
262 dump(irg, "-02-place");
265 lower_dw_ops(&lwrdw_param);
267 optimize_graph_df(irg);
268 dump(irg, "-05-localopt");
274 void write_fluffy_decls(translation_unit_t *unit);
276 typedef enum compile_mode_t {
285 static void usage(const char *argv0)
287 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
290 int main(int argc, char **argv)
303 const char *input = NULL;
304 const char *outname = NULL;
305 compile_mode_t mode = CompileAssembleLink;
307 for(int i = 1; i < argc; ++i) {
308 const char *arg = argv[i];
309 if(strcmp(arg, "-o") == 0) {
316 } else if(strcmp(arg, "-c") == 0) {
317 mode = CompileAssemble;
318 } else if(strcmp(arg, "-S") == 0) {
320 } else if(strcmp(arg, "--lextest") == 0) {
322 } else if(strcmp(arg, "--print-ast") == 0) {
324 } else if(strcmp(arg, "--print-fluffy") == 0) {
326 } else if(strcmp(arg, "-v") == 0) {
328 } else if(arg[0] == '-') {
332 strncmp(arg + 1, "std=", 4) == 0) {
333 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
340 fprintf(stderr, "Error: multiple input files specified\n");
350 fprintf(stderr, "%s: no input files\n", argv[0]);
354 if(mode == LexTest) {
359 FILE *const in = preprocess(input);
360 translation_unit_t *const unit = do_parsing(in, input);
365 if(mode == PrintAst) {
369 if(mode == PrintFluffy) {
370 ast_set_output(stdout);
371 write_fluffy_decls(unit);
375 const char *sname = NULL;
376 if(mode == Compile) {
380 get_output_name(outsname, sizeof(outsname), input, ".s");
384 create_firm_prog(unit);
388 if(mode == CompileAssemble) {
390 const char *oname = outname;
392 get_output_name(outoname, sizeof(outoname), input, ".o");
395 assemble(sname, oname);
397 assert(mode == CompileAssembleLink);
402 link(sname, outname);