Merge pull request #1266 from esdrubal/datetimenewformat
[mono.git] / mono / mini / mini-trampolines.c
index 6e56ed5d2a0abb6b68583cd27a25666b63d6b284..f65dab8c1e77f6220613e59111a84dd927597b90 100644 (file)
  */
 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 GHashTable *class_init_hash_addr;
+static GHashTable *rgctx_lazy_fetch_trampoline_hash;
+static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr;
 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;
+#define mono_trampolines_lock() mono_mutex_lock (&trampolines_mutex)
+#define mono_trampolines_unlock() mono_mutex_unlock (&trampolines_mutex)
+static mono_mutex_t trampolines_mutex;
 
 #ifdef MONO_ARCH_GSHARED_SUPPORTED
 
@@ -138,8 +138,6 @@ mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
 }
 #endif
 
-#ifdef MONO_ARCH_HAVE_IMT
-
 /*
  * Either IMPL_METHOD or AOT_ADDR will be set on return.
  */
@@ -243,7 +241,6 @@ __attribute__ ((noinline))
                }
        }
 }
-#endif
 
 /*
  * This is a super-ugly hack to fix bug #616463.
@@ -406,7 +403,6 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
        orig_vtable_slot = vtable_slot;
        vtable_slot_to_patch = vtable_slot;
 
-#ifdef MONO_ARCH_HAVE_IMT
        /* IMT call */
        if (vt && (gpointer)vtable_slot < (gpointer)vt) {
                MonoMethod *impl_method = NULL;
@@ -464,7 +460,6 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                        m = impl_method;
                }
        }
-#endif
 
        /*
         * The virtual check is needed because is_generic_method_definition (m) could
@@ -484,9 +479,7 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                else
                        g_assert (!m->klass->generic_container);
 
-#ifdef MONO_ARCH_HAVE_IMT
                generic_virtual = mono_arch_find_imt_method (regs, code);
-#endif
                if (generic_virtual) {
                        g_assert (generic_virtual->is_inflated);
                        context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
@@ -524,7 +517,6 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                        g_assert_not_reached ();
 #endif
                } else {
-#ifdef MONO_ARCH_HAVE_IMT
                        MonoObject *this_argument = mono_arch_get_this_arg_from_call (regs, code);
 
                        vt = this_argument->vtable;
@@ -537,9 +529,6 @@ common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tram
                                mono_class_setup_supertypes (this_argument->vtable->klass);
                                klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
                        }
-#else
-                       NOT_IMPLEMENTED;
-#endif
                }
 
                g_assert (vtable_slot || klass);
@@ -816,11 +805,9 @@ mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMetho
        else
                g_assert (!m->klass->generic_container);
 
-#ifdef MONO_ARCH_HAVE_IMT
        imt_method = mono_arch_find_imt_method (regs, code);
        if (imt_method->is_inflated)
                context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
-#endif
        m = mono_class_inflate_generic_method (declaring, &context);
        m = mono_marshal_get_remoting_invoke_with_check (m);
 
@@ -981,24 +968,14 @@ mono_monitor_exit_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guin
 
 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
 
-typedef struct {
-       MonoMethod *invoke;
-       gpointer impl_this;
-       gpointer impl_nothis;
-       MonoMethod *method;
-       MonoMethodSignature *invoke_sig;
-       MonoMethodSignature *sig;
-       gboolean need_rgctx_tramp;
-} DelegateTrampInfo;
-
 /*
  * Precompute data to speed up mono_delegate_trampoline ().
  * METHOD might be NULL.
  */
-static gpointer
+static MonoDelegateTrampInfo*
 create_delegate_trampoline_data (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
 {
-       DelegateTrampInfo *tramp_data;
+       MonoDelegateTrampInfo *tramp_data;
        MonoMethod *invoke;
        MonoError err;
 
@@ -1006,7 +983,7 @@ create_delegate_trampoline_data (MonoDomain *domain, MonoClass *klass, MonoMetho
        invoke = mono_get_delegate_invoke (klass);
        g_assert (invoke);
 
-       tramp_data = mono_domain_alloc (domain, sizeof (DelegateTrampInfo));
+       tramp_data = mono_domain_alloc0 (domain, sizeof (MonoDelegateTrampInfo));
        tramp_data->invoke = invoke;
        tramp_data->invoke_sig = mono_method_signature (invoke);
        tramp_data->impl_this = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
@@ -1039,7 +1016,7 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *arg, guint8* tr
        gboolean need_rgctx_tramp = FALSE;
        gboolean need_unbox_tramp = FALSE;
        gboolean enable_caching = TRUE;
-       DelegateTrampInfo *tramp_info = (DelegateTrampInfo*)arg;
+       MonoDelegateTrampInfo *tramp_info = (MonoDelegateTrampInfo*)arg;
        MonoMethod *invoke = tramp_info->invoke;
        guint8 *impl_this = tramp_info->impl_this;
        guint8 *impl_nothis = tramp_info->impl_nothis;
@@ -1161,6 +1138,9 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *arg, guint8* tr
                        delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
        }
 
+       /* Necessary for !code condition to fallback to slow path */
+       code = NULL;
+
        multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
        if (!multicast && !callvirt) {
                if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
@@ -1168,18 +1148,20 @@ mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *arg, guint8* tr
                        code = impl_this;
                else
                        code = delegate->target ? impl_this : impl_nothis;
+       }
 
-               if (code) {
-                       delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
-                       return code;
-               }
+       if (!code) {
+               /* The general, unoptimized case */
+               m = mono_marshal_get_delegate_invoke (invoke, delegate);
+               code = mono_compile_method (m);
+               code = mini_add_method_trampoline (NULL, m, code, mono_method_needs_static_rgctx_invoke (m, FALSE), FALSE);
        }
 
-       /* The general, unoptimized case */
-       m = mono_marshal_get_delegate_invoke (invoke, delegate);
-       code = mono_compile_method (m);
-       code = mini_add_method_trampoline (NULL, m, code, mono_method_needs_static_rgctx_invoke (m, FALSE), FALSE);
        delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
+       if (enable_caching && !callvirt && tramp_info->method) {
+               tramp_info->method_ptr = delegate->method_ptr;
+               tramp_info->invoke_impl = delegate->invoke_impl;
+       }
 
        return code;
 }
@@ -1308,7 +1290,7 @@ create_trampoline_code (MonoTrampolineType tramp_type)
 void
 mono_trampolines_init (void)
 {
-       InitializeCriticalSection (&trampolines_mutex);
+       mono_mutex_init_recursive (&trampolines_mutex);
 
        if (mono_aot_only)
                return;
@@ -1353,7 +1335,7 @@ mono_trampolines_cleanup (void)
        if (rgctx_lazy_fetch_trampoline_hash_addr)
                g_hash_table_destroy (rgctx_lazy_fetch_trampoline_hash_addr);
 
-       DeleteCriticalSection (&trampolines_mutex);
+       mono_mutex_destroy (&trampolines_mutex);
 }
 
 guint8 *
@@ -1540,30 +1522,29 @@ mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
 
 
 /*
- * mono_create_delegate_trampoline_with_method:
+ * mono_create_delegate_trampoline_info:
  *
  *   Create a delegate trampoline for the KLASS+METHOD pair.
  */
-gpointer
-mono_create_delegate_trampoline_with_method (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
+MonoDelegateTrampInfo*
+mono_create_delegate_trampoline_info (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
 {
 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
-       gpointer ptr;
-       guint32 code_size = 0;
-       gpointer tramp_info;
+       MonoDelegateTrampInfo *tramp_info;
        MonoClassMethodPair pair, *dpair;
+       guint32 code_size = 0;
 
        pair.klass = klass;
        pair.method = method;
        mono_domain_lock (domain);
-       ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, &pair);
+       tramp_info = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, &pair);
        mono_domain_unlock (domain);
-       if (ptr)
-               return ptr;
+       if (tramp_info)
+               return tramp_info;
 
        tramp_info = create_delegate_trampoline_data (domain, klass, method);
 
-       ptr = mono_create_specific_trampoline (tramp_info, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
+       tramp_info->invoke_impl = mono_create_specific_trampoline (tramp_info, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
        g_assert (code_size);
 
        dpair = mono_domain_alloc0 (domain, sizeof (MonoClassMethodPair));
@@ -1571,10 +1552,10 @@ mono_create_delegate_trampoline_with_method (MonoDomain *domain, MonoClass *klas
 
        /* store trampoline address */
        mono_domain_lock (domain);
-       g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash, dpair, ptr);
+       g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash, dpair, tramp_info);
        mono_domain_unlock (domain);
 
-       return ptr;
+       return tramp_info;
 #else
        return NULL;
 #endif
@@ -1583,7 +1564,24 @@ mono_create_delegate_trampoline_with_method (MonoDomain *domain, MonoClass *klas
 gpointer
 mono_create_delegate_trampoline (MonoDomain *domain, MonoClass *klass)
 {
-       return mono_create_delegate_trampoline_with_method (domain, klass, NULL);
+#ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
+       return mono_create_delegate_trampoline_info (domain, klass, NULL)->invoke_impl;
+#else
+       return NULL;
+#endif
+}
+
+gpointer
+mono_create_delegate_virtual_trampoline (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
+{
+#ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
+       MonoMethod *invoke = mono_get_delegate_invoke (klass);
+       g_assert (invoke);
+
+       return mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), method);
+#else
+       return NULL;
+#endif
 }
 
 gpointer