Do not set options which do not exist anymore.
[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 #ifdef _WIN32
29 /* remap some names */
30 #define popen(cmd, mode)  _popen(cmd, mode)
31 #define pclose(file)      _pclose(file)
32 #endif /* _WIN32 */
33
34 static int verbose;
35
36 static const ir_settings_if_conv_t *if_conv_info = NULL;
37
38 static void initialize_firm(void)
39 {
40         be_opt_register();
41         firm_init_options(NULL, 0, NULL);
42
43         const backend_params *be_params;
44         firm_parameter_t params;
45         memset(&params, 0, sizeof(params));
46
47         params.size = sizeof(params);
48         params.enable_statistics = 0;
49         params.initialize_local_func = uninitialized_local_var;
50         params.cc_mask = 0;
51         params.builtin_dbg = NULL;
52
53         /* initialize backend */
54         be_params = be_init();
55         be_set_debug_retrieve(retrieve_dbg);
56         params.arch_op_settings = be_params->arch_op_settings;
57         if_conv_info            = be_params->if_conv_info;
58
59         /* intialize firm itself */
60         init_firm(&params);
61         dbg_init(NULL, NULL, dbg_snprint);
62
63         set_opt_constant_folding(1);
64         set_opt_unreachable_code(1);
65         set_opt_control_flow_straightening(1);
66         set_opt_control_flow_weak_simplification(1);
67         set_opt_control_flow_strong_simplification(1);
68         set_opt_dyn_meth_dispatch(1);
69         set_opt_normalize(1);
70         set_opt_precise_exc_context(0);
71         set_opt_strength_red(0);
72         set_opt_fragile_ops(0);
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(FILE *const in, const char *const input)
113 {
114         lexer_open_stream(in, input);
115         translation_unit_t *unit = parse();
116         return unit;
117 }
118
119 static void lextest(const char *fname)
120 {
121         FILE *in = fopen(fname, "r");
122         if(in == NULL) {
123                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
124                 exit(1);
125         }
126
127         lexer_open_stream(in, fname);
128
129         do {
130                 lexer_next_preprocessing_token();
131                 print_token(stdout, &lexer_token);
132                 puts("");
133         } while(lexer_token.type != T_EOF);
134
135         fclose(in);
136 }
137
138 static void backend(const char *inputname, const char *outname)
139 {
140         FILE *out = fopen(outname, "w");
141         if(out == NULL) {
142                 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
143                                 strerror(errno));
144                 exit(1);
145         }
146
147         be_main(out, inputname);
148
149         fclose(out);
150 }
151
152 static void emit(const char *input_name, const char *out_name)
153 {
154         backend(input_name, out_name);
155 }
156
157 static FILE* preprocess(const char *in)
158 {
159         char buf[4096];
160
161         snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
162         if(verbose) {
163                 puts(buf);
164         }
165         FILE* f = popen(buf, "r");
166         if (f == NULL) {
167                 fprintf(stderr, "invoking preprocessor failed\n");
168                 exit(1);
169         }
170         return f;
171 }
172
173 static void link(const char *in, const char *out)
174 {
175         char buf[4096];
176
177         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
178         if(verbose) {
179                 puts(buf);
180         }
181         int err = system(buf);
182         if(err != 0) {
183                 fprintf(stderr, "linker reported an error\n");
184                 exit(1);
185         }
186 }
187
188 static void assemble(const char *in, const char *out)
189 {
190         char buf[4096];
191
192         snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
193         if(verbose) {
194                 puts(buf);
195         }
196         int err = system(buf);
197         if(err != 0) {
198                 fprintf(stderr, "assembler reported an error\n");
199                 exit(1);
200         }
201 }
202
203 static void create_firm_prog(translation_unit_t *unit)
204 {
205         translation_unit_to_firm(unit);
206
207         //dump_globals_as_text(dump_verbosity_max, "-globals");
208
209         int n_irgs = get_irp_n_irgs();
210         for(int i = 0; i < n_irgs; ++i) {
211                 ir_graph *const irg = get_irp_irg(i);
212                 dump(irg, "-start");
213         }
214
215         lower_highlevel();
216         for(int i = 0; i < n_irgs; ++i) {
217                 ir_graph *const irg = get_irp_irg(i);
218                 dump(irg, "-lower");
219         }
220 }
221
222 static void optimize(void)
223 {
224         int         arr_len;
225         ir_entity **keep_methods;
226
227         cgana(&arr_len, &keep_methods);
228         gc_irgs(arr_len, keep_methods);
229         free(keep_methods);
230
231         optimize_funccalls(0);
232
233         for(int i = 0; i < get_irp_n_irgs(); ++i) {
234                 ir_graph *irg = get_irp_irg(i);
235
236                 optimize_graph_df(irg);
237                 dump(irg, "-localopt");
238                 place_code(irg);
239                 dump(irg, "-place");
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         FILE *const in = preprocess(input);
324         translation_unit_t *const unit = do_parsing(in, input);
325         pclose(in);
326         if(unit == NULL)
327                 return 1;
328
329         if(mode == PrintAst) {
330                 print_ast(unit);
331                 return 0;
332         }
333         if(mode == PrintFluffy) {
334                 ast_set_output(stdout);
335                 write_fluffy_decls(unit);
336         }
337
338         char outsname[4096];
339         const char *sname = NULL;
340         if(mode == Compile) {
341                 sname = outname;
342         }
343         if(sname == NULL) {
344                 get_output_name(outsname, sizeof(outsname), input, ".s");
345                 sname = outsname;
346         }
347
348         create_firm_prog(unit);
349         optimize();
350         emit(input, sname);
351
352         if(mode == CompileAssemble) {
353                 char outoname[4096];
354                 const char *oname = outname;
355                 if(oname == NULL) {
356                         get_output_name(outoname, sizeof(outoname), input, ".o");
357                         oname = outoname;
358                 }
359                 assemble(sname, oname);
360         } else {
361                 assert(mode == CompileAssembleLink);
362
363                 if(outname == NULL)
364                         outname = "a.out";
365
366                 link(sname, outname);
367         }
368
369         exit_ast2firm();
370         exit_parser();
371         exit_ast();
372         exit_typehash();
373         exit_types();
374         exit_lexer();
375         exit_tokens();
376         exit_symbol_table();
377         return 0;
378 }