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