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