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