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