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