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