72fcf9f131116d31561b06333ca06a5a7948cd09
[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 RuntimeMethod*
257 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
258 {
259         RuntimeMethod *rtm;
260         error_init (error);
261
262         mono_domain_jit_code_hash_lock (domain);
263         if ((rtm = mono_internal_hash_table_lookup (&domain->jit_code_hash, method))) {
264                 mono_domain_jit_code_hash_unlock (domain);
265                 return rtm;
266         }
267         rtm = mono_domain_alloc0 (domain, sizeof (RuntimeMethod));
268         rtm->method = method;
269         rtm->param_count = mono_method_signature (method)->param_count;
270         rtm->hasthis = mono_method_signature (method)->hasthis;
271         mono_internal_hash_table_insert (&domain->jit_code_hash, method, rtm);
272         mono_domain_jit_code_hash_unlock (domain);
273
274         return rtm;
275 }
276
277 gpointer
278 mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
279 {
280         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
281                 method = mono_marshal_get_synchronized_wrapper (method);
282         return mono_interp_get_runtime_method (domain, method, error);
283 }
284
285 static inline RuntimeMethod*
286 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
287 {
288         MonoMethod *m = runtime_method->method;
289         MonoError error;
290
291         if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
292                 RuntimeMethod *ret = NULL;
293                 if (mono_object_is_transparent_proxy (obj)) {
294                         ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
295                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
296                 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
297                         ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
298                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
299                 } else {
300                         ret = runtime_method;
301                 }
302                 return ret;
303         }
304
305         mono_class_setup_vtable (obj->vtable->klass);
306
307         int slot = mono_method_get_vtable_slot (m);
308         if (mono_class_is_interface (m->klass)) {
309                 g_assert (obj->vtable->klass != m->klass);
310                 /* TODO: interface offset lookup is slow, go through IMT instead */
311                 gboolean non_exact_match;
312                 slot += mono_class_interface_offset_with_variance (obj->vtable->klass, m->klass, &non_exact_match);
313         }
314
315         MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
316         if (m->is_inflated && mono_method_get_context (m)->method_inst) {
317                 MonoGenericContext context = { NULL, NULL };
318
319                 if (mono_class_is_ginst (virtual_method->klass))
320                         context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
321                 else if (mono_class_is_gtd (virtual_method->klass))
322                         context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
323                 context.method_inst = mono_method_get_context (m)->method_inst;
324
325                 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, &error);
326                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
327         }
328
329         if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
330                 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
331         }
332
333         RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
334         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
335         return virtual_runtime_method;
336 }
337
338 static void inline
339 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
340 {
341         if (type->byref) {
342                 switch (type->type) {
343                 case MONO_TYPE_OBJECT:
344                 case MONO_TYPE_CLASS:
345                 case MONO_TYPE_STRING:
346                 case MONO_TYPE_ARRAY:
347                 case MONO_TYPE_SZARRAY:
348                         break;
349                 default:
350                         break;
351                 }
352                 result->data.p = *(gpointer*)data;
353                 return;
354         }
355         switch (type->type) {
356         case MONO_TYPE_VOID:
357                 return;
358         case MONO_TYPE_I1:
359                 result->data.i = *(gint8*)data;
360                 return;
361         case MONO_TYPE_U1:
362         case MONO_TYPE_BOOLEAN:
363                 result->data.i = *(guint8*)data;
364                 return;
365         case MONO_TYPE_I2:
366                 result->data.i = *(gint16*)data;
367                 return;
368         case MONO_TYPE_U2:
369         case MONO_TYPE_CHAR:
370                 result->data.i = *(guint16*)data;
371                 return;
372         case MONO_TYPE_I4:
373                 result->data.i = *(gint32*)data;
374                 return;
375         case MONO_TYPE_U:
376         case MONO_TYPE_I:
377                 result->data.nati = *(mono_i*)data;
378                 return;
379         case MONO_TYPE_PTR:
380                 result->data.p = *(gpointer*)data;
381                 return;
382         case MONO_TYPE_U4:
383                 result->data.i = *(guint32*)data;
384                 return;
385         case MONO_TYPE_R4:
386                 result->data.f = *(float*)data;
387                 return;
388         case MONO_TYPE_I8:
389         case MONO_TYPE_U8:
390                 result->data.l = *(gint64*)data;
391                 return;
392         case MONO_TYPE_R8:
393                 result->data.f = *(double*)data;
394                 return;
395         case MONO_TYPE_STRING:
396         case MONO_TYPE_SZARRAY:
397         case MONO_TYPE_CLASS:
398         case MONO_TYPE_OBJECT:
399         case MONO_TYPE_ARRAY:
400                 result->data.p = *(gpointer*)data;
401                 return;
402         case MONO_TYPE_VALUETYPE:
403                 if (type->data.klass->enumtype) {
404                         stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
405                         return;
406                 } else
407                         mono_value_copy (result->data.vt, data, type->data.klass);
408                 return;
409         case MONO_TYPE_GENERICINST:
410                 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
411                 return;
412         default:
413                 g_warning ("got type 0x%02x", type->type);
414                 g_assert_not_reached ();
415         }
416 }
417
418 static void inline
419 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
420 {
421         if (type->byref) {
422                 gpointer *p = (gpointer*)data;
423                 *p = val->data.p;
424                 return;
425         }
426         /* printf ("TODAT0 %p\n", data); */
427         switch (type->type) {
428         case MONO_TYPE_I1:
429         case MONO_TYPE_U1: {
430                 guint8 *p = (guint8*)data;
431                 *p = val->data.i;
432                 return;
433         }
434         case MONO_TYPE_BOOLEAN: {
435                 guint8 *p = (guint8*)data;
436                 *p = (val->data.i != 0);
437                 return;
438         }
439         case MONO_TYPE_I2:
440         case MONO_TYPE_U2:
441         case MONO_TYPE_CHAR: {
442                 guint16 *p = (guint16*)data;
443                 *p = val->data.i;
444                 return;
445         }
446         case MONO_TYPE_I: {
447                 mono_i *p = (mono_i*)data;
448                 /* In theory the value used by stloc should match the local var type
449                    but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
450                    a native int - both by csc and mcs). Not sure what to do about sign extension
451                    as it is outside the spec... doing the obvious */
452                 *p = (mono_i)val->data.nati;
453                 return;
454         }
455         case MONO_TYPE_U: {
456                 mono_u *p = (mono_u*)data;
457                 /* see above. */
458                 *p = (mono_u)val->data.nati;
459                 return;
460         }
461         case MONO_TYPE_I4:
462         case MONO_TYPE_U4: {
463                 gint32 *p = (gint32*)data;
464                 *p = val->data.i;
465                 return;
466         }
467         case MONO_TYPE_I8:
468         case MONO_TYPE_U8: {
469                 gint64 *p = (gint64*)data;
470                 *p = val->data.l;
471                 return;
472         }
473         case MONO_TYPE_R4: {
474                 float *p = (float*)data;
475                 *p = val->data.f;
476                 return;
477         }
478         case MONO_TYPE_R8: {
479                 double *p = (double*)data;
480                 *p = val->data.f;
481                 return;
482         }
483         case MONO_TYPE_STRING:
484         case MONO_TYPE_SZARRAY:
485         case MONO_TYPE_CLASS:
486         case MONO_TYPE_OBJECT:
487         case MONO_TYPE_ARRAY:
488         case MONO_TYPE_PTR: {
489                 gpointer *p = (gpointer*)data;
490                 *p = val->data.p;
491                 return;
492         }
493         case MONO_TYPE_VALUETYPE:
494                 if (type->data.klass->enumtype) {
495                         stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
496                         return;
497                 } else
498                         mono_value_copy (data, val->data.vt, type->data.klass);
499                 return;
500         case MONO_TYPE_GENERICINST:
501                 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
502                 return;
503         default:
504                 g_warning ("got type %x", type->type);
505                 g_assert_not_reached ();
506         }
507 }
508
509 static void
510 fill_in_trace (MonoException *exception, MonoInvocation *frame)
511 {
512         char *stack_trace = dump_frame (frame);
513         MonoDomain *domain = mono_domain_get();
514         (exception)->stack_trace = mono_string_new (domain, stack_trace);
515         (exception)->trace_ips = get_trace_ips (domain, frame);
516         g_free (stack_trace);
517 }
518
519 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
520
521 #define THROW_EX(exception,ex_ip)       \
522         do {\
523                 frame->ip = (ex_ip);            \
524                 frame->ex = (MonoException*)(exception);        \
525                 FILL_IN_TRACE(frame->ex, frame); \
526                 goto handle_exception;  \
527         } while (0)
528
529 static MonoObject*
530 ves_array_create (MonoInvocation *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
531 {
532         uintptr_t *lengths;
533         intptr_t *lower_bounds;
534         MonoObject *obj;
535         MonoError error;
536         int i;
537
538         lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
539         for (i = 0; i < sig->param_count; ++i) {
540                 lengths [i] = values->data.i;
541                 values ++;
542         }
543         if (klass->rank == sig->param_count) {
544                 /* Only lengths provided. */
545                 lower_bounds = NULL;
546         } else {
547                 /* lower bounds are first. */
548                 lower_bounds = (intptr_t *) lengths;
549                 lengths += klass->rank;
550         }
551         obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
552         if (!mono_error_ok (&error)) {
553                 frame->ex = mono_error_convert_to_exception (&error);
554                 FILL_IN_TRACE (frame->ex, frame);
555         }
556         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
557         return obj;
558 }
559
560 static gint32
561 ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame)
562 {
563         g_assert (!frame->ex);
564         MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
565
566         guint32 pos = 0;
567         if (ao->bounds) {
568                 for (gint32 i = 0; i < ac->rank; i++) {
569                         guint32 idx = sp [i].data.i;
570                         guint32 lower = ao->bounds [i].lower_bound;
571                         guint32 len = ao->bounds [i].length;
572                         if (idx < lower || (idx - lower) >= len) {
573                                 frame->ex = mono_get_exception_index_out_of_range ();
574                                 FILL_IN_TRACE (frame->ex, frame);
575                                 return -1;
576                         }
577                         pos = (pos * len) + idx - lower;
578                 }
579         } else {
580                 pos = sp [0].data.i;
581                 if (pos >= ao->max_length) {
582                         frame->ex = mono_get_exception_index_out_of_range ();
583                         FILL_IN_TRACE (frame->ex, frame);
584                         return -1;
585                 }
586         }
587         return pos;
588 }
589
590 static void
591 ves_array_set (MonoInvocation *frame)
592 {
593         stackval *sp = frame->stack_args + 1;
594
595         MonoObject *o = frame->stack_args->data.p;
596         MonoArray *ao = (MonoArray *) o;
597         MonoClass *ac = o->vtable->klass;
598
599         g_assert (ac->rank >= 1);
600
601         gint32 pos = ves_array_calculate_index (ao, sp, frame);
602         if (frame->ex)
603                 return;
604
605         if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
606                 MonoError error;
607                 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
608                 mono_error_cleanup (&error);
609                 if (!isinst) {
610                         frame->ex = mono_get_exception_array_type_mismatch ();
611                         FILL_IN_TRACE (frame->ex, frame);
612                         return;
613                 }
614         }
615
616         gint32 esize = mono_array_element_size (ac);
617         gpointer ea = mono_array_addr_with_size (ao, esize, pos);
618
619         MonoType *mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
620         stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
621 }
622
623 static void
624 ves_array_get (MonoInvocation *frame)
625 {
626         stackval *sp = frame->stack_args + 1;
627
628         MonoObject *o = frame->stack_args->data.p;
629         MonoArray *ao = (MonoArray *) o;
630         MonoClass *ac = o->vtable->klass;
631
632         g_assert (ac->rank >= 1);
633
634         gint32 pos = ves_array_calculate_index (ao, sp, frame);
635         if (frame->ex)
636                 return;
637
638         gint32 esize = mono_array_element_size (ac);
639         gpointer ea = mono_array_addr_with_size (ao, esize, pos);
640
641         MonoType *mt = mono_method_signature (frame->runtime_method->method)->ret;
642         stackval_from_data (mt, frame->retval, ea, FALSE);
643 }
644
645 static gpointer
646 ves_array_element_address (MonoInvocation *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
647 {
648         MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
649
650         g_assert (ac->rank >= 1);
651
652         gint32 pos = ves_array_calculate_index (ao, sp, frame);
653         if (frame->ex)
654                 return NULL;
655
656         if (needs_typecheck && !mono_class_is_assignable_from (mono_object_class ((MonoObject *) ao)->element_class, required_type->element_class)) {
657                 frame->ex = mono_get_exception_array_type_mismatch ();
658                 FILL_IN_TRACE (frame->ex, frame);
659                 return NULL;
660         }
661         gint32 esize = mono_array_element_size (ac);
662         return mono_array_addr_with_size (ao, esize, pos);
663 }
664
665 void
666 interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUnwindOptions options, void *user_data)
667 {
668         MonoError error;
669         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
670
671         MonoInvocation *frame = context->current_frame;
672
673         while (frame) {
674                 MonoStackFrameInfo fi;
675                 memset (&fi, 0, sizeof (MonoStackFrameInfo));
676
677                 /* TODO: hack to make some asserts happy. */
678                 fi.ji = (MonoJitInfo *) frame->runtime_method;
679
680                 if (frame->runtime_method)
681                         fi.method = fi.actual_method = frame->runtime_method->method;
682
683                 if (!fi.method || (fi.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (fi.method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
684                         fi.il_offset = -1;
685                         fi.type = FRAME_TYPE_MANAGED_TO_NATIVE;
686                 } else {
687                         MonoMethodHeader *hd = mono_method_get_header_checked (fi.method, &error);
688                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
689                         fi.type = FRAME_TYPE_MANAGED;
690                         fi.il_offset = frame->ip - (const unsigned short *) hd->code;
691                         if (!fi.method->wrapper_type)
692                                 fi.managed = TRUE;
693                 }
694
695                 if (func (&fi, ctx, user_data))
696                         return;
697                 frame = frame->parent;
698         }
699
700         g_assert (0);
701 }
702
703 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
704
705 struct _MethodArguments {
706         size_t ilen;
707         gpointer *iargs;
708         size_t flen;
709         double *fargs;
710         gpointer *retval;
711         size_t is_float_ret;
712 };
713
714 typedef struct _MethodArguments MethodArguments;
715
716 // TODO: this function is also arch dependent (register width).
717 static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
718 {
719         // TODO: don't malloc this data structure.
720         MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
721
722         if (sig->hasthis)
723                 margs->ilen++;
724
725         for (int i = 0; i < sig->param_count; i++) {
726                 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
727                 switch (ptype) {
728                 case MONO_TYPE_BOOLEAN:
729                 case MONO_TYPE_CHAR:
730                 case MONO_TYPE_I1:
731                 case MONO_TYPE_U1:
732                 case MONO_TYPE_I2:
733                 case MONO_TYPE_U2:
734                 case MONO_TYPE_I4:
735                 case MONO_TYPE_U4:
736                 case MONO_TYPE_I:
737                 case MONO_TYPE_U:
738                 case MONO_TYPE_PTR:
739                 case MONO_TYPE_SZARRAY:
740                 case MONO_TYPE_CLASS:
741                 case MONO_TYPE_OBJECT:
742                 case MONO_TYPE_STRING:
743                 case MONO_TYPE_I8:
744                 case MONO_TYPE_VALUETYPE:
745                 case MONO_TYPE_GENERICINST:
746                         margs->ilen++;
747                         break;
748                 case MONO_TYPE_R4:
749                 case MONO_TYPE_R8:
750                         margs->flen++;
751                         break;
752                 default:
753                         g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
754                 }
755         }
756
757         if (margs->ilen > 0)
758                 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
759
760         if (margs->flen > 0)
761                 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
762
763         if (margs->ilen > 8)
764                 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
765
766         if (margs->flen > 3)
767                 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
768
769
770         size_t int_i = 0;
771         size_t int_f = 0;
772
773         if (sig->hasthis) {
774                 margs->iargs [0] = frame->stack_args->data.p;
775                 int_i++;
776         }
777
778         for (int i = 0; i < sig->param_count; i++) {
779                 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
780                 switch (ptype) {
781                 case MONO_TYPE_BOOLEAN:
782                 case MONO_TYPE_CHAR:
783                 case MONO_TYPE_I1:
784                 case MONO_TYPE_U1:
785                 case MONO_TYPE_I2:
786                 case MONO_TYPE_U2:
787                 case MONO_TYPE_I4:
788                 case MONO_TYPE_U4:
789                 case MONO_TYPE_I:
790                 case MONO_TYPE_U:
791                 case MONO_TYPE_PTR:
792                 case MONO_TYPE_SZARRAY:
793                 case MONO_TYPE_CLASS:
794                 case MONO_TYPE_OBJECT:
795                 case MONO_TYPE_STRING:
796                 case MONO_TYPE_I8:
797                 case MONO_TYPE_VALUETYPE:
798                 case MONO_TYPE_GENERICINST:
799                         margs->iargs [int_i] = frame->stack_args [i].data.p;
800 #if DEBUG_INTERP
801                         g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
802 #endif
803                         int_i++;
804                         break;
805                 case MONO_TYPE_R4:
806                 case MONO_TYPE_R8:
807                         if (ptype == MONO_TYPE_R4)
808                                 * (float *) &(margs->fargs [int_f]) = (float) frame->stack_args [i].data.f;
809                         else
810                                 margs->fargs [int_f] = frame->stack_args [i].data.f;
811 #if DEBUG_INTERP
812                         g_print ("build_args_from_sig: margs->fargs [%d]: %p (%f) (frame @ %d)\n", int_f, margs->fargs [int_f], margs->fargs [int_f], i);
813 #endif
814                         int_f++;
815                         break;
816                 default:
817                         g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
818                 }
819         }
820
821         switch (sig->ret->type) {
822                 case MONO_TYPE_BOOLEAN:
823                 case MONO_TYPE_CHAR:
824                 case MONO_TYPE_I1:
825                 case MONO_TYPE_U1:
826                 case MONO_TYPE_I2:
827                 case MONO_TYPE_U2:
828                 case MONO_TYPE_I4:
829                 case MONO_TYPE_U4:
830                 case MONO_TYPE_I:
831                 case MONO_TYPE_U:
832                 case MONO_TYPE_PTR:
833                 case MONO_TYPE_SZARRAY:
834                 case MONO_TYPE_CLASS:
835                 case MONO_TYPE_OBJECT:
836                 case MONO_TYPE_STRING:
837                 case MONO_TYPE_I8:
838                 case MONO_TYPE_VALUETYPE:
839                 case MONO_TYPE_GENERICINST:
840                         margs->retval = &(frame->retval->data.p);
841                         break;
842                 case MONO_TYPE_R4:
843                 case MONO_TYPE_R8:
844                         margs->retval = &(frame->retval->data.p);
845                         margs->is_float_ret = 1;
846                         break;
847                 case MONO_TYPE_VOID:
848                         margs->retval = NULL;
849                         break;
850                 default:
851                         g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
852         }
853
854         return margs;
855 }
856
857 static void 
858 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
859 {
860         jmp_buf env;
861         MonoInvocation *old_frame = context->current_frame;
862         MonoInvocation *old_env_frame = context->env_frame;
863         jmp_buf *old_env = context->current_env;
864
865         if (setjmp (env)) {
866                 context->current_frame = old_frame;
867                 context->env_frame = old_env_frame;
868                 context->current_env = old_env;
869                 context->managed_code = 1;
870                 return;
871         }
872
873         frame->ex = NULL;
874         context->env_frame = frame;
875         context->current_env = &env;
876
877         g_assert (!frame->runtime_method);
878         if (!mono_interp_enter_icall_trampoline) {
879                 MonoTrampInfo *info;
880                 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
881                 // TODO:
882                 // mono_tramp_info_register (info, NULL);
883         }
884
885         MethodArguments *margs = build_args_from_sig (sig, frame);
886 #if DEBUG_INTERP
887         g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
888         g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
889 #endif
890
891         context->current_frame = frame;
892         context->managed_code = 0;
893
894         mono_interp_enter_icall_trampoline (addr, margs);
895
896         context->managed_code = 1;
897         /* domain can only be changed by native code */
898         context->domain = mono_domain_get ();
899
900         if (*mono_thread_interruption_request_flag ()) {
901                 MonoException *exc = mono_thread_interruption_checkpoint ();
902                 if (exc) {
903                         frame->ex = exc;
904                         context->search_for_handler = 1;
905                 }
906         }
907         
908         if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
909                 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
910
911         context->current_frame = old_frame;
912         context->env_frame = old_env_frame;
913         context->current_env = old_env;
914
915         g_free (margs->iargs);
916         g_free (margs->fargs);
917         g_free (margs);
918 }
919
920 void
921 mono_interp_init_delegate (MonoDelegate *del)
922 {
923         if (!del->method)
924                 del->method = ((RuntimeMethod *) del->method_ptr)->method;
925 }
926
927 /*
928  * From the spec:
929  * runtime specifies that the implementation of the method is automatically
930  * provided by the runtime and is primarily used for the methods of delegates.
931  */
932 static void
933 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
934 {
935         MonoMethod *method = frame->runtime_method->method;
936         const char *name = method->name;
937         MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
938         MonoObject *isinst_obj;
939         MonoError error;
940
941         mono_class_init (method->klass);
942
943         isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
944         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
945         if (obj && isinst_obj) {
946                 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
947                         ves_array_set (frame);
948                         return;
949                 }
950                 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
951                         ves_array_get (frame);
952                         return;
953                 }
954         }
955         
956         g_error ("Don't know how to exec runtime method %s.%s::%s", 
957                         method->klass->name_space, method->klass->name,
958                         method->name);
959 }
960
961 static char*
962 dump_stack (stackval *stack, stackval *sp)
963 {
964         stackval *s = stack;
965         GString *str = g_string_new ("");
966         
967         if (sp == stack)
968                 return g_string_free (str, FALSE);
969         
970         while (s < sp) {
971                 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
972                 ++s;
973         }
974         return g_string_free (str, FALSE);
975 }
976
977 static void
978 dump_stackval (GString *str, stackval *s, MonoType *type)
979 {
980         switch (type->type) {
981         case MONO_TYPE_I1:
982         case MONO_TYPE_U1:
983         case MONO_TYPE_I2:
984         case MONO_TYPE_U2:
985         case MONO_TYPE_I4:
986         case MONO_TYPE_U4:
987         case MONO_TYPE_CHAR:
988         case MONO_TYPE_BOOLEAN:
989                 g_string_append_printf (str, "[%d] ", s->data.i);
990                 break;
991         case MONO_TYPE_STRING:
992         case MONO_TYPE_SZARRAY:
993         case MONO_TYPE_CLASS:
994         case MONO_TYPE_OBJECT:
995         case MONO_TYPE_ARRAY:
996         case MONO_TYPE_PTR:
997         case MONO_TYPE_I:
998         case MONO_TYPE_U:
999                 g_string_append_printf (str, "[%p] ", s->data.p);
1000                 break;
1001         case MONO_TYPE_VALUETYPE:
1002                 if (type->data.klass->enumtype)
1003                         g_string_append_printf (str, "[%d] ", s->data.i);
1004                 else
1005                         g_string_append_printf (str, "[vt:%p] ", s->data.p);
1006                 break;
1007         case MONO_TYPE_R4:
1008         case MONO_TYPE_R8:
1009                 g_string_append_printf (str, "[%g] ", s->data.f);
1010                 break;
1011         case MONO_TYPE_I8:
1012         case MONO_TYPE_U8:
1013         default: {
1014                 GString *res = g_string_new ("");
1015                 mono_type_get_desc (res, type, TRUE);
1016                 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1017                 g_string_free (res, TRUE);
1018                 break;
1019         }
1020         }
1021 }
1022
1023 static char*
1024 dump_args (MonoInvocation *inv)
1025 {
1026         GString *str = g_string_new ("");
1027         int i;
1028         MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1029         
1030         if (signature->param_count == 0 && !signature->hasthis)
1031                 return g_string_free (str, FALSE);
1032
1033         if (signature->hasthis) {
1034                 MonoMethod *method = inv->runtime_method->method;
1035                 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1036         }
1037
1038         for (i = 0; i < signature->param_count; ++i)
1039                 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1040
1041         return g_string_free (str, FALSE);
1042 }
1043
1044 static char*
1045 dump_retval (MonoInvocation *inv)
1046 {
1047         GString *str = g_string_new ("");
1048         MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1049
1050         if (ret->type != MONO_TYPE_VOID)
1051                 dump_stackval (str, inv->retval, ret);
1052
1053         return g_string_free (str, FALSE);
1054 }
1055  
1056 static char*
1057 dump_frame (MonoInvocation *inv)
1058 {
1059         GString *str = g_string_new ("");
1060         int i;
1061         char *args;
1062         MonoError error;
1063
1064         for (i = 0; inv; inv = inv->parent) {
1065                 if (inv->runtime_method != NULL) {
1066                         MonoMethod *method = inv->runtime_method->method;
1067                         MonoClass *k;
1068
1069                         int codep = 0;
1070                         const char * opname = "";
1071                         char *name;
1072                         gchar *source = NULL;
1073
1074                         k = method->klass;
1075
1076                         if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1077                                 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1078                                 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1079                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1080
1081                                 if (hd != NULL) {
1082                                         if (inv->ip) {
1083                                                 opname = mono_interp_opname [*inv->ip];
1084                                                 codep = inv->ip - inv->runtime_method->code;
1085                                                 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1086                                         } else 
1087                                                 opname = "";
1088
1089 #if 0
1090                                         MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1091                                         source = mono_debug_method_lookup_location (minfo, codep);
1092 #endif
1093                                 }
1094                         }
1095                         args = dump_args (inv);
1096                         name = mono_method_full_name (method, TRUE);
1097                         if (source)
1098                                 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1099                         else
1100                                 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1101                         g_free (name);
1102                         g_free (args);
1103                         g_free (source);
1104                         ++i;
1105                 }
1106         }
1107         return g_string_free (str, FALSE);
1108 }
1109
1110 static MonoArray *
1111 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1112 {
1113         int i;
1114         MonoArray *res;
1115         MonoInvocation *inv;
1116         MonoError error;
1117
1118         for (i = 0, inv = top; inv; inv = inv->parent)
1119                 if (inv->runtime_method != NULL)
1120                         ++i;
1121
1122         res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1123         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1124
1125         for (i = 0, inv = top; inv; inv = inv->parent)
1126                 if (inv->runtime_method != NULL) {
1127                         mono_array_set (res, gpointer, i, inv->runtime_method);
1128                         ++i;
1129                         mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1130                         ++i;
1131                 }
1132
1133         return res;
1134 }
1135
1136
1137 #define MYGUINT64_MAX 18446744073709551615ULL
1138 #define MYGINT64_MAX 9223372036854775807LL
1139 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1140
1141 #define MYGUINT32_MAX 4294967295U
1142 #define MYGINT32_MAX 2147483647
1143 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1144         
1145 #define CHECK_ADD_OVERFLOW(a,b) \
1146         (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0  \
1147         : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1148
1149 #define CHECK_SUB_OVERFLOW(a,b) \
1150         (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0   \
1151         : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1152
1153 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1154         (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1155
1156 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1157         (guint32)(a) < (guint32)(b) ? -1 : 0
1158
1159 #define CHECK_ADD_OVERFLOW64(a,b) \
1160         (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0  \
1161         : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1162
1163 #define CHECK_SUB_OVERFLOW64(a,b) \
1164         (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0   \
1165         : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1166
1167 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1168         (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1169
1170 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1171         (guint64)(a) < (guint64)(b) ? -1 : 0
1172
1173 #if SIZEOF_VOID_P == 4
1174 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1175 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1176 #else
1177 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1178 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1179 #endif
1180
1181 /* Resolves to TRUE if the operands would overflow */
1182 #define CHECK_MUL_OVERFLOW(a,b) \
1183         ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1184         (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1185         (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1186         (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1187         (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1188         (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1189         (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1190
1191 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1192         ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1193         (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1194
1195 #define CHECK_MUL_OVERFLOW64(a,b) \
1196         ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1197         (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1198         (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1199         (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1200         (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1201         (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1202         (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1203
1204 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1205         ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1206         (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1207
1208 #if SIZEOF_VOID_P == 4
1209 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1210 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1211 #else
1212 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1213 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1214 #endif
1215
1216 MonoObject*
1217 mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1218 {
1219         MonoInvocation frame;
1220         ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1221         MonoObject *retval = NULL;
1222         MonoMethodSignature *sig = mono_method_signature (method);
1223         MonoClass *klass = mono_class_from_mono_type (sig->ret);
1224         int i, type, isobject = 0;
1225         void *ret = NULL;
1226         stackval result;
1227         stackval *args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
1228         ThreadContext context_struct;
1229         MonoInvocation *old_frame = NULL;
1230         jmp_buf env;
1231
1232         error_init (error);
1233         if (exc)
1234                 *exc = NULL;
1235
1236         frame.ex = NULL;
1237
1238         if (setjmp(env)) {
1239                 if (context != &context_struct) {
1240                         context->domain = mono_domain_get ();
1241                         context->current_frame = old_frame;
1242                         context->managed_code = 0;
1243                 } else 
1244                         mono_native_tls_set_value (thread_context_id, NULL);
1245                 if (exc != NULL)
1246                         *exc = (MonoObject *)frame.ex;
1247                 return retval;
1248         }
1249
1250         if (context == NULL) {
1251                 context = &context_struct;
1252                 context_struct.base_frame = &frame;
1253                 context_struct.current_frame = NULL;
1254                 context_struct.env_frame = &frame;
1255                 context_struct.current_env = &env;
1256                 context_struct.search_for_handler = 0;
1257                 context_struct.managed_code = 0;
1258                 mono_native_tls_set_value (thread_context_id, context);
1259         }
1260         else
1261                 old_frame = context->current_frame;
1262
1263         context->domain = mono_domain_get ();
1264
1265         switch (sig->ret->type) {
1266         case MONO_TYPE_VOID:
1267                 break;
1268         case MONO_TYPE_STRING:
1269         case MONO_TYPE_OBJECT:
1270         case MONO_TYPE_CLASS:
1271         case MONO_TYPE_ARRAY:
1272         case MONO_TYPE_SZARRAY:
1273                 isobject = 1;
1274                 break;
1275         case MONO_TYPE_VALUETYPE:
1276                 retval = mono_object_new_checked (context->domain, klass, error);
1277                 ret = ((char*)retval) + sizeof (MonoObject);
1278                 if (!sig->ret->data.klass->enumtype)
1279                         result.data.vt = ret;
1280                 break;
1281         default:
1282                 retval = mono_object_new_checked (context->domain, klass, error);
1283                 ret = ((char*)retval) + sizeof (MonoObject);
1284                 break;
1285         }
1286
1287         if (sig->hasthis)
1288                 args [0].data.p = obj;
1289
1290         for (i = 0; i < sig->param_count; ++i) {
1291                 int a_index = i + !!sig->hasthis;
1292                 if (sig->params [i]->byref) {
1293                         args [a_index].data.p = params [i];
1294                         continue;
1295                 }
1296                 type = sig->params [i]->type;
1297 handle_enum:
1298                 switch (type) {
1299                 case MONO_TYPE_U1:
1300                 case MONO_TYPE_I1:
1301                 case MONO_TYPE_BOOLEAN:
1302                         args [a_index].data.i = *(MonoBoolean*)params [i];
1303                         break;
1304                 case MONO_TYPE_U2:
1305                 case MONO_TYPE_I2:
1306                 case MONO_TYPE_CHAR:
1307                         args [a_index].data.i = *(gint16*)params [i];
1308                         break;
1309 #if SIZEOF_VOID_P == 4
1310                 case MONO_TYPE_U: /* use VAL_POINTER? */
1311                 case MONO_TYPE_I:
1312 #endif
1313                 case MONO_TYPE_U4:
1314                 case MONO_TYPE_I4:
1315                         args [a_index].data.i = *(gint32*)params [i];
1316                         break;
1317 #if SIZEOF_VOID_P == 8
1318                 case MONO_TYPE_U:
1319                 case MONO_TYPE_I:
1320 #endif
1321                 case MONO_TYPE_U8:
1322                 case MONO_TYPE_I8:
1323                         args [a_index].data.l = *(gint64*)params [i];
1324                         break;
1325                 case MONO_TYPE_VALUETYPE:
1326                         if (sig->params [i]->data.klass->enumtype) {
1327                                 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1328                                 goto handle_enum;
1329                         } else {
1330                                 args [a_index].data.p = params [i];
1331                         }
1332                         break;
1333                 case MONO_TYPE_STRING:
1334                 case MONO_TYPE_CLASS:
1335                 case MONO_TYPE_ARRAY:
1336                 case MONO_TYPE_SZARRAY:
1337                 case MONO_TYPE_OBJECT:
1338                 case MONO_TYPE_GENERICINST:
1339                         args [a_index].data.p = params [i];
1340                         break;
1341                 default:
1342                         g_error ("type 0x%x not handled in  runtime invoke", sig->params [i]->type);
1343                 }
1344         }
1345
1346         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1347                 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1348         INIT_FRAME (&frame,context->current_frame,args,&result,mono_get_root_domain (),method,error);
1349         if (exc)
1350                 frame.invoke_trap = 1;
1351         context->managed_code = 1;
1352         ves_exec_method_with_context (&frame, context);
1353         context->managed_code = 0;
1354         if (context == &context_struct)
1355                 mono_native_tls_set_value (thread_context_id, NULL);
1356         else
1357                 context->current_frame = old_frame;
1358         if (frame.ex != NULL) {
1359                 if (exc != NULL) {
1360                         *exc = (MonoObject*) frame.ex;
1361                         return NULL;
1362                 }
1363                 if (context->current_env != NULL) {
1364                         context->env_frame->ex = frame.ex;
1365                         longjmp(*context->current_env, 1);
1366                 }
1367                 else
1368                         printf("dropped exception...\n");
1369         }
1370         if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1371                 return NULL;
1372         if (isobject || method->string_ctor)
1373                 return result.data.p;
1374         stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1375         return retval;
1376 }
1377
1378 static stackval * 
1379 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1380 {
1381         MonoInvocation *old_frame = context->current_frame;
1382         MonoInvocation *old_env_frame = context->env_frame;
1383         jmp_buf *old_env = context->current_env;
1384         jmp_buf env;
1385
1386         if (setjmp (env)) {
1387                 context->current_frame = old_frame;
1388                 context->env_frame = old_env_frame;
1389                 context->current_env = old_env;
1390                 context->managed_code = 1;
1391                 return sp;
1392         }
1393
1394         context->env_frame = context->current_frame;
1395         context->current_env = &env;
1396         context->managed_code = 0;
1397
1398         switch (op) {
1399         case MINT_ICALL_V_V: {
1400                 void (*func)() = ptr;
1401                 func ();
1402                 break;
1403         }
1404         case MINT_ICALL_V_P: {
1405                 gpointer (*func)() = ptr;
1406                 sp++;
1407                 sp [-1].data.p = func ();
1408                 break;
1409         }
1410         case MINT_ICALL_P_V: {
1411                 void (*func)(gpointer) = ptr;
1412                 func (sp [-1].data.p);
1413                 sp --;
1414                 break;
1415         }
1416         case MINT_ICALL_P_P: {
1417                 gpointer (*func)(gpointer) = ptr;
1418                 sp [-1].data.p = func (sp [-1].data.p);
1419                 break;
1420         }
1421         case MINT_ICALL_PP_V: {
1422                 void (*func)(gpointer,gpointer) = ptr;
1423                 sp -= 2;
1424                 func (sp [0].data.p, sp [1].data.p);
1425                 break;
1426         }
1427         case MINT_ICALL_PI_V: {
1428                 void (*func)(gpointer,int) = ptr;
1429                 sp -= 2;
1430                 func (sp [0].data.p, sp [1].data.i);
1431                 break;
1432         }
1433         case MINT_ICALL_PP_P: {
1434                 gpointer (*func)(gpointer,gpointer) = ptr;
1435                 --sp;
1436                 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1437                 break;
1438         }
1439         case MINT_ICALL_PI_P: {
1440                 gpointer (*func)(gpointer,int) = ptr;
1441                 --sp;
1442                 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1443                 break;
1444         }
1445         case MINT_ICALL_PPP_V: {
1446                 void (*func)(gpointer,gpointer,gpointer) = ptr;
1447                 sp -= 3;
1448                 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1449                 break;
1450         }
1451         case MINT_ICALL_PPI_V: {
1452                 void (*func)(gpointer,gpointer,int) = ptr;
1453                 sp -= 3;
1454                 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1455                 break;
1456         }
1457         default:
1458                 g_assert_not_reached ();
1459         }
1460
1461         context->env_frame = old_env_frame;
1462         context->current_env = old_env;
1463
1464         return sp;
1465 }
1466
1467 static mono_mutex_t create_method_pointer_mutex;
1468
1469 static GHashTable *method_pointer_hash = NULL;
1470
1471 #define TRAMPS_USED 8
1472
1473 static MonoMethod *method_pointers [TRAMPS_USED] = {0};
1474
1475 #define GEN_METHOD_PTR_TRAMP(num) \
1476                 static MonoObject * mp_tramp_ ## num (MonoObject *this_obj, void **params, MonoObject **exc, void *compiled_method) { \
1477                         MonoError error; \
1478                         void *params_real[] = {this_obj, &params, &exc, &compiled_method}; \
1479                         MonoObject *ret = mono_interp_runtime_invoke (method_pointers [num], NULL, params_real, NULL, &error); \
1480                         mono_error_cleanup (&error); \
1481                         return ret; \
1482                 }
1483
1484
1485 GEN_METHOD_PTR_TRAMP (0);
1486 GEN_METHOD_PTR_TRAMP (1);
1487 GEN_METHOD_PTR_TRAMP (2);
1488 GEN_METHOD_PTR_TRAMP (3);
1489 GEN_METHOD_PTR_TRAMP (4);
1490 GEN_METHOD_PTR_TRAMP (5);
1491 GEN_METHOD_PTR_TRAMP (6);
1492 GEN_METHOD_PTR_TRAMP (7);
1493
1494 #undef GEN_METHOD_PTR_TRAMP
1495
1496 gpointer *mp_tramps[TRAMPS_USED] = {
1497         (gpointer) mp_tramp_0, (gpointer) mp_tramp_1, (gpointer) mp_tramp_2, (gpointer) mp_tramp_3,
1498         (gpointer) mp_tramp_4, (gpointer) mp_tramp_5, (gpointer) mp_tramp_6, (gpointer) mp_tramp_7
1499 };
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 < TRAMPS_USED);
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_exception;;
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                         if (sp [-1].data.i == (-1))
2558                                 THROW_EX (mono_get_exception_overflow (), ip);
2559                         BINOP(i, %);
2560                         MINT_IN_BREAK;
2561                 MINT_IN_CASE(MINT_REM_I8)
2562                         if (sp [-1].data.l == 0)
2563                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2564                         if (sp [-1].data.l == (-1))
2565                                 THROW_EX (mono_get_exception_overflow (), ip);
2566                         BINOP(l, %);
2567                         MINT_IN_BREAK;
2568                 MINT_IN_CASE(MINT_REM_R8)
2569                         /* FIXME: what do we actually do here? */
2570                         --sp;
2571                         sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2572                         ++ip;
2573                         MINT_IN_BREAK;
2574                 MINT_IN_CASE(MINT_REM_UN_I4)
2575                         if (sp [-1].data.i == 0)
2576                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2577                         BINOP_CAST(i, %, guint32);
2578                         MINT_IN_BREAK;
2579                 MINT_IN_CASE(MINT_REM_UN_I8)
2580                         if (sp [-1].data.l == 0)
2581                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2582                         BINOP_CAST(l, %, guint64);
2583                         MINT_IN_BREAK;
2584                 MINT_IN_CASE(MINT_AND_I4)
2585                         BINOP(i, &);
2586                         MINT_IN_BREAK;
2587                 MINT_IN_CASE(MINT_AND_I8)
2588                         BINOP(l, &);
2589                         MINT_IN_BREAK;
2590                 MINT_IN_CASE(MINT_OR_I4)
2591                         BINOP(i, |);
2592                         MINT_IN_BREAK;
2593                 MINT_IN_CASE(MINT_OR_I8)
2594                         BINOP(l, |);
2595                         MINT_IN_BREAK;
2596                 MINT_IN_CASE(MINT_XOR_I4)
2597                         BINOP(i, ^);
2598                         MINT_IN_BREAK;
2599                 MINT_IN_CASE(MINT_XOR_I8)
2600                         BINOP(l, ^);
2601                         MINT_IN_BREAK;
2602
2603 #define SHIFTOP(datamem, op) \
2604         --sp; \
2605         sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2606         ++ip;
2607
2608                 MINT_IN_CASE(MINT_SHL_I4)
2609                         SHIFTOP(i, <<);
2610                         MINT_IN_BREAK;
2611                 MINT_IN_CASE(MINT_SHL_I8)
2612                         SHIFTOP(l, <<);
2613                         MINT_IN_BREAK;
2614                 MINT_IN_CASE(MINT_SHR_I4)
2615                         SHIFTOP(i, >>);
2616                         MINT_IN_BREAK;
2617                 MINT_IN_CASE(MINT_SHR_I8)
2618                         SHIFTOP(l, >>);
2619                         MINT_IN_BREAK;
2620                 MINT_IN_CASE(MINT_SHR_UN_I4)
2621                         --sp;
2622                         sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2623                         ++ip;
2624                         MINT_IN_BREAK;
2625                 MINT_IN_CASE(MINT_SHR_UN_I8)
2626                         --sp;
2627                         sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2628                         ++ip;
2629                         MINT_IN_BREAK;
2630                 MINT_IN_CASE(MINT_NEG_I4)
2631                         sp [-1].data.i = - sp [-1].data.i;
2632                         ++ip;
2633                         MINT_IN_BREAK;
2634                 MINT_IN_CASE(MINT_NEG_I8)
2635                         sp [-1].data.l = - sp [-1].data.l;
2636                         ++ip;
2637                         MINT_IN_BREAK;
2638                 MINT_IN_CASE(MINT_NEG_R8)
2639                         sp [-1].data.f = - sp [-1].data.f;
2640                         ++ip;
2641                         MINT_IN_BREAK;
2642                 MINT_IN_CASE(MINT_NOT_I4)
2643                         sp [-1].data.i = ~ sp [-1].data.i;
2644                         ++ip;
2645                         MINT_IN_BREAK;
2646                 MINT_IN_CASE(MINT_NOT_I8)
2647                         sp [-1].data.l = ~ sp [-1].data.l;
2648                         ++ip;
2649                         MINT_IN_BREAK;
2650                 MINT_IN_CASE(MINT_CONV_I1_I4)
2651                         sp [-1].data.i = (gint8)sp [-1].data.i;
2652                         ++ip;
2653                         MINT_IN_BREAK;
2654                 MINT_IN_CASE(MINT_CONV_I1_I8)
2655                         sp [-1].data.i = (gint8)sp [-1].data.l;
2656                         ++ip;
2657                         MINT_IN_BREAK;
2658                 MINT_IN_CASE(MINT_CONV_I1_R8)
2659                         sp [-1].data.i = (gint8)sp [-1].data.f;
2660                         ++ip;
2661                         MINT_IN_BREAK;
2662                 MINT_IN_CASE(MINT_CONV_U1_I4)
2663                         sp [-1].data.i = (guint8)sp [-1].data.i;
2664                         ++ip;
2665                         MINT_IN_BREAK;
2666                 MINT_IN_CASE(MINT_CONV_U1_I8)
2667                         sp [-1].data.i = (guint8)sp [-1].data.l;
2668                         ++ip;
2669                         MINT_IN_BREAK;
2670                 MINT_IN_CASE(MINT_CONV_U1_R8)
2671                         sp [-1].data.i = (guint8)sp [-1].data.f;
2672                         ++ip;
2673                         MINT_IN_BREAK;
2674                 MINT_IN_CASE(MINT_CONV_I2_I4)
2675                         sp [-1].data.i = (gint16)sp [-1].data.i;
2676                         ++ip;
2677                         MINT_IN_BREAK;
2678                 MINT_IN_CASE(MINT_CONV_I2_I8)
2679                         sp [-1].data.i = (gint16)sp [-1].data.l;
2680                         ++ip;
2681                         MINT_IN_BREAK;
2682                 MINT_IN_CASE(MINT_CONV_I2_R8)
2683                         sp [-1].data.i = (gint16)sp [-1].data.f;
2684                         ++ip;
2685                         MINT_IN_BREAK;
2686                 MINT_IN_CASE(MINT_CONV_U2_I4)
2687                         sp [-1].data.i = (guint16)sp [-1].data.i;
2688                         ++ip;
2689                         MINT_IN_BREAK;
2690                 MINT_IN_CASE(MINT_CONV_U2_I8)
2691                         sp [-1].data.i = (guint16)sp [-1].data.l;
2692                         ++ip;
2693                         MINT_IN_BREAK;
2694                 MINT_IN_CASE(MINT_CONV_U2_R8)
2695                         sp [-1].data.i = (guint16)sp [-1].data.f;
2696                         ++ip;
2697                         MINT_IN_BREAK;
2698                 MINT_IN_CASE(MINT_CONV_I4_R8)
2699                         sp [-1].data.i = (gint32)sp [-1].data.f;
2700                         ++ip;
2701                         MINT_IN_BREAK;
2702                 MINT_IN_CASE(MINT_CONV_U4_I8)
2703                 MINT_IN_CASE(MINT_CONV_I4_I8)
2704                         sp [-1].data.i = (gint32)sp [-1].data.l;
2705                         ++ip;
2706                         MINT_IN_BREAK;
2707                 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2708                         sp [-2].data.i = (gint32)sp [-2].data.l;
2709                         ++ip;
2710                         MINT_IN_BREAK;
2711                 MINT_IN_CASE(MINT_CONV_U4_R8)
2712                         sp [-1].data.i = (guint32)sp [-1].data.f;
2713                         ++ip;
2714                         MINT_IN_BREAK;
2715                 MINT_IN_CASE(MINT_CONV_I8_I4)
2716                         sp [-1].data.l = sp [-1].data.i;
2717                         ++ip;
2718                         MINT_IN_BREAK;
2719                 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2720                         sp [-2].data.l = sp [-2].data.i;
2721                         ++ip;
2722                         MINT_IN_BREAK;
2723                 MINT_IN_CASE(MINT_CONV_I8_U4)
2724                         sp [-1].data.l = (guint32)sp [-1].data.i;
2725                         ++ip;
2726                         MINT_IN_BREAK;
2727                 MINT_IN_CASE(MINT_CONV_I8_R8)
2728                         sp [-1].data.l = (gint64)sp [-1].data.f;
2729                         ++ip;
2730                         MINT_IN_BREAK;
2731                 MINT_IN_CASE(MINT_CONV_R4_I4)
2732                         sp [-1].data.f = (float)sp [-1].data.i;
2733                         ++ip;
2734                         MINT_IN_BREAK;
2735                 MINT_IN_CASE(MINT_CONV_R4_I8)
2736                         sp [-1].data.f = (float)sp [-1].data.l;
2737                         ++ip;
2738                         MINT_IN_BREAK;
2739                 MINT_IN_CASE(MINT_CONV_R4_R8)
2740                         sp [-1].data.f = (float)sp [-1].data.f;
2741                         ++ip;
2742                         MINT_IN_BREAK;
2743                 MINT_IN_CASE(MINT_CONV_R8_I4)
2744                         sp [-1].data.f = (double)sp [-1].data.i;
2745                         ++ip;
2746                         MINT_IN_BREAK;
2747                 MINT_IN_CASE(MINT_CONV_R8_I8)
2748                         sp [-1].data.f = (double)sp [-1].data.l;
2749                         ++ip;
2750                         MINT_IN_BREAK;
2751                 MINT_IN_CASE(MINT_CONV_U8_I4)
2752                         sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2753                         ++ip;
2754                         MINT_IN_BREAK;
2755                 MINT_IN_CASE(MINT_CONV_U8_R8)
2756                         sp [-1].data.l = (guint64)sp [-1].data.f;
2757                         ++ip;
2758                         MINT_IN_BREAK;
2759                 MINT_IN_CASE(MINT_CPOBJ) {
2760                         c = rtm->data_items[* (guint16 *)(ip + 1)];
2761                         g_assert (c->byval_arg.type == MONO_TYPE_VALUETYPE);
2762                         stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
2763                         ip += 2;
2764                         sp -= 2;
2765                         MINT_IN_BREAK;
2766                 }
2767                 MINT_IN_CASE(MINT_LDOBJ) {
2768                         void *p;
2769                         c = rtm->data_items[* (guint16 *)(ip + 1)];
2770                         ip += 2;
2771                         p = sp [-1].data.p;
2772                         if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2773                                 int size = mono_class_value_size (c, NULL);
2774                                 sp [-1].data.p = vt_sp;
2775                                 vt_sp += (size + 7) & ~7;
2776                         }
2777                         stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2778                         MINT_IN_BREAK;
2779                 }
2780                 MINT_IN_CASE(MINT_LDSTR)
2781                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2782                         ++sp;
2783                         ip += 2;
2784                         MINT_IN_BREAK;
2785                 MINT_IN_CASE(MINT_NEWOBJ) {
2786                         MonoClass *newobj_class;
2787                         MonoMethodSignature *csig;
2788                         stackval valuetype_this;
2789                         guint32 token;
2790                         stackval retval;
2791
2792                         frame->ip = ip;
2793
2794                         token = * (guint16 *)(ip + 1);
2795                         ip += 2;
2796
2797                         child_frame.ip = NULL;
2798                         child_frame.ex = NULL;
2799
2800                         child_frame.runtime_method = rtm->data_items [token];
2801                         csig = mono_method_signature (child_frame.runtime_method->method);
2802                         newobj_class = child_frame.runtime_method->method->klass;
2803                         /*if (profiling_classes) {
2804                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2805                                 count++;
2806                                 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2807                         }*/
2808
2809                         if (newobj_class->parent == mono_defaults.array_class) {
2810                                 sp -= csig->param_count;
2811                                 child_frame.stack_args = sp;
2812                                 o = ves_array_create (&child_frame, context->domain, newobj_class, csig, sp);
2813                                 if (child_frame.ex)
2814                                         THROW_EX (child_frame.ex, ip);
2815                                 goto array_constructed;
2816                         }
2817
2818                         g_assert (csig->hasthis);
2819                         if (csig->param_count) {
2820                                 sp -= csig->param_count;
2821                                 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
2822                         }
2823                         child_frame.stack_args = sp;
2824
2825                         /*
2826                          * First arg is the object.
2827                          */
2828                         if (newobj_class->valuetype) {
2829                                 MonoType *t = &newobj_class->byval_arg;
2830                                 memset (&valuetype_this, 0, sizeof (stackval));
2831                                 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
2832                                         sp->data.p = vt_sp;
2833                                         valuetype_this.data.p = vt_sp;
2834                                 } else {
2835                                         sp->data.p = &valuetype_this;
2836                                 }
2837                         } else {
2838                                 if (newobj_class != mono_defaults.string_class) {
2839                                         context->managed_code = 0;
2840                                         o = mono_object_new_checked (context->domain, newobj_class, &error);
2841                                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2842                                         context->managed_code = 1;
2843                                         if (*mono_thread_interruption_request_flag ())
2844                                                 mono_thread_interruption_checkpoint ();
2845                                         sp->data.p = o;
2846                                 } else {
2847                                         sp->data.p = NULL;
2848                                         child_frame.retval = &retval;
2849                                 }
2850                         }
2851
2852                         g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2853
2854                         ves_exec_method_with_context (&child_frame, context);
2855
2856                         context->current_frame = frame;
2857
2858                         if (child_frame.ex) {
2859                                 /*
2860                                  * An exception occurred, need to run finally, fault and catch handlers..
2861                                  */
2862                                 frame->ex = child_frame.ex;
2863                                 goto handle_finally;
2864                         }
2865                         /*
2866                          * a constructor returns void, but we need to return the object we created
2867                          */
2868 array_constructed:
2869                         if (newobj_class->valuetype && !newobj_class->enumtype) {
2870                                 *sp = valuetype_this;
2871                         } else if (newobj_class == mono_defaults.string_class) {
2872                                 *sp = retval;
2873                         } else {
2874                                 sp->data.p = o;
2875                         }
2876                         ++sp;
2877                         MINT_IN_BREAK;
2878                 }
2879                 MINT_IN_CASE(MINT_CASTCLASS)
2880                         c = rtm->data_items [*(guint16 *)(ip + 1)];
2881                         if ((o = sp [-1].data.p)) {
2882                                 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2883                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2884                                 if (!isinst_obj)
2885                                         THROW_EX (mono_get_exception_invalid_cast (), ip);
2886                         }
2887                         ip += 2;
2888                         MINT_IN_BREAK;
2889                 MINT_IN_CASE(MINT_ISINST)
2890                         c = rtm->data_items [*(guint16 *)(ip + 1)];
2891                         if ((o = sp [-1].data.p)) {
2892                                 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2893                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2894                                 if (!isinst_obj)
2895                                         sp [-1].data.p = NULL;
2896                         }
2897                         ip += 2;
2898                         MINT_IN_BREAK;
2899                 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2900                         sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2901                         ++ip;
2902                         MINT_IN_BREAK;
2903                 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2904                         sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2905                         ++ip;
2906                         MINT_IN_BREAK;
2907                 MINT_IN_CASE(MINT_UNBOX)
2908                         c = rtm->data_items[*(guint16 *)(ip + 1)];
2909                         
2910                         o = sp [-1].data.p;
2911                         if (!o)
2912                                 THROW_EX (mono_get_exception_null_reference (), ip);
2913
2914                         MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2915                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2916                         if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
2917                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
2918
2919                         sp [-1].data.p = mono_object_unbox (o);
2920                         ip += 2;
2921                         MINT_IN_BREAK;
2922                 MINT_IN_CASE(MINT_THROW)
2923                         --sp;
2924                         frame->ex_handler = NULL;
2925                         if (!sp->data.p)
2926                                 sp->data.p = mono_get_exception_null_reference ();
2927                         THROW_EX ((MonoException *)sp->data.p, ip);
2928                         MINT_IN_BREAK;
2929                 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
2930                         o = sp [-1].data.p;
2931                         sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2932                         ip += 2;
2933                         MINT_IN_BREAK;
2934                 MINT_IN_CASE(MINT_LDFLDA)
2935                         o = sp [-1].data.p;
2936                         if (!o)
2937                                 THROW_EX (mono_get_exception_null_reference (), ip);
2938                         sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2939                         ip += 2;
2940                         MINT_IN_BREAK;
2941                 MINT_IN_CASE(MINT_CKNULL)
2942                         o = sp [-1].data.p;
2943                         if (!o)
2944                                 THROW_EX (mono_get_exception_null_reference (), ip);
2945                         ++ip;
2946                         MINT_IN_BREAK;
2947
2948 #define LDFLD(datamem, fieldtype) \
2949         o = sp [-1].data.p; \
2950         if (!o) \
2951                 THROW_EX (mono_get_exception_null_reference (), ip); \
2952         sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2953         ip += 2;
2954
2955                 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2956                 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2957                 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2958                 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2959                 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2960                 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2961                 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2962                 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2963                 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2964                 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2965
2966                 MINT_IN_CASE(MINT_LDFLD_VT)
2967                         o = sp [-1].data.p;
2968                         if (!o)
2969                                 THROW_EX (mono_get_exception_null_reference (), ip);
2970                         i32 = READ32(ip + 2);
2971                         sp [-1].data.p = vt_sp;
2972                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2973                         vt_sp += (i32 + 7) & ~7;
2974                         ip += 4;
2975                         MINT_IN_BREAK;
2976
2977                 MINT_IN_CASE(MINT_LDRMFLD) {
2978                         gpointer tmp;
2979                         MonoClassField *field;
2980                         char *addr;
2981
2982                         o = sp [-1].data.p;
2983                         if (!o)
2984                                 THROW_EX (mono_get_exception_null_reference (), ip);
2985                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2986                         ip += 2;
2987                         if (mono_object_is_transparent_proxy (o)) {
2988                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2989
2990                                 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2991                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2992                         } else {
2993                                 addr = (char*)o + field->offset;
2994                         }                               
2995
2996                         stackval_from_data (field->type, &sp [-1], addr, FALSE);
2997                         MINT_IN_BREAK;
2998                 }
2999
3000                 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3001                         MonoClassField *field;
3002                         char *addr;
3003                         gpointer tmp;
3004
3005                         o = sp [-1].data.p;
3006                         if (!o)
3007                                 THROW_EX (mono_get_exception_null_reference (), ip);
3008                         field = rtm->data_items[* (guint16 *)(ip + 1)];
3009                         i32 = READ32(ip + 2);
3010                         ip += 4;
3011                         if (mono_object_is_transparent_proxy (o)) {
3012                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3013                                 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3014                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3015                         } else {
3016                                 addr = (char*)o + field->offset;
3017                         }                               
3018
3019                         sp [-1].data.p = vt_sp;
3020                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3021                         vt_sp += (i32 + 7) & ~7;
3022                         memcpy(sp [-1].data.p, addr, i32);
3023                         MINT_IN_BREAK;
3024                 }
3025
3026 #define STFLD(datamem, fieldtype) \
3027         o = sp [-2].data.p; \
3028         if (!o) \
3029                 THROW_EX (mono_get_exception_null_reference (), ip); \
3030         sp -= 2; \
3031         * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3032         ip += 2;
3033
3034                 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3035                 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3036                 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3037                 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3038                 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3039                 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3040                 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3041                 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3042                 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
3043                 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3044
3045                 MINT_IN_CASE(MINT_STFLD_VT)
3046                         o = sp [-2].data.p;
3047                         if (!o)
3048                                 THROW_EX (mono_get_exception_null_reference (), ip);
3049                         i32 = READ32(ip + 2);
3050                         sp -= 2;
3051                         memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3052                         vt_sp -= (i32 + 7) & ~7;
3053                         ip += 4;
3054                         MINT_IN_BREAK;
3055
3056                 MINT_IN_CASE(MINT_STRMFLD) {
3057                         MonoClassField *field;
3058
3059                         o = sp [-2].data.p;
3060                         if (!o)
3061                                 THROW_EX (mono_get_exception_null_reference (), ip);
3062                         
3063                         field = rtm->data_items[* (guint16 *)(ip + 1)];
3064                         ip += 2;
3065
3066                         if (mono_object_is_transparent_proxy (o)) {
3067                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3068                                 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3069                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3070                         } else
3071                                 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3072
3073                         sp -= 2;
3074                         MINT_IN_BREAK;
3075                 }
3076                 MINT_IN_CASE(MINT_STRMFLD_VT) {
3077                         MonoClassField *field;
3078
3079                         o = sp [-2].data.p;
3080                         if (!o)
3081                                 THROW_EX (mono_get_exception_null_reference (), ip);
3082                         field = rtm->data_items[* (guint16 *)(ip + 1)];
3083                         i32 = READ32(ip + 2);
3084                         ip += 4;
3085
3086                         if (mono_object_is_transparent_proxy (o)) {
3087                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3088                                 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3089                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3090                         } else
3091                                 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3092
3093                         sp -= 2;
3094                         vt_sp -= (i32 + 7) & ~7;
3095                         MINT_IN_BREAK;
3096                 }
3097                 MINT_IN_CASE(MINT_LDSFLDA) {
3098                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3099                         sp->data.p = mono_class_static_field_address (context->domain, field);
3100                         ip += 2;
3101                         ++sp;
3102                         MINT_IN_BREAK;
3103                 }
3104                 MINT_IN_CASE(MINT_LDSFLD) {
3105                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3106                         gpointer addr = mono_class_static_field_address (context->domain, field);
3107                         stackval_from_data (field->type, sp, addr, FALSE);
3108                         ip += 2;
3109                         ++sp;
3110                         MINT_IN_BREAK;
3111                 }
3112                 MINT_IN_CASE(MINT_LDSFLD_VT) {
3113                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3114                         gpointer addr = mono_class_static_field_address (context->domain, field);
3115                         int size = READ32 (ip + 2);
3116                         ip += 4;
3117
3118                         sp->data.p = vt_sp;
3119                         vt_sp += (size + 7) & ~7;
3120                         stackval_from_data (field->type, sp, addr, FALSE);
3121                         ++sp;
3122                         MINT_IN_BREAK;
3123                 }
3124                 MINT_IN_CASE(MINT_STSFLD) {
3125                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3126                         gpointer addr = mono_class_static_field_address (context->domain, field);
3127                         ip += 2;
3128                         --sp;
3129                         stackval_to_data (field->type, sp, addr, FALSE);
3130                         MINT_IN_BREAK;
3131                 }
3132                 MINT_IN_CASE(MINT_STSFLD_VT) {
3133                         MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3134                         gpointer addr = mono_class_static_field_address (context->domain, field);
3135                         int size = READ32 (ip + 2);
3136                         ip += 4;
3137
3138                         --sp;
3139                         stackval_to_data (field->type, sp, addr, FALSE);
3140                         vt_sp -= (size + 7) & ~7;
3141                         MINT_IN_BREAK;
3142                 }
3143                 MINT_IN_CASE(MINT_STOBJ_VT) {
3144                         int size;
3145                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3146                         ip += 2;
3147                         size = mono_class_value_size (c, NULL);
3148                         memcpy(sp [-2].data.p, sp [-1].data.p, size);
3149                         vt_sp -= (size + 7) & ~7;
3150                         sp -= 2;
3151                         MINT_IN_BREAK;
3152                 }
3153                 MINT_IN_CASE(MINT_STOBJ) {
3154                         int size;
3155                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3156                         ip += 2;
3157                         size = mono_class_value_size (c, NULL);
3158                         memcpy(sp [-2].data.p, &sp [-1].data, size);
3159                         sp -= 2;
3160                         MINT_IN_BREAK;
3161                 }
3162                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3163                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3164                                 THROW_EX (mono_get_exception_overflow (), ip);
3165                         sp [-1].data.i = (guint32)sp [-1].data.f;
3166                         ++ip;
3167                         MINT_IN_BREAK;
3168                 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3169                         if (sp [-1].data.i < 0)
3170                                 THROW_EX (mono_get_exception_overflow (), ip);
3171                         sp [-1].data.l = sp [-1].data.i;
3172                         ++ip;
3173                         MINT_IN_BREAK;
3174                 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
3175                         if (sp [-1].data.l < 0)
3176                                 THROW_EX (mono_get_exception_overflow (), ip);
3177                         ++ip;
3178                         MINT_IN_BREAK;
3179                 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
3180                         if ((guint64) sp [-1].data.l > MYGINT64_MAX)
3181                                 THROW_EX (mono_get_exception_overflow (), ip);
3182                         ++ip;
3183                         MINT_IN_BREAK;
3184                 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3185                 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3186                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGINT64_MAX)
3187                                 THROW_EX (mono_get_exception_overflow (), ip);
3188                         sp [-1].data.l = (guint64)sp [-1].data.f;
3189                         ++ip;
3190                         MINT_IN_BREAK;
3191                 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3192                         if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3193                                 THROW_EX (mono_get_exception_overflow (), ip);
3194                         sp [-1].data.l = (gint64)sp [-1].data.f;
3195                         ++ip;
3196                         MINT_IN_BREAK;
3197                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3198                         if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3199                                 THROW_EX (mono_get_exception_overflow (), ip);
3200                         sp [-1].data.i = (mono_u)sp [-1].data.l;
3201                         ++ip;
3202                         MINT_IN_BREAK;
3203                 MINT_IN_CASE(MINT_BOX) {
3204                         c = rtm->data_items [* (guint16 *)(ip + 1)];
3205                         guint16 offset = * (guint16 *)(ip + 2);
3206
3207                         if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3208                                 int size = mono_class_value_size (c, NULL);
3209                                 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, sp [-1 - offset].data.p, &error);
3210                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3211                                 size = (size + 7) & ~7;
3212                                 vt_sp -= size;
3213                         } else {
3214                                 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
3215                                 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, &sp [-1 - offset], &error);
3216                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3217                         }
3218                         ip += 3;
3219                         MINT_IN_BREAK;
3220                 }
3221                 MINT_IN_CASE(MINT_NEWARR)
3222                         sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3223                         if (!mono_error_ok (&error)) {
3224                                 THROW_EX (mono_error_convert_to_exception (&error), ip);
3225                         }
3226                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3227                         ip += 2;
3228                         /*if (profiling_classes) {
3229                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3230                                 count++;
3231                                 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3232                         }*/
3233
3234                         MINT_IN_BREAK;
3235                 MINT_IN_CASE(MINT_LDLEN)
3236                         o = sp [-1].data.p;
3237                         if (!o)
3238                                 THROW_EX (mono_get_exception_null_reference (), ip);
3239                         sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3240                         ++ip;
3241                         MINT_IN_BREAK;
3242                 MINT_IN_CASE(MINT_GETCHR) {
3243                         MonoString *s;
3244                         s = sp [-2].data.p;
3245                         if (!s)
3246                                 THROW_EX (mono_get_exception_null_reference (), ip);
3247                         i32 = sp [-1].data.i;
3248                         if (i32 < 0 || i32 >= mono_string_length (s))
3249                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3250                         --sp;
3251                         sp [-1].data.i = mono_string_chars(s)[i32];
3252                         ++ip;
3253                         MINT_IN_BREAK;
3254                 }
3255                 MINT_IN_CASE(MINT_STRLEN)
3256                         ++ip;
3257                         sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3258                         MINT_IN_BREAK;
3259                 MINT_IN_CASE(MINT_ARRAY_RANK)
3260                         o = sp [-1].data.p;
3261                         if (!o)
3262                                 THROW_EX (mono_get_exception_null_reference (), ip);
3263                         sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3264                         ip++;
3265                         MINT_IN_BREAK;
3266                 MINT_IN_CASE(MINT_LDELEMA)
3267                 MINT_IN_CASE(MINT_LDELEMA_TC) {
3268                         gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
3269                         
3270                         MonoClass *klass = rtm->data_items [*(guint16 *) (ip + 1)];
3271                         guint16 numargs = *(guint16 *) (ip + 2);
3272                         ip += 3;
3273                         sp -= numargs;
3274
3275                         o = sp [0].data.p;
3276                         sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
3277                         if (frame->ex)
3278                                 THROW_EX (frame->ex, ip);
3279                         ++sp;
3280
3281                         MINT_IN_BREAK;
3282                 }
3283                 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3284                 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3285                 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3286                 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3287                 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3288                 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3289                 MINT_IN_CASE(MINT_LDELEM_I8)  /* fall through */
3290                 MINT_IN_CASE(MINT_LDELEM_I)  /* fall through */
3291                 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3292                 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3293                 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
3294                 MINT_IN_CASE(MINT_LDELEM_VT) {
3295                         MonoArray *o;
3296                         mono_u aindex;
3297
3298                         sp -= 2;
3299
3300                         o = sp [0].data.p;
3301                         if (!o)
3302                                 THROW_EX (mono_get_exception_null_reference (), ip);
3303
3304                         aindex = sp [1].data.i;
3305                         if (aindex >= mono_array_length (o))
3306                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3307
3308                         /*
3309                          * FIXME: throw mono_get_exception_array_type_mismatch () if needed 
3310                          */
3311                         switch (*ip) {
3312                         case MINT_LDELEM_I1:
3313                                 sp [0].data.i = mono_array_get (o, gint8, aindex);
3314                                 break;
3315                         case MINT_LDELEM_U1:
3316                                 sp [0].data.i = mono_array_get (o, guint8, aindex);
3317                                 break;
3318                         case MINT_LDELEM_I2:
3319                                 sp [0].data.i = mono_array_get (o, gint16, aindex);
3320                                 break;
3321                         case MINT_LDELEM_U2:
3322                                 sp [0].data.i = mono_array_get (o, guint16, aindex);
3323                                 break;
3324                         case MINT_LDELEM_I:
3325                                 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3326                                 break;
3327                         case MINT_LDELEM_I4:
3328                                 sp [0].data.i = mono_array_get (o, gint32, aindex);
3329                                 break;
3330                         case MINT_LDELEM_U4:
3331                                 sp [0].data.i = mono_array_get (o, guint32, aindex);
3332                                 break;
3333                         case MINT_LDELEM_I8:
3334                                 sp [0].data.l = mono_array_get (o, guint64, aindex);
3335                                 break;
3336                         case MINT_LDELEM_R4:
3337                                 sp [0].data.f = mono_array_get (o, float, aindex);
3338                                 break;
3339                         case MINT_LDELEM_R8:
3340                                 sp [0].data.f = mono_array_get (o, double, aindex);
3341                                 break;
3342                         case MINT_LDELEM_REF:
3343                                 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3344                                 break;
3345                         case MINT_LDELEM_VT: {
3346                                 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
3347                                 i32 = READ32 (ip + 2);
3348                                 char *src_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
3349                                 sp [0].data.vt = vt_sp;
3350                                 stackval_from_data (&klass_vt->byval_arg, sp, src_addr, FALSE);
3351                                 vt_sp += (i32 + 7) & ~7;
3352                                 ip += 3;
3353                                 break;
3354                         }
3355                         default:
3356                                 ves_abort();
3357                         }
3358
3359                         ++ip;
3360                         ++sp;
3361                         MINT_IN_BREAK;
3362                 }
3363                 MINT_IN_CASE(MINT_STELEM_I)  /* fall through */
3364                 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */ 
3365                 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
3366                 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3367                 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3368                 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3369                 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3370                 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3371                 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
3372                 MINT_IN_CASE(MINT_STELEM_VT) {
3373                         mono_u aindex;
3374
3375                         sp -= 3;
3376
3377                         o = sp [0].data.p;
3378                         if (!o)
3379                                 THROW_EX (mono_get_exception_null_reference (), ip);
3380
3381                         aindex = sp [1].data.i;
3382                         if (aindex >= mono_array_length ((MonoArray *)o))
3383                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3384
3385                         switch (*ip) {
3386                         case MINT_STELEM_I:
3387                                 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3388                                 break;
3389                         case MINT_STELEM_I1:
3390                                 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3391                                 break;
3392                         case MINT_STELEM_U1:
3393                                 mono_array_set ((MonoArray *) o, guint8, aindex, sp [2].data.i);
3394                                 break;
3395                         case MINT_STELEM_I2:
3396                                 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3397                                 break;
3398                         case MINT_STELEM_I4:
3399                                 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3400                                 break;
3401                         case MINT_STELEM_I8:
3402                                 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3403                                 break;
3404                         case MINT_STELEM_R4:
3405                                 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3406                                 break;
3407                         case MINT_STELEM_R8:
3408                                 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3409                                 break;
3410                         case MINT_STELEM_REF: {
3411                                 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3412                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3413                                 if (sp [2].data.p && !isinst_obj)
3414                                         THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3415                                 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3416                                 break;
3417                         }
3418                         case MINT_STELEM_VT: {
3419                                 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
3420                                 i32 = READ32 (ip + 2);
3421                                 char *dst_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
3422
3423                                 stackval_to_data (&klass_vt->byval_arg, &sp [2], dst_addr, FALSE);
3424                                 vt_sp -= (i32 + 7) & ~7;
3425                                 ip += 3;
3426                                 break;
3427                         }
3428                         default:
3429                                 ves_abort();
3430                         }
3431
3432                         ++ip;
3433                         MINT_IN_BREAK;
3434                 }
3435                 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3436                         if (sp [-1].data.i < 0)
3437                                 THROW_EX (mono_get_exception_overflow (), ip);
3438                         ++ip;
3439                         MINT_IN_BREAK;
3440                 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3441                         if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3442                                 THROW_EX (mono_get_exception_overflow (), ip);
3443                         sp [-1].data.i = (gint32) sp [-1].data.l;
3444                         ++ip;
3445                         MINT_IN_BREAK;
3446                 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
3447                         if (sp [-1].data.l < 0 || sp [-1].data.l > MYGINT32_MAX)
3448                                 THROW_EX (mono_get_exception_overflow (), ip);
3449                         sp [-1].data.i = (gint32) sp [-1].data.l;
3450                         ++ip;
3451                         MINT_IN_BREAK;
3452                 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3453                         if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3454                                 THROW_EX (mono_get_exception_overflow (), ip);
3455                         sp [-1].data.i = (gint32) sp [-1].data.f;
3456                         ++ip;
3457                         MINT_IN_BREAK;
3458                 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3459                         if (sp [-1].data.i < 0)
3460                                 THROW_EX (mono_get_exception_overflow (), ip);
3461                         ++ip;
3462                         MINT_IN_BREAK;
3463                 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3464                         if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3465                                 THROW_EX (mono_get_exception_overflow (), ip);
3466                         sp [-1].data.i = (guint32) sp [-1].data.l;
3467                         ++ip;
3468                         MINT_IN_BREAK;
3469                 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3470                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3471                                 THROW_EX (mono_get_exception_overflow (), ip);
3472                         sp [-1].data.i = (guint32) sp [-1].data.f;
3473                         ++ip;
3474                         MINT_IN_BREAK;
3475                 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3476                         if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3477                                 THROW_EX (mono_get_exception_overflow (), ip);
3478                         ++ip;
3479                         MINT_IN_BREAK;
3480                 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3481                         if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3482                                 THROW_EX (mono_get_exception_overflow (), ip);
3483                         sp [-1].data.i = (gint16) sp [-1].data.l;
3484                         ++ip;
3485                         MINT_IN_BREAK;
3486                 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3487                         if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3488                                 THROW_EX (mono_get_exception_overflow (), ip);
3489                         sp [-1].data.i = (gint16) sp [-1].data.f;
3490                         ++ip;
3491                         MINT_IN_BREAK;
3492                 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3493                         if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3494                                 THROW_EX (mono_get_exception_overflow (), ip);
3495                         ++ip;
3496                         MINT_IN_BREAK;
3497                 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3498                         if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3499                                 THROW_EX (mono_get_exception_overflow (), ip);
3500                         sp [-1].data.i = (guint16) sp [-1].data.l;
3501                         ++ip;
3502                         MINT_IN_BREAK;
3503                 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3504                         if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3505                                 THROW_EX (mono_get_exception_overflow (), ip);
3506                         sp [-1].data.i = (guint16) sp [-1].data.f;
3507                         ++ip;
3508                         MINT_IN_BREAK;
3509                 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3510                         if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3511                                 THROW_EX (mono_get_exception_overflow (), ip);
3512                         ++ip;
3513                         MINT_IN_BREAK;
3514                 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3515                         if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3516                                 THROW_EX (mono_get_exception_overflow (), ip);
3517                         sp [-1].data.i = (gint8) sp [-1].data.l;
3518                         ++ip;
3519                         MINT_IN_BREAK;
3520                 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3521                         if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3522                                 THROW_EX (mono_get_exception_overflow (), ip);
3523                         sp [-1].data.i = (gint8) sp [-1].data.f;
3524                         ++ip;
3525                         MINT_IN_BREAK;
3526                 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3527                         if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3528                                 THROW_EX (mono_get_exception_overflow (), ip);
3529                         ++ip;
3530                         MINT_IN_BREAK;
3531                 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3532                         if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3533                                 THROW_EX (mono_get_exception_overflow (), ip);
3534                         sp [-1].data.i = (guint8) sp [-1].data.l;
3535                         ++ip;
3536                         MINT_IN_BREAK;
3537                 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3538                         if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3539                                 THROW_EX (mono_get_exception_overflow (), ip);
3540                         sp [-1].data.i = (guint8) sp [-1].data.f;
3541                         ++ip;
3542                         MINT_IN_BREAK;
3543 #if 0
3544                 MINT_IN_CASE(MINT_LDELEM) 
3545                 MINT_IN_CASE(MINT_STELEM) 
3546                 MINT_IN_CASE(MINT_UNBOX_ANY) 
3547
3548                 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3549 #endif
3550                 MINT_IN_CASE(MINT_CKFINITE)
3551                         if (!isfinite(sp [-1].data.f))
3552                                 THROW_EX (mono_get_exception_arithmetic (), ip);
3553                         ++ip;
3554                         MINT_IN_BREAK;
3555 #if 0
3556                 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3557 #endif
3558                 MINT_IN_CASE(MINT_LDTOKEN)
3559                         sp->data.p = vt_sp;
3560                         vt_sp += 8;
3561                         * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3562                         ip += 2;
3563                         ++sp;
3564                         MINT_IN_BREAK;
3565                 MINT_IN_CASE(MINT_ADD_OVF_I4)
3566                         if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3567                                 THROW_EX (mono_get_exception_overflow (), ip);
3568                         BINOP(i, +);
3569                         MINT_IN_BREAK;
3570                 MINT_IN_CASE(MINT_ADD_OVF_I8)
3571                         if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3572                                 THROW_EX (mono_get_exception_overflow (), ip);
3573                         BINOP(l, +);
3574                         MINT_IN_BREAK;
3575                 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3576                         if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3577                                 THROW_EX (mono_get_exception_overflow (), ip);
3578                         BINOP_CAST(i, +, guint32);
3579                         MINT_IN_BREAK;
3580                 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3581                         if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3582                                 THROW_EX (mono_get_exception_overflow (), ip);
3583                         BINOP_CAST(l, +, guint64);
3584                         MINT_IN_BREAK;
3585                 MINT_IN_CASE(MINT_MUL_OVF_I4)
3586                         if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3587                                 THROW_EX (mono_get_exception_overflow (), ip);
3588                         BINOP(i, *);
3589                         MINT_IN_BREAK;
3590                 MINT_IN_CASE(MINT_MUL_OVF_I8)
3591                         if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3592                                 THROW_EX (mono_get_exception_overflow (), ip);
3593                         BINOP(l, *);
3594                         MINT_IN_BREAK;
3595                 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3596                         if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3597                                 THROW_EX (mono_get_exception_overflow (), ip);
3598                         BINOP_CAST(i, *, guint32);
3599                         MINT_IN_BREAK;
3600                 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3601                         if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3602                                 THROW_EX (mono_get_exception_overflow (), ip);
3603                         BINOP_CAST(l, *, guint64);
3604                         MINT_IN_BREAK;
3605                 MINT_IN_CASE(MINT_SUB_OVF_I4)
3606                         if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3607                                 THROW_EX (mono_get_exception_overflow (), ip);
3608                         BINOP(i, -);
3609                         MINT_IN_BREAK;
3610                 MINT_IN_CASE(MINT_SUB_OVF_I8)
3611                         if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3612                                 THROW_EX (mono_get_exception_overflow (), ip);
3613                         BINOP(l, -);
3614                         MINT_IN_BREAK;
3615                 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3616                         if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3617                                 THROW_EX (mono_get_exception_overflow (), ip);
3618                         BINOP_CAST(i, -, guint32);
3619                         MINT_IN_BREAK;
3620                 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3621                         if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3622                                 THROW_EX (mono_get_exception_overflow (), ip);
3623                         BINOP_CAST(l, -, guint64);
3624                         MINT_IN_BREAK;
3625                 MINT_IN_CASE(MINT_ENDFINALLY)
3626                         if (finally_ips) {
3627                                 ip = finally_ips->data;
3628                                 finally_ips = g_slist_remove (finally_ips, ip);
3629                                 goto main_loop;
3630                         }
3631                         if (frame->ex)
3632                                 goto handle_fault;
3633                         ves_abort();
3634                         MINT_IN_BREAK;
3635                 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3636                 MINT_IN_CASE(MINT_LEAVE_S)
3637                         while (sp > frame->stack) {
3638                                 --sp;
3639                         }
3640                         frame->ip = ip;
3641                         if (*ip == MINT_LEAVE_S) {
3642                                 ip += (short) *(ip + 1);
3643                         } else {
3644                                 ip += (gint32) READ32 (ip + 1);
3645                         }
3646                         endfinally_ip = ip;
3647                         if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3648                                 frame->ex_handler = NULL;
3649                                 frame->ex = NULL;
3650                         }
3651                         goto handle_finally;
3652                         MINT_IN_BREAK;
3653                 MINT_IN_CASE(MINT_ICALL_V_V) 
3654                 MINT_IN_CASE(MINT_ICALL_V_P)
3655                 MINT_IN_CASE(MINT_ICALL_P_V) 
3656                 MINT_IN_CASE(MINT_ICALL_P_P)
3657                 MINT_IN_CASE(MINT_ICALL_PP_V)
3658                 MINT_IN_CASE(MINT_ICALL_PI_V)
3659                 MINT_IN_CASE(MINT_ICALL_PP_P)
3660                 MINT_IN_CASE(MINT_ICALL_PI_P)
3661                 MINT_IN_CASE(MINT_ICALL_PPP_V)
3662                 MINT_IN_CASE(MINT_ICALL_PPI_V)
3663                         sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3664                         if (frame->ex != NULL)
3665                                 goto handle_exception;
3666                         ip += 2;
3667                         MINT_IN_BREAK;
3668                 MINT_IN_CASE(MINT_MONO_LDPTR) 
3669                         sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3670                         ip += 2;
3671                         ++sp;
3672                         MINT_IN_BREAK;
3673                 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3674                         sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3675                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3676                         ip += 2;
3677                         sp++;
3678                         MINT_IN_BREAK;
3679                 MINT_IN_CASE(MINT_MONO_FREE)
3680                         ++ip;
3681                         --sp;
3682                         g_error ("that doesn't seem right");
3683                         g_free (sp->data.p);
3684                         MINT_IN_BREAK;
3685                 MINT_IN_CASE(MINT_MONO_RETOBJ)
3686                         ++ip;
3687                         sp--;
3688                         stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3689                              mono_method_signature (frame->runtime_method->method)->pinvoke);
3690                         if (sp > frame->stack)
3691                                 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3692                         goto exit_frame;
3693
3694 #define RELOP(datamem, op) \
3695         --sp; \
3696         sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3697         ++ip;
3698                 MINT_IN_CASE(MINT_CEQ_I4)
3699                         RELOP(i, ==);
3700                         MINT_IN_BREAK;
3701                 MINT_IN_CASE(MINT_CEQ0_I4)
3702                         sp [-1].data.i = (sp [-1].data.i == 0);
3703                         ++ip;
3704                         MINT_IN_BREAK;
3705                 MINT_IN_CASE(MINT_CEQ_I8)
3706                         RELOP(l, ==);
3707                         MINT_IN_BREAK;
3708                 MINT_IN_CASE(MINT_CEQ_R8)
3709                         --sp; 
3710                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3711                                 sp [-1].data.i = 0;
3712                         else
3713                                 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3714                         ++ip;
3715                         MINT_IN_BREAK;
3716                 MINT_IN_CASE(MINT_CGT_I4)
3717                         RELOP(i, >);
3718                         MINT_IN_BREAK;
3719                 MINT_IN_CASE(MINT_CGT_I8)
3720                         RELOP(l, >);
3721                         MINT_IN_BREAK;
3722                 MINT_IN_CASE(MINT_CGT_R8)
3723                         --sp; 
3724                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3725                                 sp [-1].data.i = 0;
3726                         else
3727                                 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3728                         ++ip;
3729                         MINT_IN_BREAK;
3730
3731 #define RELOP_CAST(datamem, op, type) \
3732         --sp; \
3733         sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3734         ++ip;
3735
3736                 MINT_IN_CASE(MINT_CGT_UN_I4)
3737                         RELOP_CAST(i, >, guint32);
3738                         MINT_IN_BREAK;
3739                 MINT_IN_CASE(MINT_CGT_UN_I8)
3740                         RELOP_CAST(l, >, guint64);
3741                         MINT_IN_BREAK;
3742                 MINT_IN_CASE(MINT_CGT_UN_R8)
3743                         --sp; 
3744                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3745                                 sp [-1].data.i = 1;
3746                         else
3747                                 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3748                         ++ip;
3749                         MINT_IN_BREAK;
3750                 MINT_IN_CASE(MINT_CLT_I4)
3751                         RELOP(i, <);
3752                         MINT_IN_BREAK;
3753                 MINT_IN_CASE(MINT_CLT_I8)
3754                         RELOP(l, <);
3755                         MINT_IN_BREAK;
3756                 MINT_IN_CASE(MINT_CLT_R8)
3757                         --sp; 
3758                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3759                                 sp [-1].data.i = 0;
3760                         else
3761                                 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3762                         ++ip;
3763                         MINT_IN_BREAK;
3764                 MINT_IN_CASE(MINT_CLT_UN_I4)
3765                         RELOP_CAST(i, <, guint32);
3766                         MINT_IN_BREAK;
3767                 MINT_IN_CASE(MINT_CLT_UN_I8)
3768                         RELOP_CAST(l, <, guint64);
3769                         MINT_IN_BREAK;
3770                 MINT_IN_CASE(MINT_CLT_UN_R8)
3771                         --sp; 
3772                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3773                                 sp [-1].data.i = 1;
3774                         else
3775                                 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3776                         ++ip;
3777                         MINT_IN_BREAK;
3778                 MINT_IN_CASE(MINT_LDFTN) {
3779                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3780                         ++sp;
3781                         ip += 2;
3782                         MINT_IN_BREAK;
3783                 }
3784                 MINT_IN_CASE(MINT_LDVIRTFTN) {
3785                         RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3786                         ip += 2;
3787                         --sp;
3788                         if (!sp->data.p)
3789                                 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3790                                 
3791                         sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3792                         ++sp;
3793                         MINT_IN_BREAK;
3794                 }
3795
3796 #define LDARG(datamem, argtype) \
3797         sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3798         ip += 2; \
3799         ++sp; 
3800         
3801                 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3802                 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3803                 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3804                 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3805                 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3806                 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3807                 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3808                 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3809                 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3810                 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3811
3812                 MINT_IN_CASE(MINT_LDARG_VT)
3813                         sp->data.p = vt_sp;
3814                         i32 = READ32(ip + 2);
3815                         memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3816                         vt_sp += (i32 + 7) & ~7;
3817                         ip += 4;
3818                         ++sp;
3819                         MINT_IN_BREAK;
3820
3821 #define STARG(datamem, argtype) \
3822         --sp; \
3823         * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3824         ip += 2; \
3825         
3826                 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3827                 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3828                 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3829                 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3830                 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3831                 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3832                 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3833                 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3834                 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3835                 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3836
3837                 MINT_IN_CASE(MINT_STARG_VT) 
3838                         i32 = READ32(ip + 2);
3839                         --sp;
3840                         memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3841                         vt_sp -= (i32 + 7) & ~7;
3842                         ip += 4;
3843                         MINT_IN_BREAK;
3844
3845 #define STINARG(datamem, argtype) \
3846         do { \
3847                 int n = * (guint16 *)(ip + 1); \
3848                 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3849                 ip += 2; \
3850         } while (0)
3851         
3852                 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3853                 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3854                 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3855                 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3856                 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3857                 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3858                 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3859                 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3860                 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3861                 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3862
3863                 MINT_IN_CASE(MINT_STINARG_VT) {
3864                         int n = * (guint16 *)(ip + 1);
3865                         i32 = READ32(ip + 2);
3866                         memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3867                         ip += 4;
3868                         MINT_IN_BREAK;
3869                 }
3870
3871                 MINT_IN_CASE(MINT_LDARGA)
3872                         sp->data.p = frame->args + * (guint16 *)(ip + 1);
3873                         ip += 2;
3874                         ++sp;
3875                         MINT_IN_BREAK;
3876
3877 #define LDLOC(datamem, argtype) \
3878         sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3879         ip += 2; \
3880         ++sp; 
3881         
3882                 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3883                 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3884                 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3885                 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3886                 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3887                 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3888                 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3889                 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3890                 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3891                 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3892
3893                 MINT_IN_CASE(MINT_LDLOC_VT)
3894                         sp->data.p = vt_sp;
3895                         i32 = READ32(ip + 2);
3896                         memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3897                         vt_sp += (i32 + 7) & ~7;
3898                         ip += 4;
3899                         ++sp;
3900                         MINT_IN_BREAK;
3901
3902                 MINT_IN_CASE(MINT_LDLOCA_S)
3903                         sp->data.p = locals + * (guint16 *)(ip + 1);
3904                         ip += 2;
3905                         ++sp;
3906                         MINT_IN_BREAK;
3907
3908 #define STLOC(datamem, argtype) \
3909         --sp; \
3910         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3911         ip += 2;
3912         
3913                 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3914                 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3915                 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3916                 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3917                 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3918                 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3919                 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3920                 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3921                 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3922                 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3923
3924 #define STLOC_NP(datamem, argtype) \
3925         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3926         ip += 2;
3927
3928                 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3929                 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3930
3931                 MINT_IN_CASE(MINT_STLOC_VT)
3932                         i32 = READ32(ip + 2);
3933                         --sp;
3934                         memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3935                         vt_sp -= (i32 + 7) & ~7;
3936                         ip += 4;
3937                         MINT_IN_BREAK;
3938
3939                 MINT_IN_CASE(MINT_LOCALLOC) {
3940                         if (sp != frame->stack + 1) /*FIX?*/
3941                                 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3942
3943                         int len = sp [-1].data.i;
3944                         sp [-1].data.p = alloca (len);
3945                         MonoMethodHeader *header = mono_method_get_header_checked (frame->runtime_method->method, &error);
3946                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3947                         if (header->init_locals)
3948                                 memset (sp [-1].data.p, 0, len);
3949                         ++ip;
3950                         MINT_IN_BREAK;
3951                 }
3952 #if 0
3953                 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3954 #endif
3955                 MINT_IN_CASE(MINT_INITOBJ)
3956                         --sp;
3957                         memset (sp->data.vt, 0, READ32(ip + 1));
3958                         ip += 3;
3959                         MINT_IN_BREAK;
3960                 MINT_IN_CASE(MINT_CPBLK)
3961                         sp -= 3;
3962                         if (!sp [0].data.p || !sp [1].data.p)
3963                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3964                         ++ip;
3965                         /* FIXME: value and size may be int64... */
3966                         memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3967                         MINT_IN_BREAK;
3968 #if 0
3969                 MINT_IN_CASE(MINT_CONSTRAINED_) {
3970                         guint32 token;
3971                         /* FIXME: implement */
3972                         ++ip;
3973                         token = READ32 (ip);
3974                         ip += 2;
3975                         MINT_IN_BREAK;
3976                 }
3977 #endif
3978                 MINT_IN_CASE(MINT_INITBLK)
3979                         sp -= 3;
3980                         if (!sp [0].data.p)
3981                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3982                         ++ip;
3983                         /* FIXME: value and size may be int64... */
3984                         memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3985                         MINT_IN_BREAK;
3986 #if 0
3987                 MINT_IN_CASE(MINT_NO_)
3988                         /* FIXME: implement */
3989                         ip += 2;
3990                         MINT_IN_BREAK;
3991 #endif
3992                 MINT_IN_CASE(MINT_RETHROW)
3993                         /* 
3994                          * need to clarify what this should actually do:
3995                          * start the search from the last found handler in
3996                          * this method or continue in the caller or what.
3997                          * Also, do we need to run finally/fault handlers after a retrow?
3998                          * Well, this implementation will follow the usual search
3999                          * for an handler, considering the current ip as throw spot.
4000                          * We need to NULL frame->ex_handler for the later code to
4001                          * actually run the new found handler.
4002                          */
4003                         frame->ex_handler = NULL;
4004                         THROW_EX (frame->ex, ip - 1);
4005                         MINT_IN_BREAK;
4006                 MINT_IN_DEFAULT
4007                         g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4008                         THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4009                 }
4010         }
4011
4012         g_assert_not_reached ();
4013         /*
4014          * Exception handling code.
4015          * The exception object is stored in frame->ex.
4016          */
4017
4018         handle_exception:
4019         {
4020                 int i;
4021                 guint32 ip_offset;
4022                 MonoInvocation *inv;
4023                 MonoExceptionClause *clause;
4024                 /*char *message;*/
4025                 MonoObject *ex_obj;
4026
4027 #if DEBUG_INTERP
4028                 if (tracing)
4029                         g_print ("* Handling exception '%s' at IL_%04x\n", 
4030                                 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name, 
4031                                 rtm == NULL ? 0 : frame->ip - rtm->code);
4032 #endif
4033                 if (die_on_exception)
4034                         goto die_on_ex;
4035
4036                 for (inv = frame; inv; inv = inv->parent) {
4037                         MonoMethod *method;
4038                         if (inv->runtime_method == NULL)
4039                                 continue;
4040                         method = inv->runtime_method->method;
4041                         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4042                                 continue;
4043                         if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4044                                 continue;
4045                         if (inv->ip == NULL)
4046                                 continue;
4047                         ip_offset = inv->ip - inv->runtime_method->code;
4048                         inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one  - this one wins */
4049                         for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
4050                                 clause = &inv->runtime_method->clauses [i];
4051                                 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4052                                         if (!clause->flags) {
4053                                                 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4054                                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4055                                                 if (isinst_obj) {
4056                                                         /* 
4057                                                          * OK, we found an handler, now we need to execute the finally
4058                                                          * and fault blocks before branching to the handler code.
4059                                                          */
4060                                                         inv->ex_handler = clause;
4061 #if DEBUG_INTERP
4062                                                         if (tracing)
4063                                                                 g_print ("* Found handler at '%s'\n", method->name);
4064 #endif
4065                                                         goto handle_finally;
4066                                                 }
4067                                         } else {
4068                                                 g_error ("FIXME: handle filter clause");
4069                                         }
4070                                 }
4071                         }
4072                 }
4073                 /*
4074                  * If we get here, no handler was found: print a stack trace.
4075                  */
4076                 for (inv = frame; inv; inv = inv->parent) {
4077                         if (inv->invoke_trap)
4078                                 goto handle_finally;
4079                 }
4080 die_on_ex:
4081                 ex_obj = (MonoObject*)frame->ex;
4082                 mono_unhandled_exception (ex_obj);
4083                 exit (1);
4084         }
4085         handle_finally:
4086         {
4087                 int i;
4088                 guint32 ip_offset;
4089                 MonoExceptionClause *clause;
4090                 GSList *old_list = finally_ips;
4091                 MonoMethod *method = frame->runtime_method->method;
4092                 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4093                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4094                 
4095 #if DEBUG_INTERP
4096                 if (tracing)
4097                         g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4098 #endif
4099                 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
4100                                 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4101                         goto exit_frame;
4102                 }
4103                 ip_offset = frame->ip - rtm->code;
4104
4105                 if (endfinally_ip != NULL)
4106                         finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4107                 for (i = 0; i < header->num_clauses; ++i)
4108                         if (frame->ex_handler == &rtm->clauses [i])
4109                                 break;
4110                 while (i > 0) {
4111                         --i;
4112                         clause = &rtm->clauses [i];
4113                         if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4114                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4115                                         ip = rtm->code + clause->handler_offset;
4116                                         finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4117 #if DEBUG_INTERP
4118                                         if (tracing)
4119                                                 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4120 #endif
4121                                 }
4122                         }
4123                 }
4124
4125                 endfinally_ip = NULL;
4126
4127                 if (old_list != finally_ips && finally_ips) {
4128                         ip = finally_ips->data;
4129                         finally_ips = g_slist_remove (finally_ips, ip);
4130                         sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4131                         goto main_loop;
4132                 }
4133
4134                 /*
4135                  * If an exception is set, we need to execute the fault handler, too,
4136                  * otherwise, we continue normally.
4137                  */
4138                 if (frame->ex)
4139                         goto handle_fault;
4140                 ves_abort();
4141         }
4142         handle_fault:
4143         {
4144                 int i;
4145                 guint32 ip_offset;
4146                 MonoExceptionClause *clause;
4147                 MonoMethod *method = frame->runtime_method->method;
4148                 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4149                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4150                 
4151 #if DEBUG_INTERP
4152                 if (tracing)
4153                         g_print ("* Handle fault\n");
4154 #endif
4155                 ip_offset = frame->ip - rtm->code;
4156                 for (i = 0; i < header->num_clauses; ++i) {
4157                         clause = &rtm->clauses [i];
4158                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4159                                 ip = rtm->code + clause->handler_offset;
4160 #if DEBUG_INTERP
4161                                 if (tracing)
4162                                         g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4163 #endif
4164                                 goto main_loop;
4165                         }
4166                 }
4167                 /*
4168                  * If the handler for the exception was found in this method, we jump
4169                  * to it right away, otherwise we return and let the caller run
4170                  * the finally, fault and catch blocks.
4171                  * This same code should be present in the endfault opcode, but it
4172                  * is corrently not assigned in the ECMA specs: LAMESPEC.
4173                  */
4174                 if (frame->ex_handler) {
4175 #if DEBUG_INTERP
4176                         if (tracing)
4177                                 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4178 #endif
4179                         ip = rtm->code + frame->ex_handler->handler_offset;
4180                         sp = frame->stack;
4181                         vt_sp = (unsigned char *) sp + rtm->stack_size;
4182                         sp->data.p = frame->ex;
4183                         ++sp;
4184                         goto main_loop;
4185                 }
4186                 goto exit_frame;
4187         }
4188 exit_frame:
4189         DEBUG_LEAVE ();
4190 }
4191
4192 void
4193 ves_exec_method (MonoInvocation *frame)
4194 {
4195         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4196         ThreadContext context_struct;
4197         MonoError error;
4198         jmp_buf env;
4199
4200         frame->ex = NULL;
4201
4202         if (setjmp(env)) {
4203                 mono_unhandled_exception ((MonoObject*)frame->ex);
4204                 return;
4205         }
4206         if (context == NULL) {
4207                 context = &context_struct;
4208                 context_struct.domain = mono_domain_get ();
4209                 context_struct.base_frame = frame;
4210                 context_struct.current_frame = NULL;
4211                 context_struct.env_frame = frame;
4212                 context_struct.current_env = &env;
4213                 context_struct.search_for_handler = 0;
4214                 context_struct.managed_code = 0;
4215                 mono_native_tls_set_value (thread_context_id, context);
4216         }
4217         frame->ip = NULL;
4218         frame->parent = context->current_frame;
4219         frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4220         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4221         context->managed_code = 1;
4222         ves_exec_method_with_context (frame, context);
4223         context->managed_code = 0;
4224         if (frame->ex) {
4225                 if (context != &context_struct && context->current_env) {
4226                         context->env_frame->ex = frame->ex;
4227                         longjmp (*context->current_env, 1);
4228                 }
4229                 else
4230                         mono_unhandled_exception ((MonoObject*)frame->ex);
4231         }
4232         if (context->base_frame == frame)
4233                 mono_native_tls_set_value (thread_context_id, NULL);
4234         else
4235                 context->current_frame = frame->parent;
4236 }
4237
4238 static int 
4239 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4240 {
4241         MonoImage *image = mono_assembly_get_image (assembly);
4242         MonoMethod *method;
4243         MonoError error;
4244         int rval;
4245
4246         method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4247         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4248
4249         if (!method)
4250                 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4251
4252         rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4253         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4254         return rval;
4255 }
4256
4257 static void
4258 usage (void)
4259 {
4260         fprintf (stderr,
4261                  "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4262                  "Usage is: mint [options] executable args...\n\n", VERSION);
4263         fprintf (stderr,
4264                  "Runtime Debugging:\n"
4265 #ifdef DEBUG_INTERP
4266                  "   --debug\n"
4267 #endif
4268                  "   --dieonex\n"
4269                  "   --noptr\t\t\tdon't print pointer addresses in trace output\n"
4270                  "   --opcode-count\n"
4271                  "   --print-vtable\n"
4272                  "   --traceclassinit\n"
4273                  "\n"
4274                  "Development:\n"
4275                  "   --debug method_name\n"
4276                  "   --profile\n"
4277                  "   --trace\n"
4278                  "   --traceops\n"
4279                  "   --regression\n"
4280                  "\n"
4281                  "Runtime:\n"
4282                  "   --config filename  load the specified config file instead of the default\n"
4283                  "   --workers n        maximum number of worker threads\n"
4284                 );
4285         exit (1);
4286 }
4287
4288 static MonoBoolean
4289 interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
4290                           MonoReflectionMethod **method, 
4291                           gint32 *iloffset, gint32 *native_offset,
4292                           MonoString **file, gint32 *line, gint32 *column)
4293 {
4294         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4295         MonoInvocation *inv = context->current_frame;
4296         MonoError error;
4297         int i;
4298
4299         for (i = 0; inv && i < skip; inv = inv->parent)
4300                 if (inv->runtime_method != NULL)
4301                         ++i;
4302
4303         if (iloffset)
4304                 *iloffset = 0;
4305         if (native_offset)
4306                 *native_offset = 0;
4307         if (method) {
4308                 if (inv == NULL) {
4309                         *method = NULL;
4310                 } else {
4311                         *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4312                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4313                 }
4314         }
4315         if (line)
4316                 *line = 0;
4317         if (need_file_info) {
4318                 if (column)
4319                         *column = 0;
4320                 if (file)
4321                         *file = mono_string_new (mono_domain_get (), "unknown");
4322         }
4323
4324         return TRUE;
4325 }
4326
4327 static MonoArray *
4328 interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4329 {
4330         MonoDomain *domain = mono_domain_get ();
4331         MonoArray *res;
4332         MonoArray *ta = exc->trace_ips;
4333         MonoError error;
4334         int i, len;
4335
4336         if (ta == NULL) {
4337                 /* Exception is not thrown yet */
4338                 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4339                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4340                 return array;
4341         }
4342         
4343         len = mono_array_length (ta);
4344
4345         res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4346         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4347
4348         for (i = skip; i < len / 2; i++) {
4349                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4350                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4351                 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4352                 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4353
4354                 if (rtm != NULL) {
4355                         sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4356                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4357                         sf->native_offset = ip - rtm->code;
4358                 }
4359
4360 #if 0
4361                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4362
4363                 if (need_file_info) {
4364                         gchar *filename;
4365                         
4366                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4367
4368                         sf->filename = filename? mono_string_new (domain, filename): NULL;
4369                         sf->column = 0;
4370
4371                         g_free (filename);
4372                 }
4373 #endif
4374
4375                 mono_array_set (res, gpointer, i, sf);
4376         }
4377
4378         return res;
4379 }
4380
4381 void
4382 mono_interp_init ()
4383 {
4384         mono_native_tls_alloc (&thread_context_id, NULL);
4385     mono_native_tls_set_value (thread_context_id, NULL);
4386         mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4387
4388         mono_interp_transform_init ();
4389 }
4390
4391 typedef int (*TestMethod) (void);
4392
4393 static void
4394 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4395 {
4396         int result, expected, failed, cfailed, run;
4397         double elapsed, transform_time;
4398         int i;
4399         MonoObject *result_obj;
4400         static gboolean filter_method_init = FALSE;
4401         static const char *filter_method = NULL;
4402
4403         g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4404         cfailed = failed = run = 0;
4405         transform_time = elapsed = 0.0;
4406
4407         g_timer_start (timer);
4408         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4409                 MonoObject *exc = NULL;
4410                 MonoError error;
4411                 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4412                 if (!method) {
4413                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
4414                         continue;
4415                 }
4416
4417                 if (!filter_method_init) {
4418                         filter_method = g_getenv ("INTERP_FILTER_METHOD");
4419                         filter_method_init = TRUE;
4420                 }
4421                 gboolean filter = FALSE;
4422                 if (filter_method) {
4423                         const char *name = filter_method;
4424
4425                         if ((strchr (name, '.') > name) || strchr (name, ':')) {
4426                                 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
4427                                 filter = mono_method_desc_full_match (desc, method);
4428                                 mono_method_desc_free (desc);
4429                         } else {
4430                                 filter = strcmp (method->name, name) == 0;
4431                         }
4432                 } else { /* no filter, check for `Category' attribute on method */
4433                         filter = TRUE;
4434                         MonoCustomAttrInfo* ainfo = mono_custom_attrs_from_method_checked (method, &error);
4435                         mono_error_cleanup (&error);
4436
4437                         if (ainfo) {
4438                                 int j;
4439                                 for (j = 0; j < ainfo->num_attrs && filter; ++j) {
4440                                         MonoCustomAttrEntry *centry = &ainfo->attrs [j];
4441                                         if (centry->ctor == NULL)
4442                                                 continue;
4443
4444                                         MonoClass *klass = centry->ctor->klass;
4445                                         if (strcmp (klass->name, "CategoryAttribute"))
4446                                                 continue;
4447
4448                                         MonoObject *obj = mono_custom_attrs_get_attr_checked (ainfo, klass, &error);
4449                                         /* FIXME: there is an ordering problem if there're multiple attributes, do this instead:
4450                                          * MonoObject *obj = create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, &error); */
4451                                         mono_error_cleanup (&error);
4452                                         MonoMethod *getter = mono_class_get_method_from_name (klass, "get_Category", -1);
4453                                         MonoObject *str = mono_interp_runtime_invoke (getter, obj, NULL, &exc, &error);
4454                                         mono_error_cleanup (&error);
4455                                         char *utf8_str = mono_string_to_utf8_checked ((MonoString *) str, &error);
4456                                         mono_error_cleanup (&error);
4457                                         if (!strcmp (utf8_str, "!INTERPRETER")) {
4458                                                 g_print ("skip %s...\n", method->name);
4459                                                 filter = FALSE;
4460                                         }
4461                                 }
4462                         }
4463                 }
4464                 if (strncmp (method->name, "test_", 5) == 0 && filter) {
4465                         MonoError interp_error;
4466                         MonoObject *exc = NULL;
4467
4468                         result_obj = mono_interp_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
4469                         if (!mono_error_ok (&interp_error)) {
4470                                 cfailed++;
4471                                 g_print ("Test '%s' execution failed.\n", method->name);
4472                         } else if (exc != NULL) {
4473                                 g_print ("Exception in Test '%s' occured:\n", method->name);
4474                                 mono_object_describe (exc);
4475                                 run++;
4476                                 failed++;
4477                         } else {
4478                                 result = *(gint32 *) mono_object_unbox (result_obj);
4479                                 expected = atoi (method->name + 5);  // FIXME: oh no.
4480                                 run++;
4481
4482                                 if (result != expected) {
4483                                         failed++;
4484                                         g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4485                                 }
4486                         }
4487                 }
4488         }
4489         g_timer_stop (timer);
4490         elapsed = g_timer_elapsed (timer, NULL);
4491         if (failed > 0 || cfailed > 0){
4492                 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4493                                 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4494         } else {
4495                 g_print ("Results: total tests: %d, all pass \n",  run);
4496         }
4497
4498         g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4499                         elapsed - transform_time, transform_time);
4500         *total += failed + cfailed;
4501         *total_run += run;
4502 }
4503 static int
4504 interp_regression (MonoImage *image, int verbose, int *total_run)
4505 {
4506         MonoMethod *method;
4507         GTimer *timer = g_timer_new ();
4508         MonoDomain *domain = mono_domain_get ();
4509         guint32 i;
4510         int total;
4511
4512         /* load the metadata */
4513         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4514                 MonoError error;
4515                 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4516                 if (!method) {
4517                         mono_error_cleanup (&error);
4518                         continue;
4519                 }
4520                 mono_class_init (method->klass);
4521         }
4522
4523         total = 0;
4524         *total_run = 0;
4525         interp_regression_step (image, verbose, total_run, &total, timer, domain);
4526
4527         g_timer_destroy (timer);
4528         return total;
4529 }
4530
4531 int
4532 mono_interp_regression_list (int verbose, int count, char *images [])
4533 {
4534         int i, total, total_run, run;
4535         
4536         total_run = total = 0;
4537         for (i = 0; i < count; ++i) {
4538                 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4539                 if (!ass) {
4540                         g_warning ("failed to load assembly: %s", images [i]);
4541                         continue;
4542                 }
4543                 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4544                 total_run += run;
4545         }
4546         if (total > 0) {
4547                 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4548         } else {
4549                 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);
4550         }
4551         
4552         return total;
4553 }
4554