2009-04-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-amd64.c
index b6d5de3520c40042d3e4b14782f75878635fae42..92183fceb14ac6a5354fe9607b6c8b485bd7100f 100644 (file)
 #include <mono/utils/mono-math.h>
 
 #include "trace.h"
+#include "ir-emit.h"
 #include "mini-amd64.h"
-#include "inssel.h"
 #include "cpu-amd64.h"
 
+/* 
+ * Can't define this in mini-amd64.h cause that would turn on the generic code in
+ * method-to-ir.c.
+ */
+#define MONO_ARCH_IMT_REG AMD64_R11
+
 static gint lmf_tls_offset = -1;
 static gint lmf_addr_tls_offset = -1;
 static gint appdomain_tls_offset = -1;
@@ -48,8 +54,8 @@ static gboolean optimize_for_xen = TRUE;
 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
 
 #ifdef PLATFORM_WIN32
-/* Under windows, the default pinvoke calling convention is stdcall */
-#define CALLCONV_IS_STDCALL(call_conv) (((call_conv) == MONO_CALL_STDCALL) || ((call_conv) == MONO_CALL_DEFAULT))
+/* Under windows, the calling convention is never stdcall */
+#define CALLCONV_IS_STDCALL(call_conv) (FALSE)
 #else
 #define CALLCONV_IS_STDCALL(call_conv) ((call_conv) == MONO_CALL_STDCALL)
 #endif
@@ -242,13 +248,6 @@ typedef struct {
 
 #define DEBUG(a) if (cfg->verbose_level > 1) a
 
-#define NEW_ICONST(cfg,dest,val) do {  \
-               (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
-               (dest)->opcode = OP_ICONST;     \
-               (dest)->inst_c0 = (val);        \
-               (dest)->type = STACK_I4;        \
-       } while (0)
-
 #ifdef PLATFORM_WIN32
 #define PARAM_REGS 4
 
@@ -318,7 +317,7 @@ merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
        ArgumentClass class2 = ARG_CLASS_NO_CLASS;
        MonoType *ptype;
 
-       ptype = mono_type_get_underlying_type (type);
+       ptype = mini_type_get_underlying_type (NULL, type);
        switch (ptype->type) {
        case MONO_TYPE_BOOLEAN:
        case MONO_TYPE_CHAR:
@@ -395,16 +394,24 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
 {
        guint32 size, quad, nquads, i;
        ArgumentClass args [2];
-       MonoMarshalType *info;
+       MonoMarshalType *info = NULL;
        MonoClass *klass;
+       MonoGenericSharingContext tmp_gsctx;
+
+       /* 
+        * The gsctx currently contains no data, it is only used for checking whenever
+        * open types are allowed, some callers like mono_arch_get_argument_info ()
+        * don't pass it to us, so work around that.
+        */
+       if (!gsctx)
+               gsctx = &tmp_gsctx;
 
        klass = mono_class_from_mono_type (type);
-       if (sig->pinvoke) 
-               size = mono_type_native_stack_size (&klass->byval_arg, NULL);
-       else 
-               size = mini_type_stack_size (gsctx, &klass->byval_arg, NULL);
+       size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
 #ifndef PLATFORM_WIN32
-       if (!sig->pinvoke || (size == 0) || (size > 16)) {
+       if (!sig->pinvoke && !disable_vtypes_in_regs && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
+               /* We pass and return vtypes of size 8 in a register */
+       } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
 #else
        if (!sig->pinvoke) {
 #endif
@@ -425,78 +432,92 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
        else
                nquads = 1;
 
-       /*
-        * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
-        * The X87 and SSEUP stuff is left out since there are no such types in
-        * the CLR.
-        */
-       info = mono_marshal_load_type_info (klass);
-       g_assert (info);
+       if (!sig->pinvoke) {
+               /* Always pass in 1 or 2 integer registers */
+               args [0] = ARG_CLASS_INTEGER;
+               args [1] = ARG_CLASS_INTEGER;
+               /* Only the simplest cases are supported */
+               if (is_return && nquads != 1) {
+                       args [0] = ARG_CLASS_MEMORY;
+                       args [1] = ARG_CLASS_MEMORY;
+               }
+       } else {
+               /*
+                * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
+                * The X87 and SSEUP stuff is left out since there are no such types in
+                * the CLR.
+                */
+               info = mono_marshal_load_type_info (klass);
+               g_assert (info);
 
 #ifndef PLATFORM_WIN32
-       if (info->native_size > 16) {
-               ainfo->offset = *stack_size;
-               *stack_size += ALIGN_TO (info->native_size, 8);
-               ainfo->storage = ArgOnStack;
-
-               return;
-       }
-#else
-       switch (info->native_size) {
-       case 1: case 2: case 4: case 8:
-               break;
-       default:
-               if (is_return) {
-                       ainfo->storage = ArgOnStack;
+               if (info->native_size > 16) {
                        ainfo->offset = *stack_size;
                        *stack_size += ALIGN_TO (info->native_size, 8);
-               }
-               else {
-                       ainfo->storage = ArgValuetypeAddrInIReg;
+                       ainfo->storage = ArgOnStack;
 
-                       if (*gr < PARAM_REGS) {
-                               ainfo->pair_storage [0] = ArgInIReg;
-                               ainfo->pair_regs [0] = param_regs [*gr];
-                               (*gr) ++;
-                       } 
-                       else {
-                               ainfo->pair_storage [0] = ArgOnStack;
+                       return;
+               }
+#else
+               switch (info->native_size) {
+               case 1: case 2: case 4: case 8:
+                       break;
+               default:
+                       if (is_return) {
+                               ainfo->storage = ArgOnStack;
                                ainfo->offset = *stack_size;
-                               *stack_size += 8;
+                               *stack_size += ALIGN_TO (info->native_size, 8);
                        }
-               }
+                       else {
+                               ainfo->storage = ArgValuetypeAddrInIReg;
 
-               return;
-       }
+                               if (*gr < PARAM_REGS) {
+                                       ainfo->pair_storage [0] = ArgInIReg;
+                                       ainfo->pair_regs [0] = param_regs [*gr];
+                                       (*gr) ++;
+                               }
+                               else {
+                                       ainfo->pair_storage [0] = ArgOnStack;
+                                       ainfo->offset = *stack_size;
+                                       *stack_size += 8;
+                               }
+                       }
+
+                       return;
+               }
 #endif
 
-       args [0] = ARG_CLASS_NO_CLASS;
-       args [1] = ARG_CLASS_NO_CLASS;
-       for (quad = 0; quad < nquads; ++quad) {
-               int size;
-               guint32 align;
-               ArgumentClass class1;
+               args [0] = ARG_CLASS_NO_CLASS;
+               args [1] = ARG_CLASS_NO_CLASS;
+               for (quad = 0; quad < nquads; ++quad) {
+                       int size;
+                       guint32 align;
+                       ArgumentClass class1;
                
-               class1 = ARG_CLASS_NO_CLASS;
-               for (i = 0; i < info->num_fields; ++i) {
-                       size = mono_marshal_type_size (info->fields [i].field->type, 
-                                                                                  info->fields [i].mspec, 
-                                                                                  &align, TRUE, klass->unicode);
-                       if ((info->fields [i].offset < 8) && (info->fields [i].offset + size) > 8) {
-                               /* Unaligned field */
-                               NOT_IMPLEMENTED;
-                       }
+                       if (info->num_fields == 0)
+                               class1 = ARG_CLASS_MEMORY;
+                       else
+                               class1 = ARG_CLASS_NO_CLASS;
+                       for (i = 0; i < info->num_fields; ++i) {
+                               size = mono_marshal_type_size (info->fields [i].field->type, 
+                                                                                          info->fields [i].mspec, 
+                                                                                          &align, TRUE, klass->unicode);
+                               if ((info->fields [i].offset < 8) && (info->fields [i].offset + size) > 8) {
+                                       /* Unaligned field */
+                                       NOT_IMPLEMENTED;
+                               }
 
-                       /* Skip fields in other quad */
-                       if ((quad == 0) && (info->fields [i].offset >= 8))
-                               continue;
-                       if ((quad == 1) && (info->fields [i].offset < 8))
-                               continue;
+                               /* Skip fields in other quad */
+                               if ((quad == 0) && (info->fields [i].offset >= 8))
+                                       continue;
+                               if ((quad == 1) && (info->fields [i].offset < 8))
+                                       continue;
 
-                       class1 = merge_argument_class_from_type (info->fields [i].field->type, class1);
+                               class1 = merge_argument_class_from_type (info->fields [i].field->type, class1);
+                       }
+                       g_assert (class1 != ARG_CLASS_NO_CLASS);
+                       args [quad] = class1;
                }
-               g_assert (class1 != ARG_CLASS_NO_CLASS);
-               args [quad] = class1;
        }
 
        /* Post merger cleanup */
@@ -546,7 +567,10 @@ add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgIn
                        *fr = orig_fr;
 
                        ainfo->offset = *stack_size;
-                       *stack_size += ALIGN_TO (info->native_size, 8);
+                       if (sig->pinvoke)
+                               *stack_size += ALIGN_TO (info->native_size, 8);
+                       else
+                               *stack_size += nquads * sizeof (gpointer);
                        ainfo->storage = ArgOnStack;
                }
        }
@@ -578,8 +602,7 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
 
        /* return value */
        {
-               ret_type = mono_type_get_underlying_type (sig->ret);
-               ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
+               ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
                switch (ret_type->type) {
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
@@ -658,6 +681,14 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
                ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
                MonoType *ptype;
 
+#ifdef PLATFORM_WIN32
+               /* The float param registers and other param registers must be the same index on Windows x64.*/
+               if (gr > fr)
+                       fr = gr;
+               else if (fr > gr)
+                       gr = fr;
+#endif
+
                if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
                        /* We allways pass the sig cookie on the stack for simplicity */
                        /* 
@@ -675,8 +706,7 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
                        add_general (&gr, &stack_size, ainfo);
                        continue;
                }
-               ptype = mono_type_get_underlying_type (sig->params [i]);
-               ptype = mini_get_basic_type_from_generic (gsctx, ptype);
+               ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
                switch (ptype->type) {
                case MONO_TYPE_BOOLEAN:
                case MONO_TYPE_I1:
@@ -713,8 +743,12 @@ get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSign
                        add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
                        break;
                case MONO_TYPE_TYPEDBYREF:
+#ifdef PLATFORM_WIN32
+                       add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
+#else
                        stack_size += sizeof (MonoTypedRef);
                        ainfo->storage = ArgOnStack;
+#endif
                        break;
                case MONO_TYPE_U8:
                case MONO_TYPE_I8:
@@ -871,6 +905,7 @@ mono_arch_cpu_optimizazions (guint32 *exclude_mask)
                } else
                        *exclude_mask |= MONO_OPT_CMOV;
        }
+
        return opts;
 }
 
@@ -973,11 +1008,6 @@ mono_arch_compute_omit_fp (MonoCompile *cfg)
 
                locals_size += mono_type_size (ins->inst_vtype, &ialign);
        }
-
-       if ((cfg->num_varinfo > 10000) || (locals_size >= (1 << 15))) {
-               /* Avoid hitting the stack_alloc_size < (1 << 16) assertion in emit_epilog () */
-               cfg->arch.omit_fp = FALSE;
-       }
 }
 
 GList *
@@ -987,19 +1017,104 @@ mono_arch_get_global_int_regs (MonoCompile *cfg)
 
        mono_arch_compute_omit_fp (cfg);
 
-       if (cfg->arch.omit_fp)
-               regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
+       if (cfg->globalra) {
+               if (cfg->arch.omit_fp)
+                       regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R12);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R13);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R14);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R10);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R9);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R8);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
+       } else {
+               if (cfg->arch.omit_fp)
+                       regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
+
+               /* We use the callee saved registers for global allocation */
+               regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R12);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R13);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R14);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+#ifdef PLATFORM_WIN32
+               regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
+#endif
+       }
+
+       return regs;
+}
+GList*
+mono_arch_get_global_fp_regs (MonoCompile *cfg)
+{
+       GList *regs = NULL;
+       int i;
 
-       /* We use the callee saved registers for global allocation */
-       regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
-       regs = g_list_prepend (regs, (gpointer)AMD64_R12);
-       regs = g_list_prepend (regs, (gpointer)AMD64_R13);
-       regs = g_list_prepend (regs, (gpointer)AMD64_R14);
-       regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+       /* All XMM registers */
+       for (i = 0; i < 16; ++i)
+               regs = g_list_prepend (regs, GINT_TO_POINTER (i));
 
        return regs;
 }
 
+GList*
+mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
+{
+       static GList *r = NULL;
+
+       if (r == NULL) {
+               GList *regs = NULL;
+
+               regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R12);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R13);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R14);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R15);
+
+               regs = g_list_prepend (regs, (gpointer)AMD64_R10);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R9);
+               regs = g_list_prepend (regs, (gpointer)AMD64_R8);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
+               regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
+
+               InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
+       }
+
+       return r;
+}
+
+GList*
+mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
+{
+       int i;
+       static GList *r = NULL;
+
+       if (r == NULL) {
+               GList *regs = NULL;
+
+               for (i = 0; i < AMD64_XMM_NREG; ++i)
+                       regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
+
+               InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
+       }
+
+       return r;
+}
+
 /*
  * mono_arch_regalloc_cost:
  *
@@ -1020,13 +1135,97 @@ mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
                /* push+pop */
                return (ins->opcode == OP_ARG) ? 1 : 2;
 }
+
+/*
+ * mono_arch_fill_argument_info:
+ *
+ *   Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
+ * of the method.
+ */
+void
+mono_arch_fill_argument_info (MonoCompile *cfg)
+{
+       MonoMethodSignature *sig;
+       MonoMethodHeader *header;
+       MonoInst *ins;
+       int i;
+       CallInfo *cinfo;
+
+       header = mono_method_get_header (cfg->method);
+
+       sig = mono_method_signature (cfg->method);
+
+       cinfo = cfg->arch.cinfo;
+
+       /*
+        * Contrary to mono_arch_allocate_vars (), the information should describe
+        * where the arguments are at the beginning of the method, not where they can be 
+        * accessed during the execution of the method. The later makes no sense for the 
+        * global register allocator, since a variable can be in more than one location.
+        */
+       if (sig->ret->type != MONO_TYPE_VOID) {
+               switch (cinfo->ret.storage) {
+               case ArgInIReg:
+               case ArgInFloatSSEReg:
+               case ArgInDoubleSSEReg:
+                       if ((MONO_TYPE_ISSTRUCT (sig->ret) && !mono_class_from_mono_type (sig->ret)->enumtype) || (sig->ret->type == MONO_TYPE_TYPEDBYREF)) {
+                               cfg->vret_addr->opcode = OP_REGVAR;
+                               cfg->vret_addr->inst_c0 = cinfo->ret.reg;
+                       }
+                       else {
+                               cfg->ret->opcode = OP_REGVAR;
+                               cfg->ret->inst_c0 = cinfo->ret.reg;
+                       }
+                       break;
+               case ArgValuetypeInReg:
+                       cfg->ret->opcode = OP_REGOFFSET;
+                       cfg->ret->inst_basereg = -1;
+                       cfg->ret->inst_offset = -1;
+                       break;
+               default:
+                       g_assert_not_reached ();
+               }
+       }
+
+       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
+               ArgInfo *ainfo = &cinfo->args [i];
+               MonoType *arg_type;
+
+               ins = cfg->args [i];
+
+               if (sig->hasthis && (i == 0))
+                       arg_type = &mono_defaults.object_class->byval_arg;
+               else
+                       arg_type = sig->params [i - sig->hasthis];
+
+               switch (ainfo->storage) {
+               case ArgInIReg:
+               case ArgInFloatSSEReg:
+               case ArgInDoubleSSEReg:
+                       ins->opcode = OP_REGVAR;
+                       ins->inst_c0 = ainfo->reg;
+                       break;
+               case ArgOnStack:
+                       ins->opcode = OP_REGOFFSET;
+                       ins->inst_basereg = -1;
+                       ins->inst_offset = -1;
+                       break;
+               case ArgValuetypeInReg:
+                       /* Dummy */
+                       ins->opcode = OP_NOP;
+                       break;
+               default:
+                       g_assert_not_reached ();
+               }
+       }
+}
  
 void
 mono_arch_allocate_vars (MonoCompile *cfg)
 {
        MonoMethodSignature *sig;
        MonoMethodHeader *header;
-       MonoInst *inst;
+       MonoInst *ins;
        int i, offset;
        guint32 locals_stack_size, locals_stack_align;
        gint32 *offsets;
@@ -1083,19 +1282,24 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                case ArgInFloatSSEReg:
                case ArgInDoubleSSEReg:
                        if ((MONO_TYPE_ISSTRUCT (sig->ret) && !mono_class_from_mono_type (sig->ret)->enumtype) || (sig->ret->type == MONO_TYPE_TYPEDBYREF)) {
-                               /* The register is volatile */
-                               cfg->vret_addr->opcode = OP_REGOFFSET;
-                               cfg->vret_addr->inst_basereg = cfg->frame_reg;
-                               if (cfg->arch.omit_fp) {
-                                       cfg->vret_addr->inst_offset = offset;
-                                       offset += 8;
+                               if (cfg->globalra) {
+                                       cfg->vret_addr->opcode = OP_REGVAR;
+                                       cfg->vret_addr->inst_c0 = cinfo->ret.reg;
                                } else {
-                                       offset += 8;
-                                       cfg->vret_addr->inst_offset = -offset;
-                               }
-                               if (G_UNLIKELY (cfg->verbose_level > 1)) {
-                                       printf ("vret_addr =");
-                                       mono_print_ins (cfg->vret_addr);
+                                       /* The register is volatile */
+                                       cfg->vret_addr->opcode = OP_REGOFFSET;
+                                       cfg->vret_addr->inst_basereg = cfg->frame_reg;
+                                       if (cfg->arch.omit_fp) {
+                                               cfg->vret_addr->inst_offset = offset;
+                                               offset += 8;
+                                       } else {
+                                               offset += 8;
+                                               cfg->vret_addr->inst_offset = -offset;
+                                       }
+                                       if (G_UNLIKELY (cfg->verbose_level > 1)) {
+                                               printf ("vret_addr =");
+                                               mono_print_ins (cfg->vret_addr);
+                                       }
                                }
                        }
                        else {
@@ -1118,28 +1322,31 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                default:
                        g_assert_not_reached ();
                }
-               cfg->ret->dreg = cfg->ret->inst_c0;
+               if (!cfg->globalra)
+                       cfg->ret->dreg = cfg->ret->inst_c0;
        }
 
        /* Allocate locals */
-       offsets = mono_allocate_stack_slots_full (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
-       if (locals_stack_align) {
-               offset += (locals_stack_align - 1);
-               offset &= ~(locals_stack_align - 1);
-       }
-       for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
-               if (offsets [i] != -1) {
-                       MonoInst *inst = cfg->varinfo [i];
-                       inst->opcode = OP_REGOFFSET;
-                       inst->inst_basereg = cfg->frame_reg;
-                       if (cfg->arch.omit_fp)
-                               inst->inst_offset = (offset + offsets [i]);
-                       else
-                               inst->inst_offset = - (offset + offsets [i]);
-                       //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
+       if (!cfg->globalra) {
+               offsets = mono_allocate_stack_slots_full (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
+               if (locals_stack_align) {
+                       offset += (locals_stack_align - 1);
+                       offset &= ~(locals_stack_align - 1);
+               }
+               for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
+                       if (offsets [i] != -1) {
+                               MonoInst *ins = cfg->varinfo [i];
+                               ins->opcode = OP_REGOFFSET;
+                               ins->inst_basereg = cfg->frame_reg;
+                               if (cfg->arch.omit_fp)
+                                       ins->inst_offset = (offset + offsets [i]);
+                               else
+                                       ins->inst_offset = - (offset + offsets [i]);
+                               //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
+                       }
                }
+               offset += locals_stack_size;
        }
-       offset += locals_stack_size;
 
        if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
                g_assert (!cfg->arch.omit_fp);
@@ -1148,8 +1355,8 @@ mono_arch_allocate_vars (MonoCompile *cfg)
        }
 
        for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
-               inst = cfg->args [i];
-               if (inst->opcode != OP_REGVAR) {
+               ins = cfg->args [i];
+               if (ins->opcode != OP_REGVAR) {
                        ArgInfo *ainfo = &cinfo->args [i];
                        gboolean inreg = TRUE;
                        MonoType *arg_type;
@@ -1159,8 +1366,43 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                        else
                                arg_type = sig->params [i - sig->hasthis];
 
+                       if (cfg->globalra) {
+                               /* The new allocator needs info about the original locations of the arguments */
+                               switch (ainfo->storage) {
+                               case ArgInIReg:
+                               case ArgInFloatSSEReg:
+                               case ArgInDoubleSSEReg:
+                                       ins->opcode = OP_REGVAR;
+                                       ins->inst_c0 = ainfo->reg;
+                                       break;
+                               case ArgOnStack:
+                                       g_assert (!cfg->arch.omit_fp);
+                                       ins->opcode = OP_REGOFFSET;
+                                       ins->inst_basereg = cfg->frame_reg;
+                                       ins->inst_offset = ainfo->offset + ARGS_OFFSET;
+                                       break;
+                               case ArgValuetypeInReg:
+                                       ins->opcode = OP_REGOFFSET;
+                                       ins->inst_basereg = cfg->frame_reg;
+                                       /* These arguments are saved to the stack in the prolog */
+                                       offset = ALIGN_TO (offset, sizeof (gpointer));
+                                       if (cfg->arch.omit_fp) {
+                                               ins->inst_offset = offset;
+                                               offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
+                                       } else {
+                                               offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
+                                               ins->inst_offset = - offset;
+                                       }
+                                       break;
+                               default:
+                                       g_assert_not_reached ();
+                               }
+
+                               continue;
+                       }
+
                        /* FIXME: Allocate volatile arguments to registers */
-                       if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
+                       if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
                                inreg = FALSE;
 
                        /* 
@@ -1171,40 +1413,62 @@ mono_arch_allocate_vars (MonoCompile *cfg)
                        if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg))
                                inreg = FALSE;
 
-                       inst->opcode = OP_REGOFFSET;
+                       ins->opcode = OP_REGOFFSET;
 
                        switch (ainfo->storage) {
                        case ArgInIReg:
                        case ArgInFloatSSEReg:
                        case ArgInDoubleSSEReg:
-                               inst->opcode = OP_REGVAR;
-                               inst->dreg = ainfo->reg;
+                               if (inreg) {
+                                       ins->opcode = OP_REGVAR;
+                                       ins->dreg = ainfo->reg;
+                               }
                                break;
                        case ArgOnStack:
                                g_assert (!cfg->arch.omit_fp);
-                               inst->opcode = OP_REGOFFSET;
-                               inst->inst_basereg = cfg->frame_reg;
-                               inst->inst_offset = ainfo->offset + ARGS_OFFSET;
+                               ins->opcode = OP_REGOFFSET;
+                               ins->inst_basereg = cfg->frame_reg;
+                               ins->inst_offset = ainfo->offset + ARGS_OFFSET;
                                break;
                        case ArgValuetypeInReg:
                                break;
-                       case ArgValuetypeAddrInIReg:
-                               break;  /*FIXME: Not sure what to do for this case yet on Winx64*/
+                       case ArgValuetypeAddrInIReg: {
+                               MonoInst *indir;
+                               g_assert (!cfg->arch.omit_fp);
+                               
+                               MONO_INST_NEW (cfg, indir, 0);
+                               indir->opcode = OP_REGOFFSET;
+                               if (ainfo->pair_storage [0] == ArgInIReg) {
+                                       indir->inst_basereg = cfg->frame_reg;
+                                       offset = ALIGN_TO (offset, sizeof (gpointer));
+                                       offset += (sizeof (gpointer));
+                                       indir->inst_offset = - offset;
+                               }
+                               else {
+                                       indir->inst_basereg = cfg->frame_reg;
+                                       indir->inst_offset = ainfo->offset + ARGS_OFFSET;
+                               }
+                               
+                               ins->opcode = OP_VTARG_ADDR;
+                               ins->inst_left = indir;
+                               
+                               break;
+                       }
                        default:
                                NOT_IMPLEMENTED;
                        }
 
-                       if (!inreg && (ainfo->storage != ArgOnStack)) {
-                               inst->opcode = OP_REGOFFSET;
-                               inst->inst_basereg = cfg->frame_reg;
+                       if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
+                               ins->opcode = OP_REGOFFSET;
+                               ins->inst_basereg = cfg->frame_reg;
                                /* These arguments are saved to the stack in the prolog */
                                offset = ALIGN_TO (offset, sizeof (gpointer));
                                if (cfg->arch.omit_fp) {
-                                       inst->inst_offset = offset;
+                                       ins->inst_offset = offset;
                                        offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
                                } else {
                                        offset += (ainfo->storage == ArgValuetypeInReg) ? 2 * sizeof (gpointer) : sizeof (gpointer);
-                                       inst->inst_offset = - offset;
+                                       ins->inst_offset = - offset;
                                }
                        }
                }
@@ -1238,46 +1502,50 @@ mono_arch_create_vars (MonoCompile *cfg)
 }
 
 static void
-add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, MonoInst *arg, ArgStorage storage, int reg, MonoInst *tree)
+add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
 {
+       MonoInst *ins;
+
        switch (storage) {
        case ArgInIReg:
-               arg->opcode = OP_OUTARG_REG;
-               arg->inst_left = tree;
-               arg->inst_call = call;
-               arg->backend.reg3 = reg;
+               MONO_INST_NEW (cfg, ins, OP_MOVE);
+               ins->dreg = mono_alloc_ireg (cfg);
+               ins->sreg1 = tree->dreg;
+               MONO_ADD_INS (cfg->cbb, ins);
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
                break;
        case ArgInFloatSSEReg:
-               arg->opcode = OP_AMD64_OUTARG_XMMREG_R4;
-               arg->inst_left = tree;
-               arg->inst_call = call;
-               arg->backend.reg3 = reg;
+               MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
+               ins->dreg = mono_alloc_freg (cfg);
+               ins->sreg1 = tree->dreg;
+               MONO_ADD_INS (cfg->cbb, ins);
+
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
                break;
        case ArgInDoubleSSEReg:
-               arg->opcode = OP_AMD64_OUTARG_XMMREG_R8;
-               arg->inst_left = tree;
-               arg->inst_call = call;
-               arg->backend.reg3 = reg;
+               MONO_INST_NEW (cfg, ins, OP_FMOVE);
+               ins->dreg = mono_alloc_freg (cfg);
+               ins->sreg1 = tree->dreg;
+               MONO_ADD_INS (cfg->cbb, ins);
+
+               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
+
                break;
        default:
                g_assert_not_reached ();
        }
 }
 
-/* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
- * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info 
- */
-
 static int
-arg_storage_to_ldind (ArgStorage storage)
+arg_storage_to_load_membase (ArgStorage storage)
 {
        switch (storage) {
        case ArgInIReg:
-               return CEE_LDIND_I;
+               return OP_LOAD_MEMBASE;
        case ArgInDoubleSSEReg:
-               return CEE_LDIND_R8;
+               return OP_LOADR8_MEMBASE;
        case ArgInFloatSSEReg:
-               return CEE_LDIND_R4;
+               return OP_LOADR4_MEMBASE;
        default:
                g_assert_not_reached ();
        }
@@ -1291,12 +1559,15 @@ emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
        MonoInst *arg;
        MonoMethodSignature *tmp_sig;
        MonoInst *sig_arg;
-                       
+
+       if (call->tail_call)
+               NOT_IMPLEMENTED;
+
        /* FIXME: Add support for signature tokens to AOT */
        cfg->disable_aot = TRUE;
 
        g_assert (cinfo->sig_cookie.storage == ArgOnStack);
-
+                       
        /*
         * mono_ArgIterator_Setup assumes the signature cookie is 
         * passed first and all the arguments which were before it are
@@ -1309,23 +1580,18 @@ emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
        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->dreg = mono_alloc_ireg (cfg);
        sig_arg->inst_p0 = tmp_sig;
+       MONO_ADD_INS (cfg->cbb, sig_arg);
 
-       MONO_INST_NEW (cfg, arg, OP_OUTARG);
-       arg->inst_left = sig_arg;
-       arg->type = STACK_PTR;
-       MONO_INST_LIST_ADD (&arg->node, &call->out_args);
+       MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
+       arg->sreg1 = sig_arg->dreg;
+       MONO_ADD_INS (cfg->cbb, arg);
 }
 
-/* 
- * take the arguments and generate the arch-specific
- * instructions to properly call the function in call.
- * This includes pushing, moving arguments to the right register
- * etc.
- * Issue: who does the spilling if needed, and when?
- */
-MonoCallInst*
-mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
+void
+mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
+{
        MonoInst *arg, *in;
        MonoMethodSignature *sig;
        int i, n, stack_size;
@@ -1339,30 +1605,89 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
 
        cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig, sig->pinvoke);
 
+       if (cfg->compile_llvm) {
+               for (i = 0; i < n; ++i) {
+                       MonoInst *ins;
+
+                       ainfo = cinfo->args + i;
+
+                       in = call->args [i];
+
+                       /* Simply remember the arguments */
+                       switch (ainfo->storage) {
+                       case ArgInIReg:
+                               MONO_INST_NEW (cfg, ins, OP_MOVE);
+                               ins->dreg = mono_alloc_ireg (cfg);
+                               ins->sreg1 = in->dreg;
+                               break;
+                       case ArgInDoubleSSEReg:
+                       case ArgInFloatSSEReg:
+                               MONO_INST_NEW (cfg, ins, OP_FMOVE);
+                               ins->dreg = mono_alloc_freg (cfg);
+                               ins->sreg1 = in->dreg;
+                               break;
+                       case ArgOnStack:
+                               if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
+                                       cfg->exception_message = g_strdup ("vtype argument");
+                                       cfg->disable_llvm = TRUE;
+                               } else {
+                                       MONO_INST_NEW (cfg, ins, OP_MOVE);
+                                       ins->dreg = mono_alloc_ireg (cfg);
+                                       ins->sreg1 = in->dreg;
+                               }
+                               break;
+                       default:
+                               cfg->exception_message = g_strdup ("ainfo->storage");
+                               cfg->disable_llvm = TRUE;
+                               return;
+                       }
+
+                       if (!cfg->disable_llvm) {
+                               MONO_ADD_INS (cfg->cbb, ins);
+                               mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, 0, FALSE);
+                       }
+               }
+               return;
+       }
+
+       if (cinfo->need_stack_align) {
+               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
+       }
+
+       /*
+        * Emit all parameters passed in registers in non-reverse order for better readability
+        * and to help the optimization in emit_prolog ().
+        */
        for (i = 0; i < n; ++i) {
                ainfo = cinfo->args + i;
 
-               if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
-                       /* Emit the signature cookie just before the implicit arguments */
-                       emit_sig_cookie (cfg, call, cinfo);
-               }
+               in = call->args [i];
 
-               if (is_virtual && i == 0) {
-                       /* the argument will be attached to the call instruction */
-                       in = call->args [i];
-               } else {
-                       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;
-                       if (!cinfo->stack_usage)
-                               /* Keep the assignments to the arg registers in order if possible */
-                               MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
-                       else
-                               MONO_INST_LIST_ADD (&arg->node, &call->out_args);
+               if (ainfo->storage == ArgInIReg)
+                       add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
+       }
+
+       for (i = n - 1; i >= 0; --i) {
+               ainfo = cinfo->args + i;
+
+               in = call->args [i];
 
-                       if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
+               switch (ainfo->storage) {
+               case ArgInIReg:
+                       /* Already done */
+                       break;
+               case ArgInFloatSSEReg:
+               case ArgInDoubleSSEReg:
+                       add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
+                       break;
+               case ArgOnStack:
+               case ArgValuetypeInReg:
+               case ArgValuetypeAddrInIReg:
+                       if (ainfo->storage == ArgOnStack && call->tail_call) {
+                               MonoInst *call_inst = (MonoInst*)call;
+                               cfg->args [i]->flags |= MONO_INST_VOLATILE;
+                               EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
+                       } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
                                guint32 align;
                                guint32 size;
 
@@ -1370,186 +1695,218 @@ mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call,
                                        size = sizeof (MonoTypedRef);
                                        align = sizeof (gpointer);
                                }
-                               else
-                               if (sig->pinvoke)
-                                       size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
                                else {
-                                       /* 
-                                        * Other backends use mini_type_stack_size (), but that
-                                        * aligns the size to 8, which is larger than the size of
-                                        * the source, leading to reads of invalid memory if the
-                                        * source is at the end of address space.
-                                        */
-                                       size = mono_class_value_size (in->klass, &align);
-                               }
-                               if (ainfo->storage == ArgValuetypeInReg) {
-                                       if (ainfo->pair_storage [1] == ArgNone) {
-                                               MonoInst *load;
-
-                                               /* Simpler case */
-
-                                               MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [0]));
-                                               load->inst_left = in;
-
-                                               add_outarg_reg (cfg, call, arg, ainfo->pair_storage [0], ainfo->pair_regs [0], load);
-                                       }
+                                       if (sig->pinvoke)
+                                               size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
                                        else {
-                                               /* Trees can't be shared so make a copy */
-                                               MonoInst *vtaddr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
-                                               MonoInst *load, *load2, *offset_ins;
-
-                                               /* Reg1 */
-                                               MONO_INST_NEW (cfg, load, CEE_LDIND_I);
-                                               load->ssa_op = MONO_SSA_LOAD;
-                                               load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
-
-                                               NEW_ICONST (cfg, offset_ins, 0);
-                                               MONO_INST_NEW (cfg, load2, CEE_ADD);
-                                               load2->inst_left = load;
-                                               load2->inst_right = offset_ins;
-
-                                               MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [0]));
-                                               load->inst_left = load2;
-
-                                               add_outarg_reg (cfg, call, arg, ainfo->pair_storage [0], ainfo->pair_regs [0], load);
-
-                                               /* Reg2 */
-                                               MONO_INST_NEW (cfg, load, CEE_LDIND_I);
-                                               load->ssa_op = MONO_SSA_LOAD;
-                                               load->inst_i0 = (cfg)->varinfo [vtaddr->inst_c0];
-
-                                               NEW_ICONST (cfg, offset_ins, 8);
-                                               MONO_INST_NEW (cfg, load2, CEE_ADD);
-                                               load2->inst_left = load;
-                                               load2->inst_right = offset_ins;
-
-                                               MONO_INST_NEW (cfg, load, arg_storage_to_ldind (ainfo->pair_storage [1]));
-                                               load->inst_left = load2;
-
-                                               MONO_INST_NEW (cfg, arg, OP_OUTARG);
-                                               arg->cil_code = in->cil_code;
-                                               arg->type = in->type;
-                                               MONO_INST_LIST_ADD (&arg->node, &call->out_args);
-
-                                               add_outarg_reg (cfg, call, arg, ainfo->pair_storage [1], ainfo->pair_regs [1], load);
-
-                                               /* Prepend a copy inst */
-                                               MONO_INST_NEW (cfg, arg, CEE_STIND_I);
-                                               arg->cil_code = in->cil_code;
-                                               arg->ssa_op = MONO_SSA_STORE;
-                                               arg->inst_left = vtaddr;
-                                               arg->inst_right = in;
-                                               arg->type = in->type;
-
-                                               MONO_INST_LIST_ADD (&arg->node, &call->out_args);
+                                               /* 
+                                                * Other backends use mono_type_stack_size (), but that
+                                                * aligns the size to 8, which is larger than the size of
+                                                * the source, leading to reads of invalid memory if the
+                                                * source is at the end of address space.
+                                                */
+                                               size = mono_class_value_size (in->klass, &align);
                                        }
                                }
-                               else if (ainfo->storage == ArgValuetypeAddrInIReg){
+                               g_assert (in->klass);
 
-                                       /* Add a temp variable to the method*/
-                                       MonoInst *load;
-                                       MonoInst *vtaddr = mono_compile_create_var (cfg, &in->klass->byval_arg, OP_LOCAL);
-                                       
-                                       MONO_INST_NEW (cfg, load, OP_LDADDR);
-                                       load->ssa_op = MONO_SSA_LOAD;
-                                       load->inst_left = vtaddr;
-                                       
-                                       if (ainfo->pair_storage [0] == ArgInIReg) {
-                                               /* Inserted after the copy.  Load the address of the temp to the argument regster.*/
-                                               arg->opcode = OP_OUTARG_REG;
-                                               arg->inst_left = load;
-                                               arg->inst_call = call;
-                                               arg->backend.reg3 =  ainfo->pair_regs [0];
-                                       } 
-                                       else {
-                                               /* Inserted after the copy.  Load the address of the temp on the stack.*/
-                                               arg->opcode = OP_OUTARG_VT;
-                                               arg->inst_left = load;
-                                               arg->type = STACK_PTR;
-                                               arg->klass = mono_defaults.int_class;
-                                               arg->backend.is_pinvoke = sig->pinvoke;
-                                               arg->inst_imm = size;
-                                       }
-
-                                       /*Copy the argument to the temp variable.*/
-                                       MONO_INST_NEW (cfg, load, OP_MEMCPY);
-                                       load->backend.memcpy_args = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMemcpyArgs));
-                                       load->backend.memcpy_args->size = mono_class_value_size (in->klass, &align);
-                                       load->backend.memcpy_args->align = align;
-                                       load->inst_left = (cfg)->varinfo [vtaddr->inst_c0];
-                                       load->inst_right = in->inst_i0;
-                                       MONO_INST_LIST_ADD (&load->node, &call->out_args);
-                               }
-                               else {
-                                       arg->opcode = OP_OUTARG_VT;
+                               if (size > 0) {
+                                       MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
+                                       arg->sreg1 = in->dreg;
                                        arg->klass = in->klass;
-                                       arg->backend.is_pinvoke = sig->pinvoke;
-                                       arg->inst_imm = size;
+                                       arg->backend.size = size;
+                                       arg->inst_p0 = call;
+                                       arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
+                                       memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
+
+                                       MONO_ADD_INS (cfg->cbb, arg);
                                }
-                       }
-                       else {
-                               switch (ainfo->storage) {
-                               case ArgInIReg:
-                                       add_outarg_reg (cfg, call, arg, ainfo->storage, ainfo->reg, in);
-                                       break;
-                               case ArgInFloatSSEReg:
-                               case ArgInDoubleSSEReg:
-                                       add_outarg_reg (cfg, call, arg, ainfo->storage, ainfo->reg, in);
-                                       break;
-                               case ArgOnStack:
-                                       arg->opcode = OP_OUTARG;
-                                       if (!sig->params [i - sig->hasthis]->byref) {
-                                               if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4)
-                                                       arg->opcode = OP_OUTARG_R4;
-                                               else
-                                                       if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8)
-                                                               arg->opcode = OP_OUTARG_R8;
+                       } else {
+                               MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
+                               arg->sreg1 = in->dreg;
+                               if (!sig->params [i - sig->hasthis]->byref) {
+                                       if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R4) {
+                                               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
+                                               arg->opcode = OP_STORER4_MEMBASE_REG;
+                                               arg->inst_destbasereg = X86_ESP;
+                                               arg->inst_offset = 0;
+                                       } else if (sig->params [i - sig->hasthis]->type == MONO_TYPE_R8) {
+                                               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 8);
+                                               arg->opcode = OP_STORER8_MEMBASE_REG;
+                                               arg->inst_destbasereg = X86_ESP;
+                                               arg->inst_offset = 0;
                                        }
-                                       break;
-                               default:
-                                       g_assert_not_reached ();
                                }
+                               MONO_ADD_INS (cfg->cbb, arg);
                        }
+                       break;
+               default:
+                       g_assert_not_reached ();
                }
+
+               if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
+                       /* Emit the signature cookie just before the implicit arguments */
+                       emit_sig_cookie (cfg, call, cinfo);
        }
 
        /* Handle the case where there are no implicit arguments */
-       if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
+       if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
                emit_sig_cookie (cfg, call, cinfo);
-       }
 
-       if (cinfo->ret.storage == ArgValuetypeInReg) {
-               /* This is needed by mono_arch_emit_this_vret_args () */
-               if (!cfg->arch.vret_addr_loc) {
-                       cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
-                       /* Prevent it from being register allocated or optimized away */
-                       ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
+       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;
+                               /* 
+                                * Nullify the instruction computing the vret addr to enable 
+                                * future optimizations.
+                                */
+                               if (call->vret_var)
+                                       NULLIFY_INS (call->vret_var);
+                       } else {
+                               if (call->tail_call)
+                                       NOT_IMPLEMENTED;
+                               /*
+                                * The valuetype is in RAX:RDX after the call, need to be copied to
+                                * the stack. Push the address here, so the call instruction can
+                                * access it.
+                                */
+                               if (!cfg->arch.vret_addr_loc) {
+                                       cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
+                                       /* Prevent it from being register allocated or optimized away */
+                                       ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
+                               }
+
+                               MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
+                       }
                }
-       }
+               else {
+                       MONO_INST_NEW (cfg, vtarg, OP_MOVE);
+                       vtarg->sreg1 = call->vret_var->dreg;
+                       vtarg->dreg = mono_alloc_preg (cfg);
+                       MONO_ADD_INS (cfg->cbb, vtarg);
 
-       if (cinfo->need_stack_align) {
-               MONO_INST_NEW (cfg, arg, OP_AMD64_OUTARG_ALIGN_STACK);
-               arg->inst_c0 = 8;
-               MONO_INST_LIST_ADD (&arg->node, &call->out_args);
+                       mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
+               }
        }
 
 #ifdef PLATFORM_WIN32
-       /* Always reserve 32 bytes of stack space on Win64 */
-       MONO_INST_NEW (cfg, arg, OP_AMD64_OUTARG_ALIGN_STACK);
-       arg->inst_c0 = 32;
-       MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
+       if (call->inst.opcode != OP_JMP && OP_TAILCALL != call->inst.opcode) {
+               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, 0x20);
+       }
 #endif
 
        if (cfg->method->save_lmf) {
                MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
-               MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
+               MONO_ADD_INS (cfg->cbb, arg);
        }
 
        call->stack_usage = cinfo->stack_usage;
-       cfg->param_area = MAX (cfg->param_area, call->stack_usage);
-       cfg->flags |= MONO_CFG_HAS_CALLS;
+}
+
+void
+mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
+{
+       MonoInst *arg;
+       MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
+       ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
+       int size = ins->backend.size;
+
+       if (ainfo->storage == ArgValuetypeInReg) {
+               MonoInst *load;
+               int part;
+
+               for (part = 0; part < 2; ++part) {
+                       if (ainfo->pair_storage [part] == ArgNone)
+                               continue;
+
+                       MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
+                       load->inst_basereg = src->dreg;
+                       load->inst_offset = part * sizeof (gpointer);
 
-       return call;
+                       switch (ainfo->pair_storage [part]) {
+                       case ArgInIReg:
+                               load->dreg = mono_alloc_ireg (cfg);
+                               break;
+                       case ArgInDoubleSSEReg:
+                       case ArgInFloatSSEReg:
+                               load->dreg = mono_alloc_freg (cfg);
+                               break;
+                       default:
+                               g_assert_not_reached ();
+                       }
+                       MONO_ADD_INS (cfg->cbb, load);
+
+                       add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
+               }
+       } else if (ainfo->storage == ArgValuetypeAddrInIReg) {
+               MonoInst *vtaddr, *load;
+               vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
+               
+               MONO_INST_NEW (cfg, load, OP_LDADDR);
+               load->inst_p0 = vtaddr;
+               vtaddr->flags |= MONO_INST_INDIRECT;
+               load->type = STACK_MP;
+               load->klass = vtaddr->klass;
+               load->dreg = mono_alloc_ireg (cfg);
+               MONO_ADD_INS (cfg->cbb, load);
+               mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
+
+               if (ainfo->pair_storage [0] == ArgInIReg) {
+                       MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
+                       arg->dreg = mono_alloc_ireg (cfg);
+                       arg->sreg1 = load->dreg;
+                       arg->inst_imm = 0;
+                       MONO_ADD_INS (cfg->cbb, arg);
+                       mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
+               } else {
+                       MONO_INST_NEW (cfg, arg, OP_X86_PUSH);
+                       arg->sreg1 = load->dreg;
+                       MONO_ADD_INS (cfg->cbb, arg);
+               }
+       } else {
+               if (size == 8) {
+                       /* Can't use this for < 8 since it does an 8 byte memory load */
+                       MONO_INST_NEW (cfg, arg, OP_X86_PUSH_MEMBASE);
+                       arg->inst_basereg = src->dreg;
+                       arg->inst_offset = 0;
+                       MONO_ADD_INS (cfg->cbb, arg);
+               } else if (size <= 40) {
+                       MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SUB_IMM, X86_ESP, X86_ESP, ALIGN_TO (size, 8));
+                       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;
+                       arg->inst_offset = 0;
+                       arg->inst_imm = size;
+                       MONO_ADD_INS (cfg->cbb, arg);
+               }
+       }
+}
+
+void
+mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
+{
+       MonoType *ret = mini_type_get_underlying_type (NULL, mono_method_signature (method)->ret);
+
+       if (!ret->byref) {
+               if (ret->type == MONO_TYPE_R4) {
+                       MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
+                       return;
+               } else if (ret->type == MONO_TYPE_R8) {
+                       MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
+                       return;
+               }
+       }
+                       
+       MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
 }
 
 #define EMIT_COND_BRANCH(ins,cond,sign) \
@@ -1606,6 +1963,8 @@ if (ins->flags & MONO_INST_BRLABEL) { \
 static guint8*
 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
 {
+       gboolean no_patch = FALSE;
+
        /* 
         * FIXME: Add support for thunks
         */
@@ -1623,7 +1982,7 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
                        near_call = TRUE;
 
                        if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
-                               if (((MonoMethod*)data)->klass->image->assembly->aot_module)
+                               if (((MonoMethod*)data)->klass->image->aot_module)
                                        /* The callee might be an AOT method */
                                        near_call = FALSE;
                                if (((MonoMethod*)data)->dynamic)
@@ -1645,9 +2004,13 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
                        }
                }
                else {
-                       if (mono_find_class_init_trampoline_by_addr (data))
+                       if (cfg->abs_patches && g_hash_table_lookup (cfg->abs_patches, data)) {
+                               /* 
+                                * This is not really an optimization, but required because the
+                                * generic class init trampolines use R11 to pass the vtable.
+                                */
                                near_call = TRUE;
-                       else {
+                       else {
                                MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
                                if (info) {
                                        if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && 
@@ -1655,6 +2018,7 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
                                                /* A call to the wrapped function */
                                                if ((((guint64)data) >> 32) == 0)
                                                        near_call = TRUE;
+                                               no_patch = TRUE;
                                        }
                                        else if (info->func == info->wrapper) {
                                                /* No wrapper */
@@ -1667,8 +2031,10 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
                                                        near_call = TRUE;
                                        }
                                }
-                               else if ((((guint64)data) >> 32) == 0)
+                               else if ((((guint64)data) >> 32) == 0) {
                                        near_call = TRUE;
+                                       no_patch = TRUE;
+                               }
                        }
                }
 
@@ -1676,8 +2042,10 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
                        /* These methods are allocated using malloc */
                        near_call = FALSE;
 
-               if (cfg->compile_aot)
+               if (cfg->compile_aot) {
                        near_call = TRUE;
+                       no_patch = TRUE;
+               }
 
 #ifdef MONO_ARCH_NOMAP32BIT
                near_call = FALSE;
@@ -1689,7 +2057,7 @@ emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointe
                         * not span cache lines. This is required for code patching to work on SMP
                         * systems.
                         */
-                       if (((guint32)(code + 1 - cfg->native_code) % 4) != 0)
+                       if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0)
                                amd64_padding (code, 4 - ((guint32)(code + 1 - cfg->native_code) % 4));
                        mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
                        amd64_call_code (code, 0);
@@ -1748,7 +2116,7 @@ mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
        MonoInst *ins, *n;
 
        MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
-               MonoInst *last_ins = mono_inst_list_prev (&ins->node, &bb->ins_list);
+               MonoInst *last_ins = ins->prev;
 
                switch (ins->opcode) {
                case OP_ADD_IMM:
@@ -1775,8 +2143,7 @@ mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
                                 * propagation). These instruction sequences are very common
                                 * in the initlocals bblock.
                                 */
-                               for (ins2 = mono_inst_list_next (&ins->node, &bb->ins_list); ins2;
-                                               ins2 = mono_inst_list_next (&ins2->node, &bb->ins_list)) {
+                               for (ins2 = ins->next; ins2; ins2 = ins2->next) {
                                        if (((ins2->opcode == OP_STORE_MEMBASE_IMM) || (ins2->opcode == OP_STOREI4_MEMBASE_IMM) || (ins2->opcode == OP_STOREI8_MEMBASE_IMM) || (ins2->opcode == OP_STORE_MEMBASE_IMM)) && (ins2->inst_imm == 0)) {
                                                ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
                                                ins2->sreg1 = ins->dreg;
@@ -1841,13 +2208,9 @@ mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
                switch (ins->opcode) {
                case OP_ICONST:
                case OP_I8CONST: {
-                       MonoInst *next;
-
                        /* reg = 0 -> XOR (reg, reg) */
                        /* XOR sets cflags on x86, so we cant do it always */
-                       next = mono_inst_list_next (&ins->node, &bb->ins_list);
-                       if (ins->inst_c0 == 0 && (!next ||
-                                       (next && INST_IGNORES_CFLAGS (next->opcode)))) {
+                       if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
                                ins->opcode = OP_LXOR;
                                ins->sreg1 = ins->dreg;
                                ins->sreg2 = ins->dreg;
@@ -1875,8 +2238,7 @@ mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
                                 * propagation). These instruction sequences are very common
                                 * in the initlocals bblock.
                                 */
-                               for (ins2 = mono_inst_list_next (&ins->node, &bb->ins_list); ins2;
-                                               ins2 = mono_inst_list_next (&ins2->node, &bb->ins_list)) {
+                               for (ins2 = ins->next; ins2; ins2 = ins2->next) {
                                        if (((ins2->opcode == OP_STORE_MEMBASE_IMM) || (ins2->opcode == OP_STOREI4_MEMBASE_IMM) || (ins2->opcode == OP_STOREI8_MEMBASE_IMM) || (ins2->opcode == OP_STORE_MEMBASE_IMM)) && (ins2->inst_imm == 0)) {
                                                ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
                                                ins2->sreg1 = ins->dreg;
@@ -1908,7 +2270,7 @@ mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
 #define NEW_INS(cfg,ins,dest,op) do {  \
                MONO_INST_NEW ((cfg), (dest), (op)); \
         (dest)->cil_code = (ins)->cil_code; \
-               MONO_INST_LIST_ADD_TAIL (&(dest)->node, &(ins)->node); \
+        mono_bblock_insert_before_ins (bb, ins, (dest)); \
        } while (0)
 
 /*
@@ -1922,9 +2284,6 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
 {
        MonoInst *ins, *n, *temp;
 
-       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
@@ -1935,17 +2294,21 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_DIV_IMM:
                case OP_REM_IMM:
                case OP_IDIV_IMM:
-               case OP_IREM_IMM:
                case OP_IDIV_UN_IMM:
                case OP_IREM_UN_IMM:
-                       mono_decompose_op_imm (cfg, ins);
+                       mono_decompose_op_imm (cfg, bb, ins);
+                       break;
+               case OP_IREM_IMM:
+                       /* Keep the opcode if we can implement it efficiently */
+                       if (!((ins->inst_imm > 0) && (mono_is_power_of_two (ins->inst_imm) != -1)))
+                               mono_decompose_op_imm (cfg, bb, ins);
                        break;
                case OP_COMPARE_IMM:
                case OP_LCOMPARE_IMM:
                        if (!amd64_is_imm32 (ins->inst_imm)) {
                                NEW_INS (cfg, ins, temp, OP_I8CONST);
                                temp->inst_c0 = ins->inst_imm;
-                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                               temp->dreg = mono_alloc_ireg (cfg);
                                ins->opcode = OP_COMPARE;
                                ins->sreg2 = temp->dreg;
                        }
@@ -1955,7 +2318,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        if (!amd64_is_imm32 (ins->inst_offset)) {
                                NEW_INS (cfg, ins, temp, OP_I8CONST);
                                temp->inst_c0 = ins->inst_offset;
-                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                               temp->dreg = mono_alloc_ireg (cfg);
                                ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
                                ins->inst_indexreg = temp->dreg;
                        }
@@ -1965,7 +2328,7 @@ mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
                        if (!amd64_is_imm32 (ins->inst_imm)) {
                                NEW_INS (cfg, ins, temp, OP_I8CONST);
                                temp->inst_c0 = ins->inst_imm;
-                               temp->dreg = mono_regstate_next_int (cfg->rs);
+                               temp->dreg = mono_alloc_ireg (cfg);
                                ins->opcode = OP_STOREI8_MEMBASE_REG;
                                ins->sreg1 = temp->dreg;
                        }
@@ -1975,7 +2338,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 
@@ -2130,6 +2493,9 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
        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) {
                        MonoInst *loc = cfg->arch.vret_addr_loc;
@@ -2163,110 +2529,34 @@ emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
 }
 
 /*
- * emit_tls_get:
+ * mono_amd64_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_amd64_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_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
 {
+#ifdef PLATFORM_WIN32
+       g_assert (tls_offset < 64);
+       x86_prefix (code, X86_GS_PREFIX);
+       amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
+#else
        if (optimize_for_xen) {
                x86_prefix (code, X86_FS_PREFIX);
                amd64_mov_reg_mem (code, dreg, 0, 8);
-               amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
-       } else {
-               x86_prefix (code, X86_FS_PREFIX);
-               amd64_mov_reg_mem (code, dreg, tls_offset, 8);
-       }
-       return code;
-}
-
-/*
- * emit_load_volatile_arguments:
- *
- *  Load volatile arguments from the stack to the original input registers.
- * Required before a tail call.
- */
-static guint8*
-emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
-{
-       MonoMethod *method = cfg->method;
-       MonoMethodSignature *sig;
-       MonoInst *ins;
-       CallInfo *cinfo;
-       guint32 i, quad;
-
-       /* FIXME: Generate intermediate code instead */
-
-       sig = mono_method_signature (method);
-
-       cinfo = cfg->arch.cinfo;
-       
-       /* This is the opposite of the code in emit_prolog */
-       if (sig->ret->type != MONO_TYPE_VOID) {
-               if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
-                       amd64_mov_reg_membase (code, cinfo->ret.reg, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, 8);
-       }
-
-       for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
-               ArgInfo *ainfo = cinfo->args + i;
-               MonoType *arg_type;
-               ins = cfg->args [i];
-
-               if (sig->hasthis && (i == 0))
-                       arg_type = &mono_defaults.object_class->byval_arg;
-               else
-                       arg_type = sig->params [i - sig->hasthis];
-
-               if (ins->opcode != OP_REGVAR) {
-                       switch (ainfo->storage) {
-                       case ArgInIReg: {
-                               guint32 size = 8;
-
-                               /* FIXME: I1 etc */
-                               amd64_mov_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset, size);
-                               break;
-                       }
-                       case ArgInFloatSSEReg:
-                               amd64_movss_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
-                               break;
-                       case ArgInDoubleSSEReg:
-                               amd64_movsd_reg_membase (code, ainfo->reg, ins->inst_basereg, ins->inst_offset);
-                               break;
-                       case ArgValuetypeInReg:
-                               for (quad = 0; quad < 2; quad ++) {
-                                       switch (ainfo->pair_storage [quad]) {
-                                       case ArgInIReg:
-                                               amd64_mov_reg_membase (code, ainfo->pair_regs [quad], ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), sizeof (gpointer));
-                                               break;
-                                       case ArgInFloatSSEReg:
-                                       case ArgInDoubleSSEReg:
-                                               g_assert_not_reached ();
-                                               break;
-                                       case ArgNone:
-                                               break;
-                                       default:
-                                               g_assert_not_reached ();
-                                       }
-                               }
-                               break;
-                       default:
-                               break;
-                       }
-               }
-               else {
-                       g_assert (ainfo->storage == ArgInIReg);
-
-                       amd64_mov_reg_reg (code, ainfo->reg, ins->dreg, 8);
-               }
+               amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
+       } else {
+               x86_prefix (code, X86_FS_PREFIX);
+               amd64_mov_reg_mem (code, dreg, tls_offset, 8);
        }
-
+#endif
        return code;
 }
 
@@ -2289,6 +2579,8 @@ amd64_pop_reg (code, AMD64_RAX);
 #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)
 {
@@ -2296,6 +2588,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
        MonoCallInst *call;
        guint offset;
        guint8 *code = cfg->native_code + cfg->code_len;
+       MonoInst *last_ins = NULL;
        guint last_offset = 0;
        int max_len, cpos;
 
@@ -2331,6 +2624,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
        mono_debug_open_block (cfg, bb, offset);
 
+    if (mono_break_at_bb_method && mono_method_desc_full_match (mono_break_at_bb_method, cfg->method) && bb->block_num == mono_break_at_bb_bb_num)
+               x86_breakpoint (code);
+
        MONO_BB_FOR_EACH_INS (bb, ins) {
                offset = code - cfg->native_code;
 
@@ -2398,8 +2694,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
                        break;
                case OP_LOADU4_MEM:
-                       amd64_mov_reg_imm (code, ins->dreg, ins->inst_p0);
-                       amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
+                       // FIXME: Decompose this earlier
+                       if (amd64_is_imm32 (ins->inst_imm))
+                               amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
+                       else {
+                               amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
+                               amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
+                       }
                        break;
                case OP_LOADU1_MEM:
                        amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
@@ -2421,13 +2722,15 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
                        break;
                case OP_LOADU1_MEMBASE:
-                       amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
+                       /* The cpu zero extends the result into 64 bits */
+                       amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
                        break;
                case OP_LOADI1_MEMBASE:
                        amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
                        break;
                case OP_LOADU2_MEMBASE:
-                       amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
+                       /* The cpu zero extends the result into 64 bits */
+                       amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
                        break;
                case OP_LOADI2_MEMBASE:
                        amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
@@ -2543,6 +2846,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_dec_reg_size (code, ins->dreg, 4);
                        break;
                case OP_X86_MUL_REG_MEMBASE:
+               case OP_X86_MUL_MEMBASE_REG:
                        amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
                        break;
                case OP_AMD64_ICOMPARE_MEMBASE_REG:
@@ -2624,6 +2928,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_BREAK:
                        amd64_breakpoint (code);
                        break;
+               case OP_RELAXED_NOP:
+                       x86_prefix (code, X86_REP_PREFIX);
+                       x86_nop (code);
+                       break;
+               case OP_HARD_NOP:
+                       x86_nop (code);
+                       break;
                case OP_NOP:
                case OP_DUMMY_USE:
                case OP_DUMMY_STORE:
@@ -2781,6 +3092,32 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
                        }
                        break;
+               case OP_IREM_IMM: {
+                       int power = mono_is_power_of_two (ins->inst_imm);
+
+                       g_assert (ins->sreg1 == X86_EAX);
+                       g_assert (ins->dreg == X86_EAX);
+                       g_assert (power >= 0);
+
+                       if (power == 0) {
+                               amd64_mov_reg_imm (code, ins->dreg, 0);
+                               break;
+                       }
+
+                       /* Based on gcc code */
+
+                       /* Add compensation for negative dividents */
+                       amd64_mov_reg_reg_size (code, AMD64_RDX, AMD64_RAX, 4);
+                       if (power > 1)
+                               amd64_shift_reg_imm_size (code, X86_SAR, AMD64_RDX, 31, 4);
+                       amd64_shift_reg_imm_size (code, X86_SHR, AMD64_RDX, 32 - power, 4);
+                       amd64_alu_reg_reg_size (code, X86_ADD, AMD64_RAX, AMD64_RDX, 4);
+                       /* Compute remainder */
+                       amd64_alu_reg_imm_size (code, X86_AND, AMD64_RAX, (1 << power) - 1, 4);
+                       /* Remove compensation */
+                       amd64_alu_reg_reg_size (code, X86_SUB, AMD64_RAX, AMD64_RDX, 4);
+                       break;
+               }
                case OP_LMUL_OVF:
                        amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
                        EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
@@ -2985,6 +3322,31 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
                        break;
 
+               case OP_CMOV_IEQ:
+               case OP_CMOV_IGE:
+               case OP_CMOV_IGT:
+               case OP_CMOV_ILE:
+               case OP_CMOV_ILT:
+               case OP_CMOV_INE_UN:
+               case OP_CMOV_IGE_UN:
+               case OP_CMOV_IGT_UN:
+               case OP_CMOV_ILE_UN:
+               case OP_CMOV_ILT_UN:
+               case OP_CMOV_LEQ:
+               case OP_CMOV_LGE:
+               case OP_CMOV_LGT:
+               case OP_CMOV_LLE:
+               case OP_CMOV_LLT:
+               case OP_CMOV_LNE_UN:
+               case OP_CMOV_LGE_UN:
+               case OP_CMOV_LGT_UN:
+               case OP_CMOV_LLE_UN:
+               case OP_CMOV_LLT_UN:
+                       g_assert (ins->dreg == ins->sreg1);
+                       /* This needs to operate on 64 bit values */
+                       amd64_cmov_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, ins->sreg2);
+                       break;
+
                case OP_LNOT:
                        amd64_not_reg (code, ins->sreg1);
                        break;
@@ -3003,6 +3365,10 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
                        amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, 8);
                        break;
+               case OP_JUMP_TABLE:
+                       mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
+                       amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
+                       break;
                case OP_MOVE:
                        amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof (gpointer));
                        break;
@@ -3015,7 +3381,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
                        break;
                }
-               case OP_JMP: {
+               case OP_TAILCALL: {
                        /*
                         * Note: this 'frame destruction' logic is useful for tail calls, too.
                         * Keep in sync with the code in emit_epilog.
@@ -3028,8 +3394,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
 
                        g_assert (!cfg->method->save_lmf);
 
-                       code = emit_load_volatile_arguments (cfg, code);
-
                        if (cfg->arch.omit_fp) {
                                guint32 save_offset = 0;
                                /* Pop callee-saved registers */
@@ -3075,11 +3439,12 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, 8);
                        break;
                }
+               case OP_CALL:
                case OP_FCALL:
                case OP_LCALL:
                case OP_VCALL:
+               case OP_VCALL2:
                case OP_VOIDCALL:
-               case OP_CALL:
                        call = (MonoCallInst*)ins;
                        /*
                         * The AMD64 ABI forces callers to know about varargs.
@@ -3114,6 +3479,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_FCALL_REG:
                case OP_LCALL_REG:
                case OP_VCALL_REG:
+               case OP_VCALL2_REG:
                case OP_VOIDCALL_REG:
                case OP_CALL_REG:
                        call = (MonoCallInst*)ins;
@@ -3132,7 +3498,27 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                        ins->sreg1 = AMD64_R11;
                                }
                                amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
+                       } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
+                               /* 
+                                * Since the unmanaged calling convention doesn't contain a 
+                                * 'vararg' entry, we have to treat every pinvoke call as a
+                                * potential vararg call.
+                                */
+                               guint32 nregs, i;
+                               nregs = 0;
+                               for (i = 0; i < AMD64_XMM_NREG; ++i)
+                                       if (call->used_fregs & (1 << i))
+                                               nregs ++;
+                               if (ins->sreg1 == AMD64_RAX) {
+                                       amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
+                                       ins->sreg1 = AMD64_R11;
+                               }
+                               if (!nregs)
+                                       amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
+                               else
+                                       amd64_mov_reg_imm (code, AMD64_RAX, nregs);
                        }
+
                        amd64_call_reg (code, ins->sreg1);
                        if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
                                amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
@@ -3141,6 +3527,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_FCALL_MEMBASE:
                case OP_LCALL_MEMBASE:
                case OP_VCALL_MEMBASE:
+               case OP_VCALL2_MEMBASE:
                case OP_VOIDCALL_MEMBASE:
                case OP_CALL_MEMBASE:
                        call = (MonoCallInst*)ins;
@@ -3154,6 +3541,13 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                ins->sreg1 = AMD64_RAX;
                        }
 
+                       /* 
+                        * Emit a few nops to simplify get_vcall_slot ().
+                        */
+                       amd64_nop (code);
+                       amd64_nop (code);
+                       amd64_nop (code);
+
                        amd64_call_membase (code, ins->sreg1, ins->inst_offset);
                        if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
                                amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, call->stack_usage);
@@ -3162,7 +3556,6 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_AMD64_SAVE_SP_TO_LMF:
                        amd64_mov_membase_reg (code, cfg->frame_reg, cfg->arch.lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
                        break;
-               case OP_OUTARG:
                case OP_X86_PUSH:
                        amd64_push_reg (code, ins->sreg1);
                        break;
@@ -3173,8 +3566,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                case OP_X86_PUSH_MEMBASE:
                        amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
                        break;
-               case OP_X86_PUSH_OBJ: 
-                       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ins->inst_imm);
+               case OP_X86_PUSH_OBJ: {
+                       int size = ALIGN_TO (ins->inst_imm, 8);
+                       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
                        amd64_push_reg (code, AMD64_RDI);
                        amd64_push_reg (code, AMD64_RSI);
                        amd64_push_reg (code, AMD64_RCX);
@@ -3182,8 +3576,8 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
                        else
                                amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
-                       amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, 3 * 8);
-                       amd64_mov_reg_imm (code, AMD64_RCX, (ins->inst_imm >> 3));
+                       amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
+                       amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
                        amd64_cld (code);
                        amd64_prefix (code, X86_REP_PREFIX);
                        amd64_movsd (code);
@@ -3191,6 +3585,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_pop_reg (code, AMD64_RSI);
                        amd64_pop_reg (code, AMD64_RDI);
                        break;
+               }
                case OP_X86_LEA:
                        amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
                        break;
@@ -3212,12 +3607,22 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
 
                        if (ins->flags & MONO_INST_INIT) {
-                               /* FIXME: Optimize this */
-                               amd64_mov_reg_imm (code, ins->dreg, size);
-                               ins->sreg1 = ins->dreg;
+                               if (size < 64) {
+                                       int i;
 
-                               code = mono_emit_stack_alloc (code, ins);
-                               amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
+                                       amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
+                                       amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
+
+                                       for (i = 0; i < size; i += 8)
+                                               amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
+                                       amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);                                      
+                               } else {
+                                       amd64_mov_reg_imm (code, ins->dreg, size);
+                                       ins->sreg1 = ins->dreg;
+
+                                       code = mono_emit_stack_alloc (code, ins);
+                                       amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
+                               }
                        } else {
                                amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
                                amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
@@ -3267,6 +3672,9 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        ins->inst_c0 = code - cfg->native_code;
                        break;
                case OP_BR:
+                       //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
+                       //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
+                       //break;
                        if (ins->flags & MONO_INST_BRLABEL) {
                                if (ins->inst_i0->inst_c0) {
                                        amd64_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
@@ -3754,7 +4162,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
                        break;
                case OP_TLS_GET: {
-                       code = emit_tls_get (code, ins->dreg, ins->inst_offset);
+                       code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
                        break;
                }
                case OP_MEMORY_BARRIER: {
@@ -3800,8 +4208,7 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                        break;
                }
                case OP_ATOMIC_EXCHANGE_I4:
-               case OP_ATOMIC_EXCHANGE_I8:
-               case OP_ATOMIC_CAS_IMM_I4: {
+               case OP_ATOMIC_EXCHANGE_I8: {
                        guchar *br[2];
                        int sreg2 = ins->sreg2;
                        int breg = ins->inst_basereg;
@@ -3848,28 +4255,54 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
                                }
                        }
 
-                       if (ins->opcode == OP_ATOMIC_CAS_IMM_I4) {
-                               if (ins->backend.data == NULL)
-                                       amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
-                               else
-                                       amd64_mov_reg_imm (code, AMD64_RAX, ins->backend.data);
-
-                               amd64_prefix (code, X86_LOCK_PREFIX);
-                               amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
-                       } else {
-                               amd64_mov_reg_membase (code, AMD64_RAX, breg, ins->inst_offset, size);
+                       amd64_mov_reg_membase (code, AMD64_RAX, breg, ins->inst_offset, size);
 
-                               br [0] = code; amd64_prefix (code, X86_LOCK_PREFIX);
-                               amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
-                               br [1] = code; amd64_branch8 (code, X86_CC_NE, -1, FALSE);
-                               amd64_patch (br [1], br [0]);
-                       }
+                       br [0] = code; amd64_prefix (code, X86_LOCK_PREFIX);
+                       amd64_cmpxchg_membase_reg_size (code, breg, ins->inst_offset, sreg2, size);
+                       br [1] = code; amd64_branch8 (code, X86_CC_NE, -1, FALSE);
+                       amd64_patch (br [1], br [0]);
 
                        if (rdx_pushed)
                                amd64_pop_reg (code, AMD64_RDX);
 
                        break;
                }
+               case OP_ATOMIC_CAS_I4:
+               case OP_ATOMIC_CAS_I8: {
+                       guint32 size;
+
+                       if (ins->opcode == OP_ATOMIC_CAS_I8)
+                               size = 8;
+                       else
+                               size = 4;
+
+                       /* 
+                        * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
+                        * an explanation of how this works.
+                        */
+                       g_assert (ins->sreg3 == AMD64_RAX);
+                       g_assert (ins->sreg1 != AMD64_RAX);
+                       g_assert (ins->sreg1 != ins->sreg2);
+
+                       amd64_prefix (code, X86_LOCK_PREFIX);
+                       amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
+
+                       if (ins->dreg != AMD64_RAX)
+                               amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
+                       break;
+               }
+               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 in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
                        g_assert_not_reached ();
@@ -3883,15 +4316,20 @@ mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
               
                cpos += max_len;
 
+               last_ins = ins;
                last_offset = offset;
        }
 
        cfg->code_len = code - cfg->native_code;
 }
 
+#endif /* DISABLE_JIT */
+
 void
 mono_arch_register_lowlevel_calls (void)
 {
+       /* The signature doesn't matter */
+       mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
 }
 
 void
@@ -3997,7 +4435,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        MonoBasicBlock *bb;
        MonoMethodSignature *sig;
        MonoInst *ins;
-       int alloc_size, pos, max_offset, i, quad, max_epilog_size;
+       int alloc_size, pos, max_offset, i, cfa_offset, quad, max_epilog_size;
        guint8 *code;
        CallInfo *cinfo;
        gint32 lmf_offset = cfg->arch.lmf_offset;
@@ -4014,6 +4452,9 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        /* Amount of stack space allocated by register saving code */
        pos = 0;
 
+       /* Offset between RSP and the CFA */
+       cfa_offset = 0;
+
        /* 
         * The prolog consists of the following parts:
         * FP present:
@@ -4029,21 +4470,41 @@ mono_arch_emit_prolog (MonoCompile *cfg)
         * - save callee saved regs using moves
         */
 
+       // CFA = sp + 8
+       cfa_offset = 8;
+       mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
+       // IP saved at CFA - 8
+       mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
        async_exc_point (code);
 
        if (!cfg->arch.omit_fp) {
                amd64_push_reg (code, AMD64_RBP);
+               cfa_offset += 8;
+               mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
+               mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
                async_exc_point (code);
+#ifdef PLATFORM_WIN32
+               mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
+#endif
+               
                amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof (gpointer));
+               mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
                async_exc_point (code);
+#ifdef PLATFORM_WIN32
+               mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
+#endif
        }
 
        /* Save callee saved registers */
        if (!cfg->arch.omit_fp && !method->save_lmf) {
+               int offset = cfa_offset;
+
                for (i = 0; i < AMD64_NREG; ++i)
                        if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
                                amd64_push_reg (code, i);
                                pos += sizeof (gpointer);
+                               offset += 8;
+                               mono_emit_unwind_op_offset (cfg, code, i, - offset);
                                async_exc_point (code);
                        }
        }
@@ -4072,17 +4533,38 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                guint32 remaining_size = alloc_size;
                while (remaining_size >= 0x1000) {
                        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
+                       if (cfg->arch.omit_fp) {
+                               cfa_offset += 0x1000;
+                               mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
+                       }
                        async_exc_point (code);
+#ifdef PLATFORM_WIN32
+                       if (cfg->arch.omit_fp) 
+                               mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
+#endif
+
                        amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
                        remaining_size -= 0x1000;
                }
                if (remaining_size) {
                        amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
-                       async_exc_point (code);
+                       if (cfg->arch.omit_fp) {
+                               cfa_offset += remaining_size;
+                               mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
+                               async_exc_point (code);
+                       }
+#ifdef PLATFORM_WIN32
+                       if (cfg->arch.omit_fp) 
+                               mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
+#endif
                }
 #else
                amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
-               async_exc_point (code);
+               if (cfg->arch.omit_fp) {
+                       cfa_offset += alloc_size;
+                       mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
+                       async_exc_point (code);
+               }
 #endif
        }
 
@@ -4105,12 +4587,31 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                /* sp is saved right before calls */
                /* Skip method (only needed for trampoline LMF frames) */
                /* Save callee saved regs */
-               amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbx), AMD64_RBX, 8);
-               amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
-               amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r12), AMD64_R12, 8);
-               amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r13), AMD64_R13, 8);
-               amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r14), AMD64_R14, 8);
-               amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), AMD64_R15, 8);
+               for (i = 0; i < MONO_MAX_IREGS; ++i) {
+                       int offset;
+
+                       switch (i) {
+                       case AMD64_RBX: offset = G_STRUCT_OFFSET (MonoLMF, rbx); break;
+                       case AMD64_RBP: offset = G_STRUCT_OFFSET (MonoLMF, rbp); break;
+                       case AMD64_R12: offset = G_STRUCT_OFFSET (MonoLMF, r12); break;
+                       case AMD64_R13: offset = G_STRUCT_OFFSET (MonoLMF, r13); break;
+                       case AMD64_R14: offset = G_STRUCT_OFFSET (MonoLMF, r14); break;
+                       case AMD64_R15: offset = G_STRUCT_OFFSET (MonoLMF, r15); break;
+#ifdef PLATFORM_WIN32
+                       case AMD64_RDI: offset = G_STRUCT_OFFSET (MonoLMF, rdi); break;
+                       case AMD64_RSI: offset = G_STRUCT_OFFSET (MonoLMF, rsi); break;
+#endif
+                       default:
+                               offset = -1;
+                               break;
+                       }
+
+                       if (offset != -1) {
+                               amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + offset, i, 8);
+                               if (cfg->arch.omit_fp || (i != AMD64_RBP))
+                                       mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - (lmf_offset + offset)));
+                       }
+               }
        }
 
        /* Save callee saved registers */
@@ -4123,6 +4624,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                for (i = 0; i < AMD64_NREG; ++i)
                        if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
                                amd64_mov_membase_reg (code, AMD64_RSP, save_area_offset, i, 8);
+                               mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
                                save_area_offset += 8;
                                async_exc_point (code);
                        }
@@ -4141,6 +4643,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
        max_epilog_size = get_max_epilog_size (cfg);
        if (cfg->opt & MONO_OPT_BRANCH) {
                for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+                       MonoInst *ins;
                        bb->max_offset = max_offset;
 
                        if (cfg->prof_options & MONO_PROFILE_COVERAGE)
@@ -4192,6 +4695,38 @@ mono_arch_emit_prolog (MonoCompile *cfg)
 
                stack_offset = ainfo->offset + ARGS_OFFSET;
 
+               if (cfg->globalra) {
+                       /* All the other moves are done by the register allocator */
+                       switch (ainfo->storage) {
+                       case ArgInFloatSSEReg:
+                               amd64_sse_cvtss2sd_reg_reg (code, ainfo->reg, ainfo->reg);
+                               break;
+                       case ArgValuetypeInReg:
+                               for (quad = 0; quad < 2; quad ++) {
+                                       switch (ainfo->pair_storage [quad]) {
+                                       case ArgInIReg:
+                                               amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad], sizeof (gpointer));
+                                               break;
+                                       case ArgInFloatSSEReg:
+                                               amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
+                                               break;
+                                       case ArgInDoubleSSEReg:
+                                               amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof (gpointer)), ainfo->pair_regs [quad]);
+                                               break;
+                                       case ArgNone:
+                                               break;
+                                       default:
+                                               g_assert_not_reached ();
+                                       }
+                               }
+                               break;
+                       default:
+                               break;
+                       }
+
+                       continue;
+               }
+
                /* Save volatile arguments to the stack */
                if (ins->opcode != OP_REGVAR) {
                        switch (ainfo->storage) {
@@ -4237,6 +4772,10 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                                        }
                                }
                                break;
+                       case ArgValuetypeAddrInIReg:
+                               if (ainfo->pair_storage [0] == ArgInIReg)
+                                       amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0],  sizeof (gpointer));
+                               break;
                        default:
                                break;
                        }
@@ -4268,7 +4807,7 @@ 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, AMD64_RAX, appdomain_tls_offset);
+                       code = mono_amd64_emit_tls_get (code, AMD64_RAX, appdomain_tls_offset);
                        if ((domain >> 32) == 0)
                                amd64_mov_reg_imm_size (code, AMD64_ARG_REG1, domain, 4);
                        else
@@ -4276,7 +4815,7 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        amd64_alu_reg_reg (code, X86_CMP, AMD64_RAX, AMD64_ARG_REG1);
                        no_domain_branch = code;
                        x86_branch8 (code, X86_CC_NE, 0, 0);
-                       code = emit_tls_get ( code, AMD64_RAX, lmf_addr_tls_offset);
+                       code = mono_amd64_emit_tls_get ( code, AMD64_RAX, lmf_addr_tls_offset);
                        amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
                        buf = code;
                        x86_branch8 (code, X86_CC_NE, 0, 0);
@@ -4284,6 +4823,11 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, 
                                          (gpointer)"mono_jit_thread_attach", TRUE);
                        amd64_patch (buf, code);
+#ifdef PLATFORM_WIN32
+                       /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
+                       /* FIXME: Add a separate key for LMF to avoid this */
+                       amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
+#endif
                } else {
                        g_assert (!cfg->compile_aot);
                        if ((domain >> 32) == 0)
@@ -4319,7 +4863,12 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                } else {
                        if (lmf_addr_tls_offset != -1) {
                                /* Load lmf quicky using the FS register */
-                               code = emit_tls_get (code, AMD64_RAX, lmf_addr_tls_offset);
+                               code = mono_amd64_emit_tls_get (code, AMD64_RAX, lmf_addr_tls_offset);
+#ifdef PLATFORM_WIN32
+                               /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
+                               /* FIXME: Add a separate key for LMF to avoid this */
+                               amd64_alu_reg_imm (code, X86_ADD, AMD64_RAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
+#endif
                        }
                        else {
                                /* 
@@ -4360,10 +4909,10 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                MonoBasicBlock *first_bb = cfg->bb_entry;
                MonoInst *next;
 
-               next = mono_inst_list_first (&first_bb->ins_list);
+               next = mono_bb_first_ins (first_bb);
                if (!next && first_bb->next_bb) {
                        first_bb = first_bb->next_bb;
-                       next = mono_inst_list_first (&first_bb->ins_list);
+                       next = mono_bb_first_ins (first_bb);
                }
 
                if (first_bb->in_count > 1)
@@ -4409,7 +4958,8 @@ mono_arch_emit_prolog (MonoCompile *cfg)
                        }
 
                        if (match) {
-                               next = mono_inst_list_next (&next->node, &first_bb->ins_list);
+                               next = next->next;
+                               //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
                                if (!next)
                                        break;
                        }
@@ -4450,6 +5000,23 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        pos = 0;
        
        if (method->save_lmf) {
+               /* check if we need to restore protection of the stack after a stack overflow */
+               if (mono_get_jit_tls_offset () != -1) {
+                       guint8 *patch;
+                       code = mono_amd64_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
+                        */
+                       amd64_mov_reg_membase (code, X86_ECX, X86_ECX, G_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
+                       x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
+                       patch = code;
+                       x86_branch8 (code, X86_CC_Z, 0, FALSE);
+                       /* note that the call trampoline will preserve eax/edx */
+                       x86_call_reg (code, X86_ECX);
+                       x86_patch (patch, code);
+               } else {
+                       /* FIXME: maybe save the jit tls in the prolog */
+               }
                if ((lmf_tls_offset != -1) && !optimize_for_xen) {
                        /*
                         * Optimized version which uses the mono_lmf TLS variable instead of indirection
@@ -4485,6 +5052,14 @@ mono_arch_emit_epilog (MonoCompile *cfg)
                if (cfg->used_int_regs & (1 << AMD64_R15)) {
                        amd64_mov_reg_membase (code, AMD64_R15, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, r15), 8);
                }
+#ifdef PLATFORM_WIN32
+               if (cfg->used_int_regs & (1 << AMD64_RDI)) {
+                       amd64_mov_reg_membase (code, AMD64_RDI, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rdi), 8);
+               }
+               if (cfg->used_int_regs & (1 << AMD64_RSI)) {
+                       amd64_mov_reg_membase (code, AMD64_RSI, cfg->frame_reg, lmf_offset + G_STRUCT_OFFSET (MonoLMF, rsi), 8);
+               }
+#endif
        } else {
 
                if (cfg->arch.omit_fp) {
@@ -4559,15 +5134,6 @@ mono_arch_emit_epilog (MonoCompile *cfg)
        cfg->code_len = code - cfg->native_code;
 
        g_assert (cfg->code_len < cfg->code_size);
-
-       if (cfg->arch.omit_fp) {
-               /* 
-                * Encode the stack size into used_int_regs so the exception handler
-                * can access it.
-                */
-               g_assert (cfg->arch.stack_alloc_size < (1 << 16));
-               cfg->used_int_regs |= (1 << 31) | (cfg->arch.stack_alloc_size << 16);
-       }
 }
 
 void
@@ -4768,7 +5334,7 @@ mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean ena
        guchar *code = p;
        int save_mode = SAVE_NONE;
        MonoMethod *method = cfg->method;
-       int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
+       int rtype = mini_type_get_underlying_type (NULL, mono_method_signature (method)->ret)->type;
        
        switch (rtype) {
        case MONO_TYPE_VOID:
@@ -5018,53 +5584,32 @@ 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 -= 7;
 
        /* 
         * 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.
         */
-#ifdef MONO_ARCH_HAVE_IMT
-       if ((code [-2] == 0x41) && (code [-1] == 0xbb) && (code [4] == 0xff) && (x86_modrm_mod (code [5]) == 1) && (x86_modrm_reg (code [5]) == 2) && ((signed char)code [6] < 0)) {
-               /* IMT-based interface calls: with MONO_ARCH_IMT_REG == r11
-                * 41 bb 14 f8 28 08       mov    $0x828f814,%r11d
-                * ff 50 fc                call   *0xfffffffc(%rax)
-                */
-               reg = amd64_modrm_rm (code [5]);
-               disp = (signed char)code [6];
-               /* R10 is clobbered by the IMT thunk code */
-               g_assert (reg != AMD64_R10);
-       }
-#else
-       if (0) {
-       }
-#endif
-       else if ((code [-1] == 0x8b) && (amd64_modrm_mod (code [0]) == 0x2) && (code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x0)) {
-                       /*
-                        * This is a interface call
-                        * 48 8b 80 f0 e8 ff ff   mov    0xffffffffffffe8f0(%rax),%rax
-                        * ff 10                  callq  *(%rax)
-                        */
-               if (IS_REX (code [4]))
-                       rex = code [4];
-               reg = amd64_modrm_rm (code [6]);
-               disp = 0;
-               /* R10 is clobbered by the IMT thunk code */
-               g_assert (reg != AMD64_R10);
-       } else if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
+       if ((code [0] == 0x41) && (code [1] == 0xff) && (code [2] == 0x15)) {
                /* call OFFSET(%rip) */
                disp = *(guint32*)(code + 3);
                return (gpointer*)(code + disp + 7);
-       }
-       else if ((code [1] == 0xff) && (amd64_modrm_reg (code [2]) == 0x2) && (amd64_modrm_mod (code [2]) == 0x2)) {
+       } else if ((code [0] == 0xff) && (amd64_modrm_reg (code [1]) == 0x2) && (amd64_modrm_mod (code [1]) == 0x2) && (amd64_sib_index (code [2]) == 4) && (amd64_sib_scale (code [2]) == 0)) {
+               /* call *[reg+disp32] using indexed addressing */
+               /* The LLVM JIT emits this, and we emit it too for %r12 */
+               if (IS_REX (code [-1])) {
+                       rex = code [-1];
+                       g_assert (amd64_rex_x (rex) == 0);
+               }                       
+               reg = amd64_sib_base (code [2]);
+               disp = *(gint32*)(code + 3);
+       } else if ((code [1] == 0xff) && (amd64_modrm_reg (code [2]) == 0x2) && (amd64_modrm_mod (code [2]) == 0x2)) {
                /* call *[reg+disp32] */
                if (IS_REX (code [0]))
                        rex = code [0];
@@ -5072,16 +5617,19 @@ mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
                disp = *(gint32*)(code + 3);
                /* R10 is clobbered by the IMT thunk code */
                g_assert (reg != AMD64_R10);
-       }
-       else if (code [2] == 0xe8) {
+       } else if (code [2] == 0xe8) {
                /* call <ADDR> */
                return NULL;
-       }
-       else if (IS_REX (code [4]) && (code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x3)) {
+       } else if ((code [3] == 0xff) && (amd64_modrm_reg (code [4]) == 0x2) && (amd64_modrm_mod (code [4]) == 0x1) && (amd64_sib_index (code [5]) == 4) && (amd64_sib_scale (code [5]) == 0)) {
+               /* call *[r12+disp8] using indexed addressing */
+               if (IS_REX (code [2]))
+                       rex = code [2];
+               reg = amd64_sib_base (code [5]);
+               disp = *(gint8*)(code + 6);
+       } else if (IS_REX (code [4]) && (code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x3)) {
                /* call *%reg */
                return NULL;
-       }
-       else if ((code [4] == 0xff) && (amd64_modrm_reg (code [5]) == 0x2) && (amd64_modrm_mod (code [5]) == 0x1)) {
+       } else if ((code [4] == 0xff) && (amd64_modrm_reg (code [5]) == 0x2) && (amd64_modrm_mod (code [5]) == 0x1)) {
                /* call *[reg+disp8] */
                if (IS_REX (code [3]))
                        rex = code [3];
@@ -5090,11 +5638,7 @@ mono_arch_get_vcall_slot (guint8 *code, gpointer *regs, int *displacement)
                //printf ("B: [%%r%d+0x%x]\n", reg, disp);
        }
        else if ((code [5] == 0xff) && (amd64_modrm_reg (code [6]) == 0x2) && (amd64_modrm_mod (code [6]) == 0x0)) {
-                       /*
-                        * This is a interface call: should check the above code can't catch it earlier 
-                        * 8b 40 30   mov    0x30(%eax),%eax
-                        * ff 10      call   *(%eax)
-                        */
+               /* call *%reg */
                if (IS_REX (code [4]))
                        rex = code [4];
                reg = amd64_modrm_rm (code [6]);
@@ -5204,8 +5748,16 @@ mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_targe
                } else {
                        /* We have to shift the arguments left */
                        amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
-                       for (i = 0; i < sig->param_count; ++i)
+                       for (i = 0; i < sig->param_count; ++i) {
+#ifdef PLATFORM_WIN32
+                               if (i < 3)
+                                       amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
+                               else
+                                       amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
+#else
                                amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
+#endif
+                       }
 
                        amd64_jump_membase (code, AMD64_RAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
                }
@@ -5232,6 +5784,24 @@ void
 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
 {
        if (!tls_offset_inited) {
+#ifdef PLATFORM_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 ();
+               lmf_addr_tls_offset = mono_get_jit_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
                tls_offset_inited = TRUE;
 #ifdef MONO_XEN_OPT
                optimize_for_xen = access ("/proc/xen", F_OK) == 0;
@@ -5240,6 +5810,7 @@ mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
                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
        }               
 }
 
@@ -5248,50 +5819,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);
-
-       if (vt_reg != -1) {
-               MonoInst *vtarg;
-
-               if (cinfo->ret.storage == ArgValuetypeInReg) {
-                       /*
-                        * The valuetype is in RAX:RDX after the call, need to be copied to
-                        * the stack. Save the address here, so the call instruction can
-                        * access it.
-                        */
-                       MonoInst *loc = cfg->arch.vret_addr_loc;
-
-                       g_assert (loc);
-                       g_assert (loc->opcode == OP_REGOFFSET);
-
-                       MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, loc->inst_basereg, loc->inst_offset, vt_reg);
-               } else {
-                       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);
-               }
-       }
-
-       /* add the this argument */
-       if (this_reg != -1) {
-               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);
-       }
-}
-
 #ifdef MONO_ARCH_HAVE_IMT
 
 #define CMP_SIZE (6 + 1)
@@ -5315,7 +5842,8 @@ imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
  * LOCKING: called with the domain lock held
  */
 gpointer
-mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count)
+mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
+       gpointer fail_tramp)
 {
        int i;
        int size = 0;
@@ -5327,28 +5855,37 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                if (item->is_equals) {
                        if (item->check_target_idx) {
                                if (!item->compare_done) {
-                                       if (amd64_is_imm32 (item->method))
+                                       if (amd64_is_imm32 (item->key))
                                                item->chunk_size += CMP_SIZE;
                                        else
                                                item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
                                }
-                               if (vtable_is_32bit)
-                                       item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
-                               else
+                               if (item->has_target_code) {
                                        item->chunk_size += MOV_REG_IMM_SIZE;
+                               } else {
+                                       if (vtable_is_32bit)
+                                               item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
+                                       else
+                                               item->chunk_size += MOV_REG_IMM_SIZE;
+                               }
                                item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
                        } else {
-                               if (vtable_is_32bit)
-                                       item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
-                               else
-                                       item->chunk_size += MOV_REG_IMM_SIZE;
-                               item->chunk_size += JUMP_REG_SIZE;
-                               /* with assert below:
-                                * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
-                                */
+                               if (fail_tramp) {
+                                       item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
+                                               BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
+                               } else {
+                                       if (vtable_is_32bit)
+                                               item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
+                                       else
+                                               item->chunk_size += MOV_REG_IMM_SIZE;
+                                       item->chunk_size += JUMP_REG_SIZE;
+                                       /* with assert below:
+                                        * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
+                                        */
+                               }
                        }
                } else {
-                       if (amd64_is_imm32 (item->method))
+                       if (amd64_is_imm32 (item->key))
                                item->chunk_size += CMP_SIZE;
                        else
                                item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
@@ -5357,39 +5894,56 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                }
                size += item->chunk_size;
        }
-       code = mono_code_manager_reserve (domain->code_mp, size);
+       if (fail_tramp)
+               code = mono_method_alloc_generic_virtual_thunk (domain, size);
+       else
+               code = mono_domain_code_reserve (domain, size);
        start = code;
        for (i = 0; i < count; ++i) {
                MonoIMTCheckItem *item = imt_entries [i];
                item->code_target = code;
                if (item->is_equals) {
-                       if (item->check_target_idx) {
-                               if (!item->compare_done) {
-                                       if (amd64_is_imm32 (item->method))
-                                               amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->method);
+                       gboolean fail_case = !item->check_target_idx && fail_tramp;
+
+                       if (item->check_target_idx || fail_case) {
+                               if (!item->compare_done || fail_case) {
+                                       if (amd64_is_imm32 (item->key))
+                                               amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
                                        else {
-                                               amd64_mov_reg_imm (code, AMD64_R10, item->method);
+                                               amd64_mov_reg_imm (code, AMD64_R10, item->key);
                                                amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
                                        }
                                }
                                item->jmp_code = code;
                                amd64_branch8 (code, X86_CC_NE, 0, FALSE);
                                /* See the comment below about R10 */
-                               amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->vtable_slot]));
-                               amd64_jump_membase (code, AMD64_R10, 0);
+                               if (item->has_target_code) {
+                                       amd64_mov_reg_imm (code, AMD64_R10, item->value.target_code);
+                                       amd64_jump_reg (code, AMD64_R10);
+                               } else {
+                                       amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
+                                       amd64_jump_membase (code, AMD64_R10, 0);
+                               }
+
+                               if (fail_case) {
+                                       amd64_patch (item->jmp_code, code);
+                                       amd64_mov_reg_imm (code, AMD64_R10, fail_tramp);
+                                       amd64_jump_reg (code, AMD64_R10);
+                                       item->jmp_code = NULL;
+                               }
                        } else {
                                /* enable the commented code to assert on wrong method */
 #if 0
-                               if (amd64_is_imm32 (item->method))
-                                       amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->method);
+                               if (amd64_is_imm32 (item->key))
+                                       amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
                                else {
-                                       amd64_mov_reg_imm (code, AMD64_R10, item->method);
+                                       amd64_mov_reg_imm (code, AMD64_R10, item->key);
                                        amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
                                }
                                item->jmp_code = code;
                                amd64_branch8 (code, X86_CC_NE, 0, FALSE);
                                /* See the comment below about R10 */
-                               amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->vtable_slot]));
+                               amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
                                amd64_jump_membase (code, AMD64_R10, 0);
                                amd64_patch (item->jmp_code, code);
                                amd64_breakpoint (code);
@@ -5400,15 +5954,15 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                                   to be preserved for calls which
                                   require a runtime generic context,
                                   but interface calls don't. */
-                               amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->vtable_slot]));
+                               amd64_mov_reg_imm (code, AMD64_R10, & (vtable->vtable [item->value.vtable_slot]));
                                amd64_jump_membase (code, AMD64_R10, 0);
 #endif
                        }
                } else {
-                       if (amd64_is_imm32 (item->method))
-                               amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->method);
+                       if (amd64_is_imm32 (item->key))
+                               amd64_alu_reg_imm (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key);
                        else {
-                               amd64_mov_reg_imm (code, AMD64_R10, item->method);
+                               amd64_mov_reg_imm (code, AMD64_R10, item->key);
                                amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, AMD64_R10);
                        }
                        item->jmp_code = code;
@@ -5428,8 +5982,9 @@ mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckI
                        }
                }
        }
-               
-       mono_stats.imt_thunks_size += code - start;
+
+       if (!fail_tramp)
+               mono_stats.imt_thunks_size += code - start;
        g_assert (code - start <= size);
 
        return start;
@@ -5455,28 +6010,32 @@ mono_arch_find_static_call_vtable (gpointer *regs, guint8 *code)
 }
 
 MonoInst*
-mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
+mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
        MonoInst *ins = NULL;
+       int opcode = 0;
 
        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];
+                       opcode = OP_SIN;
                } else if (strcmp (cmethod->name, "Cos") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_COS);
-                       ins->inst_i0 = args [0];
+                       opcode = OP_COS;
                } else if (strcmp (cmethod->name, "Sqrt") == 0) {
-                       MONO_INST_NEW (cfg, ins, OP_SQRT);
-                       ins->inst_i0 = args [0];
+                       opcode = OP_SQRT;
                } 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];
+                       opcode = OP_ABS;
+               }
+               
+               if (opcode) {
+                       MONO_INST_NEW (cfg, ins, opcode);
+                       ins->type = STACK_R8;
+                       ins->dreg = mono_alloc_freg (cfg);
+                       ins->sreg1 = args [0]->dreg;
+                       MONO_ADD_INS (cfg->cbb, ins);
                }
 
+               opcode = 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;
@@ -5495,14 +6054,17 @@ mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethod
                                        opcode = OP_LMAX;
                                else if (fsig->params [0]->type == MONO_TYPE_U8)
                                        opcode = OP_LMAX_UN;
-                       }               
-
-                       if (opcode) {
-                               MONO_INST_NEW (cfg, ins, opcode);
-                               ins->inst_i0 = args [0];
-                               ins->inst_i1 = args [1];
                        }
                }
+               
+               if (opcode) {
+                       MONO_INST_NEW (cfg, ins, opcode);
+                       ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
+                       ins->dreg = mono_alloc_ireg (cfg);
+                       ins->sreg1 = args [0]->dreg;
+                       ins->sreg2 = args [1]->dreg;
+                       MONO_ADD_INS (cfg->cbb, ins);
+               }
 
 #if 0
                /* OP_FREM is not IEEE compatible */
@@ -5514,6 +6076,11 @@ mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethod
 #endif
        }
 
+       /* 
+        * Can't implement CompareExchange methods this way since they have
+        * three arguments.
+        */
+
        return ins;
 }