2010-07-09 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / mini / method-to-ir.c
index 7d19e5668db07be3b933f5d51e714cfbbb2e046b..9209ea28238463dc8a9fecfdae8ec243eeca21e9 100644 (file)
@@ -115,6 +115,7 @@ void mini_emit_initobj (MonoCompile *cfg, MonoInst *dest, const guchar *ip, Mono
 extern MonoMethodSignature *helper_sig_class_init_trampoline;
 extern MonoMethodSignature *helper_sig_domain_get;
 extern MonoMethodSignature *helper_sig_generic_class_init_trampoline;
+extern MonoMethodSignature *helper_sig_generic_class_init_trampoline_llvm;
 extern MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline;
 extern MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline;
 extern MonoMethodSignature *helper_sig_monitor_enter_exit_trampoline_llvm;
@@ -2083,8 +2084,37 @@ callvirt_to_call_membase (int opcode)
 static void
 emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
 {
+       int method_reg;
+
+       if (COMPILE_LLVM (cfg)) {
+               method_reg = alloc_preg (cfg);
+
+               if (imt_arg) {
+                       MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
+               } else if (cfg->compile_aot) {
+                       MONO_EMIT_NEW_AOTCONST (cfg, method_reg, call->method, MONO_PATCH_INFO_METHODCONST);
+               } else {
+                       MonoInst *ins;
+                       MONO_INST_NEW (cfg, ins, OP_PCONST);
+                       ins->inst_p0 = call->method;
+                       ins->dreg = method_reg;
+                       MONO_ADD_INS (cfg->cbb, ins);
+               }
+
+#ifdef ENABLE_LLVM
+               call->imt_arg_reg = method_reg;
+#endif
 #ifdef MONO_ARCH_IMT_REG
-       int method_reg = alloc_preg (cfg);
+       mono_call_inst_add_outarg_reg (cfg, call, method_reg, MONO_ARCH_IMT_REG, FALSE);
+#else
+       /* Need this to keep the IMT arg alive */
+       mono_call_inst_add_outarg_reg (cfg, call, method_reg, 0, FALSE);
+#endif
+               return;
+       }
+
+#ifdef MONO_ARCH_IMT_REG
+       method_reg = alloc_preg (cfg);
 
        if (imt_arg) {
                MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
@@ -2226,10 +2256,24 @@ mono_emit_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, Mo
        return (MonoInst*)call;
 }
 
+static void
+set_rgctx_arg (MonoCompile *cfg, MonoCallInst *call, int rgctx_reg, MonoInst *rgctx_arg)
+{
+#ifdef MONO_ARCH_RGCTX_REG
+       mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
+       cfg->uses_rgctx_reg = TRUE;
+       call->rgctx_reg = TRUE;
+#ifdef ENABLE_LLVM
+       call->rgctx_arg_reg = rgctx_reg;
+#endif
+#else
+       NOT_IMPLEMENTED;
+#endif
+}      
+
 inline static MonoInst*
 mono_emit_rgctx_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg)
 {
-#ifdef MONO_ARCH_RGCTX_REG
        MonoCallInst *call;
        int rgctx_reg = -1;
 
@@ -2238,16 +2282,9 @@ mono_emit_rgctx_calli (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **ar
                MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, rgctx_arg->dreg);
        }
        call = (MonoCallInst*)mono_emit_calli (cfg, sig, args, addr);
-       if (rgctx_arg) {
-               mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
-               cfg->uses_rgctx_reg = TRUE;
-               call->rgctx_reg = TRUE;
-       }
+       if (rgctx_arg)
+               set_rgctx_arg (cfg, call, rgctx_reg, rgctx_arg);
        return (MonoInst*)call;
-#else
-       g_assert_not_reached ();
-       return NULL;
-#endif
 }
 
 static MonoInst*
@@ -2402,32 +2439,19 @@ static MonoInst*
 mono_emit_rgctx_method_call_full (MonoCompile *cfg, MonoMethod *method, MonoMethodSignature *sig,
                MonoInst **args, MonoInst *this, MonoInst *imt_arg, MonoInst *vtable_arg)
 {
-#ifdef MONO_ARCH_RGCTX_REG
        int rgctx_reg = 0;
-#endif
        MonoInst *ins;
        MonoCallInst *call;
 
        if (vtable_arg) {
-#ifdef MONO_ARCH_RGCTX_REG
                rgctx_reg = mono_alloc_preg (cfg);
                MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, vtable_arg->dreg);
-#else
-               NOT_IMPLEMENTED;
-#endif
        }
        ins = mono_emit_method_call_full (cfg, method, sig, args, this, imt_arg);
 
        call = (MonoCallInst*)ins;
-       if (vtable_arg) {
-#ifdef MONO_ARCH_RGCTX_REG
-               mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
-               cfg->uses_rgctx_reg = TRUE;
-               call->rgctx_reg = TRUE;
-#else
-               NOT_IMPLEMENTED;
-#endif
-       }
+       if (vtable_arg)
+               set_rgctx_arg (cfg, call, rgctx_reg, vtable_arg);
 
        return ins;
 }
@@ -2542,6 +2566,123 @@ get_memcpy_method (void)
        return memcpy_method;
 }
 
+static void
+create_write_barrier_bitmap (MonoClass *klass, unsigned *wb_bitmap, int offset)
+{
+       MonoClassField *field;
+       gpointer iter = NULL;
+
+       while ((field = mono_class_get_fields (klass, &iter))) {
+               int foffset;
+
+               if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
+                       continue;
+               foffset = klass->valuetype ? field->offset - sizeof (MonoObject): field->offset;
+               if (mono_type_is_reference (field->type)) {
+                       g_assert ((foffset % SIZEOF_VOID_P) == 0);
+                       *wb_bitmap |= 1 << ((offset + foffset) / SIZEOF_VOID_P);
+               } else {
+                       /*FIXME support nested value types so this works for: struct X { Y y; int z;} struct Y { object a,b; }*/
+                       MonoClass *field_class = mono_class_from_mono_type (field->type);
+                       if (field_class->has_references)
+                               create_write_barrier_bitmap (field_class, wb_bitmap, offset + foffset);
+               }
+       }
+}
+
+static gboolean
+mono_emit_wb_aware_memcpy (MonoCompile *cfg, MonoClass *klass, MonoInst *iargs[4], int size, int align)
+{
+       int dest_ptr_reg, tmp_reg, destreg, srcreg, offset;
+       unsigned need_wb = 0;
+
+       if (align == 0)
+               align = 4;
+
+       /*types with references can't have alignment smaller than sizeof(void*) */
+       if (align < SIZEOF_VOID_P)
+               return FALSE;
+
+       /*This value cannot be bigger than 32 due to the way we calculate the required wb bitmap.*/
+       if (size > 32 * SIZEOF_VOID_P)
+               return FALSE;
+
+       create_write_barrier_bitmap (klass, &need_wb, 0);
+
+       /* We don't unroll more than 5 stores to avoid code bloat. */
+       if (size > 5 * SIZEOF_VOID_P) {
+               /*This is harmless and simplify mono_gc_wbarrier_value_copy_bitmap */
+               size += (SIZEOF_VOID_P - 1);
+               size &= ~(SIZEOF_VOID_P - 1);
+
+               EMIT_NEW_ICONST (cfg, iargs [2], size);
+               EMIT_NEW_ICONST (cfg, iargs [3], need_wb);
+               mono_emit_jit_icall (cfg, mono_gc_wbarrier_value_copy_bitmap, iargs);
+               return TRUE;
+       }
+
+       destreg = iargs [0]->dreg;
+       srcreg = iargs [1]->dreg;
+       offset = 0;
+
+       dest_ptr_reg = alloc_preg (cfg);
+       tmp_reg = alloc_preg (cfg);
+
+       /*tmp = dreg*/
+       EMIT_NEW_UNALU (cfg, iargs [0], OP_MOVE, dest_ptr_reg, destreg);
+
+       while (size >= SIZEOF_VOID_P) {
+               MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, tmp_reg, srcreg, offset);
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREP_MEMBASE_REG, dest_ptr_reg, 0, tmp_reg);
+
+               if (need_wb & 0x1) {
+                       MonoInst *dummy_use;
+
+                       MonoMethod *write_barrier = mono_gc_get_write_barrier ();
+                       mono_emit_method_call (cfg, write_barrier, &iargs [0], NULL);
+
+                       MONO_INST_NEW (cfg, dummy_use, OP_DUMMY_USE);
+                       dummy_use->sreg1 = dest_ptr_reg;
+                       MONO_ADD_INS (cfg->cbb, dummy_use);
+               }
+
+
+               offset += SIZEOF_VOID_P;
+               size -= SIZEOF_VOID_P;
+               need_wb >>= 1;
+
+               /*tmp += sizeof (void*)*/
+               if (size >= SIZEOF_VOID_P) {
+                       NEW_BIALU_IMM (cfg, iargs [0], OP_PADD_IMM, dest_ptr_reg, dest_ptr_reg, SIZEOF_VOID_P);
+                       MONO_ADD_INS (cfg->cbb, iargs [0]);
+               }
+       }
+
+       /* Those cannot be references since size < sizeof (void*) */
+       while (size >= 4) {
+               MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, tmp_reg, srcreg, offset);
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, destreg, offset, tmp_reg);
+               offset += 4;
+               size -= 4;
+       }
+
+       while (size >= 2) {
+               MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI2_MEMBASE, tmp_reg, srcreg, offset);
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI2_MEMBASE_REG, destreg, offset, tmp_reg);
+               offset += 2;
+               size -= 2;
+       }
+
+       while (size >= 1) {
+               MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI1_MEMBASE, tmp_reg, srcreg, offset);
+               MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI1_MEMBASE_REG, destreg, offset, tmp_reg);
+               offset += 1;
+               size -= 1;
+       }
+
+       return TRUE;
+}
+
 /*
  * Emit code to copy a valuetype of type @klass whose address is stored in
  * @src->dreg to memory whose address is stored at @dest->dreg.
@@ -2549,7 +2690,7 @@ get_memcpy_method (void)
 void
 mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *klass, gboolean native)
 {
-       MonoInst *iargs [3];
+       MonoInst *iargs [4];
        int n;
        guint32 align = 0;
        MonoMethod *memcpy_method;
@@ -2565,9 +2706,8 @@ mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *kla
        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 (klass->has_references && !native) {
+       if (cfg->gen_write_barriers && klass->has_references && !native) {
                /* Avoid barriers when storing to the stack */
                if (!((dest->opcode == OP_ADD_IMM && dest->sreg1 == cfg->frame_reg) ||
                          (dest->opcode == OP_LDADDR))) {
@@ -2578,9 +2718,13 @@ mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *kla
 
                        if (cfg->generic_sharing_context)
                                context_used = mono_class_check_context_used (klass);
-                       if (context_used) {
+
+                       /* It's ok to intrinsify under gsharing since shared code types are layout stable. */
+                       if ((cfg->opt & MONO_OPT_INTRINS) && mono_emit_wb_aware_memcpy (cfg, klass, iargs, n, align)) {
+                               return;
+                       } else if (context_used) {
                                iargs [2] = emit_get_rgctx_klass (cfg, context_used, klass, MONO_RGCTX_INFO_KLASS);
-                       } else {
+                       }  else {
                                if (cfg->compile_aot) {
                                        EMIT_NEW_CLASSCONST (cfg, iargs [2], klass);
                                } else {
@@ -2589,13 +2733,10 @@ mini_emit_stobj (MonoCompile *cfg, MonoInst *dest, MonoInst *src, MonoClass *kla
                                }
                        }
 
-                       /* FIXME: this does the memcpy as well (or
-                          should), so we don't need the memcpy
-                          afterwards */
                        mono_emit_jit_icall (cfg, mono_value_copy, iargs);
+                       return;
                }
        }
-#endif
 
        if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
                /* FIXME: Optimize the case when src/dest is OP_LDADDR */
@@ -2793,7 +2934,10 @@ emit_generic_class_init (MonoCompile *cfg, MonoClass *klass)
                EMIT_NEW_VTABLECONST (cfg, vtable_arg, vtable);
        }
 
-       call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_GENERIC_CLASS_INIT, NULL, helper_sig_generic_class_init_trampoline, &vtable_arg);
+       if (COMPILE_LLVM (cfg))
+               call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_GENERIC_CLASS_INIT, NULL, helper_sig_generic_class_init_trampoline_llvm, &vtable_arg);
+       else
+               call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_GENERIC_CLASS_INIT, NULL, helper_sig_generic_class_init_trampoline, &vtable_arg);
 #ifdef MONO_ARCH_VTABLE_REG
        mono_call_inst_add_outarg_reg (cfg, call, vtable_arg->dreg, MONO_ARCH_VTABLE_REG, FALSE);
        cfg->uses_vtable_reg = TRUE;
@@ -3921,6 +4065,21 @@ emit_array_generic_access (MonoCompile *cfg, MonoMethodSignature *fsig, MonoInst
        return store;
 }
 
+static MonoInst*
+mini_emit_inst_for_ctor (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
+{
+       MonoInst *ins = NULL;
+#ifdef MONO_ARCH_SIMD_INTRINSICS
+       if (cfg->opt & MONO_OPT_SIMD) {
+               ins = mono_emit_simd_intrinsics (cfg, cmethod, fsig, args);
+               if (ins)
+                       return ins;
+       }
+#endif
+
+       return ins;
+}
+
 static MonoInst*
 mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
 {
@@ -4228,14 +4387,12 @@ mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSign
                                g_assert_not_reached ();
                        }
 
-#if HAVE_WRITE_BARRIERS
-                       if (is_ref) {
+                       if (cfg->gen_write_barriers && is_ref) {
                                MonoInst *dummy_use;
                                MonoMethod *write_barrier = mono_gc_get_write_barrier ();
                                mono_emit_method_call (cfg, write_barrier, &args [0], NULL);
                                EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [1]);
                        }
-#endif
                }
 #endif /* MONO_ARCH_HAVE_ATOMIC_EXCHANGE */
  
@@ -4268,14 +4425,12 @@ mini_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSign
                        } else {
                                /* g_assert_not_reached (); */
                        }
-#if HAVE_WRITE_BARRIERS
-                       if (is_ref) {
+                       if (cfg->gen_write_barriers && is_ref) {
                                MonoInst *dummy_use;
                                MonoMethod *write_barrier = mono_gc_get_write_barrier ();
                                mono_emit_method_call (cfg, write_barrier, &args [0], NULL);
                                EMIT_NEW_DUMMY_USE (cfg, dummy_use, args [1]);
                        }
-#endif
                }
 #endif /* MONO_ARCH_HAVE_ATOMIC_CAS */
 
@@ -4613,7 +4768,7 @@ inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig,
                        EMIT_NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
                        *sp++ = ins;
                }
-               mono_metadata_free_mh (cheader);
+               cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
                return costs + 1;
        } else {
                if (cfg->verbose_level > 2)
@@ -4624,7 +4779,7 @@ inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig,
                /* This gets rid of the newly added bblocks */
                cfg->cbb = prev_cbb;
        }
-       mono_metadata_free_mh (cheader);
+       cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, cheader);
        return 0;
 }
 
@@ -4997,7 +5152,7 @@ set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsign
        cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code);
        g_free (method_fname);
        g_free (method_code);
-       mono_metadata_free_mh (header);
+       cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
 }
 
 static void
@@ -5147,6 +5302,17 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
        image = method->klass->image;
        header = mono_method_get_header (method);
+       if (!header) {
+               MonoLoaderError *error;
+
+               if ((error = mono_loader_get_last_error ())) {
+                       cfg->exception_type = error->exception_type;
+               } else {
+                       cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
+                       cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
+               }
+               goto exception_exit;
+       }
        generic_container = mono_method_get_generic_container (method);
        sig = mono_method_signature (method);
        num_args = sig->hasthis + sig->param_count;
@@ -6277,9 +6443,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                /* Prevent inlining of methods that contain indirect calls */
                                INLINE_FAILURE;
 
-#if MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK
-                               /* The llvm vcall trampolines doesn't support generic virtual calls yet */
-                               if (cmethod->wrapper_type == MONO_WRAPPER_NONE && mono_use_imt && !mono_use_llvm) {
+#if MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK && defined(MONO_ARCH_GSHARED_SUPPORTED)
+                               if (cmethod->wrapper_type == MONO_WRAPPER_NONE && mono_use_imt) {
                                        g_assert (!imt_arg);
                                        if (!context_used)
                                                g_assert (cmethod->is_inflated);
@@ -6491,19 +6656,13 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                INLINE_FAILURE;
 
                                if (vtable_arg) {
-#ifdef MONO_ARCH_RGCTX_REG
                                        MonoCallInst *call;
                                        int rgctx_reg = mono_alloc_preg (cfg);
 
                                        MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, rgctx_reg, vtable_arg->dreg);
                                        ins = (MonoInst*)mono_emit_calli (cfg, fsig, sp, addr);
                                        call = (MonoCallInst*)ins;
-                                       mono_call_inst_add_outarg_reg (cfg, call, rgctx_reg, MONO_ARCH_RGCTX_REG, FALSE);
-                                       cfg->uses_rgctx_reg = TRUE;
-                                       call->rgctx_reg = TRUE;
-#else
-                                       NOT_IMPLEMENTED;
-#endif
+                                       set_rgctx_arg (cfg, call, rgctx_reg, vtable_arg);
                                } else {
                                        if (addr->opcode == OP_AOTCONST && addr->inst_c1 == MONO_PATCH_INFO_ICALL_ADDR) {
                                                /* 
@@ -6980,15 +7139,13 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        ins_flag = 0;
                        MONO_ADD_INS (bblock, ins);
 
-#if HAVE_WRITE_BARRIERS
-                       if (*ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [1]->opcode == OP_PCONST) && (sp [1]->inst_p0 == 0))) {
+                       if (cfg->gen_write_barriers && *ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [1]->opcode == OP_PCONST) && (sp [1]->inst_p0 == 0))) {
                                MonoInst *dummy_use;
                                /* insert call to write barrier */
                                MonoMethod *write_barrier = mono_gc_get_write_barrier ();
                                mono_emit_method_call (cfg, write_barrier, sp, NULL);
                                EMIT_NEW_DUMMY_USE (cfg, dummy_use, sp [1]);
                        }
-#endif
 
                        inline_costs += 1;
                        ++ip;
@@ -7190,6 +7347,13 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                NEW_STORE_MEMBASE (cfg, store, OP_STORE_MEMBASE_REG, sp [0]->dreg, 0, dreg);
                                store->flags |= ins_flag;
                                MONO_ADD_INS (cfg->cbb, store);
+
+                               if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER) {
+                                       MonoInst *dummy_use;
+                                       MonoMethod *write_barrier = mono_gc_get_write_barrier ();
+                                       mono_emit_method_call (cfg, write_barrier, sp, NULL);
+                                       EMIT_NEW_DUMMY_USE (cfg, dummy_use, sp [1]);
+                               }
                        } else {
                                mini_emit_stobj (cfg, sp [0], sp [1], klass, FALSE);
                        }
@@ -7497,6 +7661,19 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                if (cmethod->klass->marshalbyref)
                                        callvirt_this_arg = sp [0];
 
+
+                               if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_emit_inst_for_ctor (cfg, cmethod, fsig, sp))) {
+                                       if (!MONO_TYPE_IS_VOID (fsig->ret)) {
+                                               type_to_eval_stack_type ((cfg), fsig->ret, ins);
+                                               *sp = ins;
+                                               sp++;
+                                       }
+
+                                       CHECK_CFG_EXCEPTION;
+                               } else
+
+
+
                                if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
                                    mono_method_check_inlining (cfg, cmethod) &&
                                    !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
@@ -7897,9 +8074,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
 
                                        EMIT_NEW_STORE_MEMBASE_TYPE (cfg, store, field->type, sp [0]->dreg, foffset, sp [1]->dreg);
+                                       if (sp [0]->opcode != OP_LDADDR)
+                                               store->flags |= MONO_INST_FAULT;
 
-#if HAVE_WRITE_BARRIERS
-                               if (mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
+                               if (cfg->gen_write_barriers && mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
                                        /* insert call to write barrier */
                                        MonoMethod *write_barrier = mono_gc_get_write_barrier ();
                                        MonoInst *iargs [2], *dummy_use;
@@ -7912,7 +8090,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                                        EMIT_NEW_DUMMY_USE (cfg, dummy_use, sp [1]);
                                }
-#endif
 
                                        store->flags |= ins_flag;
                                }
@@ -7960,7 +8137,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        sp [0] = ins;
                                }
 
-                               MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
+                               MONO_EMIT_NULL_CHECK (cfg, sp [0]->dreg);
 
                                if (*ip == CEE_LDFLDA) {
                                        dreg = alloc_preg (cfg);
@@ -7974,7 +8151,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
 
                                        EMIT_NEW_LOAD_MEMBASE_TYPE (cfg, load, field->type, sp [0]->dreg, foffset);
                                        load->flags |= ins_flag;
-                                       load->flags |= MONO_INST_FAULT;
+                                       if (sp [0]->opcode != OP_LDADDR)
+                                               load->flags |= MONO_INST_FAULT;
                                        *sp++ = load;
                                }
                        }
@@ -8107,28 +8285,8 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                        depth, field->offset);
                                */
 
-                               if (mono_class_needs_cctor_run (klass, method)) {
-                                       MonoCallInst *call;
-                                       MonoInst *vtable;
-
-                                       vtable = emit_get_rgctx_klass (cfg, context_used,
-                                               klass, MONO_RGCTX_INFO_VTABLE);
-
-                                       // FIXME: This doesn't work since it tries to pass the argument
-                                       // in the normal way, instead of using MONO_ARCH_VTABLE_REG
-                                       /* 
-                                        * The vtable pointer is always passed in a register regardless of
-                                        * the calling convention, so assign it manually, and make a call
-                                        * using a signature without parameters.
-                                        */                                     
-                                       call = (MonoCallInst*)mono_emit_abs_call (cfg, MONO_PATCH_INFO_GENERIC_CLASS_INIT, NULL, helper_sig_generic_class_init_trampoline, &vtable);
-#ifdef MONO_ARCH_VTABLE_REG
-                                       mono_call_inst_add_outarg_reg (cfg, call, vtable->dreg, MONO_ARCH_VTABLE_REG, FALSE);
-                                       cfg->uses_vtable_reg = TRUE;
-#else
-                                       NOT_IMPLEMENTED;
-#endif
-                               }
+                               if (mono_class_needs_cctor_run (klass, method))
+                                       emit_generic_class_init (cfg, klass);
 
                                /*
                                 * The pointer we're computing here is
@@ -8300,6 +8458,14 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                        CHECK_TYPELOAD (klass);
                        /* FIXME: should check item at sp [1] is compatible with the type of the store. */
                        EMIT_NEW_STORE_MEMBASE_TYPE (cfg, ins, &klass->byval_arg, sp [0]->dreg, 0, sp [1]->dreg);
+                       if (cfg->gen_write_barriers && cfg->method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER &&
+                                       generic_class_is_reference_type (cfg, klass)) {
+                               MonoInst *dummy_use;
+                               /* insert call to write barrier */
+                               MonoMethod *write_barrier = mono_gc_get_write_barrier ();
+                               mono_emit_method_call (cfg, write_barrier, sp, NULL);
+                               EMIT_NEW_DUMMY_USE (cfg, dummy_use, sp [1]);
+                       }
                        ins_flag = 0;
                        ip += 5;
                        inline_costs += 1;
@@ -9358,9 +9524,9 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                                if (cfg->generic_sharing_context)
                                                        invoke_context_used = mono_method_check_context_used (invoke);
 
-#if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE) && !defined(HAVE_WRITE_BARRIERS)
+#if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE)
                                                /* FIXME: SGEN support */
-                                               if (invoke_context_used == 0) {
+                                               if (!cfg->gen_write_barriers && invoke_context_used == 0) {
                                                        MonoInst *target_ins;
 
                                                        ip += 6;
@@ -9648,6 +9814,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                MONO_INST_NEW (cfg, ins, OP_RETHROW);
                                ins->sreg1 = load->dreg;
                                MONO_ADD_INS (bblock, ins);
+
+                               MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
+                               MONO_ADD_INS (bblock, ins);
+
                                sp = stack_start;
                                link_bblock (cfg, bblock, end_bblock);
                                start_new_bblock = 1;
@@ -9827,7 +9997,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
                cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
                g_free (mname);
-               mono_metadata_free_mh (header);
+               cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
                mono_basic_block_free (original_bb);
                return -1;
        }
@@ -9835,7 +10005,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
        if ((cfg->verbose_level > 2) && (cfg->method == method)) 
                mono_print_code (cfg, "AFTER METHOD-TO-IR");
 
-       mono_metadata_free_mh (header);
+       cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
        mono_basic_block_free (original_bb);
        return inline_costs;
  
@@ -9858,7 +10028,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
        g_slist_free (class_inits);
        mono_basic_block_free (original_bb);
        dont_inline = g_list_remove (dont_inline, method);
-       mono_metadata_free_mh (header);
+       cfg->headers_to_free = g_slist_prepend_mempool (cfg->mempool, cfg->headers_to_free, header);
        return -1;
 }