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