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