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