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