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