[wasm] Fix test_0_conv_i under interp.
[mono.git] / mono / mini / interp / interp.c
1 /**
2  * \file
3  * PLEASE NOTE: This is a research prototype.
4  *
5  *
6  * interp.c: Interpreter for CIL byte codes
7  *
8  * Authors:
9  *   Paolo Molaro (lupus@ximian.com)
10  *   Miguel de Icaza (miguel@ximian.com)
11  *   Dietmar Maurer (dietmar@ximian.com)
12  *
13  * (C) 2001, 2002 Ximian, Inc.
14  */
15 #ifndef __USE_ISOC99
16 #define __USE_ISOC99
17 #endif
18 #include "config.h"
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <setjmp.h>
24 #include <signal.h>
25 #include <math.h>
26 #include <locale.h>
27
28 #include <mono/utils/gc_wrapper.h>
29
30 #ifdef HAVE_ALLOCA_H
31 #   include <alloca.h>
32 #else
33 #   ifdef __CYGWIN__
34 #      define alloca __builtin_alloca
35 #   endif
36 #endif
37
38 /* trim excessive headers */
39 #include <mono/metadata/image.h>
40 #include <mono/metadata/assembly-internals.h>
41 #include <mono/metadata/cil-coff.h>
42 #include <mono/metadata/mono-endian.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/tokentype.h>
45 #include <mono/metadata/loader.h>
46 #include <mono/metadata/threads.h>
47 #include <mono/metadata/threadpool.h>
48 #include <mono/metadata/profiler-private.h>
49 #include <mono/metadata/appdomain.h>
50 #include <mono/metadata/reflection.h>
51 #include <mono/metadata/reflection-internals.h>
52 #include <mono/metadata/exception.h>
53 #include <mono/metadata/verify.h>
54 #include <mono/metadata/opcodes.h>
55 #include <mono/metadata/debug-helpers.h>
56 #include <mono/metadata/mono-config.h>
57 #include <mono/metadata/marshal.h>
58 #include <mono/metadata/environment.h>
59 #include <mono/metadata/mono-debug.h>
60 #include <mono/utils/atomic.h>
61
62 #include "interp.h"
63 #include "interp-internals.h"
64 #include "mintops.h"
65 #include "hacks.h"
66
67 #include <mono/mini/mini.h>
68 #include <mono/mini/jit-icalls.h>
69 #include <mono/mini/debugger-agent.h>
70
71 #ifdef TARGET_ARM
72 #include <mono/mini/mini-arm.h>
73 #endif
74
75 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
76 #ifdef _WIN32
77 #define isnan _isnan
78 #define finite _finite
79 #endif
80 #ifndef HAVE_FINITE
81 #ifdef HAVE_ISFINITE
82 #define finite isfinite
83 #endif
84 #endif
85
86 static inline void
87 init_frame (InterpFrame *frame, InterpFrame *parent_frame, InterpMethod *rmethod, stackval *method_args, stackval *method_retval)
88 {
89         frame->parent = parent_frame;
90         frame->stack_args = method_args;
91         frame->retval = method_retval;
92         frame->imethod = rmethod;
93         frame->ex = NULL;
94         frame->ip = NULL;
95         frame->invoke_trap = 0;
96 }
97
98 #define INIT_FRAME(frame,parent_frame,method_args,method_retval,domain,mono_method,error) do { \
99         InterpMethod *_rmethod = mono_interp_get_imethod ((domain), (mono_method), (error));    \
100         init_frame ((frame), (parent_frame), _rmethod, (method_args), (method_retval)); \
101         } while (0)
102
103 /*
104  * List of classes whose methods will be executed by transitioning to JITted code.
105  * Used for testing.
106  */
107 GSList *jit_classes;
108 /* If TRUE, interpreted code will be interrupted at function entry/backward branches */
109 static gboolean ss_enabled;
110
111 void ves_exec_method (InterpFrame *frame);
112
113 static char* dump_frame (InterpFrame *inv);
114 static MonoArray *get_trace_ips (MonoDomain *domain, InterpFrame *top);
115 static void ves_exec_method_with_context (InterpFrame *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally);
116
117 typedef void (*ICallMethod) (InterpFrame *frame);
118
119 static guint32 die_on_exception = 0;
120 static MonoNativeTlsKey thread_context_id;
121
122 static char* dump_args (InterpFrame *inv);
123
124 #define DEBUG_INTERP 0
125 #define COUNT_OPS 0
126 #if DEBUG_INTERP
127 int mono_interp_traceopt = 2;
128 /* If true, then we output the opcodes as we interpret them */
129 static int global_tracing = 2;
130
131 static int debug_indent_level = 0;
132
133 static int break_on_method = 0;
134 static int nested_trace = 0;
135 static GList *db_methods = NULL;
136
137 static void
138 output_indent (void)
139 {
140         int h;
141
142         for (h = 0; h < debug_indent_level; h++)
143                 g_print ("  ");
144 }
145
146 static void
147 db_match_method (gpointer data, gpointer user_data)
148 {
149         MonoMethod *m = (MonoMethod*)user_data;
150         MonoMethodDesc *desc = data;
151
152         if (mono_method_desc_full_match (desc, m))
153                 break_on_method = 1;
154 }
155
156 static void
157 debug_enter (InterpFrame *frame, int *tracing)
158 {
159         if (db_methods) {
160                 g_list_foreach (db_methods, db_match_method, (gpointer)frame->imethod->method);
161                 if (break_on_method)
162                         *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
163                 break_on_method = 0;
164         }
165         if (*tracing) {
166                 MonoMethod *method = frame->imethod->method;
167                 char *mn, *args = dump_args (frame);
168                 debug_indent_level++;
169                 output_indent ();
170                 mn = mono_method_full_name (method, FALSE);
171                 g_print ("(%p) Entering %s (", mono_thread_internal_current (), mn);
172                 g_free (mn);
173                 g_print  ("%s)\n", args);
174                 g_free (args);
175         }
176 }
177
178
179 #define DEBUG_LEAVE()   \
180         if (tracing) {  \
181                 char *mn, *args;        \
182                 args = dump_retval (frame);     \
183                 output_indent ();       \
184                 mn = mono_method_full_name (frame->imethod->method, FALSE); \
185                 g_print  ("(%p) Leaving %s", mono_thread_internal_current (),  mn);     \
186                 g_free (mn); \
187                 g_print  (" => %s\n", args);    \
188                 g_free (args);  \
189                 debug_indent_level--;   \
190                 if (tracing == 3) global_tracing = 0; \
191         }
192
193 #else
194
195 int mono_interp_traceopt = 0;
196 static void debug_enter (InterpFrame *frame, int *tracing)
197 {
198 }
199 #define DEBUG_LEAVE()
200
201 #endif
202
203 /* Set the current execution state to the resume state in context */
204 #define SET_RESUME_STATE(context) do { \
205                 ip = (context)->handler_ip;                                             \
206                 if (frame->ex) { \
207                 sp->data.p = frame->ex;                                                                                 \
208                 ++sp;                                                                                                                   \
209                 } \
210                 frame->ex = NULL;                                                                                               \
211                 (context)->has_resume_state = 0;                                                                \
212                 (context)->handler_frame = NULL;                                                                \
213                 goto main_loop;                                                                                                 \
214         } while (0)
215
216 static void
217 set_context (ThreadContext *context)
218 {
219         MonoJitTlsData *jit_tls;
220
221         mono_native_tls_set_value (thread_context_id, context);
222         jit_tls = mono_tls_get_jit_tls ();
223         if (jit_tls)
224                 jit_tls->interp_context = context;
225 }
226
227 static void
228 ves_real_abort (int line, MonoMethod *mh,
229                 const unsigned short *ip, stackval *stack, stackval *sp)
230 {
231         MonoError error;
232         fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
233         fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
234                  ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
235                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
236         g_print ("0x%04x %02x\n",
237                  ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
238         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
239         if (sp > stack)
240                 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
241 }
242
243 #define ves_abort() \
244         do {\
245                 ves_real_abort(__LINE__, frame->imethod->method, ip, frame->stack, sp); \
246                 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
247         } while (0);
248
249 static InterpMethod*
250 lookup_imethod (MonoDomain *domain, MonoMethod *method)
251 {
252         InterpMethod *rtm;
253         MonoJitDomainInfo *info;
254
255         info = domain_jit_info (domain);
256         mono_domain_jit_code_hash_lock (domain);
257         rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
258         mono_domain_jit_code_hash_unlock (domain);
259         return rtm;
260 }
261
262 InterpMethod*
263 mono_interp_get_imethod (MonoDomain *domain, MonoMethod *method, MonoError *error)
264 {
265         InterpMethod *rtm;
266         MonoJitDomainInfo *info;
267         MonoMethodSignature *sig;
268         int i;
269
270         error_init (error);
271
272         info = domain_jit_info (domain);
273         mono_domain_jit_code_hash_lock (domain);
274         rtm = mono_internal_hash_table_lookup (&info->interp_code_hash, method);
275         mono_domain_jit_code_hash_unlock (domain);
276         if (rtm)
277                 return rtm;
278
279         sig = mono_method_signature (method);
280
281         rtm = mono_domain_alloc0 (domain, sizeof (InterpMethod));
282         rtm->method = method;
283         rtm->domain = domain;
284         rtm->param_count = sig->param_count;
285         rtm->hasthis = sig->hasthis;
286         rtm->rtype = mini_get_underlying_type (sig->ret);
287         rtm->param_types = mono_domain_alloc0 (domain, sizeof (MonoType*) * sig->param_count);
288         for (i = 0; i < sig->param_count; ++i)
289                 rtm->param_types [i] = mini_get_underlying_type (sig->params [i]);
290
291         mono_domain_jit_code_hash_lock (domain);
292         if (!mono_internal_hash_table_lookup (&info->interp_code_hash, method))
293                 mono_internal_hash_table_insert (&info->interp_code_hash, method, rtm);
294         mono_domain_jit_code_hash_unlock (domain);
295
296         rtm->prof_flags = mono_profiler_get_call_instrumentation_flags (rtm->method);
297
298         return rtm;
299 }
300
301 gpointer
302 mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
303 {
304         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
305                 method = mono_marshal_get_synchronized_wrapper (method);
306         return mono_interp_get_imethod (domain, method, error);
307 }
308
309 /*
310  * interp_push_lmf:
311  *
312  * Push an LMF frame on the LMF stack
313  * to mark the transition to native code.
314  * This is needed for the native code to
315  * be able to do stack walks.
316  */
317 static void
318 interp_push_lmf (MonoLMFExt *ext, InterpFrame *frame)
319 {
320         memset (ext, 0, sizeof (MonoLMFExt));
321         ext->interp_exit = TRUE;
322         ext->interp_exit_data = frame;
323
324         mono_push_lmf (ext);
325 }
326
327 static void
328 interp_pop_lmf (MonoLMFExt *ext)
329 {
330         mono_pop_lmf (&ext->lmf);
331 }
332
333 static inline InterpMethod*
334 get_virtual_method (InterpMethod *imethod, MonoObject *obj)
335 {
336         MonoMethod *m = imethod->method;
337         MonoDomain *domain = imethod->domain;
338         InterpMethod *ret = NULL;
339         MonoError error;
340
341 #ifndef DISABLE_REMOTING
342         if (mono_object_is_transparent_proxy (obj)) {
343                 ret = mono_interp_get_imethod (domain, mono_marshal_get_remoting_invoke (m), &error);
344                 mono_error_assert_ok (&error);
345                 return ret;
346         }
347 #endif
348
349         if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
350                 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
351                         ret = mono_interp_get_imethod (domain, mono_marshal_get_synchronized_wrapper (m), &error);
352                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
353                 } else {
354                         ret = imethod;
355                 }
356                 return ret;
357         }
358
359         mono_class_setup_vtable (obj->vtable->klass);
360
361         int slot = mono_method_get_vtable_slot (m);
362         if (mono_class_is_interface (m->klass)) {
363                 g_assert (obj->vtable->klass != m->klass);
364                 /* TODO: interface offset lookup is slow, go through IMT instead */
365                 gboolean non_exact_match;
366                 slot += mono_class_interface_offset_with_variance (obj->vtable->klass, m->klass, &non_exact_match);
367         }
368
369         MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
370         if (m->is_inflated && mono_method_get_context (m)->method_inst) {
371                 MonoGenericContext context = { NULL, NULL };
372
373                 if (mono_class_is_ginst (virtual_method->klass))
374                         context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
375                 else if (mono_class_is_gtd (virtual_method->klass))
376                         context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
377                 context.method_inst = mono_method_get_context (m)->method_inst;
378
379                 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, &error);
380                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
381         }
382
383         if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
384                 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
385         }
386
387         InterpMethod *virtual_imethod = mono_interp_get_imethod (domain, virtual_method, &error);
388         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
389         return virtual_imethod;
390 }
391
392 static void inline
393 stackval_from_data (MonoType *type_, stackval *result, char *data, gboolean pinvoke)
394 {
395         MonoType *type = mini_native_type_replace_type (type_);
396         if (type->byref) {
397                 switch (type->type) {
398                 case MONO_TYPE_OBJECT:
399                 case MONO_TYPE_CLASS:
400                 case MONO_TYPE_STRING:
401                 case MONO_TYPE_ARRAY:
402                 case MONO_TYPE_SZARRAY:
403                         break;
404                 default:
405                         break;
406                 }
407                 result->data.p = *(gpointer*)data;
408                 return;
409         }
410         switch (type->type) {
411         case MONO_TYPE_VOID:
412                 return;
413         case MONO_TYPE_I1:
414                 result->data.i = *(gint8*)data;
415                 return;
416         case MONO_TYPE_U1:
417         case MONO_TYPE_BOOLEAN:
418                 result->data.i = *(guint8*)data;
419                 return;
420         case MONO_TYPE_I2:
421                 result->data.i = *(gint16*)data;
422                 return;
423         case MONO_TYPE_U2:
424         case MONO_TYPE_CHAR:
425                 result->data.i = *(guint16*)data;
426                 return;
427         case MONO_TYPE_I4:
428                 result->data.i = *(gint32*)data;
429                 return;
430         case MONO_TYPE_U:
431         case MONO_TYPE_I:
432                 result->data.nati = *(mono_i*)data;
433                 return;
434         case MONO_TYPE_PTR:
435                 result->data.p = *(gpointer*)data;
436                 return;
437         case MONO_TYPE_U4:
438                 result->data.i = *(guint32*)data;
439                 return;
440         case MONO_TYPE_R4: {
441                 float tmp;
442                 /* memmove handles unaligned case */
443                 memmove (&tmp, data, sizeof (float));
444                 result->data.f = tmp;
445                 return;
446     }
447         case MONO_TYPE_I8:
448         case MONO_TYPE_U8:
449                 memmove (&result->data.l, data, sizeof (gint64));
450                 return;
451         case MONO_TYPE_R8:
452                 memmove (&result->data.f, data, sizeof (double));
453                 return;
454         case MONO_TYPE_STRING:
455         case MONO_TYPE_SZARRAY:
456         case MONO_TYPE_CLASS:
457         case MONO_TYPE_OBJECT:
458         case MONO_TYPE_ARRAY:
459                 result->data.p = *(gpointer*)data;
460                 return;
461         case MONO_TYPE_VALUETYPE:
462                 if (type->data.klass->enumtype) {
463                         stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
464                         return;
465                 } else
466                         mono_value_copy (result->data.vt, data, type->data.klass);
467                 return;
468         case MONO_TYPE_GENERICINST: {
469                 if (mono_type_generic_inst_is_valuetype (type)) {
470                         mono_value_copy (result->data.vt, data, mono_class_from_mono_type (type));
471                         return;
472                 }
473                 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
474                 return;
475         }
476         default:
477                 g_warning ("got type 0x%02x", type->type);
478                 g_assert_not_reached ();
479         }
480 }
481
482 static void inline
483 stackval_to_data (MonoType *type_, stackval *val, char *data, gboolean pinvoke)
484 {
485         MonoType *type = mini_native_type_replace_type (type_);
486         if (type->byref) {
487                 gpointer *p = (gpointer*)data;
488                 *p = val->data.p;
489                 return;
490         }
491         /* printf ("TODAT0 %p\n", data); */
492         switch (type->type) {
493         case MONO_TYPE_I1:
494         case MONO_TYPE_U1: {
495                 guint8 *p = (guint8*)data;
496                 *p = val->data.i;
497                 return;
498         }
499         case MONO_TYPE_BOOLEAN: {
500                 guint8 *p = (guint8*)data;
501                 *p = (val->data.i != 0);
502                 return;
503         }
504         case MONO_TYPE_I2:
505         case MONO_TYPE_U2:
506         case MONO_TYPE_CHAR: {
507                 guint16 *p = (guint16*)data;
508                 *p = val->data.i;
509                 return;
510         }
511         case MONO_TYPE_I: {
512                 mono_i *p = (mono_i*)data;
513                 /* In theory the value used by stloc should match the local var type
514                    but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
515                    a native int - both by csc and mcs). Not sure what to do about sign extension
516                    as it is outside the spec... doing the obvious */
517                 *p = (mono_i)val->data.nati;
518                 return;
519         }
520         case MONO_TYPE_U: {
521                 mono_u *p = (mono_u*)data;
522                 /* see above. */
523                 *p = (mono_u)val->data.nati;
524                 return;
525         }
526         case MONO_TYPE_I4:
527         case MONO_TYPE_U4: {
528                 gint32 *p = (gint32*)data;
529                 *p = val->data.i;
530                 return;
531         }
532         case MONO_TYPE_I8:
533         case MONO_TYPE_U8: {
534                 gint64 *p = (gint64*)data;
535                 *p = val->data.l;
536                 return;
537         }
538         case MONO_TYPE_R4: {
539                 float *p = (float*)data;
540                 *p = val->data.f;
541                 return;
542         }
543         case MONO_TYPE_R8: {
544                 double *p = (double*)data;
545                 *p = val->data.f;
546                 return;
547         }
548         case MONO_TYPE_STRING:
549         case MONO_TYPE_SZARRAY:
550         case MONO_TYPE_CLASS:
551         case MONO_TYPE_OBJECT:
552         case MONO_TYPE_ARRAY: {
553                 gpointer *p = (gpointer *) data;
554                 mono_gc_wbarrier_generic_store (p, val->data.p);
555                 return;
556         }
557         case MONO_TYPE_PTR: {
558                 gpointer *p = (gpointer *) data;
559                 *p = val->data.p;
560                 return;
561         }
562         case MONO_TYPE_VALUETYPE:
563                 if (type->data.klass->enumtype) {
564                         stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
565                         return;
566                 } else
567                         mono_value_copy (data, val->data.vt, type->data.klass);
568                 return;
569         case MONO_TYPE_GENERICINST: {
570                 MonoClass *container_class = type->data.generic_class->container_class;
571
572                 if (container_class->valuetype && !container_class->enumtype) {
573                         mono_value_copy (data, val->data.vt, mono_class_from_mono_type (type));
574                         return;
575                 }
576                 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
577                 return;
578         }
579         default:
580                 g_warning ("got type %x", type->type);
581                 g_assert_not_reached ();
582         }
583 }
584
585 static void
586 fill_in_trace (MonoException *exception, InterpFrame *frame)
587 {
588         MonoError error;
589         char *stack_trace = dump_frame (frame);
590         MonoDomain *domain = frame->imethod->domain;
591         (exception)->stack_trace = mono_string_new_checked (domain, stack_trace, &error);
592         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
593         (exception)->trace_ips = get_trace_ips (domain, frame);
594         g_free (stack_trace);
595 }
596
597 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
598
599 #define THROW_EX_GENERAL(exception,ex_ip,rethrow)               \
600         do {\
601                 frame->ip = (ex_ip);            \
602                 frame->ex = (MonoException*)(exception);        \
603                 if (!rethrow) { \
604                         FILL_IN_TRACE(frame->ex, frame);        \
605                 } \
606                 MONO_PROFILER_RAISE (exception_throw, ((MonoObject *) exception));      \
607                 goto handle_exception;  \
608         } while (0)
609
610 #define THROW_EX(exception,ex_ip) THROW_EX_GENERAL ((exception), (ex_ip), FALSE)
611
612 static MonoObject*
613 ves_array_create (InterpFrame *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
614 {
615         uintptr_t *lengths;
616         intptr_t *lower_bounds;
617         MonoObject *obj;
618         MonoError error;
619         int i;
620
621         lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
622         for (i = 0; i < sig->param_count; ++i) {
623                 lengths [i] = values->data.i;
624                 values ++;
625         }
626         if (klass->rank == sig->param_count) {
627                 /* Only lengths provided. */
628                 lower_bounds = NULL;
629         } else {
630                 /* lower bounds are first. */
631                 lower_bounds = (intptr_t *) lengths;
632                 lengths += klass->rank;
633         }
634         obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
635         if (!mono_error_ok (&error)) {
636                 frame->ex = mono_error_convert_to_exception (&error);
637                 FILL_IN_TRACE (frame->ex, frame);
638         }
639         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
640         return obj;
641 }
642
643 static gint32
644 ves_array_calculate_index (MonoArray *ao, stackval *sp, InterpFrame *frame, gboolean safe)
645 {
646         g_assert (!frame->ex);
647         MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
648
649         guint32 pos = 0;
650         if (ao->bounds) {
651                 for (gint32 i = 0; i < ac->rank; i++) {
652                         guint32 idx = sp [i].data.i;
653                         guint32 lower = ao->bounds [i].lower_bound;
654                         guint32 len = ao->bounds [i].length;
655                         if (safe && (idx < lower || (idx - lower) >= len)) {
656                                 frame->ex = mono_get_exception_index_out_of_range ();
657                                 FILL_IN_TRACE (frame->ex, frame);
658                                 return -1;
659                         }
660                         pos = (pos * len) + idx - lower;
661                 }
662         } else {
663                 pos = sp [0].data.i;
664                 if (safe && pos >= ao->max_length) {
665                         frame->ex = mono_get_exception_index_out_of_range ();
666                         FILL_IN_TRACE (frame->ex, frame);
667                         return -1;
668                 }
669         }
670         return pos;
671 }
672
673 static void
674 ves_array_set (InterpFrame *frame)
675 {
676         stackval *sp = frame->stack_args + 1;
677
678         MonoObject *o = frame->stack_args->data.p;
679         MonoArray *ao = (MonoArray *) o;
680         MonoClass *ac = o->vtable->klass;
681
682         g_assert (ac->rank >= 1);
683
684         gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
685         if (frame->ex)
686                 return;
687
688         if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
689                 MonoError error;
690                 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
691                 mono_error_cleanup (&error);
692                 if (!isinst) {
693                         frame->ex = mono_get_exception_array_type_mismatch ();
694                         FILL_IN_TRACE (frame->ex, frame);
695                         return;
696                 }
697         }
698
699         gint32 esize = mono_array_element_size (ac);
700         gpointer ea = mono_array_addr_with_size (ao, esize, pos);
701
702         MonoType *mt = mono_method_signature (frame->imethod->method)->params [ac->rank];
703         stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
704 }
705
706 static void
707 ves_array_get (InterpFrame *frame, gboolean safe)
708 {
709         stackval *sp = frame->stack_args + 1;
710
711         MonoObject *o = frame->stack_args->data.p;
712         MonoArray *ao = (MonoArray *) o;
713         MonoClass *ac = o->vtable->klass;
714
715         g_assert (ac->rank >= 1);
716
717         gint32 pos = ves_array_calculate_index (ao, sp, frame, safe);
718         if (frame->ex)
719                 return;
720
721         gint32 esize = mono_array_element_size (ac);
722         gpointer ea = mono_array_addr_with_size (ao, esize, pos);
723
724         MonoType *mt = mono_method_signature (frame->imethod->method)->ret;
725         stackval_from_data (mt, frame->retval, ea, FALSE);
726 }
727
728 static gpointer
729 ves_array_element_address (InterpFrame *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
730 {
731         MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
732
733         g_assert (ac->rank >= 1);
734
735         gint32 pos = ves_array_calculate_index (ao, sp, frame, TRUE);
736         if (frame->ex)
737                 return NULL;
738
739         if (needs_typecheck && !mono_class_is_assignable_from (mono_object_class ((MonoObject *) ao)->element_class, required_type->element_class)) {
740                 frame->ex = mono_get_exception_array_type_mismatch ();
741                 FILL_IN_TRACE (frame->ex, frame);
742                 return NULL;
743         }
744         gint32 esize = mono_array_element_size (ac);
745         return mono_array_addr_with_size (ao, esize, pos);
746 }
747
748 void
749 interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUnwindOptions options, void *user_data)
750 {
751         MonoError error;
752         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
753
754         if (!context)
755                 return;
756
757         InterpFrame *frame = context->current_frame;
758
759         while (frame) {
760                 MonoStackFrameInfo fi;
761                 memset (&fi, 0, sizeof (MonoStackFrameInfo));
762
763                 /* TODO: hack to make some asserts happy. */
764                 fi.ji = (MonoJitInfo *) frame->imethod;
765
766                 if (frame->imethod)
767                         fi.method = fi.actual_method = frame->imethod->method;
768
769                 if (!fi.method || (fi.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (fi.method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
770                         fi.il_offset = -1;
771                         fi.type = FRAME_TYPE_MANAGED_TO_NATIVE;
772                 } else {
773                         MonoMethodHeader *hd = mono_method_get_header_checked (fi.method, &error);
774                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
775                         fi.type = FRAME_TYPE_MANAGED;
776                         fi.il_offset = frame->ip - (const unsigned short *) hd->code;
777                         if (!fi.method->wrapper_type)
778                                 fi.managed = TRUE;
779                 }
780
781                 if (func (&fi, ctx, user_data))
782                         return;
783                 frame = frame->parent;
784         }
785 }
786
787 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
788
789 static InterpMethodArguments* build_args_from_sig (MonoMethodSignature *sig, InterpFrame *frame)
790 {
791         InterpMethodArguments *margs = g_malloc0 (sizeof (InterpMethodArguments));
792
793 #ifdef TARGET_ARM
794         g_assert (mono_arm_eabi_supported ());
795         int i8_align = mono_arm_i8_align ();
796 #endif
797
798 #ifdef TARGET_WASM
799         margs->sig = sig;
800 #endif
801
802         if (sig->hasthis)
803                 margs->ilen++;
804
805         for (int i = 0; i < sig->param_count; i++) {
806                 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
807                 switch (ptype) {
808                 case MONO_TYPE_BOOLEAN:
809                 case MONO_TYPE_CHAR:
810                 case MONO_TYPE_I1:
811                 case MONO_TYPE_U1:
812                 case MONO_TYPE_I2:
813                 case MONO_TYPE_U2:
814                 case MONO_TYPE_I4:
815                 case MONO_TYPE_U4:
816                 case MONO_TYPE_I:
817                 case MONO_TYPE_U:
818                 case MONO_TYPE_PTR:
819                 case MONO_TYPE_SZARRAY:
820                 case MONO_TYPE_CLASS:
821                 case MONO_TYPE_OBJECT:
822                 case MONO_TYPE_STRING:
823                 case MONO_TYPE_VALUETYPE:
824                 case MONO_TYPE_GENERICINST:
825 #if SIZEOF_VOID_P == 8
826                 case MONO_TYPE_I8:
827 #endif
828                         margs->ilen++;
829                         break;
830 #if SIZEOF_VOID_P == 4
831                 case MONO_TYPE_I8:
832 #ifdef TARGET_ARM
833                         /* pairs begin at even registers */
834                         if (i8_align == 8 && margs->ilen & 1)
835                                 margs->ilen++;
836 #endif
837                         margs->ilen += 2;
838                         break;
839 #endif
840                 case MONO_TYPE_R4:
841 #if SIZEOF_VOID_P == 8
842                 case MONO_TYPE_R8:
843 #endif
844                         margs->flen++;
845                         break;
846 #if SIZEOF_VOID_P == 4
847                 case MONO_TYPE_R8:
848                         margs->flen += 2;
849                         break;
850 #endif
851                 default:
852                         g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
853                 }
854         }
855
856         if (margs->ilen > 0)
857                 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
858
859         if (margs->flen > 0)
860                 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
861
862         if (margs->ilen > INTERP_ICALL_TRAMP_IARGS)
863                 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
864
865         if (margs->flen > INTERP_ICALL_TRAMP_FARGS)
866                 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
867
868
869         size_t int_i = 0;
870         size_t int_f = 0;
871
872         if (sig->hasthis) {
873                 margs->iargs [0] = frame->stack_args->data.p;
874                 int_i++;
875         }
876
877         for (int i = 0; i < sig->param_count; i++) {
878                 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
879                 switch (ptype) {
880                 case MONO_TYPE_BOOLEAN:
881                 case MONO_TYPE_CHAR:
882                 case MONO_TYPE_I1:
883                 case MONO_TYPE_U1:
884                 case MONO_TYPE_I2:
885                 case MONO_TYPE_U2:
886                 case MONO_TYPE_I4:
887                 case MONO_TYPE_U4:
888                 case MONO_TYPE_I:
889                 case MONO_TYPE_U:
890                 case MONO_TYPE_PTR:
891                 case MONO_TYPE_SZARRAY:
892                 case MONO_TYPE_CLASS:
893                 case MONO_TYPE_OBJECT:
894                 case MONO_TYPE_STRING:
895                 case MONO_TYPE_VALUETYPE:
896                 case MONO_TYPE_GENERICINST:
897 #if SIZEOF_VOID_P == 8
898                 case MONO_TYPE_I8:
899 #endif
900                         margs->iargs [int_i] = frame->stack_args [i].data.p;
901 #if DEBUG_INTERP
902                         g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
903 #endif
904                         int_i++;
905                         break;
906 #if SIZEOF_VOID_P == 4
907                 case MONO_TYPE_I8: {
908                         stackval *sarg = &frame->stack_args [i];
909 #ifdef TARGET_ARM
910                         /* pairs begin at even registers */
911                         if (i8_align == 8 && int_i & 1)
912                                 int_i++;
913 #endif
914                         margs->iargs [int_i] = (gpointer) sarg->data.pair.lo;
915                         int_i++;
916                         margs->iargs [int_i] = (gpointer) sarg->data.pair.hi;
917 #if DEBUG_INTERP
918                         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);
919 #endif
920                         int_i++;
921                         break;
922                 }
923 #endif
924                 case MONO_TYPE_R4:
925                 case MONO_TYPE_R8:
926                         if (ptype == MONO_TYPE_R4)
927                                 * (float *) &(margs->fargs [int_f]) = (float) frame->stack_args [i].data.f;
928                         else
929                                 margs->fargs [int_f] = frame->stack_args [i].data.f;
930 #if DEBUG_INTERP
931                         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);
932 #endif
933 #if SIZEOF_VOID_P == 4
934                         int_f += 2;
935 #else
936                         int_f++;
937 #endif
938                         break;
939                 default:
940                         g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
941                 }
942         }
943
944         switch (sig->ret->type) {
945                 case MONO_TYPE_BOOLEAN:
946                 case MONO_TYPE_CHAR:
947                 case MONO_TYPE_I1:
948                 case MONO_TYPE_U1:
949                 case MONO_TYPE_I2:
950                 case MONO_TYPE_U2:
951                 case MONO_TYPE_I4:
952                 case MONO_TYPE_U4:
953                 case MONO_TYPE_I:
954                 case MONO_TYPE_U:
955                 case MONO_TYPE_PTR:
956                 case MONO_TYPE_SZARRAY:
957                 case MONO_TYPE_CLASS:
958                 case MONO_TYPE_OBJECT:
959                 case MONO_TYPE_STRING:
960                 case MONO_TYPE_I8:
961                 case MONO_TYPE_VALUETYPE:
962                 case MONO_TYPE_GENERICINST:
963                         margs->retval = &(frame->retval->data.p);
964                         margs->is_float_ret = 0;
965                         break;
966                 case MONO_TYPE_R4:
967                 case MONO_TYPE_R8:
968                         margs->retval = &(frame->retval->data.p);
969                         margs->is_float_ret = 1;
970                         break;
971                 case MONO_TYPE_VOID:
972                         margs->retval = NULL;
973                         break;
974                 default:
975                         g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
976         }
977
978         return margs;
979 }
980
981 static void 
982 ves_pinvoke_method (InterpFrame *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
983 {
984         jmp_buf env;
985         InterpFrame *old_frame = context->current_frame;
986         InterpFrame *old_env_frame = context->env_frame;
987         jmp_buf *old_env = context->current_env;
988         MonoLMFExt ext;
989
990         if (setjmp (env)) {
991                 context->current_frame = old_frame;
992                 context->env_frame = old_env_frame;
993                 context->current_env = old_env;
994                 context->managed_code = 1;
995                 return;
996         }
997
998         frame->ex = NULL;
999         context->env_frame = frame;
1000         context->current_env = &env;
1001
1002         g_assert (!frame->imethod);
1003         if (!mono_interp_enter_icall_trampoline) {
1004                 if (mono_aot_only) {
1005                         mono_interp_enter_icall_trampoline = mono_aot_get_trampoline ("enter_icall_trampoline");
1006                 } else {
1007                         MonoTrampInfo *info;
1008                         mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
1009                         // TODO:
1010                         // mono_tramp_info_register (info, NULL);
1011                 }
1012         }
1013
1014         InterpMethodArguments *margs = build_args_from_sig (sig, frame);
1015 #if DEBUG_INTERP
1016         g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
1017         g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
1018 #endif
1019
1020         context->current_frame = frame;
1021         context->managed_code = 0;
1022
1023         interp_push_lmf (&ext, frame);
1024
1025         mono_interp_enter_icall_trampoline (addr, margs);
1026
1027         interp_pop_lmf (&ext);
1028
1029         context->managed_code = 1;
1030
1031         if (*mono_thread_interruption_request_flag ()) {
1032                 MonoException *exc = mono_thread_interruption_checkpoint ();
1033                 if (exc) {
1034                         frame->ex = exc;
1035                         context->search_for_handler = 1;
1036                 }
1037         }
1038         
1039         if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
1040                 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
1041
1042         context->current_frame = old_frame;
1043         context->env_frame = old_env_frame;
1044         context->current_env = old_env;
1045
1046         g_free (margs->iargs);
1047         g_free (margs->fargs);
1048         g_free (margs);
1049 }
1050
1051 void
1052 mono_interp_init_delegate (MonoDelegate *del)
1053 {
1054         if (del->method)
1055                 return;
1056         /* shouldn't need a write barrier because we don't write a MonoObject into the field */
1057         del->method = ((InterpMethod *) del->method_ptr)->method;
1058 }
1059
1060 /*
1061  * From the spec:
1062  * runtime specifies that the implementation of the method is automatically
1063  * provided by the runtime and is primarily used for the methods of delegates.
1064  */
1065 static void
1066 ves_imethod (InterpFrame *frame, ThreadContext *context)
1067 {
1068         MonoMethod *method = frame->imethod->method;
1069         const char *name = method->name;
1070         MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
1071         MonoObject *isinst_obj;
1072         MonoError error;
1073
1074         mono_class_init (method->klass);
1075
1076         if (method->klass == mono_defaults.array_class) {
1077                 if (!strcmp (method->name, "UnsafeMov")) {
1078                         /* TODO: layout checks */
1079                         MonoType *mt = mono_method_signature (method)->ret;
1080                         stackval_from_data (mt, frame->retval, (char *) frame->stack_args, FALSE);
1081                         return;
1082                 }
1083                 if (!strcmp (method->name, "UnsafeLoad")) {
1084                         ves_array_get (frame, FALSE);
1085                         return;
1086                 }
1087         }
1088
1089         isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
1090         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1091         if (obj && isinst_obj) {
1092                 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
1093                         ves_array_set (frame);
1094                         return;
1095                 }
1096                 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
1097                         ves_array_get (frame, TRUE);
1098                         return;
1099                 }
1100         }
1101         
1102         g_error ("Don't know how to exec runtime method %s.%s::%s", 
1103                         method->klass->name_space, method->klass->name,
1104                         method->name);
1105 }
1106
1107 #if DEBUG_INTERP
1108 static char*
1109 dump_stack (stackval *stack, stackval *sp)
1110 {
1111         stackval *s = stack;
1112         GString *str = g_string_new ("");
1113         
1114         if (sp == stack)
1115                 return g_string_free (str, FALSE);
1116         
1117         while (s < sp) {
1118                 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
1119                 ++s;
1120         }
1121         return g_string_free (str, FALSE);
1122 }
1123 #endif
1124
1125 static void
1126 dump_stackval (GString *str, stackval *s, MonoType *type)
1127 {
1128         switch (type->type) {
1129         case MONO_TYPE_I1:
1130         case MONO_TYPE_U1:
1131         case MONO_TYPE_I2:
1132         case MONO_TYPE_U2:
1133         case MONO_TYPE_I4:
1134         case MONO_TYPE_U4:
1135         case MONO_TYPE_CHAR:
1136         case MONO_TYPE_BOOLEAN:
1137                 g_string_append_printf (str, "[%d] ", s->data.i);
1138                 break;
1139         case MONO_TYPE_STRING:
1140         case MONO_TYPE_SZARRAY:
1141         case MONO_TYPE_CLASS:
1142         case MONO_TYPE_OBJECT:
1143         case MONO_TYPE_ARRAY:
1144         case MONO_TYPE_PTR:
1145         case MONO_TYPE_I:
1146         case MONO_TYPE_U:
1147                 g_string_append_printf (str, "[%p] ", s->data.p);
1148                 break;
1149         case MONO_TYPE_VALUETYPE:
1150                 if (type->data.klass->enumtype)
1151                         g_string_append_printf (str, "[%d] ", s->data.i);
1152                 else
1153                         g_string_append_printf (str, "[vt:%p] ", s->data.p);
1154                 break;
1155         case MONO_TYPE_R4:
1156         case MONO_TYPE_R8:
1157                 g_string_append_printf (str, "[%g] ", s->data.f);
1158                 break;
1159         case MONO_TYPE_I8:
1160         case MONO_TYPE_U8:
1161         default: {
1162                 GString *res = g_string_new ("");
1163                 mono_type_get_desc (res, type, TRUE);
1164                 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1165                 g_string_free (res, TRUE);
1166                 break;
1167         }
1168         }
1169 }
1170
1171 #if DEBUG_INTERP
1172 static char*
1173 dump_retval (InterpFrame *inv)
1174 {
1175         GString *str = g_string_new ("");
1176         MonoType *ret = mono_method_signature (inv->imethod->method)->ret;
1177
1178         if (ret->type != MONO_TYPE_VOID)
1179                 dump_stackval (str, inv->retval, ret);
1180
1181         return g_string_free (str, FALSE);
1182 }
1183 #endif
1184
1185 static char*
1186 dump_args (InterpFrame *inv)
1187 {
1188         GString *str = g_string_new ("");
1189         int i;
1190         MonoMethodSignature *signature = mono_method_signature (inv->imethod->method);
1191         
1192         if (signature->param_count == 0 && !signature->hasthis)
1193                 return g_string_free (str, FALSE);
1194
1195         if (signature->hasthis) {
1196                 MonoMethod *method = inv->imethod->method;
1197                 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1198         }
1199
1200         for (i = 0; i < signature->param_count; ++i)
1201                 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1202
1203         return g_string_free (str, FALSE);
1204 }
1205  
1206 static char*
1207 dump_frame (InterpFrame *inv)
1208 {
1209         GString *str = g_string_new ("");
1210         int i;
1211         char *args;
1212         MonoError error;
1213
1214         for (i = 0; inv; inv = inv->parent) {
1215                 if (inv->imethod != NULL) {
1216                         MonoMethod *method = inv->imethod->method;
1217                         MonoClass *k;
1218
1219                         int codep = 0;
1220                         const char * opname = "";
1221                         char *name;
1222                         gchar *source = NULL;
1223
1224                         k = method->klass;
1225
1226                         if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1227                                 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1228                                 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1229                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1230
1231                                 if (hd != NULL) {
1232                                         if (inv->ip) {
1233                                                 opname = mono_interp_opname [*inv->ip];
1234                                                 codep = inv->ip - inv->imethod->code;
1235                                                 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1236                                         } else 
1237                                                 opname = "";
1238
1239 #if 0
1240                                         MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1241                                         source = mono_debug_method_lookup_location (minfo, codep);
1242 #endif
1243                                 }
1244                         }
1245                         args = dump_args (inv);
1246                         name = mono_method_full_name (method, TRUE);
1247                         if (source)
1248                                 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1249                         else
1250                                 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1251                         g_free (name);
1252                         g_free (args);
1253                         g_free (source);
1254                         ++i;
1255                 }
1256         }
1257         return g_string_free (str, FALSE);
1258 }
1259
1260 static MonoArray *
1261 get_trace_ips (MonoDomain *domain, InterpFrame *top)
1262 {
1263         int i;
1264         MonoArray *res;
1265         InterpFrame *inv;
1266         MonoError error;
1267
1268         for (i = 0, inv = top; inv; inv = inv->parent)
1269                 if (inv->imethod != NULL)
1270                         ++i;
1271
1272         res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1273         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1274
1275         for (i = 0, inv = top; inv; inv = inv->parent)
1276                 if (inv->imethod != NULL) {
1277                         mono_array_set (res, gpointer, i, inv->imethod);
1278                         ++i;
1279                         mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1280                         ++i;
1281                 }
1282
1283         return res;
1284 }
1285
1286
1287 #define MYGUINT64_MAX 18446744073709551615ULL
1288 #define MYGINT64_MAX 9223372036854775807LL
1289 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1290
1291 #define MYGUINT32_MAX 4294967295U
1292 #define MYGINT32_MAX 2147483647
1293 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1294         
1295 #define CHECK_ADD_OVERFLOW(a,b) \
1296         (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0  \
1297         : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1298
1299 #define CHECK_SUB_OVERFLOW(a,b) \
1300         (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0   \
1301         : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1302
1303 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1304         (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1305
1306 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1307         (guint32)(a) < (guint32)(b) ? -1 : 0
1308
1309 #define CHECK_ADD_OVERFLOW64(a,b) \
1310         (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0  \
1311         : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1312
1313 #define CHECK_SUB_OVERFLOW64(a,b) \
1314         (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0   \
1315         : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1316
1317 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1318         (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1319
1320 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1321         (guint64)(a) < (guint64)(b) ? -1 : 0
1322
1323 #if SIZEOF_VOID_P == 4
1324 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1325 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1326 #else
1327 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1328 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1329 #endif
1330
1331 /* Resolves to TRUE if the operands would overflow */
1332 #define CHECK_MUL_OVERFLOW(a,b) \
1333         ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1334         (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1335         (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1336         (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1337         (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1338         (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1339         (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1340
1341 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1342         ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1343         (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1344
1345 #define CHECK_MUL_OVERFLOW64(a,b) \
1346         ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1347         (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1348         (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1349         (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1350         (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1351         (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1352         (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1353
1354 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1355         ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1356         (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1357
1358 #if SIZEOF_VOID_P == 4
1359 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1360 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1361 #else
1362 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1363 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1364 #endif
1365
1366 MonoObject*
1367 mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1368 {
1369         InterpFrame frame;
1370         ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1371         MonoObject *retval = NULL;
1372         MonoMethodSignature *sig = mono_method_signature (method);
1373         MonoClass *klass = mono_class_from_mono_type (sig->ret);
1374         stackval result;
1375         stackval *args;
1376         ThreadContext context_struct;
1377         InterpFrame *old_frame = NULL;
1378         jmp_buf env;
1379
1380         error_init (error);
1381         if (exc)
1382                 *exc = NULL;
1383
1384         frame.ex = NULL;
1385
1386         if (setjmp(env)) {
1387                 if (context != &context_struct) {
1388                         context->current_frame = old_frame;
1389                         context->managed_code = 0;
1390                 } else
1391                         set_context (NULL);
1392                 if (exc != NULL)
1393                         *exc = (MonoObject *)frame.ex;
1394                 return retval;
1395         }
1396
1397         if (context == NULL) {
1398                 context = &context_struct;
1399                 memset (context, 0, sizeof (ThreadContext));
1400                 context_struct.base_frame = &frame;
1401                 context_struct.env_frame = &frame;
1402                 context_struct.current_env = &env;
1403                 set_context (context);
1404         }
1405         else
1406                 old_frame = context->current_frame;
1407
1408         MonoDomain *domain = mono_domain_get ();
1409
1410         MonoMethod *invoke_wrapper = mono_marshal_get_runtime_invoke_full (method, FALSE, TRUE);
1411
1412         //* <code>MonoObject *runtime_invoke (MonoObject *this_obj, void **params, MonoObject **exc, void* method)</code>
1413
1414         result.data.vt = alloca (mono_class_instance_size (klass));
1415         args = alloca (sizeof (stackval) * 4);
1416
1417         if (sig->hasthis)
1418                 args [0].data.p = obj;
1419         else
1420                 args [0].data.p = NULL;
1421         args [1].data.p = params;
1422         args [2].data.p = exc;
1423         args [3].data.p = method;
1424
1425         INIT_FRAME (&frame, context->current_frame, args, &result, domain, invoke_wrapper, error);
1426
1427         if (exc)
1428                 frame.invoke_trap = 1;
1429         context->managed_code = 1;
1430         ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1431         context->managed_code = 0;
1432         if (context == &context_struct)
1433                 set_context (NULL);
1434         else
1435                 context->current_frame = old_frame;
1436         if (frame.ex != NULL) {
1437                 if (exc != NULL) {
1438                         *exc = (MonoObject*) frame.ex;
1439                         return NULL;
1440                 }
1441                 if (context->current_env != NULL) {
1442                         context->env_frame->ex = frame.ex;
1443                         longjmp(*context->current_env, 1);
1444                 }
1445                 else
1446                         printf("dropped exception...\n");
1447         }
1448         return result.data.p;
1449 }
1450
1451 typedef struct {
1452         InterpMethod *rmethod;
1453         gpointer this_arg;
1454         gpointer res;
1455         gpointer args [16];
1456         gpointer *many_args;
1457 } InterpEntryData;
1458
1459 /* Main function for entering the interpreter from compiled code */
1460 static void
1461 interp_entry (InterpEntryData *data)
1462 {
1463         InterpFrame frame;
1464         InterpMethod *rmethod = data->rmethod;
1465         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
1466         ThreadContext context_struct;
1467         InterpFrame *old_frame;
1468         stackval result;
1469         stackval *args;
1470         MonoMethod *method;
1471         MonoMethodSignature *sig;
1472         MonoType *type;
1473         int i;
1474
1475         method = rmethod->method;
1476         sig = mono_method_signature (method);
1477
1478         // FIXME: Optimize this
1479
1480         //printf ("%s\n", mono_method_full_name (method, 1));
1481
1482         frame.ex = NULL;
1483         if (context == NULL) {
1484                 context = &context_struct;
1485                 memset (context, 0, sizeof (ThreadContext));
1486                 context_struct.base_frame = &frame;
1487                 context_struct.env_frame = &frame;
1488                 set_context (context);
1489         } else {
1490                 old_frame = context->current_frame;
1491         }
1492
1493         args = alloca (sizeof (stackval) * (sig->param_count + (sig->hasthis ? 1 : 0)));
1494         if (sig->hasthis)
1495                 args [0].data.p = data->this_arg;
1496
1497         gpointer *params;
1498         if (data->many_args)
1499                 params = data->many_args;
1500         else
1501                 params = data->args;
1502         for (i = 0; i < sig->param_count; ++i) {
1503                 int a_index = i + (sig->hasthis ? 1 : 0);
1504                 if (sig->params [i]->byref) {
1505                         args [a_index].data.p = params [i];
1506                         continue;
1507                 }
1508                 type = rmethod->param_types [i];
1509                 switch (type->type) {
1510                 case MONO_TYPE_U1:
1511                 case MONO_TYPE_I1:
1512                         args [a_index].data.i = *(MonoBoolean*)params [i];
1513                         break;
1514                 case MONO_TYPE_U2:
1515                 case MONO_TYPE_I2:
1516                         args [a_index].data.i = *(gint16*)params [i];
1517                         break;
1518                 case MONO_TYPE_U:
1519 #if SIZEOF_VOID_P == 4
1520                         args [a_index].data.p = GINT_TO_POINTER (*(guint32*)params [i]);
1521 #else
1522                         args [a_index].data.p = GINT_TO_POINTER (*(guint64*)params [i]);
1523 #endif
1524                         break;
1525                 case MONO_TYPE_I:
1526 #if SIZEOF_VOID_P == 4
1527                         args [a_index].data.p = GINT_TO_POINTER (*(gint32*)params [i]);
1528 #else
1529                         args [a_index].data.p = GINT_TO_POINTER (*(gint64*)params [i]);
1530 #endif
1531                         break;
1532                 case MONO_TYPE_U4:
1533                         args [a_index].data.i = *(guint32*)params [i];
1534                         break;
1535                 case MONO_TYPE_I4:
1536                         args [a_index].data.i = *(gint32*)params [i];
1537                         break;
1538                 case MONO_TYPE_U8:
1539                         args [a_index].data.l = *(guint64*)params [i];
1540                         break;
1541                 case MONO_TYPE_I8:
1542                         args [a_index].data.l = *(gint64*)params [i];
1543                         break;
1544                 case MONO_TYPE_PTR:
1545                 case MONO_TYPE_OBJECT:
1546                         args [a_index].data.p = *(MonoObject**)params [i];
1547                         break;
1548                 case MONO_TYPE_VALUETYPE:
1549                         args [a_index].data.p = params [i];
1550                         break;
1551                 case MONO_TYPE_GENERICINST:
1552                         if (MONO_TYPE_IS_REFERENCE (type))
1553                                 args [a_index].data.p = params [i];
1554                         else
1555                                 args [a_index].data.vt = params [i];
1556                         break;
1557                 default:
1558                         printf ("%s\n", mono_type_full_name (sig->params [i]));
1559                         NOT_IMPLEMENTED;
1560                         break;
1561                 }
1562         }
1563
1564         init_frame (&frame, NULL, data->rmethod, args, &result);
1565         context->managed_code = 1;
1566
1567         type = rmethod->rtype;
1568         switch (type->type) {
1569         case MONO_TYPE_GENERICINST:
1570                 if (!MONO_TYPE_IS_REFERENCE (type))
1571                         frame.retval->data.vt = data->res;
1572                 break;
1573         case MONO_TYPE_VALUETYPE:
1574                 frame.retval->data.vt = data->res;
1575                 break;
1576         default:
1577                 break;
1578         }
1579
1580         ves_exec_method_with_context (&frame, context, NULL, NULL, -1);
1581         context->managed_code = 0;
1582         if (context == &context_struct)
1583                 set_context (NULL);
1584         else
1585                 context->current_frame = old_frame;
1586
1587         // FIXME:
1588         g_assert (frame.ex == NULL);
1589
1590         type = rmethod->rtype;
1591         switch (type->type) {
1592         case MONO_TYPE_VOID:
1593                 break;
1594         case MONO_TYPE_I1:
1595                 *(gint8*)data->res = frame.retval->data.i;
1596                 break;
1597         case MONO_TYPE_U1:
1598                 *(guint8*)data->res = frame.retval->data.i;
1599                 break;
1600         case MONO_TYPE_I2:
1601                 *(gint16*)data->res = frame.retval->data.i;
1602                 break;
1603         case MONO_TYPE_U2:
1604                 *(guint16*)data->res = frame.retval->data.i;
1605                 break;
1606         case MONO_TYPE_I4:
1607                 *(gint32*)data->res = frame.retval->data.i;
1608                 break;
1609         case MONO_TYPE_U4:
1610                 *(guint64*)data->res = frame.retval->data.i;
1611                 break;
1612         case MONO_TYPE_I8:
1613                 *(gint64*)data->res = frame.retval->data.i;
1614                 break;
1615         case MONO_TYPE_U8:
1616                 *(guint64*)data->res = frame.retval->data.i;
1617                 break;
1618         case MONO_TYPE_I:
1619 #if SIZEOF_VOID_P == 8
1620                 *(gint64*)data->res = (gint64)frame.retval->data.p;
1621 #else
1622                 *(gint32*)data->res = (gint32)frame.retval->data.p;
1623 #endif
1624                 break;
1625         case MONO_TYPE_U:
1626 #if SIZEOF_VOID_P == 8
1627                 *(guint64*)data->res = (guint64)frame.retval->data.p;
1628 #else
1629                 *(guint32*)data->res = (guint32)frame.retval->data.p;
1630 #endif
1631                 break;
1632         case MONO_TYPE_OBJECT:
1633                 /* No need for a write barrier */
1634                 *(MonoObject**)data->res = (MonoObject*)frame.retval->data.p;
1635                 break;
1636         case MONO_TYPE_GENERICINST:
1637                 if (MONO_TYPE_IS_REFERENCE (type)) {
1638                         *(MonoObject**)data->res = *(MonoObject**)frame.retval->data.p;
1639                 } else {
1640                         /* Already set before the call */
1641                 }
1642                 break;
1643         case MONO_TYPE_VALUETYPE:
1644                 /* Already set before the call */
1645                 break;
1646         default:
1647                 printf ("%s\n", mono_type_full_name (sig->ret));
1648                 NOT_IMPLEMENTED;
1649                 break;
1650         }
1651 }
1652
1653 static stackval * 
1654 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1655 {
1656         InterpFrame *old_frame = context->current_frame;
1657         InterpFrame *old_env_frame = context->env_frame;
1658         jmp_buf *old_env = context->current_env;
1659         jmp_buf env;
1660
1661         if (setjmp (env)) {
1662                 context->current_frame = old_frame;
1663                 context->env_frame = old_env_frame;
1664                 context->current_env = old_env;
1665                 context->managed_code = 1;
1666                 return sp;
1667         }
1668
1669         context->env_frame = context->current_frame;
1670         context->current_env = &env;
1671         context->managed_code = 0;
1672
1673         switch (op) {
1674         case MINT_ICALL_V_V: {
1675                 void (*func)(void) = ptr;
1676                 func ();
1677                 break;
1678         }
1679         case MINT_ICALL_V_P: {
1680                 gpointer (*func)(void) = ptr;
1681                 sp++;
1682                 sp [-1].data.p = func ();
1683                 break;
1684         }
1685         case MINT_ICALL_P_V: {
1686                 void (*func)(gpointer) = ptr;
1687                 func (sp [-1].data.p);
1688                 sp --;
1689                 break;
1690         }
1691         case MINT_ICALL_P_P: {
1692                 gpointer (*func)(gpointer) = ptr;
1693                 sp [-1].data.p = func (sp [-1].data.p);
1694                 break;
1695         }
1696         case MINT_ICALL_PP_V: {
1697                 void (*func)(gpointer,gpointer) = ptr;
1698                 sp -= 2;
1699                 func (sp [0].data.p, sp [1].data.p);
1700                 break;
1701         }
1702         case MINT_ICALL_PI_V: {
1703                 void (*func)(gpointer,int) = ptr;
1704                 sp -= 2;
1705                 func (sp [0].data.p, sp [1].data.i);
1706                 break;
1707         }
1708         case MINT_ICALL_PP_P: {
1709                 gpointer (*func)(gpointer,gpointer) = ptr;
1710                 --sp;
1711                 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1712                 break;
1713         }
1714         case MINT_ICALL_PI_P: {
1715                 gpointer (*func)(gpointer,int) = ptr;
1716                 --sp;
1717                 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1718                 break;
1719         }
1720         case MINT_ICALL_PPP_V: {
1721                 void (*func)(gpointer,gpointer,gpointer) = ptr;
1722                 sp -= 3;
1723                 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1724                 break;
1725         }
1726         case MINT_ICALL_PPI_V: {
1727                 void (*func)(gpointer,gpointer,int) = ptr;
1728                 sp -= 3;
1729                 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1730                 break;
1731         }
1732         default:
1733                 g_assert_not_reached ();
1734         }
1735
1736         context->env_frame = old_env_frame;
1737         context->current_env = old_env;
1738
1739         return sp;
1740 }
1741
1742 static stackval *
1743 do_jit_call (stackval *sp, unsigned char *vt_sp, ThreadContext *context, InterpFrame *frame, InterpMethod *rmethod)
1744 {
1745         MonoMethodSignature *sig;
1746         MonoFtnDesc ftndesc;
1747         guint8 res_buf [256];
1748         MonoType *type;
1749         MonoLMFExt ext;
1750
1751         //printf ("%s\n", mono_method_full_name (rmethod->method, 1));
1752
1753         /*
1754          * Call JITted code through a gsharedvt_out wrapper. These wrappers receive every argument
1755          * by ref and return a return value using an explicit return value argument.
1756          */
1757         if (!rmethod->jit_wrapper) {
1758                 MonoMethod *method = rmethod->method;
1759                 MonoError error;
1760
1761                 sig = mono_method_signature (method);
1762                 g_assert (sig);
1763
1764                 MonoMethod *wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
1765                 //printf ("J: %s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
1766
1767                 gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, &error);
1768                 mono_error_assert_ok (&error);
1769
1770                 gpointer addr = mono_jit_compile_method_jit_only (method, &error);
1771                 g_assert (addr);
1772                 mono_error_assert_ok (&error);
1773
1774                 rmethod->jit_addr = addr;
1775                 rmethod->jit_sig = sig;
1776                 mono_memory_barrier ();
1777                 rmethod->jit_wrapper = jit_wrapper;
1778
1779         } else {
1780                 sig = rmethod->jit_sig;
1781         }
1782
1783         sp -= sig->param_count;
1784         if (sig->hasthis)
1785                 --sp;
1786
1787         ftndesc.addr = rmethod->jit_addr;
1788         ftndesc.arg = NULL;
1789
1790         // FIXME: Optimize this
1791
1792         gpointer args [32];
1793         int pindex = 0;
1794         int stack_index = 0;
1795         if (rmethod->hasthis) {
1796                 args [pindex ++] = sp [0].data.p;
1797                 stack_index ++;
1798         }
1799         type = rmethod->rtype;
1800         if (type->type != MONO_TYPE_VOID) {
1801                 if (MONO_TYPE_ISSTRUCT (type))
1802                         args [pindex ++] = vt_sp;
1803                 else
1804                         args [pindex ++] = res_buf;
1805         }
1806         for (int i = 0; i < rmethod->param_count; ++i) {
1807                 MonoType *t = rmethod->param_types [i];
1808                 stackval *sval = &sp [stack_index + i];
1809                 if (sig->params [i]->byref) {
1810                         args [pindex ++] = sval->data.p;
1811                 } else if (MONO_TYPE_ISSTRUCT (t)) {
1812                         args [pindex ++] = sval->data.p;
1813                 } else if (MONO_TYPE_IS_REFERENCE (t)) {
1814                         args [pindex ++] = &sval->data.p;
1815                 } else {
1816                         switch (t->type) {
1817                         case MONO_TYPE_I1:
1818                         case MONO_TYPE_U1:
1819                         case MONO_TYPE_I2:
1820                         case MONO_TYPE_U2:
1821                         case MONO_TYPE_I4:
1822                         case MONO_TYPE_U4:
1823                         case MONO_TYPE_VALUETYPE:
1824                                 args [pindex ++] = &sval->data.i;
1825                                 break;
1826                         case MONO_TYPE_PTR:
1827                         case MONO_TYPE_FNPTR:
1828                         case MONO_TYPE_I:
1829                         case MONO_TYPE_U:
1830                         case MONO_TYPE_OBJECT:
1831                                 args [pindex ++] = &sval->data.p;
1832                                 break;
1833                         case MONO_TYPE_I8:
1834                         case MONO_TYPE_U8:
1835                                 args [pindex ++] = &sval->data.l;
1836                                 break;
1837                         default:
1838                                 printf ("%s\n", mono_type_full_name (t));
1839                                 g_assert_not_reached ();
1840                         }
1841                 }
1842         }
1843
1844         interp_push_lmf (&ext, frame);
1845
1846         switch (pindex) {
1847         case 0: {
1848                 void (*func)(gpointer) = rmethod->jit_wrapper;
1849
1850                 func (&ftndesc);
1851                 break;
1852         }
1853         case 1: {
1854                 void (*func)(gpointer, gpointer) = rmethod->jit_wrapper;
1855
1856                 func (args [0], &ftndesc);
1857                 break;
1858         }
1859         case 2: {
1860                 void (*func)(gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1861
1862                 func (args [0], args [1], &ftndesc);
1863                 break;
1864         }
1865         case 3: {
1866                 void (*func)(gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1867
1868                 func (args [0], args [1], args [2], &ftndesc);
1869                 break;
1870         }
1871         case 4: {
1872                 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1873
1874                 func (args [0], args [1], args [2], args [3], &ftndesc);
1875                 break;
1876         }
1877         case 5: {
1878                 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1879
1880                 func (args [0], args [1], args [2], args [3], args [4], &ftndesc);
1881                 break;
1882         }
1883         case 6: {
1884                 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1885
1886                 func (args [0], args [1], args [2], args [3], args [4], args [5], &ftndesc);
1887                 break;
1888         }
1889         case 7: {
1890                 void (*func)(gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer, gpointer) = rmethod->jit_wrapper;
1891
1892                 func (args [0], args [1], args [2], args [3], args [4], args [5], args [6], &ftndesc);
1893                 break;
1894         }
1895         default:
1896                 g_assert_not_reached ();
1897                 break;
1898         }
1899
1900         interp_pop_lmf (&ext);
1901
1902         MonoType *rtype = rmethod->rtype;
1903         switch (rtype->type) {
1904         case MONO_TYPE_VOID:
1905         case MONO_TYPE_OBJECT:
1906         case MONO_TYPE_STRING:
1907         case MONO_TYPE_CLASS:
1908         case MONO_TYPE_ARRAY:
1909         case MONO_TYPE_SZARRAY:
1910         case MONO_TYPE_I:
1911         case MONO_TYPE_U:
1912                 sp->data.p = *(gpointer*)res_buf;
1913                 break;
1914         case MONO_TYPE_I1:
1915                 sp->data.i = *(gint8*)res_buf;
1916                 break;
1917         case MONO_TYPE_U1:
1918                 sp->data.i = *(guint8*)res_buf;
1919                 break;
1920         case MONO_TYPE_I2:
1921                 sp->data.i = *(gint16*)res_buf;
1922                 break;
1923         case MONO_TYPE_U2:
1924                 sp->data.i = *(guint16*)res_buf;
1925                 break;
1926         case MONO_TYPE_I4:
1927                 sp->data.i = *(gint32*)res_buf;
1928                 break;
1929         case MONO_TYPE_U4:
1930                 sp->data.i = *(guint32*)res_buf;
1931                 break;
1932         case MONO_TYPE_VALUETYPE:
1933                 /* The result was written to vt_sp */
1934                 sp->data.p = vt_sp;
1935                 break;
1936         case MONO_TYPE_GENERICINST:
1937                 if (MONO_TYPE_IS_REFERENCE (rtype)) {
1938                         sp->data.p = *(gpointer*)res_buf;
1939                 } else {
1940                         /* The result was written to vt_sp */
1941                         sp->data.p = vt_sp;
1942                 }
1943                 break;
1944         default:
1945                 printf ("%s\n", mono_type_full_name (rtype));
1946                 g_assert_not_reached ();
1947                 break;
1948         }
1949
1950         return sp;
1951 }
1952
1953 static void
1954 do_debugger_tramp (void (*tramp) (void), InterpFrame *frame)
1955 {
1956         MonoLMFExt ext;
1957         interp_push_lmf (&ext, frame);
1958         tramp ();
1959         interp_pop_lmf (&ext);
1960 }
1961
1962 static void
1963 do_transform_method (InterpFrame *frame, ThreadContext *context)
1964 {
1965         MonoLMFExt ext;
1966
1967         /* Use the parent frame as the current frame is not complete yet */
1968         interp_push_lmf (&ext, frame->parent);
1969
1970         frame->ex = mono_interp_transform_method (frame->imethod, context);
1971         context->managed_code = 1;
1972
1973         interp_pop_lmf (&ext);
1974 }
1975
1976 /*
1977  * These functions are the entry points into the interpreter from compiled code.
1978  * They are called by the interp_in wrappers. They have the following signature:
1979  * void (<optional this_arg>, <optional retval pointer>, <arg1>, ..., <argn>, <method ptr>)
1980  * They pack up their arguments into an InterpEntryData structure and call interp_entry ().
1981  * It would be possible for the wrappers to pack up the arguments etc, but that would make them bigger, and there are
1982  * more wrappers then these functions.
1983  * this/static * ret/void * 16 arguments -> 64 functions.
1984  */
1985
1986 #define MAX_INTERP_ENTRY_ARGS 8
1987
1988 #define INTERP_ENTRY_BASE(_method, _this_arg, _res) \
1989         InterpEntryData data; \
1990         (data).rmethod = (_method); \
1991         (data).res = (_res); \
1992         (data).this_arg = (_this_arg); \
1993         (data).many_args = NULL;
1994
1995 #define INTERP_ENTRY0(_this_arg, _res, _method) {       \
1996         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
1997         interp_entry (&data); \
1998         }
1999 #define INTERP_ENTRY1(_this_arg, _res, _method) {         \
2000         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2001         (data).args [0] = arg1; \
2002         interp_entry (&data); \
2003         }
2004 #define INTERP_ENTRY2(_this_arg, _res, _method) {  \
2005         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2006         (data).args [0] = arg1; \
2007         (data).args [1] = arg2; \
2008         interp_entry (&data); \
2009         }
2010 #define INTERP_ENTRY3(_this_arg, _res, _method) { \
2011         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2012         (data).args [0] = arg1; \
2013         (data).args [1] = arg2; \
2014         (data).args [2] = arg3; \
2015         interp_entry (&data); \
2016         }
2017 #define INTERP_ENTRY4(_this_arg, _res, _method) {       \
2018         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2019         (data).args [0] = arg1; \
2020         (data).args [1] = arg2; \
2021         (data).args [2] = arg3; \
2022         (data).args [3] = arg4; \
2023         interp_entry (&data); \
2024         }
2025 #define INTERP_ENTRY5(_this_arg, _res, _method) {       \
2026         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2027         (data).args [0] = arg1; \
2028         (data).args [1] = arg2; \
2029         (data).args [2] = arg3; \
2030         (data).args [3] = arg4; \
2031         (data).args [4] = arg5; \
2032         interp_entry (&data); \
2033         }
2034 #define INTERP_ENTRY6(_this_arg, _res, _method) {       \
2035         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2036         (data).args [0] = arg1; \
2037         (data).args [1] = arg2; \
2038         (data).args [2] = arg3; \
2039         (data).args [3] = arg4; \
2040         (data).args [4] = arg5; \
2041         (data).args [5] = arg6; \
2042         interp_entry (&data); \
2043         }
2044 #define INTERP_ENTRY7(_this_arg, _res, _method) {       \
2045         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2046         (data).args [0] = arg1; \
2047         (data).args [1] = arg2; \
2048         (data).args [2] = arg3; \
2049         (data).args [3] = arg4; \
2050         (data).args [4] = arg5; \
2051         (data).args [5] = arg6; \
2052         (data).args [6] = arg7; \
2053         interp_entry (&data); \
2054         }
2055 #define INTERP_ENTRY8(_this_arg, _res, _method) {       \
2056         INTERP_ENTRY_BASE (_method, _this_arg, _res); \
2057         (data).args [0] = arg1; \
2058         (data).args [1] = arg2; \
2059         (data).args [2] = arg3; \
2060         (data).args [3] = arg4; \
2061         (data).args [4] = arg5; \
2062         (data).args [5] = arg6; \
2063         (data).args [6] = arg7; \
2064         (data).args [7] = arg8; \
2065         interp_entry (&data); \
2066         }
2067
2068 #define ARGLIST0 InterpMethod *rmethod
2069 #define ARGLIST1 gpointer arg1, InterpMethod *rmethod
2070 #define ARGLIST2 gpointer arg1, gpointer arg2, InterpMethod *rmethod
2071 #define ARGLIST3 gpointer arg1, gpointer arg2, gpointer arg3, InterpMethod *rmethod
2072 #define ARGLIST4 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, InterpMethod *rmethod
2073 #define ARGLIST5 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, InterpMethod *rmethod
2074 #define ARGLIST6 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, InterpMethod *rmethod
2075 #define ARGLIST7 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, InterpMethod *rmethod
2076 #define ARGLIST8 gpointer arg1, gpointer arg2, gpointer arg3, gpointer arg4, gpointer arg5, gpointer arg6, gpointer arg7, gpointer arg8, InterpMethod *rmethod
2077
2078 static void interp_entry_static_0 (ARGLIST0) INTERP_ENTRY0 (NULL, NULL, rmethod)
2079 static void interp_entry_static_1 (ARGLIST1) INTERP_ENTRY1 (NULL, NULL, rmethod)
2080 static void interp_entry_static_2 (ARGLIST2) INTERP_ENTRY2 (NULL, NULL, rmethod)
2081 static void interp_entry_static_3 (ARGLIST3) INTERP_ENTRY3 (NULL, NULL, rmethod)
2082 static void interp_entry_static_4 (ARGLIST4) INTERP_ENTRY4 (NULL, NULL, rmethod)
2083 static void interp_entry_static_5 (ARGLIST5) INTERP_ENTRY5 (NULL, NULL, rmethod)
2084 static void interp_entry_static_6 (ARGLIST6) INTERP_ENTRY6 (NULL, NULL, rmethod)
2085 static void interp_entry_static_7 (ARGLIST7) INTERP_ENTRY7 (NULL, NULL, rmethod)
2086 static void interp_entry_static_8 (ARGLIST8) INTERP_ENTRY8 (NULL, NULL, rmethod)
2087 static void interp_entry_static_ret_0 (gpointer res, ARGLIST0) INTERP_ENTRY0 (NULL, res, rmethod)
2088 static void interp_entry_static_ret_1 (gpointer res, ARGLIST1) INTERP_ENTRY1 (NULL, res, rmethod)
2089 static void interp_entry_static_ret_2 (gpointer res, ARGLIST2) INTERP_ENTRY2 (NULL, res, rmethod)
2090 static void interp_entry_static_ret_3 (gpointer res, ARGLIST3) INTERP_ENTRY3 (NULL, res, rmethod)
2091 static void interp_entry_static_ret_4 (gpointer res, ARGLIST4) INTERP_ENTRY4 (NULL, res, rmethod)
2092 static void interp_entry_static_ret_5 (gpointer res, ARGLIST5) INTERP_ENTRY5 (NULL, res, rmethod)
2093 static void interp_entry_static_ret_6 (gpointer res, ARGLIST6) INTERP_ENTRY6 (NULL, res, rmethod)
2094 static void interp_entry_static_ret_7 (gpointer res, ARGLIST7) INTERP_ENTRY7 (NULL, res, rmethod)
2095 static void interp_entry_static_ret_8 (gpointer res, ARGLIST8) INTERP_ENTRY8 (NULL, res, rmethod)
2096 static void interp_entry_instance_0 (gpointer this_arg, ARGLIST0) INTERP_ENTRY0 (this_arg, NULL, rmethod)
2097 static void interp_entry_instance_1 (gpointer this_arg, ARGLIST1) INTERP_ENTRY1 (this_arg, NULL, rmethod)
2098 static void interp_entry_instance_2 (gpointer this_arg, ARGLIST2) INTERP_ENTRY2 (this_arg, NULL, rmethod)
2099 static void interp_entry_instance_3 (gpointer this_arg, ARGLIST3) INTERP_ENTRY3 (this_arg, NULL, rmethod)
2100 static void interp_entry_instance_4 (gpointer this_arg, ARGLIST4) INTERP_ENTRY4 (this_arg, NULL, rmethod)
2101 static void interp_entry_instance_5 (gpointer this_arg, ARGLIST5) INTERP_ENTRY5 (this_arg, NULL, rmethod)
2102 static void interp_entry_instance_6 (gpointer this_arg, ARGLIST6) INTERP_ENTRY6 (this_arg, NULL, rmethod)
2103 static void interp_entry_instance_7 (gpointer this_arg, ARGLIST7) INTERP_ENTRY7 (this_arg, NULL, rmethod)
2104 static void interp_entry_instance_8 (gpointer this_arg, ARGLIST8) INTERP_ENTRY8 (this_arg, NULL, rmethod)
2105 static void interp_entry_instance_ret_0 (gpointer this_arg, gpointer res, ARGLIST0) INTERP_ENTRY0 (this_arg, res, rmethod)
2106 static void interp_entry_instance_ret_1 (gpointer this_arg, gpointer res, ARGLIST1) INTERP_ENTRY1 (this_arg, res, rmethod)
2107 static void interp_entry_instance_ret_2 (gpointer this_arg, gpointer res, ARGLIST2) INTERP_ENTRY2 (this_arg, res, rmethod)
2108 static void interp_entry_instance_ret_3 (gpointer this_arg, gpointer res, ARGLIST3) INTERP_ENTRY3 (this_arg, res, rmethod)
2109 static void interp_entry_instance_ret_4 (gpointer this_arg, gpointer res, ARGLIST4) INTERP_ENTRY4 (this_arg, res, rmethod)
2110 static void interp_entry_instance_ret_5 (gpointer this_arg, gpointer res, ARGLIST5) INTERP_ENTRY5 (this_arg, res, rmethod)
2111 static void interp_entry_instance_ret_6 (gpointer this_arg, gpointer res, ARGLIST6) INTERP_ENTRY6 (this_arg, res, rmethod)
2112 static void interp_entry_instance_ret_7 (gpointer this_arg, gpointer res, ARGLIST7) INTERP_ENTRY6 (this_arg, res, rmethod)
2113 static void interp_entry_instance_ret_8 (gpointer this_arg, gpointer res, ARGLIST8) INTERP_ENTRY6 (this_arg, res, rmethod)
2114
2115 #define INTERP_ENTRY_FUNCLIST(type) interp_entry_ ## type ## _0, interp_entry_ ## type ## _1, interp_entry_ ## type ## _2, interp_entry_ ## type ## _3, interp_entry_ ## type ## _4, interp_entry_ ## type ## _5, interp_entry_ ## type ## _6, interp_entry_ ## type ## _7, interp_entry_ ## type ## _8
2116
2117 gpointer entry_funcs_static [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static) };
2118 gpointer entry_funcs_static_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (static_ret) };
2119 gpointer entry_funcs_instance [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance) };
2120 gpointer entry_funcs_instance_ret [MAX_INTERP_ENTRY_ARGS + 1] = { INTERP_ENTRY_FUNCLIST (instance_ret) };
2121
2122 /* General version for methods with more than MAX_INTERP_ENTRY_ARGS arguments */
2123 static void
2124 interp_entry_general (gpointer this_arg, gpointer res, gpointer *args, gpointer rmethod)
2125 {
2126         INTERP_ENTRY_BASE (rmethod, this_arg, res);
2127         data.many_args = args;
2128         interp_entry (&data);
2129 }
2130
2131 /*
2132  * mono_interp_create_method_pointer:
2133  *
2134  * Return a function pointer which can be used to call METHOD using the
2135  * interpreter. Return NULL for methods which are not supported.
2136  */
2137 gpointer
2138 mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
2139 {
2140         gpointer addr;
2141         MonoMethodSignature *sig = mono_method_signature (method);
2142         MonoMethod *wrapper;
2143         InterpMethod *rmethod = mono_interp_get_imethod (mono_domain_get (), method, error);
2144
2145         /* HACK: method_ptr of delegate should point to a runtime method*/
2146         if (method->wrapper_type && method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD)
2147                 return rmethod;
2148
2149         if (rmethod->jit_entry)
2150                 return rmethod->jit_entry;
2151         wrapper = mini_get_interp_in_wrapper (sig);
2152
2153         gpointer jit_wrapper = mono_jit_compile_method_jit_only (wrapper, error);
2154         mono_error_assert_ok (error);
2155
2156         //printf ("%s %s\n", mono_method_full_name (method, 1), mono_method_full_name (wrapper, 1));
2157         gpointer entry_func;
2158         if (sig->param_count > MAX_INTERP_ENTRY_ARGS) {
2159                 entry_func = interp_entry_general;
2160         } else if (sig->hasthis) {
2161                 if (sig->ret->type == MONO_TYPE_VOID)
2162                         entry_func = entry_funcs_instance [sig->param_count];
2163                 else
2164                         entry_func = entry_funcs_instance_ret [sig->param_count];
2165         } else {
2166                 if (sig->ret->type == MONO_TYPE_VOID)
2167                         entry_func = entry_funcs_static [sig->param_count];
2168                 else
2169                         entry_func = entry_funcs_static_ret [sig->param_count];
2170         }
2171         g_assert (entry_func);
2172
2173         /* This is the argument passed to the interp_in wrapper by the static rgctx trampoline */
2174         MonoFtnDesc *ftndesc = g_new0 (MonoFtnDesc, 1);
2175         ftndesc->addr = entry_func;
2176         ftndesc->arg = rmethod;
2177         mono_error_assert_ok (error);
2178
2179         /*
2180          * The wrapper is called by compiled code, which doesn't pass the extra argument, so we pass it in the
2181          * rgctx register using a trampoline.
2182          */
2183
2184         if (mono_aot_only)
2185                 addr = mono_aot_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
2186         else
2187                 addr = mono_arch_get_static_rgctx_trampoline (ftndesc, jit_wrapper);
2188
2189         mono_memory_barrier ();
2190         rmethod->jit_entry = addr;
2191
2192         return addr;
2193 }
2194
2195 #if COUNT_OPS
2196 static int opcode_counts[512];
2197
2198 #define COUNT_OP(op) opcode_counts[op]++
2199 #else
2200 #define COUNT_OP(op) 
2201 #endif
2202
2203 #if DEBUG_INTERP
2204 #define DUMP_INSTR() \
2205         if (tracing > 1) { \
2206                 char *ins; \
2207                 if (sp > frame->stack) { \
2208                         ins = dump_stack (frame->stack, sp); \
2209                 } else { \
2210                         ins = g_strdup (""); \
2211                 } \
2212                 sp->data.l = 0; \
2213                 output_indent (); \
2214                 char *mn = mono_method_full_name (frame->imethod->method, FALSE); \
2215                 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
2216                 g_free (mn); \
2217                 mono_interp_dis_mintop(rtm->code, ip); \
2218                 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
2219                 g_free (ins); \
2220         }
2221 #else
2222 #define DUMP_INSTR()
2223 #endif
2224
2225 #ifdef __GNUC__
2226 #define USE_COMPUTED_GOTO 1
2227 #endif
2228 #if USE_COMPUTED_GOTO
2229 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
2230 #define MINT_IN_CASE(x) LAB_ ## x:
2231 #if DEBUG_INTERP
2232 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
2233 #else
2234 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
2235 #endif
2236 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
2237 #else
2238 #define MINT_IN_SWITCH(op) switch (op)
2239 #define MINT_IN_CASE(x) case x:
2240 #define MINT_IN_BREAK break
2241 #define MINT_IN_DEFAULT default:
2242 #endif
2243
2244 /*
2245  * If EXIT_AT_FINALLY is not -1, exit after exiting the finally clause with that index.
2246  */
2247 static void 
2248 ves_exec_method_with_context (InterpFrame *frame, ThreadContext *context, unsigned short *start_with_ip, MonoException *filter_exception, int exit_at_finally)
2249 {
2250         InterpFrame child_frame;
2251         GSList *finally_ips = NULL;
2252         const unsigned short *endfinally_ip = NULL;
2253         const unsigned short *ip = NULL;
2254         register stackval *sp;
2255         InterpMethod *rtm;
2256 #if DEBUG_INTERP
2257         gint tracing = global_tracing;
2258         unsigned char *vtalloc;
2259 #else
2260         gint tracing = 0;
2261 #endif
2262         int i32;
2263         unsigned char *vt_sp;
2264         unsigned char *locals;
2265         MonoError error;
2266         MonoObject *o = NULL;
2267         MonoClass *c;
2268 #if USE_COMPUTED_GOTO
2269         static void *in_labels[] = {
2270 #define OPDEF(a,b,c,d) \
2271         &&LAB_ ## a,
2272 #include "mintops.def"
2273         0 };
2274 #endif
2275
2276         frame->ex = NULL;
2277         frame->ex_handler = NULL;
2278         frame->ip = NULL;
2279         context->current_frame = frame;
2280
2281         debug_enter (frame, &tracing);
2282
2283         if (!frame->imethod->transformed) {
2284                 context->managed_code = 0;
2285 #if DEBUG_INTERP
2286                 char *mn = mono_method_full_name (frame->imethod->method, TRUE);
2287                 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
2288                 g_free (mn);
2289 #endif
2290
2291                 do_transform_method (frame, context);
2292                 if (frame->ex) {
2293                         rtm = NULL;
2294                         ip = NULL;
2295                         goto exit_frame;
2296                 }
2297         }
2298
2299         rtm = frame->imethod;
2300         if (!start_with_ip ) {
2301                 frame->args = alloca (rtm->alloca_size);
2302                 memset (frame->args, 0, rtm->alloca_size);
2303
2304                 ip = rtm->code;
2305         } else {
2306                 ip = start_with_ip;
2307         }
2308         sp = frame->stack = (stackval *) ((char *) frame->args + rtm->args_size);
2309         vt_sp = (unsigned char *) sp + rtm->stack_size;
2310 #if DEBUG_INTERP
2311         vtalloc = vt_sp;
2312 #endif
2313         locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
2314         frame->locals = locals;
2315         child_frame.parent = frame;
2316
2317         if (filter_exception) {
2318                 sp->data.p = filter_exception;
2319                 sp++;
2320         }
2321
2322         /*
2323          * using while (ip < end) may result in a 15% performance drop, 
2324          * but it may be useful for debug
2325          */
2326         while (1) {
2327         main_loop:
2328                 /* g_assert (sp >= frame->stack); */
2329                 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
2330                 DUMP_INSTR();
2331                 MINT_IN_SWITCH (*ip) {
2332                 MINT_IN_CASE(MINT_INITLOCALS)
2333                         memset (locals, 0, rtm->locals_size);
2334                         ++ip;
2335                         MINT_IN_BREAK;
2336                 MINT_IN_CASE(MINT_NOP)
2337                         ++ip;
2338                         MINT_IN_BREAK;
2339                 MINT_IN_CASE(MINT_NIY)
2340                         g_error ("mint_niy: instruction not implemented yet.  This shouldn't happen.");
2341                         MINT_IN_BREAK;
2342                 MINT_IN_CASE(MINT_BREAK)
2343                         ++ip;
2344                         do_debugger_tramp (mono_debugger_agent_user_break, frame);
2345                         MINT_IN_BREAK;
2346                 MINT_IN_CASE(MINT_LDNULL) 
2347                         sp->data.p = NULL;
2348                         ++ip;
2349                         ++sp;
2350                         MINT_IN_BREAK;
2351                 MINT_IN_CASE(MINT_VTRESULT) {
2352                         int ret_size = * (guint16 *)(ip + 1);
2353                         unsigned char *ret_vt_sp = vt_sp;
2354                         vt_sp -= READ32(ip + 2);
2355                         if (ret_size > 0) {
2356                                 memmove (vt_sp, ret_vt_sp, ret_size);
2357                                 sp [-1].data.p = vt_sp;
2358                                 vt_sp += (ret_size + 7) & ~7;
2359                         }
2360                         ip += 4;
2361                         MINT_IN_BREAK;
2362                 }
2363 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
2364                 MINT_IN_CASE(MINT_LDC_I4_M1)
2365                         LDC(-1);
2366                         MINT_IN_BREAK;
2367                 MINT_IN_CASE(MINT_LDC_I4_0)
2368                         LDC(0);
2369                         MINT_IN_BREAK;
2370                 MINT_IN_CASE(MINT_LDC_I4_1)
2371                         LDC(1);
2372                         MINT_IN_BREAK;
2373                 MINT_IN_CASE(MINT_LDC_I4_2)
2374                         LDC(2);
2375                         MINT_IN_BREAK;
2376                 MINT_IN_CASE(MINT_LDC_I4_3)
2377                         LDC(3);
2378                         MINT_IN_BREAK;
2379                 MINT_IN_CASE(MINT_LDC_I4_4)
2380                         LDC(4);
2381                         MINT_IN_BREAK;
2382                 MINT_IN_CASE(MINT_LDC_I4_5)
2383                         LDC(5);
2384                         MINT_IN_BREAK;
2385                 MINT_IN_CASE(MINT_LDC_I4_6)
2386                         LDC(6);
2387                         MINT_IN_BREAK;
2388                 MINT_IN_CASE(MINT_LDC_I4_7)
2389                         LDC(7);
2390                         MINT_IN_BREAK;
2391                 MINT_IN_CASE(MINT_LDC_I4_8)
2392                         LDC(8);
2393                         MINT_IN_BREAK;
2394                 MINT_IN_CASE(MINT_LDC_I4_S) 
2395                         sp->data.i = *(const short *)(ip + 1);
2396                         ip += 2;
2397                         ++sp;
2398                         MINT_IN_BREAK;
2399                 MINT_IN_CASE(MINT_LDC_I4)
2400                         ++ip;
2401                         sp->data.i = READ32 (ip);
2402                         ip += 2;
2403                         ++sp;
2404                         MINT_IN_BREAK;
2405                 MINT_IN_CASE(MINT_LDC_I8)
2406                         ++ip;
2407                         sp->data.l = READ64 (ip);
2408                         ip += 4;
2409                         ++sp;
2410                         MINT_IN_BREAK;
2411                 MINT_IN_CASE(MINT_LDC_R4) {
2412                         guint32 val;
2413                         ++ip;
2414                         val = READ32(ip);
2415                         sp->data.f = * (float *)&val;
2416                         ip += 2;
2417                         ++sp;
2418                         MINT_IN_BREAK;
2419                 }
2420                 MINT_IN_CASE(MINT_LDC_R8) 
2421                         sp->data.l = READ64 (ip + 1); /* note union usage */
2422                         ip += 5;
2423                         ++sp;
2424                         MINT_IN_BREAK;
2425                 MINT_IN_CASE(MINT_DUP) 
2426                         sp [0] = sp[-1];
2427                         ++sp;
2428                         ++ip; 
2429                         MINT_IN_BREAK;
2430                 MINT_IN_CASE(MINT_DUP_VT)
2431                         i32 = READ32 (ip + 1);
2432                         sp->data.p = vt_sp;
2433                         memcpy(sp->data.p, sp [-1].data.p, i32);
2434                         vt_sp += (i32 + 7) & ~7;
2435                         ++sp;
2436                         ip += 3;
2437                         MINT_IN_BREAK;
2438                 MINT_IN_CASE(MINT_POP) {
2439                         guint16 u16 = (* (guint16 *)(ip + 1)) + 1;
2440                         if (u16 > 1)
2441                                 memmove (sp - u16, sp - 1, (u16 - 1) * sizeof (stackval));
2442                         sp--;
2443                         ip += 2;
2444                         MINT_IN_BREAK;
2445                 }
2446                 MINT_IN_CASE(MINT_JMP) {
2447                         InterpMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
2448
2449                         if (frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_TAIL_CALL)
2450                                 MONO_PROFILER_RAISE (method_tail_call, (frame->imethod->method, new_method->method));
2451
2452                         if (!new_method->transformed) {
2453                                 frame->ip = ip;
2454                                 frame->ex = mono_interp_transform_method (new_method, context);
2455                                 if (frame->ex)
2456                                         goto exit_frame;
2457                         }
2458                         ip += 2;
2459                         if (new_method->alloca_size > rtm->alloca_size)
2460                                 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size); 
2461                         rtm = frame->imethod = new_method;
2462                         vt_sp = (unsigned char *) sp + rtm->stack_size;
2463 #if DEBUG_INTERP
2464                         vtalloc = vt_sp;
2465 #endif
2466                         locals = vt_sp + rtm->vt_stack_size;
2467                         frame->locals = locals;
2468                         ip = rtm->new_body_start; /* bypass storing input args from callers frame */
2469                         MINT_IN_BREAK;
2470                 }
2471                 MINT_IN_CASE(MINT_CALLI) {
2472                         MonoMethodSignature *csignature;
2473                         stackval *endsp = sp;
2474
2475                         frame->ip = ip;
2476                         
2477                         csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2478                         ip += 2;
2479                         --sp;
2480                         --endsp;
2481                         child_frame.imethod = sp->data.p;
2482
2483                         sp->data.p = vt_sp;
2484                         child_frame.retval = sp;
2485                         /* decrement by the actual number of args */
2486                         sp -= csignature->param_count;
2487                         if (csignature->hasthis)
2488                                 --sp;
2489                         child_frame.stack_args = sp;
2490
2491 #ifndef DISABLE_REMOTING
2492                         /* `this' can be NULL for string:.ctor */
2493                         if (csignature->hasthis && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2494                                 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.imethod->method), &error);
2495                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2496                         } else
2497 #endif
2498                         if (child_frame.imethod->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
2499                                 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_native_wrapper (child_frame.imethod->method, FALSE, FALSE), &error);
2500                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2501                         }
2502
2503                         if (csignature->hasthis) {
2504                                 MonoObject *this_arg = sp->data.p;
2505
2506                                 if (this_arg->vtable->klass->valuetype) {
2507                                         gpointer *unboxed = mono_object_unbox (this_arg);
2508                                         sp [0].data.p = unboxed;
2509                                 }
2510                         }
2511
2512                         ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2513
2514                         context->current_frame = frame;
2515
2516                         if (context->has_resume_state) {
2517                                 if (frame == context->handler_frame)
2518                                         SET_RESUME_STATE (context);
2519                                 else
2520                                         goto exit_frame;
2521                         }
2522
2523                         if (child_frame.ex) {
2524                                 /*
2525                                  * An exception occurred, need to run finally, fault and catch handlers..
2526                                  */
2527                                 frame->ex = child_frame.ex;
2528                                 goto handle_finally;
2529                         }
2530
2531                         /* need to handle typedbyref ... */
2532                         if (csignature->ret->type != MONO_TYPE_VOID) {
2533                                 *sp = *endsp;
2534                                 sp++;
2535                         }
2536                         MINT_IN_BREAK;
2537                 }
2538                 MINT_IN_CASE(MINT_CALLI_NAT) {
2539                         MonoMethodSignature *csignature;
2540                         stackval *endsp = sp;
2541                         unsigned char *code = NULL;
2542
2543                         frame->ip = ip;
2544                         
2545                         csignature = rtm->data_items [* (guint16 *)(ip + 1)];
2546                         ip += 2;
2547                         --sp;
2548                         --endsp;
2549                         code = sp->data.p;
2550                         child_frame.imethod = NULL;
2551
2552                         sp->data.p = vt_sp;
2553                         child_frame.retval = sp;
2554                         /* decrement by the actual number of args */
2555                         sp -= csignature->param_count;
2556                         if (csignature->hasthis)
2557                                 --sp;
2558                         child_frame.stack_args = sp;
2559                         ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
2560
2561                         context->current_frame = frame;
2562
2563                         if (context->has_resume_state) {
2564                                 if (frame == context->handler_frame)
2565                                         SET_RESUME_STATE (context);
2566                                 else
2567                                         goto exit_frame;
2568                         }
2569
2570                         if (child_frame.ex) {
2571                                 /*
2572                                  * An exception occurred, need to run finally, fault and catch handlers..
2573                                  */
2574                                 frame->ex = child_frame.ex;
2575                                 if (context->search_for_handler) {
2576                                         context->search_for_handler = 0;
2577                                         goto handle_exception;
2578                                 }
2579                                 goto handle_finally;
2580                         }
2581
2582                         /* need to handle typedbyref ... */
2583                         if (csignature->ret->type != MONO_TYPE_VOID) {
2584                                 *sp = *endsp;
2585                                 sp++;
2586                         }
2587                         MINT_IN_BREAK;
2588                 }
2589                 MINT_IN_CASE(MINT_CALL) {
2590                         stackval *endsp = sp;
2591
2592                         frame->ip = ip;
2593                         
2594                         child_frame.imethod = rtm->data_items [* (guint16 *)(ip + 1)];
2595                         ip += 2;
2596                         sp->data.p = vt_sp;
2597                         child_frame.retval = sp;
2598                         /* decrement by the actual number of args */
2599                         sp -= child_frame.imethod->param_count;
2600                         if (child_frame.imethod->hasthis)
2601                                 --sp;
2602                         child_frame.stack_args = sp;
2603
2604 #ifndef DISABLE_REMOTING
2605                         /* `this' can be NULL for string:.ctor */
2606                         if (child_frame.imethod->hasthis && !child_frame.imethod->method->klass->valuetype && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
2607                                 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.imethod->method), &error);
2608                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2609                         }
2610 #endif
2611
2612                         ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2613
2614                         context->current_frame = frame;
2615
2616                         if (context->has_resume_state) {
2617                                 if (frame == context->handler_frame)
2618                                         SET_RESUME_STATE (context);
2619                                 else
2620                                         goto exit_frame;
2621                         }
2622
2623                         if (child_frame.ex) {
2624                                 /*
2625                                  * An exception occurred, need to run finally, fault and catch handlers..
2626                                  */
2627                                 frame->ex = child_frame.ex;
2628                                 goto handle_exception;;
2629                         }
2630
2631                         /* need to handle typedbyref ... */
2632                         *sp = *endsp;
2633                         sp++;
2634                         MINT_IN_BREAK;
2635                 }
2636                 MINT_IN_CASE(MINT_VCALL) {
2637                         frame->ip = ip;
2638                         
2639                         child_frame.imethod = rtm->data_items [* (guint16 *)(ip + 1)];
2640                         ip += 2;
2641
2642                         sp->data.p = vt_sp;
2643                         child_frame.retval = sp;
2644                         /* decrement by the actual number of args */
2645                         sp -= child_frame.imethod->param_count;
2646                         if (child_frame.imethod->hasthis) {
2647                                 --sp;
2648                                 MonoObject *this_arg = sp->data.p;
2649                                 if (!this_arg)
2650                                         THROW_EX (mono_get_exception_null_reference(), ip - 2);
2651                         }
2652                         child_frame.stack_args = sp;
2653
2654 #ifndef DISABLE_REMOTING
2655                         if (child_frame.imethod->hasthis && !child_frame.imethod->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
2656                                 child_frame.imethod = mono_interp_get_imethod (rtm->domain, mono_marshal_get_remoting_invoke (child_frame.imethod->method), &error);
2657                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2658                         }
2659 #endif
2660
2661                         ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2662
2663                         context->current_frame = frame;
2664
2665                         if (context->has_resume_state) {
2666                                 if (frame == context->handler_frame)
2667                                         SET_RESUME_STATE (context);
2668                                 else
2669                                         goto exit_frame;
2670                         }
2671
2672                         if (child_frame.ex) {
2673                                 /*
2674                                  * An exception occurred, need to run finally, fault and catch handlers..
2675                                  */
2676                                 frame->ex = child_frame.ex;
2677                                 goto handle_finally;
2678                         }
2679                         MINT_IN_BREAK;
2680                 }
2681
2682                 MINT_IN_CASE(MINT_JIT_CALL) {
2683                         InterpMethod *rmethod = rtm->data_items [* (guint16 *)(ip + 1)];
2684                         frame->ip = ip;
2685                         ip += 2;
2686                         sp = do_jit_call (sp, vt_sp, context, frame, rmethod);
2687
2688                         if (context->has_resume_state) {
2689                                 /*
2690                                  * If this bit is set, it means the call has thrown the exception, and we
2691                                  * reached this point because the EH code in mono_handle_exception ()
2692                                  * unwound all the JITted frames below us. mono_interp_set_resume_state ()
2693                                  * has set the fields in context to indicate where we have to resume execution.
2694                                  */
2695                                 if (frame == context->handler_frame)
2696                                         SET_RESUME_STATE (context);
2697                                 else
2698                                         goto exit_frame;
2699                         }
2700                         if (rmethod->rtype->type != MONO_TYPE_VOID)
2701                                 sp++;
2702
2703                         MINT_IN_BREAK;
2704                 }
2705
2706                 MINT_IN_CASE(MINT_CALLVIRT) {
2707                         stackval *endsp = sp;
2708                         MonoObject *this_arg;
2709                         guint32 token;
2710
2711                         frame->ip = ip;
2712                         
2713                         token = * (unsigned short *)(ip + 1);
2714                         ip += 2;
2715                         child_frame.imethod = rtm->data_items [token];
2716                         sp->data.p = vt_sp;
2717                         child_frame.retval = sp;
2718
2719                         /* decrement by the actual number of args */
2720                         sp -= child_frame.imethod->param_count + 1;
2721                         child_frame.stack_args = sp;
2722                         this_arg = sp->data.p;
2723                         if (!this_arg)
2724                                 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2725                         child_frame.imethod = get_virtual_method (child_frame.imethod, this_arg);
2726
2727                         MonoClass *this_class = this_arg->vtable->klass;
2728                         if (this_class->valuetype && child_frame.imethod->method->klass->valuetype) {
2729                                 /* unbox */
2730                                 gpointer *unboxed = mono_object_unbox (this_arg);
2731                                 sp [0].data.p = unboxed;
2732                         }
2733
2734                         ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2735
2736                         context->current_frame = frame;
2737
2738                         if (context->has_resume_state) {
2739                                 if (frame == context->handler_frame)
2740                                         SET_RESUME_STATE (context);
2741                                 else
2742                                         goto exit_frame;
2743                         }
2744
2745                         if (child_frame.ex) {
2746                                 /*
2747                                  * An exception occurred, need to run finally, fault and catch handlers..
2748                                  */
2749                                 frame->ex = child_frame.ex;
2750                                 if (context->search_for_handler) {
2751                                         context->search_for_handler = 0;
2752                                         goto handle_exception;
2753                                 }
2754                                 goto handle_finally;
2755                         }
2756
2757                         /* need to handle typedbyref ... */
2758                         *sp = *endsp;
2759                         sp++;
2760                         MINT_IN_BREAK;
2761                 }
2762                 MINT_IN_CASE(MINT_VCALLVIRT) {
2763                         MonoObject *this_arg;
2764                         guint32 token;
2765
2766                         frame->ip = ip;
2767                         
2768                         token = * (unsigned short *)(ip + 1);
2769                         ip += 2;
2770                         child_frame.imethod = rtm->data_items [token];
2771                         sp->data.p = vt_sp;
2772                         child_frame.retval = sp;
2773
2774                         /* decrement by the actual number of args */
2775                         sp -= child_frame.imethod->param_count + 1;
2776                         child_frame.stack_args = sp;
2777                         this_arg = sp->data.p;
2778                         if (!this_arg)
2779                                 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2780                         child_frame.imethod = get_virtual_method (child_frame.imethod, this_arg);
2781
2782                         MonoClass *this_class = this_arg->vtable->klass;
2783                         if (this_class->valuetype && child_frame.imethod->method->klass->valuetype) {
2784                                 gpointer *unboxed = mono_object_unbox (this_arg);
2785                                 sp [0].data.p = unboxed;
2786                         }
2787
2788                         ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
2789
2790                         context->current_frame = frame;
2791
2792                         if (context->has_resume_state) {
2793                                 if (frame == context->handler_frame)
2794                                         SET_RESUME_STATE (context);
2795                                 else
2796                                         goto exit_frame;
2797                         }
2798
2799                         if (child_frame.ex) {
2800                                 /*
2801                                  * An exception occurred, need to run finally, fault and catch handlers..
2802                                  */
2803                                 frame->ex = child_frame.ex;
2804                                 if (context->search_for_handler) {
2805                                         context->search_for_handler = 0;
2806                                         goto handle_exception;
2807                                 }
2808                                 goto handle_finally;
2809                         }
2810                         MINT_IN_BREAK;
2811                 }
2812                 MINT_IN_CASE(MINT_CALLRUN)
2813                         ves_imethod (frame, context);
2814                         if (frame->ex) {
2815                                 rtm = NULL;
2816                                 goto handle_exception;
2817                         }
2818                         goto exit_frame;
2819                 MINT_IN_CASE(MINT_RET)
2820                         --sp;
2821                         *frame->retval = *sp;
2822                         if (sp > frame->stack)
2823                                 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2824                         goto exit_frame;
2825                 MINT_IN_CASE(MINT_RET_VOID)
2826                         if (sp > frame->stack)
2827                                 g_warning ("ret.void: more values on stack: %d %s", sp-frame->stack, mono_method_full_name (frame->imethod->method, TRUE));
2828                         goto exit_frame;
2829                 MINT_IN_CASE(MINT_RET_VT)
2830                         i32 = READ32(ip + 1);
2831                         --sp;
2832                         memcpy(frame->retval->data.p, sp->data.p, i32);
2833                         if (sp > frame->stack)
2834                                 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2835                         goto exit_frame;
2836                 MINT_IN_CASE(MINT_BR_S)
2837                         ip += (short) *(ip + 1);
2838                         MINT_IN_BREAK;
2839                 MINT_IN_CASE(MINT_BR)
2840                         ip += (gint32) READ32(ip + 1);
2841                         MINT_IN_BREAK;
2842 #define ZEROP_S(datamem, op) \
2843         --sp; \
2844         if (sp->data.datamem op 0) \
2845                 ip += * (gint16 *)(ip + 1); \
2846         else \
2847                 ip += 2;
2848
2849 #define ZEROP(datamem, op) \
2850         --sp; \
2851         if (sp->data.datamem op 0) \
2852                 ip += READ32(ip + 1); \
2853         else \
2854                 ip += 3;
2855
2856                 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2857                         ZEROP_S(i, ==);
2858                         MINT_IN_BREAK;
2859                 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2860                         ZEROP_S(l, ==);
2861                         MINT_IN_BREAK;
2862                 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2863                         ZEROP_S(f, ==);
2864                         MINT_IN_BREAK;
2865                 MINT_IN_CASE(MINT_BRFALSE_I4)
2866                         ZEROP(i, ==);
2867                         MINT_IN_BREAK;
2868                 MINT_IN_CASE(MINT_BRFALSE_I8)
2869                         ZEROP(l, ==);
2870                         MINT_IN_BREAK;
2871                 MINT_IN_CASE(MINT_BRFALSE_R8)
2872                         ZEROP_S(f, ==);
2873                         MINT_IN_BREAK;
2874                 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2875                         ZEROP_S(i, !=);
2876                         MINT_IN_BREAK;
2877                 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2878                         ZEROP_S(l, !=);
2879                         MINT_IN_BREAK;
2880                 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2881                         ZEROP_S(f, !=);
2882                         MINT_IN_BREAK;
2883                 MINT_IN_CASE(MINT_BRTRUE_I4)
2884                         ZEROP(i, !=);
2885                         MINT_IN_BREAK;
2886                 MINT_IN_CASE(MINT_BRTRUE_I8)
2887                         ZEROP(l, !=);
2888                         MINT_IN_BREAK;
2889                 MINT_IN_CASE(MINT_BRTRUE_R8)
2890                         ZEROP(f, !=);
2891                         MINT_IN_BREAK;
2892 #define CONDBR_S(cond) \
2893         sp -= 2; \
2894         if (cond) \
2895                 ip += * (gint16 *)(ip + 1); \
2896         else \
2897                 ip += 2;
2898 #define BRELOP_S(datamem, op) \
2899         CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2900
2901 #define CONDBR(cond) \
2902         sp -= 2; \
2903         if (cond) \
2904                 ip += READ32(ip + 1); \
2905         else \
2906                 ip += 3;
2907
2908 #define BRELOP(datamem, op) \
2909         CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2910
2911                 MINT_IN_CASE(MINT_BEQ_I4_S)
2912                         BRELOP_S(i, ==)
2913                         MINT_IN_BREAK;
2914                 MINT_IN_CASE(MINT_BEQ_I8_S)
2915                         BRELOP_S(l, ==)
2916                         MINT_IN_BREAK;
2917                 MINT_IN_CASE(MINT_BEQ_R8_S)
2918                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2919                         MINT_IN_BREAK;
2920                 MINT_IN_CASE(MINT_BEQ_I4)
2921                         BRELOP(i, ==)
2922                         MINT_IN_BREAK;
2923                 MINT_IN_CASE(MINT_BEQ_I8)
2924                         BRELOP(l, ==)
2925                         MINT_IN_BREAK;
2926                 MINT_IN_CASE(MINT_BEQ_R8)
2927                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2928                         MINT_IN_BREAK;
2929                 MINT_IN_CASE(MINT_BGE_I4_S)
2930                         BRELOP_S(i, >=)
2931                         MINT_IN_BREAK;
2932                 MINT_IN_CASE(MINT_BGE_I8_S)
2933                         BRELOP_S(l, >=)
2934                         MINT_IN_BREAK;
2935                 MINT_IN_CASE(MINT_BGE_R8_S)
2936                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2937                         MINT_IN_BREAK;
2938                 MINT_IN_CASE(MINT_BGE_I4)
2939                         BRELOP(i, >=)
2940                         MINT_IN_BREAK;
2941                 MINT_IN_CASE(MINT_BGE_I8)
2942                         BRELOP(l, >=)
2943                         MINT_IN_BREAK;
2944                 MINT_IN_CASE(MINT_BGE_R8)
2945                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2946                         MINT_IN_BREAK;
2947                 MINT_IN_CASE(MINT_BGT_I4_S)
2948                         BRELOP_S(i, >)
2949                         MINT_IN_BREAK;
2950                 MINT_IN_CASE(MINT_BGT_I8_S)
2951                         BRELOP_S(l, >)
2952                         MINT_IN_BREAK;
2953                 MINT_IN_CASE(MINT_BGT_R8_S)
2954                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2955                         MINT_IN_BREAK;
2956                 MINT_IN_CASE(MINT_BGT_I4)
2957                         BRELOP(i, >)
2958                         MINT_IN_BREAK;
2959                 MINT_IN_CASE(MINT_BGT_I8)
2960                         BRELOP(l, >)
2961                         MINT_IN_BREAK;
2962                 MINT_IN_CASE(MINT_BGT_R8)
2963                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2964                         MINT_IN_BREAK;
2965                 MINT_IN_CASE(MINT_BLT_I4_S)
2966                         BRELOP_S(i, <)
2967                         MINT_IN_BREAK;
2968                 MINT_IN_CASE(MINT_BLT_I8_S)
2969                         BRELOP_S(l, <)
2970                         MINT_IN_BREAK;
2971                 MINT_IN_CASE(MINT_BLT_R8_S)
2972                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2973                         MINT_IN_BREAK;
2974                 MINT_IN_CASE(MINT_BLT_I4)
2975                         BRELOP(i, <)
2976                         MINT_IN_BREAK;
2977                 MINT_IN_CASE(MINT_BLT_I8)
2978                         BRELOP(l, <)
2979                         MINT_IN_BREAK;
2980                 MINT_IN_CASE(MINT_BLT_R8)
2981                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2982                         MINT_IN_BREAK;
2983                 MINT_IN_CASE(MINT_BLE_I4_S)
2984                         BRELOP_S(i, <=)
2985                         MINT_IN_BREAK;
2986                 MINT_IN_CASE(MINT_BLE_I8_S)
2987                         BRELOP_S(l, <=)
2988                         MINT_IN_BREAK;
2989                 MINT_IN_CASE(MINT_BLE_R8_S)
2990                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2991                         MINT_IN_BREAK;
2992                 MINT_IN_CASE(MINT_BLE_I4)
2993                         BRELOP(i, <=)
2994                         MINT_IN_BREAK;
2995                 MINT_IN_CASE(MINT_BLE_I8)
2996                         BRELOP(l, <=)
2997                         MINT_IN_BREAK;
2998                 MINT_IN_CASE(MINT_BLE_R8)
2999                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
3000                         MINT_IN_BREAK;
3001                 MINT_IN_CASE(MINT_BNE_UN_I4_S)
3002                         BRELOP_S(i, !=)
3003                         MINT_IN_BREAK;
3004                 MINT_IN_CASE(MINT_BNE_UN_I8_S)
3005                         BRELOP_S(l, !=)
3006                         MINT_IN_BREAK;
3007                 MINT_IN_CASE(MINT_BNE_UN_R8_S)
3008                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3009                         MINT_IN_BREAK;
3010                 MINT_IN_CASE(MINT_BNE_UN_I4)
3011                         BRELOP(i, !=)
3012                         MINT_IN_BREAK;
3013                 MINT_IN_CASE(MINT_BNE_UN_I8)
3014                         BRELOP(l, !=)
3015                         MINT_IN_BREAK;
3016                 MINT_IN_CASE(MINT_BNE_UN_R8)
3017                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
3018                         MINT_IN_BREAK;
3019
3020 #define BRELOP_S_CAST(datamem, op, type) \
3021         sp -= 2; \
3022         if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3023                 ip += * (gint16 *)(ip + 1); \
3024         else \
3025                 ip += 2;
3026
3027 #define BRELOP_CAST(datamem, op, type) \
3028         sp -= 2; \
3029         if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
3030                 ip += READ32(ip + 1); \
3031         else \
3032                 ip += 3;
3033
3034                 MINT_IN_CASE(MINT_BGE_UN_I4_S)
3035                         BRELOP_S_CAST(i, >=, guint32);
3036                         MINT_IN_BREAK;
3037                 MINT_IN_CASE(MINT_BGE_UN_I8_S)
3038                         BRELOP_S_CAST(l, >=, guint64);
3039                         MINT_IN_BREAK;
3040                 MINT_IN_CASE(MINT_BGE_UN_R8_S)
3041                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3042                         MINT_IN_BREAK;
3043                 MINT_IN_CASE(MINT_BGE_UN_I4)
3044                         BRELOP_CAST(i, >=, guint32);
3045                         MINT_IN_BREAK;
3046                 MINT_IN_CASE(MINT_BGE_UN_I8)
3047                         BRELOP_CAST(l, >=, guint64);
3048                         MINT_IN_BREAK;
3049                 MINT_IN_CASE(MINT_BGE_UN_R8)
3050                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
3051                         MINT_IN_BREAK;
3052                 MINT_IN_CASE(MINT_BGT_UN_I4_S)
3053                         BRELOP_S_CAST(i, >, guint32);
3054                         MINT_IN_BREAK;
3055                 MINT_IN_CASE(MINT_BGT_UN_I8_S)
3056                         BRELOP_S_CAST(l, >, guint64);
3057                         MINT_IN_BREAK;
3058                 MINT_IN_CASE(MINT_BGT_UN_R8_S)
3059                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3060                         MINT_IN_BREAK;
3061                 MINT_IN_CASE(MINT_BGT_UN_I4)
3062                         BRELOP_CAST(i, >, guint32);
3063                         MINT_IN_BREAK;
3064                 MINT_IN_CASE(MINT_BGT_UN_I8)
3065                         BRELOP_CAST(l, >, guint64);
3066                         MINT_IN_BREAK;
3067                 MINT_IN_CASE(MINT_BGT_UN_R8)
3068                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
3069                         MINT_IN_BREAK;
3070                 MINT_IN_CASE(MINT_BLE_UN_I4_S)
3071                         BRELOP_S_CAST(i, <=, guint32);
3072                         MINT_IN_BREAK;
3073                 MINT_IN_CASE(MINT_BLE_UN_I8_S)
3074                         BRELOP_S_CAST(l, <=, guint64);
3075                         MINT_IN_BREAK;
3076                 MINT_IN_CASE(MINT_BLE_UN_R8_S)
3077                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3078                         MINT_IN_BREAK;
3079                 MINT_IN_CASE(MINT_BLE_UN_I4)
3080                         BRELOP_CAST(i, <=, guint32);
3081                         MINT_IN_BREAK;
3082                 MINT_IN_CASE(MINT_BLE_UN_I8)
3083                         BRELOP_CAST(l, <=, guint64);
3084                         MINT_IN_BREAK;
3085                 MINT_IN_CASE(MINT_BLE_UN_R8)
3086                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
3087                         MINT_IN_BREAK;
3088                 MINT_IN_CASE(MINT_BLT_UN_I4_S)
3089                         BRELOP_S_CAST(i, <, guint32);
3090                         MINT_IN_BREAK;
3091                 MINT_IN_CASE(MINT_BLT_UN_I8_S)
3092                         BRELOP_S_CAST(l, <, guint64);
3093                         MINT_IN_BREAK;
3094                 MINT_IN_CASE(MINT_BLT_UN_R8_S)
3095                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3096                         MINT_IN_BREAK;
3097                 MINT_IN_CASE(MINT_BLT_UN_I4)
3098                         BRELOP_CAST(i, <, guint32);
3099                         MINT_IN_BREAK;
3100                 MINT_IN_CASE(MINT_BLT_UN_I8)
3101                         BRELOP_CAST(l, <, guint64);
3102                         MINT_IN_BREAK;
3103                 MINT_IN_CASE(MINT_BLT_UN_R8)
3104                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
3105                         MINT_IN_BREAK;
3106                 MINT_IN_CASE(MINT_SWITCH) {
3107                         guint32 n;
3108                         const unsigned short *st;
3109                         ++ip;
3110                         n = READ32 (ip);
3111                         ip += 2;
3112                         st = ip + 2 * n;
3113                         --sp;
3114                         if ((guint32)sp->data.i < n) {
3115                                 gint offset;
3116                                 ip += 2 * (guint32)sp->data.i;
3117                                 offset = READ32 (ip);
3118                                 ip = ip + offset;
3119                         } else {
3120                                 ip = st;
3121                         }
3122                         MINT_IN_BREAK;
3123                 }
3124                 MINT_IN_CASE(MINT_LDIND_I1)
3125                         ++ip;
3126                         sp[-1].data.i = *(gint8*)sp[-1].data.p;
3127                         MINT_IN_BREAK;
3128                 MINT_IN_CASE(MINT_LDIND_U1)
3129                         ++ip;
3130                         sp[-1].data.i = *(guint8*)sp[-1].data.p;
3131                         MINT_IN_BREAK;
3132                 MINT_IN_CASE(MINT_LDIND_I2)
3133                         ++ip;
3134                         sp[-1].data.i = *(gint16*)sp[-1].data.p;
3135                         MINT_IN_BREAK;
3136                 MINT_IN_CASE(MINT_LDIND_U2)
3137                         ++ip;
3138                         sp[-1].data.i = *(guint16*)sp[-1].data.p;
3139                         MINT_IN_BREAK;
3140                 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
3141                 MINT_IN_CASE(MINT_LDIND_U4)
3142                         ++ip;
3143                         sp[-1].data.i = *(gint32*)sp[-1].data.p;
3144                         MINT_IN_BREAK;
3145                 MINT_IN_CASE(MINT_LDIND_I8)
3146                         ++ip;
3147                         /* memmove handles unaligned case */
3148                         memmove (&sp [-1].data.l, sp [-1].data.p, sizeof (gint64));
3149                         MINT_IN_BREAK;
3150                 MINT_IN_CASE(MINT_LDIND_I) {
3151                         guint16 offset = * (guint16 *)(ip + 1);
3152                         sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
3153                         ip += 2;
3154                         MINT_IN_BREAK;
3155                 }
3156                 MINT_IN_CASE(MINT_LDIND_R4)
3157                         ++ip;
3158                         sp[-1].data.f = *(gfloat*)sp[-1].data.p;
3159                         MINT_IN_BREAK;
3160                 MINT_IN_CASE(MINT_LDIND_R8)
3161                         ++ip;
3162                         sp[-1].data.f = *(gdouble*)sp[-1].data.p;
3163                         MINT_IN_BREAK;
3164                 MINT_IN_CASE(MINT_LDIND_REF)
3165                         ++ip;
3166                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
3167                         MINT_IN_BREAK;
3168                 MINT_IN_CASE(MINT_STIND_REF) 
3169                         ++ip;
3170                         sp -= 2;
3171                         mono_gc_wbarrier_generic_store (sp->data.p, sp [1].data.p);
3172                         MINT_IN_BREAK;
3173                 MINT_IN_CASE(MINT_STIND_I1)
3174                         ++ip;
3175                         sp -= 2;
3176                         * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
3177                         MINT_IN_BREAK;
3178                 MINT_IN_CASE(MINT_STIND_I2)
3179                         ++ip;
3180                         sp -= 2;
3181                         * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
3182                         MINT_IN_BREAK;
3183                 MINT_IN_CASE(MINT_STIND_I4)
3184                         ++ip;
3185                         sp -= 2;
3186                         * (gint32 *) sp->data.p = sp[1].data.i;
3187                         MINT_IN_BREAK;
3188                 MINT_IN_CASE(MINT_STIND_I)
3189                         ++ip;
3190                         sp -= 2;
3191                         * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
3192                         MINT_IN_BREAK;
3193                 MINT_IN_CASE(MINT_STIND_I8)
3194                         ++ip;
3195                         sp -= 2;
3196                         * (gint64 *) sp->data.p = sp[1].data.l;
3197                         MINT_IN_BREAK;
3198                 MINT_IN_CASE(MINT_STIND_R4)
3199                         ++ip;
3200                         sp -= 2;
3201                         * (float *) sp->data.p = (gfloat)sp[1].data.f;
3202                         MINT_IN_BREAK;
3203                 MINT_IN_CASE(MINT_STIND_R8)
3204                         ++ip;
3205                         sp -= 2;
3206                         * (double *) sp->data.p = sp[1].data.f;
3207                         MINT_IN_BREAK;
3208                 MINT_IN_CASE(MINT_MONO_ATOMIC_STORE_I4)
3209                         ++ip;
3210                         sp -= 2;
3211                         InterlockedWrite ((gint32 *) sp->data.p, sp [1].data.i);
3212                         MINT_IN_BREAK;
3213 #define BINOP(datamem, op) \
3214         --sp; \
3215         sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
3216         ++ip;
3217                 MINT_IN_CASE(MINT_ADD_I4)
3218                         BINOP(i, +);
3219                         MINT_IN_BREAK;
3220                 MINT_IN_CASE(MINT_ADD_I8)
3221                         BINOP(l, +);
3222                         MINT_IN_BREAK;
3223                 MINT_IN_CASE(MINT_ADD_R8)
3224                         BINOP(f, +);
3225                         MINT_IN_BREAK;
3226                 MINT_IN_CASE(MINT_ADD1_I4)
3227                         ++sp [-1].data.i;
3228                         ++ip;
3229                         MINT_IN_BREAK;
3230                 MINT_IN_CASE(MINT_SUB_I4)
3231                         BINOP(i, -);
3232                         MINT_IN_BREAK;
3233                 MINT_IN_CASE(MINT_SUB_I8)
3234                         BINOP(l, -);
3235                         MINT_IN_BREAK;
3236                 MINT_IN_CASE(MINT_SUB_R8)
3237                         BINOP(f, -);
3238                         MINT_IN_BREAK;
3239                 MINT_IN_CASE(MINT_SUB1_I4)
3240                         --sp [-1].data.i;
3241                         ++ip;
3242                         MINT_IN_BREAK;
3243                 MINT_IN_CASE(MINT_MUL_I4)
3244                         BINOP(i, *);
3245                         MINT_IN_BREAK;
3246                 MINT_IN_CASE(MINT_MUL_I8)
3247                         BINOP(l, *);
3248                         MINT_IN_BREAK;
3249                 MINT_IN_CASE(MINT_MUL_R8)
3250                         BINOP(f, *);
3251                         MINT_IN_BREAK;
3252                 MINT_IN_CASE(MINT_DIV_I4)
3253                         if (sp [-1].data.i == 0)
3254                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3255                         if (sp [-1].data.i == (-1))
3256                                 THROW_EX (mono_get_exception_overflow (), ip);
3257                         BINOP(i, /);
3258                         MINT_IN_BREAK;
3259                 MINT_IN_CASE(MINT_DIV_I8)
3260                         if (sp [-1].data.l == 0)
3261                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3262                         if (sp [-1].data.l == (-1))
3263                                 THROW_EX (mono_get_exception_overflow (), ip);
3264                         BINOP(l, /);
3265                         MINT_IN_BREAK;
3266                 MINT_IN_CASE(MINT_DIV_R8)
3267                         BINOP(f, /);
3268                         MINT_IN_BREAK;
3269
3270 #define BINOP_CAST(datamem, op, type) \
3271         --sp; \
3272         sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3273         ++ip;
3274                 MINT_IN_CASE(MINT_DIV_UN_I4)
3275                         if (sp [-1].data.i == 0)
3276                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3277                         BINOP_CAST(i, /, guint32);
3278                         MINT_IN_BREAK;
3279                 MINT_IN_CASE(MINT_DIV_UN_I8)
3280                         if (sp [-1].data.l == 0)
3281                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3282                         BINOP_CAST(l, /, guint64);
3283                         MINT_IN_BREAK;
3284                 MINT_IN_CASE(MINT_REM_I4)
3285                         if (sp [-1].data.i == 0)
3286                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3287                         if (sp [-1].data.i == (-1))
3288                                 THROW_EX (mono_get_exception_overflow (), ip);
3289                         BINOP(i, %);
3290                         MINT_IN_BREAK;
3291                 MINT_IN_CASE(MINT_REM_I8)
3292                         if (sp [-1].data.l == 0)
3293                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3294                         if (sp [-1].data.l == (-1))
3295                                 THROW_EX (mono_get_exception_overflow (), ip);
3296                         BINOP(l, %);
3297                         MINT_IN_BREAK;
3298                 MINT_IN_CASE(MINT_REM_R8)
3299                         /* FIXME: what do we actually do here? */
3300                         --sp;
3301                         sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
3302                         ++ip;
3303                         MINT_IN_BREAK;
3304                 MINT_IN_CASE(MINT_REM_UN_I4)
3305                         if (sp [-1].data.i == 0)
3306                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3307                         BINOP_CAST(i, %, guint32);
3308                         MINT_IN_BREAK;
3309                 MINT_IN_CASE(MINT_REM_UN_I8)
3310                         if (sp [-1].data.l == 0)
3311                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
3312                         BINOP_CAST(l, %, guint64);
3313                         MINT_IN_BREAK;
3314                 MINT_IN_CASE(MINT_AND_I4)
3315                         BINOP(i, &);
3316                         MINT_IN_BREAK;
3317                 MINT_IN_CASE(MINT_AND_I8)
3318                         BINOP(l, &);
3319                         MINT_IN_BREAK;
3320                 MINT_IN_CASE(MINT_OR_I4)
3321                         BINOP(i, |);
3322                         MINT_IN_BREAK;
3323                 MINT_IN_CASE(MINT_OR_I8)
3324                         BINOP(l, |);
3325                         MINT_IN_BREAK;
3326                 MINT_IN_CASE(MINT_XOR_I4)
3327                         BINOP(i, ^);
3328                         MINT_IN_BREAK;
3329                 MINT_IN_CASE(MINT_XOR_I8)
3330                         BINOP(l, ^);
3331                         MINT_IN_BREAK;
3332
3333 #define SHIFTOP(datamem, op) \
3334         --sp; \
3335         sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
3336         ++ip;
3337
3338                 MINT_IN_CASE(MINT_SHL_I4)
3339                         SHIFTOP(i, <<);
3340                         MINT_IN_BREAK;
3341                 MINT_IN_CASE(MINT_SHL_I8)
3342                         SHIFTOP(l, <<);
3343                         MINT_IN_BREAK;
3344                 MINT_IN_CASE(MINT_SHR_I4)
3345                         SHIFTOP(i, >>);
3346                         MINT_IN_BREAK;
3347                 MINT_IN_CASE(MINT_SHR_I8)
3348                         SHIFTOP(l, >>);
3349                         MINT_IN_BREAK;
3350                 MINT_IN_CASE(MINT_SHR_UN_I4)
3351                         --sp;
3352                         sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
3353                         ++ip;
3354                         MINT_IN_BREAK;
3355                 MINT_IN_CASE(MINT_SHR_UN_I8)
3356                         --sp;
3357                         sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
3358                         ++ip;
3359                         MINT_IN_BREAK;
3360                 MINT_IN_CASE(MINT_NEG_I4)
3361                         sp [-1].data.i = - sp [-1].data.i;
3362                         ++ip;
3363                         MINT_IN_BREAK;
3364                 MINT_IN_CASE(MINT_NEG_I8)
3365                         sp [-1].data.l = - sp [-1].data.l;
3366                         ++ip;
3367                         MINT_IN_BREAK;
3368                 MINT_IN_CASE(MINT_NEG_R8)
3369                         sp [-1].data.f = - sp [-1].data.f;
3370                         ++ip;
3371                         MINT_IN_BREAK;
3372                 MINT_IN_CASE(MINT_NOT_I4)
3373                         sp [-1].data.i = ~ sp [-1].data.i;
3374                         ++ip;
3375                         MINT_IN_BREAK;
3376                 MINT_IN_CASE(MINT_NOT_I8)
3377                         sp [-1].data.l = ~ sp [-1].data.l;
3378                         ++ip;
3379                         MINT_IN_BREAK;
3380                 MINT_IN_CASE(MINT_CONV_I1_I4)
3381                         sp [-1].data.i = (gint8)sp [-1].data.i;
3382                         ++ip;
3383                         MINT_IN_BREAK;
3384                 MINT_IN_CASE(MINT_CONV_I1_I8)
3385                         sp [-1].data.i = (gint8)sp [-1].data.l;
3386                         ++ip;
3387                         MINT_IN_BREAK;
3388                 MINT_IN_CASE(MINT_CONV_I1_R8)
3389                         sp [-1].data.i = (gint8)sp [-1].data.f;
3390                         ++ip;
3391                         MINT_IN_BREAK;
3392                 MINT_IN_CASE(MINT_CONV_U1_I4)
3393                         sp [-1].data.i = (guint8)sp [-1].data.i;
3394                         ++ip;
3395                         MINT_IN_BREAK;
3396                 MINT_IN_CASE(MINT_CONV_U1_I8)
3397                         sp [-1].data.i = (guint8)sp [-1].data.l;
3398                         ++ip;
3399                         MINT_IN_BREAK;
3400                 MINT_IN_CASE(MINT_CONV_U1_R8)
3401                         sp [-1].data.i = (guint8)sp [-1].data.f;
3402                         ++ip;
3403                         MINT_IN_BREAK;
3404                 MINT_IN_CASE(MINT_CONV_I2_I4)
3405                         sp [-1].data.i = (gint16)sp [-1].data.i;
3406                         ++ip;
3407                         MINT_IN_BREAK;
3408                 MINT_IN_CASE(MINT_CONV_I2_I8)
3409                         sp [-1].data.i = (gint16)sp [-1].data.l;
3410                         ++ip;
3411                         MINT_IN_BREAK;
3412                 MINT_IN_CASE(MINT_CONV_I2_R8)
3413                         sp [-1].data.i = (gint16)sp [-1].data.f;
3414                         ++ip;
3415                         MINT_IN_BREAK;
3416                 MINT_IN_CASE(MINT_CONV_U2_I4)
3417                         sp [-1].data.i = (guint16)sp [-1].data.i;
3418                         ++ip;
3419                         MINT_IN_BREAK;
3420                 MINT_IN_CASE(MINT_CONV_U2_I8)
3421                         sp [-1].data.i = (guint16)sp [-1].data.l;
3422                         ++ip;
3423                         MINT_IN_BREAK;
3424                 MINT_IN_CASE(MINT_CONV_U2_R8)
3425                         sp [-1].data.i = (guint16)sp [-1].data.f;
3426                         ++ip;
3427                         MINT_IN_BREAK;
3428                 MINT_IN_CASE(MINT_CONV_I4_R8)
3429                         sp [-1].data.i = (gint32)sp [-1].data.f;
3430                         ++ip;
3431                         MINT_IN_BREAK;
3432                 MINT_IN_CASE(MINT_CONV_U4_I8)
3433                 MINT_IN_CASE(MINT_CONV_I4_I8)
3434                         sp [-1].data.i = (gint32)sp [-1].data.l;
3435                         ++ip;
3436                         MINT_IN_BREAK;
3437                 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
3438                         sp [-2].data.i = (gint32)sp [-2].data.l;
3439                         ++ip;
3440                         MINT_IN_BREAK;
3441                 MINT_IN_CASE(MINT_CONV_U4_R8)
3442                         /* needed on arm64 */
3443                         if (isinf (sp [-1].data.f))
3444                                 sp [-1].data.i = 0;
3445                         /* needed by wasm */
3446                         else if (isnan (sp [-1].data.f))
3447                                 sp [-1].data.i = 0;
3448                         else
3449                                 sp [-1].data.i = (guint32)sp [-1].data.f;
3450                         ++ip;
3451                         MINT_IN_BREAK;
3452                 MINT_IN_CASE(MINT_CONV_I8_I4)
3453                         sp [-1].data.l = sp [-1].data.i;
3454                         ++ip;
3455                         MINT_IN_BREAK;
3456                 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
3457                         sp [-2].data.l = sp [-2].data.i;
3458                         ++ip;
3459                         MINT_IN_BREAK;
3460                 MINT_IN_CASE(MINT_CONV_I8_U4)
3461                         sp [-1].data.l = (guint32)sp [-1].data.i;
3462                         ++ip;
3463                         MINT_IN_BREAK;
3464                 MINT_IN_CASE(MINT_CONV_I8_R8)
3465                         sp [-1].data.l = (gint64)sp [-1].data.f;
3466                         ++ip;
3467                         MINT_IN_BREAK;
3468                 MINT_IN_CASE(MINT_CONV_R4_I4)
3469                         sp [-1].data.f = (float)sp [-1].data.i;
3470                         ++ip;
3471                         MINT_IN_BREAK;
3472                 MINT_IN_CASE(MINT_CONV_R4_I8)
3473                         sp [-1].data.f = (float)sp [-1].data.l;
3474                         ++ip;
3475                         MINT_IN_BREAK;
3476                 MINT_IN_CASE(MINT_CONV_R4_R8)
3477                         sp [-1].data.f = (float)sp [-1].data.f;
3478                         ++ip;
3479                         MINT_IN_BREAK;
3480                 MINT_IN_CASE(MINT_CONV_R8_I4)
3481                         sp [-1].data.f = (double)sp [-1].data.i;
3482                         ++ip;
3483                         MINT_IN_BREAK;
3484                 MINT_IN_CASE(MINT_CONV_R8_I8)
3485                         sp [-1].data.f = (double)sp [-1].data.l;
3486                         ++ip;
3487                         MINT_IN_BREAK;
3488                 MINT_IN_CASE(MINT_CONV_U8_I4)
3489                         sp [-1].data.l = sp [-1].data.i & 0xffffffff;
3490                         ++ip;
3491                         MINT_IN_BREAK;
3492                 MINT_IN_CASE(MINT_CONV_U8_R8)
3493                         sp [-1].data.l = (guint64)sp [-1].data.f;
3494                         ++ip;
3495                         MINT_IN_BREAK;
3496                 MINT_IN_CASE(MINT_CPOBJ) {
3497                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3498                         g_assert (c->valuetype);
3499                         /* if this assertion fails, we need to add a write barrier */
3500                         g_assert (!MONO_TYPE_IS_REFERENCE (&c->byval_arg));
3501                         if (c->byval_arg.type == MONO_TYPE_VALUETYPE)
3502                                 stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
3503                         else
3504                                 stackval_from_data (&c->byval_arg, sp [-2].data.p, sp [-1].data.p, FALSE);
3505                         ip += 2;
3506                         sp -= 2;
3507                         MINT_IN_BREAK;
3508                 }
3509                 MINT_IN_CASE(MINT_LDOBJ) {
3510                         void *p;
3511                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3512                         ip += 2;
3513                         p = sp [-1].data.p;
3514                         if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3515                                 int size = mono_class_value_size (c, NULL);
3516                                 sp [-1].data.p = vt_sp;
3517                                 vt_sp += (size + 7) & ~7;
3518                         }
3519                         stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
3520                         MINT_IN_BREAK;
3521                 }
3522                 MINT_IN_CASE(MINT_LDSTR)
3523                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3524                         ++sp;
3525                         ip += 2;
3526                         MINT_IN_BREAK;
3527                 MINT_IN_CASE(MINT_NEWOBJ) {
3528                         MonoClass *newobj_class;
3529                         MonoMethodSignature *csig;
3530                         stackval valuetype_this;
3531                         guint32 token;
3532                         stackval retval;
3533
3534                         frame->ip = ip;
3535
3536                         token = * (guint16 *)(ip + 1);
3537                         ip += 2;
3538
3539                         child_frame.ip = NULL;
3540                         child_frame.ex = NULL;
3541
3542                         child_frame.imethod = rtm->data_items [token];
3543                         csig = mono_method_signature (child_frame.imethod->method);
3544                         newobj_class = child_frame.imethod->method->klass;
3545                         /*if (profiling_classes) {
3546                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
3547                                 count++;
3548                                 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
3549                         }*/
3550
3551                         if (newobj_class->parent == mono_defaults.array_class) {
3552                                 sp -= csig->param_count;
3553                                 child_frame.stack_args = sp;
3554                                 o = ves_array_create (&child_frame, rtm->domain, newobj_class, csig, sp);
3555                                 if (child_frame.ex)
3556                                         THROW_EX (child_frame.ex, ip);
3557                                 goto array_constructed;
3558                         }
3559
3560                         g_assert (csig->hasthis);
3561                         if (csig->param_count) {
3562                                 sp -= csig->param_count;
3563                                 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
3564                         }
3565                         child_frame.stack_args = sp;
3566
3567                         /*
3568                          * First arg is the object.
3569                          */
3570                         if (newobj_class->valuetype) {
3571                                 MonoType *t = &newobj_class->byval_arg;
3572                                 memset (&valuetype_this, 0, sizeof (stackval));
3573                                 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
3574                                         sp->data.p = vt_sp;
3575                                         valuetype_this.data.p = vt_sp;
3576                                 } else {
3577                                         sp->data.p = &valuetype_this;
3578                                 }
3579                         } else {
3580                                 if (newobj_class != mono_defaults.string_class) {
3581                                         context->managed_code = 0;
3582                                         o = mono_object_new_checked (rtm->domain, newobj_class, &error);
3583                                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3584                                         context->managed_code = 1;
3585                                         if (*mono_thread_interruption_request_flag ())
3586                                                 mono_thread_interruption_checkpoint ();
3587                                         sp->data.p = o;
3588                                 } else {
3589                                         sp->data.p = NULL;
3590                                         child_frame.retval = &retval;
3591                                 }
3592                         }
3593
3594                         g_assert (csig->call_convention == MONO_CALL_DEFAULT);
3595
3596                         ves_exec_method_with_context (&child_frame, context, NULL, NULL, -1);
3597
3598                         context->current_frame = frame;
3599
3600                         if (context->has_resume_state) {
3601                                 if (frame == context->handler_frame)
3602                                         SET_RESUME_STATE (context);
3603                                 else
3604                                         goto exit_frame;
3605                         }
3606
3607                         if (child_frame.ex) {
3608                                 /*
3609                                  * An exception occurred, need to run finally, fault and catch handlers..
3610                                  */
3611                                 frame->ex = child_frame.ex;
3612                                 goto handle_finally;
3613                         }
3614                         /*
3615                          * a constructor returns void, but we need to return the object we created
3616                          */
3617 array_constructed:
3618                         if (newobj_class->valuetype && !newobj_class->enumtype) {
3619                                 *sp = valuetype_this;
3620                         } else if (newobj_class == mono_defaults.string_class) {
3621                                 *sp = retval;
3622                         } else {
3623                                 sp->data.p = o;
3624                         }
3625                         ++sp;
3626                         MINT_IN_BREAK;
3627                 }
3628                 MINT_IN_CASE(MINT_NEWOBJ_MAGIC) {
3629                         guint32 token;
3630
3631                         frame->ip = ip;
3632                         token = * (guint16 *)(ip + 1);
3633                         ip += 2;
3634
3635                         MINT_IN_BREAK;
3636                 }
3637                 MINT_IN_CASE(MINT_CASTCLASS)
3638                         c = rtm->data_items [*(guint16 *)(ip + 1)];
3639                         if ((o = sp [-1].data.p)) {
3640                                 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3641                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3642                                 if (!isinst_obj)
3643                                         THROW_EX (mono_get_exception_invalid_cast (), ip);
3644                         }
3645                         ip += 2;
3646                         MINT_IN_BREAK;
3647                 MINT_IN_CASE(MINT_ISINST)
3648                         c = rtm->data_items [*(guint16 *)(ip + 1)];
3649                         if ((o = sp [-1].data.p)) {
3650                                 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3651                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3652                                 if (!isinst_obj)
3653                                         sp [-1].data.p = NULL;
3654                         }
3655                         ip += 2;
3656                         MINT_IN_BREAK;
3657                 MINT_IN_CASE(MINT_CONV_R_UN_I4)
3658                         sp [-1].data.f = (double)(guint32)sp [-1].data.i;
3659                         ++ip;
3660                         MINT_IN_BREAK;
3661                 MINT_IN_CASE(MINT_CONV_R_UN_I8)
3662                         sp [-1].data.f = (double)(guint64)sp [-1].data.l;
3663                         ++ip;
3664                         MINT_IN_BREAK;
3665                 MINT_IN_CASE(MINT_UNBOX)
3666                         c = rtm->data_items[*(guint16 *)(ip + 1)];
3667                         
3668                         o = sp [-1].data.p;
3669                         if (!o)
3670                                 THROW_EX (mono_get_exception_null_reference (), ip);
3671
3672                         MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
3673                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3674                         if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
3675                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
3676
3677                         sp [-1].data.p = mono_object_unbox (o);
3678                         ip += 2;
3679                         MINT_IN_BREAK;
3680                 MINT_IN_CASE(MINT_THROW)
3681                         --sp;
3682                         frame->ex_handler = NULL;
3683                         if (!sp->data.p)
3684                                 sp->data.p = mono_get_exception_null_reference ();
3685
3686                         THROW_EX ((MonoException *)sp->data.p, ip);
3687                         MINT_IN_BREAK;
3688                 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
3689                         o = sp [-1].data.p;
3690                         sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3691                         ip += 2;
3692                         MINT_IN_BREAK;
3693                 MINT_IN_CASE(MINT_LDFLDA)
3694                         o = sp [-1].data.p;
3695                         if (!o)
3696                                 THROW_EX (mono_get_exception_null_reference (), ip);
3697                         sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
3698                         ip += 2;
3699                         MINT_IN_BREAK;
3700                 MINT_IN_CASE(MINT_CKNULL)
3701                         o = sp [-1].data.p;
3702                         if (!o)
3703                                 THROW_EX (mono_get_exception_null_reference (), ip);
3704                         ++ip;
3705                         MINT_IN_BREAK;
3706
3707 #define LDFLD(datamem, fieldtype) \
3708         o = sp [-1].data.p; \
3709         if (!o) \
3710                 THROW_EX (mono_get_exception_null_reference (), ip); \
3711         sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
3712         ip += 2;
3713
3714                 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
3715                 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
3716                 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
3717                 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
3718                 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
3719                 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
3720                 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
3721                 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
3722                 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
3723                 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
3724
3725                 MINT_IN_CASE(MINT_LDFLD_VT)
3726                         o = sp [-1].data.p;
3727                         if (!o)
3728                                 THROW_EX (mono_get_exception_null_reference (), ip);
3729                         i32 = READ32(ip + 2);
3730                         sp [-1].data.p = vt_sp;
3731                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3732                         vt_sp += (i32 + 7) & ~7;
3733                         ip += 4;
3734                         MINT_IN_BREAK;
3735
3736                 MINT_IN_CASE(MINT_LDRMFLD) {
3737                         gpointer tmp;
3738                         MonoClassField *field;
3739                         char *addr;
3740
3741                         o = sp [-1].data.p;
3742                         if (!o)
3743                                 THROW_EX (mono_get_exception_null_reference (), ip);
3744                         field = rtm->data_items[* (guint16 *)(ip + 1)];
3745                         ip += 2;
3746 #ifndef DISABLE_REMOTING
3747                         if (mono_object_is_transparent_proxy (o)) {
3748                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3749
3750                                 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3751                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3752                         } else
3753 #endif
3754                                 addr = (char*)o + field->offset;
3755
3756                         stackval_from_data (field->type, &sp [-1], addr, FALSE);
3757                         MINT_IN_BREAK;
3758                 }
3759
3760                 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3761                         MonoClassField *field;
3762                         char *addr;
3763                         gpointer tmp;
3764
3765                         o = sp [-1].data.p;
3766                         if (!o)
3767                                 THROW_EX (mono_get_exception_null_reference (), ip);
3768                         field = rtm->data_items[* (guint16 *)(ip + 1)];
3769                         i32 = READ32(ip + 2);
3770                         ip += 4;
3771 #ifndef DISABLE_REMOTING
3772                         if (mono_object_is_transparent_proxy (o)) {
3773                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3774                                 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3775                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3776                         } else
3777 #endif
3778                                 addr = (char*)o + field->offset;
3779
3780                         sp [-1].data.p = vt_sp;
3781                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3782                         vt_sp += (i32 + 7) & ~7;
3783                         memcpy(sp [-1].data.p, addr, i32);
3784                         MINT_IN_BREAK;
3785                 }
3786
3787 #define STFLD(datamem, fieldtype) \
3788         o = sp [-2].data.p; \
3789         if (!o) \
3790                 THROW_EX (mono_get_exception_null_reference (), ip); \
3791         sp -= 2; \
3792         * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3793         ip += 2;
3794
3795                 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3796                 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3797                 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3798                 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3799                 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3800                 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3801                 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3802                 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3803                 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3804                 MINT_IN_CASE(MINT_STFLD_O)
3805                         o = sp [-2].data.p;
3806                         if (!o)
3807                                 THROW_EX (mono_get_exception_null_reference (), ip);
3808                         sp -= 2;
3809                         mono_gc_wbarrier_set_field (o, (char *) o + * (guint16 *)(ip + 1), sp [1].data.p);
3810                         ip += 2;
3811                         MINT_IN_BREAK;
3812
3813                 MINT_IN_CASE(MINT_STFLD_VT)
3814                         o = sp [-2].data.p;
3815                         if (!o)
3816                                 THROW_EX (mono_get_exception_null_reference (), ip);
3817                         i32 = READ32(ip + 2);
3818                         sp -= 2;
3819                         memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3820                         vt_sp -= (i32 + 7) & ~7;
3821                         ip += 4;
3822                         MINT_IN_BREAK;
3823
3824                 MINT_IN_CASE(MINT_STRMFLD) {
3825                         MonoClassField *field;
3826
3827                         o = sp [-2].data.p;
3828                         if (!o)
3829                                 THROW_EX (mono_get_exception_null_reference (), ip);
3830                         
3831                         field = rtm->data_items[* (guint16 *)(ip + 1)];
3832                         ip += 2;
3833
3834 #ifndef DISABLE_REMOTING
3835                         if (mono_object_is_transparent_proxy (o)) {
3836                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3837                                 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3838                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3839                         } else
3840 #endif
3841                                 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3842
3843                         sp -= 2;
3844                         MINT_IN_BREAK;
3845                 }
3846                 MINT_IN_CASE(MINT_STRMFLD_VT) {
3847                         MonoClassField *field;
3848
3849                         o = sp [-2].data.p;
3850                         if (!o)
3851                                 THROW_EX (mono_get_exception_null_reference (), ip);
3852                         field = rtm->data_items[* (guint16 *)(ip + 1)];
3853                         i32 = READ32(ip + 2);
3854                         ip += 4;
3855
3856 #ifndef DISABLE_REMOTING
3857                         if (mono_object_is_transparent_proxy (o)) {
3858                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3859                                 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3860                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3861                         } else
3862 #endif
3863                                 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3864
3865                         sp -= 2;
3866                         vt_sp -= (i32 + 7) & ~7;
3867                         MINT_IN_BREAK;
3868                 }
3869                 MINT_IN_CASE(MINT_LDSFLDA) {
3870                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3871                         sp->data.p = mono_class_static_field_address (rtm->domain, field);
3872                         ip += 2;
3873                         ++sp;
3874                         MINT_IN_BREAK;
3875                 }
3876                 MINT_IN_CASE(MINT_LDSFLD) {
3877                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3878                         gpointer addr = mono_class_static_field_address (rtm->domain, field);
3879                         stackval_from_data (field->type, sp, addr, FALSE);
3880                         ip += 2;
3881                         ++sp;
3882                         MINT_IN_BREAK;
3883                 }
3884                 MINT_IN_CASE(MINT_LDSFLD_VT) {
3885                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3886                         gpointer addr = mono_class_static_field_address (rtm->domain, field);
3887                         int size = READ32 (ip + 2);
3888                         ip += 4;
3889
3890                         sp->data.p = vt_sp;
3891                         vt_sp += (size + 7) & ~7;
3892                         stackval_from_data (field->type, sp, addr, FALSE);
3893                         ++sp;
3894                         MINT_IN_BREAK;
3895                 }
3896                 MINT_IN_CASE(MINT_STSFLD) {
3897                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3898                         gpointer addr = mono_class_static_field_address (rtm->domain, field);
3899                         ip += 2;
3900                         --sp;
3901                         stackval_to_data (field->type, sp, addr, FALSE);
3902                         MINT_IN_BREAK;
3903                 }
3904                 MINT_IN_CASE(MINT_STSFLD_VT) {
3905                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3906                         gpointer addr = mono_class_static_field_address (rtm->domain, field);
3907                         int size = READ32 (ip + 2);
3908                         ip += 4;
3909
3910                         --sp;
3911                         stackval_to_data (field->type, sp, addr, FALSE);
3912                         vt_sp -= (size + 7) & ~7;
3913                         MINT_IN_BREAK;
3914                 }
3915                 MINT_IN_CASE(MINT_STOBJ_VT) {
3916                         int size;
3917                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3918                         ip += 2;
3919                         size = mono_class_value_size (c, NULL);
3920                         memcpy(sp [-2].data.p, sp [-1].data.p, size);
3921                         vt_sp -= (size + 7) & ~7;
3922                         sp -= 2;
3923                         MINT_IN_BREAK;
3924                 }
3925                 MINT_IN_CASE(MINT_STOBJ) {
3926                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3927                         ip += 2;
3928
3929                         g_assert (!c->byval_arg.byref);
3930                         if (MONO_TYPE_IS_REFERENCE (&c->byval_arg))
3931                                 mono_gc_wbarrier_generic_store (sp [-2].data.p, sp [-1].data.p);
3932                         else
3933                                 stackval_from_data (&c->byval_arg, sp [-2].data.p, (char *) &sp [-1].data.p, FALSE);
3934                         sp -= 2;
3935                         MINT_IN_BREAK;
3936                 }
3937                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3938                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3939                                 THROW_EX (mono_get_exception_overflow (), ip);
3940                         sp [-1].data.i = (guint32)sp [-1].data.f;
3941                         ++ip;
3942                         MINT_IN_BREAK;
3943                 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3944                         if (sp [-1].data.i < 0)
3945                                 THROW_EX (mono_get_exception_overflow (), ip);
3946                         sp [-1].data.l = sp [-1].data.i;
3947                         ++ip;
3948                         MINT_IN_BREAK;
3949                 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
3950                         if (sp [-1].data.l < 0)
3951                                 THROW_EX (mono_get_exception_overflow (), ip);
3952                         ++ip;
3953                         MINT_IN_BREAK;
3954                 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
3955                         if ((guint64) sp [-1].data.l > MYGINT64_MAX)
3956                                 THROW_EX (mono_get_exception_overflow (), ip);
3957                         ++ip;
3958                         MINT_IN_BREAK;
3959                 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3960                 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3961                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGINT64_MAX)
3962                                 THROW_EX (mono_get_exception_overflow (), ip);
3963                         sp [-1].data.l = (guint64)sp [-1].data.f;
3964                         ++ip;
3965                         MINT_IN_BREAK;
3966                 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3967                         if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3968                                 THROW_EX (mono_get_exception_overflow (), ip);
3969                         sp [-1].data.l = (gint64)sp [-1].data.f;
3970                         ++ip;
3971                         MINT_IN_BREAK;
3972                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3973                         if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3974                                 THROW_EX (mono_get_exception_overflow (), ip);
3975                         sp [-1].data.i = (mono_u)sp [-1].data.l;
3976                         ++ip;
3977                         MINT_IN_BREAK;
3978                 MINT_IN_CASE(MINT_BOX) {
3979                         c = rtm->data_items [* (guint16 *)(ip + 1)];
3980                         guint16 offset = * (guint16 *)(ip + 2);
3981
3982                         if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype && !(mono_class_is_magic_int (c) || mono_class_is_magic_float (c))) {
3983                                 int size = mono_class_value_size (c, NULL);
3984                                 sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, sp [-1 - offset].data.p, &error);
3985                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3986                                 size = (size + 7) & ~7;
3987                                 vt_sp -= size;
3988                         } else {
3989                                 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
3990                                 sp [-1 - offset].data.p = mono_value_box_checked (rtm->domain, c, &sp [-1 - offset], &error);
3991                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3992                         }
3993                         ip += 3;
3994                         MINT_IN_BREAK;
3995                 }
3996                 MINT_IN_CASE(MINT_NEWARR)
3997                         sp [-1].data.p = (MonoObject*) mono_array_new_checked (rtm->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3998                         if (!mono_error_ok (&error)) {
3999                                 THROW_EX (mono_error_convert_to_exception (&error), ip);
4000                         }
4001                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4002                         ip += 2;
4003                         /*if (profiling_classes) {
4004                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
4005                                 count++;
4006                                 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
4007                         }*/
4008
4009                         MINT_IN_BREAK;
4010                 MINT_IN_CASE(MINT_LDLEN)
4011                         o = sp [-1].data.p;
4012                         if (!o)
4013                                 THROW_EX (mono_get_exception_null_reference (), ip);
4014                         sp [-1].data.nati = mono_array_length ((MonoArray *)o);
4015                         ++ip;
4016                         MINT_IN_BREAK;
4017                 MINT_IN_CASE(MINT_GETCHR) {
4018                         MonoString *s;
4019                         s = sp [-2].data.p;
4020                         if (!s)
4021                                 THROW_EX (mono_get_exception_null_reference (), ip);
4022                         i32 = sp [-1].data.i;
4023                         if (i32 < 0 || i32 >= mono_string_length (s))
4024                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4025                         --sp;
4026                         sp [-1].data.i = mono_string_chars(s)[i32];
4027                         ++ip;
4028                         MINT_IN_BREAK;
4029                 }
4030                 MINT_IN_CASE(MINT_STRLEN)
4031                         ++ip;
4032                         o = sp [-1].data.p;
4033                         if (!o)
4034                                 THROW_EX (mono_get_exception_null_reference (), ip);
4035                         sp [-1].data.i = mono_string_length ((MonoString*) o);
4036                         MINT_IN_BREAK;
4037                 MINT_IN_CASE(MINT_ARRAY_RANK)
4038                         o = sp [-1].data.p;
4039                         if (!o)
4040                                 THROW_EX (mono_get_exception_null_reference (), ip);
4041                         sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
4042                         ip++;
4043                         MINT_IN_BREAK;
4044                 MINT_IN_CASE(MINT_LDELEMA)
4045                 MINT_IN_CASE(MINT_LDELEMA_TC) {
4046                         gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
4047                         
4048                         MonoClass *klass = rtm->data_items [*(guint16 *) (ip + 1)];
4049                         guint16 numargs = *(guint16 *) (ip + 2);
4050                         ip += 3;
4051                         sp -= numargs;
4052
4053                         o = sp [0].data.p;
4054                         sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
4055                         if (frame->ex)
4056                                 THROW_EX (frame->ex, ip);
4057                         ++sp;
4058
4059                         MINT_IN_BREAK;
4060                 }
4061                 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
4062                 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
4063                 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
4064                 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
4065                 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
4066                 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
4067                 MINT_IN_CASE(MINT_LDELEM_I8)  /* fall through */
4068                 MINT_IN_CASE(MINT_LDELEM_I)  /* fall through */
4069                 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
4070                 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
4071                 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
4072                 MINT_IN_CASE(MINT_LDELEM_VT) {
4073                         MonoArray *o;
4074                         mono_u aindex;
4075
4076                         sp -= 2;
4077
4078                         o = sp [0].data.p;
4079                         if (!o)
4080                                 THROW_EX (mono_get_exception_null_reference (), ip);
4081
4082                         aindex = sp [1].data.i;
4083                         if (aindex >= mono_array_length (o))
4084                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4085
4086                         /*
4087                          * FIXME: throw mono_get_exception_array_type_mismatch () if needed 
4088                          */
4089                         switch (*ip) {
4090                         case MINT_LDELEM_I1:
4091                                 sp [0].data.i = mono_array_get (o, gint8, aindex);
4092                                 break;
4093                         case MINT_LDELEM_U1:
4094                                 sp [0].data.i = mono_array_get (o, guint8, aindex);
4095                                 break;
4096                         case MINT_LDELEM_I2:
4097                                 sp [0].data.i = mono_array_get (o, gint16, aindex);
4098                                 break;
4099                         case MINT_LDELEM_U2:
4100                                 sp [0].data.i = mono_array_get (o, guint16, aindex);
4101                                 break;
4102                         case MINT_LDELEM_I:
4103                                 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
4104                                 break;
4105                         case MINT_LDELEM_I4:
4106                                 sp [0].data.i = mono_array_get (o, gint32, aindex);
4107                                 break;
4108                         case MINT_LDELEM_U4:
4109                                 sp [0].data.i = mono_array_get (o, guint32, aindex);
4110                                 break;
4111                         case MINT_LDELEM_I8:
4112                                 sp [0].data.l = mono_array_get (o, guint64, aindex);
4113                                 break;
4114                         case MINT_LDELEM_R4:
4115                                 sp [0].data.f = mono_array_get (o, float, aindex);
4116                                 break;
4117                         case MINT_LDELEM_R8:
4118                                 sp [0].data.f = mono_array_get (o, double, aindex);
4119                                 break;
4120                         case MINT_LDELEM_REF:
4121                                 sp [0].data.p = mono_array_get (o, gpointer, aindex);
4122                                 break;
4123                         case MINT_LDELEM_VT: {
4124                                 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4125                                 i32 = READ32 (ip + 2);
4126                                 char *src_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4127                                 sp [0].data.vt = vt_sp;
4128                                 stackval_from_data (&klass_vt->byval_arg, sp, src_addr, FALSE);
4129                                 vt_sp += (i32 + 7) & ~7;
4130                                 ip += 3;
4131                                 break;
4132                         }
4133                         default:
4134                                 ves_abort();
4135                         }
4136
4137                         ++ip;
4138                         ++sp;
4139                         MINT_IN_BREAK;
4140                 }
4141                 MINT_IN_CASE(MINT_STELEM_I)  /* fall through */
4142                 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */ 
4143                 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
4144                 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
4145                 MINT_IN_CASE(MINT_STELEM_U2) /* fall through */
4146                 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
4147                 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
4148                 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
4149                 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
4150                 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
4151                 MINT_IN_CASE(MINT_STELEM_VT) {
4152                         mono_u aindex;
4153
4154                         sp -= 3;
4155
4156                         o = sp [0].data.p;
4157                         if (!o)
4158                                 THROW_EX (mono_get_exception_null_reference (), ip);
4159
4160                         aindex = sp [1].data.i;
4161                         if (aindex >= mono_array_length ((MonoArray *)o))
4162                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
4163
4164                         switch (*ip) {
4165                         case MINT_STELEM_I:
4166                                 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
4167                                 break;
4168                         case MINT_STELEM_I1:
4169                                 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
4170                                 break;
4171                         case MINT_STELEM_U1:
4172                                 mono_array_set ((MonoArray *) o, guint8, aindex, sp [2].data.i);
4173                                 break;
4174                         case MINT_STELEM_I2:
4175                                 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
4176                                 break;
4177                         case MINT_STELEM_U2:
4178                                 mono_array_set ((MonoArray *)o, guint16, aindex, sp [2].data.i);
4179                                 break;
4180                         case MINT_STELEM_I4:
4181                                 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
4182                                 break;
4183                         case MINT_STELEM_I8:
4184                                 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
4185                                 break;
4186                         case MINT_STELEM_R4:
4187                                 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
4188                                 break;
4189                         case MINT_STELEM_R8:
4190                                 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
4191                                 break;
4192                         case MINT_STELEM_REF: {
4193                                 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
4194                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4195                                 if (sp [2].data.p && !isinst_obj)
4196                                         THROW_EX (mono_get_exception_array_type_mismatch (), ip);
4197                                 mono_array_setref ((MonoArray *) o, aindex, sp [2].data.p);
4198                                 break;
4199                         }
4200                         case MINT_STELEM_VT: {
4201                                 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
4202                                 i32 = READ32 (ip + 2);
4203                                 char *dst_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
4204
4205                                 stackval_to_data (&klass_vt->byval_arg, &sp [2], dst_addr, FALSE);
4206                                 vt_sp -= (i32 + 7) & ~7;
4207                                 ip += 3;
4208                                 break;
4209                         }
4210                         default:
4211                                 ves_abort();
4212                         }
4213
4214                         ++ip;
4215                         MINT_IN_BREAK;
4216                 }
4217                 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
4218                         if (sp [-1].data.i < 0)
4219                                 THROW_EX (mono_get_exception_overflow (), ip);
4220                         ++ip;
4221                         MINT_IN_BREAK;
4222                 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
4223                         if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
4224                                 THROW_EX (mono_get_exception_overflow (), ip);
4225                         sp [-1].data.i = (gint32) sp [-1].data.l;
4226                         ++ip;
4227                         MINT_IN_BREAK;
4228                 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
4229                         if (sp [-1].data.l < 0 || sp [-1].data.l > MYGINT32_MAX)
4230                                 THROW_EX (mono_get_exception_overflow (), ip);
4231                         sp [-1].data.i = (gint32) sp [-1].data.l;
4232                         ++ip;
4233                         MINT_IN_BREAK;
4234                 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
4235                         if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
4236                                 THROW_EX (mono_get_exception_overflow (), ip);
4237                         sp [-1].data.i = (gint32) sp [-1].data.f;
4238                         ++ip;
4239                         MINT_IN_BREAK;
4240                 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
4241                         if (sp [-1].data.i < 0)
4242                                 THROW_EX (mono_get_exception_overflow (), ip);
4243                         ++ip;
4244                         MINT_IN_BREAK;
4245                 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
4246                         if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
4247                                 THROW_EX (mono_get_exception_overflow (), ip);
4248                         sp [-1].data.i = (guint32) sp [-1].data.l;
4249                         ++ip;
4250                         MINT_IN_BREAK;
4251                 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
4252                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
4253                                 THROW_EX (mono_get_exception_overflow (), ip);
4254                         sp [-1].data.i = (guint32) sp [-1].data.f;
4255                         ++ip;
4256                         MINT_IN_BREAK;
4257                 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
4258                         if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
4259                                 THROW_EX (mono_get_exception_overflow (), ip);
4260                         ++ip;
4261                         MINT_IN_BREAK;
4262                 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
4263                         if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
4264                                 THROW_EX (mono_get_exception_overflow (), ip);
4265                         sp [-1].data.i = (gint16) sp [-1].data.l;
4266                         ++ip;
4267                         MINT_IN_BREAK;
4268                 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
4269                         if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
4270                                 THROW_EX (mono_get_exception_overflow (), ip);
4271                         sp [-1].data.i = (gint16) sp [-1].data.f;
4272                         ++ip;
4273                         MINT_IN_BREAK;
4274                 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
4275                         if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
4276                                 THROW_EX (mono_get_exception_overflow (), ip);
4277                         ++ip;
4278                         MINT_IN_BREAK;
4279                 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
4280                         if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
4281                                 THROW_EX (mono_get_exception_overflow (), ip);
4282                         sp [-1].data.i = (guint16) sp [-1].data.l;
4283                         ++ip;
4284                         MINT_IN_BREAK;
4285                 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
4286                         if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
4287                                 THROW_EX (mono_get_exception_overflow (), ip);
4288                         sp [-1].data.i = (guint16) sp [-1].data.f;
4289                         ++ip;
4290                         MINT_IN_BREAK;
4291                 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
4292                         if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
4293                                 THROW_EX (mono_get_exception_overflow (), ip);
4294                         ++ip;
4295                         MINT_IN_BREAK;
4296                 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
4297                         if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
4298                                 THROW_EX (mono_get_exception_overflow (), ip);
4299                         sp [-1].data.i = (gint8) sp [-1].data.l;
4300                         ++ip;
4301                         MINT_IN_BREAK;
4302                 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
4303                         if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
4304                                 THROW_EX (mono_get_exception_overflow (), ip);
4305                         sp [-1].data.i = (gint8) sp [-1].data.f;
4306                         ++ip;
4307                         MINT_IN_BREAK;
4308                 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
4309                         if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
4310                                 THROW_EX (mono_get_exception_overflow (), ip);
4311                         ++ip;
4312                         MINT_IN_BREAK;
4313                 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
4314                         if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
4315                                 THROW_EX (mono_get_exception_overflow (), ip);
4316                         sp [-1].data.i = (guint8) sp [-1].data.l;
4317                         ++ip;
4318                         MINT_IN_BREAK;
4319                 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
4320                         if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
4321                                 THROW_EX (mono_get_exception_overflow (), ip);
4322                         sp [-1].data.i = (guint8) sp [-1].data.f;
4323                         ++ip;
4324                         MINT_IN_BREAK;
4325 #if 0
4326                 MINT_IN_CASE(MINT_LDELEM) 
4327                 MINT_IN_CASE(MINT_STELEM) 
4328                 MINT_IN_CASE(MINT_UNBOX_ANY) 
4329 #endif
4330                 MINT_IN_CASE(MINT_CKFINITE)
4331                         if (!isfinite(sp [-1].data.f))
4332                                 THROW_EX (mono_get_exception_arithmetic (), ip);
4333                         ++ip;
4334                         MINT_IN_BREAK;
4335                 MINT_IN_CASE(MINT_MKREFANY) {
4336                         c = rtm->data_items [*(guint16 *)(ip + 1)];
4337
4338                         /* The value address is on the stack */
4339                         gpointer addr = sp [-1].data.p;
4340                         /* Push the typedref value on the stack */
4341                         sp [-1].data.p = vt_sp;
4342                         vt_sp += sizeof (MonoTypedRef);
4343
4344                         MonoTypedRef *tref = sp [-1].data.p;
4345                         tref->klass = c;
4346                         tref->type = &c->byval_arg;
4347                         tref->value = addr;
4348
4349                         ip += 2;
4350                         MINT_IN_BREAK;
4351                 }
4352                 MINT_IN_CASE(MINT_REFANYTYPE) {
4353                         MonoTypedRef *tref = sp [-1].data.p;
4354                         MonoType *type = tref->type;
4355
4356                         vt_sp -= sizeof (MonoTypedRef);
4357                         sp [-1].data.p = vt_sp;
4358                         vt_sp += 8;
4359                         *(gpointer*)sp [-1].data.p = type;
4360                         ip ++;
4361                         MINT_IN_BREAK;
4362                 }
4363                 MINT_IN_CASE(MINT_REFANYVAL) {
4364                         MonoTypedRef *tref = sp [-1].data.p;
4365                         gpointer addr = tref->value;
4366
4367                         vt_sp -= sizeof (MonoTypedRef);
4368
4369                         sp [-1].data.p = addr;
4370                         ip ++;
4371                         MINT_IN_BREAK;
4372                 }
4373                 MINT_IN_CASE(MINT_LDTOKEN)
4374                         sp->data.p = vt_sp;
4375                         vt_sp += 8;
4376                         * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
4377                         ip += 2;
4378                         ++sp;
4379                         MINT_IN_BREAK;
4380                 MINT_IN_CASE(MINT_ADD_OVF_I4)
4381                         if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4382                                 THROW_EX (mono_get_exception_overflow (), ip);
4383                         BINOP(i, +);
4384                         MINT_IN_BREAK;
4385                 MINT_IN_CASE(MINT_ADD_OVF_I8)
4386                         if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4387                                 THROW_EX (mono_get_exception_overflow (), ip);
4388                         BINOP(l, +);
4389                         MINT_IN_BREAK;
4390                 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
4391                         if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4392                                 THROW_EX (mono_get_exception_overflow (), ip);
4393                         BINOP_CAST(i, +, guint32);
4394                         MINT_IN_BREAK;
4395                 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
4396                         if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4397                                 THROW_EX (mono_get_exception_overflow (), ip);
4398                         BINOP_CAST(l, +, guint64);
4399                         MINT_IN_BREAK;
4400                 MINT_IN_CASE(MINT_MUL_OVF_I4)
4401                         if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4402                                 THROW_EX (mono_get_exception_overflow (), ip);
4403                         BINOP(i, *);
4404                         MINT_IN_BREAK;
4405                 MINT_IN_CASE(MINT_MUL_OVF_I8)
4406                         if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4407                                 THROW_EX (mono_get_exception_overflow (), ip);
4408                         BINOP(l, *);
4409                         MINT_IN_BREAK;
4410                 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
4411                         if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4412                                 THROW_EX (mono_get_exception_overflow (), ip);
4413                         BINOP_CAST(i, *, guint32);
4414                         MINT_IN_BREAK;
4415                 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
4416                         if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4417                                 THROW_EX (mono_get_exception_overflow (), ip);
4418                         BINOP_CAST(l, *, guint64);
4419                         MINT_IN_BREAK;
4420                 MINT_IN_CASE(MINT_SUB_OVF_I4)
4421                         if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
4422                                 THROW_EX (mono_get_exception_overflow (), ip);
4423                         BINOP(i, -);
4424                         MINT_IN_BREAK;
4425                 MINT_IN_CASE(MINT_SUB_OVF_I8)
4426                         if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
4427                                 THROW_EX (mono_get_exception_overflow (), ip);
4428                         BINOP(l, -);
4429                         MINT_IN_BREAK;
4430                 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
4431                         if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
4432                                 THROW_EX (mono_get_exception_overflow (), ip);
4433                         BINOP_CAST(i, -, guint32);
4434                         MINT_IN_BREAK;
4435                 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
4436                         if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
4437                                 THROW_EX (mono_get_exception_overflow (), ip);
4438                         BINOP_CAST(l, -, guint64);
4439                         MINT_IN_BREAK;
4440                 MINT_IN_CASE(MINT_ENDFINALLY)
4441                         ip ++;
4442                         int clause_index = *ip;
4443                         if (clause_index == exit_at_finally)
4444                                 goto exit_frame;
4445                         while (sp > frame->stack) {
4446                                 --sp;
4447                         }
4448                         if (finally_ips) {
4449                                 ip = finally_ips->data;
4450                                 finally_ips = g_slist_remove (finally_ips, ip);
4451                                 goto main_loop;
4452                         }
4453                         if (frame->ex)
4454                                 goto handle_catch;
4455                         ves_abort();
4456                         MINT_IN_BREAK;
4457                 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
4458                 MINT_IN_CASE(MINT_LEAVE_S)
4459                         while (sp > frame->stack) {
4460                                 --sp;
4461                         }
4462                         frame->ip = ip;
4463                         if (*ip == MINT_LEAVE_S) {
4464                                 ip += (short) *(ip + 1);
4465                         } else {
4466                                 ip += (gint32) READ32 (ip + 1);
4467                         }
4468                         endfinally_ip = ip;
4469                         if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
4470                                 frame->ex_handler = NULL;
4471                                 frame->ex = NULL;
4472                         }
4473                         goto handle_finally;
4474                         MINT_IN_BREAK;
4475                 MINT_IN_CASE(MINT_ICALL_V_V) 
4476                 MINT_IN_CASE(MINT_ICALL_V_P)
4477                 MINT_IN_CASE(MINT_ICALL_P_V) 
4478                 MINT_IN_CASE(MINT_ICALL_P_P)
4479                 MINT_IN_CASE(MINT_ICALL_PP_V)
4480                 MINT_IN_CASE(MINT_ICALL_PI_V)
4481                 MINT_IN_CASE(MINT_ICALL_PP_P)
4482                 MINT_IN_CASE(MINT_ICALL_PI_P)
4483                 MINT_IN_CASE(MINT_ICALL_PPP_V)
4484                 MINT_IN_CASE(MINT_ICALL_PPI_V)
4485                         sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
4486                         if (*mono_thread_interruption_request_flag ()) {
4487                                 MonoException *exc = mono_thread_interruption_checkpoint ();
4488                                 if (exc) {
4489                                         frame->ex = exc;
4490                                         context->search_for_handler = 1;
4491                                 }
4492                         }
4493                         if (frame->ex != NULL)
4494                                 goto handle_exception;
4495                         ip += 2;
4496                         MINT_IN_BREAK;
4497                 MINT_IN_CASE(MINT_MONO_LDPTR) 
4498                         sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
4499                         ip += 2;
4500                         ++sp;
4501                         MINT_IN_BREAK;
4502                 MINT_IN_CASE(MINT_MONO_NEWOBJ)
4503                         sp->data.p = mono_object_new_checked (rtm->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
4504                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4505                         ip += 2;
4506                         sp++;
4507                         MINT_IN_BREAK;
4508                 MINT_IN_CASE(MINT_MONO_FREE)
4509                         ++ip;
4510                         --sp;
4511                         g_error ("that doesn't seem right");
4512                         g_free (sp->data.p);
4513                         MINT_IN_BREAK;
4514                 MINT_IN_CASE(MINT_MONO_RETOBJ)
4515                         ++ip;
4516                         sp--;
4517                         stackval_from_data (mono_method_signature (frame->imethod->method)->ret, frame->retval, sp->data.p,
4518                              mono_method_signature (frame->imethod->method)->pinvoke);
4519                         if (sp > frame->stack)
4520                                 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
4521                         goto exit_frame;
4522                 MINT_IN_CASE(MINT_MONO_TLS) {
4523                         MonoTlsKey key = *(gint32 *)(ip + 1);
4524                         sp->data.p = ((gpointer (*)()) mono_tls_get_tls_getter (key, FALSE)) ();
4525                         sp++;
4526                         ip += 3;
4527                         MINT_IN_BREAK;
4528                 }
4529                 MINT_IN_CASE(MINT_MONO_MEMORY_BARRIER) {
4530                         ++ip;
4531                         mono_memory_barrier ();
4532                         MINT_IN_BREAK;
4533                 }
4534                 MINT_IN_CASE(MINT_MONO_JIT_ATTACH) {
4535                         ++ip;
4536
4537                         context->original_domain = NULL;
4538                         MonoDomain *tls_domain = (MonoDomain *) ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4539                         gpointer tls_jit = ((gpointer (*)()) mono_tls_get_tls_getter (TLS_KEY_DOMAIN, FALSE)) ();
4540
4541                         if (tls_domain != rtm->domain || !tls_jit)
4542                                 context->original_domain = mono_jit_thread_attach (rtm->domain);
4543                         MINT_IN_BREAK;
4544                 }
4545                 MINT_IN_CASE(MINT_MONO_JIT_DETACH)
4546                         ++ip;
4547                         mono_jit_set_domain (context->original_domain);
4548                         MINT_IN_BREAK;
4549                 MINT_IN_CASE(MINT_MONO_LDDOMAIN)
4550                         sp->data.p = mono_domain_get ();
4551                         ++sp;
4552                         ++ip;
4553                         MINT_IN_BREAK;
4554                 MINT_IN_CASE(MINT_SDB_INTR_LOC)
4555                         if (G_UNLIKELY (ss_enabled)) {
4556                                 static void (*ss_tramp) (void);
4557
4558                                 if (!ss_tramp) {
4559                                         void *tramp = mini_get_single_step_trampoline ();
4560                                         mono_memory_barrier ();
4561                                         ss_tramp = tramp;
4562                                 }
4563
4564                                 /*
4565                                  * Make this point to the MINT_SDB_SEQ_POINT instruction which follows this since
4566                                  * the address of that instruction is stored as the seq point address.
4567                                  */
4568                                 frame->ip = ip + 1;
4569
4570                                 /*
4571                                  * Use the same trampoline as the JIT. This ensures that
4572                                  * the debugger has the context for the last interpreter
4573                                  * native frame.
4574                                  */
4575                                 do_debugger_tramp (ss_tramp, frame);
4576
4577                                 if (context->has_resume_state) {
4578                                         if (frame == context->handler_frame)
4579                                                 SET_RESUME_STATE (context);
4580                                         else
4581                                                 goto exit_frame;
4582                                 }
4583                         }
4584                         ++ip;
4585                         MINT_IN_BREAK;
4586                 MINT_IN_CASE(MINT_SDB_SEQ_POINT)
4587                         /* Just a placeholder for a breakpoint */
4588                         ++ip;
4589                         MINT_IN_BREAK;
4590                 MINT_IN_CASE(MINT_SDB_BREAKPOINT) {
4591                         static void (*bp_tramp) (void);
4592                         if (!bp_tramp) {
4593                                 void *tramp = mini_get_breakpoint_trampoline ();
4594                                 mono_memory_barrier ();
4595                                 bp_tramp = tramp;
4596                         }
4597
4598                         frame->ip = ip;
4599
4600                         /* Use the same trampoline as the JIT */
4601                         do_debugger_tramp (bp_tramp, frame);
4602
4603                         if (context->has_resume_state) {
4604                                 if (frame == context->handler_frame)
4605                                         SET_RESUME_STATE (context);
4606                                 else
4607                                         goto exit_frame;
4608                         }
4609
4610                         ++ip;
4611                         MINT_IN_BREAK;
4612                 }
4613
4614 #define RELOP(datamem, op) \
4615         --sp; \
4616         sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
4617         ++ip;
4618
4619 #define RELOP_FP(datamem, op, noorder) \
4620         --sp; \
4621         if (isunordered (sp [-1].data.datamem, sp [0].data.datamem)) \
4622                 sp [-1].data.i = noorder; \
4623         else \
4624                 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
4625         ++ip;
4626
4627                 MINT_IN_CASE(MINT_CEQ_I4)
4628                         RELOP(i, ==);
4629                         MINT_IN_BREAK;
4630                 MINT_IN_CASE(MINT_CEQ0_I4)
4631                         sp [-1].data.i = (sp [-1].data.i == 0);
4632                         ++ip;
4633                         MINT_IN_BREAK;
4634                 MINT_IN_CASE(MINT_CEQ_I8)
4635                         RELOP(l, ==);
4636                         MINT_IN_BREAK;
4637                 MINT_IN_CASE(MINT_CEQ_R8)
4638                         RELOP_FP(f, ==, 0);
4639                         MINT_IN_BREAK;
4640                 MINT_IN_CASE(MINT_CNE_I4)
4641                         RELOP(i, !=);
4642                         MINT_IN_BREAK;
4643                 MINT_IN_CASE(MINT_CNE_I8)
4644                         RELOP(l, !=);
4645                         MINT_IN_BREAK;
4646                 MINT_IN_CASE(MINT_CNE_R8)
4647                         RELOP_FP(f, !=, 0);
4648                         MINT_IN_BREAK;
4649                 MINT_IN_CASE(MINT_CGT_I4)
4650                         RELOP(i, >);
4651                         MINT_IN_BREAK;
4652                 MINT_IN_CASE(MINT_CGT_I8)
4653                         RELOP(l, >);
4654                         MINT_IN_BREAK;
4655                 MINT_IN_CASE(MINT_CGT_R8)
4656                         RELOP_FP(f, >, 0);
4657                         MINT_IN_BREAK;
4658                 MINT_IN_CASE(MINT_CGE_I4)
4659                         RELOP(i, >=);
4660                         MINT_IN_BREAK;
4661                 MINT_IN_CASE(MINT_CGE_I8)
4662                         RELOP(l, >=);
4663                         MINT_IN_BREAK;
4664                 MINT_IN_CASE(MINT_CGE_R8)
4665                         RELOP_FP(f, >=, 0);
4666                         MINT_IN_BREAK;
4667
4668 #define RELOP_CAST(datamem, op, type) \
4669         --sp; \
4670         sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
4671         ++ip;
4672
4673                 MINT_IN_CASE(MINT_CGE_UN_I4)
4674                         RELOP_CAST(l, >=, guint32);
4675                         MINT_IN_BREAK;
4676                 MINT_IN_CASE(MINT_CGE_UN_I8)
4677                         RELOP_CAST(l, >=, guint64);
4678                         MINT_IN_BREAK;
4679
4680                 MINT_IN_CASE(MINT_CGT_UN_I4)
4681                         RELOP_CAST(i, >, guint32);
4682                         MINT_IN_BREAK;
4683                 MINT_IN_CASE(MINT_CGT_UN_I8)
4684                         RELOP_CAST(l, >, guint64);
4685                         MINT_IN_BREAK;
4686                 MINT_IN_CASE(MINT_CGT_UN_R8)
4687                         RELOP_FP(f, >, 1);
4688                         MINT_IN_BREAK;
4689                 MINT_IN_CASE(MINT_CLT_I4)
4690                         RELOP(i, <);
4691                         MINT_IN_BREAK;
4692                 MINT_IN_CASE(MINT_CLT_I8)
4693                         RELOP(l, <);
4694                         MINT_IN_BREAK;
4695                 MINT_IN_CASE(MINT_CLT_R8)
4696                         RELOP_FP(f, <, 0);
4697                         MINT_IN_BREAK;
4698                 MINT_IN_CASE(MINT_CLT_UN_I4)
4699                         RELOP_CAST(i, <, guint32);
4700                         MINT_IN_BREAK;
4701                 MINT_IN_CASE(MINT_CLT_UN_I8)
4702                         RELOP_CAST(l, <, guint64);
4703                         MINT_IN_BREAK;
4704                 MINT_IN_CASE(MINT_CLT_UN_R8)
4705                         RELOP_FP(f, <, 1);
4706                         MINT_IN_BREAK;
4707                 MINT_IN_CASE(MINT_CLE_I4)
4708                         RELOP(i, <=);
4709                         MINT_IN_BREAK;
4710                 MINT_IN_CASE(MINT_CLE_I8)
4711                         RELOP(l, <=);
4712                         MINT_IN_BREAK;
4713                 MINT_IN_CASE(MINT_CLE_UN_I4)
4714                         RELOP_CAST(l, <=, guint32);
4715                         MINT_IN_BREAK;
4716                 MINT_IN_CASE(MINT_CLE_UN_I8)
4717                         RELOP_CAST(l, <=, guint64);
4718                         MINT_IN_BREAK;
4719                 MINT_IN_CASE(MINT_CLE_R8)
4720                         RELOP_FP(f, <=, 0);
4721                         MINT_IN_BREAK;
4722
4723 #undef RELOP
4724 #undef RELOP_FP
4725 #undef RELOP_CAST
4726
4727                 MINT_IN_CASE(MINT_LDFTN) {
4728                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
4729                         ++sp;
4730                         ip += 2;
4731                         MINT_IN_BREAK;
4732                 }
4733                 MINT_IN_CASE(MINT_LDVIRTFTN) {
4734                         InterpMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
4735                         ip += 2;
4736                         --sp;
4737                         if (!sp->data.p)
4738                                 THROW_EX (mono_get_exception_null_reference (), ip - 2);
4739                                 
4740                         sp->data.p = get_virtual_method (m, sp->data.p);
4741                         ++sp;
4742                         MINT_IN_BREAK;
4743                 }
4744
4745 #define LDARG(datamem, argtype) \
4746         sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
4747         ip += 2; \
4748         ++sp; 
4749         
4750                 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
4751                 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
4752                 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
4753                 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
4754                 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
4755                 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
4756                 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
4757                 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
4758                 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
4759                 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
4760
4761                 MINT_IN_CASE(MINT_LDARG_VT)
4762                         sp->data.p = vt_sp;
4763                         i32 = READ32(ip + 2);
4764                         memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
4765                         vt_sp += (i32 + 7) & ~7;
4766                         ip += 4;
4767                         ++sp;
4768                         MINT_IN_BREAK;
4769
4770 #define STARG(datamem, argtype) \
4771         --sp; \
4772         * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4773         ip += 2; \
4774         
4775                 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
4776                 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
4777                 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
4778                 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
4779                 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
4780                 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
4781                 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
4782                 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
4783                 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
4784                 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
4785
4786                 MINT_IN_CASE(MINT_STARG_VT) 
4787                         i32 = READ32(ip + 2);
4788                         --sp;
4789                         memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
4790                         vt_sp -= (i32 + 7) & ~7;
4791                         ip += 4;
4792                         MINT_IN_BREAK;
4793
4794 #define STINARG(datamem, argtype) \
4795         do { \
4796                 int n = * (guint16 *)(ip + 1); \
4797                 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
4798                 ip += 2; \
4799         } while (0)
4800         
4801                 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
4802                 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
4803                 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
4804                 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
4805                 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
4806                 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
4807                 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
4808                 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
4809                 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
4810                 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
4811
4812                 MINT_IN_CASE(MINT_STINARG_VT) {
4813                         int n = * (guint16 *)(ip + 1);
4814                         i32 = READ32(ip + 2);
4815                         memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
4816                         ip += 4;
4817                         MINT_IN_BREAK;
4818                 }
4819
4820                 MINT_IN_CASE(MINT_PROF_ENTER) {
4821                         ip += 1;
4822
4823                         if (MONO_PROFILER_ENABLED (method_enter)) {
4824                                 MonoProfilerCallContext *prof_ctx = NULL;
4825
4826                                 if (frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_ENTER_CONTEXT) {
4827                                         prof_ctx = g_new0 (MonoProfilerCallContext, 1);
4828                                         prof_ctx->interp_frame = frame;
4829                                         prof_ctx->method = frame->imethod->method;
4830                                 }
4831
4832                                 MONO_PROFILER_RAISE (method_enter, (frame->imethod->method, prof_ctx));
4833
4834                                 g_free (prof_ctx);
4835                         }
4836
4837                         MINT_IN_BREAK;
4838                 }
4839
4840                 MINT_IN_CASE(MINT_LDARGA)
4841                         sp->data.p = frame->args + * (guint16 *)(ip + 1);
4842                         ip += 2;
4843                         ++sp;
4844                         MINT_IN_BREAK;
4845
4846 #define LDLOC(datamem, argtype) \
4847         sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
4848         ip += 2; \
4849         ++sp; 
4850         
4851                 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
4852                 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
4853                 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
4854                 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
4855                 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
4856                 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
4857                 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
4858                 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
4859                 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
4860                 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
4861
4862                 MINT_IN_CASE(MINT_LDLOC_VT)
4863                         sp->data.p = vt_sp;
4864                         i32 = READ32(ip + 2);
4865                         memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
4866                         vt_sp += (i32 + 7) & ~7;
4867                         ip += 4;
4868                         ++sp;
4869                         MINT_IN_BREAK;
4870
4871                 MINT_IN_CASE(MINT_LDLOCA_S)
4872                         sp->data.p = locals + * (guint16 *)(ip + 1);
4873                         ip += 2;
4874                         ++sp;
4875                         MINT_IN_BREAK;
4876
4877 #define STLOC(datamem, argtype) \
4878         --sp; \
4879         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4880         ip += 2;
4881         
4882                 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
4883                 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
4884                 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
4885                 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
4886                 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
4887                 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
4888                 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
4889                 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
4890                 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
4891                 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
4892
4893 #define STLOC_NP(datamem, argtype) \
4894         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
4895         ip += 2;
4896
4897                 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
4898                 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
4899
4900                 MINT_IN_CASE(MINT_STLOC_VT)
4901                         i32 = READ32(ip + 2);
4902                         --sp;
4903                         memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
4904                         vt_sp -= (i32 + 7) & ~7;
4905                         ip += 4;
4906                         MINT_IN_BREAK;
4907
4908                 MINT_IN_CASE(MINT_LOCALLOC) {
4909                         if (sp != frame->stack + 1) /*FIX?*/
4910                                 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
4911
4912                         int len = sp [-1].data.i;
4913                         sp [-1].data.p = alloca (len);
4914                         MonoMethodHeader *header = mono_method_get_header_checked (frame->imethod->method, &error);
4915                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4916                         if (header && header->init_locals)
4917                                 memset (sp [-1].data.p, 0, len);
4918                         ++ip;
4919                         MINT_IN_BREAK;
4920                 }
4921                 MINT_IN_CASE(MINT_ENDFILTER)
4922                         /* top of stack is result of filter */
4923                         frame->retval = &sp [-1];
4924                         goto exit_frame;
4925                 MINT_IN_CASE(MINT_INITOBJ)
4926                         --sp;
4927                         memset (sp->data.vt, 0, READ32(ip + 1));
4928                         ip += 3;
4929                         MINT_IN_BREAK;
4930                 MINT_IN_CASE(MINT_CPBLK)
4931                         sp -= 3;
4932                         if (!sp [0].data.p || !sp [1].data.p)
4933                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4934                         ++ip;
4935                         /* FIXME: value and size may be int64... */
4936                         memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4937                         MINT_IN_BREAK;
4938 #if 0
4939                 MINT_IN_CASE(MINT_CONSTRAINED_) {
4940                         guint32 token;
4941                         /* FIXME: implement */
4942                         ++ip;
4943                         token = READ32 (ip);
4944                         ip += 2;
4945                         MINT_IN_BREAK;
4946                 }
4947 #endif
4948                 MINT_IN_CASE(MINT_INITBLK)
4949                         sp -= 3;
4950                         if (!sp [0].data.p)
4951                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4952                         ++ip;
4953                         /* FIXME: value and size may be int64... */
4954                         memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4955                         MINT_IN_BREAK;
4956 #if 0
4957                 MINT_IN_CASE(MINT_NO_)
4958                         /* FIXME: implement */
4959                         ip += 2;
4960                         MINT_IN_BREAK;
4961 #endif
4962            MINT_IN_CASE(MINT_RETHROW) {
4963                         /* 
4964                          * need to clarify what this should actually do:
4965                          * start the search from the last found handler in
4966                          * this method or continue in the caller or what.
4967                          * Also, do we need to run finally/fault handlers after a retrow?
4968                          * Well, this implementation will follow the usual search
4969                          * for an handler, considering the current ip as throw spot.
4970                          * We need to NULL frame->ex_handler for the later code to
4971                          * actually run the new found handler.
4972                          */
4973                         int exvar_offset = *(guint16*)(ip + 1);
4974                         frame->ex_handler = NULL;
4975                         THROW_EX_GENERAL (*(MonoException**)(frame->locals + exvar_offset), ip - 1, TRUE);
4976                         MINT_IN_BREAK;
4977            }
4978                 MINT_IN_DEFAULT
4979                         g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4980                         THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4981                 }
4982         }
4983
4984         g_assert_not_reached ();
4985         /*
4986          * Exception handling code.
4987          * The exception object is stored in frame->ex.
4988          */
4989
4990         handle_exception:
4991         {
4992                 int i;
4993                 guint32 ip_offset;
4994                 InterpFrame *inv;
4995                 MonoExceptionClause *clause;
4996                 /*char *message;*/
4997                 MonoObject *ex_obj;
4998
4999 #if DEBUG_INTERP
5000                 if (tracing)
5001                         g_print ("* Handling exception '%s' at IL_%04x\n", 
5002                                 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name, 
5003                                 rtm == NULL ? 0 : frame->ip - rtm->code);
5004 #endif
5005                 if (die_on_exception)
5006                         goto die_on_ex;
5007
5008                 for (inv = frame; inv; inv = inv->parent) {
5009                         MonoMethod *method;
5010                         if (inv->imethod == NULL)
5011                                 continue;
5012                         method = inv->imethod->method;
5013                         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
5014                                 continue;
5015                         if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
5016                                 continue;
5017                         if (inv->ip == NULL)
5018                                 continue;
5019                         ip_offset = inv->ip - inv->imethod->code;
5020                         inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one  - this one wins */
5021                         for (i = 0; i < inv->imethod->num_clauses; ++i) {
5022                                 clause = &inv->imethod->clauses [i];
5023 #if DEBUG_INTERP
5024                                 g_print ("* clause [%d]: %p\n", i, clause);
5025 #endif
5026                                 if (!MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
5027                                         continue;
5028                                 }
5029                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5030 #if DEBUG_INTERP
5031                                         if (tracing)
5032                                                 g_print ("* Filter found at '%s'\n", method->name);
5033 #endif
5034                                         InterpFrame dup_frame;
5035                                         stackval retval;
5036                                         memcpy (&dup_frame, inv, sizeof (InterpFrame));
5037                                         dup_frame.retval = &retval;
5038                                         ves_exec_method_with_context (&dup_frame, context, inv->imethod->code + clause->data.filter_offset, frame->ex, -1);
5039                                         if (dup_frame.retval->data.i) {
5040 #if DEBUG_INTERP
5041                                                 if (tracing)
5042                                                         g_print ("* Matched Filter at '%s'\n", method->name);
5043 #endif
5044                                                 inv->ex_handler = clause;
5045                                                 *(MonoException**)(inv->locals + inv->imethod->exvar_offsets [i]) = frame->ex;
5046                                                 goto handle_finally;
5047                                         }
5048                                 } else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
5049                                         MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
5050                                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5051                                         if (isinst_obj) {
5052                                                 /* 
5053                                                  * OK, we found an handler, now we need to execute the finally
5054                                                  * and fault blocks before branching to the handler code.
5055                                                  */
5056 #if DEBUG_INTERP
5057                                                 if (tracing)
5058                                                         g_print ("* Found handler at '%s'\n", method->name);
5059 #endif
5060                                                 inv->ex_handler = clause;
5061                                                 *(MonoException**)(inv->locals + inv->imethod->exvar_offsets [i]) = frame->ex;
5062                                                 goto handle_finally;
5063                                         }
5064                                 }
5065                         }
5066                 }
5067                 /*
5068                  * If we get here, no handler was found: print a stack trace.
5069                  */
5070                 for (inv = frame; inv; inv = inv->parent) {
5071                         if (inv->invoke_trap)
5072                                 goto handle_finally;
5073                 }
5074 die_on_ex:
5075                 ex_obj = (MonoObject *) frame->ex;
5076                 mono_unhandled_exception (ex_obj);
5077                 MonoJitTlsData *jit_tls = (MonoJitTlsData *) mono_tls_get_jit_tls ();
5078                 jit_tls->abort_func (ex_obj);
5079                 g_assert_not_reached ();
5080         }
5081         handle_finally:
5082         {
5083                 int i;
5084                 guint32 ip_offset;
5085                 MonoExceptionClause *clause;
5086                 GSList *old_list = finally_ips;
5087                 MonoMethod *method = frame->imethod->method;
5088                 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5089                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5090                 
5091 #if DEBUG_INTERP
5092                 if (tracing)
5093                         g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
5094 #endif
5095                 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
5096                                 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
5097                         goto exit_frame;
5098                 }
5099                 ip_offset = frame->ip - rtm->code;
5100
5101                 if (endfinally_ip != NULL)
5102                         finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
5103                 for (i = 0; i < header->num_clauses; ++i)
5104                         if (frame->ex_handler == &rtm->clauses [i])
5105                                 break;
5106                 while (i > 0) {
5107                         --i;
5108                         clause = &rtm->clauses [i];
5109                         if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
5110                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
5111                                         ip = rtm->code + clause->handler_offset;
5112                                         finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
5113 #if DEBUG_INTERP
5114                                         if (tracing)
5115                                                 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
5116 #endif
5117                                 }
5118                         }
5119                 }
5120
5121                 endfinally_ip = NULL;
5122
5123                 if (old_list != finally_ips && finally_ips) {
5124                         ip = finally_ips->data;
5125                         finally_ips = g_slist_remove (finally_ips, ip);
5126                         sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
5127                         goto main_loop;
5128                 }
5129
5130                 /*
5131                  * If an exception is set, we need to execute the fault handler, too,
5132                  * otherwise, we continue normally.
5133                  */
5134                 if (frame->ex)
5135                         goto handle_fault;
5136                 ves_abort();
5137         }
5138         handle_fault:
5139         {
5140                 int i;
5141                 guint32 ip_offset;
5142                 MonoExceptionClause *clause;
5143                 GSList *old_list = finally_ips;
5144                 MonoMethod *method = frame->imethod->method;
5145                 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
5146                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5147                 
5148 #if DEBUG_INTERP
5149                 if (tracing)
5150                         g_print ("* Handle fault\n");
5151 #endif
5152                 ip_offset = frame->ip - rtm->code;
5153                 for (i = 0; i < header->num_clauses; ++i) {
5154                         clause = &rtm->clauses [i];
5155                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
5156                                 ip = rtm->code + clause->handler_offset;
5157                                 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
5158 #if DEBUG_INTERP
5159                                 if (tracing)
5160                                         g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
5161 #endif
5162                         }
5163                 }
5164
5165                 if (old_list != finally_ips && finally_ips) {
5166                         ip = finally_ips->data;
5167                         finally_ips = g_slist_remove (finally_ips, ip);
5168                         sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
5169                         goto main_loop;
5170                 }
5171         }
5172         handle_catch:
5173         {
5174                 /*
5175                  * If the handler for the exception was found in this method, we jump
5176                  * to it right away, otherwise we return and let the caller run
5177                  * the finally, fault and catch blocks.
5178                  * This same code should be present in the endfault opcode, but it
5179                  * is corrently not assigned in the ECMA specs: LAMESPEC.
5180                  */
5181                 if (frame->ex_handler) {
5182 #if DEBUG_INTERP
5183                         if (tracing)
5184                                 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
5185 #endif
5186                         ip = rtm->code + frame->ex_handler->handler_offset;
5187                         sp = frame->stack;
5188                         vt_sp = (unsigned char *) sp + rtm->stack_size;
5189                         sp->data.p = frame->ex;
5190                         ++sp;
5191                         goto main_loop;
5192                 }
5193                 goto exit_frame;
5194         }
5195 exit_frame:
5196
5197         if (!frame->ex && MONO_PROFILER_ENABLED (method_leave) &&
5198             frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE) {
5199                 MonoProfilerCallContext *prof_ctx = NULL;
5200
5201                 if (frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_LEAVE_CONTEXT) {
5202                         prof_ctx = g_new0 (MonoProfilerCallContext, 1);
5203                         prof_ctx->interp_frame = frame;
5204                         prof_ctx->method = frame->imethod->method;
5205
5206                         MonoType *rtype = mono_method_signature (frame->imethod->method)->ret;
5207
5208                         switch (rtype->type) {
5209                         case MONO_TYPE_VOID:
5210                                 break;
5211                         case MONO_TYPE_VALUETYPE:
5212                                 prof_ctx->return_value = frame->retval->data.p;
5213                                 break;
5214                         default:
5215                                 prof_ctx->return_value = frame->retval;
5216                                 break;
5217                         }
5218                 }
5219
5220                 MONO_PROFILER_RAISE (method_leave, (frame->imethod->method, prof_ctx));
5221
5222                 g_free (prof_ctx);
5223         } else if (frame->ex && frame->imethod->prof_flags & MONO_PROFILER_CALL_INSTRUMENTATION_EXCEPTION_LEAVE)
5224                 MONO_PROFILER_RAISE (method_exception_leave, (frame->imethod->method, &frame->ex->object));
5225
5226         DEBUG_LEAVE ();
5227 }
5228
5229 void
5230 ves_exec_method (InterpFrame *frame)
5231 {
5232         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5233         ThreadContext context_struct;
5234         MonoDomain *domain = frame->imethod->domain;
5235         MonoError error;
5236         jmp_buf env;
5237
5238         frame->ex = NULL;
5239
5240         if (setjmp(env)) {
5241                 mono_unhandled_exception ((MonoObject*)frame->ex);
5242                 return;
5243         }
5244         if (context == NULL) {
5245                 context = &context_struct;
5246                 context_struct.base_frame = frame;
5247                 context_struct.current_frame = NULL;
5248                 context_struct.env_frame = frame;
5249                 context_struct.current_env = &env;
5250                 context_struct.search_for_handler = 0;
5251                 context_struct.managed_code = 0;
5252                 set_context (context);
5253         }
5254         frame->ip = NULL;
5255         frame->parent = context->current_frame;
5256         frame->imethod = mono_interp_get_imethod (domain, frame->method, &error);
5257         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
5258         context->managed_code = 1;
5259         ves_exec_method_with_context (frame, context, NULL, NULL, -1);
5260         context->managed_code = 0;
5261         if (frame->ex) {
5262                 if (context != &context_struct && context->current_env) {
5263                         context->env_frame->ex = frame->ex;
5264                         longjmp (*context->current_env, 1);
5265                 }
5266                 else
5267                         mono_unhandled_exception ((MonoObject*)frame->ex);
5268         }
5269         if (context->base_frame == frame)
5270                 set_context (NULL);
5271         else
5272                 context->current_frame = frame->parent;
5273 }
5274
5275 void
5276 mono_interp_parse_options (const char *options)
5277 {
5278         char **args, **ptr;
5279
5280         args = g_strsplit (options, ",", -1);
5281         for (ptr = args; ptr && *ptr; ptr ++) {
5282                 char *arg = *ptr;
5283
5284                 if (strncmp (arg, "jit=", 4) == 0)
5285                         jit_classes = g_slist_prepend (jit_classes, arg + 4);
5286         }
5287 }
5288
5289 void
5290 mono_interp_init ()
5291 {
5292         mono_native_tls_alloc (&thread_context_id, NULL);
5293         set_context (NULL);
5294
5295         mono_interp_transform_init ();
5296 }
5297
5298 typedef int (*TestMethod) (void);
5299
5300 static void
5301 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
5302 {
5303         int result, expected, failed, cfailed, run;
5304         double elapsed, transform_time;
5305         int i;
5306         MonoObject *result_obj;
5307         static gboolean filter_method_init = FALSE;
5308         static const char *filter_method = NULL;
5309
5310         g_print ("Test run: image=%s\n", mono_image_get_filename (image));
5311         cfailed = failed = run = 0;
5312         transform_time = elapsed = 0.0;
5313
5314         g_timer_start (timer);
5315         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5316                 MonoObject *exc = NULL;
5317                 MonoError error;
5318                 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5319                 if (!method) {
5320                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
5321                         continue;
5322                 }
5323
5324                 if (!filter_method_init) {
5325                         filter_method = g_getenv ("INTERP_FILTER_METHOD");
5326                         filter_method_init = TRUE;
5327                 }
5328                 gboolean filter = FALSE;
5329                 if (filter_method) {
5330                         const char *name = filter_method;
5331
5332                         if ((strchr (name, '.') > name) || strchr (name, ':')) {
5333                                 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
5334                                 filter = mono_method_desc_full_match (desc, method);
5335                                 mono_method_desc_free (desc);
5336                         } else {
5337                                 filter = strcmp (method->name, name) == 0;
5338                         }
5339                 } else { /* no filter, check for `Category' attribute on method */
5340                         filter = TRUE;
5341                         MonoCustomAttrInfo* ainfo = mono_custom_attrs_from_method_checked (method, &error);
5342                         mono_error_cleanup (&error);
5343
5344                         if (ainfo) {
5345                                 int j;
5346                                 for (j = 0; j < ainfo->num_attrs && filter; ++j) {
5347                                         MonoCustomAttrEntry *centry = &ainfo->attrs [j];
5348                                         if (centry->ctor == NULL)
5349                                                 continue;
5350
5351                                         MonoClass *klass = centry->ctor->klass;
5352                                         if (strcmp (klass->name, "CategoryAttribute"))
5353                                                 continue;
5354
5355                                         MonoObject *obj = mono_custom_attrs_get_attr_checked (ainfo, klass, &error);
5356                                         /* FIXME: there is an ordering problem if there're multiple attributes, do this instead:
5357                                          * MonoObject *obj = create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, &error); */
5358                                         mono_error_cleanup (&error);
5359                                         MonoMethod *getter = mono_class_get_method_from_name (klass, "get_Category", -1);
5360                                         MonoObject *str = mono_interp_runtime_invoke (getter, obj, NULL, &exc, &error);
5361                                         mono_error_cleanup (&error);
5362                                         char *utf8_str = mono_string_to_utf8_checked ((MonoString *) str, &error);
5363                                         mono_error_cleanup (&error);
5364                                         if (!strcmp (utf8_str, "!INTERPRETER")) {
5365                                                 g_print ("skip %s...\n", method->name);
5366                                                 filter = FALSE;
5367                                         }
5368                                 }
5369                         }
5370                 }
5371                 if (strncmp (method->name, "test_", 5) == 0 && filter) {
5372                         MonoError interp_error;
5373                         MonoObject *exc = NULL;
5374
5375                         result_obj = mono_interp_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
5376                         if (!mono_error_ok (&interp_error)) {
5377                                 cfailed++;
5378                                 g_print ("Test '%s' execution failed.\n", method->name);
5379                         } else if (exc != NULL) {
5380                                 g_print ("Exception in Test '%s' occured:\n", method->name);
5381                                 mono_object_describe (exc);
5382                                 run++;
5383                                 failed++;
5384                         } else {
5385                                 result = *(gint32 *) mono_object_unbox (result_obj);
5386                                 expected = atoi (method->name + 5);  // FIXME: oh no.
5387                                 run++;
5388
5389                                 if (result != expected) {
5390                                         failed++;
5391                                         g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
5392                                 }
5393                         }
5394                 }
5395         }
5396         g_timer_stop (timer);
5397         elapsed = g_timer_elapsed (timer, NULL);
5398         if (failed > 0 || cfailed > 0){
5399                 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
5400                                 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
5401         } else {
5402                 g_print ("Results: total tests: %d, all pass \n",  run);
5403         }
5404
5405         g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
5406                         elapsed - transform_time, transform_time);
5407         *total += failed + cfailed;
5408         *total_run += run;
5409 }
5410
5411 static int
5412 interp_regression (MonoImage *image, int verbose, int *total_run)
5413 {
5414         MonoMethod *method;
5415         GTimer *timer = g_timer_new ();
5416         MonoDomain *domain = mono_domain_get ();
5417         guint32 i;
5418         int total;
5419
5420         /* load the metadata */
5421         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
5422                 MonoError error;
5423                 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
5424                 if (!method) {
5425                         mono_error_cleanup (&error);
5426                         continue;
5427                 }
5428                 mono_class_init (method->klass);
5429         }
5430
5431         total = 0;
5432         *total_run = 0;
5433         interp_regression_step (image, verbose, total_run, &total, timer, domain);
5434
5435         g_timer_destroy (timer);
5436         return total;
5437 }
5438
5439 int
5440 mono_interp_regression_list (int verbose, int count, char *images [])
5441 {
5442         int i, total, total_run, run;
5443         
5444         total_run = total = 0;
5445         for (i = 0; i < count; ++i) {
5446                 MonoAssembly *ass = mono_assembly_open_predicate (images [i], FALSE, FALSE, NULL, NULL, NULL);
5447                 if (!ass) {
5448                         g_warning ("failed to load assembly: %s", images [i]);
5449                         continue;
5450                 }
5451                 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
5452                 total_run += run;
5453         }
5454         if (total > 0) {
5455                 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
5456         } else {
5457                 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);
5458         }
5459         
5460         return total;
5461 }
5462
5463 /*
5464  * mono_interp_set_resume_state:
5465  *
5466  *   Set the state the interpeter will continue to execute from after execution returns to the interpreter.
5467  */
5468 void
5469 mono_interp_set_resume_state (MonoJitTlsData *jit_tls, MonoException *ex, MonoInterpFrameHandle interp_frame, gpointer handler_ip)
5470 {
5471         ThreadContext *context;
5472
5473         g_assert (jit_tls);
5474         context = jit_tls->interp_context;
5475         g_assert (context);
5476
5477         context->has_resume_state = TRUE;
5478         context->handler_frame = interp_frame;
5479         /* This is on the stack, so it doesn't need a wbarrier */
5480         context->handler_frame->ex = ex;
5481         context->handler_ip = handler_ip;
5482 }
5483
5484 /*
5485  * mono_interp_run_finally:
5486  *
5487  *   Run the finally clause identified by CLAUSE_INDEX in the intepreter frame given by
5488  * frame->interp_frame.
5489  */
5490 void
5491 mono_interp_run_finally (StackFrameInfo *frame, int clause_index, gpointer handler_ip)
5492 {
5493        InterpFrame *iframe = frame->interp_frame;
5494        ThreadContext *context = mono_native_tls_get_value (thread_context_id);
5495
5496        ves_exec_method_with_context (iframe, context, handler_ip, NULL, clause_index);
5497 }
5498
5499 typedef struct {
5500         InterpFrame *current;
5501 } StackIter;
5502
5503 /*
5504  * mono_interp_frame_iter_init:
5505  *
5506  *   Initialize an iterator for iterating through interpreted frames.
5507  */
5508 void
5509 mono_interp_frame_iter_init (MonoInterpStackIter *iter, gpointer interp_exit_data)
5510 {
5511         StackIter *stack_iter = (StackIter*)iter;
5512
5513         stack_iter->current = (InterpFrame*)interp_exit_data;
5514 }
5515
5516 gboolean
5517 mono_interp_frame_iter_next (MonoInterpStackIter *iter, StackFrameInfo *frame)
5518 {
5519         StackIter *stack_iter = (StackIter*)iter;
5520         InterpFrame *iframe = stack_iter->current;
5521
5522         memset (frame, 0, sizeof (StackFrameInfo));
5523         /* pinvoke frames doesn't have imethod set */
5524         while (iframe && !(iframe->imethod && iframe->imethod->code))
5525                 iframe = iframe->parent;
5526         if (!iframe)
5527                 return FALSE;
5528
5529         frame->type = FRAME_TYPE_INTERP;
5530         // FIXME:
5531         frame->domain = mono_domain_get ();
5532         frame->interp_frame = iframe;
5533         frame->method = iframe->imethod->method;
5534         frame->actual_method = frame->method;
5535         /* This is the offset in the interpreter IR */
5536         frame->native_offset = (guint8*)iframe->ip - (guint8*)iframe->imethod->code;
5537         frame->ji = iframe->imethod->jinfo;
5538
5539         stack_iter->current = iframe->parent;
5540
5541         return TRUE;
5542 }
5543
5544 MonoJitInfo*
5545 mono_interp_find_jit_info (MonoDomain *domain, MonoMethod *method)
5546 {
5547         InterpMethod* rtm;
5548
5549         rtm = lookup_imethod (domain, method);
5550         if (rtm)
5551                 return rtm->jinfo;
5552         else
5553                 return NULL;
5554 }
5555
5556 void
5557 mono_interp_set_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5558 {
5559         guint16 *code = (guint16*)ip;
5560         g_assert (*code == MINT_SDB_SEQ_POINT);
5561         *code = MINT_SDB_BREAKPOINT;
5562 }
5563
5564 void
5565 mono_interp_clear_breakpoint (MonoJitInfo *jinfo, gpointer ip)
5566 {
5567         guint16 *code = (guint16*)ip;
5568         g_assert (*code == MINT_SDB_BREAKPOINT);
5569         *code = MINT_SDB_SEQ_POINT;
5570 }
5571
5572 MonoJitInfo*
5573 mono_interp_frame_get_jit_info (MonoInterpFrameHandle frame)
5574 {
5575         InterpFrame *iframe = (InterpFrame*)frame;
5576
5577         g_assert (iframe->imethod);
5578         return iframe->imethod->jinfo;
5579 }
5580
5581 gpointer
5582 mono_interp_frame_get_ip (MonoInterpFrameHandle frame)
5583 {
5584         InterpFrame *iframe = (InterpFrame*)frame;
5585
5586         g_assert (iframe->imethod);
5587         return (gpointer)iframe->ip;
5588 }
5589
5590 gpointer
5591 mono_interp_frame_get_arg (MonoInterpFrameHandle frame, int pos)
5592 {
5593         InterpFrame *iframe = (InterpFrame*)frame;
5594
5595         g_assert (iframe->imethod);
5596
5597         int arg_offset = iframe->imethod->arg_offsets [pos + (iframe->imethod->hasthis ? 1 : 0)];
5598
5599         return iframe->args + arg_offset;
5600 }
5601
5602 gpointer
5603 mono_interp_frame_get_local (MonoInterpFrameHandle frame, int pos)
5604 {
5605         InterpFrame *iframe = (InterpFrame*)frame;
5606
5607         g_assert (iframe->imethod);
5608
5609         return iframe->locals + iframe->imethod->local_offsets [pos];
5610 }
5611
5612 gpointer
5613 mono_interp_frame_get_this (MonoInterpFrameHandle frame)
5614 {
5615         InterpFrame *iframe = (InterpFrame*)frame;
5616
5617         g_assert (iframe->imethod);
5618         g_assert (iframe->imethod->hasthis);
5619
5620         int arg_offset = iframe->imethod->arg_offsets [0];
5621
5622         return iframe->args + arg_offset;
5623 }
5624
5625 void
5626 mono_interp_start_single_stepping (void)
5627 {
5628         ss_enabled = TRUE;
5629 }
5630
5631 void
5632 mono_interp_stop_single_stepping (void)
5633 {
5634         ss_enabled = FALSE;
5635 }