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