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