Contributed under the terms of the MIT/X11 license by
[mono.git] / mono / mini / mini-trampolines.c
index 3ef426d26bf163aeb20b6a7dbab17e65a9fd775a..a7b102f50b3c733c335ab69003239dc5c8397f9e 100644 (file)
@@ -22,7 +22,6 @@
 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
 
 static GHashTable *class_init_hash_addr = NULL;
-static GHashTable *delegate_trampoline_hash_addr = NULL;
 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
 
@@ -31,12 +30,20 @@ static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
 static CRITICAL_SECTION trampolines_mutex;
 
 static gpointer
-get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
+get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
 {
-       if (mono_aot_only)
-               return mono_aot_get_unbox_trampoline (m);
-       else
+       if (mono_aot_only) {
+               if (need_rgctx_tramp)
+                       /* 
+                        * The unbox trampolines call the method directly, so need to add
+                        * an rgctx tramp before them.
+                        */
+                       return mono_create_static_rgctx_trampoline (m, mono_aot_get_unbox_trampoline (m));
+               else
+                       return mono_aot_get_unbox_trampoline (m);
+       } else {
                return mono_arch_get_unbox_trampoline (gsctx, m, addr);
+       }
 }
 
 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
@@ -86,12 +93,33 @@ mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
 
        return res;
 }
+#else
+gpointer
+mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
+{
+       /* 
+        * This shouldn't happen as all arches which support generic sharing support
+        * static rgctx trampolines as well.
+        */
+       g_assert_not_reached ();
+}
 #endif
 
+gpointer*
+mono_get_vcall_slot_addr (guint8* code, mgreg_t *regs)
+{
+       gpointer vt;
+       int displacement;
+       vt = mono_arch_get_vcall_slot (code, regs, &displacement);
+       if (!vt)
+               return NULL;
+       return (gpointer*)((char*)vt + displacement);
+}
+
 #ifdef MONO_ARCH_HAVE_IMT
 
 static gpointer*
-mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
+mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp)
 {
        MonoGenericSharingContext *gsctx = mono_get_generic_context_from_code (code);
        MonoObject *this_argument = mono_arch_find_this_argument (regs, method, gsctx);
@@ -138,6 +166,9 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *co
                                impl = mono_class_get_vtable_entry (vt->klass, interface_offset + imt_method->slot);
                        }
 
+                       if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
+                               *need_rgctx_tramp = TRUE;
+
                        *impl_method = impl;
 #if DEBUG_IMT
                printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
@@ -169,7 +200,7 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *co
  *   This trampoline handles calls from JITted code.
  */
 gpointer
-mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
+mono_magic_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 {
        gpointer addr;
        gpointer *vtable_slot;
@@ -180,10 +211,9 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
        gboolean proxy = FALSE;
        gboolean need_rgctx_tramp = FALSE;
 
-#if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
        if (m == MONO_FAKE_VTABLE_METHOD) {
                int displacement;
-               MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
+               MonoVTable *vt = mono_arch_get_vcall_slot (code, regs, &displacement);
                if (!vt) {
                        int i;
                        MonoJitInfo *ji;
@@ -204,8 +234,10 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 
                        /* Avoid loading metadata or creating a generic vtable if possible */
                        addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, displacement);
+                       if (addr)
+                               addr = mono_create_ftnptr (mono_domain_get (), addr);
                        if (addr && !vt->klass->valuetype) {
-                               vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
+                               vtable_slot = mono_get_vcall_slot_addr (code, regs);
                                if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
                                        *vtable_slot = mono_get_addr_from_ftnptr (addr);
                                }
@@ -214,6 +246,9 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                        }
 
                        m = mono_class_get_vtable_entry (vt->klass, displacement);
+                       if (mono_method_needs_static_rgctx_invoke (m, FALSE))
+                               need_rgctx_tramp = TRUE;
+
                        /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
                } else {
                        /* We got here from an interface method: redirect to IMT handling */
@@ -221,7 +256,7 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                        /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
                }
        }
-#endif
+
        /* this is the IMT trampoline */
 #ifdef MONO_ARCH_HAVE_IMT
        if (m == MONO_FAKE_IMT_METHOD) {
@@ -232,19 +267,19 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
                 * needs the signature to be able to find the this argument
                 */
-               m = mono_arch_find_imt_method ((gpointer*)regs, code);
-               vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
+               m = mono_arch_find_imt_method (regs, code);
+               vtable_slot = mono_get_vcall_slot_addr (code, regs);
                g_assert (vtable_slot);
 
                gsctx = mono_get_generic_context_from_code (code);
-               this_arg = mono_arch_find_this_argument ((gpointer*)regs, m, gsctx);
+               this_arg = mono_arch_find_this_argument (regs, m, gsctx);
 
                if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
                        /* Use the slow path for now */
                        proxy = TRUE;
                    m = mono_object_get_virtual_method (this_arg, m);
                } else {
-                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
+                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp);
                        /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
                         * to be called, so we compile it and go ahead as usual.
                         */
@@ -253,11 +288,7 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                                /* Generic virtual method */
                                generic_virtual = m;
                                m = impl_method;
-#ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
                                need_rgctx_tramp = TRUE;
-#else
-                               m = mono_marshal_get_static_rgctx_invoke (m);
-#endif
                        } else {
                                m = impl_method;
                        }
@@ -280,7 +311,7 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                        g_assert (!m->klass->generic_container);
 
 #ifdef MONO_ARCH_HAVE_IMT
-               generic_virtual = mono_arch_find_imt_method ((gpointer*)regs, code);
+               generic_virtual = mono_arch_find_imt_method (regs, code);
 #endif
                if (generic_virtual) {
                        g_assert (generic_virtual->is_inflated);
@@ -288,12 +319,8 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                }
 
                m = mono_class_inflate_generic_method (declaring, &context);
-#ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
-               need_rgctx_tramp = TRUE;
-#else
                /* FIXME: only do this if the method is sharable */
-               m = mono_marshal_get_static_rgctx_invoke (m);
-#endif
+               need_rgctx_tramp = TRUE;
        } else if ((context_used = mono_method_check_context_used (m))) {
                MonoClass *klass = NULL;
                MonoMethod *actual_method = NULL;
@@ -307,7 +334,7 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 
                if (m->is_inflated && mono_method_get_context (m)->method_inst) {
 #ifdef MONO_ARCH_RGCTX_REG
-                       MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable ((gpointer*)regs, code);
+                       MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable (regs, code);
 
                        klass = mrgctx->class_vtable->klass;
                        method_inst = mrgctx->method_inst;
@@ -316,7 +343,7 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 #endif
                } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
 #ifdef MONO_ARCH_RGCTX_REG
-                       MonoVTable *vtable = mono_arch_find_static_call_vtable ((gpointer*)regs, code);
+                       MonoVTable *vtable = mono_arch_find_static_call_vtable (regs, code);
 
                        klass = vtable->klass;
 #else
@@ -324,11 +351,11 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 #endif
                } else {
 #ifdef MONO_ARCH_HAVE_IMT
-                       MonoObject *this_argument = mono_arch_find_this_argument ((gpointer*)regs, m,
+                       MonoObject *this_argument = mono_arch_find_this_argument (regs, m,
                                mono_get_generic_context_from_code (code));
 
                        vt = this_argument->vtable;
-                       vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
+                       vtable_slot = mono_get_vcall_slot_addr (code, regs);
 
                        g_assert (this_argument->vtable->klass->inited);
                        //mono_class_init (this_argument->vtable->klass);
@@ -397,27 +424,25 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 
        /* Calls made through delegates on platforms without delegate trampolines */
        if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
-               m = mono_marshal_get_static_rgctx_invoke (m);
+               need_rgctx_tramp = TRUE;
 
        addr = mono_compile_method (m);
        g_assert (addr);
 
        mono_debugger_trampoline_compiled (m, addr);
 
-#ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
        if (need_rgctx_tramp)
                addr = mono_create_static_rgctx_trampoline (m, addr);
-#endif
 
        if (generic_virtual) {
                int displacement;
-               MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
+               MonoVTable *vt = mono_arch_get_vcall_slot (code, regs, &displacement);
 
-               vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
+               vtable_slot = mono_get_vcall_slot_addr (code, regs);
                g_assert (vtable_slot);
 
                if (vt->klass->valuetype)
-                       addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);                       
+                       addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
 
                mono_method_add_generic_virtual_invocation (mono_domain_get (), 
                                                                                                        vt, vtable_slot,
@@ -454,16 +479,16 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                return addr;
        }
 
-       vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
+       vtable_slot = mono_get_vcall_slot_addr (code, regs);
 
        if (vtable_slot) {
                if (m->klass->valuetype)
-                       addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr);
+                       addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
                g_assert (*vtable_slot);
 
                if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
 #ifdef MONO_ARCH_HAVE_IMT
-                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
+                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, NULL, &need_rgctx_tramp);
 #endif
                        *vtable_slot = mono_get_addr_from_ftnptr (addr);
                }
@@ -472,7 +497,7 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                guint8 *plt_entry = mono_aot_get_plt_entry (code);
 
                if (plt_entry) {
-                       mono_arch_patch_plt_entry (plt_entry, addr);
+                       mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
                } else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
                        mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
                        if (generic_shared) {
@@ -498,9 +523,75 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 
        return addr;
 }
+#ifdef ENABLE_LLVM
+/*
+ * mono_llvm_vcall_trampoline:
+ *
+ *   This trampoline handles virtual calls when using LLVM.
+ */
+static gpointer
+mono_llvm_vcall_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
+{
+       MonoObject *this;
+       gpointer addr;
+       MonoVTable *vt;
+       gpointer *vtable_slot;
+       gboolean proxy = FALSE;
+       gboolean need_rgctx_tramp = FALSE;
+
+       /* 
+        * We have the method which is called, we need to obtain the vtable slot without
+        * disassembly which is impossible with LLVM.
+        * So we use the this argument.
+        */
+       this = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
+       g_assert (this);
+
+       vt = this->vtable;
+
+       g_assert (!m->is_generic);
+
+       /* This is a simplified version of mono_magic_trampoline () */
+       /* FIXME: Avoid code duplication */
+
+       if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
+               MonoJitInfo *ji;
+
+               if (code)
+                       ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
+               else
+                       ji = NULL;
+
+               /* Avoid recursion */
+               if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
+                       m = mono_marshal_get_synchronized_wrapper (m);
+       }
+
+       addr = mono_compile_method (m);
+       g_assert (addr);
+
+       if (m->klass->valuetype)
+               addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
+
+       vtable_slot = &(vt->vtable [mono_method_get_vtable_slot (m)]);
+       g_assert (*vtable_slot);
+
+       if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
+#ifdef MONO_ARCH_HAVE_IMT
+               vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, NULL, &need_rgctx_tramp);
+#endif
+               *vtable_slot = mono_get_addr_from_ftnptr (addr);
+         }
+
+       mono_debugger_trampoline_compiled (m, addr);
+
+       return addr;
+}
+#endif
 
 gpointer
-mono_generic_virtual_remoting_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
+mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
 {
        MonoGenericContext context = { NULL, NULL };
        MonoMethod *imt_method, *declaring;
@@ -519,7 +610,7 @@ mono_generic_virtual_remoting_trampoline (gssize *regs, guint8 *code, MonoMethod
                g_assert (!m->klass->generic_container);
 
 #ifdef MONO_ARCH_HAVE_IMT
-       imt_method = mono_arch_find_imt_method ((gpointer*)regs, code);
+       imt_method = mono_arch_find_imt_method (regs, code);
        if (imt_method->is_inflated)
                context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
 #endif
@@ -540,9 +631,9 @@ mono_generic_virtual_remoting_trampoline (gssize *regs, guint8 *code, MonoMethod
  *   This trampoline handles calls made from AOT code. We try to bypass the 
  * normal JIT compilation logic to avoid loading the metadata for the method.
  */
-#ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
+#ifdef MONO_ARCH_AOT_SUPPORTED
 gpointer
-mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
+mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info, 
                                         guint8* tramp)
 {
        MonoImage *image;
@@ -552,6 +643,7 @@ mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
        gpointer *vtable_slot;
        gboolean is_got_entry;
        guint8 *plt_entry;
+       gboolean need_rgctx_tramp = FALSE;
 
        image = *(gpointer*)(gpointer)token_info;
        token_info += sizeof (gpointer);
@@ -566,14 +658,16 @@ mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
                return mono_magic_trampoline (regs, code, method, tramp);
        }
 
-       vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
+       addr = mono_create_ftnptr (mono_domain_get (), addr);
+
+       vtable_slot = mono_get_vcall_slot_addr (code, regs);
        g_assert (!vtable_slot);
 
        /* This is a normal call through a PLT entry */
        plt_entry = mono_aot_get_plt_entry (code);
        g_assert (plt_entry);
 
-       mono_arch_patch_plt_entry (plt_entry, addr);
+       mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
 
        is_got_entry = FALSE;
 
@@ -587,7 +681,7 @@ mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
 #ifdef MONO_ARCH_HAVE_IMT
                if (!method)
                        method = mono_get_method (image, token, NULL);
-               vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
+               vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, method, NULL, &need_rgctx_tramp);
 #endif
                *vtable_slot = addr;
        }
@@ -601,7 +695,7 @@ mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
  *   This trampoline handles calls made from AOT code through the PLT table.
  */
 gpointer
-mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
+mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *aot_module, 
                                                 guint8* tramp)
 {
        guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
@@ -617,14 +711,14 @@ mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module,
  * for the type, then patches the caller code so it is not called again.
  */
 void
-mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
+mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
 {
        guint8 *plt_entry = mono_aot_get_plt_entry (code);
 
        mono_runtime_class_init (vtable);
 
        if (plt_entry) {
-               mono_arch_nullify_plt_entry (plt_entry);
+               mono_arch_nullify_plt_entry (plt_entry, regs);
        } else {
                mono_arch_nullify_class_init_trampoline (code, regs);
        }
@@ -637,21 +731,20 @@ mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guin
  * for the type.
  */
 void
-mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
+mono_generic_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
 {
-       g_assert (!vtable->initialized);
-
        mono_runtime_class_init (vtable);
 }
 
 static gpointer
-mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
+mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, guint8 *tramp)
 {
 #ifdef MONO_ARCH_VTABLE_REG
        static gboolean inited = FALSE;
        static int num_lookups = 0;
        guint32 slot = GPOINTER_TO_UINT (data);
-       gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
+       mgreg_t *r = (mgreg_t*)regs;
+       gpointer arg = (gpointer)(gssize)r [MONO_ARCH_VTABLE_REG];
        guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
        gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
 
@@ -672,13 +765,13 @@ mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, gui
 }
 
 void
-mono_monitor_enter_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
+mono_monitor_enter_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
 {
        mono_monitor_enter (obj);
 }
 
 void
-mono_monitor_exit_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
+mono_monitor_exit_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
 {
        mono_monitor_exit (obj);
 }
@@ -692,7 +785,7 @@ mono_monitor_exit_trampoline (gssize *regs, guint8 *code, MonoObject *obj, guint
  * This is called once the first time a delegate is invoked, so it must be fast.
  */
 gpointer
-mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
+mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
 {
        MonoDomain *domain = mono_domain_get ();
        MonoDelegate *delegate;
@@ -737,13 +830,8 @@ mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guin
        if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
                method = mono_marshal_get_synchronized_wrapper (method);
 
-       if (method && mono_method_needs_static_rgctx_invoke (method, FALSE)) {
-#ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
+       if (method && mono_method_needs_static_rgctx_invoke (method, FALSE))
                need_rgctx_tramp = TRUE;
-#else
-               method = mono_marshal_get_static_rgctx_invoke (method);
-#endif
-       }
 
        /* 
         * If the called address is a trampoline, replace it with the compiled method so
@@ -826,6 +914,10 @@ mono_get_trampoline_func (MonoTrampolineType tramp_type)
                return mono_monitor_enter_trampoline;
        case MONO_TRAMPOLINE_MONITOR_EXIT:
                return mono_monitor_exit_trampoline;
+#ifdef ENABLE_LLVM
+       case MONO_TRAMPOLINE_LLVM_VCALL:
+               return mono_llvm_vcall_trampoline;
+#endif
        default:
                g_assert_not_reached ();
                return NULL;
@@ -856,6 +948,9 @@ mono_trampolines_init (void)
        mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
        mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
        mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
+#ifdef ENABLE_LLVM
+       mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
+#endif
 }
 
 void
@@ -863,8 +958,6 @@ mono_trampolines_cleanup (void)
 {
        if (class_init_hash_addr)
                g_hash_table_destroy (class_init_hash_addr);
-       if (delegate_trampoline_hash_addr)
-               g_hash_table_destroy (delegate_trampoline_hash_addr);
 
        DeleteCriticalSection (&trampolines_mutex);
 }
@@ -1025,7 +1118,6 @@ mono_create_jit_trampoline (MonoMethod *method)
        return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
 }
 
-#ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
 gpointer
 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
 {
@@ -1046,7 +1138,6 @@ mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
 
        return tramp;
 }      
-#endif
 
 gpointer
 mono_create_delegate_trampoline (MonoClass *klass)
@@ -1070,13 +1161,8 @@ mono_create_delegate_trampoline (MonoClass *klass)
 
        tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
        tramp_data [0] = invoke;
-       if (mono_aot_only) {
-               tramp_data [1] = NULL;
-               tramp_data [2] = NULL;
-       } else {
-               tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
-               tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
-       }
+       tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
+       tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
 
        ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
        g_assert (code_size);
@@ -1087,12 +1173,6 @@ mono_create_delegate_trampoline (MonoClass *klass)
                                                          klass, ptr);
        mono_domain_unlock (domain);
 
-       mono_trampolines_lock ();
-       if (!delegate_trampoline_hash_addr)
-               delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
-       g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
-       mono_trampolines_unlock ();
-
        return ptr;
 #else
        return NULL;
@@ -1192,6 +1272,43 @@ mono_create_monitor_exit_trampoline (void)
 #endif
        return code;
 }
+#ifdef ENABLE_LLVM
+/*
+ * mono_create_llvm_vcall_trampoline:
+ *
+ *  LLVM emits code for virtual calls which mono_get_vcall_slot is unable to
+ * decode, i.e. only the final branch address is available:
+ * mov <offset>(%rax), %rax
+ * <random code inserted by instruction scheduling>
+ * call *%rax
+ *
+ * To work around this problem, we don't use the common vtable trampoline when
+ * llvm is enabled. Instead, we use one trampoline per method.
+ */
+gpointer
+mono_create_llvm_vcall_trampoline (MonoMethod *method)
+{
+       MonoDomain *domain;
+       gpointer res;
+
+       domain = mono_domain_get ();
+
+       mono_domain_lock (domain);
+       res = g_hash_table_lookup (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method);
+       mono_domain_unlock (domain);
+       if (res)
+               return res;
+
+       res = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_LLVM_VCALL, domain, NULL);
+
+       mono_domain_lock (domain);
+       g_hash_table_insert (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method, res);
+       mono_domain_unlock (domain);
+
+       return res;
+}
+#endif
 
 MonoVTable*
 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
@@ -1207,20 +1324,6 @@ mono_find_class_init_trampoline_by_addr (gconstpointer addr)
        return res;
 }
 
-MonoClass*
-mono_find_delegate_trampoline_by_addr (gconstpointer addr)
-{
-       MonoClass *res;
-
-       mono_trampolines_lock ();
-       if (delegate_trampoline_hash_addr)
-               res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
-       else
-               res = NULL;
-       mono_trampolines_unlock ();
-       return res;
-}
-
 guint32
 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
 {