2 * Copyright (C) 1995-2010 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 emit assembler for a backend graph
23 * @author Hannes Rapp, Matthias Braun
29 #include "bitfiddle.h"
41 #include "raw_bitset.h"
46 #include "beblocksched.h"
52 #include "bepeephole.h"
54 #include "sparc_emitter.h"
55 #include "gen_sparc_emitter.h"
56 #include "sparc_nodes_attr.h"
57 #include "sparc_new_nodes.h"
58 #include "gen_sparc_regalloc_if.h"
60 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
62 static ir_heights_t *heights;
63 static const ir_node *delay_slot_filler; /**< this node has been choosen to fill
64 the next delay slot */
66 static void sparc_emit_node(const ir_node *node);
67 static bool emitting_delay_slot;
70 * indent before instruction. (Adds additional indentation when emitting
73 static void sparc_emit_indent(void)
76 if (emitting_delay_slot)
80 static void sparc_emit_immediate(ir_node const *const node)
82 const sparc_attr_t *attr = get_sparc_attr_const(node);
83 ir_entity *entity = attr->immediate_value_entity;
86 int32_t value = attr->immediate_value;
87 assert(sparc_is_value_imm_encodeable(value));
88 be_emit_irprintf("%d", value);
90 if (get_entity_owner(entity) == get_tls_type()) {
91 be_emit_cstring("%tle_lox10(");
93 be_emit_cstring("%lo(");
95 be_gas_emit_entity(entity);
96 if (attr->immediate_value != 0) {
97 be_emit_irprintf("%+d", attr->immediate_value);
103 static void sparc_emit_high_immediate(ir_node const *node)
105 const sparc_attr_t *attr = get_sparc_attr_const(node);
106 ir_entity *entity = attr->immediate_value_entity;
108 if (entity == NULL) {
109 uint32_t value = (uint32_t) attr->immediate_value;
110 be_emit_irprintf("%%hi(0x%X)", value);
112 if (get_entity_owner(entity) == get_tls_type()) {
113 be_emit_cstring("%tle_hix22(");
115 be_emit_cstring("%hi(");
117 be_gas_emit_entity(entity);
118 if (attr->immediate_value != 0) {
119 be_emit_irprintf("%+d", attr->immediate_value);
125 static void sparc_emit_source_register(ir_node const *node, int const pos)
127 const arch_register_t *reg = arch_get_irn_register_in(node, pos);
129 be_emit_string(arch_register_get_name(reg));
132 static void sparc_emit_dest_register(ir_node const *const node, int const pos)
134 const arch_register_t *reg = arch_get_irn_register_out(node, pos);
136 be_emit_string(arch_register_get_name(reg));
142 static void sparc_emit_offset(const ir_node *node, int offset_node_pos)
144 const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
146 if (attr->is_reg_reg) {
147 assert(!attr->is_frame_entity);
148 assert(attr->base.immediate_value == 0);
149 assert(attr->base.immediate_value_entity == NULL);
151 sparc_emit_source_register(node, offset_node_pos);
152 } else if (attr->is_frame_entity) {
153 int32_t offset = attr->base.immediate_value;
155 assert(sparc_is_value_imm_encodeable(offset));
156 be_emit_irprintf("%+ld", offset);
158 } else if (attr->base.immediate_value != 0
159 || attr->base.immediate_value_entity != NULL) {
161 sparc_emit_immediate(node);
168 static void sparc_emit_load_mode(ir_node const *const node)
170 const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
171 ir_mode *mode = attr->load_store_mode;
172 int bits = get_mode_size_bits(mode);
173 bool is_signed = mode_is_signed(mode);
176 case 8: be_emit_string(is_signed ? "sb" : "ub"); break;
177 case 16: be_emit_string(is_signed ? "sh" : "uh"); break;
179 case 64: be_emit_char('d'); break;
180 case 128: be_emit_char('q'); break;
181 default: panic("invalid load/store mode %+F", mode);
186 * Emit store mode char
188 static void sparc_emit_store_mode(ir_node const *const node)
190 const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
191 ir_mode *mode = attr->load_store_mode;
192 int bits = get_mode_size_bits(mode);
195 case 8: be_emit_char('b'); break;
196 case 16: be_emit_char('h'); break;
198 case 64: be_emit_char('d'); break;
199 case 128: be_emit_char('q'); break;
200 default: panic("invalid load/store mode %+F", mode);
204 static void emit_fp_suffix(const ir_mode *mode)
206 assert(mode_is_float(mode));
207 switch (get_mode_size_bits(mode)) {
208 case 32: be_emit_char('s'); break;
209 case 64: be_emit_char('d'); break;
210 case 128: be_emit_char('q'); break;
211 default: panic("invalid FP mode");
215 static ir_node *get_jump_target(const ir_node *jump)
217 return (ir_node*)get_irn_link(jump);
221 * Returns the target label for a control flow node.
223 static void sparc_emit_cfop_target(const ir_node *node)
225 ir_node *block = get_jump_target(node);
226 be_gas_emit_block_name(block);
230 * returns true if a sparc_call calls a register and not an immediate
232 static bool is_sparc_reg_call(const ir_node *node)
234 const sparc_attr_t *attr = get_sparc_attr_const(node);
235 return attr->immediate_value_entity == NULL;
238 static int get_sparc_Call_dest_addr_pos(const ir_node *node)
240 assert(is_sparc_reg_call(node));
241 return get_irn_arity(node)-1;
244 static bool ba_is_fallthrough(const ir_node *node)
246 ir_node *block = get_nodes_block(node);
247 ir_node *next_block = (ir_node*)get_irn_link(block);
248 return get_irn_link(node) == next_block;
251 static bool is_no_instruction(const ir_node *node)
253 /* copies are nops if src_reg == dest_reg */
254 if (be_is_Copy(node) || be_is_CopyKeep(node)) {
255 const arch_register_t *src_reg = arch_get_irn_register_in(node, 0);
256 const arch_register_t *dest_reg = arch_get_irn_register_out(node, 0);
258 if (src_reg == dest_reg)
261 if (be_is_IncSP(node) && be_get_IncSP_offset(node) == 0)
263 /* Ba is not emitted if it is a simple fallthrough */
264 if (is_sparc_Ba(node) && ba_is_fallthrough(node))
267 return be_is_Keep(node) || be_is_Start(node) || is_Phi(node);
270 static bool has_delay_slot(const ir_node *node)
272 if (is_sparc_Ba(node)) {
273 return !ba_is_fallthrough(node);
276 return arch_get_irn_flags(node) & sparc_arch_irn_flag_has_delay_slot;
279 /** returns true if the emitter for this sparc node can produce more than one
280 * actual sparc instruction.
281 * Usually it is a bad sign if we have to add instructions here. We should
282 * rather try to get them lowered down. So we can actually put them into
283 * delay slots and make them more accessible to the scheduler.
285 static bool emits_multiple_instructions(const ir_node *node)
287 if (has_delay_slot(node))
290 if (is_sparc_Call(node)) {
291 return arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return;
294 return is_sparc_SMulh(node) || is_sparc_UMulh(node)
295 || is_sparc_SDiv(node) || is_sparc_UDiv(node)
296 || be_is_MemPerm(node) || be_is_Perm(node)
297 || is_sparc_SubSP(node);
300 static bool uses_reg(const ir_node *node, const arch_register_t *reg)
302 int arity = get_irn_arity(node);
305 for (i = 0; i < arity; ++i) {
306 const arch_register_t *in_reg = arch_get_irn_register_in(node, i);
313 static bool writes_reg(const ir_node *node, const arch_register_t *reg)
315 unsigned n_outs = arch_get_irn_n_outs(node);
317 for (o = 0; o < n_outs; ++o) {
318 const arch_register_t *out_reg = arch_get_irn_register_out(node, o);
325 static bool can_move_into_delayslot(const ir_node *node, const ir_node *to)
327 if (!be_can_move_before(heights, node, to))
330 if (is_sparc_Call(to)) {
332 /** all deps are used after the delay slot so, we're fine */
333 if (!is_sparc_reg_call(to))
336 check = get_irn_n(to, get_sparc_Call_dest_addr_pos(to));
337 if (skip_Proj(check) == node)
340 /* the Call also destroys the value of %o7, but since this is
341 * currently marked as ignore register in the backend, it
342 * should never be used by the instruction in the delay slot. */
343 if (uses_reg(node, &sparc_registers[REG_O7]))
346 } else if (is_sparc_Return(to)) {
347 /* return uses the value of %o7, all other values are not
348 * immediately used */
349 if (writes_reg(node, &sparc_registers[REG_O7]))
353 /* the node must not use our computed values */
354 int arity = get_irn_arity(to);
356 for (i = 0; i < arity; ++i) {
357 ir_node *in = get_irn_n(to, i);
358 if (skip_Proj(in) == node)
366 * search for an instruction that can fill the delay slot of @p node
368 static const ir_node *pick_delay_slot_for(const ir_node *node)
370 const ir_node *schedpoint = node;
372 /* currently we don't track which registers are still alive, so we can't
373 * pick any other instructions other than the one directly preceding */
374 static const unsigned PICK_DELAY_SLOT_MAX_DISTANCE = 10;
376 assert(has_delay_slot(node));
378 while (sched_has_prev(schedpoint)) {
379 schedpoint = sched_prev(schedpoint);
381 if (has_delay_slot(schedpoint))
384 /* skip things which don't really result in instructions */
385 if (is_no_instruction(schedpoint))
388 if (tries++ >= PICK_DELAY_SLOT_MAX_DISTANCE)
391 if (emits_multiple_instructions(schedpoint))
394 if (!can_move_into_delayslot(schedpoint, node))
397 /* found something */
404 void sparc_emitf(ir_node const *const node, char const *fmt, ...)
410 char const *start = fmt;
412 while (*fmt != '%' && *fmt != '\0')
414 be_emit_string_len(start, fmt - start);
431 if (*fmt < '0' || '9' <= *fmt)
433 sparc_emit_dest_register(node, *fmt++ - '0');
437 sparc_attr_t const *const attr = get_sparc_attr_const(node);
438 be_gas_emit_entity(attr->immediate_value_entity);
439 if (attr->immediate_value != 0) {
440 be_emit_irprintf(plus ? "%+d" : "%d", attr->immediate_value);
448 case 'D': mode = get_sparc_fp_conv_attr_const(node)->dest_mode; break;
449 case 'M': mode = get_sparc_fp_attr_const(node)->fp_mode; break;
450 case 'S': mode = get_sparc_fp_conv_attr_const(node)->src_mode; break;
451 default: goto unknown;
453 emit_fp_suffix(mode);
458 sparc_emit_high_immediate(node);
462 sparc_emit_cfop_target(node);
467 case 'L': sparc_emit_load_mode(node); break;
468 case 'S': sparc_emit_store_mode(node); break;
469 default: goto unknown;
474 if (*fmt < '0' || '9' <= *fmt)
476 sparc_emit_offset(node, *fmt++ - '0');
480 arch_register_t const *const reg = va_arg(ap, const arch_register_t*);
482 be_emit_string(arch_register_get_name(reg));
492 if (*fmt < '0' || '9' <= *fmt)
494 unsigned const pos = *fmt++ - '0';
495 if (imm && arch_get_irn_flags(node) & (arch_irn_flags_t)sparc_arch_irn_flag_immediate_form) {
496 sparc_emit_immediate(node);
498 sparc_emit_source_register(node, pos);
504 int const num = va_arg(ap, int);
505 be_emit_irprintf(plus ? "%+d" : "%d", num);
510 char const *const str = va_arg(ap, char const*);
516 unsigned const num = va_arg(ap, unsigned);
517 be_emit_irprintf(plus ? "%+u" : "%u", num);
523 panic("unknown format conversion in sparc_emitf()");
526 be_emit_finish_line_gas(node);
531 * Emits code for stack space management
533 static void emit_be_IncSP(const ir_node *irn)
535 int offset = be_get_IncSP_offset(irn);
540 /* SPARC stack grows downwards */
541 char const *const insn = offset > 0 ? offset = -offset, "add" : "sub";
542 sparc_emitf(irn, "%s %S0, %d, %D0", insn, offset);
546 * Emits code for stack space management.
548 static void emit_sparc_SubSP(const ir_node *irn)
550 sparc_emitf(irn, "sub %S0, %SI1, %D0");
551 sparc_emitf(irn, "add %S0, %u, %D1", SPARC_MIN_STACKSIZE);
554 static void fill_delay_slot(void)
556 emitting_delay_slot = true;
557 if (delay_slot_filler != NULL) {
558 sparc_emit_node(delay_slot_filler);
559 delay_slot_filler = NULL;
561 sparc_emitf(NULL, "nop");
563 emitting_delay_slot = false;
566 static void emit_sparc_Div(const ir_node *node, char const *const insn)
568 /* can we get the delay count of the wr instruction somewhere? */
569 unsigned wry_delay_count = 3;
572 sparc_emitf(node, "wr %S0, 0, %%y");
574 for (i = 0; i < wry_delay_count; ++i) {
578 sparc_emitf(node, "%s %S1, %SI2, %D0", insn);
581 static void emit_sparc_SDiv(const ir_node *node)
583 emit_sparc_Div(node, "sdiv");
586 static void emit_sparc_UDiv(const ir_node *node)
588 emit_sparc_Div(node, "udiv");
591 static void emit_sparc_Call(const ir_node *node)
593 if (is_sparc_reg_call(node)) {
594 int dest_addr = get_sparc_Call_dest_addr_pos(node);
595 sparc_emitf(node, "call %R", arch_get_irn_register_in(node, dest_addr));
597 sparc_emitf(node, "call %E, 0");
602 if (arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return) {
603 sparc_emitf(NULL, "unimp 8");
607 static void emit_be_Perm(const ir_node *irn)
609 sparc_emitf(irn, "xor %S1, %S0, %S0");
610 sparc_emitf(irn, "xor %S1, %S0, %S1");
611 sparc_emitf(irn, "xor %S1, %S0, %S0");
614 /* The stack pointer must always be SPARC_STACK_ALIGNMENT bytes aligned, so get
615 * the next bigger integer that's evenly divisible by it. */
616 static unsigned get_aligned_sp_change(const unsigned num_regs)
618 const unsigned bytes = num_regs * SPARC_REGISTER_SIZE;
619 return round_up2(bytes, SPARC_STACK_ALIGNMENT);
622 /* Spill register l0 or both l0 and l1, depending on n_spilled and n_to_spill.*/
623 static void memperm_emit_spill_registers(const ir_node *node, int n_spilled,
626 assert(n_spilled < n_to_spill);
628 if (n_spilled == 0) {
629 /* We always reserve stack space for two registers because during copy
630 * processing we don't know yet if we also need to handle a cycle which
631 * needs two registers. More complicated code in emit_MemPerm would
632 * prevent wasting SPARC_REGISTER_SIZE bytes of stack space but
633 * it is not worth the worse readability of emit_MemPerm. */
635 /* Keep stack pointer aligned. */
636 unsigned sp_change = get_aligned_sp_change(2);
637 sparc_emitf(node, "sub %%sp, %u, %%sp", sp_change);
639 /* Spill register l0. */
640 sparc_emitf(node, "st %%l0, [%%sp%+d]", SPARC_MIN_STACKSIZE);
643 if (n_to_spill == 2) {
644 /* Spill register l1. */
645 sparc_emitf(node, "st %%l1, [%%sp%+d]", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
649 /* Restore register l0 or both l0 and l1, depending on n_spilled. */
650 static void memperm_emit_restore_registers(const ir_node *node, int n_spilled)
654 if (n_spilled == 2) {
655 /* Restore register l1. */
656 sparc_emitf(node, "ld [%%sp%+d], %%l1", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
659 /* Restore register l0. */
660 sparc_emitf(node, "ld [%%sp%+d], %%l0", SPARC_MIN_STACKSIZE);
662 /* Restore stack pointer. */
663 sp_change = get_aligned_sp_change(2);
664 sparc_emitf(node, "add %%sp, %u, %%sp", sp_change);
667 /* Emit code to copy in_ent to out_ent. Only uses l0. */
668 static void memperm_emit_copy(const ir_node *node, ir_entity *in_ent,
671 ir_graph *irg = get_irn_irg(node);
672 be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
673 int off_in = be_get_stack_entity_offset(layout, in_ent, 0);
674 int off_out = be_get_stack_entity_offset(layout, out_ent, 0);
676 /* Load from input entity. */
677 sparc_emitf(node, "ld [%%fp%+d], %%l0", off_in);
678 /* Store to output entity. */
679 sparc_emitf(node, "st %%l0, [%%fp%+d]", off_out);
682 /* Emit code to swap ent1 and ent2. Uses l0 and l1. */
683 static void memperm_emit_swap(const ir_node *node, ir_entity *ent1,
686 ir_graph *irg = get_irn_irg(node);
687 be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
688 int off1 = be_get_stack_entity_offset(layout, ent1, 0);
689 int off2 = be_get_stack_entity_offset(layout, ent2, 0);
691 /* Load from first input entity. */
692 sparc_emitf(node, "ld [%%fp%+d], %%l0", off1);
693 /* Load from second input entity. */
694 sparc_emitf(node, "ld [%%fp%+d], %%l1", off2);
695 /* Store first value to second output entity. */
696 sparc_emitf(node, "st %%l0, [%%fp%+d]", off2);
697 /* Store second value to first output entity. */
698 sparc_emitf(node, "st %%l1, [%%fp%+d]", off1);
701 /* Find the index of ent in ents or return -1 if not found. */
702 static int get_index(ir_entity **ents, int n, ir_entity *ent)
706 for (i = 0; i < n; ++i)
714 * Emit code for a MemPerm node.
716 * Analyze MemPerm for copy chains and cyclic swaps and resolve them using
718 * This function is conceptually very similar to permute_values in
721 static void emit_be_MemPerm(const ir_node *node)
723 int memperm_arity = be_get_MemPerm_entity_arity(node);
724 /* Upper limit for the number of participating entities is twice the
725 * arity, e.g., for a simple copying MemPerm node with one input/output. */
726 int max_size = 2 * memperm_arity;
727 ir_entity **entities = ALLOCANZ(ir_entity *, max_size);
728 /* sourceof contains the input entity for each entity. If an entity is
729 * never used as an output, its entry in sourceof is a fix point. */
730 int *sourceof = ALLOCANZ(int, max_size);
731 /* n_users counts how many output entities use this entity as their input.*/
732 int *n_users = ALLOCANZ(int, max_size);
733 /* n_spilled records the number of spilled registers, either 1 or 2. */
737 /* This implementation currently only works with frame pointers. */
738 ir_graph *irg = get_irn_irg(node);
739 be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
740 assert(!layout->sp_relative && "MemPerms currently do not work without frame pointers");
742 for (i = 0; i < max_size; ++i) {
746 for (i = n = 0; i < memperm_arity; ++i) {
747 ir_entity *out = be_get_MemPerm_out_entity(node, i);
748 ir_entity *in = be_get_MemPerm_in_entity(node, i);
749 int oidx; /* Out index */
750 int iidx; /* In index */
752 /* Insert into entities to be able to operate on unique indices. */
753 if (get_index(entities, n, out) == -1)
755 if (get_index(entities, n, in) == -1)
758 oidx = get_index(entities, n, out);
759 iidx = get_index(entities, n, in);
761 sourceof[oidx] = iidx; /* Remember the source. */
762 ++n_users[iidx]; /* Increment number of users of this entity. */
765 /* First do all the copies. */
766 for (oidx = 0; oidx < n; /* empty */) {
767 int iidx = sourceof[oidx];
769 /* Nothing to do for fix points.
770 * Also, if entities[oidx] is used as an input by another copy, we
771 * can't overwrite entities[oidx] yet.*/
772 if (iidx == oidx || n_users[oidx] > 0) {
777 /* We found the end of a 'chain', so do the copy. */
778 if (n_spilled == 0) {
779 memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/1);
782 memperm_emit_copy(node, entities[iidx], entities[oidx]);
785 sourceof[oidx] = oidx;
787 assert(n_users[iidx] > 0);
788 /* Decrementing the number of users might enable us to do another
792 if (iidx < oidx && n_users[iidx] == 0) {
799 /* The rest are cycles. */
800 for (oidx = 0; oidx < n; /* empty */) {
801 int iidx = sourceof[oidx];
804 /* Nothing to do for fix points. */
810 assert(n_users[iidx] == 1);
812 /* Swap the two values to resolve the cycle. */
814 memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/2);
817 memperm_emit_swap(node, entities[iidx], entities[oidx]);
819 tidx = sourceof[iidx];
821 sourceof[iidx] = iidx;
823 /* The source of oidx is now the old source of iidx, because we swapped
824 * the two entities. */
825 sourceof[oidx] = tidx;
829 /* Only fix points should remain. */
830 for (i = 0; i < max_size; ++i) {
831 assert(sourceof[i] == i);
835 assert(n_spilled > 0 && "Useless MemPerm node");
837 memperm_emit_restore_registers(node, n_spilled);
840 static void emit_sparc_Return(const ir_node *node)
842 ir_graph *irg = get_irn_irg(node);
843 ir_entity *entity = get_irg_entity(irg);
844 ir_type *type = get_entity_type(entity);
846 const char *destreg = "%o7";
848 /* hack: we don't explicitely model register changes because of the
849 * restore node. So we have to do it manually here */
850 if (delay_slot_filler != NULL &&
851 (is_sparc_Restore(delay_slot_filler)
852 || is_sparc_RestoreZero(delay_slot_filler))) {
855 char const *const offset = get_method_calling_convention(type) & cc_compound_ret ? "12" : "8";
856 sparc_emitf(node, "jmp %s+%s", destreg, offset);
860 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
862 unsigned idx = reg->global_index;
863 if (idx < REG_I0 || idx > REG_I7)
865 idx += REG_O0 - REG_I0;
866 assert(REG_O0 <= idx && idx <= REG_O7);
867 return &sparc_registers[idx];
870 static void emit_sparc_Restore(const ir_node *node)
872 const arch_register_t *destreg
873 = arch_get_irn_register_out(node, pn_sparc_Restore_res);
874 sparc_emitf(node, "restore %S2, %SI3, %R", map_i_to_o_reg(destreg));
877 static void emit_sparc_FrameAddr(const ir_node *node)
879 const sparc_attr_t *attr = get_sparc_attr_const(node);
880 int32_t offset = attr->immediate_value;
882 char const *const insn = offset > 0 ? offset = -offset, "sub" : "add";
883 assert(sparc_is_value_imm_encodeable(offset));
884 sparc_emitf(node, "%s %S0, %d, %D0", insn, offset);
887 static const char *get_icc_unsigned(ir_relation relation)
889 switch (relation & (ir_relation_less_equal_greater)) {
890 case ir_relation_false: return "bn";
891 case ir_relation_equal: return "be";
892 case ir_relation_less: return "blu";
893 case ir_relation_less_equal: return "bleu";
894 case ir_relation_greater: return "bgu";
895 case ir_relation_greater_equal: return "bgeu";
896 case ir_relation_less_greater: return "bne";
897 case ir_relation_less_equal_greater: return "ba";
898 default: panic("Cmp has unsupported relation");
902 static const char *get_icc_signed(ir_relation relation)
904 switch (relation & (ir_relation_less_equal_greater)) {
905 case ir_relation_false: return "bn";
906 case ir_relation_equal: return "be";
907 case ir_relation_less: return "bl";
908 case ir_relation_less_equal: return "ble";
909 case ir_relation_greater: return "bg";
910 case ir_relation_greater_equal: return "bge";
911 case ir_relation_less_greater: return "bne";
912 case ir_relation_less_equal_greater: return "ba";
913 default: panic("Cmp has unsupported relation");
917 static const char *get_fcc(ir_relation relation)
920 case ir_relation_false: return "fbn";
921 case ir_relation_equal: return "fbe";
922 case ir_relation_less: return "fbl";
923 case ir_relation_less_equal: return "fble";
924 case ir_relation_greater: return "fbg";
925 case ir_relation_greater_equal: return "fbge";
926 case ir_relation_less_greater: return "fblg";
927 case ir_relation_less_equal_greater: return "fbo";
928 case ir_relation_unordered: return "fbu";
929 case ir_relation_unordered_equal: return "fbue";
930 case ir_relation_unordered_less: return "fbul";
931 case ir_relation_unordered_less_equal: return "fbule";
932 case ir_relation_unordered_greater: return "fbug";
933 case ir_relation_unordered_greater_equal: return "fbuge";
934 case ir_relation_unordered_less_greater: return "fbne";
935 case ir_relation_true: return "fba";
937 panic("invalid relation");
940 typedef const char* (*get_cc_func)(ir_relation relation);
942 static void emit_sparc_branch(const ir_node *node, get_cc_func get_cc)
944 const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
945 ir_relation relation = attr->relation;
946 const ir_node *proj_true = NULL;
947 const ir_node *proj_false = NULL;
948 const ir_node *block;
949 const ir_node *next_block;
951 foreach_out_edge(node, edge) {
952 ir_node *proj = get_edge_src_irn(edge);
953 long nr = get_Proj_proj(proj);
954 if (nr == pn_Cond_true) {
961 /* for now, the code works for scheduled and non-schedules blocks */
962 block = get_nodes_block(node);
964 /* we have a block schedule */
965 next_block = (ir_node*)get_irn_link(block);
967 if (get_irn_link(proj_true) == next_block) {
968 /* exchange both proj's so the second one can be omitted */
969 const ir_node *t = proj_true;
971 proj_true = proj_false;
973 relation = get_negated_relation(relation);
976 /* emit the true proj */
977 sparc_emitf(proj_true, "%s %L", get_cc(relation));
980 if (get_irn_link(proj_false) == next_block) {
981 if (be_options.verbose_asm) {
982 sparc_emitf(proj_false, "/* fallthrough to %L */");
985 sparc_emitf(proj_false, "ba %L");
990 static void emit_sparc_Bicc(const ir_node *node)
992 const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
993 bool is_unsigned = attr->is_unsigned;
994 emit_sparc_branch(node, is_unsigned ? get_icc_unsigned : get_icc_signed);
997 static void emit_sparc_fbfcc(const ir_node *node)
999 /* if the flags producing node was immediately in front of us, emit
1001 ir_node *flags = get_irn_n(node, n_sparc_fbfcc_flags);
1002 ir_node *prev = sched_prev(node);
1003 if (is_Block(prev)) {
1004 /* TODO: when the flags come from another block, then we have to do
1005 * more complicated tests to see wether the flag producing node is
1006 * potentially in front of us (could happen for fallthroughs) */
1007 panic("TODO: fbfcc flags come from other block");
1009 if (skip_Proj(flags) == prev) {
1010 sparc_emitf(NULL, "nop");
1012 emit_sparc_branch(node, get_fcc);
1015 static void emit_sparc_Ba(const ir_node *node)
1017 if (ba_is_fallthrough(node)) {
1018 if (be_options.verbose_asm) {
1019 sparc_emitf(node, "/* fallthrough to %L */");
1022 sparc_emitf(node, "ba %L");
1027 static void emit_sparc_SwitchJmp(const ir_node *node)
1029 const sparc_switch_jmp_attr_t *attr = get_sparc_switch_jmp_attr_const(node);
1031 sparc_emitf(node, "jmp %S0");
1034 be_emit_jump_table(node, attr->table, attr->table_entity, get_jump_target);
1037 static void emit_fmov(const ir_node *node, const arch_register_t *src_reg,
1038 const arch_register_t *dst_reg)
1040 sparc_emitf(node, "fmovs %R, %R", src_reg, dst_reg);
1043 static const arch_register_t *get_next_fp_reg(const arch_register_t *reg)
1045 unsigned idx = reg->global_index;
1046 assert(reg == &sparc_registers[idx]);
1048 assert(idx - REG_F0 < N_sparc_fp_REGS);
1049 return &sparc_registers[idx];
1052 static void emit_be_Copy(const ir_node *node)
1054 ir_mode *mode = get_irn_mode(node);
1055 const arch_register_t *src_reg = arch_get_irn_register_in(node, 0);
1056 const arch_register_t *dst_reg = arch_get_irn_register_out(node, 0);
1058 if (src_reg == dst_reg)
1061 if (mode_is_float(mode)) {
1062 unsigned bits = get_mode_size_bits(mode);
1063 int n = bits > 32 ? bits > 64 ? 3 : 1 : 0;
1065 emit_fmov(node, src_reg, dst_reg);
1066 for (i = 0; i < n; ++i) {
1067 src_reg = get_next_fp_reg(src_reg);
1068 dst_reg = get_next_fp_reg(dst_reg);
1069 emit_fmov(node, src_reg, dst_reg);
1071 } else if (mode_is_data(mode)) {
1072 sparc_emitf(node, "mov %S0, %D0");
1074 panic("invalid mode");
1078 static void emit_nothing(const ir_node *irn)
1083 typedef void (*emit_func) (const ir_node *);
1085 static inline void set_emitter(ir_op *op, emit_func sparc_emit_node)
1087 op->ops.generic = (op_func)sparc_emit_node;
1091 * Enters the emitter functions for handled nodes into the generic
1092 * pointer of an opcode.
1094 static void sparc_register_emitters(void)
1096 /* first clear the generic function pointer for all ops */
1097 ir_clear_opcodes_generic_func();
1098 /* register all emitter functions defined in spec */
1099 sparc_register_spec_emitters();
1101 /* custom emitter */
1102 set_emitter(op_be_Copy, emit_be_Copy);
1103 set_emitter(op_be_CopyKeep, emit_be_Copy);
1104 set_emitter(op_be_IncSP, emit_be_IncSP);
1105 set_emitter(op_be_MemPerm, emit_be_MemPerm);
1106 set_emitter(op_be_Perm, emit_be_Perm);
1107 set_emitter(op_sparc_Ba, emit_sparc_Ba);
1108 set_emitter(op_sparc_Bicc, emit_sparc_Bicc);
1109 set_emitter(op_sparc_Call, emit_sparc_Call);
1110 set_emitter(op_sparc_fbfcc, emit_sparc_fbfcc);
1111 set_emitter(op_sparc_FrameAddr, emit_sparc_FrameAddr);
1112 set_emitter(op_sparc_SubSP, emit_sparc_SubSP);
1113 set_emitter(op_sparc_Restore, emit_sparc_Restore);
1114 set_emitter(op_sparc_Return, emit_sparc_Return);
1115 set_emitter(op_sparc_SDiv, emit_sparc_SDiv);
1116 set_emitter(op_sparc_SwitchJmp, emit_sparc_SwitchJmp);
1117 set_emitter(op_sparc_UDiv, emit_sparc_UDiv);
1119 /* no need to emit anything for the following nodes */
1120 set_emitter(op_be_Keep, emit_nothing);
1121 set_emitter(op_sparc_Start, emit_nothing);
1122 set_emitter(op_Phi, emit_nothing);
1126 * Emits code for a node.
1128 static void sparc_emit_node(const ir_node *node)
1130 ir_op *op = get_irn_op(node);
1132 if (op->ops.generic) {
1133 emit_func func = (emit_func) op->ops.generic;
1134 be_dwarf_location(get_irn_dbg_info(node));
1137 panic("No emit handler for node %+F (graph %+F)\n", node,
1142 static ir_node *find_next_delay_slot(ir_node *from)
1144 ir_node *schedpoint = from;
1145 while (!has_delay_slot(schedpoint)) {
1146 if (!sched_has_next(schedpoint))
1148 schedpoint = sched_next(schedpoint);
1153 static bool block_needs_label(const ir_node *block, const ir_node *sched_prev)
1157 if (get_Block_entity(block) != NULL)
1160 n_cfgpreds = get_Block_n_cfgpreds(block);
1161 if (n_cfgpreds == 0) {
1163 } else if (n_cfgpreds > 1) {
1166 ir_node *cfgpred = get_Block_cfgpred(block, 0);
1167 ir_node *cfgpred_block = get_nodes_block(cfgpred);
1168 if (is_Proj(cfgpred) && is_sparc_SwitchJmp(get_Proj_pred(cfgpred)))
1170 return sched_prev != cfgpred_block || get_irn_link(cfgpred) != block;
1175 * Walks over the nodes in a block connected by scheduling edges
1176 * and emits code for each node.
1178 static void sparc_emit_block(ir_node *block, ir_node *prev)
1180 ir_node *next_delay_slot;
1181 bool needs_label = block_needs_label(block, prev);
1183 be_gas_begin_block(block, needs_label);
1185 next_delay_slot = find_next_delay_slot(sched_first(block));
1186 if (next_delay_slot != NULL)
1187 delay_slot_filler = pick_delay_slot_for(next_delay_slot);
1189 sched_foreach(block, node) {
1190 if (node == delay_slot_filler) {
1194 sparc_emit_node(node);
1196 if (node == next_delay_slot) {
1197 assert(delay_slot_filler == NULL);
1198 next_delay_slot = find_next_delay_slot(sched_next(node));
1199 if (next_delay_slot != NULL)
1200 delay_slot_filler = pick_delay_slot_for(next_delay_slot);
1206 * Emits code for function start.
1208 static void sparc_emit_func_prolog(ir_graph *irg)
1210 ir_entity *entity = get_irg_entity(irg);
1211 be_gas_emit_function_prolog(entity, 4, NULL);
1215 * Emits code for function end
1217 static void sparc_emit_func_epilog(ir_graph *irg)
1219 ir_entity *entity = get_irg_entity(irg);
1220 be_gas_emit_function_epilog(entity);
1223 static void sparc_gen_labels(ir_node *block, void *env)
1226 int n = get_Block_n_cfgpreds(block);
1229 for (n--; n >= 0; n--) {
1230 pred = get_Block_cfgpred(block, n);
1231 set_irn_link(pred, block); // link the pred of a block (which is a jmp)
1235 void sparc_emit_routine(ir_graph *irg)
1237 ir_node **block_schedule;
1241 heights = heights_new(irg);
1243 /* register all emitter functions */
1244 sparc_register_emitters();
1246 /* create the block schedule. For now, we don't need it earlier. */
1247 block_schedule = be_create_block_schedule(irg);
1249 sparc_emit_func_prolog(irg);
1250 irg_block_walk_graph(irg, sparc_gen_labels, NULL, NULL);
1252 /* inject block scheduling links & emit code of each block */
1253 n = ARR_LEN(block_schedule);
1254 for (i = 0; i < n; ++i) {
1255 ir_node *block = block_schedule[i];
1256 ir_node *next_block = i+1 < n ? block_schedule[i+1] : NULL;
1257 set_irn_link(block, next_block);
1260 for (i = 0; i < n; ++i) {
1261 ir_node *block = block_schedule[i];
1262 ir_node *prev = i>=1 ? block_schedule[i-1] : NULL;
1263 if (block == get_irg_end_block(irg))
1265 sparc_emit_block(block, prev);
1268 /* emit function epilog */
1269 sparc_emit_func_epilog(irg);
1271 heights_free(heights);
1274 void sparc_init_emitter(void)
1276 FIRM_DBG_REGISTER(dbg, "firm.be.sparc.emit");