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