15 #include <libfirm/firm.h>
16 #include <libfirm/be.h>
20 #include "type_hash.h"
23 #include "adt/error.h"
25 #define PREPROCESSOR "cpp"
29 /* remap some names */
30 #define popen(cmd, mode) _popen(cmd, mode)
31 #define pclose(file) _pclose(file)
36 static const ir_settings_if_conv_t *if_conv_info = NULL;
38 static void initialize_firm(void)
41 firm_init_options(NULL, 0, NULL);
43 const backend_params *be_params;
44 firm_parameter_t params;
45 memset(¶ms, 0, sizeof(params));
47 params.size = sizeof(params);
48 params.enable_statistics = 0;
49 params.initialize_local_func = uninitialized_local_var;
51 params.builtin_dbg = NULL;
53 /* initialize backend */
54 be_params = be_init();
55 be_set_debug_retrieve(retrieve_dbg);
56 params.arch_op_settings = be_params->arch_op_settings;
57 if_conv_info = be_params->if_conv_info;
59 /* intialize firm itself */
61 dbg_init(NULL, NULL, dbg_snprint);
63 set_opt_constant_folding(1);
64 set_opt_unreachable_code(1);
65 set_opt_control_flow_straightening(1);
66 set_opt_control_flow_weak_simplification(1);
67 set_opt_control_flow_strong_simplification(1);
68 set_opt_dyn_meth_dispatch(1);
70 set_opt_precise_exc_context(0);
71 set_opt_strength_red(0);
72 set_opt_fragile_ops(0);
73 set_opt_optimize_class_casts(0);
74 set_opt_suppress_downcast_optimization(0);
75 set_opt_remove_confirm(1);
76 set_opt_scalar_replacement(1);
77 set_opt_ldst_only_null_ptr_exceptions(1);
78 set_opt_alias_analysis(1);
81 dump_keepalive_edges(1);
84 static void dump(ir_graph *irg, const char *suffix)
87 dump_ir_block_graph(irg, suffix);
89 (void)irg, (void)suffix;
93 static void get_output_name(char *buf, size_t buflen, const char *inputname,
96 size_t last_dot = 0xffffffff;
98 for(const char *c = inputname; *c != 0; ++c) {
103 if(last_dot == 0xffffffff)
106 if(last_dot >= buflen)
107 panic("filename too long");
108 memcpy(buf, inputname, last_dot);
110 size_t extlen = strlen(newext) + 1;
111 if(extlen + last_dot >= buflen)
112 panic("filename too long");
113 memcpy(buf+last_dot, newext, extlen);
116 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
118 lexer_open_stream(in, input);
119 translation_unit_t *unit = parse();
123 static void lextest(const char *fname)
125 FILE *in = fopen(fname, "r");
127 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
131 lexer_open_stream(in, fname);
134 lexer_next_preprocessing_token();
135 print_token(stdout, &lexer_token);
137 } while(lexer_token.type != T_EOF);
142 static void backend(const char *inputname, const char *outname)
144 FILE *out = fopen(outname, "w");
146 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
151 be_main(out, inputname);
156 static void emit(const char *input_name, const char *out_name)
158 backend(input_name, out_name);
161 static FILE* preprocess(const char *in)
165 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
169 FILE* f = popen(buf, "r");
171 fprintf(stderr, "invoking preprocessor failed\n");
177 static void link(const char *in, const char *out)
181 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
185 int err = system(buf);
187 fprintf(stderr, "linker reported an error\n");
192 static void assemble(const char *in, const char *out)
196 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
200 int err = system(buf);
202 fprintf(stderr, "assembler reported an error\n");
207 static void create_firm_prog(translation_unit_t *unit)
209 translation_unit_to_firm(unit);
211 //dump_globals_as_text(dump_verbosity_max, "-globals");
213 int n_irgs = get_irp_n_irgs();
214 for(int i = 0; i < n_irgs; ++i) {
215 ir_graph *const irg = get_irp_irg(i);
220 for(int i = 0; i < n_irgs; ++i) {
221 ir_graph *const irg = get_irp_irg(i);
226 static void optimize(void)
229 ir_entity **keep_methods;
231 cgana(&arr_len, &keep_methods);
232 gc_irgs(arr_len, keep_methods);
235 optimize_funccalls(0);
237 const backend_params *const be_params = be_init();
238 create_intrinsic_fkt *const arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
239 void *const create_intrinsic_ctx = be_params->create_intrinsic_ctx;
240 lwrdw_param_t lwrdw_param = {
245 def_create_intrinsic_fkt,
248 if (arch_create_intrinsic) {
249 lwrdw_param.create_intrinsic = arch_create_intrinsic;
250 lwrdw_param.ctx = create_intrinsic_ctx;
253 for(int i = 0; i < get_irp_n_irgs(); ++i) {
254 ir_graph *irg = get_irp_irg(i);
256 optimize_graph_df(irg);
257 dump(irg, "-localopt");
262 lower_dw_ops(&lwrdw_param);
267 void write_fluffy_decls(translation_unit_t *unit);
269 typedef enum compile_mode_t {
278 static void usage(const char *argv0)
280 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
283 int main(int argc, char **argv)
296 const char *input = NULL;
297 const char *outname = NULL;
298 compile_mode_t mode = CompileAssembleLink;
300 for(int i = 1; i < argc; ++i) {
301 const char *arg = argv[i];
302 if(strcmp(arg, "-o") == 0) {
309 } else if(strcmp(arg, "-c") == 0) {
310 mode = CompileAssemble;
311 } else if(strcmp(arg, "-S") == 0) {
313 } else if(strcmp(arg, "--lextest") == 0) {
315 } else if(strcmp(arg, "--print-ast") == 0) {
317 } else if(strcmp(arg, "--print-fluffy") == 0) {
319 } else if(strcmp(arg, "-v") == 0) {
321 } else if(arg[0] == '-') {
325 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
332 fprintf(stderr, "Error: multiple input files specified\n");
342 fprintf(stderr, "%s: no input files\n", argv[0]);
346 if(mode == LexTest) {
351 FILE *const in = preprocess(input);
352 translation_unit_t *const unit = do_parsing(in, input);
357 if(mode == PrintAst) {
361 if(mode == PrintFluffy) {
362 ast_set_output(stdout);
363 write_fluffy_decls(unit);
367 const char *sname = NULL;
368 if(mode == Compile) {
372 get_output_name(outsname, sizeof(outsname), input, ".s");
376 create_firm_prog(unit);
380 if(mode == CompileAssemble) {
382 const char *oname = outname;
384 get_output_name(outoname, sizeof(outoname), input, ".o");
387 assemble(sname, oname);
389 assert(mode == CompileAssembleLink);
394 link(sname, outname);