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_dead_node_elimination(1);
69 set_opt_reassociation(1);
71 set_opt_dyn_meth_dispatch(1);
73 set_opt_tail_recursion(1);
74 set_opt_dead_method_elimination(1);
75 set_opt_precise_exc_context(0);
76 set_opt_loop_unrolling(0);
77 set_opt_strength_red(0);
78 set_opt_redundant_loadstore(1);
79 set_opt_fragile_ops(0);
80 set_opt_function_call(1);
81 set_opt_optimize_class_casts(0);
82 set_opt_suppress_downcast_optimization(0);
83 set_opt_remove_confirm(1);
84 set_opt_scalar_replacement(1);
85 set_opt_ldst_only_null_ptr_exceptions(1);
86 set_opt_alias_analysis(1);
89 dump_keepalive_edges(1);
92 static void dump(ir_graph *irg, const char *suffix)
94 dump_ir_block_graph(irg, suffix);
97 static void get_output_name(char *buf, size_t buflen, const char *inputname,
100 size_t last_dot = 0xffffffff;
102 for(const char *c = inputname; *c != 0; ++c) {
107 if(last_dot == 0xffffffff)
110 if(last_dot >= buflen)
111 panic("filename too long");
112 memcpy(buf, inputname, last_dot);
114 size_t extlen = strlen(newext) + 1;
115 if(extlen + last_dot >= buflen)
116 panic("filename too long");
117 memcpy(buf+last_dot, newext, extlen);
120 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
122 lexer_open_stream(in, input);
123 translation_unit_t *unit = parse();
127 static void lextest(const char *fname)
129 FILE *in = fopen(fname, "r");
131 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
135 lexer_open_stream(in, fname);
138 lexer_next_preprocessing_token();
139 print_token(stdout, &lexer_token);
141 } while(lexer_token.type != T_EOF);
146 static void backend(const char *inputname, const char *outname)
148 FILE *out = fopen(outname, "w");
150 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
155 be_main(out, inputname);
160 static void emit(const char *input_name, const char *out_name)
162 backend(input_name, out_name);
165 static FILE* preprocess(const char *in)
169 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
173 FILE* f = popen(buf, "r");
175 fprintf(stderr, "invoking preprocessor failed\n");
181 static void link(const char *in, const char *out)
185 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
189 int err = system(buf);
191 fprintf(stderr, "linker reported an error\n");
196 static void assemble(const char *in, const char *out)
200 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
204 int err = system(buf);
206 fprintf(stderr, "assembler reported an error\n");
211 static void create_firm_prog(translation_unit_t *unit)
213 translation_unit_to_firm(unit);
215 //dump_globals_as_text(dump_verbosity_max, "-globals");
217 int n_irgs = get_irp_n_irgs();
218 for(int i = 0; i < n_irgs; ++i) {
219 ir_graph *const irg = get_irp_irg(i);
224 for(int i = 0; i < n_irgs; ++i) {
225 ir_graph *const irg = get_irp_irg(i);
230 static void optimize(void)
233 ir_entity **keep_methods;
235 cgana(&arr_len, &keep_methods);
236 gc_irgs(arr_len, keep_methods);
239 optimize_funccalls(0);
241 for(int i = 0; i < get_irp_n_irgs(); ++i) {
242 ir_graph *irg = get_irp_irg(i);
244 optimize_graph_df(irg);
245 dump(irg, "-localopt");
253 void write_fluffy_decls(translation_unit_t *unit);
255 typedef enum compile_mode_t {
264 static void usage(const char *argv0)
266 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
269 int main(int argc, char **argv)
282 const char *input = NULL;
283 const char *outname = NULL;
284 compile_mode_t mode = CompileAssembleLink;
286 for(int i = 1; i < argc; ++i) {
287 const char *arg = argv[i];
288 if(strcmp(arg, "-o") == 0) {
295 } else if(strcmp(arg, "-c") == 0) {
296 mode = CompileAssemble;
297 } else if(strcmp(arg, "-S") == 0) {
299 } else if(strcmp(arg, "--lextest") == 0) {
301 } else if(strcmp(arg, "--print-ast") == 0) {
303 } else if(strcmp(arg, "--print-fluffy") == 0) {
305 } else if(strcmp(arg, "-v") == 0) {
307 } else if(arg[0] == '-') {
312 fprintf(stderr, "Error: multiple input files specified\n");
322 fprintf(stderr, "%s: no input files\n", argv[0]);
326 if(mode == LexTest) {
331 FILE *const in = preprocess(input);
332 translation_unit_t *const unit = do_parsing(in, input);
337 if(mode == PrintAst) {
341 if(mode == PrintFluffy) {
342 ast_set_output(stdout);
343 write_fluffy_decls(unit);
347 const char *sname = NULL;
348 if(mode == Compile) {
352 get_output_name(outsname, sizeof(outsname), input, ".s");
356 create_firm_prog(unit);
360 if(mode == CompileAssemble) {
362 const char *oname = outname;
364 get_output_name(outoname, sizeof(outoname), input, ".o");
367 assemble(sname, oname);
369 assert(mode == CompileAssembleLink);
374 link(sname, outname);