+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <limits.h>
#include "tv.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irop_t.h"
+#include "irargs_t.h"
#include "../besched.h"
#define SNPRINTF_BUF_LEN 128
-static set *cur_reg_set = NULL;
+static const arch_env_t *arch_env = NULL;
/*************************************************************
*/
char *node_offset_to_str(ir_node *n) {
char *buf;
- tarval *tv = get_ia32_offs(n);
+ tarval *tv = get_ia32_am_offs(n);
if (tv) {
buf = malloc(SNPRINTF_BUF_LEN);
/**
- * 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;
const arch_register_t *reg = NULL;
- const arch_register_t **slots;
assert(get_irn_arity(irn) > pos && "Invalid IN position");
in register we need. */
op = get_irn_n(irn, pos);
- if (is_Proj(op)) {
- pos = (int)translate_proj_pos(op);
- while(is_Proj(op))
- op = get_Proj_pred(op);
- }
+ reg = arch_get_irn_register(arch_env, op);
+
+ 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 (is_ia32_irn(op)) {
- /* The operator is an ia32 node: this node has only one out */
- slots = get_ia32_slots(op);
- reg = slots[0];
+ 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 {
- /* The operator is not an ia32 node: check for Phi or Proj */
- if (is_Phi(op)) {
- /* Phi's getting register assigned */
- reg = ia32_get_firm_reg(NULL, op, cur_reg_set);
- assert(reg && "No register assigned to Phi node");
- }
- else {
- assert(0 && "Unsupported node for IN register");
+ 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);
}
/**
* Returns the name of the in register at position pos.
*/
-const char *get_ia32_in_reg_name(ir_node *irn, int pos) {
+const char *get_ia32_reg_name(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->name;
+ if (in_out == 1) {
+ 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);
+ if (occ->conversion == 'S') {
+ buf = get_ia32_reg_name(X, nr, 1);
}
- else { /* 'd' */
- buf = get_ia32_out_reg_name(X, nr);
+ else { /* 'D' */
+ buf = get_ia32_reg_name(X, nr, 0);
}
+ 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') {
+ if (occ->conversion == 'C') {
buf = node_const_to_str(X);
}
- else { /* 'o' */
+ else { /* 'O' */
buf = node_offset_to_str(X);
}
* 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 = firm_get_arg_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 (get_ia32_reg_nr(n, 0, 1) != get_ia32_reg_nr(n, 0, 0)) {
+ if (get_irn_arity(n) > 1 && get_ia32_reg_nr(n, 1, 1) == get_ia32_reg_nr(n, 0, 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);
+ /* we only need to exchange 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);
+ lc_efprintf(ia32_get_arg_env(), F, "\tmovl %1S, %1D\t\t\t/* src -> dest for 2 address code */\n", n, n);
}
}
}
return buf;
}
+
+/*************************************************
+ * _ _ _
+ * (_) | | |
+ * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
+ * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
+ * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
+ * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
+ *
+ *************************************************/
+
/*
* coding of conditions
*/
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);
}
+
+
+/*********************************************************
+ * _ _ _
+ * (_) | (_)
+ * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
+ * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
+ * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
+ * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
+ * _/ | | |
+ * |__/ |_|
+ *********************************************************/
+
+/* jump table entry (target and corresponding number) */
typedef struct _branch_t {
ir_node *target;
int value;
} branch_t;
+/* jump table for switch generation */
typedef struct _jmp_tbl_t {
- ir_node *defProj;
- int min_value;
- int max_value;
- int num_branches;
- char *label;
- branch_t *branches;
+ ir_node *defProj; /**< default target */
+ int min_value; /**< smallest switch case */
+ int max_value; /**< largest switch case */
+ int 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 */
+/**
+ * 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) {
branch_t *b1 = (branch_t *)a;
branch_t *b2 = (branch_t *)b;
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)));
+ tbl.branches = calloc(tbl.num_branches, sizeof(tbl.branches[0]));
tbl.min_value = INT_MAX;
tbl.max_value = INT_MIN;
}
/* sort the branches by their number */
- qsort(tbl.branches, tbl.num_branches, sizeof(*(tbl.branches)), ia32_cmp_branch_t);
+ qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
/* two-complement's magic make this work without overflow */
interval = tbl.max_value - tbl.min_value;
/* 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));
}
fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
}
+ if (tbl.label)
+ free(tbl.label);
if (tbl.branches)
free(tbl.branches);
}
ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
}
+
+
+/****************************
+ * _
+ * (_)
+ * _ __ _ __ ___ _ ___
+ * | '_ \| '__/ _ \| |/ __|
+ * | |_) | | | (_) | |\__ \
+ * | .__/|_| \___/| ||___/
+ * | | _/ |
+ * |_| |__/
+ ****************************/
+
/**
* Emits code for a proj -> node
*/
}
}
+
+
+/***********************************************************************************
+ * _ __ _
+ * (_) / _| | |
+ * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
+ * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
+ * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
+ * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
+ *
+ ***********************************************************************************/
+
/**
- * Main emitting function
+ * Emits code for a node.
*/
void ia32_emit_node(ir_node *irn, void *env) {
emit_env_t *emit_env = env;
#define EMIT(a) if (get_irn_opcode(irn) == iro_##a) { emit_##a(irn, emit_env); return; }
/* generated int emitter functions */
+ IA32_EMIT(Copy);
+ IA32_EMIT(Perm);
+
IA32_EMIT(Const);
IA32_EMIT(Add);
IA32_EMIT(Mul);
IA32_EMIT(Mul_i);
- IA32_EMIT(Mulh);
- IA32_EMIT(Mulh_i);
IA32_EMIT(Cltd);
IA32_EMIT(DivMod);
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, set *reg_set) {
+void ia32_gen_routine(FILE *F, ir_graph *irg, const arch_env_t *env) {
emit_env_t emit_env;
- emit_env.mod = firm_dbg_register("be.codegen.ia32");
- emit_env.out = F;
- emit_env.reg_set = reg_set;
+ emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
+ emit_env.out = F;
+ emit_env.arch_env = env;
- cur_reg_set = reg_set;
+ arch_env = 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);
}