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