Adapted to recent changes
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "pseudo_irg.h"
6 #include "irgwalk.h"
7 #include "irprog.h"
8 #include "irprintf.h"
9
10 #include "bitset.h"
11 #include "debug.h"
12
13 #include <obstack.h>
14
15 #ifdef obstack_chunk_alloc
16 # undef obstack_chunk_alloc
17 # define obstack_chunk_alloc malloc
18 #else
19 # define obstack_chunk_alloc malloc
20 # define obstack_chunk_free free
21 #endif
22
23 #include "../bearch.h"                /* the general register allocator interface */
24 #include "bearch_ia32_t.h"
25
26 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
27 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
28 #include "ia32_gen_decls.h"           /* interface declaration emitter */
29 #include "ia32_transform.h"
30 #include "ia32_emitter.h"
31 #include "ia32_map_regs.h"
32
33 #define DEBUG_MODULE "ir.be.isa.ia32"
34
35 /* TODO: ugly */
36 static set *cur_reg_set = NULL;
37
38
39
40 /**************************************************
41  *                         _ _              _  __
42  *                        | | |            (_)/ _|
43  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
44  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
45  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
46  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
47  *            __/ |
48  *           |___/
49  **************************************************/
50
51 static ir_node *my_skip_proj(const ir_node *n) {
52         while (is_Proj(n))
53                 n = get_Proj_pred(n);
54         return (ir_node *)n;
55 }
56
57 static int is_Call_Proj(const ir_node *n) {
58         if (is_Proj(n)                               &&
59                 is_Proj(get_Proj_pred(n))                &&
60                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
61                 is_ia32_Call(get_Proj_pred(get_Proj_pred(n))))
62         {
63                 return 1;
64         }
65
66         return 0;
67 }
68
69 /**
70  * Return register requirements for an ia32 node.
71  * If the node returns a tuple (mode_T) then the proj's
72  * will be asked for this information.
73  */
74 static const arch_register_req_t *ia32_get_irn_reg_req(const arch_irn_ops_t *self, arch_register_req_t *req, const ir_node *irn, int pos) {
75         const ia32_register_req_t *irn_req;
76         long                       node_pos = pos == -1 ? 0 : pos;
77         ir_mode                   *mode     = get_irn_mode(irn);
78         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
79
80         if (mode == mode_T || mode == mode_M) {
81                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
82                 return NULL;
83         }
84
85         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
86
87
88         if (is_Call_Proj(irn)) {
89                 irn_req = ia32_projnum_reg_req_map[get_Proj_proj(irn)];
90                 memcpy(req, &(irn_req->req), sizeof(*req));
91                 return req;
92         }
93         else if (is_Proj(irn)) {
94                 if (pos == -1) {
95                         node_pos = ia32_translate_proj_pos(irn);
96                 }
97                 else {
98                         node_pos = pos;
99                 }
100
101                 irn = my_skip_proj(irn);
102
103                 DBG((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
104         }
105
106         if (is_ia32_irn(irn)) {
107                 if (pos >= 0) {
108                         irn_req = get_ia32_in_req(irn, pos);
109                 }
110                 else {
111                         irn_req = get_ia32_out_req(irn, node_pos);
112                 }
113
114                 DBG((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
115
116                 memcpy(req, &(irn_req->req), sizeof(*req));
117
118                 if (arch_register_req_is(&(irn_req->req), should_be_same) ||
119                         arch_register_req_is(&(irn_req->req), should_be_different)) {
120                         assert(irn_req->pos >= 0 && "should be same/different constraint for in -> out NYI");
121                         req->other = get_irn_n(irn, irn_req->pos);
122                 }
123         }
124         else {
125                 /* treat Phi like Const with default requirements */
126                 if (is_Phi(irn)) {
127                         DBG((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
128                         if (mode_is_float(mode))
129                                 memcpy(req, &(ia32_default_req_ia32_floating_point.req), sizeof(*req));
130                         else if (mode_is_int(mode) || mode_is_reference(mode))
131                                 memcpy(req, &(ia32_default_req_ia32_general_purpose.req), sizeof(*req));
132                         else if (mode == mode_T || mode == mode_M) {
133                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
134                                 return NULL;
135                         }
136                         else
137                                 assert(0 && "unsupported Phi-Mode");
138                 }
139                 else if (get_irn_op(irn) == op_Start) {
140                         DBG((mod, LEVEL_1, "returning reqs none for ProjX -> Start (%+F )\n", irn));
141                         switch (node_pos) {
142                                 case pn_Start_X_initial_exec:
143                                 case pn_Start_P_value_arg_base:
144                                 case pn_Start_P_globals:
145                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
146                                         break;
147                                 case pn_Start_P_frame_base:
148                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
149                                         break;
150                                 case pn_Start_T_args:
151                                         assert(0 && "ProjT(pn_Start_T_args) should not be asked");
152                         }
153                 }
154                 else if (get_irn_op(irn) == op_Return && pos > 0) {
155                         DBG((mod, LEVEL_1, "returning reqs EAX for %+F\n", irn));
156                         memcpy(req, &(ia32_default_req_ia32_general_purpose_eax.req), sizeof(*req));
157                 }
158                 else {
159                         DBG((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
160                         req = NULL;
161                 }
162         }
163
164         return req;
165 }
166
167 static void ia32_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg) {
168         int pos = 0;
169
170         if (is_Call_Proj(irn)) {
171                 /* don't skip the proj, we want to take the else below */
172         }
173         if (is_Proj(irn)) {
174                 pos = ia32_translate_proj_pos(irn);
175                 irn = my_skip_proj(irn);
176         }
177
178         if (is_ia32_irn(irn)) {
179                 const arch_register_t **slots;
180
181                 slots      = get_ia32_slots(irn);
182                 slots[pos] = reg;
183         }
184         else {
185                 ia32_set_firm_reg(irn, reg, cur_reg_set);
186         }
187 }
188
189 static const arch_register_t *ia32_get_irn_reg(const arch_irn_ops_t *self, const ir_node *irn) {
190         int pos = 0;
191         const arch_register_t *reg = NULL;
192
193         if (is_Call_Proj(irn)) {
194                 /* don't skip the proj, we want to take the else below */
195         }
196         else if (is_Proj(irn)) {
197                 pos = ia32_translate_proj_pos(irn);
198                 irn = my_skip_proj(irn);
199         }
200
201         if (is_ia32_irn(irn)) {
202                 const arch_register_t **slots;
203                 slots = get_ia32_slots(irn);
204                 reg   = slots[pos];
205         }
206         else {
207                 reg = ia32_get_firm_reg(irn, cur_reg_set);
208         }
209
210         return reg;
211 }
212
213 static arch_irn_class_t ia32_classify(const arch_irn_ops_t *self, const ir_node *irn) {
214         irn = my_skip_proj(irn);
215         if (is_cfop(irn))
216                 return arch_irn_class_branch;
217         else if (is_ia32_Call(irn))
218                 return arch_irn_class_call;
219         else if (is_ia32_irn(irn))
220                 return arch_irn_class_normal;
221         else
222                 return 0;
223 }
224
225 static arch_irn_flags_t ia32_get_flags(const arch_irn_ops_t *self, const ir_node *irn) {
226         irn = my_skip_proj(irn);
227         if (is_ia32_irn(irn))
228                 return get_ia32_flags(irn);
229         else {
230                 ir_printf("don't know flags of %+F\n", irn);
231                 return 0;
232         }
233 }
234
235 /* fill register allocator interface */
236
237 static const arch_irn_ops_t ia32_irn_ops = {
238         ia32_get_irn_reg_req,
239         ia32_set_irn_reg,
240         ia32_get_irn_reg,
241         ia32_classify,
242         ia32_get_flags
243 };
244
245
246
247 /**************************************************
248  *                _                         _  __
249  *               | |                       (_)/ _|
250  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
251  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
252  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
253  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
254  *                        __/ |
255  *                       |___/
256  **************************************************/
257
258 /**
259  * Transforms the standard firm graph into
260  * an ia32 firm graph
261  */
262 static void ia32_prepare_graph(void *self) {
263         ia32_code_gen_t *cg  = self;
264
265         if (! is_pseudo_ir_graph(cg->irg))
266                 irg_walk_blkwise_graph(cg->irg, NULL, ia32_transform_node, cg);
267 }
268
269
270
271 /**
272  * Dummy functions for hooks we don't need but which must be filled.
273  */
274 static void ia32_before_sched(void *self) {
275 }
276
277 static void ia32_before_ra(void *self) {
278 }
279
280
281
282 /**
283  * Emits the code, closes the output file and frees
284  * the code generator interface.
285  */
286 static void ia32_codegen(void *self) {
287         ia32_code_gen_t *cg = self;
288         ir_graph       *irg = cg->irg;
289         FILE           *out = cg->out;
290
291         if (cg->emit_decls) {
292                 ia32_gen_decls(cg->out);
293                 cg->emit_decls = 0;
294         }
295
296 //      ia32_finish_irg(irg);
297         ia32_gen_routine(out, irg, cg->arch_env);
298
299         cur_reg_set = NULL;
300
301         /* de-allocate code generator */
302         del_set(cg->reg_set);
303         free(self);
304 }
305
306 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
307
308 static const arch_code_generator_if_t ia32_code_gen_if = {
309         ia32_cg_init,
310         ia32_prepare_graph,
311         ia32_before_sched,   /* before scheduling hook */
312         ia32_before_ra,      /* before register allocation hook */
313         ia32_codegen         /* emit && done */
314 };
315
316 /**
317  * Initializes the code generator.
318  */
319 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
320         ia32_isa_t      *isa = (ia32_isa_t *)arch_env->isa;
321         ia32_code_gen_t *cg  = malloc(sizeof(*cg));
322
323         cg->impl       = &ia32_code_gen_if;
324         cg->irg        = irg;
325         cg->reg_set    = new_set(ia32_cmp_irn_reg_assoc, 1024);
326         cg->mod        = firm_dbg_register("be.transform.ia32");
327         cg->out        = F;
328         cg->arch_env   = arch_env;
329
330         isa->num_codegens++;
331
332         if (isa->num_codegens > 1)
333                 cg->emit_decls = 0;
334         else
335                 cg->emit_decls = 1;
336
337         cur_reg_set = cg->reg_set;
338
339         return (arch_code_generator_t *)cg;
340 }
341
342
343
344 /*****************************************************************
345  *  ____             _                  _   _____  _____
346  * |  _ \           | |                | | |_   _|/ ____|  /\
347  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
348  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
349  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
350  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
351  *
352  *****************************************************************/
353
354 /**
355  * Initializes the backend ISA and opens the output file.
356  */
357 static void *ia32_init(void) {
358         static int inited = 0;
359         ia32_isa_t *isa   = malloc(sizeof(*isa));
360
361         isa->impl = &ia32_isa_if;
362
363         if(inited)
364                 return NULL;
365
366         inited = 1;
367
368         isa->num_codegens    = 0;
369         isa->reg_projnum_map = new_set(ia32_cmp_reg_projnum_assoc, 1024);
370
371         ia32_register_init(isa);
372         ia32_create_opcodes();
373
374         return isa;
375 }
376
377
378
379 /**
380  * Closes the output file and frees the ISA structure.
381  */
382 static void ia32_done(void *self) {
383         free(self);
384 }
385
386
387
388 static int ia32_get_n_reg_class(const void *self) {
389         return N_CLASSES;
390 }
391
392 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
393         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
394         return &ia32_reg_classes[i];
395 }
396
397 static const arch_irn_ops_t *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
398         return &ia32_irn_ops;
399 }
400
401 const arch_irn_handler_t ia32_irn_handler = {
402         ia32_get_irn_ops
403 };
404
405 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
406         return &ia32_irn_handler;
407 }
408
409 long ia32_get_call_projnum_for_reg(const void *self, const arch_register_t *reg) {
410         ia32_isa_t *isa = (ia32_isa_t *)self;
411         return ia32_get_reg_projnum(reg, isa->reg_projnum_map);
412 }
413
414 /**
415  * Initializes the code generator interface.
416  */
417 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
418         return &ia32_code_gen_if;
419 }
420
421 /**
422  * Returns the default scheduler
423  */
424 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
425         return reg_pressure_selector;
426 }
427
428 #ifdef WITH_LIBCORE
429 static void ia32_register_options(lc_opt_entry_t *ent)
430 {
431 }
432 #endif /* WITH_LIBCORE */
433
434 const arch_isa_if_t ia32_isa_if = {
435 #ifdef WITH_LIBCORE
436         ia32_register_options,
437 #endif
438         ia32_init,
439         ia32_done,
440         ia32_get_n_reg_class,
441         ia32_get_reg_class,
442         ia32_get_irn_handler,
443         ia32_get_code_generator_if,
444         ia32_get_list_sched_selector,
445         ia32_get_call_projnum_for_reg
446 };