Do 64bit lowering.
[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 #if 0
87         dump_ir_block_graph(irg, suffix);
88 #else
89         (void)irg, (void)suffix;
90 #endif
91 }
92
93 static void get_output_name(char *buf, size_t buflen, const char *inputname,
94                             const char *newext)
95 {
96         size_t last_dot = 0xffffffff;
97         size_t i = 0;
98         for(const char *c = inputname; *c != 0; ++c) {
99                 if(*c == '.')
100                         last_dot = i;
101                 ++i;
102         }
103         if(last_dot == 0xffffffff)
104                 last_dot = i;
105
106         if(last_dot >= buflen)
107                 panic("filename too long");
108         memcpy(buf, inputname, last_dot);
109
110         size_t extlen = strlen(newext) + 1;
111         if(extlen + last_dot >= buflen)
112                 panic("filename too long");
113         memcpy(buf+last_dot, newext, extlen);
114 }
115
116 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
117 {
118         lexer_open_stream(in, input);
119         translation_unit_t *unit = parse();
120         return unit;
121 }
122
123 static void lextest(const char *fname)
124 {
125         FILE *in = fopen(fname, "r");
126         if(in == NULL) {
127                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
128                 exit(1);
129         }
130
131         lexer_open_stream(in, fname);
132
133         do {
134                 lexer_next_preprocessing_token();
135                 print_token(stdout, &lexer_token);
136                 puts("");
137         } while(lexer_token.type != T_EOF);
138
139         fclose(in);
140 }
141
142 static void backend(const char *inputname, const char *outname)
143 {
144         FILE *out = fopen(outname, "w");
145         if(out == NULL) {
146                 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
147                                 strerror(errno));
148                 exit(1);
149         }
150
151         be_main(out, inputname);
152
153         fclose(out);
154 }
155
156 static void emit(const char *input_name, const char *out_name)
157 {
158         backend(input_name, out_name);
159 }
160
161 static FILE* preprocess(const char *in)
162 {
163         char buf[4096];
164
165         snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
166         if(verbose) {
167                 puts(buf);
168         }
169         FILE* f = popen(buf, "r");
170         if (f == NULL) {
171                 fprintf(stderr, "invoking preprocessor failed\n");
172                 exit(1);
173         }
174         return f;
175 }
176
177 static void link(const char *in, const char *out)
178 {
179         char buf[4096];
180
181         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
182         if(verbose) {
183                 puts(buf);
184         }
185         int err = system(buf);
186         if(err != 0) {
187                 fprintf(stderr, "linker reported an error\n");
188                 exit(1);
189         }
190 }
191
192 static void assemble(const char *in, const char *out)
193 {
194         char buf[4096];
195
196         snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
197         if(verbose) {
198                 puts(buf);
199         }
200         int err = system(buf);
201         if(err != 0) {
202                 fprintf(stderr, "assembler reported an error\n");
203                 exit(1);
204         }
205 }
206
207 static void create_firm_prog(translation_unit_t *unit)
208 {
209         translation_unit_to_firm(unit);
210
211         //dump_globals_as_text(dump_verbosity_max, "-globals");
212
213         int n_irgs = get_irp_n_irgs();
214         for(int i = 0; i < n_irgs; ++i) {
215                 ir_graph *const irg = get_irp_irg(i);
216                 dump(irg, "-start");
217         }
218
219         lower_highlevel();
220         for(int i = 0; i < n_irgs; ++i) {
221                 ir_graph *const irg = get_irp_irg(i);
222                 dump(irg, "-lower");
223         }
224 }
225
226 static void optimize(void)
227 {
228         int         arr_len;
229         ir_entity **keep_methods;
230
231         cgana(&arr_len, &keep_methods);
232         gc_irgs(arr_len, keep_methods);
233         free(keep_methods);
234
235         optimize_funccalls(0);
236
237         const backend_params *const be_params = be_init();
238         create_intrinsic_fkt *const arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
239         void                 *const create_intrinsic_ctx  = be_params->create_intrinsic_ctx;
240         lwrdw_param_t lwrdw_param = {
241                 1,
242                 1,
243                 mode_Ls, mode_Lu,
244                 mode_Is, mode_Iu,
245                 def_create_intrinsic_fkt,
246                 NULL
247         };
248         if (arch_create_intrinsic) {
249                 lwrdw_param.create_intrinsic = arch_create_intrinsic;
250                 lwrdw_param.ctx              = create_intrinsic_ctx;
251         }
252
253         for(int i = 0; i < get_irp_n_irgs(); ++i) {
254                 ir_graph *irg = get_irp_irg(i);
255
256                 optimize_graph_df(irg);
257                 dump(irg, "-localopt");
258                 place_code(irg);
259                 dump(irg, "-place");
260                 optimize_cf(irg);
261                 dump(irg, "-cf");
262                 lower_dw_ops(&lwrdw_param);
263                 dump(irg, "-dw");
264         }
265 }
266
267 void write_fluffy_decls(translation_unit_t *unit);
268
269 typedef enum compile_mode_t {
270         Compile,
271         CompileAssemble,
272         CompileAssembleLink,
273         LexTest,
274         PrintAst,
275         PrintFluffy
276 } compile_mode_t;
277
278 static void usage(const char *argv0)
279 {
280         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
281 }
282
283 int main(int argc, char **argv)
284 {
285         initialize_firm();
286
287         init_symbol_table();
288         init_tokens();
289         init_lexer();
290         init_types();
291         init_typehash();
292         init_ast();
293         init_parser();
294         init_ast2firm();
295
296         const char *input   = NULL;
297         const char *outname = NULL;
298         compile_mode_t mode = CompileAssembleLink;
299
300         for(int i = 1; i < argc; ++i) {
301                 const char *arg = argv[i];
302                 if(strcmp(arg, "-o") == 0) {
303                         ++i;
304                         if(i >= argc) {
305                                 usage(argv[0]);
306                                 return 1;
307                         }
308                         outname = argv[i];
309                 } else if(strcmp(arg, "-c") == 0) {
310                         mode = CompileAssemble;
311                 } else if(strcmp(arg, "-S") == 0) {
312                         mode = Compile;
313                 } else if(strcmp(arg, "--lextest") == 0) {
314                         mode = LexTest;
315                 } else if(strcmp(arg, "--print-ast") == 0) {
316                         mode = PrintAst;
317                 } else if(strcmp(arg, "--print-fluffy") == 0) {
318                         mode = PrintFluffy;
319                 } else if(strcmp(arg, "-v") == 0) {
320                         verbose = 1;
321                 } else if(arg[0] == '-') {
322                         if (arg[1] == 'D' ||
323                                         arg[1] == 'O' ||
324                                         arg[1] == 'f') {
325                                 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
326                         } else {
327                                 usage(argv[0]);
328                                 return 1;
329                         }
330                 } else {
331                         if(input != NULL) {
332                                 fprintf(stderr, "Error: multiple input files specified\n");
333                                 usage(argv[0]);
334                                 return 1;
335                         } else {
336                                 input = arg;
337                         }
338                 }
339         }
340
341         if(input == NULL) {
342                 fprintf(stderr, "%s: no input files\n", argv[0]);
343                 return 1;
344         }
345
346         if(mode == LexTest) {
347                 lextest(input);
348                 return 0;
349         }
350
351         FILE *const in = preprocess(input);
352         translation_unit_t *const unit = do_parsing(in, input);
353         pclose(in);
354         if(unit == NULL)
355                 return 1;
356
357         if(mode == PrintAst) {
358                 print_ast(unit);
359                 return 0;
360         }
361         if(mode == PrintFluffy) {
362                 ast_set_output(stdout);
363                 write_fluffy_decls(unit);
364         }
365
366         char outsname[4096];
367         const char *sname = NULL;
368         if(mode == Compile) {
369                 sname = outname;
370         }
371         if(sname == NULL) {
372                 get_output_name(outsname, sizeof(outsname), input, ".s");
373                 sname = outsname;
374         }
375
376         create_firm_prog(unit);
377         optimize();
378         emit(input, sname);
379
380         if(mode == CompileAssemble) {
381                 char outoname[4096];
382                 const char *oname = outname;
383                 if(oname == NULL) {
384                         get_output_name(outoname, sizeof(outoname), input, ".o");
385                         oname = outoname;
386                 }
387                 assemble(sname, oname);
388         } else {
389                 assert(mode == CompileAssembleLink);
390
391                 if(outname == NULL)
392                         outname = "a.out";
393
394                 link(sname, outname);
395         }
396
397         exit_ast2firm();
398         exit_parser();
399         exit_ast();
400         exit_typehash();
401         exit_types();
402         exit_lexer();
403         exit_tokens();
404         exit_symbol_table();
405         return 0;
406 }