#include <limits.h>
+#include "bitfiddle.h"
#include "xmalloc.h"
#include "tv.h"
#include "iredges.h"
#include "dbginfo.h"
#include "heights.h"
-#include "../besched.h"
-#include "../beblocksched.h"
-#include "../beirg.h"
-#include "../begnuas.h"
-#include "../be_dbgout.h"
-#include "../benode.h"
-#include "../bestack.h"
+#include "besched.h"
+#include "beblocksched.h"
+#include "beirg.h"
+#include "begnuas.h"
+#include "be_dbgout.h"
+#include "benode.h"
+#include "bestack.h"
#include "sparc_emitter.h"
#include "gen_sparc_emitter.h"
static void sparc_emit_node(const ir_node *node);
-/**
- * Returns the register at in position pos.
- */
-static const arch_register_t *get_in_reg(const ir_node *node, int pos)
-{
- ir_node *op = get_irn_n(node, pos);
- return arch_get_irn_register(op);
-}
-
void sparc_emit_immediate(const ir_node *node)
{
const sparc_attr_t *attr = get_sparc_attr_const(node);
assert(sparc_is_value_imm_encodeable(value));
be_emit_irprintf("%d", value);
} else {
- be_emit_cstring("%lo(");
+ if (get_entity_owner(entity) == get_tls_type()) {
+ be_emit_cstring("%tle_lox10(");
+ } else {
+ be_emit_cstring("%lo(");
+ }
be_gas_emit_entity(entity);
if (attr->immediate_value != 0) {
be_emit_irprintf("%+d", attr->immediate_value);
const sparc_attr_t *attr = get_sparc_attr_const(node);
ir_entity *entity = attr->immediate_value_entity;
- be_emit_cstring("%hi(");
if (entity == NULL) {
uint32_t value = (uint32_t) attr->immediate_value;
- be_emit_irprintf("0x%X", value);
+ be_emit_irprintf("%%hi(0x%X)", value);
} else {
+ if (get_entity_owner(entity) == get_tls_type()) {
+ be_emit_cstring("%tle_hix22(");
+ } else {
+ be_emit_cstring("%hi(");
+ }
be_gas_emit_entity(entity);
if (attr->immediate_value != 0) {
be_emit_irprintf("%+d", attr->immediate_value);
}
+ be_emit_char(')');
}
- be_emit_char(')');
}
void sparc_emit_source_register(const ir_node *node, int pos)
{
- const arch_register_t *reg = get_in_reg(node, pos);
+ const arch_register_t *reg = arch_get_irn_register_in(node, pos);
be_emit_char('%');
be_emit_string(arch_register_get_name(reg));
}
void sparc_emit_dest_register(const ir_node *node, int pos)
{
- const arch_register_t *reg = arch_irn_get_register(node, pos);
+ const arch_register_t *reg = arch_get_irn_register_out(node, pos);
be_emit_char('%');
be_emit_string(arch_register_get_name(reg));
}
*/
void sparc_emit_reg_or_imm(const ir_node *node, int pos)
{
- if (arch_irn_get_flags(node) & ((arch_irn_flags_t)sparc_arch_irn_flag_immediate_form)) {
+ if (arch_get_irn_flags(node) & ((arch_irn_flags_t)sparc_arch_irn_flag_immediate_form)) {
// we have a imm input
sparc_emit_immediate(node);
} else {
}
}
-/**
- * emit integer signed/unsigned prefix char
- */
-void sparc_emit_mode_sign_prefix(const ir_node *node)
-{
- ir_mode *mode = get_irn_mode(node);
- bool is_signed = mode_is_signed(mode);
- be_emit_string(is_signed ? "s" : "u");
-}
-
static void emit_fp_suffix(const ir_mode *mode)
{
unsigned bits = get_mode_size_bits(mode);
{
/* copies are nops if src_reg == dest_reg */
if (be_is_Copy(node) || be_is_CopyKeep(node)) {
- const arch_register_t *src_reg = get_in_reg(node, 0);
- const arch_register_t *dest_reg = arch_irn_get_register(node, 0);
+ const arch_register_t *src_reg = arch_get_irn_register_in(node, 0);
+ const arch_register_t *dest_reg = arch_get_irn_register_out(node, 0);
if (src_reg == dest_reg)
return true;
static bool has_delay_slot(const ir_node *node)
{
- if (is_sparc_Ba(node) && ba_is_fallthrough(node))
- return false;
+ if (is_sparc_Ba(node)) {
+ return !ba_is_fallthrough(node);
+ }
- return is_sparc_Bicc(node) || is_sparc_fbfcc(node) || is_sparc_Ba(node)
- || is_sparc_SwitchJmp(node) || is_sparc_Call(node)
- || is_sparc_SDiv(node) || is_sparc_UDiv(node)
- || is_sparc_Return(node);
+ return arch_get_irn_flags(node) & sparc_arch_irn_flag_has_delay_slot;
}
/** returns true if the emitter for this sparc node can produce more than one
if (has_delay_slot(node))
return true;
- return is_sparc_Mulh(node) || is_sparc_SDiv(node) || is_sparc_UDiv(node)
+ if (is_sparc_Call(node)) {
+ return arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return;
+ }
+
+ return is_sparc_SMulh(node) || is_sparc_UMulh(node)
+ || is_sparc_SDiv(node) || is_sparc_UDiv(node)
|| be_is_MemPerm(node) || be_is_Perm(node);
}
if (emits_multiple_instructions(schedpoint))
continue;
+ /* if check and schedpoint are not in the same block, give up. */
+ if (check != NULL
+ && get_nodes_block(check) != get_nodes_block(schedpoint))
+ break;
+
/* allowed for delayslot: any instruction which is not necessary to
* compute an input to the branch. */
if (check != NULL
static void emit_sparc_Mulh(const ir_node *irn)
{
be_emit_cstring("\t");
- sparc_emit_mode_sign_prefix(irn);
+ if (is_sparc_UMulh(irn)) {
+ be_emit_char('u');
+ } else {
+ assert(is_sparc_SMulh(irn));
+ be_emit_char('s');
+ }
be_emit_cstring("mul ");
sparc_emit_source_register(irn, 0);
be_emit_finish_line_gas(node);
fill_delay_slot();
+
+ if (arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return) {
+ be_emit_cstring("\tunimp 8\n");
+ be_emit_write_line();
+ }
}
/**
be_emit_finish_line_gas(irn);
}
-static void emit_be_MemPerm(const ir_node *node)
+/* The stack pointer must always be SPARC_STACK_ALIGNMENT bytes aligned, so get
+ * the next bigger integer that's evenly divisible by it. */
+static unsigned get_aligned_sp_change(const unsigned num_regs)
{
- int i;
- int memperm_arity;
- int sp_change = 0;
- ir_graph *irg = get_irn_irg(node);
- be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
+ const unsigned bytes = num_regs * SPARC_REGISTER_SIZE;
+ return round_up2(bytes, SPARC_STACK_ALIGNMENT);
+}
+
+/* Spill register l0 or both l0 and l1, depending on n_spilled and n_to_spill.*/
+static void memperm_emit_spill_registers(const ir_node *node, int n_spilled,
+ int n_to_spill)
+{
+ assert(n_spilled < n_to_spill);
+
+ if (n_spilled == 0) {
+ /* We always reserve stack space for two registers because during copy
+ * processing we don't know yet if we also need to handle a cycle which
+ * needs two registers. More complicated code in emit_MemPerm would
+ * prevent wasting SPARC_REGISTER_SIZE bytes of stack space but
+ * it is not worth the worse readability of emit_MemPerm. */
+
+ /* Keep stack pointer aligned. */
+ unsigned sp_change = get_aligned_sp_change(2);
+ be_emit_irprintf("\tsub %%sp, %u, %%sp", sp_change);
+ be_emit_finish_line_gas(node);
+
+ /* Spill register l0. */
+ be_emit_irprintf("\tst %%l0, [%%sp%+d]", SPARC_MIN_STACKSIZE);
+ be_emit_finish_line_gas(node);
+ }
+
+ if (n_to_spill == 2) {
+ /* Spill register l1. */
+ be_emit_irprintf("\tst %%l1, [%%sp%+d]", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
+ be_emit_finish_line_gas(node);
+ }
+}
+
+/* Restore register l0 or both l0 and l1, depending on n_spilled. */
+static void memperm_emit_restore_registers(const ir_node *node, int n_spilled)
+{
+ unsigned sp_change;
+
+ if (n_spilled == 2) {
+ /* Restore register l1. */
+ be_emit_irprintf("\tld [%%sp%+d], %%l1", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
+ be_emit_finish_line_gas(node);
+ }
+
+ /* Restore register l0. */
+ be_emit_irprintf("\tld [%%sp%+d], %%l0", SPARC_MIN_STACKSIZE);
+ be_emit_finish_line_gas(node);
- /* this implementation only works with frame pointers currently */
- assert(layout->sp_relative == false);
+ /* Restore stack pointer. */
+ sp_change = get_aligned_sp_change(2);
+ be_emit_irprintf("\tadd %%sp, %u, %%sp", sp_change);
+ be_emit_finish_line_gas(node);
+}
- /* TODO: this implementation is slower than necessary.
- The longterm goal is however to avoid the memperm node completely */
+/* Emit code to copy in_ent to out_ent. Only uses l0. */
+static void memperm_emit_copy(const ir_node *node, ir_entity *in_ent,
+ ir_entity *out_ent)
+{
+ ir_graph *irg = get_irn_irg(node);
+ be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
+ int off_in = be_get_stack_entity_offset(layout, in_ent, 0);
+ int off_out = be_get_stack_entity_offset(layout, out_ent, 0);
- memperm_arity = be_get_MemPerm_entity_arity(node);
- // we use our local registers - so this is limited to 8 inputs !
- if (memperm_arity > 8)
- panic("memperm with more than 8 inputs not supported yet");
+ /* Load from input entity. */
+ be_emit_irprintf("\tld [%%fp%+d], %%l0", off_in);
+ be_emit_finish_line_gas(node);
- be_emit_irprintf("\tsub %%sp, %d, %%sp", memperm_arity*4);
+ /* Store to output entity. */
+ be_emit_irprintf("\tst %%l0, [%%fp%+d]", off_out);
be_emit_finish_line_gas(node);
+}
- for (i = 0; i < memperm_arity; ++i) {
- ir_entity *entity = be_get_MemPerm_in_entity(node, i);
- int offset = be_get_stack_entity_offset(layout, entity, 0);
+/* Emit code to swap ent1 and ent2. Uses l0 and l1. */
+static void memperm_emit_swap(const ir_node *node, ir_entity *ent1,
+ ir_entity *ent2)
+{
+ ir_graph *irg = get_irn_irg(node);
+ be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
+ int off1 = be_get_stack_entity_offset(layout, ent1, 0);
+ int off2 = be_get_stack_entity_offset(layout, ent2, 0);
- /* spill register */
- be_emit_irprintf("\tst %%l%d, [%%sp%+d]", i, sp_change + SPARC_MIN_STACKSIZE);
- be_emit_finish_line_gas(node);
+ /* Load from first input entity. */
+ be_emit_irprintf("\tld [%%fp%+d], %%l0", off1);
+ be_emit_finish_line_gas(node);
- /* load from entity */
- be_emit_irprintf("\tld [%%fp%+d], %%l%d", offset, i);
- be_emit_finish_line_gas(node);
- sp_change += 4;
+ /* Load from second input entity. */
+ be_emit_irprintf("\tld [%%fp%+d], %%l1", off2);
+ be_emit_finish_line_gas(node);
+
+ /* Store first value to second output entity. */
+ be_emit_irprintf("\tst %%l0, [%%fp%+d]", off2);
+ be_emit_finish_line_gas(node);
+
+ /* Store second value to first output entity. */
+ be_emit_irprintf("\tst %%l1, [%%fp%+d]", off1);
+ be_emit_finish_line_gas(node);
+}
+
+/* Find the index of ent in ents or return -1 if not found. */
+static int get_index(ir_entity **ents, int n, ir_entity *ent)
+{
+ int i;
+
+ for (i = 0; i < n; ++i)
+ if (ents[i] == ent)
+ return i;
+
+ return -1;
+}
+
+/*
+ * Emit code for a MemPerm node.
+ *
+ * Analyze MemPerm for copy chains and cyclic swaps and resolve them using
+ * loads and stores.
+ * This function is conceptually very similar to permute_values in
+ * beprefalloc.c.
+ */
+static void emit_be_MemPerm(const ir_node *node)
+{
+ int memperm_arity = be_get_MemPerm_entity_arity(node);
+ /* Upper limit for the number of participating entities is twice the
+ * arity, e.g., for a simple copying MemPerm node with one input/output. */
+ int max_size = 2 * memperm_arity;
+ ir_entity **entities = ALLOCANZ(ir_entity *, max_size);
+ /* sourceof contains the input entity for each entity. If an entity is
+ * never used as an output, its entry in sourceof is a fix point. */
+ int *sourceof = ALLOCANZ(int, max_size);
+ /* n_users counts how many output entities use this entity as their input.*/
+ int *n_users = ALLOCANZ(int, max_size);
+ /* n_spilled records the number of spilled registers, either 1 or 2. */
+ int n_spilled = 0;
+ int i, n, oidx;
+
+ for (i = 0; i < max_size; ++i) {
+ sourceof[i] = i;
}
- for (i = memperm_arity-1; i >= 0; --i) {
- ir_entity *entity = be_get_MemPerm_out_entity(node, i);
- int offset = be_get_stack_entity_offset(layout, entity, 0);
+ for (i = n = 0; i < memperm_arity; ++i) {
+ ir_entity *out = be_get_MemPerm_out_entity(node, i);
+ ir_entity *in = be_get_MemPerm_in_entity(node, i);
+ int oidx; /* Out index */
+ int iidx; /* In index */
- sp_change -= 4;
+ /* Insert into entities to be able to operate on unique indices. */
+ if (get_index(entities, n, out) == -1)
+ entities[n++] = out;
+ if (get_index(entities, n, in) == -1)
+ entities[n++] = in;
- /* store to new entity */
- be_emit_irprintf("\tst %%l%d, [%%fp%+d]", i, offset);
- be_emit_finish_line_gas(node);
- /* restore register */
- be_emit_irprintf("\tld [%%sp%+d], %%l%d", sp_change + SPARC_MIN_STACKSIZE, i);
- be_emit_finish_line_gas(node);
+ oidx = get_index(entities, n, out);
+ iidx = get_index(entities, n, in);
+
+ sourceof[oidx] = iidx; /* Remember the source. */
+ ++n_users[iidx]; /* Increment number of users of this entity. */
}
- be_emit_irprintf("\tadd %%sp, %d, %%sp", memperm_arity*4);
- be_emit_finish_line_gas(node);
+ /* First do all the copies. */
+ for (oidx = 0; oidx < n; /* empty */) {
+ int iidx = sourceof[oidx];
+
+ /* Nothing to do for fix points.
+ * Also, if entities[oidx] is used as an input by another copy, we
+ * can't overwrite entities[oidx] yet.*/
+ if (iidx == oidx || n_users[oidx] > 0) {
+ ++oidx;
+ continue;
+ }
+
+ /* We found the end of a 'chain', so do the copy. */
+ if (n_spilled == 0) {
+ memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/1);
+ n_spilled = 1;
+ }
+ memperm_emit_copy(node, entities[iidx], entities[oidx]);
+
+ /* Mark as done. */
+ sourceof[oidx] = oidx;
+
+ assert(n_users[iidx] > 0);
+ /* Decrementing the number of users might enable us to do another
+ * copy. */
+ --n_users[iidx];
+
+ if (iidx < oidx && n_users[iidx] == 0) {
+ oidx = iidx;
+ } else {
+ ++oidx;
+ }
+ }
+
+ /* The rest are cycles. */
+ for (oidx = 0; oidx < n; /* empty */) {
+ int iidx = sourceof[oidx];
+ int tidx;
+
+ /* Nothing to do for fix points. */
+ if (iidx == oidx) {
+ ++oidx;
+ continue;
+ }
+
+ assert(n_users[iidx] == 1);
+
+ /* Swap the two values to resolve the cycle. */
+ if (n_spilled < 2) {
+ memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/2);
+ n_spilled = 2;
+ }
+ memperm_emit_swap(node, entities[iidx], entities[oidx]);
+
+ tidx = sourceof[iidx];
+ /* Mark as done. */
+ sourceof[iidx] = iidx;
+
+ /* The source of oidx is now the old source of iidx, because we swapped
+ * the two entities. */
+ sourceof[oidx] = tidx;
+ }
- assert(sp_change == 0);
+#ifdef DEBUG_libfirm
+ /* Only fix points should remain. */
+ for (i = 0; i < max_size; ++i) {
+ assert(sourceof[i] == i);
+ }
+#endif
+
+ assert(n_spilled > 0 && "Useless MemPerm node");
+
+ memperm_emit_restore_registers(node, n_spilled);
}
static void emit_sparc_Return(const ir_node *node)
{
+ ir_graph *irg = get_irn_irg(node);
+ ir_entity *entity = get_irg_entity(irg);
+ ir_type *type = get_entity_type(entity);
+
const char *destreg = "%o7";
/* hack: we don't explicitely model register changes because of the
}
be_emit_cstring("\tjmp ");
be_emit_string(destreg);
- be_emit_cstring("+8");
+ if (get_method_calling_convention(type) & cc_compound_ret) {
+ be_emit_cstring("+12");
+ } else {
+ be_emit_cstring("+8");
+ }
be_emit_finish_line_gas(node);
fill_delay_slot();
}
be_emit_cstring("\t/* fallthrough to ");
sparc_emit_cfop_target(node);
be_emit_cstring(" */");
+ be_emit_finish_line_gas(node);
} else {
be_emit_cstring("\tba ");
sparc_emit_cfop_target(node);
be_emit_finish_line_gas(node);
fill_delay_slot();
}
- be_emit_finish_line_gas(node);
}
static void emit_sparc_SwitchJmp(const ir_node *node)
static void emit_be_Copy(const ir_node *node)
{
ir_mode *mode = get_irn_mode(node);
- const arch_register_t *src_reg = get_in_reg(node, 0);
- const arch_register_t *dst_reg = arch_irn_get_register(node, 0);
+ const arch_register_t *src_reg = arch_get_irn_register_in(node, 0);
+ const arch_register_t *dst_reg = arch_get_irn_register_out(node, 0);
if (src_reg == dst_reg)
return;
set_emitter(op_sparc_Call, emit_sparc_Call);
set_emitter(op_sparc_fbfcc, emit_sparc_fbfcc);
set_emitter(op_sparc_FrameAddr, emit_sparc_FrameAddr);
- set_emitter(op_sparc_Mulh, emit_sparc_Mulh);
+ set_emitter(op_sparc_SMulh, emit_sparc_Mulh);
+ set_emitter(op_sparc_UMulh, emit_sparc_Mulh);
set_emitter(op_sparc_Return, emit_sparc_Return);
set_emitter(op_sparc_SDiv, emit_sparc_SDiv);
set_emitter(op_sparc_SwitchJmp, emit_sparc_SwitchJmp);
return schedpoint;
}
+static bool block_needs_label(const ir_node *block, const ir_node *sched_prev)
+{
+ int n_cfgpreds;
+
+ if (has_Block_entity(block))
+ return true;
+
+ n_cfgpreds = get_Block_n_cfgpreds(block);
+ if (n_cfgpreds == 0) {
+ return false;
+ } else if (n_cfgpreds > 1) {
+ return true;
+ } else {
+ ir_node *cfgpred = get_Block_cfgpred(block, 0);
+ ir_node *cfgpred_block = get_nodes_block(cfgpred);
+ if (is_Proj(cfgpred) && is_sparc_SwitchJmp(get_Proj_pred(cfgpred)))
+ return true;
+ return sched_prev != cfgpred_block || get_irn_link(cfgpred) != block;
+ }
+}
+
/**
* Walks over the nodes in a block connected by scheduling edges
* and emits code for each node.
*/
-static void sparc_emit_block(ir_node *block)
+static void sparc_emit_block(ir_node *block, ir_node *prev)
{
ir_node *node;
ir_node *next_delay_slot;
assert(is_Block(block));
- be_gas_emit_block_name(block);
- be_emit_cstring(":\n");
- be_emit_write_line();
+ if (block_needs_label(block, prev)) {
+ be_gas_emit_block_name(block);
+ be_emit_cstring(":\n");
+ be_emit_write_line();
+ }
next_delay_slot = find_next_delay_slot(sched_first(block));
if (next_delay_slot != NULL)
size_t i;
size_t n;
- be_gas_elf_type_char = '#';
- be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF_SPARC;
-
heights = heights_new(irg);
/* register all emitter functions */
for (i = 0; i < n; ++i) {
ir_node *block = block_schedule[i];
+ ir_node *prev = i>=1 ? block_schedule[i-1] : NULL;
if (block == get_irg_end_block(irg))
continue;
- sparc_emit_block(block);
+ sparc_emit_block(block, prev);
}
/* emit function epilog */