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