20 #include "../besched_t.h"
21 #include "../benode_t.h"
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"
30 #define SNPRINTF_BUF_LEN 128
32 static const arch_env_t *arch_env = NULL;
33 static char printbuf[SNPRINTF_BUF_LEN];
34 static char printbuf2[SNPRINTF_BUF_LEN];
39 /*************************************************************
41 * (_) | | / _| | | | |
42 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
43 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
44 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
45 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
48 *************************************************************/
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);
59 * Return a const or symconst as string.
61 static const char *node_const_to_str(ir_node *n) {
63 switch(get_ppc32_type(n))
66 tarval_snprintf(printbuf, SNPRINTF_BUF_LEN, get_ppc32_constant_tarval(n));
69 case ppc32_ac_SymConst:
70 buf=get_id_str(get_ppc32_symconst_ident(n));
73 snprintf(printbuf, SNPRINTF_BUF_LEN, "%i", get_ppc32_offset(n));
76 assert(0 && "node_const_to_str(): Illegal offset type");
79 switch(get_ppc32_offset_mode(n))
84 snprintf(printbuf2, SNPRINTF_BUF_LEN, "lo16(%s)", buf);
87 snprintf(printbuf2, SNPRINTF_BUF_LEN, "hi16(%s)", buf);
90 snprintf(printbuf2, SNPRINTF_BUF_LEN, "ha16(%s)", buf);
93 assert(0 && "node_const_to_str(): Illegal offset mode");
99 * Returns node's offset as string.
101 static const char *node_offset_to_str(ir_node *n) {
103 if(get_ppc32_type(n)==ppc32_ac_None) return "0";
104 switch(get_ppc32_type(n))
107 tarval_snprintf(printbuf, SNPRINTF_BUF_LEN, get_ppc32_constant_tarval(n));
110 case ppc32_ac_SymConst:
111 buf=get_id_str(get_ppc32_symconst_ident(n));
113 case ppc32_ac_Offset:
114 snprintf(printbuf, SNPRINTF_BUF_LEN, "%i", get_ppc32_offset(n));
117 assert(0 && "node_offset_to_str(): Illegal offset type");
120 switch(get_ppc32_offset_mode(n))
125 snprintf(printbuf2, SNPRINTF_BUF_LEN, "lo16(%s)", buf);
128 snprintf(printbuf2, SNPRINTF_BUF_LEN, "hi16(%s)", buf);
131 snprintf(printbuf2, SNPRINTF_BUF_LEN, "ha16(%s)", buf);
134 assert(0 && "node_offset_to_str(): Illegal offset mode");
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;
146 * Returns the register at in position pos.
148 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
150 const arch_register_t *reg = NULL;
152 assert(get_irn_arity(irn) > pos && "Invalid IN position");
154 /* The out register of the operator at position pos is the
155 in register we need. */
156 op = get_irn_n(irn, pos);
158 reg = arch_get_irn_register(arch_env, op);
160 assert(reg && "no in register found");
165 * Returns the register at out position pos.
167 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
169 const arch_register_t *reg = NULL;
171 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
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 */
178 if (get_irn_mode(irn) != mode_T) {
179 reg = arch_get_irn_register(arch_env, irn);
181 else if (is_ppc32_irn(irn)) {
182 reg = get_ppc32_out_reg(irn, pos);
185 const ir_edge_t *edge;
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);
197 assert(reg && "no out register found");
202 * Returns the number of the in register at position pos.
204 int get_ppc32_reg_nr(ir_node *irn, int pos, int in_out) {
205 const arch_register_t *reg;
208 reg = get_in_reg(irn, pos);
211 reg = get_out_reg(irn, pos);
214 return arch_register_get_index(reg);
218 * Returns the name of the in register at position pos.
220 const char *get_ppc32_reg_name(ir_node *irn, int pos, int in_out) {
221 const arch_register_t *reg;
224 reg = get_in_reg(irn, pos);
227 reg = get_out_reg(irn, pos);
230 return arch_register_get_name(reg);
234 * Get the register name for a node.
236 static int ppc32_get_reg_name(lc_appendable_t *app,
237 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
240 ir_node *X = arg->v_ptr;
241 int nr = occ->width - 1;
244 return lc_arg_append(app, occ, "(null)", 6);
246 if (occ->conversion == 'S') {
247 buf = get_ppc32_reg_name(X, nr, 1);
250 buf = get_ppc32_reg_name(X, nr, 0);
253 // lc_appendable_chadd(app, '%');
254 return lc_arg_append(app, occ, buf, strlen(buf));
258 * Returns the tarval or offset of an ppc node as a string.
260 static int ppc32_const_to_str(lc_appendable_t *app,
261 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
264 ir_node *X = arg->v_ptr;
267 return lc_arg_append(app, occ, "(null)", 6);
269 if (occ->conversion == 'C') {
270 buf = node_const_to_str(X);
273 buf = node_offset_to_str(X);
276 return lc_arg_append(app, occ, buf, strlen(buf));
280 * Determines the SSE suffix depending on the mode.
282 static int ppc32_get_mode_suffix(lc_appendable_t *app,
283 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
285 ir_node *X = arg->v_ptr;
288 return lc_arg_append(app, occ, "(null)", 6);
290 if (get_mode_size_bits(get_irn_mode(X)) == 32)
291 return lc_appendable_chadd(app, 's');
293 return lc_appendable_chadd(app, 'd');
297 * Return the ppc printf arg environment.
298 * We use the firm environment with some additional handlers.
300 const lc_arg_env_t *ppc32_get_arg_env(void) {
301 static lc_arg_env_t *env = NULL;
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 };
308 /* extend the firm printer */
309 env = firm_get_arg_env();
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);
323 * Add a number to a prefix. This number will not be used a second time.
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);
333 * Returns the target label for a control flow node.
335 static char *get_cfop_target(const ir_node *irn, char *buf) {
336 ir_node *bl = get_irn_link(irn);
338 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
343 * Emits code for a unconditional jump.
345 static void emit_Jmp(const ir_node *irn, ppc32_emit_env_t *env) {
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));
353 ir_fprintf(F, "\t\t\t\t\t\t/* fallthrough(%+F) */\n", get_irn_link(irn));
357 * Emits code for a call
359 static void emit_be_Call(const ir_node *irn, ppc32_emit_env_t *env) {
361 entity *call_ent = be_Call_get_entity(irn);
365 ir_fprintf(F, "\tbl %s\t\t\t/* Branch and link(%+F) */\n", get_entity_name(call_ent), irn);
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);
375 char *branchops[8] = { 0, "beq", "blt", "ble", "bgt", "bge", "bne", "b" };
377 static void emit_ppc32_Branch(const ir_node *n, ppc32_emit_env_t *env) {
379 int projnum = get_ppc32_proj_nr(n);
381 const ir_edge_t *edge = get_irn_out_edge_first(n);
382 ir_node *proj = get_edge_src_irn(edge);
386 if(get_Proj_proj(proj) == pn_Cond_true)
391 assert(opind>=0 && opind<8);
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);
400 edge = get_irn_out_edge_next(n, edge);
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));
410 static void emit_ppc32_LoopCopy(const ir_node *n, ppc32_emit_env_t *env) {
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",
419 static void emit_ppc32_Switch(const ir_node *n, ppc32_emit_env_t *env) {
421 ir_node *proj,*defproj=NULL;
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;
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");
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));
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));
451 * Emits code for a backend Copy node
453 static void emit_be_Copy(const ir_node *n, ppc32_emit_env_t *env) {
455 const arch_register_class_t *regclass = arch_get_irn_reg_class(env->arch_env, n, 0);
457 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
459 lc_efprintf(ppc32_get_arg_env(), F, "\tmr %1D, %1S\t\t\t/* Move register */\n",n,n);
461 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
463 lc_efprintf(ppc32_get_arg_env(), F, "\tfmr %1D, %1S\t\t\t/* Move register */\n",n,n);
465 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition])
467 lc_efprintf(ppc32_get_arg_env(), F, "\tmcrf %1D, %1S\t\t\t/* Move register */\n",n,n);
469 else assert(0 && "Illegal register class for Copy");
473 * Emits code for a backend Perm node
475 static void emit_be_Perm(const ir_node *n, ppc32_emit_env_t *env) {
477 const arch_register_class_t *regclass = arch_get_irn_reg_class(env->arch_env, n, 0);
479 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
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);
485 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
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);
491 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition])
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);
497 else assert(0 && "Illegal register class for Perm");
503 * Emits code for a proj -> node
505 static void emit_Proj(const ir_node *irn, ppc32_emit_env_t *env) {
506 ir_node *pred = get_Proj_pred(irn);
508 if (get_irn_op(pred) == op_Start) {
509 switch(get_Proj_proj(irn)) {
510 case pn_Start_X_initial_exec:
519 static void emit_be_IncSP(const ir_node *irn, ppc32_emit_env_t *emit_env) {
520 FILE *F = emit_env->out;
521 unsigned offs = be_get_IncSP_offset(irn);
522 be_stack_dir_t dir = be_get_IncSP_direction(irn);
524 fprintf(F, "\t\t\t\t\t/* ignored IncSP with %c%i */\n", dir==be_stack_dir_expand ? '-' : ' ', offs);
527 // assert(offs<=0x7fff);
528 // lc_efprintf(ppc32_get_arg_env(), F, "\taddi %1S, %1S,%s%u\t\t\t/* %+F (IncSP) */\n", irn, irn,
529 // (dir == be_stack_dir_expand) ? " -" : " ", offs, irn);
532 // fprintf(F, "\t\t\t\t\t/* omitted IncSP with 0 */\n");
536 /*static void emit_Spill(const ir_node *irn, ppc32_emit_env_t *emit_env) {
537 ir_node *context = be_get_Spill_context(irn);
538 entity *entity = be_get_spill_entity(irn);
541 /***********************************************************************************
544 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
545 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
546 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
547 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
549 ***********************************************************************************/
551 static void ppc32_register_emitters(void) {
552 /* first clear generic function pointers */
553 clear_irp_opcodes_generic_func();
555 /* register generated emitter functions */
556 ppc32_register_spec_emitters();
558 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
561 EMIT(ppc32_LoopCopy);
574 * Emits code for a node.
576 static void ppc32_emit_node(ir_node *irn, void *env) {
577 ppc32_emit_env_t *emit_env = env;
578 FILE *F = emit_env->out;
579 ir_op *op = get_irn_op(irn);
580 DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
582 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
584 if (op->ops.generic) {
585 void (*emit)(ir_node *, void *) = (void (*)(ir_node *, void *))op->ops.generic;
589 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
595 * Walks over the nodes in a block connected by scheduling edges
596 * and emits code for each node.
598 static void ppc32_gen_block(ir_node *block, void *env) {
601 if (! is_Block(block))
604 fprintf(((ppc32_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
605 sched_foreach(block, irn) {
606 ppc32_emit_node(irn, env);
612 * Emits code for function start.
614 void ppc32_emit_start(FILE *F, ir_graph *irg, ppc32_emit_env_t *env) {
615 const char *irg_name = get_entity_ld_name(get_irg_entity(irg));
616 int framesize = get_type_size_bytes(get_irg_frame_type(env->cg->irg));
618 if(! strcmp(irg_name, "main")) // XXX: underscore hack
620 fprintf(F, "\t.text\n");
621 fprintf(F, "\t.globl _main\n");
622 fprintf(F, "\t.align 4\n");
623 fprintf(F, "_main:\n");
627 fprintf(F, "\t.text\n");
628 fprintf(F, "\t.globl %s\n", irg_name);
629 fprintf(F, "\t.align 4\n");
630 fprintf(F, "%s:\n", irg_name);
635 fprintf(F, "\tmflr r0\n");
636 fprintf(F, "\tstw r0, 8(r1)\n");
637 fprintf(F, "\tstwu r1, -%i(r1)\n", framesize);
641 fprintf(F, "\t\t\t\t\t/* set new frame (%d) omitted */\n", framesize);
647 // store link register in linkage area (TODO: if needed)
649 fprintf(F, "\tmflr r0\n");
650 fprintf(F, "\tstwu r0, -4(r1)\n"); // stw r0, 8(SP)
655 * Emits code for function end
657 void ppc32_emit_end(FILE *F, ir_graph *irg, ppc32_emit_env_t *env) {
658 int framesize = get_type_size_bytes(get_irg_frame_type(env->cg->irg));
662 // restore link register
664 fprintf(F, "\tlwz r0, 0(r1)\n");
665 fprintf(F, "\taddi r1, r1, 4\n");
666 fprintf(F, "\tmtlr r0\n");
670 fprintf(F, "\tlwz r1, 0(r1)\n");
671 fprintf(F, "\tlwz r0, 8(r1)\n");
672 fprintf(F, "\tmtlr r0\n");
674 fprintf(F, "\tblr\n\n");
678 * Sets labels for control flow nodes (jump target)
679 * TODO: Jump optimization
681 void ppc32_gen_labels(ir_node *block, void *env) {
685 for (n = get_Block_n_cfgpreds(block) - 1; n >= 0; --n) {
686 pred = get_Block_cfgpred(block, n);
687 set_irn_link(pred, block);
692 * Main driver: generates code for one routine
694 void ppc32_gen_routine(FILE *F, ir_graph *irg, const ppc32_code_gen_t *cg) {
695 ppc32_emit_env_t emit_env;
700 emit_env.arch_env = cg->arch_env;
702 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ppc.emit");
704 /* set the global arch_env (needed by print hooks) */
705 arch_env = cg->arch_env;
707 ppc32_register_emitters();
709 ppc32_emit_start(F, irg, &emit_env);
710 irg_block_walk_graph(irg, ppc32_gen_labels, NULL, &emit_env);
712 n = ARR_LEN(cg->blk_sched);
713 for (i = 0; i < n;) {
716 block = cg->blk_sched[i];
718 next_bl = i < n ? cg->blk_sched[i] : NULL;
720 /* set here the link. the emitter expects to find the next block here */
721 set_irn_link(block, next_bl);
722 ppc32_gen_block(block, &emit_env);
724 ppc32_emit_end(F, irg, &emit_env);