* Authors:
* Paolo Molaro (lupus@ximian.com)
* Dietmar Maurer (dietmar@ximian.com)
+ * Patrik Torstensson
*
* (C) 2003 Ximian, Inc.
*/
static gint lmf_tls_offset = -1;
+#ifdef PLATFORM_WIN32
+/* Under windows, the default pinvoke calling convention is stdcall */
+#define CALLCONV_IS_STDCALL(call_conv) (((call_conv) == MONO_CALL_STDCALL) || ((call_conv) == MONO_CALL_DEFAULT))
+#else
+#define CALLCONV_IS_STDCALL(call_conv) ((call_conv) == MONO_CALL_STDCALL)
+#endif
+
const char*
mono_arch_regname (int reg) {
switch (reg) {
case X86_EBX: return "%ebx";
case X86_ECX: return "%ecx";
case X86_EDX: return "%edx";
- case X86_ESP: return "%esp";
- case X86_EBP: return "%ebp";
+ case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
case X86_EDI: return "%edi";
case X86_ESI: return "%esi";
}
indent_level += diff;
}
+static gboolean enable_trace = TRUE;
+
static void
enter_method (MonoMethod *method, char *ebp)
{
MonoMethodSignature *sig;
char *fname;
+ if (!enable_trace)
+ return;
+
fname = mono_method_full_name (method, TRUE);
indent (1);
printf ("ENTER: %s(", fname);
char *fname;
va_list ap;
+ if (!enable_trace)
+ return;
+
va_start(ap, method);
fname = mono_method_full_name (method, TRUE);
(ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
g_assert (MONO_VARINFO (cfg, i)->reg == -1);
g_assert (i == vmv->idx);
- vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
+ vars = g_list_prepend (vars, vmv);
}
}
+ vars = mono_varlist_sort (cfg, vars, 0);
+
return vars;
}
/* add the vararg cookie before the non-implicit args */
if (call->signature->call_convention == MONO_CALL_VARARG) {
MonoInst *sig_arg;
+ /* FIXME: Add support for signature tokens to AOT */
+ cfg->disable_aot = TRUE;
MONO_INST_NEW (cfg, arg, OP_OUTARG);
MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
sig_arg->inst_p0 = call->signature;
#define DEBUG(a) if (cfg->verbose_level > 1) a
//#define DEBUG(a)
+
+/*
+ * returns the offset used by spillvar. It allocates a new
+ * spill variable if necessary.
+ */
+static int
+mono_spillvar_offset (MonoCompile *cfg, int spillvar)
+{
+ MonoSpillInfo **si, *info;
+ int i = 0;
+
+ si = &cfg->spill_info;
+
+ while (i <= spillvar) {
+
+ if (!*si) {
+ *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
+ info->next = NULL;
+ cfg->stack_offset -= sizeof (gpointer);
+ info->offset = cfg->stack_offset;
+ }
+
+ if (i == spillvar)
+ return (*si)->offset;
+
+ i++;
+ si = &(*si)->next;
+ }
+
+ g_assert_not_reached ();
+ return 0;
+}
+
+/*
+ * returns the offset used by spillvar. It allocates a new
+ * spill float variable if necessary.
+ * (same as mono_spillvar_offset but for float)
+ */
+static int
+mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
+{
+ MonoSpillInfo **si, *info;
+ int i = 0;
+
+ si = &cfg->spill_info_float;
+
+ while (i <= spillvar) {
+
+ if (!*si) {
+ *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
+ info->next = NULL;
+ cfg->stack_offset -= sizeof (double);
+ info->offset = cfg->stack_offset;
+ }
+
+ if (i == spillvar)
+ return (*si)->offset;
+
+ i++;
+ si = &(*si)->next;
+ }
+
+ g_assert_not_reached ();
+ return 0;
+}
+
+/*
+ * Creates a store for spilled floating point items
+ */
+static MonoInst*
+create_spilled_store_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
+{
+ MonoInst *store;
+ MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
+ store->sreg1 = reg;
+ store->inst_destbasereg = X86_EBP;
+ store->inst_offset = mono_spillvar_offset_float (cfg, spill);
+
+ DEBUG (g_print ("SPILLED FLOAT STORE (%d at 0x%08x(%%sp)) (from %d)\n", spill, store->inst_offset, reg));
+ return store;
+}
+
+/*
+ * Creates a load for spilled floating point items
+ */
+static MonoInst*
+create_spilled_load_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
+{
+ MonoInst *load;
+ MONO_INST_NEW (cfg, load, OP_LOADR8_SPILL_MEMBASE);
+ load->dreg = reg;
+ load->inst_basereg = X86_EBP;
+ load->inst_offset = mono_spillvar_offset_float (cfg, spill);
+
+ DEBUG (g_print ("SPILLED FLOAT LOAD (%d at 0x%08x(%%sp)) (from %d)\n", spill, load->inst_offset, reg));
+ return load;
+}
+
#define reg_is_freeable(r) ((r) >= 0 && (r) <= 7 && X86_IS_CALLEE ((r)))
typedef struct {
int killed_in;
int last_use;
int prev_use;
+ int flags; /* used to track fp spill/load */
} RegTrack;
static const char*const * ins_spec = pentium_desc;
item->data = to_insert;
}
+
#if 0
static int
alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
}
#endif
+/* flags used in reginfo->flags */
+#define MONO_X86_FP_NEEDS_LOAD_SPILL 1
+#define MONO_X86_FP_NEEDS_SPILL 2
+#define MONO_X86_FP_NEEDS_LOAD 4
+
/*#include "cprop.c"*/
/*
InstList *tmp, *reversed = NULL;
const char *spec;
guint32 src1_mask, src2_mask, dest_mask;
+ GList *fspill_list = NULL;
+ int fspill = 0;
if (!bb->code)
return;
rs->next_vireg = bb->max_ireg;
rs->next_vfreg = bb->max_freg;
mono_regstate_assign (rs);
- reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
- reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
+ reginfo = g_malloc0 (sizeof (RegTrack) * rs->next_vireg);
+ reginfof = g_malloc0 (sizeof (RegTrack) * rs->next_vfreg);
rs->ifree_mask = X86_CALLEE_REGS;
ins = bb->code;
/*if (cfg->opt & MONO_OPT_COPYPROP)
local_copy_prop (cfg, ins);*/
-
+
i = 1;
- fpcount = 0; /* FIXME: track fp stack utilization */
+ fpcount = 0;
DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
/* forward pass on the instructions to collect register liveness info */
while (ins) {
spec = ins_spec [ins->opcode];
DEBUG (print_ins (i, ins));
+
if (spec [MONO_INST_SRC1]) {
- if (spec [MONO_INST_SRC1] == 'f')
+ if (spec [MONO_INST_SRC1] == 'f') {
+ GList *spill;
reginfo1 = reginfof;
+
+ spill = g_list_first (fspill_list);
+ if (spill && fpcount < MONO_MAX_FREGS) {
+ reginfo1 [ins->sreg1].flags |= MONO_X86_FP_NEEDS_LOAD;
+ fspill_list = g_list_remove (fspill_list, spill->data);
+ } else
+ fpcount--;
+ }
else
reginfo1 = reginfo;
reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
ins->sreg1 = -1;
}
if (spec [MONO_INST_SRC2]) {
- if (spec [MONO_INST_SRC2] == 'f')
+ if (spec [MONO_INST_SRC2] == 'f') {
+ GList *spill;
reginfo2 = reginfof;
+ spill = g_list_first (fspill_list);
+ if (spill) {
+ reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD;
+ fspill_list = g_list_remove (fspill_list, spill->data);
+ if (fpcount >= MONO_MAX_FREGS) {
+ fspill++;
+ fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
+ reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD_SPILL;
+ }
+ } else
+ fpcount--;
+ }
else
reginfo2 = reginfo;
reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
ins->sreg2 = -1;
}
if (spec [MONO_INST_DEST]) {
- if (spec [MONO_INST_DEST] == 'f')
+ if (spec [MONO_INST_DEST] == 'f') {
reginfod = reginfof;
+ if (fpcount >= MONO_MAX_FREGS) {
+ reginfod [ins->dreg].flags |= MONO_X86_FP_NEEDS_SPILL;
+ fspill++;
+ fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
+ fpcount--;
+ }
+ fpcount++;
+ }
else
reginfod = reginfo;
if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
reginfod [ins->dreg + 1].last_use = i;
if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
reginfod [ins->dreg + 1].born_in = i;
- }
+ }
} else {
ins->dreg = -1;
}
ins = ins->next;
}
+ // todo: check if we have anything left on fp stack, in verify mode?
+ fspill = 0;
+
DEBUG (print_regtrack (reginfo, rs->next_vireg));
DEBUG (print_regtrack (reginfof, rs->next_vfreg));
tmp = reversed;
--i;
ins = tmp->data;
spec = ins_spec [ins->opcode];
+ prev_dreg = -1;
DEBUG (g_print ("processing:"));
DEBUG (print_ins (i, ins));
if (spec [MONO_INST_CLOB] == 's') {
}
} else {
DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
+ prev_dreg = ins->dreg;
rs->iassign [ins->dreg] = dest_reg;
rs->isymbolic [dest_reg] = ins->dreg;
ins->dreg = dest_reg;
}
}
- /* update for use with FP regs... */
- if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
+ /* Track dreg */
+ if (spec [MONO_INST_DEST] == 'f') {
+ if (reginfof [ins->dreg].flags & MONO_X86_FP_NEEDS_SPILL) {
+ GList *spill_node;
+ MonoInst *store;
+ spill_node = g_list_first (fspill_list);
+ g_assert (spill_node);
+
+ store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->dreg, ins);
+ insert_before_ins (ins, tmp, store);
+ fspill_list = g_list_remove (fspill_list, spill_node->data);
+ fspill--;
+ }
+ }
+ else if (ins->dreg >= MONO_MAX_IREGS) {
val = rs->iassign [ins->dreg];
prev_dreg = ins->dreg;
if (val < 0) {
} else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
}
- } else {
- prev_dreg = -1;
}
if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
ins->sreg1 = X86_EAX;
rs->ifree_mask &= ~ (1 << X86_EAX);
}
- if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
+
+ /* Track sreg1 */
+ if (spec [MONO_INST_SRC1] == 'f') {
+ if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD) {
+ MonoInst *load;
+ MonoInst *store = NULL;
+
+ if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
+ GList *spill_node;
+ spill_node = g_list_first (fspill_list);
+ g_assert (spill_node);
+
+ store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg1, ins);
+ fspill_list = g_list_remove (fspill_list, spill_node->data);
+ }
+
+ fspill++;
+ fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
+ load = create_spilled_load_float (cfg, fspill, ins->sreg1, ins);
+ insert_before_ins (ins, tmp, load);
+ if (store)
+ insert_before_ins (load, tmp, store);
+ }
+ }
+ else if (ins->sreg1 >= MONO_MAX_IREGS) {
val = rs->iassign [ins->sreg1];
prev_sreg1 = ins->sreg1;
if (val < 0) {
ins->next = copy;
}
}
- if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
+ /* track sreg2 */
+ if (spec [MONO_INST_SRC2] == 'f') {
+ if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD) {
+ MonoInst *load;
+ MonoInst *store = NULL;
+
+ if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
+ GList *spill_node;
+
+ spill_node = g_list_first (fspill_list);
+ g_assert (spill_node);
+ if (spec [MONO_INST_SRC1] == 'f' && (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL))
+ spill_node = g_list_next (spill_node);
+
+ store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg2, ins);
+ fspill_list = g_list_remove (fspill_list, spill_node->data);
+ }
+
+ fspill++;
+ fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
+ load = create_spilled_load_float (cfg, fspill, ins->sreg2, ins);
+ insert_before_ins (ins, tmp, load);
+ if (store)
+ insert_before_ins (load, tmp, store);
+ }
+ }
+ else if (ins->sreg2 >= MONO_MAX_IREGS) {
val = rs->iassign [ins->sreg2];
prev_sreg2 = ins->sreg2;
if (val < 0) {
DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
mono_regstate_free_int (rs, ins->sreg2);
}*/
-
+
//DEBUG (print_ins (i, ins));
/* this may result from a insert_before call */
if (!tmp->next)
bb->code = tmp->data;
tmp = tmp->next;
}
+
+ g_free (reginfo);
+ g_free (reginfof);
+ g_list_free (fspill_list);
}
static unsigned char*
mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
}
x86_call_code (code, 0);
- if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
+ if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
break;
case OP_FCALL_REG:
case OP_CALL_REG:
call = (MonoCallInst*)ins;
x86_call_reg (code, ins->sreg1);
- if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
+ if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
break;
case OP_FCALL_MEMBASE:
case OP_CALL_MEMBASE:
call = (MonoCallInst*)ins;
x86_call_membase (code, ins->sreg1, ins->inst_offset);
- if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
+ if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
break;
case OP_OUTARG:
case OP_STORER8_MEMBASE_REG:
x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
break;
+ case OP_LOADR8_SPILL_MEMBASE:
+ x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
+ x86_fxch (code, 1);
+ break;
case OP_LOADR8_MEMBASE:
x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
break;
}
void
-mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
+mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
{
MonoJumpInfo *patch_info;
/* Done by the generated code */
;
else {
- mono_runtime_class_init (vtable);
+ if (run_cctors)
+ mono_runtime_class_init (vtable);
}
*((gconstpointer *)(ip + 1)) =
(char*)vtable->data + patch_info->data.field->offset;
alloc_size -= pos;
- if (alloc_size)
+ if (alloc_size) {
+ /* See mono_emit_stack_alloc */
+#ifdef PLATFORM_WIN32
+ guint32 remaining_size = alloc_size;
+ while (remaining_size >= 0x1000) {
+ x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
+ x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
+ remaining_size -= 0x1000;
+ }
+ if (remaining_size)
+ x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
+#else
x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
+#endif
+ }
/* compute max_offset in order to use short forward jumps */
max_offset = 0;
x86_leave (code);
- if (sig->call_convention == MONO_CALL_STDCALL) {
+ if (CALLCONV_IS_STDCALL (sig->call_convention)) {
MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
stack_to_pop = arch_get_argument_info (sig, sig->param_count, arg_info);
mono_lmf_addr = &tls->lmf;
#endif
}
+
+void
+mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
+{
+
+ /* add the this argument */
+ if (this_reg != -1) {
+ MonoInst *this;
+ MONO_INST_NEW (cfg, this, OP_OUTARG);
+ this->type = this_type;
+ this->sreg1 = this_reg;
+ mono_bblock_add_inst (cfg->cbb, this);
+ }
+
+ if (vt_reg != -1) {
+ MonoInst *vtarg;
+ MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
+ vtarg->type = STACK_MP;
+ vtarg->sreg1 = vt_reg;
+ mono_bblock_add_inst (cfg->cbb, vtarg);
+ }
+}
+
+
+gint
+mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
+{
+ if (cmethod->klass == mono_defaults.math_class) {
+ if (strcmp (cmethod->name, "Sin") == 0)
+ return OP_SIN;
+ else if (strcmp (cmethod->name, "Cos") == 0)
+ return OP_COS;
+ else if (strcmp (cmethod->name, "Tan") == 0)
+ return OP_TAN;
+ else if (strcmp (cmethod->name, "Atan") == 0)
+ return OP_ATAN;
+ else if (strcmp (cmethod->name, "Sqrt") == 0)
+ return OP_SQRT;
+ else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
+ return OP_ABS;
+#if 0
+ /* OP_FREM is not IEEE compatible */
+ else if (strcmp (cmethod->name, "IEEERemainder") == 0)
+ return OP_FREM;
+#endif
+ else
+ return -1;
+ } else {
+ return -1;
+ }
+ return -1;
+}
+
+