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