[jit] Implement OP_CARD_TABLE_WBARRIER on ios.
[mono.git] / mono / mini / mini.c
index 45a4fad5e0598d5752611b629c658f6912115cb3..38751953604829806ba7e24a5b89faf35989df6f 100644 (file)
@@ -683,6 +683,19 @@ mono_tramp_info_register (MonoTrampInfo *info)
        mono_tramp_info_free (info);
 }
 
+static void
+mono_tramp_info_cleanup (void)
+{
+       GSList *l;
+
+       for (l = tramp_infos; l; l = l->next) {
+               MonoTrampInfo *info = l->data;
+
+               mono_tramp_info_free (info);
+       }
+       g_slist_free (tramp_infos);
+}
+
 G_GNUC_UNUSED static void
 break_count (void)
 {
@@ -2704,6 +2717,12 @@ mono_set_lmf (MonoLMF *lmf)
        (*mono_get_lmf_addr ()) = lmf;
 }
 
+MonoJitTlsData*
+mono_get_jit_tls (void)
+{
+       return mono_native_tls_get_value (mono_jit_tls_id);
+}
+
 static void
 mono_set_jit_tls (MonoJitTlsData *jit_tls)
 {
@@ -3132,15 +3151,12 @@ mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
                info = mono_mempool_alloc (mp, sizeof (MonoGSharedVtMethodInfo));
                res->data.gsharedvt_method = info;
                memcpy (info, oinfo, sizeof (MonoGSharedVtMethodInfo));
-               info->entries = g_ptr_array_new ();
-               if (oinfo->entries) {
-                       for (i = 0; i < oinfo->entries->len; ++i) {
-                               MonoRuntimeGenericContextInfoTemplate *otemplate = g_ptr_array_index (oinfo->entries, i);
-                               MonoRuntimeGenericContextInfoTemplate *template = mono_mempool_alloc0 (mp, sizeof (MonoRuntimeGenericContextInfoTemplate));
+               info->entries = mono_mempool_alloc (mp, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->count_entries);
+               for (i = 0; i < oinfo->num_entries; ++i) {
+                       MonoRuntimeGenericContextInfoTemplate *otemplate = &oinfo->entries [i];
+                       MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
 
-                               memcpy (template, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
-                               g_ptr_array_add (info->entries, template);
-                       }
+                       memcpy (template, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
                }
                //info->locals_types = mono_mempool_alloc0 (mp, info->nlocals * sizeof (MonoType*));
                //memcpy (info->locals_types, oinfo->locals_types, info->nlocals * sizeof (MonoType*));
@@ -3204,6 +3220,8 @@ mono_patch_info_hash (gconstpointer data)
        case MONO_PATCH_INFO_MONITOR_EXIT:
        case MONO_PATCH_INFO_CASTCLASS_CACHE:
        case MONO_PATCH_INFO_GOT_OFFSET:
+       case MONO_PATCH_INFO_NURSERY_START_SHIFTED:
+       case MONO_PATCH_INFO_NURSERY_SHIFT:
                return (ji->type << 8);
        case MONO_PATCH_INFO_SWITCH:
                return (ji->type << 8) | ji->data.table->table_size;
@@ -3579,13 +3597,13 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                        /* Make a copy into the domain mempool */
                        info = g_malloc0 (sizeof (MonoGSharedVtMethodInfo)); //mono_domain_alloc0 (domain, sizeof (MonoGSharedVtMethodInfo));
                        info->method = oinfo->method;
-                       info->entries = g_ptr_array_new ();
-                       for (i = 0; i < oinfo->entries->len; ++i) {
-                               MonoRuntimeGenericContextInfoTemplate *otemplate = g_ptr_array_index (oinfo->entries, i);
-                               MonoRuntimeGenericContextInfoTemplate *template = g_malloc0 (sizeof (MonoRuntimeGenericContextInfoTemplate));
+                       info->num_entries = oinfo->num_entries;
+                       info->entries = g_malloc0 (sizeof (MonoRuntimeGenericContextInfoTemplate) * info->num_entries);
+                       for (i = 0; i < oinfo->num_entries; ++i) {
+                               MonoRuntimeGenericContextInfoTemplate *otemplate = &oinfo->entries [i];
+                               MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
 
                                memcpy (template, otemplate, sizeof (MonoRuntimeGenericContextInfoTemplate));
-                               g_ptr_array_add (info->entries, template);
                        }
                        slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, info, entry->info_type, mono_method_get_context (entry->method));
                        break;
@@ -3653,6 +3671,23 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                target = NULL;
                break;
        }
+       case MONO_PATCH_INFO_NURSERY_START_SHIFTED: {
+               size_t nursery_size;
+               int nursery_shift;
+               gpointer nursery_start = mono_gc_get_nursery (&nursery_shift, &nursery_size);
+
+               target = (gpointer)((gsize)nursery_start >> nursery_shift);
+               break;
+       }
+       case MONO_PATCH_INFO_NURSERY_SHIFT: {
+               size_t nursery_size;
+               int nursery_shift;
+
+               mono_gc_get_nursery (&nursery_shift, &nursery_size);
+
+               target = GINT_TO_POINTER (nursery_shift);
+               break;
+       }
        default:
                g_assert_not_reached ();
        }
@@ -4276,6 +4311,9 @@ create_jit_info_for_trampoline (MonoMethod *wrapper, MonoTrampInfo *info)
        jinfo->code_size = info->code_size;
        jinfo->used_regs = mono_cache_unwind_info (uw_info, info_len);
 
+       if (!info->uw_info)
+               g_free (uw_info);
+
        return jinfo;
 }
 
@@ -4375,7 +4413,7 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
                        gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
                else
                        gi->generic_sharing_context = mono_domain_alloc0 (cfg->domain, sizeof (MonoGenericSharingContext));
-               memcpy (gi->generic_sharing_context, cfg->generic_sharing_context, sizeof (MonoGenericSharingContext));
+               mini_init_gsctx (cfg->method->dynamic ? NULL : cfg->domain, NULL, cfg->gsctx_context, gi->generic_sharing_context);
 
                if ((method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) ||
                                mini_method_get_context (method_to_compile)->method_inst ||
@@ -4493,12 +4531,24 @@ create_jit_info (MonoCompile *cfg, MonoMethod *method_to_compile)
                        MonoExceptionClause *ec = &header->clauses [i];
                        MonoJitExceptionInfo *ei = &jinfo->clauses [i];
                        MonoBasicBlock *tblock;
-                       MonoInst *exvar;
+                       MonoInst *exvar, *spvar;
 
                        ei->flags = ec->flags;
 
-                       exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
-                       ei->exvar_offset = exvar ? exvar->inst_offset : 0;
+                       /*
+                        * The spvars are needed by mono_arch_install_handler_block_guard ().
+                        */
+                       if (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
+                               int region;
+
+                               region = ((i + 1) << 8) | MONO_REGION_FINALLY | ec->flags;
+                               spvar = mono_find_spvar_for_region (cfg, region);
+                               g_assert (spvar);
+                               ei->exvar_offset = spvar->inst_offset;
+                       } else {
+                               exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
+                               ei->exvar_offset = exvar ? exvar->inst_offset : 0;
+                       }
 
                        if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
                                tblock = cfg->cil_offset_to_bb [ec->data.filter_offset];
@@ -4801,16 +4851,21 @@ mini_get_shared_method (MonoMethod *method)
 }
 
 void
-mini_init_gsctx (MonoGenericContext *context, MonoGenericSharingContext *gsctx)
+mini_init_gsctx (MonoDomain *domain, MonoMemPool *mp, MonoGenericContext *context, MonoGenericSharingContext *gsctx)
 {
        MonoGenericInst *inst;
        int i;
 
        memset (gsctx, 0, sizeof (MonoGenericSharingContext));
 
-       if (context->class_inst) {
+       if (context && context->class_inst) {
                inst = context->class_inst;
-               gsctx->var_is_vt = g_new0 (gboolean, inst->type_argc);
+               if (domain)
+                       gsctx->var_is_vt = mono_domain_alloc0 (domain, sizeof (gboolean) * inst->type_argc);
+               else if (mp)
+                       gsctx->var_is_vt = mono_mempool_alloc0 (mp, sizeof (gboolean) * inst->type_argc);
+               else
+                       gsctx->var_is_vt = g_new0 (gboolean, inst->type_argc);
 
                for (i = 0; i < inst->type_argc; ++i) {
                        MonoType *type = inst->type_argv [i];
@@ -4819,9 +4874,14 @@ mini_init_gsctx (MonoGenericContext *context, MonoGenericSharingContext *gsctx)
                                gsctx->var_is_vt [i] = TRUE;
                }
        }
-       if (context->method_inst) {
+       if (context && context->method_inst) {
                inst = context->method_inst;
-               gsctx->mvar_is_vt = g_new0 (gboolean, inst->type_argc);
+               if (domain)
+                       gsctx->mvar_is_vt = mono_domain_alloc0 (domain, sizeof (gboolean) * inst->type_argc);
+               else if (mp)
+                       gsctx->mvar_is_vt = mono_mempool_alloc0 (mp, sizeof (gboolean) * inst->type_argc);
+               else
+                       gsctx->mvar_is_vt = g_new0 (gboolean, inst->type_argc);
 
                for (i = 0; i < inst->type_argc; ++i) {
                        MonoType *type = inst->type_argv [i];
@@ -4949,7 +5009,6 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFl
                MonoMethodInflated *inflated;
                MonoGenericContext *context;
 
-               // FIXME: Free the contents of gsctx if compilation fails
                if (method_is_gshared) {
                        g_assert (method->is_inflated);
                        inflated = (MonoMethodInflated*)method;
@@ -4963,7 +5022,8 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, JitFl
                        context = &inflated->context;
                }
 
-               mini_init_gsctx (context, &cfg->gsctx);
+               mini_init_gsctx (NULL, cfg->mempool, context, &cfg->gsctx);
+               cfg->gsctx_context = context;
 
                cfg->gsharedvt = TRUE;
                // FIXME:
@@ -7034,6 +7094,13 @@ delete_jump_list (gpointer key, gpointer value, gpointer user_data)
        g_slist_free (jlist->list);
 }
 
+static void
+delete_got_slot_list (gpointer key, gpointer value, gpointer user_data)
+{
+       GSList *list = value;
+       g_slist_free (list);
+}
+
 static void
 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
 {
@@ -7075,7 +7142,7 @@ mini_free_jit_domain_info (MonoDomain *domain)
        g_hash_table_foreach (info->jump_target_hash, delete_jump_list, NULL);
        g_hash_table_destroy (info->jump_target_hash);
        if (info->jump_target_got_slot_hash) {
-               g_hash_table_foreach (info->jump_target_got_slot_hash, delete_jump_list, NULL);
+               g_hash_table_foreach (info->jump_target_got_slot_hash, delete_got_slot_list, NULL);
                g_hash_table_destroy (info->jump_target_got_slot_hash);
        }
        if (info->dynamic_code_hash) {
@@ -7669,6 +7736,8 @@ mini_cleanup (MonoDomain *domain)
        g_free (emul_opcode_opcodes);
        g_free (vtable_trampolines);
 
+       mono_tramp_info_cleanup ();
+
        mono_arch_cleanup ();
 
        mono_generic_sharing_cleanup ();
@@ -7856,7 +7925,7 @@ mono_arch_get_gsharedvt_trampoline (MonoTrampInfo **info, gboolean aot)
 
 #endif
 
-#if defined(MONO_ARCH_GSHAREDVT_SUPPORTED) && !defined(MONO_GSHARING)
+#if defined(MONO_ARCH_GSHAREDVT_SUPPORTED) && !defined(ENABLE_GSHAREDVT)
 
 gboolean
 mono_arch_gsharedvt_sig_supported (MonoMethodSignature *sig)