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