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