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