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