rename type entity into ir_entity
[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  * Add a number to a prefix. This number will not be used a second time.
324  */
325 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
326         static unsigned long id = 0;
327         snprintf(buf, buflen, "%s%lu", prefix, ++id);
328         return buf;
329 }
330
331
332 /**
333  * Returns the target label for a control flow node.
334  */
335 static char *get_cfop_target(const ir_node *irn, char *buf) {
336         ir_node *bl = get_irn_link(irn);
337
338         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
339         return buf;
340 }
341
342 /**
343  * Emits code for a unconditional jump.
344  */
345 static void emit_Jmp(const ir_node *irn, ppc32_emit_env_t *env) {
346         FILE *F = env->out;
347         ir_node *block;
348
349         block = get_nodes_block(irn);
350         if(get_irn_link(irn) != get_irn_link(block))
351                 ir_fprintf(F, "\tb %s\t\t\t/* Branch(%+F) */\n", get_cfop_target(irn, printbuf), get_irn_link(irn));
352         else
353                 ir_fprintf(F, "\t\t\t\t\t\t/* fallthrough(%+F) */\n", get_irn_link(irn));
354 }
355
356 /**
357  * Emits code for a call
358  */
359 static void emit_be_Call(const ir_node *irn, ppc32_emit_env_t *env) {
360         FILE *F = env->out;
361         ir_entity *call_ent = be_Call_get_entity(irn);
362
363         if(call_ent)
364         {
365                 ir_fprintf(F, "\tbl      %s\t\t\t/* Branch and link(%+F) */\n", get_entity_name(call_ent), irn);
366         }
367         else
368         {
369                 ir_node *node = get_irn_n(irn, be_pos_Call_ptr);
370                 lc_efprintf(ppc32_get_arg_env(), F, "\tmtlr %1D\t\t\t/* Move to link register */\n", node);
371                 ir_fprintf(F, "\tblrl\t\t\t/* Branch to link register and link(%+F) */\n", irn);
372         }
373 }
374
375 char *branchops[8] = { 0, "beq", "blt", "ble", "bgt", "bge", "bne", "b" };
376
377 static void emit_ppc32_Branch(const ir_node *n, ppc32_emit_env_t *env) {
378         FILE *F = env->out;
379         int projnum = get_ppc32_proj_nr(n);
380
381         const ir_edge_t *edge = get_irn_out_edge_first(n);
382         ir_node *proj = get_edge_src_irn(edge);
383
384         int opind;
385
386         if(get_Proj_proj(proj) == pn_Cond_true)
387                 opind = projnum;
388         else
389                 opind = 7-projnum;
390
391         assert(opind>=0 && opind<8);
392
393         if(opind)
394         {
395                 get_cfop_target(proj, printbuf);
396                 lc_efprintf(ppc32_get_arg_env(), F, "\t%-8s%1S, %s\t\t\t/* Branch(%1S) to %s */\n",
397                         branchops[opind], n, printbuf, n, printbuf);
398         }
399
400         edge = get_irn_out_edge_next(n, edge);
401
402         if(edge)
403         {
404                 ir_node *irn = get_edge_src_irn(edge);
405                 lc_efprintf(ppc32_get_arg_env(), F, "\tb       %s\t\t\t/* Branch(%+F) */\n",
406                         get_cfop_target(irn, printbuf), get_irn_link(irn));
407         }
408 }
409
410 static void emit_ppc32_LoopCopy(const ir_node *n, ppc32_emit_env_t *env) {
411         FILE *F = env->out;
412         fprintf(F, "LOOP_%ld:\n", get_irn_node_nr(n));
413         lc_efprintf(ppc32_get_arg_env(), F, "\tlwzu    %5D, 4(%2S)\t\t\t/* Load with update */\n",n,n);
414         lc_efprintf(ppc32_get_arg_env(), F, "\tstwu    %5D, 4(%3S)\t\t\t/* Store with update */\n",n,n);
415         lc_efprintf(ppc32_get_arg_env(), F, "\tbdnz    LOOP_%i\t\t\t/* Branch with decrement if CTR != 0 */\n",
416                 get_irn_node_nr(n));
417 }
418
419 static void emit_ppc32_Switch(const ir_node *n, ppc32_emit_env_t *env) {
420         FILE *F = env->out;
421         ir_node *proj,*defproj=NULL;
422         int pn;
423
424         const ir_edge_t* edge;
425         foreach_out_edge(n, edge) {
426                 proj = get_edge_src_irn(edge);
427                 assert(is_Proj(proj) && "Only proj allowed at Switch");
428                 if(get_irn_mode(proj) != mode_X) continue;
429
430                 pn = get_Proj_proj(proj);
431                 /* check for default proj */
432                 if (pn == get_ppc32_proj_nr(n)) {
433                         assert(defproj == NULL && "found two defProjs at Switch");
434                         defproj = proj;
435                 }
436                 else
437                 {
438
439                         lc_efprintf(ppc32_get_arg_env(), F, "\taddis   %2S, 0, hi16(%i)\t\t\t/* Load upper immediate */\n",n,pn);
440                         lc_efprintf(ppc32_get_arg_env(), F, "\tori     %2S, %2S, lo16(%i)\t\t\t/* Load lower immediate */\n",n,n,pn);
441                         lc_efprintf(ppc32_get_arg_env(), F, "\tcmp     %3S, %1S, %2S\t\t\t/* Compare */\n",n,n,n);
442                         lc_efprintf(ppc32_get_arg_env(), F, "\tbeq     %3S, %s\t\t\t/* Branch if equal */\n",
443                                 n,get_cfop_target(proj, printbuf));
444                 }
445         }
446         assert(defproj != NULL && "didn't find defProj at Switch");
447         lc_efprintf(ppc32_get_arg_env(), F, "\tb       %s\t\t\t/* Default case */\n", get_cfop_target(defproj, printbuf));
448 }
449
450 /**
451  * Emits code for a backend Copy node
452  */
453 static void emit_be_Copy(const ir_node *n, ppc32_emit_env_t *env) {
454         FILE *F = env->out;
455         const arch_register_class_t *regclass = arch_get_irn_reg_class(env->arch_env, n, 0);
456
457         if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
458         {
459                 lc_efprintf(ppc32_get_arg_env(), F, "\tmr      %1D, %1S\t\t\t/* Move register */\n",n,n);
460         }
461         else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
462         {
463                 lc_efprintf(ppc32_get_arg_env(), F, "\tfmr     %1D, %1S\t\t\t/* Move register */\n",n,n);
464         }
465         else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition])
466         {
467                 lc_efprintf(ppc32_get_arg_env(), F, "\tmcrf    %1D, %1S\t\t\t/* Move register */\n",n,n);
468         }
469         else assert(0 && "Illegal register class for Copy");
470 }
471
472 /**
473  * Emits code for a backend Perm node
474  */
475 static void emit_be_Perm(const ir_node *n, ppc32_emit_env_t *env) {
476         FILE *F = env->out;
477         const arch_register_class_t *regclass = arch_get_irn_reg_class(env->arch_env, n, 0);
478
479         if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
480         {
481                 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);
482                 lc_efprintf(ppc32_get_arg_env(), F, "\txor     %2S, %1S, %2S\t\t\t/* (continued) */\n",n,n,n);
483                 lc_efprintf(ppc32_get_arg_env(), F, "\txor     %1S, %1S, %2S\t\t\t/* (continued) */\n",n,n,n);
484         }
485         else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
486         {
487                 lc_efprintf(ppc32_get_arg_env(), F, "\tfmr     f0, %1S\t\t\t/* Swap %1S, %2S with moves */\n",n,n,n);
488                 lc_efprintf(ppc32_get_arg_env(), F, "\tfmr     %1S, %2S\t\t\t/* (continued) */\n",n,n);
489                 lc_efprintf(ppc32_get_arg_env(), F, "\tfmr     %2S, f0\t\t\t/* (continued) */\n",n);
490         }
491         else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition])
492         {
493                 lc_efprintf(ppc32_get_arg_env(), F, "\tmcrf    cr7, %1S\t\t\t/* Swap %1S, %2S with moves */\n",n,n,n);
494                 lc_efprintf(ppc32_get_arg_env(), F, "\tmcrf    %1S, %2S\t\t\t/* (continued) */\n",n,n);
495                 lc_efprintf(ppc32_get_arg_env(), F, "\tmcrf    %2S, cr7\t\t\t/* (continued) */\n",n);
496         }
497         else assert(0 && "Illegal register class for Perm");
498
499 }
500
501
502 /**
503  * Emits code for a proj -> node
504  */
505 static void emit_Proj(const ir_node *irn, ppc32_emit_env_t *env) {
506         ir_node *pred = get_Proj_pred(irn);
507
508         if (get_irn_op(pred) == op_Start) {
509                 switch(get_Proj_proj(irn)) {
510                         case pn_Start_X_initial_exec:
511                                 emit_Jmp(irn, env);
512                                 break;
513                         default:
514                                 break;
515                 }
516         }
517 }
518
519 static void emit_be_IncSP(const ir_node *irn, ppc32_emit_env_t *emit_env) {
520         FILE          *F    = emit_env->out;
521         int offs = be_get_IncSP_offset(irn);
522
523         fprintf(F, "\t\t\t\t\t/* ignored IncSP with %d */\n", -offs);
524
525 //      if (offs) {
526 //              assert(offs<=0x7fff);
527 //              lc_efprintf(ppc32_get_arg_env(), F, "\taddi    %1S, %1S, %d\t\t\t/* %+F (IncSP) */\n", irn, irn,
528 //                      -offs, irn);
529 //      }
530 //      else {
531 //              fprintf(F, "\t\t\t\t\t/* omitted IncSP with 0 */\n");
532 //      }
533 }
534
535 /*static void emit_Spill(const ir_node *irn, ppc32_emit_env_t *emit_env) {
536         ir_node *context = be_get_Spill_context(irn);
537         ir_entity *entity = be_get_spill_entity(irn);
538 }*/
539
540 /***********************************************************************************
541  *                  _          __                                             _
542  *                 (_)        / _|                                           | |
543  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
544  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
545  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
546  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
547  *
548  ***********************************************************************************/
549
550 static void ppc32_register_emitters(void) {
551         /* first clear generic function pointers */
552         clear_irp_opcodes_generic_func();
553
554         /* register generated emitter functions */
555         ppc32_register_spec_emitters();
556
557 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
558
559         EMIT(ppc32_Branch);
560         EMIT(ppc32_LoopCopy);
561         EMIT(ppc32_Switch);
562         EMIT(be_Call);
563         EMIT(Jmp);
564         EMIT(Proj);
565         EMIT(be_IncSP);
566         EMIT(be_Copy);
567         EMIT(be_Perm);
568 //      EMIT(Spill);
569 //      EMIT(Reload);
570 }
571
572 /**
573  * Emits code for a node.
574  */
575 static void ppc32_emit_node(ir_node *irn, void *env) {
576         ppc32_emit_env_t  *emit_env = env;
577         FILE              *F        = emit_env->out;
578         ir_op             *op       = get_irn_op(irn);
579         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
580
581         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
582
583         if (op->ops.generic) {
584             void (*emit)(ir_node *, void *) = (void (*)(ir_node *, void *))op->ops.generic;
585                 (*emit)(irn, env);
586         }
587         else {
588                 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
589         }
590 }
591
592
593 /**
594  * Walks over the nodes in a block connected by scheduling edges
595  * and emits code for each node.
596  */
597 static void ppc32_gen_block(ir_node *block, void *env) {
598         ir_node *irn;
599
600         if (! is_Block(block))
601                 return;
602
603         fprintf(((ppc32_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
604         sched_foreach(block, irn) {
605                 ppc32_emit_node(irn, env);
606         }
607 }
608
609
610 /**
611  * Emits code for function start.
612  */
613 void ppc32_emit_start(FILE *F, ir_graph *irg, ppc32_emit_env_t *env) {
614         const char *irg_name  = get_entity_ld_name(get_irg_entity(irg));
615         int         framesize = get_type_size_bytes(get_irg_frame_type(env->cg->irg));
616
617         if(! strcmp(irg_name, "main"))                                             // XXX: underscore hack
618         {
619                 fprintf(F, "\t.text\n");
620                 fprintf(F, "\t.globl _main\n");
621                 fprintf(F, "\t.align 4\n");
622                 fprintf(F, "_main:\n");
623         }
624         else
625         {
626                 fprintf(F, "\t.text\n");
627                 fprintf(F, "\t.globl %s\n", irg_name);
628                 fprintf(F, "\t.align 4\n");
629                 fprintf(F, "%s:\n", irg_name);
630         }
631
632         if(framesize > 24)
633         {
634                 fprintf(F, "\tmflr    r0\n");
635                 fprintf(F, "\tstw     r0, 8(r1)\n");
636                 fprintf(F, "\tstwu    r1, -%i(r1)\n", framesize);
637         }
638         else
639         {
640                 fprintf(F, "\t\t\t\t\t/* set new frame (%d) omitted */\n", framesize);
641         }
642
643
644 /*      if(!isleaf)
645         {
646                 // store link register in linkage area (TODO: if needed)
647
648                 fprintf(F, "\tmflr    r0\n");
649                 fprintf(F, "\tstwu    r0, -4(r1)\n");   // stw r0, 8(SP)
650         }*/
651 }
652
653 /**
654  * Emits code for function end
655  */
656 void ppc32_emit_end(FILE *F, ir_graph *irg, ppc32_emit_env_t *env) {
657         int framesize = get_type_size_bytes(get_irg_frame_type(env->cg->irg));
658
659 /*      if(!isleaf)
660         {
661                 // restore link register
662
663                 fprintf(F, "\tlwz     r0, 0(r1)\n");
664                 fprintf(F, "\taddi    r1, r1, 4\n");
665                 fprintf(F, "\tmtlr    r0\n");
666         }*/
667         if(framesize > 24)
668         {
669                 fprintf(F, "\tlwz     r1, 0(r1)\n");
670                 fprintf(F, "\tlwz     r0, 8(r1)\n");
671                 fprintf(F, "\tmtlr    r0\n");
672         }
673         fprintf(F, "\tblr\n\n");
674 }
675
676 /**
677  * Sets labels for control flow nodes (jump target)
678  * TODO: Jump optimization
679  */
680 void ppc32_gen_labels(ir_node *block, void *env) {
681         ir_node *pred;
682         int n;
683
684         for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
685                 pred = get_Block_cfgpred(block, n);
686                 set_irn_link(pred, block);
687         }
688 }
689
690 /**
691  * Main driver: generates code for one routine
692  */
693 void ppc32_gen_routine(FILE *F, ir_graph *irg, const ppc32_code_gen_t *cg) {
694         ppc32_emit_env_t emit_env;
695         ir_node *block;
696         int i, n;
697
698         emit_env.out      = F;
699         emit_env.arch_env = cg->arch_env;
700         emit_env.cg       = cg;
701         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ppc.emit");
702
703         /* set the global arch_env (needed by print hooks) */
704         arch_env = cg->arch_env;
705
706         ppc32_register_emitters();
707
708         ppc32_emit_start(F, irg, &emit_env);
709         irg_block_walk_graph(irg, ppc32_gen_labels, NULL, &emit_env);
710
711         n = ARR_LEN(cg->blk_sched);
712         for (i = 0; i < n;) {
713                 ir_node *next_bl;
714
715                 block   = cg->blk_sched[i];
716                 ++i;
717                 next_bl = i < n ? cg->blk_sched[i] : NULL;
718
719                 /* set here the link. the emitter expects to find the next block here */
720                 set_irn_link(block, next_bl);
721                 ppc32_gen_block(block, &emit_env);
722         }
723         ppc32_emit_end(F, irg, &emit_env);
724 }