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