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