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