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