Merge pull request #4767 from lambdageek/bug-55681
[mono.git] / mono / mini / interp / transform.c
index 797d01ab65409b48c91e07050f55c47947b0624b..d76c9af55a7106f54ad06fd220174e5a14bdf4fd 100644 (file)
@@ -1,4 +1,5 @@
-/*
+/**
+ * \file
  * transform CIL into different opcodes for more
  * efficient interpretation
  *
@@ -496,13 +497,13 @@ store_arg(TransformData *td, int n)
        mt = mint_type (type);
        if (mt == MINT_TYPE_VT) {
                gint32 size;
-               g_error ("data.klass");
+               MonoClass *klass = mono_class_from_mono_type (type);
                if (mono_method_signature (td->method)->pinvoke)
-                       size = mono_class_native_size (type->data.klass, NULL);
+                       size = mono_class_native_size (klass, NULL);
                else
-                       size = mono_class_value_size (type->data.klass, NULL);
+                       size = mono_class_value_size (klass, NULL);
                ADD_CODE(td, MINT_STARG_VT);
-               ADD_CODE(td, n);
+               ADD_CODE(td, td->rtm->arg_offsets [n]);
                WRITE32(td, &size);
                if (td->sp [-1].type == STACK_TYPE_VT)
                        POP_VT(td, size);
@@ -635,8 +636,37 @@ get_data_item_index (TransformData *td, void *ptr)
        return index;
 }
 
+static gboolean
+jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
+{
+       GSList *l;
+
+       if (sig->param_count > 6)
+               return FALSE;
+       if (sig->pinvoke)
+               return FALSE;
+       if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
+               return FALSE;
+       if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
+               return FALSE;
+       if (method->is_inflated)
+               return FALSE;
+       if (method->string_ctor)
+               return FALSE;
+
+       for (l = jit_classes; l; l = l->next) {
+               char *class_name = l->data;
+               // FIXME: Namespaces
+               if (!strcmp (method->klass->name, class_name))
+                       return TRUE;
+       }
+
+       //return TRUE;
+       return FALSE;
+}
+
 static void
-interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class)
+interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class, gboolean readonly)
 {
        MonoImage *image = method->klass->image;
        MonoMethodSignature *csignature;
@@ -681,11 +711,14 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
                                        else if (strcmp (target_method->name, "get_Length") == 0)
                                                op = MINT_STRLEN;
                                }
-                       } else if (target_method->klass == mono_defaults.array_class) {
-                               if (strcmp (target_method->name, "get_Rank") == 0)
+                       } else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
+                               if (!strcmp (target_method->name, "get_Rank")) {
                                        op = MINT_ARRAY_RANK;
-                               else if (strcmp (target_method->name, "get_Length") == 0)
+                               } else if (!strcmp (target_method->name, "get_Length")) {
                                        op = MINT_LDLEN;
+                               } else if (!strcmp (target_method->name, "Address")) {
+                                       op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
+                               }
                        } else if (target_method && generic_context) {
                                csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
@@ -778,14 +811,10 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
                        int offset;
                        if (mono_interp_traceopt)
                                g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
-                       for (i = csignature->param_count - 1; i >= 0; --i)
-                               store_arg (td, i + csignature->hasthis);
 
-                       if (csignature->hasthis) {
-                               g_error ("STTHIS removal");
-                               // ADD_CODE(td, MINT_STTHIS);
-                               --td->sp;
-                       }
+                       for (i = csignature->param_count - 1 + !!csignature->hasthis; i >= 0; --i)
+                               store_arg (td, i);
+
                        ADD_CODE(td, MINT_BR_S);
                        offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
                        ADD_CODE(td, offset);
@@ -850,11 +879,19 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
                is_void = TRUE;
 
        if (op >= 0) {
-               ADD_CODE(td, op);
+               ADD_CODE (td, op);
 #if SIZEOF_VOID_P == 8
                if (op == MINT_LDLEN)
-                       ADD_CODE(td, MINT_CONV_I4_I8);
+                       ADD_CODE (td, MINT_CONV_I4_I8);
 #endif
+               if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
+                       ADD_CODE (td, get_data_item_index (td, target_method->klass));
+                       ADD_CODE (td, 1 + target_method->klass->rank);
+               }
+       } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
+               ADD_CODE(td, MINT_JIT_CALL);
+               ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
+               mono_error_assert_ok (&error);
        } else {
                if (calli)
                        ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
@@ -862,7 +899,7 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
                        ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
                else
                        ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
-               
+
                if (calli) {
                        ADD_CODE(td, get_data_item_index (td, (void *)csignature));
                } else {
@@ -879,6 +916,22 @@ interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target
        }
 }
 
+static MonoClassField *
+interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, MonoGenericContext *generic_context)
+{
+       MonoClassField *field = NULL;
+       if (method->wrapper_type != MONO_WRAPPER_NONE) {
+               field = (MonoClassField *) mono_method_get_wrapper_data (method, token);
+               *klass = field->parent;
+       } else {
+               MonoError error;
+               error_init (&error);
+               field = mono_field_from_token_checked (method->klass->image, token, klass, generic_context, &error);
+               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
+       }
+       return field;
+}
+
 static void
 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
 {
@@ -927,7 +980,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        td.new_ip = td.new_code;
        td.last_new_ip = NULL;
 
-       td.stack = g_malloc0(header->max_stack * sizeof(td.stack[0]));
+       td.stack = g_malloc0 ((header->max_stack + 1) * sizeof (td.stack [0]));
        td.sp = td.stack;
        td.max_stack_height = 0;
 
@@ -941,6 +994,17 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
                td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
                td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
+
+               if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
+                       td.stack_height [c->data.filter_offset] = 0;
+                       td.vt_stack_size [c->data.filter_offset] = 0;
+                       td.is_bb_start [c->data.filter_offset] = 1;
+
+                       td.stack_height [c->data.filter_offset] = 1;
+                       td.stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
+                       td.stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
+                       td.stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
+               }
        }
 
        td.ip = header->code;
@@ -1068,15 +1132,9 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        break;
                case CEE_LDARGA_S: {
                        /* NOTE: n includes this */
-                       int n = ((guint8 *)td.ip)[1];
-                       if (n == 0 && signature->hasthis) {
-                               g_error ("LDTHISA: NOPE");
-                               ADD_CODE(&td, MINT_LDTHISA);
-                       }
-                       else {
-                               ADD_CODE(&td, MINT_LDARGA);
-                               ADD_CODE(&td, td.rtm->arg_offsets [n]);
-                       }
+                       int n = ((guint8 *) td.ip) [1];
+                       ADD_CODE (&td, MINT_LDARGA);
+                       ADD_CODE (&td, td.rtm->arg_offsets [n]);
                        PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
                        td.ip += 2;
                        break;
@@ -1219,8 +1277,9 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                case CEE_CALLVIRT: /* Fall through */
                case CEE_CALLI:    /* Fall through */
                case CEE_CALL: {
-                       interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class);
+                       interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
                        constrained_class = NULL;
+                       readonly = FALSE;
                        break;
                }
                case CEE_RET: {
@@ -1360,28 +1419,35 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        unsigned short *next_new_ip;
                        ++td.ip;
                        n = read32 (td.ip);
-                       ADD_CODE(&td, MINT_SWITCH);
-                       ADD_CODE(&td, * (unsigned short *)(&n));
-                       ADD_CODE(&td, * ((unsigned short *)&n + 1));
+                       ADD_CODE (&td, MINT_SWITCH);
+                       WRITE32 (&td, &n);
                        td.ip += 4;
                        next_ip = td.ip + n * 4;
                        next_new_ip = td.new_ip + n * 2;
+                       --td.sp;
+                       int stack_height = td.sp - td.stack;
                        for (i = 0; i < n; i++) {
                                offset = read32 (td.ip);
                                target = next_ip - td.il_code + offset;
-                               if (offset < 0)
+                               if (offset < 0) {
+#if DEBUG_INTERP
+                                       if (stack_height > 0 && stack_height != td.stack_height [target])
+                                               g_warning ("SWITCH with back branch and non-empty stack");
+#endif
                                        target = td.in_offsets [target] - (next_new_ip - td.new_code);
-                               else {
+                               } else {
+                                       td.stack_height [target] = stack_height;
+                                       td.vt_stack_size [target] = td.vt_sp;
+                                       if (stack_height > 0)
+                                               td.stack_state [target] = g_memdup (td.stack, stack_height * sizeof (td.stack [0]));
                                        int prev = td.forward_refs [target];
                                        td.forward_refs [td.ip - td.il_code] = prev;
                                        td.forward_refs [target] = td.ip - td.il_code;
                                        td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
                                }
-                               ADD_CODE(&td, * (unsigned short *)(&target));
-                               ADD_CODE(&td, * ((unsigned short *)&target + 1));
+                               WRITE32 (&td, &target);
                                td.ip += 4;
                        }
-                       --td.sp;
                        break;
                }
                case CEE_LDIND_I1:
@@ -1893,7 +1959,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                case CEE_CASTCLASS:
                        CHECK_STACK (&td, 1);
                        token = read32 (td.ip + 1);
-                       klass = mono_class_get_full (image, token, generic_context);
+                       klass = mini_get_class (method, token, generic_context);
                        ADD_CODE(&td, MINT_CASTCLASS);
                        ADD_CODE(&td, get_data_item_index (&td, klass));
                        td.sp [-1].klass = klass;
@@ -1902,7 +1968,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                case CEE_ISINST:
                        CHECK_STACK (&td, 1);
                        token = read32 (td.ip + 1);
-                       klass = mono_class_get_full (image, token, generic_context);
+                       klass = mini_get_class (method, token, generic_context);
                        ADD_CODE(&td, MINT_ISINST);
                        ADD_CODE(&td, get_data_item_index (&td, klass));
                        td.ip += 5;
@@ -1945,15 +2011,18 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        CHECK_STACK (&td, 1);
                        token = read32 (td.ip + 1);
 
-                       g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
-                       klass = mono_class_get_full (image, token, generic_context);
+                       klass = mini_get_class (method, token, generic_context);
 
                        if (mini_type_is_reference (&klass->byval_arg)) {
-                               g_error ("unbox_any: generic class is reference type");
+                               int mt = mint_type (&klass->byval_arg);
+                               ADD_CODE (&td, MINT_CASTCLASS);
+                               ADD_CODE (&td, get_data_item_index (&td, klass));
+                               SET_TYPE (td.sp - 1, stack_type [mt], klass);
+                               td.ip += 5;
                        } else if (mono_class_is_nullable (klass)) {
                                MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
                                /* td.ip is incremented by interp_transform_call */
-                               interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
+                               interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
                        } else {
                                int mt = mint_type (&klass->byval_arg);
                                ADD_CODE (&td, MINT_UNBOX);
@@ -1980,28 +2049,49 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                case CEE_LDFLDA:
                        CHECK_STACK (&td, 1);
                        token = read32 (td.ip + 1);
-                       field = mono_field_from_token (image, token, &klass, generic_context);
+                       field = interp_field_from_token (method, token, &klass, generic_context);
+                       gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
                        mono_class_init (klass);
-                       mt = mint_type(field->type);
-                       ADD_CODE(&td, MINT_LDFLDA);
-                       ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
+                       if (is_static) {
+                               ADD_CODE (&td, MINT_POP);
+                               ADD_CODE (&td, 0);
+                               ADD_CODE (&td, MINT_LDSFLDA);
+                               ADD_CODE (&td, get_data_item_index (&td, field));
+                       } else {
+                               if ((td.sp - 1)->type == STACK_TYPE_O) {
+                                       ADD_CODE (&td, MINT_LDFLDA);
+                               } else {
+                                       g_assert ((td.sp -1)->type == STACK_TYPE_MP);
+                                       ADD_CODE (&td, MINT_LDFLDA_UNSAFE);
+                               }
+                               ADD_CODE (&td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
+                       }
                        td.ip += 5;
                        SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
                        break;
                case CEE_LDFLD: {
                        CHECK_STACK (&td, 1);
                        token = read32 (td.ip + 1);
-                       field = mono_field_from_token (image, token, &klass, generic_context);
+                       field = interp_field_from_token (method, token, &klass, generic_context);
+                       gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
                        mono_class_init (klass);
 
                        MonoClass *field_klass = mono_class_from_mono_type (field->type);
                        mt = mint_type (&field_klass->byval_arg);
                        if (klass->marshalbyref) {
+                               g_assert (!is_static);
                                ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT :  MINT_LDRMFLD);
                                ADD_CODE(&td, get_data_item_index (&td, field));
                        } else  {
-                               ADD_CODE(&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
-                               ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
+                               if (is_static) {
+                                       ADD_CODE (&td, MINT_POP);
+                                       ADD_CODE (&td, 0);
+                                       ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
+                                       ADD_CODE (&td, get_data_item_index (&td, field));
+                               } else {
+                                       ADD_CODE (&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
+                                       ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
+                               }
                        }
                        if (mt == MINT_TYPE_VT) {
                                int size = mono_class_value_size (field_klass, NULL);
@@ -2020,18 +2110,28 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
                        break;
                }
-               case CEE_STFLD:
+               case CEE_STFLD: {
                        CHECK_STACK (&td, 2);
                        token = read32 (td.ip + 1);
-                       field = mono_field_from_token (image, token, &klass, generic_context);
+                       field = interp_field_from_token (method, token, &klass, generic_context);
+                       gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
                        mono_class_init (klass);
                        mt = mint_type(field->type);
+
                        if (klass->marshalbyref) {
+                               g_assert (!is_static);
                                ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
                                ADD_CODE(&td, get_data_item_index (&td, field));
                        } else  {
-                               ADD_CODE(&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
-                               ADD_CODE(&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
+                               if (is_static) {
+                                       ADD_CODE (&td, MINT_POP);
+                                       ADD_CODE (&td, 1);
+                                       ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
+                                       ADD_CODE (&td, get_data_item_index (&td, field));
+                               } else {
+                                       ADD_CODE (&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
+                                       ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
+                               }
                        }
                        if (mt == MINT_TYPE_VT) {
                                MonoClass *klass = mono_class_from_mono_type (field->type);
@@ -2042,9 +2142,10 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        td.ip += 5;
                        td.sp -= 2;
                        break;
+               }
                case CEE_LDSFLDA:
                        token = read32 (td.ip + 1);
-                       field = mono_field_from_token (image, token, &klass, generic_context);
+                       field = interp_field_from_token (method, token, &klass, generic_context);
                        ADD_CODE(&td, MINT_LDSFLDA);
                        ADD_CODE(&td, get_data_item_index (&td, field));
                        td.ip += 5;
@@ -2052,7 +2153,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        break;
                case CEE_LDSFLD:
                        token = read32 (td.ip + 1);
-                       field = mono_field_from_token (image, token, &klass, generic_context);
+                       field = interp_field_from_token (method, token, &klass, generic_context);
                        mt = mint_type(field->type);
                        ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
                        ADD_CODE(&td, get_data_item_index (&td, field));
@@ -2073,7 +2174,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                case CEE_STSFLD:
                        CHECK_STACK (&td, 1);
                        token = read32 (td.ip + 1);
-                       field = mono_field_from_token (image, token, &klass, generic_context);
+                       field = interp_field_from_token (method, token, &klass, generic_context);
                        mt = mint_type(field->type);
                        ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
                        ADD_CODE(&td, get_data_item_index (&td, field));
@@ -2093,7 +2194,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        if (method->wrapper_type != MONO_WRAPPER_NONE)
                                klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
                        else
-                               klass = mono_class_get_full (image, token, generic_context);
+                               klass = mini_get_class (method, token, generic_context);
 
                        ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
                        ADD_CODE(&td, get_data_item_index (&td, klass));
@@ -2142,6 +2243,8 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
                                break;
                        case STACK_TYPE_I8:
+                               if (*td.ip == CEE_CONV_OVF_I8_UN)
+                                       ADD_CODE (&td, MINT_CONV_OVF_I8_U8);
                                break;
                        case STACK_TYPE_I4:
                                ADD_CODE(&td, MINT_CONV_I8_U4);
@@ -2160,12 +2263,12 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        if (method->wrapper_type != MONO_WRAPPER_NONE)
                                klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
                        else
-                               klass = mono_class_get_full (image, token, generic_context);
+                               klass = mini_get_class (method, token, generic_context);
 
                        if (mono_class_is_nullable (klass)) {
                                MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
                                /* td.ip is incremented by interp_transform_call */
-                               interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL);
+                               interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
                        } else if (!klass->valuetype) {
                                /* already boxed, do nothing. */
                                td.ip += 5;
@@ -2184,20 +2287,30 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
 
                        break;
                }
-               case CEE_NEWARR:
+               case CEE_NEWARR: {
                        CHECK_STACK (&td, 1);
                        token = read32 (td.ip + 1);
 
                        if (method->wrapper_type != MONO_WRAPPER_NONE)
                                klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
                        else
-                               klass = mono_class_get_full (image, token, generic_context);
+                               klass = mini_get_class (method, token, generic_context);
 
-                       ADD_CODE(&td, MINT_NEWARR);
-                       ADD_CODE(&td, get_data_item_index (&td, klass));
-                       SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
+                       unsigned char lentype = (td.sp - 1)->type;
+                       if (lentype == STACK_TYPE_I8) {
+                               /* mimic mini behaviour */
+                               ADD_CODE (&td, MINT_CONV_OVF_U4_I8);
+                       } else {
+                               g_assert (lentype == STACK_TYPE_I4);
+                               ADD_CODE (&td, MINT_CONV_OVF_U4_I4);
+                       }
+                       SET_SIMPLE_TYPE (td.sp - 1, STACK_TYPE_I4);
+                       ADD_CODE (&td, MINT_NEWARR);
+                       ADD_CODE (&td, get_data_item_index (&td, klass));
+                       SET_TYPE (td.sp - 1, STACK_TYPE_O, klass);
                        td.ip += 5;
                        break;
+               }
                case CEE_LDLEN:
                        CHECK_STACK (&td, 1);
                        SIMPLE_OP (td, MINT_LDLEN);
@@ -2211,14 +2324,16 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        if (method->wrapper_type != MONO_WRAPPER_NONE)
                                klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
                        else
-                               klass = mono_class_get_full (image, token, generic_context);
+                               klass = mini_get_class (method, token, generic_context);
 
                        if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
                                ADD_CODE (&td, MINT_LDELEMA_TC);
                        } else {
                                ADD_CODE (&td, MINT_LDELEMA);
                        }
-                       ADD_CODE(&td, get_data_item_index (&td, klass));
+                       ADD_CODE (&td, get_data_item_index (&td, klass));
+                       /* according to spec, ldelema bytecode is only used for 1-dim arrays */
+                       ADD_CODE (&td, 2);
                        readonly = FALSE;
 
                        td.ip += 5;
@@ -2305,7 +2420,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                case CEE_LDELEM:
                        CHECK_STACK (&td, 2);
                        token = read32 (td.ip + 1);
-                       klass = mono_class_get_full (image, token, generic_context);
+                       klass = mini_get_class (method, token, generic_context);
                        switch (mint_type (&klass->byval_arg)) {
                                case MINT_TYPE_I1:
                                        ENSURE_I4 (&td, 1);
@@ -2435,11 +2550,20 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        CHECK_STACK (&td, 3);
                        ENSURE_I4 (&td, 2);
                        token = read32 (td.ip + 1);
-                       klass = mono_class_get_full (image, token, generic_context);
+                       klass = mini_get_class (method, token, generic_context);
                        switch (mint_type (&klass->byval_arg)) {
+                               case MINT_TYPE_U1:
+                                       SIMPLE_OP (td, MINT_STELEM_U1);
+                                       break;
+                               case MINT_TYPE_U2:
+                                       SIMPLE_OP (td, MINT_STELEM_U2);
+                                       break;
                                case MINT_TYPE_I4:
                                        SIMPLE_OP (td, MINT_STELEM_I4);
                                        break;
+                               case MINT_TYPE_I8:
+                                       SIMPLE_OP (td, MINT_STELEM_I8);
+                                       break;
                                case MINT_TYPE_O:
                                        SIMPLE_OP (td, MINT_STELEM_REF);
                                        break;
@@ -2568,7 +2692,10 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                        ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
                                break;
                        case STACK_TYPE_I8:
-                               ADD_CODE(&td, MINT_CONV_OVF_I4_I8);
+                               if (*td.ip == CEE_CONV_OVF_I4_UN)
+                                       ADD_CODE (&td, MINT_CONV_OVF_I4_U8);
+                               else
+                                       ADD_CODE (&td, MINT_CONV_OVF_I4_I8);
                                break;
                        default:
                                g_assert_not_reached ();
@@ -2632,6 +2759,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
                                break;
                        case STACK_TYPE_I8:
+                               ADD_CODE (&td, MINT_CONV_OVF_U8_I8);
                                break;
                        default:
                                g_assert_not_reached ();
@@ -2648,17 +2776,25 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
                                if (klass == mono_defaults.typehandle_class)
                                        handle = &((MonoClass *) handle)->byval_arg;
+
+                               if (generic_context) {
+                                       handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
+                                       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
+                               }
                        } else {
                                handle = mono_ldtoken (image, token, &klass, generic_context);
                        }
-                       mt = mint_type(&klass->byval_arg);
+                       mono_class_init (klass);
+                       mt = mint_type (&klass->byval_arg);
                        g_assert (mt == MINT_TYPE_VT);
                        size = mono_class_value_size (klass, NULL);
                        g_assert (size == sizeof(gpointer));
-                       PUSH_VT(&td, sizeof(gpointer));
-                       ADD_CODE(&td, MINT_LDTOKEN);
-                       ADD_CODE(&td, get_data_item_index (&td, handle));
-                       PUSH_SIMPLE_TYPE(&td, stack_type [mt]);
+                       PUSH_VT (&td, sizeof(gpointer));
+                       ADD_CODE (&td, MINT_LDTOKEN);
+                       ADD_CODE (&td, get_data_item_index (&td, handle));
+
+                       SET_TYPE (td.sp, stack_type [mt], klass);
+                       td.sp++;
                        td.ip += 5;
                        break;
                }
@@ -2687,6 +2823,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        ++td.ip;
                        break;
                case CEE_ENDFINALLY:
+                       td.sp = td.stack;
                        SIMPLE_OP (td, MINT_ENDFINALLY);
                        generating_code = 0;
                        break;
@@ -2710,7 +2847,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                        ADD_CODE (&td, MINT_POP);
                                        ADD_CODE (&td, 1);
                                        --td.sp;
-                                       interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL);
+                                       interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
                                        break;
                                case CEE_MONO_JIT_ICALL_ADDR: {
                                        guint32 token;
@@ -2848,6 +2985,21 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                g_assert(klass->valuetype);
                                SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
                                break;
+                       case CEE_MONO_TLS: {
+                               gint32 key = read32 (td.ip + 1);
+                               td.ip += 5;
+                               g_assert (key < TLS_KEY_NUM);
+                               ADD_CODE (&td, MINT_MONO_TLS);
+                               WRITE32 (&td, &key);
+                               PUSH_SIMPLE_TYPE (&td, STACK_TYPE_MP);
+                               break;
+                       }
+                       case CEE_MONO_ATOMIC_STORE_I4:
+                               CHECK_STACK (&td, 2);
+                               SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
+                               td.sp -= 2;
+                               td.ip++;
+                               break;
                        case CEE_MONO_SAVE_LMF:
                        case CEE_MONO_RESTORE_LMF:
                        case CEE_MONO_NOT_TAKEN:
@@ -2859,6 +3011,14 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
                                ++td.ip;
                                break;
+                       case CEE_MONO_JIT_ATTACH:
+                               ADD_CODE (&td, MINT_MONO_JIT_ATTACH);
+                               ++td.ip;
+                               break;
+                       case CEE_MONO_JIT_DETACH:
+                               ADD_CODE (&td, MINT_MONO_JIT_DETACH);
+                               ++td.ip;
+                               break;
                        default:
                                g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
                        }
@@ -2962,14 +3122,8 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                break;
                        case CEE_LDARGA: {
                                int n = read16 (td.ip + 1);
-                               if (n == 0 && signature->hasthis) {
-                                       g_error ("LDTHISA: NOPE");
-                                       ADD_CODE(&td, MINT_LDTHISA);
-                               }
-                               else {
-                                       ADD_CODE(&td, MINT_LDARGA);
-                                       ADD_CODE(&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
-                               }
+                               ADD_CODE (&td, MINT_LDARGA);
+                               ADD_CODE (&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
                                PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
                                td.ip += 3;
                                break;
@@ -3006,8 +3160,11 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                break;
 #if 0
                        case CEE_UNUSED57: ves_abort(); break;
-                       case CEE_ENDFILTER: ves_abort(); break;
 #endif
+                       case CEE_ENDFILTER:
+                               ADD_CODE (&td, MINT_ENDFILTER);
+                               ++td.ip;
+                               break;
                        case CEE_UNALIGNED_:
                                ++td.ip;
                                /* FIX: should do something? */;
@@ -3023,7 +3180,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        case CEE_INITOBJ:
                                CHECK_STACK(&td, 1);
                                token = read32 (td.ip + 1);
-                               klass = mono_class_get_full (image, token, generic_context);
+                               klass = mini_get_class (method, token, generic_context);
                                if (klass->valuetype) {
                                        ADD_CODE (&td, MINT_INITOBJ);
                                        i32 = mono_class_value_size (klass, NULL);
@@ -3048,7 +3205,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                break;
                        case CEE_CONSTRAINED_:
                                token = read32 (td.ip + 1);
-                               constrained_class = mono_class_get_full (image, token, generic_context);
+                               constrained_class = mini_get_class (method, token, generic_context);
                                mono_class_init (constrained_class);
                                td.ip += 5;
                                break;
@@ -3056,6 +3213,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                CHECK_STACK(&td, 3);
                                ADD_CODE(&td, MINT_INITBLK);
                                td.sp -= 3;
+                               td.ip += 1;
                                break;
 #if 0
                        case CEE_NO_:
@@ -3071,19 +3229,19 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                                gint32 size;
                                token = read32 (td.ip + 1);
                                td.ip += 5;
-                               if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
+                               if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
                                        int align;
                                        MonoType *type = mono_type_create_from_typespec (image, token);
                                        size = mono_type_size (type, &align);
                                } else {
-                                       guint32 align;
-                                       MonoClass *szclass = mono_class_get_full (image, token, generic_context);
+                                       int align;
+                                       MonoClass *szclass = mini_get_class (method, token, generic_context);
                                        mono_class_init (szclass);
 #if 0
                                        if (!szclass->valuetype)
                                                THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
 #endif
-                                       size = mono_class_value_size (szclass, &align);
+                                       size = mono_type_size (&szclass->byval_arg, &align);
                                } 
                                ADD_CODE(&td, MINT_LDC_I4);
                                WRITE32(&td, &size);
@@ -3118,10 +3276,11 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                        printf("\n");
                }
        }
+       g_assert (td.max_stack_height <= (header->max_stack + 1));
 
-       rtm->clauses = mono_mempool_alloc (domain->mp, header->num_clauses * sizeof(MonoExceptionClause));
+       rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
        memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
-       rtm->code = mono_mempool_alloc (domain->mp, (td.new_ip - td.new_code) * sizeof(gushort));
+       rtm->code = mono_domain_alloc0 (domain, (td.new_ip - td.new_code) * sizeof (gushort));
        memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
        g_free (td.new_code);
        rtm->new_body_start = rtm->code + body_start_offset;
@@ -3134,10 +3293,12 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
                end_off = c->handler_offset + c->handler_len;
                c->handler_offset = td.in_offsets [c->handler_offset];
                c->handler_len = td.in_offsets [end_off] - c->handler_offset;
+               if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
+                       c->data.filter_offset = td.in_offsets [c->data.filter_offset];
        }
        rtm->vt_stack_size = td.max_vt_sp;
        rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
-       rtm->data_items = mono_mempool_alloc (domain->mp, td.n_data_items * sizeof (td.data_items [0]));
+       rtm->data_items = mono_domain_alloc0 (domain, td.n_data_items * sizeof (td.data_items [0]));
        memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
        g_free (td.in_offsets);
        g_free (td.forward_refs);
@@ -3147,6 +3308,7 @@ generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, Mo
        g_free (td.stack_height);
        g_free (td.vt_stack_size);
        g_free (td.data_items);
+       g_free (td.stack);
        g_hash_table_destroy (td.data_hash);
 }
 
@@ -3180,9 +3342,11 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
        // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
        method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
        if (!method_class_vt->initialized) {
+               MonoError error;
                jmp_buf env;
                MonoInvocation *last_env_frame = context->env_frame;
                jmp_buf *old_env = context->current_env;
+               error_init (&error);
 
                if (setjmp(env)) {
                        MonoException *failed = context->env_frame->ex;
@@ -3193,7 +3357,10 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                }
                context->env_frame = context->current_frame;
                context->current_env = &env;
-               mono_runtime_class_init (method_class_vt);
+               mono_runtime_class_init_full (method_class_vt, &error);
+               if (!mono_error_ok (&error)) {
+                       return mono_error_convert_to_exception (&error);
+               }
                context->env_frame = last_env_frame;
                context->current_env = old_env;
        }
@@ -3213,7 +3380,6 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                mono_os_mutex_lock(&calc_section);
                if (runtime_method->transformed) {
                        mono_os_mutex_unlock(&calc_section);
-                       g_error ("FIXME: no jit info?");
                        mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
                        return NULL;
                }
@@ -3237,7 +3403,7 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                                } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
                                        nm = mono_marshal_get_delegate_end_invoke (method);
                                }
-                       } 
+                       }
                        if (nm == NULL) {
                                runtime_method->code = g_malloc(sizeof(short));
                                runtime_method->code[0] = MINT_CALLRUN;
@@ -3254,6 +3420,24 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                method = nm;
                header = mono_method_get_header (nm);
                mono_os_mutex_unlock(&calc_section);
+       } else if (method->klass == mono_defaults.array_class) {
+               if (!strcmp (method->name, "UnsafeMov")) {
+                       mono_os_mutex_lock (&calc_section);
+                       if (!runtime_method->transformed) {
+                               runtime_method->code = g_malloc (sizeof (short));
+                               runtime_method->code[0] = MINT_CALLRUN;
+                               runtime_method->stack_size = sizeof (stackval); /* for tracing */
+                               runtime_method->alloca_size = runtime_method->stack_size;
+                               runtime_method->transformed = TRUE;
+                       }
+                       mono_os_mutex_unlock(&calc_section);
+                       mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
+                       return NULL;
+               } else if (!strcmp (method->name, "UnsafeStore)")) {
+                       g_error ("TODO");
+               } else if (!strcmp (method->name, "UnsafeLoad)")) {
+                       g_error ("TODO");
+               }
        }
        g_assert ((signature->param_count + signature->hasthis) < 1000);
        g_assert (header->max_stack < 10000);
@@ -3285,7 +3469,7 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
                        break;
                case MonoInlineType:
                        if (method->wrapper_type == MONO_WRAPPER_NONE) {
-                               class = mono_class_get_full (image, read32 (ip + 1), generic_context);
+                               class = mini_get_class (method, read32 (ip + 1), generic_context);
                                mono_class_init (class);
                                /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
 #if 0
@@ -3378,7 +3562,7 @@ mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *cont
        }
 
        runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
-       runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions  + 1 for 0-ing in trace*/
+       runtime_method->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions  + 1 for 0-ing in trace*/
        runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
        offset = 0;
        for (i = 0; i < header->num_locals; ++i) {