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