Pass signatures instead of methods to mono_arch_get_gsharedvt_call_info ().
[mono.git] / mono / mini / mini-trampolines.c
index ba2061b4b5c69ccbf27f82afe49d90845028f567..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>
 
@@ -45,7 +49,7 @@ get_unbox_trampoline (MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
        }
 }
 
-#ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
+#ifdef MONO_ARCH_GSHARED_SUPPORTED
 
 typedef struct {
        MonoMethod *m;
@@ -99,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 ();
 
@@ -142,11 +143,11 @@ mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
 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
 
@@ -219,6 +220,16 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *cod
 
                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 (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;
 #if DEBUG_IMT
@@ -273,6 +284,101 @@ is_generic_method_definition (MonoMethod *m)
                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:
@@ -286,7 +392,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
        gpointer addr, compiled_method;
        gboolean generic_shared = FALSE;
        MonoMethod *declaring = NULL;
-       MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
+       MonoMethod *generic_virtual = NULL, *variant_iface = NULL, *orig_method = NULL;
        int context_used;
        gboolean virtual, variance_used = FALSE;
        gpointer *orig_vtable_slot, *vtable_slot_to_patch = NULL;
@@ -310,6 +416,8 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                vtable_slot = orig_vtable_slot;
                g_assert (vtable_slot);
 
+               orig_method = m;
+
                this_arg = mono_arch_get_this_arg_from_call (regs, code);
 
                if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
@@ -415,8 +523,10 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                        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
@@ -466,18 +576,8 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
        }
 
        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);
-                       need_rgctx_tramp = FALSE;
-               }
+               m = mono_marshal_get_synchronized_wrapper (m);
+               need_rgctx_tramp = FALSE;
        }
 
        /* Calls made through delegates on platforms without delegate trampolines */
@@ -489,8 +589,10 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
 
        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;
@@ -549,31 +651,46 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
        }
        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 (!target_ji)
-                                       target_ji = mono_jit_info_table_find (mono_get_root_domain (), mono_get_addr_from_ftnptr (compiled_method));
-
-                               if (!ji)
-                                       ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
-                               if (!ji)
-                                       ji = mono_jit_info_table_find (mono_get_root_domain (), (char*)code);
-
-                               if (mono_method_same_domain (ji, target_ji))
-                                       mono_arch_patch_callsite (ji->code_start, code, addr);
+                       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) {
+                               /* 
+                                * 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);
                }
        }
 
@@ -834,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
@@ -872,12 +989,14 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
        MonoMethod *method = NULL;
        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 ++;
 
@@ -907,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));
@@ -947,9 +1070,13 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
                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);
+                       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);
@@ -1010,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);
        }
 
@@ -1251,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);
@@ -1327,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);
@@ -1341,10 +1469,9 @@ mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
 }      
 
 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;
@@ -1365,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 */
@@ -1434,7 +1561,6 @@ gpointer
 mono_create_monitor_enter_trampoline (void)
 {
        static gpointer code;
-       MonoTrampInfo *info;
 
        if (mono_aot_only) {
                if (!code)
@@ -1446,6 +1572,8 @@ mono_create_monitor_enter_trampoline (void)
        mono_trampolines_lock ();
 
        if (!code) {
+               MonoTrampInfo *info;
+
                code = mono_arch_create_monitor_enter_trampoline (&info, FALSE);
                if (info) {
                        mono_save_trampoline_xdebug_info (info);
@@ -1468,7 +1596,6 @@ gpointer
 mono_create_monitor_exit_trampoline (void)
 {
        static gpointer code;
-       MonoTrampInfo *info;
 
        if (mono_aot_only) {
                if (!code)
@@ -1480,6 +1607,8 @@ mono_create_monitor_exit_trampoline (void)
        mono_trampolines_lock ();
 
        if (!code) {
+               MonoTrampInfo *info;
+
                code = mono_arch_create_monitor_exit_trampoline (&info, FALSE);
                if (info) {
                        mono_save_trampoline_xdebug_info (info);