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