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