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