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