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