more asserts
[cparser] / main.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <assert.h>
11
12 #ifndef WITH_LIBCORE
13 #define WITH_LIBCORE
14 #endif
15
16 #include <libfirm/firm.h>
17 #include <libfirm/be.h>
18
19 #include "lexer.h"
20 #include "token_t.h"
21 #include "type_hash.h"
22 #include "parser.h"
23 #include "ast2firm.h"
24 #include "adt/error.h"
25
26 #ifndef PREPROCESSOR
27 #define PREPROCESSOR "cpp"
28 #endif
29
30 #ifndef LINKER
31 #define LINKER       "gcc"
32 #endif
33
34 #ifdef _WIN32
35 /* remap some names */
36 #define popen(cmd, mode)  _popen(cmd, mode)
37 #define pclose(file)      _pclose(file)
38 #endif /* _WIN32 */
39
40 static int  verbose;
41 static bool do_dump;
42
43 static const ir_settings_if_conv_t *if_conv_info = NULL;
44 static const backend_params        *be_params    = NULL;
45
46 static void initialize_firm(void)
47 {
48         be_opt_register();
49         firm_init_options(NULL, 0, NULL);
50
51         firm_parameter_t params;
52         memset(&params, 0, sizeof(params));
53
54         params.size = sizeof(params);
55         params.enable_statistics = 0;
56         params.initialize_local_func = uninitialized_local_var;
57         params.cc_mask = 0;
58         params.builtin_dbg = NULL;
59
60         /* initialize backend */
61         be_params = be_init();
62         ir_set_debug_retrieve(retrieve_dbg);
63         params.arch_op_settings = be_params->arch_op_settings;
64         if_conv_info            = be_params->if_conv_info;
65
66         /* intialize firm itself */
67         init_firm(&params);
68         dbg_init(NULL, NULL, dbg_snprint);
69
70         set_opt_constant_folding(1);
71         set_opt_unreachable_code(1);
72         set_opt_control_flow_straightening(1);
73         set_opt_control_flow_weak_simplification(1);
74         set_opt_control_flow_strong_simplification(1);
75         set_opt_dyn_meth_dispatch(1);
76         set_opt_normalize(1);
77         set_opt_precise_exc_context(0);
78         set_opt_strength_red(0);
79         set_opt_fragile_ops(0);
80         set_opt_optimize_class_casts(0);
81         set_opt_suppress_downcast_optimization(0);
82         set_opt_remove_confirm(1);
83         set_opt_scalar_replacement(1);
84         set_opt_ldst_only_null_ptr_exceptions(1);
85         set_opt_alias_analysis(1);
86
87         dump_consts_local(1);
88         dump_keepalive_edges(1);
89 }
90
91 static void dump(ir_graph *irg, const char *suffix)
92 {
93         if(do_dump) {
94                 dump_ir_block_graph(irg, suffix);
95         }
96 }
97
98 static void get_output_name(char *buf, size_t buflen, const char *inputname,
99                             const char *newext)
100 {
101         size_t last_dot = 0xffffffff;
102         size_t i = 0;
103         for(const char *c = inputname; *c != 0; ++c) {
104                 if(*c == '.')
105                         last_dot = i;
106                 ++i;
107         }
108         if(last_dot == 0xffffffff)
109                 last_dot = i;
110
111         if(last_dot >= buflen)
112                 panic("filename too long");
113         memcpy(buf, inputname, last_dot);
114
115         size_t extlen = strlen(newext) + 1;
116         if(extlen + last_dot >= buflen)
117                 panic("filename too long");
118         memcpy(buf+last_dot, newext, extlen);
119 }
120
121 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
122 {
123         lexer_open_stream(in, input);
124         translation_unit_t *unit = parse();
125         return unit;
126 }
127
128 static void lextest(const char *fname)
129 {
130         FILE *in = fopen(fname, "r");
131         if(in == NULL) {
132                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
133                 exit(1);
134         }
135
136         lexer_open_stream(in, fname);
137
138         do {
139                 lexer_next_preprocessing_token();
140                 print_token(stdout, &lexer_token);
141                 puts("");
142         } while(lexer_token.type != T_EOF);
143
144         fclose(in);
145 }
146
147 static void backend(const char *inputname, const char *outname)
148 {
149         FILE *out = fopen(outname, "w");
150         if(out == NULL) {
151                 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
152                                 strerror(errno));
153                 exit(1);
154         }
155
156         be_main(out, inputname);
157
158         fclose(out);
159 }
160
161 static void emit(const char *input_name, const char *out_name)
162 {
163         backend(input_name, out_name);
164 }
165
166 static FILE* preprocess(const char *in)
167 {
168         char buf[4096];
169
170         snprintf(buf, sizeof(buf), PREPROCESSOR " %s", in);
171
172         if(verbose) {
173                 puts(buf);
174         }
175         FILE* f = popen(buf, "r");
176         if (f == NULL) {
177                 fprintf(stderr, "invoking preprocessor failed\n");
178                 exit(1);
179         }
180         return f;
181 }
182
183 static void link(const char *in, const char *out)
184 {
185         char buf[4096];
186
187         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
188         if(verbose) {
189                 puts(buf);
190         }
191         int err = system(buf);
192         if(err != 0) {
193                 fprintf(stderr, "linker reported an error\n");
194                 exit(1);
195         }
196 }
197
198 static void assemble(const char *in, const char *out)
199 {
200         char buf[4096];
201
202         snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
203         if(verbose) {
204                 puts(buf);
205         }
206         int err = system(buf);
207         if(err != 0) {
208                 fprintf(stderr, "assembler reported an error\n");
209                 exit(1);
210         }
211 }
212
213 static void create_firm_prog(translation_unit_t *unit)
214 {
215         translation_unit_to_firm(unit);
216
217         //dump_globals_as_text(dump_verbosity_max, "-globals");
218
219         int n_irgs = get_irp_n_irgs();
220         for(int i = 0; i < n_irgs; ++i) {
221                 ir_graph *const irg = get_irp_irg(i);
222                 dump(irg, "-start");
223         }
224
225         lower_highlevel();
226         for(int i = 0; i < n_irgs; ++i) {
227                 ir_graph *const irg = get_irp_irg(i);
228                 dump(irg, "-lower");
229         }
230 }
231
232 static void optimize(void)
233 {
234         int         arr_len;
235         ir_entity **keep_methods;
236
237         cgana(&arr_len, &keep_methods);
238         gc_irgs(arr_len, keep_methods);
239         free(keep_methods);
240
241         optimize_funccalls(0);
242
243         lwrdw_param_t lwrdw_param = {
244                 1,
245                 1,
246                 mode_Ls, mode_Lu,
247                 mode_Is, mode_Iu,
248                 def_create_intrinsic_fkt,
249                 NULL
250         };
251         if (be_params->arch_create_intrinsic_fkt) {
252                 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
253                 lwrdw_param.ctx              = be_params->create_intrinsic_ctx;
254         }
255
256         for(int i = 0; i < get_irp_n_irgs(); ++i) {
257                 ir_graph *irg = get_irp_irg(i);
258
259                 optimize_graph_df(irg);
260                 dump(irg, "-01-localopt");
261                 place_code(irg);
262                 dump(irg, "-02-place");
263                 optimize_cf(irg);
264                 dump(irg, "-03-cf");
265                 lower_dw_ops(&lwrdw_param);
266                 dump(irg, "-04-dw");
267                 optimize_graph_df(irg);
268                 dump(irg, "-05-localopt");
269                 optimize_cf(irg);
270                 dump(irg, "-06-cf");
271         }
272 }
273
274 void write_fluffy_decls(translation_unit_t *unit);
275
276 typedef enum compile_mode_t {
277         Compile,
278         CompileAssemble,
279         CompileAssembleLink,
280         LexTest,
281         PrintAst,
282         PrintFluffy
283 } compile_mode_t;
284
285 static void usage(const char *argv0)
286 {
287         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
288 }
289
290 int main(int argc, char **argv)
291 {
292         initialize_firm();
293
294         init_symbol_table();
295         init_tokens();
296         init_types();
297         init_typehash();
298         init_lexer();
299         init_ast();
300         init_parser();
301         init_ast2firm();
302
303         const char *input   = NULL;
304         const char *outname = NULL;
305         compile_mode_t mode = CompileAssembleLink;
306
307         for(int i = 1; i < argc; ++i) {
308                 const char *arg = argv[i];
309                 if(strcmp(arg, "-o") == 0) {
310                         ++i;
311                         if(i >= argc) {
312                                 usage(argv[0]);
313                                 return 1;
314                         }
315                         outname = argv[i];
316                 } else if(strcmp(arg, "-c") == 0) {
317                         mode = CompileAssemble;
318                 } else if(strcmp(arg, "-S") == 0) {
319                         mode = Compile;
320                 } else if(strcmp(arg, "--lextest") == 0) {
321                         mode = LexTest;
322                 } else if(strcmp(arg, "--print-ast") == 0) {
323                         mode = PrintAst;
324                 } else if(strcmp(arg, "--print-fluffy") == 0) {
325                         mode = PrintFluffy;
326                 } else if(strcmp(arg, "--dump") == 0) {
327                         do_dump = true;
328                 } else if(strcmp(arg, "-v") == 0) {
329                         verbose = 1;
330                 } else if(arg[0] == '-' && arg[1] == 'f') {
331                         const char *opt = &arg[2];
332                         if(opt[0] == 0) {
333                                 ++i;
334                                 if(i >= argc) {
335                                         usage(argv[0]);
336                                         return 1;
337                                 }
338                                 opt = argv[i];
339                                 if(opt[0] == '-') {
340                                         usage(argv[0]);
341                                         return 1;
342                                 }
343                         }
344                         //firm_option(opt);
345                 } else if(arg[0] == '-' && arg[1] == 'b') {
346                         const char *opt = &arg[2];
347                         if(opt[0] == 0) {
348                                 ++i;
349                                 if(i >= argc) {
350                                         usage(argv[0]);
351                                         return 1;
352                                 }
353                                 opt = argv[i];
354                                 if(opt[0] == '-') {
355                                         usage(argv[0]);
356                                         return 1;
357                                 }
358                         }
359                         //firm_be_option(opt);
360                 } else if(arg[0] == '-') {
361                         if (arg[1] == 'D' ||
362                                         arg[1] == 'O' ||
363                                         arg[1] == 'f' ||
364                                         strncmp(arg + 1, "std=", 4) == 0) {
365                                 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
366                         } else {
367                                 usage(argv[0]);
368                                 return 1;
369                         }
370                 } else {
371                         if(input != NULL) {
372                                 fprintf(stderr, "Error: multiple input files specified\n");
373                                 usage(argv[0]);
374                                 return 1;
375                         } else {
376                                 input = arg;
377                         }
378                 }
379         }
380
381         if(input == NULL) {
382                 fprintf(stderr, "%s: no input files\n", argv[0]);
383                 return 1;
384         }
385
386         if(mode == LexTest) {
387                 lextest(input);
388                 return 0;
389         }
390
391         FILE *const in = preprocess(input);
392         translation_unit_t *const unit = do_parsing(in, input);
393         pclose(in);
394         if(unit == NULL)
395                 return 1;
396
397         if(mode == PrintAst) {
398                 print_ast(unit);
399                 return 0;
400         }
401         if(mode == PrintFluffy) {
402                 ast_set_output(stdout);
403                 write_fluffy_decls(unit);
404         }
405
406         char outsname[4096];
407         const char *sname = NULL;
408         if(mode == Compile) {
409                 sname = outname;
410         }
411         if(sname == NULL) {
412                 get_output_name(outsname, sizeof(outsname), input, ".s");
413                 sname = outsname;
414         }
415
416         create_firm_prog(unit);
417         optimize();
418         emit(input, sname);
419
420         if(mode == CompileAssemble) {
421                 char outoname[4096];
422                 const char *oname = outname;
423                 if(oname == NULL) {
424                         get_output_name(outoname, sizeof(outoname), input, ".o");
425                         oname = outoname;
426                 }
427                 assemble(sname, oname);
428         } else {
429                 assert(mode == CompileAssembleLink);
430
431                 if(outname == NULL)
432                         outname = "a.out";
433
434                 link(sname, outname);
435         }
436
437         exit_ast2firm();
438         exit_parser();
439         exit_ast();
440         exit_lexer();
441         exit_typehash();
442         exit_types();
443         exit_tokens();
444         exit_symbol_table();
445         return 0;
446 }