Make pdb2mdb work with files in other directories.
[mono.git] / mono / mini / mini-trampolines.c
index 8d8e159fd66d05c4c890affbfa990eb7745a7db5..92a07726e83b16c1a2c4cb2dff0fc84a8949718a 100644 (file)
@@ -28,7 +28,7 @@ static guint32 trampoline_calls, jit_trampolines, unbox_trampolines, static_rgct
 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)
@@ -41,7 +41,7 @@ get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer
                        return mono_aot_get_unbox_trampoline (m);
        } else {
                unbox_trampolines ++;
-               return mono_arch_get_unbox_trampoline (gsctx, m, addr);
+               return mono_arch_get_unbox_trampoline (m, addr);
        }
 }
 
@@ -152,10 +152,13 @@ mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
 
 #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)
+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);
 
@@ -167,6 +170,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;
 
@@ -178,37 +182,42 @@ 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 (mono_method_needs_static_rgctx_invoke (impl, FALSE))
-                               *need_rgctx_tramp = TRUE;
+               if (impl && mono_method_needs_static_rgctx_invoke (impl, FALSE))
+                       *need_rgctx_tramp = TRUE;
 
-                       *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);
@@ -271,12 +280,13 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
        MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
        int context_used;
        gboolean virtual, proxy = FALSE, variance_used = FALSE;
-       gpointer *orig_vtable_slot, *imt_vcall_slot = NULL;
+       gpointer *orig_vtable_slot, *vtable_slot_to_patch = NULL;
        MonoJitInfo *ji = NULL;
 
        virtual = (gpointer)vtable_slot > (gpointer)vt;
 
        orig_vtable_slot = vtable_slot;
+       vtable_slot_to_patch = vtable_slot;
 
 #ifdef MONO_ARCH_HAVE_IMT
        /* IMT call */
@@ -291,15 +301,18 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                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);
+               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 {
-                       imt_vcall_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp, &variance_used);
-                       vtable_slot = imt_vcall_slot;
+                       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.
                         */
@@ -311,6 +324,17 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                        } else if (variance_used && mono_class_has_variant_generic_params (m->klass)) {
                                variant_iface = m;
                        }
+
+                       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;
                }
        }
@@ -375,7 +399,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
 #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;
@@ -442,8 +466,10 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                        ji = NULL;
 
                /* Avoid recursion */
-               if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
+               if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED)) {
                        m = mono_marshal_get_synchronized_wrapper (m);
+                       need_rgctx_tramp = FALSE;
+               }
        }
 
        /* Calls made through delegates on platforms without delegate trampolines */
@@ -465,7 +491,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                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,
@@ -506,14 +532,11 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
 
        if (vtable_slot) {
                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))) {
-                       if (imt_vcall_slot)
-                               /* This is the vcall slot which gets called through the IMT thunk */
-                               vtable_slot = imt_vcall_slot;
-                       *vtable_slot = mono_get_addr_from_ftnptr (addr);
+                       addr = get_unbox_trampoline (m, addr, need_rgctx_tramp);
+
+               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 {
@@ -532,9 +555,13 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                        {
                                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);
@@ -590,7 +617,7 @@ mono_vcall_trampoline (mgreg_t *regs, guint8 *code, int slot, guint8 *tramp)
        /*
         * Obtain the vtable from the 'this' arg.
         */
-       this = mono_arch_get_this_arg_from_call (NULL, NULL, regs, code);
+       this = mono_arch_get_this_arg_from_call (regs, code);
        g_assert (this);
 
        vt = this->vtable;
@@ -837,6 +864,7 @@ 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 enable_caching = TRUE;
        MonoMethod *invoke = tramp_data [0];
        guint8 *impl_this = tramp_data [1];
        guint8 *impl_nothis = tramp_data [2];
@@ -846,12 +874,7 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, gui
        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;
@@ -892,6 +915,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)
@@ -906,13 +936,13 @@ 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)
+                       if (enable_caching && delegate->method_code)
                                *delegate->method_code = delegate->method_ptr;
                        mono_debugger_trampoline_compiled (NULL, method, delegate->method_ptr);
                }
@@ -955,7 +985,7 @@ mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tram
        MonoJitTlsData *jit_tls = TlsGetValue (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;
@@ -1058,6 +1088,8 @@ create_trampoline_code (MonoTrampolineType tramp_type)
        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);
        }
 
@@ -1105,6 +1137,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);
 }
@@ -1180,6 +1216,8 @@ mono_create_generic_class_init_trampoline (void)
 
                        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);
                        }
                }
@@ -1358,6 +1396,8 @@ mono_create_rgctx_lazy_fetch_trampoline (guint32 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);
@@ -1401,6 +1441,8 @@ mono_create_monitor_enter_trampoline (void)
                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);
                }
        }
@@ -1433,6 +1475,8 @@ mono_create_monitor_exit_trampoline (void)
                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);
                }
        }