+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <limits.h>
+#include "xmalloc.h"
#include "tv.h"
#include "iredges.h"
#include "debug.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irop_t.h"
+#include "irargs_t.h"
#include "../besched.h"
#include "ia32_new_nodes.h"
#include "ia32_map_regs.h"
+#ifdef obstack_chunk_alloc
+# undef obstack_chunk_alloc
+# define obstack_chunk_alloc xmalloc
+#else
+# define obstack_chunk_alloc xmalloc
+# define obstack_chunk_free free
+#endif
+
+extern int obstack_printf(struct obstack *obst, char *fmt, ...);
+
#define SNPRINTF_BUF_LEN 128
static const arch_env_t *arch_env = NULL;
+char *ia32_emit_binop(const ir_node *n) {
+ static char *buf = NULL;
+
+ if (! buf) {
+ buf = xcalloc(1, SNPRINTF_BUF_LEN);
+ }
+ else {
+ memset(buf, 0, SNPRINTF_BUF_LEN);
+ }
+
+ switch(get_ia32_op_type(n)) {
+ case ia32_Normal:
+ if (get_ia32_cnst(n)) {
+ lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, get_ia32_cnst(n));
+ }
+ else {
+ lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %4S", n, n);
+ }
+ break;
+ case ia32_am_Source:
+ lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n));
+ break;
+ case ia32_am_Dest:
+ lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %4S", ia32_emit_am(n), n);
+ break;
+ default:
+ assert(0 && "unsupported op type");
+ }
+
+ return buf;
+}
+
+char *ia32_emit_unop(const ir_node *n) {
+ static char *buf = NULL;
+
+ if (! buf) {
+ buf = xcalloc(1, SNPRINTF_BUF_LEN);
+ }
+ else {
+ memset(buf, 0, SNPRINTF_BUF_LEN);
+ }
+
+ switch(get_ia32_op_type(n)) {
+ case ia32_Normal:
+ lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
+ break;
+ case ia32_am_Dest:
+ snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
+ break;
+ default:
+ assert(0 && "unsupported op type");
+ }
+
+ return buf;
+}
+
+char *ia32_emit_am(const ir_node *n) {
+ ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
+ int had_output = 0;
+ char *s;
+ int size;
+ static struct obstack *obst = NULL;
+
+ if (! obst) {
+ obst = xcalloc(1, sizeof(*obst));
+ }
+ else {
+ obstack_free(obst, NULL);
+ }
+
+ /* obstack_free with NULL results in an uninitialized obstack */
+ obstack_init(obst);
+
+ obstack_printf(obst, "[");
+
+ if (am_flav & ia32_B) {
+ lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
+ had_output = 1;
+ }
+
+ if (am_flav & ia32_I) {
+ if (had_output) {
+ obstack_printf(obst, "+");
+ }
+
+ lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
+
+ if (am_flav & ia32_S) {
+ obstack_printf(obst, "*%d", get_ia32_am_scale(n));
+ }
+
+ had_output = 1;
+ }
+
+ if (am_flav & ia32_O) {
+ obstack_printf(obst, get_ia32_am_offs(n));
+ }
+
+ obstack_printf(obst, "] ");
+
+ size = obstack_object_size(obst);
+ s = obstack_finish(obst);
+ s[size - 1] = '\0';
+
+ return s;
+}
/*************************************************************
* _ _ __ _ _
* |_| |_|
*************************************************************/
-/**
- * Return node's tarval as string.
- */
-const char *node_const_to_str(ir_node *n) {
- char *buf;
- tarval *tv = get_ia32_Immop_tarval(n);
-
- if (tv) {
- buf = malloc(SNPRINTF_BUF_LEN);
- tarval_snprintf(buf, SNPRINTF_BUF_LEN, tv);
- return buf;
- }
- else if (get_ia32_old_ir(n)) {
- return get_sc_name(get_ia32_old_ir(n));
- }
- else
- return "0";
-}
-
-/**
- * Returns node's offset as string.
- */
-char *node_offset_to_str(ir_node *n) {
- char *buf;
- tarval *tv = get_ia32_offs(n);
-
- if (tv) {
- buf = malloc(SNPRINTF_BUF_LEN);
- tarval_snprintf(buf, SNPRINTF_BUF_LEN, tv);
- return buf;
- }
- else
- return "";
-}
-
/* We always pass the ir_node which is a pointer. */
static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
return lc_arg_type_ptr;
/**
- * Returns the register at in position pos. If the IN node is not an
- * ia32 node, we check for phi and proj.
+ * Returns the register at in position pos.
*/
static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
ir_node *op;
reg = arch_get_irn_register(arch_env, op);
- assert(reg && "could not get in register");
+ assert(reg && "no in register found");
+ return reg;
+}
+
+/**
+ * Returns the register at out position pos.
+ */
+static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
+ ir_node *proj;
+ const arch_register_t *reg = NULL;
+
+ assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
+
+ /* 1st case: irn is not of mode_T, so it has only */
+ /* one OUT register -> good */
+ /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
+ /* Proj with the corresponding projnum for the register */
+
+ if (get_irn_mode(irn) != mode_T) {
+ reg = arch_get_irn_register(arch_env, irn);
+ }
+ else if (is_ia32_irn(irn)) {
+ reg = get_ia32_out_reg(irn, pos);
+ }
+ else {
+ const ir_edge_t *edge;
+
+ foreach_out_edge(irn, edge) {
+ proj = get_edge_src_irn(edge);
+ assert(is_Proj(proj) && "non-Proj from mode_T node");
+ if (get_Proj_proj(proj) == pos) {
+ reg = arch_get_irn_register(arch_env, proj);
+ break;
+ }
+ }
+ }
+ assert(reg && "no out register found");
return reg;
}
/**
* Returns the number of the in register at position pos.
*/
-int get_ia32_in_regnr(ir_node *irn, int pos) {
+int get_ia32_reg_nr(ir_node *irn, int pos, int in_out) {
const arch_register_t *reg;
- reg = get_in_reg(irn, pos);
- assert(reg && "no in register");
- return reg->index;
+ if (in_out == 1) {
+ reg = get_in_reg(irn, pos);
+ }
+ else {
+ reg = get_out_reg(irn, pos);
+ }
+
+ return arch_register_get_index(reg);
}
+enum io_direction {
+ IN_REG,
+ OUT_REG
+};
+
/**
* Returns the name of the in register at position pos.
*/
-const char *get_ia32_in_reg_name(ir_node *irn, int pos) {
+static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
const arch_register_t *reg;
- reg = get_in_reg(irn, pos);
- assert(reg && "no in register");
- return reg->name;
+ if (in_out == IN_REG) {
+ reg = get_in_reg(irn, pos);
+ }
+ else {
+ reg = get_out_reg(irn, pos);
+ }
+
+ return arch_register_get_name(reg);
}
/**
if (!X)
return lc_arg_append(app, occ, "(null)", 6);
- if (occ->conversion == 's') {
- buf = get_ia32_in_reg_name(X, nr);
- }
- else { /* 'd' */
- buf = get_ia32_out_reg_name(X, nr);
- }
+ buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
+ lc_appendable_chadd(app, '%');
return lc_arg_append(app, occ, buf, strlen(buf));
}
if (!X)
return lc_arg_append(app, occ, "(null)", 6);
- if (occ->conversion == 'c') {
- buf = node_const_to_str(X);
+ if (occ->conversion == 'C') {
+ buf = get_ia32_cnst(X);
}
- else { /* 'o' */
- buf = node_offset_to_str(X);
+ else { /* 'O' */
+ buf = get_ia32_am_offs(X);
}
return lc_arg_append(app, occ, buf, strlen(buf));
* We use the firm environment with some additional handlers.
*/
const lc_arg_env_t *ia32_get_arg_env(void) {
- static lc_arg_env_t *env = NULL;
-
- static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
- static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
- static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
-
- if(env == NULL) {
- env = lc_arg_new_env();
-
- lc_arg_register(env, "ia32:sreg", 's', &ia32_reg_handler);
- lc_arg_register(env, "ia32:dreg", 'd', &ia32_reg_handler);
- lc_arg_register(env, "ia32:cnst", 'c', &ia32_const_handler);
- lc_arg_register(env, "ia32:offs", 'o', &ia32_const_handler);
- lc_arg_register(env, "ia32:mode", 'm', &ia32_mode_handler);
- }
+ static lc_arg_env_t *env = NULL;
+
+ static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
+ static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
+ static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
+
+ if(env == NULL) {
+ /* extend the firm printer */
+ env = firm_get_arg_env();
+ //lc_arg_new_env();
+
+ lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
+ lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
+ lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
+ lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
+ lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
+ }
- return env;
+ return env;
}
-/**
- * For 2-address code we need to make sure the first src reg is equal to dest reg.
- */
-void equalize_dest_src(FILE *F, ir_node *n) {
- if (get_ia32_in_regnr(n, 0) != get_ia32_out_regnr(n, 0)) {
- if (get_irn_arity(n) > 1 && get_ia32_in_regnr(n, 1) == get_ia32_out_regnr(n, 0)) {
- if (! is_op_commutative(get_irn_op(n))) {
- /* we only need to echange for non-commutative ops */
- lc_efprintf(ia32_get_arg_env(), F, "\txchg %1s, %2s\t\t\t/* xchg src1 <-> src2 for 2 address code */\n", n, n);
- }
- }
- else {
- lc_efprintf(ia32_get_arg_env(), F, "\tmovl %1s, %1d\t\t\t/* src -> dest for 2 address code */\n", n, n);
- }
- }
-}
/*
* Add a number to a prefix. This number will not be used a second time.
*/
-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;
static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
FILE *F = env->out;
- lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2s, %1s\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
+ lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
get_irn_n(irn, 0), get_irn_n(irn, 1));
finish_CondJmp(F, irn);
}
void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
FILE *F = env->out;
- lc_efprintf(ia32_get_arg_env(), F, "\tcmp %c, %1s\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
+ lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
finish_CondJmp(F, irn);
}
FILE *F = emit_env->out;
/* fill the table structure */
- tbl.label = malloc(SNPRINTF_BUF_LEN);
+ tbl.label = xmalloc(SNPRINTF_BUF_LEN);
tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
tbl.defProj = NULL;
tbl.num_branches = get_irn_n_edges(irn);
- tbl.branches = calloc(tbl.num_branches, sizeof(tbl.branches[0]));
+ tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
tbl.min_value = INT_MAX;
tbl.max_value = INT_MIN;
/* emit the table */
if (tbl.min_value != 0) {
fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
- lc_efprintf(env, F, "(%1s)\t\t/* first switch value is not 0 */\n", irn);
+ lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
}
else {
fprintf(F, "\tcmpl %lu, ", interval);
- lc_efprintf(env, F, "%1s\t\t\t/* compare for switch */\n", irn);
+ lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
}
fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
if (tbl.num_branches > 1) {
/* create table */
- fprintf(F, "\tjmp *%s", tbl.label);
- lc_efprintf(env, F, "(,%1s,4)\t\t/* get jump table entry as target */\n", irn);
+ //fprintf(F, "\tjmp *%s", tbl.label);
+ lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
fprintf(F, "\t.align 4\n");
else { // no jump table
for (i = 0; i < tbl.num_branches; ++i) {
fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
- lc_efprintf(env, F, "%1s", irn);
+ lc_efprintf(env, F, "%1S", irn);
fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
}
void emit_Proj(ir_node *irn, emit_env_t *env) {
ir_node *pred = get_Proj_pred(irn);
- if (get_irn_opcode(pred) == iro_Start) {
+ if (get_irn_op(pred) == op_Start) {
switch(get_Proj_proj(irn)) {
case pn_Start_X_initial_exec:
emit_Jmp(irn, env);
}
}
+/**********************************
+ * _____ ____
+ * / ____| | _ \
+ * | | ___ _ __ _ _| |_) |
+ * | | / _ \| '_ \| | | | _ <
+ * | |___| (_) | |_) | |_| | |_) |
+ * \_____\___/| .__/ \__, |____/
+ * | | __/ |
+ * |_| |___/
+ **********************************/
+
+static void emit_CopyB_prolog(FILE *F, int rem, int size) {
+ fprintf(F, "\t/* memcopy %d bytes*/\n", size);
+ fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
+
+ switch(rem) {
+ case 1:
+ fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
+ break;
+ case 2:
+ fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
+ break;
+ case 3:
+ fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
+ fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
+ break;
+ }
+}
+
+void emit_ia32_CopyB(ir_node *irn, emit_env_t *emit_env) {
+ FILE *F = emit_env->out;
+ tarval *tv = get_ia32_Immop_tarval(irn);
+ int rem = get_tarval_long(tv);
+ int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
+
+ emit_CopyB_prolog(F, rem, size);
+
+ fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
+}
+
+void emit_ia32_CopyB_i(ir_node *irn, emit_env_t *emit_env) {
+ tarval *tv = get_ia32_Immop_tarval(irn);
+ int size = get_tarval_long(tv);
+ FILE *F = emit_env->out;
+
+ emit_CopyB_prolog(F, size & 0x3, size);
+
+ size >>= 2;
+ while (size--) {
+ fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
+ }
+}
+
+/********************
+ * _____ _ _
+ * / ____| | | |
+ * | | __ _| | |
+ * | | / _` | | |
+ * | |___| (_| | | |
+ * \_____\__,_|_|_|
+ *
+ ********************/
+
+void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
+}
+
/***********************************************************************************
IA32_EMIT(Const);
IA32_EMIT(Add);
- IA32_EMIT(Add_i);
IA32_EMIT(Sub);
- IA32_EMIT(Sub_i);
IA32_EMIT(Minus);
IA32_EMIT(Inc);
IA32_EMIT(Dec);
IA32_EMIT(Max);
IA32_EMIT(Min);
+ IA32_EMIT(CMov);
IA32_EMIT(And);
- IA32_EMIT(And_i);
IA32_EMIT(Or);
- IA32_EMIT(Or_i);
IA32_EMIT(Eor);
- IA32_EMIT(Eor_i);
IA32_EMIT(Not);
IA32_EMIT(Shl);
- IA32_EMIT(Shl_i);
IA32_EMIT(Shr);
- IA32_EMIT(Shr_i);
IA32_EMIT(Shrs);
- IA32_EMIT(Shrs_i);
IA32_EMIT(RotL);
- IA32_EMIT(RotL_i);
IA32_EMIT(RotR);
IA32_EMIT(Lea);
- IA32_EMIT(Lea_i);
IA32_EMIT(Mul);
- IA32_EMIT(Mul_i);
- IA32_EMIT(Mulh);
- IA32_EMIT(Mulh_i);
- IA32_EMIT(Cltd);
+ IA32_EMIT(Cdq);
IA32_EMIT(DivMod);
IA32_EMIT(Store);
IA32_EMIT(Load);
+ IA32_EMIT(CopyB);
+ IA32_EMIT(CopyB_i);
+
/* generated floating point emitter */
IA32_EMIT(fConst);
IA32_EMIT(fAdd);
IA32_EMIT(fSub);
- IA32_EMIT(fMinus);
IA32_EMIT(fMul);
IA32_EMIT(fDiv);
/* other emitter functions */
IA32_EMIT(CondJmp);
- IA32_EMIT(CondJmp_i);
IA32_EMIT(SwitchJmp);
+ IA32_EMIT(Call);
EMIT(Jmp);
EMIT(Proj);
void ia32_gen_block(ir_node *block, void *env) {
ir_node *irn;
+ if (! is_Block(block))
+ return;
+
fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
sched_foreach(block, irn) {
ia32_emit_node(irn, env);
/**
* Main driver
*/
-void ia32_gen_routine(FILE *F, ir_graph *irg, const arch_env_t *env) {
+void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
emit_env_t emit_env;
emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
emit_env.out = F;
- emit_env.arch_env = env;
+ emit_env.arch_env = cg->arch_env;
+ emit_env.cg = cg;
- arch_env = env;
+ /* set the global arch_env (needed by print hooks) */
+ arch_env = cg->arch_env;
ia32_emit_start(F, irg);
irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
- irg_block_walk_graph(irg, NULL, ia32_gen_block, &emit_env);
+ irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
ia32_emit_end(F, irg);
}