implemented/fixed EXPR_SEL
[cparser] / main.c
1 #include <config.h>
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <assert.h>
8
9 #ifndef WITH_LIBCORE
10 #define WITH_LIBCORE
11 #endif
12
13 #include <libfirm/firm.h>
14 #include <libfirm/be.h>
15
16 #include "lexer.h"
17 #include "token_t.h"
18 #include "type_hash.h"
19 #include "parser.h"
20 #include "ast2firm.h"
21 #include "adt/error.h"
22
23 #define PREPROCESSOR "gcc -E"
24 #define LINKER       "gcc"
25
26 static int verbose;
27
28 static const ir_settings_if_conv_t *if_conv_info = NULL;
29
30 static void initialize_firm(void)
31 {
32         be_opt_register();
33         firm_init_options(NULL, 0, NULL);
34
35         const backend_params *be_params;
36         firm_parameter_t params;
37         memset(&params, 0, sizeof(params));
38
39         params.size = sizeof(params);
40         params.enable_statistics = 0;
41         params.initialize_local_func = uninitialized_local_var;
42         params.cc_mask = 0;
43         params.builtin_dbg = NULL;
44
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;
50
51         /* intialize firm itself */
52         init_firm(&params);
53         dbg_init(NULL, NULL, dbg_snprint);
54
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);
62         set_opt_inline(1);
63         set_opt_dyn_meth_dispatch(1);
64         set_opt_normalize(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);
79
80         dump_consts_local(1);
81         dump_keepalive_edges(1);
82 }
83
84 static void dump(ir_graph *irg, const char *suffix)
85 {
86         dump_ir_block_graph(irg, suffix);
87 }
88
89 static void get_output_name(char *buf, size_t buflen, const char *inputname,
90                             const char *newext)
91 {
92         size_t last_dot = 0xffffffff;
93         size_t i = 0;
94         for(const char *c = inputname; *c != 0; ++c) {
95                 if(*c == '.')
96                         last_dot = i;
97                 ++i;
98         }
99         if(last_dot == 0xffffffff)
100                 last_dot = i;
101
102         if(last_dot >= buflen)
103                 panic("filename too long");
104         memcpy(buf, inputname, last_dot);
105
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);
110 }
111
112 static translation_unit_t *do_parsing(const char *fname, const char *input)
113 {
114         FILE *in = fopen(fname, "r");
115         if(in == NULL) {
116                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
117                 exit(1);
118         }
119
120         lexer_open_stream(in, input);
121
122         translation_unit_t *unit = parse();
123
124         fclose(in);
125
126         return unit;
127 }
128
129 static void lextest(const char *fname)
130 {
131         FILE *in = fopen(fname, "r");
132         if(in == NULL) {
133                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
134                 exit(1);
135         }
136
137         lexer_open_stream(in, fname);
138
139         do {
140                 lexer_next_preprocessing_token();
141                 print_token(stdout, &lexer_token);
142                 puts("");
143         } while(lexer_token.type != T_EOF);
144
145         fclose(in);
146 }
147
148 static void backend(const char *inputname, const char *outname)
149 {
150         FILE *out = fopen(outname, "w");
151         if(out == NULL) {
152                 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
153                                 strerror(errno));
154                 exit(1);
155         }
156
157         be_main(out, inputname);
158
159         fclose(out);
160 }
161
162 static void emit(const char *input_name, const char *out_name)
163 {
164         backend(input_name, out_name);
165 }
166
167 static void preprocess(const char *in, const char *out)
168 {
169         char buf[4096];
170
171         snprintf(buf, sizeof(buf), "%s %s -o %s", PREPROCESSOR, in, out);
172         if(verbose) {
173                 puts(buf);
174         }
175         int err = system(buf);
176         if(err != 0) {
177                 fprintf(stderr, "preprocessor reported an error\n");
178                 exit(1);
179         }
180 }
181
182 static void link(const char *in, const char *out)
183 {
184         char buf[4096];
185
186         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
187         if(verbose) {
188                 puts(buf);
189         }
190         int err = system(buf);
191         if(err != 0) {
192                 fprintf(stderr, "linker reported an error\n");
193                 exit(1);
194         }
195 }
196
197 static void assemble(const char *in, const char *out)
198 {
199         char buf[4096];
200
201         snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
202         if(verbose) {
203                 puts(buf);
204         }
205         int err = system(buf);
206         if(err != 0) {
207                 fprintf(stderr, "assembler reported an error\n");
208                 exit(1);
209         }
210 }
211
212 static void create_firm_prog(translation_unit_t *unit)
213 {
214         translation_unit_to_firm(unit);
215
216         //dump_globals_as_text(dump_verbosity_max, "-globals");
217
218         int n_irgs = get_irp_n_irgs();
219         for(int i = 0; i < n_irgs; ++i) {
220                 ir_graph *const irg = get_irp_irg(i);
221                 dump(irg, "-start");
222         }
223
224         lower_highlevel();
225         for(int i = 0; i < n_irgs; ++i) {
226                 ir_graph *const irg = get_irp_irg(i);
227                 dump(irg, "-lower");
228         }
229 }
230
231 static void optimize(void)
232 {
233         int         arr_len;
234         ir_entity **keep_methods;
235
236         cgana(&arr_len, &keep_methods);
237         gc_irgs(arr_len, keep_methods);
238         free(keep_methods);
239
240         optimize_funccalls(0);
241
242         for(int i = 0; i < get_irp_n_irgs(); ++i) {
243                 ir_graph *irg = get_irp_irg(i);
244
245                 optimize_graph_df(irg);
246                 dump(irg, "-localopt");
247                 place_code(irg);
248                 dump(irg, "-place");
249                 optimize_cf(irg);
250                 dump(irg, "-cf");
251         }
252 }
253
254 void write_fluffy_decls(translation_unit_t *unit);
255
256 typedef enum compile_mode_t {
257         Compile,
258         CompileAssemble,
259         CompileAssembleLink,
260         LexTest,
261         PrintAst,
262         PrintFluffy
263 } compile_mode_t;
264
265 static void usage(const char *argv0)
266 {
267         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
268 }
269
270 int main(int argc, char **argv)
271 {
272         initialize_firm();
273
274         init_symbol_table();
275         init_tokens();
276         init_lexer();
277         init_types();
278         init_typehash();
279         init_ast();
280         init_parser();
281         init_ast2firm();
282
283         const char *input   = NULL;
284         const char *outname = NULL;
285         compile_mode_t mode = CompileAssembleLink;
286
287         for(int i = 1; i < argc; ++i) {
288                 const char *arg = argv[i];
289                 if(strcmp(arg, "-o") == 0) {
290                         ++i;
291                         if(i >= argc) {
292                                 usage(argv[0]);
293                                 return 1;
294                         }
295                         outname = argv[i];
296                 } else if(strcmp(arg, "-c") == 0) {
297                         mode = CompileAssemble;
298                 } else if(strcmp(arg, "-S") == 0) {
299                         mode = Compile;
300                 } else if(strcmp(arg, "--lextest") == 0) {
301                         mode = LexTest;
302                 } else if(strcmp(arg, "--print-ast") == 0) {
303                         mode = PrintAst;
304                 } else if(strcmp(arg, "--print-fluffy") == 0) {
305                         mode = PrintFluffy;
306                 } else if(strcmp(arg, "-v") == 0) {
307                         verbose = 1;
308                 } else if(arg[0] == '-') {
309                         usage(argv[0]);
310                         return 1;
311                 } else {
312                         if(input != NULL) {
313                                 fprintf(stderr, "Error: multiple input files specified\n");
314                                 usage(argv[0]);
315                                 return 1;
316                         } else {
317                                 input = arg;
318                         }
319                 }
320         }
321
322         if(input == NULL) {
323                 fprintf(stderr, "%s: no input files\n", argv[0]);
324                 return 1;
325         }
326
327         if(mode == LexTest) {
328                 lextest(input);
329                 return 0;
330         }
331
332         const char *tmpfile = tmpnam(NULL);
333         preprocess(input, tmpfile);
334
335         translation_unit_t *unit = do_parsing(tmpfile, input);
336         if(unit == NULL)
337                 return 1;
338
339         if(mode == PrintAst) {
340                 print_ast(unit);
341                 return 0;
342         }
343         if(mode == PrintFluffy) {
344                 ast_set_output(stdout);
345                 write_fluffy_decls(unit);
346         }
347
348         char outsname[4096];
349         const char *sname = NULL;
350         if(mode == Compile) {
351                 sname = outname;
352         }
353         if(sname == NULL) {
354                 get_output_name(outsname, sizeof(outsname), input, ".s");
355                 sname = outsname;
356         }
357
358         create_firm_prog(unit);
359         optimize();
360         emit(input, sname);
361
362         if(mode == CompileAssemble) {
363                 char outoname[4096];
364                 const char *oname = outname;
365                 if(oname == NULL) {
366                         get_output_name(outoname, sizeof(outoname), input, ".o");
367                         oname = outoname;
368                 }
369                 assemble(sname, oname);
370         } else {
371                 assert(mode == CompileAssembleLink);
372
373                 if(outname == NULL)
374                         outname = "a.out";
375
376                 link(sname, outname);
377         }
378
379         exit_ast2firm();
380         exit_parser();
381         exit_ast();
382         exit_typehash();
383         exit_types();
384         exit_lexer();
385         exit_tokens();
386         exit_symbol_table();
387         return 0;
388 }