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