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