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