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