8795cdd3a7fc27dce7a3a3033d4d8a9fba01647c
[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
239         for(int i = 0; i < n_irgs; ++i) {
240                 ir_graph *const irg = get_irp_irg(i);
241                 dump(irg, "-lower");
242         }
243 }
244
245 static void optimize(void)
246 {
247         int         arr_len;
248         ir_entity **keep_methods;
249
250         cgana(&arr_len, &keep_methods);
251         gc_irgs(arr_len, keep_methods);
252         free(keep_methods);
253
254         optimize_funccalls(0);
255
256         lwrdw_param_t lwrdw_param = {
257                 1,
258                 1,
259                 mode_Ls, mode_Lu,
260                 mode_Is, mode_Iu,
261                 def_create_intrinsic_fkt,
262                 NULL
263         };
264         if (be_params->arch_create_intrinsic_fkt) {
265                 lwrdw_param.create_intrinsic = be_params->arch_create_intrinsic_fkt;
266                 lwrdw_param.ctx              = be_params->create_intrinsic_ctx;
267         }
268
269         for(int i = 0; i < get_irp_n_irgs(); ++i) {
270                 ir_graph *irg = get_irp_irg(i);
271
272                 optimize_graph_df(irg);
273                 dump(irg, "-01-localopt");
274                 place_code(irg);
275                 dump(irg, "-02-place");
276                 optimize_cf(irg);
277                 dump(irg, "-03-cf");
278                 lower_dw_ops(&lwrdw_param);
279                 dump(irg, "-04-dw");
280                 optimize_graph_df(irg);
281                 dump(irg, "-05-localopt");
282                 optimize_cf(irg);
283                 dump(irg, "-06-cf");
284         }
285 }
286
287 typedef enum compile_mode_t {
288         Compile,
289         CompileAssemble,
290         CompileAssembleLink,
291         LexTest,
292         PrintAst,
293         PrintFluffy
294 } compile_mode_t;
295
296 static void usage(const char *argv0)
297 {
298         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
299 }
300
301 int main(int argc, char **argv)
302 {
303         initialize_firm();
304
305         init_symbol_table();
306         init_tokens();
307         init_types();
308         init_typehash();
309         init_lexer();
310         init_ast();
311         init_parser();
312         init_ast2firm();
313
314         const char *input   = NULL;
315         const char *outname = NULL;
316         compile_mode_t mode = CompileAssembleLink;
317
318         for(int i = 1; i < argc; ++i) {
319                 const char *arg = argv[i];
320                 if(strcmp(arg, "-o") == 0) {
321                         ++i;
322                         if(i >= argc) {
323                                 usage(argv[0]);
324                                 return 1;
325                         }
326                         outname = argv[i];
327                 } else if(strcmp(arg, "-c") == 0) {
328                         mode = CompileAssemble;
329                 } else if(strcmp(arg, "-S") == 0) {
330                         mode = Compile;
331                 } else if(strcmp(arg, "--lextest") == 0) {
332                         mode = LexTest;
333                 } else if(strcmp(arg, "--print-ast") == 0) {
334                         mode = PrintAst;
335                 } else if(strcmp(arg, "--print-fluffy") == 0) {
336                         mode = PrintFluffy;
337                 } else if(strcmp(arg, "--dump") == 0) {
338                         do_dump = true;
339                 } else if(strcmp(arg, "-v") == 0) {
340                         verbose = 1;
341                 } else if(arg[0] == '-' && arg[1] == 'f') {
342                         const char *opt = &arg[2];
343                         if(opt[0] == 0) {
344                                 ++i;
345                                 if(i >= argc) {
346                                         usage(argv[0]);
347                                         return 1;
348                                 }
349                                 opt = argv[i];
350                                 if(opt[0] == '-') {
351                                         usage(argv[0]);
352                                         return 1;
353                                 }
354                         }
355                         //firm_option(opt);
356                 } else if(arg[0] == '-' && arg[1] == 'b') {
357                         const char *opt = &arg[2];
358                         if(opt[0] == 0) {
359                                 ++i;
360                                 if(i >= argc) {
361                                         usage(argv[0]);
362                                         return 1;
363                                 }
364                                 opt = argv[i];
365                                 if(opt[0] == '-') {
366                                         usage(argv[0]);
367                                         return 1;
368                                 }
369                         }
370                         //firm_be_option(opt);
371                 } else if(arg[0] == '-') {
372                         if (arg[1] == 'D' ||
373                                         arg[1] == 'O' ||
374                                         arg[1] == 'f' ||
375                                         strncmp(arg + 1, "std=", 4) == 0) {
376                                 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
377                         } else {
378                                 usage(argv[0]);
379                                 return 1;
380                         }
381                 } else {
382                         if(input != NULL) {
383                                 fprintf(stderr, "Error: multiple input files specified\n");
384                                 usage(argv[0]);
385                                 return 1;
386                         } else {
387                                 input = arg;
388                         }
389                 }
390         }
391
392         if(input == NULL) {
393                 fprintf(stderr, "%s: no input files\n", argv[0]);
394                 return 1;
395         }
396
397         if(mode == LexTest) {
398                 lextest(input);
399                 return 0;
400         }
401
402         FILE *const in = preprocess(input);
403         translation_unit_t *const unit = do_parsing(in, input);
404         pclose(in);
405         if(unit == NULL)
406                 return 1;
407
408         if(mode == PrintAst) {
409                 print_ast(unit);
410                 return 0;
411         }
412         if(mode == PrintFluffy) {
413                 ast_set_output(stdout);
414                 write_fluffy_decls(unit);
415         }
416
417         char outsname[4096];
418         const char *sname = NULL;
419         if(mode == Compile) {
420                 sname = outname;
421         }
422         if(sname == NULL) {
423                 get_output_name(outsname, sizeof(outsname), input, ".s");
424                 sname = outsname;
425         }
426
427         create_firm_prog(unit);
428         optimize();
429         emit(input, sname);
430
431         if(mode == CompileAssemble) {
432                 char outoname[4096];
433                 const char *oname = outname;
434                 if(oname == NULL) {
435                         get_output_name(outoname, sizeof(outoname), input, ".o");
436                         oname = outoname;
437                 }
438                 assemble(sname, oname);
439         } else {
440                 assert(mode == CompileAssembleLink);
441
442                 if(outname == NULL)
443                         outname = "a.out";
444
445                 link(sname, outname);
446         }
447
448         exit_ast2firm();
449         exit_parser();
450         exit_ast();
451         exit_lexer();
452         exit_typehash();
453         exit_types();
454         exit_tokens();
455         exit_symbol_table();
456         return 0;
457 }