2004-01-19 Bernie Solomon <bernard@ugsolutions.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 /*
1575  * Need to optimize ALU ops when natural int == int32 
1576  *
1577  * IDEA: if we maintain a stack of ip, sp to be checked
1578  * in the return opcode, we could inline simple methods that don't
1579  * use the stack or local variables....
1580  * 
1581  * The {,.S} versions of many opcodes can/should be merged to reduce code
1582  * duplication.
1583  * 
1584  */
1585 static void 
1586 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1587 {
1588         MonoDomain *domain = mono_domain_get ();        
1589         MonoInvocation child_frame;
1590         MonoMethodHeader *header;
1591         MonoMethodSignature *signature;
1592         MonoImage *image;
1593         GSList *finally_ips = NULL;
1594         const unsigned char *endfinally_ip = NULL;
1595         register const unsigned char *ip;
1596         register stackval *sp = NULL;
1597         MethodRuntimeData *rtd;
1598         void **args_pointers;
1599         gint tracing = global_tracing;
1600         unsigned char tail_recursion = 0;
1601         unsigned char unaligned_address = 0;
1602         unsigned char volatile_address = 0;
1603         vtallocation *vtalloc = NULL;
1604         MonoVTable *method_class_vt;
1605         GOTO_LABEL_VARS;
1606
1607         frame->ex = NULL;
1608         frame->ip = ip = NULL;
1609         context->current_frame = frame;
1610
1611         if (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1612                 frame->method = mono_marshal_get_native_wrapper (frame->method);
1613                 if (frame->method == NULL)
1614                         goto exit_frame;
1615         }
1616
1617         method_class_vt = mono_class_vtable (domain, frame->method->klass);
1618         if (!method_class_vt->initialized)
1619                 mono_runtime_class_init (method_class_vt);
1620         signature = frame->method->signature;
1621
1622         DEBUG_ENTER ();
1623
1624         header = ((MonoMethodNormal *)frame->method)->header;
1625
1626         if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1627                 if (!frame->method->addr) {
1628                         /* assumes all internal calls with an array this are built in... */
1629                         if (signature->hasthis && frame->method->klass->rank) {
1630                                 ves_runtime_method (frame, context);
1631                                 if (frame->ex)
1632                                         goto handle_exception;
1633                                 goto exit_frame;
1634                         }
1635                         frame->method->addr = mono_lookup_internal_call (frame->method);
1636                 }
1637                 ves_pinvoke_method (frame, frame->method->signature, frame->method->addr, 
1638                             frame->method->string_ctor, context);
1639                 if (frame->ex)
1640                         goto handle_exception;
1641                 goto exit_frame;
1642         } 
1643         else if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1644                 ves_runtime_method (frame, context);
1645                 if (frame->ex)
1646                         goto handle_exception;
1647                 goto exit_frame;
1648         }
1649
1650         /*verify_method (frame->method);*/
1651
1652         image = frame->method->klass->image;
1653
1654         if (!frame->method->info)
1655                 calc_offsets (image, frame->method);
1656         rtd = frame->method->info;
1657
1658         /*
1659          * with alloca we get the expected huge performance gain
1660          * stackval *stack = g_new0(stackval, header->max_stack);
1661          */
1662         g_assert (header->max_stack < 10000);
1663         sp = frame->stack = alloca (sizeof (stackval) * header->max_stack);
1664
1665         if (header->num_locals) {
1666                 g_assert (rtd->locals_size < 65536);
1667                 frame->locals = alloca (rtd->locals_size);
1668                 /* 
1669                  * yes, we do it unconditionally, because it needs to be done for
1670                  * some cases anyway and checking for that would be even slower.
1671                  */
1672                 memset (frame->locals, 0, rtd->locals_size);
1673         }
1674
1675         /*
1676          * Copy args from stack_args to args.
1677          */
1678         if (signature->param_count || signature->hasthis) {
1679                 int i;
1680                 int has_this = signature->hasthis;
1681
1682                 g_assert (rtd->args_size < 10000);
1683                 frame->args = alloca (rtd->args_size);
1684                 g_assert ((signature->param_count + has_this) < 1000);
1685                 args_pointers = alloca (sizeof(void*) * (signature->param_count + has_this));
1686                 if (has_this) {
1687                         gpointer *this_arg;
1688                         this_arg = args_pointers [0] = frame->args;
1689                         *this_arg = frame->obj;
1690                 }
1691                 for (i = 0; i < signature->param_count; ++i) {
1692                         args_pointers [i + has_this] = frame->args + rtd->offsets [header->num_locals + has_this + i];
1693                         stackval_to_data (signature->params [i], frame->stack_args + i, args_pointers [i + has_this], signature->pinvoke);
1694                 }
1695         }
1696
1697         child_frame.parent = frame;
1698         frame->child = &child_frame;
1699         frame->ex = NULL;
1700
1701         /* ready to go */
1702         ip = header->code;
1703
1704         /*
1705          * using while (ip < end) may result in a 15% performance drop, 
1706          * but it may be useful for debug
1707          */
1708         while (1) {
1709         main_loop:
1710                 /*g_assert (sp >= stack);*/
1711 #if DEBUG_INTERP
1712                 opcode_count++;
1713                 if (tracing > 1) {
1714                         char *ins, *discode;
1715                         if (sp > frame->stack) {
1716                                 ins = dump_stack (frame->stack, sp);
1717                         } else {
1718                                 ins = g_strdup ("");
1719                         }
1720                         output_indent ();
1721                         discode = mono_disasm_code_one (NULL, frame->method, ip, NULL);
1722                         discode [strlen (discode) - 1] = 0; /* no \n */
1723                         g_print ("(%u) %-29s %s\n", GetCurrentThreadId(), discode, ins);
1724                         g_free (ins);
1725                         g_free (discode);
1726                 }
1727 #endif
1728                 
1729                 SWITCH (*ip) {
1730                 CASE (CEE_NOP) 
1731                         ++ip;
1732                         BREAK;
1733                 CASE (CEE_BREAK)
1734                         ++ip;
1735                         G_BREAKPOINT (); /* this is not portable... */
1736                         BREAK;
1737                 CASE (CEE_LDARG_0)
1738                 CASE (CEE_LDARG_1)
1739                 CASE (CEE_LDARG_2)
1740                 CASE (CEE_LDARG_3) {
1741                         int n = (*ip)-CEE_LDARG_0;
1742                         ++ip;
1743                         vt_alloc (ARG_TYPE (signature, n), sp, signature->pinvoke);
1744                         stackval_from_data (ARG_TYPE (signature, n), sp, ARG_POS (n), signature->pinvoke);
1745                         ++sp;
1746                         BREAK;
1747                 }
1748                 CASE (CEE_LDLOC_0)
1749                 CASE (CEE_LDLOC_1)
1750                 CASE (CEE_LDLOC_2)
1751                 CASE (CEE_LDLOC_3) {
1752                         int n = (*ip)-CEE_LDLOC_0;
1753                         MonoType *vartype = LOCAL_TYPE (header, n);
1754                         ++ip;
1755                         if (vartype->type == MONO_TYPE_I4) {
1756                                 sp->type = VAL_I32;
1757                                 sp->data.i = *(gint32*) LOCAL_POS (n);
1758                                 ++sp;
1759                                 BREAK;
1760                         } else {
1761                                 vt_alloc (vartype, sp, FALSE);
1762                                 stackval_from_data (vartype, sp, LOCAL_POS (n), FALSE);
1763                         }
1764                         ++sp;
1765                         BREAK;
1766                 }
1767                 CASE (CEE_STLOC_0)
1768                 CASE (CEE_STLOC_1)
1769                 CASE (CEE_STLOC_2)
1770                 CASE (CEE_STLOC_3) {
1771                         int n = (*ip)-CEE_STLOC_0;
1772                         ++ip;
1773                         --sp;
1774                         if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1775                                 gint32 *p = (gint32*)LOCAL_POS (n);
1776                                 *p = sp->data.i;
1777                                 BREAK;
1778                         } else {
1779                                 stackval_to_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1780                                 vt_free (sp);
1781                                 BREAK;
1782                         }
1783                 }
1784                 CASE (CEE_LDARG_S)
1785                         ++ip;
1786                         vt_alloc (ARG_TYPE (signature, *ip), sp, signature->pinvoke);
1787                         stackval_from_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1788                         ++sp;
1789                         ++ip;
1790                         BREAK;
1791                 CASE (CEE_LDARGA_S) {
1792                         ++ip;
1793                         sp->data.vt = ARG_POS (*ip);
1794                         sp->type = VAL_MP;
1795                         ++sp;
1796                         ++ip;
1797                         BREAK;
1798                 }
1799                 CASE (CEE_STARG_S)
1800                         ++ip;
1801                         --sp;
1802                         stackval_to_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1803                         vt_free (sp);
1804                         ++ip;
1805                         BREAK;
1806                 CASE (CEE_LDLOC_S)
1807                         ++ip;
1808                         vt_alloc (LOCAL_TYPE (header, *ip), sp, FALSE);
1809                         stackval_from_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1810                         ++ip;
1811                         ++sp;
1812                         BREAK;
1813                 CASE (CEE_LDLOCA_S) {
1814                         ++ip;
1815                         sp->data.p = LOCAL_POS (*ip);
1816                         sp->type = VAL_MP;
1817                         ++sp;
1818                         ++ip;
1819                         BREAK;
1820                 }
1821                 CASE (CEE_STLOC_S)
1822                         ++ip;
1823                         --sp;
1824                         stackval_to_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1825                         vt_free (sp);
1826                         ++ip;
1827                         BREAK;
1828                 CASE (CEE_LDNULL) 
1829                         ++ip;
1830                         sp->type = VAL_OBJ;
1831                         sp->data.p = NULL;
1832                         ++sp;
1833                         BREAK;
1834                 CASE (CEE_LDC_I4_M1)
1835                         ++ip;
1836                         sp->type = VAL_I32;
1837                         sp->data.i = -1;
1838                         ++sp;
1839                         BREAK;
1840                 CASE (CEE_LDC_I4_0)
1841                 CASE (CEE_LDC_I4_1)
1842                 CASE (CEE_LDC_I4_2)
1843                 CASE (CEE_LDC_I4_3)
1844                 CASE (CEE_LDC_I4_4)
1845                 CASE (CEE_LDC_I4_5)
1846                 CASE (CEE_LDC_I4_6)
1847                 CASE (CEE_LDC_I4_7)
1848                 CASE (CEE_LDC_I4_8)
1849                         sp->type = VAL_I32;
1850                         sp->data.i = (*ip) - CEE_LDC_I4_0;
1851                         ++sp;
1852                         ++ip;
1853                         BREAK;
1854                 CASE (CEE_LDC_I4_S) 
1855                         ++ip;
1856                         sp->type = VAL_I32;
1857                         sp->data.i = *(const gint8 *)ip;
1858                         ++ip;
1859                         ++sp;
1860                         BREAK;
1861                 CASE (CEE_LDC_I4)
1862                         ++ip;
1863                         sp->type = VAL_I32;
1864                         sp->data.i = read32 (ip);
1865                         ip += 4;
1866                         ++sp;
1867                         BREAK;
1868                 CASE (CEE_LDC_I8)
1869                         ++ip;
1870                         sp->type = VAL_I64;
1871                         sp->data.l = read64 (ip);
1872                         ip += 8;
1873                         ++sp;
1874                         BREAK;
1875                 CASE (CEE_LDC_R4) {
1876                         float val;
1877                         ++ip;
1878                         sp->type = VAL_DOUBLE;
1879                         readr4 (ip, &val);
1880                         sp->data.f = val;
1881                         ip += 4;
1882                         ++sp;
1883                         BREAK;
1884                 }
1885                 CASE (CEE_LDC_R8) 
1886                         ++ip;
1887                         sp->type = VAL_DOUBLE;
1888                         readr8(ip, &sp->data.f);
1889                         ip += 8;
1890                         ++sp;
1891                         BREAK;
1892                 CASE (CEE_UNUSED99) ves_abort (); BREAK;
1893                 CASE (CEE_DUP) 
1894                         stackvalpush(sp - 1, sp);
1895                         ++ip; 
1896                         BREAK;
1897                 CASE (CEE_POP)
1898                         ++ip;
1899                         --sp;
1900                         vt_free (sp);
1901                         BREAK;
1902                 CASE (CEE_JMP) ves_abort(); BREAK;
1903                 CASE (CEE_CALLVIRT) /* Fall through */
1904                 CASE (CEE_CALLI)    /* Fall through */
1905                 CASE (CEE_CALL) {
1906                         MonoMethodSignature *csignature;
1907                         stackval retval;
1908                         stackval *endsp = sp;
1909                         guint32 token;
1910                         int virtual = *ip == CEE_CALLVIRT;
1911                         int calli = *ip == CEE_CALLI;
1912                         unsigned char *code = NULL;
1913
1914                         /*
1915                          * We ignore tail recursion for now.
1916                          */
1917                         tail_recursion = 0;
1918
1919                         frame->ip = ip;
1920                         
1921                         ++ip;
1922                         token = read32 (ip);
1923                         ip += 4;
1924                         if (calli) {
1925                                 MonoJitInfo *ji;
1926                                 --sp;
1927                                 code = sp->data.p;
1928                                 if (frame->method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE &&
1929                                         (ji = mono_jit_info_table_find (mono_root_domain, code)) != NULL) {
1930                                         child_frame.method = ji->method;
1931                                         csignature = ji->method->signature;
1932                                 }
1933                                 else if (frame->method->wrapper_type != MONO_WRAPPER_NONE) {
1934                                         csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (frame->method, token);
1935                                         child_frame.method = NULL;
1936                                 } else {
1937                                         g_assert_not_reached ();
1938                                 }
1939                                 g_assert (code);
1940                         } else {
1941                                 if (frame->method->wrapper_type != MONO_WRAPPER_NONE) 
1942                                         child_frame.method = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
1943                                 else
1944                                         child_frame.method = mono_get_method (image, token, NULL);
1945                                 if (!child_frame.method)
1946                                         THROW_EX (mono_get_exception_missing_method (), ip -5);
1947                                 csignature = child_frame.method->signature;
1948                                 if (virtual) {
1949                                         stackval *this_arg = &sp [-csignature->param_count-1];
1950                                         if (!this_arg->data.p)
1951                                                 THROW_EX (mono_get_exception_null_reference(), ip - 5);
1952                                         child_frame.method = get_virtual_method (domain, child_frame.method, this_arg);
1953                                         if (!child_frame.method)
1954                                                 THROW_EX (mono_get_exception_missing_method (), ip -5);
1955                                 }
1956                         }
1957
1958                         if (frame->method->wrapper_type == MONO_WRAPPER_NONE)
1959                                 if (child_frame.method && child_frame.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1960                                         child_frame.method = mono_marshal_get_synchronized_wrapper (child_frame.method);
1961
1962                         g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
1963                         /* decrement by the actual number of args */
1964                         if (csignature->param_count) {
1965                                 sp -= csignature->param_count;
1966                                 child_frame.stack_args = sp;
1967                         } else {
1968                                 child_frame.stack_args = NULL;
1969                         }
1970                         if (csignature->hasthis) {
1971                                 g_assert (sp >= frame->stack);
1972                                 --sp;
1973                                 /*
1974                                  * It may also be a TP from LD(S)FLDA
1975                                  * g_assert (sp->type == VAL_OBJ || sp->type == VAL_MP);
1976                                  */
1977                                 if (sp->type == VAL_OBJ && child_frame.method &&
1978                                     child_frame.method->klass->valuetype) /* unbox it */
1979                                         child_frame.obj = (char*)sp->data.p + sizeof (MonoObject);
1980                                 else
1981                                         child_frame.obj = sp->data.p;
1982                         } else {
1983                                 child_frame.obj = NULL;
1984                         }
1985                         if (csignature->ret->type != MONO_TYPE_VOID) {
1986                                 vt_alloc (csignature->ret, &retval, csignature->pinvoke);
1987                                 child_frame.retval = &retval;
1988                         } else {
1989                                 child_frame.retval = NULL;
1990                         }
1991
1992                         child_frame.ip = NULL;
1993                         child_frame.ex = NULL;
1994                         child_frame.ex_handler = NULL;
1995
1996                         if (!child_frame.method) {
1997                                 g_assert (code);
1998                                 ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE, context);
1999                                 if (child_frame.ex) {
2000                                         frame->ex = child_frame.ex;
2001                                         goto handle_exception;
2002                                 }
2003                         } else if (csignature->hasthis && sp->type == VAL_OBJ &&
2004                                         ((MonoObject *)sp->data.p)->vtable->klass == mono_defaults.transparent_proxy_class) {
2005                                 g_assert (child_frame.method);
2006                                 child_frame.method = mono_marshal_get_remoting_invoke (child_frame.method);
2007                                 ves_exec_method_with_context (&child_frame, context);
2008                         } else {
2009                                 switch (GPOINTER_TO_UINT (child_frame.method->addr)) {
2010                                 case INLINE_STRING_LENGTH:
2011                                         retval.type = VAL_I32;
2012                                         retval.data.i = ((MonoString*)sp->data.p)->length;
2013                                         /*g_print ("length of '%s' is %d\n", mono_string_to_utf8 (sp->data.p), retval.data.i);*/
2014                                         break;
2015                                 case INLINE_STRING_GET_CHARS: {
2016                                         int idx = GET_NATI(sp [1]);
2017                                         if ((idx < 0) || (idx >= mono_string_length ((MonoString*)sp->data.p))) {
2018                                                 child_frame.ex = mono_get_exception_index_out_of_range ();
2019                                                 FILL_IN_TRACE(child_frame.ex, &child_frame);
2020                                         }
2021                                         else {
2022                                                 retval.type = VAL_I32;
2023                                                 retval.data.i = mono_string_chars((MonoString*)sp->data.p)[idx];
2024                                         }
2025                                         break;
2026                                 }
2027                                 case INLINE_ARRAY_LENGTH:
2028                                         retval.type = VAL_I32;
2029                                         retval.data.i = mono_array_length ((MonoArray*)sp->data.p);
2030                                         break;
2031                                 case INLINE_ARRAY_RANK:
2032                                         retval.type = VAL_I32;
2033                                         retval.data.i = mono_object_class (sp->data.p)->rank;
2034                                         break;
2035                                 case INLINE_TYPE_ELEMENT_TYPE:
2036                                         retval.type = VAL_OBJ;
2037                                         {
2038                                                 MonoClass *c = mono_class_from_mono_type (((MonoReflectionType*)sp->data.p)->type);
2039                                                 if (c->enumtype && c->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
2040                                                         retval.data.p = mono_type_get_object (domain, c->enum_basetype);
2041                                                 else if (c->element_class)
2042                                                         retval.data.p = mono_type_get_object (domain, &c->element_class->byval_arg);
2043                                                 else
2044                                                         retval.data.p = NULL;
2045                                         }
2046                                         break;
2047                                 default:
2048                                         ves_exec_method_with_context (&child_frame, context);
2049                                 }
2050                         }
2051
2052                         context->current_frame = frame;
2053
2054                         while (endsp > sp) {
2055                                 --endsp;
2056                                 vt_free (endsp);
2057                         }
2058
2059                         if (child_frame.ex) {
2060                                 /*
2061                                  * An exception occurred, need to run finally, fault and catch handlers..
2062                                  */
2063                                 frame->ex = child_frame.ex;
2064                                 if (context->search_for_handler) {
2065                                         context->search_for_handler = 0;
2066                                         goto handle_exception;
2067                                 }
2068                                 goto handle_finally;
2069                         }
2070
2071                         /* need to handle typedbyref ... */
2072                         if (csignature->ret->type != MONO_TYPE_VOID) {
2073                                 *sp = retval;
2074                                 sp++;
2075                         }
2076                         BREAK;
2077                 }
2078                 CASE (CEE_RET)
2079                         if (signature->ret->type != MONO_TYPE_VOID) {
2080                                 --sp;
2081                                 if (sp->type == VAL_VALUET) {
2082                                         /* the caller has already allocated the memory */
2083                                         stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);
2084                                         vt_free (sp);
2085                                 } else {
2086                                         *frame->retval = *sp;
2087                                 }
2088                         }
2089                         if (sp > frame->stack)
2090                                 g_warning ("more values on stack: %d", sp-frame->stack);
2091
2092                         goto exit_frame;
2093                 CASE (CEE_BR_S) /* Fall through */
2094                 CASE (CEE_BR)
2095                         if (*ip == CEE_BR) {
2096                                 ++ip;
2097                                 ip += (gint32) read32(ip);
2098                                 ip += 4;
2099                         } else {
2100                                 ++ip;
2101                                 ip += (signed char) *ip;
2102                                 ++ip;
2103                         }
2104                         BREAK;
2105                 CASE (CEE_BRFALSE) /* Fall through */
2106                 CASE (CEE_BRFALSE_S) {
2107                         int result;
2108                         int broffset;
2109                         if (*ip == CEE_BRFALSE_S) {
2110                                 broffset = (signed char)ip [1];
2111                                 ip += 2;
2112                         } else {
2113                                 broffset = (gint32) read32 (ip + 1);
2114                                 ip += 5;
2115                         }
2116                         --sp;
2117                         switch (sp->type) {
2118                         case VAL_I32: result = sp->data.i == 0; break;
2119                         case VAL_I64: result = sp->data.l == 0; break;
2120                         case VAL_DOUBLE: result = sp->data.f ? 0: 1; break;
2121                         default: result = sp->data.p == NULL; break;
2122                         }
2123                         if (result)
2124                                 ip += broffset;
2125                         BREAK;
2126                 }
2127                 CASE (CEE_BRTRUE) /* Fall through */
2128                 CASE (CEE_BRTRUE_S) {
2129                         int result;
2130                         int broffset;
2131                         if (*ip == CEE_BRTRUE_S) {
2132                                 broffset = (signed char)ip [1];
2133                                 ip += 2;
2134                         } else {
2135                                 broffset = (gint32) read32 (ip + 1);
2136                                 ip += 5;
2137                         }
2138                         --sp;
2139                         switch (sp->type) {
2140                         case VAL_I32: result = sp->data.i != 0; break;
2141                         case VAL_I64: result = sp->data.l != 0; break;
2142                         case VAL_DOUBLE: result = sp->data.f ? 1 : 0; break;
2143                         default: result = sp->data.p != NULL; break;
2144                         }
2145                         if (result)
2146                                 ip += broffset;
2147                         BREAK;
2148                 }
2149                 CASE (CEE_BEQ) /* Fall through */
2150                 CASE (CEE_BEQ_S) {
2151                         int result;
2152                         int broffset;
2153                         if (*ip == CEE_BEQ_S) {
2154                                 broffset = (signed char)ip [1];
2155                                 ip += 2;
2156                         } else {
2157                                 broffset = (gint32) read32 (ip + 1);
2158                                 ip += 5;
2159                         }
2160                         sp -= 2;
2161                         if (sp->type == VAL_I32)
2162                                 result = (mono_i)sp [0].data.i == (mono_i)GET_NATI (sp [1]);
2163                         else if (sp->type == VAL_I64)
2164                                 result = sp [0].data.l == sp [1].data.l;
2165                         else if (sp->type == VAL_DOUBLE)
2166                                 result = sp [0].data.f == sp [1].data.f;
2167                         else
2168                                 result = sp [0].data.nati == (mono_i)GET_NATI (sp [1]);
2169                         if (result)
2170                                 ip += broffset;
2171                         BREAK;
2172                 }
2173                 CASE (CEE_BGE) /* Fall through */
2174                 CASE (CEE_BGE_S) {
2175                         int result;
2176                         int broffset;
2177                         if (*ip == CEE_BGE_S) {
2178                                 broffset = (signed char)ip [1];
2179                                 ip += 2;
2180                         } else {
2181                                 broffset = (gint32) read32 (ip + 1);
2182                                 ip += 5;
2183                         }
2184                         sp -= 2;
2185                         if (sp->type == VAL_I32)
2186                                 result = (mono_i)sp [0].data.i >= (mono_i)GET_NATI (sp [1]);
2187                         else if (sp->type == VAL_I64)
2188                                 result = sp [0].data.l >= sp [1].data.l;
2189                         else if (sp->type == VAL_DOUBLE)
2190                                 result = sp [0].data.f >= sp [1].data.f;
2191                         else
2192                                 result = (mono_i)sp [0].data.nati >= (mono_i)GET_NATI (sp [1]);
2193                         if (result)
2194                                 ip += broffset;
2195                         BREAK;
2196                 }
2197                 CASE (CEE_BGT) /* Fall through */
2198                 CASE (CEE_BGT_S) {
2199                         int result;
2200                         int broffset;
2201                         if (*ip == CEE_BGT_S) {
2202                                 broffset = (signed char)ip [1];
2203                                 ip += 2;
2204                         } else {
2205                                 broffset = (gint32) read32 (ip + 1);
2206                                 ip += 5;
2207                         }
2208                         sp -= 2;
2209                         if (sp->type == VAL_I32)
2210                                 result = (mono_i)sp [0].data.i > (mono_i)GET_NATI (sp [1]);
2211                         else if (sp->type == VAL_I64)
2212                                 result = sp [0].data.l > sp [1].data.l;
2213                         else if (sp->type == VAL_DOUBLE)
2214                                 result = sp [0].data.f > sp [1].data.f;
2215                         else
2216                                 result = (mono_i)sp [0].data.nati > (mono_i)GET_NATI (sp [1]);
2217                         if (result)
2218                                 ip += broffset;
2219                         BREAK;
2220                 }
2221                 CASE (CEE_BLT) /* Fall through */
2222                 CASE (CEE_BLT_S) {
2223                         int result;
2224                         int broffset;
2225                         if (*ip == CEE_BLT_S) {
2226                                 broffset = (signed char)ip [1];
2227                                 ip += 2;
2228                         } else {
2229                                 broffset = (gint32) read32 (ip + 1);
2230                                 ip += 5;
2231                         }
2232                         sp -= 2;
2233                         if (sp->type == VAL_I32)
2234                                 result = (mono_i)sp[0].data.i < (mono_i)GET_NATI(sp[1]);
2235                         else if (sp->type == VAL_I64)
2236                                 result = sp[0].data.l < sp[1].data.l;
2237                         else if (sp->type == VAL_DOUBLE)
2238                                 result = sp[0].data.f < sp[1].data.f;
2239                         else
2240                                 result = (mono_i)sp[0].data.nati < (mono_i)GET_NATI(sp[1]);
2241                         if (result)
2242                                 ip += broffset;
2243                         BREAK;
2244                 }
2245                 CASE (CEE_BLE) /* fall through */
2246                 CASE (CEE_BLE_S) {
2247                         int result;
2248                         int broffset;
2249                         if (*ip == CEE_BLE_S) {
2250                                 broffset = (signed char)ip [1];
2251                                 ip += 2;
2252                         } else {
2253                                 broffset = (gint32) read32 (ip + 1);
2254                                 ip += 5;
2255                         }
2256                         sp -= 2;
2257
2258                         if (sp->type == VAL_I32)
2259                                 result = (mono_i)sp [0].data.i <= (mono_i)GET_NATI (sp [1]);
2260                         else if (sp->type == VAL_I64)
2261                                 result = sp [0].data.l <= sp [1].data.l;
2262                         else if (sp->type == VAL_DOUBLE)
2263                                 result = sp [0].data.f <= sp [1].data.f;
2264                         else {
2265                                 result = (mono_i)sp [0].data.nati <= (mono_i)GET_NATI (sp [1]);
2266                         }
2267                         if (result)
2268                                 ip += broffset;
2269                         BREAK;
2270                 }
2271                 CASE (CEE_BNE_UN) /* Fall through */
2272                 CASE (CEE_BNE_UN_S) {
2273                         int result;
2274                         int broffset;
2275                         if (*ip == CEE_BNE_UN_S) {
2276                                 broffset = (signed char)ip [1];
2277                                 ip += 2;
2278                         } else {
2279                                 broffset = (gint32) read32 (ip + 1);
2280                                 ip += 5;
2281                         }
2282                         sp -= 2;
2283                         if (sp->type == VAL_I32)
2284                                 result = (mono_u)sp [0].data.i != (mono_u)GET_NATI (sp [1]);
2285                         else if (sp->type == VAL_I64)
2286                                 result = (guint64)sp [0].data.l != (guint64)sp [1].data.l;
2287                         else if (sp->type == VAL_DOUBLE)
2288                                 result = isunordered (sp [0].data.f, sp [1].data.f) ||
2289                                         (sp [0].data.f != sp [1].data.f);
2290                         else
2291                                 result = (mono_u)sp [0].data.nati != (mono_u)GET_NATI (sp [1]);
2292                         if (result)
2293                                 ip += broffset;
2294                         BREAK;
2295                 }
2296                 CASE (CEE_BGE_UN) /* Fall through */
2297                 CASE (CEE_BGE_UN_S) {
2298                         int result;
2299                         int broffset;
2300                         if (*ip == CEE_BGE_UN_S) {
2301                                 broffset = (signed char)ip [1];
2302                                 ip += 2;
2303                         } else {
2304                                 broffset = (gint32) read32 (ip + 1);
2305                                 ip += 5;
2306                         }
2307                         sp -= 2;
2308                         if (sp->type == VAL_I32)
2309                                 result = (mono_u)sp [0].data.i >= (mono_u)GET_NATI (sp [1]);
2310                         else if (sp->type == VAL_I64)
2311                                 result = (guint64)sp [0].data.l >= (guint64)sp [1].data.l;
2312                         else if (sp->type == VAL_DOUBLE)
2313                                 result = !isless (sp [0].data.f,sp [1].data.f);
2314                         else
2315                                 result = (mono_u)sp [0].data.nati >= (mono_u)GET_NATI (sp [1]);
2316                         if (result)
2317                                 ip += broffset;
2318                         BREAK;
2319                 }
2320                 CASE (CEE_BGT_UN) /* Fall through */
2321                 CASE (CEE_BGT_UN_S) {
2322                         int result;
2323                         int broffset;
2324                         if (*ip == CEE_BGT_UN_S) {
2325                                 broffset = (signed char)ip [1];
2326                                 ip += 2;
2327                         } else {
2328                                 broffset = (gint32) read32 (ip + 1);
2329                                 ip += 5;
2330                         }
2331                         sp -= 2;
2332                         if (sp->type == VAL_I32)
2333                                 result = (mono_u)sp [0].data.i > (mono_u)GET_NATI (sp [1]);
2334                         else if (sp->type == VAL_I64)
2335                                 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
2336                         else if (sp->type == VAL_DOUBLE)
2337                                 result = isgreater (sp [0].data.f, sp [1].data.f);
2338                         else
2339                                 result = (mono_u)sp [0].data.nati > (mono_u)GET_NATI (sp [1]);
2340                         if (result)
2341                                 ip += broffset;
2342                         BREAK;
2343                 }
2344                 CASE (CEE_BLE_UN) /* Fall through */
2345                 CASE (CEE_BLE_UN_S) {
2346                         int result;
2347                         int broffset;
2348                         if (*ip == CEE_BLE_UN_S) {
2349                                 broffset = (signed char)ip [1];
2350                                 ip += 2;
2351                         } else {
2352                                 broffset = (gint32) read32 (ip + 1);
2353                                 ip += 5;
2354                         }
2355                         sp -= 2;
2356                         if (sp->type == VAL_I32)
2357                                 result = (mono_u)sp [0].data.i <= (mono_u)GET_NATI (sp [1]);
2358                         else if (sp->type == VAL_I64)
2359                                 result = (guint64)sp [0].data.l <= (guint64)sp [1].data.l;
2360                         else if (sp->type == VAL_DOUBLE)
2361                                 result = islessequal (sp [0].data.f, sp [1].data.f);
2362                         else
2363                                 result = (mono_u)sp [0].data.nati <= (mono_u)GET_NATI (sp [1]);
2364                         if (result)
2365                                 ip += broffset;
2366                         BREAK;
2367                 }
2368                 CASE (CEE_BLT_UN) /* Fall through */
2369                 CASE (CEE_BLT_UN_S) {
2370                         int result;
2371                         int broffset;
2372                         if (*ip == CEE_BLT_UN_S) {
2373                                 broffset = (signed char)ip [1];
2374                                 ip += 2;
2375                         } else {
2376                                 broffset = (gint32) read32 (ip + 1);
2377                                 ip += 5;
2378                         }
2379                         sp -= 2;
2380                         if (sp->type == VAL_I32)
2381                                 result = (mono_u)sp[0].data.i < (mono_u)GET_NATI(sp[1]);
2382                         else if (sp->type == VAL_I64)
2383                                 result = (guint64)sp[0].data.l < (guint64)sp[1].data.l;
2384                         else if (sp->type == VAL_DOUBLE)
2385                                 result = isunordered (sp [0].data.f, sp [1].data.f) ||
2386                                         (sp [0].data.f < sp [1].data.f);
2387                         else
2388                                 result = (mono_u)sp[0].data.nati < (mono_u)GET_NATI(sp[1]);
2389                         if (result)
2390                                 ip += broffset;
2391                         BREAK;
2392                 }
2393                 CASE (CEE_SWITCH) {
2394                         guint32 n;
2395                         const unsigned char *st;
2396                         ++ip;
2397                         n = read32 (ip);
2398                         ip += 4;
2399                         st = ip + sizeof (gint32) * n;
2400                         --sp;
2401                         if ((guint32)sp->data.i < n) {
2402                                 gint offset;
2403                                 ip += sizeof (gint32) * (guint32)sp->data.i;
2404                                 offset = read32 (ip);
2405                                 ip = st + offset;
2406                         } else {
2407                                 ip = st;
2408                         }
2409                         BREAK;
2410                 }
2411                 CASE (CEE_LDIND_I1)
2412                         ++ip;
2413                         sp[-1].type = VAL_I32;
2414                         sp[-1].data.i = *(gint8*)sp[-1].data.p;
2415                         BREAK;
2416                 CASE (CEE_LDIND_U1)
2417                         ++ip;
2418                         sp[-1].type = VAL_I32;
2419                         sp[-1].data.i = *(guint8*)sp[-1].data.p;
2420                         BREAK;
2421                 CASE (CEE_LDIND_I2)
2422                         ++ip;
2423                         sp[-1].type = VAL_I32;
2424                         sp[-1].data.i = *(gint16*)sp[-1].data.p;
2425                         BREAK;
2426                 CASE (CEE_LDIND_U2)
2427                         ++ip;
2428                         sp[-1].type = VAL_I32;
2429                         sp[-1].data.i = *(guint16*)sp[-1].data.p;
2430                         BREAK;
2431                 CASE (CEE_LDIND_I4) /* Fall through */
2432                 CASE (CEE_LDIND_U4)
2433                         ++ip;
2434                         sp[-1].type = VAL_I32;
2435                         sp[-1].data.i = *(gint32*)sp[-1].data.p;
2436                         BREAK;
2437                 CASE (CEE_LDIND_I8)
2438                         ++ip;
2439                         sp[-1].type = VAL_I64;
2440                         sp[-1].data.l = *(gint64*)sp[-1].data.p;
2441                         BREAK;
2442                 CASE (CEE_LDIND_I)
2443                         ++ip;
2444                         sp[-1].type = VAL_NATI;
2445                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2446                         BREAK;
2447                 CASE (CEE_LDIND_R4)
2448                         ++ip;
2449                         sp[-1].type = VAL_DOUBLE;
2450                         sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2451                         BREAK;
2452                 CASE (CEE_LDIND_R8)
2453                         ++ip;
2454                         sp[-1].type = VAL_DOUBLE;
2455                         sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2456                         BREAK;
2457                 CASE (CEE_LDIND_REF)
2458                         ++ip;
2459                         sp[-1].type = VAL_OBJ;
2460                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2461                         BREAK;
2462                 CASE (CEE_STIND_REF) {
2463                         gpointer *p;
2464                         ++ip;
2465                         sp -= 2;
2466                         p = sp->data.p;
2467                         *p = sp[1].data.p;
2468                         BREAK;
2469                 }
2470                 CASE (CEE_STIND_I1) {
2471                         gint8 *p;
2472                         ++ip;
2473                         sp -= 2;
2474                         p = sp->data.p;
2475                         *p = (gint8)sp[1].data.i;
2476                         BREAK;
2477                 }
2478                 CASE (CEE_STIND_I2) {
2479                         gint16 *p;
2480                         ++ip;
2481                         sp -= 2;
2482                         p = sp->data.p;
2483                         *p = (gint16)sp[1].data.i;
2484                         BREAK;
2485                 }
2486                 CASE (CEE_STIND_I4) {
2487                         gint32 *p;
2488                         ++ip;
2489                         sp -= 2;
2490                         p = sp->data.p;
2491                         *p = sp[1].data.i;
2492                         BREAK;
2493                 }
2494                 CASE (CEE_STIND_I) {
2495                         mono_i *p;
2496                         ++ip;
2497                         sp -= 2;
2498                         p = sp->data.p;
2499                         *p = (mono_i)sp[1].data.p;
2500                         BREAK;
2501                 }
2502                 CASE (CEE_STIND_I8) {
2503                         gint64 *p;
2504                         ++ip;
2505                         sp -= 2;
2506                         p = sp->data.p;
2507                         *p = sp[1].data.l;
2508                         BREAK;
2509                 }
2510                 CASE (CEE_STIND_R4) {
2511                         float *p;
2512                         ++ip;
2513                         sp -= 2;
2514                         p = sp->data.p;
2515                         *p = (gfloat)sp[1].data.f;
2516                         BREAK;
2517                 }
2518                 CASE (CEE_STIND_R8) {
2519                         double *p;
2520                         ++ip;
2521                         sp -= 2;
2522                         p = sp->data.p;
2523                         *p = sp[1].data.f;
2524                         BREAK;
2525                 }
2526                 CASE (CEE_ADD)
2527                         ++ip;
2528                         --sp;
2529                         /* should probably consider the pointers as unsigned */
2530                         if (sp->type == VAL_I32) {
2531                                 if (sp [-1].type == VAL_I32)
2532                                         sp [-1].data.i += sp [0].data.i;
2533                                 else
2534                                         sp [-1].data.nati = sp [-1].data.nati + sp [0].data.i;
2535                         } else if (sp->type == VAL_I64)
2536                                 sp [-1].data.l += sp [0].data.l;
2537                         else if (sp->type == VAL_DOUBLE)
2538                                 sp [-1].data.f += sp [0].data.f;
2539                         else {
2540                                 if (sp [-1].type == VAL_I32) {
2541                                         sp [-1].data.nati = sp [-1].data.i + sp [0].data.nati;
2542                                         sp [-1].type = sp [0].type;
2543                                 } else
2544                                         sp [-1].data.nati = sp [-1].data.nati + sp [0].data.nati;
2545                         }
2546                         BREAK;
2547                 CASE (CEE_SUB)
2548                         ++ip;
2549                         --sp;
2550                         /* should probably consider the pointers as unsigned */
2551                         if (sp->type == VAL_I32) {
2552                                 if (sp [-1].type == VAL_I32)
2553                                         sp [-1].data.i -= sp [0].data.i;
2554                                 else {
2555                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.i;
2556                                         sp [-1].type = VAL_NATI;
2557                                 }
2558                         } else if (sp->type == VAL_I64)
2559                                 sp [-1].data.l -= sp [0].data.l;
2560                         else if (sp->type == VAL_DOUBLE)
2561                                 sp [-1].data.f -= sp [0].data.f;
2562                         else {
2563                                 if (sp [-1].type == VAL_I32) {
2564                                         sp [-1].data.nati = sp [-1].data.i - sp [0].data.nati;
2565                                         sp [-1].type = sp [0].type;
2566                                 } else
2567                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.nati;
2568                         }
2569                         BREAK;
2570                 CASE (CEE_MUL)
2571                         ++ip;
2572                         --sp;
2573                         if (sp->type == VAL_I32) {
2574                                 if (sp [-1].type == VAL_I32)
2575                                         sp [-1].data.i *= sp [0].data.i;
2576                                 else
2577                                         sp [-1].data.nati = sp [-1].data.nati * sp [0].data.i;
2578                         } else if (sp->type == VAL_I64)
2579                                 sp [-1].data.l *= sp [0].data.l;
2580                         else if (sp->type == VAL_DOUBLE)
2581                                 sp [-1].data.f *= sp [0].data.f;
2582                         else {
2583                                 if (sp [-1].type == VAL_NATI)
2584                                         sp [-1].data.nati = sp [-1].data.nati * sp [0].data.nati;
2585                                 else {
2586                                         sp [-1].data.nati = sp [-1].data.i * sp [0].data.nati;
2587                                         sp [-1].type = VAL_NATI;
2588                                 }
2589                         }
2590                         BREAK;
2591                 CASE (CEE_DIV)
2592                         ++ip;
2593                         --sp;
2594                         if (sp->type == VAL_I32) {
2595                                 if (sp [0].data.i == 0)
2596                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2597                                 if (sp [-1].type == VAL_I32)
2598                                         sp [-1].data.i /= sp [0].data.i;
2599                                 else
2600                                         sp [-1].data.nati /= sp [0].data.i;
2601                         } else if (sp->type == VAL_I64) {
2602                                 if (sp [0].data.l == 0)
2603                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2604                                 sp [-1].data.l /= sp [0].data.l;
2605                         } else if (sp->type == VAL_DOUBLE) {
2606                                 /* set NaN is divisor is 0.0 */
2607                                 sp [-1].data.f /= sp [0].data.f;
2608                         } else {
2609                                 if (sp [-1].type == VAL_NATI)
2610                                         sp [-1].data.nati = sp [-1].data.nati / sp [0].data.nati;
2611                                 else {
2612                                         sp [-1].data.nati = sp [-1].data.i / sp [0].data.nati;
2613                                         sp [-1].type = VAL_NATI;
2614                                 }
2615                         }
2616                         BREAK;
2617                 CASE (CEE_DIV_UN)
2618                         ++ip;
2619                         --sp;
2620                         if (sp->type == VAL_I32) {
2621                                 if (sp [0].data.i == 0)
2622                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2623                                 if (sp [-1].type == VAL_NATI)
2624                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati / (mono_u)sp [0].data.i;
2625                                 else
2626                                         sp [-1].data.i = (guint32)sp [-1].data.i / (guint32)sp [0].data.i;
2627                         } else if (sp->type == VAL_I64) {
2628                                 guint64 val;
2629                                 if (sp [0].data.l == 0)
2630                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2631                                 val = sp [-1].data.l;
2632                                 val /= (guint64)sp [0].data.l;
2633                                 sp [-1].data.l = val;
2634                         } else {
2635                                 if (sp [0].data.nati == 0)
2636                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2637                                 if (sp [-1].type == VAL_NATI)
2638                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati / (mono_u)sp [0].data.nati;
2639                                 else {
2640                                         sp [-1].data.nati = (mono_u)sp [-1].data.i / (mono_u)sp [0].data.nati;
2641                                         sp [-1].type = VAL_NATI;
2642                                 }
2643                         }
2644                         BREAK;
2645                 CASE (CEE_REM)
2646                         ++ip;
2647                         --sp;
2648                         if (sp->type == VAL_I32) {
2649                                 if (sp [0].data.i == 0)
2650                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2651                                 if (sp [-1].type == VAL_NATI)
2652                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati % (mono_i)sp [0].data.i;
2653                                 else
2654                                         sp [-1].data.i = sp [-1].data.i % sp [0].data.i;
2655                         } else if (sp->type == VAL_I64) {
2656                                 if (sp [0].data.l == 0)
2657                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2658                                 sp [-1].data.l %= sp [0].data.l;
2659                         } else if (sp->type == VAL_DOUBLE) {
2660                                 /* FIXME: what do we actually do here? */
2661                                 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2662                         } else {
2663                                 if (sp [0].data.nati == 0)
2664                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2665                                 if (sp [-1].type == VAL_NATI)
2666                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati % (mono_i)sp [0].data.nati;
2667                                 else {
2668                                         sp [-1].data.nati = (mono_i)sp [-1].data.i % (mono_i)sp [0].data.nati;
2669                                         sp [-1].type = VAL_NATI;
2670                                 }
2671                         }
2672                         BREAK;
2673                 CASE (CEE_REM_UN)
2674                         ++ip;
2675                         --sp;
2676                         if (sp->type == VAL_I32) {
2677                                 if (sp [0].data.i == 0)
2678                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2679                                 if (sp [-1].type == VAL_NATI)
2680                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati % (mono_u)sp [0].data.i;
2681                                 else
2682                                         sp [-1].data.i = (guint32)sp [-1].data.i % (guint32)sp [0].data.i;
2683                         } else if (sp->type == VAL_I64) {
2684                                 if (sp [0].data.l == 0)
2685                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2686                                 sp [-1].data.l = (guint64)sp [-1].data.l % (guint64)sp [0].data.l;
2687                         } else if (sp->type == VAL_DOUBLE) {
2688                                 /* unspecified behaviour according to the spec */
2689                         } else {
2690                                 if (sp [0].data.nati == 0)
2691                                         THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2692                                 if (sp [-1].type == VAL_NATI)
2693                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati % (mono_u)sp [0].data.nati;
2694                                 else {
2695                                         sp [-1].data.nati = (mono_u)sp [-1].data.i % (mono_u)sp [0].data.nati;
2696                                         sp [-1].type = VAL_NATI;
2697                                 }
2698                         }
2699                         BREAK;
2700                 CASE (CEE_AND)
2701                         ++ip;
2702                         --sp;
2703                         if (sp->type == VAL_I32) {
2704                                 if (sp [-1].type == VAL_NATI)
2705                                         sp [-1].data.nati &= sp [0].data.i;
2706                                 else
2707                                         sp [-1].data.i &= sp [0].data.i;
2708                         }
2709                         else if (sp->type == VAL_I64)
2710                                 sp [-1].data.l &= sp [0].data.l;
2711                         else {
2712                                 if (sp [-1].type == VAL_NATI)
2713                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati & (mono_i)sp [0].data.nati;
2714                                 else {
2715                                         sp [-1].data.nati = (mono_i)sp [-1].data.i & (mono_i)sp [0].data.nati;
2716                                         sp [-1].type = VAL_NATI;
2717                                 }
2718                         }
2719                         BREAK;
2720                 CASE (CEE_OR)
2721                         ++ip;
2722                         --sp;
2723                         if (sp->type == VAL_I32) {
2724                                 if (sp [-1].type == VAL_NATI)
2725                                         sp [-1].data.nati |= sp [0].data.i;
2726                                 else
2727                                         sp [-1].data.i |= sp [0].data.i;
2728                         }
2729                         else if (sp->type == VAL_I64)
2730                                 sp [-1].data.l |= sp [0].data.l;
2731                         else {
2732                                 if (sp [-1].type == VAL_NATI)
2733                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati | (mono_i)sp [0].data.nati;
2734                                 else {
2735                                         sp [-1].data.nati = (mono_i)sp [-1].data.i | (mono_i)sp [0].data.nati;
2736                                         sp [-1].type = VAL_NATI;
2737                                 }
2738                         }
2739                         BREAK;
2740                 CASE (CEE_XOR)
2741                         ++ip;
2742                         --sp;
2743                         if (sp->type == VAL_I32) {
2744                                 if (sp [-1].type == VAL_NATI)
2745                                         sp [-1].data.nati ^= sp [0].data.i;
2746                                 else
2747                                         sp [-1].data.i ^= sp [0].data.i;
2748                         }
2749                         else if (sp->type == VAL_I64)
2750                                 sp [-1].data.l ^= sp [0].data.l;
2751                         else {
2752                                 if (sp [-1].type == VAL_NATI)
2753                                         sp [-1].data.nati = (mono_i)sp [-1].data.nati ^ (mono_i)sp [0].data.nati;
2754                                 else {
2755                                         sp [-1].data.nati = (mono_i)sp [-1].data.i ^ (mono_i)sp [0].data.nati;
2756                                         sp [-1].type = VAL_NATI;
2757                                 }
2758                         }
2759                         BREAK;
2760                 CASE (CEE_SHL)
2761                         ++ip;
2762                         --sp;
2763                         if (sp [-1].type == VAL_I32)
2764                                 sp [-1].data.i <<= GET_NATI (sp [0]);
2765                         else if (sp [-1].type == VAL_I64)
2766                                 sp [-1].data.l <<= GET_NATI (sp [0]);
2767                         else
2768                                 sp [-1].data.nati <<= GET_NATI (sp [0]);
2769                         BREAK;
2770                 CASE (CEE_SHR)
2771                         ++ip;
2772                         --sp;
2773                         if (sp [-1].type == VAL_I32)
2774                                 sp [-1].data.i >>= GET_NATI (sp [0]);
2775                         else if (sp [-1].type == VAL_I64)
2776                                 sp [-1].data.l >>= GET_NATI (sp [0]);
2777                         else
2778                                 sp [-1].data.nati = ((mono_i)sp [-1].data.nati) >> GET_NATI (sp [0]);
2779                         BREAK;
2780                 CASE (CEE_SHR_UN)
2781                         ++ip;
2782                         --sp;
2783                         if (sp [-1].type == VAL_I32)
2784                                 sp [-1].data.i = (guint)sp [-1].data.i >> GET_NATI (sp [0]);
2785                         else if (sp [-1].type == VAL_I64)
2786                                 sp [-1].data.l = (guint64)sp [-1].data.l >> GET_NATI (sp [0]);
2787                         else
2788                                 sp [-1].data.nati = ((mono_u)sp[-1].data.nati) >> GET_NATI (sp [0]);
2789                         BREAK;
2790                 CASE (CEE_NEG)
2791                         ++ip;
2792                         --sp;
2793                         if (sp->type == VAL_I32) 
2794                                 sp->data.i = - sp->data.i;
2795                         else if (sp->type == VAL_I64)
2796                                 sp->data.l = - sp->data.l;
2797                         else if (sp->type == VAL_DOUBLE)
2798                                 sp->data.f = - sp->data.f;
2799                         else if (sp->type == VAL_NATI)
2800                                 sp->data.nati = - (mono_i)sp->data.nati;
2801                         ++sp;
2802                         BREAK;
2803                 CASE (CEE_NOT)
2804                         ++ip;
2805                         --sp;
2806                         if (sp->type == VAL_I32)
2807                                 sp->data.i = ~ sp->data.i;
2808                         else if (sp->type == VAL_I64)
2809                                 sp->data.l = ~ sp->data.l;
2810                         else if (sp->type == VAL_NATI)
2811                                 sp->data.nati = ~ (mono_i)sp->data.p;
2812                         ++sp;
2813                         BREAK;
2814                 CASE (CEE_CONV_U1) {
2815                         ++ip;
2816                         switch (sp [-1].type) {
2817                         case VAL_DOUBLE:
2818                                 sp [-1].data.i = (guint8)sp [-1].data.f;
2819                                 break;
2820                         case VAL_I64:
2821                                 sp [-1].data.i = (guint8)sp [-1].data.l;
2822                                 break;
2823                         case VAL_VALUET:
2824                                 ves_abort();
2825                         case VAL_I32:
2826                                 sp [-1].data.i = (guint8)sp [-1].data.i;
2827                                 break;
2828                         default:
2829                                 sp [-1].data.i = (guint8)sp [-1].data.nati;
2830                                 break;
2831                         }
2832                         sp [-1].type = VAL_I32;
2833                         BREAK;
2834                 }
2835                 CASE (CEE_CONV_I1) {
2836                         ++ip;
2837                         switch (sp [-1].type) {
2838                         case VAL_DOUBLE:
2839                                 sp [-1].data.i = (gint8)sp [-1].data.f;
2840                                 break;
2841                         case VAL_I64:
2842                                 sp [-1].data.i = (gint8)sp [-1].data.l;
2843                                 break;
2844                         case VAL_VALUET:
2845                                 ves_abort();
2846                         case VAL_I32:
2847                                 sp [-1].data.i = (gint8)sp [-1].data.i;
2848                                 break;
2849                         default:
2850                                 sp [-1].data.i = (gint8)sp [-1].data.nati;
2851                                 break;
2852                         }
2853                         sp [-1].type = VAL_I32;
2854                         BREAK;
2855                 }
2856                 CASE (CEE_CONV_U2) {
2857                         ++ip;
2858                         switch (sp [-1].type) {
2859                         case VAL_DOUBLE:
2860                                 sp [-1].data.i = (guint16)sp [-1].data.f;
2861                                 break;
2862                         case VAL_I64:
2863                                 sp [-1].data.i = (guint16)sp [-1].data.l;
2864                                 break;
2865                         case VAL_VALUET:
2866                                 ves_abort();
2867                         case VAL_I32:
2868                                 sp [-1].data.i = (guint16)sp [-1].data.i;
2869                                 break;
2870                         default:
2871                                 sp [-1].data.i = (guint16)sp [-1].data.nati;
2872                                 break;
2873                         }
2874                         sp [-1].type = VAL_I32;
2875                         BREAK;
2876                 }
2877                 CASE (CEE_CONV_I2) {
2878                         ++ip;
2879                         switch (sp [-1].type) {
2880                         case VAL_DOUBLE:
2881                                 sp [-1].data.i = (gint16)sp [-1].data.f;
2882                                 break;
2883                         case VAL_I64:
2884                                 sp [-1].data.i = (gint16)sp [-1].data.l;
2885                                 break;
2886                         case VAL_VALUET:
2887                                 ves_abort();
2888                         case VAL_I32:
2889                                 sp [-1].data.i = (gint16)sp [-1].data.i;
2890                                 break;
2891                         default:
2892                                 sp [-1].data.i = (gint16)sp [-1].data.nati;
2893                                 break;
2894                         }
2895                         sp [-1].type = VAL_I32;
2896                         BREAK;
2897                 }
2898                 CASE (CEE_CONV_U4) /* Fall through */
2899 #if SIZEOF_VOID_P == 4
2900                 CASE (CEE_CONV_I) /* Fall through */
2901                 CASE (CEE_CONV_U) /* Fall through */
2902 #endif
2903                 CASE (CEE_CONV_I4) {
2904                         ++ip;
2905                         switch (sp [-1].type) {
2906                         case VAL_DOUBLE:
2907                                 sp [-1].data.i = (gint32)sp [-1].data.f;
2908                                 break;
2909                         case VAL_I64:
2910                                 sp [-1].data.i = (gint32)sp [-1].data.l;
2911                                 break;
2912                         case VAL_VALUET:
2913                                 ves_abort();
2914                         case VAL_I32:
2915                                 break;
2916                         default:
2917                                 sp [-1].data.i = (gint32)sp [-1].data.p;
2918                                 break;
2919                         }
2920                         sp [-1].type = VAL_I32;
2921                         BREAK;
2922                 }
2923 #if SIZEOF_VOID_P == 8
2924                 CASE (CEE_CONV_I) /* Fall through */
2925 #endif
2926                 CASE (CEE_CONV_I8)
2927                         ++ip;
2928                         switch (sp [-1].type) {
2929                         case VAL_DOUBLE:
2930                                 sp [-1].data.l = (gint64)sp [-1].data.f;
2931                                 break;
2932                         case VAL_I64:
2933                                 break;
2934                         case VAL_VALUET:
2935                                 ves_abort();
2936                         case VAL_I32:
2937                                 sp [-1].data.l = (gint64)sp [-1].data.i;
2938                                 break;
2939                         default:
2940                                 sp [-1].data.l = (gint64)sp [-1].data.nati;
2941                                 break;
2942                         }
2943                         sp [-1].type = ip[-1] == CEE_CONV_I ? VAL_NATI : VAL_I64;
2944                         BREAK;
2945                 CASE (CEE_CONV_R4) {
2946                         ++ip;
2947                         switch (sp [-1].type) {
2948                         case VAL_DOUBLE:
2949                                 sp [-1].data.f = (float)sp [-1].data.f;
2950                                 break;
2951                         case VAL_I64:
2952                                 sp [-1].data.f = (float)sp [-1].data.l;
2953                                 break;
2954                         case VAL_VALUET:
2955                                 ves_abort();
2956                         case VAL_I32:
2957                                 sp [-1].data.f = (float)sp [-1].data.i;
2958                                 break;
2959                         default:
2960                                 sp [-1].data.f = (float)sp [-1].data.nati;
2961                                 break;
2962                         }
2963                         sp [-1].type = VAL_DOUBLE;
2964                         BREAK;
2965                 }
2966                 CASE (CEE_CONV_R8) {
2967                         ++ip;
2968                         switch (sp [-1].type) {
2969                         case VAL_DOUBLE:
2970                                 sp [-1].data.f = (double)sp [-1].data.f;
2971                                 break;
2972                         case VAL_I64:
2973                                 sp [-1].data.f = (double)sp [-1].data.l;
2974                                 break;
2975                         case VAL_VALUET:
2976                                 ves_abort();
2977                         case VAL_I32:
2978                                 sp [-1].data.f = (double)sp [-1].data.i;
2979                                 break;
2980                         default:
2981                                 sp [-1].data.f = (double)sp [-1].data.nati;
2982                                 break;
2983                         }
2984                         sp [-1].type = VAL_DOUBLE;
2985                         BREAK;
2986                 }
2987 #if SIZEOF_VOID_P == 8
2988                 CASE (CEE_CONV_U) /* Fall through */
2989 #endif
2990                 CASE (CEE_CONV_U8)
2991                         ++ip;
2992
2993                         switch (sp [-1].type){
2994                         case VAL_DOUBLE:
2995                                 sp [-1].data.l = (guint64)sp [-1].data.f;
2996                                 break;
2997                         case VAL_I64:
2998                                 break;
2999                         case VAL_VALUET:
3000                                 ves_abort();
3001                         case VAL_I32:
3002                                 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
3003                                 break;
3004                         default:
3005                                 sp [-1].data.l = (guint64) sp [-1].data.nati;
3006                                 break;
3007                         }
3008                         sp [-1].type = ip[-1] == CEE_CONV_U ? VAL_NATI : VAL_I64;
3009                         BREAK;
3010                 CASE (CEE_CPOBJ) {
3011                         MonoClass *vtklass;
3012                         ++ip;
3013                         vtklass = mono_class_get (image, read32 (ip));
3014                         ip += 4;
3015                         sp -= 2;
3016                         memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
3017                         BREAK;
3018                 }
3019                 CASE (CEE_LDOBJ) {
3020                         guint32 token;
3021                         MonoClass *c;
3022                         char *addr;
3023
3024                         ++ip;
3025                         token = read32 (ip);
3026                         ip += 4;
3027
3028                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3029                                 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3030                         else
3031                                 c = mono_class_get (image, token);
3032
3033                         addr = sp [-1].data.vt;
3034                         vt_alloc (&c->byval_arg, &sp [-1], FALSE);
3035                         stackval_from_data (&c->byval_arg, &sp [-1], addr, FALSE);
3036                         BREAK;
3037                 }
3038                 CASE (CEE_LDSTR) {
3039                         MonoObject *o;
3040                         guint32 str_index;
3041
3042                         ip++;
3043                         str_index = mono_metadata_token_index (read32 (ip));
3044                         ip += 4;
3045
3046                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE) {
3047                                 o = (MonoObject *)mono_string_new_wrapper(
3048                                         mono_method_get_wrapper_data (frame->method, str_index));
3049                         }
3050                         else
3051                                 o = (MonoObject*)mono_ldstr (domain, image, str_index);
3052                         sp->type = VAL_OBJ;
3053                         sp->data.p = o;
3054
3055                         ++sp;
3056                         BREAK;
3057                 }
3058                 CASE (CEE_NEWOBJ) {
3059                         MonoObject *o;
3060                         MonoClass *newobj_class;
3061                         MonoMethodSignature *csig;
3062                         stackval valuetype_this;
3063                         stackval *endsp = sp;
3064                         guint32 token;
3065                         stackval retval;
3066
3067                         frame->ip = ip;
3068
3069                         ip++;
3070                         token = read32 (ip);
3071                         ip += 4;
3072
3073                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3074                                 child_frame.method = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
3075                         else 
3076                                 child_frame.method = mono_get_method (image, token, NULL);
3077                         if (!child_frame.method)
3078                                 THROW_EX (mono_get_exception_missing_method (), ip -5);
3079
3080                         csig = child_frame.method->signature;
3081                         newobj_class = child_frame.method->klass;
3082                         /*if (profiling_classes) {
3083                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
3084                                 count++;
3085                                 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
3086                         }*/
3087                                 
3088
3089                         if (newobj_class->parent == mono_defaults.array_class) {
3090                                 sp -= csig->param_count;
3091                                 o = ves_array_create (domain, newobj_class, csig, sp);
3092                                 goto array_constructed;
3093                         }
3094
3095                         /*
3096                          * First arg is the object.
3097                          */
3098                         if (newobj_class->valuetype) {
3099                                 void *zero;
3100                                 vt_alloc (&newobj_class->byval_arg, &valuetype_this, csig->pinvoke);
3101                                 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
3102                                         zero = valuetype_this.data.vt;
3103                                         child_frame.obj = valuetype_this.data.vt;
3104                                 } else {
3105                                         memset (&valuetype_this, 0, sizeof (stackval));
3106                                         zero = &valuetype_this;
3107                                         child_frame.obj = &valuetype_this;
3108                                 }
3109                                 stackval_from_data (&newobj_class->byval_arg, &valuetype_this, zero, csig->pinvoke);
3110                         } else {
3111                                 if (newobj_class != mono_defaults.string_class) {
3112                                         o = mono_object_new (domain, newobj_class);
3113                                         child_frame.obj = o;
3114                                 } else {
3115                                         child_frame.retval = &retval;
3116                                 }
3117                         }
3118
3119                         if (csig->param_count) {
3120                                 sp -= csig->param_count;
3121                                 child_frame.stack_args = sp;
3122                         } else {
3123                                 child_frame.stack_args = NULL;
3124                         }
3125
3126                         g_assert (csig->call_convention == MONO_CALL_DEFAULT);
3127
3128                         child_frame.ip = NULL;
3129                         child_frame.ex = NULL;
3130                         child_frame.ex_handler = NULL;
3131
3132                         ves_exec_method_with_context (&child_frame, context);
3133
3134                         context->current_frame = frame;
3135
3136                         while (endsp > sp) {
3137                                 --endsp;
3138                                 vt_free (endsp);
3139                         }
3140
3141                         if (child_frame.ex) {
3142                                 /*
3143                                  * An exception occurred, need to run finally, fault and catch handlers..
3144                                  */
3145                                 frame->ex = child_frame.ex;
3146                                 goto handle_finally;
3147                         }
3148                         /*
3149                          * a constructor returns void, but we need to return the object we created
3150                          */
3151 array_constructed:
3152                         if (newobj_class->valuetype && !newobj_class->enumtype) {
3153                                 *sp = valuetype_this;
3154                         } else if (newobj_class == mono_defaults.string_class) {
3155                                 *sp = retval;
3156                         } else {
3157                                 sp->type = VAL_OBJ;
3158                                 sp->data.p = o;
3159                         }
3160                         ++sp;
3161                         BREAK;
3162                 }
3163                 CASE (CEE_CASTCLASS) /* Fall through */
3164                 CASE (CEE_ISINST) {
3165                         MonoObject *o;
3166                         MonoClass *c;
3167                         guint32 token;
3168                         int do_isinst = *ip == CEE_ISINST;
3169
3170                         ++ip;
3171                         token = read32 (ip);
3172                         c = mono_class_get (image, token);
3173
3174                         g_assert (sp [-1].type == VAL_OBJ);
3175
3176                         if ((o = sp [-1].data.p)) {
3177                                 if (!mono_object_isinst (o, c)) {
3178                                         if (do_isinst) {
3179                                                 sp [-1].data.p = NULL;
3180                                         } else
3181                                                 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
3182                                 }
3183                         }
3184                         ip += 4;
3185                         BREAK;
3186                 }
3187                 CASE (CEE_CONV_R_UN)
3188                         ++ip;
3189                         switch (sp [-1].type) {
3190                         case VAL_DOUBLE:
3191                                 break;
3192                         case VAL_I64:
3193                                 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
3194                                 break;
3195                         case VAL_VALUET:
3196                                 ves_abort();
3197                         case VAL_I32:
3198                                 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
3199                                 break;
3200                         default:
3201                                 sp [-1].data.f = (double)(guint64)sp [-1].data.nati;
3202                                 break;
3203                         }
3204                         sp [-1].type = VAL_DOUBLE;
3205                         BREAK;
3206                 CASE (CEE_UNUSED58)
3207                 CASE (CEE_UNUSED1) ves_abort(); BREAK;
3208                 CASE (CEE_UNBOX) {
3209                         MonoObject *o;
3210                         MonoClass *c;
3211                         guint32 token;
3212
3213                         ++ip;
3214                         token = read32 (ip);
3215                         
3216                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3217                                 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3218                         else 
3219                                 c = mono_class_get (image, token);
3220                         
3221                         o = sp [-1].data.p;
3222                         if (!o)
3223                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3224
3225                         if (!(mono_object_isinst (o, c) || 
3226                                   ((o->vtable->klass->rank == 0) && 
3227                                    (o->vtable->klass->element_class == c->element_class))))
3228                                 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
3229
3230                         sp [-1].type = VAL_MP;
3231                         sp [-1].data.p = (char *)o + sizeof (MonoObject);
3232
3233                         ip += 4;
3234                         BREAK;
3235                 }
3236                 CASE (CEE_THROW)
3237                         --sp;
3238                         frame->ex_handler = NULL;
3239                         if (!sp->data.p)
3240                                 sp->data.p = mono_get_exception_null_reference ();
3241                         THROW_EX ((MonoException *)sp->data.p, ip);
3242                         BREAK;
3243                 CASE (CEE_LDFLDA) /* Fall through */
3244                 CASE (CEE_LDFLD) {
3245                         MonoObject *obj;
3246                         MonoClassField *field;
3247                         guint32 token;
3248                         int load_addr = *ip == CEE_LDFLDA;
3249                         char *addr;
3250
3251                         if (!sp [-1].data.p)
3252                                 THROW_EX (mono_get_exception_null_reference (), ip);
3253                         
3254                         ++ip;
3255                         token = read32 (ip);
3256                         field = rtd->field_info[token].field;
3257                         ip += 4;
3258
3259                         if (sp [-1].type == VAL_OBJ) {
3260                                 obj = sp [-1].data.p;
3261                                 if (obj->vtable->klass == mono_defaults.transparent_proxy_class && field->parent->marshalbyref) {
3262                                         MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
3263                                         addr = mono_load_remote_field (obj, klass, field, NULL);
3264                                 } else {
3265                                         addr = (char*)obj + field->offset;
3266                                 }                               
3267                         } else {
3268                                 obj = sp [-1].data.vt;
3269                                 addr = (char*)obj + field->offset - sizeof (MonoObject);
3270                         }
3271
3272                         if (load_addr) {
3273                                 sp [-1].type = VAL_MP;
3274                                 sp [-1].data.p = addr;
3275                         } else {
3276                                 vt_alloc (field->type, &sp [-1], FALSE);
3277                                 stackval_from_data (field->type, &sp [-1], addr, FALSE);                                
3278                         }
3279                         BREAK;
3280                 }
3281                 CASE (CEE_STFLD) {
3282                         MonoObject *obj;
3283                         MonoClassField *field;
3284                         guint32 token, offset;
3285
3286                         sp -= 2;
3287                         
3288                         if (!sp [0].data.p)
3289                                 THROW_EX (mono_get_exception_null_reference (), ip);
3290                         
3291                         ++ip;
3292                         token = read32 (ip);
3293                         field = rtd->field_info[token].field;
3294                         ip += 4;
3295                         
3296                         if (sp [0].type == VAL_OBJ) {
3297                                 obj = sp [0].data.p;
3298
3299                                 if (obj->vtable->klass == mono_defaults.transparent_proxy_class && field->parent->marshalbyref) {
3300                                         MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
3301                                         mono_store_remote_field (obj, klass, field, &sp [1].data);
3302                                 } else {
3303                                         offset = field->offset;
3304                                         stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
3305                                         vt_free (&sp [1]);
3306                                 }
3307                         } else {
3308                                 obj = sp [0].data.vt;
3309                                 offset = field->offset - sizeof (MonoObject);
3310                                 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
3311                                 vt_free (&sp [1]);
3312                         }
3313
3314                         BREAK;
3315                 }
3316                 CASE (CEE_LDSFLD) /* Fall through */
3317                 CASE (CEE_LDSFLDA) {
3318                         MonoVTable *vt;
3319                         MonoClassField *field;
3320                         guint32 token;
3321                         int load_addr = *ip == CEE_LDSFLDA;
3322                         gpointer addr;
3323
3324                         ++ip;
3325                         token = read32 (ip);
3326                         field = rtd->field_info[token].field;
3327                         ip += 4;
3328                                                 
3329                         vt = mono_class_vtable (domain, field->parent);
3330                         if (!vt->initialized)
3331                                 mono_runtime_class_init (vt);
3332                         
3333                         if (domain->special_static_fields && (addr = g_hash_table_lookup (domain->special_static_fields, field)))
3334                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3335                         else
3336                                 addr = (char*)(vt->data) + field->offset;
3337
3338                         if (load_addr) {
3339                                 sp->type = VAL_MP;
3340                                 sp->data.p = addr;
3341                         } else {
3342                                 vt_alloc (field->type, sp, FALSE);
3343                                 stackval_from_data (field->type, sp, addr, FALSE);
3344                         }
3345                         ++sp;
3346                         BREAK;
3347                 }
3348                 CASE (CEE_STSFLD) {
3349                         MonoVTable *vt;
3350                         MonoClassField *field;
3351                         guint32 token;
3352                         gpointer addr;
3353
3354                         ++ip;
3355                         token = read32 (ip);
3356                         field = rtd->field_info[token].field;
3357                         ip += 4;
3358                         --sp;
3359
3360                         vt = mono_class_vtable (domain, field->parent);
3361                         if (!vt->initialized)
3362                                 mono_runtime_class_init (vt);
3363                         
3364                         if (domain->special_static_fields && (addr = g_hash_table_lookup (domain->special_static_fields, field)))
3365                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3366                         else
3367                                 addr = (char*)(vt->data) + field->offset;
3368
3369                         stackval_to_data (field->type, sp, addr, FALSE);
3370                         vt_free (sp);
3371                         BREAK;
3372                 }
3373                 CASE (CEE_STOBJ) {
3374                         MonoClass *vtklass;
3375                         ++ip;
3376                         vtklass = mono_class_get (image, read32 (ip));
3377                         ip += 4;
3378                         sp -= 2;
3379
3380                         /*
3381                          * LAMESPEC: According to the spec, the stack should contain a 
3382                          * pointer to a value type. In reality, it can contain anything.
3383                          */
3384                         if (sp [1].type == VAL_VALUET)
3385                                 memcpy (sp [0].data.p, sp [1].data.vt, mono_class_value_size (vtklass, NULL));
3386                         else
3387                                 memcpy (sp [0].data.p, &sp [1].data, mono_class_value_size (vtklass, NULL));
3388                         BREAK;
3389                 }
3390 #if SIZEOF_VOID_P == 8
3391                 CASE (CEE_CONV_OVF_I_UN)
3392 #endif
3393                 CASE (CEE_CONV_OVF_I8_UN) {
3394                         switch (sp [-1].type) {
3395                         case VAL_DOUBLE:
3396                                 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3397                                         THROW_EX (mono_get_exception_overflow (), ip);
3398                                 sp [-1].data.l = (guint64)sp [-1].data.f;
3399                                 break;
3400                         case VAL_I64:
3401                                 break;
3402                         case VAL_VALUET:
3403                                 ves_abort();
3404                         case VAL_I32:
3405                                 /* Can't overflow */
3406                                 sp [-1].data.l = (guint64)sp [-1].data.i;
3407                                 break;
3408                         default:
3409                                 sp [-1].data.l = (guint64)sp [-1].data.nati;
3410                                 break;
3411                         }
3412                         sp [-1].type = VAL_I64;
3413                         ++ip;
3414                         BREAK;
3415                 }
3416 #if SIZEOF_VOID_P == 8
3417                 CASE (CEE_CONV_OVF_U_UN) 
3418 #endif
3419                 CASE (CEE_CONV_OVF_U8_UN) {
3420                         switch (sp [-1].type) {
3421                         case VAL_DOUBLE:
3422                                 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT64_MAX)
3423                                         THROW_EX (mono_get_exception_overflow (), ip);
3424                                 sp [-1].data.l = (guint64)sp [-1].data.f;
3425                                 break;
3426                         case VAL_I64:
3427                                 /* nothing to do */
3428                                 break;
3429                         case VAL_VALUET:
3430                                 ves_abort();
3431                         case VAL_I32:
3432                                 /* Can't overflow */
3433                                 sp [-1].data.l = (guint64)sp [-1].data.i;
3434                                 break;
3435                         default:
3436                                 /* Can't overflow */
3437                                 sp [-1].data.l = (guint64)sp [-1].data.nati;
3438                                 break;
3439                         }
3440                         sp [-1].type = VAL_I64;
3441                         ++ip;
3442                         BREAK;
3443                 }
3444 #if SIZEOF_VOID_P == 4
3445                 CASE (CEE_CONV_OVF_I_UN)
3446                 CASE (CEE_CONV_OVF_U_UN) 
3447 #endif
3448                 CASE (CEE_CONV_OVF_I1_UN)
3449                 CASE (CEE_CONV_OVF_I2_UN)
3450                 CASE (CEE_CONV_OVF_I4_UN)
3451                 CASE (CEE_CONV_OVF_U1_UN)
3452                 CASE (CEE_CONV_OVF_U2_UN)
3453                 CASE (CEE_CONV_OVF_U4_UN) {
3454                         guint64 value;
3455                         switch (sp [-1].type) {
3456                         case VAL_DOUBLE:
3457                                 if (sp [-1].data.f <= -1.0)
3458                                         THROW_EX (mono_get_exception_overflow (), ip);
3459                                 value = (guint64)sp [-1].data.f;
3460                                 break;
3461                         case VAL_I64:
3462                                 value = (guint64)sp [-1].data.l;
3463                                 break;
3464                         case VAL_VALUET:
3465                                 ves_abort();
3466                         case VAL_I32:
3467                                 value = (guint64)sp [-1].data.i;
3468                                 break;
3469                         default:
3470                                 value = (guint64)sp [-1].data.nati;
3471                                 break;
3472                         }
3473                         switch (*ip) {
3474                         case CEE_CONV_OVF_I1_UN:
3475                                 if (value > 127)
3476                                         THROW_EX (mono_get_exception_overflow (), ip);
3477                                 sp [-1].data.i = value;
3478                                 sp [-1].type = VAL_I32;
3479                                 break;
3480                         case CEE_CONV_OVF_I2_UN:
3481                                 if (value > 32767)
3482                                         THROW_EX (mono_get_exception_overflow (), ip);
3483                                 sp [-1].data.i = value;
3484                                 sp [-1].type = VAL_I32;
3485                                 break;
3486 #if SIZEOF_VOID_P == 4
3487                         case CEE_CONV_OVF_I_UN: /* Fall through */
3488 #endif
3489                         case CEE_CONV_OVF_I4_UN:
3490                                 if (value > MYGUINT32_MAX)
3491                                         THROW_EX (mono_get_exception_overflow (), ip);
3492                                 sp [-1].data.i = value;
3493                                 sp [-1].type = VAL_I32;
3494                                 break;
3495                         case CEE_CONV_OVF_U1_UN:
3496                                 if (value > 255)
3497                                         THROW_EX (mono_get_exception_overflow (), ip);
3498                                 sp [-1].data.i = value;
3499                                 sp [-1].type = VAL_I32;
3500                                 break;
3501                         case CEE_CONV_OVF_U2_UN:
3502                                 if (value > 65535)
3503                                         THROW_EX (mono_get_exception_overflow (), ip);
3504                                 sp [-1].data.i = value;
3505                                 sp [-1].type = VAL_I32;
3506                                 break;
3507 #if SIZEOF_VOID_P == 4
3508                         case CEE_CONV_OVF_U_UN: /* Fall through */
3509 #endif
3510                         case CEE_CONV_OVF_U4_UN:
3511                                 if (value > 4294967295U)
3512                                         THROW_EX (mono_get_exception_overflow (), ip);
3513                                 sp [-1].data.i = value;
3514                                 sp [-1].type = VAL_I32;
3515                                 break;
3516                         default:
3517                                 g_assert_not_reached ();
3518                         }
3519                         ++ip;
3520                         BREAK;
3521                 }
3522                 CASE (CEE_BOX) {
3523                         guint32 token;
3524                         MonoClass *class;
3525
3526                         ip++;
3527                         token = read32 (ip);
3528
3529                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3530                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3531                         else
3532                                 class = mono_class_get (image, token);
3533                         g_assert (class != NULL);
3534
3535                         sp [-1].type = VAL_OBJ;
3536                         if (class->byval_arg.type == MONO_TYPE_VALUETYPE && !class->enumtype) 
3537                                 sp [-1].data.p = mono_value_box (domain, class, sp [-1].data.p);
3538                         else {
3539                                 stackval_to_data (&class->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3540                                 sp [-1].data.p = mono_value_box (domain, class, &sp [-1]);
3541                         }
3542                         /* need to vt_free (sp); */
3543
3544                         ip += 4;
3545
3546                         BREAK;
3547                 }
3548                 CASE (CEE_NEWARR) {
3549                         MonoClass *class;
3550                         MonoObject *o;
3551                         guint32 token;
3552
3553                         ip++;
3554                         token = read32 (ip);
3555
3556                         if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3557                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3558                         else
3559                                 class = mono_class_get (image, token);
3560
3561                         o = (MonoObject*) mono_array_new (domain, class, sp [-1].data.i);
3562                         ip += 4;
3563
3564                         sp [-1].type = VAL_OBJ;
3565                         sp [-1].data.p = o;
3566                         /*if (profiling_classes) {
3567                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3568                                 count++;
3569                                 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3570                         }*/
3571
3572                         BREAK;
3573                 }
3574                 CASE (CEE_LDLEN) {
3575                         MonoArray *o;
3576
3577                         ip++;
3578
3579                         g_assert (sp [-1].type == VAL_OBJ);
3580
3581                         o = sp [-1].data.p;
3582                         if (!o)
3583                                 THROW_EX (mono_get_exception_null_reference (), ip - 1);
3584                         
3585                         g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3586
3587                         sp [-1].type = VAL_I32;
3588                         sp [-1].data.i = mono_array_length (o);
3589
3590                         BREAK;
3591                 }
3592                 CASE (CEE_LDELEMA) {
3593                         MonoArray *o;
3594                         guint32 esize, token;
3595                         mono_u aindex;
3596                         
3597                         ++ip;
3598                         token = read32 (ip);
3599                         ip += 4;
3600                         sp -= 2;
3601
3602                         g_assert (sp [0].type == VAL_OBJ);
3603                         o = sp [0].data.p;
3604
3605                         g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3606                         
3607                         aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3608                         if (aindex >= mono_array_length (o))
3609                                 THROW_EX (mono_get_exception_index_out_of_range (), ip - 5);
3610
3611                         /* check the array element corresponds to token */
3612                         esize = mono_array_element_size (o->obj.vtable->klass);
3613                         
3614                         sp->type = VAL_MP;
3615                         sp->data.p = mono_array_addr_with_size (o, esize, aindex);
3616
3617                         ++sp;
3618                         BREAK;
3619                 }
3620                 CASE (CEE_LDELEM_I1) /* fall through */
3621                 CASE (CEE_LDELEM_U1) /* fall through */
3622                 CASE (CEE_LDELEM_I2) /* fall through */
3623                 CASE (CEE_LDELEM_U2) /* fall through */
3624                 CASE (CEE_LDELEM_I4) /* fall through */
3625                 CASE (CEE_LDELEM_U4) /* fall through */
3626                 CASE (CEE_LDELEM_I8)  /* fall through */
3627                 CASE (CEE_LDELEM_I)  /* fall through */
3628                 CASE (CEE_LDELEM_R4) /* fall through */
3629                 CASE (CEE_LDELEM_R8) /* fall through */
3630                 CASE (CEE_LDELEM_REF) {
3631                         MonoArray *o;
3632                         mono_u aindex;
3633
3634                         sp -= 2;
3635
3636                         g_assert (sp [0].type == VAL_OBJ);
3637                         o = sp [0].data.p;
3638                         if (!o)
3639                                 THROW_EX (mono_get_exception_null_reference (), ip);
3640
3641                         g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3642                         
3643                         aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3644                         if (aindex >= mono_array_length (o))
3645                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3646
3647                         /*
3648                          * FIXME: throw mono_get_exception_array_type_mismatch () if needed 
3649                          */
3650                         switch (*ip) {
3651                         case CEE_LDELEM_I1:
3652                                 sp [0].data.i = mono_array_get (o, gint8, aindex);
3653                                 sp [0].type = VAL_I32;
3654                                 break;
3655                         case CEE_LDELEM_U1:
3656                                 sp [0].data.i = mono_array_get (o, guint8, aindex);
3657                                 sp [0].type = VAL_I32;
3658                                 break;
3659                         case CEE_LDELEM_I2:
3660                                 sp [0].data.i = mono_array_get (o, gint16, aindex);
3661                                 sp [0].type = VAL_I32;
3662                                 break;
3663                         case CEE_LDELEM_U2:
3664                                 sp [0].data.i = mono_array_get (o, guint16, aindex);
3665                                 sp [0].type = VAL_I32;
3666                                 break;
3667                         case CEE_LDELEM_I:
3668                                 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3669                                 sp [0].type = VAL_NATI;
3670                                 break;
3671                         case CEE_LDELEM_I4:
3672                                 sp [0].data.i = mono_array_get (o, gint32, aindex);
3673                                 sp [0].type = VAL_I32;
3674                                 break;
3675                         case CEE_LDELEM_U4:
3676                                 sp [0].data.i = mono_array_get (o, guint32, aindex);
3677                                 sp [0].type = VAL_I32;
3678                                 break;
3679                         case CEE_LDELEM_I8:
3680                                 sp [0].data.l = mono_array_get (o, guint64, aindex);
3681                                 sp [0].type = VAL_I64;
3682                                 break;
3683                         case CEE_LDELEM_R4:
3684                                 sp [0].data.f = mono_array_get (o, float, aindex);
3685                                 sp [0].type = VAL_DOUBLE;
3686                                 break;
3687                         case CEE_LDELEM_R8:
3688                                 sp [0].data.f = mono_array_get (o, double, aindex);
3689                                 sp [0].type = VAL_DOUBLE;
3690                                 break;
3691                         case CEE_LDELEM_REF:
3692                                 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3693                                 sp [0].type = VAL_OBJ;
3694                                 break;
3695                         default:
3696                                 ves_abort();
3697                         }
3698
3699                         ++ip;
3700                         ++sp;
3701                         BREAK;
3702                 }
3703                 CASE (CEE_STELEM_I)  /* fall through */
3704                 CASE (CEE_STELEM_I1) /* fall through */ 
3705                 CASE (CEE_STELEM_I2) /* fall through */
3706                 CASE (CEE_STELEM_I4) /* fall through */
3707                 CASE (CEE_STELEM_I8) /* fall through */
3708                 CASE (CEE_STELEM_R4) /* fall through */
3709                 CASE (CEE_STELEM_R8) /* fall through */
3710                 CASE (CEE_STELEM_REF) {
3711                         MonoArray *o;
3712                         MonoClass *ac;
3713                         mono_u aindex;
3714
3715                         sp -= 3;
3716
3717                         g_assert (sp [0].type == VAL_OBJ);
3718                         o = sp [0].data.p;
3719                         if (!o)
3720                                 THROW_EX (mono_get_exception_null_reference (), ip);
3721
3722                         ac = o->obj.vtable->klass;
3723                         g_assert (MONO_CLASS_IS_ARRAY (ac));
3724                     
3725                         aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3726                         if (aindex >= mono_array_length (o))
3727                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3728
3729                         switch (*ip) {
3730                         case CEE_STELEM_I:
3731                                 mono_array_set (o, mono_i, aindex, sp [2].data.nati);
3732                                 break;
3733                         case CEE_STELEM_I1:
3734                                 mono_array_set (o, gint8, aindex, sp [2].data.i);
3735                                 break;
3736                         case CEE_STELEM_I2:
3737                                 mono_array_set (o, gint16, aindex, sp [2].data.i);
3738                                 break;
3739                         case CEE_STELEM_I4:
3740                                 mono_array_set (o, gint32, aindex, sp [2].data.i);
3741                                 break;
3742                         case CEE_STELEM_I8:
3743                                 mono_array_set (o, gint64, aindex, sp [2].data.l);
3744                                 break;
3745                         case CEE_STELEM_R4:
3746                                 mono_array_set (o, float, aindex, sp [2].data.f);
3747                                 break;
3748                         case CEE_STELEM_R8:
3749                                 mono_array_set (o, double, aindex, sp [2].data.f);
3750                                 break;
3751                         case CEE_STELEM_REF:
3752                                 g_assert (sp [2].type == VAL_OBJ);
3753                                 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3754                                         THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3755                                 mono_array_set (o, gpointer, aindex, sp [2].data.p);
3756                                 break;
3757                         default:
3758                                 ves_abort();
3759                         }
3760
3761                         ++ip;
3762                         BREAK;
3763                 }
3764                 CASE (CEE_LDELEM) 
3765                 CASE (CEE_STELEM) 
3766                 CASE (CEE_UNBOX_ANY) 
3767                 CASE (CEE_UNUSED5) 
3768                 CASE (CEE_UNUSED6) 
3769                 CASE (CEE_UNUSED7) 
3770                 CASE (CEE_UNUSED8) 
3771                 CASE (CEE_UNUSED9) 
3772                 CASE (CEE_UNUSED10) 
3773                 CASE (CEE_UNUSED11) 
3774                 CASE (CEE_UNUSED12) 
3775                 CASE (CEE_UNUSED13) 
3776                 CASE (CEE_UNUSED14) 
3777                 CASE (CEE_UNUSED15) 
3778                 CASE (CEE_UNUSED16) 
3779                 CASE (CEE_UNUSED17) ves_abort(); BREAK;
3780
3781 #if SIZEOF_VOID_P == 4
3782                 CASE (CEE_CONV_OVF_I)
3783                 CASE (CEE_CONV_OVF_U) 
3784 #endif
3785                 CASE (CEE_CONV_OVF_I1)
3786                 CASE (CEE_CONV_OVF_I2)
3787                 CASE (CEE_CONV_OVF_I4)
3788                 CASE (CEE_CONV_OVF_U1)
3789                 CASE (CEE_CONV_OVF_U2)
3790                 CASE (CEE_CONV_OVF_U4) {
3791                         gint64 value;
3792                         switch (sp [-1].type) {
3793                         case VAL_DOUBLE:
3794                                 value = (gint64)sp [-1].data.f;
3795                                 break;
3796                         case VAL_I64:
3797                                 value = (gint64)sp [-1].data.l;
3798                                 break;
3799                         case VAL_VALUET:
3800                                 ves_abort();
3801                         case VAL_I32:
3802                                 value = (gint64)sp [-1].data.i;
3803                                 break;
3804                         default:
3805                                 value = (gint64)sp [-1].data.nati;
3806                                 break;
3807                         }
3808                         switch (*ip) {
3809                         case CEE_CONV_OVF_I1:
3810                                 if (value < -128 || value > 127)
3811                                         THROW_EX (mono_get_exception_overflow (), ip);
3812                                 sp [-1].data.i = value;
3813                                 sp [-1].type = VAL_I32;
3814                                 break;
3815                         case CEE_CONV_OVF_I2:
3816                                 if (value < -32768 || value > 32767)
3817                                         THROW_EX (mono_get_exception_overflow (), ip);
3818                                 sp [-1].data.i = value;
3819                                 sp [-1].type = VAL_I32;
3820                                 break;
3821 #if SIZEOF_VOID_P == 4
3822                         case CEE_CONV_OVF_I: /* Fall through */
3823 #endif
3824                         case CEE_CONV_OVF_I4:
3825                                 if (value < MYGINT32_MIN || value > MYGINT32_MAX)
3826                                         THROW_EX (mono_get_exception_overflow (), ip);
3827                                 sp [-1].data.i = value;
3828                                 sp [-1].type = VAL_I32;
3829                                 break;
3830                         case CEE_CONV_OVF_U1:
3831                                 if (value < 0 || value > 255)
3832                                         THROW_EX (mono_get_exception_overflow (), ip);
3833                                 sp [-1].data.i = value;
3834                                 sp [-1].type = VAL_I32;
3835                                 break;
3836                         case CEE_CONV_OVF_U2:
3837                                 if (value < 0 || value > 65535)
3838                                         THROW_EX (mono_get_exception_overflow (), ip);
3839                                 sp [-1].data.i = value;
3840                                 sp [-1].type = VAL_I32;
3841                                 break;
3842 #if SIZEOF_VOID_P == 4
3843                         case CEE_CONV_OVF_U: /* Fall through */
3844 #endif
3845                         case CEE_CONV_OVF_U4:
3846                                 if (value < 0 || value > MYGUINT32_MAX)
3847                                         THROW_EX (mono_get_exception_overflow (), ip);
3848                                 sp [-1].data.i = value;
3849                                 sp [-1].type = VAL_I32;
3850                                 break;
3851                         default:
3852                                 g_assert_not_reached ();
3853                         }
3854                         ++ip;
3855                         BREAK;
3856                 }
3857
3858 #if SIZEOF_VOID_P == 8
3859                 CASE (CEE_CONV_OVF_I)
3860 #endif
3861                 CASE (CEE_CONV_OVF_I8)
3862                         /* FIXME: handle other cases */
3863                         if (sp [-1].type == VAL_I32) {
3864                                 sp [-1].data.l = (guint64)sp [-1].data.i;
3865                                 sp [-1].type = VAL_I64;
3866                         } else if(sp [-1].type == VAL_I64) {
3867                                 /* defined as NOP */
3868                         } else {
3869                                 ves_abort();
3870                         }
3871                         ++ip;
3872                         BREAK;
3873
3874 #if SIZEOF_VOID_P == 8
3875                 CASE (CEE_CONV_OVF_U)
3876 #endif
3877                 CASE (CEE_CONV_OVF_U8)
3878                         /* FIXME: handle other cases */
3879                         if (sp [-1].type == VAL_I32) {
3880                                 sp [-1].data.l = (guint64) sp [-1].data.i;
3881                                 sp [-1].type = VAL_I64;
3882                         } else if(sp [-1].type == VAL_I64) {
3883                                 /* defined as NOP */
3884                         } else {
3885                                 ves_abort();
3886                         }
3887                         ++ip;
3888                         BREAK;
3889                 CASE (CEE_UNUSED50) 
3890                 CASE (CEE_UNUSED18) 
3891                 CASE (CEE_UNUSED19) 
3892                 CASE (CEE_UNUSED20) 
3893                 CASE (CEE_UNUSED21) 
3894                 CASE (CEE_UNUSED22) 
3895                 CASE (CEE_UNUSED23) ves_abort(); BREAK;
3896                 CASE (CEE_REFANYVAL) ves_abort(); BREAK;
3897                 CASE (CEE_CKFINITE)
3898                         if (!finite(sp [-1].data.f))
3899                                 THROW_EX (mono_get_exception_arithmetic (), ip);
3900                         ++ip;
3901                         BREAK;
3902                 CASE (CEE_UNUSED24) ves_abort(); BREAK;
3903                 CASE (CEE_UNUSED25) ves_abort(); BREAK;
3904                 CASE (CEE_MKREFANY) ves_abort(); BREAK;
3905                 CASE (CEE_UNUSED59) 
3906                 CASE (CEE_UNUSED60) 
3907                 CASE (CEE_UNUSED61) 
3908                 CASE (CEE_UNUSED62) 
3909                 CASE (CEE_UNUSED63) 
3910                 CASE (CEE_UNUSED64) 
3911                 CASE (CEE_UNUSED65) 
3912                 CASE (CEE_UNUSED66) 
3913                 CASE (CEE_UNUSED67) ves_abort(); BREAK;
3914                 CASE (CEE_LDTOKEN) {
3915                         gpointer handle;
3916                         MonoClass *handle_class;
3917                         ++ip;
3918                         handle = mono_ldtoken (image, read32 (ip), &handle_class);
3919                         ip += 4;
3920                         vt_alloc (&handle_class->byval_arg, sp, FALSE);
3921                         stackval_from_data (&handle_class->byval_arg, sp, (char*)&handle, FALSE);
3922                         ++sp;
3923                         BREAK;
3924                 }
3925                 CASE (CEE_ADD_OVF)
3926                         --sp;
3927                         /* FIXME: check overflow */
3928                         if (sp->type == VAL_I32) {
3929                                 if (sp [-1].type == VAL_I32) {
3930                                         if (CHECK_ADD_OVERFLOW (sp [-1].data.i, sp [0].data.i))
3931                                                 THROW_EX (mono_get_exception_overflow (), ip);
3932                                         sp [-1].data.i = (gint32)sp [-1].data.i + (gint32)sp [0].data.i;
3933                                 } else {
3934                                         if (CHECK_ADD_OVERFLOW_NAT (sp [-1].data.nati, (mono_i)sp [0].data.i))
3935                                                 THROW_EX (mono_get_exception_overflow (), ip);
3936                                         sp [-1].data.nati = sp [-1].data.nati + (mono_i)sp [0].data.i;
3937                                 }
3938                         } else if (sp->type == VAL_I64) {
3939                                 if (CHECK_ADD_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3940                                         THROW_EX (mono_get_exception_overflow (), ip);
3941                                 sp [-1].data.l = (gint64)sp [-1].data.l + (gint64)sp [0].data.l;
3942                         } else if (sp->type == VAL_DOUBLE)
3943                                 sp [-1].data.f += sp [0].data.f;
3944                         else {
3945                                 char *p = sp [-1].data.p;
3946                                 p += GET_NATI (sp [0]);
3947                                 sp [-1].data.p = p;
3948                         }
3949                         ++ip;
3950                         BREAK;
3951                 CASE (CEE_ADD_OVF_UN)
3952                         --sp;
3953                         /* FIXME: check overflow, make unsigned */
3954                         if (sp->type == VAL_I32) {
3955                                 if (sp [-1].type == VAL_I32) {
3956                                         if (CHECK_ADD_OVERFLOW_UN (sp [-1].data.i, sp [0].data.i))
3957                                                 THROW_EX (mono_get_exception_overflow (), ip);
3958                                         sp [-1].data.i = (guint32)sp [-1].data.i + (guint32)sp [0].data.i;
3959                                 } else {
3960                                         if (CHECK_ADD_OVERFLOW_NAT_UN (sp [-1].data.nati, (mono_u)sp [0].data.i))
3961                                                 THROW_EX (mono_get_exception_overflow (), ip);
3962                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati + (mono_u)sp [0].data.i;
3963                                 }
3964                         } else if (sp->type == VAL_I64) {
3965                                 if (CHECK_ADD_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3966                                         THROW_EX (mono_get_exception_overflow (), ip);
3967                                 sp [-1].data.l = (guint64)sp [-1].data.l + (guint64)sp [0].data.l;
3968                         } else if (sp->type == VAL_DOUBLE)
3969                                 sp [-1].data.f += sp [0].data.f;
3970                         else {
3971                                 char *p = sp [-1].data.p;
3972                                 p += GET_NATI (sp [0]);
3973                                 sp [-1].data.p = p;
3974                         }
3975                         ++ip;
3976                         BREAK;
3977                 CASE (CEE_MUL_OVF)
3978                         --sp;
3979                         /* FIXME: check overflow */
3980                         if (sp->type == VAL_I32) {
3981                                 if (sp [-1].type == VAL_NATI) {
3982                                         if (CHECK_MUL_OVERFLOW_NAT (sp [-1].data.nati, sp [0].data.i))
3983                                                 THROW_EX (mono_get_exception_overflow (), ip);
3984                                         sp [-1].data.nati = sp [-1].data.nati * sp [0].data.i;
3985                                         sp [-1].type = VAL_NATI;
3986                                 } else {
3987                                         if (CHECK_MUL_OVERFLOW (sp [-1].data.i, sp [0].data.i))
3988                                                 THROW_EX (mono_get_exception_overflow (), ip);
3989                                         sp [-1].data.i *= sp [0].data.i;
3990                                 }
3991                         }
3992                         else if (sp->type == VAL_I64) {
3993                                 if (CHECK_MUL_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3994                                         THROW_EX (mono_get_exception_overflow (), ip);
3995                                 sp [-1].data.l *= sp [0].data.l;
3996                         }
3997                         else if (sp->type == VAL_DOUBLE)
3998                                 sp [-1].data.f *= sp [0].data.f;
3999                         else
4000                                 ves_abort();
4001                         ++ip;
4002                         BREAK;
4003                 CASE (CEE_MUL_OVF_UN)
4004                         --sp;
4005                         /* FIXME: check overflow, make unsigned */
4006                         if (sp->type == VAL_I32) {
4007                                 if (sp [-1].type == VAL_NATI) {
4008                                         if (CHECK_MUL_OVERFLOW_NAT_UN (sp [-1].data.nati, (mono_u)sp [0].data.i))
4009                                                 THROW_EX (mono_get_exception_overflow (), ip);
4010                                         sp [-1].data.nati = (mono_u)sp [-1].data.nati * (mono_u)sp [0].data.i;
4011                                         sp [-1].type = VAL_NATI;
4012                                 } else {
4013                                         if (CHECK_MUL_OVERFLOW_UN ((guint32)sp [-1].data.i, (guint32)sp [0].data.i))
4014                                                 THROW_EX (mono_get_exception_overflow (), ip);
4015                                         sp [-1].data.i *= sp [0].data.i;
4016                                 }
4017                         }
4018                         else if (sp->type == VAL_I64) {
4019                                 if (CHECK_MUL_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
4020                                         THROW_EX (mono_get_exception_overflow (), ip);
4021                                 sp [-1].data.l *= sp [0].data.l;
4022                         }
4023                         else if (sp->type == VAL_DOUBLE)
4024                                 sp [-1].data.f *= sp [0].data.f;
4025                         else
4026                                 ves_abort();
4027                         ++ip;
4028                         BREAK;
4029                 CASE (CEE_SUB_OVF)
4030                         --sp;
4031                         /* FIXME: handle undeflow/unsigned */
4032                         /* should probably consider the pointers as unsigned */
4033                         if (sp->type == VAL_I32) {
4034                                 if (sp [-1].type == VAL_I32) {
4035                                         if (CHECK_SUB_OVERFLOW (sp [-1].data.i, sp [0].data.i))
4036                                                 THROW_EX (mono_get_exception_overflow (), ip);
4037                                         sp [-1].data.i -= sp [0].data.i;
4038                                 } else {
4039                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.i;
4040                                         sp [-1].type = VAL_NATI;
4041                                 }
4042                         }
4043                         else if (sp->type == VAL_I64) {
4044                                 if (CHECK_SUB_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
4045                                     THROW_EX (mono_get_exception_overflow (), ip);
4046                                 sp [-1].data.l -= sp [0].data.l;
4047                         }
4048                         else if (sp->type == VAL_DOUBLE)
4049                                 sp [-1].data.f -= sp [0].data.f;
4050                         else {
4051                                 if (sp [-1].type == VAL_I32) {
4052                                         sp [-1].data.nati = sp [-1].data.i - sp [0].data.nati;
4053                                         sp [-1].type = sp [0].type;
4054                                 } else
4055                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.nati;
4056                         }
4057                         ++ip;
4058                         BREAK;
4059                 CASE (CEE_SUB_OVF_UN)
4060                         --sp;
4061                         /* FIXME: handle undeflow/unsigned */
4062                         /* should probably consider the pointers as unsigned */
4063                         if (sp->type == VAL_I32) {
4064                                 if (sp [-1].type == VAL_I32) {
4065                                         if (CHECK_SUB_OVERFLOW_UN (sp [-1].data.i, sp [0].data.i))
4066                                                 THROW_EX (mono_get_exception_overflow (), ip);
4067                                         sp [-1].data.i -= sp [0].data.i;
4068                                 } else {
4069                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.i;
4070                                         sp [-1].type = VAL_NATI;
4071                                 }
4072                         }
4073                         else if (sp->type == VAL_I64) {
4074                                 if (CHECK_SUB_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
4075                                     THROW_EX (mono_get_exception_overflow (), ip);
4076                                 sp [-1].data.l -= sp [0].data.l;
4077                         }
4078                         else if (sp->type == VAL_DOUBLE)
4079                                 sp [-1].data.f -= sp [0].data.f;
4080                         else {
4081                                 if (sp [-1].type == VAL_I32) {
4082                                         sp [-1].data.nati = sp [-1].data.i - sp [0].data.nati;
4083                                         sp [-1].type = sp [0].type;
4084                                 } else
4085                                         sp [-1].data.nati = sp [-1].data.nati - sp [0].data.nati;
4086                         }
4087                         ++ip;
4088                         BREAK;
4089                 CASE (CEE_ENDFINALLY)
4090                         if (finally_ips) {
4091                                 ip = finally_ips->data;
4092                                 finally_ips = g_slist_remove (finally_ips, ip);
4093                                 goto main_loop;
4094                         }
4095                         if (frame->ex)
4096                                 goto handle_fault;
4097                         ves_abort();
4098                         BREAK;
4099                 CASE (CEE_LEAVE) /* Fall through */
4100                 CASE (CEE_LEAVE_S)
4101                         while (sp > frame->stack) {
4102                                 --sp;
4103                                 vt_free (sp);
4104                         }
4105                         frame->ip = ip;
4106                         if (*ip == CEE_LEAVE_S) {
4107                                 ++ip;
4108                                 ip += (signed char) *ip;
4109                                 ++ip;
4110                         } else {
4111                                 ++ip;
4112                                 ip += (gint32) read32 (ip);
4113                                 ip += 4;
4114                         }
4115                         endfinally_ip = ip;
4116                         if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - header->code)) {
4117                                 frame->ex_handler = NULL;
4118                                 frame->ex = NULL;
4119                         }
4120                         goto handle_finally;
4121                         BREAK;
4122                 CASE (CEE_UNUSED41)
4123                         ++ip;
4124                         switch (*ip) {
4125                         case CEE_MONO_FUNC1: {
4126                                 MonoMarshalConv conv;
4127                                 ++ip;
4128
4129                                 conv = *ip;
4130
4131                                 ++ip;
4132
4133                                 sp--;
4134
4135                                 sp->type = VAL_NATI;
4136
4137                                 switch (conv) {
4138                                 case MONO_MARSHAL_CONV_STR_LPWSTR:
4139                                         sp->data.p = mono_string_to_utf16 (sp->data.p);
4140                                         break;
4141                                 case MONO_MARSHAL_CONV_LPSTR_STR:
4142                                         sp->data.p = mono_string_new_wrapper (sp->data.p);
4143                                         break;
4144                                 case MONO_MARSHAL_CONV_STR_LPTSTR:
4145                                 case MONO_MARSHAL_CONV_STR_LPSTR:
4146                                         sp->data.p = mono_string_to_utf8 (sp->data.p);
4147                                         break;
4148                                 case MONO_MARSHAL_CONV_STR_BSTR:
4149                                         sp->data.p = mono_string_to_bstr (sp->data.p);
4150                                         break;
4151                                 case MONO_MARSHAL_CONV_STR_TBSTR:
4152                                 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4153                                         sp->data.p = mono_string_to_ansibstr (sp->data.p);
4154                                         break;
4155                                 case MONO_MARSHAL_CONV_SB_LPSTR:
4156                                         sp->data.p = mono_string_builder_to_utf8 (sp->data.p);
4157                                         break;
4158                                 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4159                                         sp->data.p = mono_array_to_savearray (sp->data.p);
4160                                         break;
4161                                 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4162                                         sp->data.p = mono_array_to_lparray (sp->data.p);
4163                                         break;
4164                                 case MONO_MARSHAL_CONV_DEL_FTN:
4165                                         sp->data.p = mono_delegate_to_ftnptr (sp->data.p);
4166                                         break;
4167                                 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4168                                         sp->data.p = mono_marshal_string_array (sp->data.p);
4169                                         break;
4170                                 case MONO_MARSHAL_CONV_LPWSTR_STR:
4171                                         sp->data.p = mono_string_from_utf16 (sp->data.p);
4172                                         break;
4173                                 default:
4174                                         fprintf(stderr, "MONO_FUNC1 %d", conv);
4175                                         g_assert_not_reached ();
4176                                 }
4177                                 sp++; 
4178                                 break;
4179                         }
4180                         case CEE_MONO_PROC2: {
4181                                 MonoMarshalConv conv;
4182                                 ++ip;
4183                                 conv = *ip;
4184                                 ++ip;
4185
4186                                 sp -= 2;
4187
4188                                 switch (conv) {
4189                                 case MONO_MARSHAL_CONV_LPSTR_SB:
4190                                         mono_string_utf8_to_builder (sp [0].data.p, sp [1].data.p);
4191                                         break;
4192                                 case MONO_MARSHAL_FREE_ARRAY:
4193                                         mono_marshal_free_array (sp [0].data.p, sp [1].data.i);
4194                                         break;
4195                                 default:
4196                                         g_assert_not_reached ();
4197                                 }                                
4198                                 break;
4199                         }
4200                         case CEE_MONO_PROC3: {
4201                                 MonoMarshalConv conv;
4202                                 ++ip;
4203                                 conv = *ip;
4204                                 ++ip;
4205
4206                                 sp -= 3;
4207
4208                                 switch (conv) {
4209                                 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4210                                         mono_string_to_byvalstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4211                                         break;
4212                                 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4213                                         mono_string_to_byvalwstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4214                                         break;
4215                                 default:
4216                                         g_assert_not_reached ();
4217                                 }
4218                                 break;
4219                         }
4220                         case CEE_MONO_VTADDR: {
4221                                 ++ip;
4222
4223                                 sp [-1].type = VAL_MP;
4224                                 /* do nothing? */
4225                                 break;
4226                         }
4227                         case CEE_MONO_LDPTR: {
4228                                 guint32 token;
4229                                 ++ip;
4230                                 
4231                                 token = read32 (ip);
4232                                 ip += 4;
4233                                 
4234                                 sp->type = VAL_NATI;
4235                                 sp->data.p = mono_method_get_wrapper_data (frame->method, token);
4236                                 ++sp;
4237                                 break;
4238                         }
4239                         case CEE_MONO_FREE: {
4240                                 ++ip;
4241
4242                                 sp -= 1;
4243                                 g_free (sp->data.p);
4244                                 break;
4245                         }
4246                         case CEE_MONO_OBJADDR: {
4247                                 ++ip;
4248
4249                                 sp->type = VAL_MP;
4250                                 /* do nothing? */
4251                                 break;
4252                         }
4253                         case CEE_MONO_NEWOBJ: {
4254                                 MonoClass *class;
4255                                 guint32 token;
4256
4257                                 ++ip;
4258                                 token = read32 (ip);
4259                                 ip += 4;
4260
4261                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
4262                                 sp->data.p = mono_object_new (domain, class);
4263                                 sp++;
4264                                 break;
4265                         }
4266                         case CEE_MONO_RETOBJ: {
4267                                 MonoClass *class;
4268                                 guint32 token;
4269
4270                                 ++ip;
4271                                 token = read32 (ip);
4272                                 ip += 4;
4273
4274                                 sp--;
4275
4276                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
4277                                 
4278                                 stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);
4279
4280                                 if (sp > frame->stack)
4281                                         g_warning ("more values on stack: %d", sp-frame->stack);
4282                                 goto exit_frame;
4283                         }
4284                         case CEE_MONO_LDNATIVEOBJ: {
4285                                 MonoClass *class;
4286                                 guint32 token;
4287
4288                                 ++ip;
4289                                 token = read32 (ip);
4290                                 ip += 4;
4291
4292                                 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
4293                                 g_assert(class->valuetype);
4294
4295                                 sp [-1].type = VAL_MP;
4296
4297                                 break;
4298                         }
4299                         default:
4300                                 g_error ("Unimplemented opcode: 0xF0 %02x at 0x%x\n", *ip, ip-header->code);
4301                         }
4302                         BREAK;
4303                 CASE (CEE_UNUSED26) 
4304                 CASE (CEE_UNUSED27) 
4305                 CASE (CEE_UNUSED28) 
4306                 CASE (CEE_UNUSED29) 
4307                 CASE (CEE_UNUSED30) 
4308                 CASE (CEE_UNUSED31) 
4309                 CASE (CEE_UNUSED32) 
4310                 CASE (CEE_UNUSED33) 
4311                 CASE (CEE_UNUSED34) 
4312                 CASE (CEE_UNUSED35) 
4313                 CASE (CEE_UNUSED36) 
4314                 CASE (CEE_UNUSED37) 
4315                 CASE (CEE_UNUSED38) 
4316                 CASE (CEE_UNUSED39) 
4317                 CASE (CEE_UNUSED40) 
4318                 CASE (CEE_UNUSED42) 
4319                 CASE (CEE_UNUSED43) 
4320                 CASE (CEE_UNUSED44) 
4321                 CASE (CEE_UNUSED45) 
4322                 CASE (CEE_UNUSED46) 
4323                 CASE (CEE_UNUSED47) 
4324                 CASE (CEE_UNUSED48)
4325                 CASE (CEE_PREFIX7)
4326                 CASE (CEE_PREFIX6)
4327                 CASE (CEE_PREFIX5)
4328                 CASE (CEE_PREFIX4)
4329                 CASE (CEE_PREFIX3)
4330                 CASE (CEE_PREFIX2)
4331                 CASE (CEE_PREFIXREF) ves_abort(); BREAK;
4332                 /*
4333                  * Note: Exceptions thrown when executing a prefixed opcode need
4334                  * to take into account the number of prefix bytes (usually the
4335                  * throw point is just (ip - n_prefix_bytes).
4336                  */
4337                 SUB_SWITCH
4338                         ++ip;
4339                         switch (*ip) {
4340                         case CEE_ARGLIST: ves_abort(); break;
4341                         case CEE_CEQ: {
4342                                 gint32 result;
4343                                 ++ip;
4344                                 sp -= 2;
4345
4346                                 if (sp->type == VAL_I32)
4347                                         result = (mono_i)sp [0].data.i == (mono_i)GET_NATI (sp [1]);
4348                                 else if (sp->type == VAL_I64)
4349                                         result = sp [0].data.l == sp [1].data.l;
4350                                 else if (sp->type == VAL_DOUBLE) {
4351                                         if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
4352                                                 result = 0;
4353                                         else
4354                                                 result = sp [0].data.f == sp [1].data.f;
4355                                 } else
4356                                         result = sp [0].data.nati == GET_NATI (sp [1]);
4357                                 sp->type = VAL_I32;
4358                                 sp->data.i = result;
4359
4360                                 sp++;
4361                                 break;
4362                         }
4363                         case CEE_CGT: {
4364                                 gint32 result;
4365                                 ++ip;
4366                                 sp -= 2;
4367
4368                                 if (sp->type == VAL_I32)
4369                                         result = (mono_i)sp [0].data.i > (mono_i)GET_NATI (sp [1]);
4370                                 else if (sp->type == VAL_I64)
4371                                         result = sp [0].data.l > sp [1].data.l;
4372                                 else if (sp->type == VAL_DOUBLE) {
4373                                         if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
4374                                                 result = 0;
4375                                         else
4376                                                 result = sp [0].data.f > sp [1].data.f;
4377                                 } else
4378                                         result = (mono_i)sp [0].data.nati > (mono_i)GET_NATI (sp [1]);
4379                                 sp->type = VAL_I32;
4380                                 sp->data.i = result;
4381
4382                                 sp++;
4383                                 break;
4384                         }
4385                         case CEE_CGT_UN: {
4386                                 gint32 result;
4387                                 ++ip;
4388                                 sp -= 2;
4389
4390                                 if (sp->type == VAL_I32)
4391                                         result = (mono_u)sp [0].data.i > (mono_u)GET_NATI (sp [1]);
4392                                 else if (sp->type == VAL_I64)
4393                                         result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
4394                                 else if (sp->type == VAL_DOUBLE)
4395                                         result = isnan (sp [0].data.f) || isnan (sp [1].data.f) ||
4396                                                 sp[0].data.f > sp[1].data.f;
4397                                 else
4398                                         result = (mono_u)sp [0].data.nati > (mono_u)GET_NATI (sp [1]);
4399                                 sp->type = VAL_I32;
4400                                 sp->data.i = result;
4401
4402                                 sp++;
4403                                 break;
4404                         }
4405                         case CEE_CLT: {
4406                                 gint32 result;
4407                                 ++ip;
4408                                 sp -= 2;
4409
4410                                 if (sp->type == VAL_I32)
4411                                         result = (mono_i)sp [0].data.i < (mono_i)GET_NATI (sp [1]);
4412                                 else if (sp->type == VAL_I64)
4413                                         result = sp [0].data.l < sp [1].data.l;
4414                                 else if (sp->type == VAL_DOUBLE) {
4415                                         if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
4416                                                 result = 0;
4417                                         else
4418                                                 result = sp [0].data.f < sp [1].data.f;
4419                                 } else
4420                                         result = (mono_i)sp [0].data.nati < (mono_i)GET_NATI (sp [1]);
4421                                 sp->type = VAL_I32;
4422                                 sp->data.i = result;
4423
4424                                 sp++;
4425                                 break;
4426                         }
4427                         case CEE_CLT_UN: {
4428                                 gint32 result;
4429                                 ++ip;
4430                                 sp -= 2;
4431
4432                                 if (sp->type == VAL_I32)
4433                                         result = (mono_u)sp [0].data.i < (mono_u)GET_NATI (sp [1]);
4434                                 else if (sp->type == VAL_I64)
4435                                         result = (guint64)sp [0].data.l < (guint64)sp [1].data.l;
4436                                 else if (sp->type == VAL_DOUBLE)
4437                                         result = isnan (sp [0].data.f) || isnan (sp [1].data.f) ||
4438                                                 sp[0].data.f < sp[1].data.f;
4439                                 else
4440                                         result = (mono_u)sp [0].data.nati < (mono_u)GET_NATI (sp [1]);
4441                                 sp->type = VAL_I32;
4442                                 sp->data.i = result;
4443
4444                                 sp++;
4445                                 break;
4446                         }
4447                         case CEE_LDFTN:
4448                         case CEE_LDVIRTFTN: {
4449                                 int virtual = *ip == CEE_LDVIRTFTN;
4450                                 MonoMethod *m;
4451                                 guint32 token;
4452                                 ++ip;
4453                                 token = read32 (ip);
4454                                 ip += 4;
4455
4456                                 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
4457                                         m = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
4458                                 else 
4459                                         m = mono_get_method (image, token, NULL);
4460
4461                                 if (!m)
4462                                         THROW_EX (mono_get_exception_missing_method (), ip - 5);
4463                                 if (virtual) {
4464                                         --sp;
4465                                         if (!sp->data.p)
4466                                                 THROW_EX (mono_get_exception_null_reference (), ip - 5);
4467                                         
4468                                         m = get_virtual_method (domain, m, sp);
4469                                 }
4470
4471                                 /* 
4472                                  * This prevents infinite cycles since the wrapper contains
4473                                  * an ldftn too.
4474                                  */
4475                                 if (frame->method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
4476                                         if (m && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4477                                                 m = mono_marshal_get_synchronized_wrapper (m);
4478
4479                                 sp->type = VAL_NATI;
4480                                 sp->data.p = mono_create_method_pointer (m);
4481                                 ++sp;
4482                                 break;
4483                         }
4484                         case CEE_UNUSED56: ves_abort(); break;
4485                         case CEE_LDARG: {
4486                                 guint32 arg_pos;
4487                                 ++ip;
4488                                 arg_pos = read16 (ip);
4489                                 ip += 2;
4490                                 vt_alloc (ARG_TYPE (signature, arg_pos), sp, signature->pinvoke);
4491                                 stackval_from_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4492                                 ++sp;
4493                                 break;
4494                         }
4495                         case CEE_LDARGA: {
4496                                 guint32 anum;
4497                                 ++ip;
4498                                 anum = read16 (ip);
4499                                 ip += 2;
4500                                 sp->data.vt = ARG_POS (anum);
4501                                 sp->type = VAL_MP;
4502                                 ++sp;
4503                                 break;
4504                         }
4505                         case CEE_STARG: {
4506                                 guint32 arg_pos;
4507                                 ++ip;
4508                                 arg_pos = read16 (ip);
4509                                 ip += 2;
4510                                 --sp;
4511                                 stackval_to_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4512                                 vt_free (sp);
4513                                 break;
4514                         }
4515                         case CEE_LDLOC: {
4516                                 guint32 loc_pos;
4517                                 ++ip;
4518                                 loc_pos = read16 (ip);
4519                                 ip += 2;
4520                                 vt_alloc (LOCAL_TYPE (header, loc_pos), sp, FALSE);
4521                                 stackval_from_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4522                                 ++sp;
4523                                 break;
4524                         }
4525                         case CEE_LDLOCA: {
4526                                 MonoType *t;
4527                                 guint32 loc_pos;
4528
4529                                 ++ip;
4530                                 loc_pos = read16 (ip);
4531                                 ip += 2;
4532                                 t = LOCAL_TYPE (header, loc_pos);
4533                                 sp->data.vt = LOCAL_POS (loc_pos);
4534
4535                                 sp->type = VAL_MP;
4536
4537                                 ++sp;
4538                                 break;
4539                         }
4540                         case CEE_STLOC: {
4541                                 guint32 loc_pos;
4542                                 ++ip;
4543                                 loc_pos = read16 (ip);
4544                                 ip += 2;
4545                                 --sp;
4546                                 stackval_to_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4547                                 vt_free (sp);
4548                                 break;
4549                         }
4550                         case CEE_LOCALLOC:
4551                                 --sp;
4552                                 if (sp != frame->stack)
4553                                         THROW_EX (mono_get_exception_execution_engine (NULL), ip - 1);
4554                                 ++ip;
4555                                 sp->data.p = alloca (sp->data.i);
4556                                 sp->type = VAL_MP;
4557                                 sp++;
4558                                 break;
4559                         case CEE_UNUSED57: ves_abort(); break;
4560                         case CEE_ENDFILTER: ves_abort(); break;
4561                         case CEE_UNALIGNED_:
4562                                 ++ip;
4563                                 unaligned_address = 1;
4564                                 break;
4565                         case CEE_VOLATILE_:
4566                                 ++ip;
4567                                 volatile_address = 1;
4568                                 break;
4569                         case CEE_TAIL_:
4570                                 ++ip;
4571                                 tail_recursion = 1;
4572                                 break;
4573                         case CEE_INITOBJ: {
4574                                 guint32 token;
4575                                 MonoClass *class;
4576
4577                                 ++ip;
4578                                 token = read32 (ip);
4579                                 ip += 4;
4580
4581                                 class = mono_class_get (image, token);
4582
4583                                 --sp;
4584                                 g_assert (sp->type == VAL_TP || sp->type == VAL_MP);
4585                                 memset (sp->data.vt, 0, mono_class_value_size (class, NULL));
4586                                 break;
4587                         }
4588                         case CEE_CONSTRAINED_: {
4589                                 guint32 token;
4590                                 /* FIXME: implement */
4591                                 ++ip;
4592                                 token = read32 (ip);
4593                                 ip += 4;
4594                                 break;
4595                         }
4596                         case CEE_CPBLK:
4597                                 sp -= 3;
4598                                 if (!sp [0].data.p || !sp [1].data.p)
4599                                         THROW_EX (mono_get_exception_null_reference(), ip - 1);
4600                                 ++ip;
4601                                 /* FIXME: value and size may be int64... */
4602                                 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4603                                 break;
4604                         case CEE_INITBLK:
4605                                 sp -= 3;
4606                                 if (!sp [0].data.p)
4607                                         THROW_EX (mono_get_exception_null_reference(), ip - 1);
4608                                 ++ip;
4609                                 /* FIXME: value and size may be int64... */
4610                                 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4611                                 break;
4612                         case CEE_NO_:
4613                                 /* FIXME: implement */
4614                                 ip += 2;
4615                                 break;
4616                         case CEE_RETHROW:
4617                                 /* 
4618                                  * need to clarify what this should actually do:
4619                                  * start the search from the last found handler in
4620                                  * this method or continue in the caller or what.
4621                                  * Also, do we need to run finally/fault handlers after a retrow?
4622                                  * Well, this implementation will follow the usual search
4623                                  * for an handler, considering the current ip as throw spot.
4624                                  * We need to NULL frame->ex_handler for the later code to
4625                                  * actually run the new found handler.
4626                                  */
4627                                 frame->ex_handler = NULL;
4628                                 THROW_EX (frame->ex, ip - 1);
4629                                 break;
4630                         case CEE_UNUSED: ves_abort(); break;
4631                         case CEE_SIZEOF: {
4632                                 guint32 token;
4633                                 int align;
4634                                 ++ip;
4635                                 token = read32 (ip);
4636                                 ip += 4;
4637                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4638                                         MonoType *type = mono_type_create_from_typespec (image, token);
4639                                         sp->data.i = mono_type_size (type, &align);
4640                                 } else {
4641                                         MonoClass *szclass = mono_class_get (image, token);
4642                                         mono_class_init (szclass);
4643                                         if (!szclass->valuetype)
4644                                                 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
4645                                         sp->data.i = mono_class_value_size (szclass, &align);
4646                                 }
4647                                 sp->type = VAL_I32;
4648                                 ++sp;
4649                                 break;
4650                         }
4651                         case CEE_REFANYTYPE: ves_abort(); break;
4652                         default:
4653                                 g_error ("Unimplemented opcode: 0xFE %02x at 0x%x\n", *ip, ip-header->code);
4654                         }
4655                         continue;
4656                 DEFAULT;
4657                 }
4658         }
4659
4660         g_assert_not_reached ();
4661         /*
4662          * Exception handling code.
4663          * The exception object is stored in frame->ex.
4664          */
4665
4666         handle_exception:
4667         {
4668                 int i;
4669                 guint32 ip_offset;
4670                 MonoInvocation *inv;
4671                 MonoMethodHeader *hd;
4672                 MonoExceptionClause *clause;
4673                 /*char *message;*/
4674                 MonoObject *ex_obj;
4675
4676 #if DEBUG_INTERP
4677                 if (tracing)
4678                         g_print ("* Handling exception '%s' at IL_%04x\n", frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name, frame->ip - header->code);
4679 #endif
4680                 if (die_on_exception)
4681                         goto die_on_ex;
4682
4683                 if (frame->ex == quit_exception)
4684                         goto handle_finally;
4685                 
4686                 for (inv = frame; inv; inv = inv->parent) {
4687                         if (inv->method == NULL)
4688                                 continue;
4689                         if (inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4690                                 continue;
4691                         if (inv->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4692                                 continue;
4693                         hd = ((MonoMethodNormal*)inv->method)->header;
4694                         ip_offset = inv->ip - hd->code;
4695                         inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one  - this one wins */
4696                         for (i = 0; i < hd->num_clauses; ++i) {
4697                                 clause = &hd->clauses [i];
4698                                 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4699                                         if (!clause->flags) {
4700                                                 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (inv->method->klass->image, clause->token_or_filter))) {
4701                                                         /* 
4702                                                          * OK, we found an handler, now we need to execute the finally
4703                                                          * and fault blocks before branching to the handler code.
4704                                                          */
4705                                                         inv->ex_handler = clause;
4706 #if DEBUG_INTERP
4707                                                         if (tracing)
4708                                                                 g_print ("* Found handler at '%s'\n", inv->method->name);
4709 #endif
4710                                                         goto handle_finally;
4711                                                 }
4712                                         } else {
4713                                                 /* FIXME: handle filter clauses */
4714                                                 g_assert (0);
4715                                         }
4716                                 }
4717                         }
4718                 }
4719                 /*
4720                  * If we get here, no handler was found: print a stack trace.
4721                  */
4722                 for (inv = frame; inv; inv = inv->parent) {
4723                         if (inv->invoke_trap)
4724                                 goto handle_finally;
4725                 }
4726 die_on_ex:
4727                 ex_obj = (MonoObject*)frame->ex;
4728                 mono_unhandled_exception (ex_obj);
4729                 exit (1);
4730         }
4731         handle_finally:
4732         {
4733                 int i;
4734                 guint32 ip_offset;
4735                 MonoExceptionClause *clause;
4736                 GSList *old_list = finally_ips;
4737                 
4738 #if DEBUG_INTERP
4739                 if (tracing)
4740                         g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - header->code);
4741 #endif
4742                 if ((frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
4743                                 || (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4744                         goto exit_frame;
4745                 }
4746                 ip_offset = frame->ip - header->code;
4747
4748                 if (endfinally_ip != NULL)
4749                         finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4750                 for (i = 0; i < header->num_clauses; ++i)
4751                         if (frame->ex_handler == &header->clauses [i])
4752                                 break;
4753                 while (i > 0) {
4754                         --i;
4755                         clause = &header->clauses [i];
4756                         if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - header->code)))) {
4757                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4758                                         ip = header->code + clause->handler_offset;
4759                                         finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4760 #if DEBUG_INTERP
4761                                         if (tracing)
4762                                                 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4763 #endif
4764                                 }
4765                         }
4766                 }
4767
4768                 endfinally_ip = NULL;
4769
4770                 if (old_list != finally_ips && finally_ips) {
4771                         ip = finally_ips->data;
4772                         finally_ips = g_slist_remove (finally_ips, ip);
4773                         sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4774                         goto main_loop;
4775                 }
4776
4777                 /*
4778                  * If an exception is set, we need to execute the fault handler, too,
4779                  * otherwise, we continue normally.
4780                  */
4781                 if (frame->ex)
4782                         goto handle_fault;
4783                 ves_abort();
4784         }
4785         handle_fault:
4786         {
4787                 int i;
4788                 guint32 ip_offset;
4789                 MonoExceptionClause *clause;
4790                 
4791 #if DEBUG_INTERP
4792                 if (tracing)
4793                         g_print ("* Handle fault\n");
4794 #endif
4795                 ip_offset = frame->ip - header->code;
4796                 for (i = 0; i < header->num_clauses; ++i) {
4797                         clause = &header->clauses [i];
4798                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4799                                 ip = header->code + clause->handler_offset;
4800 #if DEBUG_INTERP
4801                                 if (tracing)
4802                                         g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4803 #endif
4804                                 goto main_loop;
4805                         }
4806                 }
4807                 /*
4808                  * If the handler for the exception was found in this method, we jump
4809                  * to it right away, otherwise we return and let the caller run
4810                  * the finally, fault and catch blocks.
4811                  * This same code should be present in the endfault opcode, but it
4812                  * is corrently not assigned in the ECMA specs: LAMESPEC.
4813                  */
4814                 if (frame->ex_handler) {
4815 #if DEBUG_INTERP
4816                         if (tracing)
4817                                 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4818 #endif
4819                         ip = header->code + frame->ex_handler->handler_offset;
4820                         sp = frame->stack;
4821                         sp->type = VAL_OBJ;
4822                         sp->data.p = frame->ex;
4823                         ++sp;
4824                         goto main_loop;
4825                 }
4826                 goto exit_frame;
4827         }
4828 exit_frame:
4829         DEBUG_LEAVE ();
4830 }
4831
4832 void
4833 ves_exec_method (MonoInvocation *frame)
4834 {
4835         ThreadContext *context = TlsGetValue (thread_context_id);
4836         ThreadContext context_struct;
4837         if (context == NULL) {
4838                 context = &context_struct;
4839                 context_struct.base_frame = frame;
4840                 context_struct.current_frame = NULL;
4841                 context_struct.current_env = NULL;
4842                 context_struct.search_for_handler = 0;
4843                 TlsSetValue (thread_context_id, context);
4844         }
4845         frame->ip = NULL;
4846         frame->parent = context->current_frame;
4847         ves_exec_method_with_context(frame, context);
4848         if (frame->ex) {
4849                 if (context->current_env) {
4850                         context->env_frame->ex = frame->ex;
4851                         longjmp (*context->current_env, 1);
4852                 }
4853                 else
4854                         mono_unhandled_exception ((MonoObject*)frame->ex);
4855         }
4856         if (context->base_frame == frame)
4857                 TlsSetValue (thread_context_id, NULL);
4858         else
4859                 context->current_frame = frame->parent;
4860 }
4861
4862 static int 
4863 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4864 {
4865         MonoImage *image = assembly->image;
4866         MonoCLIImageInfo *iinfo;
4867         MonoMethod *method;
4868         MonoObject *exc = NULL;
4869         int rval;
4870
4871         iinfo = image->image_info;
4872         method = mono_get_method (image, iinfo->cli_cli_header.ch_entry_point, NULL);
4873         if (!method)
4874                 g_error ("No entry point method found in %s", image->name);
4875
4876         rval = mono_runtime_run_main (method, argc, argv, &exc);
4877         if (exc != NULL)
4878                 mono_unhandled_exception (exc);
4879
4880         return rval;
4881 }
4882
4883 static void
4884 usage (void)
4885 {
4886         fprintf (stderr,
4887                  "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4888                  "Usage is: mint [options] executable args...\n\n", VERSION);
4889         fprintf (stderr,
4890                  "Runtime Debugging:\n"
4891 #ifdef DEBUG_INTERP
4892                  "   --debug\n"
4893 #endif
4894                  "   --dieonex\n"
4895                  "   --noptr\t\t\tdon't print pointer addresses in trace output\n"
4896                  "   --opcode-count\n"
4897                  "   --print-vtable\n"
4898                  "   --traceclassinit\n"
4899                  "\n"
4900                  "Development:\n"
4901                  "   --debug method_name\n"
4902                  "   --profile\n"
4903                  "   --trace\n"
4904                  "   --traceops\n"
4905                  "\n"
4906                  "Runtime:\n"
4907                  "   --config filename  load the specified config file instead of the default\n"
4908                  "   --workers n        maximum number of worker threads\n"
4909                 );
4910         exit (1);
4911 }
4912
4913 #ifdef RUN_TEST
4914 static void
4915 test_load_class (MonoImage* image)
4916 {
4917         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
4918         MonoClass *klass;
4919         int i;
4920
4921         for (i = 1; i <= t->rows; ++i) {
4922                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4923                 mono_class_init (klass);
4924         }
4925 }
4926 #endif
4927
4928 static MonoException * segv_exception = NULL;
4929
4930 static void
4931 segv_handler (int signum)
4932 {
4933         signal (signum, segv_handler);
4934         mono_raise_exception (segv_exception);
4935 }
4936
4937
4938 static void
4939 quit_handler (int signum)
4940 {
4941         signal (signum, quit_handler);
4942         mono_raise_exception (quit_exception);
4943 }
4944
4945 static MonoBoolean
4946 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
4947                           MonoReflectionMethod **method, 
4948                           gint32 *iloffset, gint32 *native_offset,
4949                           MonoString **file, gint32 *line, gint32 *column)
4950 {
4951         if (iloffset)
4952                 *iloffset = 0;
4953         if (native_offset)
4954                 *native_offset = 0;
4955         if (method)
4956                 *method = NULL;
4957         if (line)
4958                 *line = 0;
4959         if (column)
4960                 *column = 0;
4961         if (file)
4962                 *file = mono_string_new (mono_domain_get (), "unknown");
4963
4964         return TRUE;
4965 }
4966
4967 static MonoArray *
4968 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4969 {
4970         return NULL;
4971 }
4972
4973 typedef struct
4974 {
4975         MonoDomain *domain;
4976         int enable_debugging;
4977         char *file;
4978         int argc;
4979         char **argv;
4980 } MainThreadArgs;
4981
4982 static void main_thread_handler (gpointer user_data)
4983 {
4984         MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4985         MonoAssembly *assembly;
4986         char *error;
4987
4988         if (main_args->enable_debugging)
4989                 mono_debug_init (main_args->domain, MONO_DEBUG_FORMAT_MONO);
4990
4991         assembly = mono_domain_assembly_open (main_args->domain,
4992                                               main_args->file);
4993
4994         if (!assembly){
4995                 fprintf (stderr, "Can not open image %s\n", main_args->file);
4996                 exit (1);
4997         }
4998
4999         if (main_args->enable_debugging)
5000                 mono_debug_init_2 (assembly);
5001
5002 #ifdef RUN_TEST
5003         test_load_class (assembly->image);
5004 #else
5005         error = mono_verify_corlib ();
5006         if (error) {
5007                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
5008                 exit (1);
5009         }
5010         segv_exception = mono_get_exception_null_reference ();
5011         segv_exception->message = mono_string_new (main_args->domain, "Segmentation fault");
5012         signal (SIGSEGV, segv_handler);
5013         /* perhaps we should use a different class for this exception... */
5014         quit_exception = mono_get_exception_null_reference ();
5015         quit_exception->message = mono_string_new (main_args->domain, "Quit");
5016         signal (SIGINT, quit_handler);
5017
5018         ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
5019 #endif
5020 }
5021
5022 static void
5023 mono_runtime_install_handlers (void)
5024 {
5025         /* FIXME: anything to do here? */
5026 }
5027
5028 static void
5029 quit_function (MonoDomain *domain, gpointer user_data)
5030 {
5031         mono_profiler_shutdown ();
5032         
5033         mono_runtime_cleanup (domain);
5034         mono_domain_free (domain, TRUE);
5035
5036 }
5037
5038 void
5039 mono_interp_cleanup(MonoDomain *domain)
5040 {
5041     quit_function (domain, NULL);
5042 }
5043
5044 int
5045 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
5046 {
5047     return ves_exec (domain, assembly, argc, argv);
5048 }
5049
5050 MonoDomain *
5051 mono_interp_init(const char *file)
5052 {
5053         MonoDomain *domain;
5054
5055         g_set_prgname (file);
5056         mono_set_rootdir ();
5057         
5058         g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
5059         g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
5060
5061         g_thread_init (NULL);
5062
5063         thread_context_id = TlsAlloc ();
5064         TlsSetValue (thread_context_id, NULL);
5065         InitializeCriticalSection(&calc_section);
5066         InitializeCriticalSection(&create_method_pointer_mutex);
5067
5068         mono_install_compile_method (mono_create_method_pointer);
5069         mono_install_runtime_invoke (interp_mono_runtime_invoke);
5070         mono_install_remoting_trampoline (interp_create_remoting_trampoline);
5071
5072         mono_install_handler (interp_ex_handler);
5073         mono_install_stack_walk (interp_walk_stack);
5074         mono_runtime_install_cleanup (quit_function);
5075
5076         domain = mono_init (file);
5077 #ifdef __hpux /* generates very big stack frames */
5078         mono_threads_set_default_stacksize(32*1024*1024);
5079 #endif
5080         mono_init_icall ();
5081         mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
5082         mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
5083         mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
5084
5085         mono_runtime_init (domain, NULL, NULL);
5086
5087         return domain;
5088 }
5089
5090 int 
5091 mono_main (int argc, char *argv [])
5092 {
5093         MonoDomain *domain;
5094         int retval = 0, i, ocount = 0;
5095         char *file, *config_file = NULL;
5096         int enable_debugging = FALSE;
5097         MainThreadArgs main_args;
5098         const char *error;
5099
5100         setlocale (LC_ALL, "");
5101         if (argc < 2)
5102                 usage ();
5103
5104         for (i = 1; i < argc && argv [i][0] == '-'; i++){
5105                 if (strcmp (argv [i], "--trace") == 0)
5106                         global_tracing = 1;
5107                 if (strcmp (argv [i], "--noptr") == 0)
5108                         global_no_pointers = 1;
5109                 if (strcmp (argv [i], "--traceops") == 0)
5110                         global_tracing = 2;
5111                 if (strcmp (argv [i], "--dieonex") == 0) {
5112                         die_on_exception = 1;
5113                         enable_debugging = 1;
5114                 }
5115                 if (strcmp (argv [i], "--print-vtable") == 0)
5116                         mono_print_vtable = TRUE;
5117                 if (strcmp (argv [i], "--profile") == 0)
5118                         mono_profiler_load (NULL);
5119                 if (strcmp (argv [i], "--opcode-count") == 0)
5120                         ocount = 1;
5121                 if (strcmp (argv [i], "--config") == 0)
5122                         config_file = argv [++i];
5123                 if (strcmp (argv [i], "--workers") == 0) {
5124                         mono_max_worker_threads = atoi (argv [++i]);
5125                         if (mono_max_worker_threads < 1)
5126                                 mono_max_worker_threads = 1;
5127                 }
5128                 if (strcmp (argv [i], "--help") == 0)
5129                         usage ();
5130 #if DEBUG_INTERP
5131                 if (strcmp (argv [i], "--debug") == 0) {
5132                         MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
5133                         if (!desc)
5134                                 g_error ("Invalid method name '%s'", argv [i]);
5135                         db_methods = g_list_append (db_methods, desc);
5136                 }
5137 #endif
5138         }
5139         
5140         file = argv [i];
5141
5142         if (!file)
5143                 usage ();
5144
5145         domain = mono_interp_init(file);
5146         mono_config_parse (config_file);
5147
5148         error = mono_verify_corlib ();
5149         if (error) {
5150                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
5151                 exit (1);
5152         }
5153
5154         error = mono_check_corlib_version ();
5155         if (error) {
5156                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
5157                 fprintf (stderr, "Download a newer corlib at http://go-mono/daily.\n");
5158                 exit (1);
5159         }
5160
5161         main_args.domain=domain;
5162         main_args.file=file;
5163         main_args.argc=argc-i;
5164         main_args.argv=argv+i;
5165         main_args.enable_debugging=enable_debugging;
5166         
5167         mono_runtime_exec_managed_code (domain, main_thread_handler,
5168                                         &main_args);
5169
5170         quit_function (domain, NULL);
5171
5172         /* Get the return value from System.Environment.ExitCode */
5173         retval=mono_environment_exitcode_get ();
5174
5175 #if DEBUG_INTERP
5176         if (ocount) {
5177                 fprintf (stderr, "opcode count: %ld\n", opcode_count);
5178                 fprintf (stderr, "fcall count: %ld\n", fcall_count);
5179         }
5180 #endif
5181         return retval;
5182 }