2007-06-10 Sanghyeon Seo <sanxiyn@gmail.com>
[mono.git] / mono / mini / mini-x86.c
index 444c9207d4e56ce50aacb7095a7a32d0babccb5d..6da3071a44ad6c3c36387fd680bf120f9101a832 100644 (file)
 #include "mini.h"
 #include <string.h>
 #include <math.h>
-
-#ifndef PLATFORM_WIN32
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#include <sys/mman.h>
 #endif
 
 #include <mono/metadata/appdomain.h>
 #include "trace.h"
 #include "mini-x86.h"
 #include "inssel.h"
-#include "cpu-pentium.h"
+#include "cpu-x86.h"
 
 /* On windows, these hold the key returned by TlsAlloc () */
 static gint lmf_tls_offset = -1;
+static gint lmf_addr_tls_offset = -1;
 static gint appdomain_tls_offset = -1;
 static gint thread_tls_offset = -1;
 
+#ifdef MONO_XEN_OPT
+static gboolean optimize_for_xen = TRUE;
+#else
+#define optimize_for_xen 0
+#endif
+
+#ifdef PLATFORM_WIN32
+static gboolean is_win32 = TRUE;
+#else
+static gboolean is_win32 = FALSE;
+#endif
+
 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
 
+#define ARGS_OFFSET 8
+
 #ifdef PLATFORM_WIN32
 /* Under windows, the default pinvoke calling convention is stdcall */
 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
@@ -42,8 +55,6 @@ static gint thread_tls_offset = -1;
 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
 #endif
 
-#define SIGNAL_STACK_SIZE (64 * 1024)
-
 #define NOT_IMPLEMENTED g_assert_not_reached ()
 
 const char*
@@ -60,6 +71,11 @@ mono_arch_regname (int reg) {
        return "unknown";
 }
 
+const char*
+mono_arch_fregname (int reg) {
+       return "unknown";
+}
+
 typedef enum {
        ArgInIReg,
        ArgInFloatSSEReg,
@@ -87,6 +103,7 @@ typedef struct {
        guint32 reg_usage;
        guint32 freg_usage;
        gboolean need_stack_align;
+       guint32 stack_align_amount;
        ArgInfo ret;
        ArgInfo sig_cookie;
        ArgInfo args [1];
@@ -98,7 +115,8 @@ typedef struct {
 
 static X86_Reg_No param_regs [] = { 0 };
 
-#ifdef PLATFORM_WIN32
+#if defined(PLATFORM_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
+#define SMALL_STRUCTS_IN_REGS
 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
 #endif
 
@@ -136,7 +154,7 @@ add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
 
     if (*gr >= FLOAT_PARAM_REGS) {
                ainfo->storage = ArgOnStack;
-               (*stack_size) += sizeof (gpointer);
+               (*stack_size) += is_double ? 8 : 4;
     }
     else {
                /* A double register */
@@ -164,7 +182,7 @@ add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
        else 
                size = mono_type_stack_size (&klass->byval_arg, NULL);
 
-#ifdef PLATFORM_WIN32
+#ifdef SMALL_STRUCTS_IN_REGS
        if (sig->pinvoke && is_return) {
                MonoMarshalType *info;
 
@@ -216,7 +234,7 @@ add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
  * For x86 win32, see ???.
  */
 static CallInfo*
-get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
+get_call_info (MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
 {
        guint32 i, gr, fr;
        MonoType *ret_type;
@@ -224,7 +242,10 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
        guint32 stack_size = 0;
        CallInfo *cinfo;
 
-       cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
+       if (mp)
+               cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
+       else
+               cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
 
        gr = 0;
        fr = 0;
@@ -264,6 +285,13 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
                case MONO_TYPE_R8:
                        cinfo->ret.storage = ArgOnDoubleFpStack;
                        break;
+               case MONO_TYPE_GENERICINST:
+                       if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
+                               cinfo->ret.storage = ArgInIReg;
+                               cinfo->ret.reg = X86_EAX;
+                               break;
+                       }
+                       /* Fall through */
                case MONO_TYPE_VALUETYPE: {
                        guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
 
@@ -346,6 +374,12 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
                case MONO_TYPE_ARRAY:
                        add_general (&gr, &stack_size, ainfo);
                        break;
+               case MONO_TYPE_GENERICINST:
+                       if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
+                               add_general (&gr, &stack_size, ainfo);
+                               break;
+                       }
+                       /* Fall through */
                case MONO_TYPE_VALUETYPE:
                        add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
                        break;
@@ -377,6 +411,13 @@ get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
                add_general (&gr, &stack_size, &cinfo->sig_cookie);
        }
 
+#if defined(__APPLE__)
+       if ((stack_size % 16) != 0) { 
+               cinfo->need_stack_align = TRUE;
+               stack_size += cinfo->stack_align_amount = 16-(stack_size % 16);
+       }
+#endif
+
        cinfo->stack_usage = stack_size;
        cinfo->reg_usage = gr;
        cinfo->freg_usage = fr;
@@ -398,11 +439,12 @@ int
 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
 {
        int k, frame_size = 0;
-       int size, align, pad;
+       int size, pad;
+       guint32 align;
        int offset = 8;
        CallInfo *cinfo;
 
-       cinfo = get_call_info (csig, FALSE);
+       cinfo = get_call_info (NULL, csig, FALSE);
 
        if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
                frame_size += sizeof (gpointer);
@@ -422,8 +464,11 @@ mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJit
                
                if (csig->pinvoke)
                        size = mono_type_native_stack_size (csig->params [k], &align);
-               else
-                       size = mono_type_stack_size (csig->params [k], &align);
+               else {
+                       int ialign;
+                       size = mono_type_stack_size (csig->params [k], &ialign);
+                       align = ialign;
+               }
 
                /* ignore alignment for now */
                align = 1;
@@ -508,16 +553,15 @@ cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
 #endif
        if (have_cpuid) {
                /* Have to use the code manager to get around WinXP DEP */
-               MonoCodeManager *codeman = mono_code_manager_new_dynamic ();
-               CpuidFunc func;
-               void *ptr = mono_code_manager_reserve (codeman, sizeof (cpuid_impl));
-               memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
-
-               func = (CpuidFunc)ptr;
+               static CpuidFunc func = NULL;
+               void *ptr;
+               if (!func) {
+                       ptr = mono_global_codeman_reserve (sizeof (cpuid_impl));
+                       memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
+                       func = (CpuidFunc)ptr;
+               }
                func (id, p_eax, p_ebx, p_ecx, p_edx);
 
-               mono_code_manager_destroy (codeman);
-
                /*
                 * We use this approach because of issues with gcc and pic code, see:
                 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
@@ -546,7 +590,7 @@ mono_arch_cpu_init (void)
        __asm__  __volatile__ ("fldcw %0\n": : "m" (fpcw));
        __asm__  __volatile__ ("fnstcw %0\n": "=m" (fpcw));
 #else
-       _control87 (_PC_64, MCW_PC);
+       _control87 (_PC_53, MCW_PC);
 #endif
 }
 
@@ -593,12 +637,24 @@ mono_arch_is_int_overflow (void *sigctx, void *info)
 
                /* idiv REG */
                switch (x86_modrm_rm (ip [1])) {
+               case X86_EAX:
+                       reg = ctx.eax;
+                       break;
                case X86_ECX:
                        reg = ctx.ecx;
                        break;
+               case X86_EDX:
+                       reg = ctx.edx;
+                       break;
                case X86_EBX:
                        reg = ctx.ebx;
                        break;
+               case X86_ESI:
+                       reg = ctx.esi;
+                       break;
+               case X86_EDI:
+                       reg = ctx.edi;
+                       break;
                default:
                        g_assert_not_reached ();
                        reg = -1;
@@ -611,30 +667,6 @@ mono_arch_is_int_overflow (void *sigctx, void *info)
        return FALSE;
 }
 
-static gboolean
-is_regsize_var (MonoType *t) {
-       if (t->byref)
-               return TRUE;
-       switch (mono_type_get_underlying_type (t)->type) {
-       case MONO_TYPE_I4:
-       case MONO_TYPE_U4:
-       case MONO_TYPE_I:
-       case MONO_TYPE_U:
-       case MONO_TYPE_PTR:
-       case MONO_TYPE_FNPTR:
-               return TRUE;
-       case MONO_TYPE_OBJECT:
-       case MONO_TYPE_STRING:
-       case MONO_TYPE_CLASS:
-       case MONO_TYPE_SZARRAY:
-       case MONO_TYPE_ARRAY:
-               return TRUE;
-       case MONO_TYPE_VALUETYPE:
-               return FALSE;
-       }
-       return FALSE;
-}
-
 GList *
 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
 {
@@ -655,9 +687,7 @@ mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
 
                /* 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)) {
+               if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
                        g_assert (MONO_VARINFO (cfg, i)->reg == -1);
                        g_assert (i == vmv->idx);
                        vars = g_list_prepend (vars, vmv);
@@ -707,91 +737,38 @@ mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
  * The locals var stuff should most likely be split in another method.
  */
 void
-mono_arch_allocate_vars (MonoCompile *m)
+mono_arch_allocate_vars (MonoCompile *cfg)
 {
        MonoMethodSignature *sig;
        MonoMethodHeader *header;
        MonoInst *inst;
        guint32 locals_stack_size, locals_stack_align;
-       int i, offset, curinst, size, align;
+       int i, offset;
        gint32 *offsets;
        CallInfo *cinfo;
 
-       header = mono_method_get_header (m->method);
-       sig = mono_method_signature (m->method);
-
-       offset = 8;
-       curinst = 0;
-
-       cinfo = get_call_info (sig, FALSE);
-
-       switch (cinfo->ret.storage) {
-       case ArgOnStack:
-               m->ret->opcode = OP_REGOFFSET;
-               m->ret->inst_basereg = X86_EBP;
-               m->ret->inst_offset = offset;
-               offset += sizeof (gpointer);
-               break;
-       case ArgValuetypeInReg:
-               break;
-       case ArgInIReg:
-               m->ret->opcode = OP_REGVAR;
-               m->ret->inst_c0 = cinfo->ret.reg;
-               break;
-       case ArgNone:
-       case ArgOnFloatFpStack:
-       case ArgOnDoubleFpStack:
-               break;
-       default:
-               g_assert_not_reached ();
-       }
-
-       if (sig->hasthis) {
-               inst = m->varinfo [curinst];
-               if (inst->opcode != OP_REGVAR) {
-                       inst->opcode = OP_REGOFFSET;
-                       inst->inst_basereg = X86_EBP;
-               }
-               inst->inst_offset = offset;
-               offset += sizeof (gpointer);
-               curinst++;
-       }
-
-       if (sig->call_convention == MONO_CALL_VARARG) {
-               m->sig_cookie = offset;
-               offset += sizeof (gpointer);
-       }
+       header = mono_method_get_header (cfg->method);
+       sig = mono_method_signature (cfg->method);
 
-       for (i = 0; i < sig->param_count; ++i) {
-               inst = m->varinfo [curinst];
-               if (inst->opcode != OP_REGVAR) {
-                       inst->opcode = OP_REGOFFSET;
-                       inst->inst_basereg = X86_EBP;
-               }
-               inst->inst_offset = offset;
-               size = mono_type_size (sig->params [i], &align);
-               size += 4 - 1;
-               size &= ~(4 - 1);
-               offset += size;
-               curinst++;
-       }
+       cinfo = get_call_info (cfg->mempool, sig, FALSE);
 
+       cfg->frame_reg = MONO_ARCH_BASEREG;
        offset = 0;
 
-       /* reserve space to save LMF and caller saved registers */
+       /* Reserve space to save LMF and caller saved registers */
 
-       if (m->method->save_lmf) {
+       if (cfg->method->save_lmf) {
                offset += sizeof (MonoLMF);
        } else {
-               if (m->used_int_regs & (1 << X86_EBX)) {
+               if (cfg->used_int_regs & (1 << X86_EBX)) {
                        offset += 4;
                }
 
-               if (m->used_int_regs & (1 << X86_EDI)) {
+               if (cfg->used_int_regs & (1 << X86_EDI)) {
                        offset += 4;
                }
 
-               if (m->used_int_regs & (1 << X86_ESI)) {
+               if (cfg->used_int_regs & (1 << X86_ESI)) {
                        offset += 4;
                }
        }
@@ -800,39 +777,75 @@ mono_arch_allocate_vars (MonoCompile *m)
        case ArgValuetypeInReg:
                /* Allocate a local to hold the result, the epilog will copy it to the correct place */
                offset += 8;
-               m->ret->opcode = OP_REGOFFSET;
-               m->ret->inst_basereg = X86_EBP;
-               m->ret->inst_offset = - offset;
+               cfg->ret->opcode = OP_REGOFFSET;
+               cfg->ret->inst_basereg = X86_EBP;
+               cfg->ret->inst_offset = - offset;
                break;
        default:
                break;
        }
 
        /* Allocate locals */
-       offsets = mono_allocate_stack_slots (m, &locals_stack_size, &locals_stack_align);
+       offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
        if (locals_stack_align) {
                offset += (locals_stack_align - 1);
                offset &= ~(locals_stack_align - 1);
        }
-       for (i = m->locals_start; i < m->num_varinfo; i++) {
+       for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
                if (offsets [i] != -1) {
-                       MonoInst *inst = m->varinfo [i];
+                       MonoInst *inst = cfg->varinfo [i];
                        inst->opcode = OP_REGOFFSET;
                        inst->inst_basereg = X86_EBP;
                        inst->inst_offset = - (offset + offsets [i]);
                        //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
                }
        }
-       g_free (offsets);
        offset += locals_stack_size;
 
+
+       /*
+        * Allocate arguments+return value
+        */
+
+       switch (cinfo->ret.storage) {
+       case ArgOnStack:
+               cfg->ret->opcode = OP_REGOFFSET;
+               cfg->ret->inst_basereg = X86_EBP;
+               cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
+               break;
+       case ArgValuetypeInReg:
+               break;
+       case ArgInIReg:
+               cfg->ret->opcode = OP_REGVAR;
+               cfg->ret->inst_c0 = cinfo->ret.reg;
+               break;
+       case ArgNone:
+       case ArgOnFloatFpStack:
+       case ArgOnDoubleFpStack:
+               break;
+       default:
+               g_assert_not_reached ();
+       }
+
+       if (sig->call_convention == MONO_CALL_VARARG) {
+               g_assert (cinfo->sig_cookie.storage == ArgOnStack);
+               cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
+       }
+
+       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
+               ArgInfo *ainfo = &cinfo->args [i];
+               inst = cfg->args [i];
+               if (inst->opcode != OP_REGVAR) {
+                       inst->opcode = OP_REGOFFSET;
+                       inst->inst_basereg = X86_EBP;
+               }
+               inst->inst_offset = ainfo->offset + ARGS_OFFSET;
+       }
+
        offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
        offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
 
-       g_free (cinfo);
-
-       /* change sign? */
-       m->stack_offset = -offset;
+       cfg->stack_offset = offset;
 }
 
 void
@@ -843,65 +856,90 @@ mono_arch_create_vars (MonoCompile *cfg)
 
        sig = mono_method_signature (cfg->method);
 
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg->mempool, sig, FALSE);
 
        if (cinfo->ret.storage == ArgValuetypeInReg)
                cfg->ret_var_is_local = TRUE;
-
-       g_free (cinfo);
 }
 
 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
  * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info 
  */
 
+static void
+emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call)
+{
+       MonoInst *arg;
+       MonoMethodSignature *tmp_sig;
+       MonoInst *sig_arg;
+
+       /* FIXME: Add support for signature tokens to AOT */
+       cfg->disable_aot = TRUE;
+       MONO_INST_NEW (cfg, arg, OP_OUTARG);
+
+       /*
+        * mono_ArgIterator_Setup assumes the signature cookie is 
+        * passed first and all the arguments which were before it are
+        * passed on the stack after the signature. So compensate by 
+        * passing a different signature.
+        */
+       tmp_sig = mono_metadata_signature_dup (call->signature);
+       tmp_sig->param_count -= call->signature->sentinelpos;
+       tmp_sig->sentinelpos = 0;
+       memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
+
+       MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
+       sig_arg->inst_p0 = tmp_sig;
+
+       arg->inst_left = sig_arg;
+       arg->type = STACK_PTR;
+       /* prepend, so they get reversed */
+       arg->next = call->out_args;
+       call->out_args = arg;
+}
+
 /* 
  * take the arguments and generate the arch-specific
  * instructions to properly call the function in call.
  * This includes pushing, moving arguments to the right register
  * etc.
- * Issue: who does the spilling if needed, and when?
  */
 MonoCallInst*
 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
        MonoInst *arg, *in;
        MonoMethodSignature *sig;
-       int i, n, stack_size, type;
-       MonoType *ptype;
+       int i, n;
        CallInfo *cinfo;
+       int sentinelpos = 0;
 
-       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;
 
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg->mempool, sig, FALSE);
 
-       if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
-               if (cinfo->ret.storage == ArgOnStack)
-                       stack_size += sizeof (gpointer);
-       }
+       if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
+               sentinelpos = sig->sentinelpos + (is_virtual ? 1 : 0);
 
        for (i = 0; i < n; ++i) {
+               ArgInfo *ainfo = cinfo->args + i;
+
+               /* Emit the signature cookie just before the implicit arguments */
+               if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
+                       emit_sig_cookie (cfg, call);
+               }
+
                if (is_virtual && i == 0) {
                        /* the argument will be attached to the call instrucion */
                        in = call->args [i];
-                       stack_size += 4;
                } else {
+                       MonoType *t;
+
+                       if (i >= sig->hasthis)
+                               t = sig->params [i - sig->hasthis];
+                       else
+                               t = &mono_defaults.int_class->byval_arg;
+                       t = mono_type_get_underlying_type (t);
+
                        MONO_INST_NEW (cfg, arg, OP_OUTARG);
                        in = call->args [i];
                        arg->cil_code = in->cil_code;
@@ -910,77 +948,51 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                        /* prepend, so they get reversed */
                        arg->next = call->out_args;
                        call->out_args = arg;
-                       if (i >= sig->hasthis) {
-                               MonoType *t = sig->params [i - sig->hasthis];
-                               ptype = mono_type_get_underlying_type (t);
-                               if (t->byref)
-                                       type = MONO_TYPE_U;
-                               else
-                                       type = ptype->type;
-                               /* FIXME: validate arguments... */
-                               switch (type) {
-                               case MONO_TYPE_I:
-                               case MONO_TYPE_U:
-                               case MONO_TYPE_BOOLEAN:
-                               case MONO_TYPE_CHAR:
-                               case MONO_TYPE_I1:
-                               case MONO_TYPE_U1:
-                               case MONO_TYPE_I2:
-                               case MONO_TYPE_U2:
-                               case MONO_TYPE_I4:
-                               case MONO_TYPE_U4:
-                               case MONO_TYPE_STRING:
-                               case MONO_TYPE_CLASS:
-                               case MONO_TYPE_OBJECT:
-                               case MONO_TYPE_PTR:
-                               case MONO_TYPE_FNPTR:
-                               case MONO_TYPE_ARRAY:
-                               case MONO_TYPE_SZARRAY:
-                                       stack_size += 4;
-                                       break;
-                               case MONO_TYPE_I8:
-                               case MONO_TYPE_U8:
-                                       stack_size += 8;
-                                       break;
-                               case MONO_TYPE_R4:
-                                       stack_size += 4;
-                                       arg->opcode = OP_OUTARG_R4;
-                                       break;
-                               case MONO_TYPE_R8:
-                                       stack_size += 8;
-                                       arg->opcode = OP_OUTARG_R8;
-                                       break;
-                               case MONO_TYPE_VALUETYPE: {
-                                       int size;
-                                       if (sig->pinvoke) 
-                                               size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
-                                       else 
-                                               size = mono_type_stack_size (&in->klass->byval_arg, NULL);
 
-                                       stack_size += size;
-                                       arg->opcode = OP_OUTARG_VT;
-                                       arg->klass = in->klass;
-                                       arg->unused = sig->pinvoke;
-                                       arg->inst_imm = size; 
-                                       break;
+                       if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
+                               guint32 size, align;
+
+                               if (t->type == MONO_TYPE_TYPEDBYREF) {
+                                       size = sizeof (MonoTypedRef);
+                                       align = sizeof (gpointer);
                                }
-                               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); 
+                               else
+                                       if (sig->pinvoke)
+                                               size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
+                                       else {
+                                               int ialign;
+                                               size = mono_type_stack_size (&in->klass->byval_arg, &ialign);
+                                               align = ialign;
+                                       }
+                               arg->opcode = OP_OUTARG_VT;
+                               arg->klass = in->klass;
+                               arg->backend.is_pinvoke = sig->pinvoke;
+                               arg->inst_imm = size; 
+                       }
+                       else {
+                               switch (ainfo->storage) {
+                               case ArgOnStack:
+                                       arg->opcode = OP_OUTARG;
+                                       if (!t->byref) {
+                                               if (t->type == MONO_TYPE_R4)
+                                                       arg->opcode = OP_OUTARG_R4;
+                                               else
+                                                       if (t->type == MONO_TYPE_R8)
+                                                               arg->opcode = OP_OUTARG_R8;
+                                       }
                                        break;
                                default:
-                                       g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
+                                       g_assert_not_reached ();
                                }
-                       } else {
-                               /* the this argument */
-                               stack_size += 4;
                        }
                }
        }
 
+       /* Handle the case where there are no implicit arguments */
+       if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
+               emit_sig_cookie (cfg, call);
+       }
+
        if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
                if (cinfo->ret.storage == ArgValuetypeInReg) {
                        MonoInst *zero_inst;
@@ -1003,16 +1015,19 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                else
                        /* 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;
+                               cinfo->stack_usage -= 4;
        }
+       
+       call->stack_usage = cinfo->stack_usage;
 
-       call->stack_usage = stack_size;
-       g_free (cinfo);
-
-       /* 
-        * should set more info in call, such as the stack space
-        * used by the args that needs to be added back to esp
-        */
+#if defined(__APPLE__)
+       if (cinfo->need_stack_align) {
+               MONO_INST_NEW (cfg, arg, OP_X86_OUTARG_ALIGN_STACK);
+               arg->inst_c0 = cinfo->stack_align_amount;
+               arg->next = call->out_args;
+               call->out_args = arg;
+        }
+#endif 
 
        return call;
 }
@@ -1025,6 +1040,10 @@ mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean ena
 {
        guchar *code = p;
 
+#if __APPLE__
+       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
+#endif
+
        /* if some args are passed in registers, we need to save them here */
        x86_push_reg (code, X86_EBP);
 
@@ -1038,7 +1057,11 @@ mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean ena
                mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
                x86_call_code (code, 0);
        }
+#if __APPLE__
+       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
+#else
        x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
+#endif
 
        return code;
 }
@@ -1074,6 +1097,12 @@ mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean ena
        case MONO_TYPE_R8:
                save_mode = SAVE_FP;
                break;
+       case MONO_TYPE_GENERICINST:
+               if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
+                       save_mode = SAVE_EAX;
+                       break;
+               }
+               /* Fall through */
        case MONO_TYPE_VALUETYPE:
                save_mode = SAVE_STRUCT;
                break;
@@ -1176,12 +1205,20 @@ if (ins->flags & MONO_INST_BRLABEL) { \
         } \
 }
 
-/* emit an exception if condition is fail */
+/*  
+ *     Emit an exception if condition is fail and
+ *  if possible do a directly branch to target 
+ */
 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name)            \
-        do {                                                        \
-               mono_add_patch_info (cfg, code - cfg->native_code,   \
-                                   MONO_PATCH_INFO_EXC, exc_name);  \
-               x86_branch32 (code, cond, 0, signed);               \
+       do {                                                        \
+               MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
+               if (tins == NULL) {                                                                             \
+                       mono_add_patch_info (cfg, code - cfg->native_code,   \
+                                       MONO_PATCH_INFO_EXC, exc_name);  \
+                       x86_branch32 (code, cond, 0, signed);               \
+               } else {        \
+                       EMIT_COND_BRANCH (tins, cond, signed);  \
+               }                       \
        } while (0); 
 
 #define EMIT_FPCOMPARE(code) do { \
@@ -1193,67 +1230,50 @@ if (ins->flags & MONO_INST_BRLABEL) { \
 static guint8*
 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
 {
-       if (cfg->compile_aot) {
-               guint32 got_reg = X86_EAX;
-
-               if (cfg->compile_aot) {          
-                       /*
-                        * Since the patches are generated by the back end, there is
-                        * no way to generate a got_var at this point.
-                        */
-                       g_assert (cfg->got_var);
-
-                       if (cfg->got_var->opcode == OP_REGOFFSET)
-                               x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
-                       else
-                               got_reg = cfg->got_var->dreg;
-               }
-
-               mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
-               x86_call_membase (code, got_reg, 0xf0f0f0f0);
-       }
-       else {
-               mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
-               x86_call_code (code, 0);
-       }
+       mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
+       x86_call_code (code, 0);
 
        return code;
 }
 
-/* FIXME: Add more instructions */
-#define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG))
+#define INST_IGNORES_CFLAGS(opcode) (!(((opcode) == OP_ADC) || ((opcode) == OP_IADC) || ((opcode) == OP_ADC_IMM) || ((opcode) == OP_IADC_IMM) || ((opcode) == OP_SBB) || ((opcode) == OP_ISBB) || ((opcode) == OP_SBB_IMM) || ((opcode) == OP_ISBB_IMM)))
 
+/*
+ * peephole_pass_1:
+ *
+ *   Perform peephole opts which should/can be performed before local regalloc
+ */
 static void
-peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
+peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
 {
        MonoInst *ins, *last_ins = NULL;
        ins = bb->code;
 
        while (ins) {
-
                switch (ins->opcode) {
-               case OP_ICONST:
-                       /* reg = 0 -> XOR (reg, reg) */
-                       /* XOR sets cflags on x86, so we cant do it always */
-                       if (ins->inst_c0 == 0 && ins->next && INST_IGNORES_CFLAGS (ins->next)) {
-                               ins->opcode = CEE_XOR;
-                               ins->sreg1 = ins->dreg;
-                               ins->sreg2 = ins->dreg;
-                       }
+               case OP_IADD_IMM:
+               case OP_ADD_IMM:
+                       if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
+                               /* 
+                                * X86_LEA is like ADD, but doesn't have the
+                                * sreg1==dreg restriction.
+                                */
+                               ins->opcode = OP_X86_LEA_MEMBASE;
+                               ins->inst_basereg = ins->sreg1;
+                       } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
+                               ins->opcode = OP_X86_INC_REG;
                        break;
-               case OP_MUL_IMM: 
-                       /* remove unnecessary multiplication with 1 */
-                       if (ins->inst_imm == 1) {
-                               if (ins->dreg != ins->sreg1) {
-                                       ins->opcode = OP_MOVE;
-                               } else {
-                                       last_ins->next = ins->next;
-                                       ins = ins->next;
-                                       continue;
-                               }
-                       }
+               case OP_SUB_IMM:
+               case OP_ISUB_IMM:
+                       if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
+                               ins->opcode = OP_X86_LEA_MEMBASE;
+                               ins->inst_basereg = ins->sreg1;
+                               ins->inst_imm = -ins->inst_imm;
+                       } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
+                               ins->opcode = OP_X86_DEC_REG;
                        break;
                case OP_COMPARE_IMM:
+               case OP_ICOMPARE_IMM:
                        /* OP_COMPARE_IMM (reg, 0) 
                         * --> 
                         * OP_X86_TEST_NULL (reg) 
@@ -1357,55 +1377,39 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_LOADU1_MEMBASE:
                case OP_LOADI1_MEMBASE:
                        /* 
-                        * Note: if reg1 = reg2 the load op is removed
-                        *
                         * OP_STORE_MEMBASE_REG reg1, offset(basereg) 
                         * OP_LOAD_MEMBASE offset(basereg), reg2
                         * -->
                         * OP_STORE_MEMBASE_REG reg1, offset(basereg)
-                        * OP_MOVE reg1, reg2
+                        * CONV_I2/U2 reg1, reg2
                         */
-                       if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
+                       if (last_ins && X86_IS_BYTE_REG (last_ins->sreg1) &&
+                               (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
                                        ins->inst_basereg == last_ins->inst_destbasereg &&
                                        ins->inst_offset == last_ins->inst_offset) {
-                               if (ins->dreg == last_ins->sreg1) {
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;                                
-                                       continue;
-                               } else {
-                                       //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
-                                       ins->opcode = OP_MOVE;
-                                       ins->sreg1 = last_ins->sreg1;
-                               }
+                               ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? CEE_CONV_I1 : CEE_CONV_U1;
+                               ins->sreg1 = last_ins->sreg1;
                        }
                        break;
                case OP_LOADU2_MEMBASE:
                case OP_LOADI2_MEMBASE:
                        /* 
-                        * Note: if reg1 = reg2 the load op is removed
-                        *
                         * OP_STORE_MEMBASE_REG reg1, offset(basereg) 
                         * OP_LOAD_MEMBASE offset(basereg), reg2
                         * -->
                         * OP_STORE_MEMBASE_REG reg1, offset(basereg)
-                        * OP_MOVE reg1, reg2
+                        * CONV_I2/U2 reg1, reg2
                         */
                        if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
                                        ins->inst_basereg == last_ins->inst_destbasereg &&
                                        ins->inst_offset == last_ins->inst_offset) {
-                               if (ins->dreg == last_ins->sreg1) {
-                                       last_ins->next = ins->next;                             
-                                       ins = ins->next;                                
-                                       continue;
-                               } else {
-                                       //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
-                                       ins->opcode = OP_MOVE;
-                                       ins->sreg1 = last_ins->sreg1;
-                               }
+                               ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? CEE_CONV_I2 : CEE_CONV_U2;
+                               ins->sreg1 = last_ins->sreg1;
                        }
                        break;
                case CEE_CONV_I4:
                case CEE_CONV_U4:
+               case OP_ICONV_TO_I4:
                case OP_MOVE:
                        /*
                         * Removes:
@@ -1449,1134 +1453,254 @@ peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
        bb->last_ins = last_ins;
 }
 
-static const int 
-branch_cc_table [] = {
-       X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
-       X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
-       X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
-};
-
-#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)
+static void
+peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 {
-       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;
-}
+       MonoInst *ins, *last_ins = NULL;
+       ins = bb->code;
 
-/*
- * 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;
+       while (ins) {
 
-       si = &cfg->spill_info_float; 
-       
-       while (i <= spillvar) {
+               switch (ins->opcode) {
+               case OP_ICONST:
+                       /* reg = 0 -> XOR (reg, reg) */
+                       /* XOR sets cflags on x86, so we cant do it always */
+                       if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
+                               MonoInst *ins2;
 
-               if (!*si) {
-                       *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
-                       info->next = NULL;
-                       cfg->stack_offset -= sizeof (double);
-                       info->offset = cfg->stack_offset;
-               }
+                               ins->opcode = OP_IXOR;
+                               ins->sreg1 = ins->dreg;
+                               ins->sreg2 = ins->dreg;
 
-               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 is_global_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS && !X86_IS_CALLEE ((r)))
-#define reg_is_freeable(r) ((r) >= 0 && (r) < MONO_MAX_IREGS && X86_IS_CALLEE ((r)))
-
-typedef struct {
-       int born_in;
-       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;
-
-static void
-print_ins (int i, MonoInst *ins)
-{
-       const char *spec = ins_spec [ins->opcode];
-       g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
-       if (spec [MONO_INST_DEST]) {
-               if (ins->dreg >= MONO_MAX_IREGS)
-                       g_print (" R%d <-", ins->dreg);
-               else
-                       g_print (" %s <-", mono_arch_regname (ins->dreg));
-       }
-       if (spec [MONO_INST_SRC1]) {
-               if (ins->sreg1 >= MONO_MAX_IREGS)
-                       g_print (" R%d", ins->sreg1);
-               else
-                       g_print (" %s", mono_arch_regname (ins->sreg1));
-       }
-       if (spec [MONO_INST_SRC2]) {
-               if (ins->sreg2 >= MONO_MAX_IREGS)
-                       g_print (" R%d", ins->sreg2);
-               else
-                       g_print (" %s", mono_arch_regname (ins->sreg2));
-       }
-       if (spec [MONO_INST_CLOB])
-               g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
-       g_print ("\n");
-}
-
-static void
-print_regtrack (RegTrack *t, int num)
-{
-       int i;
-       char buf [32];
-       const char *r;
-       
-       for (i = 0; i < num; ++i) {
-               if (!t [i].born_in)
-                       continue;
-               if (i >= MONO_MAX_IREGS) {
-                       g_snprintf (buf, sizeof(buf), "R%d", i);
-                       r = buf;
-               } else
-                       r = mono_arch_regname (i);
-               g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
-       }
-}
-
-typedef struct InstList InstList;
-
-struct InstList {
-       InstList *prev;
-       InstList *next;
-       MonoInst *data;
-};
-
-static inline InstList*
-inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
-{
-       InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
-       item->data = data;
-       item->prev = NULL;
-       item->next = list;
-       if (list)
-               list->prev = item;
-       return item;
-}
-
-/*
- * Force the spilling of the variable in the symbolic register 'reg'.
- */
-static int
-get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-       
-       sel = cfg->rs->iassign [reg];
-       /*i = cfg->rs->isymbolic [sel];
-       g_assert (i == reg);*/
-       i = reg;
-       spill = ++cfg->spill_count;
-       cfg->rs->iassign [i] = -spill - 1;
-       mono_regstate_free_int (cfg->rs, sel);
-       /* we need to create a spill var and insert a load to sel after the current instruction */
-       MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = X86_EBP;
-       load->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next = load;
-       DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-
-       return sel;
-}
-
-static int
-get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
-{
-       MonoInst *load;
-       int i, sel, spill;
-
-       DEBUG (g_print ("\tstart regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
-       /* exclude the registers in the current instruction */
-       if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
-               if (ins->sreg1 >= MONO_MAX_IREGS)
-                       regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
-               else
-                       regmask &= ~ (1 << ins->sreg1);
-               DEBUG (g_print ("\t\texcluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
-       }
-       if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
-               if (ins->sreg2 >= MONO_MAX_IREGS)
-                       regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
-               else
-                       regmask &= ~ (1 << ins->sreg2);
-               DEBUG (g_print ("\t\texcluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
-       }
-       if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
-               regmask &= ~ (1 << ins->dreg);
-               DEBUG (g_print ("\t\texcluding dreg %s\n", mono_arch_regname (ins->dreg)));
-       }
-
-       DEBUG (g_print ("\t\tavailable regmask: 0x%08x\n", regmask));
-       g_assert (regmask); /* need at least a register we can free */
-       sel = -1;
-       /* we should track prev_use and spill the register that's farther */
-       for (i = 0; i < MONO_MAX_IREGS; ++i) {
-               if (regmask & (1 << i)) {
-                       sel = i;
-                       DEBUG (g_print ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
-                       break;
-               }
-       }
-       i = cfg->rs->isymbolic [sel];
-       spill = ++cfg->spill_count;
-       cfg->rs->iassign [i] = -spill - 1;
-       mono_regstate_free_int (cfg->rs, sel);
-       /* we need to create a spill var and insert a load to sel after the current instruction */
-       MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
-       load->dreg = sel;
-       load->inst_basereg = X86_EBP;
-       load->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (item->prev) {
-               while (ins->next != item->prev->data)
-                       ins = ins->next;
-       }
-       load->next = ins->next;
-       ins->next = load;
-       DEBUG (g_print ("\tSPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
-       i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
-       g_assert (i == sel);
-       
-       return sel;
-}
-
-static MonoInst*
-create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
-{
-       MonoInst *copy;
-       MONO_INST_NEW (cfg, copy, OP_MOVE);
-       copy->dreg = dest;
-       copy->sreg1 = src;
-       if (ins) {
-               copy->next = ins->next;
-               ins->next = copy;
-       }
-       DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
-       return copy;
-}
-
-static MonoInst*
-create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
-{
-       MonoInst *store;
-       MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
-       store->sreg1 = reg;
-       store->inst_destbasereg = X86_EBP;
-       store->inst_offset = mono_spillvar_offset (cfg, spill);
-       if (ins) {
-               store->next = ins->next;
-               ins->next = store;
-       }
-       DEBUG (g_print ("\tSPILLED STORE (%d at 0x%08x(%%ebp)) R%d (from %s)\n", spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
-       return store;
-}
-
-static void
-insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
-{
-       MonoInst *prev;
-       if (item->next) {
-               prev = item->next->data;
-
-               while (prev->next != ins)
-                       prev = prev->next;
-               to_insert->next = ins;
-               prev->next = to_insert;
-       } else {
-               to_insert->next = ins;
-       }
-       /* 
-        * needed otherwise in the next instruction we can add an ins to the 
-        * end and that would get past this instruction.
-        */
-       item->data = to_insert; 
-}
-
-
-#if  0
-static int
-alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
-{
-       int val = cfg->rs->iassign [sym_reg];
-       if (val < 0) {
-               int spill = 0;
-               if (val < -1) {
-                       /* the register gets spilled after this inst */
-                       spill = -val -1;
-               }
-               val = mono_regstate_alloc_int (cfg->rs, allow_mask);
-               if (val < 0)
-                       val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
-               cfg->rs->iassign [sym_reg] = val;
-               /* add option to store before the instruction for src registers */
-               if (spill)
-                       create_spilled_store (cfg, spill, val, sym_reg, ins);
-       }
-       cfg->rs->isymbolic [val] = sym_reg;
-       return val;
-}
-#endif
-
-/* flags used in reginfo->flags */
-enum {
-       MONO_X86_FP_NEEDS_LOAD_SPILL    = 1 << 0,
-       MONO_X86_FP_NEEDS_SPILL                 = 1 << 1,
-       MONO_X86_FP_NEEDS_LOAD                  = 1 << 2,
-       MONO_X86_REG_NOT_ECX                    = 1 << 3,
-       MONO_X86_REG_EAX                                = 1 << 4,
-       MONO_X86_REG_EDX                                = 1 << 5,
-       MONO_X86_REG_ECX                                = 1 << 6
-};
-
-static int
-mono_x86_alloc_int_reg (MonoCompile *cfg, InstList *tmp, MonoInst *ins, guint32 dest_mask, int sym_reg, int flags)
-{
-       int val;
-       int test_mask = dest_mask;
-
-       if (flags & MONO_X86_REG_EAX)
-               test_mask &= (1 << X86_EAX);
-       else if (flags & MONO_X86_REG_EDX)
-               test_mask &= (1 << X86_EDX);
-       else if (flags & MONO_X86_REG_ECX)
-               test_mask &= (1 << X86_ECX);
-       else if (flags & MONO_X86_REG_NOT_ECX)
-               test_mask &= ~ (1 << X86_ECX);
-
-       val = mono_regstate_alloc_int (cfg->rs, test_mask);
-       if (val >= 0 && test_mask != dest_mask)
-               DEBUG(g_print ("\tUsed flag to allocate reg %s for R%u\n", mono_arch_regname (val), sym_reg));
-
-       if (val < 0 && (flags & MONO_X86_REG_NOT_ECX)) {
-               DEBUG(g_print ("\tFailed to allocate flag suggested mask (%u) but exluding ECX\n", test_mask));
-               val = mono_regstate_alloc_int (cfg->rs, (dest_mask & (~1 << X86_ECX)));
-       }
-
-       if (val < 0) {
-               val = mono_regstate_alloc_int (cfg->rs, dest_mask);
-               if (val < 0)
-                       val = get_register_spilling (cfg, tmp, ins, dest_mask, sym_reg);
-       }
-
-       return val;
-}
-
-static inline void
-assign_ireg (MonoRegState *rs, int reg, int hreg)
-{
-       g_assert (reg >= MONO_MAX_IREGS);
-       g_assert (hreg < MONO_MAX_IREGS);
-       g_assert (! is_global_ireg (hreg));
-
-       rs->iassign [reg] = hreg;
-       rs->isymbolic [hreg] = reg;
-       rs->ifree_mask &= ~ (1 << hreg);
-}
-
-/*#include "cprop.c"*/
-
-/*
- * Local register allocation.
- * We first scan the list of instructions and we save the liveness info of
- * each register (when the register is first used, when it's value is set etc.).
- * We also reverse the list of instructions (in the InstList list) because assigning
- * registers backwards allows for more tricks to be used.
- */
-void
-mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
-{
-       MonoInst *ins;
-       MonoRegState *rs = cfg->rs;
-       int i, val, fpcount;
-       RegTrack *reginfo, *reginfof;
-       RegTrack *reginfo1, *reginfo2, *reginfod;
-       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 = 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;
-       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') {
-                               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;
-                       reginfo1 [ins->sreg1].last_use = i;
-                       if (spec [MONO_INST_SRC1] == 'L') {
-                               /* The virtual register is allocated sequentially */
-                               reginfo1 [ins->sreg1 + 1].prev_use = reginfo1 [ins->sreg1 + 1].last_use;
-                               reginfo1 [ins->sreg1 + 1].last_use = i;
-                               if (reginfo1 [ins->sreg1 + 1].born_in == 0 || reginfo1 [ins->sreg1 + 1].born_in > i)
-                                       reginfo1 [ins->sreg1 + 1].born_in = i;
-
-                               reginfo1 [ins->sreg1].flags |= MONO_X86_REG_EAX;
-                               reginfo1 [ins->sreg1 + 1].flags |= MONO_X86_REG_EDX;
-                       }
-               } else {
-                       ins->sreg1 = -1;
-               }
-               if (spec [MONO_INST_SRC2]) {
-                       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;
-                       reginfo2 [ins->sreg2].last_use = i;
-                       if (spec [MONO_INST_SRC2] == 'L') {
-                               /* The virtual register is allocated sequentially */
-                               reginfo2 [ins->sreg2 + 1].prev_use = reginfo2 [ins->sreg2 + 1].last_use;
-                               reginfo2 [ins->sreg2 + 1].last_use = i;
-                               if (reginfo2 [ins->sreg2 + 1].born_in == 0 || reginfo2 [ins->sreg2 + 1].born_in > i)
-                                       reginfo2 [ins->sreg2 + 1].born_in = i;
-                       }
-                       if (spec [MONO_INST_CLOB] == 's') {
-                               reginfo2 [ins->sreg1].flags |= MONO_X86_REG_NOT_ECX;
-                               reginfo2 [ins->sreg2].flags |= MONO_X86_REG_ECX;
-                       }
-               } else {
-                       ins->sreg2 = -1;
-               }
-               if (spec [MONO_INST_DEST]) {
-                       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].killed_in = i;
-                       reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
-                       reginfod [ins->dreg].last_use = i;
-                       if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
-                               reginfod [ins->dreg].born_in = i;
-                       if (spec [MONO_INST_DEST] == 'l' || spec [MONO_INST_DEST] == 'L') {
-                               /* The virtual register is allocated sequentially */
-                               reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
-                               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;
-
-                               reginfod [ins->dreg].flags |= MONO_X86_REG_EAX;
-                               reginfod [ins->dreg + 1].flags |= MONO_X86_REG_EDX;
-                       }
-               } else {
-                       ins->dreg = -1;
-               }
-
-               reversed = inst_list_prepend (cfg->mempool, reversed, ins);
-               ++i;
-               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;
-       while (tmp) {
-               int prev_dreg, prev_sreg1, prev_sreg2, clob_dreg;
-               dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
-               --i;
-               ins = tmp->data;
-               spec = ins_spec [ins->opcode];
-               prev_dreg = -1;
-               clob_dreg = -1;
-               DEBUG (g_print ("processing:"));
-               DEBUG (print_ins (i, ins));
-               if (spec [MONO_INST_CLOB] == 's') {
-                       /*
-                        * Shift opcodes, SREG2 must be RCX
-                        */
-                       if (rs->ifree_mask & (1 << X86_ECX)) {
-                               if (ins->sreg2 < MONO_MAX_IREGS) {
-                                       /* Argument already in hard reg, need to copy */
-                                       MonoInst *copy = create_copy_ins (cfg, X86_ECX, ins->sreg2, NULL);
-                                       insert_before_ins (ins, tmp, copy);
-                               }
-                               else {
-                                       DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
-                                       assign_ireg (rs, ins->sreg2, X86_ECX);
-                               }
-                       } else {
-                               int need_ecx_spill = TRUE;
-                               /* 
-                                * we first check if src1/dreg is already assigned a register
-                                * and then we force a spill of the var assigned to ECX.
-                                */
-                               /* the destination register can't be ECX */
-                               dest_mask &= ~ (1 << X86_ECX);
-                               src1_mask &= ~ (1 << X86_ECX);
-                               val = rs->iassign [ins->dreg];
                                /* 
-                                * the destination register is already assigned to ECX:
-                                * we need to allocate another register for it and then
-                                * copy from this to ECX.
+                                * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG 
+                                * since it takes 3 bytes instead of 7.
                                 */
-                               if (val == X86_ECX && ins->dreg != ins->sreg2) {
-                                       int new_dest;
-                                       new_dest = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
-                                       g_assert (new_dest >= 0);
-                                       DEBUG (g_print ("\tclob:s changing dreg R%d to %s from ECX\n", ins->dreg, mono_arch_regname (new_dest)));
-
-                                       rs->isymbolic [new_dest] = ins->dreg;
-                                       rs->iassign [ins->dreg] = new_dest;
-                                       clob_dreg = ins->dreg;
-                                       ins->dreg = new_dest;
-                                       create_copy_ins (cfg, X86_ECX, new_dest, ins);
-                                       need_ecx_spill = FALSE;
-                                       /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
-                                       val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
-                                       rs->iassign [ins->dreg] = val;
-                                       rs->isymbolic [val] = prev_dreg;
-                                       ins->dreg = val;*/
-                               }
-                               if (is_global_ireg (ins->sreg2)) {
-                                       MonoInst *copy = create_copy_ins (cfg, X86_ECX, ins->sreg2, NULL);
-                                       insert_before_ins (ins, tmp, copy);
-                               }
-                               else {
-                                       val = rs->iassign [ins->sreg2];
-                                       if (val >= 0 && val != X86_ECX) {
-                                               MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
-                                               DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
-                                               move->next = ins;
-                                               g_assert_not_reached ();
-                                               /* FIXME: where is move connected to the instruction list? */
-                                               //tmp->prev->data->next = move;
+                               for (ins2 = ins->next; ins2; ins2 = ins2->next) {
+                                       if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
+                                               ins2->opcode = OP_STORE_MEMBASE_REG;
+                                               ins2->sreg1 = ins->dreg;
                                        }
-                                       else {
-                                               if (val == X86_ECX)
-                                               need_ecx_spill = FALSE;
+                                       else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
+                                               ins2->opcode = OP_STOREI4_MEMBASE_REG;
+                                               ins2->sreg1 = ins->dreg;
                                        }
+                                       else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
+                                               /* Continue iteration */
+                                       }
+                                       else
+                                               break;
                                }
-                               if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
-                                       DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
-                                       get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
-                                       mono_regstate_free_int (rs, X86_ECX);
-                               }
-                               if (!is_global_ireg (ins->sreg2))
-                                       /* force-set sreg2 */
-                                       assign_ireg (rs, ins->sreg2, X86_ECX);
                        }
-                       ins->sreg2 = X86_ECX;
-               } else if (spec [MONO_INST_CLOB] == 'd') {
-                       /*
-                        * DIVISION/REMAINER
+                       break;
+               case OP_IADD_IMM:
+               case OP_ADD_IMM:
+                       if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
+                               ins->opcode = OP_X86_INC_REG;
+                       break;
+               case OP_ISUB_IMM:
+               case OP_SUB_IMM:
+                       if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
+                               ins->opcode = OP_X86_DEC_REG;
+                       break;
+               case OP_X86_COMPARE_MEMBASE_IMM:
+                       /* 
+                        * OP_STORE_MEMBASE_REG reg, offset(basereg)
+                        * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
+                        * -->
+                        * OP_STORE_MEMBASE_REG reg, offset(basereg)
+                        * OP_COMPARE_IMM reg, imm
+                        *
+                        * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
                         */
-                       int dest_reg = X86_EAX;
-                       int clob_reg = X86_EDX;
-                       if (spec [MONO_INST_DEST] == 'd') {
-                               dest_reg = X86_EDX; /* reminder */
-                               clob_reg = X86_EAX;
-                       }
-                       if (is_global_ireg (ins->dreg))
-                               val = ins->dreg;
-                       else
-                               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]));
-                               get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
-                               mono_regstate_free_int (rs, dest_reg);
-                       }
-                       if (val < 0) {
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       int spill = -val -1;
-                                       dest_mask = 1 << dest_reg;
-                                       prev_dreg = ins->dreg;
-                                       val = mono_regstate_alloc_int (rs, dest_mask);
-                                       if (val < 0)
-                                               val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
-                                       rs->iassign [ins->dreg] = val;
-                                       if (spill)
-                                               create_spilled_store (cfg, spill, val, prev_dreg, ins);
-                                       DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
-                                       rs->isymbolic [val] = prev_dreg;
-                                       ins->dreg = val;
-                               } else {
-                                       DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
-                                       prev_dreg = ins->dreg;
-                                       assign_ireg (rs, ins->dreg, dest_reg);
-                                       ins->dreg = dest_reg;
-                                       val = dest_reg;
-                               }
-                       }
-
-                       //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);
-                               }
-                       }
-                       if (!(rs->ifree_mask & (1 << clob_reg)) && (clob_reg != val) && (rs->isymbolic [clob_reg] >= 8)) {
-                               DEBUG (g_print ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
-                               get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [clob_reg]);
-                               mono_regstate_free_int (rs, clob_reg);
-                       }
-                       src1_mask = 1 << X86_EAX;
-                       src2_mask = 1 << X86_ECX;
-               } else if (spec [MONO_INST_DEST] == 'l') {
-                       int hreg;
-                       val = rs->iassign [ins->dreg];
-                       /* check special case when dreg have been moved from ecx (clob shift) */
-                       if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
-                               hreg = clob_dreg + 1;
-                       else
-                               hreg = ins->dreg + 1;
-
-                       /* base prev_dreg on fixed hreg, handle clob case */
-                       val = hreg - 1;
-
-                       if (val != rs->isymbolic [X86_EAX] && !(rs->ifree_mask & (1 << X86_EAX))) {
-                               DEBUG (g_print ("\t(long-low) forced 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);
-                       }
-                       if (hreg != rs->isymbolic [X86_EDX] && !(rs->ifree_mask & (1 << X86_EDX))) {
-                               DEBUG (g_print ("\t(long-high) forced spill of R%d\n", rs->isymbolic [X86_EDX]));
-                               get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
-                               mono_regstate_free_int (rs, X86_EDX);
-                       }
-               } else if (spec [MONO_INST_CLOB] == 'b') {
-                       /*
-                        * x86_set_reg instructions, dreg needs to be EAX..EDX
-                        */     
-                       dest_mask = (1 << X86_EAX) | (1 << X86_EBX) | (1 << X86_ECX) | (1 << X86_EDX);
-                       if ((ins->dreg < MONO_MAX_IREGS) && (! (dest_mask & (1 << ins->dreg)))) {
-                               /* 
-                                * ins->dreg is already a hard reg, need to allocate another
-                                * suitable hard reg and make a copy.
-                                */
-                               int new_dest = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
-                               g_assert (new_dest >= 0);
-
-                               create_copy_ins (cfg, ins->dreg, new_dest, ins);
-                               DEBUG (g_print ("\tclob:b changing dreg R%d to %s\n", ins->dreg, mono_arch_regname (new_dest)));
-                               ins->dreg = new_dest;
-
-                               /* The hard reg is no longer needed */
-                               mono_regstate_free_int (rs, new_dest);
-                       }
-               }
-
-               /*
-                * 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 (spec [MONO_INST_DEST] == 'L') {
-                       int hreg;
-                       val = rs->iassign [ins->dreg];
-                       /* check special case when dreg have been moved from ecx (clob shift) */
-                       if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
-                               hreg = clob_dreg + 1;
-                       else
-                               hreg = ins->dreg + 1;
-
-                       /* base prev_dreg on fixed hreg, handle clob case */
-                       prev_dreg = hreg - 1;
+                       if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
+                           ins->inst_basereg == last_ins->inst_destbasereg &&
+                           ins->inst_offset == last_ins->inst_offset) {
+                                       ins->opcode = OP_COMPARE_IMM;
+                                       ins->sreg1 = last_ins->sreg1;
 
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
+                                       /* check if we can remove cmp reg,0 with test null */
+                                       if (!ins->inst_imm)
+                                               ins->opcode = OP_X86_TEST_NULL;
                                }
-                               val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
-                               rs->iassign [ins->dreg] = val;
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, prev_dreg, ins);
-                       }
 
-                       DEBUG (g_print ("\tassigned dreg (long) %s to dest R%d\n", mono_arch_regname (val), hreg - 1));
-                       rs->isymbolic [val] = hreg - 1;
-                       ins->dreg = val;
-                       
-                       val = rs->iassign [hreg];
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
+                       break;
+               case OP_LOAD_MEMBASE:
+               case OP_LOADI4_MEMBASE:
+                       /* 
+                        * Note: if reg1 = reg2 the load op is removed
+                        *
+                        * OP_STORE_MEMBASE_REG reg1, offset(basereg) 
+                        * OP_LOAD_MEMBASE offset(basereg), reg2
+                        * -->
+                        * OP_STORE_MEMBASE_REG reg1, offset(basereg)
+                        * OP_MOVE reg1, reg2
+                        */
+                       if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
+                                        || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
+                           ins->inst_basereg == last_ins->inst_destbasereg &&
+                           ins->inst_offset == last_ins->inst_offset) {
+                               if (ins->dreg == last_ins->sreg1) {
+                                       last_ins->next = ins->next;                             
+                                       ins = ins->next;                                
+                                       continue;
+                               } else {
+                                       //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
+                                       ins->opcode = OP_MOVE;
+                                       ins->sreg1 = last_ins->sreg1;
                                }
-                               val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, hreg, reginfo [hreg].flags);
-                               rs->iassign [hreg] = val;
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, hreg, ins);
-                       }
 
-                       DEBUG (g_print ("\tassigned hreg (long-high) %s to dest R%d\n", mono_arch_regname (val), hreg));
-                       rs->isymbolic [val] = hreg;
-                       /* save reg allocating into unused */
-                       ins->unused = val;
+                       /* 
+                        * Note: reg1 must be different from the basereg in the second load
+                        * Note: if reg1 = reg2 is equal then second load is removed
+                        *
+                        * OP_LOAD_MEMBASE offset(basereg), reg1
+                        * OP_LOAD_MEMBASE offset(basereg), reg2
+                        * -->
+                        * OP_LOAD_MEMBASE offset(basereg), reg1
+                        * OP_MOVE reg1, reg2
+                        */
+                       } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
+                                          || last_ins->opcode == OP_LOAD_MEMBASE) &&
+                             ins->inst_basereg != last_ins->dreg &&
+                             ins->inst_basereg == last_ins->inst_basereg &&
+                             ins->inst_offset == last_ins->inst_offset) {
 
-                       /* check if we can free our long reg */
-                       if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
-                               DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (val), hreg, reginfo [hreg].born_in));
-                               mono_regstate_free_int (rs, val);
-                       }
-               }
-               else if (ins->dreg >= MONO_MAX_IREGS) {
-                       int hreg;
-                       val = rs->iassign [ins->dreg];
-                       if (spec [MONO_INST_DEST] == 'l') {
-                               /* check special case when dreg have been moved from ecx (clob shift) */
-                               if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
-                                       hreg = clob_dreg + 1;
-                               else
-                                       hreg = ins->dreg + 1;
-
-                               /* base prev_dreg on fixed hreg, handle clob case */
-                               prev_dreg = hreg - 1;
-                       } else
-                               prev_dreg = ins->dreg;
-
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
-                               rs->iassign [ins->dreg] = val;
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, prev_dreg, ins);
-                       }
-                       DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
-                       rs->isymbolic [val] = prev_dreg;
-                       ins->dreg = val;
-                       /* handle cases where lreg needs to be eax:edx */
-                       if (spec [MONO_INST_DEST] == 'l') {
-                               /* check special case when dreg have been moved from ecx (clob shift) */
-                               int hreg = prev_dreg + 1;
-                               val = rs->iassign [hreg];
-                               if (val < 0) {
-                                       int spill = 0;
-                                       if (val < -1) {
-                                               /* the register gets spilled after this inst */
-                                               spill = -val -1;
-                                       }
-                                       val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, hreg, reginfo [hreg].flags);
-                                       rs->iassign [hreg] = val;
-                                       if (spill)
-                                               create_spilled_store (cfg, spill, val, hreg, ins);
-                               }
-                               DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
-                               rs->isymbolic [val] = hreg;
-                               if (ins->dreg == X86_EAX) {
-                                       if (val != X86_EDX)
-                                               create_copy_ins (cfg, val, X86_EDX, ins);
-                               } else if (ins->dreg == X86_EDX) {
-                                       if (val == X86_EAX) {
-                                               /* swap */
-                                               g_assert_not_reached ();
-                                       } else {
-                                               /* two forced copies */
-                                               create_copy_ins (cfg, val, X86_EDX, ins);
-                                               create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
-                                       }
+                               if (ins->dreg == last_ins->dreg) {
+                                       last_ins->next = ins->next;                             
+                                       ins = ins->next;                                
+                                       continue;
                                } else {
-                                       if (val == X86_EDX) {
-                                               create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
-                                       } else {
-                                               /* two forced copies */
-                                               create_copy_ins (cfg, val, X86_EDX, ins);
-                                               create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
-                                       }
-                               }
-                               if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
-                                       DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
-                                       mono_regstate_free_int (rs, val);
+                                       ins->opcode = OP_MOVE;
+                                       ins->sreg1 = last_ins->dreg;
                                }
-                       } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
-                               /* this instruction only outputs to EAX, need to copy */
-                               create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
-                       } 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);
-                       }
-               }
-               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);
-               }
-               /* 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);
-                       }
-                       if (ins->sreg1 < MONO_MAX_IREGS) {
-                               /* The argument is already in a hard reg, need to copy */
-                               MonoInst *copy = create_copy_ins (cfg, X86_EAX, ins->sreg1, NULL);
-                               insert_before_ins (ins, tmp, copy);
-                       }
-                       else
-                               /* force-set sreg1 */
-                               assign_ireg (rs, ins->sreg1, X86_EAX);
-                       ins->sreg1 = 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);
-                               }
+                               //g_assert_not_reached ();
 
-                               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);
+#if 0
+                       /* 
+                        * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
+                        * OP_LOAD_MEMBASE offset(basereg), reg
+                        * -->
+                        * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
+                        * OP_ICONST reg, imm
+                        */
+                       } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
+                                               || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
+                                  ins->inst_basereg == last_ins->inst_destbasereg &&
+                                  ins->inst_offset == last_ins->inst_offset) {
+                               //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
+                               ins->opcode = OP_ICONST;
+                               ins->inst_c0 = last_ins->inst_imm;
+                               g_assert_not_reached (); // check this rule
+#endif
                        }
-               } else if ((spec [MONO_INST_DEST] == 'L') && (spec [MONO_INST_SRC1] == 'L')) {
-                       /* force source to be same as dest */
-                       assign_ireg (rs, ins->sreg1, ins->dreg);
-                       assign_ireg (rs, ins->sreg1 + 1, ins->unused);
-
-                       DEBUG (g_print ("\tassigned sreg1 (long) %s to sreg1 R%d\n", mono_arch_regname (ins->dreg), ins->sreg1));
-                       DEBUG (g_print ("\tassigned sreg1 (long-high) %s to sreg1 R%d\n", mono_arch_regname (ins->unused), ins->sreg1 + 1));
-
-                       ins->sreg1 = ins->dreg;
+                       break;
+               case OP_LOADU1_MEMBASE:
+               case OP_LOADI1_MEMBASE:
                        /* 
-                        * No need for saving the reg, we know that src1=dest in this cases
-                        * ins->inst_c0 = ins->unused;
+                        * OP_STORE_MEMBASE_REG reg1, offset(basereg) 
+                        * OP_LOAD_MEMBASE offset(basereg), reg2
+                        * -->
+                        * OP_STORE_MEMBASE_REG reg1, offset(basereg)
+                        * CONV_I2/U2 reg1, reg2
                         */
-               }
-               else if (ins->sreg1 >= MONO_MAX_IREGS) {
-                       val = rs->iassign [ins->sreg1];
-                       prev_sreg1 = ins->sreg1;
-                       if (val < 0) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               if (0 && ins->opcode == OP_MOVE) {
-                                       /* 
-                                        * small optimization: the dest register is already allocated
-                                        * but the src one is not: we can simply assign the same register
-                                        * here and peephole will get rid of the instruction later.
-                                        * This optimization may interfere with the clobbering handling:
-                                        * it removes a mov operation that will be added again to handle clobbering.
-                                        * There are also some other issues that should with make testjit.
-                                        */
-                                       mono_regstate_alloc_int (rs, 1 << ins->dreg);
-                                       val = rs->iassign [ins->sreg1] = ins->dreg;
-                                       //g_assert (val >= 0);
-                                       DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
-                               } else {
-                                       //g_assert (val == -1); /* source cannot be spilled */
-                                       val = mono_x86_alloc_int_reg (cfg, tmp, ins, src1_mask, ins->sreg1, reginfo [ins->sreg1].flags);
-                                       rs->iassign [ins->sreg1] = val;
-                                       DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
-                               }
-                               if (spill) {
-                                       MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
-                                       insert_before_ins (ins, tmp, store);
-                               }
+                       if (last_ins && X86_IS_BYTE_REG (last_ins->sreg1) &&
+                               (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
+                                       ins->inst_basereg == last_ins->inst_destbasereg &&
+                                       ins->inst_offset == last_ins->inst_offset) {
+                               ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? CEE_CONV_I1 : CEE_CONV_U1;
+                               ins->sreg1 = last_ins->sreg1;
                        }
-                       rs->isymbolic [val] = prev_sreg1;
-                       ins->sreg1 = val;
-               } else {
-                       prev_sreg1 = -1;
-               }
-               /* handle clobbering of sreg1 */
-               if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
-                       MonoInst *sreg2_copy = NULL;
-                       MonoInst *copy = NULL;
-
-                       if (ins->dreg == ins->sreg2) {
-                               /* 
-                                * copying sreg1 to dreg could clobber sreg2, so allocate a new
-                                * register for it.
-                                */
-                               int reg2 = 0;
-
-                               reg2 = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->sreg2, 0);
-
-                               DEBUG (g_print ("\tneed to copy sreg2 %s to reg %s\n", mono_arch_regname (ins->sreg2), mono_arch_regname (reg2)));
-                               sreg2_copy = create_copy_ins (cfg, reg2, ins->sreg2, NULL);
-                               prev_sreg2 = ins->sreg2 = reg2;
-
-                               mono_regstate_free_int (rs, reg2);
+                       break;
+               case OP_LOADU2_MEMBASE:
+               case OP_LOADI2_MEMBASE:
+                       /* 
+                        * OP_STORE_MEMBASE_REG reg1, offset(basereg) 
+                        * OP_LOAD_MEMBASE offset(basereg), reg2
+                        * -->
+                        * OP_STORE_MEMBASE_REG reg1, offset(basereg)
+                        * CONV_I2/U2 reg1, reg2
+                        */
+                       if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
+                                       ins->inst_basereg == last_ins->inst_destbasereg &&
+                                       ins->inst_offset == last_ins->inst_offset) {
+                               ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? CEE_CONV_I2 : CEE_CONV_U2;
+                               ins->sreg1 = last_ins->sreg1;
                        }
-
-                       copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
-                       DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
-                       insert_before_ins (ins, tmp, copy);
-
-                       if (sreg2_copy)
-                               insert_before_ins (copy, tmp, sreg2_copy);
-
+                       break;
+               case CEE_CONV_I4:
+               case CEE_CONV_U4:
+               case OP_ICONV_TO_I4:
+               case OP_MOVE:
                        /*
-                        * Need to prevent sreg2 to be allocated to sreg1, since that
-                        * would screw up the previous copy.
+                        * Removes:
+                        *
+                        * OP_MOVE reg, reg 
                         */
-                       src2_mask &= ~ (1 << ins->sreg1);
-                       /* we set sreg1 to dest as well */
-                       prev_sreg1 = ins->sreg1 = ins->dreg;
-                       src2_mask &= ~ (1 << ins->dreg);
-               }
-
-               /*
-                * 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) {
-                               int spill = 0;
-                               if (val < -1) {
-                                       /* the register gets spilled after this inst */
-                                       spill = -val -1;
-                               }
-                               val = mono_x86_alloc_int_reg (cfg, tmp, ins, src2_mask, ins->sreg2, reginfo [ins->sreg2].flags);
-                               rs->iassign [ins->sreg2] = val;
-                               DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
-                               if (spill)
-                                       create_spilled_store (cfg, spill, val, prev_sreg2, ins);
-                       }
-                       rs->isymbolic [val] = prev_sreg2;
-                       ins->sreg2 = val;
-                       if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
-                               DEBUG (g_print ("\tassigned sreg2 %s to R%d, but ECX is needed (R%d)\n", mono_arch_regname (val), ins->sreg2, rs->iassign [X86_ECX]));
+                       if (ins->dreg == ins->sreg1) {
+                               if (last_ins)
+                                       last_ins->next = ins->next;                             
+                               ins = ins->next;
+                               continue;
                        }
-               } else {
-                       prev_sreg2 = -1;
-               }
-
-               if (spec [MONO_INST_CLOB] == 'c') {
-                       int j, s;
-                       guint32 clob_mask = X86_CALLEE_REGS;
-                       for (j = 0; j < MONO_MAX_IREGS; ++j) {
-                               s = 1 << j;
-                               if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
-                                       //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
-                               }
+                       /* 
+                        * Removes:
+                        *
+                        * OP_MOVE sreg, dreg 
+                        * OP_MOVE dreg, sreg
+                        */
+                       if (last_ins && last_ins->opcode == OP_MOVE &&
+                           ins->sreg1 == last_ins->dreg &&
+                           ins->dreg == last_ins->sreg1) {
+                               last_ins->next = ins->next;                             
+                               ins = ins->next;                                
+                               continue;
                        }
-               }
-               if (spec [MONO_INST_CLOB] == 'a') {
-                       guint32 clob_reg = X86_EAX;
-                       if (!(rs->ifree_mask & (1 << clob_reg)) && (rs->isymbolic [clob_reg] >= 8)) {
-                               DEBUG (g_print ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
-                               get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [clob_reg]);
-                               mono_regstate_free_int (rs, clob_reg);
+                       break;
+               case OP_X86_PUSH_MEMBASE:
+                       if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
+                                        last_ins->opcode == OP_STORE_MEMBASE_REG) &&
+                           ins->inst_basereg == last_ins->inst_destbasereg &&
+                           ins->inst_offset == last_ins->inst_offset) {
+                                   ins->opcode = OP_X86_PUSH;
+                                   ins->sreg1 = last_ins->sreg1;
                        }
+                       break;
                }
-               /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
-                       DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
-                       mono_regstate_free_int (rs, ins->sreg1);
-               }
-               if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
-                       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;
+               last_ins = ins;
+               ins = ins->next;
        }
+       bb->last_ins = last_ins;
+}
+
+static const int 
+branch_cc_table [] = {
+       X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
+       X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
+       X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
+};
+
+/* Maps CMP_... constants to X86_CC_... constants */
+static const int
+cc_table [] = {
+       X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
+       X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
+};
+
+static const int
+cc_signed_table [] = {
+       TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
+       FALSE, FALSE, FALSE, FALSE
+};
+
+void
+mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
+{
+       if (cfg->opt & MONO_OPT_PEEPHOLE)
+               peephole_pass_1 (cfg, bb);
 
-       g_free (reginfo);
-       g_free (reginfof);
-       g_list_free (fspill_list);
+       mono_local_regalloc (cfg, bb);
 }
 
 static unsigned char*
@@ -2735,7 +1859,7 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
        case OP_VCALL:
        case OP_VCALL_REG:
        case OP_VCALL_MEMBASE:
-               cinfo = get_call_info (((MonoCallInst*)ins)->signature, FALSE);
+               cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature, FALSE);
                if (cinfo->ret.storage == ArgValuetypeInReg) {
                        /* Pop the destination address from the stack */
                        x86_pop_reg (code, X86_ECX);
@@ -2753,7 +1877,6 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
                                }
                        }
                }
-               g_free (cinfo);
        default:
                break;
        }
@@ -2761,6 +1884,17 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
        return code;
 }
 
+/*
+ * emit_tls_get:
+ * @code: buffer to store code to
+ * @dreg: hard register where to place the result
+ * @tls_offset: offset info
+ *
+ * emit_tls_get emits in @code the native code that puts in the dreg register
+ * the item in the thread local storage identified by tls_offset.
+ *
+ * Returns: a pointer to the end of the stored code
+ */
 static guint8*
 emit_tls_get (guint8* code, int dreg, int tls_offset)
 {
@@ -2776,12 +1910,65 @@ emit_tls_get (guint8* code, int dreg, int tls_offset)
        x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
        x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
 #else
-       x86_prefix (code, X86_GS_PREFIX);
-       x86_mov_reg_mem (code, dreg, tls_offset, 4);                    
+       if (optimize_for_xen) {
+               x86_prefix (code, X86_GS_PREFIX);
+               x86_mov_reg_mem (code, dreg, 0, 4);
+               x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
+       } else {
+               x86_prefix (code, X86_GS_PREFIX);
+               x86_mov_reg_mem (code, dreg, tls_offset, 4);
+       }
 #endif
        return code;
 }
 
+/*
+ * emit_load_volatile_arguments:
+ *
+ *  Load volatile arguments from the stack to the original input registers.
+ * Required before a tail call.
+ */
+static guint8*
+emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
+{
+       MonoMethod *method = cfg->method;
+       MonoMethodSignature *sig;
+       MonoInst *inst;
+       CallInfo *cinfo;
+       guint32 i;
+
+       /* FIXME: Generate intermediate code instead */
+
+       sig = mono_method_signature (method);
+
+       cinfo = get_call_info (cfg->mempool, sig, FALSE);
+       
+       /* This is the opposite of the code in emit_prolog */
+
+       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
+               ArgInfo *ainfo = cinfo->args + i;
+               MonoType *arg_type;
+               inst = cfg->args [i];
+
+               if (sig->hasthis && (i == 0))
+                       arg_type = &mono_defaults.object_class->byval_arg;
+               else
+                       arg_type = sig->params [i - sig->hasthis];
+
+               /*
+                * On x86, the arguments are either in their original stack locations, or in
+                * global regs.
+                */
+               if (inst->opcode == OP_REGVAR) {
+                       g_assert (ainfo->storage == ArgOnStack);
+                       
+                       x86_mov_membase_reg (code, X86_EBP, inst->inst_offset, inst->dreg, 4);
+               }
+       }
+
+       return code;
+}
+
 #define REAL_PRINT_REG(text,reg) \
 mono_assert (reg >= 0); \
 x86_push_reg (code, X86_EAX); \
@@ -2844,11 +2031,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
        offset = code - cfg->native_code;
 
+       mono_debug_open_block (cfg, bb, offset);
+
        ins = bb->code;
        while (ins) {
                offset = code - cfg->native_code;
 
-               max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
+               max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
 
                if (offset > (cfg->code_size - max_len - 16)) {
                        cfg->code_size *= 2;
@@ -2965,6 +2154,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_X86_SUB_MEMBASE:
                        x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
                        break;
+               case OP_X86_AND_MEMBASE_IMM:
+                       x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
+                       break;
+               case OP_X86_OR_MEMBASE_IMM:
+                       x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
+                       break;
+               case OP_X86_XOR_MEMBASE_IMM:
+                       x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
+                       break;
                case OP_X86_INC_MEMBASE:
                        x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
                        break;
@@ -2980,7 +2178,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_X86_MUL_MEMBASE:
                        x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
                        break;
-               case CEE_BREAK:
+               case OP_BREAK:
                        x86_breakpoint (code);
                        break;
                case OP_ADDCC:
@@ -3050,6 +2248,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
                        break;
                case CEE_XOR:
+               case OP_IXOR:
                        x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
                        break;
                case OP_XOR_IMM:
@@ -3080,14 +2279,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        guint8 *jump_to_end;
 
                        /* handle shifts below 32 bits */
-                       x86_shld_reg (code, ins->unused, ins->sreg1);
+                       x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
                        x86_shift_reg (code, X86_SHL, ins->sreg1);
 
                        x86_test_reg_imm (code, X86_ECX, 32);
                        jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
 
                        /* handle shift over 32 bit */
-                       x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
+                       x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
                        x86_clear_reg (code, ins->sreg1);
                        
                        x86_patch (jump_to_end, code);
@@ -3097,15 +2296,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        guint8 *jump_to_end;
 
                        /* handle shifts below 32 bits */
-                       x86_shrd_reg (code, ins->sreg1, ins->unused);
-                       x86_shift_reg (code, X86_SAR, ins->unused);
+                       x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
+                       x86_shift_reg (code, X86_SAR, ins->backend.reg3);
 
                        x86_test_reg_imm (code, X86_ECX, 32);
                        jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
 
                        /* handle shifts over 31 bits */
-                       x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
-                       x86_shift_reg_imm (code, X86_SAR, ins->unused, 31);
+                       x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
+                       x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
                        
                        x86_patch (jump_to_end, code);
                        }
@@ -3114,47 +2313,47 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        guint8 *jump_to_end;
 
                        /* handle shifts below 32 bits */
-                       x86_shrd_reg (code, ins->sreg1, ins->unused);
-                       x86_shift_reg (code, X86_SHR, ins->unused);
+                       x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
+                       x86_shift_reg (code, X86_SHR, ins->backend.reg3);
 
                        x86_test_reg_imm (code, X86_ECX, 32);
                        jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
 
                        /* handle shifts over 31 bits */
-                       x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
-                       x86_shift_reg_imm (code, X86_SHR, ins->unused, 31);
+                       x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
+                       x86_clear_reg (code, ins->backend.reg3);
                        
                        x86_patch (jump_to_end, code);
                        }
                        break;
                case OP_LSHL_IMM:
                        if (ins->inst_imm >= 32) {
-                               x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
+                               x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
                                x86_clear_reg (code, ins->sreg1);
-                               x86_shift_reg_imm (code, X86_SHL, ins->unused, ins->inst_imm - 32);
+                               x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
                        } else {
-                               x86_shld_reg_imm (code, ins->unused, ins->sreg1, ins->inst_imm);
+                               x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
                                x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
                        }
                        break;
                case OP_LSHR_IMM:
                        if (ins->inst_imm >= 32) {
-                               x86_mov_reg_reg (code, ins->sreg1, ins->unused,  4);
-                               x86_shift_reg_imm (code, X86_SAR, ins->unused, 0x1f);
+                               x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3,  4);
+                               x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
                                x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
                        } else {
-                               x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
-                               x86_shift_reg_imm (code, X86_SAR, ins->unused, ins->inst_imm);
+                               x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
+                               x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
                        }
                        break;
                case OP_LSHR_UN_IMM:
                        if (ins->inst_imm >= 32) {
-                               x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
-                               x86_clear_reg (code, ins->unused);
+                               x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
+                               x86_clear_reg (code, ins->backend.reg3);
                                x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
                        } else {
-                               x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
-                               x86_shift_reg_imm (code, X86_SHR, ins->unused, ins->inst_imm);
+                               x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
+                               x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
                        }
                        break;
                case CEE_NOT:
@@ -3173,7 +2372,62 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
                        break;
                case OP_MUL_IMM:
-                       x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
+                       switch (ins->inst_imm) {
+                       case 2:
+                               /* MOV r1, r2 */
+                               /* ADD r1, r1 */
+                               if (ins->dreg != ins->sreg1)
+                                       x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
+                               x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
+                               break;
+                       case 3:
+                               /* LEA r1, [r2 + r2*2] */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
+                               break;
+                       case 5:
+                               /* LEA r1, [r2 + r2*4] */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
+                               break;
+                       case 6:
+                               /* LEA r1, [r2 + r2*2] */
+                               /* ADD r1, r1          */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
+                               x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
+                               break;
+                       case 9:
+                               /* LEA r1, [r2 + r2*8] */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
+                               break;
+                       case 10:
+                               /* LEA r1, [r2 + r2*4] */
+                               /* ADD r1, r1          */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
+                               x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
+                               break;
+                       case 12:
+                               /* LEA r1, [r2 + r2*2] */
+                               /* SHL r1, 2           */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
+                               x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
+                               break;
+                       case 25:
+                               /* LEA r1, [r2 + r2*4] */
+                               /* LEA r1, [r1 + r1*4] */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
+                               x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
+                               break;
+                       case 100:
+                               /* LEA r1, [r2 + r2*4] */
+                               /* SHL r1, 2           */
+                               /* LEA r1, [r1 + r1*4] */
+                               x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
+                               x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
+                               x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
+                               break;
+                       default:
+                               x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
+                               break;
+                       }
                        break;
                case CEE_MUL_OVF:
                        x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
@@ -3249,7 +2503,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case CEE_CONV_U4:
                        g_assert_not_reached ();
-               case CEE_JMP: {
+               case OP_JMP: {
                        /*
                         * Note: this 'frame destruction' logic is useful for tail calls, too.
                         * Keep in sync with the code in emit_epilog.
@@ -3264,6 +2518,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        g_assert (!cfg->method->save_lmf);
 
+                       code = emit_load_volatile_arguments (cfg, code);
+
                        if (cfg->used_int_regs & (1 << X86_EBX))
                                pos -= 4;
                        if (cfg->used_int_regs & (1 << X86_EDI))
@@ -3395,7 +2651,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_pop_reg (code, X86_EDI);
                        break;
                case OP_X86_LEA:
-                       x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
+                       x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
                        break;
                case OP_X86_LEA_MEMBASE:
                        x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
@@ -3405,15 +2661,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_LOCALLOC:
                        /* keep alignment */
-                       x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
-                       x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
+                       x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
+                       x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
                        code = mono_emit_stack_alloc (code, ins);
                        x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
                        break;
                case CEE_RET:
                        x86_ret (code);
                        break;
-               case CEE_THROW: {
+               case OP_THROW: {
                        x86_push_reg (code, ins->sreg1);
                        code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, 
                                                          (gpointer)"mono_arch_throw_exception");
@@ -3426,13 +2682,20 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                }
                case OP_CALL_HANDLER: 
+                       /* Align stack */
+#ifdef __APPLE__
+                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 12);
+#endif
                        mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
                        x86_call_imm (code, 0);
+#ifdef __APPLE__
+                       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
+#endif
                        break;
                case OP_LABEL:
                        ins->inst_c0 = code - cfg->native_code;
                        break;
-               case CEE_BR:
+               case OP_BR:
                        //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
                        //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
                        //break;
@@ -3464,27 +2727,12 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_jump_reg (code, ins->sreg1);
                        break;
                case OP_CEQ:
-                       x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
-                       x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
-                       break;
                case OP_CLT:
-                       x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
-                       x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
-                       break;
                case OP_CLT_UN:
-                       x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
-                       x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
-                       break;
                case OP_CGT:
-                       x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
-                       x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
-                       break;
                case OP_CGT_UN:
-                       x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
-                       x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
-                       break;
                case OP_CNE:
-                       x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
+                       x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
                        x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
                        break;
                case OP_COND_EXC_EQ:
@@ -3497,12 +2745,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_COND_EXC_GE_UN:
                case OP_COND_EXC_LE:
                case OP_COND_EXC_LE_UN:
+                       EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
+                       break;
                case OP_COND_EXC_OV:
                case OP_COND_EXC_NO:
                case OP_COND_EXC_C:
                case OP_COND_EXC_NC:
-                       EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], 
-                                                   (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
+                       EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
                        break;
                case CEE_BEQ:
                case CEE_BNE_UN:
@@ -3514,7 +2763,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case CEE_BGE_UN:
                case CEE_BLE:
                case CEE_BLE_UN:
-                       EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
+                       EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
                        break;
 
                /* floating point opcodes */
@@ -3615,7 +2864,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
                        x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
                        x86_pop_reg (code, ins->dreg);
-                       x86_pop_reg (code, ins->unused);
+                       x86_pop_reg (code, ins->backend.reg3);
                        x86_fldcw_membase (code, X86_ESP, 0);
                        x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
                        break;
@@ -3650,6 +2899,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                }
                case OP_LCONV_TO_OVF_I: {
                        guint8 *br [3], *label [1];
+                       MonoInst *tins;
 
                        /* 
                         * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
@@ -3664,8 +2914,18 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        label [0] = code;
 
                        /* throw exception */
-                       mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
-                       x86_jump32 (code, 0);
+                       tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
+                       if (tins) {
+                               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
+                               if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
+                                       x86_jump8 (code, 0);
+                               else
+                                       x86_jump32 (code, 0);
+                       } else {
+                               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
+                               x86_jump32 (code, 0);
+                       }
+       
        
                        x86_patch (br [0], code);
                        /* our top bit is set, check that top word is 0xfffffff */
@@ -3957,7 +3217,18 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_FBGT:
                case OP_FBGT_UN:
                        if (cfg->opt & MONO_OPT_FCMOV) {
-                               EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
+                               if (ins->opcode == OP_FBGT) {
+                                       guchar *br1;
+
+                                       /* skip branch if C1=1 */
+                                       br1 = code;
+                                       x86_branch8 (code, X86_CC_P, 0, FALSE);
+                                       /* branch if (C0 | C3) = 1 */
+                                       EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
+                                       x86_patch (br1, code);
+                               } else {
+                                       EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
+                               }
                                break;
                        }
                        x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
@@ -4027,7 +3298,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
                        EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
                        break;
-               case CEE_CKFINITE: {
+               case OP_CKFINITE: {
                        x86_push_reg (code, X86_EAX);
                        x86_fxam (code);
                        x86_fnstsw (code);
@@ -4041,6 +3312,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        code = emit_tls_get (code, ins->dreg, ins->inst_offset);
                        break;
                }
+               case OP_MEMORY_BARRIER: {
+                       /* Not needed on x86 */
+                       break;
+               }
                case OP_ATOMIC_ADD_I4: {
                        int dreg = ins->dreg;
 
@@ -4246,7 +3521,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        x86_push_reg (code, X86_EBP);
        x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
 
-       alloc_size = cfg->stack_offset;
+       alloc_size = cfg->stack_offset;
        pos = 0;
 
        if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
@@ -4255,11 +3530,6 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        guint8 *buf;
 
                        code = emit_tls_get ( code, X86_EAX, lmf_tls_offset);
-#ifdef PLATFORM_WIN32
-                       /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
-                       /* FIXME: Add a separate key for LMF to avoid this */
-                       x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
-#endif
                        x86_test_reg_reg (code, X86_EAX, X86_EAX);
                        buf = code;
                        x86_branch8 (code, X86_CC_NE, 0, 0);
@@ -4267,8 +3537,12 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
                        x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
                        x86_patch (buf, code);
-               }
-               else {
+#ifdef PLATFORM_WIN32
+                       /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
+                       /* FIXME: Add a separate key for LMF to avoid this */
+                       x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
+#endif
+               } else {
                        g_assert (!cfg->compile_aot);
                        x86_push_imm (code, cfg->domain);
                        code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
@@ -4289,43 +3563,50 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                x86_push_reg (code, X86_EDI);
                x86_push_reg (code, X86_EBX);
 
-               /* save method info */
-               x86_push_imm (code, method);
+               if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
+                       /*
+                        * Optimized version which uses the mono_lmf TLS variable instead of indirection
+                        * through the mono_lmf_addr TLS variable.
+                        */
+                       /* %eax = previous_lmf */
+                       x86_prefix (code, X86_GS_PREFIX);
+                       x86_mov_reg_mem (code, X86_EAX, lmf_tls_offset, 4);
+                       /* skip method_info + lmf */
+                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
+                       /* push previous_lmf */
+                       x86_push_reg (code, X86_EAX);
+                       /* new lmf = ESP */
+                       x86_prefix (code, X86_GS_PREFIX);
+                       x86_mov_mem_reg (code, lmf_tls_offset, X86_ESP, 4);
+               } else {
+                       /* get the address of lmf for the current thread */
+                       /* 
+                        * This is performance critical so we try to use some tricks to make
+                        * it fast.
+                        */                                                                        
 
-               /* get the address of lmf for the current thread */
-               /* 
-                * 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 */
-                       code = emit_tls_get (code, X86_EAX, lmf_tls_offset);
+                       if (lmf_addr_tls_offset != -1) {
+                               /* Load lmf quicky using the GS register */
+                               code = emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
 #ifdef PLATFORM_WIN32
-                       /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
-                       /* FIXME: Add a separate key for LMF to avoid this */
-                       x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
+                               /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
+                               /* FIXME: Add a separate key for LMF to avoid this */
+                               x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
 #endif
-               }
-               else {
-                       if (cfg->compile_aot) {
-                               /* The GOT var does not exist yet */
-                               x86_call_imm (code, 0);
-                               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
-                               x86_pop_reg (code, X86_EAX);
-                               x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
-                               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
-                               x86_call_membase (code, X86_EAX, 0xf0f0f0f0);
-                       }
-                       else
+                       } else {
                                code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
-               }
+                       }
+
+                       /* Skip method info */
+                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
 
-               /* push lmf */
-               x86_push_reg (code, X86_EAX); 
-               /* push *lfm (previous_lmf) */
-               x86_push_membase (code, X86_EAX, 0);
-               /* *(lmf) = ESP */
-               x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
+                       /* push lmf */
+                       x86_push_reg (code, X86_EAX); 
+                       /* push *lfm (previous_lmf) */
+                       x86_push_membase (code, X86_EAX, 0);
+                       /* *(lmf) = ESP */
+                       x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
+               }
        } else {
 
                if (cfg->used_int_regs & (1 << X86_EBX)) {
@@ -4346,6 +3627,20 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
        alloc_size -= pos;
 
+#if __APPLE__
+       /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
+       {
+               int tot = alloc_size + pos + 4 + 4; /* ret ip + ebp */
+               if (tot & 4) {
+                       tot += 4;
+                       alloc_size += 4;
+               }
+               if (tot & 8) {
+                       alloc_size += 8;
+               }
+       }
+#endif
+
        if (alloc_size) {
                /* See mono_emit_stack_alloc */
 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
@@ -4362,6 +3657,15 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 #endif
        }
 
+#if __APPLE_
+       /* check the stack is aligned */
+       x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
+       x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
+       x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
+       x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
+       x86_breakpoint (code);
+#endif
+
         /* compute max_offset in order to use short forward jumps */
        max_offset = 0;
        if (cfg->opt & MONO_OPT_BRANCH) {
@@ -4379,7 +3683,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                if (ins->opcode == OP_LABEL)
                                        ins->inst_c1 = max_offset;
                                
-                               max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
+                               max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
                                ins = ins->next;
                        }
                }
@@ -4393,7 +3697,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        pos = 0;
 
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
-               inst = cfg->varinfo [pos];
+               inst = cfg->args [pos];
                if (inst->opcode == OP_REGVAR) {
                        x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
                        if (cfg->verbose_level > 2)
@@ -4435,43 +3739,57 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
                code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
 
-       /* the code restoring the registers must be kept in sync with CEE_JMP */
+       /* the code restoring the registers must be kept in sync with OP_JMP */
        pos = 0;
        
        if (method->save_lmf) {
                gint32 prev_lmf_reg;
+               gint32 lmf_offset = -sizeof (MonoLMF);
 
-               /* Find a spare register */
-               switch (sig->ret->type) {
-               case MONO_TYPE_I8:
-               case MONO_TYPE_U8:
-                       prev_lmf_reg = X86_EDI;
-                       cfg->used_int_regs |= (1 << X86_EDI);
-                       break;
-               default:
-                       prev_lmf_reg = X86_EDX;
-                       break;
-               }
+               if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
+                       /*
+                        * Optimized version which uses the mono_lmf TLS variable instead of indirection
+                        * through the mono_lmf_addr TLS variable.
+                        */
+                       /* reg = previous_lmf */
+                       x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
+
+                       /* lmf = previous_lmf */
+                       x86_prefix (code, X86_GS_PREFIX);
+                       x86_mov_mem_reg (code, lmf_tls_offset, X86_ECX, 4);
+               } else {
+                       /* Find a spare register */
+                       switch (sig->ret->type) {
+                       case MONO_TYPE_I8:
+                       case MONO_TYPE_U8:
+                               prev_lmf_reg = X86_EDI;
+                               cfg->used_int_regs |= (1 << X86_EDI);
+                               break;
+                       default:
+                               prev_lmf_reg = X86_EDX;
+                               break;
+                       }
 
-               /* reg = previous_lmf */
-               x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, -32, 4);
+                       /* reg = previous_lmf */
+                       x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
 
-               /* ecx = lmf */
-               x86_mov_reg_membase (code, X86_ECX, X86_EBP, -28, 4);
+                       /* ecx = lmf */
+                       x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
 
-               /* *(lmf) = previous_lmf */
-               x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
+                       /* *(lmf) = previous_lmf */
+                       x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
+               }
 
                /* restore caller saved regs */
                if (cfg->used_int_regs & (1 << X86_EBX)) {
-                       x86_mov_reg_membase (code, X86_EBX, X86_EBP, -20, 4);
+                       x86_mov_reg_membase (code, X86_EBX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
                }
 
                if (cfg->used_int_regs & (1 << X86_EDI)) {
-                       x86_mov_reg_membase (code, X86_EDI, X86_EBP, -16, 4);
+                       x86_mov_reg_membase (code, X86_EDI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
                }
                if (cfg->used_int_regs & (1 << X86_ESI)) {
-                       x86_mov_reg_membase (code, X86_ESI, X86_EBP, -12, 4);
+                       x86_mov_reg_membase (code, X86_ESI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
                }
 
                /* EBP is restored by LEAVE */
@@ -4501,7 +3819,7 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        }
 
        /* Load returned vtypes into registers if needed */
-       cinfo = get_call_info (sig, FALSE);
+       cinfo = get_call_info (cfg->mempool, sig, FALSE);
        if (cinfo->ret.storage == ArgValuetypeInReg) {
                for (quad = 0; quad < 2; quad ++) {
                        switch (cinfo->ret.pair_storage [quad]) {
@@ -4538,8 +3856,6 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        else
                x86_ret (code);
 
-       g_free (cinfo);
-
        cfg->code_len = code - cfg->native_code;
 
        g_assert (cfg->code_len < cfg->code_size);
@@ -4603,19 +3919,10 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                                patch_info->type = MONO_PATCH_INFO_NONE;
                        }
                        else {
-                               guint32 got_reg = X86_EAX;
                                guint32 size;
 
                                /* Compute size of code following the push <OFFSET> */
-                               if (cfg->compile_aot) {
-                                       size = 5 + 6;
-                                       if (!cfg->got_var)
-                                               size += 32;
-                                       else if (cfg->got_var->opcode == OP_REGOFFSET)
-                                               size += 6;
-                               }
-                               else
-                                       size = 5 + 5;
+                               size = 5 + 5;
 
                                if ((code - cfg->native_code) - throw_ip < 126 - size) {
                                        /* Use the shorter form */
@@ -4633,32 +3940,11 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                                        exc_throw_start [nthrows] = code;
                                }
 
-                               if (cfg->compile_aot) {          
-                                       /*
-                                        * Since the patches are generated by the back end, there is                                     * no way to generate a got_var at this point.   
-                                        */
-                                       if (!cfg->got_var) {
-                                               x86_call_imm (code, 0);
-                                               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
-                                               x86_pop_reg (code, X86_EAX);
-                                               x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
-                                       }
-                                       else {
-                                               if (cfg->got_var->opcode == OP_REGOFFSET)
-                                                       x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
-                                               else
-                                                       got_reg = cfg->got_var->dreg;
-                                       }
-                               }
-
-                               x86_push_imm (code, exc_class->type_token);
+                               x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
                                patch_info->data.name = "mono_arch_throw_corlib_exception";
                                patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
                                patch_info->ip.i = code - cfg->native_code;
-                               if (cfg->compile_aot)
-                                       x86_call_membase (code, got_reg, 0xf0f0f0f0);
-                               else
-                                       x86_call_code (code, 0);
+                               x86_call_code (code, 0);
                                x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
                                while (buf < buf2)
                                        x86_nop (buf);
@@ -4692,66 +3978,6 @@ mono_arch_flush_register_windows (void)
 {
 }
 
-#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
-
-static void
-setup_stack (MonoJitTlsData *tls)
-{
-       pthread_t self = pthread_self();
-       pthread_attr_t attr;
-       size_t stsize = 0;
-       struct sigaltstack sa;
-       guint8 *staddr = NULL;
-       guint8 *current = (guint8*)&staddr;
-
-       if (mono_running_on_valgrind ())
-               return;
-
-       /* Determine stack boundaries */
-       pthread_attr_init( &attr );
-#ifdef HAVE_PTHREAD_GETATTR_NP
-       pthread_getattr_np( self, &attr );
-#else
-#ifdef HAVE_PTHREAD_ATTR_GET_NP
-       pthread_attr_get_np( self, &attr );
-#elif defined(sun)
-       pthread_attr_getstacksize( &attr, &stsize );
-#else
-#error "Not implemented"
-#endif
-#endif
-#ifndef sun
-       pthread_attr_getstack( &attr, (void**)&staddr, &stsize );
-#endif
-
-       g_assert (staddr);
-
-       g_assert ((current > staddr) && (current < staddr + stsize));
-
-       tls->end_of_stack = staddr + stsize;
-
-       /*
-        * threads created by nptl does not seem to have a guard page, and
-        * since the main thread is not created by us, we can't even set one.
-        * Increasing stsize fools the SIGSEGV signal handler into thinking this
-        * is a stack overflow exception.
-        */
-       tls->stack_size = stsize + getpagesize ();
-
-       /* Setup an alternate signal stack */
-       tls->signal_stack = mmap (0, SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
-       tls->signal_stack_size = SIGNAL_STACK_SIZE;
-
-       g_assert (tls->signal_stack);
-
-       sa.ss_sp = tls->signal_stack;
-       sa.ss_size = SIGNAL_STACK_SIZE;
-       sa.ss_flags = SS_ONSTACK;
-       sigaltstack (&sa, NULL);
-}
-
-#endif
-
 /*
  * Support for fast access to the thread-local lmf structure using the GS
  * segment register on NPTL + kernel 2.6.x.
@@ -4781,50 +4007,52 @@ mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
                        if (thread_tls_offset >= 64)
                                thread_tls_offset = -1;
 #else
+#if MONO_XEN_OPT
+                       optimize_for_xen = access ("/proc/xen", F_OK) == 0;
+#endif
                        tls_offset_inited = TRUE;
                        appdomain_tls_offset = mono_domain_get_tls_offset ();
                        lmf_tls_offset = mono_get_lmf_tls_offset ();
+                       lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
                        thread_tls_offset = mono_thread_get_tls_offset ();
 #endif
                }
        }               
-
-#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
-       setup_stack (tls);
-#endif
 }
 
 void
 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 {
-#ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
-       struct sigaltstack sa;
-
-       sa.ss_sp = tls->signal_stack;
-       sa.ss_size = SIGNAL_STACK_SIZE;
-       sa.ss_flags = SS_DISABLE;
-       sigaltstack  (&sa, NULL);
-
-       if (tls->signal_stack)
-               munmap (tls->signal_stack, SIGNAL_STACK_SIZE);
-#endif
 }
 
 void
 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
 {
+       MonoCallInst *call = (MonoCallInst*)inst;
+       CallInfo *cinfo = get_call_info (cfg->mempool, inst->signature, FALSE);
 
        /* 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 (cinfo->args [0].storage == ArgInIReg) {
+                       MonoInst *this;
+                       MONO_INST_NEW (cfg, this, OP_MOVE);
+                       this->type = this_type;
+                       this->sreg1 = this_reg;
+                       this->dreg = mono_regstate_next_int (cfg->rs);
+                       mono_bblock_add_inst (cfg->cbb, this);
+
+                       mono_call_inst_add_outarg_reg (cfg, call, this->dreg, cinfo->args [0].reg, FALSE);
+               }
+               else {
+                       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) {
-               CallInfo * cinfo = get_call_info (inst->signature, FALSE);
                MonoInst *vtarg;
 
                if (cinfo->ret.storage == ArgValuetypeInReg) {
@@ -4839,19 +4067,24 @@ mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_re
                        vtarg->sreg1 = vt_reg;
                        mono_bblock_add_inst (cfg->cbb, vtarg);
                }
-               else {
+               else if (cinfo->ret.storage == ArgInIReg) {
+                       /* The return address is passed in a register */
+                       MONO_INST_NEW (cfg, vtarg, OP_MOVE);
+                       vtarg->sreg1 = vt_reg;
+                       vtarg->dreg = mono_regstate_next_int (cfg->rs);
+                       mono_bblock_add_inst (cfg->cbb, vtarg);
+
+                       mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
+               } else {
                        MonoInst *vtarg;
                        MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
                        vtarg->type = STACK_MP;
                        vtarg->sreg1 = vt_reg;
                        mono_bblock_add_inst (cfg->cbb, vtarg);
                }
-
-               g_free (cinfo);
        }
 }
 
-
 MonoInst*
 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
@@ -4885,6 +4118,9 @@ mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethod
                        ins->inst_i1 = args [1];
                }
 #endif
+       } else if (cmethod->klass == mono_defaults.thread_class &&
+                          strcmp (cmethod->name, "MemoryBarrier") == 0) {
+               MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
        } else if(cmethod->klass->image == mono_defaults.corlib &&
                           (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
                           (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
@@ -4913,7 +4149,7 @@ mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethod
                        ins->inst_i0 = args [0];
                        ins->inst_i1 = args [1];
                } else if (strcmp (cmethod->name, "Add") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
-                       MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_I4);
+                       MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
 
                        ins->inst_i0 = args [0];
                        ins->inst_i1 = args [1];
@@ -4999,7 +4235,21 @@ mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
         * 0xff m=2,o=2 imm32
         */
        code -= 6;
-       if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
+
+       /* 
+        * A given byte sequence can match more than case here, so we have to be
+        * really careful about the ordering of the cases. Longer sequences
+        * come first.
+        */
+       if ((code [-2] == 0x8b) && (x86_modrm_mod (code [-1]) == 0x2) && (code [4] == 0xff) && (x86_modrm_reg (code [5]) == 0x2) && (x86_modrm_mod (code [5]) == 0x0)) {
+               /*
+                * This is an interface call
+                * 8b 80 0c e8 ff ff       mov    0xffffe80c(%eax),%eax
+                * ff 10                   call   *(%eax)
+                */
+               reg = x86_modrm_rm (code [5]);
+               disp = 0;
+       } else if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
                reg = code [4] & 0x07;
                disp = (signed char)code [5];
        } else {
@@ -5010,7 +4260,7 @@ mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
                        return NULL;
                } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
                        /*
-                        * This is a interface call: should check the above code can't catch it earlier 
+                        * This is a interface call
                         * 8b 40 30   mov    0x30(%eax),%eax
                         * ff 10      call   *(%eax)
                         */
@@ -5024,22 +4274,94 @@ mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
        return (gpointer*)(((gint32)(regs [reg])) + disp);
 }
 
-gpointer
-mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs)
+gpointer
+mono_arch_get_this_arg_from_call (MonoMethodSignature *sig, gssize *regs, guint8 *code)
 {
-       guint8 reg = 0;
-       gint32 disp = 0;
+       guint32 esp = regs [X86_ESP];
+       CallInfo *cinfo;
+       gpointer res;
 
-       code -= 7;
-       if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 3) && (x86_modrm_reg (code [1]) == X86_EAX) && (code [2] == 0x8b) && (code [3] == 0x40) && (code [5] == 0xff) && (code [6] == 0xd0)) {
-               reg = x86_modrm_rm (code [1]);
-               disp = code [4];
+       cinfo = get_call_info (NULL, sig, FALSE);
 
-               if (reg == X86_EAX)
-                       return NULL;
-               else
-                       return (gpointer*)(((gint32)(regs [reg])) + disp);
+       /*
+        * The stack looks like:
+        * <other args>
+        * <this=delegate>
+        * <possible vtype return address>
+        * <return addr>
+        * <4 pointers pushed by mono_arch_create_trampoline_code ()>
+        */
+       res = (((MonoObject**)esp) [5 + (cinfo->args [0].offset / 4)]);
+       g_free (cinfo);
+       return res;
+}
+
+gpointer
+mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
+{
+       guint8 *code, *start;
+       MonoDomain *domain = mono_domain_get ();
+
+       /* FIXME: Support more cases */
+       if (MONO_TYPE_ISSTRUCT (sig->ret))
+               return NULL;
+
+       /*
+        * The stack contains:
+        * <delegate>
+        * <return addr>
+        */
+
+       if (has_target) {
+               mono_domain_lock (domain);
+               start = code = mono_code_manager_reserve (domain->code_mp, 64);
+               mono_domain_unlock (domain);
+
+               /* Replace the this argument with the target */
+               x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
+               x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, target), 4);
+               x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
+               x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
+
+               g_assert ((code - start) < 64);
+       } else {
+               if (sig->param_count == 0) {
+                       mono_domain_lock (domain);
+                       start = code = mono_code_manager_reserve (domain->code_mp, 32 + (sig->param_count * 8));
+                       mono_domain_unlock (domain);
+               
+                       x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
+                       x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
+               } else {
+                       /* 
+                        * The code below does not work in the presence of exceptions, since it 
+                        * creates a new frame.
+                        */
+                       start = NULL;
+#if 0
+                       for (i = 0; i < sig->param_count; ++i)
+                               if (!mono_is_regsize_var (sig->params [i]))
+                                       return NULL;
+
+                       mono_domain_lock (domain);
+                       start = code = mono_code_manager_reserve (domain->code_mp, 32 + (sig->param_count * 8));
+                       mono_domain_unlock (domain);
+
+                       /* Load this == delegate */
+                       x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
+
+                       /* Push arguments in opposite order, taking changes in ESP into account */
+                       for (i = 0; i < sig->param_count; ++i)
+                               x86_push_membase (code, X86_ESP, 4 + (sig->param_count * 4));
+
+                       /* Call the delegate */
+                       x86_call_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
+                       if (sig->param_count > 0)
+                               x86_alu_reg_imm (code, X86_ADD, X86_ESP, sig->param_count * 4);
+                       x86_ret (code);
+#endif
+               }
        }
 
-       return NULL;
+       return start;
 }