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