2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
23 * @author Moritz Kroll, Jens Mueller
42 #include "../besched_t.h"
43 #include "../benode_t.h"
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"
52 #define SNPRINTF_BUF_LEN 128
54 static const arch_env_t *arch_env = NULL;
55 static char printbuf[SNPRINTF_BUF_LEN];
56 static char printbuf2[SNPRINTF_BUF_LEN];
61 /*************************************************************
63 * (_) | | / _| | | | |
64 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
65 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
66 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
67 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
70 *************************************************************/
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);
81 * Return a const or symconst as string.
83 static const char *node_const_to_str(ir_node *n) {
85 switch(get_ppc32_type(n))
88 tarval_snprintf(printbuf, SNPRINTF_BUF_LEN, get_ppc32_constant_tarval(n));
91 case ppc32_ac_SymConst:
92 buf=get_id_str(get_ppc32_symconst_ident(n));
95 snprintf(printbuf, SNPRINTF_BUF_LEN, "%i", get_ppc32_offset(n));
98 assert(0 && "node_const_to_str(): Illegal offset type");
101 switch(get_ppc32_offset_mode(n))
106 snprintf(printbuf2, SNPRINTF_BUF_LEN, "lo16(%s)", buf);
109 snprintf(printbuf2, SNPRINTF_BUF_LEN, "hi16(%s)", buf);
112 snprintf(printbuf2, SNPRINTF_BUF_LEN, "ha16(%s)", buf);
115 assert(0 && "node_const_to_str(): Illegal offset mode");
121 * Returns node's offset as string.
123 static const char *node_offset_to_str(ir_node *n) {
125 if(get_ppc32_type(n)==ppc32_ac_None) return "0";
126 switch(get_ppc32_type(n))
129 tarval_snprintf(printbuf, SNPRINTF_BUF_LEN, get_ppc32_constant_tarval(n));
132 case ppc32_ac_SymConst:
133 buf=get_id_str(get_ppc32_symconst_ident(n));
135 case ppc32_ac_Offset:
136 snprintf(printbuf, SNPRINTF_BUF_LEN, "%i", get_ppc32_offset(n));
139 assert(0 && "node_offset_to_str(): Illegal offset type");
142 switch(get_ppc32_offset_mode(n))
147 snprintf(printbuf2, SNPRINTF_BUF_LEN, "lo16(%s)", buf);
150 snprintf(printbuf2, SNPRINTF_BUF_LEN, "hi16(%s)", buf);
153 snprintf(printbuf2, SNPRINTF_BUF_LEN, "ha16(%s)", buf);
156 assert(0 && "node_offset_to_str(): Illegal offset mode");
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;
168 * Returns the register at in position pos.
170 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
172 const arch_register_t *reg = NULL;
174 assert(get_irn_arity(irn) > pos && "Invalid IN position");
176 /* The out register of the operator at position pos is the
177 in register we need. */
178 op = get_irn_n(irn, pos);
180 reg = arch_get_irn_register(arch_env, op);
182 assert(reg && "no in register found");
187 * Returns the register at out position pos.
189 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
191 const arch_register_t *reg = NULL;
193 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
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 */
200 if (get_irn_mode(irn) != mode_T) {
201 reg = arch_get_irn_register(arch_env, irn);
203 else if (is_ppc32_irn(irn)) {
204 reg = get_ppc32_out_reg(irn, pos);
207 const ir_edge_t *edge;
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);
219 assert(reg && "no out register found");
224 * Returns the number of the in register at position pos.
226 int get_ppc32_reg_nr(ir_node *irn, int pos, int in_out) {
227 const arch_register_t *reg;
230 reg = get_in_reg(irn, pos);
233 reg = get_out_reg(irn, pos);
236 return arch_register_get_index(reg);
240 * Returns the name of the in register at position pos.
242 const char *get_ppc32_reg_name(ir_node *irn, int pos, int in_out) {
243 const arch_register_t *reg;
246 reg = get_in_reg(irn, pos);
249 reg = get_out_reg(irn, pos);
252 return arch_register_get_name(reg);
256 * Get the register name for a node.
258 static int ppc32_get_reg_name(lc_appendable_t *app,
259 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
262 ir_node *X = arg->v_ptr;
263 int nr = occ->width - 1;
266 return lc_arg_append(app, occ, "(null)", 6);
268 if (occ->conversion == 'S') {
269 buf = get_ppc32_reg_name(X, nr, 1);
272 buf = get_ppc32_reg_name(X, nr, 0);
275 // lc_appendable_chadd(app, '%');
276 return lc_arg_append(app, occ, buf, strlen(buf));
280 * Returns the tarval or offset of an ppc node as a string.
282 static int ppc32_const_to_str(lc_appendable_t *app,
283 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
286 ir_node *X = arg->v_ptr;
289 return lc_arg_append(app, occ, "(null)", 6);
291 if (occ->conversion == 'C') {
292 buf = node_const_to_str(X);
295 buf = node_offset_to_str(X);
298 return lc_arg_append(app, occ, buf, strlen(buf));
302 * Determines the SSE suffix depending on the mode.
304 static int ppc32_get_mode_suffix(lc_appendable_t *app,
305 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
307 ir_node *X = arg->v_ptr;
310 return lc_arg_append(app, occ, "(null)", 6);
312 if (get_mode_size_bits(get_irn_mode(X)) == 32)
313 return lc_appendable_chadd(app, 's');
315 return lc_appendable_chadd(app, 'd');
319 * Return the ppc printf arg environment.
320 * We use the firm environment with some additional handlers.
322 const lc_arg_env_t *ppc32_get_arg_env(void) {
323 static lc_arg_env_t *env = NULL;
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 };
330 /* extend the firm printer */
331 env = firm_get_arg_env();
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);
345 * Returns the target label for a control flow node.
347 static char *get_cfop_target(const ir_node *irn, char *buf) {
348 ir_node *bl = get_irn_link(irn);
350 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
355 * Emits code for a unconditional jump.
357 static void emit_Jmp(const ir_node *irn, ppc32_emit_env_t *env) {
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));
365 ir_fprintf(F, "\t\t\t\t\t\t/* fallthrough(%+F) */\n", get_irn_link(irn));
369 * Emits code for a call
371 static void emit_be_Call(const ir_node *irn, ppc32_emit_env_t *env) {
373 ir_entity *call_ent = be_Call_get_entity(irn);
377 ir_fprintf(F, "\tbl %s\t\t\t/* Branch and link(%+F) */\n", get_entity_name(call_ent), irn);
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);
387 char *branchops[8] = { 0, "beq", "blt", "ble", "bgt", "bge", "bne", "b" };
389 static void emit_ppc32_Branch(const ir_node *n, ppc32_emit_env_t *env) {
391 int projnum = get_ppc32_proj_nr(n);
393 const ir_edge_t *edge = get_irn_out_edge_first(n);
394 ir_node *proj = get_edge_src_irn(edge);
398 if(get_Proj_proj(proj) == pn_Cond_true)
403 assert(opind>=0 && opind<8);
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);
412 edge = get_irn_out_edge_next(n, edge);
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));
422 static void emit_ppc32_LoopCopy(const ir_node *n, ppc32_emit_env_t *env) {
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",
431 static void emit_ppc32_Switch(const ir_node *n, ppc32_emit_env_t *env) {
433 ir_node *proj,*defproj=NULL;
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;
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");
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));
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));
463 * Emits code for a backend Copy node
465 static void emit_be_Copy(const ir_node *n, ppc32_emit_env_t *env) {
467 const arch_register_class_t *regclass = arch_get_irn_reg_class(env->arch_env, n, 0);
469 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
471 lc_efprintf(ppc32_get_arg_env(), F, "\tmr %1D, %1S\t\t\t/* Move register */\n",n,n);
473 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
475 lc_efprintf(ppc32_get_arg_env(), F, "\tfmr %1D, %1S\t\t\t/* Move register */\n",n,n);
477 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition])
479 lc_efprintf(ppc32_get_arg_env(), F, "\tmcrf %1D, %1S\t\t\t/* Move register */\n",n,n);
481 else assert(0 && "Illegal register class for Copy");
485 * Emits code for a backend Perm node
487 static void emit_be_Perm(const ir_node *n, ppc32_emit_env_t *env) {
489 const arch_register_class_t *regclass = arch_get_irn_reg_class(env->arch_env, n, 0);
491 if (regclass == &ppc32_reg_classes[CLASS_ppc32_gp])
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);
497 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_fp])
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);
503 else if (regclass == &ppc32_reg_classes[CLASS_ppc32_condition])
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);
509 else assert(0 && "Illegal register class for Perm");
515 * Emits code for a proj -> node
517 static void emit_Proj(const ir_node *irn, ppc32_emit_env_t *env) {
518 ir_node *pred = get_Proj_pred(irn);
520 if (get_irn_op(pred) == op_Start) {
521 switch(get_Proj_proj(irn)) {
522 case pn_Start_X_initial_exec:
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);
535 fprintf(F, "\t\t\t\t\t/* ignored IncSP with %d */\n", -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,
543 // fprintf(F, "\t\t\t\t\t/* omitted IncSP with 0 */\n");
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);
552 /***********************************************************************************
555 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
556 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
557 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
558 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
560 ***********************************************************************************/
562 static void ppc32_register_emitters(void) {
563 /* first clear generic function pointers */
564 clear_irp_opcodes_generic_func();
566 /* register generated emitter functions */
567 ppc32_register_spec_emitters();
569 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
572 EMIT(ppc32_LoopCopy);
585 * Emits code for a node.
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;)
593 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
595 if (op->ops.generic) {
596 void (*emit)(ir_node *, void *) = (void (*)(ir_node *, void *))op->ops.generic;
600 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
606 * Walks over the nodes in a block connected by scheduling edges
607 * and emits code for each node.
609 static void ppc32_gen_block(ir_node *block, void *env) {
612 if (! is_Block(block))
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);
623 * Emits code for function start.
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));
629 if(! strcmp(irg_name, "main")) // XXX: underscore hack
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");
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);
646 fprintf(F, "\tmflr r0\n");
647 fprintf(F, "\tstw r0, 8(r1)\n");
648 fprintf(F, "\tstwu r1, -%i(r1)\n", framesize);
652 fprintf(F, "\t\t\t\t\t/* set new frame (%d) omitted */\n", framesize);
658 // store link register in linkage area (TODO: if needed)
660 fprintf(F, "\tmflr r0\n");
661 fprintf(F, "\tstwu r0, -4(r1)\n"); // stw r0, 8(SP)
666 * Emits code for function end
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));
673 // restore link register
675 fprintf(F, "\tlwz r0, 0(r1)\n");
676 fprintf(F, "\taddi r1, r1, 4\n");
677 fprintf(F, "\tmtlr r0\n");
681 fprintf(F, "\tlwz r1, 0(r1)\n");
682 fprintf(F, "\tlwz r0, 8(r1)\n");
683 fprintf(F, "\tmtlr r0\n");
685 fprintf(F, "\tblr\n\n");
689 * Sets labels for control flow nodes (jump target)
690 * TODO: Jump optimization
692 void ppc32_gen_labels(ir_node *block, void *env) {
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);
703 * Main driver: generates code for one routine
705 void ppc32_gen_routine(FILE *F, ir_graph *irg, const ppc32_code_gen_t *cg) {
706 ppc32_emit_env_t emit_env;
711 emit_env.arch_env = cg->arch_env;
713 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ppc.emit");
715 /* set the global arch_env (needed by print hooks) */
716 arch_env = cg->arch_env;
718 ppc32_register_emitters();
720 ppc32_emit_start(F, irg, &emit_env);
721 irg_block_walk_graph(irg, ppc32_gen_labels, NULL, &emit_env);
723 n = ARR_LEN(cg->blk_sched);
724 for (i = 0; i < n;) {
727 block = cg->blk_sched[i];
729 next_bl = i < n ? cg->blk_sched[i] : NULL;
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);
735 ppc32_emit_end(F, irg, &emit_env);