13 #include <libfirm/firm.h>
14 #include <libfirm/be.h>
18 #include "type_hash.h"
21 #include "adt/error.h"
23 #define PREPROCESSOR "gcc -E"
28 static const ir_settings_if_conv_t *if_conv_info = NULL;
30 static void initialize_firm(void)
33 firm_init_options(NULL, 0, NULL);
35 const backend_params *be_params;
36 firm_parameter_t params;
37 memset(¶ms, 0, sizeof(params));
39 params.size = sizeof(params);
40 params.enable_statistics = 0;
41 params.initialize_local_func = uninitialized_local_var;
43 params.builtin_dbg = NULL;
45 /* initialize backend */
46 be_params = be_init();
47 be_set_debug_retrieve(retrieve_dbg);
48 params.arch_op_settings = be_params->arch_op_settings;
49 if_conv_info = be_params->if_conv_info;
51 /* intialize firm itself */
53 dbg_init(NULL, NULL, dbg_snprint);
55 set_opt_constant_folding(1);
56 set_opt_unreachable_code(1);
57 set_opt_control_flow_straightening(1);
58 set_opt_control_flow_weak_simplification(1);
59 set_opt_control_flow_strong_simplification(1);
60 set_opt_dead_node_elimination(1);
61 set_opt_reassociation(1);
63 set_opt_dyn_meth_dispatch(1);
65 set_opt_tail_recursion(1);
66 set_opt_dead_method_elimination(1);
67 set_opt_precise_exc_context(0);
68 set_opt_loop_unrolling(0);
69 set_opt_strength_red(0);
70 set_opt_redundant_loadstore(1);
71 set_opt_fragile_ops(0);
72 set_opt_function_call(1);
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(const char *fname, const char *input)
114 FILE *in = fopen(fname, "r");
116 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
120 lexer_open_stream(in, input);
122 translation_unit_t *unit = parse();
129 static void lextest(const char *fname)
131 FILE *in = fopen(fname, "r");
133 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
137 lexer_open_stream(in, fname);
140 lexer_next_preprocessing_token();
141 print_token(stdout, &lexer_token);
143 } while(lexer_token.type != T_EOF);
148 static void backend(const char *inputname, const char *outname)
150 FILE *out = fopen(outname, "w");
152 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
157 be_main(out, inputname);
162 static void emit(const char *input_name, const char *out_name)
164 backend(input_name, out_name);
167 static void preprocess(const char *in, const char *out)
171 snprintf(buf, sizeof(buf), "%s %s -o %s", PREPROCESSOR, in, out);
175 int err = system(buf);
177 fprintf(stderr, "preprocessor reported an error\n");
182 static void link(const char *in, const char *out)
186 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
190 int err = system(buf);
192 fprintf(stderr, "linker reported an error\n");
197 static void assemble(const char *in, const char *out)
201 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
205 int err = system(buf);
207 fprintf(stderr, "assembler reported an error\n");
212 static void create_firm_prog(translation_unit_t *unit)
214 translation_unit_to_firm(unit);
216 int n_irgs = get_irp_n_irgs();
217 for(int i = 0; i < n_irgs; ++i) {
218 ir_graph *const irg = get_irp_irg(i);
223 static void optimize(void)
226 ir_entity **keep_methods;
228 cgana(&arr_len, &keep_methods);
229 gc_irgs(arr_len, keep_methods);
232 optimize_funccalls(0);
234 for(int i = 0; i < get_irp_n_irgs(); ++i) {
235 ir_graph *irg = get_irp_irg(i);
238 optimize_graph_df(irg);
239 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 const char *tmpfile = tmpnam(NULL);
324 preprocess(input, tmpfile);
326 translation_unit_t *unit = do_parsing(tmpfile, input);
330 if(mode == PrintAst) {
334 if(mode == PrintFluffy) {
335 ast_set_output(stdout);
336 write_fluffy_decls(unit);
340 const char *sname = NULL;
341 if(mode == Compile) {
345 get_output_name(outsname, sizeof(outsname), input, ".s");
349 create_firm_prog(unit);
353 if(mode == CompileAssemble) {
355 const char *oname = outname;
357 get_output_name(outoname, sizeof(outoname), input, ".o");
360 assemble(sname, oname);
362 assert(mode == CompileAssembleLink);
367 link(sname, outname);