+/*
+ * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
/**
- * This file implements the node emitter.
- * @author Christian Wuerdig, Matthias Braun
- * $Id$
+ * @file
+ * @brief This file implements the ia32 node emitter.
+ * @author Christian Wuerdig, Matthias Braun
+ * @version $Id$
*/
#ifdef HAVE_CONFIG_H
-#include <config.h>
+#include "config.h"
#endif
#include <limits.h>
#include "iredges_t.h"
#include "execfreq.h"
#include "error.h"
+#include "raw_bitset.h"
#include "../besched_t.h"
#include "../benode_t.h"
#include "../beabi.h"
#include "../be_dbgout.h"
+#include "../beemitter.h"
+#include "../begnuas.h"
+#include "../beirg_t.h"
#include "ia32_emitter.h"
#include "gen_ia32_emitter.h"
#include "ia32_map_regs.h"
#include "bearch_ia32_t.h"
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
+
#define BLOCK_PREFIX ".L"
#define SNPRINTF_BUF_LEN 128
-/* global arch_env for lc_printf functions */
-static const arch_env_t *arch_env = NULL;
-
-/** by default, we generate assembler code for the Linux gas */
-asm_flavour_t asm_flavour = ASM_LINUX_GAS;
-
-/**
- * Switch to a new section
- */
-void ia32_switch_section(FILE *F, section_t sec) {
- static section_t curr_sec = NO_SECTION;
- static const char *text[ASM_MAX][SECTION_MAX] = {
- {
- ".section\t.text",
- ".section\t.data",
- ".section\t.rodata",
- ".section\t.bss",
- ".section\t.tbss,\"awT\",@nobits",
- ".section\t.ctors,\"aw\",@progbits"
- },
- {
- ".section\t.text",
- ".section\t.data",
- ".section .rdata,\"dr\"",
- ".section\t.bss",
- ".section\t.tbss,\"awT\",@nobits",
- ".section\t.ctors,\"aw\",@progbits"
- }
- };
-
- if (curr_sec == sec)
- return;
-
- curr_sec = sec;
- switch (sec) {
-
- case NO_SECTION:
- break;
-
- case SECTION_TEXT:
- case SECTION_DATA:
- case SECTION_RODATA:
- case SECTION_COMMON:
- case SECTION_TLS:
- case SECTION_CTOR:
- fprintf(F, "\t%s\n", text[asm_flavour][sec]);
- break;
-
- default:
- break;
- }
-}
-
-static void ia32_dump_function_object(FILE *F, const char *name)
-{
- switch (asm_flavour) {
- case ASM_LINUX_GAS:
- fprintf(F, "\t.type\t%s, @function\n", name);
- break;
- case ASM_MINGW_GAS:
- fprintf(F, "\t.def\t%s;\t.scl\t2;\t.type\t32;\t.endef\n", name);
- break;
- default:
- break;
- }
-}
-
-static void ia32_dump_function_size(FILE *F, const char *name)
-{
- switch (asm_flavour) {
- case ASM_LINUX_GAS:
- fprintf(F, "\t.size\t%s, .-%s\n", name, name);
- break;
- default:
- break;
- }
-}
+static const arch_env_t *arch_env;
+static const ia32_isa_t *isa;
+static ia32_code_gen_t *cg;
/**
* Returns the register at in position pos.
*/
-static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
- ir_node *op;
- const arch_register_t *reg = NULL;
+static const arch_register_t *get_in_reg(const ir_node *irn, int pos)
+{
+ ir_node *op;
+ const arch_register_t *reg = NULL;
assert(get_irn_arity(irn) > pos && "Invalid IN position");
assert(reg && "no in register found");
- /* in case of a joker register: just return a valid register */
- if (arch_register_type_is(reg, joker)) {
- arch_register_req_t req;
- const arch_register_req_t *p_req;
+ if(reg == &ia32_gp_regs[REG_GP_NOREG])
+ panic("trying to emit noreg for %+F input %d", irn, pos);
+
+ /* in case of unknown register: just return a valid register */
+ if (reg == &ia32_gp_regs[REG_GP_UKNWN]) {
+ const arch_register_req_t *req;
/* ask for the requirements */
- p_req = arch_get_register_req(arch_env, &req, irn, pos);
+ req = arch_get_register_req(arch_env, irn, pos);
- if (arch_register_req_is(p_req, limited)) {
+ if (arch_register_req_is(req, limited)) {
/* in case of limited requirements: get the first allowed register */
-
- bitset_t *bs = bitset_alloca(arch_register_class_n_regs(p_req->cls));
- int idx;
-
- p_req->limited(p_req->limited_env, bs);
- idx = bitset_next_set(bs, 0);
- reg = arch_register_for_index(p_req->cls, idx);
+ unsigned idx = rbitset_next(req->limited, 0, 1);
+ reg = arch_register_for_index(req->cls, idx);
} else {
/* otherwise get first register in class */
- reg = arch_register_for_index(p_req->cls, 0);
+ reg = arch_register_for_index(req->cls, 0);
}
}
/**
* Returns the register at out position pos.
*/
-static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
- ir_node *proj;
- const arch_register_t *reg = NULL;
+static const arch_register_t *get_out_reg(const ir_node *irn, int pos)
+{
+ ir_node *proj;
+ const arch_register_t *reg = NULL;
/* 1st case: irn is not of mode_T, so it has only */
/* one OUT register -> good */
/* Proj with the corresponding projnum for the register */
if (get_irn_mode(irn) != mode_T) {
+ assert(pos == 0);
reg = arch_get_irn_register(arch_env, irn);
} else if (is_ia32_irn(irn)) {
reg = get_ia32_out_reg(irn, pos);
return reg;
}
-/**
- * Returns an ident for the given tarval tv.
- */
-static ident *get_ident_for_tv(tarval *tv) {
- char buf[256];
- int len = tarval_snprintf(buf, sizeof(buf), tv);
- assert(len);
- return new_id_from_str(buf);
-}
-
-/**
- * Determine the gnu assembler suffix that indicates a mode
- */
-static char get_mode_suffix(const ir_mode *mode) {
- if(mode_is_float(mode)) {
- switch(get_mode_size_bits(mode)) {
- case 32:
- return 's';
- case 64:
- return 'l';
- case 80:
- return 't';
- }
- } else {
- assert(mode_is_int(mode) || mode_is_reference(mode));
- switch(get_mode_size_bits(mode)) {
- case 64:
- return 'q';
- case 32:
- return 'l';
- case 16:
- return 'w';
- case 8:
- return 'b';
- }
- }
- panic("Can't output mode_suffix for %+F\n", mode);
-}
-
-static int produces_result(const ir_node *node) {
- return !(is_ia32_St(node) ||
- is_ia32_Store8Bit(node) ||
- is_ia32_CondJmp(node) ||
- is_ia32_xCondJmp(node) ||
- is_ia32_CmpSet(node) ||
- is_ia32_xCmpSet(node) ||
- is_ia32_SwitchJmp(node));
-}
-
-static const char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
- switch(get_mode_size_bits(mode)) {
- case 8:
- return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
- case 16:
- return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
- default:
- return (char *)arch_register_get_name(reg);
- }
-}
-
-#if 0
-/**
- * Determines the SSE suffix depending on the mode.
- */
-static int ia32_print_mode_suffix(lc_appendable_t *app,
- const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
-{
- ir_node *irn = arg->v_ptr;
- ir_mode *mode = get_ia32_ls_mode(irn);
-
- if (mode_is_float(mode)) {
- return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
- } else {
- if(get_mode_size_bits(mode) == 32)
- return 0;
-
- if(mode_is_signed(mode))
- lc_appendable_chadd(app, 's');
- else
- lc_appendable_chadd(app, 'z');
-
- lc_appendable_chadd(app, get_mode_suffix(mode));
- return 2;
- }
-}
-#endif
-
/**
* Add a number to a prefix. This number will not be used a second time.
*/
-static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
+static char *get_unique_label(char *buf, size_t buflen, const char *prefix)
+{
static unsigned long id = 0;
snprintf(buf, buflen, "%s%lu", prefix, ++id);
return buf;
* |_| |_|
*************************************************************/
-void ia32_emit_ident(ia32_emit_env_t *env, ident *id)
+static void emit_8bit_register(const arch_register_t *reg)
+{
+ const char *reg_name = arch_register_get_name(reg);
+
+ be_emit_char('%');
+ be_emit_char(reg_name[1]);
+ be_emit_char('l');
+}
+
+static void emit_16bit_register(const arch_register_t *reg)
{
- size_t len = get_id_strlen(id);
- const char* str = get_id_str(id);
+ const char *reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
- ia32_emit_string_len(env, str, len);
+ be_emit_char('%');
+ be_emit_string(reg_name);
}
-void ia32_emit_irprintf(ia32_emit_env_t *env, const char *fmt, ...)
+static void emit_register(const arch_register_t *reg, const ir_mode *mode)
{
- char buf[128];
- va_list ap;
+ const char *reg_name;
+
+ if(mode != NULL) {
+ int size = get_mode_size_bits(mode);
+ if(size == 8) {
+ emit_8bit_register(reg);
+ return;
+ } else if(size == 16) {
+ emit_16bit_register(reg);
+ return;
+ } else {
+ assert(mode_is_float(mode) || size == 32);
+ }
+ }
- va_start(ap, fmt);
- ir_vsnprintf(buf, sizeof(buf), fmt, ap);
- va_end(ap);
+ reg_name = arch_register_get_name(reg);
- ia32_emit_string(env, buf);
+ be_emit_char('%');
+ be_emit_string(reg_name);
}
-void ia32_emit_source_register(ia32_emit_env_t *env, const ir_node *node, int pos)
+void ia32_emit_source_register(const ir_node *node, int pos)
{
- const arch_register_t *reg = get_in_reg(node, pos);
- const char *reg_name = arch_register_get_name(reg);
+ const arch_register_t *reg = get_in_reg(node, pos);
- assert(pos < get_irn_arity(node));
+ emit_register(reg, NULL);
+}
+
+static void emit_ia32_Immediate(const ir_node *node);
+
+void ia32_emit_8bit_source_register_or_immediate(const ir_node *node, int pos)
+{
+ const arch_register_t *reg;
+ ir_node *in = get_irn_n(node, pos);
+ if(is_ia32_Immediate(in)) {
+ emit_ia32_Immediate(in);
+ return;
+ }
- ia32_emit_char(env, '%');
- ia32_emit_string(env, reg_name);
+ reg = get_in_reg(node, pos);
+ emit_8bit_register(reg);
}
-void ia32_emit_dest_register(ia32_emit_env_t *env, const ir_node *node, int pos) {
- const arch_register_t *reg = get_out_reg(node, pos);
- const char *reg_name = arch_register_get_name(reg);
+void ia32_emit_dest_register(const ir_node *node, int pos)
+{
+ const arch_register_t *reg = get_out_reg(node, pos);
- ia32_emit_char(env, '%');
- ia32_emit_string(env, reg_name);
+ emit_register(reg, NULL);
}
-void ia32_emit_x87_name(ia32_emit_env_t *env, const ir_node *node, int pos)
+void ia32_emit_8bit_dest_register(const ir_node *node, int pos)
{
- ia32_attr_t *attr = get_ia32_attr(node);
+ const arch_register_t *reg = get_out_reg(node, pos);
- assert(pos < 7);
- ia32_emit_char(env, '%');
- ia32_emit_string(env, attr->x87[pos]->name);
+ emit_register(reg, mode_Bu);
}
-void ia32_emit_immediate(ia32_emit_env_t *env, const ir_node *node)
+void ia32_emit_x87_register(const ir_node *node, int pos)
{
- tarval *tv;
- ident *id;
+ const ia32_x87_attr_t *attr = get_ia32_x87_attr_const(node);
- switch(get_ia32_immop_type(node)) {
- case ia32_ImmConst:
- tv = get_ia32_Immop_tarval(node);
- id = get_ident_for_tv(tv);
- break;
- case ia32_ImmSymConst:
- id = get_ia32_Immop_symconst(node);
- break;
+ assert(pos < 3);
+ be_emit_char('%');
+ be_emit_string(attr->x87[pos]->name);
+}
+
+static void ia32_emit_mode_suffix_mode(const ir_mode *mode)
+{
+ if(mode_is_float(mode)) {
+ switch(get_mode_size_bits(mode)) {
+ case 32: be_emit_char('s'); return;
+ case 64: be_emit_char('l'); return;
+ case 80: be_emit_char('t'); return;
+ }
+ } else {
+ assert(mode_is_int(mode) || mode_is_reference(mode));
+ switch(get_mode_size_bits(mode)) {
+ case 64: be_emit_cstring("ll"); return;
+ /* gas docu says q is the suffix but gcc, objdump and icc use
+ ll apparently */
+ case 32: be_emit_char('l'); return;
+ case 16: be_emit_char('w'); return;
+ case 8: be_emit_char('b'); return;
+ }
+ }
+ panic("Can't output mode_suffix for %+F\n", mode);
+}
+
+void ia32_emit_mode_suffix(const ir_node *node)
+{
+ ir_mode *mode = get_ia32_ls_mode(node);
+ if(mode == NULL)
+ mode = mode_Iu;
+
+ ia32_emit_mode_suffix_mode(mode);
+}
+
+void ia32_emit_x87_mode_suffix(const ir_node *node)
+{
+ ir_mode *mode = get_ia32_ls_mode(node);
+ assert(mode != NULL);
+ /* we only need to emit the mode on address mode */
+ if(get_ia32_op_type(node) != ia32_Normal)
+ ia32_emit_mode_suffix_mode(mode);
+}
+
+static
+char get_xmm_mode_suffix(ir_mode *mode)
+{
+ assert(mode_is_float(mode));
+ switch(get_mode_size_bits(mode)) {
+ case 32:
+ return 's';
+ case 64:
+ return 'd';
default:
assert(0);
- ia32_emit_string(env, "BAD");
- return;
}
-
- ia32_emit_ident(env, id);
+ return '%';
}
-void ia32_emit_mode_suffix(ia32_emit_env_t *env, const ir_mode *mode)
+void ia32_emit_xmm_mode_suffix(const ir_node *node)
{
- ia32_emit_char(env, get_mode_suffix(mode));
+ ir_mode *mode = get_ia32_ls_mode(node);
+ assert(mode != NULL);
+ be_emit_char('s');
+ be_emit_char(get_xmm_mode_suffix(mode));
}
-void ia32_emit_x87_mode_suffix(ia32_emit_env_t *env, const ir_node *node)
+void ia32_emit_xmm_mode_suffix_s(const ir_node *node)
{
ir_mode *mode = get_ia32_ls_mode(node);
- if(mode != NULL)
- ia32_emit_mode_suffix(env, mode);
+ assert(mode != NULL);
+ be_emit_char(get_xmm_mode_suffix(mode));
}
-void ia32_emit_extend_suffix(ia32_emit_env_t *env, const ir_mode *mode)
+void ia32_emit_extend_suffix(const ir_mode *mode)
{
if(get_mode_size_bits(mode) == 32)
return;
if(mode_is_signed(mode)) {
- ia32_emit_char(env, 's');
+ be_emit_char('s');
} else {
- ia32_emit_char(env, 'z');
+ be_emit_char('z');
}
}
-/**
- * Emits registers and/or address mode of a binary operation.
- */
-void ia32_emit_binop(ia32_emit_env_t *env, const ir_node *node) {
- switch(get_ia32_op_type(node)) {
- case ia32_Normal:
- if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
- ia32_emit_char(env, '$');
- ia32_emit_immediate(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 2);
- } else {
- const arch_register_t *in1 = get_in_reg(node, 2);
- const arch_register_t *in2 = get_in_reg(node, 3);
- const arch_register_t *out = produces_result(node) ? get_out_reg(node, 0) : NULL;
- const arch_register_t *in;
- const char *in_name;
-
- in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
- out = out ? out : in1;
- in_name = arch_register_get_name(in);
+static
+void ia32_emit_function_object(const char *name)
+{
+ switch (be_gas_flavour) {
+ case GAS_FLAVOUR_NORMAL:
+ be_emit_cstring("\t.type\t");
+ be_emit_string(name);
+ be_emit_cstring(", @function\n");
+ be_emit_write_line();
+ break;
+ case GAS_FLAVOUR_MINGW:
+ be_emit_cstring("\t.def\t");
+ be_emit_string(name);
+ be_emit_cstring(";\t.scl\t2;\t.type\t32;\t.endef\n");
+ be_emit_write_line();
+ break;
+ default:
+ break;
+ }
+}
- if (is_ia32_emit_cl(node)) {
- assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in) && "shift operation needs ecx");
- in_name = "cl";
- }
+static
+void ia32_emit_function_size(const char *name)
+{
+ switch (be_gas_flavour) {
+ case GAS_FLAVOUR_NORMAL:
+ be_emit_cstring("\t.size\t");
+ be_emit_string(name);
+ be_emit_cstring(", .-");
+ be_emit_string(name);
+ be_emit_char('\n');
+ be_emit_write_line();
+ break;
+ default:
+ break;
+ }
+}
- ia32_emit_char(env, '%');
- ia32_emit_string(env, in_name);
- ia32_emit_cstring(env, ", %");
- ia32_emit_string(env, arch_register_get_name(out));
- }
- break;
- case ia32_AddrModeS:
- if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
- assert(!produces_result(node) && "Source AM with Const must not produce result");
- ia32_emit_am(env, node);
- ia32_emit_cstring(env, ", $");
- ia32_emit_immediate(env, node);
- } else if (produces_result(node)) {
- ia32_emit_am(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
- } else {
- ia32_emit_am(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 2);
- }
- break;
- case ia32_AddrModeD:
- if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
- ia32_emit_char(env, '$');
- ia32_emit_immediate(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_am(env, node);
- } else {
- const arch_register_t *in1 = get_in_reg(node, get_irn_arity(node) == 5 ? 3 : 2);
- ir_mode *mode = get_ia32_ls_mode(node);
- const char *in_name;
- in_name = ia32_get_reg_name_for_mode(env, mode, in1);
+void ia32_emit_source_register_or_immediate(const ir_node *node, int pos)
+{
+ ir_node *in = get_irn_n(node, pos);
+ if(is_ia32_Immediate(in)) {
+ emit_ia32_Immediate(in);
+ } else {
+ const ir_mode *mode = get_ia32_ls_mode(node);
+ const arch_register_t *reg = get_in_reg(node, pos);
+ emit_register(reg, mode);
+ }
+}
- if (is_ia32_emit_cl(node)) {
- assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
- in_name = "cl";
- }
+/**
+ * Emits registers and/or address mode of a binary operation.
+ */
+void ia32_emit_binop(const ir_node *node) {
+ const ir_node *right_op = get_irn_n(node, n_ia32_binary_right);
+ const ir_mode *mode = get_ia32_ls_mode(node);
+ const arch_register_t *reg_left;
- ia32_emit_char(env, '%');
- ia32_emit_string(env, in_name);
- ia32_emit_cstring(env, ", ");
- ia32_emit_am(env, node);
- }
+ switch(get_ia32_op_type(node)) {
+ case ia32_Normal:
+ reg_left = get_in_reg(node, n_ia32_binary_left);
+ if(is_ia32_Immediate(right_op)) {
+ emit_ia32_Immediate(right_op);
+ be_emit_cstring(", ");
+ emit_register(reg_left, mode);
break;
- default:
- assert(0 && "unsupported op type");
+ } else {
+ const arch_register_t *reg_right
+ = get_in_reg(node, n_ia32_binary_right);
+ emit_register(reg_right, mode);
+ be_emit_cstring(", ");
+ emit_register(reg_left, mode);
+ }
+ break;
+ case ia32_AddrModeS:
+ if(is_ia32_Immediate(right_op)) {
+ emit_ia32_Immediate(right_op);
+ be_emit_cstring(", ");
+ ia32_emit_am(node);
+ } else {
+ reg_left = get_in_reg(node, n_ia32_binary_left);
+ ia32_emit_am(node);
+ be_emit_cstring(", ");
+ emit_register(reg_left, mode);
+ }
+ break;
+ case ia32_AddrModeD:
+ panic("DestMode can't be output by %%binop anymore");
+ break;
+ default:
+ assert(0 && "unsupported op type");
}
}
/**
* Emits registers and/or address mode of a binary operation.
*/
-void ia32_emit_x87_binop(ia32_emit_env_t *env, const ir_node *node) {
+void ia32_emit_x87_binop(const ir_node *node) {
switch(get_ia32_op_type(node)) {
case ia32_Normal:
- if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
- // should not happen...
- assert(0);
- } else {
- ia32_attr_t *attr = get_ia32_attr(node);
- const arch_register_t *in1 = attr->x87[0];
- const arch_register_t *in2 = attr->x87[1];
- const arch_register_t *out = attr->x87[2];
+ {
+ const ia32_x87_attr_t *x87_attr = get_ia32_x87_attr_const(node);
+ const arch_register_t *in1 = x87_attr->x87[0];
+ const arch_register_t *in2 = x87_attr->x87[1];
+ const arch_register_t *out = x87_attr->x87[2];
const arch_register_t *in;
- in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
+ in = out ? ((out == in2) ? in1 : in2) : in2;
out = out ? out : in1;
- ia32_emit_char(env, '%');
- ia32_emit_string(env, arch_register_get_name(in));
- ia32_emit_cstring(env, ", %");
- ia32_emit_string(env, arch_register_get_name(out));
+ be_emit_char('%');
+ be_emit_string(arch_register_get_name(in));
+ be_emit_cstring(", %");
+ be_emit_string(arch_register_get_name(out));
}
break;
case ia32_AddrModeS:
- case ia32_AddrModeD:
- ia32_emit_am(env, node);
+ ia32_emit_am(node);
break;
+ case ia32_AddrModeD:
default:
assert(0 && "unsupported op type");
}
}
+void ia32_emit_am_or_dest_register(const ir_node *node,
+ int pos) {
+ if(get_ia32_op_type(node) == ia32_Normal) {
+ ia32_emit_dest_register(node, pos);
+ } else {
+ assert(get_ia32_op_type(node) == ia32_AddrModeD);
+ ia32_emit_am(node);
+ }
+}
+
/**
* Emits registers and/or address mode of a unary operation.
*/
-void ia32_emit_unop(ia32_emit_env_t *env, const ir_node *node) {
+void ia32_emit_unop(const ir_node *node, int pos) {
+ const ir_node *op;
+
switch(get_ia32_op_type(node)) {
- case ia32_Normal:
- if (is_ia32_ImmConst(node) || is_ia32_ImmSymConst(node)) {
- ia32_emit_char(env, '$');
- ia32_emit_immediate(env, node);
- } else {
- if (is_ia32_IMul(node) || is_ia32_Mulh(node)) {
- /* MulS and Mulh implicitly multiply by EAX */
- ia32_emit_source_register(env, node, 3);
- } else if(is_ia32_IDiv(node)) {
- ia32_emit_source_register(env, node, 1);
- } else if(is_ia32_Push(node)) {
- ia32_emit_source_register(env, node, 2);
- } else if(is_ia32_Pop(node)) {
- ia32_emit_dest_register(env, node, 1);
- } else {
- ia32_emit_dest_register(env, node, 0);
- }
- }
- break;
- case ia32_AddrModeS:
- case ia32_AddrModeD:
- ia32_emit_am(env, node);
- break;
- default:
- assert(0 && "unsupported op type");
+ case ia32_Normal:
+ op = get_irn_n(node, pos);
+ if (is_ia32_Immediate(op)) {
+ emit_ia32_Immediate(op);
+ } else {
+ ia32_emit_source_register(node, pos);
+ }
+ break;
+ case ia32_AddrModeS:
+ case ia32_AddrModeD:
+ ia32_emit_am(node);
+ break;
+ default:
+ assert(0 && "unsupported op type");
}
}
/**
* Emits address mode.
*/
-void ia32_emit_am(ia32_emit_env_t *env, const ir_node *node) {
- ia32_am_flavour_t am_flav = get_ia32_am_flavour(node);
- ident *id = get_ia32_am_sc(node);
- int offs = get_ia32_am_offs_int(node);
+void ia32_emit_am(const ir_node *node) {
+ ir_entity *ent = get_ia32_am_sc(node);
+ int offs = get_ia32_am_offs_int(node);
+ ir_node *base = get_irn_n(node, 0);
+ int has_base = !is_ia32_NoReg_GP(base);
+ ir_node *index = get_irn_n(node, 1);
+ int has_index = !is_ia32_NoReg_GP(index);
/* just to be sure... */
assert(!is_ia32_use_frame(node) || get_ia32_frame_ent(node) != NULL);
/* emit offset */
- if (id != NULL) {
+ if (ent != NULL) {
+ ident *id;
+
+ set_entity_backend_marked(ent, 1);
+ id = get_entity_ld_ident(ent);
if (is_ia32_am_sc_sign(node))
- ia32_emit_char(env, '-');
- ia32_emit_ident(env, id);
+ be_emit_char('-');
+ be_emit_ident(id);
+
+ if(get_entity_owner(ent) == get_tls_type()) {
+ if (get_entity_visibility(ent) == visibility_external_allocated) {
+ be_emit_cstring("@INDNTPOFF");
+ } else {
+ be_emit_cstring("@NTPOFF");
+ }
+ }
}
if(offs != 0) {
- if(id != NULL) {
- ia32_emit_irprintf(env, "%+d", offs);
+ if(ent != NULL) {
+ be_emit_irprintf("%+d", offs);
} else {
- ia32_emit_irprintf(env, "%d", offs);
+ be_emit_irprintf("%d", offs);
}
}
- if (am_flav & (ia32_B | ia32_I)) {
- ia32_emit_char(env, '(');
+ if (has_base || has_index) {
+ be_emit_char('(');
/* emit base */
- if (am_flav & ia32_B) {
- ia32_emit_source_register(env, node, 0);
+ if (has_base) {
+ const arch_register_t *reg = get_in_reg(node, n_ia32_base);
+ emit_register(reg, NULL);
}
/* emit index + scale */
- if (am_flav & ia32_I) {
- ia32_emit_char(env, ',');
- ia32_emit_source_register(env, node, 1);
-
- if (am_flav & ia32_S) {
- ia32_emit_irprintf(env, ",%d", 1 << get_ia32_am_scale(node));
+ if (has_index) {
+ const arch_register_t *reg = get_in_reg(node, n_ia32_index);
+ int scale;
+ be_emit_char(',');
+ emit_register(reg, NULL);
+
+ scale = get_ia32_am_scale(node);
+ if (scale > 0) {
+ be_emit_irprintf(",%d", 1 << get_ia32_am_scale(node));
}
}
- ia32_emit_char(env, ')');
- }
-}
-
-#if 0
-/**
- * Formated print of commands and comments.
- */
-static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
- unsigned lineno;
- const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
-
- if (name)
- fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
- else
- fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
-}
-#endif
-
-void ia32_write_line(ia32_emit_env_t *env)
-{
- char *finished_line = obstack_finish(env->obst);
-
- fwrite(finished_line, env->linelength, 1, env->out);
- env->linelength = 0;
- obstack_free(env->obst, finished_line);
-}
-
-void ia32_pad_comment(ia32_emit_env_t *env)
-{
- while(env->linelength <= 30) {
- ia32_emit_char(env, ' ');
- }
- ia32_emit_cstring(env, " ");
-}
-
-void ia32_emit_finish_line(ia32_emit_env_t *env, const ir_node *node)
-{
- dbg_info *dbg;
- const char *sourcefile;
- unsigned lineno;
-
- if(node == NULL) {
- ia32_emit_char(env, '\n');
- ia32_write_line(env);
- return;
+ be_emit_char(')');
}
- ia32_pad_comment(env);
- ia32_emit_cstring(env, "/* ");
- ia32_emit_irprintf(env, "%+F ", node);
-
- dbg = get_irn_dbg_info(node);
- sourcefile = be_retrieve_dbg_info(dbg, &lineno);
- if(sourcefile != NULL) {
- ia32_emit_string(env, sourcefile);
- ia32_emit_irprintf(env, ":%u", lineno);
+ /* special case if nothing is set */
+ if(ent == NULL && offs == 0 && !has_base && !has_index) {
+ be_emit_char('0');
}
- ia32_emit_cstring(env, " */\n");
- ia32_write_line(env);
}
-
/*************************************************
* _ _ _
* (_) | | |
*/
struct cmp2conditon_t {
const char *name;
- pn_Cmp num;
+ int num;
};
/*
{ "g", pn_Cmp_Gt }, /* > */
{ "ge", pn_Cmp_Ge }, /* >= */
{ "ne", pn_Cmp_Lg }, /* != */
- { NULL, pn_Cmp_Leg}, /* Floating point: ordered */
- { NULL, pn_Cmp_Uo }, /* Floating point: unordered */
- { "e", pn_Cmp_Ue }, /* Floating point: unordered or == */
- { "b", pn_Cmp_Ul }, /* Floating point: unordered or < */
- { "be", pn_Cmp_Ule }, /* Floating point: unordered or <= */
- { "a", pn_Cmp_Ug }, /* Floating point: unordered or > */
- { "ae", pn_Cmp_Uge }, /* Floating point: unordered or >= */
- { "ne", pn_Cmp_Ne }, /* Floating point: unordered or != */
- { NULL, pn_Cmp_True }, /* always true */
+ { NULL, pn_Cmp_Leg}, /* always true */
};
/*
{ "a", pn_Cmp_Gt }, /* > */
{ "ae", pn_Cmp_Ge }, /* >= */
{ "ne", pn_Cmp_Lg }, /* != */
- { NULL, pn_Cmp_True }, /* always true */
+ { NULL, pn_Cmp_Leg }, /* always true */
};
-/*
- * returns the condition code
+enum {
+ ia32_pn_Cmp_unsigned = 0x1000,
+ ia32_pn_Cmp_float = 0x2000,
+};
+
+/**
+ * walks up a tree of copies/perms/spills/reloads to find the original value
+ * that is moved around
*/
-static const char *get_cmp_suffix(int cmp_code)
+static ir_node *find_original_value(ir_node *node)
{
- assert( (cmp2condition_s[cmp_code & 15].num) == (cmp_code & 15));
- assert( (cmp2condition_u[cmp_code & 7].num) == (cmp_code & 7));
+ inc_irg_visited(current_ir_graph);
+ while(1) {
+ mark_irn_visited(node);
+ if(be_is_Copy(node)) {
+ node = be_get_Copy_op(node);
+ } else if(be_is_CopyKeep(node)) {
+ node = be_get_CopyKeep_op(node);
+ } else if(is_Proj(node)) {
+ ir_node *pred = get_Proj_pred(node);
+ if(be_is_Perm(pred)) {
+ node = get_irn_n(pred, get_Proj_proj(node));
+ } else if(be_is_MemPerm(pred)) {
+ node = get_irn_n(pred, get_Proj_proj(node) + 1);
+ } else if(is_ia32_Load(pred)) {
+ node = get_irn_n(pred, n_ia32_Load_mem);
+ } else {
+ return node;
+ }
+ } else if(is_ia32_Store(node)) {
+ node = get_irn_n(node, n_ia32_Store_val);
+ } else if(is_Phi(node)) {
+ int i, arity;
+ arity = get_irn_arity(node);
+ for(i = 0; i < arity; ++i) {
+ ir_node *in = get_irn_n(node, i);
+ if(irn_visited(in))
+ continue;
+ node = in;
+ break;
+ }
+ assert(i < arity);
+ } else {
+ return node;
+ }
+ }
+}
+
+static int determine_final_pnc(const ir_node *node, int flags_pos,
+ int pnc)
+{
+ ir_node *flags = get_irn_n(node, flags_pos);
+ const ia32_attr_t *flags_attr;
+ flags = skip_Proj(flags);
+
+ if(is_ia32_Sahf(flags)) {
+ ir_node *cmp = get_irn_n(flags, n_ia32_Sahf_val);
+ if(!(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
+ || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp))) {
+ cmp = find_original_value(cmp);
+ assert(is_ia32_FucomFnstsw(cmp) || is_ia32_FucompFnstsw(cmp)
+ || is_ia32_FucomppFnstsw(cmp) || is_ia32_FtstFnstsw(cmp));
+ }
- if((cmp_code & ia32_pn_Cmp_Unsigned)) {
- return cmp2condition_u[cmp_code & 7].name;
+ flags_attr = get_ia32_attr_const(cmp);
+ if(flags_attr->data.ins_permuted)
+ pnc = get_mirrored_pnc(pnc);
+ pnc |= ia32_pn_Cmp_float;
+ } else if(is_ia32_Ucomi(flags) || is_ia32_Fucomi(flags)
+ || is_ia32_Fucompi(flags)) {
+ flags_attr = get_ia32_attr_const(flags);
+
+ if(flags_attr->data.ins_permuted)
+ pnc = get_mirrored_pnc(pnc);
+ pnc |= ia32_pn_Cmp_float;
} else {
- return cmp2condition_s[cmp_code & 15].name;
+ assert(is_ia32_Cmp(flags) || is_ia32_Test(flags)
+ || is_ia32_Cmp8Bit(flags) || is_ia32_Test8Bit(flags));
+ flags_attr = get_ia32_attr_const(flags);
+
+ if(flags_attr->data.ins_permuted)
+ pnc = get_mirrored_pnc(pnc);
+ if(flags_attr->data.cmp_unsigned)
+ pnc |= ia32_pn_Cmp_unsigned;
}
+
+ return pnc;
}
-void ia32_emit_cmp_suffix(ia32_emit_env_t *env, long pnc)
+static void ia32_emit_cmp_suffix(int pnc)
{
- ia32_emit_string(env, get_cmp_suffix(pnc));
+ const char *str;
+
+ if((pnc & ia32_pn_Cmp_float) || (pnc & ia32_pn_Cmp_unsigned)) {
+ pnc = pnc & 7;
+ assert(cmp2condition_u[pnc].num == pnc);
+ str = cmp2condition_u[pnc].name;
+ } else {
+ pnc = pnc & 7;
+ assert(cmp2condition_s[pnc].num == pnc);
+ str = cmp2condition_s[pnc].name;
+ }
+
+ be_emit_string(str);
}
+void ia32_emit_cmp_suffix_node(const ir_node *node,
+ int flags_pos)
+{
+ const ia32_attr_t *attr = get_ia32_attr_const(node);
+
+ pn_Cmp pnc = get_ia32_pncode(node);
+
+ pnc = determine_final_pnc(node, flags_pos, pnc);
+ if(attr->data.ins_permuted) {
+ if(pnc & ia32_pn_Cmp_float) {
+ pnc = get_negated_pnc(pnc, mode_F);
+ } else {
+ pnc = get_negated_pnc(pnc, mode_Iu);
+ }
+ }
+
+ ia32_emit_cmp_suffix(pnc);
+}
/**
* Returns the target block for a control flow node.
*/
-static ir_node *get_cfop_target_block(const ir_node *irn) {
+static
+ir_node *get_cfop_target_block(const ir_node *irn) {
return get_irn_link(irn);
}
/**
- * Returns the target label for a control flow node.
+ * Emits a block label for the given block.
+ */
+static
+void ia32_emit_block_name(const ir_node *block)
+{
+ if (has_Block_label(block)) {
+ be_emit_string(be_gas_label_prefix());
+ be_emit_irprintf("%u", (unsigned)get_Block_label(block));
+ } else {
+ be_emit_cstring(BLOCK_PREFIX);
+ be_emit_irprintf("%d", get_irn_node_nr(block));
+ }
+}
+
+/**
+ * Emits the target label for a control flow node.
*/
-void ia32_emit_cfop_target(ia32_emit_env_t * env, const ir_node *node) {
+static void ia32_emit_cfop_target(const ir_node *node)
+{
ir_node *block = get_cfop_target_block(node);
- ia32_emit_cstring(env, BLOCK_PREFIX);
- ia32_emit_irprintf(env, "%d", get_irn_node_nr(block));
+ ia32_emit_block_name(block);
}
/** Return the next block in Block schedule */
-static ir_node *next_blk_sched(const ir_node *block) {
+static ir_node *next_blk_sched(const ir_node *block)
+{
return get_irn_link(block);
}
/**
* Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
*/
-static void finish_CondJmp(ia32_emit_env_t *env, const ir_node *node,
- ir_mode *mode, long pnc) {
+static void emit_ia32_Jcc(const ir_node *node)
+{
const ir_node *proj_true;
const ir_node *proj_false;
const ir_node *block;
- const ir_node *next_block;
- int flipped = 0;
-
- /* get both Proj's */
- proj_true = get_proj(node, pn_Cond_true);
- assert(proj_true && "CondJmp without true Proj");
-
- proj_false = get_proj(node, pn_Cond_false);
- assert(proj_false && "CondJmp without false Proj");
-
- /* for now, the code works for scheduled and non-schedules blocks */
- block = get_nodes_block(node);
-
- /* we have a block schedule */
- next_block = next_blk_sched(block);
-
- if (get_cfop_target_block(proj_true) == next_block) {
- /* exchange both proj's so the second one can be omitted */
- const ir_node *t = proj_true;
-
- proj_true = proj_false;
- proj_false = t;
- flipped = 1;
- pnc = get_negated_pnc(pnc, mode);
- }
-
- /* in case of unordered compare, check for parity */
- if (pnc & pn_Cmp_Uo) {
- ia32_emit_cstring(env, "\tjp ");
- ia32_emit_cfop_target(env, proj_true);
- ia32_emit_finish_line(env, proj_true);
- }
-
- ia32_emit_cstring(env, "\tj");
- ia32_emit_cmp_suffix(env, pnc);
- ia32_emit_char(env, ' ');
- ia32_emit_cfop_target(env, proj_true);
- ia32_emit_finish_line(env, proj_true);
-
- /* the second Proj might be a fallthrough */
- if (get_cfop_target_block(proj_false) != next_block) {
- ia32_emit_cstring(env, "\tjmp ");
- ia32_emit_cfop_target(env, proj_false);
- ia32_emit_finish_line(env, proj_false);
- } else {
- ia32_emit_cstring(env, "\t/* fallthrough to");
- ia32_emit_cfop_target(env, proj_false);
- ia32_emit_cstring(env, " */");
- ia32_emit_finish_line(env, proj_false);
- }
-}
-
-/**
- * Emits code for conditional jump.
- */
-static void CondJmp_emitter(ia32_emit_env_t *env, const ir_node *node) {
- ia32_emit_cstring(env, "\tcmp ");
- ia32_emit_binop(env, node);
- ia32_emit_finish_line(env, node);
-
- finish_CondJmp(env, node, mode_Iu, get_ia32_pncode(node));
-}
-
-/**
- * Emits code for conditional jump with two variables.
- */
-static void emit_ia32_CondJmp(ia32_emit_env_t *env, const ir_node *node) {
- CondJmp_emitter(env, node);
-}
-
-/**
- * Emits code for conditional test and jump.
- */
-static void TestJmp_emitter(ia32_emit_env_t *env, const ir_node *node) {
- if(is_ia32_ImmSymConst(node) || is_ia32_ImmConst(node)) {
- ia32_emit_cstring(env, "\ttest $");
- ia32_emit_immediate(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_finish_line(env, node);
- } else {
- ia32_emit_cstring(env, "\ttest ");
- ia32_emit_source_register(env, node, 1);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_finish_line(env, node);
- }
- finish_CondJmp(env, node, mode_Iu, get_ia32_pncode(node));
-}
-
-/**
- * Emits code for conditional test and jump with two variables.
- */
-static void emit_ia32_TestJmp(ia32_emit_env_t *env, const ir_node *node) {
- TestJmp_emitter(env, node);
-}
-
-static void emit_ia32_CJmp(ia32_emit_env_t *env, const ir_node *node) {
- ia32_emit_cstring(env, "/* omitted redundant test */");
- ia32_emit_finish_line(env, node);
-
- finish_CondJmp(env, node, mode_Is, get_ia32_pncode(node));
-}
-
-static void emit_ia32_CJmpAM(ia32_emit_env_t *env, const ir_node *node) {
- ia32_emit_cstring(env, "/* omitted redundant test/cmp */");
- ia32_emit_finish_line(env, node);
-
- finish_CondJmp(env, node, mode_Is, get_ia32_pncode(node));
-}
+ const ir_node *next_block;
+ pn_Cmp pnc = get_ia32_pncode(node);
-/**
- * Emits code for conditional SSE floating point jump with two variables.
- */
-static void emit_ia32_xCondJmp(ia32_emit_env_t *env, const ir_node *node) {
- ia32_emit_cstring(env, "\tucomis");
- ia32_emit_mode_suffix(env, get_irn_mode(node));
- ia32_emit_char(env, ' ');
- ia32_emit_binop(env, node);
- ia32_emit_finish_line(env, node);
+ pnc = determine_final_pnc(node, 0, pnc);
- finish_CondJmp(env, node, mode_F, get_ia32_pncode(node));
-}
+ /* get both Projs */
+ proj_true = get_proj(node, pn_ia32_Jcc_true);
+ assert(proj_true && "Jcc without true Proj");
-/**
- * Emits code for conditional x87 floating point jump with two variables.
- */
-static void emit_ia32_x87CondJmp(ia32_emit_env_t *env, const ir_node *node) {
- ia32_attr_t *attr = get_ia32_attr(node);
- const char *reg = attr->x87[1]->name;
- long pnc = get_ia32_pncode(node);
-
- switch (get_ia32_irn_opcode(node)) {
- case iro_ia32_fcomrJmp:
- pnc = get_inversed_pnc(pnc);
- case iro_ia32_fcomJmp:
- default:
- ia32_emit_cstring(env, "\tfucom ");
- break;
- case iro_ia32_fcomrpJmp:
- pnc = get_inversed_pnc(pnc);
- case iro_ia32_fcompJmp:
- ia32_emit_cstring(env, "\tfucomp ");
- break;
- case iro_ia32_fcomrppJmp:
- pnc = get_inversed_pnc(pnc);
- case iro_ia32_fcomppJmp:
- ia32_emit_cstring(env, "\tfucompp ");
- reg = "";
- break;
- }
+ proj_false = get_proj(node, pn_ia32_Jcc_false);
+ assert(proj_false && "Jcc without false Proj");
- if(reg[0] != '\0') {
- ia32_emit_char(env, '%');
- ia32_emit_string(env, reg);
- }
- ia32_emit_finish_line(env, node);
-
- ia32_emit_cstring(env, "\tfnstsw %ax");
- ia32_emit_finish_line(env, node);
- ia32_emit_cstring(env, "\tsahf");
- ia32_emit_finish_line(env, node);
-
- finish_CondJmp(env, node, mode_E, pnc);
-}
-
-static void CMov_emitter(ia32_emit_env_t *env, const ir_node *node) {
- long pnc = get_ia32_pncode(node);
- int is_PsiCondCMov = is_ia32_PsiCondCMov(node);
- int idx_left = 2 - is_PsiCondCMov;
- int idx_right = 3 - is_PsiCondCMov;
- const arch_register_t *in1, *in2, *out;
-
- out = arch_get_irn_register(env->arch_env, node);
- in1 = arch_get_irn_register(env->arch_env, get_irn_n(node, idx_left));
- in2 = arch_get_irn_register(env->arch_env, get_irn_n(node, idx_right));
-
- /* we have to emit the cmp first, because the destination register */
- /* could be one of the compare registers */
- if (is_ia32_CmpCMov(node)) {
- ia32_emit_cstring(env, "\tcmp ");
- ia32_emit_source_register(env, node, 1);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- } else if (is_ia32_xCmpCMov(node)) {
- ia32_emit_cstring(env, "\tucomis");
- ia32_emit_mode_suffix(env, get_irn_mode(node));
- ia32_emit_char(env, ' ');
- ia32_emit_source_register(env, node, 1);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- } else if (is_PsiCondCMov) {
- /* omit compare because flags are already set by And/Or */
- ia32_emit_cstring(env, "\ttest ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- } else {
- assert(0 && "unsupported CMov");
- }
- ia32_emit_finish_line(env, node);
+ block = get_nodes_block(node);
+ next_block = next_blk_sched(block);
- if (REGS_ARE_EQUAL(out, in2)) {
- /* best case: default in == out -> do nothing */
- } else if (REGS_ARE_EQUAL(out, in1)) {
- ir_node *n = (ir_node*) node;
- /* true in == out -> need complement compare and exchange true and default in */
- ir_node *t = get_irn_n(n, idx_left);
- set_irn_n(n, idx_left, get_irn_n(n, idx_right));
- set_irn_n(n, idx_right, t);
+ if (get_cfop_target_block(proj_true) == next_block) {
+ /* exchange both proj's so the second one can be omitted */
+ const ir_node *t = proj_true;
- pnc = get_negated_pnc(pnc, get_irn_mode(node));
- } else {
- /* out is different from in: need copy default -> out */
- if (is_PsiCondCMov) {
- ia32_emit_cstring(env, "\tmovl ");
- ia32_emit_dest_register(env, node, 2);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
+ proj_true = proj_false;
+ proj_false = t;
+ if(pnc & ia32_pn_Cmp_float) {
+ pnc = get_negated_pnc(pnc, mode_F);
} else {
- ia32_emit_cstring(env, "\tmovl ");
- ia32_emit_source_register(env, node, 3);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
+ pnc = get_negated_pnc(pnc, mode_Iu);
}
- ia32_emit_finish_line(env, node);
}
- if (is_PsiCondCMov) {
- ia32_emit_cstring(env, "\tcmov");
- ia32_emit_cmp_suffix(env, pnc);
- ia32_emit_cstring(env, "l ");
- ia32_emit_source_register(env, node, 1);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
- } else {
- ia32_emit_cstring(env, "\tcmov");
- ia32_emit_cmp_suffix(env, pnc);
- ia32_emit_cstring(env, "l ");
- ia32_emit_source_register(env, node, 2);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
- }
- ia32_emit_finish_line(env, node);
-}
-
-static void emit_ia32_CmpCMov(ia32_emit_env_t *env, const ir_node *node) {
- CMov_emitter(env, node);
-}
-
-static void emit_ia32_PsiCondCMov(ia32_emit_env_t *env, const ir_node *node) {
- CMov_emitter(env, node);
-}
-
-static void emit_ia32_xCmpCMov(ia32_emit_env_t *env, const ir_node *node) {
- CMov_emitter(env, node);
-}
-
-static void Set_emitter(ia32_emit_env_t *env, const ir_node *node, ir_mode *mode) {
- int pnc = get_ia32_pncode(node);
- const char *reg8bit;
- const arch_register_t *out;
+ if (pnc & ia32_pn_Cmp_float) {
+ /* Some floating point comparisons require a test of the parity flag,
+ * which indicates that the result is unordered */
+ switch (pnc & 15) {
+ case pn_Cmp_Uo:
+ be_emit_cstring("\tjp ");
+ ia32_emit_cfop_target(proj_true);
+ be_emit_finish_line_gas(proj_true);
+ break;
- out = arch_get_irn_register(env->arch_env, node);
- reg8bit = ia32_get_mapped_reg_name(env->isa->regs_8bit, out);
+ case pn_Cmp_Leg:
+ be_emit_cstring("\tjnp ");
+ ia32_emit_cfop_target(proj_true);
+ be_emit_finish_line_gas(proj_true);
+ break;
- if (is_ia32_CmpSet(node)) {
- ia32_emit_cstring(env, "\tcmp ");
- ia32_emit_binop(env, node);
- } else if (is_ia32_xCmpSet(node)) {
- ia32_emit_cstring(env, "\tucomis");
- ia32_emit_mode_suffix(env, get_irn_mode(get_irn_n(node, 2)));
- ia32_emit_char(env, ' ');
- ia32_emit_binop(env, node);
- } else if (is_ia32_PsiCondSet(node)) {
- ia32_emit_cstring(env, "\tcmp $0, ");
- ia32_emit_source_register(env, node, 0);
+ case pn_Cmp_Eq:
+ case pn_Cmp_Lt:
+ case pn_Cmp_Le:
+ be_emit_cstring("\tjp ");
+ ia32_emit_cfop_target(proj_false);
+ be_emit_finish_line_gas(proj_false);
+ goto emit_jcc;
+
+ case pn_Cmp_Ug:
+ case pn_Cmp_Uge:
+ case pn_Cmp_Ne:
+ be_emit_cstring("\tjp ");
+ ia32_emit_cfop_target(proj_true);
+ be_emit_finish_line_gas(proj_true);
+ goto emit_jcc;
+
+ default:
+ goto emit_jcc;
+ }
} else {
- assert(0 && "unsupported Set");
+emit_jcc:
+ be_emit_cstring("\tj");
+ ia32_emit_cmp_suffix(pnc);
+ be_emit_char(' ');
+ ia32_emit_cfop_target(proj_true);
+ be_emit_finish_line_gas(proj_true);
}
- ia32_emit_finish_line(env, node);
-
- /* use mov to clear target because it doesn't affect the eflags */
- ia32_emit_cstring(env, "\tmovl $0, %");
- ia32_emit_string(env, arch_register_get_name(out));
- ia32_emit_finish_line(env, node);
-
- ia32_emit_cstring(env, "\tset");
- ia32_emit_cmp_suffix(env, pnc);
- ia32_emit_cstring(env, " %");
- ia32_emit_string(env, reg8bit);
- ia32_emit_finish_line(env, node);
-}
-
-static void emit_ia32_CmpSet(ia32_emit_env_t *env, const ir_node *node) {
- Set_emitter(env, node, get_irn_mode(get_irn_n(node, 2)));
-}
-
-static void emit_ia32_PsiCondSet(ia32_emit_env_t *env, const ir_node *node) {
- Set_emitter(env, node, get_irn_mode(get_irn_n(node, 0)));
-}
-static void emit_ia32_xCmpSet(ia32_emit_env_t *env, const ir_node *node) {
- Set_emitter(env, node, get_irn_mode(get_irn_n(node, 2)));
+ /* the second Proj might be a fallthrough */
+ if (get_cfop_target_block(proj_false) != next_block) {
+ be_emit_cstring("\tjmp ");
+ ia32_emit_cfop_target(proj_false);
+ be_emit_finish_line_gas(proj_false);
+ } else {
+ be_emit_cstring("\t/* fallthrough to ");
+ ia32_emit_cfop_target(proj_false);
+ be_emit_cstring(" */");
+ be_emit_finish_line_gas(proj_false);
+ }
}
-static void emit_ia32_xCmp(ia32_emit_env_t *env, const ir_node *node) {
- int sse_pnc = -1;
- long pnc = get_ia32_pncode(node);
- long unord = pnc & pn_Cmp_Uo;
+static void emit_ia32_CMov(const ir_node *node)
+{
+ const ia32_attr_t *attr = get_ia32_attr_const(node);
+ int ins_permuted = attr->data.ins_permuted;
+ const arch_register_t *out = arch_get_irn_register(arch_env, node);
+ pn_Cmp pnc = get_ia32_pncode(node);
+ const arch_register_t *in_true;
+ const arch_register_t *in_false;
- assert( (pnc & ia32_pn_Cmp_Unsigned) == 0);
+ pnc = determine_final_pnc(node, n_ia32_CMov_eflags, pnc);
- switch (pnc) {
- case pn_Cmp_Leg: /* odered */
- sse_pnc = 7;
- break;
- case pn_Cmp_Uo: /* unordered */
- sse_pnc = 3;
- break;
- case pn_Cmp_Ue:
- case pn_Cmp_Eq: /* == */
- sse_pnc = 0;
- break;
- case pn_Cmp_Ul:
- case pn_Cmp_Lt: /* < */
- sse_pnc = 1;
- break;
- case pn_Cmp_Ule:
- case pn_Cmp_Le: /* <= */
- sse_pnc = 2;
- break;
- case pn_Cmp_Ug:
- case pn_Cmp_Gt: /* > */
- sse_pnc = 6;
- break;
- case pn_Cmp_Uge:
- case pn_Cmp_Ge: /* >= */
- sse_pnc = 5;
- break;
- case pn_Cmp_Ne:
- case pn_Cmp_Lg: /* != */
- sse_pnc = 4;
- break;
- }
+ in_true = arch_get_irn_register(arch_env,
+ get_irn_n(node, n_ia32_CMov_val_true));
+ in_false = arch_get_irn_register(arch_env,
+ get_irn_n(node, n_ia32_CMov_val_false));
- assert(sse_pnc >= 0 && "unsupported compare");
+ /* should be same constraint fullfilled? */
+ if(out == in_false) {
+ /* yes -> nothing to do */
+ } else if(out == in_true) {
+ const arch_register_t *tmp;
- if (unord && sse_pnc != 3) {
- /*
- We need a separate compare against unordered.
- Quick and Dirty solution:
- - get some memory on stack
- - compare
- - store result
- - compare
- - and result and stored result
- - cleanup stack
- */
- ia32_emit_cstring(env, "\tsubl $8, %esp");
- ia32_emit_finish_line(env, node);
+ assert(get_ia32_op_type(node) == ia32_Normal);
- ia32_emit_cstring(env, "\tcmpsd $3, ");
- ia32_emit_binop(env, node);
- ia32_emit_finish_line(env, node);
+ ins_permuted = !ins_permuted;
- ia32_emit_cstring(env, "\tmovsd ");
- ia32_emit_dest_register(env, node, 0);
- ia32_emit_cstring(env, ", (%esp)");
- ia32_emit_finish_line(env, node);
+ tmp = in_true;
+ in_true = in_false;
+ in_false = tmp;
+ } else {
+ /* we need a mov */
+ be_emit_cstring("\tmovl ");
+ emit_register(in_false, NULL);
+ be_emit_cstring(", ");
+ emit_register(out, NULL);
+ be_emit_finish_line_gas(node);
}
- ia32_emit_cstring(env, "\tcmpsd ");
- ia32_emit_irprintf(env, "%d, ", sse_pnc);
- ia32_emit_binop(env, node);
- ia32_emit_finish_line(env, node);
+ if(ins_permuted) {
+ if(pnc & ia32_pn_Cmp_float) {
+ pnc = get_negated_pnc(pnc, mode_F);
+ } else {
+ pnc = get_negated_pnc(pnc, mode_Iu);
+ }
+ }
- if (unord && sse_pnc != 3) {
- ia32_emit_cstring(env, "\tandpd (%esp), ");
- ia32_emit_dest_register(env, node, 0);
- ia32_emit_finish_line(env, node);
+ /* TODO: handling of Nans isn't correct yet */
- ia32_emit_cstring(env, "\taddl $8, %esp");
- ia32_emit_finish_line(env, node);
+ be_emit_cstring("\tcmov");
+ ia32_emit_cmp_suffix(pnc);
+ be_emit_char(' ');
+ if(get_ia32_op_type(node) == ia32_AddrModeS) {
+ ia32_emit_am(node);
+ } else {
+ emit_register(in_true, get_ia32_ls_mode(node));
}
+ be_emit_cstring(", ");
+ emit_register(out, get_ia32_ls_mode(node));
+ be_emit_finish_line_gas(node);
}
/*********************************************************
/* jump table for switch generation */
typedef struct _jmp_tbl_t {
ir_node *defProj; /**< default target */
- int min_value; /**< smallest switch case */
- int max_value; /**< largest switch case */
- int num_branches; /**< number of jumps */
+ long min_value; /**< smallest switch case */
+ long max_value; /**< largest switch case */
+ long num_branches; /**< number of jumps */
char *label; /**< label of the jump table */
branch_t *branches; /**< jump array */
} jmp_tbl_t;
/**
* Compare two variables of type branch_t. Used to sort all switch cases
*/
-static int ia32_cmp_branch_t(const void *a, const void *b) {
+static
+int ia32_cmp_branch_t(const void *a, const void *b) {
branch_t *b1 = (branch_t *)a;
branch_t *b2 = (branch_t *)b;
* possible otherwise a cmp-jmp cascade). Port from
* cggg ia32 backend
*/
-static void emit_ia32_SwitchJmp(ia32_emit_env_t *env, const ir_node *node) {
+static
+void emit_ia32_SwitchJmp(const ir_node *node) {
unsigned long interval;
int last_value, i;
long pnc;
interval = tbl.max_value - tbl.min_value;
/* emit the table */
- ia32_emit_cstring(env, "\tcmpl $");
- ia32_emit_irprintf(env, "%u, ", interval);
- ia32_emit_source_register(env, node, 0);
- ia32_emit_finish_line(env, node);
+ be_emit_cstring("\tcmpl $");
+ be_emit_irprintf("%u, ", interval);
+ ia32_emit_source_register(node, 0);
+ be_emit_finish_line_gas(node);
- ia32_emit_cstring(env, "\tja ");
- ia32_emit_cfop_target(env, tbl.defProj);
- ia32_emit_finish_line(env, node);
+ be_emit_cstring("\tja ");
+ ia32_emit_cfop_target(tbl.defProj);
+ be_emit_finish_line_gas(node);
if (tbl.num_branches > 1) {
/* create table */
- ia32_emit_cstring(env, "\tjmp *");
- ia32_emit_string(env, tbl.label);
- ia32_emit_cstring(env, "(,");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_cstring(env, ",4)");
- ia32_emit_finish_line(env, node);
+ be_emit_cstring("\tjmp *");
+ be_emit_string(tbl.label);
+ be_emit_cstring("(,");
+ ia32_emit_source_register(node, 0);
+ be_emit_cstring(",4)");
+ be_emit_finish_line_gas(node);
- ia32_switch_section(env->out, SECTION_RODATA);
- ia32_emit_cstring(env, "\t.align 4\n");
- ia32_write_line(env);
+ be_gas_emit_switch_section(GAS_SECTION_RODATA);
+ be_emit_cstring("\t.align 4\n");
+ be_emit_write_line();
- ia32_emit_string(env, tbl.label);
- ia32_emit_cstring(env, ":\n");
- ia32_write_line(env);
+ be_emit_string(tbl.label);
+ be_emit_cstring(":\n");
+ be_emit_write_line();
- ia32_emit_cstring(env, ".long ");
- ia32_emit_cfop_target(env, tbl.branches[0].target);
- ia32_emit_finish_line(env, NULL);
+ be_emit_cstring(".long ");
+ ia32_emit_cfop_target(tbl.branches[0].target);
+ be_emit_finish_line_gas(NULL);
last_value = tbl.branches[0].value;
for (i = 1; i < tbl.num_branches; ++i) {
while (++last_value < tbl.branches[i].value) {
- ia32_emit_cstring(env, ".long ");
- ia32_emit_cfop_target(env, tbl.defProj);
- ia32_emit_finish_line(env, NULL);
+ be_emit_cstring(".long ");
+ ia32_emit_cfop_target(tbl.defProj);
+ be_emit_finish_line_gas(NULL);
}
- ia32_emit_cstring(env, ".long ");
- ia32_emit_cfop_target(env, tbl.branches[i].target);
- ia32_emit_finish_line(env, NULL);
+ be_emit_cstring(".long ");
+ ia32_emit_cfop_target(tbl.branches[i].target);
+ be_emit_finish_line_gas(NULL);
}
- ia32_switch_section(env->out, SECTION_TEXT);
+ be_gas_emit_switch_section(GAS_SECTION_TEXT);
} else {
/* one jump is enough */
- ia32_emit_cstring(env, "\tjmp ");
- ia32_emit_cfop_target(env, tbl.branches[0].target);
- ia32_emit_finish_line(env, node);
+ be_emit_cstring("\tjmp ");
+ ia32_emit_cfop_target(tbl.branches[0].target);
+ be_emit_finish_line_gas(node);
}
if (tbl.label)
/**
* Emits code for a unconditional jump.
*/
-static void emit_Jmp(ia32_emit_env_t *env, const ir_node *node) {
+static void emit_Jmp(const ir_node *node)
+{
ir_node *block, *next_block;
/* for now, the code works for scheduled and non-schedules blocks */
/* we have a block schedule */
next_block = next_blk_sched(block);
if (get_cfop_target_block(node) != next_block) {
- ia32_emit_cstring(env, "\tjmp ");
- ia32_emit_cfop_target(env, node);
+ be_emit_cstring("\tjmp ");
+ ia32_emit_cfop_target(node);
+ } else {
+ be_emit_cstring("\t/* fallthrough to ");
+ ia32_emit_cfop_target(node);
+ be_emit_cstring(" */");
+ }
+ be_emit_finish_line_gas(node);
+}
+
+static void emit_ia32_Immediate(const ir_node *node)
+{
+ const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
+
+ be_emit_char('$');
+ if(attr->symconst != NULL) {
+ ident *id = get_entity_ld_ident(attr->symconst);
+
+ if(attr->attr.data.am_sc_sign)
+ be_emit_char('-');
+ be_emit_ident(id);
+ }
+ if(attr->symconst == NULL || attr->offset != 0) {
+ if(attr->symconst != NULL) {
+ be_emit_irprintf("%+d", attr->offset);
+ } else {
+ be_emit_irprintf("0x%X", attr->offset);
+ }
+ }
+}
+
+static const char* emit_asm_operand(const ir_node *node, const char *s)
+{
+ const ia32_attr_t *ia32_attr = get_ia32_attr_const(node);
+ const ia32_asm_attr_t *attr = CONST_CAST_IA32_ATTR(ia32_asm_attr_t,
+ ia32_attr);
+ const arch_register_t *reg;
+ const ia32_asm_reg_t *asm_regs = attr->register_map;
+ const ia32_asm_reg_t *asm_reg;
+ const char *reg_name;
+ char c;
+ char modifier = 0;
+ int num = -1;
+ int p;
+
+ assert(*s == '%');
+ c = *(++s);
+
+ /* parse modifiers */
+ switch(c) {
+ case 0:
+ ir_fprintf(stderr, "Warning: asm text (%+F) ends with %\n", node);
+ be_emit_char('%');
+ return s + 1;
+ case '%':
+ be_emit_char('%');
+ return s + 1;
+ case 'w':
+ case 'b':
+ case 'h':
+ modifier = c;
+ ++s;
+ break;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ default:
+ ir_fprintf(stderr, "Warning: asm text (%+F) contains unknown modifier "
+ "'%c' for asm op\n", node, c);
+ ++s;
+ break;
+ }
+
+ /* parse number */
+ sscanf(s, "%d%n", &num, &p);
+ if(num < 0) {
+ ir_fprintf(stderr, "Warning: Couldn't parse assembler operand (%+F)\n",
+ node);
+ return s;
+ } else {
+ s += p;
+ }
+
+ if(num < 0 || num >= ARR_LEN(asm_regs)) {
+ ir_fprintf(stderr, "Error: Custom assembler references invalid "
+ "input/output (%+F)\n", node);
+ return s;
+ }
+ asm_reg = & asm_regs[num];
+ assert(asm_reg->valid);
+
+ /* get register */
+ if(asm_reg->use_input == 0) {
+ reg = get_out_reg(node, asm_reg->inout_pos);
+ } else {
+ ir_node *pred = get_irn_n(node, asm_reg->inout_pos);
+
+ /* might be an immediate value */
+ if(is_ia32_Immediate(pred)) {
+ emit_ia32_Immediate(pred);
+ return s;
+ }
+ reg = get_in_reg(node, asm_reg->inout_pos);
+ }
+ if(reg == NULL) {
+ ir_fprintf(stderr, "Warning: no register assigned for %d asm op "
+ "(%+F)\n", num, node);
+ return s;
+ }
+
+ if(asm_reg->memory) {
+ be_emit_char('(');
+ }
+
+ /* emit it */
+ if(modifier != 0) {
+ be_emit_char('%');
+ switch(modifier) {
+ case 'b':
+ reg_name = ia32_get_mapped_reg_name(isa->regs_8bit, reg);
+ break;
+ case 'h':
+ reg_name = ia32_get_mapped_reg_name(isa->regs_8bit_high, reg);
+ break;
+ case 'w':
+ reg_name = ia32_get_mapped_reg_name(isa->regs_16bit, reg);
+ break;
+ default:
+ panic("Invalid asm op modifier");
+ }
+ be_emit_string(reg_name);
} else {
- ia32_emit_cstring(env, "\t/* fallthrough to ");
- ia32_emit_cfop_target(env, node);
- ia32_emit_cstring(env, " */");
+ emit_register(reg, asm_reg->mode);
+ }
+
+ if(asm_reg->memory) {
+ be_emit_char(')');
+ }
+
+ return s;
+}
+
+/**
+ * Emits code for an ASM pseudo op.
+ */
+static void emit_ia32_Asm(const ir_node *node)
+{
+ const void *gen_attr = get_irn_generic_attr_const(node);
+ const ia32_asm_attr_t *attr
+ = CONST_CAST_IA32_ATTR(ia32_asm_attr_t, gen_attr);
+ ident *asm_text = attr->asm_text;
+ const char *s = get_id_str(asm_text);
+
+ be_emit_cstring("# Begin ASM \t");
+ be_emit_finish_line_gas(node);
+
+ if (s[0] != '\t')
+ be_emit_char('\t');
+
+ while(*s != 0) {
+ if(*s == '%') {
+ s = emit_asm_operand(node, s);
+ continue;
+ } else {
+ be_emit_char(*s);
+ }
+ ++s;
}
- ia32_emit_finish_line(env, node);
+
+ be_emit_char('\n');
+ be_emit_write_line();
+
+ be_emit_cstring("# End ASM\n");
+ be_emit_write_line();
}
/**********************************
/**
* Emit movsb/w instructions to make mov count divideable by 4
*/
-static void emit_CopyB_prolog(ia32_emit_env_t *env, int rem) {
- ia32_emit_cstring(env, "\tcld");
- ia32_emit_finish_line(env, NULL);
+static void emit_CopyB_prolog(int rem) {
+ be_emit_cstring("\tcld");
+ be_emit_finish_line_gas(NULL);
switch(rem) {
case 1:
- ia32_emit_cstring(env, "\tmovsb");
- ia32_emit_finish_line(env, NULL);
+ be_emit_cstring("\tmovsb");
+ be_emit_finish_line_gas(NULL);
break;
case 2:
- ia32_emit_cstring(env, "\tmovsw");
- ia32_emit_finish_line(env, NULL);
+ be_emit_cstring("\tmovsw");
+ be_emit_finish_line_gas(NULL);
break;
case 3:
- ia32_emit_cstring(env, "\tmovsb");
- ia32_emit_finish_line(env, NULL);
- ia32_emit_cstring(env, "\tmovsw");
- ia32_emit_finish_line(env, NULL);
+ be_emit_cstring("\tmovsb");
+ be_emit_finish_line_gas(NULL);
+ be_emit_cstring("\tmovsw");
+ be_emit_finish_line_gas(NULL);
break;
}
}
/**
* Emit rep movsd instruction for memcopy.
*/
-static void emit_ia32_CopyB(ia32_emit_env_t *env, const ir_node *node) {
- tarval *tv = get_ia32_Immop_tarval(node);
- int rem = get_tarval_long(tv);
+static void emit_ia32_CopyB(const ir_node *node)
+{
+ int rem = get_ia32_pncode(node);
- emit_CopyB_prolog(env, rem);
+ emit_CopyB_prolog(rem);
- ia32_emit_cstring(env, "\trep movsd");
- ia32_emit_finish_line(env, node);
+ be_emit_cstring("\trep movsd");
+ be_emit_finish_line_gas(node);
}
/**
* Emits unrolled memcopy.
*/
-static void emit_ia32_CopyB_i(ia32_emit_env_t *env, const ir_node *node) {
- tarval *tv = get_ia32_Immop_tarval(node);
- int size = get_tarval_long(tv);
+static void emit_ia32_CopyB_i(const ir_node *node)
+{
+ int size = get_ia32_pncode(node);
- emit_CopyB_prolog(env, size & 0x3);
+ emit_CopyB_prolog(size & 0x3);
size >>= 2;
while (size--) {
- ia32_emit_cstring(env, "\tmovsd");
- ia32_emit_finish_line(env, NULL);
+ be_emit_cstring("\tmovsd");
+ be_emit_finish_line_gas(NULL);
}
}
/**
* Emit code for conversions (I, FP), (FP, I) and (FP, FP).
*/
-static void emit_ia32_Conv_with_FP(ia32_emit_env_t *env, const ir_node *node) {
+static void emit_ia32_Conv_with_FP(const ir_node *node)
+{
ir_mode *ls_mode = get_ia32_ls_mode(node);
int ls_bits = get_mode_size_bits(ls_mode);
- ia32_emit_cstring(env, "\tcvt");
+ be_emit_cstring("\tcvt");
if(is_ia32_Conv_I2FP(node)) {
if(ls_bits == 32) {
- ia32_emit_cstring(env, "si2ss");
+ be_emit_cstring("si2ss");
} else {
- ia32_emit_cstring(env, "si2sd");
+ be_emit_cstring("si2sd");
}
} else if(is_ia32_Conv_FP2I(node)) {
if(ls_bits == 32) {
- ia32_emit_cstring(env, "ss2si");
+ be_emit_cstring("ss2si");
} else {
- ia32_emit_cstring(env, "sd2si");
+ be_emit_cstring("sd2si");
}
} else {
assert(is_ia32_Conv_FP2FP(node));
if(ls_bits == 32) {
- ia32_emit_cstring(env, "sd2ss");
+ be_emit_cstring("sd2ss");
} else {
- ia32_emit_cstring(env, "ss2sd");
+ be_emit_cstring("ss2sd");
}
}
+ be_emit_char(' ');
switch(get_ia32_op_type(node)) {
case ia32_Normal:
- ia32_emit_dest_register(env, node, 0);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 2);
+ ia32_emit_source_register(node, n_ia32_unary_op);
break;
case ia32_AddrModeS:
- ia32_emit_dest_register(env, node, 0);
- ia32_emit_cstring(env, ", ");
- ia32_emit_am(env, node);
+ ia32_emit_am(node);
break;
default:
assert(0 && "unsupported op type for Conv");
}
- ia32_emit_finish_line(env, node);
+ be_emit_cstring(", ");
+ ia32_emit_dest_register(node, 0);
+ be_emit_finish_line_gas(node);
}
-static void emit_ia32_Conv_I2FP(ia32_emit_env_t *env, const ir_node *node) {
- emit_ia32_Conv_with_FP(env, node);
+static void emit_ia32_Conv_I2FP(const ir_node *node)
+{
+ emit_ia32_Conv_with_FP(node);
}
-static void emit_ia32_Conv_FP2I(ia32_emit_env_t *env, const ir_node *node) {
- emit_ia32_Conv_with_FP(env, node);
+static void emit_ia32_Conv_FP2I(const ir_node *node)
+{
+ emit_ia32_Conv_with_FP(node);
}
-static void emit_ia32_Conv_FP2FP(ia32_emit_env_t *env, const ir_node *node) {
- emit_ia32_Conv_with_FP(env, node);
+static void emit_ia32_Conv_FP2FP(const ir_node *node)
+{
+ emit_ia32_Conv_with_FP(node);
}
/**
* Emits code for an Int conversion.
*/
-static void emit_ia32_Conv_I2I(ia32_emit_env_t *env, const ir_node *node) {
- const char *sign_suffix;
- ir_mode *smaller_mode = get_ia32_ls_mode(node);
- int smaller_bits = get_mode_size_bits(smaller_mode);
- int signed_mode;
+static void emit_ia32_Conv_I2I(const ir_node *node)
+{
+ const char *sign_suffix;
+ ir_mode *smaller_mode = get_ia32_ls_mode(node);
+ int smaller_bits = get_mode_size_bits(smaller_mode);
+ int signed_mode;
const arch_register_t *in_reg, *out_reg;
assert(!mode_is_float(smaller_mode));
sign_suffix = signed_mode ? "s" : "z";
}
+ out_reg = get_out_reg(node, 0);
+
switch(get_ia32_op_type(node)) {
case ia32_Normal:
- in_reg = get_in_reg(node, 2);
- out_reg = get_out_reg(node, 0);
+ in_reg = get_in_reg(node, n_ia32_unary_op);
- if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
- REGS_ARE_EQUAL(out_reg, in_reg) &&
- signed_mode)
+ if (in_reg == &ia32_gp_regs[REG_EAX] &&
+ out_reg == &ia32_gp_regs[REG_EAX] &&
+ signed_mode &&
+ smaller_bits == 16)
{
/* argument and result are both in EAX and */
- /* signedness is ok: -> use converts */
- if (smaller_bits == 8) {
- ia32_emit_cstring(env, "\tcbtw");
- } else if (smaller_bits == 16) {
- ia32_emit_cstring(env, "\tcwtl");
- } else {
- assert(0);
- }
- } else if (REGS_ARE_EQUAL(out_reg, in_reg) && !signed_mode) {
- /* argument and result are in the same register */
- /* and signedness is ok: -> use and with mask */
- int mask = (1 << smaller_bits) - 1;
- ia32_emit_cstring(env, "\tandl $0x");
- ia32_emit_irprintf(env, "%x, ", mask);
- ia32_emit_dest_register(env, node, 0);
+ /* signedness is ok: -> use the smaller cwtl opcode */
+ be_emit_cstring("\tcwtl");
} else {
- const char *sreg = ia32_get_reg_name_for_mode(env, smaller_mode, in_reg);
-
- ia32_emit_cstring(env, "\tmov");
- ia32_emit_string(env, sign_suffix);
- ia32_emit_mode_suffix(env, smaller_mode);
- ia32_emit_cstring(env, "l %");
- ia32_emit_string(env, sreg);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
+ be_emit_cstring("\tmov");
+ be_emit_string(sign_suffix);
+ ia32_emit_mode_suffix_mode(smaller_mode);
+ be_emit_cstring("l ");
+ emit_register(in_reg, smaller_mode);
+ be_emit_cstring(", ");
+ emit_register(out_reg, NULL);
}
break;
case ia32_AddrModeS: {
- ia32_emit_cstring(env, "\tmov");
- ia32_emit_string(env, sign_suffix);
- ia32_emit_mode_suffix(env, smaller_mode);
- ia32_emit_cstring(env, "l %");
- ia32_emit_am(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
+ be_emit_cstring("\tmov");
+ be_emit_string(sign_suffix);
+ ia32_emit_mode_suffix_mode(smaller_mode);
+ be_emit_cstring("l ");
+ ia32_emit_am(node);
+ be_emit_cstring(", ");
+ emit_register(out_reg, NULL);
break;
}
default:
assert(0 && "unsupported op type for Conv");
}
- ia32_emit_finish_line(env, node);
-}
-
-/**
- * Emits code for an 8Bit Int conversion.
- */
-void emit_ia32_Conv_I2I8Bit(ia32_emit_env_t *env, const ir_node *node) {
- emit_ia32_Conv_I2I(env, node);
+ be_emit_finish_line_gas(node);
}
/**
* Emits a backend call
*/
-static void emit_be_Call(ia32_emit_env_t *env, const ir_node *node) {
+static void emit_be_Call(const ir_node *node)
+{
ir_entity *ent = be_Call_get_entity(node);
- ia32_emit_cstring(env, "\tcall ");
+ be_emit_cstring("\tcall ");
if (ent) {
- ia32_emit_string(env, get_entity_ld_name(ent));
+ set_entity_backend_marked(ent, 1);
+ be_emit_string(get_entity_ld_name(ent));
} else {
- ia32_emit_char(env, '*');
- ia32_emit_dest_register(env, get_irn_n(node, be_pos_Call_ptr), 0);
+ const arch_register_t *reg = get_in_reg(node, be_pos_Call_ptr);
+ be_emit_char('*');
+ emit_register(reg, NULL);
}
- ia32_emit_finish_line(env, node);
+ be_emit_finish_line_gas(node);
}
/**
* Emits code to increase stack pointer.
*/
-static void emit_be_IncSP(ia32_emit_env_t *env, const ir_node *node) {
- int offs = be_get_IncSP_offset(node);
+static void emit_be_IncSP(const ir_node *node)
+{
+ int offs = be_get_IncSP_offset(node);
+ const arch_register_t *reg = arch_get_irn_register(arch_env, node);
if (offs == 0)
return;
if (offs > 0) {
- ia32_emit_cstring(env, "\tsubl $");
- ia32_emit_irprintf(env, "%u, ", offs);
- ia32_emit_source_register(env, node, 0);
+ be_emit_cstring("\tsubl $");
+ be_emit_irprintf("%u, ", offs);
+ emit_register(reg, NULL);
} else {
- ia32_emit_cstring(env, "\taddl $");
- ia32_emit_irprintf(env, "%u, ", -offs);
- ia32_emit_source_register(env, node, 0);
+ be_emit_cstring("\taddl $");
+ be_emit_irprintf("%u, ", -offs);
+ emit_register(reg, NULL);
}
- ia32_emit_finish_line(env, node);
-}
-
-/**
- * Emits code to set stack pointer.
- */
-static void emit_be_SetSP(ia32_emit_env_t *env, const ir_node *node) {
- ia32_emit_cstring(env, "\tmovl ");
- ia32_emit_source_register(env, node, 2);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
- ia32_emit_finish_line(env, node);
+ be_emit_finish_line_gas(node);
}
/**
* Emits code for Copy/CopyKeep.
*/
-static void Copy_emitter(ia32_emit_env_t *env, const ir_node *node, const ir_node *op) {
- const arch_env_t *aenv = env->arch_env;
+static void Copy_emitter(const ir_node *node, const ir_node *op)
+{
+ const arch_register_t *in = arch_get_irn_register(arch_env, op);
+ const arch_register_t *out = arch_get_irn_register(arch_env, node);
+ ir_mode *mode;
- if (REGS_ARE_EQUAL(arch_get_irn_register(aenv, node), arch_get_irn_register(aenv, op)) ||
- arch_register_type_is(arch_get_irn_register(aenv, op), virtual))
+ if(in == out) {
+ return;
+ }
+ if(is_unknown_reg(in))
+ return;
+ /* copies of vf nodes aren't real... */
+ if(arch_register_get_class(in) == &ia32_reg_classes[CLASS_ia32_vfp])
return;
- if (mode_is_float(get_irn_mode(node))) {
- ia32_emit_cstring(env, "\tmovsd ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
+ mode = get_irn_mode(node);
+ if (mode == mode_E) {
+ be_emit_cstring("\tmovsd ");
+ emit_register(in, NULL);
+ be_emit_cstring(", ");
+ emit_register(out, NULL);
} else {
- ia32_emit_cstring(env, "\tmovl ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
+ be_emit_cstring("\tmovl ");
+ emit_register(in, NULL);
+ be_emit_cstring(", ");
+ emit_register(out, NULL);
}
- ia32_emit_finish_line(env, node);
+ be_emit_finish_line_gas(node);
}
-static void emit_be_Copy(ia32_emit_env_t *env, const ir_node *node) {
- Copy_emitter(env, node, be_get_Copy_op(node));
+static void emit_be_Copy(const ir_node *node)
+{
+ Copy_emitter(node, be_get_Copy_op(node));
}
-static void emit_be_CopyKeep(ia32_emit_env_t *env, const ir_node *node) {
- Copy_emitter(env, node, be_get_CopyKeep_op(node));
+static void emit_be_CopyKeep(const ir_node *node)
+{
+ Copy_emitter(node, be_get_CopyKeep_op(node));
}
/**
* Emits code for exchange.
*/
-static void emit_be_Perm(ia32_emit_env_t *env, const ir_node *node) {
- const arch_register_t *in1, *in2;
- const arch_register_class_t *cls1, *cls2;
+static void emit_be_Perm(const ir_node *node)
+{
+ const arch_register_t *in0, *in1;
+ const arch_register_class_t *cls0, *cls1;
- in1 = arch_get_irn_register(env->arch_env, get_irn_n(node, 0));
- in2 = arch_get_irn_register(env->arch_env, get_irn_n(node, 1));
+ in0 = arch_get_irn_register(arch_env, get_irn_n(node, 0));
+ in1 = arch_get_irn_register(arch_env, get_irn_n(node, 1));
+ cls0 = arch_register_get_class(in0);
cls1 = arch_register_get_class(in1);
- cls2 = arch_register_get_class(in2);
-
- assert(cls1 == cls2 && "Register class mismatch at Perm");
-
- if (cls1 == &ia32_reg_classes[CLASS_ia32_gp]) {
-#if 0
- if(emit_env->isa->opt_arch == arch_athlon) {
- // xchg commands are Vector path on athlons and therefore stall the DirectPath pipeline
- // it is often beneficial to use the 3 xor trick instead of an xchg
- cmnt_buf[0] = 0;
- lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xor %1S, %2S", irn, irn);
- IA32_DO_EMIT(irn);
- lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xor %2S, %1S", irn, irn);
- IA32_DO_EMIT(irn);
- lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xor %1S, %2S", irn, irn);
- } else {
-#endif
- ia32_emit_cstring(env, "\txchg ");
- ia32_emit_source_register(env, node, 1);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_finish_line(env, node);
-#if 0
- }
-#endif
- } else if (cls1 == &ia32_reg_classes[CLASS_ia32_xmm]) {
- ia32_emit_cstring(env, "\tpxorq ");
- ia32_emit_source_register(env, node, 1);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_finish_line(env, NULL);
-
- ia32_emit_cstring(env, "\tpxorq ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 1);
- ia32_emit_finish_line(env, NULL);
-
- ia32_emit_cstring(env, "\tpxorq ");
- ia32_emit_source_register(env, node, 1);
- ia32_emit_cstring(env, ", ");
- ia32_emit_source_register(env, node, 0);
- ia32_emit_finish_line(env, node);
- } else if (cls1 == &ia32_reg_classes[CLASS_ia32_vfp]) {
+
+ assert(cls0 == cls1 && "Register class mismatch at Perm");
+
+ if (cls0 == &ia32_reg_classes[CLASS_ia32_gp]) {
+ be_emit_cstring("\txchg ");
+ emit_register(in1, NULL);
+ be_emit_cstring(", ");
+ emit_register(in0, NULL);
+ be_emit_finish_line_gas(node);
+ } else if (cls0 == &ia32_reg_classes[CLASS_ia32_xmm]) {
+ be_emit_cstring("\txorpd ");
+ emit_register(in1, NULL);
+ be_emit_cstring(", ");
+ emit_register(in0, NULL);
+ be_emit_finish_line_gas(NULL);
+
+ be_emit_cstring("\txorpd ");
+ emit_register(in0, NULL);
+ be_emit_cstring(", ");
+ emit_register(in1, NULL);
+ be_emit_finish_line_gas(NULL);
+
+ be_emit_cstring("\txorpd ");
+ emit_register(in1, NULL);
+ be_emit_cstring(", ");
+ emit_register(in0, NULL);
+ be_emit_finish_line_gas(node);
+ } else if (cls0 == &ia32_reg_classes[CLASS_ia32_vfp]) {
/* is a NOP */
- } else if (cls1 == &ia32_reg_classes[CLASS_ia32_st]) {
+ } else if (cls0 == &ia32_reg_classes[CLASS_ia32_st]) {
/* is a NOP */
+ } else {
+ panic("unexpected register class in be_Perm (%+F)\n", node);
}
}
/**
* Emits code for Constant loading.
*/
-static void emit_ia32_Const(ia32_emit_env_t *env, const ir_node *node) {
- ir_mode *mode = get_irn_mode(node);
- ia32_immop_type_t imm_tp = get_ia32_immop_type(node);
-
- if (imm_tp == ia32_ImmSymConst) {
- ia32_emit_cstring(env, "\tmovl $");
- ia32_emit_immediate(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
- } else {
- tarval *tv = get_ia32_Immop_tarval(node);
- assert(mode == mode_Iu);
- /* beware: in some rare cases mode is mode_b which has no tarval_null() */
- if (tarval_is_null(tv)) {
- if (env->isa->opt_arch == arch_pentium_4) {
- /* P4 prefers sub r, r, others xor r, r */
- ia32_emit_cstring(env, "\tsubl ");
- } else {
- ia32_emit_cstring(env, "\txorl ");
- }
- ia32_emit_dest_register(env, node, 0);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
- } else {
- ia32_emit_cstring(env, "\tmovl $");
- ia32_emit_immediate(env, node);
- ia32_emit_cstring(env, ", ");
- ia32_emit_dest_register(env, node, 0);
- }
- }
- ia32_emit_finish_line(env, node);
+static void emit_ia32_Const(const ir_node *node)
+{
+ be_emit_cstring("\tmovl ");
+ emit_ia32_Immediate(node);
+ be_emit_cstring(", ");
+ ia32_emit_dest_register(node, 0);
+
+ be_emit_finish_line_gas(node);
}
/**
* Emits code to load the TLS base
*/
-static void emit_ia32_LdTls(ia32_emit_env_t *env, const ir_node *node) {
- ia32_emit_cstring(env, "\tmovl %gs:0, ");
- ia32_emit_dest_register(env, node, 0);
- ia32_emit_finish_line(env, node);
+static void emit_ia32_LdTls(const ir_node *node)
+{
+ be_emit_cstring("\tmovl %gs:0, ");
+ ia32_emit_dest_register(node, 0);
+ be_emit_finish_line_gas(node);
+}
+
+/* helper function for emit_ia32_Minus64Bit */
+static void emit_mov(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
+{
+ be_emit_cstring("\tmovl ");
+ emit_register(src, NULL);
+ be_emit_cstring(", ");
+ emit_register(dst, NULL);
+ be_emit_finish_line_gas(node);
+}
+
+/* helper function for emit_ia32_Minus64Bit */
+static void emit_neg(const ir_node* node, const arch_register_t *reg)
+{
+ be_emit_cstring("\tnegl ");
+ emit_register(reg, NULL);
+ be_emit_finish_line_gas(node);
+}
+
+/* helper function for emit_ia32_Minus64Bit */
+static void emit_sbb0(const ir_node* node, const arch_register_t *reg)
+{
+ be_emit_cstring("\tsbbl $0, ");
+ emit_register(reg, NULL);
+ be_emit_finish_line_gas(node);
+}
+
+/* helper function for emit_ia32_Minus64Bit */
+static void emit_sbb(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
+{
+ be_emit_cstring("\tsbbl ");
+ emit_register(src, NULL);
+ be_emit_cstring(", ");
+ emit_register(dst, NULL);
+ be_emit_finish_line_gas(node);
+}
+
+/* helper function for emit_ia32_Minus64Bit */
+static void emit_xchg(const ir_node* node, const arch_register_t *src, const arch_register_t *dst)
+{
+ be_emit_cstring("\txchgl ");
+ emit_register(src, NULL);
+ be_emit_cstring(", ");
+ emit_register(dst, NULL);
+ be_emit_finish_line_gas(node);
+}
+
+/* helper function for emit_ia32_Minus64Bit */
+static void emit_zero(const ir_node* node, const arch_register_t *reg)
+{
+ be_emit_cstring("\txorl ");
+ emit_register(reg, NULL);
+ be_emit_cstring(", ");
+ emit_register(reg, NULL);
+ be_emit_finish_line_gas(node);
+}
+
+static void emit_ia32_Minus64Bit(const ir_node *node)
+{
+ const arch_register_t *in_lo = get_in_reg(node, 0);
+ const arch_register_t *in_hi = get_in_reg(node, 1);
+ const arch_register_t *out_lo = get_out_reg(node, 0);
+ const arch_register_t *out_hi = get_out_reg(node, 1);
+
+ if (out_lo == in_lo) {
+ if (out_hi != in_hi) {
+ /* a -> a, b -> d */
+ goto zero_neg;
+ } else {
+ /* a -> a, b -> b */
+ goto normal_neg;
+ }
+ } else if (out_lo == in_hi) {
+ if (out_hi == in_lo) {
+ /* a -> b, b -> a */
+ emit_xchg(node, in_lo, in_hi);
+ goto normal_neg;
+ } else {
+ /* a -> b, b -> d */
+ emit_mov(node, in_hi, out_hi);
+ emit_mov(node, in_lo, out_lo);
+ goto normal_neg;
+ }
+ } else {
+ if (out_hi == in_lo) {
+ /* a -> c, b -> a */
+ emit_mov(node, in_lo, out_lo);
+ goto zero_neg;
+ } else if (out_hi == in_hi) {
+ /* a -> c, b -> b */
+ emit_mov(node, in_lo, out_lo);
+ goto normal_neg;
+ } else {
+ /* a -> c, b -> d */
+ emit_mov(node, in_lo, out_lo);
+ goto zero_neg;
+ }
+ }
+
+normal_neg:
+ emit_neg( node, out_hi);
+ emit_neg( node, out_lo);
+ emit_sbb0(node, out_hi);
+ return;
+
+zero_neg:
+ emit_zero(node, out_hi);
+ emit_neg( node, out_lo);
+ emit_sbb( node, in_hi, out_hi);
}
-static void emit_be_Return(ia32_emit_env_t *env, const ir_node *node) {
- ia32_emit_cstring(env, "\tret");
- ia32_emit_finish_line(env, node);
+static void emit_be_Return(const ir_node *node)
+{
+ unsigned pop;
+ be_emit_cstring("\tret");
+
+ pop = be_Return_get_pop(node);
+ if(pop > 0) {
+ be_emit_irprintf(" $%d", pop);
+ }
+ be_emit_finish_line_gas(node);
}
-static void emit_Nothing(ia32_emit_env_t *env, const ir_node *node) {
+static void emit_Nothing(const ir_node *node)
+{
+ (void) node;
}
* Enters the emitter functions for handled nodes into the generic
* pointer of an opcode.
*/
-static void ia32_register_emitters(void) {
+static
+void ia32_register_emitters(void) {
#define IA32_EMIT2(a,b) op_ia32_##a->ops.generic = (op_func)emit_ia32_##b
#define IA32_EMIT(a) IA32_EMIT2(a,a)
ia32_register_spec_emitters();
/* other ia32 emitter functions */
- IA32_EMIT(CondJmp);
- IA32_EMIT(TestJmp);
- IA32_EMIT(CJmp);
- IA32_EMIT(CJmpAM);
- IA32_EMIT(CmpCMov);
- IA32_EMIT(PsiCondCMov);
- IA32_EMIT(CmpSet);
- IA32_EMIT(PsiCondSet);
+ IA32_EMIT(Asm);
+ IA32_EMIT(CMov);
IA32_EMIT(SwitchJmp);
IA32_EMIT(CopyB);
IA32_EMIT(CopyB_i);
IA32_EMIT(Conv_FP2I);
IA32_EMIT(Conv_FP2FP);
IA32_EMIT(Conv_I2I);
- IA32_EMIT(Conv_I2I8Bit);
+ IA32_EMIT2(Conv_I2I8Bit, Conv_I2I);
IA32_EMIT(Const);
IA32_EMIT(LdTls);
- IA32_EMIT(xCmp);
- IA32_EMIT(xCmpSet);
- IA32_EMIT(xCmpCMov);
- IA32_EMIT(xCondJmp);
- IA32_EMIT2(fcomJmp, x87CondJmp);
- IA32_EMIT2(fcompJmp, x87CondJmp);
- IA32_EMIT2(fcomppJmp, x87CondJmp);
- IA32_EMIT2(fcomrJmp, x87CondJmp);
- IA32_EMIT2(fcomrpJmp, x87CondJmp);
- IA32_EMIT2(fcomrppJmp, x87CondJmp);
+ IA32_EMIT(Minus64Bit);
+ IA32_EMIT(Jcc);
/* benode emitter */
BE_EMIT(Call);
BE_EMIT(IncSP);
- BE_EMIT(SetSP);
BE_EMIT(Copy);
BE_EMIT(CopyKeep);
BE_EMIT(Perm);
/**
* Emit the debug support for node node.
*/
-static void ia32_emit_dbg(ia32_emit_env_t *env, const ir_node *node) {
+static void ia32_emit_dbg(const ir_node *node)
+{
dbg_info *db = get_irn_dbg_info(node);
unsigned lineno;
const char *fname = be_retrieve_dbg_info(db, &lineno);
- if (! env->cg->birg->main_env->options->stabs_debug_support)
+ if (! cg->birg->main_env->options->stabs_debug_support)
return;
if (fname) {
if (last_name != fname) {
last_line = -1;
- be_dbg_include_begin(env->cg->birg->main_env->db_handle, fname);
+ be_dbg_include_begin(cg->birg->main_env->db_handle, fname);
last_name = fname;
}
if (last_line != lineno) {
char name[64];
- FILE *F = env->out;
snprintf(name, sizeof(name), ".LM%u", ++num);
last_line = lineno;
- be_dbg_line(env->cg->birg->main_env->db_handle, lineno, name);
- fprintf(F, "%s:\n", name);
+ be_dbg_line(cg->birg->main_env->db_handle, lineno, name);
+ be_emit_string(name);
+ be_emit_cstring(":\n");
+ be_emit_write_line();
}
}
}
-typedef void (*emit_func_ptr) (ia32_emit_env_t *, const ir_node *);
+typedef void (*emit_func_ptr) (const ir_node *);
/**
* Emits code for a node.
*/
-static void ia32_emit_node(ia32_emit_env_t *env, const ir_node *node) {
+static void ia32_emit_node(const ir_node *node)
+{
ir_op *op = get_irn_op(node);
- DEBUG_ONLY(firm_dbg_module_t *mod = env->mod;)
- DBG((mod, LEVEL_1, "emitting code for %+F\n", node));
+ DBG((dbg, LEVEL_1, "emitting code for %+F\n", node));
if (op->ops.generic) {
emit_func_ptr func = (emit_func_ptr) op->ops.generic;
- ia32_emit_dbg(env, node);
- (*func) (env, node);
+ ia32_emit_dbg(node);
+ (*func) (node);
} else {
- emit_Nothing(env, node);
- ir_fprintf(stderr, "Warning: No emit handler for node %+F (%+G)\n", node, node);
+ emit_Nothing(node);
+ ir_fprintf(stderr, "Error: No emit handler for node %+F (%+G, graph %+F)\n", node, node, current_ir_graph);
+ abort();
}
}
/**
* Emits gas alignment directives
*/
-static void ia32_emit_alignment(ia32_emit_env_t *env, unsigned align, unsigned skip) {
- ia32_emit_cstring(env, "\t.p2align ");
- ia32_emit_irprintf(env, "%u,,%u\n", align, skip);
- ia32_write_line(env);
+static void ia32_emit_alignment(unsigned align, unsigned skip)
+{
+ be_emit_cstring("\t.p2align ");
+ be_emit_irprintf("%u,,%u\n", align, skip);
+ be_emit_write_line();
}
/**
* Emits gas alignment directives for Functions depended on cpu architecture.
*/
-static void ia32_emit_align_func(ia32_emit_env_t *env, cpu_support cpu) {
+static void ia32_emit_align_func(cpu_support cpu)
+{
unsigned align;
unsigned maximum_skip;
align = 4;
}
maximum_skip = (1 << align) - 1;
- ia32_emit_alignment(env, align, maximum_skip);
+ ia32_emit_alignment(align, maximum_skip);
}
/**
* Emits gas alignment directives for Labels depended on cpu architecture.
*/
-static void ia32_emit_align_label(ia32_emit_env_t *env, cpu_support cpu) {
+static void ia32_emit_align_label(cpu_support cpu)
+{
unsigned align; unsigned maximum_skip;
switch (cpu) {
align = 4;
}
maximum_skip = (1 << align) - 1;
- ia32_emit_alignment(env, align, maximum_skip);
+ ia32_emit_alignment(align, maximum_skip);
}
-static int is_first_loop_block(ia32_emit_env_t *env, ir_node *block, ir_node *prev_block) {
- ir_exec_freq *exec_freq = env->cg->birg->exec_freq;
- double block_freq, prev_freq;
+/**
+ * Test wether a block should be aligned.
+ * For cpus in the P4/Athlon class it is usefull to align jump labels to
+ * 16 bytes. However we should only do that if the alignment nops before the
+ * label aren't executed more often than we have jumps to the label.
+ */
+static int should_align_block(ir_node *block, ir_node *prev)
+{
static const double DELTA = .0001;
- cpu_support cpu = env->isa->opt_arch;
+ ir_exec_freq *exec_freq = cg->birg->exec_freq;
+ double block_freq;
+ double prev_freq = 0; /**< execfreq of the fallthrough block */
+ double jmp_freq = 0; /**< execfreq of all non-fallthrough blocks */
+ cpu_support cpu = isa->opt_arch;
+ int i, n_cfgpreds;
if(exec_freq == NULL)
return 0;
return 0;
block_freq = get_block_execfreq(exec_freq, block);
- prev_freq = get_block_execfreq(exec_freq, prev_block);
-
- if(block_freq < DELTA || prev_freq < DELTA)
+ if(block_freq < DELTA)
return 0;
- block_freq /= prev_freq;
+ n_cfgpreds = get_Block_n_cfgpreds(block);
+ for(i = 0; i < n_cfgpreds; ++i) {
+ ir_node *pred = get_Block_cfgpred_block(block, i);
+ double pred_freq = get_block_execfreq(exec_freq, pred);
+
+ if(pred == prev) {
+ prev_freq += pred_freq;
+ } else {
+ jmp_freq += pred_freq;
+ }
+ }
+
+ if(prev_freq < DELTA && !(jmp_freq < DELTA))
+ return 1;
+
+ jmp_freq /= prev_freq;
switch (cpu) {
case arch_athlon:
case arch_athlon_64:
case arch_k6:
- return block_freq > 3;
+ return jmp_freq > 3;
default:
- break;
+ return jmp_freq > 2;
}
-
- return block_freq > 2;
}
-/**
- * Walks over the nodes in a block connected by scheduling edges
- * and emits code for each node.
- */
-static void ia32_gen_block(ia32_emit_env_t *env, ir_node *block, ir_node *last_block) {
- ir_graph *irg = get_irn_irg(block);
- ir_node *start_block = get_irg_start_block(irg);
- int need_label = 1;
- const ir_node *node;
- int i;
-
- assert(is_Block(block));
-
- if (block == start_block)
- need_label = 0;
-
- if (need_label && get_irn_arity(block) == 1) {
- ir_node *pred_block = get_Block_cfgpred_block(block, 0);
-
- if (pred_block == last_block && get_irn_n_edges_kind(pred_block, EDGE_KIND_BLOCK) <= 2)
- need_label = 0;
- }
+static void ia32_emit_block_header(ir_node *block, ir_node *prev)
+{
+ int n_cfgpreds;
+ int need_label;
+ int i, arity;
+ ir_exec_freq *exec_freq = cg->birg->exec_freq;
- /* special case: if one of our cfg preds is a switch-jmp we need a label, */
- /* otherwise there might be jump table entries jumping to */
- /* non-existent (omitted) labels */
- for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
- ir_node *pred = get_Block_cfgpred(block, i);
+ n_cfgpreds = get_Block_n_cfgpreds(block);
+ need_label = (n_cfgpreds != 0);
- if (is_Proj(pred)) {
- assert(get_irn_mode(pred) == mode_X);
- if (is_ia32_SwitchJmp(get_Proj_pred(pred))) {
- need_label = 1;
- break;
- }
- }
+ if (should_align_block(block, prev)) {
+ assert(need_label);
+ ia32_emit_align_label(isa->opt_arch);
}
- if (need_label) {
- int i, arity;
- int align = 1;
- ir_exec_freq *exec_freq = env->cg->birg->exec_freq;
-
- /* align the loop headers */
- if (! is_first_loop_block(env, block, last_block)) {
- /* align blocks where the previous block has no fallthrough */
- arity = get_irn_arity(block);
-
- for (i = 0; i < arity; ++i) {
- ir_node *predblock = get_Block_cfgpred_block(block, i);
+ if(need_label) {
+ ia32_emit_block_name(block);
+ be_emit_char(':');
- if (predblock == last_block) {
- align = 0;
- break;
- }
- }
- }
-
- if (align)
- ia32_emit_align_label(env, env->isa->opt_arch);
-
- ia32_emit_cstring(env, BLOCK_PREFIX);
- ia32_emit_irprintf(env, "%d:", get_irn_node_nr(block));
- ia32_pad_comment(env);
- ia32_emit_cstring(env, " /* preds:");
+ be_emit_pad_comment();
+ be_emit_cstring(" /* preds:");
/* emit list of pred blocks in comment */
arity = get_irn_arity(block);
for (i = 0; i < arity; ++i) {
ir_node *predblock = get_Block_cfgpred_block(block, i);
- ia32_emit_irprintf(env, " %d", get_irn_node_nr(predblock));
+ be_emit_irprintf(" %d", get_irn_node_nr(predblock));
}
-
- if (exec_freq != NULL) {
- ia32_emit_irprintf(env, " freq: %f", get_block_execfreq(exec_freq, block));
- }
- ia32_emit_cstring(env, " */\n");
- ia32_write_line(env);
+ } else {
+ be_emit_cstring("\t/* ");
+ ia32_emit_block_name(block);
+ be_emit_cstring(": ");
+ }
+ if (exec_freq != NULL) {
+ be_emit_irprintf(" freq: %f",
+ get_block_execfreq(exec_freq, block));
}
+ be_emit_cstring(" */\n");
+ be_emit_write_line();
+}
+
+/**
+ * Walks over the nodes in a block connected by scheduling edges
+ * and emits code for each node.
+ */
+static void ia32_gen_block(ir_node *block, ir_node *last_block)
+{
+ const ir_node *node;
+
+ ia32_emit_block_header(block, last_block);
/* emit the contents of the block */
- ia32_emit_dbg(env, block);
+ ia32_emit_dbg(block);
sched_foreach(block, node) {
- ia32_emit_node(env, node);
+ ia32_emit_node(node);
}
}
/**
* Emits code for function start.
*/
-static void ia32_emit_func_prolog(ia32_emit_env_t *env, ir_graph *irg) {
- FILE *F = env->out;
+static void ia32_emit_func_prolog(ir_graph *irg)
+{
ir_entity *irg_ent = get_irg_entity(irg);
const char *irg_name = get_entity_ld_name(irg_ent);
- cpu_support cpu = env->isa->opt_arch;
- const be_irg_t *birg = env->cg->birg;
+ cpu_support cpu = isa->opt_arch;
+ const be_irg_t *birg = cg->birg;
- fprintf(F, "\n");
- ia32_switch_section(F, SECTION_TEXT);
+ be_emit_write_line();
+ be_gas_emit_switch_section(GAS_SECTION_TEXT);
be_dbg_method_begin(birg->main_env->db_handle, irg_ent, be_abi_get_stack_layout(birg->abi));
- ia32_emit_align_func(env, cpu);
+ ia32_emit_align_func(cpu);
if (get_entity_visibility(irg_ent) == visibility_external_visible) {
- fprintf(F, ".globl %s\n", irg_name);
+ be_emit_cstring(".global ");
+ be_emit_string(irg_name);
+ be_emit_char('\n');
+ be_emit_write_line();
}
- ia32_dump_function_object(F, irg_name);
- fprintf(F, "%s:\n", irg_name);
+ ia32_emit_function_object(irg_name);
+ be_emit_string(irg_name);
+ be_emit_cstring(":\n");
+ be_emit_write_line();
}
/**
* Emits code for function end
*/
-static void ia32_emit_func_epilog(ia32_emit_env_t *env, ir_graph *irg) {
- const char *irg_name = get_entity_ld_name(get_irg_entity(irg));
- const be_irg_t *birg = env->cg->birg;
- FILE *F = env->out;
+static void ia32_emit_func_epilog(ir_graph *irg)
+{
+ const char *irg_name = get_entity_ld_name(get_irg_entity(irg));
+ const be_irg_t *birg = cg->birg;
- ia32_dump_function_size(F, irg_name);
+ ia32_emit_function_size(irg_name);
be_dbg_method_end(birg->main_env->db_handle);
- fprintf(F, "\n");
+ be_emit_char('\n');
+ be_emit_write_line();
}
/**
* Block-walker:
* Sets labels for control flow nodes (jump target)
*/
-static void ia32_gen_labels(ir_node *block, void *data) {
+static void ia32_gen_labels(ir_node *block, void *data)
+{
ir_node *pred;
int n = get_Block_n_cfgpreds(block);
+ (void) data;
for (n--; n >= 0; n--) {
pred = get_Block_cfgpred(block, n);
}
}
+/**
+ * Emit an exception label if the current instruction can fail.
+ */
+void ia32_emit_exc_label(const ir_node *node)
+{
+ if (get_ia32_exc_label(node)) {
+ be_emit_irprintf(".EXL%u\n", 0);
+ be_emit_write_line();
+ }
+}
+
/**
* Main driver. Emits the code for one routine.
*/
-void ia32_gen_routine(ia32_code_gen_t *cg, FILE *F, ir_graph *irg) {
- ia32_emit_env_t env;
+void ia32_gen_routine(ia32_code_gen_t *ia32_cg, ir_graph *irg)
+{
ir_node *block;
ir_node *last_block = NULL;
int i, n;
- struct obstack obst;
-
- obstack_init(&obst);
- env.out = F;
- env.arch_env = cg->arch_env;
- env.cg = cg;
- env.isa = (ia32_isa_t *)cg->arch_env->isa;
- env.obst = &obst;
- env.linelength = 0;
- FIRM_DBG_REGISTER(env.mod, "firm.be.ia32.emitter");
-
- /* set the global arch_env (needed by print hooks) */
+ cg = ia32_cg;
+ isa = (const ia32_isa_t*) cg->arch_env->isa;
arch_env = cg->arch_env;
ia32_register_emitters();
- ia32_emit_func_prolog(&env, irg);
- irg_block_walk_graph(irg, ia32_gen_labels, NULL, &env);
+ ia32_emit_func_prolog(irg);
+ irg_block_walk_graph(irg, ia32_gen_labels, NULL, NULL);
n = ARR_LEN(cg->blk_sched);
for (i = 0; i < n;) {
/* set here the link. the emitter expects to find the next block here */
set_irn_link(block, next_bl);
- ia32_gen_block(&env, block, last_block);
+ ia32_gen_block(block, last_block);
last_block = block;
}
- ia32_emit_func_epilog(&env, irg);
+ ia32_emit_func_epilog(irg);
+}
+
+void ia32_init_emitter(void)
+{
+ FIRM_DBG_REGISTER(dbg, "firm.be.ia32.emitter");
}