2004-02-16 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / mini / mini-x86.c
index 1de04e11b4d5b5a4d8cf1697cf93c9a96c3d217f..de32a0a1fea0e52eab0ddb85324bb8347b45a008 100644 (file)
@@ -4,20 +4,33 @@
  * Authors:
  *   Paolo Molaro (lupus@ximian.com)
  *   Dietmar Maurer (dietmar@ximian.com)
+ *   Patrik Torstensson
  *
  * (C) 2003 Ximian, Inc.
  */
 #include "mini.h"
 #include <string.h>
+#include <math.h>
 
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/debug-helpers.h>
+#include <mono/metadata/profiler-private.h>
+#include <mono/utils/mono-math.h>
 
 #include "mini-x86.h"
 #include "inssel.h"
 #include "cpu-pentium.h"
 
-int mono_exc_esp_offset = 0;
+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
+
+static gpointer mono_arch_get_lmf_addr (void);
 
 const char*
 mono_arch_regname (int reg) {
@@ -26,8 +39,7 @@ mono_arch_regname (int 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";
        }
@@ -102,13 +114,19 @@ arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgum
 static int indent_level = 0;
 
 static void indent (int diff) {
-       int v = indent_level;
+       int v;
+       if (diff < 0)
+               indent_level += diff;
+       v = indent_level;
        while (v-- > 0) {
                printf (". ");
        }
-       indent_level += diff;
+       if (diff > 0)
+               indent_level += diff;
 }
 
+static gboolean enable_trace = TRUE;
+
 static void
 enter_method (MonoMethod *method, char *ebp)
 {
@@ -119,6 +137,9 @@ 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);
@@ -153,7 +174,7 @@ enter_method (MonoMethod *method, char *ebp)
                                if (class == mono_defaults.string_class) {
                                        printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
                                } else {
-                                       printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
+                                       printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
                                }
                        } else 
                                printf ("this:NULL, ");
@@ -247,6 +268,9 @@ leave_method (MonoMethod *method, ...)
        char *fname;
        va_list ap;
 
+       if (!enable_trace)
+               return;
+
        va_start(ap, method);
 
        fname = mono_method_full_name (method, TRUE);
@@ -413,6 +437,23 @@ cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
        return 0;
 }
 
+/*
+ * Initialize the cpu to execute managed code.
+ */
+void
+mono_arch_cpu_init (void)
+{
+       guint16 fpcw;
+
+       /* spec compliance requires running with double precision */
+       __asm__  __volatile__ ("fnstcw %0\n": "=m" (fpcw));
+       fpcw &= ~X86_FPCW_PRECC_MASK;
+       fpcw |= X86_FPCW_PREC_DOUBLE;
+       __asm__  __volatile__ ("fldcw %0\n": : "m" (fpcw));
+       __asm__  __volatile__ ("fnstcw %0\n": "=m" (fpcw));
+
+}
+
 /*
  * This function returns the optimizations supported on this cpu.
  */
@@ -421,7 +462,7 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
 {
        int eax, ebx, ecx, edx;
        guint32 opts = 0;
-
+       
        *exclude_mask = 0;
        /* Feature Flags function, flags returned in EDX. */
        if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
@@ -452,7 +493,7 @@ is_regsize_var (MonoType *t) {
        case MONO_TYPE_CLASS:
        case MONO_TYPE_SZARRAY:
        case MONO_TYPE_ARRAY:
-               return FALSE;
+               return TRUE;
        case MONO_TYPE_VALUETYPE:
                if (t->data.klass->enumtype)
                        return is_regsize_var (t->data.klass->enum_basetype);
@@ -475,17 +516,23 @@ mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
                if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
                        continue;
 
-               if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
+               if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) || 
+                   (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
                        continue;
 
-               /* we can only allocate 32 bit values */
-               if (is_regsize_var (ins->inst_vtype)) {
+               /* we dont allocate I1 to registers because there is no simply way to sign extend 
+                * 8bit quantities in caller saved registers on x86 */
+               if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) || 
+                   (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
+                   (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;
 }
 
@@ -517,7 +564,7 @@ mono_arch_allocate_vars (MonoCompile *m)
        header = ((MonoMethodNormal *)m->method)->header;
 
        sig = m->method->signature;
-       
+
        offset = 8;
        curinst = 0;
        if (MONO_TYPE_ISSTRUCT (sig->ret)) {
@@ -547,6 +594,11 @@ mono_arch_allocate_vars (MonoCompile *m)
                curinst++;
        }
 
+       if (sig->call_convention == MONO_CALL_VARARG) {
+               m->sig_cookie = offset;
+               offset += sizeof (gpointer);
+       }
+
        for (i = 0; i < sig->param_count; ++i) {
                inst = m->varinfo [curinst];
                if (inst->opcode != OP_REGVAR) {
@@ -562,14 +614,24 @@ mono_arch_allocate_vars (MonoCompile *m)
        }
 
        offset = 0;
+
        /* reserve space to save LMF and caller saved registers */
-       offset += sizeof (MonoLMF);
 
-       /* reserve space to store the esp */
-       offset += sizeof (gpointer);
+       if (m->method->save_lmf) {
+               offset += sizeof (MonoLMF);
+       } else {
+               if (m->used_int_regs & (1 << X86_EBX)) {
+                       offset += 4;
+               }
 
-       /* this is a global constant */
-       mono_exc_esp_offset = -offset;
+               if (m->used_int_regs & (1 << X86_EDI)) {
+                       offset += 4;
+               }
+
+               if (m->used_int_regs & (1 << X86_ESI)) {
+                       offset += 4;
+               }
+       }
 
        for (i = curinst; i < m->num_varinfo; ++i) {
                inst = m->varinfo [i];
@@ -579,7 +641,7 @@ mono_arch_allocate_vars (MonoCompile *m)
 
                /* inst->unused indicates native sized value types, this is used by the
                * pinvoke wrappers when they call functions returning structure */
-               if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
+               if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
                        size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
                else
                        size = mono_type_size (inst->inst_vtype, &align);
@@ -612,26 +674,35 @@ mono_arch_allocate_vars (MonoCompile *m)
  */
 MonoCallInst*
 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
-       MonoInst *arg, *in, **rev_args;
+       MonoInst *arg, *in;
        MonoMethodSignature *sig;
        int i, n, stack_size, type;
        MonoType *ptype;
 
+       stack_size = 0;
+       /* 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;
+               arg->inst_left = sig_arg;
+               arg->type = STACK_PTR;
+               /* prepend, so they get reversed */
+               arg->next = call->out_args;
+               call->out_args = arg;
+               stack_size += sizeof (gpointer);
+       }
        sig = call->signature;
        n = sig->param_count + sig->hasthis;
-       rev_args = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
-       
-       if (sig->ret && (sig->ret->type == MONO_TYPE_I8 || sig->ret->type == MONO_TYPE_U8)) {
-               //g_warning ("long value returned");
-       }
+
        if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
-               stack_size = 4;
-       else
-               stack_size = 0;
+               stack_size += sizeof (gpointer);
        for (i = 0; i < n; ++i) {
                if (is_virtual && i == 0) {
                        /* the argument will be attached to the call instrucion */
-                       rev_args [n - 1] = arg = NULL;
                        in = call->args [i];
                        stack_size += 4;
                } else {
@@ -640,7 +711,9 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                        arg->cil_code = in->cil_code;
                        arg->inst_left = in;
                        arg->type = in->type;
-                       rev_args [n - i - 1] = arg;
+                       /* prepend, so they get reversed */
+                       arg->next = call->out_args;
+                       call->out_args = arg;
                        if (i >= sig->hasthis) {
                                ptype = sig->params [i - sig->hasthis];
                                if (ptype->byref)
@@ -699,9 +772,19 @@ handle_enum:
                                                goto handle_enum;
                                        }
                                        break;
+                               case MONO_TYPE_TYPEDBYREF:
+                                       stack_size += sizeof (MonoTypedRef);
+                                       arg->opcode = OP_OUTARG_VT;
+                                       arg->klass = in->klass;
+                                       arg->unused = sig->pinvoke;
+                                       arg->inst_imm = sizeof (MonoTypedRef); 
+                                       break;
+                               case MONO_TYPE_GENERICINST:
+                                       type = ptype->data.generic_inst->generic_type->type;
+                                       goto handle_enum;
+
                                default:
-                                       g_warning ("unknown type 0x%02x\n", type);
-                                       g_assert_not_reached ();
+                                       g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
                                }
                        } else {
                                /* the this argument */
@@ -709,8 +792,9 @@ handle_enum:
                        }
                }
        }
-       /* they need to be pushed in reverse order */
-       call->args = rev_args;
+       /* if the function returns a struct, the called method already does a ret $0x4 */
+       if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
+               stack_size -= 4;
        call->stack_usage = stack_size;
        /* 
         * should set more info in call, such as the stack space
@@ -1076,6 +1160,104 @@ branch_cc_table [] = {
 
 #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 {
@@ -1083,9 +1265,10 @@ 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;
+static const char*const * ins_spec = pentium_desc;
 
 static void
 print_ins (int i, MonoInst *ins)
@@ -1300,6 +1483,7 @@ insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
        item->data = to_insert; 
 }
 
+
 #if  0
 static int
 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
@@ -1324,7 +1508,12 @@ alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg,
 }
 #endif
 
-#include "cprop.c"
+/* 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"*/
 
 /*
  * Local register allocation.
@@ -1344,31 +1533,43 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
        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);
-       
+       /*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;
@@ -1377,8 +1578,21 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                        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;
@@ -1387,8 +1601,16 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                        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 */
@@ -1403,7 +1625,7 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                                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;
                }
@@ -1412,6 +1634,9 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                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;
@@ -1421,6 +1646,7 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                --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') {
@@ -1493,8 +1719,11 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                } else if (spec [MONO_INST_CLOB] == 'd') { /* division */
                        int dest_reg = X86_EAX;
-                       if (spec [MONO_INST_DEST] == 'd')
+                       int clob_reg = X86_EDX;
+                       if (spec [MONO_INST_DEST] == 'd') {
                                dest_reg = X86_EDX; /* reminder */
+                               clob_reg = X86_EAX;
+                       }
                        val = rs->iassign [ins->dreg];
                        if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
                                DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
@@ -1505,7 +1734,7 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                                if (val < -1) {
                                        /* the register gets spilled after this inst */
                                        int spill = -val -1;
-                                       dest_mask = 1 << (dest_reg == X86_EAX? X86_EDX: X86_EAX);
+                                       dest_mask = 1 << clob_reg;
                                        prev_dreg = ins->dreg;
                                        val = mono_regstate_alloc_int (rs, dest_mask);
                                        if (val < 0)
@@ -1521,6 +1750,7 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                                        }
                                } 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;
@@ -1530,6 +1760,11 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                                //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
                                if (val != dest_reg) { /* force a copy */
                                        create_copy_ins (cfg, val, dest_reg, ins);
+                                       if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
+                                               DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
+                                               get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
+                                               mono_regstate_free_int (rs, dest_reg);
+                                       }
                                }
                        }
                        src1_mask = 1 << X86_EAX;
@@ -1547,8 +1782,22 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                                mono_regstate_free_int (rs, X86_EDX);
                        }
                }
-               /* 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) {
@@ -1617,14 +1866,49 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                        } 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));
                        mono_regstate_free_int (rs, ins->dreg);
                }
-               if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
+               /* put src1 in EAX if it needs to be */
+               if (spec [MONO_INST_SRC1] == 'a') {
+                       if (!(rs->ifree_mask & (1 << X86_EAX))) {
+                               DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
+                               get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
+                               mono_regstate_free_int (rs, X86_EAX);
+                       }
+                       /* force-set sreg1 */
+                       rs->iassign [ins->sreg1] = X86_EAX;
+                       rs->isymbolic [X86_EAX] = ins->sreg1;
+                       ins->sreg1 = X86_EAX;
+                       rs->ifree_mask &= ~ (1 << X86_EAX);
+               }
+
+               /* 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) {
@@ -1679,7 +1963,33 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                                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) {
@@ -1723,13 +2033,17 @@ mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
                        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*
@@ -1899,17 +2213,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
        cpos = bb->max_offset;
 
-       if (mono_trace_coverage) {
-               MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
+       if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
+               MonoProfileCoverageInfo *cov = cfg->coverage_info;
                g_assert (!mono_compile_aot);
                cpos += 6;
 
-               // fixme: make this work with inlining
-               g_assert_not_reached ();
-               //if (bb->cil_code)
-               //cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
+               cov->data [bb->dfn].cil_code = bb->cil_code;
                /* this is not thread save, but good enough */
-               /* fixme: howto handle overflows? */
                x86_inc_mem (code, &cov->data [bb->dfn].count); 
        }
 
@@ -1931,6 +2241,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                mono_debug_record_line_number (cfg, ins, offset);
 
                switch (ins->opcode) {
+               case OP_BIGMUL:
+                       x86_mul_reg (code, ins->sreg2, TRUE);
+                       break;
+               case OP_BIGMUL_UN:
+                       x86_mul_reg (code, ins->sreg2, FALSE);
+                       break;
+               case OP_X86_SETEQ_MEMBASE:
+                       x86_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
+                       break;
                case OP_STOREI1_MEMBASE_IMM:
                        x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
                        break;
@@ -2125,6 +2444,12 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case CEE_NEG:
                        x86_neg_reg (code, ins->sreg1);
                        break;
+               case OP_SEXT_I1:
+                       x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
+                       break;
+               case OP_SEXT_I2:
+                       x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
+                       break;
                case CEE_MUL:
                        x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
                        break;
@@ -2188,24 +2513,38 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case CEE_JMP: {
                        /*
                         * Note: this 'frame destruction' logic is useful for tail calls, too.
+                        * Keep in sync with the code in emit_epilog.
                         */
-                       int pos = -4;
-                       if (cfg->used_int_regs & (1 << X86_EBX)) {
-                               x86_mov_reg_membase (code, X86_EBX, X86_EBP, pos, 4);
+                       int pos = 0;
+
+                       /* FIXME: no tracing support... */
+                       if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
+                               code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
+                       /* reset offset to make max_len work */
+                       offset = code - cfg->native_code;
+
+                       g_assert (!cfg->method->save_lmf);
+
+                       if (cfg->used_int_regs & (1 << X86_EBX))
                                pos -= 4;
-                       }
-                       if (cfg->used_int_regs & (1 << X86_EDI)) {
-                               x86_mov_reg_membase (code, X86_EDI, X86_EBP, pos, 4);
+                       if (cfg->used_int_regs & (1 << X86_EDI))
                                pos -= 4;
-                       }
-                       if (cfg->used_int_regs & (1 << X86_ESI)) {
-                               x86_mov_reg_membase (code, X86_ESI, X86_EBP, pos, 4);
+                       if (cfg->used_int_regs & (1 << X86_ESI))
                                pos -= 4;
-                       }
+                       if (pos)
+                               x86_lea_membase (code, X86_ESP, X86_EBP, pos);
+       
+                       if (cfg->used_int_regs & (1 << X86_ESI))
+                               x86_pop_reg (code, X86_ESI);
+                       if (cfg->used_int_regs & (1 << X86_EDI))
+                               x86_pop_reg (code, X86_EDI);
+                       if (cfg->used_int_regs & (1 << X86_EBX))
+                               x86_pop_reg (code, X86_EBX);
+       
                        /* restore ESP/EBP */
                        x86_leave (code);
                        offset = code - cfg->native_code;
-                       mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, ins->inst_p0);
+                       mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
                        x86_jump32 (code, 0);
                        break;
                }
@@ -2213,6 +2552,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        /* ensure ins->sreg1 is not NULL */
                        x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
                        break;
+               case OP_ARGLIST: {
+                       int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
+                       x86_push_reg (code, hreg);
+                       x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
+                       x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
+                       x86_pop_reg (code, hreg);
+                       break;
+               }
                case OP_FCALL:
                case OP_LCALL:
                case OP_VCALL:
@@ -2225,7 +2572,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
                        }
                        x86_call_code (code, 0);
-                       if (call->stack_usage)
+                       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:
@@ -2235,7 +2582,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_CALL_REG:
                        call = (MonoCallInst*)ins;
                        x86_call_reg (code, ins->sreg1);
-                       if (call->stack_usage)
+                       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:
@@ -2245,7 +2592,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_CALL_MEMBASE:
                        call = (MonoCallInst*)ins;
                        x86_call_membase (code, ins->sreg1, ins->inst_offset);
-                       if (call->stack_usage)
+                       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:
@@ -2279,6 +2626,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_X86_LEA:
                        x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
                        break;
+               case OP_X86_LEA_MEMBASE:
+                       x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
+                       break;
                case OP_X86_XCHG:
                        x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
                        break;
@@ -2299,23 +2649,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_call_code (code, 0);
                        break;
                }
-               case OP_ENDFILTER:
-                       if (ins->sreg1 != X86_EAX)
-                               x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
-                       x86_mov_reg_membase (code, X86_ESP, X86_EBP, mono_exc_esp_offset, 4);
-                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
-                       x86_ret (code);
-                       break;
-               case CEE_ENDFINALLY:
-                       /* 
-                        * restore ESP - which can be modified when we allocate value types in the filter
-                        */
-                       x86_mov_reg_membase (code, X86_ESP, X86_EBP, mono_exc_esp_offset, 4);
-                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
-                       x86_ret (code);
-                       break;
-               case OP_HANDLER: 
-                       x86_mov_membase_reg (code, X86_EBP, mono_exc_esp_offset, X86_ESP, 4);
+               case OP_CALL_HANDLER: 
                        mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
                        x86_call_imm (code, 0);
                        break;
@@ -2403,7 +2737,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_R8CONST: {
                        double d = *(double *)ins->inst_p0;
 
-                       if (d == 0.0) {
+                       if ((d == 0.0) && (mono_signbit (d) == 0)) {
                                x86_fldz (code);
                        } else if (d == 1.0) {
                                x86_fld1 (code);
@@ -2416,7 +2750,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_R4CONST: {
                        float f = *(float *)ins->inst_p0;
 
-                       if (f == 0.0) {
+                       if ((f == 0.0) && (mono_signbit (f) == 0)) {
                                x86_fldz (code);
                        } else if (f == 1.0) {
                                x86_fld1 (code);
@@ -2429,6 +2763,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                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;
@@ -2566,10 +2904,41 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_ABS:
                        x86_fabs (code);
                        break;          
-               case OP_TAN:
+               case OP_TAN: {
+                       /* 
+                        * it really doesn't make sense to inline all this code,
+                        * it's here just to show that things may not be as simple 
+                        * as they appear.
+                        */
+                       guchar *check_pos, *end_tan, *pop_jump;
+                       x86_push_reg (code, X86_EAX);
                        x86_fptan (code);
-                       break;          
+                       x86_fnstsw (code);
+                       x86_test_reg_imm (code, X86_EAX, 0x400);
+                       check_pos = code;
+                       x86_branch8 (code, X86_CC_NE, 0, FALSE);
+                       x86_fstp (code, 0); /* pop the 1.0 */
+                       end_tan = code;
+                       x86_jump8 (code, 0);
+                       x86_fldpi (code);
+                       x86_fp_op (code, X86_FADD, 0);
+                       x86_fxch (code, 1);
+                       x86_fprem1 (code);
+                       x86_fstsw (code);
+                       x86_test_reg_imm (code, X86_EAX, 0x400);
+                       pop_jump = code;
+                       x86_branch8 (code, X86_CC_NE, 0, FALSE);
+                       x86_fstp (code, 1);
+                       x86_fptan (code);
+                       x86_patch (pop_jump, code);
+                       x86_fstp (code, 0); /* pop the 1.0 */
+                       x86_patch (check_pos, code);
+                       x86_patch (end_tan, code);
+                       x86_pop_reg (code, X86_EAX);
+                       break;
+               }
                case OP_ATAN:
+                       x86_fld1 (code);
                        x86_fpatan (code);
                        break;          
                case OP_SQRT:
@@ -2585,9 +2954,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        /* we need to exchange ST(0) with ST(1) */
                        x86_fxch (code, 1);
 
-                       /* this requires a loop, because fprem1 somtimes 
+                       /* this requires a loop, because fprem somtimes 
                         * returns a partial remainder */
                        l1 = code;
+                       /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
+                       /* x86_fprem1 (code); */
                        x86_fprem (code);
                        x86_fnstsw (code);
                        x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x0400);
@@ -2728,7 +3099,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_FBEQ:
                        if (cfg->opt & MONO_OPT_FCMOV) {
+                               guchar *jump = code;
+                               x86_branch8 (code, X86_CC_P, 0, TRUE);
                                EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
+                               x86_patch (jump, code);
                                break;
                        }
                        x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
@@ -2842,12 +3216,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 void
 mono_arch_register_lowlevel_calls (void)
 {
+       mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
        mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
        mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
 }
 
 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;
 
@@ -2868,33 +3243,47 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, Mono
                case MONO_PATCH_INFO_IP:
                        *((gpointer *)(ip)) = ip;
                        continue;
+               case MONO_PATCH_INFO_METHOD_REL:
+                       *((gpointer *)(ip)) = code + patch_info->data.offset;
+                       continue;
                case MONO_PATCH_INFO_INTERNAL_METHOD: {
                        MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
                        if (!mi) {
                                g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
                                g_assert_not_reached ();
                        }
-                       target = mi->wrapper;
+                       target = mono_icall_get_wrapper (mi);
+                       break;
+               }
+               case MONO_PATCH_INFO_METHOD_JUMP: {
+                       GSList *list;
+
+                       /* get the trampoline to the method from the domain */
+                       target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
+                       if (!domain->jump_target_hash)
+                               domain->jump_target_hash = g_hash_table_new (NULL, NULL);
+                       list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
+                       list = g_slist_prepend (list, ip);
+                       g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
                        break;
                }
                case MONO_PATCH_INFO_METHOD:
                        if (patch_info->data.method == method) {
                                target = code;
-                       } else {
+                       } else
                                /* get the trampoline to the method from the domain */
                                target = mono_arch_create_jit_trampoline (patch_info->data.method);
-                       }
                        break;
                case MONO_PATCH_INFO_SWITCH: {
-                       gpointer *table = (gpointer *)patch_info->data.target;
+                       gpointer *jump_table = mono_mempool_alloc (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
                        int i;
 
-                       *((gconstpointer *)(ip + 2)) = patch_info->data.target;
+                       *((gconstpointer *)(ip + 2)) = jump_table;
 
                        for (i = 0; i < patch_info->table_size; i++) {
-                               table [i] = (int)patch_info->data.table [i] + code;
+                               jump_table [i] = code + (int)patch_info->data.table [i];
                        }
-                       /* we put into the table the absolute address, no need fo x86_patch in this case */
+                       /* we put into the table the absolute address, no need for x86_patch in this case */
                        continue;
                }
                case MONO_PATCH_INFO_METHODCONST:
@@ -2903,10 +3292,69 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, Mono
                case MONO_PATCH_INFO_FIELD:
                        *((gconstpointer *)(ip + 1)) = patch_info->data.target;
                        continue;
+               case MONO_PATCH_INFO_IID:
+                       mono_class_init (patch_info->data.klass);
+                       *((guint32 *)(ip + 1)) = patch_info->data.klass->interface_id;
+                       continue;                       
+               case MONO_PATCH_INFO_VTABLE:
+                       *((gconstpointer *)(ip + 1)) = mono_class_vtable (domain, patch_info->data.klass);
+                       continue;
+               case MONO_PATCH_INFO_CLASS_INIT: {
+                       guint8 *code = ip;
+                       /* Might already been changed to a nop */
+                       x86_call_imm (code, 0);
+                       target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
+                       break;
+               }
+               case MONO_PATCH_INFO_SFLDA: {
+                       MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
+                       if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
+                               /* Done by the generated code */
+                               ;
+                       else {
+                               if (run_cctors)
+                                       mono_runtime_class_init (vtable);
+                       }
+                       *((gconstpointer *)(ip + 1)) = 
+                               (char*)vtable->data + patch_info->data.field->offset;
+                       continue;
+               }
                case MONO_PATCH_INFO_R4:
                case MONO_PATCH_INFO_R8:
                        *((gconstpointer *)(ip + 2)) = patch_info->data.target;
                        continue;
+               case MONO_PATCH_INFO_EXC_NAME:
+                       *((gconstpointer *)(ip + 1)) = patch_info->data.name;
+                       continue;
+               case MONO_PATCH_INFO_LDSTR:
+                       *((gconstpointer *)(ip + 1)) = 
+                               mono_ldstr (domain, patch_info->data.token->image, 
+                                                       mono_metadata_token_index (patch_info->data.token->token));
+                       continue;
+               case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
+                       gpointer handle;
+                       MonoClass *handle_class;
+
+                       handle = mono_ldtoken (patch_info->data.token->image, 
+                                                                  patch_info->data.token->token, &handle_class);
+                       mono_class_init (handle_class);
+                       mono_class_init (mono_class_from_mono_type (handle));
+
+                       *((gconstpointer *)(ip + 1)) = 
+                               mono_type_get_object (domain, handle);
+                       continue;
+               }
+               case MONO_PATCH_INFO_LDTOKEN: {
+                       gpointer handle;
+                       MonoClass *handle_class;
+
+                       handle = mono_ldtoken (patch_info->data.token->image,
+                                                                  patch_info->data.token->token, &handle_class);
+                       mono_class_init (handle_class);
+
+                       *((gconstpointer *)(ip + 1)) = handle;
+                       continue;
+               }
                default:
                        g_assert_not_reached ();
                }
@@ -2923,10 +3371,10 @@ mono_arch_max_epilog_size (MonoCompile *cfg)
        if (cfg->method->save_lmf)
                max_epilog_size += 128;
        
-       if (mono_jit_trace_calls)
+       if (mono_jit_trace_calls != NULL)
                max_epilog_size += 50;
 
-       if (mono_jit_profile)
+       if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
                max_epilog_size += 50;
 
        /* count the number of exception infos */
@@ -2955,7 +3403,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        int alloc_size, pos, max_offset, i;
        guint8 *code;
 
-       cfg->code_size = 256;
+       cfg->code_size =  MAX (((MonoMethodNormal *)method)->header->code_size * 4, 256);
        code = cfg->native_code = g_malloc (cfg->code_size);
 
        x86_push_reg (code, X86_EBP);
@@ -2966,7 +3414,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        if (method->save_lmf) {
                pos += sizeof (MonoLMF);
-               
+
                /* save the current IP */
                mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
                x86_push_imm (code, 0);
@@ -2979,11 +3427,28 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
                /* save method info */
                x86_push_imm (code, method);
-       
+
                /* get the address of lmf for the current thread */
-               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
-                                    (gpointer)"mono_get_lmf_addr");
-               x86_call_code (code, 0);
+               /* 
+                * This is performance critical so we try to use some tricks to make
+                * it fast.
+                */
+               if (lmf_tls_offset != -1) {
+                       /* Load lmf quicky using the GS register */
+                       x86_prefix (code, X86_GS_PREFIX);
+                       x86_mov_reg_mem (code, X86_EAX, 0, 4);
+                       x86_mov_reg_membase (code, X86_EAX, X86_EAX, lmf_tls_offset, 4);
+               }
+               else {
+#ifdef HAVE_KW_THREAD
+                       mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
+                                                                (gpointer)"mono_arch_get_lmf_addr");
+#else
+                       mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
+                                                                (gpointer)"mono_get_lmf_addr");
+#endif
+                       x86_call_code (code, 0);
+               }
 
                /* push lmf */
                x86_push_reg (code, X86_EAX); 
@@ -3011,8 +3476,21 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        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;
@@ -3021,7 +3499,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        MonoInst *ins = bb->code;
                        bb->max_offset = max_offset;
 
-                       if (mono_trace_coverage)
+                       if (cfg->prof_options & MONO_PROFILE_COVERAGE)
                                max_offset += 6; 
 
                        while (ins) {
@@ -3031,7 +3509,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                }
        }
 
-       if (mono_jit_trace_calls)
+       if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
                code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
 
        /* load arguments allocated to register from the stack */
@@ -3058,15 +3536,17 @@ mono_arch_emit_epilog (MonoCompile *cfg)
 {
        MonoJumpInfo *patch_info;
        MonoMethod *method = cfg->method;
+       MonoMethodSignature *sig = method->signature;
        int pos;
+       guint32 stack_to_pop;
        guint8 *code;
 
        code = cfg->native_code + cfg->code_len;
 
-       if (mono_jit_trace_calls)
+       if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
                code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
 
-       
+       /* the code restoring the registers must be kept in sync with CEE_JMP */
        pos = 0;
        
        if (method->save_lmf) {
@@ -3117,14 +3597,31 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        }
 
        x86_leave (code);
-       x86_ret (code);
+
+       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);
+       }
+       else
+       if (MONO_TYPE_ISSTRUCT (cfg->method->signature->ret))
+         stack_to_pop = 4;
+       else
+         stack_to_pop = 0;
+
+       if (stack_to_pop)
+               x86_ret_imm (code, stack_to_pop);
+       else
+               x86_ret (code);
 
        /* add code to raise exceptions */
        for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
                switch (patch_info->type) {
                case MONO_PATCH_INFO_EXC:
                        x86_patch (patch_info->ip.i + cfg->native_code, code);
+                       mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);
                        x86_push_imm (code, patch_info->data.target);
+                       mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_METHOD_REL, (gpointer)patch_info->ip.i);
                        x86_push_imm (code, patch_info->ip.i + cfg->native_code);
                        patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
                        patch_info->data.name = "mono_arch_throw_exception_by_name";
@@ -3142,3 +3639,115 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        g_assert (cfg->code_len < cfg->code_size);
 
 }
+
+void
+mono_arch_flush_icache (guint8 *code, gint size)
+{
+       /* not needed */
+}
+
+/*
+ * Support for fast access to the thread-local lmf structure using the GS
+ * segment register on NPTL + kernel 2.6.x.
+ */
+
+static gboolean tls_offset_inited = FALSE;
+
+#ifdef HAVE_KW_THREAD
+static __thread gpointer mono_lmf_addr;
+#endif
+
+static gpointer
+mono_arch_get_lmf_addr (void)
+{
+#ifdef HAVE_KW_THREAD
+       return mono_lmf_addr;
+#else
+       g_assert_not_reached ();
+       return NULL;
+#endif
+}
+
+void
+mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
+{
+       if (!tls_offset_inited) {
+               guint8 *code;
+
+               tls_offset_inited = TRUE;
+
+               if (getenv ("MONO_NPTL")) {
+                       /* 
+                        * Determine the offset of mono_lfm_addr inside the TLS structures
+                        * by disassembling the function above.
+                        */
+                       code = (guint8*)&mono_arch_get_lmf_addr;
+
+                       /* This is generated by gcc 3.3.2 */
+                       if ((code [0] == 0x55) && (code [1] == 0x89) && (code [2] == 0xe5) &&
+                               (code [3] == 0x65) && (code [4] == 0xa1) && (code [5] == 0x00) &&
+                               (code [6] == 0x00) && (code [7] == 0x00) && (code [8] == 0x00) &&
+                               (code [9] == 0x8b) && (code [10] == 0x80)) {
+                               lmf_tls_offset = *(int*)&(code [11]);
+                       }
+               }
+       }               
+
+#ifdef HAVE_KW_THREAD
+       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;
+}
+
+