Fixed name mangling for private entities
[libfirm] / ir / be / ppc32 / ppc32_emitter.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   ppc emitter
23  * @author  Moritz Kroll, Jens Mueller
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <limits.h>
29
30 #include "xmalloc.h"
31 #include "tv.h"
32 #include "iredges.h"
33 #include "debug.h"
34 #include "irgwalk.h"
35 #include "irprintf.h"
36 #include "irop_t.h"
37 #include "irnode_t.h"
38 #include "irargs_t.h"
39 #include "error.h"
40
41 #include "../besched.h"
42 #include "../benode.h"
43
44 #include "ppc32_emitter.h"
45 #include "gen_ppc32_emitter.h"
46 #include "gen_ppc32_regalloc_if.h"
47 #include "ppc32_nodes_attr.h"
48 #include "ppc32_new_nodes.h"
49 #include "ppc32_map_regs.h"
50
51 #define SNPRINTF_BUF_LEN 128
52
53 static char printbuf[SNPRINTF_BUF_LEN];
54
55 extern int isleaf;
56
57
58 /*************************************************************
59  *             _       _    __   _          _
60  *            (_)     | |  / _| | |        | |
61  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
62  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
63  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
64  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
65  * | |                                       | |
66  * |_|                                       |_|
67  *************************************************************/
68 /**
69  * Returns the register at in position pos.
70  */
71 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
72         ir_node                *op;
73         const arch_register_t  *reg = NULL;
74
75         assert(get_irn_arity(irn) > pos && "Invalid IN position");
76
77         /* The out register of the operator at position pos is the
78            in register we need. */
79         op = get_irn_n(irn, pos);
80
81         reg = arch_get_irn_register(op);
82
83         assert(reg && "no in register found");
84         return reg;
85 }
86
87 /**
88  * Returns the register at out position pos.
89  */
90 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
91         ir_node                *proj;
92         const arch_register_t  *reg = NULL;
93
94         assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
95
96         /* 1st case: irn is not of mode_T, so it has only                 */
97         /*           one OUT register -> good                             */
98         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
99         /*           Proj with the corresponding projnum for the register */
100
101         if (get_irn_mode(irn) != mode_T) {
102                 reg = arch_get_irn_register(irn);
103         } else if (is_ppc32_irn(irn)) {
104                 reg = arch_irn_get_register(irn, pos);
105         } else {
106                 const ir_edge_t *edge;
107
108                 foreach_out_edge(irn, edge) {
109                         proj = get_edge_src_irn(edge);
110                         assert(is_Proj(proj) && "non-Proj from mode_T node");
111                         if (get_Proj_proj(proj) == pos) {
112                                 reg = arch_get_irn_register(proj);
113                                 break;
114                         }
115                 }
116         }
117
118         assert(reg && "no out register found");
119         return reg;
120 }
121
122 /**
123  * Emit the name of the source register at given input position.
124  */
125 void ppc32_emit_source_register(const ir_node *node, int pos) {
126         const arch_register_t *reg = get_in_reg(node, pos);
127         be_emit_string(arch_register_get_name(reg));
128 }
129
130 /**
131  * Emit the name of the destination register at given output position.
132  */
133 void ppc32_emit_dest_register(const ir_node *node, int pos) {
134         const arch_register_t *reg = get_out_reg(node, pos);
135         be_emit_string(arch_register_get_name(reg));
136 }
137
138 void ppc32_emit_rlwimi_helper(const ir_node *n) {
139         const rlwimi_const_t *rlwimi_const = get_ppc32_rlwimi_const(n);
140
141         be_emit_irprintf("%i, %i, %i", rlwimi_const->shift,
142                 rlwimi_const->maskA, rlwimi_const->maskB);
143 }
144
145 /**
146  * Emit a const or symconst.
147  */
148 void ppc32_emit_immediate(const ir_node *n) {
149         const char *buf;
150
151         switch (get_ppc32_type(n)) {
152         case ppc32_ac_Const:
153                 tarval_snprintf(printbuf, SNPRINTF_BUF_LEN, get_ppc32_constant_tarval(n));
154                 buf = printbuf;
155                 break;
156         case ppc32_ac_SymConst:
157                 buf = get_id_str(get_ppc32_symconst_ident(n));
158                 break;
159         case ppc32_ac_Offset:
160                 be_emit_irprintf("%i", get_ppc32_offset(n));
161                 return;
162         default:
163                 assert(0 && "node_const_to_str(): Illegal offset type");
164                 return;
165         }
166         switch (get_ppc32_offset_mode(n)) {
167         case ppc32_ao_None:
168                 be_emit_string(buf);
169                 return;
170         case ppc32_ao_Lo16:
171                 be_emit_irprintf("lo16(%s)", buf);
172                 return;
173         case ppc32_ao_Hi16:
174                 be_emit_irprintf("hi16(%s)", buf);
175                 return;
176         case ppc32_ao_Ha16:
177                 be_emit_irprintf("ha16(%s)", buf);
178                 return;
179         default:
180                 assert(0 && "node_const_to_str(): Illegal offset mode");
181                 return;
182         }
183 }
184
185 /**
186  * Emits a node's offset.
187  */
188 void ppc32_emit_offset(const ir_node *n) {
189         const char *buf;
190         if (get_ppc32_type(n) == ppc32_ac_None) {
191                 be_emit_char('0');
192                 return;
193         }
194
195         switch (get_ppc32_type(n)) {
196         case ppc32_ac_Const:
197                 tarval_snprintf(printbuf, SNPRINTF_BUF_LEN, get_ppc32_constant_tarval(n));
198                 buf = printbuf;
199                 break;
200         case ppc32_ac_SymConst:
201                 buf = get_id_str(get_ppc32_symconst_ident(n));
202                 break;
203         case ppc32_ac_Offset:
204                 be_emit_irprintf("%i", get_ppc32_offset(n));
205                 return;
206         default:
207                 assert(0 && "node_offset_to_str(): Illegal offset type");
208                 return;
209         }
210         switch (get_ppc32_offset_mode(n)) {
211         case ppc32_ao_None:
212                 be_emit_string(buf);
213                 return;
214         case ppc32_ao_Lo16:
215                 be_emit_irprintf("lo16(%s)", buf);
216                 return;
217         case ppc32_ao_Hi16:
218                 be_emit_irprintf("hi16(%s)", buf);
219                 return;
220         case ppc32_ao_Ha16:
221                 be_emit_irprintf("ha16(%s)", buf);
222                 return;
223         default:
224                 assert(0 && "node_offset_to_str(): Illegal offset mode");
225                 return;
226         }
227 }
228
229 /**
230  * Returns the target label for a control flow node.
231  */
232 static char *get_cfop_target(const ir_node *irn, char *buf) {
233         ir_node *bl = get_irn_link(irn);
234
235         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
236         return buf;
237 }
238
239 /**
240  * Emits code for a unconditional jump.
241  */
242 static void emit_Jmp(const ir_node *irn) {
243         ir_node *block = get_nodes_block(irn);
244
245         if (get_irn_link(irn) != get_irn_link(block)) {
246                 be_emit_irprintf("\tb %s", get_cfop_target(irn, printbuf));
247         } else {
248                 be_emit_irprintf("/* fallthrough(%+F) */", get_irn_link(irn));
249         }
250         be_emit_finish_line_gas(irn);
251 }
252
253 /**
254  * Emits code for a call
255  */
256 static void emit_be_Call(const ir_node *irn) {
257         ir_entity *call_ent = be_Call_get_entity(irn);
258
259         if (call_ent) {
260                 be_emit_irprintf("\tbl %s", get_entity_ld_name(call_ent));
261         } else {
262                 be_emit_cstring("\tmtlr ");
263                 ppc32_emit_source_register(irn, be_pos_Call_ptr);
264                 be_emit_pad_comment();
265                 be_emit_cstring("/* Move to link register and link */\n");
266                 be_emit_write_line();
267                 be_emit_cstring("\tblrl");
268         }
269         be_emit_finish_line_gas(irn);
270 }
271
272 static void emit_ppc32_Branch(const ir_node *irn) {
273         static const char *branchops[8] = { 0, "beq", "blt", "ble", "bgt", "bge", "bne", "b" };
274         int projnum = get_ppc32_proj_nr(irn);
275
276         const ir_edge_t *edge = get_irn_out_edge_first(irn);
277         ir_node *proj = get_edge_src_irn(edge);
278
279         int opind;
280
281         if (get_Proj_proj(proj) == pn_Cond_true)
282                 opind = projnum;
283         else
284                 opind = 7 - projnum;
285
286         assert(opind>=0 && opind<8);
287
288         if (opind){
289                 get_cfop_target(proj, printbuf);
290                 be_emit_irprintf("\t%8s", branchops[opind]);
291                 ppc32_emit_source_register(irn, 0);
292                 be_emit_cstring(", ");
293                 be_emit_string(printbuf);
294                 be_emit_finish_line_gas(irn);
295         }
296
297         edge = get_irn_out_edge_next(irn, edge);
298         if (edge) {
299                 ir_node *blk = get_edge_src_irn(edge);
300                 be_emit_cstring("\tb ");
301                 be_emit_string(get_cfop_target(blk, printbuf));
302                 be_emit_finish_line_gas(irn);
303         }
304 }
305
306 static void emit_ppc32_LoopCopy(const ir_node *irn) {
307         be_emit_irprintf("LOOP_%ld:\n", get_irn_node_nr(irn));
308         be_emit_write_line();
309
310         be_emit_cstring("\tlwzu ");
311         ppc32_emit_dest_register(irn, 4);
312         be_emit_cstring(", 4(");
313         ppc32_emit_source_register(irn, 1);
314         be_emit_char(')');
315         be_emit_pad_comment();
316         be_emit_cstring("/* Load with update */\n");
317         be_emit_write_line();
318
319         be_emit_cstring("\tstwu ");
320         ppc32_emit_dest_register(irn, 4);
321         be_emit_cstring(", 4(");
322         ppc32_emit_source_register(irn, 2);
323         be_emit_char(')');
324         be_emit_pad_comment();
325         be_emit_cstring("/* Store with update */\n");
326         be_emit_write_line();
327
328         be_emit_irprintf("\tbdnz LOOP_%i", get_irn_node_nr(irn));
329         be_emit_finish_line_gas(irn);
330 }
331
332 static void emit_ppc32_Switch(const ir_node *irn) {
333         ir_node *proj, *defproj = NULL;
334         int pn;
335
336         const ir_edge_t* edge;
337         foreach_out_edge(irn, edge) {
338                 proj = get_edge_src_irn(edge);
339                 assert(is_Proj(proj) && "Only proj allowed at Switch");
340                 if (get_irn_mode(proj) != mode_X) continue;
341
342                 pn = get_Proj_proj(proj);
343                 /* check for default proj */
344                 if (pn == get_ppc32_proj_nr(irn)) {
345                         assert(defproj == NULL && "found two defProjs at Switch");
346                         defproj = proj;
347                 } else {
348                         be_emit_cstring("\taddis ");
349                         ppc32_emit_source_register(irn, 1);
350                         be_emit_irprintf(", 0, hi16(%i)", pn);
351                         be_emit_pad_comment();
352                         be_emit_cstring("/* Load upper immediate */\n");
353                         be_emit_write_line();
354
355                         be_emit_cstring("\tori ");
356                         ppc32_emit_source_register(irn, 1);
357                         be_emit_cstring(", ");
358                         ppc32_emit_source_register(irn, 1);
359                         be_emit_irprintf(", lo16(%i)", pn);
360                         be_emit_pad_comment();
361                         be_emit_cstring("/* Load lower immediate */\n");
362                         be_emit_write_line();
363
364                         be_emit_cstring("\tcmp ");
365                         ppc32_emit_source_register(irn, 2);
366                         be_emit_cstring(", ");
367                         ppc32_emit_source_register(irn, 0);
368                         be_emit_cstring(", ");
369                         ppc32_emit_source_register(irn, 1);
370                         be_emit_pad_comment();
371                         be_emit_cstring("/* Compare */\n");
372                         be_emit_write_line();
373
374                         be_emit_cstring("\tbeq ");
375                         ppc32_emit_source_register(irn, 2);
376                         be_emit_irprintf(", %s", get_cfop_target(proj, printbuf));
377                         be_emit_cstring("/* Branch if equal */\n");
378                         be_emit_write_line();
379                 }
380         }
381         assert(defproj != NULL && "didn't find defProj at Switch");
382         be_emit_irprintf("\tb %s", get_cfop_target(defproj, printbuf));
383         be_emit_finish_line_gas(irn);
384 }
385
386 /**
387  * Emits code for a backend Copy node
388  */
389 static void emit_be_Copy(const ir_node *irn) {
390         const arch_register_class_t *regclass = arch_get_irn_reg_class(irn, 0);
391
392         if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp]) {
393                 be_emit_cstring("\tmr ");
394         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp]) {
395                 be_emit_cstring("\tfmr ");
396         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition]) {
397                 be_emit_cstring("\tmcrf ");
398         } else {
399                 assert(0 && "Illegal register class for Copy");
400                 panic("ppc32 Emitter: Illegal register class for Copy");
401         }
402         ppc32_emit_dest_register(irn, 0);
403         be_emit_cstring(", ");
404         ppc32_emit_source_register(irn, 0);
405         be_emit_finish_line_gas(irn);
406 }
407
408 /**
409  * Emits code for a backend Perm node
410  */
411 static void emit_be_Perm(const ir_node *irn) {
412         const arch_register_class_t *regclass = arch_get_irn_reg_class(irn, 0);
413
414         if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp]) {
415                 be_emit_cstring("\txor ");
416                 ppc32_emit_source_register(irn, 0);
417                 be_emit_cstring(", ");
418                 ppc32_emit_source_register(irn, 0);
419                 be_emit_cstring(", ");
420                 ppc32_emit_source_register(irn, 1);
421                 be_emit_pad_comment();
422                 be_emit_cstring("/* Swap with XOR */\n");
423                 be_emit_write_line();
424
425                 be_emit_cstring("\txor ");
426                 ppc32_emit_source_register(irn, 1);
427                 be_emit_cstring(", ");
428                 ppc32_emit_source_register(irn, 0);
429                 be_emit_cstring(", ");
430                 ppc32_emit_source_register(irn, 1);
431                 be_emit_pad_comment();
432                 be_emit_cstring("/* (continued) */\n");
433                 be_emit_write_line();
434
435                 be_emit_cstring("\txor ");
436                 ppc32_emit_source_register(irn, 0);
437                 be_emit_cstring(", ");
438                 ppc32_emit_source_register(irn, 0);
439                 be_emit_cstring(", ");
440                 ppc32_emit_source_register(irn, 1);
441         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp]) {
442                 be_emit_cstring("\tfmr f0, ");
443                 ppc32_emit_source_register(irn, 0);
444                 be_emit_pad_comment();
445                 be_emit_cstring("/* Swap with moves */\n");
446                 be_emit_write_line();
447
448                 be_emit_cstring("\tfmr ");
449                 ppc32_emit_source_register(irn, 0);
450                 be_emit_cstring(", ");
451                 ppc32_emit_source_register(irn, 1);
452                 be_emit_pad_comment();
453                 be_emit_cstring("/* (continued) */\n");
454                 be_emit_write_line();
455
456                 be_emit_cstring("\tfmr ");
457                 ppc32_emit_source_register(irn, 1);
458                 be_emit_cstring(", f0");
459         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition]) {
460                 be_emit_cstring("\tmcrf cr7, ");
461                 ppc32_emit_source_register(irn, 0);
462                 be_emit_pad_comment();
463                 be_emit_cstring("/* Swap with moves */\n");
464                 be_emit_write_line();
465
466                 be_emit_cstring("\tmcrf ");
467                 ppc32_emit_source_register(irn, 0);
468                 be_emit_cstring(", ");
469                 ppc32_emit_source_register(irn, 1);
470                 be_emit_pad_comment();
471                 be_emit_cstring("/* (continued) */\n");
472                 be_emit_write_line();
473
474                 be_emit_cstring("\tmcrf ");
475                 ppc32_emit_source_register(irn, 1);
476                 be_emit_cstring(", cr7");
477         } else {
478                 assert(0 && "Illegal register class for Perm");
479                 panic("ppc32 Emitter: Illegal register class for Perm");
480         }
481         be_emit_finish_line_gas(irn);
482 }
483
484
485 /**
486  * Emits code for a proj -> node
487  */
488 static void emit_Proj(const ir_node *irn) {
489         ir_node *pred = get_Proj_pred(irn);
490
491         if (is_Start(pred)) {
492                 if (get_Proj_proj(irn) == pn_Start_X_initial_exec) {
493                         emit_Jmp(irn);
494                 }
495         }
496 }
497
498 static void emit_be_IncSP(const ir_node *irn) {
499         int offs = be_get_IncSP_offset(irn);
500
501         be_emit_irprintf("\t/* ignored IncSP with %d */", -offs);
502         be_emit_finish_line_gas(irn);
503
504 //      if (offs) {
505 //              assert(offs<=0x7fff);
506 //              lc_efprintf(ppc32_get_arg_env(), F, "\taddi    %1S, %1S, %d\t\t\t/* %+F (IncSP) */\n", irn, irn,
507 //                      -offs, irn);
508 //      }
509 //      else {
510 //              fprintf(F, "\t\t\t\t\t/* omitted IncSP with 0 */\n");
511 //      }
512 }
513
514 /*static void emit_Spill(const ir_node *irn, ppc32_emit_env_t *emit_env) {
515         ir_node *context = be_get_Spill_context(irn);
516         ir_entity *entity = be_get_spill_entity(irn);
517 }*/
518
519 /***********************************************************************************
520  *                  _          __                                             _
521  *                 (_)        / _|                                           | |
522  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
523  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
524  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
525  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
526  *
527  ***********************************************************************************/
528 /**
529  * The type of a emitter function.
530  */
531 typedef void (emit_func)(const ir_node *irn);
532
533 /**
534  * Set a node emitter. Make it a bit more type safe.
535  */
536 static inline void set_emitter(ir_op *op, emit_func ppc32_emit_node) {
537         op->ops.generic = (op_func)ppc32_emit_node;
538 }
539
540 static void ppc32_register_emitters(void) {
541         /* first clear generic function pointers */
542         clear_irp_opcodes_generic_func();
543
544         /* register generated emitter functions */
545         ppc32_register_spec_emitters();
546
547         set_emitter(op_ppc32_Branch, emit_ppc32_Branch);
548         set_emitter(op_ppc32_LoopCopy, emit_ppc32_LoopCopy);
549         set_emitter(op_ppc32_Switch, emit_ppc32_Switch);
550         set_emitter(op_be_Call, emit_be_Call);
551         set_emitter(op_Jmp, emit_Jmp);
552         set_emitter(op_Proj, emit_Proj);
553         set_emitter(op_be_IncSP, emit_be_IncSP);
554         set_emitter(op_be_Copy, emit_be_Copy);
555         set_emitter(op_be_Perm, emit_be_Perm);
556 //      set_emitter(op_Spill, emit_Spill);
557 //      set_emitter(op_Reload, emit_Reload);
558 }
559
560 /**
561  * Emits code for a node.
562  */
563 static void ppc32_emit_node(const ir_node *irn) {
564         ir_op *op = get_irn_op(irn);
565
566         if (op->ops.generic) {
567                 emit_func *emit = (emit_func *)op->ops.generic;
568                 (*emit)(irn);
569         } else {
570                 be_emit_cstring("\t/* TODO */");
571                 be_emit_finish_line_gas(irn);
572         }
573 }
574
575
576 /**
577  * Walks over the nodes in a block connected by scheduling edges
578  * and emits code for each node.
579  */
580 static void ppc32_gen_block(const ir_node *block) {
581         ir_node *irn;
582
583         if (! is_Block(block))
584                 return;
585
586         be_emit_irprintf("BLOCK_%ld:\n", get_irn_node_nr(block));
587         be_emit_write_line();
588         sched_foreach(block, irn) {
589                 ppc32_emit_node(irn);
590         }
591 }
592
593
594 /**
595  * Emits code for function start.
596  */
597 static void ppc32_emit_start(ir_graph *irg) {
598         const char *irg_name  = get_entity_ld_name(get_irg_entity(irg));
599         int         framesize = get_type_size_bytes(get_irg_frame_type(irg));
600
601         if(! strcmp(irg_name, "main")) {                                           // XXX: underscore hack
602                 irg_name = "_main";
603         }
604
605         be_emit_irprintf("\t.text\n\t.globl %s\n\t.align 4\n%s:\n", irg_name, irg_name);
606
607         if (framesize > 24) {
608                 be_emit_cstring("\tmflr    r0\n");
609                 be_emit_cstring("\tstw     r0, 8(r1)\n");
610                 be_emit_irprintf("\tstwu    r1, -%i(r1)\n", framesize);
611         } else {
612                 be_emit_irprintf("\t/* set new frame (%d) omitted */\n", framesize);
613         }
614         be_emit_write_line();
615
616 /*      if(!isleaf) {
617                 // store link register in linkage area (TODO: if needed)
618
619                 be_emit_cstring("\tmflr    r0\n");
620                 be_emit_cstring("\tstwu    r0, -4(r1)\n");   // stw r0, 8(SP)
621                 be_emit_write_line();
622         }*/
623 }
624
625 /**
626  * Emits code for function end
627  */
628 static void ppc32_emit_end(ir_graph *irg) {
629         int framesize = get_type_size_bytes(get_irg_frame_type(irg));
630         (void) irg;
631
632 /*      if(!isleaf) {
633                 // restore link register
634
635                 be_emit_cstring("\tlwz     r0, 0(r1)\n");
636                 be_emit_cstring("\taddi    r1, r1, 4\n");
637                 be_emit_cstring("\tmtlr    r0\n");
638                 be_emit_write_line();
639         }*/
640         if(framesize > 24) {
641                 be_emit_cstring("\tlwz    r1, 0(r1)\n");
642                 be_emit_cstring("\tlwz    r0, 8(r1)\n");
643                 be_emit_cstring("\tmtlr   r0\n");
644                 be_emit_write_line();
645         }
646         be_emit_cstring("\tblr\n\n");
647         be_emit_write_line();
648 }
649
650 /**
651  * Sets labels for control flow nodes (jump target)
652  * TODO: Jump optimization
653  */
654 void ppc32_gen_labels(ir_node *block, void *env) {
655         ir_node *pred;
656         int n;
657         (void) env;
658
659         for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
660                 pred = get_Block_cfgpred(block, n);
661                 set_irn_link(pred, block);
662         }
663 }
664
665 /**
666  * Main driver: generates code for one routine
667  */
668 void ppc32_gen_routine(const ppc32_code_gen_t *cg, ir_graph *irg)
669 {
670         ir_node *block;
671         int i, n;
672
673         ppc32_register_emitters();
674
675         ppc32_emit_start(irg);
676         irg_block_walk_graph(irg, ppc32_gen_labels, NULL, NULL);
677
678         n = ARR_LEN(cg->blk_sched);
679         for (i = 0; i < n;) {
680                 ir_node *next_bl;
681
682                 block   = cg->blk_sched[i];
683                 ++i;
684                 next_bl = i < n ? cg->blk_sched[i] : NULL;
685
686                 /* set here the link. the emitter expects to find the next block here */
687                 set_irn_link(block, next_bl);
688                 ppc32_gen_block(block);
689         }
690         ppc32_emit_end(irg);
691 }