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