6b240ad886c0c72148c3d0979288f055281bddc4
[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 #ifndef PREPROCESSOR
26 #define PREPROCESSOR "cpp"
27 #endif
28
29 #ifndef LINKER
30 #define LINKER       "gcc"
31 #endif
32
33 #ifdef _WIN32
34 /* remap some names */
35 #define popen(cmd, mode)  _popen(cmd, mode)
36 #define pclose(file)      _pclose(file)
37 #endif /* _WIN32 */
38
39 static int verbose;
40
41 static const ir_settings_if_conv_t *if_conv_info = NULL;
42 static const backend_params        *be_params    = NULL;
43
44 static void initialize_firm(void)
45 {
46         be_opt_register();
47         firm_init_options(NULL, 0, NULL);
48
49         firm_parameter_t params;
50         memset(&params, 0, sizeof(params));
51
52         params.size = sizeof(params);
53         params.enable_statistics = 0;
54         params.initialize_local_func = uninitialized_local_var;
55         params.cc_mask = 0;
56         params.builtin_dbg = NULL;
57
58         /* initialize backend */
59         be_params = be_init();
60         ir_set_debug_retrieve(retrieve_dbg);
61         params.arch_op_settings = be_params->arch_op_settings;
62         if_conv_info            = be_params->if_conv_info;
63
64         /* intialize firm itself */
65         init_firm(&params);
66         dbg_init(NULL, NULL, dbg_snprint);
67
68         set_opt_constant_folding(1);
69         set_opt_unreachable_code(1);
70         set_opt_control_flow_straightening(1);
71         set_opt_control_flow_weak_simplification(1);
72         set_opt_control_flow_strong_simplification(1);
73         set_opt_dyn_meth_dispatch(1);
74         set_opt_normalize(1);
75         set_opt_precise_exc_context(0);
76         set_opt_strength_red(0);
77         set_opt_fragile_ops(0);
78         set_opt_optimize_class_casts(0);
79         set_opt_suppress_downcast_optimization(0);
80         set_opt_remove_confirm(1);
81         set_opt_scalar_replacement(1);
82         set_opt_ldst_only_null_ptr_exceptions(1);
83         set_opt_alias_analysis(1);
84
85         dump_consts_local(1);
86         dump_keepalive_edges(1);
87 }
88
89 static void dump(ir_graph *irg, const char *suffix)
90 {
91 #if 0
92         dump_ir_block_graph(irg, suffix);
93 #else
94         (void)irg, (void)suffix;
95 #endif
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_lexer();
297         init_types();
298         init_typehash();
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, "-v") == 0) {
327                         verbose = 1;
328                 } else if(arg[0] == '-' && arg[1] == 'f') {
329                         const char *opt = &arg[2];
330                         if(opt[0] == 0) {
331                                 ++i;
332                                 if(i >= argc) {
333                                         usage(argv[0]);
334                                         return 1;
335                                 }
336                                 opt = argv[i];
337                                 if(opt[0] == '-') {
338                                         usage(argv[0]);
339                                         return 1;
340                                 }
341                         }
342                         //firm_option(opt);
343                 } else if(arg[0] == '-' && arg[1] == 'b') {
344                         const char *opt = &arg[2];
345                         if(opt[0] == 0) {
346                                 ++i;
347                                 if(i >= argc) {
348                                         usage(argv[0]);
349                                         return 1;
350                                 }
351                                 opt = argv[i];
352                                 if(opt[0] == '-') {
353                                         usage(argv[0]);
354                                         return 1;
355                                 }
356                         }
357                         //firm_be_option(opt);
358                 } else if(arg[0] == '-') {
359                         if (arg[1] == 'D' ||
360                                         arg[1] == 'O' ||
361                                         arg[1] == 'f' ||
362                                         strncmp(arg + 1, "std=", 4) == 0) {
363                                 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
364                         } else {
365                                 usage(argv[0]);
366                                 return 1;
367                         }
368                 } else {
369                         if(input != NULL) {
370                                 fprintf(stderr, "Error: multiple input files specified\n");
371                                 usage(argv[0]);
372                                 return 1;
373                         } else {
374                                 input = arg;
375                         }
376                 }
377         }
378
379         if(input == NULL) {
380                 fprintf(stderr, "%s: no input files\n", argv[0]);
381                 return 1;
382         }
383
384         if(mode == LexTest) {
385                 lextest(input);
386                 return 0;
387         }
388
389         FILE *const in = preprocess(input);
390         translation_unit_t *const unit = do_parsing(in, input);
391         pclose(in);
392         if(unit == NULL)
393                 return 1;
394
395         if(mode == PrintAst) {
396                 print_ast(unit);
397                 return 0;
398         }
399         if(mode == PrintFluffy) {
400                 ast_set_output(stdout);
401                 write_fluffy_decls(unit);
402         }
403
404         char outsname[4096];
405         const char *sname = NULL;
406         if(mode == Compile) {
407                 sname = outname;
408         }
409         if(sname == NULL) {
410                 get_output_name(outsname, sizeof(outsname), input, ".s");
411                 sname = outsname;
412         }
413
414         create_firm_prog(unit);
415         optimize();
416         emit(input, sname);
417
418         if(mode == CompileAssemble) {
419                 char outoname[4096];
420                 const char *oname = outname;
421                 if(oname == NULL) {
422                         get_output_name(outoname, sizeof(outoname), input, ".o");
423                         oname = outoname;
424                 }
425                 assemble(sname, oname);
426         } else {
427                 assert(mode == CompileAssembleLink);
428
429                 if(outname == NULL)
430                         outname = "a.out";
431
432                 link(sname, outname);
433         }
434
435         exit_ast2firm();
436         exit_parser();
437         exit_ast();
438         exit_typehash();
439         exit_types();
440         exit_lexer();
441         exit_tokens();
442         exit_symbol_table();
443         return 0;
444 }