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