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