Fix the build.
[mono.git] / mono / mini / aot-compiler.c
index 5d0cd89cfeb838e6affd664457f6b2b7dab665f0..70e293d123d782331054c6abf1aa31b5916be2c3 100644 (file)
@@ -40,6 +40,7 @@
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/metadata-internals.h>
+#include <mono/metadata/reflection-internals.h>
 #include <mono/metadata/marshal.h>
 #include <mono/metadata/gc-internals.h>
 #include <mono/metadata/mempool-internals.h>
@@ -152,8 +153,8 @@ typedef struct MonoAotStats {
        int methods_without_got_slots, direct_calls, all_calls, llvm_count;
        int got_slots, offsets_size;
        int method_categories [METHOD_CAT_NUM];
-       int got_slot_types [MONO_PATCH_INFO_NONE];
-       int got_slot_info_sizes [MONO_PATCH_INFO_NONE];
+       int got_slot_types [MONO_PATCH_INFO_NUM];
+       int got_slot_info_sizes [MONO_PATCH_INFO_NUM];
        int jit_time, gen_time, link_time;
 } MonoAotStats;
 
@@ -3121,7 +3122,7 @@ is_plt_patch (MonoJumpInfo *patch_info)
        case MONO_PATCH_INFO_METHOD:
        case MONO_PATCH_INFO_INTERNAL_METHOD:
        case MONO_PATCH_INFO_JIT_ICALL_ADDR:
-       case MONO_PATCH_INFO_ICALL_ADDR:
+       case MONO_PATCH_INFO_ICALL_ADDR_CALL:
        case MONO_PATCH_INFO_RGCTX_FETCH:
                return TRUE;
        default:
@@ -3745,7 +3746,11 @@ add_wrappers (MonoAotCompile *acfg)
                        if (method)
                                add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
 
-                       cattr = mono_custom_attrs_from_class (klass);
+                       cattr = mono_custom_attrs_from_class_checked (klass, &error);
+                       if (!is_ok (&error)) {
+                               mono_error_cleanup (&error);
+                               continue;
+                       }
 
                        if (cattr) {
                                int j;
@@ -3910,7 +3915,12 @@ add_wrappers (MonoAotCompile *acfg)
                 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
                 * name to avoid defining a new assembly to contain it.
                 */
-               cattr = mono_custom_attrs_from_method (method);
+               cattr = mono_custom_attrs_from_method_checked (method, &error);
+               if (!is_ok (&error)) {
+                       char *name = mono_method_get_full_name (method);
+                       report_loader_error (acfg, &error, "Failed to load custom attributes from method %s due to %s\n", name, mono_error_get_message (&error));
+                       g_free (name);
+               }
 
                if (cattr) {
                        for (j = 0; j < cattr->num_attrs; ++j)
@@ -3950,8 +3960,9 @@ add_wrappers (MonoAotCompile *acfg)
                                slen = mono_metadata_decode_value (p, &p);
                                n = (char *)g_memdup (p, slen + 1);
                                n [slen] = 0;
-                               t = mono_reflection_type_from_name (n, acfg->image);
+                               t = mono_reflection_type_from_name_checked (n, acfg->image, &error);
                                g_assert (t);
+                               mono_error_assert_ok (&error);
                                g_free (n);
 
                                klass = mono_class_from_mono_type (t);
@@ -4156,7 +4167,7 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
        if (!klass->generic_class && !klass->rank)
                return;
 
-       if (klass->exception_type)
+       if (mono_class_has_failure (klass))
                return;
 
        if (!acfg->ginst_hash)
@@ -4246,6 +4257,7 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
 
                /* Add the T[]/InternalEnumerator class */
                if (!strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IEnumerator`1")) {
+                       MonoError error;
                        MonoClass *nclass;
 
                        iter = NULL;
@@ -4254,7 +4266,8 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
                                        break;
                        }
                        g_assert (nclass);
-                       nclass = mono_class_inflate_generic_class (nclass, mono_generic_class_get_context (klass->generic_class));
+                       nclass = mono_class_inflate_generic_class_checked (nclass, mono_generic_class_get_context (klass->generic_class), &error);
+                       mono_error_assert_ok (&error); /* FIXME don't swallow the error */
                        add_generic_class (acfg, nclass, FALSE, "ICollection<T>");
                }
 
@@ -4272,8 +4285,9 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
 
        /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
        if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "Comparer`1")) {
+               MonoError error;
                MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
-               MonoClass *icomparable, *gcomparer;
+               MonoClass *icomparable, *gcomparer, *icomparable_inst;
                MonoGenericContext ctx;
                MonoType *args [16];
 
@@ -4284,16 +4298,24 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
                args [0] = &tclass->byval_arg;
                ctx.class_inst = mono_metadata_get_generic_inst (1, args);
 
-               if (mono_class_is_assignable_from (mono_class_inflate_generic_class (icomparable, &ctx), tclass)) {
+               icomparable_inst = mono_class_inflate_generic_class_checked (icomparable, &ctx, &error);
+               mono_error_assert_ok (&error); /* FIXME don't swallow the error */
+
+               if (mono_class_is_assignable_from (icomparable_inst, tclass)) {
+                       MonoClass *gcomparer_inst;
                        gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericComparer`1");
-                       add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx), FALSE, "Comparer<T>");
+                       gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, &error);
+                       mono_error_assert_ok (&error); /* FIXME don't swallow the error */
+
+                       add_generic_class (acfg, gcomparer_inst, FALSE, "Comparer<T>");
                }
        }
 
        /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
        if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "EqualityComparer`1")) {
+               MonoError error;
                MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
-               MonoClass *iface, *gcomparer;
+               MonoClass *iface, *gcomparer, *iface_inst;
                MonoGenericContext ctx;
                MonoType *args [16];
 
@@ -4304,9 +4326,17 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
                args [0] = &tclass->byval_arg;
                ctx.class_inst = mono_metadata_get_generic_inst (1, args);
 
-               if (mono_class_is_assignable_from (mono_class_inflate_generic_class (iface, &ctx), tclass)) {
+               iface_inst = mono_class_inflate_generic_class_checked (iface, &ctx, &error);
+               mono_error_assert_ok (&error); /* FIXME don't swallow the error */
+
+               if (mono_class_is_assignable_from (iface_inst, tclass)) {
+                       MonoClass *gcomparer_inst;
+                       MonoError error;
+
                        gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericEqualityComparer`1");
-                       add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx), FALSE, "EqualityComparer<T>");
+                       gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, &error);
+                       mono_error_assert_ok (&error); /* FIXME don't swallow the error */
+                       add_generic_class (acfg, gcomparer_inst, FALSE, "EqualityComparer<T>");
                }
        }
 
@@ -4318,12 +4348,17 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
                MonoType *args [16];
 
                if (mono_class_is_enum (tclass)) {
+                       MonoClass *enum_comparer_inst;
+                       MonoError error;
+
                        memset (&ctx, 0, sizeof (ctx));
                        args [0] = &tclass->byval_arg;
                        ctx.class_inst = mono_metadata_get_generic_inst (1, args);
 
                        enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "EnumEqualityComparer`1");
-                       add_generic_class (acfg, mono_class_inflate_generic_class (enum_comparer, &ctx), FALSE, "EqualityComparer<T>");
+                       enum_comparer_inst = mono_class_inflate_generic_class_checked (enum_comparer, &ctx, &error);
+                       mono_error_assert_ok (&error); /* FIXME don't swallow the error */
+                       add_generic_class (acfg, enum_comparer_inst, FALSE, "EqualityComparer<T>");
                }
        }
 
@@ -4335,12 +4370,17 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth,
                MonoType *args [16];
 
                if (mono_class_is_enum (tclass)) {
+                       MonoClass *comparer_inst;
+                       MonoError error;
+
                        memset (&ctx, 0, sizeof (ctx));
                        args [0] = &tclass->byval_arg;
                        ctx.class_inst = mono_metadata_get_generic_inst (1, args);
 
                        comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "ObjectComparer`1");
-                       add_generic_class (acfg, mono_class_inflate_generic_class (comparer, &ctx), FALSE, "Comparer<T>");
+                       comparer_inst = mono_class_inflate_generic_class_checked (comparer, &ctx, &error);
+                       mono_error_assert_ok (&error); /* FIXME don't swallow the error */
+                       add_generic_class (acfg, comparer_inst, FALSE, "Comparer<T>");
                }
        }
 }
@@ -4358,15 +4398,20 @@ add_instances_of (MonoAotCompile *acfg, MonoClass *klass, MonoType **insts, int
        memset (&ctx, 0, sizeof (ctx));
 
        for (i = 0; i < ninsts; ++i) {
+               MonoError error;
+               MonoClass *generic_inst;
                args [0] = insts [i];
                ctx.class_inst = mono_metadata_get_generic_inst (1, args);
-               add_generic_class (acfg, mono_class_inflate_generic_class (klass, &ctx), force, "");
+               generic_inst = mono_class_inflate_generic_class_checked (klass, &ctx, &error);
+               mono_error_assert_ok (&error); /* FIXME don't swallow the error */
+               add_generic_class (acfg, generic_inst, force, "");
        }
 }
 
 static void
 add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method)
 {
+       MonoError error;
        MonoMethodHeader *header;
        MonoMethodSignature *sig;
        int j, depth;
@@ -4381,14 +4426,14 @@ add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method)
                                add_generic_class_with_depth (acfg, mono_class_from_mono_type (sig->params [j]), depth + 1, "arg");
        }
 
-       header = mono_method_get_header (method);
+       header = mono_method_get_header_checked (method, &error);
 
        if (header) {
                for (j = 0; j < header->num_locals; ++j)
                        if (header->locals [j]->type == MONO_TYPE_GENERICINST)
                                add_generic_class_with_depth (acfg, mono_class_from_mono_type (header->locals [j]), depth + 1, "local");
        } else {
-               mono_loader_clear_error ();
+               mono_error_cleanup (&error); /* FIXME report the error */
        }
 }
 
@@ -4737,10 +4782,10 @@ is_direct_callable (MonoAotCompile *acfg, MonoMethod *method, MonoJumpInfo *patc
                        if (direct_callable)
                                return TRUE;
                }
-       } else if ((patch_info->type == MONO_PATCH_INFO_ICALL_ADDR && patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
+       } else if ((patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL && patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
                if (acfg->aot_opts.direct_pinvoke)
                        return TRUE;
-       } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR) {
+       } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
                if (acfg->aot_opts.direct_icalls)
                        return TRUE;
                return FALSE;
@@ -5023,7 +5068,7 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                                        }
 
                                        acfg->stats.all_calls ++;
-                               } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR) {
+                               } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
                                        if (!got_only && is_direct_callable (acfg, method, patch_info)) {
                                                if (!(patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
                                                        direct_pinvoke = mono_lookup_icall_symbol (patch_info->data.method);
@@ -5337,6 +5382,7 @@ encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint
        case MONO_PATCH_INFO_METHOD:
        case MONO_PATCH_INFO_METHOD_JUMP:
        case MONO_PATCH_INFO_ICALL_ADDR:
+       case MONO_PATCH_INFO_ICALL_ADDR_CALL:
        case MONO_PATCH_INFO_METHOD_RGCTX:
        case MONO_PATCH_INFO_METHOD_CODE_SLOT:
                encode_method_ref (acfg, patch_info->data.method, p, &p);
@@ -5558,11 +5604,13 @@ emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
        buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
        p = buf = (guint8 *)g_malloc (buf_size);
 
-       if (mono_class_get_cctor (method->klass))
+       if (mono_class_get_cctor (method->klass)) {
+               encode_value (1, p, &p);
                encode_klass_ref (acfg, method->klass, p, &p);
-       else
+       } else {
                /* Not needed when loading the method */
                encode_value (0, p, &p);
+       }
 
        g_assert (!(cfg->opt & MONO_OPT_SHARED));
 
@@ -6010,7 +6058,8 @@ get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cac
        case MONO_PATCH_INFO_RGCTX_FETCH:
                debug_sym = g_strdup_printf ("%s_rgctx_fetch_%d", prefix, acfg->label_generator ++);
                break;
-       case MONO_PATCH_INFO_ICALL_ADDR: {
+       case MONO_PATCH_INFO_ICALL_ADDR:
+       case MONO_PATCH_INFO_ICALL_ADDR_CALL: {
                char *s = get_debug_sym (ji->data.method, "", cache);
                
                debug_sym = g_strdup_printf ("%s_icall_native_%s", prefix, s);
@@ -7405,6 +7454,7 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
        }
        /* Make a copy of the argument/local info */
        {
+               MonoError error;
                MonoInst **args, **locals;
                MonoMethodSignature *sig;
                MonoMethodHeader *header;
@@ -7418,7 +7468,8 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                }
                cfg->args = args;
 
-               header = mono_method_get_header (method);
+               header = mono_method_get_header_checked (method, &error);
+               mono_error_assert_ok (&error); /* FIXME don't swallow the error */
                locals = (MonoInst **)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
                for (i = 0; i < header->num_locals; ++i) {
                        locals [i] = (MonoInst *)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
@@ -7788,7 +7839,7 @@ mono_aot_get_direct_call_symbol (MonoJumpInfoType type, gconstpointer data)
                if (type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
                        /* Call to a C function implementing a jit icall */
                        sym = mono_lookup_jit_icall_symbol ((const char *)data);
-               } else if (type == MONO_PATCH_INFO_ICALL_ADDR) {
+               } else if (type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
                        MonoMethod *method = (MonoMethod *)data;
                        if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
                                sym = mono_lookup_icall_symbol (method);
@@ -7818,7 +7869,7 @@ mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
                if (type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
                        /* Call to a C function implementing a jit icall */
                        sym = mono_lookup_jit_icall_symbol ((const char *)data);
-               } else if (type == MONO_PATCH_INFO_ICALL_ADDR) {
+               } else if (type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
                        MonoMethod *method = (MonoMethod *)data;
                        if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
                                sym = mono_lookup_icall_symbol (method);
@@ -10013,11 +10064,11 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
 
        memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
        acfg->aot_opts.write_symbols = TRUE;
-       acfg->aot_opts.ntrampolines = 1024;
-       acfg->aot_opts.nrgctx_trampolines = 1024;
-       acfg->aot_opts.nimt_trampolines = 128;
+       acfg->aot_opts.ntrampolines = 4096;
+       acfg->aot_opts.nrgctx_trampolines = 4096;
+       acfg->aot_opts.nimt_trampolines = 512;
        acfg->aot_opts.nrgctx_fetch_trampolines = 128;
-       acfg->aot_opts.ngsharedvt_arg_trampolines = 128;
+       acfg->aot_opts.ngsharedvt_arg_trampolines = 512;
        acfg->aot_opts.llvm_path = g_strdup ("");
        acfg->aot_opts.temp_path = g_strdup ("");
 #ifdef MONOTOUCH
@@ -10425,7 +10476,7 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
                int i;
 
                aot_printf (acfg, "GOT slot distribution:\n");
-               for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
+               for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
                        if (acfg->stats.got_slot_types [i])
                                aot_printf (acfg, "\t%s: %d (%d)\n", get_patch_name (i), acfg->stats.got_slot_types [i], acfg->stats.got_slot_info_sizes [i]);
                aot_printf (acfg, "\nMethod stats:\n");