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