fix some goto/label bugs
[cparser] / main.c
1 #include <config.h>
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #ifndef WITH_LIBCORE
9 #define WITH_LIBCORE
10 #endif
11
12 #include <libfirm/firm.h>
13 #include <libfirm/be.h>
14
15 #include "lexer.h"
16 #include "token_t.h"
17 #include "type_hash.h"
18 #include "parser.h"
19 #include "ast2firm.h"
20 #include "adt/error.h"
21
22 #define LINKER "gcc"
23
24 static int verbose;
25
26 static const ir_settings_if_conv_t *if_conv_info = NULL;
27
28 static void initialize_firm(void)
29 {
30         be_opt_register();
31         firm_init_options(NULL, 0, NULL);
32
33         const backend_params *be_params;
34         firm_parameter_t params;
35         memset(&params, 0, sizeof(params));
36
37         params.size = sizeof(params);
38         params.enable_statistics = 0;
39         params.initialize_local_func = uninitialized_local_var;
40         params.cc_mask = 0;
41         params.builtin_dbg = NULL;
42
43         /* initialize backend */
44         be_params = be_init();
45         be_set_debug_retrieve(retrieve_dbg);
46         params.arch_op_settings = be_params->arch_op_settings;
47         if_conv_info            = be_params->if_conv_info;
48
49         /* intialize firm itself */
50         init_firm(&params);
51         dbg_init(NULL, NULL, dbg_snprint);
52
53         set_opt_constant_folding(1);
54         set_opt_unreachable_code(1);
55         set_opt_control_flow_straightening(1);
56         set_opt_control_flow_weak_simplification(1);
57         set_opt_control_flow_strong_simplification(1);
58         set_opt_dead_node_elimination(1);
59         set_opt_reassociation(1);
60         set_opt_inline(1);
61         set_opt_dyn_meth_dispatch(1);
62         set_opt_normalize(1);
63         set_opt_tail_recursion(1);
64         set_opt_dead_method_elimination(1);
65         set_opt_precise_exc_context(0);
66         set_opt_loop_unrolling(0);
67         set_opt_strength_red(0);
68         set_opt_redundant_loadstore(1);
69         set_opt_fragile_ops(0);
70         set_opt_function_call(1);
71         set_opt_optimize_class_casts(0);
72         set_opt_suppress_downcast_optimization(0);
73         set_opt_remove_confirm(1);
74         set_opt_scalar_replacement(1);
75         set_opt_ldst_only_null_ptr_exceptions(1);
76         set_opt_alias_analysis(1);
77
78         dump_consts_local(1);
79         dump_keepalive_edges(1);
80 }
81
82 static void dump(ir_graph *irg, const char *suffix)
83 {
84         dump_ir_block_graph(irg, suffix);
85 }
86
87 static void get_output_name(char *buf, size_t buflen, const char *inputname,
88                             const char *newext)
89 {
90         size_t last_dot = 0xffffffff;
91         size_t i = 0;
92         for(const char *c = inputname; *c != 0; ++c) {
93                 if(*c == '.')
94                         last_dot = i;
95                 ++i;
96         }
97         if(last_dot == 0xffffffff)
98                 last_dot = i;
99
100         if(last_dot >= buflen)
101                 panic("filename too long");
102         memcpy(buf, inputname, last_dot);
103
104         size_t extlen = strlen(newext) + 1;
105         if(extlen + last_dot >= buflen)
106                 panic("filename too long");
107         memcpy(buf+last_dot, newext, extlen);
108 }
109
110 static translation_unit_t *do_parsing(const char *fname)
111 {
112         FILE *in = fopen(fname, "r");
113         if(in == NULL) {
114                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
115                 exit(1);
116         }
117
118         lexer_open_stream(in, fname);
119
120         translation_unit_t *unit = parse();
121
122         fclose(in);
123
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 void link(const char *in, const char *out)
166 {
167         char buf[4096];
168
169         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
170         if(verbose) {
171                 puts(buf);
172         }
173         int err = system(buf);
174         if(err != 0) {
175                 fprintf(stderr, "linker reported an error\n");
176                 exit(1);
177         }
178 }
179
180
181
182 static void create_firm_prog(translation_unit_t *unit)
183 {
184         translation_unit_to_firm(unit);
185
186         int n_irgs = get_irp_n_irgs();
187         for(int i = 0; i < n_irgs; ++i) {
188                 ir_graph *const irg = get_irp_irg(i);
189                 dump(irg, "-start");
190         }
191 }
192
193 static void optimize(void)
194 {
195         for(int i = 0; i < get_irp_n_irgs(); ++i) {
196                 ir_graph *irg = get_irp_irg(i);
197                 place_code(irg);
198                 dump(irg, "-place");
199                 optimize_graph_df(irg);
200                 dump(irg, "-localopt");
201                 optimize_cf(irg);
202                 dump(irg, "-cf");
203         }
204 }
205
206 void write_fluffy_decls(translation_unit_t *unit);
207
208 int main(int argc, char **argv)
209 {
210         initialize_firm();
211
212         init_symbol_table();
213         init_tokens();
214         init_lexer();
215         init_types();
216         init_typehash();
217         init_ast();
218         init_parser();
219         init_ast2firm();
220
221         if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
222                 lextest(argv[2]);
223                 return 0;
224         }
225
226         if(argc > 2 && strcmp(argv[1], "--print-ast") == 0) {
227                 translation_unit_t *unit = do_parsing(argv[2]);
228                 ast_set_output(stdout);
229                 if(unit != NULL) {
230                         print_ast(unit);
231                 }
232                 return 0;
233         }
234
235         if(argc > 2 && strcmp(argv[1], "--print-fluffy") == 0) {
236                 translation_unit_t *unit = do_parsing(argv[2]);
237                 ast_set_output(stdout);
238                 write_fluffy_decls(unit);
239                 return 0;
240         }
241
242         for(int i = 1; i < argc; ++i) {
243                 const char *input = argv[i];
244                 char        outfname[4096];
245
246                 get_output_name(outfname, sizeof(outfname), input, ".s");
247
248                 translation_unit_t *unit = do_parsing(input);
249                 if(unit == NULL) {
250                         return 1;
251                 }
252                 create_firm_prog(unit);
253                 optimize();
254                 emit(input, outfname);
255                 link(outfname, "a.out");
256         }
257
258         exit_ast2firm();
259         exit_parser();
260         exit_ast();
261         exit_typehash();
262         exit_types();
263         exit_lexer();
264         exit_tokens();
265         exit_symbol_table();
266         return 0;
267 }