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