2003-12-19 Bernie Solomon <bernard@ugsolutions.com>
[mono.git] / mono / 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
26 #include <mono/os/gc_wrapper.h>
27
28 #ifdef HAVE_ALLOCA_H
29 #   include <alloca.h>
30 #else
31 #   ifdef __CYGWIN__
32 #      define alloca __builtin_alloca
33 #   endif
34 #endif
35
36 /* trim excessive headers */
37 #include <mono/metadata/image.h>
38 #include <mono/metadata/assembly.h>
39 #include <mono/metadata/cil-coff.h>
40 #include <mono/metadata/mono-endian.h>
41 #include <mono/metadata/tabledefs.h>
42 #include <mono/metadata/blob.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/exception.h>
51 #include <mono/metadata/verify.h>
52 #include <mono/metadata/opcodes.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/io-layer/io-layer.h>
55 #include <mono/metadata/socket-io.h>
56 #include <mono/metadata/mono-config.h>
57 #include <mono/metadata/marshal.h>
58 #include <mono/metadata/environment.h>
59 #include <mono/metadata/mono-debug.h>
60 #include <mono/os/util.h>
61
62 /*#include <mono/cli/types.h>*/
63 #include "interp.h"
64 #include "embed.h"
65 #include "hacks.h"
66
67 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
68 #ifdef _WIN32
69 #define isnan _isnan
70 #define finite _finite
71 #endif
72 #ifndef HAVE_FINITE
73 #ifdef HAVE_ISFINITE
74 #define finite isfinite
75 #endif
76 #endif
77
78 /* If true, then we output the opcodes as we interpret them */
79 static int global_tracing = 0;
80 static int global_no_pointers = 0;
81
82 static int debug_indent_level = 0;
83
84 /*
85  * Pull the list of opcodes
86  */
87 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
88         a = i,
89
90 enum {
91 #include "mono/cil/opcode.def"
92         LAST = 0xff
93 };
94 #undef OPDEF
95
96 #if SIZEOF_VOID_P == 8
97 #define GET_NATI(sp) ((sp).type == VAL_I32 ? (sp).data.i : (sp).data.nati)
98 #else
99 #define GET_NATI(sp) (sp).data.i
100 #endif
101 #define CSIZE(x) (sizeof (x) / 4)
102
103 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,mono_method)   \
104         do {    \
105                 (frame)->parent = (parent_frame);       \
106                 (frame)->obj = (obj_this);      \
107                 (frame)->stack_args = (method_args);    \
108                 (frame)->retval = (method_retval);      \
109                 (frame)->method = (mono_method);        \
110                 (frame)->ex_handler = NULL;     \
111                 (frame)->ex = NULL;     \
112                 (frame)->child = NULL;  \
113                 (frame)->ip = NULL;     \
114                 (frame)->invoke_trap = 0;       \
115         } while (0)
116
117 typedef struct {
118         MonoInvocation *base_frame;
119         MonoInvocation *current_frame;
120         MonoInvocation *env_frame;
121         jmp_buf *current_env;
122         int search_for_handler;
123 } ThreadContext;
124
125 static MonoException * quit_exception = NULL;
126
127 void ves_exec_method (MonoInvocation *frame);
128
129 static char* dump_stack (stackval *stack, stackval *sp);
130 static char* dump_frame (MonoInvocation *inv);
131 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
132
133 typedef void (*ICallMethod) (MonoInvocation *frame);
134
135 static guint32 die_on_exception = 0;
136 static guint32 thread_context_id = 0;
137
138 #define DEBUG_INTERP 1
139 #if DEBUG_INTERP
140
141 static unsigned long opcode_count = 0;
142 static unsigned long fcall_count = 0;
143 static int break_on_method = 0;
144 static GList *db_methods = NULL;
145
146 static void
147 output_indent (void)
148 {
149         int h;
150
151         for (h = 0; h < debug_indent_level; h++)
152                 g_print ("  ");
153 }
154
155 static void
156 db_match_method (gpointer data, gpointer user_data)
157 {
158         MonoMethod *m = (MonoMethod*)user_data;
159         MonoMethodDesc *desc = data;
160
161         if (mono_method_desc_full_match (desc, m))
162                 break_on_method = 1;
163 }
164
165 #define DEBUG_ENTER()   \
166         fcall_count++;  \
167         g_list_foreach (db_methods, db_match_method, (gpointer)frame->method);  \
168         if (break_on_method) tracing=2; \
169         break_on_method = 0;    \
170         if (tracing) {  \
171                 char *mn, *args = dump_stack (frame->stack_args, frame->stack_args+signature->param_count);     \
172                 debug_indent_level++;   \
173                 output_indent ();       \
174                 mn = mono_method_full_name (frame->method, FALSE); \
175                 g_print ("(%u) Entering %s (", GetCurrentThreadId(), mn);       \
176                 g_free (mn); \
177                 if (signature->hasthis) { \
178                         if (global_no_pointers) { \
179                                 g_print ("this%s ", frame->obj ? "" : "=null"); \
180                         } else { \
181                                 g_print ("%p ", frame->obj); } \
182                 } \
183                 g_print ("%s)\n", args);        \
184                 g_free (args);  \
185         }       \
186         if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)    \
187                 mono_profiler_method_enter (frame->method);
188
189 #define DEBUG_LEAVE()   \
190         if (tracing) {  \
191                 char *mn, *args;        \
192                 if (signature->ret->type != MONO_TYPE_VOID)     \
193                         args = dump_stack (frame->retval, frame->retval + 1);   \
194                 else    \
195                         args = g_strdup ("");   \
196                 output_indent ();       \
197                 mn = mono_method_full_name (frame->method, FALSE); \
198                 g_print ("(%u) Leaving %s", GetCurrentThreadId(),  mn); \
199                 g_free (mn); \
200                 g_print (" => %s\n", args);     \
201                 g_free (args);  \
202                 debug_indent_level--;   \
203         }       \
204         if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)    \
205                 mono_profiler_method_leave (frame->method);
206
207 #else
208
209 #define DEBUG_ENTER()
210 #define DEBUG_LEAVE()
211
212 #endif
213
214 static void
215 interp_ex_handler (MonoException *ex) {
216         ThreadContext *context = TlsGetValue (thread_context_id);
217         char *stack_trace;
218         stack_trace = dump_frame (context->current_frame);
219         ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
220         g_free (stack_trace);
221         if (context == NULL || context->current_env == NULL) {
222                 char *strace = mono_string_to_utf8 (ex->stack_trace);
223                 fprintf(stderr, "Nothing can catch this exception: ");
224                 fprintf(stderr, "%s", ex->object.vtable->klass->name);
225                 if (ex->message != NULL) {
226                         char *m = mono_string_to_utf8 (ex->message);
227                         fprintf(stderr, ": %s", m);
228                         g_free(m);
229                 }
230                 fprintf(stderr, "\n");
231                 fprintf(stderr, "%s\n", strace);
232                 g_free (strace);
233                 if (ex->inner_ex != NULL) {
234                         ex = (MonoException *)ex->inner_ex;
235                         fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
236                         if (ex->message != NULL) {
237                                 char *m = mono_string_to_utf8 (ex->message);
238                                 fprintf(stderr, ": %s", m);
239                                 g_free(m);
240                         }
241                         strace = mono_string_to_utf8 (ex->stack_trace);
242                         fprintf(stderr, "\n");
243                         fprintf(stderr, "%s\n", strace);
244                         g_free (strace);
245                 }
246                 exit(1);
247         }
248         context->env_frame->ex = ex;
249         context->search_for_handler = 1;
250         longjmp (*context->current_env, 1);
251 }
252
253 static void
254 ves_real_abort (int line, MonoMethod *mh,
255                 const unsigned char *ip, stackval *stack, stackval *sp)
256 {
257         MonoMethodNormal *mm = (MonoMethodNormal *)mh;
258         fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
259         fprintf (stderr, "Line=%d IP=0x%04x, Aborted execution\n", line,
260                  ip-mm->header->code);
261         g_print ("0x%04x %02x\n",
262                  ip-mm->header->code, *ip);
263         if (sp > stack)
264                 printf ("\t[%d] %d 0x%08x %0.5f\n", sp-stack, sp[-1].type, sp[-1].data.i, sp[-1].data.f);
265 }
266 #define ves_abort() do {ves_real_abort(__LINE__, frame->method, ip, frame->stack, sp); THROW_EX (mono_get_exception_execution_engine (NULL), ip);} while (0);
267
268 static gpointer
269 interp_create_remoting_trampoline (MonoMethod *method)
270 {
271         return mono_marshal_get_remoting_invoke (method);
272 }
273
274 static MonoMethod*
275 get_virtual_method (MonoDomain *domain, MonoMethod *m, stackval *objs)
276 {
277         MonoObject *obj;
278         MonoClass *klass;
279         MonoMethod **vtable;
280         gboolean is_proxy = FALSE;
281         MonoMethod *res;
282
283         if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL))
284                         return m;
285
286         obj = objs->data.p;
287         if ((klass = obj->vtable->klass) == mono_defaults.transparent_proxy_class) {
288                 klass = ((MonoTransparentProxy *)obj)->klass;
289                 is_proxy = TRUE;
290         }
291         vtable = (MonoMethod **)klass->vtable;
292
293         if (m->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
294                 res = ((MonoMethod **)obj->vtable->interface_offsets [m->klass->interface_id]) [m->slot];
295         } else {
296                 res = vtable [m->slot];
297         }
298         g_assert (res);
299
300         if (is_proxy)
301                 return mono_marshal_get_remoting_invoke (res);
302         
303         return res;
304 }
305
306 void inline
307 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
308 {
309         if (type->byref) {
310                 switch (type->type) {
311                 case MONO_TYPE_OBJECT:
312                 case MONO_TYPE_CLASS:
313                 case MONO_TYPE_STRING:
314                 case MONO_TYPE_ARRAY:
315                 case MONO_TYPE_SZARRAY:
316                         result->type = VAL_OBJ;
317                         break;
318                 default:
319                         result->type = VAL_MP;
320                         break;
321                 }
322                 result->data.p = *(gpointer*)data;
323                 return;
324         }
325         switch (type->type) {
326         case MONO_TYPE_VOID:
327                 return;
328         case MONO_TYPE_I1:
329                 result->type = VAL_I32;
330                 result->data.i = *(gint8*)data;
331                 return;
332         case MONO_TYPE_U1:
333         case MONO_TYPE_BOOLEAN:
334                 result->type = VAL_I32;
335                 result->data.i = *(guint8*)data;
336                 return;
337         case MONO_TYPE_I2:
338                 result->type = VAL_I32;
339                 result->data.i = *(gint16*)data;
340                 return;
341         case MONO_TYPE_U2:
342         case MONO_TYPE_CHAR:
343                 result->type = VAL_I32;
344                 result->data.i = *(guint16*)data;
345                 return;
346         case MONO_TYPE_I4:
347                 result->type = VAL_I32;
348                 result->data.i = *(gint32*)data;
349                 return;
350         case MONO_TYPE_U:
351         case MONO_TYPE_I:
352                 result->type = VAL_NATI;
353                 result->data.nati = *(mono_i*)data;
354                 return;
355         case MONO_TYPE_PTR:
356                 result->type = VAL_TP;
357                 result->data.p = *(gpointer*)data;
358                 return;
359         case MONO_TYPE_U4:
360                 result->type = VAL_I32;
361                 result->data.i = *(guint32*)data;
362                 return;
363         case MONO_TYPE_R4:
364                 result->type = VAL_DOUBLE;
365                 result->data.f = *(float*)data;
366                 return;
367         case MONO_TYPE_I8:
368         case MONO_TYPE_U8:
369                 result->type = VAL_I64;
370                 result->data.l = *(gint64*)data;
371                 return;
372         case MONO_TYPE_R8:
373                 result->type = VAL_DOUBLE;
374                 result->data.f = *(double*)data;
375                 return;
376         case MONO_TYPE_STRING:
377         case MONO_TYPE_SZARRAY:
378         case MONO_TYPE_CLASS:
379         case MONO_TYPE_OBJECT:
380         case MONO_TYPE_ARRAY:
381                 result->type = VAL_OBJ;
382                 result->data.p = *(gpointer*)data;
383                 return;
384         case MONO_TYPE_VALUETYPE:
385                 if (type->data.klass->enumtype) {
386                         stackval_from_data (type->data.klass->enum_basetype, result, data, pinvoke);
387                         return;
388                 } else {
389                         int size;
390                         result->type = VAL_VALUET;
391                         
392                         if (pinvoke)
393                                 size = mono_class_native_size (type->data.klass, NULL);
394                         else
395                                 size = mono_class_value_size (type->data.klass, NULL);
396                         memcpy (result->data.vt, data, size);
397                 }
398                 return;
399         default:
400                 g_warning ("got type 0x%02x", type->type);
401                 g_assert_not_reached ();
402         }
403 }
404
405 void inline
406 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
407 {
408         if (type->byref) {
409                 gpointer *p = (gpointer*)data;
410                 *p = val->data.p;
411                 return;
412         }
413         //printf ("TODAT0 %p\n", data);
414         switch (type->type) {
415         case MONO_TYPE_I1:
416         case MONO_TYPE_U1: {
417                 guint8 *p = (guint8*)data;
418                 *p = val->data.i;
419                 return;
420         }
421         case MONO_TYPE_BOOLEAN: {
422                 guint8 *p = (guint8*)data;
423                 *p = (val->data.i != 0);
424                 return;
425         }
426         case MONO_TYPE_I2:
427         case MONO_TYPE_U2:
428         case MONO_TYPE_CHAR: {
429                 guint16 *p = (guint16*)data;
430                 *p = val->data.i;
431                 return;
432         }
433         case MONO_TYPE_I: {
434                 mono_i *p = (mono_i*)data;
435                 /* In theory the value used by stloc should match the local var type
436                    but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
437                    a native int - both by csc and mcs). Not sure what to do about sign extension
438                    as it is outside the spec... doing the obvious */
439                 *p = val->type == VAL_I32 ? val->data.i : val->data.nati;
440                 return;
441         }
442         case MONO_TYPE_U: {
443                 mono_u *p = (mono_u*)data;
444                 /* see above. */
445                 *p = val->type == VAL_I32 ? (guint32)val->data.i : val->data.nati;
446                 return;
447         }
448         case MONO_TYPE_I4:
449         case MONO_TYPE_U4: {
450                 gint32 *p = (gint32*)data;
451                 *p = val->data.i;
452                 return;
453         }
454         case MONO_TYPE_I8:
455         case MONO_TYPE_U8: {
456                 gint64 *p = (gint64*)data;
457                 *p = val->data.l;
458                 return;
459         }
460         case MONO_TYPE_R4: {
461                 float *p = (float*)data;
462                 *p = val->data.f;
463                 return;
464         }
465         case MONO_TYPE_R8: {
466                 double *p = (double*)data;
467                 *p = val->data.f;
468                 return;
469         }
470         case MONO_TYPE_STRING:
471         case MONO_TYPE_SZARRAY:
472         case MONO_TYPE_CLASS:
473         case MONO_TYPE_OBJECT:
474         case MONO_TYPE_ARRAY:
475         case MONO_TYPE_PTR: {
476                 gpointer *p = (gpointer*)data;
477                 *p = val->data.p;
478                 return;
479         }
480         case MONO_TYPE_VALUETYPE:
481                 if (type->data.klass->enumtype) {
482                         stackval_to_data (type->data.klass->enum_basetype, val, data, pinvoke);
483                         return;
484                 } else {
485                         int size;
486
487                         if (pinvoke)
488                                 size = mono_class_native_size (type->data.klass, NULL);
489                         else
490                                 size = mono_class_value_size (type->data.klass, NULL);
491
492                         memcpy (data, val->data.vt, size);
493                 }
494                 return;
495         default:
496                 g_warning ("got type %x", type->type);
497                 g_assert_not_reached ();
498         }
499 }
500
501 #define FILL_IN_TRACE(exception, frame) \
502         do { \
503                 char *stack_trace;      \
504                 stack_trace = dump_frame (frame);       \
505                 (exception)->stack_trace = mono_string_new (mono_domain_get(), stack_trace);    \
506                 g_free (stack_trace);   \
507         } while (0)
508
509 #define THROW_EX(exception,ex_ip)       \
510         do {\
511                 frame->ip = (ex_ip);            \
512                 frame->ex = (MonoException*)(exception);        \
513                 FILL_IN_TRACE(frame->ex, frame); \
514                 goto handle_exception;  \
515         } while (0)
516
517 static MonoObject*
518 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
519 {
520         guint32 *lengths;
521         guint32 *lower_bounds;
522         int i;
523
524         lengths = alloca (sizeof (guint32) * klass->rank * 2);
525         for (i = 0; i < sig->param_count; ++i) {
526                 lengths [i] = values->data.i;
527                 values ++;
528         }
529         if (klass->rank == sig->param_count) {
530                 /* Only lengths provided. */
531                 lower_bounds = NULL;
532         } else {
533                 /* lower bounds are first. */
534                 lower_bounds = lengths;
535                 lengths += klass->rank;
536         }
537         return (MonoObject*)mono_array_new_full (domain, klass, lengths, lower_bounds);
538 }
539
540 static void 
541 ves_array_set (MonoInvocation *frame)
542 {
543         stackval *sp = frame->stack_args;
544         MonoObject *o;
545         MonoArray *ao;
546         MonoClass *ac;
547         gint32 i, t, pos, esize;
548         gpointer ea;
549         MonoType *mt;
550
551         o = frame->obj;
552         ao = (MonoArray *)o;
553         ac = o->vtable->klass;
554
555         g_assert (ac->rank >= 1);
556
557         pos = sp [0].data.i;
558         if (ao->bounds != NULL) {
559                 pos -= ao->bounds [0].lower_bound;
560                 for (i = 1; i < ac->rank; i++) {
561                         if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >= 
562                             ao->bounds [i].length) {
563                                 frame->ex = mono_get_exception_index_out_of_range ();
564                                 FILL_IN_TRACE(frame->ex, frame);
565                                 return;
566                         }
567                         pos = pos*ao->bounds [i].length + sp [i].data.i - 
568                                 ao->bounds [i].lower_bound;
569                 }
570         } else if (pos >= ao->max_length) {
571                 frame->ex = mono_get_exception_index_out_of_range ();
572                 FILL_IN_TRACE(frame->ex, frame);
573                 return;
574         }
575
576         esize = mono_array_element_size (ac);
577         ea = mono_array_addr_with_size (ao, esize, pos);
578
579         mt = frame->method->signature->params [ac->rank];
580         stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
581 }
582
583 static void 
584 ves_array_get (MonoInvocation *frame)
585 {
586         stackval *sp = frame->stack_args;
587         MonoObject *o;
588         MonoArray *ao;
589         MonoClass *ac;
590         gint32 i, t, pos, esize;
591         gpointer ea;
592         MonoType *mt;
593
594         o = frame->obj;
595         ao = (MonoArray *)o;
596         ac = o->vtable->klass;
597
598         g_assert (ac->rank >= 1);
599
600         pos = sp [0].data.i;
601         if (ao->bounds != NULL) {
602                 pos -= ao->bounds [0].lower_bound;
603                 for (i = 1; i < ac->rank; i++) {
604                         if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >= 
605                             ao->bounds [i].length) {
606                                 frame->ex = mono_get_exception_index_out_of_range ();
607                                 FILL_IN_TRACE(frame->ex, frame);
608                                 return;
609                         }
610
611                         pos = pos*ao->bounds [i].length + sp [i].data.i - 
612                                 ao->bounds [i].lower_bound;
613                 }
614         } else if (pos >= ao->max_length) {
615                 frame->ex = mono_get_exception_index_out_of_range ();
616                 FILL_IN_TRACE(frame->ex, frame);
617                 return;
618         }
619
620         esize = mono_array_element_size (ac);
621         ea = mono_array_addr_with_size (ao, esize, pos);
622
623         mt = frame->method->signature->ret;
624         stackval_from_data (mt, frame->retval, ea, FALSE);
625 }
626
627 static void
628 ves_array_element_address (MonoInvocation *frame)
629 {
630         stackval *sp = frame->stack_args;
631         MonoObject *o;
632         MonoArray *ao;
633         MonoClass *ac;
634         gint32 i, t, pos, esize;
635         gpointer ea;
636
637         o = frame->obj;
638         ao = (MonoArray *)o;
639         ac = o->vtable->klass;
640
641         g_assert (ac->rank >= 1);
642
643         pos = sp [0].data.i;
644         if (ao->bounds != NULL) {
645                 pos -= ao->bounds [0].lower_bound;
646                 for (i = 1; i < ac->rank; i++) {
647                         if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >= 
648                             ao->bounds [i].length) {
649                                 frame->ex = mono_get_exception_index_out_of_range ();
650                                 FILL_IN_TRACE(frame->ex, frame);
651                                 return;
652                         }
653                         pos = pos*ao->bounds [i].length + sp [i].data.i - 
654                                 ao->bounds [i].lower_bound;
655                 }
656         } else if (pos >= ao->max_length) {
657                 frame->ex = mono_get_exception_index_out_of_range ();
658                 FILL_IN_TRACE(frame->ex, frame);
659                 return;
660         }
661
662         esize = mono_array_element_size (ac);
663         ea = mono_array_addr_with_size (ao, esize, pos);
664
665         frame->retval->type = VAL_MP;
666         frame->retval->data.p = ea;
667 }
668
669 static void
670 interp_walk_stack (MonoStackWalk func, gpointer user_data)
671 {
672         ThreadContext *context = TlsGetValue (thread_context_id);
673         MonoInvocation *frame = context->current_frame;
674         int il_offset;
675         MonoMethodHeader *hd;
676
677         while (frame) {
678                 gboolean managed = FALSE;
679                 if (!frame->method || (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || 
680                                 (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
681                         il_offset = -1;
682                 else {
683                         hd = ((MonoMethodNormal*)frame->method)->header;
684                         il_offset = frame->ip - hd->code;
685                         if (!frame->method->wrapper_type)
686                                 managed = TRUE;
687                 }
688                 if (func (frame->method, -1, il_offset, managed, user_data))
689                         return;
690                 frame = frame->parent;
691         }
692 }
693
694 static void 
695 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFunc addr, gboolean string_ctor, ThreadContext *context)
696 {
697         jmp_buf env;
698         MonoPIFunc func;
699         MonoInvocation *old_frame = context->current_frame;
700         MonoInvocation *old_env_frame = context->env_frame;
701         jmp_buf *old_env = context->current_env;
702
703         if (setjmp (env)) {
704                 context->current_frame = old_frame;
705                 context->env_frame = old_env_frame;
706                 context->current_env = old_env;
707                 context->search_for_handler = 0;
708                 return;
709         }
710
711         context->env_frame = frame;
712         context->current_env = &env;
713
714         if (frame->method) {
715                 if (!frame->method->info) {
716                         func = frame->method->info = mono_arch_create_trampoline (sig, string_ctor);
717                 } else { 
718                         func = (MonoPIFunc)frame->method->info;
719                 }
720         } else {
721                 func = mono_arch_create_trampoline (sig, string_ctor);
722         }
723
724         context->current_frame = frame;
725
726         func (addr, &frame->retval->data.p, frame->obj, frame->stack_args);
727
728         if (string_ctor) {
729                 stackval_from_data (&mono_defaults.string_class->byval_arg, 
730                                     frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
731         } else if (!MONO_TYPE_ISSTRUCT (sig->ret))
732                 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
733
734         context->current_frame = old_frame;
735         context->env_frame = old_env_frame;
736         context->current_env = old_env;
737 }
738
739 /*
740  * From the spec:
741  * runtime specifies that the implementation of the method is automatically
742  * provided by the runtime and is primarily used for the methods of delegates.
743  */
744 static void
745 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
746 {
747         const char *name = frame->method->name;
748         MonoObject *obj = (MonoObject*)frame->obj;
749         MonoInvocation call;
750         MonoMethod *nm;
751
752
753         mono_class_init (frame->method->klass);
754         
755         if (obj && mono_object_isinst (obj, mono_defaults.multicastdelegate_class)) {
756                 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
757                         mono_delegate_ctor (obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
758                         return;
759                 }
760                 if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
761                         nm = mono_marshal_get_delegate_invoke (frame->method);
762                         INIT_FRAME(&call,frame,obj,frame->stack_args,frame->retval,nm);
763                         ves_exec_method_with_context (&call, context);
764                         frame->ex = call.ex;
765                         return;
766                 }
767                 if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
768                         nm = mono_marshal_get_delegate_begin_invoke (frame->method);
769                         INIT_FRAME(&call,frame,obj,frame->stack_args,frame->retval,nm);
770                         ves_exec_method_with_context (&call, context);
771                         frame->ex = call.ex;
772                         return;
773                 }
774                 if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
775                         nm = mono_marshal_get_delegate_end_invoke (frame->method);
776                         INIT_FRAME(&call,frame,obj,frame->stack_args,frame->retval,nm);
777                         ves_exec_method_with_context (&call, context);
778                         frame->ex = call.ex;
779                         return;
780                 }
781         }
782
783         if (obj && mono_object_isinst (obj, mono_defaults.array_class)) {
784                 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
785                         ves_array_set (frame);
786                         return;
787                 }
788                 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
789                         ves_array_get (frame);
790                         return;
791                 }
792                 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
793                         ves_array_element_address (frame);
794                         return;
795                 }
796         }
797         
798         g_error ("Don't know how to exec runtime method %s.%s::%s", 
799                         frame->method->klass->name_space, frame->method->klass->name,
800                         frame->method->name);
801 }
802
803 static char*
804 dump_stack (stackval *stack, stackval *sp)
805 {
806         stackval *s = stack;
807         GString *str = g_string_new ("");
808         
809         if (sp == stack)
810                 return g_string_free (str, FALSE);
811         
812         while (s < sp) {
813                 switch (s->type) {
814 #if SIZEOF_VOID_P == 4
815                 case VAL_NATI: g_string_sprintfa (str, "[%d/0x%0x] ", s->data.nati, s->data.nati); break;
816 #else
817                 case VAL_NATI: g_string_sprintfa (str, "[%lld/0x%0llx] ", s->data.nati, s->data.nati); break;
818 #endif
819                 case VAL_I32: g_string_sprintfa (str, "[%d] ", s->data.i); break;
820                 case VAL_I64: g_string_sprintfa (str, "[%lldL] ", s->data.l); break;
821                 case VAL_DOUBLE: g_string_sprintfa (str, "[%0.5f] ", s->data.f); break;
822                 case VAL_VALUET:
823                         if (!global_no_pointers)
824                                 g_string_sprintfa (str, "[vt: %p] ", s->data.vt);
825                         else
826                                 g_string_sprintfa (str, "[vt%s] ", s->data.vt ? "" : "=null");
827                         break;
828                 case VAL_OBJ: {
829                         MonoObject *obj =  s->data.p;
830                         if (global_no_pointers && obj && obj->vtable) {
831                                 MonoClass *klass = mono_object_class (obj);
832                                 if (klass == mono_defaults.string_class) {
833                                         char *utf8 = mono_string_to_utf8 ((MonoString*) obj);
834                                         g_string_sprintfa (str, "[str:%s] ", utf8);
835                                         g_free (utf8);
836                                         break;
837                                 } else if (klass == mono_defaults.sbyte_class) {
838                                         g_string_sprintfa (str, "[b:%d] ",
839                                                            *(gint8 *)((guint8 *) obj + sizeof (MonoObject)));
840                                         break;
841                                 } else if (klass == mono_defaults.int16_class) {
842                                         g_string_sprintfa (str, "[b:%d] ",
843                                                            *(gint16 *)((guint8 *) obj + sizeof (MonoObject)));
844                                         break;
845                                 } else if (klass == mono_defaults.int32_class) {
846                                         g_string_sprintfa (str, "[b:%d] ",
847                                                            *(gint32 *)((guint8 *) obj + sizeof (MonoObject)));
848                                         break;
849                                 } else if (klass == mono_defaults.byte_class) {
850                                         g_string_sprintfa (str, "[b:%u] ",
851                                                            *(guint8 *)((guint8 *) obj + sizeof (MonoObject)));
852                                         break;
853                                 } else if (klass == mono_defaults.char_class
854                                            || klass == mono_defaults.uint16_class) {
855                                         g_string_sprintfa (str, "[b:%u] ",
856                                                            *(guint16 *)((guint8 *) obj + sizeof (MonoObject)));
857                                         break;
858                                 } else if (klass == mono_defaults.uint32_class) {
859                                         g_string_sprintfa (str, "[b:%u] ",
860                                                            *(guint32 *)((guint8 *) obj + sizeof (MonoObject)));
861                                         break;
862                                 } else if (klass == mono_defaults.int64_class) {
863                                         g_string_sprintfa (str, "[b:%lld] ",
864                                                            *(gint64 *)((guint8 *) obj + sizeof (MonoObject)));
865                                         break;
866                                 } else if (klass == mono_defaults.uint64_class) {
867                                         g_string_sprintfa (str, "[b:%llu] ",
868                                                            *(guint64 *)((guint8 *) obj + sizeof (MonoObject)));
869                                         break;
870                                 } else if (klass == mono_defaults.double_class) {
871                                         g_string_sprintfa (str, "[b:%0.5f] ",
872                                                            *(gdouble *)((guint8 *) obj + sizeof (MonoObject)));
873                                         break;
874                                 } else if (klass == mono_defaults.single_class) {
875                                         g_string_sprintfa (str, "[b:%0.5f] ",
876                                                            *(gfloat *)((guint8 *) obj + sizeof (MonoObject)));
877                                         break;
878                                 } else if (klass == mono_defaults.boolean_class) {
879                                         g_string_sprintfa (str, "[b:%s] ",
880                                                            *(gboolean *)((guint8 *) obj + sizeof (MonoObject))
881                                                            ? "true" : "false");
882                                         break;
883                                 }
884                         }
885                         /* fall thru */
886                 }
887                 default:
888                         if (!global_no_pointers)
889                                 g_string_sprintfa (str, "[%c:%p] ", s->type == VAL_OBJ ? 'O' : s->type == VAL_MP ? 'M' : '?', s->data.p);
890                         else
891                                 g_string_sprintfa (str, s->data.p ? "[ptr] " : "[null] ");
892                         break;
893                 }
894                 ++s;
895         }
896         return g_string_free (str, FALSE);
897 }
898
899 static char*
900 dump_frame (MonoInvocation *inv)
901 {
902         GString *str = g_string_new ("");
903         int i;
904         char *args;
905         for (i = 0; inv; inv = inv->parent, ++i) {
906                 if (inv->method != NULL) {
907                         MonoClass *k;
908
909                         int codep = 0;
910                         const char * opname = "";
911                         gchar *source = NULL;
912
913                         if (!inv->method) {
914                                 --i;
915                                 continue;
916                         }
917
918                         k = inv->method->klass;
919
920                         if ((inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
921                                 (inv->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
922                                 MonoMethodHeader *hd = ((MonoMethodNormal *)inv->method)->header;
923
924                                 if (hd != NULL) {
925                                         if (inv->ip)
926                                                 codep = *(inv->ip) == 0xfe? inv->ip [1] + 256: *(inv->ip);
927                                         else
928                                                 codep = 0;
929                                         opname = mono_opcode_names [codep];
930                                         codep = inv->ip - hd->code;
931         
932                                         source = mono_debug_source_location_from_il_offset (inv->method, codep, NULL);
933                                 }
934                         }
935                         args = dump_stack (inv->stack_args, inv->stack_args + inv->method->signature->param_count);
936                         if (source)
937                                 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s.%s::%s (%s) at %s\n", i, codep, opname,
938                                                    k->name_space, k->name, inv->method->name, args, source);
939                         else
940                                 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s.%s::%s (%s)\n", i, codep, opname,
941                                                    k->name_space, k->name, inv->method->name, args);
942                         g_free (args);
943                         g_free (source);
944                 }
945         }
946         return g_string_free (str, FALSE);
947 }
948
949 typedef enum {
950         INLINE_STRING_LENGTH = 1,
951         INLINE_STRING_GET_CHARS,
952         INLINE_ARRAY_LENGTH,
953         INLINE_ARRAY_RANK,
954         INLINE_TYPE_ELEMENT_TYPE
955 } InlineMethod;
956
957 typedef struct
958 {
959         MonoClassField *field;
960 } MonoRuntimeFieldInfo;
961
962 typedef struct
963 {
964         guint32 locals_size;
965         guint32 args_size;
966         MonoRuntimeFieldInfo *field_info;
967         guint32 offsets[1];
968 } MethodRuntimeData;
969
970 static void
971 write32(unsigned char *p, guint32 v)
972 {
973         p[0] = v & 0xff;
974         p[1] = (v >> 8) & 0xff;
975         p[2] = (v >> 16) & 0xff;
976         p[3] = (v >> 24) & 0xff;
977 }
978
979 static CRITICAL_SECTION calc_section;
980
981 static void
982 calc_offsets (MonoImage *image, MonoMethod *method)
983 {
984         int i, align, size, offset = 0;
985         MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
986         MonoMethodSignature *signature = method->signature;
987         register const unsigned char *ip, *end;
988         const MonoOpcode *opcode;
989         guint32 token;
990         MonoMethod *m;
991         MonoClass *class;
992         MonoDomain *domain = mono_domain_get ();
993         MethodRuntimeData *rtd;
994         int n_fields = 0;
995
996         mono_profiler_method_jit (method); /* sort of... */
997         /* intern the strings in the method. */
998         ip = header->code;
999         end = ip + header->code_size;
1000         while (ip < end) {
1001                 i = *ip;
1002                 if (*ip == 0xfe) {
1003                         ip++;
1004                         i = *ip + 256;
1005                 }
1006                 opcode = &mono_opcodes [i];
1007                 switch (opcode->argument) {
1008                 case MonoInlineNone:
1009                         ++ip;
1010                         break;
1011                 case MonoInlineString:
1012                         mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
1013                         ip += 5;
1014                         break;
1015                 case MonoInlineType:
1016                         if (method->wrapper_type == MONO_WRAPPER_NONE) {
1017                                 class = mono_class_get (image, read32 (ip + 1));
1018                                 mono_class_init (class);
1019                                 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE))
1020                                         mono_class_vtable (domain, class);
1021                         }
1022                         ip += 5;
1023                         break;
1024                 case MonoInlineField:
1025                         n_fields++;
1026                         ip += 5;
1027                         break;
1028                 case MonoInlineMethod:
1029                         if (method->wrapper_type == MONO_WRAPPER_NONE) {
1030                                 m = mono_get_method (image, read32 (ip + 1), NULL);
1031                                 mono_class_init (m->klass);
1032                                 if (!(m->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
1033                                         mono_class_vtable (domain, m->klass);
1034                         }
1035                         ip += 5;
1036                         break;
1037                 case MonoInlineTok:
1038                 case MonoInlineSig:
1039                 case MonoShortInlineR:
1040                 case MonoInlineI:
1041                 case MonoInlineBrTarget:
1042                         ip += 5;
1043                         break;
1044                 case MonoInlineVar:
1045                         ip += 3;
1046                         break;
1047                 case MonoShortInlineVar:
1048                 case MonoShortInlineI:
1049                 case MonoShortInlineBrTarget:
1050                         ip += 2;
1051                         break;
1052                 case MonoInlineSwitch: {
1053                         guint32 n;
1054                         ++ip;
1055                         n = read32 (ip);
1056                         ip += 4;
1057                         ip += 4 * n;
1058                         break;
1059                 }
1060                 case MonoInlineR:
1061                 case MonoInlineI8:
1062                         ip += 9;
1063                         break;
1064                 default:
1065                         g_assert_not_reached ();
1066                 }
1067
1068         }
1069
1070         /* the rest needs to be locked so it is only done once */
1071         EnterCriticalSection(&calc_section);
1072         if (method->info != NULL) {
1073                 LeaveCriticalSection(&calc_section);
1074                 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
1075                 return;
1076         }
1077         rtd = (MethodRuntimeData *)g_malloc0 (sizeof(MethodRuntimeData) + (header->num_locals - 1 + signature->hasthis + signature->param_count) * sizeof(guint32));
1078         for (i = 0; i < header->num_locals; ++i) {
1079                 size = mono_type_size (header->locals [i], &align);
1080                 offset += align - 1;
1081                 offset &= ~(align - 1);
1082                 rtd->offsets [i] = offset;
1083                 offset += size;
1084         }
1085         rtd->locals_size = offset;
1086         offset = 0;
1087         if (signature->hasthis) {
1088                 offset += sizeof (gpointer) - 1;
1089                 offset &= ~(sizeof (gpointer) - 1);
1090                 rtd->offsets [header->num_locals] = offset;
1091                 offset += sizeof (gpointer);
1092         }
1093         for (i = 0; i < signature->param_count; ++i) {
1094                 if (signature->pinvoke) {
1095                         size = mono_type_native_stack_size (signature->params [i], &align);
1096                         align = 8;
1097                 }
1098                 else
1099                         size = mono_type_stack_size (signature->params [i], &align);
1100                 offset += align - 1;
1101                 offset &= ~(align - 1);
1102                 rtd->offsets [signature->hasthis + header->num_locals + i] = offset;
1103                 offset += size;
1104         }
1105         rtd->args_size = offset;
1106         rtd->field_info = g_malloc(n_fields * sizeof(MonoRuntimeFieldInfo));
1107
1108         header->code = g_memdup(header->code, header->code_size);
1109         n_fields = 0;
1110         ip = header->code;
1111         end = ip + header->code_size;
1112         while (ip < end) {
1113                 i = *ip;
1114                 if (*ip == 0xfe) {
1115                         ip++;
1116                         i = *ip + 256;
1117                 }
1118                 opcode = &mono_opcodes [i];
1119                 switch (opcode->argument) {
1120                 case MonoInlineNone:
1121                         ++ip;
1122                         break;
1123                 case MonoInlineString:
1124                         ip += 5;
1125                         break;
1126                 case MonoInlineType:
1127                         ip += 5;
1128                         break;
1129                 case MonoInlineField:
1130                         token = read32 (ip + 1);
1131                         rtd->field_info[n_fields].field = mono_field_from_token (image, token, &class);
1132                         mono_class_vtable (domain, class);
1133                         g_assert(rtd->field_info[n_fields].field->parent == class);
1134                         write32 ((unsigned char *)ip + 1, n_fields);
1135                         n_fields++;
1136                         ip += 5;
1137                         break;
1138                 case MonoInlineMethod:
1139                         ip += 5;
1140                         break;
1141                 case MonoInlineTok:
1142                 case MonoInlineSig:
1143                 case MonoShortInlineR:
1144                 case MonoInlineI:
1145                 case MonoInlineBrTarget:
1146                         ip += 5;
1147                         break;
1148                 case MonoInlineVar:
1149                         ip += 3;
1150                         break;
1151                 case MonoShortInlineVar:
1152                 case MonoShortInlineI:
1153                 case MonoShortInlineBrTarget:
1154                         ip += 2;
1155                         break;
1156                 case MonoInlineSwitch: {
1157                         guint32 n;
1158                         ++ip;
1159                         n = read32 (ip);
1160                         ip += 4;
1161                         ip += 4 * n;
1162                         break;
1163                 }
1164                 case MonoInlineR:
1165                 case MonoInlineI8:
1166                         ip += 9;
1167                         break;
1168                 default:
1169                         g_assert_not_reached ();
1170                 }
1171
1172         }
1173
1174         /*
1175          * We store the inline info in addr, since it's unused for IL methods.
1176          */
1177         if (method->klass == mono_defaults.string_class) {
1178                 if (strcmp (method->name, "get_Length") == 0)
1179                         method->addr = GUINT_TO_POINTER (INLINE_STRING_LENGTH);
1180                 else if (strcmp (method->name, "get_Chars") == 0)
1181                         method->addr = GUINT_TO_POINTER (INLINE_STRING_GET_CHARS);
1182         } else if (method->klass == mono_defaults.array_class) {
1183                 if (strcmp (method->name, "get_Length") == 0)
1184                         method->addr = GUINT_TO_POINTER (INLINE_ARRAY_LENGTH);
1185                 else if (strcmp (method->name, "get_Rank") == 0 || strcmp (method->name, "GetRank") == 0)
1186                         method->addr = GUINT_TO_POINTER (INLINE_ARRAY_RANK);
1187         } else if (method->klass == mono_defaults.monotype_class) {
1188                 if (strcmp (method->name, "GetElementType") == 0)
1189                         method->addr = GUINT_TO_POINTER (INLINE_TYPE_ELEMENT_TYPE);
1190         }
1191         mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
1192         method->info = rtd;
1193         LeaveCriticalSection(&calc_section);
1194 }
1195
1196 #define LOCAL_POS(n)            (frame->locals + rtd->offsets [n])
1197 #define LOCAL_TYPE(header, n)   ((header)->locals [(n)])
1198
1199 #define ARG_POS(n)              (args_pointers [(n)])
1200 #define ARG_TYPE(sig, n)        ((n) ? (sig)->params [(n) - (sig)->hasthis] : \
1201                                 (sig)->hasthis ? &frame->method->klass->this_arg: (sig)->params [(0)])
1202
1203 typedef struct _vtallocation vtallocation;
1204
1205 struct _vtallocation {
1206         vtallocation *next;
1207         guint32 size;
1208         guint32 max_size;
1209         union {
1210                 char data [MONO_ZERO_LEN_ARRAY];
1211                 double force_alignment;
1212         } u;
1213 };
1214
1215 #define vt_allocmem(sz, var) \
1216         do { \
1217                 vtallocation *tmp, *prev; \
1218                 prev = NULL; \
1219                 tmp = vtalloc; \
1220                 while (tmp && (tmp->max_size < (sz))) { \
1221                         prev = tmp; \
1222                         tmp = tmp->next; \
1223                 } \
1224                 if (!tmp) { \
1225                         tmp = alloca (sizeof (vtallocation) + (sz));    \
1226                         tmp->max_size = (sz);   \
1227                         g_assert ((sz) < 10000);        \
1228                 } \
1229                 else \
1230                         if (prev) \
1231                                 prev->next = tmp->next; \
1232                         else \
1233                                 vtalloc = tmp->next; \
1234                 tmp->size = (sz);       \
1235                 var = tmp->u.data;      \
1236         } while(0)
1237
1238 #define vt_alloc(vtype,sp,native)       \
1239         if ((vtype)->type == MONO_TYPE_VALUETYPE && !(vtype)->data.klass->enumtype) {   \
1240                 if (!(vtype)->byref) {  \
1241                         guint32 vtsize; \
1242                         if (native) vtsize = mono_class_native_size ((vtype)->data.klass, NULL);        \
1243                         else vtsize = mono_class_value_size ((vtype)->data.klass, NULL);        \
1244                         vt_allocmem(vtsize, (sp)->data.vt);     \
1245                 }       \
1246         }
1247
1248 #define vt_free(sp)     \
1249         do {    \
1250                 if ((sp)->type == VAL_VALUET) { \
1251                         vtallocation *tmp = (vtallocation*)(((char*)(sp)->data.vt) - G_STRUCT_OFFSET (vtallocation, u));        \
1252                         tmp->next = vtalloc; \
1253                         vtalloc = tmp; \
1254                 }       \
1255         } while (0)
1256
1257 #define stackvalpush(val, sp) \
1258         do {    \
1259                 (sp)->type = (val)->type; \
1260                 (sp)->data = (val)->data; \
1261                 if ((val)->type == VAL_VALUET) {        \
1262                         vtallocation *vala = (vtallocation*)(((char*)(val)->data.vt) - G_STRUCT_OFFSET (vtallocation, u));      \
1263                         vt_allocmem(vala->size, (sp)->data.vt); \
1264                         memcpy((sp)->data.vt, (val)->data.vt, vala->size);      \
1265                 }       \
1266                 (sp)++; \
1267         } while (0)
1268
1269 #define stackvalcpy(src, dest) \
1270         do {    \
1271                 (dest)->type = (src)->type; \
1272                 (dest)->data = (src)->data; \
1273                 if ((dest)->type == VAL_VALUET) {       \
1274                         vtallocation *tmp = (vtallocation*)(((char*)(src)->data.vt) - G_STRUCT_OFFSET (vtallocation, u));       \
1275                         memcpy((dest)->data.vt, (src)->data.vt, tmp->size);     \
1276                 }       \
1277         } while (0)
1278
1279 /*
1280 static void
1281 verify_method (MonoMethod *m)
1282 {
1283         GSList *errors, *tmp;
1284         MonoVerifyInfo *info;
1285
1286         errors = mono_method_verify (m, MONO_VERIFY_ALL);
1287         if (errors)
1288                 g_print ("Method %s.%s::%s has invalid IL.\n", m->klass->name_space, m->klass->name, m->name);
1289         for (tmp = errors; tmp; tmp = tmp->next) {
1290                 info = tmp->data;
1291                 g_print ("%s\n", info->message);
1292         }
1293         if (errors)
1294                 G_BREAKPOINT ();
1295         mono_free_verify_list (errors);
1296 }
1297 */
1298
1299 #define MYGUINT64_MAX 18446744073709551615ULL
1300 #define MYGINT64_MAX 9223372036854775807LL
1301 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1302
1303 #define MYGUINT32_MAX 4294967295U
1304 #define MYGINT32_MAX 2147483647
1305 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1306         
1307 #define CHECK_ADD_OVERFLOW(a,b) \
1308         (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0  \
1309         : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1310
1311 #define CHECK_SUB_OVERFLOW(a,b) \
1312         (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0   \
1313         : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1314
1315 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1316         (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1317
1318 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1319         (guint32)(a) < (guint32)(b) ? -1 : 0
1320
1321 #define CHECK_ADD_OVERFLOW64(a,b) \
1322         (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0  \
1323         : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1324
1325 #define CHECK_SUB_OVERFLOW64(a,b) \
1326         (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0   \
1327         : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1328
1329 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1330         (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1331
1332 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1333         (guint64)(a) < (guint64)(b) ? -1 : 0
1334
1335 #if SIZEOF_VOID_P == 4
1336 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1337 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1338 #else
1339 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1340 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1341 #endif
1342
1343 /* Resolves to TRUE if the operands would overflow */
1344 #define CHECK_MUL_OVERFLOW(a,b) \
1345         ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1346         (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1347         (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1348         (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1349         (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1350         (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1351         (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1352
1353 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1354         ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1355         (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1356
1357 #define CHECK_MUL_OVERFLOW64(a,b) \
1358         ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1359         (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1360         (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1361         (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1362         (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1363         (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1364         (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1365
1366 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1367         ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1368         (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1369
1370 #if SIZEOF_VOID_P == 4
1371 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1372 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1373 #else
1374 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1375 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1376 #endif
1377
1378 static MonoObject*
1379 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1380 {
1381         MonoInvocation frame;
1382         ThreadContext *context = TlsGetValue (thread_context_id);
1383         MonoObject *retval = NULL;
1384         MonoMethodSignature *sig = method->signature;
1385         MonoClass *klass = mono_class_from_mono_type (sig->ret);
1386         int i, type, isobject = 0;
1387         void *ret;
1388         stackval result;
1389         stackval *args = alloca (sizeof (stackval) * sig->param_count);
1390         ThreadContext context_struct;
1391         MonoInvocation *old_frame;
1392
1393         if (context == NULL) {
1394                 context = &context_struct;
1395                 context_struct.base_frame = &frame;
1396                 context_struct.current_frame = NULL;
1397                 context_struct.env_frame = NULL;
1398                 context_struct.current_env = NULL;
1399                 context_struct.search_for_handler = 0;
1400                 TlsSetValue (thread_context_id, context);
1401         }
1402         else
1403                 old_frame = context->current_frame;
1404
1405
1406         /* FIXME: Set frame for execption handling.  */
1407
1408         switch (sig->ret->type) {
1409         case MONO_TYPE_VOID:
1410                 break;
1411         case MONO_TYPE_STRING:
1412         case MONO_TYPE_OBJECT:
1413         case MONO_TYPE_CLASS:
1414         case MONO_TYPE_ARRAY:
1415         case MONO_TYPE_SZARRAY:
1416                 isobject = 1;
1417                 break;
1418         case MONO_TYPE_VALUETYPE:
1419                 retval = mono_object_new (mono_domain_get (), klass);
1420                 ret = ((char*)retval) + sizeof (MonoObject);
1421                 if (!sig->ret->data.klass->enumtype)
1422                         result.data.vt = ret;
1423                 break;
1424         default:
1425                 retval = mono_object_new (mono_domain_get (), klass);
1426                 ret = ((char*)retval) + sizeof (MonoObject);
1427                 break;
1428         }
1429
1430         for (i = 0; i < sig->param_count; ++i) {
1431                 if (sig->params [i]->byref) {
1432                         args [i].type = VAL_POINTER;
1433                         args [i].data.p = params [i];
1434                         continue;
1435                 }
1436                 type = sig->params [i]->type;
1437 handle_enum:
1438                 switch (type) {
1439                 case MONO_TYPE_U1:
1440                 case MONO_TYPE_I1:
1441                 case MONO_TYPE_BOOLEAN:
1442                         args [i].type = VAL_I32;
1443                         args [i].data.i = *(MonoBoolean*)params [i];
1444                         break;
1445                 case MONO_TYPE_U2:
1446                 case MONO_TYPE_I2:
1447                 case MONO_TYPE_CHAR:
1448                         args [i].type = VAL_I32;
1449                         args [i].data.i = *(gint16*)params [i];
1450                         break;
1451 #if SIZEOF_VOID_P == 4
1452                 case MONO_TYPE_U: /* use VAL_POINTER? */
1453                 case MONO_TYPE_I:
1454 #endif
1455                 case MONO_TYPE_U4:
1456                 case MONO_TYPE_I4:
1457                         args [i].type = VAL_I32;
1458                         args [i].data.i = *(gint32*)params [i];
1459                         break;
1460 #if SIZEOF_VOID_P == 8
1461                 case MONO_TYPE_U:
1462                 case MONO_TYPE_I:
1463 #endif
1464                 case MONO_TYPE_U8:
1465                 case MONO_TYPE_I8:
1466                         args [i].type = VAL_I64;
1467                         args [i].data.l = *(gint64*)params [i];
1468                         break;
1469                 case MONO_TYPE_VALUETYPE:
1470                         if (sig->params [i]->data.klass->enumtype) {
1471                                 type = sig->params [i]->data.klass->enum_basetype->type;
1472                                 goto handle_enum;
1473                         } else {
1474                                 args [i].type = VAL_POINTER;
1475                                 args [i].data.p = params [i];
1476                         }
1477                         break;
1478                 case MONO_TYPE_STRING:
1479                 case MONO_TYPE_CLASS:
1480                 case MONO_TYPE_ARRAY:
1481                 case MONO_TYPE_SZARRAY:
1482                 case MONO_TYPE_OBJECT:
1483                         args [i].type = VAL_OBJ;
1484                         args [i].data.p = params [i];
1485                         break;
1486                 default:
1487                         g_error ("type 0x%x not handled in  runtime invoke", sig->params [i]->type);
1488                 }
1489         }
1490
1491         if (method->klass->valuetype)
1492                 /* Unbox the instance, since valuetype methods expect an interior pointer. */
1493                 obj = mono_object_unbox (obj);
1494
1495         INIT_FRAME(&frame,context->current_frame,obj,args,&result,method);
1496         if (exc)
1497                 frame.invoke_trap = 1;
1498         ves_exec_method_with_context (&frame, context);
1499         if (context == &context_struct)
1500                 TlsSetValue (thread_context_id, NULL);
1501         else
1502                 context->current_frame = old_frame;
1503         if (frame.ex != NULL) {
1504                 if (exc != NULL) {
1505                         *exc = (MonoObject*) frame.ex;
1506                         if (*exc == (MonoObject *)quit_exception)
1507                                 mono_print_unhandled_exception(*exc);
1508                         return NULL;
1509                 }
1510                 if (context->current_env != NULL) {
1511                         context->env_frame->ex = frame.ex;
1512                         longjmp(*context->current_env, 1);
1513                 }
1514                 else
1515                         printf("dropped exception...\n");
1516         }
1517         if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1518                 return NULL;
1519         if (isobject || method->string_ctor)
1520                 return result.data.p;
1521         stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1522         return retval;
1523 }
1524
1525 static CRITICAL_SECTION create_method_pointer_mutex;
1526
1527 static MonoGHashTable *method_pointer_hash = NULL;
1528
1529 void *
1530 mono_create_method_pointer (MonoMethod *method)
1531 {
1532         gpointer addr;
1533         MonoJitInfo *ji;
1534
1535         EnterCriticalSection (&create_method_pointer_mutex);
1536         if (!method_pointer_hash) {
1537                 method_pointer_hash = mono_g_hash_table_new (NULL, NULL);
1538         }
1539         addr = mono_g_hash_table_lookup (method_pointer_hash, method);
1540         if (addr) {
1541                 LeaveCriticalSection (&create_method_pointer_mutex);
1542                 return addr;
1543         }
1544
1545         /*
1546          * If it is a static P/Invoke method, we can just return the pointer
1547          * to the method implementation.
1548          */
1549         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && method->addr) {
1550                 ji = g_new0 (MonoJitInfo, 1);
1551                 ji->method = method;
1552                 ji->code_size = 1;
1553                 ji->code_start = method->addr;
1554
1555                 mono_jit_info_table_add (mono_root_domain, ji);
1556                 
1557                 addr = method->addr;
1558         }               
1559         else
1560                 addr = mono_arch_create_method_pointer (method);
1561
1562         mono_g_hash_table_insert (method_pointer_hash, method, addr);
1563         LeaveCriticalSection (&create_method_pointer_mutex);
1564
1565         return addr;
1566 }
1567
1568 static MonoMethod *
1569 get_native_wrapper(MonoMethod *method, ThreadContext *context)
1570 {
1571         jmp_buf env;
1572         jmp_buf *old_env;
1573         MonoMethod *wrapper;
1574         old_env = context->current_env;
1575         if (setjmp(env) != 0) {
1576                 context->current_env = old_env;
1577                 context->search_for_handler = 1;
1578                 return NULL;
1579         }
1580         context->current_env = &env;
1581         wrapper = mono_marshal_get_native_wrapper (method);
1582         context->current_env = old_env;
1583         return wrapper;
1584 }
1585
1586 /*
1587  * Need to optimize ALU ops when natural int == int32 
1588  *
1589  * IDEA: if we maintain a stack of ip, sp to be checked
1590  * in the return opcode, we could inline simple methods that don't
1591  * use the stack or local variables....
1592  * 
1593  * The {,.S} versions of many opcodes can/should be merged to reduce code
1594  * duplication.
1595  * 
1596  */
1597 static void 
1598 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1599 {
1600         MonoDomain *domain = mono_domain_get ();        
1601         MonoInvocation child_frame;
1602         MonoMethodHeader *header;
1603         MonoMethodSignature *signature;
1604         MonoImage *image;
1605         GSList *finally_ips = NULL;
1606         const unsigned char *endfinally_ip = NULL;
1607         register const unsigned char *ip;
1608         register stackval *sp = NULL;
1609         MethodRuntimeData *rtd;
1610         void **args_pointers;
1611         gint tracing = global_tracing;
1612         unsigned char tail_recursion = 0;
1613         unsigned char unaligned_address = 0;
1614         unsigned char volatile_address = 0;
1615         vtallocation *vtalloc = NULL;
1616         MonoVTable *method_class_vt;
1617         GOTO_LABEL_VARS;
1618
1619         frame->ex = NULL;
1620         frame->ip = ip = NULL;
1621         context->current_frame = frame;
1622
1623         if (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1624                 frame->method = get_native_wrapper (frame->method, context);
1625                 if (frame->method == NULL)
1626                         goto exit_frame;
1627         }
1628
1629         method_class_vt = mono_class_vtable (domain, frame->method->klass);
1630         if (!method_class_vt->initialized)
1631                 mono_runtime_class_init (method_class_vt);
1632         signature = frame->method->signature;
1633
1634         DEBUG_ENTER ();
1635
1636         header = ((MonoMethodNormal *)frame->method)->header;
1637
1638         if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1639                 if (!frame->method->addr) {
1640                         /* assumes all internal calls with an array this are built in... */
1641                         if (signature->hasthis && frame->method->klass->rank) {
1642                                 ves_runtime_method (frame, context);
1643                                 if (frame->ex)
1644                                         goto handle_exception;
1645                                 goto exit_frame;
1646                         }
1647                         frame->method->addr = mono_lookup_internal_call (frame->method);
1648                 }
1649                 ves_pinvoke_method (frame, frame->method->signature, frame->method->addr, 
1650                             frame->method->string_ctor, context);
1651                 if (frame->ex)
1652                         goto handle_exception;
1653                 goto exit_frame;
1654         } 
1655         else if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1656                 ves_runtime_method (frame, context);
1657                 if (frame->ex)
1658                         goto handle_exception;
1659                 goto exit_frame;
1660         }
1661
1662         /*verify_method (frame->method);*/
1663
1664         image = frame->method->klass->image;
1665
1666         if (!frame->method->info)
1667                 calc_offsets (image, frame->method);
1668         rtd = frame->method->info;
1669
1670         /*
1671          * with alloca we get the expected huge performance gain
1672          * stackval *stack = g_new0(stackval, header->max_stack);
1673          */
1674         g_assert (header->max_stack < 10000);
1675         sp = frame->stack = alloca (sizeof (stackval) * header->max_stack);
1676
1677         if (header->num_locals) {
1678                 g_assert (rtd->locals_size < 65536);
1679                 frame->locals = alloca (rtd->locals_size);
1680                 /* 
1681                  * yes, we do it unconditionally, because it needs to be done for
1682                  * some cases anyway and checking for that would be even slower.
1683                  */
1684                 memset (frame->locals, 0, rtd->locals_size);
1685         }
1686
1687         /*
1688          * Copy args from stack_args to args.
1689          */
1690         if (signature->param_count || signature->hasthis) {
1691                 int i;
1692                 int has_this = signature->hasthis;
1693
1694                 g_assert (rtd->args_size < 10000);
1695                 frame->args = alloca (rtd->args_size);
1696                 g_assert ((signature->param_count + has_this) < 1000);
1697                 args_pointers = alloca (sizeof(void*) * (signature->param_count + has_this));
1698                 if (has_this) {
1699                         gpointer *this_arg;
1700                         this_arg = args_pointers [0] = frame->args;
1701                         *this_arg = frame->obj;
1702                 }
1703                 for (i = 0; i < signature->param_count; ++i) {
1704                         args_pointers [i + has_this] = frame->args + rtd->offsets [header->num_locals + has_this + i];
1705                         stackval_to_data (signature->params [i], frame->stack_args + i, args_pointers [i + has_this], signature->pinvoke);
1706                 }
1707         }
1708
1709         child_frame.parent = frame;
1710         frame->child = &child_frame;
1711         frame->ex = NULL;
1712
1713         /* ready to go */
1714         ip = header->code;
1715
1716         /*
1717          * using while (ip < end) may result in a 15% performance drop, 
1718          * but it may be useful for debug
1719          */
1720         while (1) {
1721         main_loop:
1722                 /*g_assert (sp >= stack);*/
1723 #if DEBUG_INTERP
1724                 opcode_count++;
1725                 if (tracing > 1) {
1726                         char *ins, *discode;
1727                         if (sp > frame->stack) {
1728                                 ins = dump_stack (frame->stack, sp);
1729                         } else {
1730                                 ins = g_strdup ("");
1731                         }
1732                         output_indent ();
1733                         discode = mono_disasm_code_one (NULL, frame->method, ip, NULL);
1734                         discode [strlen (discode) - 1] = 0; /* no \n */
1735                         g_print ("(%u) %-29s %s\n", GetCurrentThreadId(), discode, ins);
1736                         g_free (ins);
1737                         g_free (discode);
1738                 }
1739 #endif
1740                 
1741                 SWITCH (*ip) {
1742                 CASE (CEE_NOP) 
1743                         ++ip;
1744                         BREAK;
1745                 CASE (CEE_BREAK)
1746                         ++ip;
1747                         G_BREAKPOINT (); /* this is not portable... */
1748                         BREAK;
1749                 CASE (CEE_LDARG_0)
1750                 CASE (CEE_LDARG_1)
1751                 CASE (CEE_LDARG_2)
1752                 CASE (CEE_LDARG_3) {
1753                         int n = (*ip)-CEE_LDARG_0;
1754                         ++ip;
1755                         vt_alloc (ARG_TYPE (signature, n), sp, signature->pinvoke);
1756                         stackval_from_data (ARG_TYPE (signature, n), sp, ARG_POS (n), signature->pinvoke);
1757                         ++sp;
1758                         BREAK;
1759                 }
1760                 CASE (CEE_LDLOC_0)
1761                 CASE (CEE_LDLOC_1)
1762                 CASE (CEE_LDLOC_2)
1763                 CASE (CEE_LDLOC_3) {
1764                         int n = (*ip)-CEE_LDLOC_0;
1765                         MonoType *vartype = LOCAL_TYPE (header, n);
1766                         ++ip;
1767                         if (vartype->type == MONO_TYPE_I4) {
1768                                 sp->type = VAL_I32;
1769                                 sp->data.i = *(gint32*) LOCAL_POS (n);
1770                                 ++sp;
1771                                 BREAK;
1772                         } else {
1773                                 vt_alloc (vartype, sp, FALSE);
1774                                 stackval_from_data (vartype, sp, LOCAL_POS (n), FALSE);
1775                         }
1776                         ++sp;
1777                         BREAK;
1778                 }
1779                 CASE (CEE_STLOC_0)
1780                 CASE (CEE_STLOC_1)
1781                 CASE (CEE_STLOC_2)
1782                 CASE (CEE_STLOC_3) {
1783                         int n = (*ip)-CEE_STLOC_0;
1784                         ++ip;
1785                         --sp;
1786                         if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1787                                 gint32 *p = (gint32*)LOCAL_POS (n);
1788                                 *p = sp->data.i;
1789                                 BREAK;
1790                         } else {
1791                                 stackval_to_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1792                                 vt_free (sp);
1793                                 BREAK;
1794                         }
1795                 }
1796                 CASE (CEE_LDARG_S)
1797                         ++ip;
1798                         vt_alloc (ARG_TYPE (signature, *ip), sp, signature->pinvoke);
1799                         stackval_from_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1800                         ++sp;
1801                         ++ip;
1802                         BREAK;
1803                 CASE (CEE_LDARGA_S) {
1804                         ++ip;
1805                         sp->data.vt = ARG_POS (*ip);
1806                         sp->type = VAL_MP;
1807                         ++sp;
1808                         ++ip;
1809                         BREAK;
1810                 }
1811                 CASE (CEE_STARG_S)
1812                         ++ip;
1813                         --sp;
1814                         stackval_to_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1815                         vt_free (sp);
1816                         ++ip;
1817                         BREAK;
1818                 CASE (CEE_LDLOC_S)
1819                         ++ip;
1820                         vt_alloc (LOCAL_TYPE (header, *ip), sp, FALSE);
1821                         stackval_from_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1822                         ++ip;
1823                         ++sp;
1824                         BREAK;
1825                 CASE (CEE_LDLOCA_S) {
1826                         ++ip;
1827                         sp->data.p = LOCAL_POS (*ip);
1828                         sp->type = VAL_MP;
1829                         ++sp;
1830                         ++ip;
1831                         BREAK;
1832                 }
1833                 CASE (CEE_STLOC_S)
1834                         ++ip;
1835                         --sp;
1836                         stackval_to_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1837                         vt_free (sp);
1838                         ++ip;
1839                         BREAK;
1840                 CASE (CEE_LDNULL) 
1841                         ++ip;
1842                         sp->type = VAL_OBJ;
1843                         sp->data.p = NULL;
1844                         ++sp;
1845                         BREAK;
1846                 CASE (CEE_LDC_I4_M1)
1847                         ++ip;
1848                         sp->type = VAL_I32;
1849                         sp->data.i = -1;
1850                         ++sp;
1851                         BREAK;
1852                 CASE (CEE_LDC_I4_0)
1853                 CASE (CEE_LDC_I4_1)
1854                 CASE (CEE_LDC_I4_2)
1855                 CASE (CEE_LDC_I4_3)
1856                 CASE (CEE_LDC_I4_4)
1857                 CASE (CEE_LDC_I4_5)
1858                 CASE (CEE_LDC_I4_6)
1859                 CASE (CEE_LDC_I4_7)
1860                 CASE (CEE_LDC_I4_8)
1861                         sp->type = VAL_I32;
1862                         sp->data.i = (*ip) - CEE_LDC_I4_0;
1863                         ++sp;
1864                         ++ip;
1865                         BREAK;
1866                 CASE (CEE_LDC_I4_S) 
1867                         ++ip;
1868                         sp->type = VAL_I32;
1869                         sp->data.i = *(const gint8 *)ip;
1870                         ++ip;
1871                         ++sp;
1872                         BREAK;
1873                 CASE (CEE_LDC_I4)
1874                         ++ip;
1875                         sp->type = VAL_I32;
1876                         sp->data.i = read32 (ip);
1877                         ip += 4;
1878                         ++sp;
1879                         BREAK;
1880                 CASE (CEE_LDC_I8)
1881                         ++ip;
1882                         sp->type = VAL_I64;
1883                         sp->data.l = read64 (ip);
1884                         ip += 8;
1885                         ++sp;
1886                         BREAK;
1887                 CASE (CEE_LDC_R4) {
1888                         float val;
1889                         ++ip;
1890                         sp->type = VAL_DOUBLE;
1891                         readr4 (ip, &val);
1892                         sp->data.f = val;
1893                         ip += 4;
1894                         ++sp;
1895                         BREAK;
1896                 }
1897                 CASE (CEE_LDC_R8) 
1898                         ++ip;
1899                         sp->type = VAL_DOUBLE;
1900                         readr8(ip, &sp->data.f);
1901                         ip += 8;
1902                         ++sp;
1903                         BREAK;
1904                 CASE (CEE_UNUSED99) ves_abort (); BREAK;
1905                 CASE (CEE_DUP) 
1906                         stackvalpush(sp - 1, sp);
1907                         ++ip; 
1908                         BREAK;
1909                 CASE (CEE_POP)
1910                         ++ip;
1911                         --sp;
1912                         vt_free (sp);
1913                         BREAK;
1914                 CASE (CEE_JMP) ves_abort(); BREAK;
1915                 CASE (CEE_CALLVIRT) /* Fall through */
1916                 CASE (CEE_CALLI)    /* Fall through */
1917                 CASE (CEE_CALL) {
1918                         MonoMethodSignature *csignature;
1919                         stackval retval;
1920                         stackval *endsp = sp;
1921                         guint32 token;
1922                         int virtual = *ip == CEE_CALLVIRT;
1923                         int calli = *ip == CEE_CALLI;
1924                         unsigned char *code = NULL;
1925
1926                         /*
1927                          * We ignore tail recursion for now.
1928                          */
1929                         tail_recursion = 0;
1930
1931                         frame->ip = ip;
1932                         
1933                         ++ip;
1934                         token = read32 (ip);
1935                         ip += 4;
1936                         if (calli) {
1937                                 MonoJitInfo *ji;
1938                                 --sp;
1939                                 code = sp->data.p;
1940                                 if (frame->method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE &&
1941                                         (ji = mono_jit_info_table_find (mono_root_domain, code)) != NULL) {
1942                                         child_frame.method = ji->method;
1943                                         csignature = ji->method->signature;
1944                                 }
1945                                 else if (frame->method->wrapper_type != MONO_WRAPPER_NONE) {
1946                                         csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (frame->method, token);
1947                                         child_frame.method = NULL;
1948                                 } else {
1949                                         g_assert_not_reached ();
1950                                 }
1951                                 g_assert (code);
1952                         } else {
1953                                 if (frame->method->wrapper_type != MONO_WRAPPER_NONE) 
1954                                         child_frame.method = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
1955                                 else
1956                                         child_frame.method = mono_get_method (image, token, NULL);
1957                                 if (!child_frame.method)
1958                                         THROW_EX (mono_get_exception_missing_method (), ip -5);
1959                                 csignature = child_frame.method->signature;
1960                                 if (virtual) {
1961                                         stackval *this_arg = &sp [-csignature->param_count-1];
1962                                         if (!this_arg->data.p)
1963                                                 THROW_EX (mono_get_exception_null_reference(), ip - 5);
1964                                         child_frame.method = get_virtual_method (domain, child_frame.method, this_arg);
1965                                         if (!child_frame.method)
1966                                                 THROW_EX (mono_get_exception_missing_method (), ip -5);
1967                                 }
1968                         }
1969
1970                         if (frame->method->wrapper_type == MONO_WRAPPER_NONE)
1971                                 if (child_frame.method && child_frame.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1972                                         child_frame.method = mono_marshal_get_synchronized_wrapper (child_frame.method);
1973
1974                         g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
1975                         /* decrement by the actual number of args */
1976                         if (csignature->param_count) {
1977                                 sp -= csignature->param_count;
1978                                 child_frame.stack_args = sp;
1979                         } else {
1980                                 child_frame.stack_args = NULL;
1981                         }
1982                         if (csignature->hasthis) {
1983                                 g_assert (sp >= frame->stack);
1984                                 --sp;
1985                                 /*
1986                                  * It may also be a TP from LD(S)FLDA
1987                                  * g_assert (sp->type == VAL_OBJ || sp->type == VAL_MP);
1988                                  */
1989                                 if (sp->type == VAL_OBJ && child_frame.method &&
1990                                     child_frame.method->klass->valuetype) /* unbox it */
1991                                         child_frame.obj = (char*)sp->data.p + sizeof (MonoObject);
1992                                 else
1993                                         child_frame.obj = sp->data.p;
1994                         } else {
1995                                 child_frame.obj = NULL;
1996                         }
1997                         if (csignature->ret->type != MONO_TYPE_VOID) {
1998                                 vt_alloc (csignature->ret, &retval, csignature->pinvoke);
1999                                 child_frame.retval = &retval;
2000                         } else {
2001                                 child_frame.retval = NULL;
2002                         }
2003
2004                         child_frame.ip = NULL;
2005                         child_frame.ex = NULL;
2006                         child_frame.ex_handler = NULL;
2007
2008                         if (!child_frame.method) {
2009                                 g_assert (code);
2010                                 ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE, context);
2011                                 if (child_frame.ex) {
2012                                         frame->ex = child_frame.ex;
2013                                         goto handle_exception;
2014                                 }
2015                         } else if (csignature->hasthis && sp->type == VAL_OBJ &&
2016                                         ((MonoObject *)sp->data.p)->vtable->klass == mono_defaults.transparent_proxy_class) {
2017                                 g_assert (child_frame.method);
2018                                 child_frame.method = mono_marshal_get_remoting_invoke (child_frame.method);
2019                                 ves_exec_method_with_context (&child_frame, context);
2020                         } else {
2021                                 switch (GPOINTER_TO_UINT (child_frame.method->addr)) {
2022                                 case INLINE_STRING_LENGTH:
2023                                         retval.type = VAL_I32;
2024                                         retval.data.i = ((MonoString*)sp->data.p)->length;
2025                                         /*g_print ("length of '%s' is %d\n", mono_string_to_utf8 (sp->data.p), retval.data.i);*/
2026                                         break;
2027                                 case INLINE_STRING_GET_CHARS: {
2028                                         int idx = GET_NATI(sp [1]);
2029                                         if ((idx < 0) || (idx >= mono_string_length ((MonoString*)sp->data.p))) {
2030                                                 child_frame.ex = mono_get_exception_index_out_of_range ();
2031                                                 FILL_IN_TRACE(child_frame.ex, &child_frame);
2032                                         }
2033                                         else {
2034                                                 retval.type = VAL_I32;
2035                                                 retval.data.i = mono_string_chars((MonoString*)sp->data.p)[idx];
2036                                         }
2037                                         break;
2038                                 }
2039                                 case INLINE_ARRAY_LENGTH:
2040                                         retval.type = VAL_I32;
2041                                         retval.data.i = mono_array_length ((MonoArray*)sp->data.p);
2042                                         break;
2043                                 case INLINE_ARRAY_RANK:
2044                                         retval.type = VAL_I32;
2045                                         retval.data.i = mono_object_class (sp->data.p)->rank;
2046                                         break;
2047                                 case INLINE_TYPE_ELEMENT_TYPE:
2048                                         retval.type = VAL_OBJ;
2049                                         {
2050                                                 MonoClass *c = mono_class_from_mono_type (((MonoReflectionType*)sp->data.p)->type);
2051                                                 if (c->enumtype && c->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
2052                                                         retval.data.p = mono_type_get_object (domain, c->enum_basetype);
2053                                                 else if (c->element_class)
2054                                                         retval.data.p = mono_type_get_object (domain, &c->element_class->byval_arg);
2055                                                 else
2056                                                         retval.data.p = NULL;
2057                                         }
2058                                         break;
2059                                 default:
2060                                         ves_exec_method_with_context (&child_frame, context);
2061                                 }
2062                         }
2063
2064                         context->current_frame = frame;
2065
2066                         while (endsp > sp) {
2067                                 --endsp;
2068                                 vt_free (endsp);
2069                         }
2070
2071                         if (child_frame.ex) {
2072                                 /*
2073                                  * An exception occurred, need to run finally, fault and catch handlers..
2074                                  */
2075                                 frame->ex = child_frame.ex;
2076                                 if (context->search_for_handler) {
2077                                         context->search_for_handler = 0;
2078                                         goto handle_exception;
2079                                 }
2080                                 goto handle_finally;
2081                         }
2082
2083                         /* need to handle typedbyref ... */
2084                         if (csignature->ret->type != MONO_TYPE_VOID) {
2085                                 *sp = retval;
2086                                 sp++;
2087                         }
2088                         BREAK;
2089                 }
2090                 CASE (CEE_RET)
2091                         if (signature->ret->type != MONO_TYPE_VOID) {
2092                                 --sp;
2093                                 if (sp->type == VAL_VALUET) {
2094                                         /* the caller has already allocated the memory */
2095                                         stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);
2096                                         vt_free (sp);
2097                                 } else {
2098                                         *frame->retval = *sp;
2099                                 }
2100                         }
2101                         if (sp > frame->stack)
2102                                 g_warning ("more values on stack: %d", sp-frame->stack);
2103
2104                         goto exit_frame;
2105                 CASE (CEE_BR_S) /* Fall through */
2106                 CASE (CEE_BR)
2107                         if (*ip == CEE_BR) {
2108                                 ++ip;
2109                                 ip += (gint32) read32(ip);
2110                                 ip += 4;
2111                         } else {
2112                                 ++ip;
2113                                 ip += (signed char) *ip;
2114                                 ++ip;
2115                         }
2116                         BREAK;
2117                 CASE (CEE_BRFALSE) /* Fall through */
2118                 CASE (CEE_BRFALSE_S) {
2119                         int result;
2120                         int broffset;
2121                         if (*ip == CEE_BRFALSE_S) {
2122                                 broffset = (signed char)ip [1];
2123                                 ip += 2;
2124                         } else {
2125                                 broffset = (gint32) read32 (ip + 1);
2126                                 ip += 5;
2127                         }
2128                         --sp;
2129                         switch (sp->type) {
2130                         case VAL_I32: result = sp->data.i == 0; break;
2131                         case VAL_I64: result = sp->data.l == 0; break;
2132                         case VAL_DOUBLE: result = sp->data.f ? 0: 1; break;
2133                         default: result = sp->data.p == NULL; break;
2134                         }
2135                         if (result)
2136                                 ip += broffset;
2137                         BREAK;
2138                 }
2139                 CASE (CEE_BRTRUE) /* Fall through */
2140                 CASE (CEE_BRTRUE_S) {
2141                         int result;
2142                         int broffset;
2143                         if (*ip == CEE_BRTRUE_S) {
2144                                 broffset = (signed char)ip [1];
2145                                 ip += 2;
2146                         } else {
2147                                 broffset = (gint32) read32 (ip + 1);
2148                                 ip += 5;
2149                         }
2150                         --sp;
2151                         switch (sp->type) {
2152                         case VAL_I32: result = sp->data.i != 0; break;
2153                         case VAL_I64: result = sp->data.l != 0; break;
2154                         case VAL_DOUBLE: result = sp->data.f ? 1 : 0; break;
2155                         default: result = sp->data.p != NULL; break;
2156                         }
2157                         if (result)
2158                                 ip += broffset;
2159                         BREAK;
2160                 }
2161                 CASE (CEE_BEQ) /* Fall through */
2162                 CASE (CEE_BEQ_S) {
2163                         int result;
2164                         int broffset;
2165                         if (*ip == CEE_BEQ_S) {
2166                                 broffset = (signed char)ip [1];
2167                                 ip += 2;
2168                         } else {
2169                                 broffset = (gint32) read32 (ip + 1);
2170                                 ip += 5;
2171                         }
2172                         sp -= 2;
2173                         if (sp->type == VAL_I32)
2174                                 result = (mono_i)sp [0].data.i == (mono_i)GET_NATI (sp [1]);
2175                         else if (sp->type == VAL_I64)
2176                                 result = sp [0].data.l == sp [1].data.l;
2177                         else if (sp->type == VAL_DOUBLE)
2178                                 result = sp [0].data.f == sp [1].data.f;
2179                         else
2180                                 result = sp [0].data.nati == (mono_i)GET_NATI (sp [1]);
2181                         if (result)
2182                                 ip += broffset;
2183                         BREAK;
2184                 }
2185                 CASE (CEE_BGE) /* Fall through */
2186                 CASE (CEE_BGE_S) {
2187                         int result;
2188                         int broffset;
2189                         if (*ip == CEE_BGE_S) {
2190                                 broffset = (signed char)ip [1];
2191                                 ip += 2;
2192                         } else {
2193                                 broffset = (gint32) read32 (ip + 1);
2194                                 ip += 5;
2195                         }
2196                         sp -= 2;
2197                         if (sp->type == VAL_I32)
2198                                 result = (mono_i)sp [0].data.i >= (mono_i)GET_NATI (sp [1]);
2199                         else if (sp->type == VAL_I64)
2200                                 result = sp [0].data.l >= sp [1].data.l;
2201                         else if (sp->type == VAL_DOUBLE)
2202                                 result = sp [0].data.f >= sp [1].data.f;
2203                         else
2204                                 result = (mono_i)sp [0].data.nati >= (mono_i)GET_NATI (sp [1]);
2205                         if (result)
2206                                 ip += broffset;
2207                         BREAK;
2208                 }
2209                 CASE (CEE_BGT) /* Fall through */
2210                 CASE (CEE_BGT_S) {
2211                         int result;
2212                         int broffset;
2213                         if (*ip == CEE_BGT_S) {
2214                                 broffset = (signed char)ip [1];
2215                                 ip += 2;
2216                         } else {
2217                                 broffset = (gint32) read32 (ip + 1);
2218                                 ip += 5;
2219                         }
2220                         sp -= 2;
2221                         if (sp->type == VAL_I32)
2222                                 result = (mono_i)sp [0].data.i > (mono_i)GET_NATI (sp [1]);
2223                         else if (sp->type == VAL_I64)
2224                                 result = sp [0].data.l > sp [1].data.l;
2225                         else if (sp->type == VAL_DOUBLE)
2226                                 result = sp [0].data.f > sp [1].data.f;
2227                         else
2228                                 result = (mono_i)sp [0].data.nati > (mono_i)GET_NATI (sp [1]);
2229                         if (result)
2230                                 ip += broffset;
2231                         BREAK;
2232                 }
2233                 CASE (CEE_BLT) /* Fall through */
2234                 CASE (CEE_BLT_S) {
2235                         int result;
2236                         int broffset;
2237                         if (*ip == CEE_BLT_S) {
2238                                 broffset = (signed char)ip [1];
2239                                 ip += 2;
2240                         } else {
2241                                 broffset = (gint32) read32 (ip + 1);
2242                                 ip += 5;
2243                         }
2244                         sp -= 2;
2245                         if (sp->type == VAL_I32)
2246                                 result = (mono_i)sp[0].data.i < (mono_i)GET_NATI(sp[1]);
2247                         else if (sp->type == VAL_I64)
2248                                 result = sp[0].data.l < sp[1].data.l;
2249                         else if (sp->type == VAL_DOUBLE)
2250                                 result = sp[0].data.f < sp[1].data.f;
2251                         else
2252                                 result = (mono_i)sp[0].data.nati < (mono_i)GET_NATI(sp[1]);
2253                         if (result)
2254                                 ip += broffset;
2255                         BREAK;
2256                 }
2257                 CASE (CEE_BLE) /* fall through */
2258                 CASE (CEE_BLE_S) {
2259                         int result;
2260                         int broffset;
2261                         if (*ip == CEE_BLE_S) {
2262                                 broffset = (signed char)ip [1];
2263                                 ip += 2;
2264                         } else {
2265                                 broffset = (gint32) read32 (ip + 1);
2266                                 ip += 5;
2267                         }
2268                         sp -= 2;
2269
2270                         if (sp->type == VAL_I32)
2271                                 result = (mono_i)sp [0].data.i <= (mono_i)GET_NATI (sp [1]);
2272                         else if (sp->type == VAL_I64)
2273                                 result = sp [0].data.l <= sp [1].data.l;
2274                         else if (sp->type == VAL_DOUBLE)
2275                                 result = sp [0].data.f <= sp [1].data.f;
2276                         else {
2277                                 result = (mono_i)sp [0].data.nati <= (mono_i)GET_NATI (sp [1]);
2278                         }
2279                         if (result)
2280                                 ip += broffset;
2281                         BREAK;
2282                 }
2283                 CASE (CEE_BNE_UN) /* Fall through */
2284                 CASE (CEE_BNE_UN_S) {
2285                         int result;
2286                         int broffset;
2287                         if (*ip == CEE_BNE_UN_S) {
2288                                 broffset = (signed char)ip [1];
2289                                 ip += 2;
2290                         } else {
2291                                 broffset = (gint32) read32 (ip + 1);
2292                                 ip += 5;
2293                         }
2294                         sp -= 2;
2295                         if (sp->type == VAL_I32)
2296                                 result = (mono_u)sp [0].data.i != (mono_u)GET_NATI (sp [1]);
2297                         else if (sp->type == VAL_I64)
2298                                 result = (guint64)sp [0].data.l != (guint64)sp [1].data.l;
2299                         else if (sp->type == VAL_DOUBLE)
2300                                 result = isunordered (sp [0].data.f, sp [1].data.f) ||
2301                                         (sp [0].data.f != sp [1].data.f);
2302                         else
2303                                 result = (mono_u)sp [0].data.nati != (mono_u)GET_NATI (sp [1]);
2304                         if (result)
2305                                 ip += broffset;
2306                         BREAK;
2307                 }
2308                 CASE (CEE_BGE_UN) /* Fall through */
2309                 CASE (CEE_BGE_UN_S) {
2310                         int result;
2311                         int broffset;
2312                         if (*ip == CEE_BGE_UN_S) {
2313                                 broffset = (signed char)ip [1];
2314                                 ip += 2;
2315                         } else {
2316                                 broffset = (gint32) read32 (ip + 1);
2317                                 ip += 5;
2318                         }
2319                         sp -= 2;
2320                         if (sp->type == VAL_I32)
2321                                 result = (mono_u)sp [0].data.i >= (mono_u)GET_NATI (sp [1]);
2322                         else if (sp->type == VAL_I64)
2323                                 result = (guint64)sp [0].data.l >= (guint64)sp [1].data.l;
2324                         else if (sp->type == VAL_DOUBLE)
2325                                 result = !isless (sp [0].data.f,sp [1].data.f);
2326                         else
2327                                 result = (mono_u)sp [0].data.nati >= (mono_u)GET_NATI (sp [1]);
2328                         if (result)
2329                                 ip += broffset;
2330                         BREAK;
2331                 }
2332                 CASE (CEE_BGT_UN) /* Fall through */
2333                 CASE (CEE_BGT_UN_S) {
2334                         int result;
2335                         int broffset;
2336                         if (*ip == CEE_BGT_UN_S) {
2337                                 broffset = (signed char)ip [1];
2338                                 ip += 2;
2339                         } else {
2340                                 broffset = (gint32) read32 (ip + 1);
2341                                 ip += 5;
2342                         }
2343                         sp -= 2;
2344                         if (sp->type == VAL_I32)
2345                                 result = (mono_u)sp [0].data.i > (mono_u)GET_NATI (sp [1]);
2346                         else if (sp->type == VAL_I64)
2347                                 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
2348                         else if (sp->type == VAL_DOUBLE)
2349                                 result = isgreater (sp [0].data.f, sp [1].data.f);
2350                         else
2351                                 result = (mono_u)sp [0].data.nati > (mono_u)GET_NATI (sp [1]);
2352                         if (result)
2353                                 ip += broffset;
2354                         BREAK;
2355                 }
2356                 CASE (CEE_BLE_UN) /* Fall through */
2357                 CASE (CEE_BLE_UN_S) {
2358                         int result;
2359                         int broffset;
2360                         if (*ip == CEE_BLE_UN_S) {
2361                                 broffset = (signed char)ip [1];
2362                                 ip += 2;
2363                         } else {
2364                                 broffset = (gint32) read32 (ip + 1);
2365                                 ip += 5;
2366                         }
2367                         sp -= 2;
2368                         if (sp->type == VAL_I32)
2369                                 result = (mono_u)sp [0].data.i <= (mono_u)GET_NATI (sp [1]);
2370                         else if (sp->type == VAL_I64)
2371                                 result = (guint64)sp [0].data.l <= (guint64)sp [1].data.l;
2372                         else if (sp->type == VAL_DOUBLE)
2373                                 result = islessequal (sp [0].data.f, sp [1].data.f);
2374                         else
2375                                 result = (mono_u)sp [0].data.nati <= (mono_u)GET_NATI (sp [1]);
2376                         if (result)
2377                                 ip += broffset;
2378                         BREAK;
2379                 }
2380                 CASE (CEE_BLT_UN) /* Fall through */
2381                 CASE (CEE_BLT_UN_S) {
2382                         int result;
2383                         int broffset;
2384                         if (*ip == CEE_BLT_UN_S) {
2385                                 broffset = (signed char)ip [1];
2386                                 ip += 2;
2387                         } else {
2388                                 broffset = (gint32) read32 (ip + 1);
2389                                 ip += 5;
2390                         }
2391                         sp -= 2;
2392                         if (sp->type == VAL_I32)
2393                                 result = (mono_u)sp[0].data.i < (mono_u)GET_NATI(sp[1]);
2394                         else if (sp->type == VAL_I64)
2395                                 result = (guint64)sp[0].data.l < (guint64)sp[1].data.l;
2396                         else if (sp->type == VAL_DOUBLE)
2397                                 result = isunordered (sp [0].data.f, sp [1].data.f) ||
2398                                         (sp [0].data.f < sp [1].data.f);
2399                         else
2400                                 result = (mono_u)sp[0].data.nati < (mono_u)GET_NATI(sp[1]);
2401                         if (result)
2402                                 ip += broffset;
2403                         BREAK;
2404                 }
2405                 CASE (CEE_SWITCH) {
2406                         guint32 n;
2407                         const unsigned char *st;
2408                         ++ip;
2409                         n = read32 (ip);
2410                         ip += 4;
2411                         st = ip + sizeof (gint32) * n;
2412                         --sp;
2413                         if ((guint32)sp->data.i < n) {
2414                                 gint offset;
2415                                 ip += sizeof (gint32) * (guint32)sp->data.i;
2416                                 offset = read32 (ip);
2417                                 ip = st + offset;
2418                         } else {
2419                                 ip = st;
2420                         }
2421                         BREAK;
2422                 }
2423                 CASE (CEE_LDIND_I1)
2424                         ++ip;
2425                         sp[-1].type = VAL_I32;
2426                         sp[-1].data.i = *(gint8*)sp[-1].data.p;
2427                         BREAK;
2428                 CASE (CEE_LDIND_U1)
2429                         ++ip;
2430                         sp[-1].type = VAL_I32;
2431                         sp[-1].data.i = *(guint8*)sp[-1].data.p;
2432                         BREAK;
2433                 CASE (CEE_LDIND_I2)
2434                         ++ip;
2435                         sp[-1].type = VAL_I32;
2436                         sp[-1].data.i = *(gint16*)sp[-1].data.p;
2437                         BREAK;
2438                 CASE (CEE_LDIND_U2)
2439                         ++ip;
2440                         sp[-1].type = VAL_I32;
2441                         sp[-1].data.i = *(guint16*)sp[-1].data.p;
2442                         BREAK;
2443                 CASE (CEE_LDIND_I4) /* Fall through */
2444                 CASE (CEE_LDIND_U4)
2445                         ++ip;
2446                         sp[-1].type = VAL_I32;
2447                         sp[-1].data.i = *(gint32*)sp[-1].data.p;
2448                         BREAK;
2449                 CASE (CEE_LDIND_I8)
2450                         ++ip;
2451                         sp[-1].type = VAL_I64;
2452                         sp[-1].data.l = *(gint64*)sp[-1].data.p;
2453                         BREAK;
2454                 CASE (CEE_LDIND_I)
2455                         ++ip;
2456                         sp[-1].type = VAL_NATI;
2457                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2458                         BREAK;
2459                 CASE (CEE_LDIND_R4)
2460                         ++ip;
2461                         sp[-1].type = VAL_DOUBLE;
2462                         sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2463                         BREAK;
2464                 CASE (CEE_LDIND_R8)
2465                         ++ip;
2466                         sp[-1].type = VAL_DOUBLE;
2467                         sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2468                         BREAK;
2469                 CASE (CEE_LDIND_REF)
2470                         ++ip;
2471                         sp[-1].type = VAL_OBJ;
2472                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2473                         BREAK;
2474                 CASE (CEE_STIND_REF) {
2475                         gpointer *p;
2476                         ++ip;
2477                         sp -= 2;
2478                         p = sp->data.p;
2479                         *p = sp[1].data.p;
2480                         BREAK;
2481                 }
2482                 CASE (CEE_STIND_I1) {
2483                         gint8 *p;
2484                         ++ip;
2485                         sp -= 2;
2486                         p = sp->data.p;
2487                         *p = (gint8)sp[1].data.i;
2488                         BREAK;
2489                 }
2490                 CASE (CEE_STIND_I2) {
2491                         gint16 *p;
2492                         ++ip;
2493                         sp -= 2;
2494                         p = sp->data.p;
2495                         *p = (gint16)sp[1].data.i;
2496                         BREAK;
2497                 }
2498                 CASE (CEE_STIND_I4) {
2499                         gint32 *p;
2500                         ++ip;
2501                         sp -= 2;
2502                         p = sp->data.p;
2503                         *p = sp[1].data.i;
2504                         BREAK;
2505                 }
2506                 CASE (CEE_STIND_I) {
2507                         mono_i *p;
2508                         ++ip;
2509                         sp -= 2;
2510                         p = sp->data.p;
2511                         *p = (mono_i)sp[1].data.p;
2512                         BREAK;
2513                 }
2514                 CASE (CEE_STIND_I8) {
2515                         gint64 *p;
2516                         ++ip;
2517                         sp -= 2;
2518                         p = sp->data.p;
2519                         *p = sp[1].data.l;
2520                         BREAK;
2521                 }
2522                 CASE (CEE_STIND_R4) {
2523                         float *p;
2524                         ++ip;
2525                         sp -= 2;
2526                         p = sp->data.p;
2527                         *p = (gfloat)sp[1].data.f;
2528                         BREAK;
2529                 }
2530                 CASE (CEE_STIND_R8) {
2531                         double *p;
2532                         ++ip;
2533                         sp -= 2;
2534                         p = sp->data.p;
2535                         *p = sp[1].data.f;
2536                         BREAK;
2537                 }
2538                 CASE (CEE_ADD)
2539                         ++ip;
2540                         --sp;
2541                         /* should probably consider the pointers as unsigned */
2542                         if (sp->type == VAL_I32) {
2543                                 if (sp [-1].type == VAL_I32)
2544                                         sp [-1].data.i += sp [0].data.i;
2545                                 else
2546                                         sp [-1].data.nati = sp [-1].data.nati + sp [0].data.i;
2547                         } else if (sp->type == VAL_I64)
2548                                 sp [-1].data.l += sp [0].data.l;
2549                         else if (sp->type == VAL_DOUBLE)
2550                                 sp [-1].data.f += sp [0].data.f;
2551                         else {
2552                                 if (sp [-1].type == VAL_I32) {
2553                                         sp [-1].data.nati = sp [-1].data.i + sp [0].data.nati;
2554                                         sp [-1].type = sp [0].type;
2555                                 } else
2556                                         sp [-1].data.nati = sp [-1].data.nati + sp [0].data.nati;
2557                         }
2558                         BREAK;
2559                 CASE (CEE_SUB)
2560                         ++ip;
2561                         --sp;
2562                         /* should probably consider the pointers as unsigned */
2563                         if (sp->type == VAL_I32) {
2564                                 if (sp [-1].type == VAL_I32)
2565                                         sp [-1].data.i -= sp [0].data.i;
2566                                 else {
2567                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.i;
2568                                         sp [-1].type = VAL_NATI;
2569                                 }
2570                         } else if (sp->type == VAL_I64)
2571                                 sp [-1].data.l -= sp [0].data.l;
2572                         else if (sp->type == VAL_DOUBLE)
2573                                 sp [-1].data.f -= sp [0].data.f;
2574                         else {
2575                                 if (sp [-1].type == VAL_I32) {
2576                                         sp [-1].data.nati = sp [-1].data.i - sp [0].data.nati;
2577                                         sp [-1].type = sp [0].type;
2578                                 } else
2579                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.nati;
2580                         }
2581                         BREAK;
2582                 CASE (CEE_MUL)
2583                         ++ip;
2584                         --sp;
2585                         if (sp->type == VAL_I32) {
2586                                 if (sp [-1].type == VAL_I32)
2587                                         sp [-1].data.i *= sp [0].data.i;
2588                                 else
2589                                         sp [-1].data.nati = sp [-1].data.nati * sp [0].data.i;
2590                         } else if (sp->type == VAL_I64)
2591                                 sp [-1].data.l *= sp [0].data.l;
2592                         else if (sp->type == VAL_DOUBLE)
2593                                 sp [-1].data.f *= sp [0].data.f;
2594                         else {
2595                                 if (sp [-1].type == VAL_NATI)
2596                                         sp [-1].data.nati = sp [-1].data.nati * sp [0].data.nati;
2597                                 else {
2598                                         sp [-1].data.nati = sp [-1].data.i * sp [0].data.nati;
2599                                         sp [-1].type = VAL_NATI;
2600                                 }
2601                         }
2602                         BREAK;
2603                 CASE (CEE_DIV)
2604                         ++ip;
2605                         --sp;
2606                         if (sp->type == VAL_I32) {
2607                                 if (sp [0].data.i == 0)
2608                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2609                                 if (sp [-1].type == VAL_I32)
2610                                         sp [-1].data.i /= sp [0].data.i;
2611                                 else
2612                                         sp [-1].data.nati /= sp [0].data.i;
2613                         } else if (sp->type == VAL_I64) {
2614                                 if (sp [0].data.l == 0)
2615                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2616                                 sp [-1].data.l /= sp [0].data.l;
2617                         } else if (sp->type == VAL_DOUBLE) {
2618                                 /* set NaN is divisor is 0.0 */
2619                                 sp [-1].data.f /= sp [0].data.f;
2620                         } else {
2621                                 if (sp [-1].type == VAL_NATI)
2622                                         sp [-1].data.nati = sp [-1].data.nati / sp [0].data.nati;
2623                                 else {
2624                                         sp [-1].data.nati = sp [-1].data.i / sp [0].data.nati;
2625                                         sp [-1].type = VAL_NATI;
2626                                 }
2627                         }
2628                         BREAK;
2629                 CASE (CEE_DIV_UN)
2630                         ++ip;
2631                         --sp;
2632                         if (sp->type == VAL_I32) {
2633                                 if (sp [0].data.i == 0)
2634                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2635                                 if (sp [-1].type == VAL_NATI)
2636                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati / (mono_u)sp [0].data.i;
2637                                 else
2638                                         sp [-1].data.i = (guint32)sp [-1].data.i / (guint32)sp [0].data.i;
2639                         } else if (sp->type == VAL_I64) {
2640                                 guint64 val;
2641                                 if (sp [0].data.l == 0)
2642                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2643                                 val = sp [-1].data.l;
2644                                 val /= (guint64)sp [0].data.l;
2645                                 sp [-1].data.l = val;
2646                         } else {
2647                                 if (sp [0].data.nati == 0)
2648                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2649                                 if (sp [-1].type == VAL_NATI)
2650                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati / (mono_u)sp [0].data.nati;
2651                                 else {
2652                                         sp [-1].data.nati = (mono_u)sp [-1].data.i / (mono_u)sp [0].data.nati;
2653                                         sp [-1].type = VAL_NATI;
2654                                 }
2655                         }
2656                         BREAK;
2657                 CASE (CEE_REM)
2658                         ++ip;
2659                         --sp;
2660                         if (sp->type == VAL_I32) {
2661                                 if (sp [0].data.i == 0)
2662                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2663                                 if (sp [-1].type == VAL_NATI)
2664                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati % (mono_i)sp [0].data.i;
2665                                 else
2666                                         sp [-1].data.i = sp [-1].data.i % sp [0].data.i;
2667                         } else if (sp->type == VAL_I64) {
2668                                 if (sp [0].data.l == 0)
2669                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2670                                 sp [-1].data.l %= sp [0].data.l;
2671                         } else if (sp->type == VAL_DOUBLE) {
2672                                 /* FIXME: what do we actually do here? */
2673                                 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2674                         } else {
2675                                 if (sp [0].data.nati == 0)
2676                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2677                                 if (sp [-1].type == VAL_NATI)
2678                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati % (mono_i)sp [0].data.nati;
2679                                 else {
2680                                         sp [-1].data.nati = (mono_i)sp [-1].data.i % (mono_i)sp [0].data.nati;
2681                                         sp [-1].type = VAL_NATI;
2682                                 }
2683                         }
2684                         BREAK;
2685                 CASE (CEE_REM_UN)
2686                         ++ip;
2687                         --sp;
2688                         if (sp->type == VAL_I32) {
2689                                 if (sp [0].data.i == 0)
2690                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2691                                 if (sp [-1].type == VAL_NATI)
2692                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati % (mono_u)sp [0].data.i;
2693                                 else
2694                                         sp [-1].data.i = (guint32)sp [-1].data.i % (guint32)sp [0].data.i;
2695                         } else if (sp->type == VAL_I64) {
2696                                 if (sp [0].data.l == 0)
2697                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2698                                 sp [-1].data.l = (guint64)sp [-1].data.l % (guint64)sp [0].data.l;
2699                         } else if (sp->type == VAL_DOUBLE) {
2700                                 /* unspecified behaviour according to the spec */
2701                         } else {
2702                                 if (sp [0].data.nati == 0)
2703                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2704                                 if (sp [-1].type == VAL_NATI)
2705                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati % (mono_u)sp [0].data.nati;
2706                                 else {
2707                                         sp [-1].data.nati = (mono_u)sp [-1].data.i % (mono_u)sp [0].data.nati;
2708                                         sp [-1].type = VAL_NATI;
2709                                 }
2710                         }
2711                         BREAK;
2712                 CASE (CEE_AND)
2713                         ++ip;
2714                         --sp;
2715                         if (sp->type == VAL_I32) {
2716                                 if (sp [-1].type == VAL_NATI)
2717                                         sp [-1].data.nati &= sp [0].data.i;
2718                                 else
2719                                         sp [-1].data.i &= sp [0].data.i;
2720                         }
2721                         else if (sp->type == VAL_I64)
2722                                 sp [-1].data.l &= sp [0].data.l;
2723                         else {
2724                                 if (sp [-1].type == VAL_NATI)
2725                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati & (mono_i)sp [0].data.nati;
2726                                 else {
2727                                         sp [-1].data.nati = (mono_i)sp [-1].data.i & (mono_i)sp [0].data.nati;
2728                                         sp [-1].type = VAL_NATI;
2729                                 }
2730                         }
2731                         BREAK;
2732                 CASE (CEE_OR)
2733                         ++ip;
2734                         --sp;
2735                         if (sp->type == VAL_I32) {
2736                                 if (sp [-1].type == VAL_NATI)
2737                                         sp [-1].data.nati |= sp [0].data.i;
2738                                 else
2739                                         sp [-1].data.i |= sp [0].data.i;
2740                         }
2741                         else if (sp->type == VAL_I64)
2742                                 sp [-1].data.l |= sp [0].data.l;
2743                         else {
2744                                 if (sp [-1].type == VAL_NATI)
2745                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati | (mono_i)sp [0].data.nati;
2746                                 else {
2747                                         sp [-1].data.nati = (mono_i)sp [-1].data.i | (mono_i)sp [0].data.nati;
2748                                         sp [-1].type = VAL_NATI;
2749                                 }
2750                         }
2751                         BREAK;
2752                 CASE (CEE_XOR)
2753                         ++ip;
2754                         --sp;
2755                         if (sp->type == VAL_I32) {
2756                                 if (sp [-1].type == VAL_NATI)
2757                                         sp [-1].data.nati ^= sp [0].data.i;
2758                                 else
2759                                         sp [-1].data.i ^= sp [0].data.i;
2760                         }
2761                         else if (sp->type == VAL_I64)
2762                                 sp [-1].data.l ^= sp [0].data.l;
2763                         else {
2764                                 if (sp [-1].type == VAL_NATI)
2765                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati ^ (mono_i)sp [0].data.nati;
2766                                 else {
2767                                         sp [-1].data.nati = (mono_i)sp [-1].data.i ^ (mono_i)sp [0].data.nati;
2768                                         sp [-1].type = VAL_NATI;
2769                                 }
2770                         }
2771                         BREAK;
2772                 CASE (CEE_SHL)
2773                         ++ip;
2774                         --sp;
2775                         if (sp [-1].type == VAL_I32)
2776                                 sp [-1].data.i <<= GET_NATI (sp [0]);
2777                         else if (sp [-1].type == VAL_I64)
2778                                 sp [-1].data.l <<= GET_NATI (sp [0]);
2779                         else
2780                                 sp [-1].data.nati <<= GET_NATI (sp [0]);
2781                         BREAK;
2782                 CASE (CEE_SHR)
2783                         ++ip;
2784                         --sp;
2785                         if (sp [-1].type == VAL_I32)
2786                                 sp [-1].data.i >>= GET_NATI (sp [0]);
2787                         else if (sp [-1].type == VAL_I64)
2788                                 sp [-1].data.l >>= GET_NATI (sp [0]);
2789                         else
2790                                 sp [-1].data.nati = ((mono_i)sp [-1].data.nati) >> GET_NATI (sp [0]);
2791                         BREAK;
2792                 CASE (CEE_SHR_UN)
2793                         ++ip;
2794                         --sp;
2795                         if (sp [-1].type == VAL_I32)
2796                                 sp [-1].data.i = (guint)sp [-1].data.i >> GET_NATI (sp [0]);
2797                         else if (sp [-1].type == VAL_I64)
2798                                 sp [-1].data.l = (guint64)sp [-1].data.l >> GET_NATI (sp [0]);
2799                         else
2800                                 sp [-1].data.nati = ((mono_u)sp[-1].data.nati) >> GET_NATI (sp [0]);
2801                         BREAK;
2802                 CASE (CEE_NEG)
2803                         ++ip;
2804                         --sp;
2805                         if (sp->type == VAL_I32) 
2806                                 sp->data.i = - sp->data.i;
2807                         else if (sp->type == VAL_I64)
2808                                 sp->data.l = - sp->data.l;
2809                         else if (sp->type == VAL_DOUBLE)
2810                                 sp->data.f = - sp->data.f;
2811                         else if (sp->type == VAL_NATI)
2812                                 sp->data.nati = - (mono_i)sp->data.nati;
2813                         ++sp;
2814                         BREAK;
2815                 CASE (CEE_NOT)
2816                         ++ip;
2817                         --sp;
2818                         if (sp->type == VAL_I32)
2819                                 sp->data.i = ~ sp->data.i;
2820                         else if (sp->type == VAL_I64)
2821                                 sp->data.l = ~ sp->data.l;
2822                         else if (sp->type == VAL_NATI)
2823                                 sp->data.nati = ~ (mono_i)sp->data.p;
2824                         ++sp;
2825                         BREAK;
2826                 CASE (CEE_CONV_U1) {
2827                         ++ip;
2828                         switch (sp [-1].type) {
2829                         case VAL_DOUBLE:
2830                                 sp [-1].data.i = (guint8)sp [-1].data.f;
2831                                 break;
2832                         case VAL_I64:
2833                                 sp [-1].data.i = (guint8)sp [-1].data.l;
2834                                 break;
2835                         case VAL_VALUET:
2836                                 ves_abort();
2837                         case VAL_I32:
2838                                 sp [-1].data.i = (guint8)sp [-1].data.i;
2839                                 break;
2840                         default:
2841                                 sp [-1].data.i = (guint8)sp [-1].data.nati;
2842                                 break;
2843                         }
2844                         sp [-1].type = VAL_I32;
2845                         BREAK;
2846                 }
2847                 CASE (CEE_CONV_I1) {
2848                         ++ip;
2849                         switch (sp [-1].type) {
2850                         case VAL_DOUBLE:
2851                                 sp [-1].data.i = (gint8)sp [-1].data.f;
2852                                 break;
2853                         case VAL_I64:
2854                                 sp [-1].data.i = (gint8)sp [-1].data.l;
2855                                 break;
2856                         case VAL_VALUET:
2857                                 ves_abort();
2858                         case VAL_I32:
2859                                 sp [-1].data.i = (gint8)sp [-1].data.i;
2860                                 break;
2861                         default:
2862                                 sp [-1].data.i = (gint8)sp [-1].data.nati;
2863                                 break;
2864                         }
2865                         sp [-1].type = VAL_I32;
2866                         BREAK;
2867                 }
2868                 CASE (CEE_CONV_U2) {
2869                         ++ip;
2870                         switch (sp [-1].type) {
2871                         case VAL_DOUBLE:
2872                                 sp [-1].data.i = (guint16)sp [-1].data.f;
2873                                 break;
2874                         case VAL_I64:
2875                                 sp [-1].data.i = (guint16)sp [-1].data.l;
2876                                 break;
2877                         case VAL_VALUET:
2878                                 ves_abort();
2879                         case VAL_I32:
2880                                 sp [-1].data.i = (guint16)sp [-1].data.i;
2881                                 break;
2882                         default:
2883                                 sp [-1].data.i = (guint16)sp [-1].data.nati;
2884                                 break;
2885                         }
2886                         sp [-1].type = VAL_I32;
2887                         BREAK;
2888                 }
2889                 CASE (CEE_CONV_I2) {
2890                         ++ip;
2891                         switch (sp [-1].type) {
2892                         case VAL_DOUBLE:
2893                                 sp [-1].data.i = (gint16)sp [-1].data.f;
2894                                 break;
2895                         case VAL_I64:
2896                                 sp [-1].data.i = (gint16)sp [-1].data.l;
2897                                 break;
2898                         case VAL_VALUET:
2899                                 ves_abort();
2900                         case VAL_I32:
2901                                 sp [-1].data.i = (gint16)sp [-1].data.i;
2902                                 break;
2903                         default:
2904                                 sp [-1].data.i = (gint16)sp [-1].data.nati;
2905                                 break;
2906                         }
2907                         sp [-1].type = VAL_I32;
2908                         BREAK;
2909                 }
2910                 CASE (CEE_CONV_U4) /* Fall through */
2911 #if SIZEOF_VOID_P == 4
2912                 CASE (CEE_CONV_I) /* Fall through */
2913                 CASE (CEE_CONV_U) /* Fall through */
2914 #endif
2915                 CASE (CEE_CONV_I4) {
2916                         ++ip;
2917                         switch (sp [-1].type) {
2918                         case VAL_DOUBLE:
2919                                 sp [-1].data.i = (gint32)sp [-1].data.f;
2920                                 break;
2921                         case VAL_I64:
2922                                 sp [-1].data.i = (gint32)sp [-1].data.l;
2923                                 break;
2924                         case VAL_VALUET:
2925                                 ves_abort();
2926                         case VAL_I32:
2927                                 break;
2928                         default:
2929                                 sp [-1].data.i = (gint32)sp [-1].data.p;
2930                                 break;
2931                         }
2932                         sp [-1].type = VAL_I32;
2933                         BREAK;
2934                 }
2935 #if SIZEOF_VOID_P == 8
2936                 CASE (CEE_CONV_I) /* Fall through */
2937 #endif
2938                 CASE (CEE_CONV_I8)
2939                         ++ip;
2940                         switch (sp [-1].type) {
2941                         case VAL_DOUBLE:
2942                                 sp [-1].data.l = (gint64)sp [-1].data.f;
2943                                 break;
2944                         case VAL_I64:
2945                                 break;
2946                         case VAL_VALUET:
2947                                 ves_abort();
2948                         case VAL_I32:
2949                                 sp [-1].data.l = (gint64)sp [-1].data.i;
2950                                 break;
2951                         default:
2952                                 sp [-1].data.l = (gint64)sp [-1].data.nati;
2953                                 break;
2954                         }
2955                         sp [-1].type = ip[-1] == CEE_CONV_I ? VAL_NATI : VAL_I64;
2956                         BREAK;
2957                 CASE (CEE_CONV_R4) {
2958                         ++ip;
2959                         switch (sp [-1].type) {
2960                         case VAL_DOUBLE:
2961                                 sp [-1].data.f = (float)sp [-1].data.f;
2962                                 break;
2963                         case VAL_I64:
2964                                 sp [-1].data.f = (float)sp [-1].data.l;
2965                                 break;
2966                         case VAL_VALUET:
2967                                 ves_abort();
2968                         case VAL_I32:
2969                                 sp [-1].data.f = (float)sp [-1].data.i;
2970                                 break;
2971                         default:
2972                                 sp [-1].data.f = (float)sp [-1].data.nati;
2973                                 break;
2974                         }
2975                         sp [-1].type = VAL_DOUBLE;
2976                         BREAK;
2977                 }
2978                 CASE (CEE_CONV_R8) {
2979                         ++ip;
2980                         switch (sp [-1].type) {
2981                         case VAL_DOUBLE:
2982                                 sp [-1].data.f = (double)sp [-1].data.f;
2983                                 break;
2984                         case VAL_I64:
2985                                 sp [-1].data.f = (double)sp [-1].data.l;
2986                                 break;
2987                         case VAL_VALUET:
2988                                 ves_abort();
2989                         case VAL_I32:
2990                                 sp [-1].data.f = (double)sp [-1].data.i;
2991                                 break;
2992                         default:
2993                                 sp [-1].data.f = (double)sp [-1].data.nati;
2994                                 break;
2995                         }
2996                         sp [-1].type = VAL_DOUBLE;
2997                         BREAK;
2998                 }
2999 #if SIZEOF_VOID_P == 8
3000                 CASE (CEE_CONV_U) /* Fall through */
3001 #endif
3002                 CASE (CEE_CONV_U8)
3003                         ++ip;
3004
3005                         switch (sp [-1].type){
3006                         case VAL_DOUBLE:
3007                                 sp [-1].data.l = (guint64)sp [-1].data.f;
3008                                 break;
3009                         case VAL_I64:
3010                                 break;
3011                         case VAL_VALUET:
3012                                 ves_abort();
3013                         case VAL_I32:
3014                                 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
3015                                 break;
3016                         default:
3017                                 sp [-1].data.l = (guint64) sp [-1].data.nati;
3018                                 break;
3019                         }
3020                         sp [-1].type = ip[-1] == CEE_CONV_U ? VAL_NATI : VAL_I64;
3021                         BREAK;
3022                 CASE (CEE_CPOBJ) {
3023                         MonoClass *vtklass;
3024                         ++ip;
3025                         vtklass = mono_class_get (image, read32 (ip));
3026                         ip += 4;
3027                         sp -= 2;
3028                         memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
3029                         BREAK;
3030                 }
3031                 CASE (CEE_LDOBJ) {
3032                         guint32 token;
3033                         MonoClass *c;
3034                         char *addr;
3035
3036                         ++ip;
3037                         token = read32 (ip);
3038                         ip += 4;
3039
3040                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3041                                 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3042                         else
3043                                 c = mono_class_get (image, token);
3044
3045                         addr = sp [-1].data.vt;
3046                         vt_alloc (&c->byval_arg, &sp [-1], FALSE);
3047                         stackval_from_data (&c->byval_arg, &sp [-1], addr, FALSE);
3048                         BREAK;
3049                 }
3050                 CASE (CEE_LDSTR) {
3051                         MonoObject *o;
3052                         guint32 str_index;
3053
3054                         ip++;
3055                         str_index = mono_metadata_token_index (read32 (ip));
3056                         ip += 4;
3057
3058                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE) {
3059                                 o = (MonoObject *)mono_string_new_wrapper(
3060                                         mono_method_get_wrapper_data (frame->method, str_index));
3061                         }
3062                         else
3063                                 o = (MonoObject*)mono_ldstr (domain, image, str_index);
3064                         sp->type = VAL_OBJ;
3065                         sp->data.p = o;
3066
3067                         ++sp;
3068                         BREAK;
3069                 }
3070                 CASE (CEE_NEWOBJ) {
3071                         MonoObject *o;
3072                         MonoClass *newobj_class;
3073                         MonoMethodSignature *csig;
3074                         stackval valuetype_this;
3075                         stackval *endsp = sp;
3076                         guint32 token;
3077                         stackval retval;
3078
3079                         frame->ip = ip;
3080
3081                         ip++;
3082                         token = read32 (ip);
3083                         ip += 4;
3084
3085                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3086                                 child_frame.method = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
3087                         else 
3088                                 child_frame.method = mono_get_method (image, token, NULL);
3089                         if (!child_frame.method)
3090                                 THROW_EX (mono_get_exception_missing_method (), ip -5);
3091
3092                         csig = child_frame.method->signature;
3093                         newobj_class = child_frame.method->klass;
3094                         /*if (profiling_classes) {
3095                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
3096                                 count++;
3097                                 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
3098                         }*/
3099                                 
3100
3101                         if (newobj_class->parent == mono_defaults.array_class) {
3102                                 sp -= csig->param_count;
3103                                 o = ves_array_create (domain, newobj_class, csig, sp);
3104                                 goto array_constructed;
3105                         }
3106
3107                         /*
3108                          * First arg is the object.
3109                          */
3110                         if (newobj_class->valuetype) {
3111                                 void *zero;
3112                                 vt_alloc (&newobj_class->byval_arg, &valuetype_this, csig->pinvoke);
3113                                 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
3114                                         zero = valuetype_this.data.vt;
3115                                         child_frame.obj = valuetype_this.data.vt;
3116                                 } else {
3117                                         memset (&valuetype_this, 0, sizeof (stackval));
3118                                         zero = &valuetype_this;
3119                                         child_frame.obj = &valuetype_this;
3120                                 }
3121                                 stackval_from_data (&newobj_class->byval_arg, &valuetype_this, zero, csig->pinvoke);
3122                         } else {
3123                                 if (newobj_class != mono_defaults.string_class) {
3124                                         o = mono_object_new (domain, newobj_class);
3125                                         child_frame.obj = o;
3126                                 } else {
3127                                         child_frame.retval = &retval;
3128                                 }
3129                         }
3130
3131                         if (csig->param_count) {
3132                                 sp -= csig->param_count;
3133                                 child_frame.stack_args = sp;
3134                         } else {
3135                                 child_frame.stack_args = NULL;
3136                         }
3137
3138                         g_assert (csig->call_convention == MONO_CALL_DEFAULT);
3139
3140                         child_frame.ip = NULL;
3141                         child_frame.ex = NULL;
3142                         child_frame.ex_handler = NULL;
3143
3144                         ves_exec_method_with_context (&child_frame, context);
3145
3146                         context->current_frame = frame;
3147
3148                         while (endsp > sp) {
3149                                 --endsp;
3150                                 vt_free (endsp);
3151                         }
3152
3153                         if (child_frame.ex) {
3154                                 /*
3155                                  * An exception occurred, need to run finally, fault and catch handlers..
3156                                  */
3157                                 frame->ex = child_frame.ex;
3158                                 goto handle_finally;
3159                         }
3160                         /*
3161                          * a constructor returns void, but we need to return the object we created
3162                          */
3163 array_constructed:
3164                         if (newobj_class->valuetype && !newobj_class->enumtype) {
3165                                 *sp = valuetype_this;
3166                         } else if (newobj_class == mono_defaults.string_class) {
3167                                 *sp = retval;
3168                         } else {
3169                                 sp->type = VAL_OBJ;
3170                                 sp->data.p = o;
3171                         }
3172                         ++sp;
3173                         BREAK;
3174                 }
3175                 CASE (CEE_CASTCLASS) /* Fall through */
3176                 CASE (CEE_ISINST) {
3177                         MonoObject *o;
3178                         MonoClass *c;
3179                         guint32 token;
3180                         int do_isinst = *ip == CEE_ISINST;
3181
3182                         ++ip;
3183                         token = read32 (ip);
3184                         c = mono_class_get (image, token);
3185
3186                         g_assert (sp [-1].type == VAL_OBJ);
3187
3188                         if ((o = sp [-1].data.p)) {
3189                                 if (!mono_object_isinst (o, c)) {
3190                                         if (do_isinst) {
3191                                                 sp [-1].data.p = NULL;
3192                                         } else
3193                                                 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
3194                                 }
3195                         }
3196                         ip += 4;
3197                         BREAK;
3198                 }
3199                 CASE (CEE_CONV_R_UN)
3200                         ++ip;
3201                         switch (sp [-1].type) {
3202                         case VAL_DOUBLE:
3203                                 break;
3204                         case VAL_I64:
3205                                 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
3206                                 break;
3207                         case VAL_VALUET:
3208                                 ves_abort();
3209                         case VAL_I32:
3210                                 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
3211                                 break;
3212                         default:
3213                                 sp [-1].data.f = (double)(guint64)sp [-1].data.nati;
3214                                 break;
3215                         }
3216                         sp [-1].type = VAL_DOUBLE;
3217                         BREAK;
3218                 CASE (CEE_UNUSED58)
3219                 CASE (CEE_UNUSED1) ves_abort(); BREAK;
3220                 CASE (CEE_UNBOX) {
3221                         MonoObject *o;
3222                         MonoClass *c;
3223                         guint32 token;
3224
3225                         ++ip;
3226                         token = read32 (ip);
3227                         
3228                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3229                                 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3230                         else 
3231                                 c = mono_class_get (image, token);
3232                         
3233                         o = sp [-1].data.p;
3234                         if (!o)
3235                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3236
3237                         if (!(mono_object_isinst (o, c) || 
3238                                   ((o->vtable->klass->rank == 0) && 
3239                                    (o->vtable->klass->element_class == c->element_class))))
3240                                 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
3241
3242                         sp [-1].type = VAL_MP;
3243                         sp [-1].data.p = (char *)o + sizeof (MonoObject);
3244
3245                         ip += 4;
3246                         BREAK;
3247                 }
3248                 CASE (CEE_THROW)
3249                         --sp;
3250                         frame->ex_handler = NULL;
3251                         if (!sp->data.p)
3252                                 sp->data.p = mono_get_exception_null_reference ();
3253                         THROW_EX ((MonoException *)sp->data.p, ip);
3254                         BREAK;
3255                 CASE (CEE_LDFLDA) /* Fall through */
3256                 CASE (CEE_LDFLD) {
3257                         MonoObject *obj;
3258                         MonoClassField *field;
3259                         guint32 token;
3260                         int load_addr = *ip == CEE_LDFLDA;
3261                         char *addr;
3262
3263                         if (!sp [-1].data.p)
3264                                 THROW_EX (mono_get_exception_null_reference (), ip);
3265                         
3266                         ++ip;
3267                         token = read32 (ip);
3268                         field = rtd->field_info[token].field;
3269                         ip += 4;
3270
3271                         if (sp [-1].type == VAL_OBJ) {
3272                                 obj = sp [-1].data.p;
3273                                 if (obj->vtable->klass == mono_defaults.transparent_proxy_class && field->parent->marshalbyref) {
3274                                         MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
3275                                         addr = mono_load_remote_field (obj, klass, field, NULL);
3276                                 } else {
3277                                         addr = (char*)obj + field->offset;
3278                                 }                               
3279                         } else {
3280                                 obj = sp [-1].data.vt;
3281                                 addr = (char*)obj + field->offset - sizeof (MonoObject);
3282                         }
3283
3284                         if (load_addr) {
3285                                 sp [-1].type = VAL_MP;
3286                                 sp [-1].data.p = addr;
3287                         } else {
3288                                 vt_alloc (field->type, &sp [-1], FALSE);
3289                                 stackval_from_data (field->type, &sp [-1], addr, FALSE);                                
3290                         }
3291                         BREAK;
3292                 }
3293                 CASE (CEE_STFLD) {
3294                         MonoObject *obj;
3295                         MonoClassField *field;
3296                         guint32 token, offset;
3297
3298                         sp -= 2;
3299                         
3300                         if (!sp [0].data.p)
3301                                 THROW_EX (mono_get_exception_null_reference (), ip);
3302                         
3303                         ++ip;
3304                         token = read32 (ip);
3305                         field = rtd->field_info[token].field;
3306                         ip += 4;
3307                         
3308                         if (sp [0].type == VAL_OBJ) {
3309                                 obj = sp [0].data.p;
3310
3311                                 if (obj->vtable->klass == mono_defaults.transparent_proxy_class && field->parent->marshalbyref) {
3312                                         MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
3313                                         mono_store_remote_field (obj, klass, field, &sp [1].data);
3314                                 } else {
3315                                         offset = field->offset;
3316                                         stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
3317                                         vt_free (&sp [1]);
3318                                 }
3319                         } else {
3320                                 obj = sp [0].data.vt;
3321                                 offset = field->offset - sizeof (MonoObject);
3322                                 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
3323                                 vt_free (&sp [1]);
3324                         }
3325
3326                         BREAK;
3327                 }
3328                 CASE (CEE_LDSFLD) /* Fall through */
3329                 CASE (CEE_LDSFLDA) {
3330                         MonoVTable *vt;
3331                         MonoClassField *field;
3332                         guint32 token;
3333                         int load_addr = *ip == CEE_LDSFLDA;
3334                         gpointer addr;
3335
3336                         ++ip;
3337                         token = read32 (ip);
3338                         field = rtd->field_info[token].field;
3339                         ip += 4;
3340                                                 
3341                         vt = mono_class_vtable (domain, field->parent);
3342                         if (!vt->initialized)
3343                                 mono_runtime_class_init (vt);
3344                         
3345                         if (domain->special_static_fields && (addr = g_hash_table_lookup (domain->special_static_fields, field)))
3346                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3347                         else
3348                                 addr = (char*)(vt->data) + field->offset;
3349
3350                         if (load_addr) {
3351                                 sp->type = VAL_MP;
3352                                 sp->data.p = addr;
3353                         } else {
3354                                 vt_alloc (field->type, sp, FALSE);
3355                                 stackval_from_data (field->type, sp, addr, FALSE);
3356                         }
3357                         ++sp;
3358                         BREAK;
3359                 }
3360                 CASE (CEE_STSFLD) {
3361                         MonoVTable *vt;
3362                         MonoClassField *field;
3363                         guint32 token;
3364                         gpointer addr;
3365
3366                         ++ip;
3367                         token = read32 (ip);
3368                         field = rtd->field_info[token].field;
3369                         ip += 4;
3370                         --sp;
3371
3372                         vt = mono_class_vtable (domain, field->parent);
3373                         if (!vt->initialized)
3374                                 mono_runtime_class_init (vt);
3375                         
3376                         if (domain->special_static_fields && (addr = g_hash_table_lookup (domain->special_static_fields, field)))
3377                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3378                         else
3379                                 addr = (char*)(vt->data) + field->offset;
3380
3381                         stackval_to_data (field->type, sp, addr, FALSE);
3382                         vt_free (sp);
3383                         BREAK;
3384                 }
3385                 CASE (CEE_STOBJ) {
3386                         MonoClass *vtklass;
3387                         ++ip;
3388                         vtklass = mono_class_get (image, read32 (ip));
3389                         ip += 4;
3390                         sp -= 2;
3391
3392                         /*
3393                          * LAMESPEC: According to the spec, the stack should contain a 
3394                          * pointer to a value type. In reality, it can contain anything.
3395                          */
3396                         if (sp [1].type == VAL_VALUET)
3397                                 memcpy (sp [0].data.p, sp [1].data.vt, mono_class_value_size (vtklass, NULL));
3398                         else
3399                                 memcpy (sp [0].data.p, &sp [1].data, mono_class_value_size (vtklass, NULL));
3400                         BREAK;
3401                 }
3402 #if SIZEOF_VOID_P == 8
3403                 CASE (CEE_CONV_OVF_I_UN)
3404 #endif
3405                 CASE (CEE_CONV_OVF_I8_UN) {
3406                         switch (sp [-1].type) {
3407                         case VAL_DOUBLE:
3408                                 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3409                                         THROW_EX (mono_get_exception_overflow (), ip);
3410                                 sp [-1].data.l = (guint64)sp [-1].data.f;
3411                                 break;
3412                         case VAL_I64:
3413                                 break;
3414                         case VAL_VALUET:
3415                                 ves_abort();
3416                         case VAL_I32:
3417                                 /* Can't overflow */
3418                                 sp [-1].data.l = (guint64)sp [-1].data.i;
3419                                 break;
3420                         default:
3421                                 sp [-1].data.l = (guint64)sp [-1].data.nati;
3422                                 break;
3423                         }
3424                         sp [-1].type = VAL_I64;
3425                         ++ip;
3426                         BREAK;
3427                 }
3428 #if SIZEOF_VOID_P == 8
3429                 CASE (CEE_CONV_OVF_U_UN) 
3430 #endif
3431                 CASE (CEE_CONV_OVF_U8_UN) {
3432                         switch (sp [-1].type) {
3433                         case VAL_DOUBLE:
3434                                 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT64_MAX)
3435                                         THROW_EX (mono_get_exception_overflow (), ip);
3436                                 sp [-1].data.l = (guint64)sp [-1].data.f;
3437                                 break;
3438                         case VAL_I64:
3439                                 /* nothing to do */
3440                                 break;
3441                         case VAL_VALUET:
3442                                 ves_abort();
3443                         case VAL_I32:
3444                                 /* Can't overflow */
3445                                 sp [-1].data.l = (guint64)sp [-1].data.i;
3446                                 break;
3447                         default:
3448                                 /* Can't overflow */
3449                                 sp [-1].data.l = (guint64)sp [-1].data.nati;
3450                                 break;
3451                         }
3452                         sp [-1].type = VAL_I64;
3453                         ++ip;
3454                         BREAK;
3455                 }
3456 #if SIZEOF_VOID_P == 4
3457                 CASE (CEE_CONV_OVF_I_UN)
3458                 CASE (CEE_CONV_OVF_U_UN) 
3459 #endif
3460                 CASE (CEE_CONV_OVF_I1_UN)
3461                 CASE (CEE_CONV_OVF_I2_UN)
3462                 CASE (CEE_CONV_OVF_I4_UN)
3463                 CASE (CEE_CONV_OVF_U1_UN)
3464                 CASE (CEE_CONV_OVF_U2_UN)
3465                 CASE (CEE_CONV_OVF_U4_UN) {
3466                         guint64 value;
3467                         switch (sp [-1].type) {
3468                         case VAL_DOUBLE:
3469                                 if (sp [-1].data.f <= -1.0)
3470                                         THROW_EX (mono_get_exception_overflow (), ip);
3471                                 value = (guint64)sp [-1].data.f;
3472                                 break;
3473                         case VAL_I64:
3474                                 value = (guint64)sp [-1].data.l;
3475                                 break;
3476                         case VAL_VALUET:
3477                                 ves_abort();
3478                         case VAL_I32:
3479                                 value = (guint64)sp [-1].data.i;
3480                                 break;
3481                         default:
3482                                 value = (guint64)sp [-1].data.nati;
3483                                 break;
3484                         }
3485                         switch (*ip) {
3486                         case CEE_CONV_OVF_I1_UN:
3487                                 if (value > 127)
3488                                         THROW_EX (mono_get_exception_overflow (), ip);
3489                                 sp [-1].data.i = value;
3490                                 sp [-1].type = VAL_I32;
3491                                 break;
3492                         case CEE_CONV_OVF_I2_UN:
3493                                 if (value > 32767)
3494                                         THROW_EX (mono_get_exception_overflow (), ip);
3495                                 sp [-1].data.i = value;
3496                                 sp [-1].type = VAL_I32;
3497                                 break;
3498 #if SIZEOF_VOID_P == 4
3499                         case CEE_CONV_OVF_I_UN: /* Fall through */
3500 #endif
3501                         case CEE_CONV_OVF_I4_UN:
3502                                 if (value > MYGUINT32_MAX)
3503                                         THROW_EX (mono_get_exception_overflow (), ip);
3504                                 sp [-1].data.i = value;
3505                                 sp [-1].type = VAL_I32;
3506                                 break;
3507                         case CEE_CONV_OVF_U1_UN:
3508                                 if (value > 255)
3509                                         THROW_EX (mono_get_exception_overflow (), ip);
3510                                 sp [-1].data.i = value;
3511                                 sp [-1].type = VAL_I32;
3512                                 break;
3513                         case CEE_CONV_OVF_U2_UN:
3514                                 if (value > 65535)
3515                                         THROW_EX (mono_get_exception_overflow (), ip);
3516                                 sp [-1].data.i = value;
3517                                 sp [-1].type = VAL_I32;
3518                                 break;
3519 #if SIZEOF_VOID_P == 4
3520                         case CEE_CONV_OVF_U_UN: /* Fall through */
3521 #endif
3522                         case CEE_CONV_OVF_U4_UN:
3523                                 if (value > 4294967295U)
3524                                         THROW_EX (mono_get_exception_overflow (), ip);
3525                                 sp [-1].data.i = value;
3526                                 sp [-1].type = VAL_I32;
3527                                 break;
3528                         default:
3529                                 g_assert_not_reached ();
3530                         }
3531                         ++ip;
3532                         BREAK;
3533                 }
3534                 CASE (CEE_BOX) {
3535                         guint32 token;
3536                         MonoClass *class;
3537
3538                         ip++;
3539                         token = read32 (ip);
3540
3541                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3542                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3543                         else
3544                                 class = mono_class_get (image, token);
3545                         g_assert (class != NULL);
3546
3547                         sp [-1].type = VAL_OBJ;
3548                         if (class->byval_arg.type == MONO_TYPE_VALUETYPE && !class->enumtype) 
3549                                 sp [-1].data.p = mono_value_box (domain, class, sp [-1].data.p);
3550                         else {
3551                                 stackval_to_data (&class->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3552                                 sp [-1].data.p = mono_value_box (domain, class, &sp [-1]);
3553                         }
3554                         /* need to vt_free (sp); */
3555
3556                         ip += 4;
3557
3558                         BREAK;
3559                 }
3560                 CASE (CEE_NEWARR) {
3561                         MonoClass *class;
3562                         MonoObject *o;
3563                         guint32 token;
3564
3565                         ip++;
3566                         token = read32 (ip);
3567
3568                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3569                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3570                         else
3571                                 class = mono_class_get (image, token);
3572
3573                         o = (MonoObject*) mono_array_new (domain, class, sp [-1].data.i);
3574                         ip += 4;
3575
3576                         sp [-1].type = VAL_OBJ;
3577                         sp [-1].data.p = o;
3578                         /*if (profiling_classes) {
3579                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3580                                 count++;
3581                                 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3582                         }*/
3583
3584                         BREAK;
3585                 }
3586                 CASE (CEE_LDLEN) {
3587                         MonoArray *o;
3588
3589                         ip++;
3590
3591                         g_assert (sp [-1].type == VAL_OBJ);
3592
3593                         o = sp [-1].data.p;
3594                         if (!o)
3595                                 THROW_EX (mono_get_exception_null_reference (), ip - 1);
3596                         
3597                         g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3598
3599                         sp [-1].type = VAL_I32;
3600                         sp [-1].data.i = mono_array_length (o);
3601
3602                         BREAK;
3603                 }
3604                 CASE (CEE_LDELEMA) {
3605                         MonoArray *o;
3606                         guint32 esize, token;
3607                         mono_u aindex;
3608                         
3609                         ++ip;
3610                         token = read32 (ip);
3611                         ip += 4;
3612                         sp -= 2;
3613
3614                         g_assert (sp [0].type == VAL_OBJ);
3615                         o = sp [0].data.p;
3616
3617                         g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3618                         
3619                         aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3620                         if (aindex >= mono_array_length (o))
3621                                 THROW_EX (mono_get_exception_index_out_of_range (), ip - 5);
3622
3623                         /* check the array element corresponds to token */
3624                         esize = mono_array_element_size (o->obj.vtable->klass);
3625                         
3626                         sp->type = VAL_MP;
3627                         sp->data.p = mono_array_addr_with_size (o, esize, aindex);
3628
3629                         ++sp;
3630                         BREAK;
3631                 }
3632                 CASE (CEE_LDELEM_I1) /* fall through */
3633                 CASE (CEE_LDELEM_U1) /* fall through */
3634                 CASE (CEE_LDELEM_I2) /* fall through */
3635                 CASE (CEE_LDELEM_U2) /* fall through */
3636                 CASE (CEE_LDELEM_I4) /* fall through */
3637                 CASE (CEE_LDELEM_U4) /* fall through */
3638                 CASE (CEE_LDELEM_I8)  /* fall through */
3639                 CASE (CEE_LDELEM_I)  /* fall through */
3640                 CASE (CEE_LDELEM_R4) /* fall through */
3641                 CASE (CEE_LDELEM_R8) /* fall through */
3642                 CASE (CEE_LDELEM_REF) {
3643                         MonoArray *o;
3644                         mono_u aindex;
3645
3646                         sp -= 2;
3647
3648                         g_assert (sp [0].type == VAL_OBJ);
3649                         o = sp [0].data.p;
3650                         if (!o)
3651                                 THROW_EX (mono_get_exception_null_reference (), ip);
3652
3653                         g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3654                         
3655                         aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3656                         if (aindex >= mono_array_length (o))
3657                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3658
3659                         /*
3660                          * FIXME: throw mono_get_exception_array_type_mismatch () if needed 
3661                          */
3662                         switch (*ip) {
3663                         case CEE_LDELEM_I1:
3664                                 sp [0].data.i = mono_array_get (o, gint8, aindex);
3665                                 sp [0].type = VAL_I32;
3666                                 break;
3667                         case CEE_LDELEM_U1:
3668                                 sp [0].data.i = mono_array_get (o, guint8, aindex);
3669                                 sp [0].type = VAL_I32;
3670                                 break;
3671                         case CEE_LDELEM_I2:
3672                                 sp [0].data.i = mono_array_get (o, gint16, aindex);
3673                                 sp [0].type = VAL_I32;
3674                                 break;
3675                         case CEE_LDELEM_U2:
3676                                 sp [0].data.i = mono_array_get (o, guint16, aindex);
3677                                 sp [0].type = VAL_I32;
3678                                 break;
3679                         case CEE_LDELEM_I:
3680                                 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3681                                 sp [0].type = VAL_NATI;
3682                                 break;
3683                         case CEE_LDELEM_I4:
3684                                 sp [0].data.i = mono_array_get (o, gint32, aindex);
3685                                 sp [0].type = VAL_I32;
3686                                 break;
3687                         case CEE_LDELEM_U4:
3688                                 sp [0].data.i = mono_array_get (o, guint32, aindex);
3689                                 sp [0].type = VAL_I32;
3690                                 break;
3691                         case CEE_LDELEM_I8:
3692                                 sp [0].data.l = mono_array_get (o, guint64, aindex);
3693                                 sp [0].type = VAL_I64;
3694                                 break;
3695                         case CEE_LDELEM_R4:
3696                                 sp [0].data.f = mono_array_get (o, float, aindex);
3697                                 sp [0].type = VAL_DOUBLE;
3698                                 break;
3699                         case CEE_LDELEM_R8:
3700                                 sp [0].data.f = mono_array_get (o, double, aindex);
3701                                 sp [0].type = VAL_DOUBLE;
3702                                 break;
3703                         case CEE_LDELEM_REF:
3704                                 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3705                                 sp [0].type = VAL_OBJ;
3706                                 break;
3707                         default:
3708                                 ves_abort();
3709                         }
3710
3711                         ++ip;
3712                         ++sp;
3713                         BREAK;
3714                 }
3715                 CASE (CEE_STELEM_I)  /* fall through */
3716                 CASE (CEE_STELEM_I1) /* fall through */ 
3717                 CASE (CEE_STELEM_I2) /* fall through */
3718                 CASE (CEE_STELEM_I4) /* fall through */
3719                 CASE (CEE_STELEM_I8) /* fall through */
3720                 CASE (CEE_STELEM_R4) /* fall through */
3721                 CASE (CEE_STELEM_R8) /* fall through */
3722                 CASE (CEE_STELEM_REF) {
3723                         MonoArray *o;
3724                         MonoClass *ac;
3725                         mono_u aindex;
3726
3727                         sp -= 3;
3728
3729                         g_assert (sp [0].type == VAL_OBJ);
3730                         o = sp [0].data.p;
3731                         if (!o)
3732                                 THROW_EX (mono_get_exception_null_reference (), ip);
3733
3734                         ac = o->obj.vtable->klass;
3735                         g_assert (MONO_CLASS_IS_ARRAY (ac));
3736                     
3737                         aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3738                         if (aindex >= mono_array_length (o))
3739                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3740
3741                         /*
3742                          * FIXME: throw mono_get_exception_array_type_mismatch () if needed 
3743                          */
3744                         switch (*ip) {
3745                         case CEE_STELEM_I:
3746                                 mono_array_set (o, mono_i, aindex, sp [2].data.nati);
3747                                 break;
3748                         case CEE_STELEM_I1:
3749                                 mono_array_set (o, gint8, aindex, sp [2].data.i);
3750                                 break;
3751                         case CEE_STELEM_I2:
3752                                 mono_array_set (o, gint16, aindex, sp [2].data.i);
3753                                 break;
3754                         case CEE_STELEM_I4:
3755                                 mono_array_set (o, gint32, aindex, sp [2].data.i);
3756                                 break;
3757                         case CEE_STELEM_I8:
3758                                 mono_array_set (o, gint64, aindex, sp [2].data.l);
3759                                 break;
3760                         case CEE_STELEM_R4:
3761                                 mono_array_set (o, float, aindex, sp [2].data.f);
3762                                 break;
3763                         case CEE_STELEM_R8:
3764                                 mono_array_set (o, double, aindex, sp [2].data.f);
3765                                 break;
3766                         case CEE_STELEM_REF:
3767                                 g_assert (sp [2].type == VAL_OBJ);
3768                                 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3769                                         THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3770                                 mono_array_set (o, gpointer, aindex, sp [2].data.p);
3771                                 break;
3772                         default:
3773                                 ves_abort();
3774                         }
3775
3776                         ++ip;
3777                         BREAK;
3778                 }
3779                 CASE (CEE_LDELEM) 
3780                 CASE (CEE_STELEM) 
3781                 CASE (CEE_UNBOX_ANY) 
3782                 CASE (CEE_UNUSED5) 
3783                 CASE (CEE_UNUSED6) 
3784                 CASE (CEE_UNUSED7) 
3785                 CASE (CEE_UNUSED8) 
3786                 CASE (CEE_UNUSED9) 
3787                 CASE (CEE_UNUSED10) 
3788                 CASE (CEE_UNUSED11) 
3789                 CASE (CEE_UNUSED12) 
3790                 CASE (CEE_UNUSED13) 
3791                 CASE (CEE_UNUSED14) 
3792                 CASE (CEE_UNUSED15) 
3793                 CASE (CEE_UNUSED16) 
3794                 CASE (CEE_UNUSED17) ves_abort(); BREAK;
3795
3796 #if SIZEOF_VOID_P == 4
3797                 CASE (CEE_CONV_OVF_I)
3798                 CASE (CEE_CONV_OVF_U) 
3799 #endif
3800                 CASE (CEE_CONV_OVF_I1)
3801                 CASE (CEE_CONV_OVF_I2)
3802                 CASE (CEE_CONV_OVF_I4)
3803                 CASE (CEE_CONV_OVF_U1)
3804                 CASE (CEE_CONV_OVF_U2)
3805                 CASE (CEE_CONV_OVF_U4) {
3806                         gint64 value;
3807                         switch (sp [-1].type) {
3808                         case VAL_DOUBLE:
3809                                 value = (gint64)sp [-1].data.f;
3810                                 break;
3811                         case VAL_I64:
3812                                 value = (gint64)sp [-1].data.l;
3813                                 break;
3814                         case VAL_VALUET:
3815                                 ves_abort();
3816                         case VAL_I32:
3817                                 value = (gint64)sp [-1].data.i;
3818                                 break;
3819                         default:
3820                                 value = (gint64)sp [-1].data.nati;
3821                                 break;
3822                         }
3823                         switch (*ip) {
3824                         case CEE_CONV_OVF_I1:
3825                                 if (value < -128 || value > 127)
3826                                         THROW_EX (mono_get_exception_overflow (), ip);
3827                                 sp [-1].data.i = value;
3828                                 sp [-1].type = VAL_I32;
3829                                 break;
3830                         case CEE_CONV_OVF_I2:
3831                                 if (value < -32768 || value > 32767)
3832                                         THROW_EX (mono_get_exception_overflow (), ip);
3833                                 sp [-1].data.i = value;
3834                                 sp [-1].type = VAL_I32;
3835                                 break;
3836 #if SIZEOF_VOID_P == 4
3837                         case CEE_CONV_OVF_I: /* Fall through */
3838 #endif
3839                         case CEE_CONV_OVF_I4:
3840                                 if (value < MYGINT32_MIN || value > MYGINT32_MAX)
3841                                         THROW_EX (mono_get_exception_overflow (), ip);
3842                                 sp [-1].data.i = value;
3843                                 sp [-1].type = VAL_I32;
3844                                 break;
3845                         case CEE_CONV_OVF_U1:
3846                                 if (value < 0 || value > 255)
3847                                         THROW_EX (mono_get_exception_overflow (), ip);
3848                                 sp [-1].data.i = value;
3849                                 sp [-1].type = VAL_I32;
3850                                 break;
3851                         case CEE_CONV_OVF_U2:
3852                                 if (value < 0 || value > 65535)
3853                                         THROW_EX (mono_get_exception_overflow (), ip);
3854                                 sp [-1].data.i = value;
3855                                 sp [-1].type = VAL_I32;
3856                                 break;
3857 #if SIZEOF_VOID_P == 4
3858                         case CEE_CONV_OVF_U: /* Fall through */
3859 #endif
3860                         case CEE_CONV_OVF_U4:
3861                                 if (value < 0 || value > MYGUINT32_MAX)
3862                                         THROW_EX (mono_get_exception_overflow (), ip);
3863                                 sp [-1].data.i = value;
3864                                 sp [-1].type = VAL_I32;
3865                                 break;
3866                         default:
3867                                 g_assert_not_reached ();
3868                         }
3869                         ++ip;
3870                         BREAK;
3871                 }
3872
3873 #if SIZEOF_VOID_P == 8
3874                 CASE (CEE_CONV_OVF_I)
3875 #endif
3876                 CASE (CEE_CONV_OVF_I8)
3877                         /* FIXME: handle other cases */
3878                         if (sp [-1].type == VAL_I32) {
3879                                 sp [-1].data.l = (guint64)sp [-1].data.i;
3880                                 sp [-1].type = VAL_I64;
3881                         } else if(sp [-1].type == VAL_I64) {
3882                                 /* defined as NOP */
3883                         } else {
3884                                 ves_abort();
3885                         }
3886                         ++ip;
3887                         BREAK;
3888
3889 #if SIZEOF_VOID_P == 8
3890                 CASE (CEE_CONV_OVF_U)
3891 #endif
3892                 CASE (CEE_CONV_OVF_U8)
3893                         /* FIXME: handle other cases */
3894                         if (sp [-1].type == VAL_I32) {
3895                                 sp [-1].data.l = (guint64) sp [-1].data.i;
3896                                 sp [-1].type = VAL_I64;
3897                         } else if(sp [-1].type == VAL_I64) {
3898                                 /* defined as NOP */
3899                         } else {
3900                                 ves_abort();
3901                         }
3902                         ++ip;
3903                         BREAK;
3904                 CASE (CEE_UNUSED50) 
3905                 CASE (CEE_UNUSED18) 
3906                 CASE (CEE_UNUSED19) 
3907                 CASE (CEE_UNUSED20) 
3908                 CASE (CEE_UNUSED21) 
3909                 CASE (CEE_UNUSED22) 
3910                 CASE (CEE_UNUSED23) ves_abort(); BREAK;
3911                 CASE (CEE_REFANYVAL) ves_abort(); BREAK;
3912                 CASE (CEE_CKFINITE)
3913                         if (!finite(sp [-1].data.f))
3914                                 THROW_EX (mono_get_exception_arithmetic (), ip);
3915                         ++ip;
3916                         BREAK;
3917                 CASE (CEE_UNUSED24) ves_abort(); BREAK;
3918                 CASE (CEE_UNUSED25) ves_abort(); BREAK;
3919                 CASE (CEE_MKREFANY) ves_abort(); BREAK;
3920                 CASE (CEE_UNUSED59) 
3921                 CASE (CEE_UNUSED60) 
3922                 CASE (CEE_UNUSED61) 
3923                 CASE (CEE_UNUSED62) 
3924                 CASE (CEE_UNUSED63) 
3925                 CASE (CEE_UNUSED64) 
3926                 CASE (CEE_UNUSED65) 
3927                 CASE (CEE_UNUSED66) 
3928                 CASE (CEE_UNUSED67) ves_abort(); BREAK;
3929                 CASE (CEE_LDTOKEN) {
3930                         gpointer handle;
3931                         MonoClass *handle_class;
3932                         ++ip;
3933                         handle = mono_ldtoken (image, read32 (ip), &handle_class);
3934                         ip += 4;
3935                         vt_alloc (&handle_class->byval_arg, sp, FALSE);
3936                         stackval_from_data (&handle_class->byval_arg, sp, (char*)&handle, FALSE);
3937                         ++sp;
3938                         BREAK;
3939                 }
3940                 CASE (CEE_ADD_OVF)
3941                         --sp;
3942                         /* FIXME: check overflow */
3943                         if (sp->type == VAL_I32) {
3944                                 if (sp [-1].type == VAL_I32) {
3945                                         if (CHECK_ADD_OVERFLOW (sp [-1].data.i, sp [0].data.i))
3946                                                 THROW_EX (mono_get_exception_overflow (), ip);
3947                                         sp [-1].data.i = (gint32)sp [-1].data.i + (gint32)sp [0].data.i;
3948                                 } else {
3949                                         if (CHECK_ADD_OVERFLOW_NAT (sp [-1].data.nati, (mono_i)sp [0].data.i))
3950                                                 THROW_EX (mono_get_exception_overflow (), ip);
3951                                         sp [-1].data.nati = sp [-1].data.nati + (mono_i)sp [0].data.i;
3952                                 }
3953                         } else if (sp->type == VAL_I64) {
3954                                 if (CHECK_ADD_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3955                                         THROW_EX (mono_get_exception_overflow (), ip);
3956                                 sp [-1].data.l = (gint64)sp [-1].data.l + (gint64)sp [0].data.l;
3957                         } else if (sp->type == VAL_DOUBLE)
3958                                 sp [-1].data.f += sp [0].data.f;
3959                         else {
3960                                 char *p = sp [-1].data.p;
3961                                 p += GET_NATI (sp [0]);
3962                                 sp [-1].data.p = p;
3963                         }
3964                         ++ip;
3965                         BREAK;
3966                 CASE (CEE_ADD_OVF_UN)
3967                         --sp;
3968                         /* FIXME: check overflow, make unsigned */
3969                         if (sp->type == VAL_I32) {
3970                                 if (sp [-1].type == VAL_I32) {
3971                                         if (CHECK_ADD_OVERFLOW_UN (sp [-1].data.i, sp [0].data.i))
3972                                                 THROW_EX (mono_get_exception_overflow (), ip);
3973                                         sp [-1].data.i = (guint32)sp [-1].data.i + (guint32)sp [0].data.i;
3974                                 } else {
3975                                         if (CHECK_ADD_OVERFLOW_NAT_UN (sp [-1].data.nati, (mono_u)sp [0].data.i))
3976                                                 THROW_EX (mono_get_exception_overflow (), ip);
3977                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati + (mono_u)sp [0].data.i;
3978                                 }
3979                         } else if (sp->type == VAL_I64) {
3980                                 if (CHECK_ADD_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3981                                         THROW_EX (mono_get_exception_overflow (), ip);
3982                                 sp [-1].data.l = (guint64)sp [-1].data.l + (guint64)sp [0].data.l;
3983                         } else if (sp->type == VAL_DOUBLE)
3984                                 sp [-1].data.f += sp [0].data.f;
3985                         else {
3986                                 char *p = sp [-1].data.p;
3987                                 p += GET_NATI (sp [0]);
3988                                 sp [-1].data.p = p;
3989                         }
3990                         ++ip;
3991                         BREAK;
3992                 CASE (CEE_MUL_OVF)
3993                         --sp;
3994                         /* FIXME: check overflow */
3995                         if (sp->type == VAL_I32) {
3996                                 if (sp [-1].type == VAL_NATI) {
3997                                         if (CHECK_MUL_OVERFLOW_NAT (sp [-1].data.nati, sp [0].data.i))
3998                                                 THROW_EX (mono_get_exception_overflow (), ip);
3999                                         sp [-1].data.nati = sp [-1].data.nati * sp [0].data.i;
4000                                         sp [-1].type = VAL_NATI;
4001                                 } else {
4002                                         if (CHECK_MUL_OVERFLOW (sp [-1].data.i, sp [0].data.i))
4003                                                 THROW_EX (mono_get_exception_overflow (), ip);
4004                                         sp [-1].data.i *= sp [0].data.i;
4005                                 }
4006                         }
4007                         else if (sp->type == VAL_I64) {
4008                                 if (CHECK_MUL_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
4009                                         THROW_EX (mono_get_exception_overflow (), ip);
4010                                 sp [-1].data.l *= sp [0].data.l;
4011                         }
4012                         else if (sp->type == VAL_DOUBLE)
4013                                 sp [-1].data.f *= sp [0].data.f;
4014                         else
4015                                 ves_abort();
4016                         ++ip;
4017                         BREAK;
4018                 CASE (CEE_MUL_OVF_UN)
4019                         --sp;
4020                         /* FIXME: check overflow, make unsigned */
4021                         if (sp->type == VAL_I32) {
4022                                 if (sp [-1].type == VAL_NATI) {
4023                                         if (CHECK_MUL_OVERFLOW_NAT_UN (sp [-1].data.nati, (mono_u)sp [0].data.i))
4024                                                 THROW_EX (mono_get_exception_overflow (), ip);
4025                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati * (mono_u)sp [0].data.i;
4026                                         sp [-1].type = VAL_NATI;
4027                                 } else {
4028                                         if (CHECK_MUL_OVERFLOW_UN ((guint32)sp [-1].data.i, (guint32)sp [0].data.i))
4029                                                 THROW_EX (mono_get_exception_overflow (), ip);
4030                                         sp [-1].data.i *= sp [0].data.i;
4031                                 }
4032                         }
4033                         else if (sp->type == VAL_I64) {
4034                                 if (CHECK_MUL_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
4035                                         THROW_EX (mono_get_exception_overflow (), ip);
4036                                 sp [-1].data.l *= sp [0].data.l;
4037                         }
4038                         else if (sp->type == VAL_DOUBLE)
4039                                 sp [-1].data.f *= sp [0].data.f;
4040                         else
4041                                 ves_abort();
4042                         ++ip;
4043                         BREAK;
4044                 CASE (CEE_SUB_OVF)
4045                         --sp;
4046                         /* FIXME: handle undeflow/unsigned */
4047                         /* should probably consider the pointers as unsigned */
4048                         if (sp->type == VAL_I32) {
4049                                 if (sp [-1].type == VAL_I32) {
4050                                         if (CHECK_SUB_OVERFLOW (sp [-1].data.i, sp [0].data.i))
4051                                                 THROW_EX (mono_get_exception_overflow (), ip);
4052                                         sp [-1].data.i -= sp [0].data.i;
4053                                 } else {
4054                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.i;
4055                                         sp [-1].type = VAL_NATI;
4056                                 }
4057                         }
4058                         else if (sp->type == VAL_I64) {
4059                                 if (CHECK_SUB_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
4060                                     THROW_EX (mono_get_exception_overflow (), ip);
4061                                 sp [-1].data.l -= sp [0].data.l;
4062                         }
4063                         else if (sp->type == VAL_DOUBLE)
4064                                 sp [-1].data.f -= sp [0].data.f;
4065                         else {
4066                                 if (sp [-1].type == VAL_I32) {
4067                                         sp [-1].data.nati = sp [-1].data.i - sp [0].data.nati;
4068                                         sp [-1].type = sp [0].type;
4069                                 } else
4070                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.nati;
4071                         }
4072                         ++ip;
4073                         BREAK;
4074                 CASE (CEE_SUB_OVF_UN)
4075                         --sp;
4076                         /* FIXME: handle undeflow/unsigned */
4077                         /* should probably consider the pointers as unsigned */
4078                         if (sp->type == VAL_I32) {
4079                                 if (sp [-1].type == VAL_I32) {
4080                                         if (CHECK_SUB_OVERFLOW_UN (sp [-1].data.i, sp [0].data.i))
4081                                                 THROW_EX (mono_get_exception_overflow (), ip);
4082                                         sp [-1].data.i -= sp [0].data.i;
4083                                 } else {
4084                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.i;
4085                                         sp [-1].type = VAL_NATI;
4086                                 }
4087                         }
4088                         else if (sp->type == VAL_I64) {
4089                                 if (CHECK_SUB_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
4090                                     THROW_EX (mono_get_exception_overflow (), ip);
4091                                 sp [-1].data.l -= sp [0].data.l;
4092                         }
4093                         else if (sp->type == VAL_DOUBLE)
4094                                 sp [-1].data.f -= sp [0].data.f;
4095                         else {
4096                                 if (sp [-1].type == VAL_I32) {
4097                                         sp [-1].data.nati = sp [-1].data.i - sp [0].data.nati;
4098                                         sp [-1].type = sp [0].type;
4099                                 } else
4100                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.nati;
4101                         }
4102                         ++ip;
4103                         BREAK;
4104                 CASE (CEE_ENDFINALLY)
4105                         if (finally_ips) {
4106                                 ip = finally_ips->data;
4107                                 finally_ips = g_slist_remove (finally_ips, ip);
4108                                 goto main_loop;
4109                         }
4110                         if (frame->ex)
4111                                 goto handle_fault;
4112                         ves_abort();
4113                         BREAK;
4114                 CASE (CEE_LEAVE) /* Fall through */
4115                 CASE (CEE_LEAVE_S)
4116                         while (sp > frame->stack) {
4117                                 --sp;
4118                                 vt_free (sp);
4119                         }
4120                         frame->ip = ip;
4121                         if (*ip == CEE_LEAVE_S) {
4122                                 ++ip;
4123                                 ip += (signed char) *ip;
4124                                 ++ip;
4125                         } else {
4126                                 ++ip;
4127                                 ip += (gint32) read32 (ip);
4128                                 ip += 4;
4129                         }
4130                         endfinally_ip = ip;
4131                         if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - header->code)) {
4132                                 frame->ex_handler = NULL;
4133                                 frame->ex = NULL;
4134                         }
4135                         goto handle_finally;
4136                         BREAK;
4137                 CASE (CEE_UNUSED41)
4138                         ++ip;
4139                         switch (*ip) {
4140                         case CEE_MONO_FUNC1: {
4141                                 MonoMarshalConv conv;
4142                                 ++ip;
4143
4144                                 conv = *ip;
4145
4146                                 ++ip;
4147
4148                                 sp--;
4149
4150                                 sp->type = VAL_NATI;
4151
4152                                 switch (conv) {
4153                                 case MONO_MARSHAL_CONV_STR_LPWSTR:
4154                                         sp->data.p = mono_string_to_utf16 (sp->data.p);
4155                                         break;
4156                                 case MONO_MARSHAL_CONV_LPSTR_STR:
4157                                         sp->data.p = mono_string_new_wrapper (sp->data.p);
4158                                         break;
4159                                 case MONO_MARSHAL_CONV_STR_LPTSTR:
4160                                 case MONO_MARSHAL_CONV_STR_LPSTR:
4161                                         sp->data.p = mono_string_to_utf8 (sp->data.p);
4162                                         break;
4163                                 case MONO_MARSHAL_CONV_STR_BSTR:
4164                                         sp->data.p = mono_string_to_bstr (sp->data.p);
4165                                         break;
4166                                 case MONO_MARSHAL_CONV_STR_TBSTR:
4167                                 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4168                                         sp->data.p = mono_string_to_ansibstr (sp->data.p);
4169                                         break;
4170                                 case MONO_MARSHAL_CONV_SB_LPSTR:
4171                                         sp->data.p = mono_string_builder_to_utf8 (sp->data.p);
4172                                         break;
4173                                 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4174                                         sp->data.p = mono_array_to_savearray (sp->data.p);
4175                                         break;
4176                                 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4177                                         sp->data.p = mono_array_to_lparray (sp->data.p);
4178                                         break;
4179                                 case MONO_MARSHAL_CONV_DEL_FTN:
4180                                         sp->data.p = mono_delegate_to_ftnptr (sp->data.p);
4181                                         break;
4182                                 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4183                                         sp->data.p = mono_marshal_string_array (sp->data.p);
4184                                         break;
4185                                 case MONO_MARSHAL_CONV_LPWSTR_STR:
4186                                         sp->data.p = mono_string_from_utf16 (sp->data.p);
4187                                         break;
4188                                 default:
4189                                         fprintf(stderr, "MONO_FUNC1 %d", conv);
4190                                         g_assert_not_reached ();
4191                                 }
4192                                 sp++; 
4193                                 break;
4194                         }
4195                         case CEE_MONO_PROC2: {
4196                                 MonoMarshalConv conv;
4197                                 ++ip;
4198                                 conv = *ip;
4199                                 ++ip;
4200
4201                                 sp -= 2;
4202
4203                                 switch (conv) {
4204                                 case MONO_MARSHAL_CONV_LPSTR_SB:
4205                                         mono_string_utf8_to_builder (sp [0].data.p, sp [1].data.p);
4206                                         break;
4207                                 case MONO_MARSHAL_FREE_ARRAY:
4208                                         mono_marshal_free_array (sp [0].data.p, sp [1].data.i);
4209                                         break;
4210                                 default:
4211                                         g_assert_not_reached ();
4212                                 }                                
4213                                 break;
4214                         }
4215                         case CEE_MONO_PROC3: {
4216                                 MonoMarshalConv conv;
4217                                 ++ip;
4218                                 conv = *ip;
4219                                 ++ip;
4220
4221                                 sp -= 3;
4222
4223                                 switch (conv) {
4224                                 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4225                                         mono_string_to_byvalstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4226                                         break;
4227                                 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4228                                         mono_string_to_byvalwstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4229                                         break;
4230                                 default:
4231                                         g_assert_not_reached ();
4232                                 }
4233                                 break;
4234                         }
4235                         case CEE_MONO_VTADDR: {
4236                                 ++ip;
4237
4238                                 sp [-1].type = VAL_MP;
4239                                 /* do nothing? */
4240                                 break;
4241                         }
4242                         case CEE_MONO_LDPTR: {
4243                                 guint32 token;
4244                                 ++ip;
4245                                 
4246                                 token = read32 (ip);
4247                                 ip += 4;
4248                                 
4249                                 sp->type = VAL_NATI;
4250                                 sp->data.p = mono_method_get_wrapper_data (frame->method, token);
4251                                 ++sp;
4252                                 break;
4253                         }
4254                         case CEE_MONO_FREE: {
4255                                 ++ip;
4256
4257                                 sp -= 1;
4258                                 g_free (sp->data.p);
4259                                 break;
4260                         }
4261                         case CEE_MONO_OBJADDR: {
4262                                 ++ip;
4263
4264                                 sp->type = VAL_MP;
4265                                 /* do nothing? */
4266                                 break;
4267                         }
4268                         case CEE_MONO_NEWOBJ: {
4269                                 MonoClass *class;
4270                                 guint32 token;
4271
4272                                 ++ip;
4273                                 token = read32 (ip);
4274                                 ip += 4;
4275
4276                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
4277                                 sp->data.p = mono_object_new (domain, class);
4278                                 sp++;
4279                                 break;
4280                         }
4281                         case CEE_MONO_RETOBJ: {
4282                                 MonoClass *class;
4283                                 guint32 token;
4284
4285                                 ++ip;
4286                                 token = read32 (ip);
4287                                 ip += 4;
4288
4289                                 sp--;
4290
4291                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
4292                                 
4293                                 stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);
4294
4295                                 if (sp > frame->stack)
4296                                         g_warning ("more values on stack: %d", sp-frame->stack);
4297                                 goto exit_frame;
4298                         }
4299                         case CEE_MONO_LDNATIVEOBJ: {
4300                                 MonoClass *class;
4301                                 guint32 token;
4302
4303                                 ++ip;
4304                                 token = read32 (ip);
4305                                 ip += 4;
4306
4307                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
4308                                 g_assert(class->valuetype);
4309
4310                                 sp [-1].type = VAL_MP;
4311
4312                                 break;
4313                         }
4314                         default:
4315                                 g_error ("Unimplemented opcode: 0xF0 %02x at 0x%x\n", *ip, ip-header->code);
4316                         }
4317                         BREAK;
4318                 CASE (CEE_UNUSED26) 
4319                 CASE (CEE_UNUSED27) 
4320                 CASE (CEE_UNUSED28) 
4321                 CASE (CEE_UNUSED29) 
4322                 CASE (CEE_UNUSED30) 
4323                 CASE (CEE_UNUSED31) 
4324                 CASE (CEE_UNUSED32) 
4325                 CASE (CEE_UNUSED33) 
4326                 CASE (CEE_UNUSED34) 
4327                 CASE (CEE_UNUSED35) 
4328                 CASE (CEE_UNUSED36) 
4329                 CASE (CEE_UNUSED37) 
4330                 CASE (CEE_UNUSED38) 
4331                 CASE (CEE_UNUSED39) 
4332                 CASE (CEE_UNUSED40) 
4333                 CASE (CEE_UNUSED42) 
4334                 CASE (CEE_UNUSED43) 
4335                 CASE (CEE_UNUSED44) 
4336                 CASE (CEE_UNUSED45) 
4337                 CASE (CEE_UNUSED46) 
4338                 CASE (CEE_UNUSED47) 
4339                 CASE (CEE_UNUSED48)
4340                 CASE (CEE_PREFIX7)
4341                 CASE (CEE_PREFIX6)
4342                 CASE (CEE_PREFIX5)
4343                 CASE (CEE_PREFIX4)
4344                 CASE (CEE_PREFIX3)
4345                 CASE (CEE_PREFIX2)
4346                 CASE (CEE_PREFIXREF) ves_abort(); BREAK;
4347                 /*
4348                  * Note: Exceptions thrown when executing a prefixed opcode need
4349                  * to take into account the number of prefix bytes (usually the
4350                  * throw point is just (ip - n_prefix_bytes).
4351                  */
4352                 SUB_SWITCH
4353                         ++ip;
4354                         switch (*ip) {
4355                         case CEE_ARGLIST: ves_abort(); break;
4356                         case CEE_CEQ: {
4357                                 gint32 result;
4358                                 ++ip;
4359                                 sp -= 2;
4360
4361                                 if (sp->type == VAL_I32)
4362                                         result = (mono_i)sp [0].data.i == (mono_i)GET_NATI (sp [1]);
4363                                 else if (sp->type == VAL_I64)
4364                                         result = sp [0].data.l == sp [1].data.l;
4365                                 else if (sp->type == VAL_DOUBLE) {
4366                                         if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
4367                                                 result = 0;
4368                                         else
4369                                                 result = sp [0].data.f == sp [1].data.f;
4370                                 } else
4371                                         result = sp [0].data.nati == GET_NATI (sp [1]);
4372                                 sp->type = VAL_I32;
4373                                 sp->data.i = result;
4374
4375                                 sp++;
4376                                 break;
4377                         }
4378                         case CEE_CGT: {
4379                                 gint32 result;
4380                                 ++ip;
4381                                 sp -= 2;
4382
4383                                 if (sp->type == VAL_I32)
4384                                         result = (mono_i)sp [0].data.i > (mono_i)GET_NATI (sp [1]);
4385                                 else if (sp->type == VAL_I64)
4386                                         result = sp [0].data.l > sp [1].data.l;
4387                                 else if (sp->type == VAL_DOUBLE) {
4388                                         if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
4389                                                 result = 0;
4390                                         else
4391                                                 result = sp [0].data.f > sp [1].data.f;
4392                                 } else
4393                                         result = (mono_i)sp [0].data.nati > (mono_i)GET_NATI (sp [1]);
4394                                 sp->type = VAL_I32;
4395                                 sp->data.i = result;
4396
4397                                 sp++;
4398                                 break;
4399                         }
4400                         case CEE_CGT_UN: {
4401                                 gint32 result;
4402                                 ++ip;
4403                                 sp -= 2;
4404
4405                                 if (sp->type == VAL_I32)
4406                                         result = (mono_u)sp [0].data.i > (mono_u)GET_NATI (sp [1]);
4407                                 else if (sp->type == VAL_I64)
4408                                         result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
4409                                 else if (sp->type == VAL_DOUBLE)
4410                                         result = isnan (sp [0].data.f) || isnan (sp [1].data.f) ||
4411                                                 sp[0].data.f > sp[1].data.f;
4412                                 else
4413                                         result = (mono_u)sp [0].data.nati > (mono_u)GET_NATI (sp [1]);
4414                                 sp->type = VAL_I32;
4415                                 sp->data.i = result;
4416
4417                                 sp++;
4418                                 break;
4419                         }
4420                         case CEE_CLT: {
4421                                 gint32 result;
4422                                 ++ip;
4423                                 sp -= 2;
4424
4425                                 if (sp->type == VAL_I32)
4426                                         result = (mono_i)sp [0].data.i < (mono_i)GET_NATI (sp [1]);
4427                                 else if (sp->type == VAL_I64)
4428                                         result = sp [0].data.l < sp [1].data.l;
4429                                 else if (sp->type == VAL_DOUBLE) {
4430                                         if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
4431                                                 result = 0;
4432                                         else
4433                                                 result = sp [0].data.f < sp [1].data.f;
4434                                 } else
4435                                         result = (mono_i)sp [0].data.nati < (mono_i)GET_NATI (sp [1]);
4436                                 sp->type = VAL_I32;
4437                                 sp->data.i = result;
4438
4439                                 sp++;
4440                                 break;
4441                         }
4442                         case CEE_CLT_UN: {
4443                                 gint32 result;
4444                                 ++ip;
4445                                 sp -= 2;
4446
4447                                 if (sp->type == VAL_I32)
4448                                         result = (mono_u)sp [0].data.i < (mono_u)GET_NATI (sp [1]);
4449                                 else if (sp->type == VAL_I64)
4450                                         result = (guint64)sp [0].data.l < (guint64)sp [1].data.l;
4451                                 else if (sp->type == VAL_DOUBLE)
4452                                         result = isnan (sp [0].data.f) || isnan (sp [1].data.f) ||
4453                                                 sp[0].data.f < sp[1].data.f;
4454                                 else
4455                                         result = (mono_u)sp [0].data.nati < (mono_u)GET_NATI (sp [1]);
4456                                 sp->type = VAL_I32;
4457                                 sp->data.i = result;
4458
4459                                 sp++;
4460                                 break;
4461                         }
4462                         case CEE_LDFTN:
4463                         case CEE_LDVIRTFTN: {
4464                                 int virtual = *ip == CEE_LDVIRTFTN;
4465                                 MonoMethod *m;
4466                                 guint32 token;
4467                                 ++ip;
4468                                 token = read32 (ip);
4469                                 ip += 4;
4470
4471                                 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
4472                                         m = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
4473                                 else 
4474                                         m = mono_get_method (image, token, NULL);
4475
4476                                 if (!m)
4477                                         THROW_EX (mono_get_exception_missing_method (), ip - 5);
4478                                 if (virtual) {
4479                                         --sp;
4480                                         if (!sp->data.p)
4481                                                 THROW_EX (mono_get_exception_null_reference (), ip - 5);
4482                                         
4483                                         m = get_virtual_method (domain, m, sp);
4484                                 }
4485
4486                                 /* 
4487                                  * This prevents infinite cycles since the wrapper contains
4488                                  * an ldftn too.
4489                                  */
4490                                 if (frame->method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
4491                                         if (m && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4492                                                 m = mono_marshal_get_synchronized_wrapper (m);
4493
4494                                 sp->type = VAL_NATI;
4495                                 sp->data.p = mono_create_method_pointer (m);
4496                                 ++sp;
4497                                 break;
4498                         }
4499                         case CEE_UNUSED56: ves_abort(); break;
4500                         case CEE_LDARG: {
4501                                 guint32 arg_pos;
4502                                 ++ip;
4503                                 arg_pos = read16 (ip);
4504                                 ip += 2;
4505                                 vt_alloc (ARG_TYPE (signature, arg_pos), sp, signature->pinvoke);
4506                                 stackval_from_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4507                                 ++sp;
4508                                 break;
4509                         }
4510                         case CEE_LDARGA: {
4511                                 guint32 anum;
4512                                 ++ip;
4513                                 anum = read16 (ip);
4514                                 ip += 2;
4515                                 sp->data.vt = ARG_POS (anum);
4516                                 sp->type = VAL_MP;
4517                                 ++sp;
4518                                 break;
4519                         }
4520                         case CEE_STARG: {
4521                                 guint32 arg_pos;
4522                                 ++ip;
4523                                 arg_pos = read16 (ip);
4524                                 ip += 2;
4525                                 --sp;
4526                                 stackval_to_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4527                                 vt_free (sp);
4528                                 break;
4529                         }
4530                         case CEE_LDLOC: {
4531                                 guint32 loc_pos;
4532                                 ++ip;
4533                                 loc_pos = read16 (ip);
4534                                 ip += 2;
4535                                 vt_alloc (LOCAL_TYPE (header, loc_pos), sp, FALSE);
4536                                 stackval_from_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4537                                 ++sp;
4538                                 break;
4539                         }
4540                         case CEE_LDLOCA: {
4541                                 MonoType *t;
4542                                 guint32 loc_pos;
4543
4544                                 ++ip;
4545                                 loc_pos = read16 (ip);
4546                                 ip += 2;
4547                                 t = LOCAL_TYPE (header, loc_pos);
4548                                 sp->data.vt = LOCAL_POS (loc_pos);
4549
4550                                 sp->type = VAL_MP;
4551
4552                                 ++sp;
4553                                 break;
4554                         }
4555                         case CEE_STLOC: {
4556                                 guint32 loc_pos;
4557                                 ++ip;
4558                                 loc_pos = read16 (ip);
4559                                 ip += 2;
4560                                 --sp;
4561                                 stackval_to_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4562                                 vt_free (sp);
4563                                 break;
4564                         }
4565                         case CEE_LOCALLOC:
4566                                 --sp;
4567                                 if (sp != frame->stack)
4568                                         THROW_EX (mono_get_exception_execution_engine (NULL), ip - 1);
4569                                 ++ip;
4570                                 sp->data.p = alloca (sp->data.i);
4571                                 sp->type = VAL_MP;
4572                                 sp++;
4573                                 break;
4574                         case CEE_UNUSED57: ves_abort(); break;
4575                         case CEE_ENDFILTER: ves_abort(); break;
4576                         case CEE_UNALIGNED_:
4577                                 ++ip;
4578                                 unaligned_address = 1;
4579                                 break;
4580                         case CEE_VOLATILE_:
4581                                 ++ip;
4582                                 volatile_address = 1;
4583                                 break;
4584                         case CEE_TAIL_:
4585                                 ++ip;
4586                                 tail_recursion = 1;
4587                                 break;
4588                         case CEE_INITOBJ: {
4589                                 guint32 token;
4590                                 MonoClass *class;
4591
4592                                 ++ip;
4593                                 token = read32 (ip);
4594                                 ip += 4;
4595
4596                                 class = mono_class_get (image, token);
4597
4598                                 --sp;
4599                                 g_assert (sp->type == VAL_TP || sp->type == VAL_MP);
4600                                 memset (sp->data.vt, 0, mono_class_value_size (class, NULL));
4601                                 break;
4602                         }
4603                         case CEE_CONSTRAINED_: {
4604                                 guint32 token;
4605                                 /* FIXME: implement */
4606                                 ++ip;
4607                                 token = read32 (ip);
4608                                 ip += 4;
4609                                 break;
4610                         }
4611                         case CEE_CPBLK:
4612                                 sp -= 3;
4613                                 if (!sp [0].data.p || !sp [1].data.p)
4614                                         THROW_EX (mono_get_exception_null_reference(), ip - 1);
4615                                 ++ip;
4616                                 /* FIXME: value and size may be int64... */
4617                                 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4618                                 break;
4619                         case CEE_INITBLK:
4620                                 sp -= 3;
4621                                 if (!sp [0].data.p)
4622                                         THROW_EX (mono_get_exception_null_reference(), ip - 1);
4623                                 ++ip;
4624                                 /* FIXME: value and size may be int64... */
4625                                 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4626                                 break;
4627                         case CEE_NO_:
4628                                 /* FIXME: implement */
4629                                 ip += 2;
4630                                 break;
4631                         case CEE_RETHROW:
4632                                 /* 
4633                                  * need to clarify what this should actually do:
4634                                  * start the search from the last found handler in
4635                                  * this method or continue in the caller or what.
4636                                  * Also, do we need to run finally/fault handlers after a retrow?
4637                                  * Well, this implementation will follow the usual search
4638                                  * for an handler, considering the current ip as throw spot.
4639                                  * We need to NULL frame->ex_handler for the later code to
4640                                  * actually run the new found handler.
4641                                  */
4642                                 frame->ex_handler = NULL;
4643                                 THROW_EX (frame->ex, ip - 1);
4644                                 break;
4645                         case CEE_UNUSED: ves_abort(); break;
4646                         case CEE_SIZEOF: {
4647                                 guint32 token;
4648                                 int align;
4649                                 ++ip;
4650                                 token = read32 (ip);
4651                                 ip += 4;
4652                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4653                                         MonoType *type = mono_type_create_from_typespec (image, token);
4654                                         sp->data.i = mono_type_size (type, &align);
4655                                 } else {
4656                                         MonoClass *szclass = mono_class_get (image, token);
4657                                         mono_class_init (szclass);
4658                                         if (!szclass->valuetype)
4659                                                 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
4660                                         sp->data.i = mono_class_value_size (szclass, &align);
4661                                 }
4662                                 sp->type = VAL_I32;
4663                                 ++sp;
4664                                 break;
4665                         }
4666                         case CEE_REFANYTYPE: ves_abort(); break;
4667                         default:
4668                                 g_error ("Unimplemented opcode: 0xFE %02x at 0x%x\n", *ip, ip-header->code);
4669                         }
4670                         continue;
4671                 DEFAULT;
4672                 }
4673         }
4674
4675         g_assert_not_reached ();
4676         /*
4677          * Exception handling code.
4678          * The exception object is stored in frame->ex.
4679          */
4680
4681         handle_exception:
4682         {
4683                 int i;
4684                 guint32 ip_offset;
4685                 MonoInvocation *inv;
4686                 MonoMethodHeader *hd;
4687                 MonoExceptionClause *clause;
4688                 /*char *message;*/
4689                 MonoObject *ex_obj;
4690
4691 #if DEBUG_INTERP
4692                 if (tracing)
4693                         g_print ("* Handling exception '%s' at IL_%04x\n", frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name, frame->ip - header->code);
4694 #endif
4695                 if (die_on_exception)
4696                         goto die_on_ex;
4697
4698                 if (frame->ex == quit_exception)
4699                         goto handle_finally;
4700                 
4701                 for (inv = frame; inv; inv = inv->parent) {
4702                         if (inv->method == NULL)
4703                                 continue;
4704                         if (inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4705                                 continue;
4706                         if (inv->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4707                                 continue;
4708                         hd = ((MonoMethodNormal*)inv->method)->header;
4709                         ip_offset = inv->ip - hd->code;
4710                         inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one  - this one wins */
4711                         for (i = 0; i < hd->num_clauses; ++i) {
4712                                 clause = &hd->clauses [i];
4713                                 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4714                                         if (!clause->flags) {
4715                                                 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (inv->method->klass->image, clause->token_or_filter))) {
4716                                                         /* 
4717                                                          * OK, we found an handler, now we need to execute the finally
4718                                                          * and fault blocks before branching to the handler code.
4719                                                          */
4720                                                         inv->ex_handler = clause;
4721 #if DEBUG_INTERP
4722                                                         if (tracing)
4723                                                                 g_print ("* Found handler at '%s'\n", inv->method->name);
4724 #endif
4725                                                         goto handle_finally;
4726                                                 }
4727                                         } else {
4728                                                 /* FIXME: handle filter clauses */
4729                                                 g_assert (0);
4730                                         }
4731                                 }
4732                         }
4733                 }
4734                 /*
4735                  * If we get here, no handler was found: print a stack trace.
4736                  */
4737                 for (inv = frame; inv; inv = inv->parent) {
4738                         if (inv->invoke_trap)
4739                                 goto handle_finally;
4740                 }
4741 die_on_ex:
4742                 ex_obj = (MonoObject*)frame->ex;
4743                 mono_unhandled_exception (ex_obj);
4744                 exit (1);
4745         }
4746         handle_finally:
4747         {
4748                 int i;
4749                 guint32 ip_offset;
4750                 MonoExceptionClause *clause;
4751                 GSList *old_list = finally_ips;
4752                 
4753 #if DEBUG_INTERP
4754                 if (tracing)
4755                         g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - header->code);
4756 #endif
4757                 if ((frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
4758                                 || (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4759                         goto exit_frame;
4760                 }
4761                 ip_offset = frame->ip - header->code;
4762
4763                 if (endfinally_ip != NULL)
4764                         finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4765                 for (i = 0; i < header->num_clauses; ++i)
4766                         if (frame->ex_handler == &header->clauses [i])
4767                                 break;
4768                 while (i > 0) {
4769                         --i;
4770                         clause = &header->clauses [i];
4771                         if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - header->code)))) {
4772                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4773                                         ip = header->code + clause->handler_offset;
4774                                         finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4775 #if DEBUG_INTERP
4776                                         if (tracing)
4777                                                 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4778 #endif
4779                                 }
4780                         }
4781                 }
4782
4783                 endfinally_ip = NULL;
4784
4785                 if (old_list != finally_ips && finally_ips) {
4786                         ip = finally_ips->data;
4787                         finally_ips = g_slist_remove (finally_ips, ip);
4788                         sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4789                         goto main_loop;
4790                 }
4791
4792                 /*
4793                  * If an exception is set, we need to execute the fault handler, too,
4794                  * otherwise, we continue normally.
4795                  */
4796                 if (frame->ex)
4797                         goto handle_fault;
4798                 ves_abort();
4799         }
4800         handle_fault:
4801         {
4802                 int i;
4803                 guint32 ip_offset;
4804                 MonoExceptionClause *clause;
4805                 
4806 #if DEBUG_INTERP
4807                 if (tracing)
4808                         g_print ("* Handle fault\n");
4809 #endif
4810                 ip_offset = frame->ip - header->code;
4811                 for (i = 0; i < header->num_clauses; ++i) {
4812                         clause = &header->clauses [i];
4813                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4814                                 ip = header->code + clause->handler_offset;
4815 #if DEBUG_INTERP
4816                                 if (tracing)
4817                                         g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4818 #endif
4819                                 goto main_loop;
4820                         }
4821                 }
4822                 /*
4823                  * If the handler for the exception was found in this method, we jump
4824                  * to it right away, otherwise we return and let the caller run
4825                  * the finally, fault and catch blocks.
4826                  * This same code should be present in the endfault opcode, but it
4827                  * is corrently not assigned in the ECMA specs: LAMESPEC.
4828                  */
4829                 if (frame->ex_handler) {
4830 #if DEBUG_INTERP
4831                         if (tracing)
4832                                 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4833 #endif
4834                         ip = header->code + frame->ex_handler->handler_offset;
4835                         sp = frame->stack;
4836                         sp->type = VAL_OBJ;
4837                         sp->data.p = frame->ex;
4838                         ++sp;
4839                         goto main_loop;
4840                 }
4841                 goto exit_frame;
4842         }
4843 exit_frame:
4844         DEBUG_LEAVE ();
4845 }
4846
4847 void
4848 ves_exec_method (MonoInvocation *frame)
4849 {
4850         ThreadContext *context = TlsGetValue (thread_context_id);
4851         ThreadContext context_struct;
4852         if (context == NULL) {
4853                 context = &context_struct;
4854                 context_struct.base_frame = frame;
4855                 context_struct.current_frame = NULL;
4856                 context_struct.current_env = NULL;
4857                 context_struct.search_for_handler = 0;
4858                 TlsSetValue (thread_context_id, context);
4859         }
4860         frame->ip = NULL;
4861         frame->parent = context->current_frame;
4862         ves_exec_method_with_context(frame, context);
4863         if (frame->ex) {
4864                 if (context->current_env) {
4865                         context->env_frame->ex = frame->ex;
4866                         longjmp (*context->current_env, 1);
4867                 }
4868                 else
4869                         mono_unhandled_exception ((MonoObject*)frame->ex);
4870         }
4871         if (context->base_frame == frame)
4872                 TlsSetValue (thread_context_id, NULL);
4873         else
4874                 context->current_frame = frame->parent;
4875 }
4876
4877 static int 
4878 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4879 {
4880         MonoImage *image = assembly->image;
4881         MonoCLIImageInfo *iinfo;
4882         MonoMethod *method;
4883         MonoObject *exc = NULL;
4884         int rval;
4885
4886         iinfo = image->image_info;
4887         method = mono_get_method (image, iinfo->cli_cli_header.ch_entry_point, NULL);
4888         if (!method)
4889                 g_error ("No entry point method found in %s", image->name);
4890
4891         rval = mono_runtime_run_main (method, argc, argv, &exc);
4892         if (exc != NULL)
4893                 mono_unhandled_exception (exc);
4894
4895         return rval;
4896 }
4897
4898 static void
4899 usage (void)
4900 {
4901         fprintf (stderr,
4902                  "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4903                  "Usage is: mint [options] executable args...\n\n", VERSION);
4904         fprintf (stderr,
4905                  "Runtime Debugging:\n"
4906 #ifdef DEBUG_INTERP
4907                  "   --debug\n"
4908 #endif
4909                  "   --dieonex\n"
4910                  "   --noptr\t\t\tdon't print pointer addresses in trace output\n"
4911                  "   --opcode-count\n"
4912                  "   --print-vtable\n"
4913                  "   --traceclassinit\n"
4914                  "\n"
4915                  "Development:\n"
4916                  "   --debug method_name\n"
4917                  "   --profile\n"
4918                  "   --trace\n"
4919                  "   --traceops\n"
4920                  "\n"
4921                  "Runtime:\n"
4922                  "   --config filename  load the specified config file instead of the default\n"
4923                  "   --workers n        maximum number of worker threads\n"
4924                 );
4925         exit (1);
4926 }
4927
4928 #ifdef RUN_TEST
4929 static void
4930 test_load_class (MonoImage* image)
4931 {
4932         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
4933         MonoClass *klass;
4934         int i;
4935
4936         for (i = 1; i <= t->rows; ++i) {
4937                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4938                 mono_class_init (klass);
4939         }
4940 }
4941 #endif
4942
4943 static MonoException * segv_exception = NULL;
4944
4945 static void
4946 segv_handler (int signum)
4947 {
4948         signal (signum, segv_handler);
4949         mono_raise_exception (segv_exception);
4950 }
4951
4952
4953 static void
4954 quit_handler (int signum)
4955 {
4956         signal (signum, quit_handler);
4957         mono_raise_exception (quit_exception);
4958 }
4959
4960 static MonoBoolean
4961 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
4962                           MonoReflectionMethod **method, 
4963                           gint32 *iloffset, gint32 *native_offset,
4964                           MonoString **file, gint32 *line, gint32 *column)
4965 {
4966         if (iloffset)
4967                 *iloffset = 0;
4968         if (native_offset)
4969                 *native_offset = 0;
4970         if (method)
4971                 *method = NULL;
4972         if (line)
4973                 *line = 0;
4974         if (column)
4975                 *column = 0;
4976         if (file)
4977                 *file = mono_string_new (mono_domain_get (), "unknown");
4978
4979         return TRUE;
4980 }
4981
4982 static MonoArray *
4983 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4984 {
4985         return NULL;
4986 }
4987
4988 typedef struct
4989 {
4990         MonoDomain *domain;
4991         int enable_debugging;
4992         char *file;
4993         int argc;
4994         char **argv;
4995 } MainThreadArgs;
4996
4997 static void main_thread_handler (gpointer user_data)
4998 {
4999         MainThreadArgs *main_args=(MainThreadArgs *)user_data;
5000         MonoAssembly *assembly;
5001         char *error;
5002
5003         if (main_args->enable_debugging)
5004                 mono_debug_init (main_args->domain, MONO_DEBUG_FORMAT_MONO);
5005
5006         assembly = mono_domain_assembly_open (main_args->domain,
5007                                               main_args->file);
5008
5009         if (!assembly){
5010                 fprintf (stderr, "Can not open image %s\n", main_args->file);
5011                 exit (1);
5012         }
5013
5014         if (main_args->enable_debugging)
5015                 mono_debug_init_2 (assembly);
5016
5017 #ifdef RUN_TEST
5018         test_load_class (assembly->image);
5019 #else
5020         error = mono_verify_corlib ();
5021         if (error) {
5022                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
5023                 exit (1);
5024         }
5025         segv_exception = mono_get_exception_null_reference ();
5026         segv_exception->message = mono_string_new (main_args->domain, "Segmentation fault");
5027         signal (SIGSEGV, segv_handler);
5028         /* perhaps we should use a different class for this exception... */
5029         quit_exception = mono_get_exception_null_reference ();
5030         quit_exception->message = mono_string_new (main_args->domain, "Quit");
5031         signal (SIGINT, quit_handler);
5032
5033         ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
5034 #endif
5035 }
5036
5037 static void
5038 mono_runtime_install_handlers (void)
5039 {
5040         /* FIXME: anything to do here? */
5041 }
5042
5043 static void
5044 quit_function (MonoDomain *domain, gpointer user_data)
5045 {
5046         mono_profiler_shutdown ();
5047         
5048         mono_runtime_cleanup (domain);
5049         mono_domain_free (domain, TRUE);
5050
5051 }
5052
5053 void
5054 mono_interp_cleanup(MonoDomain *domain)
5055 {
5056     quit_function (domain, NULL);
5057 }
5058
5059 int
5060 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
5061 {
5062     return ves_exec (domain, assembly, argc, argv);
5063 }
5064
5065 MonoDomain *
5066 mono_interp_init(const char *file)
5067 {
5068         MonoDomain *domain;
5069
5070         g_set_prgname (file);
5071         mono_set_rootdir ();
5072         
5073         g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
5074         g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
5075
5076         g_thread_init (NULL);
5077
5078         thread_context_id = TlsAlloc ();
5079         TlsSetValue (thread_context_id, NULL);
5080         InitializeCriticalSection(&calc_section);
5081         InitializeCriticalSection(&create_method_pointer_mutex);
5082
5083         mono_install_compile_method (mono_create_method_pointer);
5084         mono_install_runtime_invoke (interp_mono_runtime_invoke);
5085         mono_install_remoting_trampoline (interp_create_remoting_trampoline);
5086
5087         mono_install_handler (interp_ex_handler);
5088         mono_install_stack_walk (interp_walk_stack);
5089         mono_runtime_install_cleanup (quit_function);
5090
5091         domain = mono_init (file);
5092 #ifdef __hpux /* generates very big stack frames */
5093         mono_threads_set_default_stacksize(32*1024*1024);
5094 #endif
5095         mono_init_icall ();
5096         mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
5097         mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
5098         mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
5099
5100         mono_runtime_init (domain, NULL, NULL);
5101
5102         return domain;
5103 }
5104
5105 int 
5106 mono_main (int argc, char *argv [])
5107 {
5108         MonoDomain *domain;
5109         int retval = 0, i, ocount = 0;
5110         char *file, *config_file = NULL;
5111         int enable_debugging = FALSE;
5112         MainThreadArgs main_args;
5113         const char *error;
5114         
5115         if (argc < 2)
5116                 usage ();
5117
5118         for (i = 1; i < argc && argv [i][0] == '-'; i++){
5119                 if (strcmp (argv [i], "--trace") == 0)
5120                         global_tracing = 1;
5121                 if (strcmp (argv [i], "--noptr") == 0)
5122                         global_no_pointers = 1;
5123                 if (strcmp (argv [i], "--traceops") == 0)
5124                         global_tracing = 2;
5125                 if (strcmp (argv [i], "--dieonex") == 0) {
5126                         die_on_exception = 1;
5127                         enable_debugging = 1;
5128                 }
5129                 if (strcmp (argv [i], "--print-vtable") == 0)
5130                         mono_print_vtable = TRUE;
5131                 if (strcmp (argv [i], "--profile") == 0)
5132                         mono_profiler_load (NULL);
5133                 if (strcmp (argv [i], "--opcode-count") == 0)
5134                         ocount = 1;
5135                 if (strcmp (argv [i], "--config") == 0)
5136                         config_file = argv [++i];
5137                 if (strcmp (argv [i], "--workers") == 0) {
5138                         mono_max_worker_threads = atoi (argv [++i]);
5139                         if (mono_max_worker_threads < 1)
5140                                 mono_max_worker_threads = 1;
5141                 }
5142                 if (strcmp (argv [i], "--help") == 0)
5143                         usage ();
5144 #if DEBUG_INTERP
5145                 if (strcmp (argv [i], "--debug") == 0) {
5146                         MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
5147                         if (!desc)
5148                                 g_error ("Invalid method name '%s'", argv [i]);
5149                         db_methods = g_list_append (db_methods, desc);
5150                 }
5151 #endif
5152         }
5153         
5154         file = argv [i];
5155
5156         if (!file)
5157                 usage ();
5158
5159         domain = mono_interp_init(file);
5160         mono_config_parse (config_file);
5161
5162         error = mono_verify_corlib ();
5163         if (error) {
5164                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
5165                 exit (1);
5166         }
5167
5168         error = mono_check_corlib_version ();
5169         if (error) {
5170                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
5171                 fprintf (stderr, "Download a newer corlib at http://go-mono/daily.\n");
5172                 exit (1);
5173         }
5174
5175         main_args.domain=domain;
5176         main_args.file=file;
5177         main_args.argc=argc-i;
5178         main_args.argv=argv+i;
5179         main_args.enable_debugging=enable_debugging;
5180         
5181         mono_runtime_exec_managed_code (domain, main_thread_handler,
5182                                         &main_args);
5183
5184         quit_function (domain, NULL);
5185
5186         /* Get the return value from System.Environment.ExitCode */
5187         retval=mono_environment_exitcode_get ();
5188
5189 #if DEBUG_INTERP
5190         if (ocount) {
5191                 fprintf (stderr, "opcode count: %ld\n", opcode_count);
5192                 fprintf (stderr, "fcall count: %ld\n", fcall_count);
5193         }
5194 #endif
5195         return retval;
5196 }