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)
86 dump_ir_block_graph(irg, suffix);
89 static void get_output_name(char *buf, size_t buflen, const char *inputname,
92 size_t last_dot = 0xffffffff;
94 for(const char *c = inputname; *c != 0; ++c) {
99 if(last_dot == 0xffffffff)
102 if(last_dot >= buflen)
103 panic("filename too long");
104 memcpy(buf, inputname, last_dot);
106 size_t extlen = strlen(newext) + 1;
107 if(extlen + last_dot >= buflen)
108 panic("filename too long");
109 memcpy(buf+last_dot, newext, extlen);
112 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
114 lexer_open_stream(in, input);
115 translation_unit_t *unit = parse();
119 static void lextest(const char *fname)
121 FILE *in = fopen(fname, "r");
123 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
127 lexer_open_stream(in, fname);
130 lexer_next_preprocessing_token();
131 print_token(stdout, &lexer_token);
133 } while(lexer_token.type != T_EOF);
138 static void backend(const char *inputname, const char *outname)
140 FILE *out = fopen(outname, "w");
142 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
147 be_main(out, inputname);
152 static void emit(const char *input_name, const char *out_name)
154 backend(input_name, out_name);
157 static FILE* preprocess(const char *in)
161 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
165 FILE* f = popen(buf, "r");
167 fprintf(stderr, "invoking preprocessor failed\n");
173 static void link(const char *in, const char *out)
177 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
181 int err = system(buf);
183 fprintf(stderr, "linker reported an error\n");
188 static void assemble(const char *in, const char *out)
192 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
196 int err = system(buf);
198 fprintf(stderr, "assembler reported an error\n");
203 static void create_firm_prog(translation_unit_t *unit)
205 translation_unit_to_firm(unit);
207 //dump_globals_as_text(dump_verbosity_max, "-globals");
209 int n_irgs = get_irp_n_irgs();
210 for(int i = 0; i < n_irgs; ++i) {
211 ir_graph *const irg = get_irp_irg(i);
216 for(int i = 0; i < n_irgs; ++i) {
217 ir_graph *const irg = get_irp_irg(i);
222 static void optimize(void)
225 ir_entity **keep_methods;
227 cgana(&arr_len, &keep_methods);
228 gc_irgs(arr_len, keep_methods);
231 optimize_funccalls(0);
233 for(int i = 0; i < get_irp_n_irgs(); ++i) {
234 ir_graph *irg = get_irp_irg(i);
236 optimize_graph_df(irg);
237 dump(irg, "-localopt");
245 void write_fluffy_decls(translation_unit_t *unit);
247 typedef enum compile_mode_t {
256 static void usage(const char *argv0)
258 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
261 int main(int argc, char **argv)
274 const char *input = NULL;
275 const char *outname = NULL;
276 compile_mode_t mode = CompileAssembleLink;
278 for(int i = 1; i < argc; ++i) {
279 const char *arg = argv[i];
280 if(strcmp(arg, "-o") == 0) {
287 } else if(strcmp(arg, "-c") == 0) {
288 mode = CompileAssemble;
289 } else if(strcmp(arg, "-S") == 0) {
291 } else if(strcmp(arg, "--lextest") == 0) {
293 } else if(strcmp(arg, "--print-ast") == 0) {
295 } else if(strcmp(arg, "--print-fluffy") == 0) {
297 } else if(strcmp(arg, "-v") == 0) {
299 } else if(arg[0] == '-') {
304 fprintf(stderr, "Error: multiple input files specified\n");
314 fprintf(stderr, "%s: no input files\n", argv[0]);
318 if(mode == LexTest) {
323 FILE *const in = preprocess(input);
324 translation_unit_t *const unit = do_parsing(in, input);
329 if(mode == PrintAst) {
333 if(mode == PrintFluffy) {
334 ast_set_output(stdout);
335 write_fluffy_decls(unit);
339 const char *sname = NULL;
340 if(mode == Compile) {
344 get_output_name(outsname, sizeof(outsname), input, ".s");
348 create_firm_prog(unit);
352 if(mode == CompileAssemble) {
354 const char *oname = outname;
356 get_output_name(outoname, sizeof(outoname), input, ".o");
359 assemble(sname, oname);
361 assert(mode == CompileAssembleLink);
366 link(sname, outname);