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