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