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