Merge pull request #607 from maksimenko/master
[mono.git] / mono / mini / method-to-ir.c
index 67436dca1faff439074fb06bc82cdb399472f285..7e0121d5778226621ec86e243031fd48d4d8f5cc 100644 (file)
@@ -11,6 +11,9 @@
  */
 
 #include <config.h>
+
+#ifndef DISABLE_JIT
+
 #include <signal.h>
 
 #ifdef HAVE_UNISTD_H
@@ -154,7 +157,7 @@ static MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline_llvm = NULL
 #define FREG 'f'
 #define VREG 'v'
 #define XREG 'x'
-#if SIZEOF_REGISTER == 8
+#if SIZEOF_REGISTER == 8 && SIZEOF_REGISTER == SIZEOF_VOIDP
 #define LREG IREG
 #else
 #define LREG 'l'
@@ -351,12 +354,6 @@ mono_create_helper_signatures (void)
        helper_sig_monitor_enter_exit_trampoline_llvm = mono_create_icall_signature ("void object");
 }
 
-/* 
- * Can't put this at the beginning, since other files reference stuff from this
- * file.
- */
-#ifndef DISABLE_JIT
-
 /*
  * When using gsharedvt, some instatiations might be verifiable, and some might be not. i.e. 
  * foo<T> (int i) { ldarg.0; box T; }
@@ -2248,6 +2245,24 @@ mono_patch_info_new (MonoMemPool *mp, int ip, MonoJumpInfoType type, gconstpoint
        return ji;
 }
 
+static int
+mini_class_check_context_used (MonoCompile *cfg, MonoClass *klass)
+{
+       if (cfg->generic_sharing_context)
+               return mono_class_check_context_used (klass);
+       else
+               return 0;
+}
+
+static int
+mini_method_check_context_used (MonoCompile *cfg, MonoMethod *method)
+{
+       if (cfg->generic_sharing_context)
+               return mono_method_check_context_used (method);
+       else
+               return 0;
+}
+
 inline static MonoCallInst *
 mono_emit_call_args (MonoCompile *cfg, MonoMethodSignature *sig, 
                                         MonoInst **args, int calli, int virtual, int tail, int rgctx, int unbox_trampoline)
@@ -2398,7 +2413,7 @@ static MonoInst*
 mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig,
                                                        MonoInst **args, MonoInst *this, MonoInst *imt_arg, MonoInst *rgctx_arg)
 {
-       gboolean might_be_remote;
+       gboolean might_be_remote = FALSE;
        gboolean virtual = this != NULL;
        gboolean enable_for_aot = TRUE;
        int context_used;
@@ -2420,10 +2435,11 @@ mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSign
                sig = ctor_sig;
        }
 
-       context_used = mono_method_check_context_used (method);
+       context_used = mini_method_check_context_used (cfg, method);
 
+#ifndef DISABLE_REMOTING
        might_be_remote = this && sig->hasthis &&
-               (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
+               (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) &&
                !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && (!MONO_CHECK_THIS (this) || context_used);
 
        if (might_be_remote && context_used) {
@@ -2435,14 +2451,17 @@ mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSign
 
                return mono_emit_calli (cfg, sig, args, addr, NULL, NULL);
        }
+#endif
 
        need_unbox_trampoline = method->klass == mono_defaults.object_class || (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE);
 
        call = mono_emit_call_args (cfg, sig, args, FALSE, virtual, FALSE, rgctx_arg ? TRUE : FALSE, need_unbox_trampoline);
 
+#ifndef DISABLE_REMOTING
        if (might_be_remote)
                call->method = mono_marshal_get_remoting_invoke_with_check (method);
        else
+#endif
                call->method = method;
        call->inst.flags |= MONO_INST_HAS_METHOD;
        call->inst.inst_left = this;
@@ -2486,18 +2505,20 @@ mono_emit_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSign
                        (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) || 
                         (MONO_METHOD_IS_FINAL (method) &&
                          method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) &&
-                       !(method->klass->marshalbyref && context_used)) {
+                       !(mono_class_is_marshalbyref (method->klass) && context_used)) {
                        /* 
                         * the method is not virtual, we just need to ensure this is not null
                         * and then we can call the method directly.
                         */
-                       if (method->klass->marshalbyref || method->klass == mono_defaults.object_class) {
+#ifndef DISABLE_REMOTING
+                       if (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class) {
                                /* 
                                 * The check above ensures method is not gshared, this is needed since
                                 * gshared methods can't have wrappers.
                                 */
                                method = call->method = mono_marshal_get_remoting_invoke_with_check (method);
                        }
+#endif
 
                        if (!method->string_ctor)
                                MONO_EMIT_NEW_CHECK_THIS (cfg, this_reg);
@@ -2864,7 +2885,7 @@ mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *kla
 
        if (mini_is_gsharedvt_klass (cfg, klass)) {
                g_assert (!native);
-               context_used = mono_class_check_context_used (klass);
+               context_used = mini_class_check_context_used (cfg, klass);
                size_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_VALUE_SIZE);
        }
 
@@ -2878,13 +2899,12 @@ mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *kla
                /* Avoid barriers when storing to the stack */
                if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
                          (dest->opcode == OP_LDADDR))) {
-                       int context_used = 0;
+                       int context_used;
 
                        iargs [0] = dest;
                        iargs [1] = src;
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
                        if (!size_ins && (cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
@@ -2946,7 +2966,7 @@ mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, MonoClass
 
        mono_class_init (klass);
        if (mini_is_gsharedvt_klass (cfg, klass)) {
-               context_used = mono_class_check_context_used (klass);
+               context_used = mini_class_check_context_used (cfg, klass);
                size_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_VALUE_SIZE);
                n = -1;
        } else {
@@ -3048,6 +3068,34 @@ emit_get_rgctx_klass (MonoCompile *cfg, int context_used,
        return emit_rgctx_fetch (cfg, rgctx, entry);
 }
 
+static MonoInst*
+emit_get_rgctx_sig (MonoCompile *cfg, int context_used,
+                                       MonoMethodSignature *sig, MonoRgctxInfoType rgctx_type)
+{
+       MonoJumpInfoRgctxEntry *entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_SIGNATURE, sig, rgctx_type);
+       MonoInst *rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
+
+       return emit_rgctx_fetch (cfg, rgctx, entry);
+}
+
+static MonoInst*
+emit_get_rgctx_gsharedvt_call (MonoCompile *cfg, int context_used,
+                                                          MonoMethodSignature *sig, MonoMethod *cmethod, MonoRgctxInfoType rgctx_type)
+{
+       MonoJumpInfoGSharedVtCall *call_info;
+       MonoJumpInfoRgctxEntry *entry;
+       MonoInst *rgctx;
+
+       call_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoGSharedVtCall));
+       call_info->sig = sig;
+       call_info->method = cmethod;
+
+       entry = mono_patch_info_rgctx_entry_new (cfg->mempool, cfg->current_method, context_used & MONO_GENERIC_CONTEXT_USED_METHOD, MONO_PATCH_INFO_GSHAREDVT_CALL, call_info, rgctx_type);
+       rgctx = emit_get_rgctx (cfg, cfg->current_method, context_used);
+
+       return emit_rgctx_fetch (cfg, rgctx, entry);
+}
+
 /*
  * emit_get_rgctx_method:
  *
@@ -3097,10 +3145,9 @@ emit_generic_class_init (MonoCompile *cfg, MonoClass *klass)
 {
        MonoInst *vtable_arg;
        MonoCallInst *call;
-       int context_used = 0;
+       int context_used;
 
-       if (cfg->generic_sharing_context)
-               context_used = mono_class_check_context_used (klass);
+       context_used = mini_class_check_context_used (cfg, klass);
 
        if (context_used) {
                vtable_arg = emit_get_rgctx_klass (cfg, context_used,
@@ -3180,10 +3227,9 @@ static void
 mini_emit_check_array_type (MonoCompile *cfg, MonoInst *obj, MonoClass *array_class)
 {
        int vtable_reg = alloc_preg (cfg);
-       int context_used = 0;
+       int context_used;
 
-       if (cfg->generic_sharing_context)
-               context_used = mono_class_check_context_used (array_class);
+       context_used = mini_class_check_context_used (cfg, array_class);
 
        save_cast_details (cfg, array_class, obj->dreg);
 
@@ -3410,18 +3456,55 @@ handle_box (MonoCompile *cfg, MonoInst *val, MonoClass *klass, int context_used)
                }
        }
 
-       alloc = handle_alloc (cfg, klass, TRUE, context_used);
-       if (!alloc)
-               return NULL;
-
        if (mini_is_gsharedvt_klass (cfg, klass)) {
+               MonoBasicBlock *is_ref_bb, *end_bb;
+               MonoInst *res, *is_ref, *src_var, *addr;
+               int addr_reg, dreg;
+
+               dreg = alloc_ireg (cfg);
+
+               NEW_BBLOCK (cfg, is_ref_bb);
+               NEW_BBLOCK (cfg, end_bb);
+               is_ref = emit_get_rgctx_klass (cfg, context_used, klass,
+                                                                          MONO_RGCTX_INFO_CLASS_IS_REF);
+               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, is_ref->dreg, 1);
+               MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_IBEQ, is_ref_bb);
+
+               /* Non-ref case */
+               alloc = handle_alloc (cfg, klass, TRUE, context_used);
+               if (!alloc)
+                       return NULL;
                EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
                ins->opcode = OP_STOREV_MEMBASE;
+
+               EMIT_NEW_UNALU (cfg, res, OP_MOVE, dreg, alloc->dreg);
+               res->type = STACK_OBJ;
+               res->klass = klass;
+               MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
+               
+               /* Ref case */
+               MONO_START_BB (cfg, is_ref_bb);
+               addr_reg = alloc_ireg (cfg);
+
+               /* val is a vtype, so has to load the value manually */
+               src_var = get_vreg_to_inst (cfg, val->dreg);
+               if (!src_var)
+                       src_var = mono_compile_create_var_for_vreg (cfg, &klass->byval_arg, OP_LOCAL, val->dreg);
+               EMIT_NEW_VARLOADA (cfg, addr, src_var, src_var->inst_vtype);
+               MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, addr->dreg, 0);
+               MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
+
+               MONO_START_BB (cfg, end_bb);
+
+               return res;
        } else {
+               alloc = handle_alloc (cfg, klass, TRUE, context_used);
+               if (!alloc)
+                       return NULL;
+
                EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, alloc->dreg, sizeof (MonoObject), val->dreg);
+               return alloc;
        }
-
-       return alloc;
 }
 
 
@@ -3454,7 +3537,7 @@ mini_class_has_reference_variant_generic_argument (MonoCompile *cfg, MonoClass *
 }
 
 // FIXME: This doesn't work yet (class libs tests fail?)
-#define is_complex_isinst(klass) (TRUE || (klass->flags & TYPE_ATTRIBUTE_INTERFACE) || klass->rank || mono_class_is_nullable (klass) || klass->marshalbyref || (klass->flags & TYPE_ATTRIBUTE_SEALED) || klass->byval_arg.type == MONO_TYPE_VAR || klass->byval_arg.type == MONO_TYPE_MVAR)
+#define is_complex_isinst(klass) (TRUE || (klass->flags & TYPE_ATTRIBUTE_INTERFACE) || klass->rank || mono_class_is_nullable (klass) || mono_class_is_marshalbyref (klass) || (klass->flags & TYPE_ATTRIBUTE_SEALED) || klass->byval_arg.type == MONO_TYPE_VAR || klass->byval_arg.type == MONO_TYPE_MVAR)
 
 /*
  * Returns NULL and set the cfg exception on error.
@@ -3709,26 +3792,37 @@ handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
        2) if the object is a proxy whose type cannot be determined */
 
        MonoInst *ins;
+#ifndef DISABLE_REMOTING
        MonoBasicBlock *true_bb, *false_bb, *false2_bb, *end_bb, *no_proxy_bb, *interface_fail_bb;
+#else
+       MonoBasicBlock *true_bb, *false_bb, *end_bb;
+#endif
        int obj_reg = src->dreg;
        int dreg = alloc_ireg (cfg);
        int tmp_reg;
+#ifndef DISABLE_REMOTING
        int klass_reg = alloc_preg (cfg);
+#endif
 
        NEW_BBLOCK (cfg, true_bb);
        NEW_BBLOCK (cfg, false_bb);
-       NEW_BBLOCK (cfg, false2_bb);
        NEW_BBLOCK (cfg, end_bb);
+#ifndef DISABLE_REMOTING
+       NEW_BBLOCK (cfg, false2_bb);
        NEW_BBLOCK (cfg, no_proxy_bb);
+#endif
 
        MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
        MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBEQ, false_bb);
 
        if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
+#ifndef DISABLE_REMOTING
                NEW_BBLOCK (cfg, interface_fail_bb);
+#endif
 
                tmp_reg = alloc_preg (cfg);
                MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
+#ifndef DISABLE_REMOTING
                mini_emit_iface_cast (cfg, tmp_reg, klass, interface_fail_bb, true_bb);
                MONO_START_BB (cfg, interface_fail_bb);
                MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoVTable, klass));
@@ -3739,7 +3833,11 @@ handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
                MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoTransparentProxy, custom_type_info));
                MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, tmp_reg, 0);
                MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_PBNE_UN, false2_bb);                
+#else
+               mini_emit_iface_cast (cfg, tmp_reg, klass, false_bb, true_bb);
+#endif
        } else {
+#ifndef DISABLE_REMOTING
                tmp_reg = alloc_preg (cfg);
                MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
                MONO_EMIT_NEW_LOAD_MEMBASE (cfg, klass_reg, tmp_reg, G_STRUCT_OFFSET (MonoVTable, klass));
@@ -3760,6 +3858,9 @@ handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
                MONO_START_BB (cfg, no_proxy_bb);
 
                mini_emit_isninst_cast (cfg, klass_reg, klass, false_bb, true_bb);
+#else
+               g_error ("transparent proxy support is disabled while trying to JIT code that uses it");
+#endif
        }
 
        MONO_START_BB (cfg, false_bb);
@@ -3767,10 +3868,12 @@ handle_cisinst (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
        MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
        MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
 
+#ifndef DISABLE_REMOTING
        MONO_START_BB (cfg, false2_bb);
 
        MONO_EMIT_NEW_ICONST (cfg, dreg, 2);
        MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
+#endif
 
        MONO_START_BB (cfg, true_bb);
 
@@ -3795,13 +3898,20 @@ handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
        an InvalidCastException exception is thrown otherwhise*/
        
        MonoInst *ins;
+#ifndef DISABLE_REMOTING
        MonoBasicBlock *end_bb, *ok_result_bb, *no_proxy_bb, *interface_fail_bb, *fail_1_bb;
+#else
+       MonoBasicBlock *ok_result_bb;
+#endif
        int obj_reg = src->dreg;
        int dreg = alloc_ireg (cfg);
        int tmp_reg = alloc_preg (cfg);
-       int klass_reg = alloc_preg (cfg);
 
+#ifndef DISABLE_REMOTING
+       int klass_reg = alloc_preg (cfg);
        NEW_BBLOCK (cfg, end_bb);
+#endif
+
        NEW_BBLOCK (cfg, ok_result_bb);
 
        MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, obj_reg, 0);
@@ -3810,6 +3920,7 @@ handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
        save_cast_details (cfg, klass, obj_reg);
 
        if (klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
+#ifndef DISABLE_REMOTING
                NEW_BBLOCK (cfg, interface_fail_bb);
        
                MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
@@ -3826,8 +3937,13 @@ handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
                
                MONO_EMIT_NEW_ICONST (cfg, dreg, 1);
                MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, end_bb);
-               
+#else
+               MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
+               mini_emit_iface_cast (cfg, tmp_reg, klass, NULL, NULL);
+               MONO_EMIT_NEW_BRANCH_BLOCK (cfg, OP_BR, ok_result_bb);
+#endif
        } else {
+#ifndef DISABLE_REMOTING
                NEW_BBLOCK (cfg, no_proxy_bb);
 
                MONO_EMIT_NEW_LOAD_MEMBASE (cfg, tmp_reg, obj_reg, G_STRUCT_OFFSET (MonoObject, vtable));
@@ -3855,13 +3971,18 @@ handle_ccastclass (MonoCompile *cfg, MonoClass *klass, MonoInst *src)
                MONO_START_BB (cfg, no_proxy_bb);
 
                mini_emit_castclass (cfg, obj_reg, klass_reg, klass, ok_result_bb);
+#else
+               g_error ("Transparent proxy support is disabled while trying to JIT code that uses it");
+#endif
        }
 
        MONO_START_BB (cfg, ok_result_bb);
 
        MONO_EMIT_NEW_ICONST (cfg, dreg, 0);
 
+#ifndef DISABLE_REMOTING
        MONO_START_BB (cfg, end_bb);
+#endif
 
        /* FIXME: */
        MONO_INST_NEW (cfg, ins, OP_ICONST);
@@ -4035,7 +4156,7 @@ mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
        /*runtime, icall and pinvoke are checked by summary call*/
        if ((method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
            (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
-           (method->klass->marshalbyref) ||
+           (mono_class_is_marshalbyref (method->klass)) ||
            header.has_clauses)
                return FALSE;
 
@@ -4191,7 +4312,7 @@ mini_emit_ldelema_1_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, Mono
 
                /* gsharedvt */
                g_assert (cfg->generic_sharing_context);
-               context_used = mono_class_check_context_used (klass);
+               context_used = mini_class_check_context_used (cfg, klass);
                g_assert (context_used);
                rgctx_ins = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_ARRAY_ELEMENT_SIZE);
                MONO_EMIT_NEW_BIALU (cfg, OP_IMUL, mult_reg, index2_reg, rgctx_ins->dreg);
@@ -4965,6 +5086,9 @@ mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSign
                }
 #endif /* MONO_ARCH_HAVE_ATOMIC_CAS */
 
+               if (strcmp (cmethod->name, "MemoryBarrier") == 0)
+                       ins = emit_memory_barrier (cfg, FullBarrier);
+
                if (ins)
                        return ins;
        } else if (cmethod->klass->image == mono_defaults.corlib) {
@@ -5500,10 +5624,13 @@ mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, Mono
 {
        MonoMethod *method;
 
-       if (m->wrapper_type != MONO_WRAPPER_NONE)
-               return mono_method_get_wrapper_data (m, token);
-
-       method = mono_get_method_full (m->klass->image, token, klass, context);
+       if (m->wrapper_type != MONO_WRAPPER_NONE) {
+               method = mono_method_get_wrapper_data (m, token);
+               if (context)
+                       method = mono_class_inflate_generic_method (method, context);
+       } else {
+               method = mono_get_method_full (m->klass->image, token, klass, context);
+       }
 
        return method;
 }
@@ -5524,15 +5651,38 @@ mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
 {
        MonoClass *klass;
 
-       if (method->wrapper_type != MONO_WRAPPER_NONE)
+       if (method->wrapper_type != MONO_WRAPPER_NONE) {
                klass = mono_method_get_wrapper_data (method, token);
-       else
+               if (context)
+                       klass = mono_class_inflate_generic_class (klass, context);
+       } else {
                klass = mono_class_get_full (method->klass->image, token, context);
+       }
        if (klass)
                mono_class_init (klass);
        return klass;
 }
 
+static inline MonoMethodSignature*
+mini_get_signature (MonoMethod *method, guint32 token, MonoGenericContext *context)
+{
+       MonoMethodSignature *fsig;
+
+       if (method->wrapper_type != MONO_WRAPPER_NONE) {
+               MonoError error;
+
+               fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
+               if (context) {
+                       fsig = mono_inflate_generic_signature (fsig, context, &error);
+                       // FIXME:
+                       g_assert (mono_error_ok (&error));
+               }
+       } else {
+               fsig = mono_metadata_parse_signature (method->klass->image, token);
+       }
+       return fsig;
+}
+
 /*
  * Returns TRUE if the JIT should abort inlining because "callee"
  * is influenced by security attributes.
@@ -6948,6 +7098,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        int calli = *ip == CEE_CALLI;
                        gboolean pass_imt_from_rgctx = FALSE;
                        MonoInst *imt_arg = NULL;
+                       MonoInst *keep_this_alive = NULL;
                        gboolean pass_vtable = FALSE;
                        gboolean pass_mrgctx = FALSE;
                        MonoInst *vtable_arg = NULL;
@@ -6958,6 +7109,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        gboolean emit_widen = TRUE;
                        gboolean push_res = TRUE;
                        gboolean skip_ret = FALSE;
+                       gboolean delegate_invoke = FALSE;
 
                        CHECK_OPSIZE (5);
                        token = read32 (ip + 1);
@@ -6965,16 +7117,12 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        ins = NULL;
 
                        if (calli) {
-                               GSHAREDVT_FAILURE (*ip);
+                               //GSHAREDVT_FAILURE (*ip);
                                cmethod = NULL;
                                CHECK_STACK (1);
                                --sp;
                                addr = *sp;
-                               if (method->wrapper_type != MONO_WRAPPER_NONE)
-                                       fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
-                               else
-                                       fsig = mono_metadata_parse_signature (image, token);
-
+                               fsig = mini_get_signature (method, token, generic_context);
                                n = fsig->param_count + fsig->hasthis;
 
                                if (method->dynamic && fsig->pinvoke) {
@@ -6993,39 +7141,36 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
                        } else {
                                MonoMethod *cil_method;
-                               
-                               if (method->wrapper_type != MONO_WRAPPER_NONE) {
-                                       if (constrained_call && cfg->verbose_level > 2)
-                                               printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_call));
-                                       cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
-                                       cil_method = cmethod;
-                                       if (constrained_call && !((constrained_call->byval_arg.type == MONO_TYPE_VAR ||
-                                                       constrained_call->byval_arg.type == MONO_TYPE_MVAR) &&
-                                                       cfg->generic_sharing_context)) {
-                                               cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_call, generic_context);
-                                       }
-                               } else if (constrained_call) {
-                                       if (cfg->verbose_level > 2)
-                                               printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_call));
-
-                                       GSHAREDVT_FAILURE (*ip);
 
-                                       if ((constrained_call->byval_arg.type == MONO_TYPE_VAR || constrained_call->byval_arg.type == MONO_TYPE_MVAR) && cfg->generic_sharing_context) {
-                                               /* 
-                                                * This is needed since get_method_constrained can't find 
-                                                * the method in klass representing a type var.
-                                                * The type var is guaranteed to be a reference type in this
-                                                * case.
-                                                */
-                                               cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
-                                               cil_method = cmethod;
-                                               g_assert (!cmethod->klass->valuetype);
+                               cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
+                               cil_method = cmethod;
+                               
+                               if (constrained_call) {
+                                       if (method->wrapper_type != MONO_WRAPPER_NONE) {
+                                               if (cfg->verbose_level > 2)
+                                                       printf ("DM Constrained call to %s\n", mono_type_get_full_name (constrained_call));
+                                               if (!((constrained_call->byval_arg.type == MONO_TYPE_VAR ||
+                                                          constrained_call->byval_arg.type == MONO_TYPE_MVAR) &&
+                                                         cfg->generic_sharing_context)) {
+                                                       cmethod = mono_get_method_constrained_with_method (image, cil_method, constrained_call, generic_context);
+                                               }
                                        } else {
-                                               cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context, &cil_method);
+                                               if (cfg->verbose_level > 2)
+                                                       printf ("Constrained call to %s\n", mono_type_get_full_name (constrained_call));
+
+                                               if ((constrained_call->byval_arg.type == MONO_TYPE_VAR || constrained_call->byval_arg.type == MONO_TYPE_MVAR) && cfg->generic_sharing_context) {
+                                                       /* 
+                                                        * This is needed since get_method_constrained can't find 
+                                                        * the method in klass representing a type var.
+                                                        * The type var is guaranteed to be a reference type in this
+                                                        * case.
+                                                        */
+                                                       if (!mini_is_gsharedvt_klass (cfg, constrained_call))
+                                                               g_assert (!cmethod->klass->valuetype);
+                                               } else {
+                                                       cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context, &cil_method);
+                                               }
                                        }
-                               } else {
-                                       cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
-                                       cil_method = cmethod;
                                }
                                        
                                if (!cmethod || mono_loader_get_last_error ())
@@ -7056,7 +7201,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                         */
                                        const int test_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_STATIC;
                                        const int expected_flags = METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_FINAL;
-                                       if (!virtual && cmethod->klass->marshalbyref && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
+                                       if (!virtual && mono_class_is_marshalbyref (cmethod->klass) && (cmethod->flags & test_flags) == expected_flags && cfg->method->wrapper_type == MONO_WRAPPER_NONE)
                                                virtual = 1;
                                }
 
@@ -7135,6 +7280,33 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        sp -= n;
 
                        if (constrained_call) {
+                               if (mini_is_gsharedvt_klass (cfg, constrained_call)) {
+                                       /*
+                                        * Constrained calls need to behave differently at runtime dependending on whenever the receiver is instantiated as ref type or as a vtype.
+                                        */
+                                       /* Special case Object:ToString () as its easy to implement */
+                                       if (cmethod->klass == mono_defaults.object_class && !strcmp (cmethod->name, "ToString")) {
+                                               MonoInst *args [3];
+
+                                               args [0] = sp [0];
+                                               EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
+                                               args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_call), constrained_call, MONO_RGCTX_INFO_KLASS);
+                                               ins = mono_emit_jit_icall (cfg, mono_object_tostring_gsharedvt, args);
+                                               goto call_end;
+                                       } else if (cmethod->klass == mono_defaults.object_class && !strcmp (cmethod->name, "GetHashCode")) {
+                                               MonoInst *args [3];
+
+                                               args [0] = sp [0];
+                                               EMIT_NEW_METHODCONST (cfg, args [1], cmethod);
+                                               args [2] = emit_get_rgctx_klass (cfg, mono_class_check_context_used (constrained_call), constrained_call, MONO_RGCTX_INFO_KLASS);
+                                               ins = mono_emit_jit_icall (cfg, mono_object_gethashcode_gsharedvt, args);
+                                               goto call_end;
+                                       } else if (constrained_call->valuetype && cmethod->klass->valuetype) {
+                                               /* The 'Own method' case below */
+                                       } else {
+                                               GSHAREDVT_FAILURE (*ip);
+                                       }
+                               }
                                /*
                                 * We have the `constrained.' prefix opcode.
                                 */
@@ -7147,6 +7319,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_call->byval_arg, sp [0]->dreg, 0);
                                        ins->klass = constrained_call;
                                        sp [0] = handle_box (cfg, ins, constrained_call, mono_class_check_context_used (constrained_call));
+                                       bblock = cfg->cbb;
                                        CHECK_CFG_EXCEPTION;
                                } else if (!constrained_call->valuetype) {
                                        int dreg = alloc_ireg_ref (cfg);
@@ -7181,6 +7354,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                        EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &constrained_call->byval_arg, sp [0]->dreg, 0);
                                                        ins->klass = constrained_call;
                                                        sp [0] = handle_box (cfg, ins, constrained_call, mono_class_check_context_used (constrained_call));
+                                                       bblock = cfg->cbb;
                                                        CHECK_CFG_EXCEPTION;
                                                }
                                        }
@@ -7192,6 +7366,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (!calli && check_call_signature (cfg, fsig, sp))
                                UNVERIFIED;
 
+#ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
+                       if (cmethod && (cmethod->klass->parent == mono_defaults.multicastdelegate_class) && !strcmp (cmethod->name, "Invoke"))
+                               delegate_invoke = TRUE;
+#endif
+
                        if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_sharable_method (cfg, cmethod, fsig, sp))) {
                                bblock = cfg->cbb;
                                if (!MONO_TYPE_IS_VOID (fsig->ret)) {
@@ -7249,7 +7428,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                                        if (sharing_enabled && context_sharable)
                                                pass_mrgctx = TRUE;
-                                       if (cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, mono_method_signature (cmethod)))
+                                       if (cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig))
                                                pass_mrgctx = TRUE;
                                }
                        }
@@ -7257,7 +7436,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (cfg->generic_sharing_context && cmethod) {
                                MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
 
-                               context_used = mono_method_check_context_used (cmethod);
+                               context_used = mini_method_check_context_used (cfg, cmethod);
 
                                if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
                                        /* Generic method interface
@@ -7312,7 +7491,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                /* !marshalbyref is needed to properly handle generic methods + remoting */
                                if ((!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
                                         MONO_METHOD_IS_FINAL (cmethod)) &&
-                                       !cmethod->klass->marshalbyref) {
+                                       !mono_class_is_marshalbyref (cmethod->klass)) {
                                        if (virtual)
                                                check_this = TRUE;
                                        virtual = 0;
@@ -7335,7 +7514,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                            (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) && 
                            !(MONO_METHOD_IS_FINAL (cmethod) && 
                              cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
-                           fsig->generic_param_count) {
+                           fsig->generic_param_count && 
+                               !(cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig))) {
                                MonoInst *this_temp, *this_arg_temp, *store;
                                MonoInst *iargs [4];
                                gboolean use_imt = FALSE;
@@ -7491,40 +7671,61 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                         * Making generic calls out of gsharedvt methods.
                         */
                        if (cmethod && cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig)) {
+                               MonoRgctxInfoType info_type;
+
                                if (virtual) {
                                        //if (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
                                                //GSHAREDVT_FAILURE (*ip);
                                        // disable for possible remoting calls
-                                       if (fsig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))
+                                       if (fsig->hasthis && (mono_class_is_marshalbyref (method->klass) || method->klass == mono_defaults.object_class))
                                                GSHAREDVT_FAILURE (*ip);
-                                       // virtual generic calls were disabled earlier
+                                       if (fsig->generic_param_count) {
+                                               /* virtual generic call */
+                                               g_assert (mono_use_imt);
+                                               g_assert (!imt_arg);
+                                               /* Same as the virtual generic case above */
+                                               imt_arg = emit_get_rgctx_method (cfg, context_used,
+                                                                                                                cmethod, MONO_RGCTX_INFO_METHOD);
+                                               /* This is not needed, as the trampoline code will pass one, and it might be passed in the same reg as the imt arg */
+                                               vtable_arg = NULL;
+                                       }
                                }
 
                                if (cmethod->klass->rank && cmethod->klass->byval_arg.type != MONO_TYPE_SZARRAY)
                                        /* test_0_multi_dim_arrays () in gshared.cs */
                                        GSHAREDVT_FAILURE (*ip);
 
+                               if ((cmethod->klass->parent == mono_defaults.multicastdelegate_class) && (!strcmp (cmethod->name, "Invoke")))
+                                       keep_this_alive = sp [0];
+
                                if (virtual && (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))
-                                       addr = emit_get_rgctx_method (cfg, context_used,
-                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT);
+                                       info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE_VIRT;
                                else
-                                       addr = emit_get_rgctx_method (cfg, context_used,
-                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
+                                       info_type = MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE;
+                               addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, info_type);
+
                                ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
                                goto call_end;
-                       }
+                       } else if (calli && cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig)) {
+                               /*
+                                * We pass the address to the gsharedvt trampoline in the rgctx reg
+                                */
+                               MonoInst *callee = addr;
 
-                       if (virtual && cmethod && cfg->gsharedvt && cmethod->slot == -1) {
-                               mono_class_setup_vtable (cmethod->klass);
-                               if (cmethod->slot == -1)
-                                       // FIXME: How can this happen ?
+                               if (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE)
+                                       /* Not tested */
                                        GSHAREDVT_FAILURE (*ip);
+
+                               addr = emit_get_rgctx_sig (cfg, context_used,
+                                                                                         fsig, MONO_RGCTX_INFO_SIG_GSHAREDVT_OUT_TRAMPOLINE_CALLI);
+                               ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, NULL, callee);
+                               goto call_end;
                        }
-                       
+
                        /* Generic sharing */
                        /* FIXME: only do this for generic methods if
                           they are not shared! */
-                       if (context_used && !imt_arg && !array_rank &&
+                       if (context_used && !imt_arg && !array_rank && !delegate_invoke &&
                                (!mono_method_is_generic_sharable_impl (cmethod, TRUE) ||
                                 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
                                (!virtual || MONO_METHOD_IS_FINAL (cmethod) ||
@@ -7541,6 +7742,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                 * the method in the rgctx and do an
                                 * indirect call.
                                 */
+                               if (fsig->hasthis)
+                                       MONO_EMIT_NEW_CHECK_THIS (cfg, sp [0]->dreg);
+
                                addr = emit_get_rgctx_method (cfg, context_used, cmethod, MONO_RGCTX_INFO_GENERIC_METHOD_CODE);
                                ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
                                goto call_end;
@@ -7560,20 +7764,29 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                /* Prevent inlining of methods with indirect calls */
                                INLINE_FAILURE ("indirect call");
 
-                               if (addr->opcode == OP_AOTCONST && addr->inst_c1 == MONO_PATCH_INFO_ICALL_ADDR) {
+                               if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST || addr->opcode == OP_GOT_ENTRY) {
+                                       int info_type;
+                                       gpointer info_data;
+
                                        /* 
                                         * Instead of emitting an indirect call, emit a direct call
                                         * with the contents of the aotconst as the patch info.
                                         */
-                                       ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR, addr->inst_p0, fsig, sp);
-                                       NULLIFY_INS (addr);
-                               } else if (addr->opcode == OP_GOT_ENTRY && addr->inst_right->inst_c1 == MONO_PATCH_INFO_ICALL_ADDR) {
-                                       ins = (MonoInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_ICALL_ADDR, addr->inst_right->inst_left, fsig, sp);
-                                       NULLIFY_INS (addr);
-                               } else {
-                                       ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
+                                       if (addr->opcode == OP_PCONST || addr->opcode == OP_AOTCONST) {
+                                               info_type = addr->inst_c1;
+                                               info_data = addr->inst_p0;
+                                       } else {
+                                               info_type = addr->inst_right->inst_c1;
+                                               info_data = addr->inst_right->inst_left;
+                                       }
+                                       
+                                       if (info_type == MONO_PATCH_INFO_ICALL_ADDR || info_type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
+                                               ins = (MonoInst*)mono_emit_abs_call (cfg, info_type, info_data, fsig, sp);
+                                               NULLIFY_INS (addr);
+                                               goto call_end;
+                                       }
                                }
-
+                               ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr, imt_arg, vtable_arg);
                                goto call_end;
                        }
                                        
@@ -7675,7 +7888,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                GET_BBLOCK (cfg, tblock, ip + 5);
                                if (tblock == bblock || tblock->in_count == 0)
                                        skip_ret = TRUE;
-                               push_res = TRUE;
+                               push_res = FALSE;
 
                                goto call_end;
                        }
@@ -7708,6 +7921,13 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        *sp++ = ins;
                        }
 
+                       if (keep_this_alive) {
+                               MonoInst *dummy_use;
+
+                               /* See mono_emit_method_call_full () */
+                               EMIT_NEW_DUMMY_USE (cfg, dummy_use, keep_this_alive);
+                       }
+
                        CHECK_CFG_EXCEPTION;
 
                        ip += 5;
@@ -7716,6 +7936,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                ip += 1;
                        }
                        ins_flag = 0;
+                       constrained_call = NULL;
                        if (need_seq_point)
                                emit_seq_point (cfg, method, ip, FALSE);
                        break;
@@ -8486,8 +8707,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (!mono_class_init (cmethod->klass))
                                TYPE_LOAD_ERROR (cmethod->klass);
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_method_check_context_used (cmethod);
+                       context_used = mini_method_check_context_used (cfg, cmethod);
 
                        if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
                                if (check_linkdemand (cfg, method, cmethod))
@@ -8502,9 +8722,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                CHECK_TYPELOAD (cmethod->klass);
                        }
 
-                       if (cmethod->klass->valuetype)
-                               GSHAREDVT_FAILURE (*ip);
-
                        /*
                        if (cfg->gsharedvt) {
                                if (mini_is_gsharedvt_variable_signature (sig))
@@ -8600,6 +8817,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        alloc = mono_emit_jit_icall (cfg, mono_array_new_2, sp);
                                else if (fsig->param_count == 3)
                                        alloc = mono_emit_jit_icall (cfg, mono_array_new_3, sp);
+                               else if (fsig->param_count == 4)
+                                       alloc = mono_emit_jit_icall (cfg, mono_array_new_4, sp);
                                else
                                        alloc = handle_array_new (cfg, fsig->param_count, sp, ip);
                        } else if (cmethod->string_ctor) {
@@ -8654,7 +8873,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                                /* Now call the actual ctor */
                                /* Avoid virtual calls to ctors if possible */
-                               if (cmethod->klass->marshalbyref)
+                               if (mono_class_is_marshalbyref (cmethod->klass))
                                        callvirt_this_arg = sp [0];
 
 
@@ -8687,8 +8906,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                } else if (cfg->gsharedvt && mini_is_gsharedvt_signature (cfg, fsig)) {
                                        MonoInst *addr;
 
-                                       addr = emit_get_rgctx_method (cfg, context_used,
-                                                                                                 cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
+                                       addr = emit_get_rgctx_gsharedvt_call (cfg, context_used, fsig, cmethod, MONO_RGCTX_INFO_METHOD_GSHAREDVT_OUT_TRAMPOLINE);
                                        mono_emit_calli (cfg, fsig, sp, addr, NULL, vtable_arg);
                                } else if (context_used &&
                                                (!mono_method_is_generic_sharable_impl (cmethod, TRUE) ||
@@ -8729,8 +8947,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp [0]->type != STACK_OBJ)
                                UNVERIFIED;
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
                                MonoMethod *mono_castclass = mono_marshal_get_castclass_with_cache ();
@@ -8752,7 +8969,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                *sp++ = mono_emit_method_call (cfg, mono_castclass, args, NULL);
                                ip += 5;
                                inline_costs += 2;
-                       } else if (!context_used && (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+                       } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
                                MonoMethod *mono_castclass;
                                MonoInst *iargs [1];
                                int costs;
@@ -8791,8 +9008,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp [0]->type != STACK_OBJ)
                                UNVERIFIED;
  
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        if (!context_used && mini_class_has_reference_variant_generic_argument (cfg, klass, context_used)) {
                                MonoMethod *mono_isinst = mono_marshal_get_isinst_with_cache ();
@@ -8813,7 +9029,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                *sp++ = mono_emit_method_call (cfg, mono_isinst, args, NULL);
                                ip += 5;
                                inline_costs += 2;
-                       } else if (!context_used && (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+                       } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
                                MonoMethod *mono_isinst;
                                MonoInst *iargs [1];
                                int costs;
@@ -8853,12 +9069,42 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
  
                        mono_save_token_info (cfg, image, token, klass);
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
-                       if (mini_is_gsharedvt_klass (cfg, klass))
-                               /* Need to check for nullable types at runtime */
-                               GSHAREDVT_FAILURE (*ip);
+                       if (mini_is_gsharedvt_klass (cfg, klass)) {
+                               MonoInst *obj, *addr, *klass_inst, *args[16];
+                               int dreg;
+
+                               /* Need to check for nullable types at runtime, but those are disabled in mini_is_gsharedvt_sharable_method*/
+                               if (mono_class_is_nullable (klass))
+                                       GSHAREDVT_FAILURE (*ip);
+
+                               obj = *sp;
+
+                               klass_inst = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
+
+                               /* obj */
+                               args [0] = obj;
+
+                               /* klass */
+                               args [1] = klass_inst;
+
+                               /* CASTCLASS */
+                               obj = mono_emit_jit_icall (cfg, mono_object_castclass, args);
+
+                               /* UNBOX */
+                               dreg = alloc_dreg (cfg, STACK_MP);
+                               NEW_BIALU_IMM (cfg, addr, OP_ADD_IMM, dreg, obj->dreg, sizeof (MonoObject));
+                               MONO_ADD_INS (cfg->cbb, addr);
+
+                               /* LDOBJ */
+                               EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, addr->dreg, 0);
+                               *sp++ = ins;
+
+                               ip += 5;
+                               inline_costs += 2;
+                               break;
+                       }
 
                        if (generic_class_is_reference_type (cfg, klass)) {
                                /* CASTCLASS FIXME kill this huge slice of duplicated code*/
@@ -8883,7 +9129,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        *sp++ = mono_emit_method_call (cfg, mono_castclass, args, NULL);
                                        ip += 5;
                                        inline_costs += 2;
-                               } else if (!context_used && (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+                               } else if (!context_used && (mono_class_is_marshalbyref (klass) || klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
                                        MonoMethod *mono_castclass;
                                        MonoInst *iargs [1];
                                        int costs;
@@ -8945,8 +9191,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                        mono_save_token_info (cfg, image, token, klass);
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        if (generic_class_is_reference_type (cfg, klass)) {
                                *sp++ = val;
@@ -9033,6 +9278,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        }
 
                        *sp++ = handle_box (cfg, val, klass, context_used);
+                       bblock = cfg->cbb;
 
                        CHECK_CFG_EXCEPTION;
                        ip += 5;
@@ -9049,8 +9295,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                        mono_save_token_info (cfg, image, token, klass);
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        if (mono_class_is_nullable (klass)) {
                                MonoInst *val;
@@ -9155,8 +9400,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                is_instance = FALSE;
                        }
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        /* INSTANCE CASE */
 
@@ -9164,7 +9408,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (op == CEE_STFLD) {
                                if (target_type_is_incompatible (cfg, field->type, sp [1]))
                                        UNVERIFIED;
-                               if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
+#ifndef DISABLE_REMOTING
+                               if ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class) {
                                        MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
                                        MonoInst *iargs [5];
 
@@ -9190,7 +9435,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        } else {
                                                mono_emit_method_call (cfg, stfld_wrapper, iargs, NULL);
                                        }
-                               } else {
+                               } else
+#endif
+                               {
                                        MonoInst *store;
 
                                        MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
@@ -9198,8 +9445,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        if (mini_is_gsharedvt_klass (cfg, klass)) {
                                                MonoInst *offset_ins;
 
-                                               if (cfg->generic_sharing_context)
-                                                       context_used = mono_class_check_context_used (klass);
+                                               context_used = mini_class_check_context_used (cfg, klass);
 
                                                offset_ins = emit_get_rgctx_field (cfg, context_used, field, MONO_RGCTX_INFO_FIELD_OFFSET);
                                                dreg = alloc_ireg_mp (cfg);
@@ -9229,7 +9475,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                break;
                        }
 
-                       if (is_instance && ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class)) {
+#ifndef DISABLE_REMOTING
+                       if (is_instance && ((mono_class_is_marshalbyref (klass) && !MONO_CHECK_THIS (sp [0])) || mono_class_is_contextbound (klass) || klass == mono_defaults.marshalbyrefobject_class)) {
                                MonoMethod *wrapper = (op == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type); 
                                MonoInst *iargs [4];
 
@@ -9255,7 +9502,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        ins = mono_emit_method_call (cfg, wrapper, iargs, NULL);
                                        *sp++ = ins;
                                }
-                       } else if (is_instance) {
+                       } else 
+#endif
+                       if (is_instance) {
                                if (sp [0]->type == STACK_VTYPE) {
                                        MonoInst *var;
 
@@ -9336,8 +9585,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        GENERIC_SHARING_FAILURE (op);
 #endif
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        ftype = mono_field_get_type (field);
 
@@ -9648,8 +9896,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        klass = mini_get_class (method, token, generic_context);
                        CHECK_TYPELOAD (klass);
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        if (sp [0]->type == STACK_I8 || (SIZEOF_VOID_P == 8 && sp [0]->type == STACK_PTR)) {
                                MONO_INST_NEW (cfg, ins, OP_LCONV_TO_I4);
@@ -9663,21 +9910,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (context_used) {
                                MonoInst *args [3];
                                MonoClass *array_class = mono_array_class_get (klass, 1);
-                               /* FIXME: we cannot get a managed
-                                  allocator because we can't get the
-                                  open generic class's vtable.  We
-                                  have the same problem in
-                                  handle_alloc().  This
-                                  needs to be solved so that we can
-                                  have managed allocs of shared
-                                  generic classes. */
-                               /*
-                               MonoVTable *array_class_vtable = mono_class_vtable (cfg->domain, array_class);
-                               MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class_vtable, 1);
-                               */
-                               MonoMethod *managed_alloc = NULL;
+                               MonoMethod *managed_alloc = mono_gc_get_managed_array_allocator (array_class);
 
-                               /* FIXME: Decompose later to help abcrem */
+                               /* FIXME: Use OP_NEWARR and decompose later to help abcrem */
 
                                /* vtable */
                                args [0] = emit_get_rgctx_klass (cfg, context_used,
@@ -9915,8 +10150,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_TYPELOAD (klass);
                        mono_class_init (klass);
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        // FIXME:
                        src_var = get_vreg_to_inst (cfg, sp [0]->dreg);
@@ -9956,8 +10190,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_TYPELOAD (klass);
                        mono_class_init (klass);
 
-                       if (cfg->generic_sharing_context)
-                               context_used = mono_class_check_context_used (klass);
+                       context_used = mini_class_check_context_used (cfg, klass);
 
                        loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
                        EMIT_NEW_TEMPLOADA (cfg, addr, loc->inst_c0);
@@ -10028,11 +10261,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        if (mono_type_get_type (handle) == MONO_TYPE_CLASS)
                                                context_used = 0;
                                        else
-                                               context_used = mono_class_check_context_used (mono_class_from_mono_type (handle));
+                                               context_used = mini_class_check_context_used (cfg, mono_class_from_mono_type (handle));
                                } else if (handle_class == mono_defaults.fieldhandle_class)
-                                       context_used = mono_class_check_context_used (((MonoClassField*)handle)->parent);
+                                       context_used = mini_class_check_context_used (cfg, ((MonoClassField*)handle)->parent);
                                else if (handle_class == mono_defaults.methodhandle_class)
-                                       context_used = mono_method_check_context_used (handle);
+                                       context_used = mini_method_check_context_used (cfg, handle);
                                else
                                        g_assert_not_reached ();
                        }
@@ -10043,10 +10276,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                MonoInst *addr, *vtvar, *iargs [3];
                                int method_context_used;
 
-                               if (cfg->generic_sharing_context)
-                                       method_context_used = mono_method_check_context_used (method);
-                               else
-                                       method_context_used = 0;
+                               method_context_used = mini_method_check_context_used (cfg, method);
 
                                vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL); 
 
@@ -10307,23 +10537,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                token = read32 (ip + 2);
 
                                ptr = mono_method_get_wrapper_data (method, token);
-                               if (cfg->compile_aot && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
-                                       MonoJitICallInfo *callinfo;
-                                       const char *icall_name;
-
-                                       icall_name = method->name + strlen ("__icall_wrapper_");
-                                       g_assert (icall_name);
-                                       callinfo = mono_find_jit_icall_by_name (icall_name);
-                                       g_assert (callinfo);
-                                               
-                                       if (ptr == callinfo->func) {
-                                               /* Will be transformed into an AOTCONST later */
-                                               EMIT_NEW_PCONST (cfg, ins, ptr);
-                                               *sp++ = ins;
-                                               ip += 6;
-                                               break;
-                                       }
-                               }
                                /* FIXME: Generalize this */
                                if (cfg->compile_aot && ptr == mono_thread_interruption_request_flag ()) {
                                        EMIT_NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
@@ -10339,6 +10552,23 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                cfg->disable_aot = 1;
                                break;
                        }
+                       case CEE_MONO_JIT_ICALL_ADDR: {
+                               MonoJitICallInfo *callinfo;
+                               gpointer ptr;
+
+                               CHECK_STACK_OVF (1);
+                               CHECK_OPSIZE (6);
+                               token = read32 (ip + 2);
+
+                               ptr = mono_method_get_wrapper_data (method, token);
+                               callinfo = mono_find_jit_icall_by_addr (ptr);
+                               g_assert (callinfo);
+                               EMIT_NEW_JIT_ICALL_ADDRCONST (cfg, ins, (char*)callinfo->name);
+                               *sp++ = ins;
+                               ip += 6;
+                               inline_costs += 10 * num_calls++;
+                               break;
+                       }
                        case CEE_MONO_ICALL_ADDR: {
                                MonoMethod *cmethod;
                                gpointer ptr;
@@ -10698,8 +10928,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                                mono_save_token_info (cfg, image, n, cmethod);
 
-                               if (cfg->generic_sharing_context)
-                                       context_used = mono_method_check_context_used (cmethod);
+                               context_used = mini_method_check_context_used (cfg, cmethod);
 
                                cil_method = cmethod;
                                if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
@@ -10721,14 +10950,13 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        if (ctor_method && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
                                                MonoInst *target_ins;
                                                MonoMethod *invoke;
-                                               int invoke_context_used = 0;
+                                               int invoke_context_used;
 
                                                invoke = mono_get_delegate_invoke (ctor_method->klass);
                                                if (!invoke || !mono_method_signature (invoke))
                                                        LOAD_ERROR;
 
-                                               if (cfg->generic_sharing_context)
-                                                       invoke_context_used = mono_method_check_context_used (invoke);
+                                               invoke_context_used = mini_method_check_context_used (cfg, invoke);
 
                                                target_ins = sp [-1];
 
@@ -10771,8 +10999,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        case CEE_LDVIRTFTN: {
                                MonoInst *args [2];
 
-                               GSHAREDVT_FAILURE (*ip);
-
                                CHECK_STACK (1);
                                CHECK_OPSIZE (6);
                                n = read32 (ip + 2);
@@ -10781,8 +11007,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        LOAD_ERROR;
                                mono_class_init (cmethod->klass);
  
-                               if (cfg->generic_sharing_context)
-                                       context_used = mono_method_check_context_used (cmethod);
+                               context_used = mini_method_check_context_used (cfg, cmethod);
 
                                if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
                                        if (check_linkdemand (cfg, method, cmethod))
@@ -10967,10 +11192,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        case CEE_CONSTRAINED_:
                                CHECK_OPSIZE (6);
                                token = read32 (ip + 2);
-                               if (method->wrapper_type != MONO_WRAPPER_NONE)
-                                       constrained_call =  (MonoClass *)mono_method_get_wrapper_data (method, token);
-                               else
-                                       constrained_call = mono_class_get_full (image, token, generic_context);
+                               constrained_call = mini_get_class (method, token, generic_context);
                                CHECK_TYPELOAD (constrained_call);
                                ip += 6;
                                break;
@@ -11297,8 +11519,6 @@ store_membase_reg_to_store_membase_imm (int opcode)
        return -1;
 }              
 
-#endif /* DISABLE_JIT */
-
 int
 mono_op_to_op_imm (int opcode)
 {
@@ -11754,8 +11974,6 @@ mono_op_to_op_imm_noemul (int opcode)
        }
 }
 
-#ifndef DISABLE_JIT
-
 /**
  * mono_handle_global_vregs:
  *
@@ -12244,6 +12462,7 @@ mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
 
                                        ins->dreg = alloc_dreg (cfg, stacktypes [regtype]);
 
+#if SIZEOF_REGISTER != 8
                                        if (regtype == 'l') {
                                                NEW_STORE_MEMBASE (cfg, store_ins, OP_STOREI4_MEMBASE_REG, var->inst_basereg, var->inst_offset + MINI_LS_WORD_OFFSET, ins->dreg + 1);
                                                mono_bblock_insert_after_ins (bb, ins, store_ins);
@@ -12251,7 +12470,9 @@ mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
                                                mono_bblock_insert_after_ins (bb, ins, store_ins);
                                                def_ins = store_ins;
                                        }
-                                       else {
+                                       else
+#endif
+                                       {
                                                g_assert (store_opcode != OP_STOREV_MEMBASE);
 
                                                /* Try to fuse the store into the instruction itself */
@@ -12410,6 +12631,7 @@ mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
                                                sregs [srcindex] = sreg;
                                                //mono_inst_set_src_registers (ins, sregs);
 
+#if SIZEOF_REGISTER != 8
                                                if (regtype == 'l') {
                                                        NEW_LOAD_MEMBASE (cfg, load_ins, OP_LOADI4_MEMBASE, sreg + 2, var->inst_basereg, var->inst_offset + MINI_MS_WORD_OFFSET);
                                                        mono_bblock_insert_before_ins (bb, ins, load_ins);
@@ -12417,7 +12639,9 @@ mono_spill_global_vars (MonoCompile *cfg, gboolean *need_local_opts)
                                                        mono_bblock_insert_before_ins (bb, ins, load_ins);
                                                        use_ins = load_ins;
                                                }
-                                               else {
+                                               else
+#endif
+                                               {
 #if SIZEOF_REGISTER == 4
                                                        g_assert (load_opcode != OP_LOADI8_MEMBASE);
 #endif