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