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