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