2006-07-19 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini.c
index f8239ccc7207f35592b4c60234935307b99345a0..10f42e6e2bbca395e8a9b2de8e1e629813256313 100644 (file)
 #include <math.h>
 #include <sys/time.h>
 
-#ifdef sun    // Solaris x86
-#include <sys/types.h>
-#include <sys/ucontext.h>
-#endif
-
 #ifdef HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
 #endif
@@ -52,6 +47,7 @@
 #include <mono/utils/mono-math.h>
 #include <mono/utils/mono-compiler.h>
 #include <mono/utils/mono-counters.h>
+#include <mono/utils/mono-logger.h>
 #include <mono/os/gc_wrapper.h>
 
 #include "mini.h"
@@ -78,6 +74,8 @@
 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= CEE_BEQ && (ins)->opcode <= CEE_BLT_UN) || ((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN) || ((ins)->opcode >= OP_IBEQ && (ins)->opcode <= OP_IBLT_UN))
 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
 
+#define MONO_IS_BRANCH_OP(ins) (MONO_IS_COND_BRANCH_OP(ins) || ((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_BR_REG) || ((ins)->opcode == CEE_SWITCH))
+
 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
 
 static void setup_stat_profiler (void);
@@ -88,7 +86,7 @@ static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *m
 static gpointer mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method);
 
 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, 
-                         const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
+                         const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier);
 
 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
 
@@ -101,6 +99,7 @@ static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
 static MonoMethodSignature *helper_sig_domain_get = NULL;
 
 static guint32 default_opt = 0;
+static gboolean default_opt_set = FALSE;
 
 guint32 mono_jit_tls_id = -1;
 MonoTraceSpec *mono_jit_trace_calls = NULL;
@@ -188,9 +187,9 @@ get_method_from_ip (void *ip)
 {
        MonoJitInfo *ji;
        char *method;
-       char *source;
        char *res;
        MonoDomain *domain = mono_domain_get ();
+       MonoDebugSourceLocation *location;
        FindTrampUserData user_data;
        
        ji = mono_jit_info_table_find (domain, ip);
@@ -210,11 +209,12 @@ get_method_from_ip (void *ip)
                        return NULL;
        }
        method = mono_method_full_name (ji->method, TRUE);
-       source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
+       /* FIXME: unused ? */
+       location = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
 
        res = g_strdup_printf (" %s + 0x%x (%p %p) [%p - %s]", method, (int)((char*)ip - (char*)ji->code_start), ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
 
-       g_free (source);
+       mono_debug_free_source_location (location);
        g_free (method);
 
        return res;
@@ -232,7 +232,7 @@ mono_print_method_from_ip (void *ip)
 {
        MonoJitInfo *ji;
        char *method;
-       char *source;
+       MonoDebugSourceLocation *source;
        MonoDomain *domain = mono_domain_get ();
        FindTrampUserData user_data;
        
@@ -253,14 +253,14 @@ mono_print_method_from_ip (void *ip)
                return;
        }
        method = mono_method_full_name (ji->method, TRUE);
-       source = mono_debug_source_location_from_address (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), NULL, domain);
+       source = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
 
        g_print ("IP %p at offset 0x%x of method %s (%p %p)[domain %p - %s]\n", ip, (int)((char*)ip - (char*)ji->code_start), method, ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
 
        if (source)
-               g_print ("%s\n", source);
+               g_print ("%s:%d\n", source->source_file, source->row);
 
-       g_free (source);
+       mono_debug_free_source_location (source);
        g_free (method);
 }
        
@@ -329,6 +329,9 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
         (vi)->idx = (id); \
 } while (0)
 
+//#define UNVERIFIED do { G_BREAKPOINT (); goto unverified; } while (0)
+#define UNVERIFIED do { goto unverified; } while (0)
+
 /*
  * Basic blocks have two numeric identifiers:
  * dfn: Depth First Number
@@ -344,7 +347,7 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do {  \
                (tblock) = g_hash_table_lookup (bbhash, (ip));  \
                if (!(tblock)) {        \
-                       if ((ip) >= end || (ip) < header->code) goto unverified; \
+                       if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
                        (tblock) = NEW_BBLOCK (cfg);    \
                        (tblock)->cil_code = (ip);      \
                        ADD_BBLOCK (cfg, (bbhash), (tblock));   \
@@ -399,7 +402,7 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
                (dest)->type = STACK_PTR;                               \
        } while (0)
 
-#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do { \
+#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
                MonoInst *group, *got_var, *got_loc;                    \
                (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
                (dest)->opcode = OP_GOT_ENTRY;                          \
@@ -409,7 +412,8 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
                group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
                (dest)->inst_p0 = got_var;                              \
                (dest)->inst_p1 = group;                                \
-               (dest)->type = (stack_type);                            \
+               (dest)->type = (stack_type);                    \
+        (dest)->klass = (stack_class);          \
        } while (0)
 
 #else
@@ -422,12 +426,13 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
                (dest)->type = STACK_PTR;       \
     } while (0)
 
-#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type) do {    \
+#define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
                (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
                (dest)->opcode = OP_AOTCONST;   \
                (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token));  \
                (dest)->inst_p1 = (gpointer)(patch_type); \
                (dest)->type = (stack_type);    \
+        (dest)->klass = (stack_class);          \
     } while (0)
 
 #endif
@@ -444,15 +449,15 @@ mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
 
 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
 
-#define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ)
+#define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
 
-#define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ)
+#define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ, mono_defaults.monotype_class)
 
-#define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR)
+#define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
 
 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
                if (cfg->compile_aot) { \
-                       NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ); \
+                       NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
                } else { \
                        NEW_PCONST (cfg, args [0], (entry).blob); \
                } \
@@ -782,7 +787,7 @@ link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
  *
  *   Unlink two basic blocks.
  */
-void
+static void
 mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
 {
        int i, pos;
@@ -1070,117 +1075,6 @@ reverse_branch_op (guint32 opcode)
        return opcode;
 }
 
-guint
-mono_type_to_ldind (MonoType *type)
-{
-       if (type->byref)
-               return CEE_LDIND_I;
-
-handle_enum:
-       switch (type->type) {
-       case MONO_TYPE_I1:
-               return CEE_LDIND_I1;
-       case MONO_TYPE_U1:
-       case MONO_TYPE_BOOLEAN:
-               return CEE_LDIND_U1;
-       case MONO_TYPE_I2:
-               return CEE_LDIND_I2;
-       case MONO_TYPE_U2:
-       case MONO_TYPE_CHAR:
-               return CEE_LDIND_U2;
-       case MONO_TYPE_I4:
-               return CEE_LDIND_I4;
-       case MONO_TYPE_U4:
-               return CEE_LDIND_U4;
-       case MONO_TYPE_I:
-       case MONO_TYPE_U:
-       case MONO_TYPE_PTR:
-       case MONO_TYPE_FNPTR:
-               return CEE_LDIND_I;
-       case MONO_TYPE_CLASS:
-       case MONO_TYPE_STRING:
-       case MONO_TYPE_OBJECT:
-       case MONO_TYPE_SZARRAY:
-       case MONO_TYPE_ARRAY:    
-               return CEE_LDIND_REF;
-       case MONO_TYPE_I8:
-       case MONO_TYPE_U8:
-               return CEE_LDIND_I8;
-       case MONO_TYPE_R4:
-               return CEE_LDIND_R4;
-       case MONO_TYPE_R8:
-               return CEE_LDIND_R8;
-       case MONO_TYPE_VALUETYPE:
-               if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
-                       goto handle_enum;
-               }
-               return CEE_LDOBJ;
-       case MONO_TYPE_TYPEDBYREF:
-               return CEE_LDOBJ;
-       case MONO_TYPE_GENERICINST:
-               type = &type->data.generic_class->container_class->byval_arg;
-               goto handle_enum;
-       default:
-               g_error ("unknown type 0x%02x in type_to_ldind", type->type);
-       }
-       return -1;
-}
-
-guint
-mono_type_to_stind (MonoType *type)
-{
-       if (type->byref)
-               return CEE_STIND_I;
-
-handle_enum:
-       switch (type->type) {
-       case MONO_TYPE_I1:
-       case MONO_TYPE_U1:
-       case MONO_TYPE_BOOLEAN:
-               return CEE_STIND_I1;
-       case MONO_TYPE_I2:
-       case MONO_TYPE_U2:
-       case MONO_TYPE_CHAR:
-               return CEE_STIND_I2;
-       case MONO_TYPE_I4:
-       case MONO_TYPE_U4:
-               return CEE_STIND_I4;
-       case MONO_TYPE_I:
-       case MONO_TYPE_U:
-       case MONO_TYPE_PTR:
-       case MONO_TYPE_FNPTR:
-               return CEE_STIND_I;
-       case MONO_TYPE_CLASS:
-       case MONO_TYPE_STRING:
-       case MONO_TYPE_OBJECT:
-       case MONO_TYPE_SZARRAY:
-       case MONO_TYPE_ARRAY:    
-               return CEE_STIND_REF;
-       case MONO_TYPE_I8:
-       case MONO_TYPE_U8:
-               return CEE_STIND_I8;
-       case MONO_TYPE_R4:
-               return CEE_STIND_R4;
-       case MONO_TYPE_R8:
-               return CEE_STIND_R8;
-       case MONO_TYPE_VALUETYPE:
-               if (type->data.klass->enumtype) {
-                       type = type->data.klass->enum_basetype;
-                       goto handle_enum;
-               }
-               return CEE_STOBJ;
-       case MONO_TYPE_TYPEDBYREF:
-               return CEE_STOBJ;
-       case MONO_TYPE_GENERICINST:
-               type = &type->data.generic_class->container_class->byval_arg;
-               goto handle_enum;
-       default:
-               g_error ("unknown type 0x%02x in type_to_stind", type->type);
-       }
-       return -1;
-}
-
 /*
  * Returns the type used in the eval stack when @type is loaded.
  * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
@@ -1720,7 +1614,19 @@ type_from_stack_type (MonoInst *ins) {
        case STACK_I8: return &mono_defaults.int64_class->byval_arg;
        case STACK_PTR: return &mono_defaults.int_class->byval_arg;
        case STACK_R8: return &mono_defaults.double_class->byval_arg;
-       case STACK_MP: return &mono_defaults.int_class->byval_arg;
+       case STACK_MP:
+               /* 
+                * FIXME: This doesn't work because mono_class_from_mono_type ()
+                * returns the original klass for a byref type, not a 'byref' class,
+                * causing the JIT to create variables with the wrong type, for
+                * example.
+                */
+               /*
+               if (ins->klass)
+                       return &ins->klass->this_arg;
+               else
+               */
+                       return &mono_defaults.object_class->this_arg;
        case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
        case STACK_VTYPE: return &ins->klass->byval_arg;
        default:
@@ -1828,7 +1734,7 @@ mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest
        NEW_TEMPSTORE (cfg, inst, dest, load);
        if (inst->opcode == CEE_STOBJ) {
                NEW_TEMPLOADA (cfg, inst, dest);
-               handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
+               handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE, FALSE);
        } else {
                inst->cil_code = NULL;
                mono_add_ins_to_end (bb, inst);
@@ -1868,6 +1774,66 @@ mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
        return res;
 }
 
+/*
+ * merge_stacks:
+ *
+ * Merge stack state between two basic blocks according to Ecma 335, Partition III,
+ * section 1.8.1.1. Store the resulting stack state into stack_2.
+ * Returns: TRUE, if verification succeeds, FALSE otherwise.
+ * FIXME: We should store the stack state in a dedicated structure instead of in
+ * MonoInst's.
+ */
+static gboolean
+merge_stacks (MonoCompile *cfg, MonoStackSlot *state_1, MonoStackSlot *state_2, guint32 size)
+{
+       int i;
+
+       if (cfg->dont_verify_stack_merge)
+               return TRUE;
+
+       /* FIXME: Implement all checks from the spec */
+
+       for (i = 0; i < size; ++i) {
+               MonoStackSlot *slot1 = &state_1 [i];
+               MonoStackSlot *slot2 = &state_2 [i];
+
+               if (slot1->type != slot2->type)
+                       return FALSE;
+
+               switch (slot1->type) {
+               case STACK_PTR:
+                       /* FIXME: Perform merge ? */
+                       /* klass == NULL means a native int */
+                       if (slot1->klass && slot2->klass) {
+                               if (slot1->klass != slot2->klass)
+                                       return FALSE;
+                       }
+                       break;
+               case STACK_MP:
+                       /* FIXME: Change this to an assert and fix the JIT to allways fill this */
+                       if (slot1->klass && slot2->klass) {
+                               if (slot1->klass != slot2->klass)
+                                       return FALSE;
+                       }
+                       break;
+               case STACK_OBJ: {
+                       MonoClass *klass1 = slot1->klass;
+                       MonoClass *klass2 = slot2->klass;
+
+                       if (!klass1) {
+                               /* slot1 is ldnull */
+                       } else if (!klass2) {
+                               /* slot2 is ldnull */
+                               slot2->klass = slot1->klass;
+                       }
+                       break;
+               }
+               }
+       }
+
+       return TRUE;
+}
+
 /*
  * This function is called to handle items that are left on the evaluation stack
  * at basic block boundaries. What happens is that we save the values to local variables
@@ -1878,18 +1844,64 @@ mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
  * which case its old value should be used.
  * A single joint point will use the same variables (stored in the array bb->out_stack or
  * bb->in_stack, if the basic block is before or after the joint point).
+ * If the stack merge fails at a join point, cfg->unverifiable is set.
  */
 static int
 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
        int i, bindex;
        MonoBasicBlock *outb;
        MonoInst *inst, **locals;
+       MonoStackSlot *stack_state;
        gboolean found;
 
        if (!count)
                return 0;
        if (cfg->verbose_level > 3)
                g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
+
+       stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot) * count);
+       for (i = 0; i < count; ++i) {
+               stack_state [i].type = sp [i]->type;
+               stack_state [i].klass = sp [i]->klass;
+
+               /* Check that instructions other than ldnull have ins->klass set */
+               if (!cfg->dont_verify_stack_merge && (sp [i]->type == STACK_OBJ) && !((sp [i]->opcode == OP_PCONST) && sp [i]->inst_c0 == 0))
+                       g_assert (sp [i]->klass);
+       }
+
+       /* Perform verification and stack state merge */
+       for (i = 0; i < bb->out_count; ++i) {
+               outb = bb->out_bb [i];
+
+               /* exception handlers are linked, but they should not be considered for stack args */
+               if (outb->flags & BB_EXCEPTION_HANDLER)
+                       continue;
+               if (outb->stack_state) {
+                       gboolean verified;
+
+                       if (count != outb->in_scount) {
+                               cfg->unverifiable = TRUE;
+                               return 0;
+                       }
+                       verified = merge_stacks (cfg, stack_state, outb->stack_state, count);
+                       if (!verified) {
+                               cfg->unverifiable = TRUE;
+                               return 0;
+                       }
+
+                       if (cfg->verbose_level > 3) {
+                               int j;
+
+                               for (j = 0; j < count; ++j)
+                                       printf ("\tStack state of BB%d, slot %d=%d\n", outb->block_num, j, outb->stack_state [j].type);
+                       }
+               } else {
+                       /* Make a copy of the stack state */
+                       outb->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot) * count);
+                       memcpy (outb->stack_state, stack_state, sizeof (MonoStackSlot) * count);
+               }
+       }
+
        if (!bb->out_scount) {
                bb->out_scount = count;
                //g_print ("bblock %d has out:", bb->block_num);
@@ -1950,7 +1962,7 @@ handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int coun
                NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
                if (inst->opcode == CEE_STOBJ) {
                        NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
-                       handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
+                       handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE, FALSE);
                } else {
                        inst->cil_code = sp [i]->cil_code;
                        mono_add_ins_to_end (bb, inst);
@@ -2091,7 +2103,7 @@ handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack,
                        store->cil_code = ins->cil_code;
                        if (store->opcode == CEE_STOBJ) {
                                NEW_TEMPLOADA (cfg, store, temp->inst_c0);
-                               handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
+                               handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE, FALSE);
                        } else
                                MONO_ADD_INS (bblock, store);
                        NEW_TEMPLOAD (cfg, load, temp->inst_c0);
@@ -2420,9 +2432,7 @@ mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod
        call->inst.flags |= MONO_INST_HAS_METHOD;
        call->inst.inst_left = this;
 
-       if (!virtual)
-               mono_get_got_var (cfg);
-       else if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
+       if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
                /* Needed by the code generated in inssel.brg */
                mono_get_got_var (cfg);
 
@@ -2466,8 +2476,6 @@ mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer f
        call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
        call->fptr = func;
 
-       mono_get_got_var (cfg);
-
        return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
 }
 
@@ -2481,7 +2489,6 @@ mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer fun
                g_assert_not_reached ();
        }
 
-       mono_get_got_var (cfg);
        return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
 }
 
@@ -2504,8 +2511,6 @@ mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJit
 
        call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
 
-       mono_get_got_var (cfg);
-
        if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
                temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
                temp->flags |= MONO_INST_IS_TEMP;
@@ -2633,7 +2638,7 @@ get_memcpy_method (void)
 }
 
 static void
-handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
+handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier) {
        MonoInst *iargs [3];
        int n;
        guint32 align = 0;
@@ -2650,6 +2655,19 @@ handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst
        else
                n = mono_class_value_size (klass, &align);
 
+#if HAVE_WRITE_BARRIERS
+       /* if native is true there should be no references in the struct */
+       if (write_barrier && klass->has_references && !native) {
+               iargs [0] = dest;
+               iargs [1] = src;
+               NEW_PCONST (cfg, iargs [2], klass);
+
+               mono_emit_jit_icall (cfg, bblock, mono_value_copy, iargs, ip);
+               return;
+       }
+#endif
+
+       /* FIXME: add write barrier handling */
        if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
                MonoInst *inst;
                if (dest->opcode == OP_LDADDR) {
@@ -2743,10 +2761,15 @@ handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboole
                NEW_CLASSCONST (cfg, iargs [1], klass);
 
                alloc_ftn = mono_object_new;
+       } else if (cfg->compile_aot && bblock->out_of_line && klass->type_token && klass->image == mono_defaults.corlib) {
+               /* This happens often in argument checking code, eg. throw new FooException... */
+               /* Avoid relocations by calling a helper function specialized to mscorlib */
+               NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
+               return mono_emit_jit_icall (cfg, bblock, mono_helper_newobj_mscorlib, iargs, ip);
        } else {
                MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
                gboolean pass_lw;
-               
+
                alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
                if (pass_lw) {
                        guint32 lw = vtable->klass->instance_size;
@@ -2804,7 +2827,7 @@ handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const gucha
        vstore->inst_right = val;
 
        if (vstore->opcode == CEE_STOBJ) {
-               handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
+               handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE, TRUE);
        } else
                MONO_ADD_INS (bblock, vstore);
 
@@ -2927,8 +2950,14 @@ mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
        if (!(cfg->opt & MONO_OPT_SHARED)) {
                vtable = mono_class_vtable (cfg->domain, method->klass);
                if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
-                       if (cfg->run_cctors)
+                       if (cfg->run_cctors) {
+                               /* This makes so that inline cannot trigger */
+                               /* .cctors: too many apps depend on them */
+                               /* running with a specific order... */
+                               if (! vtable->initialized)
+                                       return FALSE;
                                mono_runtime_class_init (vtable);
+                       }
                }
                else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
                        return FALSE;
@@ -3050,38 +3079,6 @@ mono_find_jit_opcode_emulation (int opcode)
                return NULL;
 }
 
-static MonoException*
-mini_loader_error_to_exception (MonoLoaderError *error)
-{
-       MonoException *ex = NULL;
-
-       switch (error->kind) {
-       case MONO_LOADER_ERROR_TYPE: {
-               MonoString *class_name = mono_string_new (mono_domain_get (), error->class_name);
-               
-               ex = mono_get_exception_type_load (class_name, error->assembly_name);
-               break;
-       }
-       case MONO_LOADER_ERROR_METHOD:
-       case MONO_LOADER_ERROR_FIELD: {
-               char *class_name;
-               
-               class_name = g_strdup_printf ("%s%s%s", error->klass->name_space, *error->klass->name_space ? "." : "", error->klass->name);
-
-               if (error->kind == MONO_LOADER_ERROR_METHOD)
-                       ex = mono_get_exception_missing_method (class_name, error->member_name);
-               else
-                       ex = mono_get_exception_missing_field (class_name, error->member_name);
-               g_free (class_name);
-               break;
-       }
-       default:
-               g_assert_not_reached ();
-       }
-
-       return ex;
-}
-
 static MonoInst*
 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
@@ -3213,7 +3210,7 @@ mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *s
                        store->cil_code = sp [0]->cil_code;
                        if (store->opcode == CEE_STOBJ) {
                                NEW_TEMPLOADA (cfg, store, temp->inst_c0);
-                               handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
+                               handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE, FALSE);
                        } else {
                                MONO_ADD_INS (bblock, store);
                        } 
@@ -3221,6 +3218,56 @@ mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *s
                sp++;
        }
 }
+#define MONO_INLINE_CALLED_LIMITED_METHODS 0
+#define MONO_INLINE_CALLER_LIMITED_METHODS 0
+
+#if (MONO_INLINE_CALLED_LIMITED_METHODS)
+static char*
+mono_inline_called_method_name_limit = NULL;
+static gboolean check_inline_called_method_name_limit (MonoMethod *called_method) {
+       char *called_method_name = mono_method_full_name (called_method, TRUE);
+       int strncmp_result;
+       
+       if (mono_inline_called_method_name_limit == NULL) {
+               char *limit_string = getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
+               if (limit_string != NULL) {
+                       mono_inline_called_method_name_limit = limit_string;
+               } else {
+                       mono_inline_called_method_name_limit = (char *) "";
+               }
+       }
+       
+       strncmp_result = strncmp (called_method_name, mono_inline_called_method_name_limit, strlen (mono_inline_called_method_name_limit));
+       g_free (called_method_name);
+       
+       //return (strncmp_result <= 0);
+       return (strncmp_result == 0);
+}
+#endif
+
+#if (MONO_INLINE_CALLER_LIMITED_METHODS)
+static char*
+mono_inline_caller_method_name_limit = NULL;
+static gboolean check_inline_caller_method_name_limit (MonoMethod *caller_method) {
+       char *caller_method_name = mono_method_full_name (caller_method, TRUE);
+       int strncmp_result;
+       
+       if (mono_inline_caller_method_name_limit == NULL) {
+               char *limit_string = getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
+               if (limit_string != NULL) {
+                       mono_inline_caller_method_name_limit = limit_string;
+               } else {
+                       mono_inline_caller_method_name_limit = (char *) "";
+               }
+       }
+       
+       strncmp_result = strncmp (caller_method_name, mono_inline_caller_method_name_limit, strlen (mono_inline_caller_method_name_limit));
+       g_free (caller_method_name);
+       
+       //return (strncmp_result <= 0);
+       return (strncmp_result == 0);
+}
+#endif
 
 static int
 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
@@ -3231,6 +3278,14 @@ inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig,
        MonoBasicBlock *ebblock, *sbblock;
        int i, costs, new_locals_offset;
        MonoMethod *prev_inlined_method;
+#if (MONO_INLINE_CALLED_LIMITED_METHODS)
+       if ((! inline_allways) && ! check_inline_called_method_name_limit (cmethod))
+               return 0;
+#endif
+#if (MONO_INLINE_CALLER_LIMITED_METHODS)
+       if ((! inline_allways) && ! check_inline_caller_method_name_limit (cfg->method))
+               return 0;
+#endif
 
        if (cfg->verbose_level > 2)
                g_print ("INLINE START %p %s -> %s\n", cmethod,  mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
@@ -3319,13 +3374,13 @@ inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig,
  * * consider using an array instead of an hash table (bb_hash)
  */
 
-#define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
-#define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
-#define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
-#define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) goto unverified
-#define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) goto unverified
-#define CHECK_OPSIZE(size) if (ip + size > end) goto unverified
-
+#define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
+#define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
+#define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
+#define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
+#define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
+#define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
+#define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
 
 /* offset from br.s -> br like opcodes */
 #define BIG_BRANCH_OFFSET 13
@@ -3352,7 +3407,7 @@ get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header
                cli_addr = ip - start;
                i = mono_opcode_value ((const guint8 **)&ip, end);
                if (i < 0)
-                       goto unverified;
+                       UNVERIFIED;
                opcode = &mono_opcodes [i];
                switch (opcode->argument) {
                case MonoInlineNone:
@@ -3417,7 +3472,7 @@ get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header
                        
                        /* Find the start of the bblock containing the throw */
                        bblock = NULL;
-                       while ((bb_start > start) && !bblock) {
+                       while ((bb_start >= start) && !bblock) {
                                bblock = g_hash_table_lookup (bbhash, (bb_start));
                                bb_start --;
                        }
@@ -3480,12 +3535,22 @@ mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
        return klass;
 }
 
+/*
+ * Returns TRUE if the JIT should abort inlining because "callee"
+ * is influenced by security attributes.
+ */
 static
-void check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
+gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
 {
-       guint32 result = mono_declsec_linkdemand (cfg->domain, caller, callee);
+       guint32 result;
+       
+       if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
+               return TRUE;
+       }
+       
+       result = mono_declsec_linkdemand (cfg->domain, caller, callee);
        if (result == MONO_JIT_SECURITY_OK)
-               return;
+               return FALSE;
 
        if (result == MONO_JIT_LINKDEMAND_ECMA) {
                /* Generate code to throw a SecurityException before the actual call/link */
@@ -3504,6 +3569,8 @@ void check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
                cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
                cfg->exception_data = result;
        }
+       
+       return FALSE;
 }
 
 static gboolean
@@ -3649,12 +3716,16 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
        dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
        dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
        dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
+       dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
 
-       /* still some type unsefety issues in marshal wrappers... (unknown is PtrToStructure) */
+       /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
        dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
        dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
        dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
 
+       /* Not turned on yet */
+       cfg->dont_verify_stack_merge = TRUE;
+
        image = method->klass->image;
        header = mono_method_get_header (method);
        generic_container = method->generic_container;
@@ -3685,10 +3756,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
        dont_inline = g_list_prepend (dont_inline, method);
        if (cfg->method == method) {
 
-               if (cfg->method->save_lmf)
-                       /* Needed by the prolog code */
-                       mono_get_got_var (cfg);
-
                if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
                        cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
 
@@ -3753,6 +3820,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                tblock->in_scount = 1;
                                tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
                                tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
+                               tblock->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot));
+                               tblock->stack_state [0].type = STACK_OBJ;
+                               /* FIXME? */
+                               tblock->stack_state [0].klass = mono_defaults.object_class;
 
                                /* 
                                 * Add a dummy use for the exvar so its liveness info will be
@@ -3767,6 +3838,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        tblock->real_offset = clause->data.filter_offset;
                                        tblock->in_scount = 1;
                                        tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
+                                       tblock->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot));
+                                       tblock->stack_state [0].type = STACK_OBJ;
+                                       /* FIXME? */
+                                       tblock->stack_state [0].klass = mono_defaults.object_class;
+
                                        /* The filter block shares the exvar with the handler block */
                                        tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
                                        MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
@@ -3880,7 +3956,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
        if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
                ip = err_pos;
-               goto unverified;
+               UNVERIFIED;
        }
 
        if (cfg->method == method)
@@ -3893,7 +3969,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                param_types [n + sig->hasthis] = sig->params [n];
        for (n = 0; n < header->num_locals; ++n) {
                if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
-                       goto unverified;
+                       UNVERIFIED;
        }
        class_inits = NULL;
 
@@ -3953,6 +4029,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                if (sp != stack_start) {
                                        handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                        sp = stack_start;
+                                       CHECK_UNVERIFIABLE (cfg);
                                }
                                bblock->next_bb = tblock;
                                bblock = tblock;
@@ -4033,10 +4110,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        NEW_LOCSTORE (cfg, ins, n, *sp);
                        ins->cil_code = ip;
                        if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
-                               goto unverified;
+                               UNVERIFIED;
                        if (ins->opcode == CEE_STOBJ) {
                                NEW_LOCLOADA (cfg, ins, n);
-                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
+                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
                        } else
                                MONO_ADD_INS (bblock, ins);
                        ++ip;
@@ -4069,10 +4146,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        handle_loaded_temps (cfg, bblock, stack_start, sp);
                        ins->cil_code = ip;
                        if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
-                               goto unverified;
+                               UNVERIFIED;
                        if (ins->opcode == CEE_STOBJ) {
                                NEW_ARGLOADA (cfg, ins, ip [1]);
-                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
+                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
                        } else
                                MONO_ADD_INS (bblock, ins);
                        ip += 2;
@@ -4104,10 +4181,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        NEW_LOCSTORE (cfg, ins, ip [1], *sp);
                        ins->cil_code = ip;
                        if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
-                               goto unverified;
+                               UNVERIFIED;
                        if (ins->opcode == CEE_STOBJ) {
                                NEW_LOCLOADA (cfg, ins, ip [1]);
-                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
+                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
                        } else
                                MONO_ADD_INS (bblock, ins);
                        ip += 2;
@@ -4232,7 +4309,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                store->cil_code = ip;
                                if (store->opcode == CEE_STOBJ) {
                                        NEW_TEMPLOADA (cfg, store, temp->inst_c0);
-                                       handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE);
+                                       handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE, FALSE);
                                } else {
                                        MONO_ADD_INS (bblock, store);
                                }
@@ -4258,7 +4335,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                case CEE_JMP:
                        CHECK_OPSIZE (5);
                        if (stack_start != sp)
-                               goto unverified;
+                               UNVERIFIED;
                        MONO_INST_NEW (cfg, ins, CEE_JMP);
                        token = read32 (ip + 1);
                        /* FIXME: check the signature matches */
@@ -4268,7 +4345,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                goto load_error;
 
                        if (mono_use_security_manager) {
-                               check_linkdemand (cfg, method, cmethod, bblock, ip);
+                               if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                       INLINE_FAILURE;
                        }
 
                        ins->inst_p0 = cmethod;
@@ -4311,14 +4389,16 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                if (!cmethod)
                                        goto load_error;
                                if (!dont_verify && !can_access_method (method, cmethod))
-                                       goto unverified;
+                                       UNVERIFIED;
 
                                if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
                                        /* MS.NET seems to silently convert this to a callvirt */
                                        virtual = 1;
 
-                               if (!cmethod->klass->inited)
-                                       mono_class_init (cmethod->klass);
+                               if (!cmethod->klass->inited){
+                                       if (!mono_class_init (cmethod->klass))
+                                               goto load_error;
+                               }
 
                                if (mono_method_signature (cmethod)->pinvoke) {
                                        MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
@@ -4332,7 +4412,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                n = fsig->param_count + fsig->hasthis;
 
                                if (mono_use_security_manager) {
-                                       check_linkdemand (cfg, method, cmethod, bblock, ip);
+                                       if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                               INLINE_FAILURE;
                                }
 
                                if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
@@ -4345,20 +4426,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                        }
 
-                       if (!virtual) {
-                               mono_get_got_var (cfg);
-                       } else {
-                               /* code in inssel.brg might transform a virtual call to a normal call */
-                               if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || 
-                                       ((cmethod->flags & METHOD_ATTRIBUTE_FINAL) && 
-                                        cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK))
-                                       mono_get_got_var (cfg);
-                       }
-
-                       if (cmethod && cmethod->klass->generic_container) {
-                               // G_BREAKPOINT ();
-                               goto unverified;
-                       }
+                       if (cmethod && cmethod->klass->generic_container)
+                               UNVERIFIED;
 
                        CHECK_STACK (n);
 
@@ -4397,16 +4466,15 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        ins->cil_code = ip;
                                        ins->inst_i0 = sp [0];
                                        ins->type = STACK_OBJ;
+                                       ins->klass = mono_class_from_mono_type (&constrained_call->byval_arg);
                                        sp [0] = ins;
                                } else if (cmethod->klass->valuetype)
                                        virtual = 0;
                                constrained_call = NULL;
                        }
 
-                       if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp)) {
-                               // G_BREAKPOINT ();
-                               goto unverified;
-                       }
+                       if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
+                               UNVERIFIED;
 
                        if (cmethod && virtual && 
                            (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) && 
@@ -4430,7 +4498,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
                                NEW_PCONST (cfg, iargs [1], cmethod);
                                NEW_PCONST (cfg, iargs [2], ((MonoMethodInflated *) cmethod)->context);
-                               temp = mono_emit_jit_icall (cfg, bblock, helper_compile_generic_method, iargs, ip);
+                               temp = mono_emit_jit_icall (cfg, bblock, mono_helper_compile_generic_method, iargs, ip);
 
                                NEW_TEMPLOAD (cfg, addr, temp);
                                NEW_TEMPLOAD (cfg, sp [0], this_temp->inst_c0);
@@ -4441,12 +4509,14 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
 
                                ip += 5;
+                               ins_flag = 0;
                                break;
                        }
 
                        if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
                                 (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
                                int i;
+
                                /* Prevent inlining of methods with tail calls (the call stack would be altered) */
                                INLINE_FAILURE;
                                /* FIXME: This assumes the two methods has the same number and type of arguments */
@@ -4462,7 +4532,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        ins->cil_code = ip;
                                        if (ins->opcode == CEE_STOBJ) {
                                                NEW_ARGLOADA (cfg, ins, i);
-                                               handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE);
+                                               handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE, FALSE);
                                        }
                                        else
                                                MONO_ADD_INS (bblock, ins);
@@ -4491,6 +4561,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
 
                                ip += 5;
+                               ins_flag = 0;
                                break;
                        }
 
@@ -4528,6 +4599,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        bblock = ebblock;
 
                                        inline_costs += costs;
+                                       ins_flag = 0;
                                        break;
                                }
                        }
@@ -4566,7 +4638,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                ip += 6;
                                        else
                                                ip += 5;
-
+                                       ins_flag = 0;
                                        break;
                                }
                        }
@@ -4606,7 +4678,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                 * the same MonoInst is added to two different trees and this is not 
                                                 * allowed by burg.
                                                 */
-                                               mono_emit_jit_icall (cfg, bblock, helper_stelem_ref_check, iargs, ip);
+                                               mono_emit_jit_icall (cfg, bblock, mono_helper_stelem_ref_check, iargs, ip);
 
                                                NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
                                                NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
@@ -4616,7 +4688,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
                                        ins->cil_code = ip;
                                        if (ins->opcode == CEE_STOBJ) {
-                                               handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
+                                               handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE, TRUE);
                                        } else {
                                                MONO_ADD_INS (bblock, ins);
                                        }
@@ -4653,6 +4725,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        }
 
                        ip += 5;
+                       ins_flag = 0;
                        break;
                }
                case CEE_RET:
@@ -4668,7 +4741,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        if (store->opcode == CEE_STOBJ) {
                                                g_assert_not_reached ();
                                                NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
-                                               handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
+                                               /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
+                                               handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE, FALSE);
                                        } else
                                                MONO_ADD_INS (bblock, store);
                                } 
@@ -4681,7 +4755,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        ins->opcode = mono_type_to_stind (mono_method_signature (method)->ret);
                                        if (ins->opcode == CEE_STOBJ) {
                                                NEW_RETLOADA (cfg, ins);
-                                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
+                                               /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
+                                               handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
                                        } else {
                                                ins->opcode = OP_SETRET;
                                                ins->cil_code = ip;
@@ -4692,7 +4767,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
                        }
                        if (sp != stack_start)
-                               goto unverified;
+                               UNVERIFIED;
                        MONO_INST_NEW (cfg, ins, CEE_BR);
                        ins->cil_code = ip++;
                        ins->inst_target_bb = end_bblock;
@@ -4714,6 +4789,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp != stack_start) {
                                handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                sp = stack_start;
+                               CHECK_UNVERIFIABLE (cfg);
                        }
                        start_new_bblock = 1;
                        inline_costs += BRANCH_COST;
@@ -4723,7 +4799,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_OPSIZE (2);
                        CHECK_STACK (1);
                        if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
-                               goto unverified;
+                               UNVERIFIED;
                        MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
                        ins->cil_code = ip++;
                        target = ip + 1 + *(signed char*)ip;
@@ -4732,6 +4808,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp != stack_start) {
                                handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                sp = stack_start;
+                               CHECK_UNVERIFIABLE (cfg);
                        }
                        inline_costs += BRANCH_COST;
                        break;
@@ -4755,6 +4832,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp != stack_start) {
                                handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                sp = stack_start;
+                               CHECK_UNVERIFIABLE (cfg);
                        }
                        inline_costs += BRANCH_COST;
                        break;
@@ -4772,6 +4850,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp != stack_start) {
                                handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                sp = stack_start;
+                               CHECK_UNVERIFIABLE (cfg);
                        }
                        start_new_bblock = 1;
                        inline_costs += BRANCH_COST;
@@ -4781,7 +4860,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_OPSIZE (5);
                        CHECK_STACK (1);
                        if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
-                               goto unverified;
+                               UNVERIFIED;
                        MONO_INST_NEW (cfg, ins, *ip);
                        ins->cil_code = ip++;
                        target = ip + 4 + (gint32)read32(ip);
@@ -4790,6 +4869,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp != stack_start) {
                                handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                sp = stack_start;
+                               CHECK_UNVERIFIABLE (cfg);
                        }
                        inline_costs += BRANCH_COST;
                        break;
@@ -4813,6 +4893,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp != stack_start) {
                                handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                sp = stack_start;
+                               CHECK_UNVERIFIABLE (cfg);
                        }
                        inline_costs += BRANCH_COST;
                        break;
@@ -4824,7 +4905,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        --sp;
                        ins->inst_left = *sp;
                        if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR)) 
-                               goto unverified;
+                               UNVERIFIED;
                        ins->cil_code = ip;
                        ip += 5;
                        CHECK_OPSIZE (n * sizeof (guint32));
@@ -4845,6 +4926,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (sp != stack_start) {
                                handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                sp = stack_start;
+                               CHECK_UNVERIFIABLE (cfg);
                        }
                        /* Needed by the code generated in inssel.brg */
                        mono_get_got_var (cfg);
@@ -4870,6 +4952,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        ins->type = ldind_type [*ip - CEE_LDIND_I1];
                        ins->flags |= ins_flag;
                        ins_flag = 0;
+                       if (ins->type == STACK_OBJ)
+                               ins->klass = mono_defaults.object_class;
                        ++ip;
                        break;
                case CEE_STIND_REF:
@@ -4880,6 +4964,16 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                case CEE_STIND_R4:
                case CEE_STIND_R8:
                        CHECK_STACK (2);
+#if HAVE_WRITE_BARRIERS
+                       if (*ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER) {
+                               /* insert call to write barrier */
+                               MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
+                               sp -= 2;
+                               mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), sp, ip, NULL);
+                               ip++;
+                               break;
+                       }
+#endif
                        MONO_INST_NEW (cfg, ins, *ip);
                        ins->cil_code = ip++;
                        sp -= 2;
@@ -4916,7 +5010,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (mono_find_jit_opcode_emulation (ins->opcode)) {
                                --sp;
                                *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
-                               mono_get_got_var (cfg);
                        }
                        ip++;
                        break;
@@ -4948,7 +5041,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (mono_find_jit_opcode_emulation (ins->opcode)) {
                                --sp;
                                *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
-                               mono_get_got_var (cfg);
                        }
                        ip++;
                        break;
@@ -4970,7 +5062,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (mono_find_jit_opcode_emulation (ins->opcode)) {
                                --sp;
                                *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
-                               mono_get_got_var (cfg);
                        }
                        ip++;                   
                        break;
@@ -5032,6 +5123,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                load->cil_code = ip;
                                load->inst_i0 = sp [1];
                                load->type = STACK_OBJ;
+                               load->klass = klass;
                                load->flags |= ins_flag;
                                MONO_INST_NEW (cfg, store, CEE_STIND_REF);
                                store->cil_code = ip;
@@ -5080,6 +5172,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                ins->cil_code = ip;
                                ins->inst_i0 = sp [0];
                                ins->type = STACK_OBJ;
+                               ins->klass = klass;
                                ins->flags |= ins_flag;
                                ins_flag = 0;
                                *sp++ = ins;
@@ -5113,7 +5206,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        ins->cil_code = ip;
                                        g_assert (ins->opcode == CEE_STOBJ);
                                        NEW_LOCLOADA (cfg, ins, loc_index);
-                                       handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
+                                       handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
                                        ip += 5;
                                        ip += stloc_len;
                                        break;
@@ -5155,6 +5248,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
                                ins->cil_code = ip;
                                ins->type = STACK_OBJ;
+                               ins->klass = mono_defaults.string_class;
                                *sp = ins;
                        }
                        else if (method->wrapper_type != MONO_WRAPPER_NONE) {
@@ -5188,10 +5282,19 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                MonoInst *iargs [2];
                                                int temp;
 
-                                               /* Avoid creating the string object */
-                                               NEW_IMAGECONST (cfg, iargs [0], image);
-                                               NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
-                                               temp = mono_emit_jit_icall (cfg, bblock, helper_ldstr, iargs, ip);
+                                               if (cfg->compile_aot && cfg->method->klass->image == mono_defaults.corlib) {
+                                                       /* 
+                                                        * Avoid relocations by using a version of helper_ldstr
+                                                        * specialized to mscorlib.
+                                                        */
+                                                       NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
+                                                       temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr_mscorlib, iargs, ip);
+                                               } else {
+                                                       /* Avoid creating the string object */
+                                                       NEW_IMAGECONST (cfg, iargs [0], image);
+                                                       NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
+                                                       temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr, iargs, ip);
+                                               }
                                                NEW_TEMPLOAD (cfg, *sp, temp);
                                        } 
                                        else
@@ -5204,6 +5307,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                ins->cil_code = ip;
                                                ins->type = STACK_OBJ;
                                                ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
+                                               ins->klass = mono_defaults.string_class;
                                                *sp = ins;
                                        }
                                }
@@ -5224,10 +5328,12 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                goto load_error;
                        fsig = mono_method_get_signature (cmethod, image, token);
 
-                       mono_class_init (cmethod->klass);
+                       if (!mono_class_init (cmethod->klass))
+                               goto load_error;
 
                        if (mono_use_security_manager) {
-                               check_linkdemand (cfg, method, cmethod, bblock, ip);
+                               if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                       INLINE_FAILURE;
                        }
 
                        n = fsig->param_count;
@@ -5265,10 +5371,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        /* 
                                         * The code generated by mini_emit_virtual_call () expects
                                         * iargs [0] to be a boxed instance, but luckily the vcall
-                                        * will be transformed into a normal call there. The AOT
-                                        * case needs an already allocate got_var.
+                                        * will be transformed into a normal call there.
                                         */
-                                       mono_get_got_var (cfg);
                                } else {
                                        temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
                                        NEW_TEMPLOAD (cfg, *sp, temp);
@@ -5332,7 +5436,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (!klass)
                                goto load_error;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
 
                        /* Needed by the code generated in inssel.brg */
                        mono_get_got_var (cfg);
@@ -5452,6 +5556,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        add->inst_left = ins;
                        add->inst_right = vtoffset;
                        add->type = STACK_MP;
+                       add->klass = mono_defaults.object_class;
                        *sp = add;
                        ip += 5;
                        /* LDOBJ impl */
@@ -5528,7 +5633,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (!klass)
                                goto load_error;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
 
                        /* Needed by the code generated in inssel.brg */
                        mono_get_got_var (cfg);
@@ -5588,7 +5693,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        
                        link_bblock (cfg, bblock, end_bblock);
                        start_new_bblock = 1;
-                       mono_get_got_var (cfg);
                        break;
                case CEE_LDFLD:
                case CEE_LDFLDA:
@@ -5607,9 +5711,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                --sp;
                        }
                        if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
-                               goto unverified;
+                               UNVERIFIED;
                        if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
-                               goto unverified;
+                               UNVERIFIED;
                        CHECK_OPSIZE (5);
                        token = read32 (ip + 1);
                        if (method->wrapper_type != MONO_WRAPPER_NONE) {
@@ -5622,13 +5726,13 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                goto load_error;
                        mono_class_init (klass);
                        if (!dont_verify && !can_access_field (method, field))
-                               goto unverified;
+                               UNVERIFIED;
 
                        foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
                        /* FIXME: mark instructions for use in SSA */
                        if (*ip == CEE_STFLD) {
                                if (target_type_is_incompatible (cfg, field->type, sp [1]))
-                                       goto unverified;
+                                       UNVERIFIED;
                                if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
                                        MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
                                        MonoInst *iargs [5];
@@ -5661,6 +5765,22 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        } else {
                                                mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
                                        }
+#if HAVE_WRITE_BARRIERS
+                               } else if (mono_type_to_stind (field->type) == CEE_STIND_REF) {
+                                       /* insert call to write barrier */
+                                       MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
+                                       MonoInst *iargs [2];
+                                       NEW_ICONST (cfg, offset_ins, foffset);
+                                       MONO_INST_NEW (cfg, ins, OP_PADD);
+                                       ins->cil_code = ip;
+                                       ins->inst_left = *sp;
+                                       ins->inst_right = offset_ins;
+                                       ins->type = STACK_MP;
+                                       ins->klass = mono_defaults.object_class;
+                                       iargs [0] = ins;
+                                       iargs [1] = sp [1];
+                                       mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), iargs, ip, NULL);
+#endif
                                } else {
                                        MonoInst *store;
                                        NEW_ICONST (cfg, offset_ins, foffset);
@@ -5679,7 +5799,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        ins_flag = 0;
                                        if (store->opcode == CEE_STOBJ) {
                                                handle_stobj (cfg, bblock, ins, sp [1], ip, 
-                                                             mono_class_from_mono_type (field->type), FALSE, FALSE);
+                                                             mono_class_from_mono_type (field->type), FALSE, FALSE, TRUE);
                                        } else
                                                MONO_ADD_INS (bblock, store);
                                }
@@ -5805,8 +5925,14 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                        g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
                                                class_inits = g_slist_prepend (class_inits, vtable);
                                        } else {
-                                               if (cfg->run_cctors)
+                                               if (cfg->run_cctors) {
+                                                       /* This makes so that inline cannot trigger */
+                                                       /* .cctors: too many apps depend on them */
+                                                       /* running with a specific order... */
+                                                       if (! vtable->initialized)
+                                                               INLINE_FAILURE;
                                                        mono_runtime_class_init (vtable);
+                                               }
                                        }
                                        addr = (char*)vtable->data + field->offset;
 
@@ -5845,7 +5971,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                ins_flag = 0;
 
                                if (store->opcode == CEE_STOBJ) {
-                                       handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
+                                       handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE, FALSE);
                                } else
                                        MONO_ADD_INS (bblock, store);
                        } else {
@@ -5888,6 +6014,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                NEW_ICONST (cfg, *sp, *((guint32 *)addr));
                                                sp++;
                                                break;
+#ifndef HAVE_MOVING_COLLECTOR
                                        case MONO_TYPE_I:
                                        case MONO_TYPE_U:
                                        case MONO_TYPE_STRING:
@@ -5901,6 +6028,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                type_to_eval_stack_type (field->type, *sp);
                                                sp++;
                                                break;
+#endif
                                        case MONO_TYPE_I8:
                                        case MONO_TYPE_U8:
                                                MONO_INST_NEW (cfg, *sp, OP_I8CONST);
@@ -5944,7 +6072,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                goto load_error;
                        n = mono_type_to_stind (&klass->byval_arg);
                        if (n == CEE_STOBJ) {
-                               handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
+                               handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE, TRUE);
                        } else {
                                /* FIXME: should check item at sp [1] is compatible with the type of the store. */
                                MonoInst *store;
@@ -5976,11 +6104,12 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                break;
                        }
                        if (klass == mono_defaults.void_class)
-                               goto unverified;
+                               UNVERIFIED;
                        if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
-                               goto unverified;
+                               UNVERIFIED;
                        /* frequent check in generic code: box (struct), brtrue */
-                       if (ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
+                       if (!mono_class_is_nullable (klass) &&
+                           ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
                                /*g_print ("box-brtrue opt at 0x%04x in %s\n", real_offset, method->name);*/
                                MONO_INST_NEW (cfg, ins, CEE_POP);
                                MONO_ADD_INS (bblock, ins);
@@ -6008,6 +6137,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                if (sp != stack_start) {
                                        handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
                                        sp = stack_start;
+                                       CHECK_UNVERIFIABLE (cfg);
                                }
                                start_new_bblock = 1;
                                break;
@@ -6042,6 +6172,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        ins->inst_newa_class = klass;
                        ins->inst_newa_len = *sp;
                        ins->type = STACK_OBJ;
+                       ins->klass = klass;
                        ip += 5;
                        *sp++ = ins;
                        /* 
@@ -6065,7 +6196,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_STACK (1);
                        --sp;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
                        MONO_INST_NEW (cfg, ins, *ip);
                        ins->cil_code = ip++;
                        ins->inst_left = *sp;
@@ -6077,7 +6208,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        sp -= 2;
                        CHECK_OPSIZE (5);
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
 
                        klass = mini_get_class (method, read32 (ip + 1), generic_context);
                        if (!klass)
@@ -6093,6 +6224,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                check->klass = klass;
                                check->inst_left = sp [0];
                                check->type = STACK_OBJ;
+                               check->klass = klass;
                                sp [0] = check;
                        }
                        
@@ -6107,7 +6239,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_STACK (2);
                        sp -= 2;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
                        CHECK_OPSIZE (5);
                        token = read32 (ip + 1);
                        klass = mono_class_get_full (image, token, generic_context);
@@ -6144,7 +6276,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_STACK (2);
                        sp -= 2;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
                        klass = array_access_to_klass (*ip);
                        NEW_LDELEMA (cfg, load, sp, klass);
                        load->cil_code = ip;
@@ -6153,6 +6285,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        ins->inst_left = load;
                        *sp++ = ins;
                        ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
+                       ins->klass = klass;
                        ++ip;
                        break;
                }
@@ -6172,7 +6305,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_STACK (3);
                        sp -= 3;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
                        klass = array_access_to_klass (*ip);
                        NEW_LDELEMA (cfg, load, sp, klass);
                        load->cil_code = ip;
@@ -6196,7 +6329,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_STACK (3);
                        sp -= 3;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
                        CHECK_OPSIZE (5);
                        token = read32 (ip + 1);
                        klass = mono_class_get_full (image, token, generic_context);
@@ -6219,7 +6352,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                                n = mono_type_to_stind (&klass->byval_arg);
                                if (n == CEE_STOBJ)
-                                       handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE);
+                                       handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE, TRUE);
                                else {
                                        MONO_INST_NEW (cfg, ins, n);
                                        ins->cil_code = ip;
@@ -6240,9 +6373,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_STACK (3);
                        sp -= 3;
                        if (sp [0]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
                        if (sp [2]->type != STACK_OBJ)
-                               goto unverified;
+                               UNVERIFIED;
 
                        handle_loaded_temps (cfg, bblock, stack_start, sp);
 
@@ -6420,7 +6553,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        if (mono_find_jit_opcode_emulation (ins->opcode)) {
                                --sp;
                                *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
-                               mono_get_got_var (cfg);
                        }
                        ip++;
                        break;
@@ -6644,10 +6776,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
 
                                NEW_RETLOADA (cfg, ins);
-                               handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
+                               handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE, FALSE);
                                
                                if (sp != stack_start)
-                                       goto unverified;
+                                       UNVERIFIED;
                                
                                MONO_INST_NEW (cfg, ins, CEE_BR);
                                ins->cil_code = ip;
@@ -6779,7 +6911,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                mono_class_init (cmethod->klass);
 
                                if (mono_use_security_manager) {
-                                       check_linkdemand (cfg, method, cmethod, bblock, ip);
+                                       if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                               INLINE_FAILURE;
                                }
 
                                handle_loaded_temps (cfg, bblock, stack_start, sp);
@@ -6809,7 +6942,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                mono_class_init (cmethod->klass);
 
                                if (mono_use_security_manager) {
-                                       check_linkdemand (cfg, method, cmethod, bblock, ip);
+                                       if (check_linkdemand (cfg, method, cmethod, bblock, ip))
+                                               INLINE_FAILURE;
                                }
 
                                handle_loaded_temps (cfg, bblock, stack_start, sp);
@@ -6855,10 +6989,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                NEW_ARGSTORE (cfg, ins, n, *sp);
                                ins->cil_code = ip;
                                if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
-                                       goto unverified;
+                                       UNVERIFIED;
                                if (ins->opcode == CEE_STOBJ) {
                                        NEW_ARGLOADA (cfg, ins, n);
-                                       handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
+                                       handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
                                } else
                                        MONO_ADD_INS (bblock, ins);
                                ip += 4;
@@ -6892,11 +7026,11 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                handle_loaded_temps (cfg, bblock, stack_start, sp);
                                NEW_LOCSTORE (cfg, ins, n, *sp);
                                if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
-                                       goto unverified;
+                                       UNVERIFIED;
                                ins->cil_code = ip;
                                if (ins->opcode == CEE_STOBJ) {
                                        NEW_LOCLOADA (cfg, ins, n);
-                                       handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
+                                       handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
                                } else
                                        MONO_ADD_INS (bblock, ins);
                                ip += 4;
@@ -6906,7 +7040,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                CHECK_STACK (1);
                                --sp;
                                if (sp != stack_start) 
-                                       goto unverified;
+                                       UNVERIFIED;
                                if (cfg->method != method) 
                                        /* 
                                         * Inlining this into a loop in a parent could lead to 
@@ -6934,7 +7068,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                CHECK_STACK (1);
                                --sp;
                                if ((sp != stack_start) || (sp [0]->type != STACK_I4)) 
-                                       goto unverified;
+                                       UNVERIFIED;
                                MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
                                ins->inst_left = *sp;
                                ins->cil_code = ip;
@@ -6955,7 +7089,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
                                g_assert (nearest);
                                if ((ip - header->code) != nearest->handler_offset)
-                                       goto unverified;
+                                       UNVERIFIED;
 
                                break;
                        }
@@ -6989,6 +7123,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        NEW_PCONST (cfg, load, NULL);
                                        load->cil_code = ip;
                                        load->type = STACK_OBJ;
+                                       load->klass = klass;
                                        MONO_INST_NEW (cfg, store, CEE_STIND_REF);
                                        store->cil_code = ip;
                                        handle_loaded_temps (cfg, bblock, stack_start, sp);
@@ -7076,7 +7211,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                link_bblock (cfg, bblock, end_bblock);
                                start_new_bblock = 1;
                                ip += 2;
-                               mono_get_got_var (cfg);
                                break;
                        }
                        case CEE_SIZEOF:
@@ -7124,7 +7258,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                }
        }
        if (start_new_bblock != 1)
-               goto unverified;
+               UNVERIFIED;
 
        bblock->cil_length = ip - bblock->cil_code;
        bblock->next_bb = end_bblock;
@@ -7435,6 +7569,7 @@ mono_init_trampolines (void)
        mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
 #ifdef MONO_ARCH_HAVE_PIC_AOT
        mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
+       mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
 #endif
 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
        mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
@@ -7521,7 +7656,9 @@ mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method,
 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
        code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
 
-       ji = g_new0 (MonoJitInfo, 1);
+       mono_domain_lock (domain);
+       ji = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
+       mono_domain_unlock (domain);
        ji->code_start = code;
        ji->code_size = code_size;
        ji->method = method;
@@ -7603,7 +7740,7 @@ mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
 }      
 #endif
 
-gpointer
+static gpointer
 mono_create_delegate_trampoline (MonoMethod *method, gpointer addr)
 {
 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
@@ -8217,6 +8354,7 @@ mini_thread_cleanup (MonoThread *thread)
                g_free (jit_tls->first_lmf);
                g_free (jit_tls);
                thread->jit_data = NULL;
+               TlsSetValue (mono_jit_tls_id, NULL);
        }
 }
 
@@ -8246,6 +8384,36 @@ mono_remove_patch_info (MonoCompile *cfg, int ip)
        }
 }
 
+/**
+ * mono_patch_info_dup_mp:
+ *
+ * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
+ */
+MonoJumpInfo*
+mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
+{
+       MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
+       memcpy (res, patch_info, sizeof (MonoJumpInfo));
+
+       switch (patch_info->type) {
+       case MONO_PATCH_INFO_LDSTR:
+       case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
+       case MONO_PATCH_INFO_LDTOKEN:
+       case MONO_PATCH_INFO_DECLSEC:
+               res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
+               memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
+               break;
+       case MONO_PATCH_INFO_SWITCH:
+               res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
+               memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
+               break;
+       default:
+               break;
+       }
+
+       return res;
+}
+
 gpointer
 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
 {
@@ -8324,6 +8492,10 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                mono_class_init (patch_info->data.klass);
                target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
                break;
+       case MONO_PATCH_INFO_ADJUSTED_IID:
+               mono_class_init (patch_info->data.klass);
+               target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
+               break;
        case MONO_PATCH_INFO_VTABLE:
                target = mono_class_vtable (domain, patch_info->data.klass);
                break;
@@ -8635,6 +8807,13 @@ remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *p
        if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
                return FALSE;
        }
+
+       /* 
+        * Do not touch BBs following a try block as the code in 
+        * mini_method_compile needs them to compute the length of the try block.
+        */
+       if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
+               return FALSE;
        
        /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
        if ((target_bb != NULL) && (target_bb != bb)) {
@@ -8689,6 +8868,11 @@ merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
 
        replace_basic_block (bb, bbn, bb);
 
+       /* Nullify branch at the end of bb */
+       if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
+               bb->last_ins->opcode = CEE_NOP;
+       }               
+
        if (bb->last_ins) {
                if (bbn->code) {
                        bb->last_ins->next = bbn->code;
@@ -9761,6 +9945,12 @@ mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gbool
        if (parts == 3)
                return cfg;
 
+       if (cfg->verbose_level > 4) {
+               printf ("BEFORE DECOMPSE START\n");
+               mono_print_code (cfg);
+               printf ("BEFORE DECOMPSE END\n");
+       }
+       
        decompose_pass (cfg);
 
        if (cfg->got_var) {
@@ -9956,7 +10146,7 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
                MonoMethod *nm;
                MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
 
-               if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
+               if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE && !MONO_CLASS_IS_IMPORT(method->klass))
                        g_error ("Method '%s' in assembly '%s' contains native code and mono can't run it. The assembly was probably created by Managed C++.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
 
                if (!piinfo->addr) {
@@ -10006,8 +10196,7 @@ mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, in
 
                mono_destroy_compile (cfg);
                if (error) {
-                       MonoException *ex = mini_loader_error_to_exception (error);
-                       mono_loader_clear_error ();
+                       MonoException *ex = mono_loader_error_prepare_exception (error);
                        mono_raise_exception (ex);
                } else {
                        g_assert_not_reached ();
@@ -10183,6 +10372,7 @@ mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
        mono_domain_lock (domain);
        g_hash_table_remove (domain->dynamic_code_hash, method);
        g_hash_table_remove (domain->jit_code_hash, method);
+       g_hash_table_remove (domain->jump_trampoline_hash, method);
        mono_domain_unlock (domain);
 
 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
@@ -10279,10 +10469,6 @@ mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObjec
 #ifdef __sparc
 #define GET_CONTEXT \
     void *ctx = context;
-#elif defined(sun)    // Solaris x86
-#define GET_CONTEXT \
-    ucontext_t *uctx = context; \
-    struct sigcontext *ctx = (struct sigcontext *)&(uctx->uc_mcontext);
 #elif defined (MONO_ARCH_USE_SIGACTION)
 #define GET_CONTEXT \
     void *ctx = context;
@@ -10678,7 +10864,7 @@ mini_init (const char *filename)
        MonoDomain *domain;
 
        /* Happens when using the embedding interface */
-       if (default_opt == 0)
+       if (!default_opt_set)
                default_opt = mono_parse_default_optimizations (NULL);
 
        InitializeCriticalSection (&jit_mutex);
@@ -10714,6 +10900,7 @@ mini_init (const char *filename)
                pthread_attr_t attr;
                pthread_getattr_np (pthread_self (), &attr);
                pthread_attr_getstack (&attr, &sstart, &size);
+               pthread_attr_destroy (&attr); 
                /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
 #ifdef __ia64__
                /*
@@ -10888,8 +11075,8 @@ mini_init (const char *filename)
        register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
        register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
        register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
-       register_icall (helper_stelem_ref, "helper_stelem_ref", "void ptr int32 object", FALSE);
-       register_icall (helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
+       register_icall (mono_helper_stelem_ref, "helper_stelem_ref", "void ptr int32 object", FALSE);
+       register_icall (mono_helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
        register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
        register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
        register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
@@ -10898,8 +11085,11 @@ mini_init (const char *filename)
        register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
        register_icall (mono_ldftn_nosync, "mono_ldftn_nosync", "ptr ptr", FALSE);
        register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
-       register_icall (helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
-       register_icall (helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
+       register_icall (mono_helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
+       register_icall (mono_helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
+       register_icall (mono_helper_ldstr_mscorlib, "helper_ldstr_mscorlib", "object int", FALSE);
+       register_icall (mono_helper_newobj_mscorlib, "helper_newobj_mscorlib", "object int", FALSE);
+       register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
 #endif
 
 #define JIT_RUNTIME_WORKS
@@ -11005,7 +11195,17 @@ mini_cleanup (MonoDomain *domain)
                g_hash_table_destroy (class_init_hash_addr);
        g_free (emul_opcode_map);
 
+       mono_cleanup ();
+
+       mono_trace_cleanup ();
+
        mono_counters_dump (-1, stdout);
+
+       TlsFree(mono_jit_tls_id);
+
+       DeleteCriticalSection (&jit_mutex);
+
+       DeleteCriticalSection (&mono_delegate_section);
 }
 
 void
@@ -11013,6 +11213,7 @@ mono_set_defaults (int verbose_level, guint32 opts)
 {
        mini_verbose = verbose_level;
        default_opt = opts;
+       default_opt_set = TRUE;
 }
 
 static void