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