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