2004-07-26 Bernie Solomon <bernard@ugsolutions.com>
[mono.git] / mono / interpreter / interp.c
1 /*
2  * PLEASE NOTE: This is a research prototype.
3  *
4  *
5  * interp.c: Interpreter for CIL byte codes
6  *
7  * Authors:
8  *   Paolo Molaro (lupus@ximian.com)
9  *   Miguel de Icaza (miguel@ximian.com)
10  *   Dietmar Maurer (dietmar@ximian.com)
11  *
12  * (C) 2001, 2002 Ximian, Inc.
13  */
14 #ifndef __USE_ISOC99
15 #define __USE_ISOC99
16 #endif
17 #include "config.h"
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <setjmp.h>
23 #include <signal.h>
24 #include <math.h>
25 #include <locale.h>
26
27 #include <mono/os/gc_wrapper.h>
28
29 #ifdef HAVE_ALLOCA_H
30 #   include <alloca.h>
31 #else
32 #   ifdef __CYGWIN__
33 #      define alloca __builtin_alloca
34 #   endif
35 #endif
36
37 /* trim excessive headers */
38 #include <mono/metadata/image.h>
39 #include <mono/metadata/assembly.h>
40 #include <mono/metadata/cil-coff.h>
41 #include <mono/metadata/mono-endian.h>
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/tokentype.h>
44 #include <mono/metadata/loader.h>
45 #include <mono/metadata/threads.h>
46 #include <mono/metadata/threadpool.h>
47 #include <mono/metadata/profiler-private.h>
48 #include <mono/metadata/appdomain.h>
49 #include <mono/metadata/reflection.h>
50 #include <mono/metadata/exception.h>
51 #include <mono/metadata/verify.h>
52 #include <mono/metadata/opcodes.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/metadata/mono-config.h>
55 #include <mono/metadata/marshal.h>
56 #include <mono/metadata/environment.h>
57 #include <mono/metadata/mono-debug.h>
58 #include <mono/os/util.h>
59
60 #include "interp.h"
61 #include "mintops.h"
62 #include "embed.h"
63 #include "hacks.h"
64
65 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
66         a = i,
67
68 enum {
69 #include "mono/cil/opcode.def"
70         CEE_LASTOP
71 };
72 #undef OPDEF
73
74 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
75 #ifdef _WIN32
76 #define isnan _isnan
77 #define finite _finite
78 #endif
79 #ifndef HAVE_FINITE
80 #ifdef HAVE_ISFINITE
81 #define finite isfinite
82 #endif
83 #endif
84
85 static gint *abort_requested;
86
87 /* If true, then we output the opcodes as we interpret them */
88 static int global_tracing = 0;
89 static int global_no_pointers = 0;
90
91 int mono_interp_traceopt = 0;
92
93 static int debug_indent_level = 0;
94
95 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,mono_method)   \
96         do {    \
97                 (frame)->parent = (parent_frame);       \
98                 (frame)->obj = (obj_this);      \
99                 (frame)->stack_args = (method_args);    \
100                 (frame)->retval = (method_retval);      \
101                 (frame)->runtime_method = mono_interp_get_runtime_method (mono_method); \
102                 (frame)->ex = NULL;     \
103                 (frame)->ip = NULL;     \
104                 (frame)->invoke_trap = 0;       \
105         } while (0)
106
107 void ves_exec_method (MonoInvocation *frame);
108
109 static char* dump_stack (stackval *stack, stackval *sp);
110 static char* dump_frame (MonoInvocation *inv);
111 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
112 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
113
114 typedef void (*ICallMethod) (MonoInvocation *frame);
115
116 static guint32 die_on_exception = 0;
117 static guint32 thread_context_id = 0;
118
119 #define DEBUG_INTERP 1
120 #define COUNT_OPS 0
121 #if DEBUG_INTERP
122
123 static int break_on_method = 0;
124 static int nested_trace = 0;
125 static GList *db_methods = NULL;
126
127 static void
128 output_indent (void)
129 {
130         int h;
131
132         for (h = 0; h < debug_indent_level; h++)
133                 g_print ("  ");
134 }
135
136 static void
137 db_match_method (gpointer data, gpointer user_data)
138 {
139         MonoMethod *m = (MonoMethod*)user_data;
140         MonoMethodDesc *desc = data;
141
142         if (mono_method_desc_full_match (desc, m))
143                 break_on_method = 1;
144 }
145
146 #define DEBUG_ENTER()   \
147         if (db_methods) { \
148                 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);  \
149                 if (break_on_method) tracing=nested_trace ? (global_tracing = 2, 3) : 2;        \
150                 break_on_method = 0;    \
151         } \
152         if (tracing) {  \
153                 MonoMethod *method = frame->runtime_method->method ;\
154                 char *mn, *args = dump_args (frame);    \
155                 debug_indent_level++;   \
156                 output_indent ();       \
157                 mn = mono_method_full_name (method, FALSE); \
158                 g_print ("(%u) Entering %s (", GetCurrentThreadId(), mn);       \
159                 g_free (mn); \
160                 g_print ("%s)\n", args);        \
161                 g_free (args);  \
162         }       \
163         if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)    \
164                 mono_profiler_method_enter (frame->runtime_method->method);
165
166 #define DEBUG_LEAVE()   \
167         if (tracing) {  \
168                 char *mn, *args;        \
169                 args = dump_retval (frame);     \
170                 output_indent ();       \
171                 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
172                 g_print ("(%u) Leaving %s", GetCurrentThreadId(),  mn); \
173                 g_free (mn); \
174                 g_print (" => %s\n", args);     \
175                 g_free (args);  \
176                 debug_indent_level--;   \
177                 if (tracing == 3) global_tracing = 0; \
178         }       \
179         if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)    \
180                 mono_profiler_method_leave (frame->runtime_method->method);
181
182 #else
183
184 #define DEBUG_ENTER()
185 #define DEBUG_LEAVE()
186
187 #endif
188
189 static void
190 interp_ex_handler (MonoException *ex) {
191         ThreadContext *context = TlsGetValue (thread_context_id);
192         char *stack_trace;
193         if (context == NULL)
194                 return;
195         stack_trace = dump_frame (context->current_frame);
196         ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
197         g_free (stack_trace);
198         if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
199                 char *strace = mono_string_to_utf8 (ex->stack_trace);
200                 fprintf(stderr, "Nothing can catch this exception: ");
201                 fprintf(stderr, "%s", ex->object.vtable->klass->name);
202                 if (ex->message != NULL) {
203                         char *m = mono_string_to_utf8 (ex->message);
204                         fprintf(stderr, ": %s", m);
205                         g_free(m);
206                 }
207                 fprintf(stderr, "\n");
208                 fprintf(stderr, "%s\n", strace);
209                 g_free (strace);
210                 if (ex->inner_ex != NULL) {
211                         ex = (MonoException *)ex->inner_ex;
212                         fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
213                         if (ex->message != NULL) {
214                                 char *m = mono_string_to_utf8 (ex->message);
215                                 fprintf(stderr, ": %s", m);
216                                 g_free(m);
217                         }
218                         strace = mono_string_to_utf8 (ex->stack_trace);
219                         fprintf(stderr, "\n");
220                         fprintf(stderr, "%s\n", strace);
221                         g_free (strace);
222                 }
223                 /* wait for other threads to also collapse */
224                 Sleep(1000);
225                 exit(1);
226         }
227         context->env_frame->ex = ex;
228         context->search_for_handler = 1;
229         longjmp (*context->current_env, 1);
230 }
231
232 static void
233 ves_real_abort (int line, MonoMethod *mh,
234                 const unsigned short *ip, stackval *stack, stackval *sp)
235 {
236         MonoMethodNormal *mm = (MonoMethodNormal *)mh;
237         fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
238         fprintf (stderr, "Line=%d IP=0x%04x, Aborted execution\n", line,
239                  ip-(const unsigned short *)mm->header->code);
240         g_print ("0x%04x %02x\n",
241                  ip-(const unsigned short *)mm->header->code, *ip);
242         if (sp > stack)
243                 printf ("\t[%d] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
244 }
245
246 #define ves_abort() \
247         do {\
248                 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
249                 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
250         } while (0);
251
252 static gpointer
253 interp_create_remoting_trampoline (MonoMethod *method)
254 {
255         return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke (method));
256 }
257
258 static CRITICAL_SECTION runtime_method_lookup_section;
259
260 RuntimeMethod*
261 mono_interp_get_runtime_method (MonoMethod *method)
262 {
263         MonoDomain *domain = mono_domain_get ();
264         RuntimeMethod *rtm;
265
266         EnterCriticalSection (&runtime_method_lookup_section);
267         if ((rtm = g_hash_table_lookup (domain->jit_code_hash, method))) {
268                 LeaveCriticalSection (&runtime_method_lookup_section);
269                 return rtm;
270         }
271         rtm = mono_mempool_alloc (domain->mp, sizeof (RuntimeMethod));
272         memset (rtm, 0, sizeof (*rtm));
273         rtm->method = method;
274         rtm->param_count = method->signature->param_count;
275         rtm->hasthis = method->signature->hasthis;
276         rtm->valuetype = method->klass->valuetype;
277         g_hash_table_insert (domain->jit_code_hash, method, rtm);
278         LeaveCriticalSection (&runtime_method_lookup_section);
279
280         return rtm;
281 }
282
283 static gpointer
284 interp_create_trampoline (MonoMethod *method)
285 {
286         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
287                 method = mono_marshal_get_synchronized_wrapper (method);
288         return mono_interp_get_runtime_method (method);
289 }
290
291 static inline RuntimeMethod*
292 get_virtual_method (RuntimeMethod *runtime_method, MonoObject *obj)
293 {
294         MonoMethod *m = runtime_method->method;
295
296         if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
297                 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) 
298                         return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke (m));
299                 else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
300                         return mono_interp_get_runtime_method (mono_marshal_get_synchronized_wrapper (m));
301                 else
302                         return runtime_method;
303         }
304
305         if (m->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
306                 return ((RuntimeMethod **)obj->vtable->interface_offsets [m->klass->interface_id]) [m->slot];
307         } else {
308                 return ((RuntimeMethod **)obj->vtable->vtable) [m->slot];
309         }
310 }
311
312 void inline
313 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
314 {
315         if (type->byref) {
316                 switch (type->type) {
317                 case MONO_TYPE_OBJECT:
318                 case MONO_TYPE_CLASS:
319                 case MONO_TYPE_STRING:
320                 case MONO_TYPE_ARRAY:
321                 case MONO_TYPE_SZARRAY:
322                         break;
323                 default:
324                         break;
325                 }
326                 result->data.p = *(gpointer*)data;
327                 return;
328         }
329         switch (type->type) {
330         case MONO_TYPE_VOID:
331                 return;
332         case MONO_TYPE_I1:
333                 result->data.i = *(gint8*)data;
334                 return;
335         case MONO_TYPE_U1:
336         case MONO_TYPE_BOOLEAN:
337                 result->data.i = *(guint8*)data;
338                 return;
339         case MONO_TYPE_I2:
340                 result->data.i = *(gint16*)data;
341                 return;
342         case MONO_TYPE_U2:
343         case MONO_TYPE_CHAR:
344                 result->data.i = *(guint16*)data;
345                 return;
346         case MONO_TYPE_I4:
347                 result->data.i = *(gint32*)data;
348                 return;
349         case MONO_TYPE_U:
350         case MONO_TYPE_I:
351                 result->data.nati = *(mono_i*)data;
352                 return;
353         case MONO_TYPE_PTR:
354                 result->data.p = *(gpointer*)data;
355                 return;
356         case MONO_TYPE_U4:
357                 result->data.i = *(guint32*)data;
358                 return;
359         case MONO_TYPE_R4:
360                 result->data.f = *(float*)data;
361                 return;
362         case MONO_TYPE_I8:
363         case MONO_TYPE_U8:
364                 result->data.l = *(gint64*)data;
365                 return;
366         case MONO_TYPE_R8:
367                 result->data.f = *(double*)data;
368                 return;
369         case MONO_TYPE_STRING:
370         case MONO_TYPE_SZARRAY:
371         case MONO_TYPE_CLASS:
372         case MONO_TYPE_OBJECT:
373         case MONO_TYPE_ARRAY:
374                 result->data.p = *(gpointer*)data;
375                 return;
376         case MONO_TYPE_VALUETYPE:
377                 if (type->data.klass->enumtype) {
378                         stackval_from_data (type->data.klass->enum_basetype, result, data, pinvoke);
379                         return;
380                 } else {
381                         int size;
382                         
383                         if (pinvoke)
384                                 size = mono_class_native_size (type->data.klass, NULL);
385                         else
386                                 size = mono_class_value_size (type->data.klass, NULL);
387                         memcpy (result->data.vt, data, size);
388                 }
389                 return;
390         default:
391                 g_warning ("got type 0x%02x", type->type);
392                 g_assert_not_reached ();
393         }
394 }
395
396 void inline
397 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
398 {
399         if (type->byref) {
400                 gpointer *p = (gpointer*)data;
401                 *p = val->data.p;
402                 return;
403         }
404         /* printf ("TODAT0 %p\n", data); */
405         switch (type->type) {
406         case MONO_TYPE_I1:
407         case MONO_TYPE_U1: {
408                 guint8 *p = (guint8*)data;
409                 *p = val->data.i;
410                 return;
411         }
412         case MONO_TYPE_BOOLEAN: {
413                 guint8 *p = (guint8*)data;
414                 *p = (val->data.i != 0);
415                 return;
416         }
417         case MONO_TYPE_I2:
418         case MONO_TYPE_U2:
419         case MONO_TYPE_CHAR: {
420                 guint16 *p = (guint16*)data;
421                 *p = val->data.i;
422                 return;
423         }
424         case MONO_TYPE_I: {
425                 mono_i *p = (mono_i*)data;
426                 /* In theory the value used by stloc should match the local var type
427                    but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
428                    a native int - both by csc and mcs). Not sure what to do about sign extension
429                    as it is outside the spec... doing the obvious */
430                 *p = (mono_i)val->data.nati;
431                 return;
432         }
433         case MONO_TYPE_U: {
434                 mono_u *p = (mono_u*)data;
435                 /* see above. */
436                 *p = (mono_u)val->data.nati;
437                 return;
438         }
439         case MONO_TYPE_I4:
440         case MONO_TYPE_U4: {
441                 gint32 *p = (gint32*)data;
442                 *p = val->data.i;
443                 return;
444         }
445         case MONO_TYPE_I8:
446         case MONO_TYPE_U8: {
447                 gint64 *p = (gint64*)data;
448                 *p = val->data.l;
449                 return;
450         }
451         case MONO_TYPE_R4: {
452                 float *p = (float*)data;
453                 *p = val->data.f;
454                 return;
455         }
456         case MONO_TYPE_R8: {
457                 double *p = (double*)data;
458                 *p = val->data.f;
459                 return;
460         }
461         case MONO_TYPE_STRING:
462         case MONO_TYPE_SZARRAY:
463         case MONO_TYPE_CLASS:
464         case MONO_TYPE_OBJECT:
465         case MONO_TYPE_ARRAY:
466         case MONO_TYPE_PTR: {
467                 gpointer *p = (gpointer*)data;
468                 *p = val->data.p;
469                 return;
470         }
471         case MONO_TYPE_VALUETYPE:
472                 if (type->data.klass->enumtype) {
473                         stackval_to_data (type->data.klass->enum_basetype, val, data, pinvoke);
474                         return;
475                 } else {
476                         int size;
477
478                         if (pinvoke)
479                                 size = mono_class_native_size (type->data.klass, NULL);
480                         else
481                                 size = mono_class_value_size (type->data.klass, NULL);
482
483                         memcpy (data, val->data.p, size);
484                 }
485                 return;
486         default:
487                 g_warning ("got type %x", type->type);
488                 g_assert_not_reached ();
489         }
490 }
491
492 static void
493 fill_in_trace (MonoException *exception, MonoInvocation *frame)
494 {
495         char *stack_trace = dump_frame (frame);
496         MonoDomain *domain = mono_domain_get();
497         (exception)->stack_trace = mono_string_new (domain, stack_trace);
498         (exception)->trace_ips = get_trace_ips (domain, frame);
499         g_free (stack_trace);
500 }
501
502 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
503
504 #define THROW_EX(exception,ex_ip)       \
505         do {\
506                 frame->ip = (ex_ip);            \
507                 frame->ex = (MonoException*)(exception);        \
508                 FILL_IN_TRACE(frame->ex, frame); \
509                 goto handle_exception;  \
510         } while (0)
511
512 static MonoObject*
513 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
514 {
515         guint32 *lengths;
516         guint32 *lower_bounds;
517         int i;
518
519         lengths = alloca (sizeof (guint32) * klass->rank * 2);
520         for (i = 0; i < sig->param_count; ++i) {
521                 lengths [i] = values->data.i;
522                 values ++;
523         }
524         if (klass->rank == sig->param_count) {
525                 /* Only lengths provided. */
526                 lower_bounds = NULL;
527         } else {
528                 /* lower bounds are first. */
529                 lower_bounds = lengths;
530                 lengths += klass->rank;
531         }
532         return (MonoObject*)mono_array_new_full (domain, klass, lengths, lower_bounds);
533 }
534
535 static void 
536 ves_array_set (MonoInvocation *frame)
537 {
538         stackval *sp = frame->stack_args;
539         MonoObject *o;
540         MonoArray *ao;
541         MonoClass *ac;
542         gint32 i, t, pos, esize;
543         gpointer ea;
544         MonoType *mt;
545
546         o = frame->obj;
547         ao = (MonoArray *)o;
548         ac = o->vtable->klass;
549
550         g_assert (ac->rank >= 1);
551
552         pos = sp [0].data.i;
553         if (ao->bounds != NULL) {
554                 pos -= ao->bounds [0].lower_bound;
555                 for (i = 1; i < ac->rank; i++) {
556                         if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >= 
557                             ao->bounds [i].length) {
558                                 frame->ex = mono_get_exception_index_out_of_range ();
559                                 FILL_IN_TRACE(frame->ex, frame);
560                                 return;
561                         }
562                         pos = pos*ao->bounds [i].length + sp [i].data.i - 
563                                 ao->bounds [i].lower_bound;
564                 }
565         } else if (pos >= ao->max_length) {
566                 frame->ex = mono_get_exception_index_out_of_range ();
567                 FILL_IN_TRACE(frame->ex, frame);
568                 return;
569         }
570
571 #if 0 /* FIX */
572         if (sp [ac->rank].data.p && !mono_object_isinst (sp [ac->rank].data.p, mono_object_class (o)->element_class)) {
573                 frame->ex = mono_get_exception_array_type_mismatch ();
574                 FILL_IN_TRACE (frame->ex, frame);
575                 return;
576         }
577 #endif
578
579         esize = mono_array_element_size (ac);
580         ea = mono_array_addr_with_size (ao, esize, pos);
581
582         mt = frame->runtime_method->method->signature->params [ac->rank];
583         stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
584 }
585
586 static void 
587 ves_array_get (MonoInvocation *frame)
588 {
589         stackval *sp = frame->stack_args;
590         MonoObject *o;
591         MonoArray *ao;
592         MonoClass *ac;
593         gint32 i, t, pos, esize;
594         gpointer ea;
595         MonoType *mt;
596
597         o = frame->obj;
598         ao = (MonoArray *)o;
599         ac = o->vtable->klass;
600
601         g_assert (ac->rank >= 1);
602
603         pos = sp [0].data.i;
604         if (ao->bounds != NULL) {
605                 pos -= ao->bounds [0].lower_bound;
606                 for (i = 1; i < ac->rank; i++) {
607                         if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >= 
608                             ao->bounds [i].length) {
609                                 frame->ex = mono_get_exception_index_out_of_range ();
610                                 FILL_IN_TRACE(frame->ex, frame);
611                                 return;
612                         }
613
614                         pos = pos*ao->bounds [i].length + sp [i].data.i - 
615                                 ao->bounds [i].lower_bound;
616                 }
617         } else if (pos >= ao->max_length) {
618                 frame->ex = mono_get_exception_index_out_of_range ();
619                 FILL_IN_TRACE(frame->ex, frame);
620                 return;
621         }
622
623         esize = mono_array_element_size (ac);
624         ea = mono_array_addr_with_size (ao, esize, pos);
625
626         mt = frame->runtime_method->method->signature->ret;
627         stackval_from_data (mt, frame->retval, ea, FALSE);
628 }
629
630 static void
631 ves_array_element_address (MonoInvocation *frame)
632 {
633         stackval *sp = frame->stack_args;
634         MonoObject *o;
635         MonoArray *ao;
636         MonoClass *ac;
637         gint32 i, t, pos, esize;
638         gpointer ea;
639
640         o = frame->obj;
641         ao = (MonoArray *)o;
642         ac = o->vtable->klass;
643
644         g_assert (ac->rank >= 1);
645
646         pos = sp [0].data.i;
647         if (ao->bounds != NULL) {
648                 pos -= ao->bounds [0].lower_bound;
649                 for (i = 1; i < ac->rank; i++) {
650                         if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >= 
651                             ao->bounds [i].length) {
652                                 frame->ex = mono_get_exception_index_out_of_range ();
653                                 FILL_IN_TRACE(frame->ex, frame);
654                                 return;
655                         }
656                         pos = pos*ao->bounds [i].length + sp [i].data.i - 
657                                 ao->bounds [i].lower_bound;
658                 }
659         } else if (pos >= ao->max_length) {
660                 frame->ex = mono_get_exception_index_out_of_range ();
661                 FILL_IN_TRACE(frame->ex, frame);
662                 return;
663         }
664
665         esize = mono_array_element_size (ac);
666         ea = mono_array_addr_with_size (ao, esize, pos);
667
668         frame->retval->data.p = ea;
669 }
670
671 static void
672 interp_walk_stack (MonoStackWalk func, gpointer user_data)
673 {
674         ThreadContext *context = TlsGetValue (thread_context_id);
675         MonoInvocation *frame = context->current_frame;
676         int il_offset;
677         MonoMethodHeader *hd;
678
679         while (frame) {
680                 gboolean managed = FALSE;
681                 MonoMethod *method = frame->runtime_method->method;
682                 if (!method || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || 
683                                 (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
684                         il_offset = -1;
685                 else {
686                         hd = ((MonoMethodNormal*)method)->header;
687                         il_offset = frame->ip - (const unsigned short *)hd->code;
688                         if (!method->wrapper_type)
689                                 managed = TRUE;
690                 }
691                 if (func (method, -1, il_offset, managed, user_data))
692                         return;
693                 frame = frame->parent;
694         }
695 }
696
697 static void 
698 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFunc addr, gboolean string_ctor, ThreadContext *context)
699 {
700         jmp_buf env;
701         MonoPIFunc func;
702         MonoInvocation *old_frame = context->current_frame;
703         MonoInvocation *old_env_frame = context->env_frame;
704         jmp_buf *old_env = context->current_env;
705
706         if (setjmp (env)) {
707                 context->current_frame = old_frame;
708                 context->env_frame = old_env_frame;
709                 context->current_env = old_env;
710                 context->managed_code = 1;
711                 return;
712         }
713
714         frame->ex = NULL;
715         context->env_frame = frame;
716         context->current_env = &env;
717
718         if (frame->runtime_method) {
719                 func = frame->runtime_method->func;
720         } else {
721                 func = mono_arch_create_trampoline (sig, string_ctor);
722         }
723
724         context->current_frame = frame;
725         context->managed_code = 0;
726
727         func (addr, &frame->retval->data.p, frame->obj, frame->stack_args);
728
729         context->managed_code = 1;
730         /* domain can only be changed by native code */
731         context->domain = mono_domain_get ();
732
733         if (*abort_requested)
734                 mono_thread_interruption_checkpoint ();
735         
736         if (string_ctor) {
737                 stackval_from_data (&mono_defaults.string_class->byval_arg, 
738                                     frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
739         } else if (!MONO_TYPE_ISSTRUCT (sig->ret))
740                 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
741
742         context->current_frame = old_frame;
743         context->env_frame = old_env_frame;
744         context->current_env = old_env;
745 }
746
747 static void
748 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
749 {
750         MonoDelegate *delegate = (MonoDelegate *)this;
751
752         delegate->method_info = mono_method_get_object (domain, runtime_method->method, NULL);
753         delegate->target = target;
754
755         if (target && target->vtable->klass == mono_defaults.transparent_proxy_class) {
756                 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
757                 delegate->method_ptr = mono_interp_get_runtime_method (method);
758         } else {
759                 delegate->method_ptr = runtime_method;
760         }
761 }
762
763 /*
764  * From the spec:
765  * runtime specifies that the implementation of the method is automatically
766  * provided by the runtime and is primarily used for the methods of delegates.
767  */
768 static void
769 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
770 {
771         MonoMethod *method = frame->runtime_method->method;
772         const char *name = method->name;
773         MonoObject *obj = (MonoObject*)frame->obj;
774
775         mono_class_init (method->klass);
776         
777         if (obj && mono_object_isinst (obj, mono_defaults.multicastdelegate_class)) {
778                 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
779                         interp_delegate_ctor (context->domain, obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
780                         return;
781                 }
782         }
783
784         if (obj && mono_object_isinst (obj, mono_defaults.array_class)) {
785                 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
786                         ves_array_set (frame);
787                         return;
788                 }
789                 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
790                         ves_array_get (frame);
791                         return;
792                 }
793                 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
794                         ves_array_element_address (frame);
795                         return;
796                 }
797         }
798         
799         g_error ("Don't know how to exec runtime method %s.%s::%s", 
800                         method->klass->name_space, method->klass->name,
801                         method->name);
802 }
803
804 static char*
805 dump_stack (stackval *stack, stackval *sp)
806 {
807         stackval *s = stack;
808         GString *str = g_string_new ("");
809         
810         if (sp == stack)
811                 return g_string_free (str, FALSE);
812         
813         while (s < sp) {
814                 g_string_sprintfa (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
815                 ++s;
816         }
817         return g_string_free (str, FALSE);
818 }
819
820 static void
821 dump_stackval (GString *str, stackval *s, MonoType *type)
822 {
823         switch (type->type) {
824         case MONO_TYPE_I1:
825         case MONO_TYPE_U1:
826         case MONO_TYPE_I2:
827         case MONO_TYPE_U2:
828         case MONO_TYPE_I4:
829         case MONO_TYPE_U4:
830         case MONO_TYPE_CHAR:
831         case MONO_TYPE_BOOLEAN:
832                 g_string_sprintfa (str, "[%d] ", s->data.i);
833                 break;
834         case MONO_TYPE_STRING:
835         case MONO_TYPE_SZARRAY:
836         case MONO_TYPE_CLASS:
837         case MONO_TYPE_OBJECT:
838         case MONO_TYPE_ARRAY:
839         case MONO_TYPE_PTR:
840         case MONO_TYPE_I:
841         case MONO_TYPE_U:
842                 g_string_sprintfa (str, "[%p] ", s->data.p);
843                 break;
844         case MONO_TYPE_VALUETYPE:
845                 if (type->data.klass->enumtype)
846                         g_string_sprintfa (str, "[%d] ", s->data.i);
847                 else
848                         g_string_sprintfa (str, "[vt:%p] ", s->data.p);
849                 break;
850         case MONO_TYPE_R4:
851         case MONO_TYPE_R8:
852                 g_string_sprintfa (str, "[%g] ", s->data.f);
853                 break;
854         case MONO_TYPE_I8:
855         case MONO_TYPE_U8:
856         default:
857                 g_string_sprintfa (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
858                 break;
859         }
860 }
861
862 static char*
863 dump_args (MonoInvocation *inv)
864 {
865         GString *str = g_string_new ("");
866         int i;
867         MonoMethodSignature *signature = inv->runtime_method->method->signature;
868         
869         if (signature->param_count == 0)
870                 return g_string_free (str, FALSE);
871
872         if (signature->hasthis)
873                 g_string_sprintfa (str, "%p ", inv->obj);
874
875         for (i = 0; i < signature->param_count; ++i)
876                 dump_stackval (str, inv->stack_args + i, signature->params [i]);
877
878         return g_string_free (str, FALSE);
879 }
880
881 static char*
882 dump_retval (MonoInvocation *inv)
883 {
884         GString *str = g_string_new ("");
885         MonoType *ret = inv->runtime_method->method->signature->ret;
886
887         if (ret->type != MONO_TYPE_VOID)
888                 dump_stackval (str, inv->retval, ret);
889
890         return g_string_free (str, FALSE);
891 }
892  
893 static char*
894 dump_frame (MonoInvocation *inv)
895 {
896         GString *str = g_string_new ("");
897         int i;
898         char *args;
899         for (i = 0; inv; inv = inv->parent) {
900                 if (inv->runtime_method != NULL) {
901                         MonoMethod *method = inv->runtime_method->method;
902                         MonoClass *k;
903
904                         int codep = 0;
905                         const char * opname = "";
906                         char *name;
907                         gchar *source = NULL;
908
909                         k = method->klass;
910
911                         if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
912                                 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
913                                 MonoMethodHeader *hd = ((MonoMethodNormal *)method)->header;
914
915                                 if (hd != NULL) {
916                                         if (inv->ip) {
917                                                 opname = mono_interp_opname [*inv->ip];
918                                                 codep = inv->ip - inv->runtime_method->code;
919                                         } else 
920                                                 opname = "";
921         
922                                         source = mono_debug_source_location_from_il_offset (method, codep, NULL);
923                                 }
924                         }
925                         args = dump_args (inv);
926                         name = mono_method_full_name (method, TRUE);
927                         if (source)
928                                 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname,
929                                                    name, args, source);
930                         else
931                                 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname,
932                                                    name, args);
933                         g_free (name);
934                         g_free (args);
935                         g_free (source);
936                         ++i;
937                 }
938         }
939         return g_string_free (str, FALSE);
940 }
941
942 static MonoArray *
943 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
944 {
945         int i;
946         MonoArray *res;
947         MonoInvocation *inv;
948
949         for (i = 0, inv = top; inv; inv = inv->parent)
950                 if (inv->runtime_method != NULL)
951                         ++i;
952
953         res = mono_array_new (domain, mono_defaults.int_class, 2 * i);
954
955         for (i = 0, inv = top; inv; inv = inv->parent)
956                 if (inv->runtime_method != NULL) {
957                         mono_array_set (res, gpointer, i, inv->runtime_method);
958                         ++i;
959                         mono_array_set (res, gpointer, i, (gpointer)inv->ip);
960                         ++i;
961                 }
962
963         return res;
964 }
965
966
967 #define MYGUINT64_MAX 18446744073709551615ULL
968 #define MYGINT64_MAX 9223372036854775807LL
969 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
970
971 #define MYGUINT32_MAX 4294967295U
972 #define MYGINT32_MAX 2147483647
973 #define MYGINT32_MIN (-MYGINT32_MAX -1)
974         
975 #define CHECK_ADD_OVERFLOW(a,b) \
976         (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0  \
977         : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
978
979 #define CHECK_SUB_OVERFLOW(a,b) \
980         (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0   \
981         : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
982
983 #define CHECK_ADD_OVERFLOW_UN(a,b) \
984         (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
985
986 #define CHECK_SUB_OVERFLOW_UN(a,b) \
987         (guint32)(a) < (guint32)(b) ? -1 : 0
988
989 #define CHECK_ADD_OVERFLOW64(a,b) \
990         (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0  \
991         : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
992
993 #define CHECK_SUB_OVERFLOW64(a,b) \
994         (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0   \
995         : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
996
997 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
998         (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
999
1000 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1001         (guint64)(a) < (guint64)(b) ? -1 : 0
1002
1003 #if SIZEOF_VOID_P == 4
1004 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1005 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1006 #else
1007 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1008 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1009 #endif
1010
1011 /* Resolves to TRUE if the operands would overflow */
1012 #define CHECK_MUL_OVERFLOW(a,b) \
1013         ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1014         (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1015         (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1016         (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1017         (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1018         (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1019         (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1020
1021 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1022         ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1023         (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1024
1025 #define CHECK_MUL_OVERFLOW64(a,b) \
1026         ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1027         (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1028         (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1029         (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1030         (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1031         (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1032         (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1033
1034 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1035         ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1036         (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1037
1038 #if SIZEOF_VOID_P == 4
1039 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1040 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1041 #else
1042 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1043 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1044 #endif
1045
1046 static MonoObject*
1047 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1048 {
1049         MonoInvocation frame;
1050         ThreadContext * volatile context = TlsGetValue (thread_context_id);
1051         MonoObject *retval = NULL;
1052         MonoMethodSignature *sig = method->signature;
1053         MonoClass *klass = mono_class_from_mono_type (sig->ret);
1054         int i, type, isobject = 0;
1055         void *ret = NULL;
1056         stackval result;
1057         stackval *args = alloca (sizeof (stackval) * sig->param_count);
1058         ThreadContext context_struct;
1059         MonoInvocation *old_frame = NULL;
1060         jmp_buf env;
1061
1062         frame.ex = NULL;
1063
1064         if (setjmp(env)) {
1065                 if (context != &context_struct) {
1066                         context->domain = mono_domain_get ();
1067                         context->current_frame = old_frame;
1068                         context->managed_code = 0;
1069                 } else 
1070                         TlsSetValue (thread_context_id, NULL);
1071                 if (exc != NULL)
1072                         *exc = (MonoObject *)frame.ex;
1073                 return retval;
1074         }
1075
1076         if (context == NULL) {
1077                 context = &context_struct;
1078                 context_struct.base_frame = &frame;
1079                 context_struct.current_frame = NULL;
1080                 context_struct.env_frame = &frame;
1081                 context_struct.current_env = &env;
1082                 context_struct.search_for_handler = 0;
1083                 context_struct.managed_code = 0;
1084                 TlsSetValue (thread_context_id, context);
1085         }
1086         else
1087                 old_frame = context->current_frame;
1088
1089         context->domain = mono_domain_get ();
1090
1091         switch (sig->ret->type) {
1092         case MONO_TYPE_VOID:
1093                 break;
1094         case MONO_TYPE_STRING:
1095         case MONO_TYPE_OBJECT:
1096         case MONO_TYPE_CLASS:
1097         case MONO_TYPE_ARRAY:
1098         case MONO_TYPE_SZARRAY:
1099                 isobject = 1;
1100                 break;
1101         case MONO_TYPE_VALUETYPE:
1102                 retval = mono_object_new (context->domain, klass);
1103                 ret = ((char*)retval) + sizeof (MonoObject);
1104                 if (!sig->ret->data.klass->enumtype)
1105                         result.data.vt = ret;
1106                 break;
1107         default:
1108                 retval = mono_object_new (context->domain, klass);
1109                 ret = ((char*)retval) + sizeof (MonoObject);
1110                 break;
1111         }
1112
1113         for (i = 0; i < sig->param_count; ++i) {
1114                 if (sig->params [i]->byref) {
1115                         args [i].data.p = params [i];
1116                         continue;
1117                 }
1118                 type = sig->params [i]->type;
1119 handle_enum:
1120                 switch (type) {
1121                 case MONO_TYPE_U1:
1122                 case MONO_TYPE_I1:
1123                 case MONO_TYPE_BOOLEAN:
1124                         args [i].data.i = *(MonoBoolean*)params [i];
1125                         break;
1126                 case MONO_TYPE_U2:
1127                 case MONO_TYPE_I2:
1128                 case MONO_TYPE_CHAR:
1129                         args [i].data.i = *(gint16*)params [i];
1130                         break;
1131 #if SIZEOF_VOID_P == 4
1132                 case MONO_TYPE_U: /* use VAL_POINTER? */
1133                 case MONO_TYPE_I:
1134 #endif
1135                 case MONO_TYPE_U4:
1136                 case MONO_TYPE_I4:
1137                         args [i].data.i = *(gint32*)params [i];
1138                         break;
1139 #if SIZEOF_VOID_P == 8
1140                 case MONO_TYPE_U:
1141                 case MONO_TYPE_I:
1142 #endif
1143                 case MONO_TYPE_U8:
1144                 case MONO_TYPE_I8:
1145                         args [i].data.l = *(gint64*)params [i];
1146                         break;
1147                 case MONO_TYPE_VALUETYPE:
1148                         if (sig->params [i]->data.klass->enumtype) {
1149                                 type = sig->params [i]->data.klass->enum_basetype->type;
1150                                 goto handle_enum;
1151                         } else {
1152                                 args [i].data.p = params [i];
1153                         }
1154                         break;
1155                 case MONO_TYPE_STRING:
1156                 case MONO_TYPE_CLASS:
1157                 case MONO_TYPE_ARRAY:
1158                 case MONO_TYPE_SZARRAY:
1159                 case MONO_TYPE_OBJECT:
1160                         args [i].data.p = params [i];
1161                         break;
1162                 default:
1163                         g_error ("type 0x%x not handled in  runtime invoke", sig->params [i]->type);
1164                 }
1165         }
1166
1167         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
1168                 method = mono_marshal_get_native_wrapper (method);
1169         INIT_FRAME(&frame,context->current_frame,obj,args,&result,method);
1170         if (exc)
1171                 frame.invoke_trap = 1;
1172         context->managed_code = 1;
1173         ves_exec_method_with_context (&frame, context);
1174         context->managed_code = 0;
1175         if (context == &context_struct)
1176                 TlsSetValue (thread_context_id, NULL);
1177         else
1178                 context->current_frame = old_frame;
1179         if (frame.ex != NULL) {
1180                 if (exc != NULL) {
1181                         *exc = (MonoObject*) frame.ex;
1182                         return NULL;
1183                 }
1184                 if (context->current_env != NULL) {
1185                         context->env_frame->ex = frame.ex;
1186                         longjmp(*context->current_env, 1);
1187                 }
1188                 else
1189                         printf("dropped exception...\n");
1190         }
1191         if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1192                 return NULL;
1193         if (isobject || method->string_ctor)
1194                 return result.data.p;
1195         stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1196         return retval;
1197 }
1198
1199 static stackval * 
1200 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1201 {
1202         MonoInvocation *old_frame = context->current_frame;
1203         MonoInvocation *old_env_frame = context->env_frame;
1204         jmp_buf *old_env = context->current_env;
1205         jmp_buf env;
1206
1207         if (setjmp (env)) {
1208                 context->current_frame = old_frame;
1209                 context->env_frame = old_env_frame;
1210                 context->current_env = old_env;
1211                 context->managed_code = 1;
1212                 return;
1213         }
1214
1215         context->env_frame = context->current_frame;
1216         context->current_env = &env;
1217         context->managed_code = 0;
1218
1219         switch (op) {
1220         case MINT_ICALL_P_V: {
1221                 void (*func)(gpointer) = ptr;
1222                 func (sp [-1].data.p);
1223                 sp --;
1224                 break;
1225         }
1226         case MINT_ICALL_P_P: {
1227                 gpointer (*func)(gpointer) = ptr;
1228                 sp [-1].data.p = func (sp [-1].data.p);
1229                 break;
1230         }
1231         case MINT_ICALL_PP_V: {
1232                 void (*func)(gpointer,gpointer) = ptr;
1233                 sp -= 2;
1234                 func (sp [0].data.p, sp [1].data.p);
1235                 break;
1236         }
1237         case MINT_ICALL_PI_V: {
1238                 void (*func)(gpointer,int) = ptr;
1239                 sp -= 2;
1240                 func (sp [0].data.p, sp [1].data.i);
1241                 break;
1242         }
1243         case MINT_ICALL_PP_P: {
1244                 gpointer (*func)(gpointer,gpointer) = ptr;
1245                 --sp;
1246                 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1247                 break;
1248         }
1249         case MINT_ICALL_PI_P: {
1250                 gpointer (*func)(gpointer,int) = ptr;
1251                 --sp;
1252                 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1253                 break;
1254         }
1255         case MINT_ICALL_PPP_V: {
1256                 void (*func)(gpointer,gpointer,gpointer) = ptr;
1257                 sp -= 3;
1258                 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1259                 break;
1260         }
1261         case MINT_ICALL_PPI_V: {
1262                 void (*func)(gpointer,gpointer,int) = ptr;
1263                 sp -= 3;
1264                 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1265                 break;
1266         }
1267         default:
1268                 g_assert_not_reached ();
1269         }
1270
1271         context->env_frame = old_env_frame;
1272         context->current_env = old_env;
1273
1274         return sp;
1275 }
1276
1277 static CRITICAL_SECTION create_method_pointer_mutex;
1278
1279 static MonoGHashTable *method_pointer_hash = NULL;
1280
1281 static void *
1282 mono_create_method_pointer (MonoMethod *method)
1283 {
1284         gpointer addr;
1285         MonoJitInfo *ji;
1286
1287         EnterCriticalSection (&create_method_pointer_mutex);
1288         if (!method_pointer_hash) {
1289                 MONO_GC_REGISTER_ROOT (method_pointer_hash);
1290                 method_pointer_hash = mono_g_hash_table_new (NULL, NULL);
1291         }
1292         addr = mono_g_hash_table_lookup (method_pointer_hash, method);
1293         if (addr) {
1294                 LeaveCriticalSection (&create_method_pointer_mutex);
1295                 return addr;
1296         }
1297
1298         /*
1299          * If it is a static P/Invoke method, we can just return the pointer
1300          * to the method implementation.
1301          */
1302         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && method->addr) {
1303                 ji = g_new0 (MonoJitInfo, 1);
1304                 ji->method = method;
1305                 ji->code_size = 1;
1306                 ji->code_start = method->addr;
1307
1308                 mono_jit_info_table_add (mono_get_root_domain (), ji);
1309                 
1310                 addr = method->addr;
1311         }               
1312         else
1313                 addr = mono_arch_create_method_pointer (method);
1314
1315         mono_g_hash_table_insert (method_pointer_hash, method, addr);
1316         LeaveCriticalSection (&create_method_pointer_mutex);
1317
1318         return addr;
1319 }
1320
1321 #if COUNT_OPS
1322 static int opcode_counts[512];
1323
1324 #define COUNT_OP(op) opcode_counts[op]++
1325 #else
1326 #define COUNT_OP(op) 
1327 #endif
1328
1329 #if DEBUG_INTERP
1330 #define DUMP_INSTR() \
1331         if (tracing > 1) { \
1332                 char *ins; \
1333                 if (sp > frame->stack) { \
1334                         ins = dump_stack (frame->stack, sp); \
1335                 } else { \
1336                         ins = g_strdup (""); \
1337                 } \
1338                 sp->data.l = 0; \
1339                 output_indent (); \
1340                 g_print ("(%u) ", GetCurrentThreadId()); \
1341                 mono_interp_dis_mintop(rtm->code, ip); \
1342                 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1343                 g_free (ins); \
1344         }
1345 #else
1346 #define DUMP_INSTR()
1347 #endif
1348
1349 #ifdef __GNUC__
1350 #define USE_COMPUTED_GOTO 1
1351 #endif
1352 #if USE_COMPUTED_GOTO
1353 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1354 #define MINT_IN_CASE(x) LAB_ ## x:
1355 #if DEBUG_INTERP
1356 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1357 #else
1358 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1359 #endif
1360 #define MINT_IN_DEFAULT mint_default:
1361 #else
1362 #define MINT_IN_SWITCH(op) switch (op)
1363 #define MINT_IN_CASE(x) case x:
1364 #define MINT_IN_BREAK break
1365 #define MINT_IN_DEFAULT default:
1366 #endif
1367
1368 /* 
1369  * Defining this causes register allocation errors in some versions of gcc:
1370  * error: unable to find a register to spill in class `SIREG'
1371  */
1372 /* #define MINT_USE_DEDICATED_IP_REG */
1373
1374 static void 
1375 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1376 {
1377         MonoInvocation child_frame;
1378         GSList *finally_ips = NULL;
1379         const unsigned short *endfinally_ip = NULL;
1380 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1381         register const unsigned short *ip asm ("%esi");
1382 #else
1383         register const unsigned short *ip;
1384 #endif
1385         register stackval *sp;
1386         RuntimeMethod *rtm;
1387 #if DEBUG_INTERP
1388         gint tracing = global_tracing;
1389         unsigned char *vtalloc;
1390 #endif
1391         int i32;
1392         unsigned char *vt_sp;
1393         char *locals;
1394         MonoObject *o = NULL;
1395         MonoClass *c;
1396 #if USE_COMPUTED_GOTO
1397         static void *in_labels[] = {
1398 #define OPDEF(a,b,c,d) \
1399         &&LAB_ ## a,
1400 #include "mintops.def"
1401         0 };
1402 #endif
1403
1404         frame->ex = NULL;
1405         frame->ex_handler = NULL;
1406         frame->ip = NULL;
1407         context->current_frame = frame;
1408
1409         DEBUG_ENTER ();
1410
1411         if (!frame->runtime_method->transformed) {
1412                 context->managed_code = 0;
1413                 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1414                 context->managed_code = 1;
1415                 if (frame->ex) {
1416                         rtm = NULL;
1417                         ip = NULL;
1418                         goto exit_frame;
1419                 }
1420         }
1421
1422         rtm = frame->runtime_method;
1423         frame->args = alloca (rtm->alloca_size);
1424         sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1425 #if DEBUG_INTERP
1426         if (tracing > 1)
1427                 memset(sp, 0, rtm->stack_size);
1428 #endif
1429         vt_sp = (char *)sp + rtm->stack_size;
1430 #if DEBUG_INTERP
1431         vtalloc = vt_sp;
1432 #endif
1433         locals = vt_sp + rtm->vt_stack_size;
1434
1435         child_frame.parent = frame;
1436
1437         /* ready to go */
1438         ip = rtm->code;
1439
1440         /*
1441          * using while (ip < end) may result in a 15% performance drop, 
1442          * but it may be useful for debug
1443          */
1444         while (1) {
1445         main_loop:
1446                 /* g_assert (sp >= frame->stack); */
1447                 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1448                 DUMP_INSTR();
1449                 MINT_IN_SWITCH (*ip) {
1450                 MINT_IN_CASE(MINT_INITLOCALS)
1451                         memset (locals, 0, rtm->locals_size);
1452                         ++ip;
1453                         MINT_IN_BREAK;
1454                 MINT_IN_CASE(MINT_NOP)
1455                         ++ip;
1456                         MINT_IN_BREAK;
1457                 MINT_IN_CASE(MINT_BREAK)
1458                         ++ip;
1459                         G_BREAKPOINT (); /* this is not portable... */
1460                         MINT_IN_BREAK;
1461                 MINT_IN_CASE(MINT_LDNULL) 
1462                         sp->data.p = NULL;
1463                         ++ip;
1464                         ++sp;
1465                         MINT_IN_BREAK;
1466                 MINT_IN_CASE(MINT_VTRESULT) {
1467                         int ret_size = * (guint16 *)(ip + 1);
1468                         char *ret_vt_sp = vt_sp;
1469                         vt_sp -= READ32(ip + 2);
1470                         if (ret_size > 0) {
1471                                 memmove (vt_sp, ret_vt_sp, ret_size);
1472                                 vt_sp += (ret_size + 7) & ~7;
1473                         }
1474                         ip += 4;
1475                         MINT_IN_BREAK;
1476                 }
1477 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1478                 MINT_IN_CASE(MINT_LDC_I4_M1)
1479                         LDC(-1);
1480                         MINT_IN_BREAK;
1481                 MINT_IN_CASE(MINT_LDC_I4_0)
1482                         LDC(0);
1483                         MINT_IN_BREAK;
1484                 MINT_IN_CASE(MINT_LDC_I4_1)
1485                         LDC(1);
1486                         MINT_IN_BREAK;
1487                 MINT_IN_CASE(MINT_LDC_I4_2)
1488                         LDC(2);
1489                         MINT_IN_BREAK;
1490                 MINT_IN_CASE(MINT_LDC_I4_3)
1491                         LDC(3);
1492                         MINT_IN_BREAK;
1493                 MINT_IN_CASE(MINT_LDC_I4_4)
1494                         LDC(4);
1495                         MINT_IN_BREAK;
1496                 MINT_IN_CASE(MINT_LDC_I4_5)
1497                         LDC(5);
1498                         MINT_IN_BREAK;
1499                 MINT_IN_CASE(MINT_LDC_I4_6)
1500                         LDC(6);
1501                         MINT_IN_BREAK;
1502                 MINT_IN_CASE(MINT_LDC_I4_7)
1503                         LDC(7);
1504                         MINT_IN_BREAK;
1505                 MINT_IN_CASE(MINT_LDC_I4_8)
1506                         LDC(8);
1507                         MINT_IN_BREAK;
1508                 MINT_IN_CASE(MINT_LDC_I4_S) 
1509                         sp->data.i = *(const short *)(ip + 1);
1510                         ip += 2;
1511                         ++sp;
1512                         MINT_IN_BREAK;
1513                 MINT_IN_CASE(MINT_LDC_I4)
1514                         ++ip;
1515                         sp->data.i = READ32 (ip);
1516                         ip += 2;
1517                         ++sp;
1518                         MINT_IN_BREAK;
1519                 MINT_IN_CASE(MINT_LDC_I8)
1520                         ++ip;
1521                         sp->data.l = READ64 (ip);
1522                         ip += 4;
1523                         ++sp;
1524                         MINT_IN_BREAK;
1525                 MINT_IN_CASE(MINT_LDC_R4) {
1526                         guint32 val;
1527                         ++ip;
1528                         val = READ32(ip);
1529                         sp->data.f = * (float *)&val;
1530                         ip += 2;
1531                         ++sp;
1532                         MINT_IN_BREAK;
1533                 }
1534                 MINT_IN_CASE(MINT_LDC_R8) 
1535                         sp->data.l = READ64 (ip + 1); /* note union usage */
1536                         ip += 5;
1537                         ++sp;
1538                         MINT_IN_BREAK;
1539                 MINT_IN_CASE(MINT_DUP) 
1540                         sp [0] = sp[-1];
1541                         ++sp;
1542                         ++ip; 
1543                         MINT_IN_BREAK;
1544                 MINT_IN_CASE(MINT_DUP_VT)
1545                         i32 = READ32 (ip + 1);
1546                         sp->data.p = vt_sp;
1547                         memcpy(sp->data.p, sp [-1].data.p, i32);
1548                         vt_sp += (i32 + 7) & ~7;
1549                         ++sp;
1550                         ip += 3;
1551                         MINT_IN_BREAK;
1552                 MINT_IN_CASE(MINT_POP)
1553                         ++ip;
1554                         --sp;
1555                         MINT_IN_BREAK;
1556                 MINT_IN_CASE(MINT_JMP) {
1557                         RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1558                         if (!new_method->transformed) {
1559                                 frame->ip = ip;
1560                                 frame->ex = mono_interp_transform_method (new_method, context);
1561                                 if (frame->ex)
1562                                         goto exit_frame;
1563                         }
1564                         ip += 2;
1565                         if (new_method->alloca_size > rtm->alloca_size)
1566                                 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size); 
1567                         rtm = frame->runtime_method = new_method;
1568                         vt_sp = (char *)sp + rtm->stack_size;
1569 #if DEBUG_INTERP
1570                         vtalloc = vt_sp;
1571 #endif
1572                         locals = vt_sp + rtm->vt_stack_size;
1573                         ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1574                         MINT_IN_BREAK;
1575                 }
1576                 MINT_IN_CASE(MINT_CALLI) {
1577                         MonoMethodSignature *csignature;
1578                         stackval *endsp = sp;
1579
1580                         frame->ip = ip;
1581                         
1582                         csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1583                         ip += 2;
1584                         --sp;
1585                         --endsp;
1586                         child_frame.runtime_method = sp->data.p;
1587
1588                         sp->data.p = vt_sp;
1589                         child_frame.retval = sp;
1590                         /* decrement by the actual number of args */
1591                         sp -= csignature->param_count;
1592                         child_frame.stack_args = sp;
1593                         if (csignature->hasthis) {
1594                                 --sp;
1595                                 child_frame.obj = sp->data.p;
1596                         } else {
1597                                 child_frame.obj = NULL;
1598                         }
1599                         if (csignature->hasthis &&
1600                                         ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1601                                 child_frame.runtime_method = mono_interp_get_runtime_method (
1602                                                                 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1603                         } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
1604                                 child_frame.runtime_method = mono_interp_get_runtime_method (
1605                                                                 mono_marshal_get_native_wrapper (child_frame.runtime_method->method));
1606
1607                         ves_exec_method_with_context (&child_frame, context);
1608
1609                         context->current_frame = frame;
1610
1611                         if (child_frame.ex) {
1612                                 /*
1613                                  * An exception occurred, need to run finally, fault and catch handlers..
1614                                  */
1615                                 frame->ex = child_frame.ex;
1616                                 goto handle_finally;
1617                         }
1618
1619                         /* need to handle typedbyref ... */
1620                         if (csignature->ret->type != MONO_TYPE_VOID) {
1621                                 *sp = *endsp;
1622                                 sp++;
1623                         }
1624                         MINT_IN_BREAK;
1625                 }
1626                 MINT_IN_CASE(MINT_CALLI_NAT) {
1627                         MonoMethodSignature *csignature;
1628                         stackval *endsp = sp;
1629                         unsigned char *code = NULL;
1630
1631                         frame->ip = ip;
1632                         
1633                         csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1634                         ip += 2;
1635                         --sp;
1636                         --endsp;
1637                         code = sp->data.p;
1638                         child_frame.runtime_method = NULL;
1639
1640                         sp->data.p = vt_sp;
1641                         child_frame.retval = sp;
1642                         /* decrement by the actual number of args */
1643                         sp -= csignature->param_count;
1644                         child_frame.stack_args = sp;
1645                         if (csignature->hasthis) {
1646                                 --sp;
1647                                 child_frame.obj = sp->data.p;
1648                         } else {
1649                                 child_frame.obj = NULL;
1650                         }
1651                         ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE, context);
1652
1653                         context->current_frame = frame;
1654
1655                         if (child_frame.ex) {
1656                                 /*
1657                                  * An exception occurred, need to run finally, fault and catch handlers..
1658                                  */
1659                                 frame->ex = child_frame.ex;
1660                                 if (context->search_for_handler) {
1661                                         context->search_for_handler = 0;
1662                                         goto handle_exception;
1663                                 }
1664                                 goto handle_finally;
1665                         }
1666
1667                         /* need to handle typedbyref ... */
1668                         if (csignature->ret->type != MONO_TYPE_VOID) {
1669                                 *sp = *endsp;
1670                                 sp++;
1671                         }
1672                         MINT_IN_BREAK;
1673                 }
1674                 MINT_IN_CASE(MINT_CALL) {
1675                         stackval *endsp = sp;
1676
1677                         frame->ip = ip;
1678                         
1679                         child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1680                         ip += 2;
1681                         sp->data.p = vt_sp;
1682                         child_frame.retval = sp;
1683                         /* decrement by the actual number of args */
1684                         sp -= child_frame.runtime_method->param_count;
1685                         child_frame.stack_args = sp;
1686                         if (child_frame.runtime_method->hasthis) {
1687                                 --sp;
1688                                 child_frame.obj = sp->data.p;
1689                         } else {
1690                                 child_frame.obj = NULL;
1691                         }
1692                         if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype &&
1693                                         ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1694                                 child_frame.runtime_method = mono_interp_get_runtime_method (
1695                                                                 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1696                         }
1697                         ves_exec_method_with_context (&child_frame, context);
1698
1699                         context->current_frame = frame;
1700
1701                         if (child_frame.ex) {
1702                                 /*
1703                                  * An exception occurred, need to run finally, fault and catch handlers..
1704                                  */
1705                                 frame->ex = child_frame.ex;
1706                                 goto handle_finally;
1707                         }
1708
1709                         /* need to handle typedbyref ... */
1710                         *sp = *endsp;
1711                         sp++;
1712                         MINT_IN_BREAK;
1713                 }
1714                 MINT_IN_CASE(MINT_VCALL) {
1715                         frame->ip = ip;
1716                         
1717                         child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1718                         ip += 2;
1719
1720                         sp->data.p = vt_sp;
1721                         child_frame.retval = sp;
1722                         /* decrement by the actual number of args */
1723                         sp -= child_frame.runtime_method->param_count;
1724                         child_frame.stack_args = sp;
1725                         if (child_frame.runtime_method->hasthis) {
1726                                 --sp;
1727                                 child_frame.obj = sp->data.p;
1728                         } else {
1729                                 child_frame.obj = NULL;
1730                         }
1731
1732                         if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype &&
1733                                         ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1734                                 child_frame.runtime_method = mono_interp_get_runtime_method (
1735                                                                 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1736                         }
1737
1738                         ves_exec_method_with_context (&child_frame, context);
1739
1740                         context->current_frame = frame;
1741
1742                         if (child_frame.ex) {
1743                                 /*
1744                                  * An exception occurred, need to run finally, fault and catch handlers..
1745                                  */
1746                                 frame->ex = child_frame.ex;
1747                                 goto handle_finally;
1748                         }
1749                         MINT_IN_BREAK;
1750                 }
1751                 MINT_IN_CASE(MINT_CALLVIRT) {
1752                         stackval *endsp = sp;
1753                         MonoObject *this_arg;
1754                         guint32 token;
1755
1756                         frame->ip = ip;
1757                         
1758                         token = * (unsigned short *)(ip + 1);
1759                         ip += 2;
1760                         child_frame.runtime_method = rtm->data_items [token];
1761                         sp->data.p = vt_sp;
1762                         child_frame.retval = sp;
1763
1764                         /* decrement by the actual number of args */
1765                         sp -= child_frame.runtime_method->param_count;
1766                         child_frame.stack_args = sp;
1767                         --sp;
1768                         child_frame.obj = this_arg = sp->data.p;
1769                         if (!this_arg)
1770                                 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1771                         child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
1772
1773                         if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1774                                 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1775                         }
1776
1777                         ves_exec_method_with_context (&child_frame, context);
1778
1779                         context->current_frame = frame;
1780
1781                         if (child_frame.ex) {
1782                                 /*
1783                                  * An exception occurred, need to run finally, fault and catch handlers..
1784                                  */
1785                                 frame->ex = child_frame.ex;
1786                                 if (context->search_for_handler) {
1787                                         context->search_for_handler = 0;
1788                                         goto handle_exception;
1789                                 }
1790                                 goto handle_finally;
1791                         }
1792
1793                         /* need to handle typedbyref ... */
1794                         *sp = *endsp;
1795                         sp++;
1796                         MINT_IN_BREAK;
1797                 }
1798                 MINT_IN_CASE(MINT_VCALLVIRT) {
1799                         MonoObject *this_arg;
1800                         guint32 token;
1801
1802                         frame->ip = ip;
1803                         
1804                         token = * (unsigned short *)(ip + 1);
1805                         ip += 2;
1806                         child_frame.runtime_method = rtm->data_items [token];
1807                         sp->data.p = vt_sp;
1808                         child_frame.retval = sp;
1809
1810                         /* decrement by the actual number of args */
1811                         sp -= child_frame.runtime_method->param_count;
1812                         child_frame.stack_args = sp;
1813                         --sp;
1814                         child_frame.obj = this_arg = sp->data.p;
1815                         if (!this_arg)
1816                                 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1817                         child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
1818
1819                         if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1820                                 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1821                         }
1822
1823                         ves_exec_method_with_context (&child_frame, context);
1824
1825                         context->current_frame = frame;
1826
1827                         if (child_frame.ex) {
1828                                 /*
1829                                  * An exception occurred, need to run finally, fault and catch handlers..
1830                                  */
1831                                 frame->ex = child_frame.ex;
1832                                 if (context->search_for_handler) {
1833                                         context->search_for_handler = 0;
1834                                         goto handle_exception;
1835                                 }
1836                                 goto handle_finally;
1837                         }
1838                         MINT_IN_BREAK;
1839                 }
1840                 MINT_IN_CASE(MINT_CALLINT)
1841                         ves_pinvoke_method (frame, frame->runtime_method->method->signature, frame->runtime_method->method->addr, 
1842                                     frame->runtime_method->method->string_ctor, context);
1843                         if (frame->ex) {
1844                                 rtm = NULL;
1845                                 goto handle_exception;
1846                         }
1847                         goto exit_frame;
1848                 MINT_IN_CASE(MINT_CALLRUN)
1849                         ves_runtime_method (frame, context);
1850                         if (frame->ex) {
1851                                 rtm = NULL;
1852                                 goto handle_exception;
1853                         }
1854                         goto exit_frame;
1855                 MINT_IN_CASE(MINT_RET)
1856                         --sp;
1857                         *frame->retval = *sp;
1858                         if (sp > frame->stack)
1859                                 g_warning ("ret: more values on stack: %d", sp-frame->stack);
1860                         goto exit_frame;
1861                 MINT_IN_CASE(MINT_RET_VOID)
1862                         if (sp > frame->stack)
1863                                 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
1864                         goto exit_frame;
1865                 MINT_IN_CASE(MINT_RET_VT)
1866                         i32 = READ32(ip + 1);
1867                         --sp;
1868                         memcpy(frame->retval->data.p, sp->data.p, i32);
1869                         if (sp > frame->stack)
1870                                 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
1871                         goto exit_frame;
1872                 MINT_IN_CASE(MINT_BR_S)
1873                         ip += (short) *(ip + 1);
1874                         MINT_IN_BREAK;
1875                 MINT_IN_CASE(MINT_BR)
1876                         ip += (gint32) READ32(ip + 1);
1877                         MINT_IN_BREAK;
1878 #define ZEROP_S(datamem, op) \
1879         --sp; \
1880         if (sp->data.datamem op 0) \
1881                 ip += * (gint16 *)(ip + 1); \
1882         else \
1883                 ip += 2;
1884
1885 #define ZEROP(datamem, op) \
1886         --sp; \
1887         if (sp->data.datamem op 0) \
1888                 ip += READ32(ip + 1); \
1889         else \
1890                 ip += 3;
1891
1892                 MINT_IN_CASE(MINT_BRFALSE_I4_S)
1893                         ZEROP_S(i, ==);
1894                         MINT_IN_BREAK;
1895                 MINT_IN_CASE(MINT_BRFALSE_I8_S)
1896                         ZEROP_S(l, ==);
1897                         MINT_IN_BREAK;
1898                 MINT_IN_CASE(MINT_BRFALSE_R8_S)
1899                         ZEROP_S(f, ==);
1900                         MINT_IN_BREAK;
1901                 MINT_IN_CASE(MINT_BRFALSE_I4)
1902                         ZEROP(i, ==);
1903                         MINT_IN_BREAK;
1904                 MINT_IN_CASE(MINT_BRFALSE_I8)
1905                         ZEROP(l, ==);
1906                         MINT_IN_BREAK;
1907                 MINT_IN_CASE(MINT_BRFALSE_R8)
1908                         ZEROP_S(f, ==);
1909                         MINT_IN_BREAK;
1910                 MINT_IN_CASE(MINT_BRTRUE_I4_S)
1911                         ZEROP_S(i, !=);
1912                         MINT_IN_BREAK;
1913                 MINT_IN_CASE(MINT_BRTRUE_I8_S)
1914                         ZEROP_S(l, !=);
1915                         MINT_IN_BREAK;
1916                 MINT_IN_CASE(MINT_BRTRUE_R8_S)
1917                         ZEROP_S(f, !=);
1918                         MINT_IN_BREAK;
1919                 MINT_IN_CASE(MINT_BRTRUE_I4)
1920                         ZEROP(i, !=);
1921                         MINT_IN_BREAK;
1922                 MINT_IN_CASE(MINT_BRTRUE_I8)
1923                         ZEROP(l, !=);
1924                         MINT_IN_BREAK;
1925                 MINT_IN_CASE(MINT_BRTRUE_R8)
1926                         ZEROP(f, !=);
1927                         MINT_IN_BREAK;
1928 #define CONDBR_S(cond) \
1929         sp -= 2; \
1930         if (cond) \
1931                 ip += * (gint16 *)(ip + 1); \
1932         else \
1933                 ip += 2;
1934 #define BRELOP_S(datamem, op) \
1935         CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
1936
1937 #define CONDBR(cond) \
1938         sp -= 2; \
1939         if (cond) \
1940                 ip += READ32(ip + 1); \
1941         else \
1942                 ip += 3;
1943
1944 #define BRELOP(datamem, op) \
1945         CONDBR(sp[0].data.datamem op sp[1].data.datamem)
1946
1947                 MINT_IN_CASE(MINT_BEQ_I4_S)
1948                         BRELOP_S(i, ==)
1949                         MINT_IN_BREAK;
1950                 MINT_IN_CASE(MINT_BEQ_I8_S)
1951                         BRELOP_S(l, ==)
1952                         MINT_IN_BREAK;
1953                 MINT_IN_CASE(MINT_BEQ_R8_S)
1954                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
1955                         MINT_IN_BREAK;
1956                 MINT_IN_CASE(MINT_BEQ_I4)
1957                         BRELOP(i, ==)
1958                         MINT_IN_BREAK;
1959                 MINT_IN_CASE(MINT_BEQ_I8)
1960                         BRELOP(l, ==)
1961                         MINT_IN_BREAK;
1962                 MINT_IN_CASE(MINT_BEQ_R8)
1963                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
1964                         MINT_IN_BREAK;
1965                 MINT_IN_CASE(MINT_BGE_I4_S)
1966                         BRELOP_S(i, >=)
1967                         MINT_IN_BREAK;
1968                 MINT_IN_CASE(MINT_BGE_I8_S)
1969                         BRELOP_S(l, >=)
1970                         MINT_IN_BREAK;
1971                 MINT_IN_CASE(MINT_BGE_R8_S)
1972                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
1973                         MINT_IN_BREAK;
1974                 MINT_IN_CASE(MINT_BGE_I4)
1975                         BRELOP(i, >=)
1976                         MINT_IN_BREAK;
1977                 MINT_IN_CASE(MINT_BGE_I8)
1978                         BRELOP(l, >=)
1979                         MINT_IN_BREAK;
1980                 MINT_IN_CASE(MINT_BGE_R8)
1981                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
1982                         MINT_IN_BREAK;
1983                 MINT_IN_CASE(MINT_BGT_I4_S)
1984                         BRELOP_S(i, >)
1985                         MINT_IN_BREAK;
1986                 MINT_IN_CASE(MINT_BGT_I8_S)
1987                         BRELOP_S(l, >)
1988                         MINT_IN_BREAK;
1989                 MINT_IN_CASE(MINT_BGT_R8_S)
1990                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
1991                         MINT_IN_BREAK;
1992                 MINT_IN_CASE(MINT_BGT_I4)
1993                         BRELOP(i, >)
1994                         MINT_IN_BREAK;
1995                 MINT_IN_CASE(MINT_BGT_I8)
1996                         BRELOP(l, >)
1997                         MINT_IN_BREAK;
1998                 MINT_IN_CASE(MINT_BGT_R8)
1999                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2000                         MINT_IN_BREAK;
2001                 MINT_IN_CASE(MINT_BLT_I4_S)
2002                         BRELOP_S(i, <)
2003                         MINT_IN_BREAK;
2004                 MINT_IN_CASE(MINT_BLT_I8_S)
2005                         BRELOP_S(l, <)
2006                         MINT_IN_BREAK;
2007                 MINT_IN_CASE(MINT_BLT_R8_S)
2008                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2009                         MINT_IN_BREAK;
2010                 MINT_IN_CASE(MINT_BLT_I4)
2011                         BRELOP(i, <)
2012                         MINT_IN_BREAK;
2013                 MINT_IN_CASE(MINT_BLT_I8)
2014                         BRELOP(l, <)
2015                         MINT_IN_BREAK;
2016                 MINT_IN_CASE(MINT_BLT_R8)
2017                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2018                         MINT_IN_BREAK;
2019                 MINT_IN_CASE(MINT_BLE_I4_S)
2020                         BRELOP_S(i, <=)
2021                         MINT_IN_BREAK;
2022                 MINT_IN_CASE(MINT_BLE_I8_S)
2023                         BRELOP_S(l, <=)
2024                         MINT_IN_BREAK;
2025                 MINT_IN_CASE(MINT_BLE_R8_S)
2026                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2027                         MINT_IN_BREAK;
2028                 MINT_IN_CASE(MINT_BLE_I4)
2029                         BRELOP(i, <=)
2030                         MINT_IN_BREAK;
2031                 MINT_IN_CASE(MINT_BLE_I8)
2032                         BRELOP(l, <=)
2033                         MINT_IN_BREAK;
2034                 MINT_IN_CASE(MINT_BLE_R8)
2035                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2036                         MINT_IN_BREAK;
2037                 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2038                         BRELOP_S(i, !=)
2039                         MINT_IN_BREAK;
2040                 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2041                         BRELOP_S(l, !=)
2042                         MINT_IN_BREAK;
2043                 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2044                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2045                         MINT_IN_BREAK;
2046                 MINT_IN_CASE(MINT_BNE_UN_I4)
2047                         BRELOP(i, !=)
2048                         MINT_IN_BREAK;
2049                 MINT_IN_CASE(MINT_BNE_UN_I8)
2050                         BRELOP(l, !=)
2051                         MINT_IN_BREAK;
2052                 MINT_IN_CASE(MINT_BNE_UN_R8)
2053                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2054                         MINT_IN_BREAK;
2055
2056 #define BRELOP_S_CAST(datamem, op, type) \
2057         sp -= 2; \
2058         if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2059                 ip += * (gint16 *)(ip + 1); \
2060         else \
2061                 ip += 2;
2062
2063 #define BRELOP_CAST(datamem, op, type) \
2064         sp -= 2; \
2065         if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2066                 ip += READ32(ip + 1); \
2067         else \
2068                 ip += 3;
2069
2070                 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2071                         BRELOP_S_CAST(i, >=, guint32);
2072                         MINT_IN_BREAK;
2073                 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2074                         BRELOP_S_CAST(l, >=, guint64);
2075                         MINT_IN_BREAK;
2076                 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2077                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2078                         MINT_IN_BREAK;
2079                 MINT_IN_CASE(MINT_BGE_UN_I4)
2080                         BRELOP_CAST(i, >=, guint32);
2081                         MINT_IN_BREAK;
2082                 MINT_IN_CASE(MINT_BGE_UN_I8)
2083                         BRELOP_CAST(l, >=, guint64);
2084                         MINT_IN_BREAK;
2085                 MINT_IN_CASE(MINT_BGE_UN_R8)
2086                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2087                         MINT_IN_BREAK;
2088                 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2089                         BRELOP_S_CAST(i, >, guint32);
2090                         MINT_IN_BREAK;
2091                 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2092                         BRELOP_S_CAST(l, >, guint64);
2093                         MINT_IN_BREAK;
2094                 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2095                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2096                         MINT_IN_BREAK;
2097                 MINT_IN_CASE(MINT_BGT_UN_I4)
2098                         BRELOP_CAST(i, >, guint32);
2099                         MINT_IN_BREAK;
2100                 MINT_IN_CASE(MINT_BGT_UN_I8)
2101                         BRELOP_CAST(l, >, guint64);
2102                         MINT_IN_BREAK;
2103                 MINT_IN_CASE(MINT_BGT_UN_R8)
2104                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2105                         MINT_IN_BREAK;
2106                 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2107                         BRELOP_S_CAST(i, <=, guint32);
2108                         MINT_IN_BREAK;
2109                 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2110                         BRELOP_S_CAST(l, <=, guint64);
2111                         MINT_IN_BREAK;
2112                 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2113                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2114                         MINT_IN_BREAK;
2115                 MINT_IN_CASE(MINT_BLE_UN_I4)
2116                         BRELOP_CAST(i, <=, guint32);
2117                         MINT_IN_BREAK;
2118                 MINT_IN_CASE(MINT_BLE_UN_I8)
2119                         BRELOP_CAST(l, <=, guint64);
2120                         MINT_IN_BREAK;
2121                 MINT_IN_CASE(MINT_BLE_UN_R8)
2122                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2123                         MINT_IN_BREAK;
2124                 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2125                         BRELOP_S_CAST(i, <, guint32);
2126                         MINT_IN_BREAK;
2127                 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2128                         BRELOP_S_CAST(l, <, guint64);
2129                         MINT_IN_BREAK;
2130                 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2131                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2132                         MINT_IN_BREAK;
2133                 MINT_IN_CASE(MINT_BLT_UN_I4)
2134                         BRELOP_CAST(i, <, guint32);
2135                         MINT_IN_BREAK;
2136                 MINT_IN_CASE(MINT_BLT_UN_I8)
2137                         BRELOP_CAST(l, <, guint64);
2138                         MINT_IN_BREAK;
2139                 MINT_IN_CASE(MINT_BLT_UN_R8)
2140                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2141                         MINT_IN_BREAK;
2142                 MINT_IN_CASE(MINT_SWITCH) {
2143                         guint32 n;
2144                         const unsigned short *st;
2145                         ++ip;
2146                         n = READ32 (ip);
2147                         ip += 2;
2148                         st = ip + 2 * n;
2149                         --sp;
2150                         if ((guint32)sp->data.i < n) {
2151                                 gint offset;
2152                                 ip += 2 * (guint32)sp->data.i;
2153                                 offset = READ32 (ip);
2154                                 ip = st + offset;
2155                         } else {
2156                                 ip = st;
2157                         }
2158                         MINT_IN_BREAK;
2159                 }
2160                 MINT_IN_CASE(MINT_LDIND_I1)
2161                         ++ip;
2162                         sp[-1].data.i = *(gint8*)sp[-1].data.p;
2163                         MINT_IN_BREAK;
2164                 MINT_IN_CASE(MINT_LDIND_U1)
2165                         ++ip;
2166                         sp[-1].data.i = *(guint8*)sp[-1].data.p;
2167                         MINT_IN_BREAK;
2168                 MINT_IN_CASE(MINT_LDIND_I2)
2169                         ++ip;
2170                         sp[-1].data.i = *(gint16*)sp[-1].data.p;
2171                         MINT_IN_BREAK;
2172                 MINT_IN_CASE(MINT_LDIND_U2)
2173                         ++ip;
2174                         sp[-1].data.i = *(guint16*)sp[-1].data.p;
2175                         MINT_IN_BREAK;
2176                 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2177                 MINT_IN_CASE(MINT_LDIND_U4)
2178                         ++ip;
2179                         sp[-1].data.i = *(gint32*)sp[-1].data.p;
2180                         MINT_IN_BREAK;
2181                 MINT_IN_CASE(MINT_LDIND_I8)
2182                         ++ip;
2183                         sp[-1].data.l = *(gint64*)sp[-1].data.p;
2184                         MINT_IN_BREAK;
2185                 MINT_IN_CASE(MINT_LDIND_I)
2186                         ++ip;
2187                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2188                         MINT_IN_BREAK;
2189                 MINT_IN_CASE(MINT_LDIND_R4)
2190                         ++ip;
2191                         sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2192                         MINT_IN_BREAK;
2193                 MINT_IN_CASE(MINT_LDIND_R8)
2194                         ++ip;
2195                         sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2196                         MINT_IN_BREAK;
2197                 MINT_IN_CASE(MINT_LDIND_REF)
2198                         ++ip;
2199                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2200                         MINT_IN_BREAK;
2201                 MINT_IN_CASE(MINT_STIND_REF) 
2202                         ++ip;
2203                         sp -= 2;
2204                         * (gpointer *) sp->data.p = sp[1].data.p;
2205                         MINT_IN_BREAK;
2206                 MINT_IN_CASE(MINT_STIND_I1)
2207                         ++ip;
2208                         sp -= 2;
2209                         * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2210                         MINT_IN_BREAK;
2211                 MINT_IN_CASE(MINT_STIND_I2)
2212                         ++ip;
2213                         sp -= 2;
2214                         * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2215                         MINT_IN_BREAK;
2216                 MINT_IN_CASE(MINT_STIND_I4)
2217                         ++ip;
2218                         sp -= 2;
2219                         * (gint32 *) sp->data.p = sp[1].data.i;
2220                         MINT_IN_BREAK;
2221                 MINT_IN_CASE(MINT_STIND_I)
2222                         ++ip;
2223                         sp -= 2;
2224                         * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2225                         MINT_IN_BREAK;
2226                 MINT_IN_CASE(MINT_STIND_I8)
2227                         ++ip;
2228                         sp -= 2;
2229                         * (gint64 *) sp->data.p = sp[1].data.l;
2230                         MINT_IN_BREAK;
2231                 MINT_IN_CASE(MINT_STIND_R4)
2232                         ++ip;
2233                         sp -= 2;
2234                         * (float *) sp->data.p = (gfloat)sp[1].data.f;
2235                         MINT_IN_BREAK;
2236                 MINT_IN_CASE(MINT_STIND_R8)
2237                         ++ip;
2238                         sp -= 2;
2239                         * (double *) sp->data.p = sp[1].data.f;
2240                         MINT_IN_BREAK;
2241 #define BINOP(datamem, op) \
2242         --sp; \
2243         sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2244         ++ip;
2245                 MINT_IN_CASE(MINT_ADD_I4)
2246                         BINOP(i, +);
2247                         MINT_IN_BREAK;
2248                 MINT_IN_CASE(MINT_ADD_I8)
2249                         BINOP(l, +);
2250                         MINT_IN_BREAK;
2251                 MINT_IN_CASE(MINT_ADD_R8)
2252                         BINOP(f, +);
2253                         MINT_IN_BREAK;
2254                 MINT_IN_CASE(MINT_ADD1_I4)
2255                         ++sp [-1].data.i;
2256                         ++ip;
2257                         MINT_IN_BREAK;
2258                 MINT_IN_CASE(MINT_SUB_I4)
2259                         BINOP(i, -);
2260                         MINT_IN_BREAK;
2261                 MINT_IN_CASE(MINT_SUB_I8)
2262                         BINOP(l, -);
2263                         MINT_IN_BREAK;
2264                 MINT_IN_CASE(MINT_SUB_R8)
2265                         BINOP(f, -);
2266                         MINT_IN_BREAK;
2267                 MINT_IN_CASE(MINT_SUB1_I4)
2268                         --sp [-1].data.i;
2269                         ++ip;
2270                         MINT_IN_BREAK;
2271                 MINT_IN_CASE(MINT_MUL_I4)
2272                         BINOP(i, *);
2273                         MINT_IN_BREAK;
2274                 MINT_IN_CASE(MINT_MUL_I8)
2275                         BINOP(l, *);
2276                         MINT_IN_BREAK;
2277                 MINT_IN_CASE(MINT_MUL_R8)
2278                         BINOP(f, *);
2279                         MINT_IN_BREAK;
2280                 MINT_IN_CASE(MINT_DIV_I4)
2281                         if (sp [-1].data.i == 0)
2282                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2283                         BINOP(i, /);
2284                         MINT_IN_BREAK;
2285                 MINT_IN_CASE(MINT_DIV_I8)
2286                         if (sp [-1].data.l == 0)
2287                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2288                         BINOP(l, /);
2289                         MINT_IN_BREAK;
2290                 MINT_IN_CASE(MINT_DIV_R8)
2291                         BINOP(f, /);
2292                         MINT_IN_BREAK;
2293
2294 #define BINOP_CAST(datamem, op, type) \
2295         --sp; \
2296         sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2297         ++ip;
2298                 MINT_IN_CASE(MINT_DIV_UN_I4)
2299                         if (sp [-1].data.i == 0)
2300                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2301                         BINOP_CAST(i, /, guint32);
2302                         MINT_IN_BREAK;
2303                 MINT_IN_CASE(MINT_DIV_UN_I8)
2304                         if (sp [-1].data.l == 0)
2305                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2306                         BINOP_CAST(l, /, guint64);
2307                         MINT_IN_BREAK;
2308                 MINT_IN_CASE(MINT_REM_I4)
2309                         if (sp [-1].data.i == 0)
2310                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2311                         BINOP(i, %);
2312                         MINT_IN_BREAK;
2313                 MINT_IN_CASE(MINT_REM_I8)
2314                         if (sp [-1].data.l == 0)
2315                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2316                         BINOP(l, %);
2317                         MINT_IN_BREAK;
2318                 MINT_IN_CASE(MINT_REM_R8)
2319                         /* FIXME: what do we actually do here? */
2320                         --sp;
2321                         sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2322                         ++ip;
2323                         MINT_IN_BREAK;
2324                 MINT_IN_CASE(MINT_REM_UN_I4)
2325                         if (sp [-1].data.i == 0)
2326                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2327                         BINOP_CAST(i, %, guint32);
2328                         MINT_IN_BREAK;
2329                 MINT_IN_CASE(MINT_REM_UN_I8)
2330                         if (sp [-1].data.l == 0)
2331                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2332                         BINOP_CAST(l, %, guint64);
2333                         MINT_IN_BREAK;
2334                 MINT_IN_CASE(MINT_AND_I4)
2335                         BINOP(i, &);
2336                         MINT_IN_BREAK;
2337                 MINT_IN_CASE(MINT_AND_I8)
2338                         BINOP(l, &);
2339                         MINT_IN_BREAK;
2340                 MINT_IN_CASE(MINT_OR_I4)
2341                         BINOP(i, |);
2342                         MINT_IN_BREAK;
2343                 MINT_IN_CASE(MINT_OR_I8)
2344                         BINOP(l, |);
2345                         MINT_IN_BREAK;
2346                 MINT_IN_CASE(MINT_XOR_I4)
2347                         BINOP(i, ^);
2348                         MINT_IN_BREAK;
2349                 MINT_IN_CASE(MINT_XOR_I8)
2350                         BINOP(l, ^);
2351                         MINT_IN_BREAK;
2352
2353 #define SHIFTOP(datamem, op) \
2354         --sp; \
2355         sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2356         ++ip;
2357
2358                 MINT_IN_CASE(MINT_SHL_I4)
2359                         SHIFTOP(i, <<);
2360                         MINT_IN_BREAK;
2361                 MINT_IN_CASE(MINT_SHL_I8)
2362                         SHIFTOP(l, <<);
2363                         MINT_IN_BREAK;
2364                 MINT_IN_CASE(MINT_SHR_I4)
2365                         SHIFTOP(i, >>);
2366                         MINT_IN_BREAK;
2367                 MINT_IN_CASE(MINT_SHR_I8)
2368                         SHIFTOP(l, >>);
2369                         MINT_IN_BREAK;
2370                 MINT_IN_CASE(MINT_SHR_UN_I4)
2371                         --sp;
2372                         sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2373                         ++ip;
2374                         MINT_IN_BREAK;
2375                 MINT_IN_CASE(MINT_SHR_UN_I8)
2376                         --sp;
2377                         sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2378                         ++ip;
2379                         MINT_IN_BREAK;
2380                 MINT_IN_CASE(MINT_NEG_I4)
2381                         sp [-1].data.i = - sp [-1].data.i;
2382                         ++ip;
2383                         MINT_IN_BREAK;
2384                 MINT_IN_CASE(MINT_NEG_I8)
2385                         sp [-1].data.l = - sp [-1].data.l;
2386                         ++ip;
2387                         MINT_IN_BREAK;
2388                 MINT_IN_CASE(MINT_NEG_R8)
2389                         sp [-1].data.f = - sp [-1].data.f;
2390                         ++ip;
2391                         MINT_IN_BREAK;
2392                 MINT_IN_CASE(MINT_NOT_I4)
2393                         sp [-1].data.i = ~ sp [-1].data.i;
2394                         ++ip;
2395                         MINT_IN_BREAK;
2396                 MINT_IN_CASE(MINT_NOT_I8)
2397                         sp [-1].data.l = ~ sp [-1].data.l;
2398                         ++ip;
2399                         MINT_IN_BREAK;
2400                 MINT_IN_CASE(MINT_CONV_I1_I4)
2401                         sp [-1].data.i = (gint8)sp [-1].data.i;
2402                         ++ip;
2403                         MINT_IN_BREAK;
2404                 MINT_IN_CASE(MINT_CONV_I1_I8)
2405                         sp [-1].data.i = (gint8)sp [-1].data.l;
2406                         ++ip;
2407                         MINT_IN_BREAK;
2408                 MINT_IN_CASE(MINT_CONV_I1_R8)
2409                         sp [-1].data.i = (gint8)sp [-1].data.f;
2410                         ++ip;
2411                         MINT_IN_BREAK;
2412                 MINT_IN_CASE(MINT_CONV_U1_I4)
2413                         sp [-1].data.i = (guint8)sp [-1].data.i;
2414                         ++ip;
2415                         MINT_IN_BREAK;
2416                 MINT_IN_CASE(MINT_CONV_U1_I8)
2417                         sp [-1].data.i = (guint8)sp [-1].data.l;
2418                         ++ip;
2419                         MINT_IN_BREAK;
2420                 MINT_IN_CASE(MINT_CONV_U1_R8)
2421                         sp [-1].data.i = (guint8)sp [-1].data.f;
2422                         ++ip;
2423                         MINT_IN_BREAK;
2424                 MINT_IN_CASE(MINT_CONV_I2_I4)
2425                         sp [-1].data.i = (gint16)sp [-1].data.i;
2426                         ++ip;
2427                         MINT_IN_BREAK;
2428                 MINT_IN_CASE(MINT_CONV_I2_I8)
2429                         sp [-1].data.i = (gint16)sp [-1].data.l;
2430                         ++ip;
2431                         MINT_IN_BREAK;
2432                 MINT_IN_CASE(MINT_CONV_I2_R8)
2433                         sp [-1].data.i = (gint16)sp [-1].data.f;
2434                         ++ip;
2435                         MINT_IN_BREAK;
2436                 MINT_IN_CASE(MINT_CONV_U2_I4)
2437                         sp [-1].data.i = (guint16)sp [-1].data.i;
2438                         ++ip;
2439                         MINT_IN_BREAK;
2440                 MINT_IN_CASE(MINT_CONV_U2_I8)
2441                         sp [-1].data.i = (guint16)sp [-1].data.l;
2442                         ++ip;
2443                         MINT_IN_BREAK;
2444                 MINT_IN_CASE(MINT_CONV_U2_R8)
2445                         sp [-1].data.i = (guint16)sp [-1].data.f;
2446                         ++ip;
2447                         MINT_IN_BREAK;
2448                 MINT_IN_CASE(MINT_CONV_I4_R8)
2449                         sp [-1].data.i = (gint32)sp [-1].data.f;
2450                         ++ip;
2451                         MINT_IN_BREAK;
2452                 MINT_IN_CASE(MINT_CONV_U4_I8)
2453                 MINT_IN_CASE(MINT_CONV_I4_I8)
2454                         sp [-1].data.i = (gint32)sp [-1].data.l;
2455                         ++ip;
2456                         MINT_IN_BREAK;
2457                 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2458                         sp [-2].data.i = (gint32)sp [-2].data.l;
2459                         ++ip;
2460                         MINT_IN_BREAK;
2461                 MINT_IN_CASE(MINT_CONV_U4_R8)
2462                         sp [-1].data.i = (guint32)sp [-1].data.f;
2463                         ++ip;
2464                         MINT_IN_BREAK;
2465                 MINT_IN_CASE(MINT_CONV_I8_I4)
2466                         sp [-1].data.l = sp [-1].data.i;
2467                         ++ip;
2468                         MINT_IN_BREAK;
2469                 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2470                         sp [-2].data.l = sp [-2].data.i;
2471                         ++ip;
2472                         MINT_IN_BREAK;
2473                 MINT_IN_CASE(MINT_CONV_I8_U4)
2474                         sp [-1].data.l = (guint32)sp [-1].data.i;
2475                         ++ip;
2476                         MINT_IN_BREAK;
2477                 MINT_IN_CASE(MINT_CONV_I8_R8)
2478                         sp [-1].data.l = (gint64)sp [-1].data.f;
2479                         ++ip;
2480                         MINT_IN_BREAK;
2481                 MINT_IN_CASE(MINT_CONV_R4_I4)
2482                         sp [-1].data.f = (float)sp [-1].data.i;
2483                         ++ip;
2484                         MINT_IN_BREAK;
2485                 MINT_IN_CASE(MINT_CONV_R4_I8)
2486                         sp [-1].data.f = (float)sp [-1].data.l;
2487                         ++ip;
2488                         MINT_IN_BREAK;
2489                 MINT_IN_CASE(MINT_CONV_R4_R8)
2490                         sp [-1].data.f = (float)sp [-1].data.f;
2491                         ++ip;
2492                         MINT_IN_BREAK;
2493                 MINT_IN_CASE(MINT_CONV_R8_I4)
2494                         sp [-1].data.f = (double)sp [-1].data.i;
2495                         ++ip;
2496                         MINT_IN_BREAK;
2497                 MINT_IN_CASE(MINT_CONV_R8_I8)
2498                         sp [-1].data.f = (double)sp [-1].data.l;
2499                         ++ip;
2500                         MINT_IN_BREAK;
2501                 MINT_IN_CASE(MINT_CONV_U8_I4)
2502                         sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2503                         ++ip;
2504                         MINT_IN_BREAK;
2505                 MINT_IN_CASE(MINT_CONV_U8_R8)
2506                         sp [-1].data.l = (guint64)sp [-1].data.f;
2507                         ++ip;
2508                         MINT_IN_BREAK;
2509 #if 0
2510                 MINT_IN_CASE(MINT_CPOBJ) {
2511                         MonoClass *vtklass;
2512                         ++ip;
2513                         vtklass = rtm->data_items[READ32 (ip)];
2514                         ip += 2;
2515                         sp -= 2;
2516                         memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2517                         MINT_IN_BREAK;
2518                 }
2519 #endif
2520                 MINT_IN_CASE(MINT_LDOBJ) {
2521                         int size;
2522                         void *p;
2523                         c = rtm->data_items[* (guint16 *)(ip + 1)];
2524                         ip += 2;
2525                         if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2526                                 p = sp [-1].data.p;
2527                                 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2528                         } else {
2529                                 size = mono_class_value_size (c, NULL);
2530                                 p = sp [-1].data.p;
2531                                 sp [-1].data.p = vt_sp;
2532                                 memcpy(vt_sp, p, size);
2533                                 vt_sp += (size + 7) & ~7;
2534                         }
2535                         MINT_IN_BREAK;
2536                 }
2537                 MINT_IN_CASE(MINT_LDSTR)
2538                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2539                         ++sp;
2540                         ip += 2;
2541                         MINT_IN_BREAK;
2542                 MINT_IN_CASE(MINT_NEWOBJ) {
2543                         MonoClass *newobj_class;
2544                         MonoMethodSignature *csig;
2545                         stackval valuetype_this;
2546                         guint32 token;
2547                         stackval retval;
2548
2549                         frame->ip = ip;
2550
2551                         token = * (guint16 *)(ip + 1);
2552                         ip += 2;
2553
2554                         child_frame.runtime_method = rtm->data_items [token];
2555                         csig = child_frame.runtime_method->method->signature;
2556                         newobj_class = child_frame.runtime_method->method->klass;
2557                         /*if (profiling_classes) {
2558                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2559                                 count++;
2560                                 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2561                         }*/
2562                                 
2563
2564                         if (newobj_class->parent == mono_defaults.array_class) {
2565                                 sp -= csig->param_count;
2566                                 o = ves_array_create (context->domain, newobj_class, csig, sp);
2567                                 goto array_constructed;
2568                         }
2569
2570                         /*
2571                          * First arg is the object.
2572                          */
2573                         if (newobj_class->valuetype) {
2574                                 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2575                                         child_frame.obj = vt_sp;
2576                                         valuetype_this.data.p = vt_sp;
2577                                 } else {
2578                                         memset (&valuetype_this, 0, sizeof (stackval));
2579                                         child_frame.obj = &valuetype_this;
2580                                 }
2581                         } else {
2582                                 if (newobj_class != mono_defaults.string_class) {
2583                                         context->managed_code = 0;
2584                                         o = mono_object_new (context->domain, newobj_class);
2585                                         context->managed_code = 1;
2586                                         if (*abort_requested)
2587                                                 mono_thread_interruption_checkpoint ();
2588                                         child_frame.obj = o;
2589                                 } else {
2590                                         child_frame.retval = &retval;
2591                                 }
2592                         }
2593
2594                         if (csig->param_count) {
2595                                 sp -= csig->param_count;
2596                                 child_frame.stack_args = sp;
2597                         } else {
2598                                 child_frame.stack_args = NULL;
2599                         }
2600
2601                         g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2602
2603                         child_frame.ip = NULL;
2604                         child_frame.ex = NULL;
2605
2606                         ves_exec_method_with_context (&child_frame, context);
2607
2608                         context->current_frame = frame;
2609
2610                         if (child_frame.ex) {
2611                                 /*
2612                                  * An exception occurred, need to run finally, fault and catch handlers..
2613                                  */
2614                                 frame->ex = child_frame.ex;
2615                                 goto handle_finally;
2616                         }
2617                         /*
2618                          * a constructor returns void, but we need to return the object we created
2619                          */
2620 array_constructed:
2621                         if (newobj_class->valuetype && !newobj_class->enumtype) {
2622                                 *sp = valuetype_this;
2623                         } else if (newobj_class == mono_defaults.string_class) {
2624                                 *sp = retval;
2625                         } else {
2626                                 sp->data.p = o;
2627                         }
2628                         ++sp;
2629                         MINT_IN_BREAK;
2630                 }
2631                 MINT_IN_CASE(MINT_CASTCLASS)
2632                         c = rtm->data_items [*(guint16 *)(ip + 1)];
2633                         if ((o = sp [-1].data.p)) {
2634                                 if (c->marshalbyref) {
2635                                         if (!mono_object_isinst_mbyref (o, c))
2636                                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
2637                                 } else {
2638                                         MonoVTable *vt = o->vtable;
2639                                         MonoClass *oklass = vt->klass;
2640                                         if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2641                                                 if (c->interface_id > vt->max_interface_id ||
2642                                                     vt->interface_offsets [c->interface_id] == 0) {
2643                                                         THROW_EX (mono_get_exception_invalid_cast (), ip);
2644                                                 }
2645                                         } else if (c->rank) {
2646                                                 if (!mono_object_isinst (o, c))
2647                                                         THROW_EX (mono_get_exception_invalid_cast (), ip);
2648                                         } else if (!mono_class_has_parent (oklass, c)) {
2649                                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
2650                                         }
2651                                 }
2652                         }
2653                         ip += 2;
2654                         MINT_IN_BREAK;
2655                 MINT_IN_CASE(MINT_ISINST)
2656                         c = rtm->data_items [*(guint16 *)(ip + 1)];
2657                         if ((o = sp [-1].data.p)) {
2658                                 if (c->marshalbyref) {
2659                                         if (!mono_object_isinst_mbyref (o, c))
2660                                                 sp [-1].data.p = NULL;
2661                                 } else {
2662                                         MonoVTable *vt = o->vtable;
2663                                         MonoClass *oklass = vt->klass;
2664                                         if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2665                                                 if (c->interface_id > vt->max_interface_id ||
2666                                                     vt->interface_offsets [c->interface_id] == 0) {
2667                                                         sp [-1].data.p = NULL;
2668                                                 }
2669                                         } else if (c->rank) {
2670                                                 if (!mono_object_isinst (o, c))
2671                                                         sp [-1].data.p = NULL;
2672                                         } else if (!mono_class_has_parent (oklass, c)) {
2673                                                 sp [-1].data.p = NULL;
2674                                         }
2675                                 }
2676                         }
2677                         ip += 2;
2678                         MINT_IN_BREAK;
2679                 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2680                         sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2681                         ++ip;
2682                         MINT_IN_BREAK;
2683                 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2684                         sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2685                         ++ip;
2686                         MINT_IN_BREAK;
2687                 MINT_IN_CASE(MINT_UNBOX)
2688                         c = rtm->data_items[*(guint16 *)(ip + 1)];
2689                         
2690                         o = sp [-1].data.p;
2691                         if (!o)
2692                                 THROW_EX (mono_get_exception_null_reference(), ip);
2693
2694                         if (!(mono_object_isinst (o, c) || 
2695                                   ((o->vtable->klass->rank == 0) && 
2696                                    (o->vtable->klass->element_class == c->element_class))))
2697                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
2698
2699                         sp [-1].data.p = (char *)o + sizeof (MonoObject);
2700                         ip += 2;
2701                         MINT_IN_BREAK;
2702                 MINT_IN_CASE(MINT_THROW)
2703                         --sp;
2704                         frame->ex_handler = NULL;
2705                         if (!sp->data.p)
2706                                 sp->data.p = mono_get_exception_null_reference ();
2707                         THROW_EX ((MonoException *)sp->data.p, ip);
2708                         MINT_IN_BREAK;
2709                 MINT_IN_CASE(MINT_LDFLDA)
2710                         o = sp [-1].data.p;
2711                         if (!o)
2712                                 THROW_EX (mono_get_exception_null_reference (), ip);
2713                         sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2714                         ip += 2;
2715                         MINT_IN_BREAK;
2716                 MINT_IN_CASE(MINT_CKNULL)
2717                         o = sp [-1].data.p;
2718                         if (!o)
2719                                 THROW_EX (mono_get_exception_null_reference (), ip);
2720                         ++ip;
2721                         MINT_IN_BREAK;
2722
2723 #define LDFLD(datamem, fieldtype) \
2724         o = sp [-1].data.p; \
2725         if (!o) \
2726                 THROW_EX (mono_get_exception_null_reference (), ip); \
2727         sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2728         ip += 2;
2729
2730                 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2731                 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2732                 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2733                 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2734                 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2735                 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2736                 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2737                 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2738                 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2739                 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2740
2741                 MINT_IN_CASE(MINT_LDFLD_VT)
2742                         o = sp [-1].data.p;
2743                         if (!o)
2744                                 THROW_EX (mono_get_exception_null_reference (), ip);
2745                         i32 = READ32(ip + 2);
2746                         sp [-1].data.p = vt_sp;
2747                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2748                         vt_sp += (i32 + 7) & ~7;
2749                         ip += 4;
2750                         MINT_IN_BREAK;
2751
2752                 MINT_IN_CASE(MINT_LDRMFLD) {
2753                         MonoClassField *field;
2754                         char *addr;
2755
2756                         o = sp [-1].data.p;
2757                         if (!o)
2758                                 THROW_EX (mono_get_exception_null_reference (), ip);
2759                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2760                         ip += 2;
2761                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2762                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2763                                 addr = mono_load_remote_field (o, klass, field, NULL);
2764                         } else {
2765                                 addr = (char*)o + field->offset;
2766                         }                               
2767
2768                         stackval_from_data (field->type, &sp [-1], addr, FALSE);
2769                         MINT_IN_BREAK;
2770                 }
2771
2772                 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2773                         MonoClassField *field;
2774                         char *addr;
2775
2776                         o = sp [-1].data.p;
2777                         if (!o)
2778                                 THROW_EX (mono_get_exception_null_reference (), ip);
2779                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2780                         i32 = READ32(ip + 2);
2781                         ip += 4;
2782                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2783                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2784                                 addr = mono_load_remote_field (o, klass, field, NULL);
2785                         } else {
2786                                 addr = (char*)o + field->offset;
2787                         }                               
2788
2789                         sp [-1].data.p = vt_sp;
2790                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2791                         vt_sp += (i32 + 7) & ~7;
2792                         memcpy(sp [-1].data.p, addr, i32);
2793                         MINT_IN_BREAK;
2794                 }
2795
2796 #define STFLD(datamem, fieldtype) \
2797         o = sp [-2].data.p; \
2798         if (!o) \
2799                 THROW_EX (mono_get_exception_null_reference (), ip); \
2800         sp -= 2; \
2801         * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2802         ip += 2;
2803
2804                 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2805                 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2806                 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2807                 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2808                 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2809                 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2810                 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2811                 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2812                 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2813                 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2814
2815                 MINT_IN_CASE(MINT_STFLD_VT)
2816                         o = sp [-2].data.p;
2817                         if (!o)
2818                                 THROW_EX (mono_get_exception_null_reference (), ip);
2819                         i32 = READ32(ip + 2);
2820                         sp -= 2;
2821                         memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2822                         vt_sp -= (i32 + 7) & ~7;
2823                         ip += 4;
2824                         MINT_IN_BREAK;
2825
2826                 MINT_IN_CASE(MINT_STRMFLD) {
2827                         MonoClassField *field;
2828
2829                         o = sp [-2].data.p;
2830                         if (!o)
2831                                 THROW_EX (mono_get_exception_null_reference (), ip);
2832                         
2833                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2834                         ip += 2;
2835
2836                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2837                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2838                                 mono_store_remote_field (o, klass, field, &sp [-1].data);
2839                         } else
2840                                 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
2841
2842                         sp -= 2;
2843                         MINT_IN_BREAK;
2844                 }
2845                 MINT_IN_CASE(MINT_STRMFLD_VT) {
2846                         MonoClassField *field;
2847
2848                         o = sp [-2].data.p;
2849                         if (!o)
2850                                 THROW_EX (mono_get_exception_null_reference (), ip);
2851                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2852                         i32 = READ32(ip + 2);
2853                         ip += 4;
2854
2855                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2856                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2857                                 mono_store_remote_field (o, klass, field, &sp [-1].data);
2858                         } else
2859                                 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
2860
2861                         sp -= 2;
2862                         vt_sp -= (i32 + 7) & ~7;
2863                         MINT_IN_BREAK;
2864                 }
2865                 MINT_IN_CASE(MINT_LDSFLDA) {
2866                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2867                         MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
2868                         gpointer addr;
2869
2870                         if (!vt->initialized) {
2871                                 frame->ip = ip;
2872                                 mono_runtime_class_init (vt);
2873                         }
2874                         ip += 2;
2875
2876                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2877                                 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2878                         else
2879                                 sp->data.p = (char*)(vt->data) + field->offset;
2880                         ++sp;
2881                         MINT_IN_BREAK;
2882                 }
2883                 MINT_IN_CASE(MINT_LDSFLD) {
2884                         MonoVTable *vt;
2885                         MonoClassField *field;
2886                         gpointer addr;
2887
2888                         field = rtm->data_items[*(guint16 *)(ip + 1)];
2889                         vt = rtm->data_items [*(guint16 *)(ip + 2)];
2890                         if (!vt->initialized) {
2891                                 frame->ip = ip;
2892                                 mono_runtime_class_init (vt);
2893                         }
2894                         ip += 3;
2895                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2896                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2897                         else
2898                                 addr = (char*)(vt->data) + field->offset;
2899
2900                         stackval_from_data (field->type, sp, addr, FALSE);
2901                         ++sp;
2902                         MINT_IN_BREAK;
2903                 }
2904                 MINT_IN_CASE(MINT_LDSFLD_I4) {
2905                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2906                         MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
2907                         if (!vt->initialized) {
2908                                 frame->ip = ip;
2909                                 mono_runtime_class_init (vt);
2910                         }
2911                         ip += 3;
2912                         sp->data.i = * (gint32 *)((char*)(vt->data) + field->offset);
2913                         ++sp;
2914                         MINT_IN_BREAK;
2915                 }
2916                 MINT_IN_CASE(MINT_LDSFLD_O) {
2917                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2918                         MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
2919                         if (!vt->initialized) {
2920                                 frame->ip = ip;
2921                                 mono_runtime_class_init (vt);
2922                         }
2923                         ip += 3;
2924                         sp->data.p = * (gpointer *)((char*)(vt->data) + field->offset);
2925                         ++sp;
2926                         MINT_IN_BREAK;
2927                 }
2928                 MINT_IN_CASE(MINT_LDSFLD_VT) {
2929                         MonoVTable *vt;
2930                         MonoClassField *field;
2931                         guint32 token;
2932                         gpointer addr;
2933                         int size;
2934
2935                         token = * (guint16 *)(ip + 1);
2936                         size = READ32(ip + 2);
2937                         field = rtm->data_items[token];
2938                         ip += 4;
2939                                                 
2940                         vt = mono_class_vtable (context->domain, field->parent);
2941                         if (!vt->initialized) {
2942                                 frame->ip = ip - 2;
2943                                 mono_runtime_class_init (vt);
2944                         }
2945                         
2946                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2947                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2948                         else
2949                                 addr = (char*)(vt->data) + field->offset;
2950
2951                         sp->data.p = vt_sp;
2952                         vt_sp += (size + 7) & ~7;
2953                         stackval_from_data (field->type, sp, addr, FALSE);
2954                         ++sp;
2955                         MINT_IN_BREAK;
2956                 }
2957                 MINT_IN_CASE(MINT_STSFLD) {
2958                         MonoVTable *vt;
2959                         MonoClassField *field;
2960                         guint32 token;
2961                         gpointer addr;
2962
2963                         token = * (guint16 *)(ip + 1);
2964                         field = rtm->data_items[token];
2965                         ip += 2;
2966                         --sp;
2967
2968                         vt = mono_class_vtable (context->domain, field->parent);
2969                         if (!vt->initialized) {
2970                                 frame->ip = ip - 2;
2971                                 mono_runtime_class_init (vt);
2972                         }
2973                         
2974                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2975                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2976                         else
2977                                 addr = (char*)(vt->data) + field->offset;
2978
2979                         stackval_to_data (field->type, sp, addr, FALSE);
2980                         MINT_IN_BREAK;
2981                 }
2982                 MINT_IN_CASE(MINT_STSFLD_VT) {
2983                         MonoVTable *vt;
2984                         MonoClassField *field;
2985                         guint32 token;
2986                         gpointer addr;
2987                         int size;
2988
2989                         token = * (guint16 *)(ip + 1);
2990                         size = READ32(ip + 2);
2991                         field = rtm->data_items[token];
2992                         ip += 4;
2993                                                 
2994                         vt = mono_class_vtable (context->domain, field->parent);
2995                         if (!vt->initialized) {
2996                                 frame->ip = ip - 2;
2997                                 mono_runtime_class_init (vt);
2998                         }
2999                         
3000                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3001                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3002                         else
3003                                 addr = (char*)(vt->data) + field->offset;
3004                         --sp;
3005                         stackval_to_data (field->type, sp, addr, FALSE);
3006                         vt_sp -= (size + 7) & ~7;
3007                         MINT_IN_BREAK;
3008                 }
3009                 MINT_IN_CASE(MINT_STOBJ_VT) {
3010                         int size;
3011                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3012                         ip += 2;
3013                         size = mono_class_value_size (c, NULL);
3014                         memcpy(sp [-2].data.p, sp [-1].data.p, size);
3015                         vt_sp -= (size + 7) & ~7;
3016                         sp -= 2;
3017                         MINT_IN_BREAK;
3018                 }
3019                 MINT_IN_CASE(MINT_STOBJ) {
3020                         int size;
3021                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3022                         ip += 2;
3023                         size = mono_class_value_size (c, NULL);
3024                         memcpy(sp [-2].data.p, &sp [-1].data, size);
3025                         sp -= 2;
3026                         MINT_IN_BREAK;
3027                 }
3028                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3029                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3030                                 THROW_EX (mono_get_exception_overflow (), ip);
3031                         sp [-1].data.i = (guint32)sp [-1].data.f;
3032                         ++ip;
3033                         MINT_IN_BREAK;
3034                 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3035                         if (sp [-1].data.i < 0)
3036                                 THROW_EX (mono_get_exception_overflow (), ip);
3037                         sp [-1].data.l = sp [-1].data.i;
3038                         ++ip;
3039                         MINT_IN_BREAK;
3040                 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3041                 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3042                         if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3043                                 THROW_EX (mono_get_exception_overflow (), ip);
3044                         sp [-1].data.l = (guint64)sp [-1].data.f;
3045                         ++ip;
3046                         MINT_IN_BREAK;
3047                 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3048                         if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3049                                 THROW_EX (mono_get_exception_overflow (), ip);
3050                         sp [-1].data.l = (gint64)sp [-1].data.f;
3051                         ++ip;
3052                         MINT_IN_BREAK;
3053                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3054                         if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3055                                 THROW_EX (mono_get_exception_overflow (), ip);
3056                         sp [-1].data.i = (mono_u)sp [-1].data.l;
3057                         ++ip;
3058                         MINT_IN_BREAK;
3059                 MINT_IN_CASE(MINT_BOX)
3060                         c = rtm->data_items [* (guint16 *)(ip + 1)];
3061
3062                         if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3063                                 int size = mono_class_value_size (c, NULL);
3064                                 sp [-1].data.p = mono_value_box (context->domain, c, sp [-1].data.p);
3065                                 size = (size + 7) & ~7;
3066                                 vt_sp -= size;
3067                         }                               
3068                         else {
3069                                 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3070                                 sp [-1].data.p = mono_value_box (context->domain, c, &sp [-1]);
3071                         }
3072                         ip += 2;
3073                         MINT_IN_BREAK;
3074                 MINT_IN_CASE(MINT_NEWARR)
3075                         sp [-1].data.p = (MonoObject*) mono_array_new (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i);
3076                         ip += 2;
3077                         /*if (profiling_classes) {
3078                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3079                                 count++;
3080                                 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3081                         }*/
3082
3083                         MINT_IN_BREAK;
3084                 MINT_IN_CASE(MINT_LDLEN)
3085                         o = sp [-1].data.p;
3086                         if (!o)
3087                                 THROW_EX (mono_get_exception_null_reference (), ip);
3088                         sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3089                         ++ip;
3090                         MINT_IN_BREAK;
3091                 MINT_IN_CASE(MINT_GETCHR) {
3092                         MonoString *s;
3093                         s = sp [-2].data.p;
3094                         if (!s)
3095                                 THROW_EX (mono_get_exception_null_reference (), ip);
3096                         i32 = sp [-1].data.i;
3097                         if (i32 < 0 || i32 >= mono_string_length (s))
3098                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3099                         --sp;
3100                         sp [-1].data.i = mono_string_chars(s)[i32];
3101                         ++ip;
3102                         MINT_IN_BREAK;
3103                 }
3104                 MINT_IN_CASE(MINT_STRLEN)
3105                         ++ip;
3106                         sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3107                         MINT_IN_BREAK;
3108                 MINT_IN_CASE(MINT_ARRAY_RANK)
3109                         o = sp [-1].data.p;
3110                         if (!o)
3111                                 THROW_EX (mono_get_exception_null_reference (), ip);
3112                         sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3113                         ip++;
3114                         MINT_IN_BREAK;
3115                 MINT_IN_CASE(MINT_LDELEMA) {
3116                         guint32 esize;
3117                         mono_u aindex;
3118                         
3119                         /*token = READ32 (ip)*/;
3120                         ip += 2;
3121                         sp -= 2;
3122
3123                         o = sp [0].data.p;
3124
3125                         aindex = sp [1].data.i;
3126                         if (aindex >= mono_array_length ((MonoArray *) o))
3127                                 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3128
3129                         /* check the array element corresponds to token */
3130                         esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3131                         
3132                         sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3133                         ++sp;
3134                         MINT_IN_BREAK;
3135                 }
3136                 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3137                 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3138                 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3139                 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3140                 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3141                 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3142                 MINT_IN_CASE(MINT_LDELEM_I8)  /* fall through */
3143                 MINT_IN_CASE(MINT_LDELEM_I)  /* fall through */
3144                 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3145                 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3146                 MINT_IN_CASE(MINT_LDELEM_REF) {
3147                         MonoArray *o;
3148                         mono_u aindex;
3149
3150                         sp -= 2;
3151
3152                         o = sp [0].data.p;
3153                         if (!o)
3154                                 THROW_EX (mono_get_exception_null_reference (), ip);
3155
3156                         aindex = sp [1].data.i;
3157                         if (aindex >= mono_array_length (o))
3158                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3159
3160                         /*
3161                          * FIXME: throw mono_get_exception_array_type_mismatch () if needed 
3162                          */
3163                         switch (*ip) {
3164                         case MINT_LDELEM_I1:
3165                                 sp [0].data.i = mono_array_get (o, gint8, aindex);
3166                                 break;
3167                         case MINT_LDELEM_U1:
3168                                 sp [0].data.i = mono_array_get (o, guint8, aindex);
3169                                 break;
3170                         case MINT_LDELEM_I2:
3171                                 sp [0].data.i = mono_array_get (o, gint16, aindex);
3172                                 break;
3173                         case MINT_LDELEM_U2:
3174                                 sp [0].data.i = mono_array_get (o, guint16, aindex);
3175                                 break;
3176                         case MINT_LDELEM_I:
3177                                 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3178                                 break;
3179                         case MINT_LDELEM_I4:
3180                                 sp [0].data.i = mono_array_get (o, gint32, aindex);
3181                                 break;
3182                         case MINT_LDELEM_U4:
3183                                 sp [0].data.i = mono_array_get (o, guint32, aindex);
3184                                 break;
3185                         case MINT_LDELEM_I8:
3186                                 sp [0].data.l = mono_array_get (o, guint64, aindex);
3187                                 break;
3188                         case MINT_LDELEM_R4:
3189                                 sp [0].data.f = mono_array_get (o, float, aindex);
3190                                 break;
3191                         case MINT_LDELEM_R8:
3192                                 sp [0].data.f = mono_array_get (o, double, aindex);
3193                                 break;
3194                         case MINT_LDELEM_REF:
3195                                 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3196                                 break;
3197                         default:
3198                                 ves_abort();
3199                         }
3200
3201                         ++ip;
3202                         ++sp;
3203                         MINT_IN_BREAK;
3204                 }
3205                 MINT_IN_CASE(MINT_STELEM_I)  /* fall through */
3206                 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */ 
3207                 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3208                 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3209                 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3210                 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3211                 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3212                 MINT_IN_CASE(MINT_STELEM_REF) {
3213                         mono_u aindex;
3214
3215                         sp -= 3;
3216
3217                         o = sp [0].data.p;
3218                         if (!o)
3219                                 THROW_EX (mono_get_exception_null_reference (), ip);
3220
3221                         aindex = sp [1].data.i;
3222                         if (aindex >= mono_array_length ((MonoArray *)o))
3223                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3224
3225                         switch (*ip) {
3226                         case MINT_STELEM_I:
3227                                 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3228                                 break;
3229                         case MINT_STELEM_I1:
3230                                 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3231                                 break;
3232                         case MINT_STELEM_I2:
3233                                 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3234                                 break;
3235                         case MINT_STELEM_I4:
3236                                 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3237                                 break;
3238                         case MINT_STELEM_I8:
3239                                 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3240                                 break;
3241                         case MINT_STELEM_R4:
3242                                 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3243                                 break;
3244                         case MINT_STELEM_R8:
3245                                 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3246                                 break;
3247                         case MINT_STELEM_REF:
3248                                 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3249                                         THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3250                                 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3251                                 break;
3252                         default:
3253                                 ves_abort();
3254                         }
3255
3256                         ++ip;
3257                         MINT_IN_BREAK;
3258                 }
3259                 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3260                         if (sp [-1].data.i < 0)
3261                                 THROW_EX (mono_get_exception_overflow (), ip);
3262                         ++ip;
3263                         MINT_IN_BREAK;
3264                 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3265                         if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3266                                 THROW_EX (mono_get_exception_overflow (), ip);
3267                         sp [-1].data.i = (gint32) sp [-1].data.l;
3268                         ++ip;
3269                         MINT_IN_BREAK;
3270                 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3271                         if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3272                                 THROW_EX (mono_get_exception_overflow (), ip);
3273                         sp [-1].data.i = (gint32) sp [-1].data.f;
3274                         ++ip;
3275                         MINT_IN_BREAK;
3276                 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3277                         if (sp [-1].data.i < 0)
3278                                 THROW_EX (mono_get_exception_overflow (), ip);
3279                         ++ip;
3280                         MINT_IN_BREAK;
3281                 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3282                         if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3283                                 THROW_EX (mono_get_exception_overflow (), ip);
3284                         sp [-1].data.i = (guint32) sp [-1].data.l;
3285                         ++ip;
3286                         MINT_IN_BREAK;
3287                 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3288                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3289                                 THROW_EX (mono_get_exception_overflow (), ip);
3290                         sp [-1].data.i = (guint32) sp [-1].data.f;
3291                         ++ip;
3292                         MINT_IN_BREAK;
3293                 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3294                         if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3295                                 THROW_EX (mono_get_exception_overflow (), ip);
3296                         ++ip;
3297                         MINT_IN_BREAK;
3298                 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3299                         if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3300                                 THROW_EX (mono_get_exception_overflow (), ip);
3301                         sp [-1].data.i = (gint16) sp [-1].data.l;
3302                         ++ip;
3303                         MINT_IN_BREAK;
3304                 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3305                         if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3306                                 THROW_EX (mono_get_exception_overflow (), ip);
3307                         sp [-1].data.i = (gint16) sp [-1].data.f;
3308                         ++ip;
3309                         MINT_IN_BREAK;
3310                 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3311                         if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3312                                 THROW_EX (mono_get_exception_overflow (), ip);
3313                         ++ip;
3314                         MINT_IN_BREAK;
3315                 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3316                         if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3317                                 THROW_EX (mono_get_exception_overflow (), ip);
3318                         sp [-1].data.i = (guint16) sp [-1].data.l;
3319                         ++ip;
3320                         MINT_IN_BREAK;
3321                 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3322                         if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3323                                 THROW_EX (mono_get_exception_overflow (), ip);
3324                         sp [-1].data.i = (guint16) sp [-1].data.f;
3325                         ++ip;
3326                         MINT_IN_BREAK;
3327                 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3328                         if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3329                                 THROW_EX (mono_get_exception_overflow (), ip);
3330                         ++ip;
3331                         MINT_IN_BREAK;
3332                 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3333                         if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3334                                 THROW_EX (mono_get_exception_overflow (), ip);
3335                         sp [-1].data.i = (gint8) sp [-1].data.l;
3336                         ++ip;
3337                         MINT_IN_BREAK;
3338                 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3339                         if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3340                                 THROW_EX (mono_get_exception_overflow (), ip);
3341                         sp [-1].data.i = (gint8) sp [-1].data.f;
3342                         ++ip;
3343                         MINT_IN_BREAK;
3344                 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3345                         if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3346                                 THROW_EX (mono_get_exception_overflow (), ip);
3347                         ++ip;
3348                         MINT_IN_BREAK;
3349                 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3350                         if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3351                                 THROW_EX (mono_get_exception_overflow (), ip);
3352                         sp [-1].data.i = (guint8) sp [-1].data.l;
3353                         ++ip;
3354                         MINT_IN_BREAK;
3355                 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3356                         if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3357                                 THROW_EX (mono_get_exception_overflow (), ip);
3358                         sp [-1].data.i = (guint8) sp [-1].data.f;
3359                         ++ip;
3360                         MINT_IN_BREAK;
3361 #if 0
3362                 MINT_IN_CASE(MINT_LDELEM) 
3363                 MINT_IN_CASE(MINT_STELEM) 
3364                 MINT_IN_CASE(MINT_UNBOX_ANY) 
3365
3366                 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3367 #endif
3368                 MINT_IN_CASE(MINT_CKFINITE)
3369                         if (!finite(sp [-1].data.f))
3370                                 THROW_EX (mono_get_exception_arithmetic (), ip);
3371                         ++ip;
3372                         MINT_IN_BREAK;
3373 #if 0
3374                 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3375 #endif
3376                 MINT_IN_CASE(MINT_LDTOKEN)
3377                         sp->data.p = vt_sp;
3378                         vt_sp += 8;
3379                         * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3380                         ip += 2;
3381                         ++sp;
3382                         MINT_IN_BREAK;
3383                 MINT_IN_CASE(MINT_ADD_OVF_I4)
3384                         if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3385                                 THROW_EX (mono_get_exception_overflow (), ip);
3386                         BINOP(i, +);
3387                         MINT_IN_BREAK;
3388                 MINT_IN_CASE(MINT_ADD_OVF_I8)
3389                         if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3390                                 THROW_EX (mono_get_exception_overflow (), ip);
3391                         BINOP(l, +);
3392                         MINT_IN_BREAK;
3393                 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3394                         if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3395                                 THROW_EX (mono_get_exception_overflow (), ip);
3396                         BINOP_CAST(i, +, guint32);
3397                         MINT_IN_BREAK;
3398                 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3399                         if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3400                                 THROW_EX (mono_get_exception_overflow (), ip);
3401                         BINOP_CAST(l, +, guint64);
3402                         MINT_IN_BREAK;
3403                 MINT_IN_CASE(MINT_MUL_OVF_I4)
3404                         if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3405                                 THROW_EX (mono_get_exception_overflow (), ip);
3406                         BINOP(i, *);
3407                         MINT_IN_BREAK;
3408                 MINT_IN_CASE(MINT_MUL_OVF_I8)
3409                         if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3410                                 THROW_EX (mono_get_exception_overflow (), ip);
3411                         BINOP(l, *);
3412                         MINT_IN_BREAK;
3413                 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3414                         if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3415                                 THROW_EX (mono_get_exception_overflow (), ip);
3416                         BINOP_CAST(i, *, guint32);
3417                         MINT_IN_BREAK;
3418                 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3419                         if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3420                                 THROW_EX (mono_get_exception_overflow (), ip);
3421                         BINOP_CAST(l, *, guint64);
3422                         MINT_IN_BREAK;
3423                 MINT_IN_CASE(MINT_SUB_OVF_I4)
3424                         if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3425                                 THROW_EX (mono_get_exception_overflow (), ip);
3426                         BINOP(i, -);
3427                         MINT_IN_BREAK;
3428                 MINT_IN_CASE(MINT_SUB_OVF_I8)
3429                         if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3430                                 THROW_EX (mono_get_exception_overflow (), ip);
3431                         BINOP(l, -);
3432                         MINT_IN_BREAK;
3433                 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3434                         if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3435                                 THROW_EX (mono_get_exception_overflow (), ip);
3436                         BINOP_CAST(i, -, guint32);
3437                         MINT_IN_BREAK;
3438                 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3439                         if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3440                                 THROW_EX (mono_get_exception_overflow (), ip);
3441                         BINOP_CAST(l, -, guint64);
3442                         MINT_IN_BREAK;
3443                 MINT_IN_CASE(MINT_ENDFINALLY)
3444                         if (finally_ips) {
3445                                 ip = finally_ips->data;
3446                                 finally_ips = g_slist_remove (finally_ips, ip);
3447                                 goto main_loop;
3448                         }
3449                         if (frame->ex)
3450                                 goto handle_fault;
3451                         ves_abort();
3452                         MINT_IN_BREAK;
3453                 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3454                 MINT_IN_CASE(MINT_LEAVE_S)
3455                         while (sp > frame->stack) {
3456                                 --sp;
3457                         }
3458                         frame->ip = ip;
3459                         if (*ip == MINT_LEAVE_S) {
3460                                 ip += (short) *(ip + 1);
3461                         } else {
3462                                 ip += (gint32) READ32 (ip + 1);
3463                         }
3464                         endfinally_ip = ip;
3465                         if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3466                                 frame->ex_handler = NULL;
3467                                 frame->ex = NULL;
3468                         }
3469                         goto handle_finally;
3470                         MINT_IN_BREAK;
3471                 MINT_IN_CASE(MINT_ICALL_P_V) 
3472                 MINT_IN_CASE(MINT_ICALL_P_P)
3473                 MINT_IN_CASE(MINT_ICALL_PP_V)
3474                 MINT_IN_CASE(MINT_ICALL_PI_V)
3475                 MINT_IN_CASE(MINT_ICALL_PP_P)
3476                 MINT_IN_CASE(MINT_ICALL_PI_P)
3477                 MINT_IN_CASE(MINT_ICALL_PPP_V)
3478                 MINT_IN_CASE(MINT_ICALL_PPI_V)
3479                         sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3480                         if (frame->ex != NULL)
3481                                 goto handle_exception;
3482                         ip += 2;
3483                         MINT_IN_BREAK;
3484                 MINT_IN_CASE(MINT_MONO_LDPTR) 
3485                         sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3486                         ip += 2;
3487                         ++sp;
3488                         MINT_IN_BREAK;
3489                 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3490                         sp->data.p = mono_object_new (context->domain, rtm->data_items [*(guint16 *)(ip + 1)]);
3491                         ip += 2;
3492                         sp++;
3493                         MINT_IN_BREAK;
3494                 MINT_IN_CASE(MINT_MONO_FREE)
3495                         ++ip;
3496                         --sp;
3497                         g_free (sp->data.p);
3498                         MINT_IN_BREAK;
3499                 MINT_IN_CASE(MINT_MONO_RETOBJ)
3500                         ++ip;
3501                         sp--;
3502                         stackval_from_data (frame->runtime_method->method->signature->ret, frame->retval, sp->data.p, frame->runtime_method->method->signature->pinvoke);
3503                         if (sp > frame->stack)
3504                                 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3505                         goto exit_frame;
3506
3507 #define RELOP(datamem, op) \
3508         --sp; \
3509         sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3510         ++ip;
3511                 MINT_IN_CASE(MINT_CEQ_I4)
3512                         RELOP(i, ==);
3513                         MINT_IN_BREAK;
3514                 MINT_IN_CASE(MINT_CEQ0_I4)
3515                         sp [-1].data.i = (sp [-1].data.i == 0);
3516                         ++ip;
3517                         MINT_IN_BREAK;
3518                 MINT_IN_CASE(MINT_CEQ_I8)
3519                         RELOP(l, ==);
3520                         MINT_IN_BREAK;
3521                 MINT_IN_CASE(MINT_CEQ_R8)
3522                         --sp; 
3523                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3524                                 sp [-1].data.i = 0;
3525                         else
3526                                 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3527                         ++ip;
3528                         MINT_IN_BREAK;
3529                 MINT_IN_CASE(MINT_CGT_I4)
3530                         RELOP(i, >);
3531                         MINT_IN_BREAK;
3532                 MINT_IN_CASE(MINT_CGT_I8)
3533                         RELOP(l, >);
3534                         MINT_IN_BREAK;
3535                 MINT_IN_CASE(MINT_CGT_R8)
3536                         --sp; 
3537                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3538                                 sp [-1].data.i = 0;
3539                         else
3540                                 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3541                         ++ip;
3542                         MINT_IN_BREAK;
3543
3544 #define RELOP_CAST(datamem, op, type) \
3545         --sp; \
3546         sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3547         ++ip;
3548
3549                 MINT_IN_CASE(MINT_CGT_UN_I4)
3550                         RELOP_CAST(i, >, guint32);
3551                         MINT_IN_BREAK;
3552                 MINT_IN_CASE(MINT_CGT_UN_I8)
3553                         RELOP_CAST(l, >, guint64);
3554                         MINT_IN_BREAK;
3555                 MINT_IN_CASE(MINT_CGT_UN_R8)
3556                         --sp; 
3557                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3558                                 sp [-1].data.i = 1;
3559                         else
3560                                 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3561                         ++ip;
3562                         MINT_IN_BREAK;
3563                 MINT_IN_CASE(MINT_CLT_I4)
3564                         RELOP(i, <);
3565                         MINT_IN_BREAK;
3566                 MINT_IN_CASE(MINT_CLT_I8)
3567                         RELOP(l, <);
3568                         MINT_IN_BREAK;
3569                 MINT_IN_CASE(MINT_CLT_R8)
3570                         --sp; 
3571                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3572                                 sp [-1].data.i = 0;
3573                         else
3574                                 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3575                         ++ip;
3576                         MINT_IN_BREAK;
3577                 MINT_IN_CASE(MINT_CLT_UN_I4)
3578                         RELOP_CAST(i, <, guint32);
3579                         MINT_IN_BREAK;
3580                 MINT_IN_CASE(MINT_CLT_UN_I8)
3581                         RELOP_CAST(l, <, guint64);
3582                         MINT_IN_BREAK;
3583                 MINT_IN_CASE(MINT_CLT_UN_R8)
3584                         --sp; 
3585                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3586                                 sp [-1].data.i = 1;
3587                         else
3588                                 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3589                         ++ip;
3590                         MINT_IN_BREAK;
3591                 MINT_IN_CASE(MINT_LDFTN) {
3592                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3593                         ++sp;
3594                         ip += 2;
3595                         MINT_IN_BREAK;
3596                 }
3597                 MINT_IN_CASE(MINT_LDVIRTFTN) {
3598                         RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3599                         ip += 2;
3600                         --sp;
3601                         if (!sp->data.p)
3602                                 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3603                                 
3604                         sp->data.p = get_virtual_method (m, sp->data.p);
3605                         ++sp;
3606                         MINT_IN_BREAK;
3607                 }
3608
3609                 MINT_IN_CASE(MINT_LDTHIS)
3610                         sp->data.p = frame->obj;
3611                         ++ip;
3612                         ++sp; 
3613                         MINT_IN_BREAK;
3614                 MINT_IN_CASE(MINT_STTHIS)
3615                         --sp; 
3616                         frame->obj = sp->data.p;
3617                         ++ip;
3618                         MINT_IN_BREAK;
3619                 MINT_IN_CASE(MINT_LDTHISA)
3620                         sp->data.p = &frame->obj;
3621                         ++ip;
3622                         ++sp; 
3623                         MINT_IN_BREAK;
3624
3625 #define LDARG(datamem, argtype) \
3626         sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3627         ip += 2; \
3628         ++sp; 
3629         
3630                 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3631                 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3632                 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3633                 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3634                 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3635                 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3636                 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3637                 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3638                 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3639                 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3640
3641                 MINT_IN_CASE(MINT_LDARG_VT)
3642                         sp->data.p = vt_sp;
3643                         i32 = READ32(ip + 2);
3644                         memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3645                         vt_sp += (i32 + 7) & ~7;
3646                         ip += 4;
3647                         ++sp;
3648                         MINT_IN_BREAK;
3649
3650 #define STARG(datamem, argtype) \
3651         --sp; \
3652         * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3653         ip += 2; \
3654         
3655                 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3656                 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3657                 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3658                 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3659                 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3660                 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3661                 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3662                 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3663                 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3664                 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3665
3666                 MINT_IN_CASE(MINT_STARG_VT) 
3667                         i32 = READ32(ip + 2);
3668                         --sp;
3669                         memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3670                         vt_sp -= (i32 + 7) & ~7;
3671                         ip += 4;
3672                         MINT_IN_BREAK;
3673
3674 #define STINARG(datamem, argtype) \
3675         do { \
3676                 int n = * (guint16 *)(ip + 1); \
3677                 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3678                 ip += 2; \
3679         } while (0)
3680         
3681                 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3682                 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3683                 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3684                 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3685                 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3686                 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3687                 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3688                 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3689                 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3690                 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3691
3692                 MINT_IN_CASE(MINT_STINARG_VT) {
3693                         int n = * (guint16 *)(ip + 1);
3694                         i32 = READ32(ip + 2);
3695                         memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3696                         ip += 4;
3697                         MINT_IN_BREAK;
3698                 }
3699
3700                 MINT_IN_CASE(MINT_LDARGA)
3701                         sp->data.p = frame->args + * (guint16 *)(ip + 1);
3702                         ip += 2;
3703                         ++sp;
3704                         MINT_IN_BREAK;
3705
3706 #define LDLOC(datamem, argtype) \
3707         sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3708         ip += 2; \
3709         ++sp; 
3710         
3711                 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3712                 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3713                 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3714                 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3715                 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3716                 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3717                 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3718                 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3719                 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3720                 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3721
3722                 MINT_IN_CASE(MINT_LDLOC_VT)
3723                         sp->data.p = vt_sp;
3724                         i32 = READ32(ip + 2);
3725                         memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3726                         vt_sp += (i32 + 7) & ~7;
3727                         ip += 4;
3728                         ++sp;
3729                         MINT_IN_BREAK;
3730
3731                 MINT_IN_CASE(MINT_LDLOCA_S)
3732                         sp->data.p = locals + * (guint16 *)(ip + 1);
3733                         ip += 2;
3734                         ++sp;
3735                         MINT_IN_BREAK;
3736
3737 #define STLOC(datamem, argtype) \
3738         --sp; \
3739         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3740         ip += 2;
3741         
3742                 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3743                 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3744                 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3745                 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3746                 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3747                 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3748                 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3749                 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3750                 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3751                 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3752
3753 #define STLOC_NP(datamem, argtype) \
3754         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3755         ip += 2;
3756
3757                 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3758                 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3759
3760                 MINT_IN_CASE(MINT_STLOC_VT)
3761                         i32 = READ32(ip + 2);
3762                         --sp;
3763                         memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3764                         vt_sp -= (i32 + 7) & ~7;
3765                         ip += 4;
3766                         MINT_IN_BREAK;
3767
3768                 MINT_IN_CASE(MINT_LOCALLOC)
3769                         if (sp != frame->stack + 1) /*FIX?*/
3770                                 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3771                         sp [-1].data.p = alloca (sp [-1].data.i);
3772                         ++ip;
3773                         MINT_IN_BREAK;
3774 #if 0
3775                 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3776 #endif
3777                 MINT_IN_CASE(MINT_INITOBJ)
3778                         --sp;
3779                         memset (sp->data.vt, 0, READ32(ip + 1));
3780                         ip += 3;
3781                         MINT_IN_BREAK;
3782                 MINT_IN_CASE(MINT_CPBLK)
3783                         sp -= 3;
3784                         if (!sp [0].data.p || !sp [1].data.p)
3785                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3786                         ++ip;
3787                         /* FIXME: value and size may be int64... */
3788                         memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3789                         MINT_IN_BREAK;
3790 #if 0
3791                 MINT_IN_CASE(MINT_CONSTRAINED_) {
3792                         guint32 token;
3793                         /* FIXME: implement */
3794                         ++ip;
3795                         token = READ32 (ip);
3796                         ip += 2;
3797                         MINT_IN_BREAK;
3798                 }
3799 #endif
3800                 MINT_IN_CASE(MINT_INITBLK)
3801                         sp -= 3;
3802                         if (!sp [0].data.p)
3803                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3804                         ++ip;
3805                         /* FIXME: value and size may be int64... */
3806                         memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3807                         MINT_IN_BREAK;
3808 #if 0
3809                 MINT_IN_CASE(MINT_NO_)
3810                         /* FIXME: implement */
3811                         ip += 2;
3812                         MINT_IN_BREAK;
3813 #endif
3814                 MINT_IN_CASE(MINT_RETHROW)
3815                         /* 
3816                          * need to clarify what this should actually do:
3817                          * start the search from the last found handler in
3818                          * this method or continue in the caller or what.
3819                          * Also, do we need to run finally/fault handlers after a retrow?
3820                          * Well, this implementation will follow the usual search
3821                          * for an handler, considering the current ip as throw spot.
3822                          * We need to NULL frame->ex_handler for the later code to
3823                          * actually run the new found handler.
3824                          */
3825                         frame->ex_handler = NULL;
3826                         THROW_EX (frame->ex, ip - 1);
3827                         MINT_IN_BREAK;
3828                 MINT_IN_DEFAULT
3829                         g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3830                         THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3831                 }
3832         }
3833
3834         g_assert_not_reached ();
3835         /*
3836          * Exception handling code.
3837          * The exception object is stored in frame->ex.
3838          */
3839
3840         handle_exception:
3841         {
3842                 int i;
3843                 guint32 ip_offset;
3844                 MonoInvocation *inv;
3845                 MonoExceptionClause *clause;
3846                 /*char *message;*/
3847                 MonoObject *ex_obj;
3848
3849 #if DEBUG_INTERP
3850                 if (tracing)
3851                         g_print ("* Handling exception '%s' at IL_%04x\n", 
3852                                 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name, 
3853                                 rtm == NULL ? 0 : frame->ip - rtm->code);
3854 #endif
3855                 if (die_on_exception)
3856                         goto die_on_ex;
3857
3858                 for (inv = frame; inv; inv = inv->parent) {
3859                         MonoMethod *method;
3860                         if (inv->runtime_method == NULL)
3861                                 continue;
3862                         method = inv->runtime_method->method;
3863                         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3864                                 continue;
3865                         if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3866                                 continue;
3867                         if (inv->ip == NULL)
3868                                 continue;
3869                         ip_offset = inv->ip - inv->runtime_method->code;
3870                         inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one  - this one wins */
3871                         for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3872                                 clause = &inv->runtime_method->clauses [i];
3873                                 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3874                                         if (!clause->flags) {
3875                                                 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (method->klass->image, clause->token_or_filter))) {
3876                                                         /* 
3877                                                          * OK, we found an handler, now we need to execute the finally
3878                                                          * and fault blocks before branching to the handler code.
3879                                                          */
3880                                                         inv->ex_handler = clause;
3881 #if DEBUG_INTERP
3882                                                         if (tracing)
3883                                                                 g_print ("* Found handler at '%s'\n", method->name);
3884 #endif
3885                                                         goto handle_finally;
3886                                                 }
3887                                         } else {
3888                                                 /* FIXME: handle filter clauses */
3889                                                 g_assert (0);
3890                                         }
3891                                 }
3892                         }
3893                 }
3894                 /*
3895                  * If we get here, no handler was found: print a stack trace.
3896                  */
3897                 for (inv = frame; inv; inv = inv->parent) {
3898                         if (inv->invoke_trap)
3899                                 goto handle_finally;
3900                 }
3901 die_on_ex:
3902                 ex_obj = (MonoObject*)frame->ex;
3903                 mono_unhandled_exception (ex_obj);
3904                 exit (1);
3905         }
3906         handle_finally:
3907         {
3908                 int i;
3909                 guint32 ip_offset;
3910                 MonoExceptionClause *clause;
3911                 GSList *old_list = finally_ips;
3912                 MonoMethod *method = frame->runtime_method->method;
3913                 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
3914                 
3915 #if DEBUG_INTERP
3916                 if (tracing)
3917                         g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
3918 #endif
3919                 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
3920                                 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
3921                         goto exit_frame;
3922                 }
3923                 ip_offset = frame->ip - rtm->code;
3924
3925                 if (endfinally_ip != NULL)
3926                         finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
3927                 for (i = 0; i < header->num_clauses; ++i)
3928                         if (frame->ex_handler == &rtm->clauses [i])
3929                                 break;
3930                 while (i > 0) {
3931                         --i;
3932                         clause = &rtm->clauses [i];
3933                         if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
3934                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
3935                                         ip = rtm->code + clause->handler_offset;
3936                                         finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
3937 #if DEBUG_INTERP
3938                                         if (tracing)
3939                                                 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
3940 #endif
3941                                 }
3942                         }
3943                 }
3944
3945                 endfinally_ip = NULL;
3946
3947                 if (old_list != finally_ips && finally_ips) {
3948                         ip = finally_ips->data;
3949                         finally_ips = g_slist_remove (finally_ips, ip);
3950                         sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
3951                         goto main_loop;
3952                 }
3953
3954                 /*
3955                  * If an exception is set, we need to execute the fault handler, too,
3956                  * otherwise, we continue normally.
3957                  */
3958                 if (frame->ex)
3959                         goto handle_fault;
3960                 ves_abort();
3961         }
3962         handle_fault:
3963         {
3964                 int i;
3965                 guint32 ip_offset;
3966                 MonoExceptionClause *clause;
3967                 MonoMethodHeader *header = ((MonoMethodNormal *)frame->runtime_method->method)->header;
3968                 
3969 #if DEBUG_INTERP
3970                 if (tracing)
3971                         g_print ("* Handle fault\n");
3972 #endif
3973                 ip_offset = frame->ip - rtm->code;
3974                 for (i = 0; i < header->num_clauses; ++i) {
3975                         clause = &rtm->clauses [i];
3976                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3977                                 ip = rtm->code + clause->handler_offset;
3978 #if DEBUG_INTERP
3979                                 if (tracing)
3980                                         g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
3981 #endif
3982                                 goto main_loop;
3983                         }
3984                 }
3985                 /*
3986                  * If the handler for the exception was found in this method, we jump
3987                  * to it right away, otherwise we return and let the caller run
3988                  * the finally, fault and catch blocks.
3989                  * This same code should be present in the endfault opcode, but it
3990                  * is corrently not assigned in the ECMA specs: LAMESPEC.
3991                  */
3992                 if (frame->ex_handler) {
3993 #if DEBUG_INTERP
3994                         if (tracing)
3995                                 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
3996 #endif
3997                         ip = rtm->code + frame->ex_handler->handler_offset;
3998                         sp = frame->stack;
3999                         vt_sp = (char *)sp + rtm->stack_size;
4000                         sp->data.p = frame->ex;
4001                         ++sp;
4002                         goto main_loop;
4003                 }
4004                 goto exit_frame;
4005         }
4006 exit_frame:
4007         DEBUG_LEAVE ();
4008 }
4009
4010 void
4011 ves_exec_method (MonoInvocation *frame)
4012 {
4013         ThreadContext *context = TlsGetValue (thread_context_id);
4014         ThreadContext context_struct;
4015         jmp_buf env;
4016
4017         frame->ex = NULL;
4018
4019         if (setjmp(env)) {
4020                 mono_unhandled_exception ((MonoObject*)frame->ex);
4021                 return;
4022         }
4023         if (context == NULL) {
4024                 context = &context_struct;
4025                 context_struct.domain = mono_domain_get ();
4026                 context_struct.base_frame = frame;
4027                 context_struct.current_frame = NULL;
4028                 context_struct.env_frame = frame;
4029                 context_struct.current_env = &env;
4030                 context_struct.search_for_handler = 0;
4031                 context_struct.managed_code = 0;
4032                 TlsSetValue (thread_context_id, context);
4033         }
4034         frame->ip = NULL;
4035         frame->parent = context->current_frame;
4036         frame->runtime_method = mono_interp_get_runtime_method (frame->method);
4037         context->managed_code = 1;
4038         ves_exec_method_with_context(frame, context);
4039         context->managed_code = 0;
4040         if (frame->ex) {
4041                 if (context != &context_struct && context->current_env) {
4042                         context->env_frame->ex = frame->ex;
4043                         longjmp (*context->current_env, 1);
4044                 }
4045                 else
4046                         mono_unhandled_exception ((MonoObject*)frame->ex);
4047         }
4048         if (context->base_frame == frame)
4049                 TlsSetValue (thread_context_id, NULL);
4050         else
4051                 context->current_frame = frame->parent;
4052 }
4053
4054 static int 
4055 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4056 {
4057         MonoImage *image = mono_assembly_get_image (assembly);
4058         MonoMethod *method;
4059         MonoObject *exc = NULL;
4060         int rval;
4061
4062         method = mono_get_method (image, mono_image_get_entry_point (image), NULL);
4063         if (!method)
4064                 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4065
4066         rval = mono_runtime_run_main (method, argc, argv, &exc);
4067         if (exc != NULL)
4068                 mono_unhandled_exception (exc);
4069
4070         return rval;
4071 }
4072
4073 static void
4074 usage (void)
4075 {
4076         fprintf (stderr,
4077                  "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4078                  "Usage is: mint [options] executable args...\n\n", VERSION);
4079         fprintf (stderr,
4080                  "Runtime Debugging:\n"
4081 #ifdef DEBUG_INTERP
4082                  "   --debug\n"
4083 #endif
4084                  "   --dieonex\n"
4085                  "   --noptr\t\t\tdon't print pointer addresses in trace output\n"
4086                  "   --opcode-count\n"
4087                  "   --print-vtable\n"
4088                  "   --traceclassinit\n"
4089                  "\n"
4090                  "Development:\n"
4091                  "   --debug method_name\n"
4092                  "   --profile\n"
4093                  "   --trace\n"
4094                  "   --traceops\n"
4095                  "\n"
4096                  "Runtime:\n"
4097                  "   --config filename  load the specified config file instead of the default\n"
4098                  "   --workers n        maximum number of worker threads\n"
4099                 );
4100         exit (1);
4101 }
4102
4103 #ifdef RUN_TEST
4104 static void
4105 test_load_class (MonoImage* image)
4106 {
4107         MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
4108         MonoClass *klass;
4109         int i;
4110
4111         for (i = 1; i <= t->rows; ++i) {
4112                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4113                 mono_class_init (klass);
4114         }
4115 }
4116 #endif
4117
4118 static void
4119 add_signal_handler (int signo, void (*handler)(int))
4120 {
4121 #ifdef PLATFORM_WIN32
4122         signal (signo, handler);
4123 #else
4124         struct sigaction sa;
4125
4126         sa.sa_handler = handler;
4127         sigemptyset (&sa.sa_mask);
4128         sa.sa_flags = 0;
4129
4130         g_assert (sigaction (signo, &sa, NULL) != -1);
4131 #endif
4132 }
4133
4134 static void
4135 segv_handler (int signum)
4136 {
4137         ThreadContext *context = TlsGetValue (thread_context_id);
4138         MonoException *segv_exception;
4139
4140         if (context == NULL)
4141                 return;
4142         segv_exception = mono_get_exception_null_reference ();
4143         segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4144         mono_raise_exception (segv_exception);
4145 }
4146
4147
4148 static void
4149 quit_handler (int signum)
4150 {
4151         ThreadContext *context = TlsGetValue (thread_context_id);
4152         MonoException *quit_exception;
4153
4154         if (context == NULL)
4155                 return;
4156         quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4157         mono_raise_exception (quit_exception);
4158 }
4159
4160 static void
4161 abrt_handler (int signum)
4162 {
4163         ThreadContext *context = TlsGetValue (thread_context_id);
4164         MonoException *abrt_exception;
4165
4166         if (context == NULL)
4167                 return;
4168         abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4169         mono_raise_exception (abrt_exception);
4170 }
4171
4172 static void
4173 thread_abort_handler (int signum)
4174 {
4175         ThreadContext *context = TlsGetValue (thread_context_id);
4176         MonoThread *thread;
4177         MonoException *exc;
4178
4179         if (context == NULL)
4180                 return;
4181
4182         exc = mono_thread_request_interruption (context->managed_code); 
4183         if (exc) mono_raise_exception (exc);
4184 }
4185
4186 static MonoBoolean
4187 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
4188                           MonoReflectionMethod **method, 
4189                           gint32 *iloffset, gint32 *native_offset,
4190                           MonoString **file, gint32 *line, gint32 *column)
4191 {
4192         ThreadContext *context = TlsGetValue (thread_context_id);
4193         MonoInvocation *inv = context->current_frame;
4194         int i;
4195
4196         for (i = 0; inv && i < skip; inv = inv->parent)
4197                 if (inv->runtime_method != NULL)
4198                         ++i;
4199
4200         if (iloffset)
4201                 *iloffset = 0;
4202         if (native_offset)
4203                 *native_offset = 0;
4204         if (method)
4205                 *method = inv == NULL ? NULL : mono_method_get_object (context->domain, inv->runtime_method->method, NULL);
4206         if (line)
4207                 *line = 0;
4208         if (need_file_info) {
4209                 if (column)
4210                         *column = 0;
4211                 if (file)
4212                         *file = mono_string_new (mono_domain_get (), "unknown");
4213         }
4214
4215         return TRUE;
4216 }
4217
4218 static MonoArray *
4219 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4220 {
4221         MonoDomain *domain = mono_domain_get ();
4222         MonoArray *res;
4223         MonoArray *ta = exc->trace_ips;
4224         int i, len;
4225
4226         if (ta == NULL) {
4227                 /* Exception is not thrown yet */
4228                 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
4229         }
4230         
4231         len = mono_array_length (ta);
4232
4233         res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
4234
4235         for (i = skip; i < len / 2; i++) {
4236                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
4237                 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4238                 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4239
4240                 if (rtm != NULL) {
4241                         sf->method = mono_method_get_object (domain, rtm->method, NULL);
4242                         sf->native_offset = ip - rtm->code;
4243                 }
4244
4245 #if 0
4246                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4247
4248                 if (need_file_info) {
4249                         gchar *filename;
4250                         
4251                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4252
4253                         sf->filename = filename? mono_string_new (domain, filename): NULL;
4254                         sf->column = 0;
4255
4256                         g_free (filename);
4257                 }
4258 #endif
4259
4260                 mono_array_set (res, gpointer, i, sf);
4261         }
4262
4263         return res;
4264 }
4265
4266 static MonoObject *
4267 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
4268                                                    MonoReflectionMethod *info)
4269 {
4270         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4271         MonoObject *delegate;
4272
4273         mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4274
4275         delegate = mono_object_new (mono_object_domain (type), delegate_class);
4276
4277         interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (info->method));
4278
4279         return delegate;
4280 }
4281
4282
4283 typedef struct
4284 {
4285         MonoDomain *domain;
4286         int enable_debugging;
4287         char *file;
4288         int argc;
4289         char **argv;
4290 } MainThreadArgs;
4291
4292 static void main_thread_handler (gpointer user_data)
4293 {
4294         MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4295         MonoAssembly *assembly;
4296
4297         if (main_args->enable_debugging) {
4298                 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4299                 mono_debug_init_1 (main_args->domain);
4300         }
4301
4302         assembly = mono_domain_assembly_open (main_args->domain,
4303                                               main_args->file);
4304
4305         if (!assembly){
4306                 fprintf (stderr, "Can not open image %s\n", main_args->file);
4307                 exit (1);
4308         }
4309
4310         if (main_args->enable_debugging)
4311                 mono_debug_init_2 (assembly);
4312
4313 #ifdef RUN_TEST
4314         test_load_class (assembly->image);
4315 #else
4316
4317         ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4318 #endif
4319 }
4320
4321 static void
4322 mono_runtime_install_handlers (void)
4323 {
4324         add_signal_handler (SIGSEGV, segv_handler);
4325         add_signal_handler (SIGINT, quit_handler);
4326         add_signal_handler (SIGABRT, abrt_handler);
4327         add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4328 }
4329
4330 static void
4331 quit_function (MonoDomain *domain, gpointer user_data)
4332 {
4333         mono_profiler_shutdown ();
4334         
4335         mono_runtime_cleanup (domain);
4336         mono_domain_free (domain, TRUE);
4337
4338 }
4339
4340 void
4341 mono_interp_cleanup(MonoDomain *domain)
4342 {
4343         quit_function (domain, NULL);
4344 }
4345
4346 int
4347 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4348 {
4349         return ves_exec (domain, assembly, argc, argv);
4350 }
4351
4352 MonoDomain *
4353 mono_interp_init(const char *file)
4354 {
4355         MonoDomain *domain;
4356
4357         g_set_prgname (file);
4358         mono_set_rootdir ();
4359         
4360         g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4361         g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4362
4363         g_thread_init (NULL);
4364
4365         thread_context_id = TlsAlloc ();
4366         TlsSetValue (thread_context_id, NULL);
4367         InitializeCriticalSection (&runtime_method_lookup_section);
4368         InitializeCriticalSection (&create_method_pointer_mutex);
4369
4370         mono_runtime_install_handlers ();
4371         mono_interp_transform_init ();
4372         mono_install_compile_method (mono_create_method_pointer);
4373         mono_install_runtime_invoke (interp_mono_runtime_invoke);
4374         mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4375         mono_install_trampoline (interp_create_trampoline);
4376
4377         mono_install_handler (interp_ex_handler);
4378         mono_install_stack_walk (interp_walk_stack);
4379         mono_runtime_install_cleanup (quit_function);
4380         abort_requested = mono_thread_interruption_request_flag ();
4381
4382         domain = mono_init (file);
4383 #ifdef __hpux /* generates very big stack frames */
4384         mono_threads_set_default_stacksize(32*1024*1024);
4385 #endif
4386         mono_init_icall ();
4387         mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4388         mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4389         mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4390         mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4391
4392         mono_runtime_init (domain, NULL, NULL);
4393
4394         return domain;
4395 }
4396
4397 int 
4398 mono_main (int argc, char *argv [])
4399 {
4400         MonoDomain *domain;
4401         int retval = 0, i;
4402         char *file, *config_file = NULL;
4403         int enable_debugging = FALSE;
4404         MainThreadArgs main_args;
4405         const char *error;
4406
4407         setlocale (LC_ALL, "");
4408         if (argc < 2)
4409                 usage ();
4410
4411         for (i = 1; i < argc && argv [i][0] == '-'; i++){
4412                 if (strcmp (argv [i], "--trace") == 0)
4413                         global_tracing = 1;
4414                 if (strcmp (argv [i], "--noptr") == 0)
4415                         global_no_pointers = 1;
4416                 if (strcmp (argv [i], "--traceops") == 0)
4417                         global_tracing = 2;
4418                 if (strcmp (argv [i], "--traceopt") == 0)
4419                         ++mono_interp_traceopt;
4420                 if (strcmp (argv [i], "--dieonex") == 0) {
4421                         die_on_exception = 1;
4422                         enable_debugging = 1;
4423                 }
4424                 if (strcmp (argv [i], "--print-vtable") == 0)
4425                         mono_print_vtable = TRUE;
4426                 if (strcmp (argv [i], "--profile") == 0)
4427                         mono_profiler_load (NULL);
4428                 if (strcmp (argv [i], "--config") == 0)
4429                         config_file = argv [++i];
4430                 if (strcmp (argv [i], "--workers") == 0) {
4431                         mono_max_worker_threads = atoi (argv [++i]);
4432                         if (mono_max_worker_threads < 1)
4433                                 mono_max_worker_threads = 1;
4434                 }
4435                 if (strcmp (argv [i], "--help") == 0)
4436                         usage ();
4437 #if DEBUG_INTERP
4438                 if (strcmp (argv [i], "--debug") == 0) {
4439                         MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
4440                         if (!desc)
4441                                 g_error ("Invalid method name '%s'", argv [i]);
4442                         db_methods = g_list_append (db_methods, desc);
4443                 }
4444                 if (strcmp (argv [i], "--nested") == 0)
4445                         nested_trace = 1;
4446 #endif
4447         }
4448         
4449         file = argv [i];
4450
4451         if (!file)
4452                 usage ();
4453
4454         domain = mono_interp_init(file);
4455         mono_config_parse (config_file);
4456
4457         error = mono_check_corlib_version ();
4458         if (error) {
4459                 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
4460                 fprintf (stderr, "Download a newer corlib at http://www.go-mono.com/daily.\n");
4461                 exit (1);
4462         }
4463
4464         main_args.domain=domain;
4465         main_args.file=file;
4466         main_args.argc=argc-i;
4467         main_args.argv=argv+i;
4468         main_args.enable_debugging=enable_debugging;
4469         
4470         mono_runtime_exec_managed_code (domain, main_thread_handler,
4471                                         &main_args);
4472
4473         quit_function (domain, NULL);
4474
4475         /* Get the return value from System.Environment.ExitCode */
4476         retval=mono_environment_exitcode_get ();
4477
4478 #if COUNT_OPS
4479         for (i = 0; i < 512; i++)
4480                 if (opcode_counts[i] != 0)
4481                         printf("%s %d\n", mono_interp_opname[i], opcode_counts[i]);
4482 #endif
4483         return retval;
4484 }