6dd6b78b94e65d859f151aa89f084c4bffd14e7b
[libfirm] / ir / be / ppc32 / bearch_ppc32.c
1 /* The main ppc backend driver file. */
2 /* $Id$ */
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #include "pseudo_irg.h"
9 #include "irgwalk.h"
10 #include "irprog.h"
11 #include "irprintf.h"
12 #include "ircons.h"
13 #include "irgmod.h"
14 #include "irdump.h"
15
16 #include "bitset.h"
17 #include "debug.h"
18
19 #include "../bearch.h"                /* the general register allocator interface */
20 #include "../benode_t.h"
21 #include "../belower.h"
22 #include "../besched_t.h"
23 #include "../be.h"
24 #include "../beabi.h"
25
26 #include "pset.h"
27
28 #include "bearch_ppc32_t.h"
29
30 #include "ppc32_new_nodes.h"           /* ppc nodes interface */
31 #include "gen_ppc32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
32 #include "ppc32_gen_decls.h"           /* interface declaration emitter */
33 #include "ppc32_transform.h"
34 #include "ppc32_transform_conv.h"
35 #include "ppc32_emitter.h"
36 #include "ppc32_map_regs.h"
37
38 #define DEBUG_MODULE "firm.be.ppc.isa"
39
40 int isleaf;
41 pset *symbol_pset = NULL;
42
43 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
44 static set *cur_reg_set = NULL;
45
46 /**************************************************
47  *                         _ _              _  __
48  *                        | | |            (_)/ _|
49  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
50  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
51  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
52  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
53  *            __/ |
54  *           |___/
55  **************************************************/
56
57 static ir_node *my_skip_proj(const ir_node *n) {
58         while (is_Proj(n))
59                 n = get_Proj_pred(n);
60         return (ir_node *)n;
61 }
62
63 /**
64  * Return register requirements for a ppc node.
65  * If the node returns a tuple (mode_T) then the proj's
66  * will be asked for this information.
67  */
68 static const arch_register_req_t *ppc32_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
69         const ppc32_register_req_t *irn_req;
70         long               node_pos = pos == -1 ? 0 : pos;
71         ir_mode           *mode     = get_irn_mode(irn);
72         FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
73
74         if (is_Block(irn) || mode == mode_X || mode == mode_M) {
75                 DBG((mod, LEVEL_1, "ignoring block, mode_X or mode_M node %+F\n", irn));
76                 return NULL;
77         }
78
79         if (mode == mode_T && pos < 0) {
80                 DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F", irn));
81                 return NULL;
82         }
83
84         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
85
86         if (is_Proj(irn)) {
87                 /* in case of a proj, we need to get the correct OUT slot */
88                 /* of the node corresponding to the proj number */
89                 if (pos == -1) {
90                         node_pos = ppc32_translate_proj_pos(irn);
91                 }
92                 else {
93                         node_pos = pos;
94                 }
95
96                 irn = my_skip_proj(irn);
97
98                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
99         }
100
101         /* get requirements for our own nodes */
102         if (is_ppc32_irn(irn)) {
103                 if (pos >= 0) {
104                         irn_req = get_ppc32_in_req(irn, pos);
105                 }
106                 else {
107                         irn_req = get_ppc32_out_req(irn, node_pos);
108                 }
109
110                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
111
112                 memcpy(req, &(irn_req->req), sizeof(*req));
113
114                 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
115                         assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
116                         req->other_same = get_irn_n(irn, irn_req->same_pos);
117                 }
118
119                 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
120                         assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
121                         req->other_different = get_irn_n(irn, irn_req->different_pos);
122                 }
123         }
124         /* get requirements for FIRM nodes */
125         else {
126                 /* treat Phi like Const with default requirements */
127                 if (is_Phi(irn)) {
128                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
129
130                         if (mode_is_float(mode)) {
131                                 memcpy(req, &(ppc32_default_req_ppc32_fp.req), sizeof(*req));
132                         }
133                         else if (mode_is_int(mode) || mode_is_reference(mode)) {
134                                 memcpy(req, &(ppc32_default_req_ppc32_gp.req), sizeof(*req));
135                         }
136                         else if (mode == mode_T || mode == mode_M) {
137                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
138                                 return NULL;
139                         }
140                         else {
141                                 assert(0 && "unsupported Phi-Mode");
142                         }
143                 }
144                 else {
145                         DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
146                         req = NULL;
147                 }
148         }
149
150         return req;
151 }
152
153 static void ppc32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
154         int pos = 0;
155
156         if (is_Proj(irn)) {
157
158                 if (get_irn_mode(irn) == mode_X) {
159                         return;
160                 }
161
162                 pos = ppc32_translate_proj_pos(irn);
163                 irn = my_skip_proj(irn);
164         }
165
166         if (is_ppc32_irn(irn)) {
167                 const arch_register_t **slots;
168
169                 slots      = get_ppc32_slots(irn);
170                 slots[pos] = reg;
171         }
172         else {
173                 /* here we set the registers for the Phi nodes */
174                 ppc32_set_firm_reg(irn, reg, cur_reg_set);
175         }
176 }
177
178 static const arch_register_t *ppc32_get_irn_reg(const void *self, const ir_node *irn) {
179         int pos = 0;
180         const arch_register_t *reg = NULL;
181
182         if (is_Proj(irn)) {
183
184                 if (get_irn_mode(irn) == mode_X) {
185                         return NULL;
186                 }
187
188                 pos = ppc32_translate_proj_pos(irn);
189                 irn = my_skip_proj(irn);
190         }
191
192         if (is_ppc32_irn(irn)) {
193                 const arch_register_t **slots;
194                 slots = get_ppc32_slots(irn);
195                 reg   = slots[pos];
196         }
197         else {
198                 reg = ppc32_get_firm_reg(irn, cur_reg_set);
199         }
200
201         return reg;
202 }
203
204 static arch_irn_class_t ppc32_classify(const void *self, const ir_node *irn) {
205         irn = my_skip_proj(irn);
206
207         if (is_cfop(irn)) {
208                 return arch_irn_class_branch;
209         }
210         else if (is_ppc32_irn(irn)) {
211                 return arch_irn_class_normal;
212         }
213
214         return 0;
215 }
216
217 static arch_irn_flags_t ppc32_get_flags(const void *self, const ir_node *irn) {
218         irn = my_skip_proj(irn);
219
220         if (is_ppc32_irn(irn)) {
221                 return get_ppc32_flags(irn);
222         }
223         else if (is_Unknown(irn)) {
224                 return arch_irn_flags_ignore;
225         }
226
227         return 0;
228 }
229
230 static entity *ppc32_get_frame_entity(const void *self, const ir_node *irn) {
231         if(!is_ppc32_irn(irn)) return NULL;
232         if(get_ppc32_type(irn)!=ppc32_ac_FrameEntity) return NULL;
233         return get_ppc32_frame_entity(irn);
234 }
235
236 static void ppc32_set_frame_entity(const void *self, const ir_node *irn, entity *ent) {
237         if (! is_ppc32_irn(irn) || get_ppc32_type(irn) != ppc32_ac_FrameEntity)
238                 return;
239         set_ppc32_frame_entity(irn, ent);
240 }
241
242 /**
243  * This function is called by the generic backend to correct offsets for
244  * nodes accessing the stack.
245  */
246 static void ppc32_set_stack_bias(const void *self, ir_node *irn, int bias) {
247         set_ppc32_offset(irn, bias);
248 }
249
250 static int ppc32_get_sp_bias(const void *self, const ir_node *irn) {
251         return 0;
252 }
253
254 typedef struct
255 {
256         const be_abi_call_t *call;
257         ir_graph *irg;
258 } ppc32_abi_env;
259
260 /**
261  * Initialize the callback object.
262  * @param call The call object.
263  * @param aenv The architecture environment.
264  * @param irg  The graph with the method.
265  * @return     Some pointer. This pointer is passed to all other callback functions as self object.
266  */
267 static void *ppc32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
268 {
269         ppc32_abi_env *env = xmalloc(sizeof(ppc32_abi_env));
270         env->call = call;
271         env->irg = irg;
272         return env;
273 }
274
275 /**
276  * Destroy the callback object.
277  * @param self The callback object.
278  */
279 static void ppc32_abi_done(void *self)
280 {
281         free(self);
282 }
283
284 /**
285  * Get the between type for that call.
286  * @param self The callback object.
287  * @return The between type of for that call.
288  */
289 static ir_type *ppc32_abi_get_between_type(void *self)
290 {
291         static ir_type *between_type = NULL;
292         static entity *old_bp_ent    = NULL;
293
294         if(!between_type) {
295                 entity *ret_addr_ent;
296                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
297                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
298
299                 between_type           = new_type_class(new_id_from_str("ppc32_between_type"));
300                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
301                 ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
302
303                 set_entity_offset_bytes(old_bp_ent, 0);
304                 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
305                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
306         }
307
308         return between_type;
309 }
310
311 /**
312  * Put all registers which are saved by the prologue/epilogue in a set.
313  * @param self The callback object.
314  * @param regs A set.
315  */
316 static void ppc32_abi_regs_saved_by_me(void *self, pset *regs)
317 {
318 }
319
320 /**
321  * Generate the prologue.
322  * @param self    The callback object.
323  * @param mem     A pointer to the mem node. Update this if you define new memory.
324  * @param reg_map A mapping mapping all callee_save/ignore/parameter registers to their defining nodes.
325  * @return        The register which shall be used as a stack frame base.
326  *
327  * All nodes which define registers in @p reg_map must keep @p reg_map current.
328  */
329 static const arch_register_t *ppc32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
330 {
331         ppc32_abi_env *env = (ppc32_abi_env *) self;
332         be_abi_call_flags_t flags = be_abi_call_get_flags(env->call);
333         isleaf = flags.bits.irg_is_leaf;
334
335         if(flags.bits.try_omit_fp)
336                 return &ppc32_gp_regs[REG_R1];
337         else
338                 return &ppc32_gp_regs[REG_R31];
339 }
340
341 /**
342  * Generate the epilogue.
343  * @param self    The callback object.
344  * @param mem     Memory one can attach to.
345  * @param reg_map A mapping mapping all callee_save/ignore/return registers to their defining nodes.
346  *
347  * All nodes which define registers in @p reg_map must keep @p reg_map current.
348  * Also, the @p mem variable must be updated, if memory producing nodes are inserted.
349  */
350 static void ppc32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
351 {
352 }
353
354 static const be_abi_callbacks_t ppc32_abi_callbacks = {
355         ppc32_abi_init,
356         ppc32_abi_done,
357         ppc32_abi_get_between_type,
358         ppc32_abi_regs_saved_by_me,
359         ppc32_abi_prologue,
360         ppc32_abi_epilogue,
361 };
362
363 /* fill register allocator interface */
364
365 static const arch_irn_ops_if_t ppc32_irn_ops_if = {
366         ppc32_get_irn_reg_req,
367         ppc32_set_irn_reg,
368         ppc32_get_irn_reg,
369         ppc32_classify,
370         ppc32_get_flags,
371         ppc32_get_frame_entity,
372         ppc32_set_frame_entity,
373         ppc32_set_stack_bias,
374         ppc32_get_sp_bias,
375         NULL,    /* get_inverse             */
376         NULL,    /* get_op_estimated_cost   */
377         NULL,    /* possible_memory_operand */
378         NULL,    /* perform_memory_operand  */
379 };
380
381 ppc32_irn_ops_t ppc32_irn_ops = {
382         &ppc32_irn_ops_if,
383         NULL
384 };
385
386
387
388 /**************************************************
389  *                _                         _  __
390  *               | |                       (_)/ _|
391  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
392  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
393  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
394  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
395  *                        __/ |
396  *                       |___/
397  **************************************************/
398
399 static void ppc32_before_abi(void *self) {
400         ppc32_code_gen_t *cg = self;
401         ir_type *frame_type = get_irg_frame_type(cg->irg);
402
403         frame_alloc_area(frame_type, 24, 4, 1);
404
405         ppc32_init_conv_walk();
406         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_conv_walk, cg);
407
408         if (cg->area_size) {
409                 if(cg->area_size < 32) cg->area_size = 32;
410                 cg->area = frame_alloc_area(get_irg_frame_type(cg->irg), cg->area_size+24, 16, 1);
411         }
412 }
413
414 static void ppc32_search_start_successor(ir_node *block, void *env) {
415         ppc32_code_gen_t *cg = env;
416         int n = get_Block_n_cfgpreds(block);
417         ir_node *startblock = get_irg_start_block(cg->irg);
418         if(block == startblock) return;
419
420         for (n--; n >= 0; n--) {
421                 ir_node *predblock = get_irn_n(get_Block_cfgpred(block, n), -1);
422                 if(predblock == startblock)
423                 {
424                         cg->start_succ_block = block;
425                         return;
426                 }
427         }
428 }
429
430 /**
431  * Transforms the standard firm graph into
432  * a ppc firm graph
433  */
434 static void ppc32_prepare_graph(void *self) {
435         ppc32_code_gen_t *cg = self;
436
437         irg_block_walk_graph(cg->irg, NULL, ppc32_search_start_successor, cg);
438         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_pretransform_walk, cg);
439         be_dump(cg->irg, "-pretransformed", dump_ir_block_graph);
440
441         ppc32_register_transformers();
442         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_node, cg);
443         be_dump(cg->irg, "-transformed", dump_ir_block_graph);
444         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_const, cg);
445 }
446
447
448
449 /**
450  * Called immediatly before emit phase.
451  */
452 static void ppc32_finish_irg(void *self) {
453         /* TODO: - fix offsets for nodes accessing stack
454                          - ...
455         */
456 }
457
458
459 /**
460  * These are some hooks which must be filled but are probably not needed.
461  */
462 static void ppc32_before_sched(void *self) {
463         /* Some stuff you need to do after scheduling but before register allocation */
464 }
465
466 /**
467  * Called before the register allocator.
468  * Calculate a block schedule here. We need it for the x87
469  * simulator and the emitter.
470  */
471 static void ppc32_before_ra(void *self) {
472         ppc32_code_gen_t *cg = self;
473         cg->blk_sched = sched_create_block_schedule(cg->irg, cg->birg->execfreqs);
474 }
475
476 static void ppc32_transform_spill(ir_node *node, void *env)
477 {
478         ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
479
480         if(be_is_Spill(node))
481         {
482                 ir_node  *store, *proj;
483                 dbg_info *dbg   = get_irn_dbg_info(node);
484                 ir_node  *block = get_nodes_block(node);
485
486                 const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, 1);
487
488                 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
489                 {
490                         store = new_rd_ppc32_Stw(dbg, current_ir_graph, block,
491                                 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
492                 }
493                 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
494                 {
495                         store = new_rd_ppc32_Stfd(dbg, current_ir_graph, block,
496                                 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
497                 }
498                 else assert(0 && "Spill for register class not supported yet!");
499
500                 set_ppc32_frame_entity(store, be_get_frame_entity(node));
501
502                 proj = new_rd_Proj(dbg, current_ir_graph, block, store, mode_M, pn_Store_M);
503
504                 if (sched_is_scheduled(node)) {
505                         sched_add_after(sched_prev(node), store);
506                         sched_add_after(store, proj);
507
508                         sched_remove(node);
509                 }
510
511                 exchange(node, proj);
512         }
513
514         if(be_is_Reload(node))
515         {
516                 ir_node *load, *proj;
517                 const arch_register_t *reg;
518                 dbg_info *dbg   = get_irn_dbg_info(node);
519                 ir_node  *block = get_nodes_block(node);
520                 ir_mode  *mode  = get_irn_mode(node);
521
522                 const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, -1);
523
524                 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
525                 {
526                         load = new_rd_ppc32_Lwz(dbg, current_ir_graph, block,   get_irn_n(node, 0), get_irn_n(node, 1));
527                 }
528                 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
529                 {
530                         load = new_rd_ppc32_Lfd(dbg, current_ir_graph, block,   get_irn_n(node, 0), get_irn_n(node, 1));
531                 }
532                 else assert(0 && "Reload for register class not supported yet!");
533
534                 set_ppc32_frame_entity(load, be_get_frame_entity(node));
535
536                 proj = new_rd_Proj(dbg, current_ir_graph, block, load, mode, pn_Load_res);
537
538                 if (sched_is_scheduled(node)) {
539                         sched_add_after(sched_prev(node), load);
540                         sched_add_after(load, proj);
541
542                         sched_remove(node);
543                 }
544
545                 /* copy the register from the old node to the new Load */
546                 reg = arch_get_irn_register(cgenv->arch_env, node);
547                 arch_set_irn_register(cgenv->arch_env, load, reg);
548
549                 exchange(node, proj);
550         }
551 }
552
553 /**
554  * Some stuff to do immediately after register allocation
555  */
556 static void ppc32_after_ra(void *self) {
557         ppc32_code_gen_t *cg = self;
558         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_spill, cg);
559 }
560
561 /**
562  * Emits the code, closes the output file and frees
563  * the code generator interface.
564  */
565 static void ppc32_emit_and_done(void *self) {
566         ppc32_code_gen_t *cg = self;
567         ir_graph           *irg = cg->irg;
568         FILE               *out = cg->isa->out;
569
570         if (cg->emit_decls) {
571                 ppc32_gen_decls(out);
572                 cg->emit_decls = 0;
573         }
574
575         dump_ir_block_graph_sched(irg, "-ppc-finished");
576         ppc32_gen_routine(out, irg, cg);
577
578         cur_reg_set = NULL;
579
580         /* de-allocate code generator */
581         del_set(cg->reg_set);
582         free(self);
583
584         if(symbol_pset)
585         {
586                 del_pset(symbol_pset);
587                 symbol_pset = NULL;
588         }
589 }
590
591 int is_direct_entity(entity *ent);
592
593 /**
594  * Collects all SymConsts which need to be accessed "indirectly"
595  *
596  * @param node    the firm node
597  * @param env     the debug module
598  */
599 void ppc32_collect_symconsts_walk(ir_node *node, void *env) {
600         if(get_irn_op(node) == op_SymConst)
601         {
602                 entity *ent = get_SymConst_entity(node);
603                 if(!is_direct_entity(ent))
604                         pset_insert_ptr(symbol_pset, ent);
605         }
606 }
607
608 static void *ppc32_cg_init(const be_irg_t *birg);
609
610 static const arch_code_generator_if_t ppc32_code_gen_if = {
611         ppc32_cg_init,
612         ppc32_before_abi,
613         ppc32_prepare_graph,
614         ppc32_before_sched,   /* before scheduling hook */
615         ppc32_before_ra,      /* before register allocation hook */
616         ppc32_after_ra,
617         ppc32_finish_irg,
618         ppc32_emit_and_done
619 };
620
621 /**
622  * Initializes the code generator.
623  */
624 static void *ppc32_cg_init(const be_irg_t *birg) {
625         ppc32_isa_t      *isa = (ppc32_isa_t *)birg->main_env->arch_env->isa;
626         ppc32_code_gen_t *cg  = xmalloc(sizeof(*cg));
627
628         cg->impl      = &ppc32_code_gen_if;
629         cg->irg       = birg->irg;
630         cg->reg_set   = new_set(ppc32_cmp_irn_reg_assoc, 1024);
631         cg->arch_env  = birg->main_env->arch_env;
632         cg->isa       = isa;
633         cg->birg      = birg;
634         cg->area_size = 0;
635         cg->area      = NULL;
636         cg->start_succ_block = NULL;
637         cg->blk_sched = NULL;
638         FIRM_DBG_REGISTER(cg->mod, "firm.be.ppc.cg");
639
640         isa->num_codegens++;
641
642         if (isa->num_codegens > 1)
643                 cg->emit_decls = 0;
644         else
645         {
646                 int i;
647                 cg->emit_decls = 1;
648                 symbol_pset = pset_new_ptr(8);
649                 for(i=0; i<get_irp_n_irgs(); i++)
650                 {
651                         cg->irg = get_irp_irg(i);
652                         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_collect_symconsts_walk, cg);
653                 }
654                 cg->irg = birg->irg;
655         }
656
657         cur_reg_set = cg->reg_set;
658
659         ppc32_irn_ops.cg = cg;
660
661         return (arch_code_generator_t *)cg;
662 }
663
664
665
666 /*****************************************************************
667  *  ____             _                  _   _____  _____
668  * |  _ \           | |                | | |_   _|/ ____|  /\
669  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
670  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
671  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
672  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
673  *
674  *****************************************************************/
675
676 static ppc32_isa_t ppc32_isa_template = {
677         &ppc32_isa_if,
678         &ppc32_gp_regs[REG_R1],  // stack pointer
679         &ppc32_gp_regs[REG_R31], // base pointer
680         -1,                                   // stack is decreasing
681         0,                                    // num codegens... ??
682         NULL
683 };
684
685 /**
686  * Initializes the backend ISA and opens the output file.
687  */
688 static void *ppc32_init(FILE *file_handle) {
689         static int inited = 0;
690         ppc32_isa_t *isa;
691
692         if(inited)
693                 return NULL;
694
695         isa = xmalloc(sizeof(*isa));
696         memcpy(isa, &ppc32_isa_template, sizeof(*isa));
697
698         isa->out = file_handle;
699
700         ppc32_register_init(isa);
701         ppc32_create_opcodes();
702
703         inited = 1;
704
705         return isa;
706 }
707
708
709
710 /**
711  * Closes the output file and frees the ISA structure.
712  */
713 static void ppc32_done(void *self) {
714         free(self);
715 }
716
717
718
719 static int ppc32_get_n_reg_class(const void *self) {
720         return N_CLASSES;
721 }
722
723 static const arch_register_class_t *ppc32_get_reg_class(const void *self, int i) {
724         assert(i >= 0 && i < N_CLASSES && "Invalid ppc register class requested.");
725         return &ppc32_reg_classes[i];
726 }
727
728
729
730 /**
731  * Get the register class which shall be used to store a value of a given mode.
732  * @param self The this pointer.
733  * @param mode The mode in question.
734  * @return A register class which can hold values of the given mode.
735  */
736 const arch_register_class_t *ppc32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
737         if (mode_is_float(mode))
738                 return &ppc32_reg_classes[CLASS_ppc32_fp];
739         else
740                 return &ppc32_reg_classes[CLASS_ppc32_gp];
741 }
742
743
744 /**
745  * Get the ABI restrictions for procedure calls.
746  * @param self        The this pointer.
747  * @param method_type The type of the method (procedure) in question.
748  * @param abi         The abi object to be modified
749  */
750 static void ppc32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
751         ir_type  *tp;
752         ir_mode  *mode;
753         int       i, n = get_method_n_params(method_type);
754         int               stackoffs = 0, lastoffs = 0, stackparamsize;
755
756         int               gpregi = REG_R3;
757         int               fpregi = REG_F1;
758
759         const arch_register_t *reg;
760         be_abi_call_flags_t call_flags = { { 0, 0, 1, 0, 0, 0, 1 } };
761
762         if(get_type_visibility(method_type)!=visibility_external_allocated)
763                 call_flags.bits.call_has_imm = 1;
764
765         /* set stack parameter passing style */
766         be_abi_call_set_flags(abi, call_flags, &ppc32_abi_callbacks);
767
768         for (i = 0; i < n; i++) {
769                 tp   = get_method_param_type(method_type, i);
770                 if(is_atomic_type(tp))
771                 {
772                         mode = get_type_mode(tp);
773
774                         if(mode_is_float(mode))
775                         {
776                                 if(fpregi <= REG_F13)
777                                 {
778                                         if(get_mode_size_bits(mode) == 32) gpregi++, stackparamsize=4;
779                                         else gpregi += 2, stackparamsize=8;                                                             // mode == irm_D
780                                         reg = &ppc32_fp_regs[fpregi++];
781                                 }
782                                 else
783                                 {
784                                         if(get_mode_size_bits(mode) == 32) stackparamsize=4;
785                                         else stackparamsize=8;                                                          // mode == irm_D
786                                         reg = NULL;
787                                 }
788                         }
789                         else
790                         {
791                                 if(gpregi <= REG_R10)
792                                         reg = &ppc32_gp_regs[gpregi++];
793                                 else
794                                         reg = NULL;
795                                 stackparamsize=4;
796                         }
797
798                         if(reg)
799                                 be_abi_call_param_reg(abi, i, reg);
800                         else
801                         {
802                                 be_abi_call_param_stack(abi, i, 4, stackoffs-lastoffs, 0);
803                                 lastoffs = stackoffs+stackparamsize;
804                         }
805                         stackoffs += stackparamsize;
806                 }
807                 else
808                 {
809                         be_abi_call_param_stack(abi, i, 4, stackoffs-lastoffs, 0);
810                         stackoffs += (get_type_size_bytes(tp)+3) & -4;
811                         lastoffs = stackoffs;
812                 }
813         }
814
815         /* explain where result can be found if any */
816         if (get_method_n_ress(method_type) > 0) {
817                 tp   = get_method_res_type(method_type, 0);
818                 mode = get_type_mode(tp);
819
820                 be_abi_call_res_reg(abi, 0,
821                         mode_is_float(mode) ? &ppc32_fp_regs[REG_F1] : &ppc32_gp_regs[REG_R3]);
822         }
823 }
824
825 static const void *ppc32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
826         return &ppc32_irn_ops;
827 }
828
829 const arch_irn_handler_t ppc32_irn_handler = {
830         ppc32_get_irn_ops
831 };
832
833 const arch_irn_handler_t *ppc32_get_irn_handler(const void *self) {
834         return &ppc32_irn_handler;
835 }
836
837 int ppc32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
838         return is_ppc32_irn(irn);
839 }
840
841 /**
842  * Initializes the code generator interface.
843  */
844 static const arch_code_generator_if_t *ppc32_get_code_generator_if(void *self) {
845         return &ppc32_code_gen_if;
846 }
847
848 list_sched_selector_t ppc32_sched_selector;
849
850 /**
851  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
852  */
853 static const list_sched_selector_t *ppc32_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
854         memcpy(&ppc32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
855         ppc32_sched_selector.to_appear_in_schedule = ppc32_to_appear_in_schedule;
856         return &ppc32_sched_selector;
857 }
858
859 /**
860  * Returns the necessary byte alignment for storing a register of given class.
861  */
862 static int ppc32_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
863         ir_mode *mode = arch_register_class_mode(cls);
864         return get_mode_size_bytes(mode);
865 }
866
867 /**
868  * Returns the libFirm configuration parameter for this backend.
869  */
870 static const backend_params *ppc32_get_libfirm_params(void) {
871         static arch_dep_params_t ad = {
872                 1,  /* allow subs */
873                 0,      /* Muls are fast enough on ARM */
874                 31, /* shift would be ok */
875                 0,  /* SMUL is needed, only in Arch M*/
876                 0,  /* UMUL is needed, only in Arch M */
877                 32, /* SMUL & UMUL available for 32 bit */
878         };
879         static backend_params p = {
880                 NULL,  /* no additional opcodes */
881                 NULL,  /* will be set later */
882                 1,     /* need dword lowering */
883                 NULL,  /* but yet no creator function */
884                 NULL,  /* context for create_intrinsic_fkt */
885         };
886
887         p.dep_param = &ad;
888         return &p;
889 }
890
891 #ifdef WITH_LIBCORE
892 static void ppc32_register_options(lc_opt_entry_t *ent)
893 {
894 }
895 #endif /* WITH_LIBCORE */
896
897 const arch_isa_if_t ppc32_isa_if = {
898         ppc32_init,
899         ppc32_done,
900         ppc32_get_n_reg_class,
901         ppc32_get_reg_class,
902         ppc32_get_reg_class_for_mode,
903         ppc32_get_call_abi,
904         ppc32_get_irn_handler,
905         ppc32_get_code_generator_if,
906         ppc32_get_list_sched_selector,
907         ppc32_get_reg_class_alignment,
908         ppc32_get_libfirm_params,
909 #ifdef WITH_LIBCORE
910         ppc32_register_options
911 #endif
912 };