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