2007-08-30 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mono / mini / mini-trampolines.c
index 1f85fadedd1cd55e44f4e5fdb5490a7ee309073a..7ec5dc352d1c6cbf5c145341bd434606920b1e63 100644 (file)
 
 #include "mini.h"
 
+#ifdef MONO_ARCH_HAVE_IMT
+
+static gpointer*
+mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method)
+{
+       MonoObject *this_argument = mono_arch_find_this_argument (regs, method);
+       MonoVTable *vt = this_argument->vtable;
+       int displacement = slot - ((gpointer*)vt);
+       
+       if (displacement > 0) {
+               /* slot is in the vtable, not in the IMT */
+#if DEBUG_IMT
+               printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
+#endif
+               return slot;
+       } else {
+               MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
+               int interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
+               int imt_slot = MONO_IMT_SIZE + displacement;
+
+#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 vtable_offset = interface_offset + imt_method->slot;
+                       gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
+#if DEBUG_IMT
+                       printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, and colliding becomes %p[%d] (interface_offset = %d, method->slot = %d)\n", slot, imt_slot, vtable_slot, vtable_offset, interface_offset, imt_method->slot);
+#endif
+                       g_assert (vtable_offset >= 0);
+                       return vtable_slot;
+               } else {
+#if DEBUG_IMT
+                       printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
+#endif
+                       return slot;
+               }
+       }
+}
+#endif
+
 /**
  * mono_magic_trampoline:
  *
@@ -39,8 +83,12 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
 
                g_assert (*vtable_slot);
 
-               if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
+               if (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);
+#endif
                        *vtable_slot = mono_get_addr_from_ftnptr (addr);
+               }
        }
        else {
                guint8 *plt_entry = mono_aot_get_plt_entry (code);
@@ -50,7 +98,7 @@ mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
                        mono_arch_patch_plt_entry (plt_entry, addr);
                } else {
                        MonoJitInfo *ji = 
-                               mono_jit_info_table_find (mono_domain_get (), code);
+                               mono_jit_info_table_find (mono_domain_get (), (char*)code);
                        MonoJitInfo *target_ji = 
                                mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
 
@@ -126,8 +174,14 @@ mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info,
         * is AppDomain:InvokeInDomain, so this is the same check as in 
         * mono_method_same_domain () but without loading the metadata for the method.
         */
-       if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
+       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
+               if (!method)
+                       method = mono_get_method (image, token, NULL);
+               vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method);
+#endif
                *vtable_slot = addr;
+       }
 
        return addr;
 }
@@ -178,20 +232,77 @@ mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guin
 /**
  * mono_delegate_trampoline:
  *
- *   This trampoline handles calls made from the delegate invoke wrapper. It patches
- * the function address inside the delegate.
+ *   This trampoline handles calls made to Delegate:Invoke ().
  */
 gpointer
-mono_delegate_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
+mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
 {
-       gpointer addr;
+       MonoDomain *domain = mono_domain_get ();
+       MonoDelegate *delegate;
+       MonoJitInfo *ji;
+       gpointer iter;
+       MonoMethod *invoke;
+       gboolean multicast;
+
+       /* Find the Invoke method */
+       iter = NULL;
+       while ((invoke = mono_class_get_methods (klass, &iter))) {
+               if (!strcmp (invoke->name, "Invoke"))
+                       break;
+       }
+       g_assert (invoke);
 
-       addr = mono_compile_method (m);
-       g_assert (addr);
+       /* Obtain the delegate object according to the calling convention */
 
-       mono_arch_patch_delegate_trampoline (code, tramp, regs, addr);
+       delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
 
-       return addr;
+       /* 
+        * If the called address is a trampoline, replace it with the compiled method so
+        * further calls don't have to go through the trampoline.
+        */
+       ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
+       if (ji)
+               delegate->method_ptr = mono_compile_method (ji->method);
+
+       multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
+       if (!multicast) {
+               guint8* code;
+               GHashTable *cache;
+
+               mono_domain_lock (domain);
+               if (delegate->target != NULL) {
+                       if (!domain->delegate_invoke_impl_with_target_hash)
+                               domain->delegate_invoke_impl_with_target_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+                       cache = domain->delegate_invoke_impl_with_target_hash;
+               } else {
+                       if (!domain->delegate_invoke_impl_no_target_hash)
+                               domain->delegate_invoke_impl_no_target_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+                       cache = domain->delegate_invoke_impl_no_target_hash;
+               }
+               code = g_hash_table_lookup (cache, mono_method_signature (invoke));
+               mono_domain_unlock (domain);
+               if (code) {
+                       delegate->invoke_impl = code;
+                       return code;
+               }
+
+               code = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), delegate->target != NULL);
+
+               if (code) {
+                       mono_domain_lock (domain);
+                       g_hash_table_insert (cache, mono_method_signature (invoke), code);
+                       mono_domain_unlock (domain);
+
+                       delegate->invoke_impl = code;
+                       return code;
+               }
+       }
+
+       /* The general, unoptimized case */
+       delegate->invoke_impl = mono_compile_method (mono_marshal_get_delegate_invoke (invoke));
+       return delegate->invoke_impl;
 }
 
 #endif
+
+