fix while statement_to_firm
[cparser] / main.c
1 #include <config.h>
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #ifndef WITH_LIBCORE
9 #define WITH_LIBCORE
10 #endif
11
12 #include <libfirm/firm.h>
13 #include <libfirm/be.h>
14
15 #include "lexer.h"
16 #include "token_t.h"
17 #include "type_hash.h"
18 #include "parser.h"
19 #include "ast2firm.h"
20 #include "adt/error.h"
21
22 #define LINKER "gcc"
23
24 static int verbose;
25
26 static const ir_settings_if_conv_t *if_conv_info = NULL;
27
28 static void initialize_firm(void)
29 {
30         be_opt_register();
31         firm_init_options(NULL, 0, NULL);
32
33         const backend_params *be_params;
34         firm_parameter_t params;
35         memset(&params, 0, sizeof(params));
36
37         params.size = sizeof(params);
38         params.enable_statistics = 0;
39         params.initialize_local_func = uninitialized_local_var;
40         params.cc_mask = 0;
41         params.builtin_dbg = NULL;
42
43         /* initialize backend */
44         be_params = be_init();
45         be_set_debug_retrieve(retrieve_dbg);
46         params.arch_op_settings = be_params->arch_op_settings;
47         if_conv_info            = be_params->if_conv_info;
48
49         /* intialize firm itself */
50         init_firm(&params);
51         dbg_init(NULL, NULL, dbg_snprint);
52
53         set_opt_constant_folding(1);
54         set_opt_unreachable_code(1);
55         set_opt_control_flow_straightening(1);
56         set_opt_control_flow_weak_simplification(1);
57         set_opt_control_flow_strong_simplification(1);
58         set_opt_dead_node_elimination(1);
59         set_opt_reassociation(1);
60         set_opt_inline(1);
61         set_opt_dyn_meth_dispatch(1);
62         set_opt_normalize(1);
63         set_opt_tail_recursion(1);
64         set_opt_dead_method_elimination(1);
65         set_opt_precise_exc_context(0);
66         set_opt_loop_unrolling(0);
67         set_opt_strength_red(0);
68         set_opt_redundant_loadstore(1);
69         set_opt_fragile_ops(0);
70         set_opt_function_call(1);
71         set_opt_optimize_class_casts(0);
72         set_opt_suppress_downcast_optimization(0);
73         set_opt_remove_confirm(1);
74         set_opt_scalar_replacement(1);
75         set_opt_ldst_only_null_ptr_exceptions(1);
76         set_opt_alias_analysis(1);
77
78         dump_consts_local(1);
79 }
80
81 static void dump(const char *suffix)
82 {
83         dump_ir_block_graph(current_ir_graph, suffix);
84 }
85
86 static void get_output_name(char *buf, size_t buflen, const char *inputname,
87                             const char *newext)
88 {
89         size_t last_dot = 0xffffffff;
90         size_t i = 0;
91         for(const char *c = inputname; *c != 0; ++c) {
92                 if(*c == '.')
93                         last_dot = i;
94                 ++i;
95         }
96         if(last_dot == 0xffffffff)
97                 last_dot = i;
98
99         if(last_dot >= buflen)
100                 panic("filename too long");
101         memcpy(buf, inputname, last_dot);
102
103         size_t extlen = strlen(newext) + 1;
104         if(extlen + last_dot >= buflen)
105                 panic("filename too long");
106         memcpy(buf+last_dot, newext, extlen);
107 }
108
109 static translation_unit_t *do_parsing(const char *fname)
110 {
111         FILE *in = fopen(fname, "r");
112         if(in == NULL) {
113                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
114                 exit(1);
115         }
116
117         lexer_open_stream(in, fname);
118
119         translation_unit_t *unit = parse();
120
121         fclose(in);
122
123         return unit;
124 }
125
126 static void lextest(const char *fname)
127 {
128         FILE *in = fopen(fname, "r");
129         if(in == NULL) {
130                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
131                 exit(1);
132         }
133
134         lexer_open_stream(in, fname);
135
136         do {
137                 lexer_next_preprocessing_token();
138                 print_token(stdout, &lexer_token);
139                 puts("");
140         } while(lexer_token.type != T_EOF);
141
142         fclose(in);
143 }
144
145 static void backend(const char *inputname, const char *outname)
146 {
147         FILE *out = fopen(outname, "w");
148         if(out == NULL) {
149                 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
150                                 strerror(errno));
151                 exit(1);
152         }
153
154         be_main(out, inputname);
155
156         fclose(out);
157 }
158
159 static void emit(const char *input_name, const char *out_name)
160 {
161         backend(input_name, out_name);
162 }
163
164 static void link(const char *in, const char *out)
165 {
166         char buf[4096];
167
168         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
169         if(verbose) {
170                 puts(buf);
171         }
172         int err = system(buf);
173         if(err != 0) {
174                 fprintf(stderr, "linker reported an error\n");
175                 exit(1);
176         }
177 }
178
179
180
181 static void create_firm_prog(translation_unit_t *unit)
182 {
183         translation_unit_to_firm(unit);
184
185         int n_irgs = get_irp_n_irgs();
186         for(int i = 0; i < n_irgs; ++i) {
187                 current_ir_graph = get_irp_irg(i);
188                 dump("-start");
189         }
190 }
191
192 void write_fluffy_decls(translation_unit_t *unit);
193
194 int main(int argc, char **argv)
195 {
196         initialize_firm();
197
198         init_symbol_table();
199         init_tokens();
200         init_lexer();
201         init_types();
202         init_typehash();
203         init_ast();
204         init_parser();
205         init_ast2firm();
206
207         if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
208                 lextest(argv[2]);
209                 return 0;
210         }
211
212         if(argc > 2 && strcmp(argv[1], "--print-ast") == 0) {
213                 translation_unit_t *unit = do_parsing(argv[2]);
214                 ast_set_output(stdout);
215                 print_ast(unit);
216                 return 0;
217         }
218
219         if(argc > 2 && strcmp(argv[1], "--print-fluffy") == 0) {
220                 translation_unit_t *unit = do_parsing(argv[2]);
221                 ast_set_output(stdout);
222                 write_fluffy_decls(unit);
223                 return 0;
224         }
225
226         for(int i = 1; i < argc; ++i) {
227                 const char *input = argv[i];
228                 char        outfname[4096];
229
230                 get_output_name(outfname, sizeof(outfname), input, ".s");
231
232                 translation_unit_t *unit = do_parsing(input);
233                 create_firm_prog(unit);
234                 emit(input, outfname);
235                 link(outfname, "a.out");
236         }
237
238         exit_ast2firm();
239         exit_parser();
240         exit_ast();
241         exit_typehash();
242         exit_types();
243         exit_lexer();
244         exit_tokens();
245         exit_symbol_table();
246         return 0;
247 }