2010-02-10 Miguel de Icaza <miguel@novell.com>
[mono.git] / mono / mini / mini-trampolines.c
index 8ec71e706c588c10a4d7fabde4a55a94e7f4cf77..2860fb4e20fd544126fdb0728d809dab4a73bcb3 100644 (file)
@@ -20,6 +20,7 @@ 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;
 
 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
@@ -81,6 +82,7 @@ rgctx_tramp_info_hash (gconstpointer data)
  * methods code. These trampolines are similar to the unbox trampolines, they
  * perform the same task as the static rgctx wrappers, but they are smaller/faster,
  * and can be made to work with full AOT.
+ * On PPC addr should be an ftnptr and the return value is an ftnptr too.
  */
 gpointer
 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
@@ -91,6 +93,10 @@ mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
        RgctxTrampInfo tmp_info;
        RgctxTrampInfo *info;
 
+#ifdef PPC_USES_FUNCTION_DESCRIPTOR
+       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
@@ -154,7 +160,7 @@ mono_get_vcall_slot_addr (guint8* code, mgreg_t *regs)
 #ifdef MONO_ARCH_HAVE_IMT
 
 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)
+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)
 {
        MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (method), regs, code);
        MonoVTable *vt = this_argument->vtable;
@@ -172,11 +178,10 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *cod
                int imt_slot = MONO_IMT_SIZE + displacement;
 
                /*This has to be variance aware since imt_method can be from an interface that vt->klass doesn't directly implement*/
-               interface_offset = mono_class_interface_offset_with_variance (vt->klass, imt_method->klass);
+               interface_offset = mono_class_interface_offset_with_variance (vt->klass, imt_method->klass, variance_used);
 
                if (interface_offset < 0) {
-                       g_print ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, 0), mono_type_get_name_full (&imt_method->klass->byval_arg, 0));
-                       g_assert_not_reached ();
+                       g_error ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, 0), mono_type_get_name_full (&imt_method->klass->byval_arg, 0));
                }
                mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
 
@@ -243,13 +248,13 @@ mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *cod
 static gpointer
 common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp, MonoVTable *vt, gpointer *vtable_slot, gboolean need_rgctx_tramp)
 {
-       gpointer addr;
+       gpointer addr, compiled_method;
        gboolean generic_shared = FALSE;
        MonoMethod *m;
        MonoMethod *declaring = NULL;
-       MonoMethod *generic_virtual = NULL;
+       MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
        int context_used;
-       gboolean proxy = FALSE;
+       gboolean proxy = FALSE, variance_used = FALSE;
        gpointer *orig_vtable_slot;
        MonoJitInfo *ji = NULL;
 
@@ -322,7 +327,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
                        proxy = TRUE;
                    m = mono_object_get_virtual_method (this_arg, m);
                } else {
-                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp);
+                       vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp, &variance_used);
                        /* 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.
                         */
@@ -330,11 +335,11 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
                        if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
                                /* Generic virtual method */
                                generic_virtual = m;
-                               m = impl_method;
                                need_rgctx_tramp = TRUE;
-                       } else {
-                               m = impl_method;
+                       } else if (variance_used && mono_class_has_variant_generic_params (m->klass)) {
+                               variant_iface = m;
                        }
+                       m = impl_method;
                }
        }
 #endif
@@ -483,7 +488,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
        if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
                need_rgctx_tramp = TRUE;
 
-       addr = mono_compile_method (m);
+       addr = compiled_method = mono_compile_method (m);
        g_assert (addr);
 
        mono_debugger_trampoline_compiled (code, m, addr);
@@ -491,16 +496,18 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
        if (need_rgctx_tramp)
                addr = mono_create_static_rgctx_trampoline (m, addr);
 
-       if (generic_virtual) {
+       if (generic_virtual || variant_iface) {
+               MonoMethod *target = generic_virtual ? generic_virtual : variant_iface;
+
                vtable_slot = orig_vtable_slot;
                g_assert (vtable_slot);
 
-               if (vt->klass->valuetype)
+               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);
 
                mono_method_add_generic_virtual_invocation (mono_domain_get (), 
                                                                                                        vt, vtable_slot,
-                                                                                                       generic_virtual, addr);
+                                                                                                       target, addr);
 
                return addr;
        }
@@ -536,13 +543,14 @@ 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);
 
                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);
+                       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);
                }
@@ -565,7 +573,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp
 
                        } else {
                                MonoJitInfo *target_ji = 
-                                       mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
+                                       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);
@@ -591,6 +599,8 @@ mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp)
        int displacement;
        MonoVTable *vt;
 
+       trampoline_calls ++;
+
        if (code && !mono_use_llvm)
                vt = mono_arch_get_vcall_slot (code, regs, &displacement);
        else
@@ -617,6 +627,8 @@ mono_llvm_vcall_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *
        gpointer *vtable_slot;
        int slot;
 
+       trampoline_calls ++;
+
        /* 
         * We have the method which is called, we need to obtain the vtable slot without
         * disassembly which is impossible with LLVM.
@@ -648,6 +660,8 @@ mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMetho
        MonoMethod *imt_method, *declaring;
        gpointer addr;
 
+       trampoline_calls ++;
+
        g_assert (m->is_generic);
 
        if (m->is_inflated)
@@ -696,6 +710,8 @@ mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
        guint8 *plt_entry;
        gboolean need_rgctx_tramp = FALSE;
 
+       trampoline_calls ++;
+
        image = *(gpointer*)(gpointer)token_info;
        token_info += sizeof (gpointer);
        token = *(guint32*)(gpointer)token_info;
@@ -730,9 +746,10 @@ mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
         */
        if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
 #ifdef MONO_ARCH_HAVE_IMT
+               gboolean variance_used = FALSE;
                if (!method)
                        method = mono_get_method (image, token, NULL);
-               vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, method, NULL, &need_rgctx_tramp);
+               vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, method, NULL, &need_rgctx_tramp, &variance_used);
 #endif
                *vtable_slot = addr;
        }
@@ -750,8 +767,19 @@ 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);
+       gpointer res;
+
+       trampoline_calls ++;
 
-       return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
+       res = mono_aot_plt_resolve (aot_module, plt_info_offset, code);
+       if (!res) {
+               if (mono_loader_get_last_error ())
+                       mono_raise_exception (mono_loader_error_prepare_exception (mono_loader_get_last_error ()));
+               // FIXME: Error handling (how ?)
+               g_assert (res);
+       }
+
+       return res;
 }
 #endif
 
@@ -766,6 +794,8 @@ mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, gui
 {
        guint8 *plt_entry = mono_aot_get_plt_entry (code);
 
+       trampoline_calls ++;
+
        mono_runtime_class_init (vtable);
 
        if (plt_entry) {
@@ -784,6 +814,8 @@ mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, gui
 void
 mono_generic_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
 {
+       trampoline_calls ++;
+
        mono_runtime_class_init (vtable);
 }
 
@@ -799,6 +831,8 @@ mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, gu
        guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
        gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
 
+       trampoline_calls ++;
+
        if (!inited) {
                mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
                inited = TRUE;
@@ -849,6 +883,8 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
        guint8 *impl_this = tramp_data [1];
        guint8 *impl_nothis = tramp_data [2];
 
+       trampoline_calls ++;
+
        /* Obtain the delegate object according to the calling convention */
 
        /* 
@@ -1009,6 +1045,8 @@ mono_trampolines_init (void)
 #ifdef MONO_ARCH_LLVM_SUPPORTED
        mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
 #endif
+
+       mono_counters_register ("Calls to trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &trampoline_calls);
 }
 
 void
@@ -1083,7 +1121,8 @@ mono_create_generic_class_init_trampoline (void)
 
        if (!code) {
                if (mono_aot_only)
-                       code = mono_aot_get_named_code ("generic_class_init_trampoline");
+                       /* 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 ();
        }