compound type specifiers can have __attribute__, too. Parse it (and ignore it for...
[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
43 static void initialize_firm(void)
44 {
45         be_opt_register();
46         firm_init_options(NULL, 0, NULL);
47
48         const backend_params *be_params;
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         be_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 #ifdef _WIN32
171         snprintf(buf, sizeof(buf), PREPROCESSOR " %s",in);
172 #else
173         snprintf(buf, sizeof(buf), PREPROCESSOR " %s -o -",in);
174 #endif
175
176         if(verbose) {
177                 puts(buf);
178         }
179         FILE* f = popen(buf, "r");
180         if (f == NULL) {
181                 fprintf(stderr, "invoking preprocessor failed\n");
182                 exit(1);
183         }
184         return f;
185 }
186
187 static void link(const char *in, const char *out)
188 {
189         char buf[4096];
190
191         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
192         if(verbose) {
193                 puts(buf);
194         }
195         int err = system(buf);
196         if(err != 0) {
197                 fprintf(stderr, "linker reported an error\n");
198                 exit(1);
199         }
200 }
201
202 static void assemble(const char *in, const char *out)
203 {
204         char buf[4096];
205
206         snprintf(buf, sizeof(buf), "%s %s -c -o %s", LINKER, in, out);
207         if(verbose) {
208                 puts(buf);
209         }
210         int err = system(buf);
211         if(err != 0) {
212                 fprintf(stderr, "assembler reported an error\n");
213                 exit(1);
214         }
215 }
216
217 static void create_firm_prog(translation_unit_t *unit)
218 {
219         translation_unit_to_firm(unit);
220
221         //dump_globals_as_text(dump_verbosity_max, "-globals");
222
223         int n_irgs = get_irp_n_irgs();
224         for(int i = 0; i < n_irgs; ++i) {
225                 ir_graph *const irg = get_irp_irg(i);
226                 dump(irg, "-start");
227         }
228
229         lower_highlevel();
230         for(int i = 0; i < n_irgs; ++i) {
231                 ir_graph *const irg = get_irp_irg(i);
232                 dump(irg, "-lower");
233         }
234 }
235
236 static void optimize(void)
237 {
238         int         arr_len;
239         ir_entity **keep_methods;
240
241         cgana(&arr_len, &keep_methods);
242         gc_irgs(arr_len, keep_methods);
243         free(keep_methods);
244
245         optimize_funccalls(0);
246
247         const backend_params *const be_params = be_init();
248         create_intrinsic_fkt *const arch_create_intrinsic = be_params->arch_create_intrinsic_fkt;
249         void                 *const create_intrinsic_ctx  = be_params->create_intrinsic_ctx;
250         lwrdw_param_t lwrdw_param = {
251                 1,
252                 1,
253                 mode_Ls, mode_Lu,
254                 mode_Is, mode_Iu,
255                 def_create_intrinsic_fkt,
256                 NULL
257         };
258         if (arch_create_intrinsic) {
259                 lwrdw_param.create_intrinsic = arch_create_intrinsic;
260                 lwrdw_param.ctx              = create_intrinsic_ctx;
261         }
262
263         for(int i = 0; i < get_irp_n_irgs(); ++i) {
264                 ir_graph *irg = get_irp_irg(i);
265
266                 optimize_graph_df(irg);
267                 dump(irg, "-01-localopt");
268                 place_code(irg);
269                 dump(irg, "-02-place");
270                 optimize_cf(irg);
271                 dump(irg, "-03-cf");
272                 lower_dw_ops(&lwrdw_param);
273                 dump(irg, "-04-dw");
274                 optimize_graph_df(irg);
275                 dump(irg, "-05-localopt");
276                 optimize_cf(irg);
277                 dump(irg, "-06-cf");
278         }
279 }
280
281 void write_fluffy_decls(translation_unit_t *unit);
282
283 typedef enum compile_mode_t {
284         Compile,
285         CompileAssemble,
286         CompileAssembleLink,
287         LexTest,
288         PrintAst,
289         PrintFluffy
290 } compile_mode_t;
291
292 static void usage(const char *argv0)
293 {
294         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
295 }
296
297 int main(int argc, char **argv)
298 {
299         initialize_firm();
300
301         init_symbol_table();
302         init_tokens();
303         init_lexer();
304         init_types();
305         init_typehash();
306         init_ast();
307         init_parser();
308         init_ast2firm();
309
310         const char *input   = NULL;
311         const char *outname = NULL;
312         compile_mode_t mode = CompileAssembleLink;
313
314         for(int i = 1; i < argc; ++i) {
315                 const char *arg = argv[i];
316                 if(strcmp(arg, "-o") == 0) {
317                         ++i;
318                         if(i >= argc) {
319                                 usage(argv[0]);
320                                 return 1;
321                         }
322                         outname = argv[i];
323                 } else if(strcmp(arg, "-c") == 0) {
324                         mode = CompileAssemble;
325                 } else if(strcmp(arg, "-S") == 0) {
326                         mode = Compile;
327                 } else if(strcmp(arg, "--lextest") == 0) {
328                         mode = LexTest;
329                 } else if(strcmp(arg, "--print-ast") == 0) {
330                         mode = PrintAst;
331                 } else if(strcmp(arg, "--print-fluffy") == 0) {
332                         mode = PrintFluffy;
333                 } else if(strcmp(arg, "-v") == 0) {
334                         verbose = 1;
335                 } else if(arg[0] == '-') {
336                         if (arg[1] == 'D' ||
337                                         arg[1] == 'O' ||
338                                         arg[1] == 'f' ||
339                                         strncmp(arg + 1, "std=", 4) == 0) {
340                                 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
341                         } else {
342                                 usage(argv[0]);
343                                 return 1;
344                         }
345                 } else {
346                         if(input != NULL) {
347                                 fprintf(stderr, "Error: multiple input files specified\n");
348                                 usage(argv[0]);
349                                 return 1;
350                         } else {
351                                 input = arg;
352                         }
353                 }
354         }
355
356         if(input == NULL) {
357                 fprintf(stderr, "%s: no input files\n", argv[0]);
358                 return 1;
359         }
360
361         if(mode == LexTest) {
362                 lextest(input);
363                 return 0;
364         }
365
366         FILE *const in = preprocess(input);
367         translation_unit_t *const unit = do_parsing(in, input);
368         pclose(in);
369         if(unit == NULL)
370                 return 1;
371
372         if(mode == PrintAst) {
373                 print_ast(unit);
374                 return 0;
375         }
376         if(mode == PrintFluffy) {
377                 ast_set_output(stdout);
378                 write_fluffy_decls(unit);
379         }
380
381         char outsname[4096];
382         const char *sname = NULL;
383         if(mode == Compile) {
384                 sname = outname;
385         }
386         if(sname == NULL) {
387                 get_output_name(outsname, sizeof(outsname), input, ".s");
388                 sname = outsname;
389         }
390
391         create_firm_prog(unit);
392         optimize();
393         emit(input, sname);
394
395         if(mode == CompileAssemble) {
396                 char outoname[4096];
397                 const char *oname = outname;
398                 if(oname == NULL) {
399                         get_output_name(outoname, sizeof(outoname), input, ".o");
400                         oname = outoname;
401                 }
402                 assemble(sname, oname);
403         } else {
404                 assert(mode == CompileAssembleLink);
405
406                 if(outname == NULL)
407                         outname = "a.out";
408
409                 link(sname, outname);
410         }
411
412         exit_ast2firm();
413         exit_parser();
414         exit_ast();
415         exit_typehash();
416         exit_types();
417         exit_lexer();
418         exit_tokens();
419         exit_symbol_table();
420         return 0;
421 }