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