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