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