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