Merge branch 'master' into msbuilddll2
[mono.git] / mono / mini / mini.c
index fd5a595687dcb93d0047116831fcf69d3704799b..631233ebcfb89609dccbbd2bee31aecd3d8fb998 100644 (file)
@@ -49,6 +49,7 @@
 #include <mono/metadata/mempool-internals.h>
 #include <mono/metadata/attach.h>
 #include <mono/metadata/runtime.h>
+#include <mono/metadata/mono-debug-debugger.h>
 #include <mono/utils/mono-math.h>
 #include <mono/utils/mono-compiler.h>
 #include <mono/utils/mono-counters.h>
@@ -71,7 +72,6 @@
 
 #include "jit-icalls.h"
 
-#include "debug-mini.h"
 #include "mini-gc.h"
 #include "debugger-agent.h"
 
@@ -897,6 +897,8 @@ mono_type_to_store_membase (MonoCompile *cfg, MonoType *type)
        if (type->byref)
                return OP_STORE_MEMBASE_REG;
 
+       type = mini_replace_type (type);
+
 handle_enum:
        switch (type->type) {
        case MONO_TYPE_I1:
@@ -959,7 +961,7 @@ mono_type_to_load_membase (MonoCompile *cfg, MonoType *type)
        if (type->byref)
                return OP_LOAD_MEMBASE;
 
-       type = mono_type_get_underlying_type (type);
+       type = mini_replace_type (type);
 
        switch (type->type) {
        case MONO_TYPE_I1:
@@ -1037,6 +1039,8 @@ mini_type_to_ldind (MonoCompile* cfg, MonoType *type)
 guint
 mini_type_to_stind (MonoCompile* cfg, MonoType *type)
 {
+       type = mini_replace_type (type);
+
        if (cfg->generic_sharing_context && !type->byref) {
                if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR) {
                        if (mini_type_var_is_vt (cfg, type))
@@ -1216,6 +1220,8 @@ mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode,
        int num = cfg->num_varinfo;
        gboolean regpair;
 
+       type = mini_replace_type (type);
+
        if ((num + 1) >= cfg->varinfo_count) {
                int orig_count = cfg->varinfo_count;
                cfg->varinfo_count = cfg->varinfo_count ? (cfg->varinfo_count * 2) : 64;
@@ -1242,7 +1248,7 @@ mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode,
                if (type->byref) {
                        mono_mark_vreg_as_mp (cfg, vreg);
                } else {
-                       MonoType *t = mini_type_get_underlying_type (NULL, type);
+                       MonoType *t = mini_replace_type (type);
                        if ((MONO_TYPE_ISSTRUCT (t) && inst->klass->has_references) || mini_type_is_reference (cfg, t)) {
                                inst->flags |= MONO_INST_GC_TRACK;
                                mono_mark_vreg_as_ref (cfg, vreg);
@@ -1324,6 +1330,7 @@ MonoInst*
 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
 {
        int dreg;
+       type = mini_replace_type (type);
 
        if (mono_type_is_long (type))
                dreg = mono_alloc_dreg (cfg, STACK_I8);
@@ -2838,7 +2845,6 @@ mono_thread_start_cb (intptr_t tid, gpointer stack_start, gpointer func)
        MonoInternalThread *thread;
        void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
        thread = mono_thread_internal_current ();
-       mono_debugger_thread_created (tid, thread->root_domain_thread, jit_tls, func);
        if (thread)
                thread->jit_data = jit_tls;
 
@@ -2862,7 +2868,6 @@ mono_thread_attach_cb (intptr_t tid, gpointer stack_start)
        MonoInternalThread *thread;
        void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
        thread = mono_thread_internal_current ();
-       mono_debugger_thread_created (tid, thread->root_domain_thread, (MonoJitTlsData *) jit_tls, NULL);
        if (thread)
                thread->jit_data = jit_tls;
        if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
@@ -2877,8 +2882,6 @@ mini_thread_cleanup (MonoInternalThread *thread)
        MonoJitTlsData *jit_tls = thread->jit_data;
 
        if (jit_tls) {
-               mono_debugger_thread_cleanup (jit_tls);
-
                /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
                 * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
                 * not a trivial thing.
@@ -2926,6 +2929,7 @@ mini_get_tls_offset (MonoTlsKey key)
                g_assert (offset != -1);
                break;
        }
+
        return offset;
 }
 
@@ -2948,6 +2952,18 @@ mono_create_tls_get_offset (MonoCompile *cfg, int offset)
        return ins;
 }
 
+gboolean
+mini_tls_get_supported (MonoCompile *cfg, MonoTlsKey key)
+{
+       if (!MONO_ARCH_HAVE_TLS_GET)
+               return FALSE;
+
+       if (cfg->compile_aot)
+               return ARCH_HAVE_TLS_GET_REG;
+       else
+               return mini_get_tls_offset (key) != -1;
+}
+
 MonoInst*
 mono_create_tls_get (MonoCompile *cfg, MonoTlsKey key)
 {
@@ -3007,7 +3023,7 @@ mono_get_lmf_addr_intrinsic (MonoCompile* cfg)
 void
 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
 {
-       MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
+       MonoJumpInfo *ji = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfo));
 
        ji->ip.i = ip;
        ji->type = type;
@@ -3020,7 +3036,7 @@ mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpoin
 void
 mono_add_patch_info_rel (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target, int relocation)
 {
-       MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
+       MonoJumpInfo *ji = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfo));
 
        ji->ip.i = ip;
        ji->type = type;
@@ -3156,6 +3172,7 @@ mono_patch_info_hash (gconstpointer data)
        case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
        case MONO_PATCH_INFO_SIGNATURE:
        case MONO_PATCH_INFO_TLS_OFFSET:
+       case MONO_PATCH_INFO_METHOD_CODE_SLOT:
                return (ji->type << 8) | (gssize)ji->data.target;
        case MONO_PATCH_INFO_GSHAREDVT_CALL:
                return (ji->type << 8) | (gssize)ji->data.gsharedvt->method;
@@ -3320,6 +3337,21 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                }
 #endif
                break;
+       case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
+               gpointer code_slot;
+
+               mono_domain_lock (domain);
+               if (!domain_jit_info (domain)->method_code_hash)
+                       domain_jit_info (domain)->method_code_hash = g_hash_table_new (NULL, NULL);
+               code_slot = g_hash_table_lookup (domain_jit_info (domain)->method_code_hash, patch_info->data.method);
+               if (!code_slot) {
+                       code_slot = mono_domain_alloc0 (domain, sizeof (gpointer));
+                       g_hash_table_insert (domain_jit_info (domain)->method_code_hash, patch_info->data.method, code_slot);
+               }
+               mono_domain_unlock (domain);
+               target = code_slot;
+               break;
+       }
        case MONO_PATCH_INFO_SWITCH: {
                gpointer *jump_table;
                int i;
@@ -3592,9 +3624,16 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                target = (gpointer) (size_t) mono_jit_tls_id;
                break;
        }
-       case MONO_PATCH_INFO_TLS_OFFSET:
-               target = GINT_TO_POINTER (mini_get_tls_offset (GPOINTER_TO_INT (patch_info->data.target)));
+       case MONO_PATCH_INFO_TLS_OFFSET: {
+               int offset;
+
+               offset = mini_get_tls_offset (GPOINTER_TO_INT (patch_info->data.target));
+#ifdef MONO_ARCH_HAVE_TRANSLATE_TLS_OFFSET
+               offset = mono_arch_translate_tls_offset (offset);
+#endif
+               target = GINT_TO_POINTER (offset);
                break;
+       }
        case MONO_PATCH_INFO_OBJC_SELECTOR_REF: {
                target = NULL;
                break;
@@ -4557,20 +4596,43 @@ static MonoType*
 get_gsharedvt_type (MonoType *t)
 {
        MonoGenericParam *par = t->data.generic_param;
+       MonoGenericParam *copy;
        MonoType *res;
+       MonoImage *image = NULL;
 
        /* 
         * Create an anonymous gparam with a different serial so normal gshared and gsharedvt methods have
         * a different instantiation.
         */
        g_assert (mono_generic_param_info (par));
-       par = g_memdup (par, sizeof (MonoGenericParamFull));
-       par->owner = NULL;
+       if (par->owner) {
+               image = par->owner->image;
+
+               mono_image_lock (image);
+               if (!image->gsharedvt_types)
+                       image->gsharedvt_types = g_hash_table_new (NULL, NULL);
+               res = g_hash_table_lookup (image->gsharedvt_types, par);
+               mono_image_unlock (image);
+               if (res)
+                       return res;
+               copy = mono_image_alloc0 (image, sizeof (MonoGenericParamFull));
+               memcpy (copy, par, sizeof (MonoGenericParamFull));
+       } else {
+               copy = g_memdup (par, sizeof (MonoGenericParamFull));
+       }
+       copy->owner = NULL;
        // FIXME:
-       par->image = mono_defaults.corlib;
-       par->serial = 1;
+       copy->image = mono_defaults.corlib;
+       copy->serial = 1;
        res = mono_metadata_type_dup (NULL, t);
-       res->data.generic_param = par;
+       res->data.generic_param = copy;
+
+       if (par->owner) {
+               mono_image_lock (image);
+               /* Duplicates are ok */
+               g_hash_table_insert (image->gsharedvt_types, par, res);
+               mono_image_unlock (image);
+       }
 
        return res;
 }
@@ -4953,7 +5015,7 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
 #endif
 
        /* The debugger has no liveness information, so avoid sharing registers/stack slots */
-       if (mono_debug_using_mono_debugger () || debug_options.mdb_optimizations) {
+       if (debug_options.mdb_optimizations) {
                cfg->disable_reuse_registers = TRUE;
                cfg->disable_reuse_stack_slots = TRUE;
                /* 
@@ -5379,7 +5441,9 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        if (COMPILE_SOFT_FLOAT (cfg))
                mono_decompose_soft_float (cfg);
 #endif
-       if (!COMPILE_LLVM (cfg))
+       if (COMPILE_LLVM (cfg))
+               mono_decompose_vtype_opts_llvm (cfg);
+       else
                mono_decompose_vtype_opts (cfg);
        if (cfg->flags & MONO_CFG_HAS_ARRAY_ACCESS)
                mono_decompose_array_access_opts (cfg);
@@ -5780,9 +5844,6 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
                if (jinfo)
                        mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
                return code;
-
-               //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE) 
-               //mono_debug_add_wrapper (method, nm);
        } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
                const char *name = method->name;
                char *full_name, *msg;
@@ -6167,7 +6228,6 @@ mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt, MonoException
                if (!callinfo->wrapper) {
                        callinfo->wrapper = p;
                        mono_register_jit_icall_wrapper (callinfo, p);
-                       mono_debug_add_icall_wrapper (method, callinfo);
                }
                mono_jit_unlock ();
                mono_loader_unlock ();
@@ -7052,11 +7112,6 @@ mini_init (const char *filename, const char *runtime_version)
 
        InitializeCriticalSection (&jit_mutex);
 
-#ifdef MONO_DEBUGGER_SUPPORTED
-       if (mini_debug_running_inside_mdb ())
-               mini_debugger_init ();
-#endif
-
 #ifdef MONO_HAVE_FAST_TLS
        MONO_FAST_TLS_INIT (mono_jit_tls);
        MONO_FAST_TLS_INIT (mono_lmf_addr);
@@ -7211,11 +7266,11 @@ mini_init (const char *filename, const char *runtime_version)
        /*Init arch tls information only after the metadata side is inited to make sure we see dynamic appdomain tls keys*/
        mono_arch_finish_init ();
 
+       mono_icall_init ();
+
        /* This must come after mono_init () in the aot-only case */
        mono_exceptions_init ();
 
-       mono_icall_init ();
-
        /* This should come after mono_init () too */
        mini_gc_init ();
 
@@ -7394,6 +7449,8 @@ mini_init (const char *filename, const char *runtime_version)
 
 #if SIZEOF_REGISTER == 4
        register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, "mono_fconv_u4", TRUE);
+#else
+       register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "ulong double", mono_fconv_u8, "mono_fconv_u8", TRUE);
 #endif
 
        /* other jit icalls */
@@ -7430,7 +7487,7 @@ mini_init (const char *filename, const char *runtime_version)
        register_icall (mono_array_new_4, "mono_array_new_4", "object ptr int int int int", FALSE);
        register_icall (mono_get_native_calli_wrapper, "mono_get_native_calli_wrapper", "ptr ptr ptr ptr", FALSE);
        register_icall (mono_resume_unwind, "mono_resume_unwind", "void", TRUE);
-       register_icall (mono_gsharedvt_constrained_call, "mono_gsharedvt_constrained_call", "object ptr ptr ptr ptr ptr", TRUE);
+       register_icall (mono_gsharedvt_constrained_call, "mono_gsharedvt_constrained_call", "object ptr ptr ptr ptr ptr", FALSE);
        register_icall (mono_gsharedvt_value_copy, "mono_gsharedvt_value_copy", "void ptr ptr ptr", TRUE);
 
        register_icall (mono_gc_wbarrier_value_copy_bitmap, "mono_gc_wbarrier_value_copy_bitmap", "void ptr ptr int int", FALSE);
@@ -7576,8 +7633,6 @@ mini_cleanup (MonoDomain *domain)
        mono_domain_free (domain, TRUE);
 #endif
 
-       mono_debugger_cleanup ();
-
 #ifdef ENABLE_LLVM
        if (mono_use_llvm)
                mono_llvm_cleanup ();
@@ -7894,3 +7949,14 @@ mono_jumptable_get_entry (guint8 *code_ptr)
        return mono_arch_jumptable_entry_from_code (code_ptr);
 }
 #endif
+
+/*
+ * mini_replace_type:
+ *
+ * Replace the type used in the metadata stream with what the JIT will actually use during compilation.
+*/
+MonoType*
+mini_replace_type (MonoType *type)
+{
+       return mono_type_get_underlying_type (type);
+}