[threads] Enable blocking transition with MONO_ENABLE_BLOCKING_TRANSITION env variabl...
[mono.git] / mono / mini / interp / interp.c
index 64c101a30abf76c83ba629403c8a022818f180d8..3a14d6923357789c25eb54c3c7e0d5eb08e93f3c 100644 (file)
 
 #include <mono/mini/mini.h>
 #include <mono/mini/jit-icalls.h>
+#include <mono/mini/debugger-agent.h>
 
+#ifdef TARGET_ARM
+#include <mono/mini/mini-arm.h>
+#endif
 
 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
 #ifdef _WIN32
@@ -101,10 +105,11 @@ init_frame (MonoInvocation *frame, MonoInvocation *parent_frame, RuntimeMethod *
  * Used for testing.
  */
 GSList *jit_classes;
+/* If TRUE, interpreted code will be interrupted at function entry/backward branches */
+static gboolean ss_enabled;
 
 void ves_exec_method (MonoInvocation *frame);
 
-static char* dump_stack (stackval *stack, stackval *sp);
 static char* dump_frame (MonoInvocation *inv);
 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally);
@@ -202,8 +207,10 @@ static void debug_enter (MonoInvocation *frame, int *tracing)
 /* Set the current execution state to the resume state in context */
 #define SET_RESUME_STATE(context) do { \
                ip = (context)->handler_ip;                                             \
+               if (frame->ex) { \
                sp->data.p = frame->ex;                                                                                 \
                ++sp;                                                                                                                   \
+               } \
                frame->ex = NULL;                                                                                               \
                (context)->has_resume_state = 0;                                                                \
                (context)->handler_frame = NULL;                                                                \
@@ -211,51 +218,14 @@ static void debug_enter (MonoInvocation *frame, int *tracing)
        } while (0)
 
 static void
-interp_ex_handler (MonoException *ex) {
-       MonoError error;
-       ThreadContext *context = mono_native_tls_get_value (thread_context_id);
-       char *stack_trace;
-       if (context == NULL)
-               return;
-       stack_trace = dump_frame (context->current_frame);
-       ex->stack_trace = mono_string_new_checked (mono_domain_get(), stack_trace, &error);
-       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-       g_free (stack_trace);
-       if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
-               char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
-               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-               fprintf(stderr, "Nothing can catch this exception: ");
-               fprintf(stderr, "%s", ex->object.vtable->klass->name);
-               if (ex->message != NULL) {
-                       char *m = mono_string_to_utf8_checked (ex->message, &error);
-                       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       fprintf(stderr, ": %s", m);
-                       g_free(m);
-               }
-               fprintf(stderr, "\n%s\n", strace);
-               g_free (strace);
-               if (ex->inner_ex != NULL) {
-                       ex = (MonoException *)ex->inner_ex;
-                       fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
-                       if (ex->message != NULL) {
-                               char *m = mono_string_to_utf8_checked (ex->message, &error);
-                               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                               fprintf(stderr, ": %s", m);
-                               g_free(m);
-                       }
-                       strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
-                       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       fprintf(stderr, "\n");
-                       fprintf(stderr, "%s\n", strace);
-                       g_free (strace);
-               }
-               /* wait for other threads to also collapse */
-               // Sleep(1000); // TODO: proper sleep
-               exit(1);
-       }
-       context->env_frame->ex = ex;
-       context->search_for_handler = 1;
-       longjmp (*context->current_env, 1);
+set_context (ThreadContext *context)
+{
+       MonoJitTlsData *jit_tls;
+
+       mono_native_tls_set_value (thread_context_id, context);
+       jit_tls = mono_tls_get_jit_tls ();
+       if (jit_tls)
+               jit_tls->interp_context = context;
 }
 
 static void
@@ -280,6 +250,19 @@ ves_real_abort (int line, MonoMethod *mh,
                THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
        } while (0);
 
+static RuntimeMethod*
+lookup_runtime_method (MonoDomain *domain, MonoMethod *method)
+{
+       RuntimeMethod *rtm;
+       MonoJitDomainInfo *info;
+
+       info = domain_jit_info (domain);
+       mono_domain_jit_code_hash_lock (domain);
+       rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
+       mono_domain_jit_code_hash_unlock (domain);
+       return rtm;
+}
+
 RuntimeMethod*
 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
 {
@@ -301,6 +284,7 @@ mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoErro
 
        rtm = mono_domain_alloc0 (domain, sizeof (RuntimeMethod));
        rtm->method = method;
+       rtm->domain = domain;
        rtm->param_count = sig->param_count;
        rtm->hasthis = sig->hasthis;
        rtm->rtype = mini_get_underlying_type (sig->ret);
@@ -324,18 +308,48 @@ mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError
        return mono_interp_get_runtime_method (domain, method, error);
 }
 
+/*
+ * interp_push_lmf:
+ *
+ * Push an LMF frame on the LMF stack
+ * to mark the transition to native code.
+ * This is needed for the native code to
+ * be able to do stack walks.
+ */
+static void
+interp_push_lmf (MonoLMFExt *ext, MonoInvocation *frame)
+{
+       memset (ext, 0, sizeof (MonoLMFExt));
+       ext->interp_exit = TRUE;
+       ext->interp_exit_data = frame;
+
+       mono_push_lmf (ext);
+}
+
+static void
+interp_pop_lmf (MonoLMFExt *ext)
+{
+       mono_pop_lmf (&ext->lmf);
+}
+
 static inline RuntimeMethod*
-get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
+get_virtual_method (RuntimeMethod *runtime_method, MonoObject *obj)
 {
        MonoMethod *m = runtime_method->method;
+       MonoDomain *domain = runtime_method->domain;
+       RuntimeMethod *ret = NULL;
        MonoError error;
 
+#ifndef DISABLE_REMOTING
+       if (mono_object_is_transparent_proxy (obj)) {
+               ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
+               mono_error_assert_ok (&error);
+               return ret;
+       }
+#endif
+
        if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
-               RuntimeMethod *ret = NULL;
-               if (mono_object_is_transparent_proxy (obj)) {
-                       ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
-                       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-               } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
+               if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
                        ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
                        mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                } else {
@@ -424,15 +438,19 @@ stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvo
        case MONO_TYPE_U4:
                result->data.i = *(guint32*)data;
                return;
-       case MONO_TYPE_R4:
-               result->data.f = *(float*)data;
+       case MONO_TYPE_R4: {
+               float tmp;
+               /* memmove handles unaligned case */
+               memmove (&tmp, data, sizeof (float));
+               result->data.f = tmp;
                return;
+    }
        case MONO_TYPE_I8:
        case MONO_TYPE_U8:
-               result->data.l = *(gint64*)data;
+               memmove (&result->data.l, data, sizeof (gint64));
                return;
        case MONO_TYPE_R8:
-               result->data.f = *(double*)data;
+               memmove (&result->data.f, data, sizeof (double));
                return;
        case MONO_TYPE_STRING:
        case MONO_TYPE_SZARRAY:
@@ -448,9 +466,14 @@ stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvo
                } else
                        mono_value_copy (result->data.vt, data, type->data.klass);
                return;
-       case MONO_TYPE_GENERICINST:
+       case MONO_TYPE_GENERICINST: {
+               if (mono_type_generic_inst_is_valuetype (type)) {
+                       mono_value_copy (result->data.vt, data, mono_class_from_mono_type (type));
+                       return;
+               }
                stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
                return;
+       }
        default:
                g_warning ("got type 0x%02x", type->type);
                g_assert_not_reached ();
@@ -564,7 +587,7 @@ fill_in_trace (MonoException *exception, MonoInvocation *frame)
 {
        MonoError error;
        char *stack_trace = dump_frame (frame);
-       MonoDomain *domain = mono_domain_get();
+       MonoDomain *domain = frame->runtime_method->domain;
        (exception)->stack_trace = mono_string_new_checked (domain, stack_trace, &error);
        mono_error_cleanup (&error); /* FIXME: don't swallow the error */
        (exception)->trace_ips = get_trace_ips (domain, frame);
@@ -573,14 +596,18 @@ fill_in_trace (MonoException *exception, MonoInvocation *frame)
 
 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
 
-#define THROW_EX(exception,ex_ip)      \
+#define THROW_EX_GENERAL(exception,ex_ip,rethrow)              \
        do {\
                frame->ip = (ex_ip);            \
                frame->ex = (MonoException*)(exception);        \
-               FILL_IN_TRACE(frame->ex, frame); \
+               if (!rethrow) { \
+                       FILL_IN_TRACE(frame->ex, frame);        \
+               } \
                goto handle_exception;  \
        } while (0)
 
+#define THROW_EX(exception,ex_ip) THROW_EX_GENERAL ((exception), (ex_ip), FALSE)
+
 static MonoObject*
 ves_array_create (MonoInvocation *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
 {
@@ -613,7 +640,7 @@ ves_array_create (MonoInvocation *frame, MonoDomain *domain, MonoClass *klass, M
 }
 
 static gint32
-ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame)
+ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame, gboolean safe)
 {
        g_assert (!frame->ex);
        MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
@@ -624,7 +651,7 @@ ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame)
                        guint32 idx = sp [i].data.i;
                        guint32 lower = ao->bounds [i].lower_bound;
                        guint32 len = ao->bounds [i].length;
-                       if (idx < lower || (idx - lower) >= len) {
+                       if (safe && (idx < lower || (idx - lower) >= len)) {
                                frame->ex = mono_get_exception_index_out_of_range ();
                                FILL_IN_TRACE (frame->ex, frame);
                                return -1;
@@ -633,7 +660,7 @@ ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame)
                }
        } else {
                pos = sp [0].data.i;
-               if (pos >= ao->max_length) {
+               if (safe && pos >= ao->max_length) {
                        frame->ex = mono_get_exception_index_out_of_range ();
                        FILL_IN_TRACE (frame->ex, frame);
                        return -1;
@@ -653,7 +680,7 @@ ves_array_set (MonoInvocation *frame)
 
        g_assert (ac->rank >= 1);
 
-       gint32 pos = ves_array_calculate_index (ao, sp, frame);
+       gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
        if (frame->ex)
                return;
 
@@ -676,7 +703,7 @@ ves_array_set (MonoInvocation *frame)
 }
 
 static void
-ves_array_get (MonoInvocation *frame)
+ves_array_get (MonoInvocation *frame, gboolean safe)
 {
        stackval *sp = frame->stack_args + 1;
 
@@ -686,7 +713,7 @@ ves_array_get (MonoInvocation *frame)
 
        g_assert (ac->rank >= 1);
 
-       gint32 pos = ves_array_calculate_index (ao, sp, frame);
+       gint32 pos = ves_array_calculate_index (ao, sp, frame, safe);
        if (frame->ex)
                return;
 
@@ -704,7 +731,7 @@ ves_array_element_address (MonoInvocation *frame, MonoClass *required_type, Mono
 
        g_assert (ac->rank >= 1);
 
-       gint32 pos = ves_array_calculate_index (ao, sp, frame);
+       gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
        if (frame->ex)
                return NULL;
 
@@ -758,22 +785,14 @@ interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUn
 
 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
 
-struct _MethodArguments {
-       size_t ilen;
-       gpointer *iargs;
-       size_t flen;
-       double *fargs;
-       gpointer *retval;
-       size_t is_float_ret;
-};
-
-typedef struct _MethodArguments MethodArguments;
-
-// TODO: this function is also arch dependent (register width).
-static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
+static InterpMethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
 {
-       // TODO: don't malloc this data structure.
-       MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
+       InterpMethodArguments *margs = g_malloc0 (sizeof (InterpMethodArguments));
+
+#ifdef TARGET_ARM
+       g_assert (mono_arm_eabi_supported ());
+       int i8_align = mono_arm_i8_align ();
+#endif
 
        if (sig->hasthis)
                margs->ilen++;
@@ -796,15 +815,34 @@ static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvoc
                case MONO_TYPE_CLASS:
                case MONO_TYPE_OBJECT:
                case MONO_TYPE_STRING:
-               case MONO_TYPE_I8:
                case MONO_TYPE_VALUETYPE:
                case MONO_TYPE_GENERICINST:
+#if SIZEOF_VOID_P == 8
+               case MONO_TYPE_I8:
+#endif
                        margs->ilen++;
                        break;
+#if SIZEOF_VOID_P == 4
+               case MONO_TYPE_I8:
+#ifdef TARGET_ARM
+                       /* pairs begin at even registers */
+                       if (i8_align == 8 && margs->ilen & 1)
+                               margs->ilen++;
+#endif
+                       margs->ilen += 2;
+                       break;
+#endif
                case MONO_TYPE_R4:
+#if SIZEOF_VOID_P == 8
                case MONO_TYPE_R8:
+#endif
                        margs->flen++;
                        break;
+#if SIZEOF_VOID_P == 4
+               case MONO_TYPE_R8:
+                       margs->flen += 2;
+                       break;
+#endif
                default:
                        g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
                }
@@ -816,10 +854,10 @@ static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvoc
        if (margs->flen > 0)
                margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
 
-       if (margs->ilen > 12)
+       if (margs->ilen > INTERP_ICALL_TRAMP_IARGS)
                g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
 
-       if (margs->flen > 3)
+       if (margs->flen > INTERP_ICALL_TRAMP_FARGS)
                g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
 
 
@@ -849,15 +887,35 @@ static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvoc
                case MONO_TYPE_CLASS:
                case MONO_TYPE_OBJECT:
                case MONO_TYPE_STRING:
-               case MONO_TYPE_I8:
                case MONO_TYPE_VALUETYPE:
                case MONO_TYPE_GENERICINST:
+#if SIZEOF_VOID_P == 8
+               case MONO_TYPE_I8:
+#endif
                        margs->iargs [int_i] = frame->stack_args [i].data.p;
 #if DEBUG_INTERP
                        g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
 #endif
                        int_i++;
                        break;
+#if SIZEOF_VOID_P == 4
+               case MONO_TYPE_I8: {
+                       stackval *sarg = &frame->stack_args [i];
+#ifdef TARGET_ARM
+                       /* pairs begin at even registers */
+                       if (i8_align == 8 && int_i & 1)
+                               int_i++;
+#endif
+                       margs->iargs [int_i] = (gpointer) sarg->data.pair.lo;
+                       int_i++;
+                       margs->iargs [int_i] = (gpointer) sarg->data.pair.hi;
+#if DEBUG_INTERP
+                       g_print ("build_args_from_sig: margs->iargs [%d/%d]: 0x%016llx, hi=0x%08x lo=0x%08x (frame @ %d)\n", int_i - 1, int_i, *((guint64 *) &margs->iargs [int_i - 1]), sarg->data.pair.hi, sarg->data.pair.lo, i);
+#endif
+                       int_i++;
+                       break;
+               }
+#endif
                case MONO_TYPE_R4:
                case MONO_TYPE_R8:
                        if (ptype == MONO_TYPE_R4)
@@ -867,7 +925,11 @@ static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvoc
 #if DEBUG_INTERP
                        g_print ("build_args_from_sig: margs->fargs [%d]: %p (%f) (frame @ %d)\n", int_f, margs->fargs [int_f], margs->fargs [int_f], i);
 #endif
+#if SIZEOF_VOID_P == 4
+                       int_f += 2;
+#else
                        int_f++;
+#endif
                        break;
                default:
                        g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
@@ -894,6 +956,7 @@ static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvoc
                case MONO_TYPE_VALUETYPE:
                case MONO_TYPE_GENERICINST:
                        margs->retval = &(frame->retval->data.p);
+                       margs->is_float_ret = 0;
                        break;
                case MONO_TYPE_R4:
                case MONO_TYPE_R8:
@@ -933,13 +996,17 @@ ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV a
 
        g_assert (!frame->runtime_method);
        if (!mono_interp_enter_icall_trampoline) {
-               MonoTrampInfo *info;
-               mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
-               // TODO:
-               // mono_tramp_info_register (info, NULL);
+               if (mono_aot_only) {
+                       mono_interp_enter_icall_trampoline = mono_aot_get_trampoline ("enter_icall_trampoline");
+               } else {
+                       MonoTrampInfo *info;
+                       mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
+                       // TODO:
+                       // mono_tramp_info_register (info, NULL);
+               }
        }
 
-       MethodArguments *margs = build_args_from_sig (sig, frame);
+       InterpMethodArguments *margs = build_args_from_sig (sig, frame);
 #if DEBUG_INTERP
        g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
        g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
@@ -948,23 +1015,13 @@ ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV a
        context->current_frame = frame;
        context->managed_code = 0;
 
-       /*
-        * Push an LMF frame on the LMF stack
-        * to mark the transition to native code.
-        */
-       memset (&ext, 0, sizeof (ext));
-       ext.interp_exit = TRUE;
-       ext.interp_exit_data = frame;
-
-       mono_push_lmf (&ext);
+       interp_push_lmf (&ext, frame);
 
        mono_interp_enter_icall_trampoline (addr, margs);
 
-       mono_pop_lmf (&ext.lmf);
+       interp_pop_lmf (&ext);
 
        context->managed_code = 1;
-       /* domain can only be changed by native code */
-       context->domain = mono_domain_get ();
 
        if (*mono_thread_interruption_request_flag ()) {
                MonoException *exc = mono_thread_interruption_checkpoint ();
@@ -1018,6 +1075,10 @@ ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
                        stackval_from_data (mt, frame->retval, (char *) frame->stack_args, FALSE);
                        return;
                }
+               if (!strcmp (method->name, "UnsafeLoad")) {
+                       ves_array_get (frame, FALSE);
+                       return;
+               }
        }
 
        isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
@@ -1028,7 +1089,7 @@ ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
                        return;
                }
                if (*name == 'G' && (strcmp (name, "Get") == 0)) {
-                       ves_array_get (frame);
+                       ves_array_get (frame, TRUE);
                        return;
                }
        }
@@ -1038,6 +1099,7 @@ ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
                        method->name);
 }
 
+#if DEBUG_INTERP
 static char*
 dump_stack (stackval *stack, stackval *sp)
 {
@@ -1053,6 +1115,7 @@ dump_stack (stackval *stack, stackval *sp)
        }
        return g_string_free (str, FALSE);
 }
+#endif
 
 static void
 dump_stackval (GString *str, stackval *s, MonoType *type)
@@ -1100,6 +1163,20 @@ dump_stackval (GString *str, stackval *s, MonoType *type)
        }
 }
 
+#if DEBUG_INTERP
+static char*
+dump_retval (MonoInvocation *inv)
+{
+       GString *str = g_string_new ("");
+       MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
+
+       if (ret->type != MONO_TYPE_VOID)
+               dump_stackval (str, inv->retval, ret);
+
+       return g_string_free (str, FALSE);
+}
+#endif
+
 static char*
 dump_args (MonoInvocation *inv)
 {
@@ -1120,18 +1197,6 @@ dump_args (MonoInvocation *inv)
 
        return g_string_free (str, FALSE);
 }
-
-static char*
-dump_retval (MonoInvocation *inv)
-{
-       GString *str = g_string_new ("");
-       MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
-
-       if (ret->type != MONO_TYPE_VOID)
-               dump_stackval (str, inv->retval, ret);
-
-       return g_string_free (str, FALSE);
-}
  
 static char*
 dump_frame (MonoInvocation *inv)
@@ -1301,10 +1366,8 @@ mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoOb
        MonoObject *retval = NULL;
        MonoMethodSignature *sig = mono_method_signature (method);
        MonoClass *klass = mono_class_from_mono_type (sig->ret);
-       int i, type, isobject = 0;
-       void *ret = NULL;
        stackval result;
-       stackval *args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
+       stackval *args;
        ThreadContext context_struct;
        MonoInvocation *old_frame = NULL;
        jmp_buf env;
@@ -1317,11 +1380,10 @@ mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoOb
 
        if (setjmp(env)) {
                if (context != &context_struct) {
-                       context->domain = mono_domain_get ();
                        context->current_frame = old_frame;
                        context->managed_code = 0;
-               } else 
-                       mono_native_tls_set_value (thread_context_id, NULL);
+               } else
+                       set_context (NULL);
                if (exc != NULL)
                        *exc = (MonoObject *)frame.ex;
                return retval;
@@ -1333,130 +1395,37 @@ mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoOb
                context_struct.base_frame = &frame;
                context_struct.env_frame = &frame;
                context_struct.current_env = &env;
-               mono_native_tls_set_value (thread_context_id, context);
+               set_context (context);
        }
        else
                old_frame = context->current_frame;
 
-       context->domain = mono_domain_get ();
+       MonoDomain *domain = mono_domain_get ();
 
-       switch (sig->ret->type) {
-       case MONO_TYPE_VOID:
-               break;
-       case MONO_TYPE_STRING:
-       case MONO_TYPE_OBJECT:
-       case MONO_TYPE_CLASS:
-       case MONO_TYPE_ARRAY:
-       case MONO_TYPE_SZARRAY:
-               isobject = 1;
-               break;
-       case MONO_TYPE_VALUETYPE:
-               retval = mono_object_new_checked (context->domain, klass, error);
-               ret = mono_object_unbox (retval);
-               if (!sig->ret->data.klass->enumtype)
-                       result.data.vt = ret;
-               else
-                       result.data.vt = alloca (mono_class_instance_size (klass));
-               break;
-       case MONO_TYPE_GENERICINST:
-               if (!MONO_TYPE_IS_REFERENCE (sig->ret)) {
-                       retval = mono_object_new_checked (context->domain, klass, error);
-                       ret = mono_object_unbox (retval);
-                       if (!sig->ret->data.klass->enumtype)
-                               result.data.vt = ret;
-                       else
-                               result.data.vt = alloca (mono_class_instance_size (klass));
-               } else {
-                       isobject = 1;
-               }
-               break;
+       MonoMethod *invoke_wrapper = mono_marshal_get_runtime_invoke_full (method, FALSE, TRUE);
 
-       case MONO_TYPE_PTR:
-               retval = mono_object_new_checked (context->domain, mono_defaults.int_class, error);
-               ret = mono_object_unbox (retval);
-               break;
-       default:
-               retval = mono_object_new_checked (context->domain, klass, error);
-               ret = mono_object_unbox (retval);
-               break;
-       }
+       //* <code>MonoObject *runtime_invoke (MonoObject *this_obj, void **params, MonoObject **exc, void* method)</code>
+
+       result.data.vt = alloca (mono_class_instance_size (klass));
+       args = alloca (sizeof (stackval) * 4);
 
        if (sig->hasthis)
                args [0].data.p = obj;
+       else
+               args [0].data.p = NULL;
+       args [1].data.p = params;
+       args [2].data.p = exc;
+       args [3].data.p = method;
 
-       for (i = 0; i < sig->param_count; ++i) {
-               int a_index = i + !!sig->hasthis;
-               if (sig->params [i]->byref) {
-                       args [a_index].data.p = params [i];
-                       continue;
-               }
-               type = sig->params [i]->type;
-handle_enum:
-               switch (type) {
-               case MONO_TYPE_U1:
-               case MONO_TYPE_I1:
-               case MONO_TYPE_BOOLEAN:
-                       args [a_index].data.i = *(MonoBoolean*)params [i];
-                       break;
-               case MONO_TYPE_U2:
-               case MONO_TYPE_I2:
-               case MONO_TYPE_CHAR:
-                       args [a_index].data.i = *(gint16*)params [i];
-                       break;
-#if SIZEOF_VOID_P == 4
-               case MONO_TYPE_U: /* use VAL_POINTER? */
-               case MONO_TYPE_I:
-#endif
-               case MONO_TYPE_U4:
-               case MONO_TYPE_I4:
-                       args [a_index].data.i = *(gint32*)params [i];
-                       break;
-#if SIZEOF_VOID_P == 8
-               case MONO_TYPE_U:
-               case MONO_TYPE_I:
-#endif
-               case MONO_TYPE_U8:
-               case MONO_TYPE_I8:
-                       args [a_index].data.l = *(gint64*)params [i];
-                       break;
-               case MONO_TYPE_R4:
-                       args [a_index].data.f = *(gfloat *) params [i];
-                       break;
-               case MONO_TYPE_R8:
-                       args [a_index].data.f = *(gdouble *) params [i];
-                       break;
-               case MONO_TYPE_VALUETYPE:
-                       if (sig->params [i]->data.klass->enumtype) {
-                               type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
-                               goto handle_enum;
-                       } else {
-                               args [a_index].data.p = params [i];
-                       }
-                       break;
-               case MONO_TYPE_STRING:
-               case MONO_TYPE_PTR:
-               case MONO_TYPE_CLASS:
-               case MONO_TYPE_ARRAY:
-               case MONO_TYPE_SZARRAY:
-               case MONO_TYPE_OBJECT:
-               case MONO_TYPE_GENERICINST:
-                       args [a_index].data.p = params [i];
-                       break;
-               default:
-                       g_error ("type 0x%x not handled in  runtime invoke", sig->params [i]->type);
-               }
-       }
+       INIT_FRAME (&frame, context->current_frame, args, &result, domain, invoke_wrapper, error);
 
-       if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
-               method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
-       INIT_FRAME (&frame,context->current_frame,args,&result,mono_get_root_domain (),method,error);
        if (exc)
                frame.invoke_trap = 1;
        context->managed_code = 1;
        ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
        context->managed_code = 0;
        if (context == &context_struct)
-               mono_native_tls_set_value (thread_context_id, NULL);
+               set_context (NULL);
        else
                context->current_frame = old_frame;
        if (frame.ex != NULL) {
@@ -1471,12 +1440,7 @@ handle_enum:
                else
                        printf("dropped exception...\n");
        }
-       if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
-               return NULL;
-       if (isobject || method->string_ctor)
-               return result.data.p;
-       stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
-       return retval;
+       return result.data.p;
 }
 
 typedef struct {
@@ -1516,11 +1480,10 @@ interp_entry (InterpEntryData *data)
                memset (context, 0, sizeof (ThreadContext));
                context_struct.base_frame = &frame;
                context_struct.env_frame = &frame;
-               mono_native_tls_set_value (thread_context_id, context);
-       }
-       else
+               set_context (context);
+       } else {
                old_frame = context->current_frame;
-       context->domain = mono_domain_get ();
+       }
 
        args = alloca (sizeof (stackval) * (sig->param_count + (sig->hasthis ? 1 : 0)));
        if (sig->hasthis)
@@ -1612,7 +1575,7 @@ interp_entry (InterpEntryData *data)
        ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
        context->managed_code = 0;
        if (context == &context_struct)
-               mono_native_tls_set_value (thread_context_id, NULL);
+               set_context (NULL);
        else
                context->current_frame = old_frame;
 
@@ -1704,12 +1667,12 @@ do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
 
        switch (op) {
        case MINT_ICALL_V_V: {
-               void (*func)() = ptr;
+               void (*func)(void) = ptr;
                func ();
                break;
        }
        case MINT_ICALL_V_P: {
-               gpointer (*func)() = ptr;
+               gpointer (*func)(void) = ptr;
                sp++;
                sp [-1].data.p = func ();
                break;
@@ -1771,6 +1734,240 @@ do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
        return sp;
 }
 
+static stackval *
+do_jit_call (stackval *sp, unsigned char *vt_sp, ThreadContext *context, MonoInvocation *frame, RuntimeMethod *rmethod)
+{
+       MonoMethodSignature *sig;
+       MonoFtnDesc ftndesc;
+       guint8 res_buf [256];
+       MonoType *type;
+       MonoLMFExt ext;
+
+       //printf ("%s\n", mono_method_full_name (rmethod->method, 1));
+
+       /*
+        * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
+        * by ref and return a return value using an explicit return value argument.
+        */
+       if (!rmethod->jit_wrapper) {
+               MonoMethod *method = rmethod->method;
+               MonoError error;
+
+               sig = mono_method_signature (method);
+               g_assert (sig);
+
+               MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
+               //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
+
+               gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, &error);
+               mono_error_assert_ok (&error);
+
+               gpointer addr = mono_jit_compile_method_jit_only (method, &error);
+               g_assert (addr);
+               mono_error_assert_ok (&error);
+
+               rmethod->jit_addr = addr;
+               rmethod->jit_sig = sig;
+               mono_memory_barrier ();
+               rmethod->jit_wrapper = jit_wrapper;
+
+       } else {
+               sig = rmethod->jit_sig;
+       }
+
+       sp -= sig->param_count;
+       if (sig->hasthis)
+               --sp;
+
+       ftndesc.addr = rmethod->jit_addr;
+       ftndesc.arg = NULL;
+
+       // FIXME: Optimize this
+
+       gpointer args [32];
+       int pindex = 0;
+       int stack_index = 0;
+       if (rmethod->hasthis) {
+               args [pindex ++] = sp [0].data.p;
+               stack_index ++;
+       }
+       type = rmethod->rtype;
+       if (type->type != MONO_TYPE_VOID) {
+               if (MONO_TYPE_ISSTRUCT (type))
+                       args [pindex ++] = vt_sp;
+               else
+                       args [pindex ++] = res_buf;
+       }
+       for (int i = 0; i < rmethod->param_count; ++i) {
+               MonoType *t = rmethod->param_types [i];
+               stackval *sval = &sp [stack_index + i];
+               if (sig->params [i]->byref) {
+                       args [pindex ++] = sval->data.p;
+               } else if (MONO_TYPE_ISSTRUCT (t)) {
+                       args [pindex ++] = sval->data.p;
+               } else if (MONO_TYPE_IS_REFERENCE (t)) {
+                       args [pindex ++] = &sval->data.p;
+               } else {
+                       switch (t->type) {
+                       case MONO_TYPE_I1:
+                       case MONO_TYPE_U1:
+                       case MONO_TYPE_I2:
+                       case MONO_TYPE_U2:
+                       case MONO_TYPE_I4:
+                       case MONO_TYPE_U4:
+                       case MONO_TYPE_VALUETYPE:
+                               args [pindex ++] = &sval->data.i;
+                               break;
+                       case MONO_TYPE_PTR:
+                       case MONO_TYPE_FNPTR:
+                       case MONO_TYPE_I:
+                       case MONO_TYPE_U:
+                       case MONO_TYPE_OBJECT:
+                               args [pindex ++] = &sval->data.p;
+                               break;
+                       case MONO_TYPE_I8:
+                       case MONO_TYPE_U8:
+                               args [pindex ++] = &sval->data.l;
+                               break;
+                       default:
+                               printf ("%s\n", mono_type_full_name (t));
+                               g_assert_not_reached ();
+                       }
+               }
+       }
+
+       interp_push_lmf (&ext, frame);
+
+       switch (pindex) {
+       case 0: {
+               void (*func)(gpointer) = rmethod->jit_wrapper;
+
+               func (&ftndesc);
+               break;
+       }
+       case 1: {
+               void (*func)(gpointer, gpointer) = rmethod->jit_wrapper;
+
+               func (args [0], &ftndesc);
+               break;
+       }
+       case 2: {
+               void (*func)(gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+
+               func (args [0], args [1], &ftndesc);
+               break;
+       }
+       case 3: {
+               void (*func)(gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+
+               func (args [0], args [1], args [2], &ftndesc);
+               break;
+       }
+       case 4: {
+               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+
+               func (args [0], args [1], args [2], args [3], &ftndesc);
+               break;
+       }
+       case 5: {
+               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+
+               func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
+               break;
+       }
+       case 6: {
+               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+
+               func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
+               break;
+       }
+       case 7: {
+               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+
+               func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
+               break;
+       }
+       default:
+               g_assert_not_reached ();
+               break;
+       }
+
+       interp_pop_lmf (&ext);
+
+       MonoType *rtype = rmethod->rtype;
+       switch (rtype->type) {
+       case MONO_TYPE_VOID:
+       case MONO_TYPE_OBJECT:
+       case MONO_TYPE_STRING:
+       case MONO_TYPE_CLASS:
+       case MONO_TYPE_ARRAY:
+       case MONO_TYPE_SZARRAY:
+       case MONO_TYPE_I:
+       case MONO_TYPE_U:
+               sp->data.p = *(gpointer*)res_buf;
+               break;
+       case MONO_TYPE_I1:
+               sp->data.i = *(gint8*)res_buf;
+               break;
+       case MONO_TYPE_U1:
+               sp->data.i = *(guint8*)res_buf;
+               break;
+       case MONO_TYPE_I2:
+               sp->data.i = *(gint16*)res_buf;
+               break;
+       case MONO_TYPE_U2:
+               sp->data.i = *(guint16*)res_buf;
+               break;
+       case MONO_TYPE_I4:
+               sp->data.i = *(gint32*)res_buf;
+               break;
+       case MONO_TYPE_U4:
+               sp->data.i = *(guint32*)res_buf;
+               break;
+       case MONO_TYPE_VALUETYPE:
+               /* The result was written to vt_sp */
+               sp->data.p = vt_sp;
+               break;
+       case MONO_TYPE_GENERICINST:
+               if (MONO_TYPE_IS_REFERENCE (rtype)) {
+                       sp->data.p = *(gpointer*)res_buf;
+               } else {
+                       /* The result was written to vt_sp */
+                       sp->data.p = vt_sp;
+               }
+               break;
+       default:
+               printf ("%s\n", mono_type_full_name (rtype));
+               g_assert_not_reached ();
+               break;
+       }
+
+       return sp;
+}
+
+static void
+do_debugger_tramp (void (*tramp) (void), MonoInvocation *frame)
+{
+       MonoLMFExt ext;
+       interp_push_lmf (&ext, frame);
+       tramp ();
+       interp_pop_lmf (&ext);
+}
+
+static void
+do_transform_method (MonoInvocation *frame, ThreadContext *context)
+{
+       MonoLMFExt ext;
+
+       /* Use the parent frame as the current frame is not complete yet */
+       interp_push_lmf (&ext, frame->parent);
+
+       frame->ex = mono_interp_transform_method (frame->runtime_method, context);
+       context->managed_code = 1;
+
+       interp_pop_lmf (&ext);
+}
+
 /*
  * These functions are the entry points into the interpreter from compiled code.
  * They are called by the interp_in wrappers. They have the following signature:
@@ -1938,13 +2135,12 @@ mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
        gpointer addr;
        MonoMethodSignature *sig = mono_method_signature (method);
        MonoMethod *wrapper;
-       RuntimeMethod *rmethod;
+       RuntimeMethod *rmethod = mono_interp_get_runtime_method (mono_domain_get (), method, error);
 
        /* HACK: method_ptr of delegate should point to a runtime method*/
        if (method->wrapper_type && method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
-               return mono_interp_get_runtime_method (mono_domain_get (), method, error);
+               return rmethod;
 
-       rmethod = mono_interp_get_runtime_method (mono_domain_get (), method, error);
        if (rmethod->jit_entry)
                return rmethod->jit_entry;
        wrapper = mini_get_interp_in_wrapper (sig);
@@ -1980,9 +2176,10 @@ mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
         * rgctx register using a trampoline.
         */
 
-       // FIXME: AOT
-       g_assert (!mono_aot_only);
-       addr = mono_arch_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
+       if (mono_aot_only)
+               addr = mono_aot_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
+       else
+               addr = mono_arch_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
 
        mono_memory_barrier ();
        rmethod->jit_entry = addr;
@@ -2054,6 +2251,8 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
 #if DEBUG_INTERP
        gint tracing = global_tracing;
        unsigned char *vtalloc;
+#else
+       gint tracing = 0;
 #endif
        int i32;
        unsigned char *vt_sp;
@@ -2074,9 +2273,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
        frame->ip = NULL;
        context->current_frame = frame;
 
-#if DEBUG_INTERP
        debug_enter (frame, &tracing);
-#endif
 
        if (!frame->runtime_method->transformed) {
                context->managed_code = 0;
@@ -2085,8 +2282,8 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
                g_free (mn);
 #endif
-               frame->ex = mono_interp_transform_method (frame->runtime_method, context);
-               context->managed_code = 1;
+
+               do_transform_method (frame, context);
                if (frame->ex) {
                        rtm = NULL;
                        ip = NULL;
@@ -2109,6 +2306,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
        vtalloc = vt_sp;
 #endif
        locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
+       frame->locals = locals;
        child_frame.parent = frame;
 
        if (filter_exception) {
@@ -2135,7 +2333,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_BREAK)
                        ++ip;
-                       G_BREAKPOINT (); /* this is not portable... */
+                       do_debugger_tramp (mono_debugger_agent_user_break, frame);
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_LDNULL) 
                        sp->data.p = NULL;
@@ -2254,6 +2452,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        vtalloc = vt_sp;
 #endif
                        locals = vt_sp + rtm->vt_stack_size;
+                       frame->locals = locals;
                        ip = rtm->new_body_start; /* bypass storing input args from callers frame */
                        MINT_IN_BREAK;
                }
@@ -2277,12 +2476,15 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                                --sp;
                        child_frame.stack_args = sp;
 
+#ifndef DISABLE_REMOTING
                        /* `this' can be NULL for string:.ctor */
                        if (csignature->hasthis && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
-                               child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
+                               child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
-                               child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
+                       } else
+#endif
+                       if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
+                               child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                        }
 
@@ -2387,58 +2589,13 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                                --sp;
                        child_frame.stack_args = sp;
 
+#ifndef DISABLE_REMOTING
                        /* `this' can be NULL for string:.ctor */
                        if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
-                               child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
-                               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       }
-
-                       ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
-
-                       context->current_frame = frame;
-
-                       if (context->has_resume_state) {
-                               if (frame == context->handler_frame)
-                                       SET_RESUME_STATE (context);
-                               else
-                                       goto exit_frame;
-                       }
-
-                       if (child_frame.ex) {
-                               /*
-                                * An exception occurred, need to run finally, fault and catch handlers..
-                                */
-                               frame->ex = child_frame.ex;
-                               goto handle_exception;;
-                       }
-
-                       /* need to handle typedbyref ... */
-                       *sp = *endsp;
-                       sp++;
-                       MINT_IN_BREAK;
-               }
-               MINT_IN_CASE(MINT_VCALL) {
-                       frame->ip = ip;
-                       
-                       child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
-                       ip += 2;
-
-                       sp->data.p = vt_sp;
-                       child_frame.retval = sp;
-                       /* decrement by the actual number of args */
-                       sp -= child_frame.runtime_method->param_count;
-                       if (child_frame.runtime_method->hasthis) {
-                               --sp;
-                               MonoObject *this_arg = sp->data.p;
-                               if (!this_arg)
-                                       THROW_EX (mono_get_exception_null_reference(), ip - 2);
-                       }
-                       child_frame.stack_args = sp;
-
-                       if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
-                               child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
+                               child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                        }
+#endif
 
                        ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
 
@@ -2456,179 +2613,65 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                                 * An exception occurred, need to run finally, fault and catch handlers..
                                 */
                                frame->ex = child_frame.ex;
-                               goto handle_finally;
-                       }
-                       MINT_IN_BREAK;
-               }
-
-               MINT_IN_CASE(MINT_JIT_CALL) {
-                       MonoMethodSignature *sig;
-                       RuntimeMethod *rmethod = rtm->data_items [* (guint16 *)(ip + 1)];
-                       MonoFtnDesc ftndesc;
-                       guint8 res_buf [256];
-                       MonoType *type;
-                       MonoLMFExt ext;
-
-                       //printf ("%s\n", mono_method_full_name (rmethod->method, 1));
-
-                       /*
-                        * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
-                        * by ref and return a return value using an explicit return value argument.
-                        */
-                       if (!rmethod->jit_wrapper) {
-                               MonoMethod *method = rmethod->method;
-                               MonoError error;
-
-                               sig = mono_method_signature (method);
-                               g_assert (sig);
-
-                               MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
-                               //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
-
-                               gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, &error);
-                               mono_error_assert_ok (&error);
-
-                               gpointer addr = mono_jit_compile_method_jit_only (method, &error);
-                               g_assert (addr);
-                               mono_error_assert_ok (&error);
-
-                               rmethod->jit_addr = addr;
-                               rmethod->jit_sig = sig;
-                               mono_memory_barrier ();
-                               rmethod->jit_wrapper = jit_wrapper;
-
-                       } else {
-                               sig = rmethod->jit_sig;
-                       }
-
-                       frame->ip = ip;
-                       ip += 2;
-                       sp -= sig->param_count;
-                       if (sig->hasthis)
-                               --sp;
-
-                       ftndesc.addr = rmethod->jit_addr;
-                       ftndesc.arg = NULL;
-
-                       // FIXME: Optimize this
-
-                       gpointer args [32];
-                       int pindex = 0;
-                       int stack_index = 0;
-                       if (rmethod->hasthis) {
-                               args [pindex ++] = sp [0].data.p;
-                               stack_index ++;
-                       }
-                       type = rmethod->rtype;
-                       if (type->type != MONO_TYPE_VOID) {
-                               if (MONO_TYPE_ISSTRUCT (type))
-                                       args [pindex ++] = vt_sp;
-                               else
-                                       args [pindex ++] = res_buf;
-                       }
-                       for (int i = 0; i < rmethod->param_count; ++i) {
-                               MonoType *t = rmethod->param_types [i];
-                               stackval *sval = &sp [stack_index + i];
-                               if (sig->params [i]->byref) {
-                                       args [pindex ++] = sval->data.p;
-                               } else if (MONO_TYPE_ISSTRUCT (t)) {
-                                       args [pindex ++] = sval->data.p;
-                               } else if (MONO_TYPE_IS_REFERENCE (t)) {
-                                       args [pindex ++] = &sval->data.p;
-                               } else {
-                                       switch (t->type) {
-                                       case MONO_TYPE_I1:
-                                       case MONO_TYPE_U1:
-                                       case MONO_TYPE_I2:
-                                       case MONO_TYPE_U2:
-                                       case MONO_TYPE_I4:
-                                       case MONO_TYPE_U4:
-                                       case MONO_TYPE_VALUETYPE:
-                                               args [pindex ++] = &sval->data.i;
-                                               break;
-                                       case MONO_TYPE_PTR:
-                                       case MONO_TYPE_FNPTR:
-                                       case MONO_TYPE_I:
-                                       case MONO_TYPE_U:
-                                       case MONO_TYPE_OBJECT:
-                                               args [pindex ++] = &sval->data.p;
-                                               break;
-                                       case MONO_TYPE_I8:
-                                       case MONO_TYPE_U8:
-                                               args [pindex ++] = &sval->data.l;
-                                               break;
-                                       default:
-                                               printf ("%s\n", mono_type_full_name (t));
-                                               g_assert_not_reached ();
-                                       }
-                               }
-                       }
-
-                       /*
-                        * Push an LMF frame on the LMF stack
-                        * to mark the transition to compiled code.
-                        */
-                       memset (&ext, 0, sizeof (ext));
-                       ext.interp_exit = TRUE;
-                       ext.interp_exit_data = frame;
-
-                       mono_push_lmf (&ext);
-
-                       switch (pindex) {
-                       case 0: {
-                               void (*func)(gpointer) = rmethod->jit_wrapper;
-
-                               func (&ftndesc);
-                               break;
+                               goto handle_exception;;
                        }
-                       case 1: {
-                               void (*func)(gpointer, gpointer) = rmethod->jit_wrapper;
 
-                               func (args [0], &ftndesc);
-                               break;
-                       }
-                       case 2: {
-                               void (*func)(gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+                       /* need to handle typedbyref ... */
+                       *sp = *endsp;
+                       sp++;
+                       MINT_IN_BREAK;
+               }
+               MINT_IN_CASE(MINT_VCALL) {
+                       frame->ip = ip;
+                       
+                       child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
+                       ip += 2;
 
-                               func (args [0], args [1], &ftndesc);
-                               break;
+                       sp->data.p = vt_sp;
+                       child_frame.retval = sp;
+                       /* decrement by the actual number of args */
+                       sp -= child_frame.runtime_method->param_count;
+                       if (child_frame.runtime_method->hasthis) {
+                               --sp;
+                               MonoObject *this_arg = sp->data.p;
+                               if (!this_arg)
+                                       THROW_EX (mono_get_exception_null_reference(), ip - 2);
                        }
-                       case 3: {
-                               void (*func)(gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+                       child_frame.stack_args = sp;
 
-                               func (args [0], args [1], args [2], &ftndesc);
-                               break;
+#ifndef DISABLE_REMOTING
+                       if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
+                               child_frame.runtime_method = mono_interp_get_runtime_method (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
+                               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                        }
-                       case 4: {
-                               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+#endif
 
-                               func (args [0], args [1], args [2], args [3], &ftndesc);
-                               break;
-                       }
-                       case 5: {
-                               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+                       ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
 
-                               func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
-                               break;
-                       }
-                       case 6: {
-                               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
+                       context->current_frame = frame;
 
-                               func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
-                               break;
+                       if (context->has_resume_state) {
+                               if (frame == context->handler_frame)
+                                       SET_RESUME_STATE (context);
+                               else
+                                       goto exit_frame;
                        }
-                       case 7: {
-                               void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
 
-                               func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
-                               break;
-                       }
-                       default:
-                               g_assert_not_reached ();
-                               break;
+                       if (child_frame.ex) {
+                               /*
+                                * An exception occurred, need to run finally, fault and catch handlers..
+                                */
+                               frame->ex = child_frame.ex;
+                               goto handle_finally;
                        }
+                       MINT_IN_BREAK;
+               }
 
-                       mono_pop_lmf (&ext.lmf);
+               MINT_IN_CASE(MINT_JIT_CALL) {
+                       RuntimeMethod *rmethod = rtm->data_items [* (guint16 *)(ip + 1)];
+                       frame->ip = ip;
+                       ip += 2;
+                       sp = do_jit_call (sp, vt_sp, context, frame, rmethod);
 
                        if (context->has_resume_state) {
                                /*
@@ -2642,56 +2685,9 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                                else
                                        goto exit_frame;
                        }
-
-                       MonoType *rtype = rmethod->rtype;
-                       switch (rtype->type) {
-                       case MONO_TYPE_VOID:
-                       case MONO_TYPE_OBJECT:
-                       case MONO_TYPE_STRING:
-                       case MONO_TYPE_CLASS:
-                       case MONO_TYPE_ARRAY:
-                       case MONO_TYPE_SZARRAY:
-                       case MONO_TYPE_I:
-                       case MONO_TYPE_U:
-                               sp->data.p = *(gpointer*)res_buf;
-                               break;
-                       case MONO_TYPE_I1:
-                               sp->data.i = *(gint8*)res_buf;
-                               break;
-                       case MONO_TYPE_U1:
-                               sp->data.i = *(guint8*)res_buf;
-                               break;
-                       case MONO_TYPE_I2:
-                               sp->data.i = *(gint16*)res_buf;
-                               break;
-                       case MONO_TYPE_U2:
-                               sp->data.i = *(guint16*)res_buf;
-                               break;
-                       case MONO_TYPE_I4:
-                               sp->data.i = *(gint32*)res_buf;
-                               break;
-                       case MONO_TYPE_U4:
-                               sp->data.i = *(guint32*)res_buf;
-                               break;
-                       case MONO_TYPE_VALUETYPE:
-                               /* The result was written to vt_sp */
-                               sp->data.p = vt_sp;
-                               break;
-                       case MONO_TYPE_GENERICINST:
-                               if (MONO_TYPE_IS_REFERENCE (rtype)) {
-                                       sp->data.p = *(gpointer*)res_buf;
-                               } else {
-                                       /* The result was written to vt_sp */
-                                       sp->data.p = vt_sp;
-                               }
-                               break;
-                       default:
-                               printf ("%s\n", mono_type_full_name (rtype));
-                               g_assert_not_reached ();
-                               break;
-                       }
-                       if (rtype->type != MONO_TYPE_VOID)
+                       if (rmethod->rtype->type != MONO_TYPE_VOID)
                                sp++;
+
                        MINT_IN_BREAK;
                }
 
@@ -2714,7 +2710,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        this_arg = sp->data.p;
                        if (!this_arg)
                                THROW_EX (mono_get_exception_null_reference(), ip - 2);
-                       child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
+                       child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
 
                        MonoClass *this_class = this_arg->vtable->klass;
                        if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
@@ -2769,7 +2765,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        this_arg = sp->data.p;
                        if (!this_arg)
                                THROW_EX (mono_get_exception_null_reference(), ip - 2);
-                       child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
+                       child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
 
                        MonoClass *this_class = this_arg->vtable->klass;
                        if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
@@ -2816,7 +2812,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        goto exit_frame;
                MINT_IN_CASE(MINT_RET_VOID)
                        if (sp > frame->stack)
-                               g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
+                               g_warning ("ret.void: more values on stack: %d %s", sp-frame->stack, mono_method_full_name (frame->runtime_method->method, TRUE));
                        goto exit_frame;
                MINT_IN_CASE(MINT_RET_VT)
                        i32 = READ32(ip + 1);
@@ -3107,7 +3103,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                                gint offset;
                                ip += 2 * (guint32)sp->data.i;
                                offset = READ32 (ip);
-                               ip = st + offset;
+                               ip = ip + offset;
                        } else {
                                ip = st;
                        }
@@ -3136,7 +3132,8 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_LDIND_I8)
                        ++ip;
-                       sp[-1].data.l = *(gint64*)sp[-1].data.p;
+                       /* memmove handles unaligned case */
+                       memmove (&sp [-1].data.l, sp [-1].data.p, sizeof (gint64));
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_LDIND_I) {
                        guint16 offset = * (guint16 *)(ip + 1);
@@ -3430,7 +3427,11 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        ++ip;
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_CONV_U4_R8)
-                       sp [-1].data.i = (guint32)sp [-1].data.f;
+                       /* needed on arm64 */
+                       if (isinf (sp [-1].data.f))
+                               sp [-1].data.i = 0;
+                       else
+                               sp [-1].data.i = (guint32)sp [-1].data.f;
                        ++ip;
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_CONV_I8_I4)
@@ -3479,10 +3480,13 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_CPOBJ) {
                        c = rtm->data_items[* (guint16 *)(ip + 1)];
-                       g_assert (c->byval_arg.type == MONO_TYPE_VALUETYPE);
+                       g_assert (c->valuetype);
                        /* if this assertion fails, we need to add a write barrier */
                        g_assert (!MONO_TYPE_IS_REFERENCE (&c->byval_arg));
-                       stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
+                       if (c->byval_arg.type == MONO_TYPE_VALUETYPE)
+                               stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
+                       else
+                               stackval_from_data (&c->byval_arg, sp [-2].data.p, sp [-1].data.p, FALSE);
                        ip += 2;
                        sp -= 2;
                        MINT_IN_BREAK;
@@ -3532,7 +3536,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        if (newobj_class->parent == mono_defaults.array_class) {
                                sp -= csig->param_count;
                                child_frame.stack_args = sp;
-                               o = ves_array_create (&child_frame, context->domain, newobj_class, csig, sp);
+                               o = ves_array_create (&child_frame, rtm->domain, newobj_class, csig, sp);
                                if (child_frame.ex)
                                        THROW_EX (child_frame.ex, ip);
                                goto array_constructed;
@@ -3560,7 +3564,7 @@ ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context, uns
                        } else {
                                if (newobj_class != mono_defaults.string_class) {
                                        context->managed_code = 0;
-                                       o = mono_object_new_checked (context->domain, newobj_class, &error);
+                                       o = mono_object_new_checked (rtm->domain, newobj_class, &error);
                                        mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                                        context->managed_code = 1;
                                        if (*mono_thread_interruption_request_flag ())
@@ -3654,6 +3658,7 @@ array_constructed:
                        frame->ex_handler = NULL;
                        if (!sp->data.p)
                                sp->data.p = mono_get_exception_null_reference ();
+
                        THROW_EX ((MonoException *)sp->data.p, ip);
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
@@ -3714,14 +3719,15 @@ array_constructed:
                                THROW_EX (mono_get_exception_null_reference (), ip);
                        field = rtm->data_items[* (guint16 *)(ip + 1)];
                        ip += 2;
+#ifndef DISABLE_REMOTING
                        if (mono_object_is_transparent_proxy (o)) {
                                MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
 
                                addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       } else {
+                       } else
+#endif
                                addr = (char*)o + field->offset;
-                       }                               
 
                        stackval_from_data (field->type, &sp [-1], addr, FALSE);
                        MINT_IN_BREAK;
@@ -3738,13 +3744,14 @@ array_constructed:
                        field = rtm->data_items[* (guint16 *)(ip + 1)];
                        i32 = READ32(ip + 2);
                        ip += 4;
+#ifndef DISABLE_REMOTING
                        if (mono_object_is_transparent_proxy (o)) {
                                MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
                                addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       } else {
+                       } else
+#endif
                                addr = (char*)o + field->offset;
-                       }                               
 
                        sp [-1].data.p = vt_sp;
                        memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
@@ -3800,11 +3807,13 @@ array_constructed:
                        field = rtm->data_items[* (guint16 *)(ip + 1)];
                        ip += 2;
 
+#ifndef DISABLE_REMOTING
                        if (mono_object_is_transparent_proxy (o)) {
                                MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
                                mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                        } else
+#endif
                                stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
 
                        sp -= 2;
@@ -3820,11 +3829,13 @@ array_constructed:
                        i32 = READ32(ip + 2);
                        ip += 4;
 
+#ifndef DISABLE_REMOTING
                        if (mono_object_is_transparent_proxy (o)) {
                                MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
                                mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                        } else
+#endif
                                memcpy((char*)o + field->offset, sp [-1].data.p, i32);
 
                        sp -= 2;
@@ -3833,14 +3844,14 @@ array_constructed:
                }
                MINT_IN_CASE(MINT_LDSFLDA) {
                        MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
-                       sp->data.p = mono_class_static_field_address (context->domain, field);
+                       sp->data.p = mono_class_static_field_address (rtm->domain, field);
                        ip += 2;
                        ++sp;
                        MINT_IN_BREAK;
                }
                MINT_IN_CASE(MINT_LDSFLD) {
                        MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
-                       gpointer addr = mono_class_static_field_address (context->domain, field);
+                       gpointer addr = mono_class_static_field_address (rtm->domain, field);
                        stackval_from_data (field->type, sp, addr, FALSE);
                        ip += 2;
                        ++sp;
@@ -3848,7 +3859,7 @@ array_constructed:
                }
                MINT_IN_CASE(MINT_LDSFLD_VT) {
                        MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
-                       gpointer addr = mono_class_static_field_address (context->domain, field);
+                       gpointer addr = mono_class_static_field_address (rtm->domain, field);
                        int size = READ32 (ip + 2);
                        ip += 4;
 
@@ -3860,7 +3871,7 @@ array_constructed:
                }
                MINT_IN_CASE(MINT_STSFLD) {
                        MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
-                       gpointer addr = mono_class_static_field_address (context->domain, field);
+                       gpointer addr = mono_class_static_field_address (rtm->domain, field);
                        ip += 2;
                        --sp;
                        stackval_to_data (field->type, sp, addr, FALSE);
@@ -3868,7 +3879,7 @@ array_constructed:
                }
                MINT_IN_CASE(MINT_STSFLD_VT) {
                        MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
-                       gpointer addr = mono_class_static_field_address (context->domain, field);
+                       gpointer addr = mono_class_static_field_address (rtm->domain, field);
                        int size = READ32 (ip + 2);
                        ip += 4;
 
@@ -3946,20 +3957,20 @@ array_constructed:
 
                        if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
                                int size = mono_class_value_size (c, NULL);
-                               sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, sp [-1 - offset].data.p, &error);
+                               sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, sp [-1 - offset].data.p, &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                                size = (size + 7) & ~7;
                                vt_sp -= size;
                        } else {
                                stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
-                               sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, &sp [-1 - offset], &error);
+                               sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, &sp [-1 - offset], &error);
                                mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                        }
                        ip += 3;
                        MINT_IN_BREAK;
                }
                MINT_IN_CASE(MINT_NEWARR)
-                       sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
+                       sp [-1].data.p = (MonoObject*) mono_array_new_checked (rtm->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
                        if (!mono_error_ok (&error)) {
                                THROW_EX (mono_error_convert_to_exception (&error), ip);
                        }
@@ -4465,7 +4476,7 @@ array_constructed:
                        ++sp;
                        MINT_IN_BREAK;
                MINT_IN_CASE(MINT_MONO_NEWOBJ)
-                       sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
+                       sp->data.p = mono_object_new_checked (rtm->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
                        mono_error_cleanup (&error); /* FIXME: don't swallow the error */
                        ip += 2;
                        sp++;
@@ -4491,6 +4502,11 @@ array_constructed:
                        ip += 3;
                        MINT_IN_BREAK;
                }
+               MINT_IN_CASE(MINT_MONO_MEMORY_BARRIER) {
+                       ++ip;
+                       mono_memory_barrier ();
+                       MINT_IN_BREAK;
+               }
                MINT_IN_CASE(MINT_MONO_JIT_ATTACH) {
                        ++ip;
 
@@ -4498,14 +4514,78 @@ array_constructed:
                        MonoDomain *tls_domain = (MonoDomain *) ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
                        gpointer tls_jit = ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
 
-                       if (tls_domain != context->domain || !tls_jit)
-                               context->original_domain = mono_jit_thread_attach (context->domain);
+                       if (tls_domain != rtm->domain || !tls_jit)
+                               context->original_domain = mono_jit_thread_attach (rtm->domain);
                        MINT_IN_BREAK;
                }
                MINT_IN_CASE(MINT_MONO_JIT_DETACH)
                        ++ip;
                        mono_jit_set_domain (context->original_domain);
                        MINT_IN_BREAK;
+               MINT_IN_CASE(MINT_MONO_LDDOMAIN)
+                       sp->data.p = mono_domain_get ();
+                       ++sp;
+                       ++ip;
+                       MINT_IN_BREAK;
+               MINT_IN_CASE(MINT_SDB_INTR_LOC)
+                       if (G_UNLIKELY (ss_enabled)) {
+                               static void (*ss_tramp) (void);
+
+                               if (!ss_tramp) {
+                                       void *tramp = mini_get_single_step_trampoline ();
+                                       mono_memory_barrier ();
+                                       ss_tramp = tramp;
+                               }
+
+                               /*
+                                * Make this point to the MINT_SDB_SEQ_POINT instruction which follows this since
+                                * the address of that instruction is stored as the seq point address.
+                                */
+                               frame->ip = ip + 1;
+
+                               /*
+                                * Use the same trampoline as the JIT. This ensures that
+                                * the debugger has the context for the last interpreter
+                                * native frame.
+                                */
+                               do_debugger_tramp (ss_tramp, frame);
+
+                               if (context->has_resume_state) {
+                                       if (frame == context->handler_frame)
+                                               SET_RESUME_STATE (context);
+                                       else
+                                               goto exit_frame;
+                               }
+                       }
+                       ++ip;
+                       MINT_IN_BREAK;
+               MINT_IN_CASE(MINT_SDB_SEQ_POINT)
+                       /* Just a placeholder for a breakpoint */
+                       ++ip;
+                       MINT_IN_BREAK;
+               MINT_IN_CASE(MINT_SDB_BREAKPOINT) {
+                       static void (*bp_tramp) (void);
+                       if (!bp_tramp) {
+                               void *tramp = mini_get_breakpoint_trampoline ();
+                               mono_memory_barrier ();
+                               bp_tramp = tramp;
+                       }
+
+                       frame->ip = ip;
+
+                       /* Use the same trampoline as the JIT */
+                       do_debugger_tramp (bp_tramp, frame);
+
+                       if (context->has_resume_state) {
+                               if (frame == context->handler_frame)
+                                       SET_RESUME_STATE (context);
+                               else
+                                       goto exit_frame;
+                       }
+
+                       ++ip;
+                       MINT_IN_BREAK;
+               }
 
 #define RELOP(datamem, op) \
        --sp; \
@@ -4604,7 +4684,7 @@ array_constructed:
                        if (!sp->data.p)
                                THROW_EX (mono_get_exception_null_reference (), ip - 2);
                                
-                       sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
+                       sp->data.p = get_virtual_method (m, sp->data.p);
                        ++sp;
                        MINT_IN_BREAK;
                }
@@ -4806,7 +4886,7 @@ array_constructed:
                        ip += 2;
                        MINT_IN_BREAK;
 #endif
-               MINT_IN_CASE(MINT_RETHROW)
+          MINT_IN_CASE(MINT_RETHROW) {
                        /* 
                         * need to clarify what this should actually do:
                         * start the search from the last found handler in
@@ -4817,9 +4897,11 @@ array_constructed:
                         * We need to NULL frame->ex_handler for the later code to
                         * actually run the new found handler.
                         */
+                       int exvar_offset = *(guint16*)(ip + 1);
                        frame->ex_handler = NULL;
-                       THROW_EX (frame->ex, ip - 1);
+                       THROW_EX_GENERAL (*(MonoException**)(frame->locals + exvar_offset), ip - 1, TRUE);
                        MINT_IN_BREAK;
+          }
                MINT_IN_DEFAULT
                        g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
                        THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
@@ -4887,6 +4969,7 @@ array_constructed:
                                                        g_print ("* Matched Filter at '%s'\n", method->name);
 #endif
                                                inv->ex_handler = clause;
+                                               *(MonoException**)(inv->locals + inv->runtime_method->exvar_offsets [i]) = frame->ex;
                                                goto handle_finally;
                                        }
                                } else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
@@ -4902,6 +4985,7 @@ array_constructed:
                                                        g_print ("* Found handler at '%s'\n", method->name);
 #endif
                                                inv->ex_handler = clause;
+                                               *(MonoException**)(inv->locals + inv->runtime_method->exvar_offsets [i]) = frame->ex;
                                                goto handle_finally;
                                        }
                                }
@@ -5033,6 +5117,7 @@ ves_exec_method (MonoInvocation *frame)
 {
        ThreadContext *context = mono_native_tls_get_value (thread_context_id);
        ThreadContext context_struct;
+       MonoDomain *domain = frame->runtime_method->domain;
        MonoError error;
        jmp_buf env;
 
@@ -5044,18 +5129,17 @@ ves_exec_method (MonoInvocation *frame)
        }
        if (context == NULL) {
                context = &context_struct;
-               context_struct.domain = mono_domain_get ();
                context_struct.base_frame = frame;
                context_struct.current_frame = NULL;
                context_struct.env_frame = frame;
                context_struct.current_env = &env;
                context_struct.search_for_handler = 0;
                context_struct.managed_code = 0;
-               mono_native_tls_set_value (thread_context_id, context);
+               set_context (context);
        }
        frame->ip = NULL;
        frame->parent = context->current_frame;
-       frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
+       frame->runtime_method = mono_interp_get_runtime_method (domain, frame->method, &error);
        mono_error_cleanup (&error); /* FIXME: don't swallow the error */
        context->managed_code = 1;
        ves_exec_method_with_context (frame, context, NULL, NULL, -1);
@@ -5069,160 +5153,11 @@ ves_exec_method (MonoInvocation *frame)
                        mono_unhandled_exception ((MonoObject*)frame->ex);
        }
        if (context->base_frame == frame)
-               mono_native_tls_set_value (thread_context_id, NULL);
+               set_context (NULL);
        else
                context->current_frame = frame->parent;
 }
 
-static int 
-ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
-{
-       MonoImage *image = mono_assembly_get_image (assembly);
-       MonoMethod *method;
-       MonoError error;
-       int rval;
-
-       method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
-       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-
-       if (!method)
-               g_error ("No entry point method found in %s", mono_image_get_filename (image));
-
-       rval = mono_runtime_run_main_checked (method, argc, argv, &error);
-       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-       return rval;
-}
-
-static void
-usage (void)
-{
-       fprintf (stderr,
-                "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
-                "Usage is: mint [options] executable args...\n\n", VERSION);
-       fprintf (stderr,
-                "Runtime Debugging:\n"
-#ifdef DEBUG_INTERP
-                "   --debug\n"
-#endif
-                "   --dieonex\n"
-                "   --noptr\t\t\tdon't print pointer addresses in trace output\n"
-                "   --opcode-count\n"
-                "   --print-vtable\n"
-                "   --traceclassinit\n"
-                "\n"
-                "Development:\n"
-                "   --debug method_name\n"
-                "   --profile\n"
-                "   --trace\n"
-                "   --traceops\n"
-                "   --regression\n"
-                "\n"
-                "Runtime:\n"
-                "   --config filename  load the specified config file instead of the default\n"
-                "   --workers n        maximum number of worker threads\n"
-               );
-       exit (1);
-}
-
-static MonoBoolean
-interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
-                         MonoReflectionMethod **method, 
-                         gint32 *iloffset, gint32 *native_offset,
-                         MonoString **file, gint32 *line, gint32 *column)
-{
-       ThreadContext *context = mono_native_tls_get_value (thread_context_id);
-       MonoInvocation *inv = context->current_frame;
-       MonoError error;
-       int i;
-
-       for (i = 0; inv && i < skip; inv = inv->parent)
-               if (inv->runtime_method != NULL)
-                       ++i;
-
-       if (iloffset)
-               *iloffset = 0;
-       if (native_offset)
-               *native_offset = 0;
-       if (method) {
-               if (inv == NULL) {
-                       *method = NULL;
-               } else {
-                       *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
-                       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-               }
-       }
-       if (line)
-               *line = 0;
-       if (need_file_info) {
-               if (column)
-                       *column = 0;
-               if (file) {
-                       *file = mono_string_new_checked (mono_domain_get (), "unknown", &error);
-                       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-               }
-       }
-
-       return TRUE;
-}
-
-static MonoArray *
-interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
-{
-       MonoDomain *domain = mono_domain_get ();
-       MonoArray *res;
-       MonoArray *ta = exc->trace_ips;
-       MonoError error;
-       int i, len;
-
-       if (ta == NULL) {
-               /* Exception is not thrown yet */
-               MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
-               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-               return array;
-       }
-       
-       len = mono_array_length (ta);
-
-       res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
-       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-
-       for (i = skip; i < len / 2; i++) {
-               MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
-               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-               gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
-               RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
-
-               if (rtm != NULL) {
-                       sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
-                       mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       sf->native_offset = ip - rtm->code;
-               }
-
-#if 0
-               sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
-
-               if (need_file_info) {
-                       gchar *filename;
-                       
-                       filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
-
-                       sf->filename = NULL;
-                       if (filename) {
-                               sf->filename = mono_string_new_checked (domain, filename, &error);
-                               mono_error_cleanup (&error); /* FIXME: don't swallow the error */
-                       }
-                       sf->column = 0;
-
-                       g_free (filename);
-               }
-#endif
-
-               mono_array_set (res, gpointer, i, sf);
-       }
-
-       return res;
-}
-
 void
 mono_interp_parse_options (const char *options)
 {
@@ -5241,7 +5176,7 @@ void
 mono_interp_init ()
 {
        mono_native_tls_alloc (&thread_context_id, NULL);
-       mono_native_tls_set_value (thread_context_id, NULL);
+       set_context (NULL);
 
        mono_interp_transform_init ();
 }
@@ -5417,12 +5352,16 @@ mono_interp_regression_list (int verbose, int count, char *images [])
  *   Set the state the interpeter will continue to execute from after execution returns to the interpreter.
  */
 void
-mono_interp_set_resume_state (MonoException *ex, StackFrameInfo *frame, gpointer handler_ip)
+mono_interp_set_resume_state (MonoJitTlsData *jit_tls, MonoException *ex, MonoInterpFrameHandle interp_frame, gpointer handler_ip)
 {
-       ThreadContext *context = mono_native_tls_get_value (thread_context_id);
+       ThreadContext *context;
+
+       g_assert (jit_tls);
+       context = jit_tls->interp_context;
+       g_assert (context);
 
        context->has_resume_state = TRUE;
-       context->handler_frame = frame->interp_frame;
+       context->handler_frame = interp_frame;
        /* This is on the stack, so it doesn't need a wbarrier */
        context->handler_frame->ex = ex;
        context->handler_ip = handler_ip;
@@ -5468,20 +5407,115 @@ mono_interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
 
        memset (frame, 0, sizeof (StackFrameInfo));
        /* pinvoke frames doesn't have runtime_method set */
-       while (iframe && !iframe->runtime_method)
+       while (iframe && !(iframe->runtime_method && iframe->runtime_method->code))
                iframe = iframe->parent;
        if (!iframe)
                return FALSE;
 
        frame->type = FRAME_TYPE_INTERP;
+       // FIXME:
+       frame->domain = mono_domain_get ();
        frame->interp_frame = iframe;
        frame->method = iframe->runtime_method->method;
        frame->actual_method = frame->method;
        /* This is the offset in the interpreter IR */
-       frame->native_offset = iframe->ip - iframe->runtime_method->code;
+       frame->native_offset = (guint8*)iframe->ip - (guint8*)iframe->runtime_method->code;
        frame->ji = iframe->runtime_method->jinfo;
 
        stack_iter->current = iframe->parent;
 
        return TRUE;
 }
+
+MonoJitInfo*
+mono_interp_find_jit_info (MonoDomain *domain, MonoMethod *method)
+{
+       RuntimeMethod* rtm;
+
+       rtm = lookup_runtime_method (domain, method);
+       if (rtm)
+               return rtm->jinfo;
+       else
+               return NULL;
+}
+
+void
+mono_interp_set_breakpoint (MonoJitInfo *jinfo, gpointer ip)
+{
+       guint16 *code = (guint16*)ip;
+       g_assert (*code == MINT_SDB_SEQ_POINT);
+       *code = MINT_SDB_BREAKPOINT;
+}
+
+void
+mono_interp_clear_breakpoint (MonoJitInfo *jinfo, gpointer ip)
+{
+       guint16 *code = (guint16*)ip;
+       g_assert (*code == MINT_SDB_BREAKPOINT);
+       *code = MINT_SDB_SEQ_POINT;
+}
+
+MonoJitInfo*
+mono_interp_frame_get_jit_info (MonoInterpFrameHandle frame)
+{
+       MonoInvocation *iframe = (MonoInvocation*)frame;
+
+       g_assert (iframe->runtime_method);
+       return iframe->runtime_method->jinfo;
+}
+
+gpointer
+mono_interp_frame_get_ip (MonoInterpFrameHandle frame)
+{
+       MonoInvocation *iframe = (MonoInvocation*)frame;
+
+       g_assert (iframe->runtime_method);
+       return (gpointer)iframe->ip;
+}
+
+gpointer
+mono_interp_frame_get_arg (MonoInterpFrameHandle frame, int pos)
+{
+       MonoInvocation *iframe = (MonoInvocation*)frame;
+
+       g_assert (iframe->runtime_method);
+
+       int arg_offset = iframe->runtime_method->arg_offsets [pos + (iframe->runtime_method->hasthis ? 1 : 0)];
+
+       return iframe->args + arg_offset;
+}
+
+gpointer
+mono_interp_frame_get_local (MonoInterpFrameHandle frame, int pos)
+{
+       MonoInvocation *iframe = (MonoInvocation*)frame;
+
+       g_assert (iframe->runtime_method);
+
+       return iframe->locals + iframe->runtime_method->local_offsets [pos];
+}
+
+gpointer
+mono_interp_frame_get_this (MonoInterpFrameHandle frame)
+{
+       MonoInvocation *iframe = (MonoInvocation*)frame;
+
+       g_assert (iframe->runtime_method);
+       g_assert (iframe->runtime_method->hasthis);
+
+       int arg_offset = iframe->runtime_method->arg_offsets [0];
+
+       return iframe->args + arg_offset;
+}
+
+void
+mono_interp_start_single_stepping (void)
+{
+       ss_enabled = TRUE;
+}
+
+void
+mono_interp_stop_single_stepping (void)
+{
+       ss_enabled = FALSE;
+}