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