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