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);
99 static void get_output_name(char *buf, size_t buflen, const char *inputname,
102 size_t last_dot = 0xffffffff;
104 for(const char *c = inputname; *c != 0; ++c) {
109 if(last_dot == 0xffffffff)
112 if(last_dot >= buflen)
113 panic("filename too long");
114 memcpy(buf, inputname, last_dot);
116 size_t extlen = strlen(newext) + 1;
117 if(extlen + last_dot >= buflen)
118 panic("filename too long");
119 memcpy(buf+last_dot, newext, extlen);
122 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
124 lexer_open_stream(in, input);
125 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 FILE* preprocess(const char *in)
171 snprintf(buf, sizeof(buf), PREPROCESSOR " %s", in);
176 FILE* f = popen(buf, "r");
178 fprintf(stderr, "invoking preprocessor failed\n");
184 static void link(const char *in, const char *out)
188 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
192 int err = system(buf);
194 fprintf(stderr, "linker reported an error\n");
199 static void assemble(const char *in, const char *out)
203 snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
207 int err = system(buf);
209 fprintf(stderr, "assembler reported an error\n");
214 static void create_firm_prog(translation_unit_t *unit)
216 translation_unit_to_firm(unit);
218 //dump_globals_as_text(dump_verbosity_max, "-globals");
220 int n_irgs = get_irp_n_irgs();
221 for(int i = 0; i < n_irgs; ++i) {
222 ir_graph *const irg = get_irp_irg(i);
227 for(int i = 0; i < n_irgs; ++i) {
228 ir_graph *const irg = get_irp_irg(i);
233 static void optimize(void)
236 ir_entity **keep_methods;
238 cgana(&arr_len, &keep_methods);
239 gc_irgs(arr_len, keep_methods);
242 optimize_funccalls(0);
244 lwrdw_param_t lwrdw_param = {
249 def_create_intrinsic_fkt,
252 if (be_params->arch_create_intrinsic_fkt) {
253 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
254 lwrdw_param.ctx = be_params->create_intrinsic_ctx;
257 for(int i = 0; i < get_irp_n_irgs(); ++i) {
258 ir_graph *irg = get_irp_irg(i);
260 optimize_graph_df(irg);
261 dump(irg, "-01-localopt");
263 dump(irg, "-02-place");
266 lower_dw_ops(&lwrdw_param);
268 optimize_graph_df(irg);
269 dump(irg, "-05-localopt");
275 void write_fluffy_decls(translation_unit_t *unit);
277 typedef enum compile_mode_t {
286 static void usage(const char *argv0)
288 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
291 int main(int argc, char **argv)
304 const char *input = NULL;
305 const char *outname = NULL;
306 compile_mode_t mode = CompileAssembleLink;
308 for(int i = 1; i < argc; ++i) {
309 const char *arg = argv[i];
310 if(strcmp(arg, "-o") == 0) {
317 } else if(strcmp(arg, "-c") == 0) {
318 mode = CompileAssemble;
319 } else if(strcmp(arg, "-S") == 0) {
321 } else if(strcmp(arg, "--lextest") == 0) {
323 } else if(strcmp(arg, "--print-ast") == 0) {
325 } else if(strcmp(arg, "--print-fluffy") == 0) {
327 } else if(strcmp(arg, "-v") == 0) {
329 } else if(arg[0] == '-' && arg[1] == 'f') {
330 const char *opt = &arg[2];
344 } else if(arg[0] == '-' && arg[1] == 'b') {
345 const char *opt = &arg[2];
358 //firm_be_option(opt);
359 } else if(arg[0] == '-') {
363 strncmp(arg + 1, "std=", 4) == 0) {
364 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
371 fprintf(stderr, "Error: multiple input files specified\n");
381 fprintf(stderr, "%s: no input files\n", argv[0]);
385 if(mode == LexTest) {
390 FILE *const in = preprocess(input);
391 translation_unit_t *const unit = do_parsing(in, input);
396 if(mode == PrintAst) {
400 if(mode == PrintFluffy) {
401 ast_set_output(stdout);
402 write_fluffy_decls(unit);
406 const char *sname = NULL;
407 if(mode == Compile) {
411 get_output_name(outsname, sizeof(outsname), input, ".s");
415 create_firm_prog(unit);
419 if(mode == CompileAssemble) {
421 const char *oname = outname;
423 get_output_name(outoname, sizeof(outoname), input, ".o");
426 assemble(sname, oname);
428 assert(mode == CompileAssembleLink);
433 link(sname, outname);