Remove unnecessary global variable.
[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_t.h"
42 #include "../benode_t.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 = get_ppc32_out_reg(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                 set_entity_backend_marked(call_ent, 1);
261                 be_emit_irprintf("\tbl %s", get_entity_ld_name(call_ent));
262         } else {
263                 be_emit_cstring("\tmtlr ");
264                 ppc32_emit_source_register(irn, be_pos_Call_ptr);
265                 be_emit_pad_comment();
266                 be_emit_cstring("/* Move to link register and link */\n");
267                 be_emit_write_line();
268                 be_emit_cstring("\tblrl");
269         }
270         be_emit_finish_line_gas(irn);
271 }
272
273 static void emit_ppc32_Branch(const ir_node *irn) {
274         static const char *branchops[8] = { 0, "beq", "blt", "ble", "bgt", "bge", "bne", "b" };
275         int projnum = get_ppc32_proj_nr(irn);
276
277         const ir_edge_t *edge = get_irn_out_edge_first(irn);
278         ir_node *proj = get_edge_src_irn(edge);
279
280         int opind;
281
282         if (get_Proj_proj(proj) == pn_Cond_true)
283                 opind = projnum;
284         else
285                 opind = 7 - projnum;
286
287         assert(opind>=0 && opind<8);
288
289         if (opind){
290                 get_cfop_target(proj, printbuf);
291                 be_emit_irprintf("\t%8s", branchops[opind]);
292                 ppc32_emit_source_register(irn, 0);
293                 be_emit_cstring(", ");
294                 be_emit_string(printbuf);
295                 be_emit_finish_line_gas(irn);
296         }
297
298         edge = get_irn_out_edge_next(irn, edge);
299         if (edge) {
300                 ir_node *blk = get_edge_src_irn(edge);
301                 be_emit_cstring("\tb ");
302                 be_emit_string(get_cfop_target(blk, printbuf));
303                 be_emit_finish_line_gas(irn);
304         }
305 }
306
307 static void emit_ppc32_LoopCopy(const ir_node *irn) {
308         be_emit_irprintf("LOOP_%ld:\n", get_irn_node_nr(irn));
309         be_emit_write_line();
310
311         be_emit_cstring("\tlwzu ");
312         ppc32_emit_dest_register(irn, 4);
313         be_emit_cstring(", 4(");
314         ppc32_emit_source_register(irn, 1);
315         be_emit_char(')');
316         be_emit_pad_comment();
317         be_emit_cstring("/* Load with update */\n");
318         be_emit_write_line();
319
320         be_emit_cstring("\tstwu ");
321         ppc32_emit_dest_register(irn, 4);
322         be_emit_cstring(", 4(");
323         ppc32_emit_source_register(irn, 2);
324         be_emit_char(')');
325         be_emit_pad_comment();
326         be_emit_cstring("/* Store with update */\n");
327         be_emit_write_line();
328
329         be_emit_irprintf("\tbdnz LOOP_%i", get_irn_node_nr(irn));
330         be_emit_finish_line_gas(irn);
331 }
332
333 static void emit_ppc32_Switch(const ir_node *irn) {
334         ir_node *proj, *defproj = NULL;
335         int pn;
336
337         const ir_edge_t* edge;
338         foreach_out_edge(irn, edge) {
339                 proj = get_edge_src_irn(edge);
340                 assert(is_Proj(proj) && "Only proj allowed at Switch");
341                 if (get_irn_mode(proj) != mode_X) continue;
342
343                 pn = get_Proj_proj(proj);
344                 /* check for default proj */
345                 if (pn == get_ppc32_proj_nr(irn)) {
346                         assert(defproj == NULL && "found two defProjs at Switch");
347                         defproj = proj;
348                 } else {
349                         be_emit_cstring("\taddis ");
350                         ppc32_emit_source_register(irn, 1);
351                         be_emit_irprintf(", 0, hi16(%i)", pn);
352                         be_emit_pad_comment();
353                         be_emit_cstring("/* Load upper immediate */\n");
354                         be_emit_write_line();
355
356                         be_emit_cstring("\tori ");
357                         ppc32_emit_source_register(irn, 1);
358                         be_emit_cstring(", ");
359                         ppc32_emit_source_register(irn, 1);
360                         be_emit_irprintf(", lo16(%i)", pn);
361                         be_emit_pad_comment();
362                         be_emit_cstring("/* Load lower immediate */\n");
363                         be_emit_write_line();
364
365                         be_emit_cstring("\tcmp ");
366                         ppc32_emit_source_register(irn, 2);
367                         be_emit_cstring(", ");
368                         ppc32_emit_source_register(irn, 0);
369                         be_emit_cstring(", ");
370                         ppc32_emit_source_register(irn, 1);
371                         be_emit_pad_comment();
372                         be_emit_cstring("/* Compare */\n");
373                         be_emit_write_line();
374
375                         be_emit_cstring("\tbeq ");
376                         ppc32_emit_source_register(irn, 2);
377                         be_emit_irprintf(", %s", get_cfop_target(proj, printbuf));
378                         be_emit_cstring("/* Branch if equal */\n");
379                         be_emit_write_line();
380                 }
381         }
382         assert(defproj != NULL && "didn't find defProj at Switch");
383         be_emit_irprintf("\tb %s", get_cfop_target(defproj, printbuf));
384         be_emit_finish_line_gas(irn);
385 }
386
387 /**
388  * Emits code for a backend Copy node
389  */
390 static void emit_be_Copy(const ir_node *irn) {
391         const arch_register_class_t *regclass = arch_get_irn_reg_class(irn, 0);
392
393         if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp]) {
394                 be_emit_cstring("\tmr ");
395         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp]) {
396                 be_emit_cstring("\tfmr ");
397         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition]) {
398                 be_emit_cstring("\tmcrf ");
399         } else {
400                 assert(0 && "Illegal register class for Copy");
401                 panic("ppc32 Emitter: Illegal register class for Copy");
402         }
403         ppc32_emit_dest_register(irn, 0);
404         be_emit_cstring(", ");
405         ppc32_emit_source_register(irn, 0);
406         be_emit_finish_line_gas(irn);
407 }
408
409 /**
410  * Emits code for a backend Perm node
411  */
412 static void emit_be_Perm(const ir_node *irn) {
413         const arch_register_class_t *regclass = arch_get_irn_reg_class(irn, 0);
414
415         if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp]) {
416                 be_emit_cstring("\txor ");
417                 ppc32_emit_source_register(irn, 0);
418                 be_emit_cstring(", ");
419                 ppc32_emit_source_register(irn, 0);
420                 be_emit_cstring(", ");
421                 ppc32_emit_source_register(irn, 1);
422                 be_emit_pad_comment();
423                 be_emit_cstring("/* Swap with XOR */\n");
424                 be_emit_write_line();
425
426                 be_emit_cstring("\txor ");
427                 ppc32_emit_source_register(irn, 1);
428                 be_emit_cstring(", ");
429                 ppc32_emit_source_register(irn, 0);
430                 be_emit_cstring(", ");
431                 ppc32_emit_source_register(irn, 1);
432                 be_emit_pad_comment();
433                 be_emit_cstring("/* (continued) */\n");
434                 be_emit_write_line();
435
436                 be_emit_cstring("\txor ");
437                 ppc32_emit_source_register(irn, 0);
438                 be_emit_cstring(", ");
439                 ppc32_emit_source_register(irn, 0);
440                 be_emit_cstring(", ");
441                 ppc32_emit_source_register(irn, 1);
442         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp]) {
443                 be_emit_cstring("\tfmr f0, ");
444                 ppc32_emit_source_register(irn, 0);
445                 be_emit_pad_comment();
446                 be_emit_cstring("/* Swap with moves */\n");
447                 be_emit_write_line();
448
449                 be_emit_cstring("\tfmr ");
450                 ppc32_emit_source_register(irn, 0);
451                 be_emit_cstring(", ");
452                 ppc32_emit_source_register(irn, 1);
453                 be_emit_pad_comment();
454                 be_emit_cstring("/* (continued) */\n");
455                 be_emit_write_line();
456
457                 be_emit_cstring("\tfmr ");
458                 ppc32_emit_source_register(irn, 1);
459                 be_emit_cstring(", f0");
460         } else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition]) {
461                 be_emit_cstring("\tmcrf cr7, ");
462                 ppc32_emit_source_register(irn, 0);
463                 be_emit_pad_comment();
464                 be_emit_cstring("/* Swap with moves */\n");
465                 be_emit_write_line();
466
467                 be_emit_cstring("\tmcrf ");
468                 ppc32_emit_source_register(irn, 0);
469                 be_emit_cstring(", ");
470                 ppc32_emit_source_register(irn, 1);
471                 be_emit_pad_comment();
472                 be_emit_cstring("/* (continued) */\n");
473                 be_emit_write_line();
474
475                 be_emit_cstring("\tmcrf ");
476                 ppc32_emit_source_register(irn, 1);
477                 be_emit_cstring(", cr7");
478         } else {
479                 assert(0 && "Illegal register class for Perm");
480                 panic("ppc32 Emitter: Illegal register class for Perm");
481         }
482         be_emit_finish_line_gas(irn);
483 }
484
485
486 /**
487  * Emits code for a proj -> node
488  */
489 static void emit_Proj(const ir_node *irn) {
490         ir_node *pred = get_Proj_pred(irn);
491
492         if (is_Start(pred)) {
493                 if (get_Proj_proj(irn) == pn_Start_X_initial_exec) {
494                         emit_Jmp(irn);
495                 }
496         }
497 }
498
499 static void emit_be_IncSP(const ir_node *irn) {
500         int offs = be_get_IncSP_offset(irn);
501
502         be_emit_irprintf("\t/* ignored IncSP with %d */", -offs);
503         be_emit_finish_line_gas(irn);
504
505 //      if (offs) {
506 //              assert(offs<=0x7fff);
507 //              lc_efprintf(ppc32_get_arg_env(), F, "\taddi    %1S, %1S, %d\t\t\t/* %+F (IncSP) */\n", irn, irn,
508 //                      -offs, irn);
509 //      }
510 //      else {
511 //              fprintf(F, "\t\t\t\t\t/* omitted IncSP with 0 */\n");
512 //      }
513 }
514
515 /*static void emit_Spill(const ir_node *irn, ppc32_emit_env_t *emit_env) {
516         ir_node *context = be_get_Spill_context(irn);
517         ir_entity *entity = be_get_spill_entity(irn);
518 }*/
519
520 /***********************************************************************************
521  *                  _          __                                             _
522  *                 (_)        / _|                                           | |
523  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
524  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
525  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
526  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
527  *
528  ***********************************************************************************/
529 /**
530  * The type of a emitter function.
531  */
532 typedef void (emit_func)(const ir_node *irn);
533
534 /**
535  * Set a node emitter. Make it a bit more type safe.
536  */
537 static inline void set_emitter(ir_op *op, emit_func ppc32_emit_node) {
538         op->ops.generic = (op_func)ppc32_emit_node;
539 }
540
541 static void ppc32_register_emitters(void) {
542         /* first clear generic function pointers */
543         clear_irp_opcodes_generic_func();
544
545         /* register generated emitter functions */
546         ppc32_register_spec_emitters();
547
548         set_emitter(op_ppc32_Branch, emit_ppc32_Branch);
549         set_emitter(op_ppc32_LoopCopy, emit_ppc32_LoopCopy);
550         set_emitter(op_ppc32_Switch, emit_ppc32_Switch);
551         set_emitter(op_be_Call, emit_be_Call);
552         set_emitter(op_Jmp, emit_Jmp);
553         set_emitter(op_Proj, emit_Proj);
554         set_emitter(op_be_IncSP, emit_be_IncSP);
555         set_emitter(op_be_Copy, emit_be_Copy);
556         set_emitter(op_be_Perm, emit_be_Perm);
557 //      set_emitter(op_Spill, emit_Spill);
558 //      set_emitter(op_Reload, emit_Reload);
559 }
560
561 /**
562  * Emits code for a node.
563  */
564 static void ppc32_emit_node(const ir_node *irn) {
565         ir_op *op = get_irn_op(irn);
566
567         if (op->ops.generic) {
568                 emit_func *emit = (emit_func *)op->ops.generic;
569                 (*emit)(irn);
570         } else {
571                 be_emit_cstring("\t/* TODO */");
572                 be_emit_finish_line_gas(irn);
573         }
574 }
575
576
577 /**
578  * Walks over the nodes in a block connected by scheduling edges
579  * and emits code for each node.
580  */
581 static void ppc32_gen_block(const ir_node *block) {
582         ir_node *irn;
583
584         if (! is_Block(block))
585                 return;
586
587         be_emit_irprintf("BLOCK_%ld:\n", get_irn_node_nr(block));
588         be_emit_write_line();
589         sched_foreach(block, irn) {
590                 ppc32_emit_node(irn);
591         }
592 }
593
594
595 /**
596  * Emits code for function start.
597  */
598 static void ppc32_emit_start(ir_graph *irg) {
599         const char *irg_name  = get_entity_ld_name(get_irg_entity(irg));
600         int         framesize = get_type_size_bytes(get_irg_frame_type(irg));
601
602         if(! strcmp(irg_name, "main")) {                                           // XXX: underscore hack
603                 irg_name = "_main";
604         }
605
606         be_emit_irprintf("\t.text\n\t.globl %s\n\t.align 4\n%s:\n", irg_name, irg_name);
607
608         if (framesize > 24) {
609                 be_emit_cstring("\tmflr    r0\n");
610                 be_emit_cstring("\tstw     r0, 8(r1)\n");
611                 be_emit_irprintf("\tstwu    r1, -%i(r1)\n", framesize);
612         } else {
613                 be_emit_irprintf("\t/* set new frame (%d) omitted */\n", framesize);
614         }
615         be_emit_write_line();
616
617 /*      if(!isleaf) {
618                 // store link register in linkage area (TODO: if needed)
619
620                 be_emit_cstring("\tmflr    r0\n");
621                 be_emit_cstring("\tstwu    r0, -4(r1)\n");   // stw r0, 8(SP)
622                 be_emit_write_line();
623         }*/
624 }
625
626 /**
627  * Emits code for function end
628  */
629 static void ppc32_emit_end(ir_graph *irg) {
630         int framesize = get_type_size_bytes(get_irg_frame_type(irg));
631         (void) irg;
632
633 /*      if(!isleaf) {
634                 // restore link register
635
636                 be_emit_cstring("\tlwz     r0, 0(r1)\n");
637                 be_emit_cstring("\taddi    r1, r1, 4\n");
638                 be_emit_cstring("\tmtlr    r0\n");
639                 be_emit_write_line();
640         }*/
641         if(framesize > 24) {
642                 be_emit_cstring("\tlwz    r1, 0(r1)\n");
643                 be_emit_cstring("\tlwz    r0, 8(r1)\n");
644                 be_emit_cstring("\tmtlr   r0\n");
645                 be_emit_write_line();
646         }
647         be_emit_cstring("\tblr\n\n");
648         be_emit_write_line();
649 }
650
651 /**
652  * Sets labels for control flow nodes (jump target)
653  * TODO: Jump optimization
654  */
655 void ppc32_gen_labels(ir_node *block, void *env) {
656         ir_node *pred;
657         int n;
658         (void) env;
659
660         for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
661                 pred = get_Block_cfgpred(block, n);
662                 set_irn_link(pred, block);
663         }
664 }
665
666 /**
667  * Main driver: generates code for one routine
668  */
669 void ppc32_gen_routine(const ppc32_code_gen_t *cg, ir_graph *irg)
670 {
671         ir_node *block;
672         int i, n;
673
674         ppc32_register_emitters();
675
676         ppc32_emit_start(irg);
677         irg_block_walk_graph(irg, ppc32_gen_labels, NULL, NULL);
678
679         n = ARR_LEN(cg->blk_sched);
680         for (i = 0; i < n;) {
681                 ir_node *next_bl;
682
683                 block   = cg->blk_sched[i];
684                 ++i;
685                 next_bl = i < n ? cg->blk_sched[i] : NULL;
686
687                 /* set here the link. the emitter expects to find the next block here */
688                 set_irn_link(block, next_bl);
689                 ppc32_gen_block(block);
690         }
691         ppc32_emit_end(irg);
692 }