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