2 * Copyright (C) 1995-2008 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
22 * @brief implementation of mips assembly emitter
23 * @author Matthias Braun, Mehdi
42 #include "../besched.h"
43 #include "../benode_t.h"
44 #include "../beutil.h"
45 #include "../begnuas.h"
47 #include "mips_emitter.h"
48 #include "gen_mips_emitter.h"
49 #include "mips_nodes_attr.h"
50 #include "mips_new_nodes.h"
51 #include "mips_map_regs.h"
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55 #define BLOCK_PREFIX ".L"
57 #define SNPRINTF_BUF_LEN 128
60 * Returns the register at in position pos.
62 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
65 const arch_register_t *reg = NULL;
67 assert(get_irn_arity(node) > pos && "Invalid IN position");
69 /* The out register of the operator at position pos is the
70 in register we need. */
71 op = get_irn_n(node, pos);
73 reg = arch_get_irn_register(op);
75 assert(reg && "no in register found");
80 * Returns the register at out position pos.
82 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
85 const arch_register_t *reg = NULL;
87 /* 1st case: irn is not of mode_T, so it has only */
88 /* one OUT register -> good */
89 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
90 /* Proj with the corresponding projnum for the register */
92 if (get_irn_mode(node) != mode_T) {
93 reg = arch_get_irn_register(node);
94 } else if (is_mips_irn(node)) {
95 reg = get_mips_out_reg(node, pos);
97 const ir_edge_t *edge;
99 foreach_out_edge(node, edge) {
100 proj = get_edge_src_irn(edge);
101 assert(is_Proj(proj) && "non-Proj from mode_T node");
102 if (get_Proj_proj(proj) == pos) {
103 reg = arch_get_irn_register(proj);
109 assert(reg && "no out register found");
113 /*************************************************************
115 * (_) | | / _| | | | |
116 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
117 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
118 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
119 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
122 *************************************************************/
125 * Emit the name of the source register at given input position.
127 void mips_emit_source_register(const ir_node *node, int pos)
129 const arch_register_t *reg = get_in_reg(node, pos);
131 be_emit_string(arch_register_get_name(reg));
135 * Emit the name of the destination register at given output position.
137 void mips_emit_dest_register(const ir_node *node, int pos)
139 const arch_register_t *reg = get_out_reg(node, pos);
141 be_emit_string(arch_register_get_name(reg));
145 static const char *get_symconst_str(ir_node *node)
149 switch(get_SymConst_kind(node)) {
150 case symconst_addr_name:
151 id = get_SymConst_name(node);
152 return get_id_str(id);
153 case symconst_addr_ent:
154 id = get_entity_ident(get_SymConst_entity(node));
155 return get_id_str(id);
157 panic("Unsupported SymConst kind");
164 * Return a const or symconst as string.
166 static const char *node_const_to_str(ir_node *n)
169 const mips_attr_t *attr = get_mips_attr(n);
172 if(is_mips_load_r(n) || is_mips_store_r(n)) {
173 mips_attr_t *attr = get_mips_attr(n);
176 if(attr->tv != NULL) {
177 val = get_tarval_long(attr->tv);
178 snprintf(buf, sizeof(buf), "%ld", val);
182 if(attr->stack_entity != NULL) {
183 snprintf(buf, sizeof(buf), "%d", attr->stack_entity_offset);
187 symconst = get_irn_n(n, 1);
188 assert(get_irn_opcode(symconst) == iro_SymConst);
190 return get_symconst_str(symconst);
191 } else if(is_mips_la(n)) {
192 snprintf(buf, sizeof(buf), "%s", get_id_str(attr->symconst_id));
194 } else if(is_mips_lli(n)) {
195 assert(attr->tv != NULL);
196 if(get_mode_sign(get_tarval_mode(attr->tv))) {
197 long val = get_tarval_long(attr->tv);
198 snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
200 unsigned long val = get_tarval_long(attr->tv);
201 snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
205 } else if(is_mips_lui(n)) {
206 assert(attr->tv != NULL);
207 if(get_mode_sign(get_tarval_mode(attr->tv))) {
208 long val = get_tarval_long(attr->tv);
209 val = (val & 0xffff0000) >> 16;
210 snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
212 unsigned long val = get_tarval_long(attr->tv);
213 val = (val & 0xffff0000) >> 16;
214 snprintf(buf, sizeof(buf), "0x%04lX", val & 0xffff);
220 assert(attr->tv != NULL);
221 val = get_tarval_long(attr->tv);
222 snprintf(buf, sizeof(buf), "%ld", val);
228 void mips_emit_load_store_address(const ir_node *node, int pos)
230 const mips_load_store_attr_t *attr = get_mips_load_store_attr_const(node);
232 be_emit_irprintf("%d(", attr->offset);
233 mips_emit_source_register(node, pos);
237 void mips_emit_immediate_suffix(const ir_node *node, int pos)
239 ir_node *op = get_irn_n(node, pos);
240 if(is_mips_Immediate(op))
244 void mips_emit_immediate(const ir_node *node)
246 const mips_immediate_attr_t *attr = get_mips_immediate_attr_const(node);
248 switch(attr->imm_type) {
250 be_emit_irprintf("%d", attr->val);
252 case MIPS_IMM_SYMCONST_LO:
253 be_emit_cstring("%lo($");
254 be_emit_ident(get_entity_ld_ident(attr->entity));
256 be_emit_irprintf("%+d", attr->val);
260 case MIPS_IMM_SYMCONST_HI:
261 be_emit_cstring("%hi($");
262 be_emit_ident(get_entity_ld_ident(attr->entity));
264 be_emit_irprintf("%+d", attr->val);
269 panic("invalid immediate type found");
274 * Emit the name of the destination register at given output position.
276 void mips_emit_source_register_or_immediate(const ir_node *node, int pos)
278 const ir_node *op = get_irn_n(node, pos);
279 if(is_mips_Immediate(op)) {
280 mips_emit_immediate(op);
282 mips_emit_source_register(node, pos);
288 * Add a number to a prefix. This number will not be used a second time.
290 static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
292 static unsigned long id = 0;
293 snprintf(buf, buflen, "%s%lu", prefix, ++id);
298 /************************************************************************/
300 /************************************************************************/
303 void mips_emit_IncSP(const ir_node *node)
305 int offset = be_get_IncSP_offset(node);
311 if(offset > 0xffff || offset < -0xffff) {
312 panic("stackframe > 2^16 bytes not supported yet");
316 be_emit_irprintf("\tsubu $sp, $sp, %d", offset);
318 be_emit_irprintf("\taddu $sp, $sp, %d", -offset);
320 be_emit_finish_line_gas(node);
323 static void mips_emit_Copy(const ir_node *node)
325 be_emit_cstring("\tmove ");
326 mips_emit_dest_register(node, 0);
327 be_emit_cstring(", ");
328 mips_emit_source_register(node, 0);
329 be_emit_finish_line_gas(node);
332 static void mips_emit_Return(const ir_node* node)
334 be_emit_cstring("\tj $ra");
335 be_emit_finish_line_gas(node);
338 static __attribute__((unused))
339 void mips_emit_nops(int n)
343 for(i = 0; i < n; ++i) {
344 be_emit_cstring("\tnop\n");
345 be_emit_write_line();
349 static void mips_emit_Perm(const ir_node *node)
351 assert(get_irn_arity(node) == 2);
353 be_emit_cstring("\txor ");
354 mips_emit_source_register(node, 0);
355 be_emit_cstring(", ");
356 mips_emit_source_register(node, 0);
357 be_emit_cstring(", ");
358 mips_emit_source_register(node, 1);
359 be_emit_finish_line_gas(node);
361 /* mips_emit_nops(3); */
363 be_emit_cstring("\txor ");
364 mips_emit_source_register(node, 1);
365 be_emit_cstring(", ");
366 mips_emit_source_register(node, 1);
367 be_emit_cstring(", ");
368 mips_emit_source_register(node, 0);
369 be_emit_finish_line_gas(node);
371 /* mips_emit_nops(3); */
373 be_emit_cstring("\txor ");
374 mips_emit_source_register(node, 0);
375 be_emit_cstring(", ");
376 mips_emit_source_register(node, 0);
377 be_emit_cstring(", ");
378 mips_emit_source_register(node, 1);
379 be_emit_finish_line_gas(node);
381 /* mips_emit_nops(3); */
384 /************************************************************************/
386 /************************************************************************/
388 static void mips_emit_Call(const ir_node *node)
392 be_emit_cstring("\tjal ");
394 /* call of immediate value (label) */
395 callee = be_Call_get_entity(node);
397 be_emit_ident(get_entity_ld_ident(callee));
399 mips_emit_source_register(node, be_pos_Call_ptr);
401 be_emit_finish_line_gas(node);
404 /************************************************************************
406 * | |_ _ _ __ ___ _ __ ___
407 * _ | | | | | '_ ` _ \| '_ \/ __|
408 * | |_| | |_| | | | | | | |_) \__ \
409 * \___/ \__,_|_| |_| |_| .__/|___/
411 ************************************************************************/
413 const char* mips_get_block_label(const ir_node* block)
416 snprintf(buf, sizeof(buf), "BLOCK_%ld", get_irn_node_nr(block));
422 * Emits a block label from the given block.
424 static void mips_emit_block_label(const ir_node *block)
426 if (has_Block_label(block)) {
427 be_emit_string(be_gas_block_label_prefix());
428 be_emit_irprintf("%lu", get_Block_label(block));
430 be_emit_cstring(BLOCK_PREFIX);
431 be_emit_irprintf("%ld", get_irn_node_nr(block));
436 static void mips_emit_Jump(const ir_node *node)
438 const ir_node *block = get_irn_link(node);
439 assert(is_Block(block));
441 be_emit_cstring("\tb ");
442 mips_emit_block_label(block);
443 be_emit_finish_line_gas(node);
446 ir_node *mips_get_jump_block(const ir_node* node, long projn)
448 const ir_edge_t *oute;
449 for(oute = get_irn_out_edge_first(node); oute != NULL;
450 oute = get_irn_out_edge_next(node, oute)) {
451 ir_node *proj = get_edge_src_irn(oute);
453 assert(is_Proj(proj));
455 n = get_Proj_proj(proj);
457 return get_irn_link(proj);
463 void mips_emit_jump_target_proj(const ir_node *node, long projn)
465 ir_node *jumpblock = mips_get_jump_block(node, projn);
466 assert(jumpblock != NULL);
468 mips_emit_block_label(jumpblock);
471 void mips_emit_jump_target(const ir_node *node)
473 ir_node *jumpblock = get_irn_link(node);
474 assert(jumpblock != NULL);
476 mips_emit_block_label(jumpblock);
479 void mips_emit_jump_or_fallthrough(const ir_node *node, long pn)
481 ir_node *jumpblock = mips_get_jump_block(node, pn);
482 assert(jumpblock != NULL);
484 /* TODO: use fallthrough when possible */
485 be_emit_cstring("b ");
486 mips_emit_block_label(jumpblock);
489 /************************************************************************
491 * / ___|_ _(_) |_ ___| |__ | |_ _ _ __ ___ _ __ *
492 * \___ \ \ /\ / / | __/ __| '_ \ _ | | | | | '_ ` _ \| '_ \ *
493 * ___) \ V V /| | || (__| | | | |_| | |_| | | | | | | |_) | *
494 * |____/ \_/\_/ |_|\__\___|_| |_|\___/ \__,_|_| |_| |_| .__/ *
497 ************************************************************************/
500 /* jump table entry (target and corresponding number) */
501 typedef struct _branch_t {
506 /* jump table for switch generation */
507 typedef struct _jmp_tbl_t {
508 ir_node *defBlock; /**< default target */
509 int min_value; /**< smallest switch case */
510 int max_value; /**< largest switch case */
511 int num_branches; /**< number of jumps */
512 char *label; /**< label of the jump table */
513 branch_t *branches; /**< jump array */
517 * Compare two variables of type branch_t. Used to sort all switch cases
519 static int mips_cmp_branch_t(const void *a, const void *b)
521 branch_t *b1 = (branch_t *)a;
522 branch_t *b2 = (branch_t *)b;
524 if (b1->value <= b2->value)
530 const char* mips_get_jumptbl_label(const ir_node* switchjmp)
533 snprintf(buf, sizeof(buf), "__JUMPTBL%ld", get_irn_node_nr(switchjmp));
539 * Emits code for a SwitchJmp (creates a jump table if
540 * possible otherwise a cmp-jmp cascade). Stolen from ia32
542 void emit_mips_jump_table(const ir_node *irn)
544 int lastval, i, i2, pn;
547 const ir_edge_t *edge;
548 const mips_attr_t *attr = get_mips_attr_const(irn);
550 /* fill the table structure */
551 tbl.label = XMALLOCN(char, SNPRINTF_BUF_LEN);
552 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
554 tbl.num_branches = get_irn_n_edges(irn);
555 tbl.branches = XMALLOCNZ(branch_t, tbl.num_branches);
556 tbl.min_value = INT_MAX;
557 tbl.max_value = INT_MIN;
560 /* go over all proj's and collect them */
561 foreach_out_edge(irn, edge) {
562 proj = get_edge_src_irn(edge);
563 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
565 pn = get_Proj_proj(proj);
567 /* create branch entry */
568 tbl.branches[i].target = get_irn_link(proj);
569 tbl.branches[i].value = pn;
571 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
572 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
577 /* sort the branches by their number */
578 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), mips_cmp_branch_t);
580 be_emit_string(mips_get_jumptbl_label(irn));
581 be_emit_cstring(":\n");
582 be_emit_write_line();
583 lastval = tbl.min_value;
584 for(i = 0; i < tbl.num_branches; ++i) {
585 const branch_t *branch = &tbl.branches[i];
586 int value = branch->value;
588 for(i2 = lastval + 1; i2 < value; ++i2) {
589 be_emit_cstring("\t.word ");
590 be_emit_ident(get_entity_ld_ident(attr->symconst));
592 be_emit_write_line();
595 be_emit_cstring("\t.word ");
596 mips_emit_block_label(branch->target);
598 be_emit_write_line();
600 lastval = branch->value;
609 static void dump_jump_tables(ir_node* node, void *data)
614 if(is_mips_SwitchJump(node)) {
615 be_emit_cstring(".data\n");
616 be_emit_write_line();
618 emit_mips_jump_table(node);
620 be_emit_cstring(".text\n");
621 be_emit_write_line();
626 /***********************************************************************************
629 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
630 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
631 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
632 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
634 ***********************************************************************************/
636 static void mips_emit_nothing(const ir_node *node)
641 static void mips_emit_this_shouldnt_happen(const ir_node *node)
643 panic("Found non-lowered node %+F while emitting", node);
647 * The type of a emitter function.
649 typedef void (*emit_func) (const ir_node *);
652 * Set a node emitter. Make it a bit more type safe.
654 static void register_emitter(ir_op *op, emit_func func)
656 op->ops.generic = (op_func) func;
660 * Register emitter functions for mips backend
662 void mips_register_emitters(void)
664 /* first clear the generic function pointer for all ops */
665 clear_irp_opcodes_generic_func();
667 /* register all emitter functions defined in spec */
668 mips_register_spec_emitters();
671 register_emitter(op_be_IncSP, mips_emit_IncSP);
672 register_emitter(op_be_AddSP, mips_emit_this_shouldnt_happen);
673 register_emitter(op_be_Call, mips_emit_Call);
674 register_emitter(op_be_Copy, mips_emit_Copy);
675 register_emitter(op_be_Keep, mips_emit_nothing);
676 register_emitter(op_be_Barrier, mips_emit_nothing);
677 register_emitter(op_be_Return, mips_emit_Return);
678 register_emitter(op_be_RegParams, mips_emit_nothing);
679 register_emitter(op_be_Spill, mips_emit_this_shouldnt_happen);
680 register_emitter(op_be_Reload, mips_emit_this_shouldnt_happen);
681 register_emitter(op_be_Perm, mips_emit_Perm);
683 register_emitter(op_Start, mips_emit_nothing);
684 register_emitter(op_Proj, mips_emit_nothing);
685 register_emitter(op_SymConst, mips_emit_this_shouldnt_happen);
686 register_emitter(op_Const, mips_emit_this_shouldnt_happen);
687 register_emitter(op_Jmp, mips_emit_Jump);
688 register_emitter(op_Cmp, mips_emit_this_shouldnt_happen);
689 register_emitter(op_Cond, mips_emit_this_shouldnt_happen);
690 register_emitter(op_Phi, mips_emit_nothing);
694 * Emits assembly for a single node
696 static void mips_emit_node(const ir_node *node)
698 ir_op *op = get_irn_op(node);
700 if (op->ops.generic) {
701 emit_func emit = (emit_func) op->ops.generic;
704 panic("No emitter defined for node %+F", node);
709 * Walks over the nodes in a block connected by scheduling edges
710 * and emits code for each node.
712 void mips_gen_block(const ir_node *block)
716 if (! is_Block(block))
719 mips_emit_block_label(block);
720 be_emit_cstring(":\n");
721 be_emit_write_line();
723 sched_foreach(block, node) {
724 mips_emit_node(node);
728 be_emit_write_line();
732 * Emits code for function start.
734 void mips_emit_func_prolog(ir_graph *irg)
736 ident *irg_ident = get_entity_ld_ident(get_irg_entity(irg));
739 //irg_walk_graph(irg, NULL, dump_jump_tables, env);
741 be_emit_write_line();
742 be_gas_emit_switch_section(GAS_SECTION_TEXT);
744 be_emit_cstring("\t.balign\t4\n");
746 be_emit_cstring("\t.global\t");
747 be_emit_ident(irg_ident);
750 be_emit_cstring("\t.set\tnomips16\n");
752 be_emit_cstring("\t.ent\t");
753 be_emit_ident(irg_ident);
756 be_emit_ident(irg_ident);
757 be_emit_cstring(":\n");
759 be_emit_cstring("\t.frame\t$fp, 24, $ra\n");
760 be_emit_cstring("\t.mask\t0xc0000000, -4\n");
761 be_emit_cstring("\t.fmask\t0x00000000, 0\n");
763 be_emit_write_line();
767 * Emits code for function end
769 void mips_emit_func_epilog(ir_graph *irg)
771 ident *irg_ident = get_entity_ident(get_irg_entity(irg));
773 be_emit_cstring("\t.end\t");
774 be_emit_ident(irg_ident);
776 be_emit_write_line();
780 * Sets labels for control flow nodes (jump target)
782 void mips_gen_labels(ir_node *block, void *env)
785 int n = get_Block_n_cfgpreds(block);
788 for (n--; n >= 0; n--) {
789 pred = get_Block_cfgpred(block, n);
790 set_irn_link(pred, block);
797 void mips_gen_routine(mips_code_gen_t *mips_cg, ir_graph *irg)
801 mips_register_emitters();
803 irg_block_walk_graph(irg, mips_gen_labels, NULL, NULL);
805 mips_emit_func_prolog(irg);
807 n = ARR_LEN(mips_cg->block_schedule);
808 for (i = 0; i < n; ++i) {
809 ir_node *block = mips_cg->block_schedule[i];
810 mips_gen_block(block);
813 mips_emit_func_epilog(irg);
816 void mips_init_emitter(void)
818 FIRM_DBG_REGISTER(dbg, "firm.be.mips.emitter");