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