2009-12-21 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-x86.c
index ffb3666e3a0d4e971f68d0e5a566ec9b3b05d5d3..e0f010dc451871d157c31f40737ce7af42a64fac 100644 (file)
 #include <mono/metadata/mono-debug.h>
 #include <mono/utils/mono-math.h>
 #include <mono/utils/mono-counters.h>
+#include <mono/utils/mono-mmap.h>
 
 #include "trace.h"
 #include "mini-x86.h"
-#include "inssel.h"
 #include "cpu-x86.h"
+#include "ir-emit.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;
@@ -40,7 +40,7 @@ static gboolean optimize_for_xen = TRUE;
 #define optimize_for_xen 0
 #endif
 
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
 static gboolean is_win32 = TRUE;
 #else
 static gboolean is_win32 = FALSE;
@@ -55,7 +55,7 @@ static CRITICAL_SECTION mini_arch_mutex;
 
 #define ARGS_OFFSET 8
 
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_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))
 #else
@@ -65,6 +65,15 @@ static CRITICAL_SECTION mini_arch_mutex;
 MonoBreakpointInfo
 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
 
+/*
+ * The code generated for sequence points reads from this location, which is
+ * made read-only when single stepping is enabled.
+ */
+static gpointer ss_trigger_page;
+
+/* Enabled breakpoints read from this trigger page */
+static gpointer bp_trigger_page;
+
 const char*
 mono_arch_regname (int reg)
 {
@@ -171,7 +180,7 @@ typedef struct {
 
 static X86_Reg_No param_regs [] = { 0 };
 
-#if defined(PLATFORM_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
+#if defined(TARGET_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
 #define SMALL_STRUCTS_IN_REGS
 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
 #endif
@@ -287,18 +296,12 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
  * For x86 win32, see ???.
  */
 static CallInfo*
-get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
+get_call_info_internal (MonoGenericSharingContext *gsctx, CallInfo *cinfo, MonoMethodSignature *sig, gboolean is_pinvoke)
 {
        guint32 i, gr, fr;
        MonoType *ret_type;
        int n = sig->hasthis + sig->param_count;
        guint32 stack_size = 0;
-       CallInfo *cinfo;
-
-       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;
@@ -476,6 +479,20 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
        return cinfo;
 }
 
+static CallInfo*
+get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
+{
+       int n = sig->hasthis + sig->param_count;
+       CallInfo *cinfo;
+
+       if (mp)
+               cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
+       else
+               cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
+
+       return get_call_info_internal (gsctx, cinfo, sig, is_pinvoke);
+}
+
 /*
  * mono_arch_get_argument_info:
  * @csig:  a method signature
@@ -486,6 +503,9 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
  * padding. arg_info should be large enought to hold param_count + 1 entries. 
  *
  * Returns the size of the argument area on the stack.
+ * This should be signal safe, since it is called from
+ * mono_arch_find_jit_info_ext ().
+ * FIXME: The metadata calls might not be signal safe.
  */
 int
 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
@@ -496,7 +516,10 @@ mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJit
        int offset = 8;
        CallInfo *cinfo;
 
-       cinfo = get_call_info (NULL, NULL, csig, FALSE);
+       /* Avoid g_malloc as it is not signal safe */
+       cinfo = (CallInfo*)g_newa (guint8*, sizeof (CallInfo) + (sizeof (ArgInfo) * (csig->param_count + 1)));
+
+       cinfo = get_call_info_internal (NULL, cinfo, csig, FALSE);
 
        if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
                args_size += sizeof (gpointer);
@@ -528,15 +551,13 @@ mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJit
                offset += size;
        }
 
-       if (mono_do_x86_stack_align)
+       if (mono_do_x86_stack_align && !CALLCONV_IS_STDCALL (csig))
                align = MONO_ARCH_FRAME_ALIGNMENT;
        else
                align = 4;
        args_size += pad = (align - (args_size & (align - 1))) & (align - 1);
        arg_info [k].pad = pad;
 
-       g_free (cinfo);
-
        return args_size;
 }
 
@@ -649,6 +670,10 @@ void
 mono_arch_init (void)
 {
        InitializeCriticalSection (&mini_arch_mutex);
+
+       ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ);
+       bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
+       mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
 }
 
 /*
@@ -699,7 +724,6 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
  *
  * Returns a bitmask corresponding to all supported versions.
  * 
- * TODO detect other versions like SSE4a.
  */
 guint32
 mono_arch_cpu_enumerate_simd_versions (void)
@@ -721,6 +745,20 @@ mono_arch_cpu_enumerate_simd_versions (void)
                if (ecx & (1 << 20))
                        sse_opts |= 1 << SIMD_VERSION_SSE42;
        }
+
+       /* Yes, all this needs to be done to check for sse4a.
+          See: "Amd: CPUID Specification"
+        */
+       if (cpuid (0x80000000, &eax, &ebx, &ecx, &edx)) {
+               /* eax greater or equal than 0x80000001, ebx = 'htuA', ecx = DMAc', edx = 'itne'*/
+               if ((((unsigned int) eax) >= 0x80000001) && (ebx == 0x68747541) && (ecx == 0x444D4163) && (edx == 0x69746E65)) {
+                       cpuid (0x80000001, &eax, &ebx, &ecx, &edx);
+                       if (ecx & (1 << 6))
+                               sse_opts |= 1 << SIMD_VERSION_SSE4a;
+               }
+       }
+
+
        return sse_opts;        
 }
 
@@ -837,7 +875,78 @@ mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
                /* push+pop+possible load if it is an argument */
                return (ins->opcode == OP_ARG) ? 3 : 2;
 }
+
+static void
+set_needs_stack_frame (MonoCompile *cfg, gboolean flag)
+{
+       static int inited = FALSE;
+       static int count = 0;
+
+       if (cfg->arch.need_stack_frame_inited) {
+               g_assert (cfg->arch.need_stack_frame == flag);
+               return;
+       }
+
+       cfg->arch.need_stack_frame = flag;
+       cfg->arch.need_stack_frame_inited = TRUE;
+
+       if (flag)
+               return;
+
+       if (!inited) {
+               mono_counters_register ("Could eliminate stack frame", MONO_COUNTER_INT|MONO_COUNTER_JIT, &count);
+               inited = TRUE;
+       }
+       ++count;
+
+       //g_print ("will eliminate %s.%s.%s\n", cfg->method->klass->name_space, cfg->method->klass->name, cfg->method->name);
+}
+
+static gboolean
+needs_stack_frame (MonoCompile *cfg)
+{
+       MonoMethodSignature *sig;
+       MonoMethodHeader *header;
+       gboolean result = FALSE;
+
+#if defined(__APPLE__)
+       /*OSX requires stack frame code to have the correct alignment. */
+       return TRUE;
+#endif
+
+       if (cfg->arch.need_stack_frame_inited)
+               return cfg->arch.need_stack_frame;
+
+       header = mono_method_get_header (cfg->method);
+       sig = mono_method_signature (cfg->method);
+
+       if (cfg->disable_omit_fp)
+               result = TRUE;
+       else if (cfg->flags & MONO_CFG_HAS_ALLOCA)
+               result = TRUE;
+       else if (cfg->method->save_lmf)
+               result = TRUE;
+       else if (cfg->stack_offset)
+               result = TRUE;
+       else if (cfg->param_area)
+               result = TRUE;
+       else if (cfg->flags & (MONO_CFG_HAS_CALLS | MONO_CFG_HAS_ALLOCA | MONO_CFG_HAS_TAIL))
+               result = TRUE;
+       else if (header->num_clauses)
+               result = TRUE;
+       else if (sig->param_count + sig->hasthis)
+               result = TRUE;
+       else if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
+               result = TRUE;
+       else if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
+               (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
+               result = TRUE;
+
+       set_needs_stack_frame (cfg, result);
+
+       return cfg->arch.need_stack_frame;
+}
+
 /*
  * Set var information according to the calling convention. X86 version.
  * The locals var stuff should most likely be split in another method.
@@ -893,10 +1002,24 @@ mono_arch_allocate_vars (MonoCompile *cfg)
 
        /* Allocate locals */
        offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
+       if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
+               char *mname = mono_method_full_name (cfg->method, TRUE);
+               cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
+               cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
+               g_free (mname);
+               return;
+       }
        if (locals_stack_align) {
                offset += (locals_stack_align - 1);
                offset &= ~(locals_stack_align - 1);
        }
+       /*
+        * EBP is at alignment 8 % MONO_ARCH_FRAME_ALIGNMENT, so if we
+        * have locals larger than 8 bytes we need to make sure that
+        * they have the appropriate offset.
+        */
+       if (MONO_ARCH_FRAME_ALIGNMENT > 8 && locals_stack_align > 8)
+               offset += MONO_ARCH_FRAME_ALIGNMENT - sizeof (gpointer) * 2;
        for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
                if (offsets [i] != -1) {
                        MonoInst *inst = cfg->varinfo [i];
@@ -963,9 +1086,6 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                inst->inst_offset = ainfo->offset + ARGS_OFFSET;
        }
 
-       offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
-       offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
-
        cfg->stack_offset = offset;
 }
 
@@ -986,38 +1106,6 @@ mono_arch_create_vars (MonoCompile *cfg)
        }
 }
 
-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;
-}
-
 /*
  * It is expensive to adjust esp for each individual fp argument pushed on the stack
  * so we try to do it just once when we have multiple fp arguments in a row.
@@ -1027,7 +1115,7 @@ emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call)
  * fp_arg_setup is the first argument in the execution sequence where the esp register
  * is modified.
  */
-static int
+static G_GNUC_UNUSED int
 collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_setup)
 {
        int fp_space = 0;
@@ -1045,157 +1133,8 @@ collect_fp_stack_space (MonoMethodSignature *sig, int start_arg, int *fp_arg_set
        return fp_space;
 }
 
-/* 
- * 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.
- */
-MonoCallInst*
-mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
-       MonoInst *arg, *in;
-       MonoMethodSignature *sig;
-       int i, n;
-       CallInfo *cinfo;
-       int sentinelpos = 0;
-       int fp_args_space = 0, fp_args_offset = 0, fp_arg_setup = -1;
-
-       sig = call->signature;
-       n = sig->param_count + sig->hasthis;
-
-       cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, FALSE);
-
-       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];
-               } else {
-                       MonoType *t;
-
-                       if (i >= sig->hasthis)
-                               t = sig->params [i - sig->hasthis];
-                       else
-                               t = &mono_defaults.int_class->byval_arg;
-                       t = mini_type_get_underlying_type (cfg->generic_sharing_context, t);
-
-                       MONO_INST_NEW (cfg, arg, OP_OUTARG);
-                       in = call->args [i];
-                       arg->cil_code = in->cil_code;
-                       arg->inst_left = in;
-                       arg->type = in->type;
-                       /* prepend, so they get reversed */
-                       arg->next = call->out_args;
-                       call->out_args = arg;
-
-                       if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
-                               gint align;
-                               guint32 ialign;
-                               guint32 size;
-
-                               if (t->type == MONO_TYPE_TYPEDBYREF) {
-                                       size = sizeof (MonoTypedRef);
-                                       align = sizeof (gpointer);
-                               }
-                               else {
-                                       size = mini_type_stack_size_full (cfg->generic_sharing_context, &in->klass->byval_arg, &ialign, sig->pinvoke);
-                               }
-                               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;
-                                                       /* we store in the upper bits of backen.arg_info the needed
-                                                        * esp adjustment and in the lower bits the offset from esp
-                                                        * where the arg needs to be stored
-                                                        */
-                                                       if (!fp_args_space) {
-                                                               fp_args_space = collect_fp_stack_space (sig, i - sig->hasthis, &fp_arg_setup);
-                                                               fp_args_offset = fp_args_space;
-                                                       }
-                                                       arg->backend.arg_info = fp_args_space - fp_args_offset;
-                                                       fp_args_offset -= sizeof (double);
-                                                       if (i - sig->hasthis == fp_arg_setup) {
-                                                               arg->backend.arg_info |= fp_args_space << 16;
-                                                       }
-                                                       if (fp_args_offset == 0) {
-                                                               /* the allocated esp stack is finished:
-                                                                * prepare for an eventual second run of fp args
-                                                                */
-                                                               fp_args_space = 0;
-                                                       }
-                                               }
-                                       }
-                                       break;
-                               default:
-                                       g_assert_not_reached ();
-                               }
-                       }
-               }
-       }
-
-       /* 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;
-                       /*
-                        * After the call, the struct is in registers, but needs to be saved to the memory pointed
-                        * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
-                        * before calling the function. So we add a dummy instruction to represent pushing the 
-                        * struct return address to the stack. The return address will be saved to this stack slot 
-                        * by the code emitted in this_vret_args.
-                        */
-                       MONO_INST_NEW (cfg, arg, OP_OUTARG);
-                       MONO_INST_NEW (cfg, zero_inst, OP_ICONST);
-                       zero_inst->inst_p0 = 0;
-                       arg->inst_left = zero_inst;
-                       arg->type = STACK_PTR;
-                       /* prepend, so they get reversed */
-                       arg->next = call->out_args;
-                       call->out_args = arg;
-               }
-               else
-                       /* if the function returns a struct, the called method already does a ret $0x4 */
-                       if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
-                               cinfo->stack_usage -= 4;
-       }
-
-       call->stack_usage = cinfo->stack_usage;
-
-       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;
-        }
-
-       return call;
-}
-
 static void
-emit_sig_cookie2 (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
+emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
 {
        MonoMethodSignature *tmp_sig;
 
@@ -1216,6 +1155,108 @@ emit_sig_cookie2 (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
        MONO_EMIT_NEW_BIALU_IMM (cfg, OP_X86_PUSH_IMM, -1, -1, tmp_sig);
 }
 
+#ifdef ENABLE_LLVM
+LLVMCallInfo*
+mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
+{
+       int i, n;
+       CallInfo *cinfo;
+       ArgInfo *ainfo;
+       int j;
+       LLVMCallInfo *linfo;
+
+       n = sig->param_count + sig->hasthis;
+
+       cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
+
+       linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
+
+       /*
+        * LLVM always uses the native ABI while we use our own ABI, the
+        * only difference is the handling of vtypes:
+        * - we only pass/receive them in registers in some cases, and only 
+        *   in 1 or 2 integer registers.
+        */
+       if (cinfo->ret.storage == ArgValuetypeInReg) {
+               if (sig->pinvoke) {
+                       cfg->exception_message = g_strdup ("pinvoke + vtypes");
+                       cfg->disable_llvm = TRUE;
+                       return linfo;
+               }
+
+               cfg->exception_message = g_strdup ("vtype ret in call");
+               cfg->disable_llvm = TRUE;
+               /*
+               linfo->ret.storage = LLVMArgVtypeInReg;
+               for (j = 0; j < 2; ++j)
+                       linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
+               */
+       }
+
+       if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage == ArgInIReg) {
+               /* Vtype returned using a hidden argument */
+               linfo->ret.storage = LLVMArgVtypeRetAddr;
+       }
+
+       if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != ArgInIReg) {
+               // FIXME:
+               cfg->exception_message = g_strdup ("vtype ret in call");
+               cfg->disable_llvm = TRUE;
+       }
+
+       for (i = 0; i < n; ++i) {
+               ainfo = cinfo->args + i;
+
+               linfo->args [i].storage = LLVMArgNone;
+
+               switch (ainfo->storage) {
+               case ArgInIReg:
+                       linfo->args [i].storage = LLVMArgInIReg;
+                       break;
+               case ArgInDoubleSSEReg:
+               case ArgInFloatSSEReg:
+                       linfo->args [i].storage = LLVMArgInFPReg;
+                       break;
+               case ArgOnStack:
+                       if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
+                               linfo->args [i].storage = LLVMArgVtypeByVal;
+                       } else {
+                               linfo->args [i].storage = LLVMArgInIReg;
+                               if (!sig->params [i - sig->hasthis]->byref) {
+                                       if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4) {
+                                               linfo->args [i].storage = LLVMArgInFPReg;
+                                       } else if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8) {
+                                               linfo->args [i].storage = LLVMArgInFPReg;
+                                       }
+                               }
+                       }
+                       break;
+               case ArgValuetypeInReg:
+                       if (sig->pinvoke) {
+                               cfg->exception_message = g_strdup ("pinvoke + vtypes");
+                               cfg->disable_llvm = TRUE;
+                               return linfo;
+                       }
+
+                       cfg->exception_message = g_strdup ("vtype arg");
+                       cfg->disable_llvm = TRUE;
+                       /*
+                       linfo->args [i].storage = LLVMArgVtypeInReg;
+                       for (j = 0; j < 2; ++j)
+                               linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
+                       */
+                       break;
+               default:
+                       cfg->exception_message = g_strdup ("ainfo->storage");
+                       cfg->disable_llvm = TRUE;
+                       break;
+               }
+       }
+
+       return linfo;
+}
+#endif
+
 void
 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
 {
@@ -1242,32 +1283,21 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
        }
 
        if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
-               MonoInst *vtarg;
-
                if (cinfo->ret.storage == ArgValuetypeInReg) {
-                       if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
-                               /*
-                                * Tell the JIT to use a more efficient calling convention: call using
-                                * OP_CALL, compute the result location after the call, and save the 
-                                * result there.
-                                */
-                               call->vret_in_reg = TRUE;
-                       } else {
-                               /*
-                                * The valuetype is in EAX:EDX after the call, needs to be copied to
-                                * the stack. Save the address here, so the call instruction can
-                                * access it.
-                                */
-                               MONO_INST_NEW (cfg, vtarg, OP_X86_PUSH);
-                               vtarg->sreg1 = call->vret_var->dreg;
-                               MONO_ADD_INS (cfg->cbb, vtarg);
-                       }
+                       /*
+                        * Tell the JIT to use a more efficient calling convention: call using
+                        * OP_CALL, compute the result location after the call, and save the 
+                        * result there.
+                        */
+                       call->vret_in_reg = TRUE;
+                       if (call->vret_var)
+                               NULLIFY_INS (call->vret_var);
                }
        }
 
        /* Handle the case where there are no implicit arguments */
        if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
-               emit_sig_cookie2 (cfg, call, cinfo);
+               emit_sig_cookie (cfg, call, cinfo);
        }
 
        /* Arguments are pushed in the reverse order */
@@ -1343,7 +1373,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
 
                if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
                        /* Emit the signature cookie just before the implicit arguments */
-                       emit_sig_cookie2 (cfg, call, cinfo);
+                       emit_sig_cookie (cfg, call, cinfo);
                }
        }
 
@@ -1358,7 +1388,7 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                        /* The return address is passed in a register */
                        MONO_INST_NEW (cfg, vtarg, OP_MOVE);
                        vtarg->sreg1 = call->inst.dreg;
-                       vtarg->dreg = mono_regstate_next_int (cfg->rs);
+                       vtarg->dreg = mono_alloc_ireg (cfg);
                        MONO_ADD_INS (cfg->cbb, vtarg);
                                
                        mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
@@ -1370,8 +1400,9 @@ mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
                        MONO_ADD_INS (cfg->cbb, vtarg);
                }
 
-               /* if the function returns a struct, the called method already does a ret $0x4 */
-               cinfo->stack_usage -= 4;
+               /* if the function returns a struct on stack, the called method already does a ret $0x4 */
+               if (cinfo->ret.storage != ArgValuetypeInReg)
+                       cinfo->stack_usage -= 4;
        }
 
        call->stack_usage = cinfo->stack_usage;
@@ -1390,7 +1421,7 @@ mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
                MONO_ADD_INS (cfg->cbb, arg);
        } else if (size <= 20) {        
                MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 4));
-               mini_emit_memcpy2 (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
+               mini_emit_memcpy (cfg, X86_ESP, 0, src->dreg, 0, size, 4);
        } else {
                MONO_INST_NEW (cfg, arg, OP_X86_PUSH_OBJ);
                arg->inst_basereg = src->dreg;
@@ -1408,14 +1439,22 @@ mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
 
        if (!ret->byref) {
                if (ret->type == MONO_TYPE_R4) {
+                       if (COMPILE_LLVM (cfg))
+                               MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
                        /* Nothing to do */
                        return;
                } else if (ret->type == MONO_TYPE_R8) {
+                       if (COMPILE_LLVM (cfg))
+                               MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
                        /* Nothing to do */
                        return;
                } else if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
-                       MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, val->dreg + 1);
-                       MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, val->dreg + 2);
+                       if (COMPILE_LLVM (cfg))
+                               MONO_EMIT_NEW_UNALU (cfg, OP_LMOVE, cfg->ret->dreg, val->dreg);
+                       else {
+                               MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EAX, val->dreg + 1);
+                               MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, X86_EDX, val->dreg + 2);
+                       }
                        return;
                }
        }
@@ -1461,42 +1500,51 @@ enum {
 };
 
 void*
-mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
+mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
 {
        guchar *code = p;
-       int arg_size = 0, save_mode = SAVE_NONE;
+       int arg_size = 0, stack_usage = 0, save_mode = SAVE_NONE;
        MonoMethod *method = cfg->method;
        
        switch (mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret)->type) {
        case MONO_TYPE_VOID:
                /* special case string .ctor icall */
-               if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
+               if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class) {
                        save_mode = SAVE_EAX;
-               else
+                       stack_usage = enable_arguments ? 8 : 4;
+               } else
                        save_mode = SAVE_NONE;
                break;
        case MONO_TYPE_I8:
        case MONO_TYPE_U8:
                save_mode = SAVE_EAX_EDX;
+               stack_usage = enable_arguments ? 16 : 8;
                break;
        case MONO_TYPE_R4:
        case MONO_TYPE_R8:
                save_mode = SAVE_FP;
+               stack_usage = enable_arguments ? 16 : 8;
                break;
        case MONO_TYPE_GENERICINST:
                if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
                        save_mode = SAVE_EAX;
+                       stack_usage = enable_arguments ? 8 : 4;
                        break;
                }
                /* Fall through */
        case MONO_TYPE_VALUETYPE:
+               // FIXME: Handle SMALL_STRUCT_IN_REG here for proper alignment on darwin-x86
                save_mode = SAVE_STRUCT;
+               stack_usage = enable_arguments ? 4 : 0;
                break;
        default:
                save_mode = SAVE_EAX;
+               stack_usage = enable_arguments ? 8 : 4;
                break;
        }
 
+       x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage - 4);
+
        switch (save_mode) {
        case SAVE_EAX_EDX:
                x86_push_reg (code, X86_EDX);
@@ -1544,6 +1592,7 @@ mono_arch_instrument_epilog (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);
        }
+
        x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
 
        switch (save_mode) {
@@ -1562,33 +1611,22 @@ mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean ena
        default:
                break;
        }
+       
+       x86_alu_reg_imm (code, X86_ADD, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - stack_usage);
 
        return code;
 }
 
 #define EMIT_COND_BRANCH(ins,cond,sign) \
-if (ins->flags & MONO_INST_BRLABEL) { \
-        if (ins->inst_i0->inst_c0) { \
-               x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
-        } else { \
-               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
-               if ((cfg->opt & MONO_OPT_BRANCH) && \
-                    x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
-                       x86_branch8 (code, cond, 0, sign); \
-                else \
-                       x86_branch32 (code, cond, 0, sign); \
-        } \
+if (ins->inst_true_bb->native_offset) { \
+       x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
 } else { \
-        if (ins->inst_true_bb->native_offset) { \
-               x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
-        } else { \
-               mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
-               if ((cfg->opt & MONO_OPT_BRANCH) && \
-                    x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
-                       x86_branch8 (code, cond, 0, sign); \
-                else \
-                       x86_branch32 (code, cond, 0, sign); \
-        } \
+       mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
+       if ((cfg->opt & MONO_OPT_BRANCH) && \
+            x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
+               x86_branch8 (code, cond, 0, sign); \
+        else \
+               x86_branch32 (code, cond, 0, sign); \
 }
 
 /*  
@@ -1770,9 +1808,6 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 {
        MonoInst *ins, *next;
 
-       if (bb->max_vreg > cfg->rs->next_vreg)
-               cfg->rs->next_vreg = bb->max_vreg;
-
        /*
         * FIXME: Need to add more instructions, but the current machine 
         * description can't model some parts of the composite instructions like
@@ -1797,7 +1832,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                }
        }
 
-       bb->max_vreg = cfg->rs->next_vreg;
+       bb->max_vreg = cfg->next_vreg;
 }
 
 static const int 
@@ -1874,7 +1909,7 @@ mono_emit_stack_alloc (guchar *code, MonoInst* tree)
        int sreg = tree->sreg1;
        int need_touch = FALSE;
 
-#if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
+#if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
        need_touch = TRUE;
 #endif
 
@@ -1979,9 +2014,6 @@ mono_emit_stack_alloc (guchar *code, MonoInst* tree)
 static guint8*
 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
 {
-       CallInfo *cinfo;
-       int quad;
-
        /* Move return value to the target register */
        switch (ins->opcode) {
        case OP_CALL:
@@ -1990,42 +2022,6 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
                if (ins->dreg != X86_EAX)
                        x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
                break;
-       case OP_VCALL:
-       case OP_VCALL_REG:
-       case OP_VCALL_MEMBASE:
-       case OP_VCALL2:
-       case OP_VCALL2_REG:
-       case OP_VCALL2_MEMBASE:
-               cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, ((MonoCallInst*)ins)->signature, FALSE);
-               if (cinfo->ret.storage == ArgValuetypeInReg) {
-                       /* Pop the destination address from the stack */
-                       x86_pop_reg (code, X86_ECX);
-                       
-                       for (quad = 0; quad < 2; quad ++) {
-                               switch (cinfo->ret.pair_storage [quad]) {
-                               case ArgInIReg:
-                                       g_assert (cinfo->ret.pair_regs [quad] != X86_ECX);
-                                       x86_mov_membase_reg (code, X86_ECX, (quad * sizeof (gpointer)), cinfo->ret.pair_regs [quad], sizeof (gpointer));
-                                       break;
-                               case ArgNone:
-                                       break;
-                               default:
-                                       g_assert_not_reached ();
-                               }
-                       }
-               }
-               break;
-       case OP_FCALL: {
-               MonoCallInst *call = (MonoCallInst*)ins;
-               if (call->method && !mono_method_signature (call->method)->ret->byref && mono_method_signature (call->method)->ret->type == MONO_TYPE_R4) {
-                       /* Avoid some precision issues by saving/reloading the return value */
-                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
-                       x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
-                       x86_fld_membase (code, X86_ESP, 0, FALSE);
-                       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
-               }
-               break;
-       }
        default:
                break;
        }
@@ -2034,20 +2030,21 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
 }
 
 /*
- * emit_tls_get:
+ * mono_x86_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.
+ * mono_x86_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)
+guint8*
+mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset)
 {
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        /* 
         * See the Under the Hood article in the May 1996 issue of Microsoft Systems 
         * Journal and/or a disassembly of the TlsGet () function.
@@ -2137,6 +2134,8 @@ x86_pop_reg (code, X86_EAX);
 #define LOOP_ALIGNMENT 8
 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
 
+#ifndef DISABLE_JIT
+
 void
 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 {
@@ -2228,10 +2227,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_mov_mem_imm (code, ins->inst_p0, ins->inst_c0, 4);
                        break;
                case OP_LOADU4_MEM:
-                       if (cfg->new_ir)
-                               x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
-                       else
-                               x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
+                       x86_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
                        break;
                case OP_LOAD_MEM:
                case OP_LOADI4_MEM:
@@ -2377,6 +2373,31 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_NOT_REACHED:
                case OP_NOT_NULL:
                        break;
+               case OP_SEQ_POINT: {
+                       int i;
+
+                       if (cfg->compile_aot)
+                               NOT_IMPLEMENTED;
+
+                       /* 
+                        * Read from the single stepping trigger page. This will cause a
+                        * SIGSEGV when single stepping is enabled.
+                        * We do this _before_ the breakpoint, so single stepping after
+                        * a breakpoint is hit will step to the next IL offset.
+                        */
+                       if (ins->flags & MONO_INST_SINGLE_STEP_LOC)
+                               x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)ss_trigger_page);
+
+                       mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
+
+                       /* 
+                        * A placeholder for a possible breakpoint inserted by
+                        * mono_arch_set_breakpoint ().
+                        */
+                       for (i = 0; i < 6; ++i)
+                               x86_nop (code);
+                       break;
+               }
                case OP_ADDCC:
                case OP_IADDCC:
                case OP_IADD:
@@ -2471,6 +2492,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                 */
                                x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EDX);
                                x86_alu_reg_reg (code, X86_SUB, X86_EAX, X86_EDX);
+                       } else if (power == 0) {
+                               x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
                        } else {
                                /* Based on gcc code */
 
@@ -2863,6 +2886,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_VOIDCALL_MEMBASE:
                case OP_CALL_MEMBASE:
                        call = (MonoCallInst*)ins;
+
+                       /* 
+                        * Emit a few nops to simplify get_vcall_slot ().
+                        */
+                       x86_nop (code);
+                       x86_nop (code);
+                       x86_nop (code);
+
                        x86_call_membase (code, ins->sreg1, ins->inst_offset);
                        if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
                                if (call->stack_usage == 4)
@@ -2872,7 +2903,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        }
                        code = emit_move_return_value (cfg, ins, code);
                        break;
-               case OP_OUTARG:
                case OP_X86_PUSH:
                        x86_push_reg (code, ins->sreg1);
                        break;
@@ -2934,12 +2964,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                }
                case OP_THROW: {
+                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
                        x86_push_reg (code, ins->sreg1);
                        code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, 
                                                          (gpointer)"mono_arch_throw_exception");
                        break;
                }
                case OP_RETHROW: {
+                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, MONO_ARCH_FRAME_ALIGNMENT - 4);
                        x86_push_reg (code, ins->sreg1);
                        code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, 
                                                          (gpointer)"mono_arch_rethrow_exception");
@@ -2974,28 +3006,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        ins->inst_c0 = code - cfg->native_code;
                        break;
                case OP_BR:
-                       if (ins->flags & MONO_INST_BRLABEL) {
-                               if (ins->inst_i0->inst_c0) {
-                                       x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
-                               } else {
-                                       mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
-                                       if ((cfg->opt & MONO_OPT_BRANCH) &&
-                                           x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
-                                               x86_jump8 (code, 0);
-                                       else 
-                                               x86_jump32 (code, 0);
-                               }
+                       if (ins->inst_target_bb->native_offset) {
+                               x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
                        } else {
-                               if (ins->inst_target_bb->native_offset) {
-                                       x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
-                               } else {
-                                       mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
-                                       if ((cfg->opt & MONO_OPT_BRANCH) &&
-                                           x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
-                                               x86_jump8 (code, 0);
-                                       else 
-                                               x86_jump32 (code, 0);
-                               } 
+                               mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
+                               if ((cfg->opt & MONO_OPT_BRANCH) &&
+                                   x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
+                                       x86_jump8 (code, 0);
+                               else 
+                                       x86_jump32 (code, 0);
                        }
                        break;
                case OP_BR_REG:
@@ -3123,10 +3142,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_STORER8_MEMBASE_REG:
                        x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
                        break;
-               case OP_LOADR8_SPILL_MEMBASE:
-                       x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
-                       x86_fxch (code, 1);
-                       break;
                case OP_LOADR8_MEMBASE:
                        x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
                        break;
@@ -3136,7 +3151,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_LOADR4_MEMBASE:
                        x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
                        break;
-               case OP_ICONV_TO_R4: /* FIXME: change precision */
+               case OP_ICONV_TO_R4:
+                       x86_push_reg (code, ins->sreg1);
+                       x86_fild_membase (code, X86_ESP, 0, FALSE);
+                       /* Change precision */
+                       x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
+                       x86_fld_membase (code, X86_ESP, 0, FALSE);
+                       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
+                       break;
                case OP_ICONV_TO_R8:
                        x86_push_reg (code, ins->sreg1);
                        x86_fild_membase (code, X86_ESP, 0, FALSE);
@@ -3155,7 +3177,11 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
                        break;
                case OP_FCONV_TO_R4:
-                       /* FIXME: nothing to do ?? */
+                       /* Change precision */
+                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
+                       x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
+                       x86_fld_membase (code, X86_ESP, 0, FALSE);
+                       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
                        break;
                case OP_FCONV_TO_I1:
                        code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
@@ -3191,6 +3217,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_push_reg (code, ins->sreg2);
                        x86_push_reg (code, ins->sreg1);
                        x86_fild_membase (code, X86_ESP, 0, TRUE);
+                       /* Change precision */
+                       x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
+                       x86_fld_membase (code, X86_ESP, 0, TRUE);
                        x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
                        break;
                case OP_LCONV_TO_R4_2:
@@ -3202,18 +3231,14 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_fld_membase (code, X86_ESP, 0, FALSE);
                        x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
                        break;
-               case OP_LCONV_TO_R_UN:
                case OP_LCONV_TO_R_UN_2: { 
                        static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
                        guint8 *br;
 
                        /* load 64bit integer to FP stack */
-                       x86_push_imm (code, 0);
                        x86_push_reg (code, ins->sreg2);
                        x86_push_reg (code, ins->sreg1);
                        x86_fild_membase (code, X86_ESP, 0, TRUE);
-                       /* store as 80bit FP value */
-                       x86_fst80_membase (code, X86_ESP, 0);
                        
                        /* test if lreg is negative */
                        x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
@@ -3221,14 +3246,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
        
                        /* add correction constant mn */
                        x86_fld80_mem (code, mn);
-                       x86_fld80_membase (code, X86_ESP, 0);
                        x86_fp_op_reg (code, X86_FADD, 1, TRUE);
-                       x86_fst80_membase (code, X86_ESP, 0);
 
                        x86_patch (br, code);
 
-                       x86_fld80_membase (code, X86_ESP, 0);
-                       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
+                       /* Change precision */
+                       x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
+                       x86_fld_membase (code, X86_ESP, 0, TRUE);
+
+                       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
 
                        break;
                }
@@ -3351,6 +3377,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_SQRT:
                        x86_fsqrt (code);
                        break;
+               case OP_ROUND:
+                       x86_frndint (code);
+                       break;
                case OP_IMIN:
                        g_assert (cfg->opt & MONO_OPT_CMOV);
                        g_assert (ins->dreg == ins->sreg1);
@@ -3684,7 +3713,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                }
                case OP_TLS_GET: {
-                       code = emit_tls_get (code, ins->dreg, ins->inst_offset);
+                       code = mono_x86_emit_tls_get (code, ins->dreg, ins->inst_offset);
                        break;
                }
                case OP_MEMORY_BARRIER: {
@@ -3755,8 +3784,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        break;
                }
-               case OP_ATOMIC_EXCHANGE_I4:
-               case OP_ATOMIC_CAS_IMM_I4: {
+               case OP_ATOMIC_EXCHANGE_I4: {
                        guchar *br[2];
                        int sreg2 = ins->sreg2;
                        int breg = ins->inst_basereg;
@@ -3769,39 +3797,44 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        
                        /* We need the EAX reg for the cmpxchg */
                        if (ins->sreg2 == X86_EAX) {
-                               x86_push_reg (code, X86_EDX);
-                               x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
-                               sreg2 = X86_EDX;
+                               sreg2 = (breg == X86_EDX) ? X86_EBX : X86_EDX;
+                               x86_push_reg (code, sreg2);
+                               x86_mov_reg_reg (code, sreg2, X86_EAX, 4);
                        }
 
                        if (breg == X86_EAX) {
-                               x86_push_reg (code, X86_ESI);
-                               x86_mov_reg_reg (code, X86_ESI, X86_EAX, 4);
-                               breg = X86_ESI;
+                               breg = (sreg2 == X86_ESI) ? X86_EDI : X86_ESI;
+                               x86_push_reg (code, breg);
+                               x86_mov_reg_reg (code, breg, X86_EAX, 4);
                        }
 
-                       if (ins->opcode == OP_ATOMIC_CAS_IMM_I4) {
-                               x86_mov_reg_imm (code, X86_EAX, ins->backend.data);
+                       x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
 
-                               x86_prefix (code, X86_LOCK_PREFIX);
-                               x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
-                       } else {
-                               x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
-
-                               br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
-                               x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
-                               br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
-                               x86_patch (br [1], br [0]);
-                       }
+                       br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
+                       x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
+                       br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
+                       x86_patch (br [1], br [0]);
 
                        if (breg != ins->inst_basereg)
-                               x86_pop_reg (code, X86_ESI);
+                               x86_pop_reg (code, breg);
 
                        if (ins->sreg2 != sreg2)
-                               x86_pop_reg (code, X86_EDX);
+                               x86_pop_reg (code, sreg2);
 
                        break;
                }
+               case OP_ATOMIC_CAS_I4: {
+                       g_assert (ins->sreg3 == X86_EAX);
+                       g_assert (ins->sreg1 != X86_EAX);
+                       g_assert (ins->sreg1 != ins->sreg2);
+
+                       x86_prefix (code, X86_LOCK_PREFIX);
+                       x86_cmpxchg_membase_reg (code, ins->sreg1, ins->inst_offset, ins->sreg2);
+
+                       if (ins->dreg != X86_EAX)
+                               x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
+                       break;
+               }
 #ifdef MONO_ARCH_SIMD_INTRINSICS
                case OP_ADDPS:
                        x86_sse_alu_ps_reg_reg (code, X86_SSE_ADD, ins->sreg1, ins->sreg2);
@@ -3861,10 +3894,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_DUPPS_LOW:
                        x86_sse_alu_ss_reg_reg (code, X86_SSE_MOVSLDUP, ins->dreg, ins->sreg1);
                        break;
-               case OP_SHUFLEPS:
-                       g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
-                       x86_pshufd_reg_reg (code, ins->dreg, ins->sreg1, ins->inst_c0);
-                       break;
 
                case OP_PSHUFLEW_HIGH:
                        g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
@@ -3913,6 +3942,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_XORPD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_XOR, ins->sreg1, ins->sreg2);
                        break;
+               case OP_SQRTPD:
+                       x86_sse_alu_pd_reg_reg (code, X86_SSE_SQRT, ins->dreg, ins->sreg1);
+                       break;
                case OP_ADDSUBPD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_ADDSUB, ins->sreg1, ins->sreg2);
                        break;
@@ -3949,6 +3981,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_PADDD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDD, ins->sreg1, ins->sreg2);
                        break;
+               case OP_PADDQ:
+                       x86_sse_alu_pd_reg_reg (code, X86_SSE_PADDQ, ins->sreg1, ins->sreg2);
+                       break;
 
                case OP_PSUBB:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBB, ins->sreg1, ins->sreg2);
@@ -3959,6 +3994,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_PSUBD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBD, ins->sreg1, ins->sreg2);
                        break;
+               case OP_PSUBQ:
+                       x86_sse_alu_pd_reg_reg (code, X86_SSE_PSUBQ, ins->sreg1, ins->sreg2);
+                       break;
 
                case OP_PMAXB_UN:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PMAXUB, ins->sreg1, ins->sreg2);
@@ -4016,6 +4054,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_PCMPEQD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPEQD, ins->sreg1, ins->sreg2);
                        break;
+               case OP_PCMPEQQ:
+                       x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPEQQ, ins->sreg1, ins->sreg2);
+                       break;
 
                case OP_PCMPGTB:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTB, ins->sreg1, ins->sreg2);
@@ -4026,6 +4067,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_PCMPGTD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PCMPGTD, ins->sreg1, ins->sreg2);
                        break;
+               case OP_PCMPGTQ:
+                       x86_sse_alu_sse41_reg_reg (code, X86_SSE_PCMPGTQ, ins->sreg1, ins->sreg2);
+                       break;
 
                case OP_PSUM_ABS_DIFF:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PSADBW, ins->sreg1, ins->sreg2);
@@ -4040,6 +4084,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_UNPACK_LOWD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLDQ, ins->sreg1, ins->sreg2);
                        break;
+               case OP_UNPACK_LOWQ:
+                       x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKLQDQ, ins->sreg1, ins->sreg2);
+                       break;
                case OP_UNPACK_LOWPS:
                        x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKL, ins->sreg1, ins->sreg2);
                        break;
@@ -4056,6 +4103,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_UNPACK_HIGHD:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHDQ, ins->sreg1, ins->sreg2);
                        break;
+               case OP_UNPACK_HIGHQ:
+                       x86_sse_alu_pd_reg_reg (code, X86_SSE_PUNPCKHQDQ, ins->sreg1, ins->sreg2);
+                       break;
                case OP_UNPACK_HIGHPS:
                        x86_sse_alu_ps_reg_reg (code, X86_SSE_UNPCKH, ins->sreg1, ins->sreg2);
                        break;
@@ -4108,6 +4158,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_PMULD:
                        x86_sse_alu_sse41_reg_reg (code, X86_SSE_PMULLD, ins->sreg1, ins->sreg2);
                        break;
+               case OP_PMULQ:
+                       x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULUDQ, ins->sreg1, ins->sreg2);
+                       break;
                case OP_PMULW_HIGH_UN:
                        x86_sse_alu_pd_reg_reg (code, X86_SSE_PMULHUW, ins->sreg1, ins->sreg2);
                        break;
@@ -4157,12 +4210,88 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_sse_shift_reg_reg (code, X86_SSE_PSLLD_REG, ins->dreg, ins->sreg2);
                        break;
 
+               case OP_PSHRQ:
+                       x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHR, ins->dreg, ins->inst_imm);
+                       break;
+               case OP_PSHRQ_REG:
+                       x86_sse_shift_reg_reg (code, X86_SSE_PSRLQ_REG, ins->dreg, ins->sreg2);
+                       break;
+
+               case OP_PSHLQ:
+                       x86_sse_shift_reg_imm (code, X86_SSE_PSHIFTQ, X86_SSE_SHL, ins->dreg, ins->inst_imm);
+                       break;
+               case OP_PSHLQ_REG:
+                       x86_sse_shift_reg_reg (code, X86_SSE_PSLLQ_REG, ins->dreg, ins->sreg2);
+                       break;          
+                       
                case OP_ICONV_TO_X:
                        x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
                        break;
                case OP_EXTRACT_I4:
                        x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
                        break;
+               case OP_EXTRACT_I1:
+               case OP_EXTRACT_U1:
+                       x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
+                       if (ins->inst_c0)
+                               x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
+                       x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
+                       break;
+               case OP_EXTRACT_I2:
+               case OP_EXTRACT_U2:
+                       x86_movd_reg_xreg (code, ins->dreg, ins->sreg1);
+                       if (ins->inst_c0)
+                               x86_shift_reg_imm (code, X86_SHR, ins->dreg, 16);
+                       x86_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE);
+                       break;
+               case OP_EXTRACT_R8:
+                       if (ins->inst_c0)
+                               x86_sse_alu_pd_membase_reg (code, X86_SSE_MOVHPD_MEMBASE_REG, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1);
+                       else
+                               x86_sse_alu_sd_membase_reg (code, X86_SSE_MOVSD_MEMBASE_REG, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1);
+                       x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE);
+                       break;
+
+               case OP_INSERT_I2:
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->sreg1, ins->sreg2, ins->inst_c0);
+                       break;
+               case OP_EXTRACTX_U2:
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PEXTRW, ins->dreg, ins->sreg1, ins->inst_c0);
+                       break;
+               case OP_INSERTX_U1_SLOW:
+                       /*sreg1 is the extracted ireg (scratch)
+                       /sreg2 is the to be inserted ireg (scratch)
+                       /dreg is the xreg to receive the value*/
+
+                       /*clear the bits from the extracted word*/
+                       x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
+                       /*shift the value to insert if needed*/
+                       if (ins->inst_c0 & 1)
+                               x86_shift_reg_imm (code, X86_SHL, ins->sreg2, 8);
+                       /*join them together*/
+                       x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
+                       break;
+               case OP_INSERTX_I4_SLOW:
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
+                       x86_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
+                       break;
+
+               case OP_INSERTX_R4_SLOW:
+                       x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
+                       /*TODO if inst_c0 == 0 use movss*/
+                       x86_sse_alu_pd_reg_membase_imm (code, X86_SSE_PINSRW, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset + 0, ins->inst_c0 * 2);
+                       x86_sse_alu_pd_reg_membase_imm (code, X86_SSE_PINSRW, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset + 2, ins->inst_c0 * 2 + 1);
+                       break;
+               case OP_INSERTX_R8_SLOW:
+                       x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
+                       if (ins->inst_c0)
+                               x86_sse_alu_pd_reg_membase (code, X86_SSE_MOVHPD_REG_MEMBASE, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
+                       else
+                               x86_sse_alu_pd_reg_membase (code, X86_SSE_MOVSD_REG_MEMBASE, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
+                       break;
+
                case OP_STOREX_MEMBASE_REG:
                case OP_STOREX_MEMBASE:
                        x86_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
@@ -4175,6 +4304,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                case OP_STOREX_ALIGNED_MEMBASE_REG:
                        x86_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
+                       break;
+               case OP_STOREX_NTA_MEMBASE_REG:
+                       x86_sse_alu_reg_membase (code, X86_SSE_MOVNTPS, ins->dreg, ins->sreg1, ins->inst_offset);
+                       break;
+               case OP_PREFETCH_MEMBASE:
+                       x86_sse_alu_reg_membase (code, X86_SSE_PREFETCH, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
+
                        break;
                case OP_XMOVE:
                        /*FIXME the peephole pass should have killed this*/
@@ -4188,14 +4324,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        x86_mov_membase_reg (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, ins->sreg1, 4);
                        x86_fld_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE);
                        break;
-               case OP_PUSH_R4:
-                       x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
-                       x86_fst_membase (code, X86_ESP, 0, FALSE, TRUE);
-                       break;
-               case OP_LOADX_STACK: 
-                       x86_movups_reg_membase (code, ins->dreg, X86_ESP, 0);
-                       x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
-                       break;
 
                case OP_FCONV_TO_R8_X:
                        x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
@@ -4219,7 +4347,47 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                break;
                        }                       
                        break;
+
+               case OP_EXPAND_I1:
+                       /*FIXME this causes a partial register stall, maybe it would not be that bad to use shift + mask + or*/
+                       /*The +4 is to get a mov ?h, ?l over the same reg.*/
+                       x86_mov_reg_reg (code, ins->dreg + 4, ins->dreg, 1);
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
+                       x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
+                       break;
+               case OP_EXPAND_I2:
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 0);
+                       x86_sse_alu_pd_reg_reg_imm (code, X86_SSE_PINSRW, ins->dreg, ins->sreg1, 1);
+                       x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
+                       break;
+               case OP_EXPAND_I4:
+                       x86_movd_xreg_reg (code, ins->dreg, ins->sreg1);
+                       x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
+                       break;
+               case OP_EXPAND_R4:
+                       x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, FALSE, TRUE);
+                       x86_movd_xreg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
+                       x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0);
+                       break;
+               case OP_EXPAND_R8:
+                       x86_fst_membase (code, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset, TRUE, TRUE);
+                       x86_movsd_reg_membase (code, ins->dreg, ins->backend.spill_var->inst_basereg, ins->backend.spill_var->inst_offset);
+                       x86_sse_shift_reg_imm (code, X86_SSE_PSHUFD, ins->dreg, ins->dreg, 0x44);
+                       break;
 #endif
+               case OP_LIVERANGE_START: {
+                       if (cfg->verbose_level > 1)
+                               printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
+                       MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
+                       break;
+               }
+               case OP_LIVERANGE_END: {
+                       if (cfg->verbose_level > 1)
+                               printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
+                       MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
+                       break;
+               }
                default:
                        g_warning ("unknown opcode %s\n", mono_inst_name (ins->opcode));
                        g_assert_not_reached ();
@@ -4237,6 +4405,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
        cfg->code_len = code - cfg->native_code;
 }
 
+#endif /* DISABLE_JIT */
+
 void
 mono_arch_register_lowlevel_calls (void)
 {
@@ -4284,6 +4454,8 @@ mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, Mono
                case MONO_PATCH_INFO_LABEL:
                case MONO_PATCH_INFO_RGCTX_FETCH:
                case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
+               case MONO_PATCH_INFO_MONITOR_ENTER:
+               case MONO_PATCH_INFO_MONITOR_EXIT:
                        x86_patch (ip, target);
                        break;
                case MONO_PATCH_INFO_NONE:
@@ -4304,18 +4476,37 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        MonoBasicBlock *bb;
        MonoMethodSignature *sig;
        MonoInst *inst;
-       int alloc_size, pos, max_offset, i;
+       int alloc_size, pos, max_offset, i, cfa_offset;
        guint8 *code;
+       gboolean need_stack_frame;
 
-       cfg->code_size =  MAX (mono_method_get_header (method)->code_size * 4, 10240);
+       cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 10240);
 
        if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
                cfg->code_size += 512;
 
        code = cfg->native_code = g_malloc (cfg->code_size);
 
-       x86_push_reg (code, X86_EBP);
-       x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
+       /* Offset between RSP and the CFA */
+       cfa_offset = 0;
+
+       // CFA = sp + 4
+       cfa_offset = sizeof (gpointer);
+       mono_emit_unwind_op_def_cfa (cfg, code, X86_ESP, sizeof (gpointer));
+       // IP saved at CFA - 4
+       /* There is no IP reg on x86 */
+       mono_emit_unwind_op_offset (cfg, code, X86_NREG, -cfa_offset);
+
+       need_stack_frame = needs_stack_frame (cfg);
+
+       if (need_stack_frame) {
+               x86_push_reg (code, X86_EBP);
+               cfa_offset += sizeof (gpointer);
+               mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
+               mono_emit_unwind_op_offset (cfg, code, X86_EBP, - cfa_offset);
+               x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
+               mono_emit_unwind_op_def_cfa_reg (cfg, code, X86_EBP);
+       }
 
        alloc_size = cfg->stack_offset;
        pos = 0;
@@ -4325,11 +4516,11 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                if (appdomain_tls_offset != -1 && lmf_tls_offset != -1) {
                        guint8 *buf, *no_domain_branch;
 
-                       code = emit_tls_get (code, X86_EAX, appdomain_tls_offset);
+                       code = mono_x86_emit_tls_get (code, X86_EAX, appdomain_tls_offset);
                        x86_alu_reg_imm (code, X86_CMP, X86_EAX, GPOINTER_TO_UINT (cfg->domain));
                        no_domain_branch = code;
                        x86_branch8 (code, X86_CC_NE, 0, 0);
-                       code = emit_tls_get ( code, X86_EAX, lmf_tls_offset);
+                       code = mono_x86_emit_tls_get ( code, X86_EAX, lmf_tls_offset);
                        x86_test_reg_reg (code, X86_EAX, X86_EAX);
                        buf = code;
                        x86_branch8 (code, X86_CC_NE, 0, 0);
@@ -4338,7 +4529,7 @@ 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);
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_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));
@@ -4355,15 +4546,26 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        if (method->save_lmf) {
                pos += sizeof (MonoLMF);
 
+               if (cfg->compile_aot)
+                       cfg->disable_aot = TRUE;
+
                /* save the current IP */
                mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
                x86_push_imm_template (code);
+               cfa_offset += sizeof (gpointer);
 
                /* save all caller saved regs */
                x86_push_reg (code, X86_EBP);
+               cfa_offset += sizeof (gpointer);
                x86_push_reg (code, X86_ESI);
+               cfa_offset += sizeof (gpointer);
+               mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
                x86_push_reg (code, X86_EDI);
+               cfa_offset += sizeof (gpointer);
+               mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
                x86_push_reg (code, X86_EBX);
+               cfa_offset += sizeof (gpointer);
+               mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
 
                if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
                        /*
@@ -4389,8 +4591,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
                        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
+                               code = mono_x86_emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
+#ifdef TARGET_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));
@@ -4414,32 +4616,51 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                if (cfg->used_int_regs & (1 << X86_EBX)) {
                        x86_push_reg (code, X86_EBX);
                        pos += 4;
+                       cfa_offset += sizeof (gpointer);
+                       mono_emit_unwind_op_offset (cfg, code, X86_EBX, - cfa_offset);
                }
 
                if (cfg->used_int_regs & (1 << X86_EDI)) {
                        x86_push_reg (code, X86_EDI);
                        pos += 4;
+                       cfa_offset += sizeof (gpointer);
+                       mono_emit_unwind_op_offset (cfg, code, X86_EDI, - cfa_offset);
                }
 
                if (cfg->used_int_regs & (1 << X86_ESI)) {
                        x86_push_reg (code, X86_ESI);
                        pos += 4;
+                       cfa_offset += sizeof (gpointer);
+                       mono_emit_unwind_op_offset (cfg, code, X86_ESI, - cfa_offset);
                }
        }
 
        alloc_size -= pos;
 
        /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
-       if (mono_do_x86_stack_align) {
-               int tot = alloc_size + pos + 4 + 4; /* ret ip + ebp */
+       if (mono_do_x86_stack_align && need_stack_frame) {
+               int tot = alloc_size + pos + 4; /* ret ip */
+               if (need_stack_frame)
+                       tot += 4; /* ebp */
                tot &= MONO_ARCH_FRAME_ALIGNMENT - 1;
-               alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
+               if (tot)
+                       alloc_size += MONO_ARCH_FRAME_ALIGNMENT - tot;
        }
 
        if (alloc_size) {
                /* See mono_emit_stack_alloc */
-#if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
+#if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
                guint32 remaining_size = alloc_size;
+               /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
+               guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 8; /*8 is the max size of x86_alu_reg_imm + x86_test_membase_reg*/
+               guint32 offset = code - cfg->native_code;
+               if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
+                       while (required_code_size >= (cfg->code_size - offset))
+                               cfg->code_size *= 2;
+                       cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
+                       code = cfg->native_code + offset;
+                       mono_jit_stats.code_reallocs++;
+               }
                while (remaining_size >= 0x1000) {
                        x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
                        x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
@@ -4450,6 +4671,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 #else
                x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
 #endif
+
+               g_assert (need_stack_frame);
        }
 
        if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED ||
@@ -4459,7 +4682,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
 #if DEBUG_STACK_ALIGNMENT
        /* check the stack is aligned */
-       if (method->wrapper_type == MONO_WRAPPER_NONE) {
+       if (need_stack_frame && method->wrapper_type == MONO_WRAPPER_NONE) {
                x86_mov_reg_reg (code, X86_ECX, X86_ESP, 4);
                x86_alu_reg_imm (code, X86_AND, X86_ECX, MONO_ARCH_FRAME_ALIGNMENT - 1);
                x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
@@ -4507,6 +4730,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
                inst = cfg->args [pos];
                if (inst->opcode == OP_REGVAR) {
+                       g_assert (need_stack_frame);
                        x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
                        if (cfg->verbose_level > 2)
                                g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
@@ -4531,7 +4755,8 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        guint8 *code;
        int max_epilog_size = 16;
        CallInfo *cinfo;
-       
+       gboolean need_stack_frame = needs_stack_frame (cfg);
+
        if (cfg->method->save_lmf)
                max_epilog_size += 128;
 
@@ -4556,7 +4781,7 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                /* check if we need to restore protection of the stack after a stack overflow */
                if (mono_get_jit_tls_offset () != -1) {
                        guint8 *patch;
-                       code = emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
+                       code = mono_x86_emit_tls_get (code, X86_ECX, mono_get_jit_tls_offset ());
                        /* we load the value in a separate instruction: this mechanism may be
                         * used later as a safer way to do thread interruption
                         */
@@ -4628,8 +4853,10 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                        pos -= 4;
                }
 
-               if (pos)
+               if (pos) {
+                       g_assert (need_stack_frame);
                        x86_lea_membase (code, X86_ESP, X86_EBP, pos);
+               }
 
                if (cfg->used_int_regs & (1 << X86_ESI)) {
                        x86_pop_reg (code, X86_ESI);
@@ -4664,7 +4891,8 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                }
        }
 
-       x86_leave (code);
+       if (need_stack_frame)
+               x86_leave (code);
 
        if (CALLCONV_IS_STDCALL (sig)) {
                MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
@@ -4675,10 +4903,12 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        else
                stack_to_pop = 0;
 
-       if (stack_to_pop)
+       if (stack_to_pop) {
+               g_assert (need_stack_frame);
                x86_ret_imm (code, stack_to_pop);
-       else
+       } else {
                x86_ret (code);
+       }
 
        cfg->code_len = code - cfg->native_code;
 
@@ -4748,6 +4978,8 @@ mono_arch_emit_exceptions (MonoCompile *cfg)
                                /* Compute size of code following the push <OFFSET> */
                                size = 5 + 5;
 
+                               /*This is aligned to 16 bytes by the callee. This way we save a few bytes here.*/
+
                                if ((code - cfg->native_code) - throw_ip < 126 - size) {
                                        /* Use the shorter form */
                                        buf = buf2 = code;
@@ -4820,22 +5052,19 @@ mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
 {
        if (!tls_offset_inited) {
                if (!getenv ("MONO_NO_TLS")) {
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                        /* 
                         * We need to init this multiple times, since when we are first called, the key might not
                         * be initialized yet.
                         */
                        appdomain_tls_offset = mono_domain_get_tls_key ();
                        lmf_tls_offset = mono_get_jit_tls_key ();
-                       thread_tls_offset = mono_thread_get_tls_key ();
 
                        /* Only 64 tls entries can be accessed using inline code */
                        if (appdomain_tls_offset >= 64)
                                appdomain_tls_offset = -1;
                        if (lmf_tls_offset >= 64)
                                lmf_tls_offset = -1;
-                       if (thread_tls_offset >= 64)
-                               thread_tls_offset = -1;
 #else
 #if MONO_XEN_OPT
                        optimize_for_xen = access ("/proc/xen", F_OK) == 0;
@@ -4844,7 +5073,6 @@ mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
                        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
                }
        }               
@@ -4855,66 +5083,6 @@ mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
 {
 }
 
-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->generic_sharing_context, cfg->mempool, inst->signature, FALSE);
-
-       /* add the this argument */
-       if (this_reg != -1) {
-               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) {
-               MonoInst *vtarg;
-
-               if (cinfo->ret.storage == ArgValuetypeInReg) {
-                       /*
-                        * The valuetype is in EAX:EDX after the call, needs to be copied to
-                        * the stack. Save the address here, so the call instruction can
-                        * access it.
-                        */
-                       MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
-                       vtarg->inst_destbasereg = X86_ESP;
-                       vtarg->inst_offset = inst->stack_usage;
-                       vtarg->sreg1 = vt_reg;
-                       mono_bblock_add_inst (cfg->cbb, vtarg);
-               }
-               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);
-               }
-       }
-}
-
 #ifdef MONO_ARCH_HAVE_IMT
 
 // Linear handler, the bsearch head compare is shorter
@@ -4975,7 +5143,7 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
        if (fail_tramp)
                code = mono_method_alloc_generic_virtual_thunk (domain, size);
        else
-               code = mono_code_manager_reserve (domain->code_mp, size);
+               code = mono_domain_code_reserve (domain, size);
        start = code;
        for (i = 0; i < count; ++i) {
                MonoIMTCheckItem *item = imt_entries [i];
@@ -4986,7 +5154,7 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                        x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
                                item->jmp_code = code;
                                x86_branch8 (code, X86_CC_NE, 0, FALSE);
-                               if (fail_tramp)
+                               if (item->has_target_code)
                                        x86_jump_code (code, item->value.target_code);
                                else
                                        x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
@@ -4995,7 +5163,10 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                        x86_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)item->key);
                                        item->jmp_code = code;
                                        x86_branch8 (code, X86_CC_NE, 0, FALSE);
-                                       x86_jump_code (code, item->value.target_code);
+                                       if (item->has_target_code)
+                                               x86_jump_code (code, item->value.target_code);
+                                       else
+                                               x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
                                        x86_patch (item->jmp_code, code);
                                        x86_jump_code (code, fail_tramp);
                                        item->jmp_code = NULL;
@@ -5006,7 +5177,10 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                        item->jmp_code = code;
                                        x86_branch8 (code, X86_CC_NE, 0, FALSE);
 #endif
-                                       x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
+                                       if (item->has_target_code)
+                                               x86_jump_code (code, item->value.target_code);
+                                       else
+                                               x86_jump_mem (code, & (vtable->vtable [item->value.vtable_slot]));
 #if ENABLE_WRONG_METHOD_CHECK
                                        x86_patch (item->jmp_code, code);
                                        x86_breakpoint (code);
@@ -5040,13 +5214,13 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
 }
 
 MonoMethod*
-mono_arch_find_imt_method (gpointer *regs, guint8 *code)
+mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
 {
        return (MonoMethod*) regs [MONO_ARCH_IMT_REG];
 }
 
 MonoObject*
-mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
+mono_arch_find_this_argument (mgreg_t *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
 {
        MonoMethodSignature *sig = mono_method_signature (method);
        CallInfo *cinfo = get_call_info (gsctx, NULL, sig, FALSE);
@@ -5069,72 +5243,11 @@ mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSha
 #endif
 
 MonoVTable*
-mono_arch_find_static_call_vtable (gpointer *regs, guint8 *code)
+mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
 {
        return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
 }
 
-MonoInst*
-mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
-{
-       MonoInst *ins = NULL;
-
-       if (cmethod->klass == mono_defaults.math_class) {
-               if (strcmp (cmethod->name, "Sin") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_SIN);
-                       ins->inst_i0 = args [0];
-               } else if (strcmp (cmethod->name, "Cos") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_COS);
-                       ins->inst_i0 = args [0];
-               } else if (strcmp (cmethod->name, "Tan") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_TAN);
-                       ins->inst_i0 = args [0];
-               } else if (strcmp (cmethod->name, "Atan") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_ATAN);
-                       ins->inst_i0 = args [0];
-               } else if (strcmp (cmethod->name, "Sqrt") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_SQRT);
-                       ins->inst_i0 = args [0];
-               } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
-                       MONO_INST_NEW (cfg, ins, OP_ABS);
-                       ins->inst_i0 = args [0];
-               }
-
-               if (cfg->opt & MONO_OPT_CMOV) {
-                       int opcode = 0;
-
-                       if (strcmp (cmethod->name, "Min") == 0) {
-                               if (fsig->params [0]->type == MONO_TYPE_I4)
-                                       opcode = OP_IMIN;
-                               else if (fsig->params [0]->type == MONO_TYPE_U4)
-                                       opcode = OP_IMIN_UN;
-                       } else if (strcmp (cmethod->name, "Max") == 0) {
-                               if (fsig->params [0]->type == MONO_TYPE_I4)
-                                       opcode = OP_IMAX;
-                               else if (fsig->params [0]->type == MONO_TYPE_U4)
-                                       opcode = OP_IMAX_UN;
-                       }               
-
-                       if (opcode) {
-                               MONO_INST_NEW (cfg, ins, opcode);
-                               ins->inst_i0 = args [0];
-                               ins->inst_i1 = args [1];
-                       }
-               }
-
-#if 0
-               /* OP_FREM is not IEEE compatible */
-               else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_FREM);
-                       ins->inst_i0 = args [0];
-                       ins->inst_i1 = args [1];
-               }
-#endif
-       }
-
-       return ins;
-}
-
 MonoInst*
 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
@@ -5154,6 +5267,8 @@ mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMetho
                        opcode = OP_SQRT;
                } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
                        opcode = OP_ABS;
+               } else if (strcmp (cmethod->name, "Round") == 0 && fsig->param_count == 1 && fsig->params [0]->type == MONO_TYPE_R8) {
+                       opcode = OP_ROUND;
                }
                
                if (opcode) {
@@ -5218,18 +5333,6 @@ MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
        return ins;
 }
 
-MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
-{
-       MonoInst* ins;
-
-       if (thread_tls_offset == -1)
-               return NULL;
-
-       MONO_INST_NEW (cfg, ins, OP_TLS_GET);
-       ins->inst_offset = thread_tls_offset;
-       return ins;
-}
-
 guint32
 mono_arch_get_patch_offset (guint8 *code)
 {
@@ -5308,7 +5411,7 @@ mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guin
 }
 
 gpointer
-mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
+mono_arch_get_vcall_slot (guint8 *code, mgreg_t *regs, int *displacement)
 {
        guint8 buf [8];
        guint8 reg = 0;
@@ -5319,85 +5422,87 @@ mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
 
        *displacement = 0;
 
-       /* go to the start of the call instruction
-        *
-        * address_byte = (m << 6) | (o << 3) | reg
-        * call opcode: 0xff address_byte displacement
-        * 0xff m=1,o=2 imm8
-        * 0xff m=2,o=2 imm32
-        */
        code -= 6;
 
        /* 
         * 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.
+        * There are two types of calls:
+        * - direct calls: 0xff address_byte 8/32 bits displacement
+        * - indirect calls: nop nop nop <call>
+        * The nops make sure we don't confuse the instruction preceeding an indirect
+        * call with a direct call.
         */
-       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;
-#ifdef MONO_ARCH_HAVE_IMT
-       } else if ((code [-2] == 0xba) && (code [3] == 0xff) && (x86_modrm_mod (code [4]) == 1) && (x86_modrm_reg (code [4]) == 2) && ((signed char)code [5] < 0)) {
-               /* IMT-based interface calls: with MONO_ARCH_IMT_REG == edx
-                * ba 14 f8 28 08          mov    $0x828f814,%edx
-                * ff 50 fc                call   *0xfffffffc(%eax)
-                */
-               reg = code [4] & 0x07;
-               disp = (signed char)code [5];
-#endif
-       } else if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
+       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 {
-               if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
-                       reg = code [1] & 0x07;
-                       disp = *((gint32*)(code + 2));
-               } else if ((code [1] == 0xe8)) {
-                       return NULL;
-               } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
-                       /*
-                        * This is a interface call
-                        * 8b 40 30   mov    0x30(%eax),%eax
-                        * ff 10      call   *(%eax)
-                        */
-                       disp = 0;
-                       reg = code [5] & 0x07;
-               }
-               else
+       } else if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
+               reg = code [1] & 0x07;
+               disp = *((gint32*)(code + 2));
+       } else if ((code [1] == 0xe8)) {
                        return NULL;
+       } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
+               /*
+                * This is a interface call
+                * 8b 40 30   mov    0x30(%eax),%eax
+                * ff 10      call   *(%eax)
+                */
+               disp = 0;
+               reg = code [5] & 0x07;
        }
+       else
+               return NULL;
 
        *displacement = disp;
-       return regs [reg];
+       return (gpointer)regs [reg];
 }
 
-gpointer*
-mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
+/*
+ * mono_x86_get_this_arg_offset:
+ *
+ *   Return the offset of the stack location where this is passed during a virtual
+ * call.
+ */
+guint32
+mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig)
 {
-       gpointer vt;
-       int displacement;
-       vt = mono_arch_get_vcall_slot (code, regs, &displacement);
-       if (!vt)
-               return NULL;
-       return (gpointer*)((char*)vt + displacement);
+       CallInfo *cinfo = NULL;
+       int offset;
+
+       if (MONO_TYPE_ISSTRUCT (sig->ret)) {
+               cinfo = get_call_info (gsctx, NULL, sig, FALSE);
+
+               offset = cinfo->args [0].offset;
+       } else {
+               offset = 0;
+       }
+
+       return offset;
 }
 
 gpointer
 mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig,
-               gssize *regs, guint8 *code)
+               mgreg_t *regs, guint8 *code)
 {
        guint32 esp = regs [X86_ESP];
-       CallInfo *cinfo;
+       CallInfo *cinfo = NULL;
        gpointer res;
+       int offset;
 
-       if (!gsctx && code)
-               gsctx = mono_get_generic_context_from_code (code);
-       cinfo = get_call_info (gsctx, NULL, sig, FALSE);
+       /* 
+        * Avoid expensive calls to get_generic_context_from_code () + get_call_info 
+        * if possible.
+        */
+       if (MONO_TYPE_ISSTRUCT (sig->ret)) {
+               if (!gsctx && code)
+                       gsctx = mono_get_generic_context_from_code (code);
+               cinfo = get_call_info (gsctx, NULL, sig, FALSE);
+
+               offset = cinfo->args [0].offset;
+       } else {
+               offset = 0;
+       }
 
        /*
         * The stack looks like:
@@ -5407,8 +5512,9 @@ mono_arch_get_this_arg_from_call (MonoGenericSharingContext *gsctx, MonoMethodSi
         * <return addr>
         * <4 pointers pushed by mono_arch_create_trampoline_code ()>
         */
-       res = (((MonoObject**)esp) [5 + (cinfo->args [0].offset / 4)]);
-       g_free (cinfo);
+       res = (((MonoObject**)esp) [5 + (offset / 4)]);
+       if (cinfo)
+               g_free (cinfo);
        return res;
 }
 
@@ -5511,11 +5617,15 @@ gpointer
 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
 {
        switch (reg) {
+       case X86_EAX: return (gpointer)ctx->eax;
+       case X86_EBX: return (gpointer)ctx->ebx;
        case X86_ECX: return (gpointer)ctx->ecx;
        case X86_EDX: return (gpointer)ctx->edx;
-       case X86_EBP: return (gpointer)ctx->ebp;
        case X86_ESP: return (gpointer)ctx->esp;
-       default: return ((gpointer)(&ctx->eax)[reg]);
+       case X86_EBP: return (gpointer)ctx->ebp;
+       case X86_ESI: return (gpointer)ctx->esi;
+       case X86_EDI: return (gpointer)ctx->edi;
+       default: g_assert_not_reached ();
        }
 }
 
@@ -5538,11 +5648,9 @@ void
 mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
 {
        MonoInst *fconv;
-
        int dreg, src_opcode;
-       g_assert (cfg->new_ir);
 
-       if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD))
+       if (!(cfg->opt & MONO_OPT_SSE2) || !(cfg->opt & MONO_OPT_SIMD) || COMPILE_LLVM (cfg))
                return;
 
        switch (src_opcode = ins->opcode) {
@@ -5576,8 +5684,291 @@ mono_arch_decompose_opts (MonoCompile *cfg, MonoInst *ins)
        ins->dreg = dreg;
        ins->type = STACK_I4;
        ins->backend.source_opcode = src_opcode;
+}
+
+#endif /* #ifdef MONO_ARCH_SIMD_INTRINSICS */
+
+void
+mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
+{
+       MonoInst *ins;
+       int vreg;
+
+       if (long_ins->opcode == OP_LNEG) {
+               ins = long_ins;
+               MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 1, ins->sreg1 + 1);
+               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ADC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
+               MONO_EMIT_NEW_UNALU (cfg, OP_INEG, ins->dreg + 2, ins->dreg + 2);
+               NULLIFY_INS (ins);
+               return;
+       }
+
+#ifdef MONO_ARCH_SIMD_INTRINSICS
+
+       if (!(cfg->opt & MONO_OPT_SIMD))
+               return;
+       
+       /*TODO move this to simd-intrinsic.c once we support sse 4.1 dword extractors since we need the runtime caps info */ 
+       switch (long_ins->opcode) {
+       case OP_EXTRACT_I8:
+               vreg = long_ins->sreg1;
+       
+               if (long_ins->inst_c0) {
+                       MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
+                       ins->klass = long_ins->klass;
+                       ins->sreg1 = long_ins->sreg1;
+                       ins->inst_c0 = 2;
+                       ins->type = STACK_VTYPE;
+                       ins->dreg = vreg = alloc_ireg (cfg);
+                       MONO_ADD_INS (cfg->cbb, ins);
+               }
+       
+               MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
+               ins->klass = mono_defaults.int32_class;
+               ins->sreg1 = vreg;
+               ins->type = STACK_I4;
+               ins->dreg = long_ins->dreg + 1;
+               MONO_ADD_INS (cfg->cbb, ins);
+       
+               MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
+               ins->klass = long_ins->klass;
+               ins->sreg1 = long_ins->sreg1;
+               ins->inst_c0 = long_ins->inst_c0 ? 3 : 1;
+               ins->type = STACK_VTYPE;
+               ins->dreg = vreg = alloc_ireg (cfg);
+               MONO_ADD_INS (cfg->cbb, ins);
+       
+               MONO_INST_NEW (cfg, ins, OP_EXTRACT_I4);
+               ins->klass = mono_defaults.int32_class;
+               ins->sreg1 = vreg;
+               ins->type = STACK_I4;
+               ins->dreg = long_ins->dreg + 2;
+               MONO_ADD_INS (cfg->cbb, ins);
+       
+               long_ins->opcode = OP_NOP;
+               break;
+       case OP_INSERTX_I8_SLOW:
+               MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
+               ins->dreg = long_ins->dreg;
+               ins->sreg1 = long_ins->dreg;
+               ins->sreg2 = long_ins->sreg2 + 1;
+               ins->inst_c0 = long_ins->inst_c0 * 2;
+               MONO_ADD_INS (cfg->cbb, ins);
+
+               MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
+               ins->dreg = long_ins->dreg;
+               ins->sreg1 = long_ins->dreg;
+               ins->sreg2 = long_ins->sreg2 + 2;
+               ins->inst_c0 = long_ins->inst_c0 * 2 + 1;
+               MONO_ADD_INS (cfg->cbb, ins);
+
+               long_ins->opcode = OP_NOP;
+               break;
+       case OP_EXPAND_I8:
+               MONO_INST_NEW (cfg, ins, OP_ICONV_TO_X);
+               ins->dreg = long_ins->dreg;
+               ins->sreg1 = long_ins->sreg1 + 1;
+               ins->klass = long_ins->klass;
+               ins->type = STACK_VTYPE;
+               MONO_ADD_INS (cfg->cbb, ins);
+
+               MONO_INST_NEW (cfg, ins, OP_INSERTX_I4_SLOW);
+               ins->dreg = long_ins->dreg;
+               ins->sreg1 = long_ins->dreg;
+               ins->sreg2 = long_ins->sreg1 + 2;
+               ins->inst_c0 = 1;
+               ins->klass = long_ins->klass;
+               ins->type = STACK_VTYPE;
+               MONO_ADD_INS (cfg->cbb, ins);
+
+               MONO_INST_NEW (cfg, ins, OP_PSHUFLED);
+               ins->dreg = long_ins->dreg;
+               ins->sreg1 = long_ins->dreg;;
+               ins->inst_c0 = 0x44; /*Magic number for swizzling (X,Y,X,Y)*/
+               ins->klass = long_ins->klass;
+               ins->type = STACK_VTYPE;
+               MONO_ADD_INS (cfg->cbb, ins);
+
+               long_ins->opcode = OP_NOP;
+               break;
+       }
+#endif /* MONO_ARCH_SIMD_INTRINSICS */
+}
+
+#if __APPLE__
+#define DBG_SIGNAL SIGBUS
+#else
+#define DBG_SIGNAL SIGSEGV
+#endif
+
+/* Soft Debug support */
+#ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
+
+/*
+ * mono_arch_set_breakpoint:
+ *
+ *   Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
+ * The location should contain code emitted by OP_SEQ_POINT.
+ */
+void
+mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
+{
+       guint8 *code = ip;
+
+       /* 
+        * In production, we will use int3 (has to fix the size in the md 
+        * file). But that could confuse gdb, so during development, we emit a SIGSEGV
+        * instead.
+        */
+       g_assert (code [0] == 0x90);
+       x86_alu_reg_mem (code, X86_CMP, X86_EAX, (guint32)bp_trigger_page);
+}
+
+/*
+ * mono_arch_clear_breakpoint:
+ *
+ *   Clear the breakpoint at IP.
+ */
+void
+mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
+{
+       guint8 *code = ip;
+       int i;
+
+       for (i = 0; i < 6; ++i)
+               x86_nop (code);
+}
+       
+/*
+ * mono_arch_start_single_stepping:
+ *
+ *   Start single stepping.
+ */
+void
+mono_arch_start_single_stepping (void)
+{
+       mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
+}
+       
+/*
+ * mono_arch_stop_single_stepping:
+ *
+ *   Stop single stepping.
+ */
+void
+mono_arch_stop_single_stepping (void)
+{
+       mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
+}
+
+/*
+ * mono_arch_is_single_step_event:
+ *
+ *   Return whenever the machine state in SIGCTX corresponds to a single
+ * step event.
+ */
+gboolean
+mono_arch_is_single_step_event (void *info, void *sigctx)
+{
+#ifdef TARGET_WIN32
+       EXCEPTION_RECORD* einfo = (EXCEPTION_RECORD*)info;      /* Sometimes the address is off by 4 */
+       if ((einfo->ExceptionInformation[1] >= ss_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)ss_trigger_page + 128))
+               return TRUE;
+       else
+               return FALSE;
+#else
+       siginfo_t* sinfo = (siginfo_t*) info;
+       /* Sometimes the address is off by 4 */
+       if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128))
+               return TRUE;
+       else
+               return FALSE;
+#endif
+}
 
+gboolean
+mono_arch_is_breakpoint_event (void *info, void *sigctx)
+{
+#ifdef TARGET_WIN32
+       EXCEPTION_RECORD* einfo = (EXCEPTION_RECORD*)info;      /* Sometimes the address is off by 4 */
+       if ((einfo->ExceptionInformation[1] >= bp_trigger_page && (guint8*)einfo->ExceptionInformation[1] <= (guint8*)bp_trigger_page + 128))
+               return TRUE;
+       else
+               return FALSE;
+#else
+       siginfo_t* sinfo = (siginfo_t*)info;
+       /* Sometimes the address is off by 4 */
+       if (sinfo->si_signo == DBG_SIGNAL && (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128))
+               return TRUE;
+       else
+               return FALSE;
+#endif
+}
+
+/*
+ * mono_arch_get_ip_for_breakpoint:
+ *
+ *   See mini-amd64.c for docs.
+ */
+guint8*
+mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
+{
+       guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
+
+       return ip;
+}
+
+#define BREAKPOINT_SIZE 6
+
+/*
+ * mono_arch_get_ip_for_single_step:
+ *
+ *   See mini-amd64.c for docs.
+ */
+guint8*
+mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
+{
+       guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
+
+       /* Size of x86_alu_reg_imm */
+       ip += 6;
+
+       return ip;
+}
+
+/*
+ * mono_arch_skip_breakpoint:
+ *
+ *   See mini-amd64.c for docs.
+ */
+void
+mono_arch_skip_breakpoint (MonoContext *ctx)
+{
+       MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + BREAKPOINT_SIZE);
+}
+
+/*
+ * mono_arch_skip_single_step:
+ *
+ *   See mini-amd64.c for docs.
+ */
+void
+mono_arch_skip_single_step (MonoContext *ctx)
+{
+       MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 6);
+}
 
+/*
+ * mono_arch_get_seq_point_info:
+ *
+ *   See mini-amd64.c for docs.
+ */
+gpointer
+mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
+{
+       NOT_IMPLEMENTED;
+       return NULL;
 }
+
 #endif