fixed warnings (unused variable)
[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         (void) self;
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(const void *self, ir_node *irn, const arch_register_t *reg) {
143         int pos = 0;
144         (void) self;
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 void *self, const ir_node *irn) {
169         int pos = 0;
170         const arch_register_t *reg = NULL;
171         (void) self;
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 void *self, const ir_node *irn) {
196         (void) self;
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 void *self, const ir_node *irn) {
210         (void) self;
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 void *self, const ir_node *irn) {
224         (void) self;
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(const void *self, ir_node *irn, ir_entity *ent) {
231         (void) self;
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(const void *self, ir_node *irn, int bias) {
242         (void) self;
243         set_ppc32_offset(irn, bias);
244 }
245
246 static int ppc32_get_sp_bias(const void *self, const ir_node *irn) {
247         (void) self;
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         (void) aenv;
268         ppc32_abi_env *env = xmalloc(sizeof(ppc32_abi_env));
269         env->call = call;
270         env->irg = irg;
271         return env;
272 }
273
274 /**
275  * Destroy the callback object.
276  * @param self The callback object.
277  */
278 static void ppc32_abi_done(void *self)
279 {
280         free(self);
281 }
282
283 /**
284  * Get the between type for that call.
285  * @param self The callback object.
286  * @return The between type of for that call.
287  */
288 static ir_type *ppc32_abi_get_between_type(void *self)
289 {
290         static ir_type *between_type = NULL;
291         static ir_entity *old_bp_ent = NULL;
292         (void) self;
293
294         if(!between_type) {
295                 ir_entity *ret_addr_ent;
296                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
297                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
298
299                 between_type           = new_type_class(new_id_from_str("ppc32_between_type"));
300                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
301                 ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
302
303                 set_entity_offset(old_bp_ent, 0);
304                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
305                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
306         }
307
308         return between_type;
309 }
310
311 /**
312  * Put all registers which are saved by the prologue/epilogue in a set.
313  * @param self The callback object.
314  * @param regs A set.
315  */
316 static void ppc32_abi_regs_saved_by_me(void *self, pset *regs)
317 {
318         (void) self;
319         (void) regs;
320 }
321
322 /**
323  * Generate the prologue.
324  * @param self    The callback object.
325  * @param mem     A pointer to the mem node. Update this if you define new memory.
326  * @param reg_map A mapping mapping all callee_save/ignore/parameter registers to their defining nodes.
327  * @return        The register which shall be used as a stack frame base.
328  *
329  * All nodes which define registers in @p reg_map must keep @p reg_map current.
330  */
331 static const arch_register_t *ppc32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
332 {
333         ppc32_abi_env *env = (ppc32_abi_env *) self;
334         be_abi_call_flags_t flags = be_abi_call_get_flags(env->call);
335         (void) mem;
336         (void) reg_map;
337         isleaf = flags.bits.irg_is_leaf;
338
339         if(flags.bits.try_omit_fp)
340                 return &ppc32_gp_regs[REG_R1];
341         else
342                 return &ppc32_gp_regs[REG_R31];
343 }
344
345 /**
346  * Generate the epilogue.
347  * @param self    The callback object.
348  * @param mem     Memory one can attach to.
349  * @param reg_map A mapping mapping all callee_save/ignore/return registers to their defining nodes.
350  *
351  * All nodes which define registers in @p reg_map must keep @p reg_map current.
352  * Also, the @p mem variable must be updated, if memory producing nodes are inserted.
353  */
354 static void ppc32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
355 {
356         (void) self;
357         (void) bl;
358         (void) mem;
359         (void) reg_map;
360 }
361
362 static const be_abi_callbacks_t ppc32_abi_callbacks = {
363         ppc32_abi_init,
364         ppc32_abi_done,
365         ppc32_abi_get_between_type,
366         ppc32_abi_regs_saved_by_me,
367         ppc32_abi_prologue,
368         ppc32_abi_epilogue,
369 };
370
371 /* fill register allocator interface */
372
373 static const arch_irn_ops_if_t ppc32_irn_ops_if = {
374         ppc32_get_irn_reg_req,
375         ppc32_set_irn_reg,
376         ppc32_get_irn_reg,
377         ppc32_classify,
378         ppc32_get_flags,
379         ppc32_get_frame_entity,
380         ppc32_set_frame_entity,
381         ppc32_set_stack_bias,
382         ppc32_get_sp_bias,
383         NULL,    /* get_inverse             */
384         NULL,    /* get_op_estimated_cost   */
385         NULL,    /* possible_memory_operand */
386         NULL,    /* perform_memory_operand  */
387 };
388
389 ppc32_irn_ops_t ppc32_irn_ops = {
390         &ppc32_irn_ops_if,
391         NULL
392 };
393
394
395
396 /**************************************************
397  *                _                         _  __
398  *               | |                       (_)/ _|
399  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
400  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
401  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
402  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
403  *                        __/ |
404  *                       |___/
405  **************************************************/
406
407 static void ppc32_before_abi(void *self) {
408         ppc32_code_gen_t *cg = self;
409         ir_type *frame_type = get_irg_frame_type(cg->irg);
410
411         frame_alloc_area(frame_type, 24, 4, 1);
412
413         ppc32_init_conv_walk();
414         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_conv_walk, cg);
415
416         if (cg->area_size) {
417                 if(cg->area_size < 32) cg->area_size = 32;
418                 cg->area = frame_alloc_area(get_irg_frame_type(cg->irg), cg->area_size+24, 16, 1);
419         }
420 }
421
422 static void ppc32_search_start_successor(ir_node *block, void *env) {
423         ppc32_code_gen_t *cg = env;
424         int n = get_Block_n_cfgpreds(block);
425         ir_node *startblock = get_irg_start_block(cg->irg);
426         if(block == startblock) return;
427
428         for (n--; n >= 0; n--) {
429                 ir_node *predblock = get_irn_n(get_Block_cfgpred(block, n), -1);
430                 if(predblock == startblock)
431                 {
432                         cg->start_succ_block = block;
433                         return;
434                 }
435         }
436 }
437
438 /**
439  * Transforms the standard firm graph into
440  * a ppc firm graph
441  */
442 static void ppc32_prepare_graph(void *self) {
443         ppc32_code_gen_t *cg = self;
444
445         irg_block_walk_graph(cg->irg, NULL, ppc32_search_start_successor, cg);
446         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_pretransform_walk, cg);
447         be_dump(cg->irg, "-pretransformed", dump_ir_block_graph);
448
449         ppc32_register_transformers();
450         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_node, cg);
451         be_dump(cg->irg, "-transformed", dump_ir_block_graph);
452         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_const, cg);
453 }
454
455
456
457 /**
458  * Called immediatly before emit phase.
459  */
460 static void ppc32_finish_irg(void *self) {
461         (void) self;
462         /* TODO: - fix offsets for nodes accessing stack
463                          - ...
464         */
465 }
466
467
468 /**
469  * These are some hooks which must be filled but are probably not needed.
470  */
471 static void ppc32_before_sched(void *self) {
472         (void) self;
473         /* Some stuff you need to do after scheduling but before register allocation */
474 }
475
476 /**
477  * Called before the register allocator.
478  * Calculate a block schedule here. We need it for the x87
479  * simulator and the emitter.
480  */
481 static void ppc32_before_ra(void *self) {
482         ppc32_code_gen_t *cg = self;
483         cg->blk_sched = be_create_block_schedule(cg->irg, cg->birg->exec_freq);
484 }
485
486 static void ppc32_transform_spill(ir_node *node, void *env)
487 {
488         ppc32_code_gen_t *cgenv = (ppc32_code_gen_t *)env;
489
490         if(be_is_Spill(node))
491         {
492                 ir_node  *store, *proj;
493                 dbg_info *dbg   = get_irn_dbg_info(node);
494                 ir_node  *block = get_nodes_block(node);
495
496                 const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, 1);
497
498                 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
499                 {
500                         store = new_rd_ppc32_Stw(dbg, current_ir_graph, block,
501                                 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
502                 }
503                 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
504                 {
505                         store = new_rd_ppc32_Stfd(dbg, current_ir_graph, block,
506                                 get_irn_n(node, 0), get_irn_n(node, 1), new_rd_NoMem(current_ir_graph));
507                 }
508                 else assert(0 && "Spill for register class not supported yet!");
509
510                 set_ppc32_frame_entity(store, be_get_frame_entity(node));
511
512                 proj = new_rd_Proj(dbg, current_ir_graph, block, store, mode_M, pn_Store_M);
513
514                 if (sched_is_scheduled(node)) {
515                         sched_add_after(sched_prev(node), store);
516                         sched_add_after(store, proj);
517
518                         sched_remove(node);
519                 }
520
521                 exchange(node, proj);
522         }
523
524         if(be_is_Reload(node))
525         {
526                 ir_node *load, *proj;
527                 const arch_register_t *reg;
528                 dbg_info *dbg   = get_irn_dbg_info(node);
529                 ir_node  *block = get_nodes_block(node);
530                 ir_mode  *mode  = get_irn_mode(node);
531
532                 const arch_register_class_t *regclass = arch_get_irn_reg_class(cgenv->arch_env, node, -1);
533
534                 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
535                 {
536                         load = new_rd_ppc32_Lwz(dbg, current_ir_graph, block,   get_irn_n(node, 0), get_irn_n(node, 1));
537                 }
538                 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
539                 {
540                         load = new_rd_ppc32_Lfd(dbg, current_ir_graph, block,   get_irn_n(node, 0), get_irn_n(node, 1));
541                 }
542                 else assert(0 && "Reload for register class not supported yet!");
543
544                 set_ppc32_frame_entity(load, be_get_frame_entity(node));
545
546                 proj = new_rd_Proj(dbg, current_ir_graph, block, load, mode, pn_Load_res);
547
548                 if (sched_is_scheduled(node)) {
549                         sched_add_after(sched_prev(node), load);
550                         sched_add_after(load, proj);
551
552                         sched_remove(node);
553                 }
554
555                 /* copy the register from the old node to the new Load */
556                 reg = arch_get_irn_register(cgenv->arch_env, node);
557                 arch_set_irn_register(cgenv->arch_env, load, reg);
558
559                 exchange(node, proj);
560         }
561 }
562
563 /**
564  * Some stuff to do immediately after register allocation
565  */
566 static void ppc32_after_ra(void *self) {
567         ppc32_code_gen_t *cg = self;
568         be_coalesce_spillslots(cg->birg);
569         irg_walk_blkwise_graph(cg->irg, NULL, ppc32_transform_spill, cg);
570 }
571
572 /**
573  * Emits the code, closes the output file and frees
574  * the code generator interface.
575  */
576 static void ppc32_emit_and_done(void *self) {
577         ppc32_code_gen_t *cg = self;
578         ir_graph         *irg = cg->irg;
579
580         dump_ir_block_graph_sched(irg, "-ppc-finished");
581         ppc32_gen_routine(cg, irg);
582
583         cur_reg_set = NULL;
584
585         /* de-allocate code generator */
586         del_set(cg->reg_set);
587         free(self);
588 }
589
590 int is_direct_entity(ir_entity *ent);
591
592 static void *ppc32_cg_init(be_irg_t *birg);
593
594 static const arch_code_generator_if_t ppc32_code_gen_if = {
595         ppc32_cg_init,
596         ppc32_before_abi,
597         ppc32_prepare_graph,
598         NULL,                 /* spill */
599         ppc32_before_sched,   /* before scheduling hook */
600         ppc32_before_ra,      /* before register allocation hook */
601         ppc32_after_ra,
602         ppc32_finish_irg,
603         ppc32_emit_and_done
604 };
605
606 /**
607  * Initializes the code generator.
608  */
609 static void *ppc32_cg_init(be_irg_t *birg) {
610         ppc32_isa_t      *isa = (ppc32_isa_t *)birg->main_env->arch_env->isa;
611         ppc32_code_gen_t *cg  = xmalloc(sizeof(*cg));
612
613         cg->impl      = &ppc32_code_gen_if;
614         cg->irg       = birg->irg;
615         cg->reg_set   = new_set(ppc32_cmp_irn_reg_assoc, 1024);
616         cg->arch_env  = birg->main_env->arch_env;
617         cg->isa       = isa;
618         cg->birg      = birg;
619         cg->area_size = 0;
620         cg->area      = NULL;
621         cg->start_succ_block = NULL;
622         cg->blk_sched = NULL;
623         FIRM_DBG_REGISTER(cg->mod, "firm.be.ppc.cg");
624
625         cur_reg_set = cg->reg_set;
626         ppc32_irn_ops.cg = cg;
627
628         return (arch_code_generator_t *)cg;
629 }
630
631
632
633 /*****************************************************************
634  *  ____             _                  _   _____  _____
635  * |  _ \           | |                | | |_   _|/ ____|  /\
636  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
637  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
638  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
639  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
640  *
641  *****************************************************************/
642
643 static ppc32_isa_t ppc32_isa_template = {
644         {
645                 &ppc32_isa_if,           /* isa interface */
646                 &ppc32_gp_regs[REG_R1],  /* stack pointer */
647                 &ppc32_gp_regs[REG_R31], /* base pointer */
648                 -1,                      /* stack is decreasing */
649                 NULL,                    /* main environment */
650                 7,                       /* spill costs */
651                 5,                       /* reload costs */
652         },
653         NULL_EMITTER,           /* emitter environment */
654         NULL                    /* symbol set */
655 };
656
657 /**
658  * Collects all SymConsts which need to be accessed "indirectly"
659  *
660  * @param node    the firm node
661  * @param env     the symbol set
662  */
663 static void ppc32_collect_symconsts_walk(ir_node *node, void *env) {
664         pset *symbol_set = env;
665
666         if (is_SymConst(node)) {
667                 ir_entity *ent = get_SymConst_entity(node);
668                 set_entity_backend_marked(ent, 1);
669                 if (! is_direct_entity(ent))
670                         pset_insert_ptr(symbol_set, ent);
671         }
672 }
673
674 /**
675  * Initializes the backend ISA and opens the output file.
676  */
677 static void *ppc32_init(FILE *file_handle) {
678         static int inited = 0;
679         ppc32_isa_t *isa;
680         int i;
681
682         if (inited)
683                 return NULL;
684
685         isa = xmalloc(sizeof(*isa));
686         memcpy(isa, &ppc32_isa_template, sizeof(*isa));
687
688         be_emit_init_env(&isa->emit, file_handle);
689
690         ppc32_register_init();
691         ppc32_create_opcodes();
692
693         inited = 1;
694
695         isa->symbol_set = pset_new_ptr(8);
696         for (i = 0; i < get_irp_n_irgs(); ++i) {
697                 ir_graph *irg = get_irp_irg(i);
698                 irg_walk_blkwise_graph(irg, NULL, ppc32_collect_symconsts_walk, isa->symbol_set);
699         }
700
701         /* we mark referenced global entities, so we can only emit those which
702          * are actually referenced. (Note: you mustn't use the type visited flag
703          * elsewhere in the backend)
704          */
705         inc_master_type_visited();
706
707         return isa;
708 }
709
710 static void ppc32_dump_indirect_symbols(ppc32_isa_t *isa) {
711         ir_entity *ent;
712
713         foreach_pset(isa->symbol_set, ent) {
714                 const char *ld_name = get_entity_ld_name(ent);
715                 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);
716                 be_emit_write_line(&isa->emit);
717         }
718 }
719
720 /**
721  * Closes the output file and frees the ISA structure.
722  */
723 static void ppc32_done(void *self) {
724         ppc32_isa_t *isa = self;
725
726         be_gas_emit_decls(&isa->emit, isa->arch_isa.main_env, 1);
727         be_gas_emit_switch_section(&isa->emit, GAS_SECTION_DATA);
728         ppc32_dump_indirect_symbols(isa);
729
730         be_emit_destroy_env(&isa->emit);
731         del_pset(isa->symbol_set);
732
733         free(self);
734 }
735
736
737
738 static int ppc32_get_n_reg_class(const void *self) {
739         (void) self;
740         return N_CLASSES;
741 }
742
743 static const arch_register_class_t *ppc32_get_reg_class(const void *self, int i) {
744         (void) self;
745         assert(i >= 0 && i < N_CLASSES && "Invalid ppc register class requested.");
746         return &ppc32_reg_classes[i];
747 }
748
749
750
751 /**
752  * Get the register class which shall be used to store a value of a given mode.
753  * @param self The this pointer.
754  * @param mode The mode in question.
755  * @return A register class which can hold values of the given mode.
756  */
757 const arch_register_class_t *ppc32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
758         (void) self;
759         if (mode_is_float(mode))
760                 return &ppc32_reg_classes[CLASS_ppc32_fp];
761         else
762                 return &ppc32_reg_classes[CLASS_ppc32_gp];
763 }
764
765
766 /**
767  * Get the ABI restrictions for procedure calls.
768  * @param self        The this pointer.
769  * @param method_type The type of the method (procedure) in question.
770  * @param abi         The abi object to be modified
771  */
772 static void ppc32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
773         ir_type  *tp;
774         ir_mode  *mode;
775         int       i, n = get_method_n_params(method_type);
776         int               stackoffs = 0, lastoffs = 0, stackparamsize;
777
778         int               gpregi = REG_R3;
779         int               fpregi = REG_F1;
780
781         const arch_register_t *reg;
782         be_abi_call_flags_t call_flags = { { 0, 0, 1, 0, 0, 0, 1 } };
783
784         (void) self;
785         if(get_type_visibility(method_type)!=visibility_external_allocated)
786                 call_flags.bits.call_has_imm = 1;
787
788         /* set stack parameter passing style */
789         be_abi_call_set_flags(abi, call_flags, &ppc32_abi_callbacks);
790
791         for (i = 0; i < n; i++) {
792                 tp   = get_method_param_type(method_type, i);
793                 if(is_atomic_type(tp))
794                 {
795                         mode = get_type_mode(tp);
796
797                         if(mode_is_float(mode))
798                         {
799                                 if(fpregi <= REG_F13)
800                                 {
801                                         if(get_mode_size_bits(mode) == 32) gpregi++, stackparamsize=4;
802                                         else gpregi += 2, stackparamsize=8;                                                             // mode == irm_D
803                                         reg = &ppc32_fp_regs[fpregi++];
804                                 }
805                                 else
806                                 {
807                                         if(get_mode_size_bits(mode) == 32) stackparamsize=4;
808                                         else stackparamsize=8;                                                          // mode == irm_D
809                                         reg = NULL;
810                                 }
811                         }
812                         else
813                         {
814                                 if(gpregi <= REG_R10)
815                                         reg = &ppc32_gp_regs[gpregi++];
816                                 else
817                                         reg = NULL;
818                                 stackparamsize=4;
819                         }
820
821                         if(reg)
822                                 be_abi_call_param_reg(abi, i, reg);
823                         else
824                         {
825                                 be_abi_call_param_stack(abi, i, 4, stackoffs-lastoffs, 0);
826                                 lastoffs = stackoffs+stackparamsize;
827                         }
828                         stackoffs += stackparamsize;
829                 }
830                 else
831                 {
832                         be_abi_call_param_stack(abi, i, 4, stackoffs-lastoffs, 0);
833                         stackoffs += (get_type_size_bytes(tp)+3) & -4;
834                         lastoffs = stackoffs;
835                 }
836         }
837
838         /* explain where result can be found if any */
839         if (get_method_n_ress(method_type) > 0) {
840                 tp   = get_method_res_type(method_type, 0);
841                 mode = get_type_mode(tp);
842
843                 be_abi_call_res_reg(abi, 0,
844                         mode_is_float(mode) ? &ppc32_fp_regs[REG_F1] : &ppc32_gp_regs[REG_R3]);
845         }
846 }
847
848 static const void *ppc32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
849         (void) self;
850         (void) irn;
851         return &ppc32_irn_ops;
852 }
853
854 const arch_irn_handler_t ppc32_irn_handler = {
855         ppc32_get_irn_ops
856 };
857
858 const arch_irn_handler_t *ppc32_get_irn_handler(const void *self) {
859         (void) self;
860         return &ppc32_irn_handler;
861 }
862
863 int ppc32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
864         (void) block_env;
865         if(!is_ppc32_irn(irn))
866                 return -1;
867
868         return 1;
869 }
870
871 /**
872  * Initializes the code generator interface.
873  */
874 static const arch_code_generator_if_t *ppc32_get_code_generator_if(void *self) {
875         (void) self;
876         return &ppc32_code_gen_if;
877 }
878
879 list_sched_selector_t ppc32_sched_selector;
880
881 /**
882  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
883  */
884 static const list_sched_selector_t *ppc32_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
885         (void) self;
886         (void) selector;
887         memcpy(&ppc32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
888         ppc32_sched_selector.to_appear_in_schedule = ppc32_to_appear_in_schedule;
889         return &ppc32_sched_selector;
890 }
891
892 static const ilp_sched_selector_t *ppc32_get_ilp_sched_selector(const void *self) {
893         (void) self;
894         return NULL;
895 }
896
897 /**
898  * Returns the necessary byte alignment for storing a register of given class.
899  */
900 static int ppc32_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
901         (void) self;
902         ir_mode *mode = arch_register_class_mode(cls);
903         return get_mode_size_bytes(mode);
904 }
905
906 static const be_execution_unit_t ***ppc32_get_allowed_execution_units(const void *self, const ir_node *irn) {
907         (void) self;
908         (void) irn;
909         /* TODO */
910         assert(0);
911         return NULL;
912 }
913
914 static const be_machine_t *ppc32_get_machine(const void *self) {
915         (void) self;
916         /* TODO */
917         assert(0);
918         return NULL;
919 }
920
921 /**
922  * Return irp irgs in the desired order.
923  */
924 static ir_graph **ppc32_get_irg_list(const void *self, ir_graph ***irg_list) {
925         (void) self;
926         (void) irg_list;
927         return NULL;
928 }
929
930 /**
931  * Returns the libFirm configuration parameter for this backend.
932  */
933 static const backend_params *ppc32_get_libfirm_params(void) {
934         static arch_dep_params_t ad = {
935                 1,  /* allow subs */
936                 0,      /* Muls are fast enough on ARM */
937                 31, /* shift would be ok */
938                 0,  /* SMUL is needed, only in Arch M*/
939                 0,  /* UMUL is needed, only in Arch M */
940                 32, /* SMUL & UMUL available for 32 bit */
941         };
942         static backend_params p = {
943                 1,     /* need dword lowering */
944                 0,     /* don't support inlien assembler yet */
945                 NULL,  /* no additional opcodes */
946                 NULL,  /* will be set later */
947                 NULL,  /* but yet no creator function */
948                 NULL,  /* context for create_intrinsic_fkt */
949                 NULL,  /* no if conversion settings */
950         };
951
952         p.dep_param = &ad;
953         return &p;
954 }
955
956 const arch_isa_if_t ppc32_isa_if = {
957         ppc32_init,
958         ppc32_done,
959         ppc32_get_n_reg_class,
960         ppc32_get_reg_class,
961         ppc32_get_reg_class_for_mode,
962         ppc32_get_call_abi,
963         ppc32_get_irn_handler,
964         ppc32_get_code_generator_if,
965         ppc32_get_list_sched_selector,
966         ppc32_get_ilp_sched_selector,
967         ppc32_get_reg_class_alignment,
968         ppc32_get_libfirm_params,
969         ppc32_get_allowed_execution_units,
970         ppc32_get_machine,
971         ppc32_get_irg_list,
972 };
973
974 void be_init_arch_ppc32(void)
975 {
976         be_register_isa_if("ppc32", &ppc32_isa_if);
977 }
978
979 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_ppc32);