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