we need _GNU_SOURCE for popen
[cparser] / main.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <assert.h>
10
11 #ifndef WITH_LIBCORE
12 #define WITH_LIBCORE
13 #endif
14
15 #include <libfirm/firm.h>
16 #include <libfirm/be.h>
17
18 #include "lexer.h"
19 #include "token_t.h"
20 #include "type_hash.h"
21 #include "parser.h"
22 #include "ast2firm.h"
23 #include "adt/error.h"
24
25 #define PREPROCESSOR "cpp"
26 #define LINKER       "gcc"
27
28 static int verbose;
29
30 static const ir_settings_if_conv_t *if_conv_info = NULL;
31
32 static void initialize_firm(void)
33 {
34         be_opt_register();
35         firm_init_options(NULL, 0, NULL);
36
37         const backend_params *be_params;
38         firm_parameter_t params;
39         memset(&params, 0, sizeof(params));
40
41         params.size = sizeof(params);
42         params.enable_statistics = 0;
43         params.initialize_local_func = uninitialized_local_var;
44         params.cc_mask = 0;
45         params.builtin_dbg = NULL;
46
47         /* initialize backend */
48         be_params = be_init();
49         be_set_debug_retrieve(retrieve_dbg);
50         params.arch_op_settings = be_params->arch_op_settings;
51         if_conv_info            = be_params->if_conv_info;
52
53         /* intialize firm itself */
54         init_firm(&params);
55         dbg_init(NULL, NULL, dbg_snprint);
56
57         set_opt_constant_folding(1);
58         set_opt_unreachable_code(1);
59         set_opt_control_flow_straightening(1);
60         set_opt_control_flow_weak_simplification(1);
61         set_opt_control_flow_strong_simplification(1);
62         set_opt_dead_node_elimination(1);
63         set_opt_reassociation(1);
64         set_opt_inline(1);
65         set_opt_dyn_meth_dispatch(1);
66         set_opt_normalize(1);
67         set_opt_tail_recursion(1);
68         set_opt_dead_method_elimination(1);
69         set_opt_precise_exc_context(0);
70         set_opt_loop_unrolling(0);
71         set_opt_strength_red(0);
72         set_opt_redundant_loadstore(1);
73         set_opt_fragile_ops(0);
74         set_opt_function_call(1);
75         set_opt_optimize_class_casts(0);
76         set_opt_suppress_downcast_optimization(0);
77         set_opt_remove_confirm(1);
78         set_opt_scalar_replacement(1);
79         set_opt_ldst_only_null_ptr_exceptions(1);
80         set_opt_alias_analysis(1);
81
82         dump_consts_local(1);
83         dump_keepalive_edges(1);
84 }
85
86 static void dump(ir_graph *irg, const char *suffix)
87 {
88         dump_ir_block_graph(irg, suffix);
89 }
90
91 static void get_output_name(char *buf, size_t buflen, const char *inputname,
92                             const char *newext)
93 {
94         size_t last_dot = 0xffffffff;
95         size_t i = 0;
96         for(const char *c = inputname; *c != 0; ++c) {
97                 if(*c == '.')
98                         last_dot = i;
99                 ++i;
100         }
101         if(last_dot == 0xffffffff)
102                 last_dot = i;
103
104         if(last_dot >= buflen)
105                 panic("filename too long");
106         memcpy(buf, inputname, last_dot);
107
108         size_t extlen = strlen(newext) + 1;
109         if(extlen + last_dot >= buflen)
110                 panic("filename too long");
111         memcpy(buf+last_dot, newext, extlen);
112 }
113
114 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
115 {
116         lexer_open_stream(in, input);
117         translation_unit_t *unit = parse();
118         return unit;
119 }
120
121 static void lextest(const char *fname)
122 {
123         FILE *in = fopen(fname, "r");
124         if(in == NULL) {
125                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
126                 exit(1);
127         }
128
129         lexer_open_stream(in, fname);
130
131         do {
132                 lexer_next_preprocessing_token();
133                 print_token(stdout, &lexer_token);
134                 puts("");
135         } while(lexer_token.type != T_EOF);
136
137         fclose(in);
138 }
139
140 static void backend(const char *inputname, const char *outname)
141 {
142         FILE *out = fopen(outname, "w");
143         if(out == NULL) {
144                 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
145                                 strerror(errno));
146                 exit(1);
147         }
148
149         be_main(out, inputname);
150
151         fclose(out);
152 }
153
154 static void emit(const char *input_name, const char *out_name)
155 {
156         backend(input_name, out_name);
157 }
158
159 static FILE* preprocess(const char *in)
160 {
161         char buf[4096];
162
163         snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
164         if(verbose) {
165                 puts(buf);
166         }
167         FILE* f = popen(buf, "r");
168         if (f == NULL) {
169                 fprintf(stderr, "invoking preprocessor failed\n");
170                 exit(1);
171         }
172         return f;
173 }
174
175 static void link(const char *in, const char *out)
176 {
177         char buf[4096];
178
179         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
180         if(verbose) {
181                 puts(buf);
182         }
183         int err = system(buf);
184         if(err != 0) {
185                 fprintf(stderr, "linker reported an error\n");
186                 exit(1);
187         }
188 }
189
190 static void assemble(const char *in, const char *out)
191 {
192         char buf[4096];
193
194         snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
195         if(verbose) {
196                 puts(buf);
197         }
198         int err = system(buf);
199         if(err != 0) {
200                 fprintf(stderr, "assembler reported an error\n");
201                 exit(1);
202         }
203 }
204
205 static void create_firm_prog(translation_unit_t *unit)
206 {
207         translation_unit_to_firm(unit);
208
209         //dump_globals_as_text(dump_verbosity_max, "-globals");
210
211         int n_irgs = get_irp_n_irgs();
212         for(int i = 0; i < n_irgs; ++i) {
213                 ir_graph *const irg = get_irp_irg(i);
214                 dump(irg, "-start");
215         }
216
217         lower_highlevel();
218         for(int i = 0; i < n_irgs; ++i) {
219                 ir_graph *const irg = get_irp_irg(i);
220                 dump(irg, "-lower");
221         }
222 }
223
224 static void optimize(void)
225 {
226         int         arr_len;
227         ir_entity **keep_methods;
228
229         cgana(&arr_len, &keep_methods);
230         gc_irgs(arr_len, keep_methods);
231         free(keep_methods);
232
233         optimize_funccalls(0);
234
235         for(int i = 0; i < get_irp_n_irgs(); ++i) {
236                 ir_graph *irg = get_irp_irg(i);
237
238                 optimize_graph_df(irg);
239                 dump(irg, "-localopt");
240                 place_code(irg);
241                 dump(irg, "-place");
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         FILE *const in = preprocess(input);
326         translation_unit_t *const unit = do_parsing(in, input);
327         pclose(in);
328         if(unit == NULL)
329                 return 1;
330
331         if(mode == PrintAst) {
332                 print_ast(unit);
333                 return 0;
334         }
335         if(mode == PrintFluffy) {
336                 ast_set_output(stdout);
337                 write_fluffy_decls(unit);
338         }
339
340         char outsname[4096];
341         const char *sname = NULL;
342         if(mode == Compile) {
343                 sname = outname;
344         }
345         if(sname == NULL) {
346                 get_output_name(outsname, sizeof(outsname), input, ".s");
347                 sname = outsname;
348         }
349
350         create_firm_prog(unit);
351         optimize();
352         emit(input, sname);
353
354         if(mode == CompileAssemble) {
355                 char outoname[4096];
356                 const char *oname = outname;
357                 if(oname == NULL) {
358                         get_output_name(outoname, sizeof(outoname), input, ".o");
359                         oname = outoname;
360                 }
361                 assemble(sname, oname);
362         } else {
363                 assert(mode == CompileAssembleLink);
364
365                 if(outname == NULL)
366                         outname = "a.out";
367
368                 link(sname, outname);
369         }
370
371         exit_ast2firm();
372         exit_parser();
373         exit_ast();
374         exit_typehash();
375         exit_types();
376         exit_lexer();
377         exit_tokens();
378         exit_symbol_table();
379         return 0;
380 }