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