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