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