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