more work on local variable support
[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 #include <libfirm/firm.h>
9 #include <libfirm/be.h>
10
11 #include "lexer.h"
12 #include "token_t.h"
13 #include "type_hash.h"
14 #include "parser.h"
15 #include "ast2firm.h"
16 #include "adt/error.h"
17
18 #define LINKER "gcc"
19
20 static int verbose;
21
22 static const ir_settings_if_conv_t *if_conv_info = NULL;
23
24 static void initialize_firm(void)
25 {
26         be_opt_register();
27         firm_init_options(NULL, 0, NULL);
28
29         const backend_params *be_params;
30         firm_parameter_t params;
31         memset(&params, 0, sizeof(params));
32
33         params.size = sizeof(params);
34         params.enable_statistics = 0;
35         params.initialize_local_func = uninitialized_local_var;
36         params.cc_mask = 0;
37         params.builtin_dbg = NULL;
38
39         /* initialize backend */
40         be_params = be_init();
41         be_set_debug_retrieve(retrieve_dbg);
42         params.arch_op_settings = be_params->arch_op_settings;
43         if_conv_info            = be_params->if_conv_info;
44
45         /* intialize firm itself */
46         init_firm(&params);
47         dbg_init(NULL, NULL, dbg_snprint);
48
49         set_opt_constant_folding(1);
50         set_opt_unreachable_code(1);
51         set_opt_control_flow_straightening(1);
52         set_opt_control_flow_weak_simplification(1);
53         set_opt_control_flow_strong_simplification(1);
54         set_opt_dead_node_elimination(1);
55         set_opt_reassociation(1);
56         set_opt_inline(1);
57         set_opt_dyn_meth_dispatch(1);
58         set_opt_normalize(1);
59         set_opt_tail_recursion(1);
60         set_opt_dead_method_elimination(1);
61         set_opt_precise_exc_context(0);
62         set_opt_loop_unrolling(0);
63         set_opt_strength_red(0);
64         set_opt_redundant_loadstore(1);
65         set_opt_fragile_ops(0);
66         set_opt_function_call(1);
67         set_opt_optimize_class_casts(0);
68         set_opt_suppress_downcast_optimization(0);
69         set_opt_remove_confirm(1);
70         set_opt_scalar_replacement(1);
71         set_opt_ldst_only_null_ptr_exceptions(1);
72         set_opt_alias_analysis(1);
73
74         dump_consts_local(1);
75 }
76
77 static void dump(const char *suffix)
78 {
79         dump_ir_block_graph(current_ir_graph, suffix);
80 }
81
82 static void get_output_name(char *buf, size_t buflen, const char *inputname,
83                             const char *newext)
84 {
85         size_t last_dot = 0xffffffff;
86         size_t i = 0;
87         for(const char *c = inputname; *c != 0; ++c) {
88                 if(*c == '.')
89                         last_dot = i;
90                 ++i;
91         }
92         if(last_dot == 0xffffffff)
93                 last_dot = i;
94
95         if(last_dot >= buflen)
96                 panic("filename too long");
97         memcpy(buf, inputname, last_dot);
98
99         size_t extlen = strlen(newext) + 1;
100         if(extlen + last_dot >= buflen)
101                 panic("filename too long");
102         memcpy(buf+last_dot, newext, extlen);
103 }
104
105 static translation_unit_t *do_parsing(const char *fname)
106 {
107         FILE *in = fopen(fname, "r");
108         if(in == NULL) {
109                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
110                 exit(1);
111         }
112
113         lexer_open_stream(in, fname);
114
115         translation_unit_t *unit = parse();
116
117         fclose(in);
118
119         return unit;
120 }
121
122 static void lextest(const char *fname)
123 {
124         FILE *in = fopen(fname, "r");
125         if(in == NULL) {
126                 fprintf(stderr, "Couldn't open '%s': %s\n", fname, strerror(errno));
127                 exit(1);
128         }
129
130         lexer_open_stream(in, fname);
131
132         do {
133                 lexer_next_preprocessing_token();
134                 print_token(stdout, &lexer_token);
135                 puts("");
136         } while(lexer_token.type != T_EOF);
137
138         fclose(in);
139 }
140
141 static void backend(const char *inputname, const char *outname)
142 {
143         FILE *out = fopen(outname, "w");
144         if(out == NULL) {
145                 fprintf(stderr, "couldn't open '%s' for writing: %s\n", outname,
146                                 strerror(errno));
147                 exit(1);
148         }
149
150         be_main(out, inputname);
151
152         fclose(out);
153 }
154
155 static void emit(const char *input_name, const char *out_name)
156 {
157         backend(input_name, out_name);
158 }
159
160 static void link(const char *in, const char *out)
161 {
162         char buf[4096];
163
164         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
165         if(verbose) {
166                 puts(buf);
167         }
168         int err = system(buf);
169         if(err != 0) {
170                 fprintf(stderr, "linker reported an error\n");
171                 exit(1);
172         }
173 }
174
175
176
177 static void create_firm_prog(translation_unit_t *unit)
178 {
179         translation_unit_to_firm(unit);
180
181         int n_irgs = get_irp_n_irgs();
182         for(int i = 0; i < n_irgs; ++i) {
183                 current_ir_graph = get_irp_irg(i);
184                 dump("-start");
185         }
186 }
187
188 void write_fluffy_decls(translation_unit_t *unit);
189
190 int main(int argc, char **argv)
191 {
192         initialize_firm();
193
194         init_symbol_table();
195         init_tokens();
196         init_lexer();
197         init_types();
198         init_typehash();
199         init_ast();
200         init_parser();
201         init_ast2firm();
202
203         if(argc > 2 && strcmp(argv[1], "--lextest") == 0) {
204                 lextest(argv[2]);
205                 return 0;
206         }
207
208         if(argc > 2 && strcmp(argv[1], "--print-ast") == 0) {
209                 translation_unit_t *unit = do_parsing(argv[2]);
210                 ast_set_output(stdout);
211                 print_ast(unit);
212                 return 0;
213         }
214
215         if(argc > 2 && strcmp(argv[1], "--print-fluffy") == 0) {
216                 translation_unit_t *unit = do_parsing(argv[2]);
217                 ast_set_output(stdout);
218                 write_fluffy_decls(unit);
219                 return 0;
220         }
221
222         for(int i = 1; i < argc; ++i) {
223                 const char *input = argv[i];
224                 char        outfname[4096];
225
226                 get_output_name(outfname, sizeof(outfname), input, ".s");
227
228                 translation_unit_t *unit = do_parsing(input);
229                 create_firm_prog(unit);
230                 emit(input, outfname);
231                 link(outfname, "a.out");
232         }
233
234         exit_ast2firm();
235         exit_parser();
236         exit_ast();
237         exit_typehash();
238         exit_types();
239         exit_lexer();
240         exit_tokens();
241         exit_symbol_table();
242         return 0;
243 }