inline is not a type qualifier anymore, fix function entity creation
[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
225 static void optimize(void)
226 {
227         int         arr_len;
228         ir_entity **keep_methods;
229
230         cgana(&arr_len, &keep_methods);
231         gc_irgs(arr_len, keep_methods);
232         free(keep_methods);
233
234         optimize_funccalls(0);
235
236         for(int i = 0; i < get_irp_n_irgs(); ++i) {
237                 ir_graph *irg = get_irp_irg(i);
238                 place_code(irg);
239                 dump(irg, "-place");
240                 optimize_graph_df(irg);
241                 dump(irg, "-localopt");
242                 optimize_cf(irg);
243                 dump(irg, "-cf");
244         }
245 }
246
247 void write_fluffy_decls(translation_unit_t *unit);
248
249 typedef enum compile_mode_t {
250         Compile,
251         CompileAssemble,
252         CompileAssembleLink,
253         LexTest,
254         PrintAst,
255         PrintFluffy
256 } compile_mode_t;
257
258 static void usage(const char *argv0)
259 {
260         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
261 }
262
263 int main(int argc, char **argv)
264 {
265         initialize_firm();
266
267         init_symbol_table();
268         init_tokens();
269         init_lexer();
270         init_types();
271         init_typehash();
272         init_ast();
273         init_parser();
274         init_ast2firm();
275
276         const char *input   = NULL;
277         const char *outname = NULL;
278         compile_mode_t mode = CompileAssembleLink;
279
280         for(int i = 1; i < argc; ++i) {
281                 const char *arg = argv[i];
282                 if(strcmp(arg, "-o") == 0) {
283                         ++i;
284                         if(i >= argc) {
285                                 usage(argv[0]);
286                                 return 1;
287                         }
288                         outname = argv[i];
289                 } else if(strcmp(arg, "-c") == 0) {
290                         mode = CompileAssemble;
291                 } else if(strcmp(arg, "-S") == 0) {
292                         mode = Compile;
293                 } else if(strcmp(arg, "--lextest") == 0) {
294                         mode = LexTest;
295                 } else if(strcmp(arg, "--print-ast") == 0) {
296                         mode = PrintAst;
297                 } else if(strcmp(arg, "--print-fluffy") == 0) {
298                         mode = PrintFluffy;
299                 } else if(strcmp(arg, "-v") == 0) {
300                         verbose = 1;
301                 } else if(arg[0] == '-') {
302                         usage(argv[0]);
303                         return 1;
304                 } else {
305                         if(input != NULL) {
306                                 fprintf(stderr, "Error: multiple input files specified\n");
307                                 usage(argv[0]);
308                                 return 1;
309                         } else {
310                                 input = arg;
311                         }
312                 }
313         }
314
315         if(input == NULL) {
316                 fprintf(stderr, "%s: no input files\n", argv[0]);
317                 return 1;
318         }
319
320         if(mode == LexTest) {
321                 lextest(input);
322                 return 0;
323         }
324
325         const char *tmpfile = tmpnam(NULL);
326         preprocess(input, tmpfile);
327
328         translation_unit_t *unit = do_parsing(tmpfile, input);
329         if(unit == NULL)
330                 return 1;
331
332         if(mode == PrintAst) {
333                 print_ast(unit);
334                 return 0;
335         }
336         if(mode == PrintFluffy) {
337                 ast_set_output(stdout);
338                 write_fluffy_decls(unit);
339         }
340
341         char outsname[4096];
342         const char *sname = NULL;
343         if(mode == Compile) {
344                 sname = outname;
345         }
346         if(sname == NULL) {
347                 get_output_name(outsname, sizeof(outsname), input, ".s");
348                 sname = outsname;
349         }
350
351         create_firm_prog(unit);
352         optimize();
353         emit(input, sname);
354
355         if(mode == CompileAssemble) {
356                 char outoname[4096];
357                 const char *oname = outname;
358                 if(oname == NULL) {
359                         get_output_name(outoname, sizeof(outoname), input, ".o");
360                         oname = outoname;
361                 }
362                 assemble(sname, oname);
363         } else {
364                 assert(mode == CompileAssembleLink);
365
366                 if(outname == NULL)
367                         outname = "a.out";
368
369                 link(sname, outname);
370         }
371
372         exit_ast2firm();
373         exit_parser();
374         exit_ast();
375         exit_typehash();
376         exit_types();
377         exit_lexer();
378         exit_tokens();
379         exit_symbol_table();
380         return 0;
381 }