Pass signatures instead of methods to mono_arch_get_gsharedvt_call_info ().
[mono.git] / mono / mini / mini-trampolines.c
index 94a4f480ce3fb378f5d143c75cce637ea6577bb3..06525fec08564e1b83811010aa122083be058743 100644 (file)
@@ -1,4 +1,8 @@
-
+/*
+ * (C) 2003 Ximian, Inc.
+ * (C) 2003-2011 Novell, Inc.
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
+ */
 #include <config.h>
 #include <glib.h>
 
@@ -21,14 +25,14 @@ guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
 static GHashTable *class_init_hash_addr = NULL;
 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
-static guint32 trampoline_calls;
+static guint32 trampoline_calls, jit_trampolines, unbox_trampolines, static_rgctx_trampolines;
 
 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
 static CRITICAL_SECTION trampolines_mutex;
 
 static gpointer
-get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
+get_unbox_trampoline (MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
 {
        if (mono_aot_only) {
                if (need_rgctx_tramp)
@@ -40,11 +44,12 @@ get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer
                else
                        return mono_aot_get_unbox_trampoline (m);
        } else {
-               return mono_arch_get_unbox_trampoline (gsctx, m, addr);
+               unbox_trampolines ++;
+               return mono_arch_get_unbox_trampoline (m, addr);
        }
 }
 
-#ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
+#ifdef MONO_ARCH_GSHARED_SUPPORTED
 
 typedef struct {
        MonoMethod *m;
@@ -98,10 +103,7 @@ mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
        g_assert (((gpointer*)addr) [2] == 0);
 #endif
 
-       if (mini_method_get_context (m)->method_inst)
-               ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
-       else
-               ctx = mono_class_vtable (mono_domain_get (), m->klass);
+       ctx = mini_method_get_rgctx (m);
 
        domain = mono_domain_get ();
 
@@ -133,37 +135,40 @@ mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
        g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, info, res);
        mono_domain_unlock (domain);
 
+       static_rgctx_trampolines ++;
+
        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 ();
+       /* 
+        * 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
 
+/*
+ * Either IMPL_METHOD or AOT_ADDR will be set on return.
+ */
 static gpointer*
-mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp, gboolean *variance_used)
+#ifdef __GNUC__
+/*
+ * This works against problems when compiling with gcc 4.6 on arm. The 'then' part of
+ * this line gets executed, even when the condition is false:
+ *             if (impl && mono_method_needs_static_rgctx_invoke (impl, FALSE))
+ *                     *need_rgctx_tramp = TRUE;
+ */
+__attribute__ ((noinline))
+#endif
+mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp, gboolean *variance_used, gpointer *aot_addr)
 {
-       MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (method), regs, code);
+       MonoObject *this_argument = mono_arch_get_this_arg_from_call (regs, code);
        MonoVTable *vt = this_argument->vtable;
        int displacement = slot - ((gpointer*)vt);
 
@@ -175,6 +180,7 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *cod
                return slot;
        } else {
                MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
+               MonoMethod *impl;
                int interface_offset;
                int imt_slot = MONO_IMT_SIZE + displacement;
 
@@ -186,37 +192,52 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *cod
                }
                mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
 
-               if (impl_method) {
-                       MonoMethod *impl;
+               if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
+                       MonoGenericContext context = { NULL, NULL };
 
-                       if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
-                               MonoGenericContext context = { NULL, NULL };
+                       /* 
+                        * Generic virtual method, imt_method contains the inflated interface 
+                        * method, need to get the inflated impl method.
+                        */
+                       /* imt_method->slot might not be set */
+                       impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
 
-                               /* 
-                                * Generic virtual method, imt_method contains the inflated interface 
-                                * method, need to get the inflated impl method.
-                                */
-                               /* imt_method->slot might not be set */
-                               impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
-
-                               if (impl->klass->generic_class)
-                                       context.class_inst = impl->klass->generic_class->context.class_inst;
-                               context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
-                               impl = mono_class_inflate_generic_method (impl, &context);
-                       } else {
+                       if (impl->klass->generic_class)
+                               context.class_inst = impl->klass->generic_class->context.class_inst;
+                       context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
+                       impl = mono_class_inflate_generic_method (impl, &context);
+               } else {
+                       /* Avoid loading metadata or creating a generic vtable if possible */
+                       if (!vt->klass->valuetype)
+                               *aot_addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, interface_offset + mono_method_get_vtable_slot (imt_method));
+                       else
+                               *aot_addr = NULL;
+                       if (*aot_addr)
+                               impl = NULL;
+                       else
                                impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_vtable_slot (imt_method));
-                       }
+               }
+
+               if (impl && mono_method_needs_static_rgctx_invoke (impl, FALSE))
+                       *need_rgctx_tramp = TRUE;
+               if (impl && impl->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
+                       WrapperInfo *info = mono_marshal_get_wrapper_info (impl);
 
-                       if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
+                       if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER) {
+                               // FIXME: This needs a gsharedvt-out trampoline, since the caller uses the gsharedvt calling conv, but the
+                               // wrapper is a normal non-generic method.
                                *need_rgctx_tramp = TRUE;
+                               //g_assert_not_reached ();
+                       }
+               }
 
-                       *impl_method = impl;
+               *impl_method = impl;
 #if DEBUG_IMT
                printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
                                method->klass->name_space, method->klass->name, method->name, 
                                imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
 #endif
-               }
+
                g_assert (imt_slot < MONO_IMT_SIZE);
                if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
                        int slot = mono_method_get_vtable_index (imt_method);
@@ -240,6 +261,125 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *cod
 }
 #endif
 
+/*
+ * This is a super-ugly hack to fix bug #616463.
+ *
+ * The problem is that we don't always set is_generic for generic
+ * method definitions.  See the comment at the end of
+ * mono_class_inflate_generic_method_full_checked() in class.c.
+ */
+static gboolean
+is_generic_method_definition (MonoMethod *m)
+{
+       MonoGenericContext *context;
+       if (m->is_generic)
+               return TRUE;
+       if (!m->is_inflated)
+               return FALSE;
+
+       context = mono_method_get_context (m);
+       if (!context->method_inst)
+               return FALSE;
+       if (context->method_inst == mono_method_get_generic_container (((MonoMethodInflated*)m)->declaring)->context.method_inst)
+               return TRUE;
+       return FALSE;
+}
+static gboolean
+ji_is_gsharedvt (MonoJitInfo *ji)
+{
+       if (ji && ji->has_generic_jit_info && (mono_jit_info_get_generic_sharing_context (ji)->var_is_vt ||
+                                                                                  mono_jit_info_get_generic_sharing_context (ji)->mvar_is_vt))
+               return TRUE;
+       else
+               return FALSE;
+}
+
+/*
+ * mini_add_method_trampoline:
+ *
+ *   Add static rgctx/gsharedvt_in trampoline to M/COMPILED_METHOD if needed. Return the trampoline address, or
+ * COMPILED_METHOD if no trampoline is needed.
+ * ORIG_METHOD is the method the caller originally called i.e. an iface method, or NULL.
+ */
+gpointer
+mini_add_method_trampoline (MonoMethod *orig_method, MonoMethod *m, gpointer compiled_method, MonoJitInfo *caller_ji, gboolean add_static_rgctx_tramp)
+{
+       gpointer addr = compiled_method;
+       gboolean callee_gsharedvt, callee_array_helper;
+       MonoJitInfo *ji = 
+               mini_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (compiled_method), NULL);
+
+       // FIXME: This loads information from AOT
+       callee_gsharedvt = ji_is_gsharedvt (ji);
+
+       callee_array_helper = FALSE;
+       if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
+               WrapperInfo *info = mono_marshal_get_wrapper_info (m);
+
+               /*
+                * generic array helpers.
+                * Have to replace the wrappers with the original generic instances.
+                */
+               if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER) {
+                       callee_array_helper = TRUE;
+                       m = info->d.generic_array_helper.method;
+               }
+       } else if (m->wrapper_type == MONO_WRAPPER_UNKNOWN) {
+               WrapperInfo *info = mono_marshal_get_wrapper_info (m);
+
+               /* Same for synchronized inner wrappers */
+               if (info && info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
+                       m = info->d.synchronized_inner.method;
+               }
+       }
+
+       if (!orig_method)
+               orig_method = m;
+
+       if (callee_gsharedvt)
+               g_assert (m->is_inflated);
+
+       addr = compiled_method;
+
+       if (callee_gsharedvt && mini_is_gsharedvt_variable_signature (mono_method_signature (ji->method))) {
+               static gpointer tramp_addr;
+               gpointer info;
+               MonoMethod *wrapper;
+               MonoGenericSharingContext *gsctx;
+               MonoMethodSignature *sig, *gsig;
+
+               /* Here m is a generic instance, while ji->method is the gsharedvt method implementing it */
+
+               /* Call from normal/gshared code to gsharedvt code with variable signature */
+               gsctx = mono_jit_info_get_generic_sharing_context (ji);
+
+               sig = mono_method_signature (m);
+               gsig = mono_method_signature (ji->method); 
+
+               info = mono_arch_get_gsharedvt_call_info (compiled_method, sig, gsig, gsctx, TRUE, -1);
+
+               if (!tramp_addr) {
+                       wrapper = mono_marshal_get_gsharedvt_in_wrapper ();
+                       addr = mono_compile_method (wrapper);
+                       mono_memory_barrier ();
+                       tramp_addr = addr;
+               }
+               addr = tramp_addr;
+
+               if (mono_aot_only)
+                       addr = mono_aot_get_gsharedvt_arg_trampoline (info, addr);
+               else
+                       addr = mono_arch_get_gsharedvt_arg_trampoline (mono_domain_get (), info, addr);
+
+               //printf ("IN: %s\n", mono_method_full_name (m, TRUE));
+       }
+
+       if (add_static_rgctx_tramp && !callee_array_helper)
+               addr = mono_create_static_rgctx_trampoline (m, addr);
+
+       return addr;
+}
+
 /**
  * common_call_trampoline:
  *
@@ -247,71 +387,26 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *cod
  * from JITted and LLVM compiled code.
  */
 static gpointer
-common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp, MonoVTable *vt, gpointer *vtable_slot, gboolean need_rgctx_tramp)
+common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tramp, MonoVTable *vt, gpointer *vtable_slot, gboolean need_rgctx_tramp)
 {
        gpointer addr, compiled_method;
        gboolean generic_shared = FALSE;
-       MonoMethod *m;
        MonoMethod *declaring = NULL;
-       MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
+       MonoMethod *generic_virtual = NULL, *variant_iface = NULL, *orig_method = NULL;
        int context_used;
-       gboolean proxy = FALSE, variance_used = FALSE;
-       gpointer *orig_vtable_slot;
+       gboolean virtual, variance_used = FALSE;
+       gpointer *orig_vtable_slot, *vtable_slot_to_patch = NULL;
        MonoJitInfo *ji = NULL;
 
-       m = arg;
+       virtual = (gpointer)vtable_slot > (gpointer)vt;
 
        orig_vtable_slot = vtable_slot;
+       vtable_slot_to_patch = vtable_slot;
 
-       if (m == MONO_FAKE_VTABLE_METHOD) {
-               int displacement;
-               if (!vt) {
-                       int i;
-                       MonoJitInfo *ji;
-
-                       ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
-                       if (ji)
-                               printf ("Caller: %s\n", mono_method_full_name (ji->method, TRUE));
-                       /* Print some debug info */
-                       for (i = 0; i < 32; ++i)
-                               printf ("0x%x ", code [-32 + i]);
-                       printf ("\n");
-                       g_assert (vt);
-               }
-               displacement = (guint8*)vtable_slot - (guint8*)vt;
-               if (displacement > 0) {
-                       int slot = (displacement - G_STRUCT_OFFSET (MonoVTable, vtable)) / sizeof (gpointer);
-                       g_assert (slot >= 0);
-
-                       /* Avoid loading metadata or creating a generic vtable if possible */
-                       addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, slot);
-                       if (addr)
-                               addr = mono_create_ftnptr (mono_domain_get (), addr);
-                       if (addr && !vt->klass->valuetype) {
-                               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);
-                               }
-
-                               return addr;
-                       }
-
-                       m = mono_class_get_vtable_entry (vt->klass, slot);
-                       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 */
-                       m = MONO_FAKE_IMT_METHOD;
-                       /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
-               }
-       }
-
-       /* this is the IMT trampoline */
 #ifdef MONO_ARCH_HAVE_IMT
-       if (m == MONO_FAKE_IMT_METHOD) {
-               MonoMethod *impl_method;
+       /* IMT call */
+       if (vt && (gpointer)vtable_slot < (gpointer)vt) {
+               MonoMethod *impl_method = NULL;
                MonoObject *this_arg;
 
                /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
@@ -321,14 +416,19 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
                vtable_slot = orig_vtable_slot;
                g_assert (vtable_slot);
 
-               this_arg = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
+               orig_method = m;
+
+               this_arg = mono_arch_get_this_arg_from_call (regs, code);
 
                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);
+                       vtable_slot_to_patch = NULL;
                } else {
-                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp, &variance_used);
+                       addr = NULL;
+                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp, &variance_used, &addr);
+                       /* This is the vcall slot which gets called through the IMT thunk */
+                       vtable_slot_to_patch = vtable_slot;
                        /* 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.
                         */
@@ -340,26 +440,27 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
                        } else if (variance_used && mono_class_has_variant_generic_params (m->klass)) {
                                variant_iface = m;
                        }
-                       m = impl_method;
-               }
-       }
-#endif
 
-#ifdef MONO_ARCH_LLVM_SUPPORTED
-       if (!vtable_slot && code && !need_rgctx_tramp && mono_method_needs_static_rgctx_invoke (m, FALSE)) {
-               /*
-                * Call this only if the called method is shared, cause it is slow/loads a lot of
-                * data in AOT.
-                */
-               ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
-               if (ji && ji->from_llvm) {
-                       /* LLVM can't pass an rgctx arg */
-                       need_rgctx_tramp = TRUE;
+                       if (addr && !generic_virtual && !variant_iface) {
+                               /*
+                                * We found AOT compiled code for the method, skip the rest.
+                                */
+                               if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
+                                       *vtable_slot = addr;
+
+                               return mono_create_ftnptr (mono_domain_get (), addr);
+                       }
+
+                       m = impl_method;
                }
        }
 #endif
 
-       if (m->is_generic) {
+       /*
+        * The virtual check is needed because is_generic_method_definition (m) could
+        * return TRUE for methods used in IMT calls too.
+        */
+       if (virtual && is_generic_method_definition (m)) {
                MonoGenericContext context = { NULL, NULL };
                MonoMethod *declaring;
 
@@ -414,7 +515,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
 #endif
                } else {
 #ifdef MONO_ARCH_HAVE_IMT
-                       MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
+                       MonoObject *this_argument = mono_arch_get_this_arg_from_call (regs, code);
 
                        vt = this_argument->vtable;
                        vtable_slot = orig_vtable_slot;
@@ -422,8 +523,10 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
                        g_assert (this_argument->vtable->klass->inited);
                        //mono_class_init (this_argument->vtable->klass);
 
-                       if (!vtable_slot)
+                       if (!vtable_slot) {
+                               mono_class_setup_supertypes (this_argument->vtable->klass);
                                klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
+                       }
 #else
                        NOT_IMPLEMENTED;
 #endif
@@ -473,16 +576,8 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
        }
 
        if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
-               MonoJitInfo *ji;
-
-               if (code)
-                       ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
-               else
-                       ji = NULL;
-
-               /* Avoid recursion */
-               if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
-                       m = mono_marshal_get_synchronized_wrapper (m);
+               m = mono_marshal_get_synchronized_wrapper (m);
+               need_rgctx_tramp = FALSE;
        }
 
        /* Calls made through delegates on platforms without delegate trampolines */
@@ -494,8 +589,10 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
 
        mono_debugger_trampoline_compiled (code, m, addr);
 
-       if (need_rgctx_tramp)
-               addr = mono_create_static_rgctx_trampoline (m, addr);
+       // FIXME: Is this needed ?
+       if (!ji)
+               ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
+       addr = mini_add_method_trampoline (orig_method, m, compiled_method, ji, need_rgctx_tramp);
 
        if (generic_virtual || variant_iface) {
                MonoMethod *target = generic_virtual ? generic_virtual : variant_iface;
@@ -504,7 +601,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
                g_assert (vtable_slot);
 
                if (vt->klass->valuetype) /*FIXME is this required variant iface?*/
-                       addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
+                       addr = get_unbox_trampoline (m, addr, need_rgctx_tramp);
 
                mono_method_add_generic_virtual_invocation (mono_domain_get (), 
                                                                                                        vt, vtable_slot,
@@ -544,41 +641,56 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
        vtable_slot = orig_vtable_slot;
 
        if (vtable_slot) {
-               gboolean variance_used = FALSE;
                if (m->klass->valuetype)
-                       addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
-               g_assert (*vtable_slot);
+                       addr = get_unbox_trampoline (m, addr, need_rgctx_tramp);
 
-               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, &variance_used);
-#endif
-                       *vtable_slot = mono_get_addr_from_ftnptr (addr);
+               if (vtable_slot_to_patch && (mono_aot_is_got_entry (code, (guint8*)vtable_slot_to_patch) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot_to_patch))) {
+                       g_assert (*vtable_slot_to_patch);
+                       *vtable_slot_to_patch = mono_get_addr_from_ftnptr (addr);
                }
        }
        else {
                guint8 *plt_entry = mono_aot_get_plt_entry (code);
+               gboolean no_patch = FALSE;
+               MonoJitInfo *target_ji;
 
                if (plt_entry) {
+                       if (generic_shared) {
+                               target_ji =
+                                       mini_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (compiled_method), NULL);
+                               if (!ji)
+                                       ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
+
+                               if (ji && target_ji && generic_shared && ji->has_generic_jit_info && !target_ji->has_generic_jit_info) {
+                                       no_patch = TRUE;
+                               }
+                       }
+                       if (!no_patch)
                                mono_aot_patch_plt_entry (plt_entry, NULL, regs, addr);
                } else {
                        if (generic_shared) {
                                if (m->wrapper_type != MONO_WRAPPER_NONE)
                                        m = mono_marshal_method_from_wrapper (m);
-                               g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
+                               //g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
                        }
 
                        /* Patch calling code */
-                       {
-                               MonoJitInfo *target_ji = 
-                                       mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (compiled_method));
-
-                               if (!ji)
-                                       ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
+                       target_ji =
+                               mini_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (compiled_method), NULL);
+                       if (!ji)
+                               ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
 
-                               if (mono_method_same_domain (ji, target_ji))
-                                       mono_arch_patch_callsite (ji->code_start, code, addr);
+                       if (ji && target_ji && generic_shared && ji->has_generic_jit_info && !target_ji->has_generic_jit_info) {
+                               /* 
+                                * Can't patch the call as the caller is gshared, but the callee is not. Happens when
+                                * generic sharing fails.
+                                * FIXME: Performance problem.
+                                */
+                               no_patch = TRUE;
                        }
+
+                       if (!no_patch && mono_method_same_domain (ji, target_ji))
+                               mono_arch_patch_callsite (ji->code_start, code, addr);
                }
        }
 
@@ -588,68 +700,92 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
 /**
  * mono_magic_trampoline:
  *
- *   This trampoline handles calls from JITted code.
+ *   This trampoline handles normal calls from JITted code.
  */
 gpointer
 mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp)
 {
-       gpointer *vtable_slot;
-       int displacement;
-       MonoVTable *vt;
-
        trampoline_calls ++;
 
-       if (code && !mono_use_llvm)
-               vt = mono_arch_get_vcall_slot (code, regs, &displacement);
-       else
-               vt = NULL;
-       if (vt)
-               vtable_slot = (gpointer*)((char*)vt + displacement);
-       else
-               vtable_slot = NULL;
-
-       return common_call_trampoline (regs, code, arg, tramp, vt, vtable_slot, FALSE);
+       return common_call_trampoline (regs, code, arg, tramp, NULL, NULL, FALSE);
 }
 
-#ifdef MONO_ARCH_LLVM_SUPPORTED
 /*
- * mono_llvm_vcall_trampoline:
+ * mono_vcall_trampoline:
  *
- *   This trampoline handles virtual calls when using LLVM.
+ *   This trampoline handles virtual calls.
  */
 static gpointer
-mono_llvm_vcall_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
+mono_vcall_trampoline (mgreg_t *regs, guint8 *code, int slot, guint8 *tramp)
 {
        MonoObject *this;
        MonoVTable *vt;
        gpointer *vtable_slot;
-       int slot;
+       MonoMethod *m;
+       gboolean need_rgctx_tramp = FALSE;
+       gpointer addr;
 
        trampoline_calls ++;
 
-       /* 
-        * 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.
+       /*
+        * We need to obtain the following pieces of information:
+        * - the method which needs to be compiled.
+        * - the vtable slot.
+        * We use one vtable trampoline per vtable slot index, so we need only the vtable,
+        * the other two can be computed from the vtable + the slot index.
+        */
+#ifndef MONO_ARCH_THIS_AS_FIRST_ARG
+       /* All architectures should support this */
+       g_assert_not_reached ();
+#endif
+
+       /*
+        * Obtain the vtable from the 'this' arg.
         */
-       this = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
+       this = mono_arch_get_this_arg_from_call (regs, code);
        g_assert (this);
 
-       slot = mono_method_get_vtable_slot (m);
+       vt = this->vtable;
 
-       g_assert (slot != -1);
+       if (slot >= 0) {
+               /* Normal virtual call */
+               vtable_slot = &(vt->vtable [slot]);
 
-       g_assert (this->vtable->klass->vtable [slot] == m);
+               /* Avoid loading metadata or creating a generic vtable if possible */
+               addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, slot);
+               if (addr && !vt->klass->valuetype) {
+                       if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
+                               *vtable_slot = addr;
 
-       vt = this->vtable;
+                       return mono_create_ftnptr (mono_domain_get (), addr);
+               }
 
-       g_assert (!m->is_generic);
+               /*
+                * Bug #616463 (see
+                * is_generic_method_definition() above) also
+                * goes away if we do a
+                * mono_class_setup_vtable (vt->klass) here,
+                * because we then inflate the method
+                * correctly, put it in the cache, and the
+                * "wrong" inflation invocation still looks up
+                * the correctly inflated method.
+                *
+                * The hack above seems more stable and
+                * trustworthy.
+                */
+               m = mono_class_get_vtable_entry (vt->klass, slot);
+
+               need_rgctx_tramp = mono_method_needs_static_rgctx_invoke (m, 0);
+       } else {
+               /* IMT call */
+               vtable_slot = &(((gpointer*)vt) [slot]);
 
-       vtable_slot = &(vt->vtable [slot]);
+               m = NULL;
+               need_rgctx_tramp = FALSE;
+       }
 
-       return common_call_trampoline (regs, code, m, tramp, vt, vtable_slot, mono_method_needs_static_rgctx_invoke (m, 0));
+       return common_call_trampoline (regs, code, m, tramp, vt, vtable_slot, need_rgctx_tramp);
 }
-#endif
 
 gpointer
 mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
@@ -703,7 +839,6 @@ mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
        guint32 token;
        MonoMethod *method = NULL;
        gpointer addr;
-       gpointer *vtable_slot;
        guint8 *plt_entry;
 
        trampoline_calls ++;
@@ -723,9 +858,6 @@ mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
 
        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);
@@ -819,9 +951,9 @@ mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, gu
        num_lookups++;
 
        if (mrgctx)
-               return mono_method_fill_runtime_generic_context (arg, index);
+               return mono_method_fill_runtime_generic_context (arg, code, index);
        else
-               return mono_class_fill_runtime_generic_context (arg, index);
+               return mono_class_fill_runtime_generic_context (arg, code, index);
 #else
        g_assert_not_reached ();
 #endif
@@ -855,23 +987,21 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
        MonoJitInfo *ji;
        MonoMethod *m;
        MonoMethod *method = NULL;
-       gboolean multicast, callvirt;
+       gboolean multicast, callvirt = FALSE;
        gboolean need_rgctx_tramp = FALSE;
+       gboolean need_unbox_tramp = FALSE;
+       gboolean enable_caching = TRUE;
        MonoMethod *invoke = tramp_data [0];
        guint8 *impl_this = tramp_data [1];
        guint8 *impl_nothis = tramp_data [2];
        MonoError err;
        MonoMethodSignature *sig;
+       gpointer addr, compiled_method;
 
        trampoline_calls ++;
 
        /* Obtain the delegate object according to the calling convention */
-
-       /* 
-        * Avoid calling mono_get_generic_context_from_code () now since it is expensive, 
-        * get_this_arg_from_call will call it if needed.
-        */
-       delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
+       delegate = mono_arch_get_this_arg_from_call (regs, code);
 
        if (delegate->method) {
                method = delegate->method;
@@ -896,8 +1026,12 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
                        if (!sig)
                                mono_error_raise_exception (&err);
                                
-                       if (sig->hasthis && method->klass->valuetype)
-                               method = mono_marshal_get_unbox_wrapper (method);
+                       if (sig->hasthis && method->klass->valuetype) {
+                               if (mono_aot_only)
+                                       need_unbox_tramp = TRUE;
+                               else
+                                       method = mono_marshal_get_unbox_wrapper (method);
+                       }
                }
        } else {
                ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
@@ -912,6 +1046,13 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
                        mono_error_raise_exception (&err);
 
                callvirt = !delegate->target && sig->hasthis;
+               if (delegate->target && 
+                       method->flags & METHOD_ATTRIBUTE_VIRTUAL && 
+                       method->flags & METHOD_ATTRIBUTE_ABSTRACT &&
+                       method->klass->flags & TYPE_ATTRIBUTE_ABSTRACT) {
+                       method = mono_object_get_virtual_method (delegate->target, method);
+                       enable_caching = FALSE;
+               }
        }
 
        if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
@@ -926,13 +1067,17 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
         */
        if (method && !callvirt) {
                /* Avoid the overhead of looking up an already compiled method if possible */
-               if (delegate->method_code && *delegate->method_code) {
+               if (enable_caching && delegate->method_code && *delegate->method_code) {
                        delegate->method_ptr = *delegate->method_code;
                } else {
-                       delegate->method_ptr = mono_compile_method (method);
-                       if (need_rgctx_tramp)
-                               delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
-                       if (delegate->method_code)
+                       compiled_method = addr = mono_compile_method (method);
+                       if (need_unbox_tramp)
+                               // FIXME: GSHAREDVT
+                               addr = get_unbox_trampoline (method, addr, need_rgctx_tramp);
+                       else
+                               addr = mini_add_method_trampoline (NULL, method, compiled_method, NULL, need_rgctx_tramp);
+                       delegate->method_ptr = addr;
+                       if (enable_caching && delegate->method_code)
                                *delegate->method_code = delegate->method_ptr;
                        mono_debugger_trampoline_compiled (NULL, method, delegate->method_ptr);
                }
@@ -972,10 +1117,10 @@ mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tram
 {
        MonoContext ctx;
        MonoException *exc;
-       MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+       MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
        gpointer resume_ip = jit_tls->handler_block_return_address;
 
-       memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
+       memcpy (&ctx, &jit_tls->handler_block_context, sizeof (MonoContext));
        MONO_CONTEXT_SET_IP (&ctx, jit_tls->handler_block_return_address);
 
        jit_tls->handler_block_return_address = NULL;
@@ -992,7 +1137,7 @@ mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tram
                if (!restore_context)
                        restore_context = mono_get_restore_context ();
 
-               mono_handle_exception (&ctx, exc, NULL, FALSE);
+               mono_handle_exception (&ctx, exc);
                restore_context (&ctx);
        }
 
@@ -1057,10 +1202,8 @@ mono_get_trampoline_func (MonoTrampolineType tramp_type)
                return mono_monitor_enter_trampoline;
        case MONO_TRAMPOLINE_MONITOR_EXIT:
                return mono_monitor_exit_trampoline;
-#ifdef MONO_ARCH_LLVM_SUPPORTED
-       case MONO_TRAMPOLINE_LLVM_VCALL:
-               return mono_llvm_vcall_trampoline;
-#endif
+       case MONO_TRAMPOLINE_VCALL:
+               return mono_vcall_trampoline;
 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
        case MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD:
                return mono_handler_block_guard_trampoline;
@@ -1071,6 +1214,23 @@ mono_get_trampoline_func (MonoTrampolineType tramp_type)
        }
 }
 
+static guchar*
+create_trampoline_code (MonoTrampolineType tramp_type)
+{
+       MonoTrampInfo *info;
+       guchar *code;
+
+       code = mono_arch_create_generic_trampoline (tramp_type, &info, FALSE);
+       if (info) {
+               mono_save_trampoline_xdebug_info (info);
+               if (mono_jit_map_is_enabled ())
+                       mono_emit_jit_tramp (info->code, info->code_size, info->name);
+               mono_tramp_info_free (info);
+       }
+
+       return code;
+}
+
 void
 mono_trampolines_init (void)
 {
@@ -1079,31 +1239,32 @@ mono_trampolines_init (void)
        if (mono_aot_only)
                return;
 
-       mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
-       mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
-       mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
-       mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
-       mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
+       mono_trampoline_code [MONO_TRAMPOLINE_JIT] = create_trampoline_code (MONO_TRAMPOLINE_JIT);
+       mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
+       mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
+       mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
+       mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
 #ifdef MONO_ARCH_AOT_SUPPORTED
-       mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
-       mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
+       mono_trampoline_code [MONO_TRAMPOLINE_AOT] = create_trampoline_code (MONO_TRAMPOLINE_AOT);
+       mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
 #endif
 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
-       mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
-#endif
-       mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
-       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 MONO_ARCH_LLVM_SUPPORTED
-       mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
+       mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
 #endif
+       mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
+       mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
+       mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
+       mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
+       mono_trampoline_code [MONO_TRAMPOLINE_VCALL] = create_trampoline_code (MONO_TRAMPOLINE_VCALL);
 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
-       mono_trampoline_code [MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
+       mono_trampoline_code [MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD] = create_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
        mono_create_handler_block_trampoline ();
 #endif
 
        mono_counters_register ("Calls to trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &trampoline_calls);
+       mono_counters_register ("JIT trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &jit_trampolines);
+       mono_counters_register ("Unbox trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unbox_trampolines);
+       mono_counters_register ("Static rgctx trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &static_rgctx_trampolines);
 }
 
 void
@@ -1111,6 +1272,10 @@ mono_trampolines_cleanup (void)
 {
        if (class_init_hash_addr)
                g_hash_table_destroy (class_init_hash_addr);
+       if (rgctx_lazy_fetch_trampoline_hash)
+               g_hash_table_destroy (rgctx_lazy_fetch_trampoline_hash);
+       if (rgctx_lazy_fetch_trampoline_hash_addr)
+               g_hash_table_destroy (rgctx_lazy_fetch_trampoline_hash_addr);
 
        DeleteCriticalSection (&trampolines_mutex);
 }
@@ -1173,15 +1338,24 @@ mono_create_generic_class_init_trampoline (void)
 {
 #ifdef MONO_ARCH_VTABLE_REG
        static gpointer code;
+       MonoTrampInfo *info;
 
        mono_trampolines_lock ();
 
        if (!code) {
                if (mono_aot_only)
                        /* get_named_code () might return an ftnptr, but our caller expects a direct pointer */
-                       code = mono_get_addr_from_ftnptr (mono_aot_get_named_code ("generic_class_init_trampoline"));
-               else
-                       code = mono_arch_create_generic_class_init_trampoline ();
+                       code = mono_get_addr_from_ftnptr (mono_aot_get_trampoline ("generic_class_init_trampoline"));
+               else {
+                       code = mono_arch_create_generic_class_init_trampoline (&info, FALSE);
+
+                       if (info) {
+                               mono_save_trampoline_xdebug_info (info);
+                               if (mono_jit_map_is_enabled ())
+                                       mono_emit_jit_tramp (info->code, info->code_size, info->name);
+                               mono_tramp_info_free (info);
+                       }
+               }
        }
 
        mono_trampolines_unlock ();
@@ -1204,8 +1378,9 @@ mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean ad
         * We cannot recover the correct type of a shared generic
         * method from its native code address, so we use the
         * trampoline instead.
+        * For synchronized methods, the trampoline adds the wrapper.
         */
-       if (code && !ji->has_generic_jit_info)
+       if (code && !ji->has_generic_jit_info && !(method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
                return code;
 
        mono_domain_lock (domain);
@@ -1261,7 +1436,7 @@ mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
        g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
        mono_domain_unlock (domain);
 
-       mono_jit_stats.method_trampolines++;
+       jit_trampolines++;
 
        return tramp;
 }      
@@ -1280,7 +1455,7 @@ mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
        MonoDomain *domain = mono_domain_get ();
        guint8 *buf, *start;
 
-       buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
+       buf = start = mono_domain_alloc0 (domain, 2 * sizeof (gpointer));
 
        *(gpointer*)(gpointer)buf = image;
        buf += sizeof (gpointer);
@@ -1288,16 +1463,15 @@ mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
 
        tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
 
-       mono_jit_stats.method_trampolines++;
+       jit_trampolines++;
 
        return tramp;
 }      
 
 gpointer
-mono_create_delegate_trampoline (MonoClass *klass)
+mono_create_delegate_trampoline (MonoDomain *domain, MonoClass *klass)
 {
 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
-       MonoDomain *domain = mono_domain_get ();
        gpointer ptr;
        guint32 code_size = 0;
        gpointer *tramp_data;
@@ -1318,7 +1492,7 @@ mono_create_delegate_trampoline (MonoClass *klass)
        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);
+       ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
        g_assert (code_size);
 
        /* store trampoline address */
@@ -1338,6 +1512,7 @@ mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
 {
        static gboolean inited = FALSE;
        static int num_trampolines = 0;
+       MonoTrampInfo *info;
 
        gpointer tramp, ptr;
 
@@ -1353,7 +1528,13 @@ mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
        if (tramp)
                return tramp;
 
-       tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
+       tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, FALSE);
+       if (info) {
+               mono_save_trampoline_xdebug_info (info);
+               if (mono_jit_map_is_enabled ())
+                       mono_emit_jit_tramp (info->code, info->code_size, info->name);
+               mono_tramp_info_free (info);
+       }
        ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
 
        mono_trampolines_lock ();
@@ -1383,15 +1564,24 @@ mono_create_monitor_enter_trampoline (void)
 
        if (mono_aot_only) {
                if (!code)
-                       code = mono_aot_get_named_code ("monitor_enter_trampoline");
+                       code = mono_aot_get_trampoline ("monitor_enter_trampoline");
                return code;
        }
 
 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
        mono_trampolines_lock ();
 
-       if (!code)
-               code = mono_arch_create_monitor_enter_trampoline ();
+       if (!code) {
+               MonoTrampInfo *info;
+
+               code = mono_arch_create_monitor_enter_trampoline (&info, FALSE);
+               if (info) {
+                       mono_save_trampoline_xdebug_info (info);
+                       if (mono_jit_map_is_enabled ())
+                               mono_emit_jit_tramp (info->code, info->code_size, info->name);
+                       mono_tramp_info_free (info);
+               }
+       }
 
        mono_trampolines_unlock ();
 #else
@@ -1409,15 +1599,24 @@ mono_create_monitor_exit_trampoline (void)
 
        if (mono_aot_only) {
                if (!code)
-                       code = mono_aot_get_named_code ("monitor_exit_trampoline");
+                       code = mono_aot_get_trampoline ("monitor_exit_trampoline");
                return code;
        }
 
 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
        mono_trampolines_lock ();
 
-       if (!code)
-               code = mono_arch_create_monitor_exit_trampoline ();
+       if (!code) {
+               MonoTrampInfo *info;
+
+               code = mono_arch_create_monitor_exit_trampoline (&info, FALSE);
+               if (info) {
+                       mono_save_trampoline_xdebug_info (info);
+                       if (mono_jit_map_is_enabled ())
+                               mono_emit_jit_tramp (info->code, info->code_size, info->name);
+                       mono_tramp_info_free (info);
+               }
+       }
 
        mono_trampolines_unlock ();
 #else
@@ -1428,41 +1627,6 @@ mono_create_monitor_exit_trampoline (void)
 }
  
 #ifdef MONO_ARCH_LLVM_SUPPORTED
-/*
- * 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;
-}
-
 /*
  * mono_create_llvm_imt_trampoline:
  *
@@ -1515,3 +1679,51 @@ mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
        mono_trampolines_unlock ();
        return offset;
 }
+
+static const char*tramp_names [MONO_TRAMPOLINE_NUM] = {
+       "jit",
+       "jump",
+       "class_init",
+       "generic_class_init",
+       "rgctx_lazy_fetch",
+       "aot",
+       "aot_plt",
+       "delegate",
+       "restore_stack_prot",
+       "generic_virtual_remoting",
+       "monitor_enter",
+       "monitor_exit",
+       "vcall",
+#ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
+       "handler_block_guard"
+#endif
+};
+
+/*
+ * mono_get_generic_trampoline_name:
+ *
+ *   Returns a pointer to malloc-ed memory.
+ */
+char*
+mono_get_generic_trampoline_name (MonoTrampolineType tramp_type)
+{
+       return g_strdup_printf ("generic_trampoline_%s", tramp_names [tramp_type]);
+}
+
+/*
+ * mono_get_rgctx_fetch_trampoline_name:
+ *
+ *   Returns a pointer to malloc-ed memory.
+ */
+char*
+mono_get_rgctx_fetch_trampoline_name (int slot)
+{
+       gboolean mrgctx;
+       int index;
+
+       mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
+       index = MONO_RGCTX_SLOT_INDEX (slot);
+
+       return g_strdup_printf ("rgctx_fetch_trampoline_%s_%d", mrgctx ? "mrgctx" : "rgctx", index);
+}
+