33166b8459cd9fada6d1d427cca0bb4b23e4c899
[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                 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1498                 context->managed_code = 1;
1499                 if (frame->ex) {
1500                         rtm = NULL;
1501                         ip = NULL;
1502                         goto exit_frame;
1503                 }
1504         }
1505
1506         rtm = frame->runtime_method;
1507         frame->args = alloca (rtm->alloca_size);
1508         sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1509 #if DEBUG_INTERP
1510         if (tracing > 1)
1511                 memset(sp, 0, rtm->stack_size);
1512 #endif
1513         vt_sp = (unsigned char *) sp + rtm->stack_size;
1514 #if DEBUG_INTERP
1515         vtalloc = vt_sp;
1516 #endif
1517         locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1518
1519         child_frame.parent = frame;
1520
1521         /* ready to go */
1522         ip = rtm->code;
1523
1524         /*
1525          * using while (ip < end) may result in a 15% performance drop, 
1526          * but it may be useful for debug
1527          */
1528         while (1) {
1529         main_loop:
1530                 /* g_assert (sp >= frame->stack); */
1531                 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1532                 DUMP_INSTR();
1533                 MINT_IN_SWITCH (*ip) {
1534                 MINT_IN_CASE(MINT_INITLOCALS)
1535                         memset (locals, 0, rtm->locals_size);
1536                         ++ip;
1537                         MINT_IN_BREAK;
1538                 MINT_IN_CASE(MINT_NOP)
1539                         ++ip;
1540                         MINT_IN_BREAK;
1541                 MINT_IN_CASE(MINT_BREAK)
1542                         ++ip;
1543                         G_BREAKPOINT (); /* this is not portable... */
1544                         MINT_IN_BREAK;
1545                 MINT_IN_CASE(MINT_LDNULL) 
1546                         sp->data.p = NULL;
1547                         ++ip;
1548                         ++sp;
1549                         MINT_IN_BREAK;
1550                 MINT_IN_CASE(MINT_VTRESULT) {
1551                         int ret_size = * (guint16 *)(ip + 1);
1552                         unsigned char *ret_vt_sp = vt_sp;
1553                         vt_sp -= READ32(ip + 2);
1554                         if (ret_size > 0) {
1555                                 memmove (vt_sp, ret_vt_sp, ret_size);
1556                                 vt_sp += (ret_size + 7) & ~7;
1557                         }
1558                         ip += 4;
1559                         MINT_IN_BREAK;
1560                 }
1561 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1562                 MINT_IN_CASE(MINT_LDC_I4_M1)
1563                         LDC(-1);
1564                         MINT_IN_BREAK;
1565                 MINT_IN_CASE(MINT_LDC_I4_0)
1566                         LDC(0);
1567                         MINT_IN_BREAK;
1568                 MINT_IN_CASE(MINT_LDC_I4_1)
1569                         LDC(1);
1570                         MINT_IN_BREAK;
1571                 MINT_IN_CASE(MINT_LDC_I4_2)
1572                         LDC(2);
1573                         MINT_IN_BREAK;
1574                 MINT_IN_CASE(MINT_LDC_I4_3)
1575                         LDC(3);
1576                         MINT_IN_BREAK;
1577                 MINT_IN_CASE(MINT_LDC_I4_4)
1578                         LDC(4);
1579                         MINT_IN_BREAK;
1580                 MINT_IN_CASE(MINT_LDC_I4_5)
1581                         LDC(5);
1582                         MINT_IN_BREAK;
1583                 MINT_IN_CASE(MINT_LDC_I4_6)
1584                         LDC(6);
1585                         MINT_IN_BREAK;
1586                 MINT_IN_CASE(MINT_LDC_I4_7)
1587                         LDC(7);
1588                         MINT_IN_BREAK;
1589                 MINT_IN_CASE(MINT_LDC_I4_8)
1590                         LDC(8);
1591                         MINT_IN_BREAK;
1592                 MINT_IN_CASE(MINT_LDC_I4_S) 
1593                         sp->data.i = *(const short *)(ip + 1);
1594                         ip += 2;
1595                         ++sp;
1596                         MINT_IN_BREAK;
1597                 MINT_IN_CASE(MINT_LDC_I4)
1598                         ++ip;
1599                         sp->data.i = READ32 (ip);
1600                         ip += 2;
1601                         ++sp;
1602                         MINT_IN_BREAK;
1603                 MINT_IN_CASE(MINT_LDC_I8)
1604                         ++ip;
1605                         sp->data.l = READ64 (ip);
1606                         ip += 4;
1607                         ++sp;
1608                         MINT_IN_BREAK;
1609                 MINT_IN_CASE(MINT_LDC_R4) {
1610                         guint32 val;
1611                         ++ip;
1612                         val = READ32(ip);
1613                         sp->data.f = * (float *)&val;
1614                         ip += 2;
1615                         ++sp;
1616                         MINT_IN_BREAK;
1617                 }
1618                 MINT_IN_CASE(MINT_LDC_R8) 
1619                         sp->data.l = READ64 (ip + 1); /* note union usage */
1620                         ip += 5;
1621                         ++sp;
1622                         MINT_IN_BREAK;
1623                 MINT_IN_CASE(MINT_DUP) 
1624                         sp [0] = sp[-1];
1625                         ++sp;
1626                         ++ip; 
1627                         MINT_IN_BREAK;
1628                 MINT_IN_CASE(MINT_DUP_VT)
1629                         i32 = READ32 (ip + 1);
1630                         sp->data.p = vt_sp;
1631                         memcpy(sp->data.p, sp [-1].data.p, i32);
1632                         vt_sp += (i32 + 7) & ~7;
1633                         ++sp;
1634                         ip += 3;
1635                         MINT_IN_BREAK;
1636                 MINT_IN_CASE(MINT_POP)
1637                         ++ip;
1638                         --sp;
1639                         MINT_IN_BREAK;
1640                 MINT_IN_CASE(MINT_JMP) {
1641                         RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1642                         if (!new_method->transformed) {
1643                                 frame->ip = ip;
1644                                 frame->ex = mono_interp_transform_method (new_method, context);
1645                                 if (frame->ex)
1646                                         goto exit_frame;
1647                         }
1648                         ip += 2;
1649                         if (new_method->alloca_size > rtm->alloca_size)
1650                                 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size); 
1651                         rtm = frame->runtime_method = new_method;
1652                         vt_sp = (unsigned char *) sp + rtm->stack_size;
1653 #if DEBUG_INTERP
1654                         vtalloc = vt_sp;
1655 #endif
1656                         locals = vt_sp + rtm->vt_stack_size;
1657                         ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1658                         MINT_IN_BREAK;
1659                 }
1660                 MINT_IN_CASE(MINT_CALLI) {
1661                         MonoMethodSignature *csignature;
1662                         stackval *endsp = sp;
1663
1664                         frame->ip = ip;
1665                         
1666                         csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1667                         ip += 2;
1668                         --sp;
1669                         --endsp;
1670                         child_frame.runtime_method = sp->data.p;
1671
1672                         sp->data.p = vt_sp;
1673                         child_frame.retval = sp;
1674                         /* decrement by the actual number of args */
1675                         sp -= csignature->param_count;
1676                         child_frame.stack_args = sp;
1677                         if (csignature->hasthis) {
1678                                 --sp;
1679                                 child_frame.obj = sp->data.p;
1680                         } else {
1681                                 child_frame.obj = NULL;
1682                         }
1683                         if (csignature->hasthis && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1684                                 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1685                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1686                         } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1687                                 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1688                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1689                         }
1690
1691                         ves_exec_method_with_context (&child_frame, context);
1692
1693                         context->current_frame = frame;
1694
1695                         if (child_frame.ex) {
1696                                 /*
1697                                  * An exception occurred, need to run finally, fault and catch handlers..
1698                                  */
1699                                 frame->ex = child_frame.ex;
1700                                 goto handle_finally;
1701                         }
1702
1703                         /* need to handle typedbyref ... */
1704                         if (csignature->ret->type != MONO_TYPE_VOID) {
1705                                 *sp = *endsp;
1706                                 sp++;
1707                         }
1708                         MINT_IN_BREAK;
1709                 }
1710                 MINT_IN_CASE(MINT_CALLI_NAT) {
1711                         MonoMethodSignature *csignature;
1712                         stackval *endsp = sp;
1713                         unsigned char *code = NULL;
1714
1715                         frame->ip = ip;
1716                         
1717                         csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1718                         ip += 2;
1719                         --sp;
1720                         --endsp;
1721                         code = sp->data.p;
1722                         child_frame.runtime_method = NULL;
1723
1724                         sp->data.p = vt_sp;
1725                         child_frame.retval = sp;
1726                         /* decrement by the actual number of args */
1727                         sp -= csignature->param_count;
1728                         child_frame.stack_args = sp;
1729                         if (csignature->hasthis) {
1730                                 --sp;
1731                                 child_frame.obj = sp->data.p;
1732                         } else {
1733                                 child_frame.obj = NULL;
1734                         }
1735                         ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1736
1737                         context->current_frame = frame;
1738
1739                         if (child_frame.ex) {
1740                                 /*
1741                                  * An exception occurred, need to run finally, fault and catch handlers..
1742                                  */
1743                                 frame->ex = child_frame.ex;
1744                                 if (context->search_for_handler) {
1745                                         context->search_for_handler = 0;
1746                                         goto handle_exception;
1747                                 }
1748                                 goto handle_finally;
1749                         }
1750
1751                         /* need to handle typedbyref ... */
1752                         if (csignature->ret->type != MONO_TYPE_VOID) {
1753                                 *sp = *endsp;
1754                                 sp++;
1755                         }
1756                         MINT_IN_BREAK;
1757                 }
1758                 MINT_IN_CASE(MINT_CALL) {
1759                         stackval *endsp = sp;
1760
1761                         frame->ip = ip;
1762                         
1763                         child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1764                         ip += 2;
1765                         sp->data.p = vt_sp;
1766                         child_frame.retval = sp;
1767                         /* decrement by the actual number of args */
1768                         sp -= child_frame.runtime_method->param_count;
1769                         child_frame.stack_args = sp;
1770                         if (child_frame.runtime_method->hasthis) {
1771                                 --sp;
1772                                 child_frame.obj = sp->data.p;
1773                         } else {
1774                                 child_frame.obj = NULL;
1775                         }
1776                         if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1777                                 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1778                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1779                         }
1780                         ves_exec_method_with_context (&child_frame, context);
1781
1782                         context->current_frame = frame;
1783
1784                         if (child_frame.ex) {
1785                                 /*
1786                                  * An exception occurred, need to run finally, fault and catch handlers..
1787                                  */
1788                                 frame->ex = child_frame.ex;
1789                                 goto handle_finally;
1790                         }
1791
1792                         /* need to handle typedbyref ... */
1793                         *sp = *endsp;
1794                         sp++;
1795                         MINT_IN_BREAK;
1796                 }
1797                 MINT_IN_CASE(MINT_VCALL) {
1798                         frame->ip = ip;
1799                         
1800                         child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1801                         ip += 2;
1802
1803                         sp->data.p = vt_sp;
1804                         child_frame.retval = sp;
1805                         /* decrement by the actual number of args */
1806                         sp -= child_frame.runtime_method->param_count;
1807                         child_frame.stack_args = sp;
1808                         if (child_frame.runtime_method->hasthis) {
1809                                 --sp;
1810                                 child_frame.obj = sp->data.p;
1811                         } else {
1812                                 child_frame.obj = NULL;
1813                         }
1814
1815                         if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1816                                 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1817                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1818                         }
1819
1820                         ves_exec_method_with_context (&child_frame, context);
1821
1822                         context->current_frame = frame;
1823
1824                         if (child_frame.ex) {
1825                                 /*
1826                                  * An exception occurred, need to run finally, fault and catch handlers..
1827                                  */
1828                                 frame->ex = child_frame.ex;
1829                                 goto handle_finally;
1830                         }
1831                         MINT_IN_BREAK;
1832                 }
1833                 MINT_IN_CASE(MINT_CALLVIRT) {
1834                         stackval *endsp = sp;
1835                         MonoObject *this_arg;
1836                         guint32 token;
1837
1838                         frame->ip = ip;
1839                         
1840                         token = * (unsigned short *)(ip + 1);
1841                         ip += 2;
1842                         child_frame.runtime_method = rtm->data_items [token];
1843                         sp->data.p = vt_sp;
1844                         child_frame.retval = sp;
1845
1846                         /* decrement by the actual number of args */
1847                         sp -= child_frame.runtime_method->param_count;
1848                         child_frame.stack_args = sp;
1849                         --sp;
1850                         child_frame.obj = this_arg = sp->data.p;
1851                         if (!this_arg)
1852                                 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1853                         child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1854
1855                         if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1856                                 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1857                         }
1858
1859                         ves_exec_method_with_context (&child_frame, context);
1860
1861                         context->current_frame = frame;
1862
1863                         if (child_frame.ex) {
1864                                 /*
1865                                  * An exception occurred, need to run finally, fault and catch handlers..
1866                                  */
1867                                 frame->ex = child_frame.ex;
1868                                 if (context->search_for_handler) {
1869                                         context->search_for_handler = 0;
1870                                         goto handle_exception;
1871                                 }
1872                                 goto handle_finally;
1873                         }
1874
1875                         /* need to handle typedbyref ... */
1876                         *sp = *endsp;
1877                         sp++;
1878                         MINT_IN_BREAK;
1879                 }
1880                 MINT_IN_CASE(MINT_VCALLVIRT) {
1881                         MonoObject *this_arg;
1882                         guint32 token;
1883
1884                         frame->ip = ip;
1885                         
1886                         token = * (unsigned short *)(ip + 1);
1887                         ip += 2;
1888                         child_frame.runtime_method = rtm->data_items [token];
1889                         sp->data.p = vt_sp;
1890                         child_frame.retval = sp;
1891
1892                         /* decrement by the actual number of args */
1893                         sp -= child_frame.runtime_method->param_count;
1894                         child_frame.stack_args = sp;
1895                         --sp;
1896                         child_frame.obj = this_arg = sp->data.p;
1897                         if (!this_arg)
1898                                 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1899                         child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1900
1901                         if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1902                                 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1903                         }
1904
1905                         ves_exec_method_with_context (&child_frame, context);
1906
1907                         context->current_frame = frame;
1908
1909                         if (child_frame.ex) {
1910                                 /*
1911                                  * An exception occurred, need to run finally, fault and catch handlers..
1912                                  */
1913                                 frame->ex = child_frame.ex;
1914                                 if (context->search_for_handler) {
1915                                         context->search_for_handler = 0;
1916                                         goto handle_exception;
1917                                 }
1918                                 goto handle_finally;
1919                         }
1920                         MINT_IN_BREAK;
1921                 }
1922                 MINT_IN_CASE(MINT_CALLINT)
1923                         ves_pinvoke_method (frame, mono_method_signature (frame->runtime_method->method), ((MonoMethodPInvoke*) frame->runtime_method->method)->addr, 
1924                                     frame->runtime_method->method->string_ctor, context);
1925                         if (frame->ex) {
1926                                 rtm = NULL;
1927                                 goto handle_exception;
1928                         }
1929                         goto exit_frame;
1930                 MINT_IN_CASE(MINT_CALLRUN)
1931                         ves_runtime_method (frame, context);
1932                         if (frame->ex) {
1933                                 rtm = NULL;
1934                                 goto handle_exception;
1935                         }
1936                         goto exit_frame;
1937                 MINT_IN_CASE(MINT_RET)
1938                         --sp;
1939                         *frame->retval = *sp;
1940                         if (sp > frame->stack)
1941                                 g_warning ("ret: more values on stack: %d", sp-frame->stack);
1942                         goto exit_frame;
1943                 MINT_IN_CASE(MINT_RET_VOID)
1944                         if (sp > frame->stack)
1945                                 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
1946                         goto exit_frame;
1947                 MINT_IN_CASE(MINT_RET_VT)
1948                         i32 = READ32(ip + 1);
1949                         --sp;
1950                         memcpy(frame->retval->data.p, sp->data.p, i32);
1951                         if (sp > frame->stack)
1952                                 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
1953                         goto exit_frame;
1954                 MINT_IN_CASE(MINT_BR_S)
1955                         ip += (short) *(ip + 1);
1956                         MINT_IN_BREAK;
1957                 MINT_IN_CASE(MINT_BR)
1958                         ip += (gint32) READ32(ip + 1);
1959                         MINT_IN_BREAK;
1960 #define ZEROP_S(datamem, op) \
1961         --sp; \
1962         if (sp->data.datamem op 0) \
1963                 ip += * (gint16 *)(ip + 1); \
1964         else \
1965                 ip += 2;
1966
1967 #define ZEROP(datamem, op) \
1968         --sp; \
1969         if (sp->data.datamem op 0) \
1970                 ip += READ32(ip + 1); \
1971         else \
1972                 ip += 3;
1973
1974                 MINT_IN_CASE(MINT_BRFALSE_I4_S)
1975                         ZEROP_S(i, ==);
1976                         MINT_IN_BREAK;
1977                 MINT_IN_CASE(MINT_BRFALSE_I8_S)
1978                         ZEROP_S(l, ==);
1979                         MINT_IN_BREAK;
1980                 MINT_IN_CASE(MINT_BRFALSE_R8_S)
1981                         ZEROP_S(f, ==);
1982                         MINT_IN_BREAK;
1983                 MINT_IN_CASE(MINT_BRFALSE_I4)
1984                         ZEROP(i, ==);
1985                         MINT_IN_BREAK;
1986                 MINT_IN_CASE(MINT_BRFALSE_I8)
1987                         ZEROP(l, ==);
1988                         MINT_IN_BREAK;
1989                 MINT_IN_CASE(MINT_BRFALSE_R8)
1990                         ZEROP_S(f, ==);
1991                         MINT_IN_BREAK;
1992                 MINT_IN_CASE(MINT_BRTRUE_I4_S)
1993                         ZEROP_S(i, !=);
1994                         MINT_IN_BREAK;
1995                 MINT_IN_CASE(MINT_BRTRUE_I8_S)
1996                         ZEROP_S(l, !=);
1997                         MINT_IN_BREAK;
1998                 MINT_IN_CASE(MINT_BRTRUE_R8_S)
1999                         ZEROP_S(f, !=);
2000                         MINT_IN_BREAK;
2001                 MINT_IN_CASE(MINT_BRTRUE_I4)
2002                         ZEROP(i, !=);
2003                         MINT_IN_BREAK;
2004                 MINT_IN_CASE(MINT_BRTRUE_I8)
2005                         ZEROP(l, !=);
2006                         MINT_IN_BREAK;
2007                 MINT_IN_CASE(MINT_BRTRUE_R8)
2008                         ZEROP(f, !=);
2009                         MINT_IN_BREAK;
2010 #define CONDBR_S(cond) \
2011         sp -= 2; \
2012         if (cond) \
2013                 ip += * (gint16 *)(ip + 1); \
2014         else \
2015                 ip += 2;
2016 #define BRELOP_S(datamem, op) \
2017         CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2018
2019 #define CONDBR(cond) \
2020         sp -= 2; \
2021         if (cond) \
2022                 ip += READ32(ip + 1); \
2023         else \
2024                 ip += 3;
2025
2026 #define BRELOP(datamem, op) \
2027         CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2028
2029                 MINT_IN_CASE(MINT_BEQ_I4_S)
2030                         BRELOP_S(i, ==)
2031                         MINT_IN_BREAK;
2032                 MINT_IN_CASE(MINT_BEQ_I8_S)
2033                         BRELOP_S(l, ==)
2034                         MINT_IN_BREAK;
2035                 MINT_IN_CASE(MINT_BEQ_R8_S)
2036                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2037                         MINT_IN_BREAK;
2038                 MINT_IN_CASE(MINT_BEQ_I4)
2039                         BRELOP(i, ==)
2040                         MINT_IN_BREAK;
2041                 MINT_IN_CASE(MINT_BEQ_I8)
2042                         BRELOP(l, ==)
2043                         MINT_IN_BREAK;
2044                 MINT_IN_CASE(MINT_BEQ_R8)
2045                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2046                         MINT_IN_BREAK;
2047                 MINT_IN_CASE(MINT_BGE_I4_S)
2048                         BRELOP_S(i, >=)
2049                         MINT_IN_BREAK;
2050                 MINT_IN_CASE(MINT_BGE_I8_S)
2051                         BRELOP_S(l, >=)
2052                         MINT_IN_BREAK;
2053                 MINT_IN_CASE(MINT_BGE_R8_S)
2054                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2055                         MINT_IN_BREAK;
2056                 MINT_IN_CASE(MINT_BGE_I4)
2057                         BRELOP(i, >=)
2058                         MINT_IN_BREAK;
2059                 MINT_IN_CASE(MINT_BGE_I8)
2060                         BRELOP(l, >=)
2061                         MINT_IN_BREAK;
2062                 MINT_IN_CASE(MINT_BGE_R8)
2063                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2064                         MINT_IN_BREAK;
2065                 MINT_IN_CASE(MINT_BGT_I4_S)
2066                         BRELOP_S(i, >)
2067                         MINT_IN_BREAK;
2068                 MINT_IN_CASE(MINT_BGT_I8_S)
2069                         BRELOP_S(l, >)
2070                         MINT_IN_BREAK;
2071                 MINT_IN_CASE(MINT_BGT_R8_S)
2072                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2073                         MINT_IN_BREAK;
2074                 MINT_IN_CASE(MINT_BGT_I4)
2075                         BRELOP(i, >)
2076                         MINT_IN_BREAK;
2077                 MINT_IN_CASE(MINT_BGT_I8)
2078                         BRELOP(l, >)
2079                         MINT_IN_BREAK;
2080                 MINT_IN_CASE(MINT_BGT_R8)
2081                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2082                         MINT_IN_BREAK;
2083                 MINT_IN_CASE(MINT_BLT_I4_S)
2084                         BRELOP_S(i, <)
2085                         MINT_IN_BREAK;
2086                 MINT_IN_CASE(MINT_BLT_I8_S)
2087                         BRELOP_S(l, <)
2088                         MINT_IN_BREAK;
2089                 MINT_IN_CASE(MINT_BLT_R8_S)
2090                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2091                         MINT_IN_BREAK;
2092                 MINT_IN_CASE(MINT_BLT_I4)
2093                         BRELOP(i, <)
2094                         MINT_IN_BREAK;
2095                 MINT_IN_CASE(MINT_BLT_I8)
2096                         BRELOP(l, <)
2097                         MINT_IN_BREAK;
2098                 MINT_IN_CASE(MINT_BLT_R8)
2099                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2100                         MINT_IN_BREAK;
2101                 MINT_IN_CASE(MINT_BLE_I4_S)
2102                         BRELOP_S(i, <=)
2103                         MINT_IN_BREAK;
2104                 MINT_IN_CASE(MINT_BLE_I8_S)
2105                         BRELOP_S(l, <=)
2106                         MINT_IN_BREAK;
2107                 MINT_IN_CASE(MINT_BLE_R8_S)
2108                         CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2109                         MINT_IN_BREAK;
2110                 MINT_IN_CASE(MINT_BLE_I4)
2111                         BRELOP(i, <=)
2112                         MINT_IN_BREAK;
2113                 MINT_IN_CASE(MINT_BLE_I8)
2114                         BRELOP(l, <=)
2115                         MINT_IN_BREAK;
2116                 MINT_IN_CASE(MINT_BLE_R8)
2117                         CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2118                         MINT_IN_BREAK;
2119                 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2120                         BRELOP_S(i, !=)
2121                         MINT_IN_BREAK;
2122                 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2123                         BRELOP_S(l, !=)
2124                         MINT_IN_BREAK;
2125                 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2126                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2127                         MINT_IN_BREAK;
2128                 MINT_IN_CASE(MINT_BNE_UN_I4)
2129                         BRELOP(i, !=)
2130                         MINT_IN_BREAK;
2131                 MINT_IN_CASE(MINT_BNE_UN_I8)
2132                         BRELOP(l, !=)
2133                         MINT_IN_BREAK;
2134                 MINT_IN_CASE(MINT_BNE_UN_R8)
2135                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2136                         MINT_IN_BREAK;
2137
2138 #define BRELOP_S_CAST(datamem, op, type) \
2139         sp -= 2; \
2140         if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2141                 ip += * (gint16 *)(ip + 1); \
2142         else \
2143                 ip += 2;
2144
2145 #define BRELOP_CAST(datamem, op, type) \
2146         sp -= 2; \
2147         if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2148                 ip += READ32(ip + 1); \
2149         else \
2150                 ip += 3;
2151
2152                 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2153                         BRELOP_S_CAST(i, >=, guint32);
2154                         MINT_IN_BREAK;
2155                 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2156                         BRELOP_S_CAST(l, >=, guint64);
2157                         MINT_IN_BREAK;
2158                 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2159                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2160                         MINT_IN_BREAK;
2161                 MINT_IN_CASE(MINT_BGE_UN_I4)
2162                         BRELOP_CAST(i, >=, guint32);
2163                         MINT_IN_BREAK;
2164                 MINT_IN_CASE(MINT_BGE_UN_I8)
2165                         BRELOP_CAST(l, >=, guint64);
2166                         MINT_IN_BREAK;
2167                 MINT_IN_CASE(MINT_BGE_UN_R8)
2168                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2169                         MINT_IN_BREAK;
2170                 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2171                         BRELOP_S_CAST(i, >, guint32);
2172                         MINT_IN_BREAK;
2173                 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2174                         BRELOP_S_CAST(l, >, guint64);
2175                         MINT_IN_BREAK;
2176                 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2177                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2178                         MINT_IN_BREAK;
2179                 MINT_IN_CASE(MINT_BGT_UN_I4)
2180                         BRELOP_CAST(i, >, guint32);
2181                         MINT_IN_BREAK;
2182                 MINT_IN_CASE(MINT_BGT_UN_I8)
2183                         BRELOP_CAST(l, >, guint64);
2184                         MINT_IN_BREAK;
2185                 MINT_IN_CASE(MINT_BGT_UN_R8)
2186                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2187                         MINT_IN_BREAK;
2188                 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2189                         BRELOP_S_CAST(i, <=, guint32);
2190                         MINT_IN_BREAK;
2191                 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2192                         BRELOP_S_CAST(l, <=, guint64);
2193                         MINT_IN_BREAK;
2194                 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2195                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2196                         MINT_IN_BREAK;
2197                 MINT_IN_CASE(MINT_BLE_UN_I4)
2198                         BRELOP_CAST(i, <=, guint32);
2199                         MINT_IN_BREAK;
2200                 MINT_IN_CASE(MINT_BLE_UN_I8)
2201                         BRELOP_CAST(l, <=, guint64);
2202                         MINT_IN_BREAK;
2203                 MINT_IN_CASE(MINT_BLE_UN_R8)
2204                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2205                         MINT_IN_BREAK;
2206                 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2207                         BRELOP_S_CAST(i, <, guint32);
2208                         MINT_IN_BREAK;
2209                 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2210                         BRELOP_S_CAST(l, <, guint64);
2211                         MINT_IN_BREAK;
2212                 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2213                         CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2214                         MINT_IN_BREAK;
2215                 MINT_IN_CASE(MINT_BLT_UN_I4)
2216                         BRELOP_CAST(i, <, guint32);
2217                         MINT_IN_BREAK;
2218                 MINT_IN_CASE(MINT_BLT_UN_I8)
2219                         BRELOP_CAST(l, <, guint64);
2220                         MINT_IN_BREAK;
2221                 MINT_IN_CASE(MINT_BLT_UN_R8)
2222                         CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2223                         MINT_IN_BREAK;
2224                 MINT_IN_CASE(MINT_SWITCH) {
2225                         guint32 n;
2226                         const unsigned short *st;
2227                         ++ip;
2228                         n = READ32 (ip);
2229                         ip += 2;
2230                         st = ip + 2 * n;
2231                         --sp;
2232                         if ((guint32)sp->data.i < n) {
2233                                 gint offset;
2234                                 ip += 2 * (guint32)sp->data.i;
2235                                 offset = READ32 (ip);
2236                                 ip = st + offset;
2237                         } else {
2238                                 ip = st;
2239                         }
2240                         MINT_IN_BREAK;
2241                 }
2242                 MINT_IN_CASE(MINT_LDIND_I1)
2243                         ++ip;
2244                         sp[-1].data.i = *(gint8*)sp[-1].data.p;
2245                         MINT_IN_BREAK;
2246                 MINT_IN_CASE(MINT_LDIND_U1)
2247                         ++ip;
2248                         sp[-1].data.i = *(guint8*)sp[-1].data.p;
2249                         MINT_IN_BREAK;
2250                 MINT_IN_CASE(MINT_LDIND_I2)
2251                         ++ip;
2252                         sp[-1].data.i = *(gint16*)sp[-1].data.p;
2253                         MINT_IN_BREAK;
2254                 MINT_IN_CASE(MINT_LDIND_U2)
2255                         ++ip;
2256                         sp[-1].data.i = *(guint16*)sp[-1].data.p;
2257                         MINT_IN_BREAK;
2258                 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2259                 MINT_IN_CASE(MINT_LDIND_U4)
2260                         ++ip;
2261                         sp[-1].data.i = *(gint32*)sp[-1].data.p;
2262                         MINT_IN_BREAK;
2263                 MINT_IN_CASE(MINT_LDIND_I8)
2264                         ++ip;
2265                         sp[-1].data.l = *(gint64*)sp[-1].data.p;
2266                         MINT_IN_BREAK;
2267                 MINT_IN_CASE(MINT_LDIND_I)
2268                         ++ip;
2269                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2270                         MINT_IN_BREAK;
2271                 MINT_IN_CASE(MINT_LDIND_R4)
2272                         ++ip;
2273                         sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2274                         MINT_IN_BREAK;
2275                 MINT_IN_CASE(MINT_LDIND_R8)
2276                         ++ip;
2277                         sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2278                         MINT_IN_BREAK;
2279                 MINT_IN_CASE(MINT_LDIND_REF)
2280                         ++ip;
2281                         sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2282                         MINT_IN_BREAK;
2283                 MINT_IN_CASE(MINT_STIND_REF) 
2284                         ++ip;
2285                         sp -= 2;
2286                         * (gpointer *) sp->data.p = sp[1].data.p;
2287                         MINT_IN_BREAK;
2288                 MINT_IN_CASE(MINT_STIND_I1)
2289                         ++ip;
2290                         sp -= 2;
2291                         * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2292                         MINT_IN_BREAK;
2293                 MINT_IN_CASE(MINT_STIND_I2)
2294                         ++ip;
2295                         sp -= 2;
2296                         * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2297                         MINT_IN_BREAK;
2298                 MINT_IN_CASE(MINT_STIND_I4)
2299                         ++ip;
2300                         sp -= 2;
2301                         * (gint32 *) sp->data.p = sp[1].data.i;
2302                         MINT_IN_BREAK;
2303                 MINT_IN_CASE(MINT_STIND_I)
2304                         ++ip;
2305                         sp -= 2;
2306                         * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2307                         MINT_IN_BREAK;
2308                 MINT_IN_CASE(MINT_STIND_I8)
2309                         ++ip;
2310                         sp -= 2;
2311                         * (gint64 *) sp->data.p = sp[1].data.l;
2312                         MINT_IN_BREAK;
2313                 MINT_IN_CASE(MINT_STIND_R4)
2314                         ++ip;
2315                         sp -= 2;
2316                         * (float *) sp->data.p = (gfloat)sp[1].data.f;
2317                         MINT_IN_BREAK;
2318                 MINT_IN_CASE(MINT_STIND_R8)
2319                         ++ip;
2320                         sp -= 2;
2321                         * (double *) sp->data.p = sp[1].data.f;
2322                         MINT_IN_BREAK;
2323 #define BINOP(datamem, op) \
2324         --sp; \
2325         sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2326         ++ip;
2327                 MINT_IN_CASE(MINT_ADD_I4)
2328                         BINOP(i, +);
2329                         MINT_IN_BREAK;
2330                 MINT_IN_CASE(MINT_ADD_I8)
2331                         BINOP(l, +);
2332                         MINT_IN_BREAK;
2333                 MINT_IN_CASE(MINT_ADD_R8)
2334                         BINOP(f, +);
2335                         MINT_IN_BREAK;
2336                 MINT_IN_CASE(MINT_ADD1_I4)
2337                         ++sp [-1].data.i;
2338                         ++ip;
2339                         MINT_IN_BREAK;
2340                 MINT_IN_CASE(MINT_SUB_I4)
2341                         BINOP(i, -);
2342                         MINT_IN_BREAK;
2343                 MINT_IN_CASE(MINT_SUB_I8)
2344                         BINOP(l, -);
2345                         MINT_IN_BREAK;
2346                 MINT_IN_CASE(MINT_SUB_R8)
2347                         BINOP(f, -);
2348                         MINT_IN_BREAK;
2349                 MINT_IN_CASE(MINT_SUB1_I4)
2350                         --sp [-1].data.i;
2351                         ++ip;
2352                         MINT_IN_BREAK;
2353                 MINT_IN_CASE(MINT_MUL_I4)
2354                         BINOP(i, *);
2355                         MINT_IN_BREAK;
2356                 MINT_IN_CASE(MINT_MUL_I8)
2357                         BINOP(l, *);
2358                         MINT_IN_BREAK;
2359                 MINT_IN_CASE(MINT_MUL_R8)
2360                         BINOP(f, *);
2361                         MINT_IN_BREAK;
2362                 MINT_IN_CASE(MINT_DIV_I4)
2363                         if (sp [-1].data.i == 0)
2364                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2365                         if (sp [-1].data.i == (-1))
2366                                 THROW_EX (mono_get_exception_overflow (), ip);
2367                         BINOP(i, /);
2368                         MINT_IN_BREAK;
2369                 MINT_IN_CASE(MINT_DIV_I8)
2370                         if (sp [-1].data.l == 0)
2371                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2372                         if (sp [-1].data.l == (-1))
2373                                 THROW_EX (mono_get_exception_overflow (), ip);
2374                         BINOP(l, /);
2375                         MINT_IN_BREAK;
2376                 MINT_IN_CASE(MINT_DIV_R8)
2377                         BINOP(f, /);
2378                         MINT_IN_BREAK;
2379
2380 #define BINOP_CAST(datamem, op, type) \
2381         --sp; \
2382         sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2383         ++ip;
2384                 MINT_IN_CASE(MINT_DIV_UN_I4)
2385                         if (sp [-1].data.i == 0)
2386                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2387                         BINOP_CAST(i, /, guint32);
2388                         MINT_IN_BREAK;
2389                 MINT_IN_CASE(MINT_DIV_UN_I8)
2390                         if (sp [-1].data.l == 0)
2391                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2392                         BINOP_CAST(l, /, guint64);
2393                         MINT_IN_BREAK;
2394                 MINT_IN_CASE(MINT_REM_I4)
2395                         if (sp [-1].data.i == 0)
2396                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2397                         BINOP(i, %);
2398                         MINT_IN_BREAK;
2399                 MINT_IN_CASE(MINT_REM_I8)
2400                         if (sp [-1].data.l == 0)
2401                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2402                         BINOP(l, %);
2403                         MINT_IN_BREAK;
2404                 MINT_IN_CASE(MINT_REM_R8)
2405                         /* FIXME: what do we actually do here? */
2406                         --sp;
2407                         sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2408                         ++ip;
2409                         MINT_IN_BREAK;
2410                 MINT_IN_CASE(MINT_REM_UN_I4)
2411                         if (sp [-1].data.i == 0)
2412                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2413                         BINOP_CAST(i, %, guint32);
2414                         MINT_IN_BREAK;
2415                 MINT_IN_CASE(MINT_REM_UN_I8)
2416                         if (sp [-1].data.l == 0)
2417                                 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2418                         BINOP_CAST(l, %, guint64);
2419                         MINT_IN_BREAK;
2420                 MINT_IN_CASE(MINT_AND_I4)
2421                         BINOP(i, &);
2422                         MINT_IN_BREAK;
2423                 MINT_IN_CASE(MINT_AND_I8)
2424                         BINOP(l, &);
2425                         MINT_IN_BREAK;
2426                 MINT_IN_CASE(MINT_OR_I4)
2427                         BINOP(i, |);
2428                         MINT_IN_BREAK;
2429                 MINT_IN_CASE(MINT_OR_I8)
2430                         BINOP(l, |);
2431                         MINT_IN_BREAK;
2432                 MINT_IN_CASE(MINT_XOR_I4)
2433                         BINOP(i, ^);
2434                         MINT_IN_BREAK;
2435                 MINT_IN_CASE(MINT_XOR_I8)
2436                         BINOP(l, ^);
2437                         MINT_IN_BREAK;
2438
2439 #define SHIFTOP(datamem, op) \
2440         --sp; \
2441         sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2442         ++ip;
2443
2444                 MINT_IN_CASE(MINT_SHL_I4)
2445                         SHIFTOP(i, <<);
2446                         MINT_IN_BREAK;
2447                 MINT_IN_CASE(MINT_SHL_I8)
2448                         SHIFTOP(l, <<);
2449                         MINT_IN_BREAK;
2450                 MINT_IN_CASE(MINT_SHR_I4)
2451                         SHIFTOP(i, >>);
2452                         MINT_IN_BREAK;
2453                 MINT_IN_CASE(MINT_SHR_I8)
2454                         SHIFTOP(l, >>);
2455                         MINT_IN_BREAK;
2456                 MINT_IN_CASE(MINT_SHR_UN_I4)
2457                         --sp;
2458                         sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2459                         ++ip;
2460                         MINT_IN_BREAK;
2461                 MINT_IN_CASE(MINT_SHR_UN_I8)
2462                         --sp;
2463                         sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2464                         ++ip;
2465                         MINT_IN_BREAK;
2466                 MINT_IN_CASE(MINT_NEG_I4)
2467                         sp [-1].data.i = - sp [-1].data.i;
2468                         ++ip;
2469                         MINT_IN_BREAK;
2470                 MINT_IN_CASE(MINT_NEG_I8)
2471                         sp [-1].data.l = - sp [-1].data.l;
2472                         ++ip;
2473                         MINT_IN_BREAK;
2474                 MINT_IN_CASE(MINT_NEG_R8)
2475                         sp [-1].data.f = - sp [-1].data.f;
2476                         ++ip;
2477                         MINT_IN_BREAK;
2478                 MINT_IN_CASE(MINT_NOT_I4)
2479                         sp [-1].data.i = ~ sp [-1].data.i;
2480                         ++ip;
2481                         MINT_IN_BREAK;
2482                 MINT_IN_CASE(MINT_NOT_I8)
2483                         sp [-1].data.l = ~ sp [-1].data.l;
2484                         ++ip;
2485                         MINT_IN_BREAK;
2486                 MINT_IN_CASE(MINT_CONV_I1_I4)
2487                         sp [-1].data.i = (gint8)sp [-1].data.i;
2488                         ++ip;
2489                         MINT_IN_BREAK;
2490                 MINT_IN_CASE(MINT_CONV_I1_I8)
2491                         sp [-1].data.i = (gint8)sp [-1].data.l;
2492                         ++ip;
2493                         MINT_IN_BREAK;
2494                 MINT_IN_CASE(MINT_CONV_I1_R8)
2495                         sp [-1].data.i = (gint8)sp [-1].data.f;
2496                         ++ip;
2497                         MINT_IN_BREAK;
2498                 MINT_IN_CASE(MINT_CONV_U1_I4)
2499                         sp [-1].data.i = (guint8)sp [-1].data.i;
2500                         ++ip;
2501                         MINT_IN_BREAK;
2502                 MINT_IN_CASE(MINT_CONV_U1_I8)
2503                         sp [-1].data.i = (guint8)sp [-1].data.l;
2504                         ++ip;
2505                         MINT_IN_BREAK;
2506                 MINT_IN_CASE(MINT_CONV_U1_R8)
2507                         sp [-1].data.i = (guint8)sp [-1].data.f;
2508                         ++ip;
2509                         MINT_IN_BREAK;
2510                 MINT_IN_CASE(MINT_CONV_I2_I4)
2511                         sp [-1].data.i = (gint16)sp [-1].data.i;
2512                         ++ip;
2513                         MINT_IN_BREAK;
2514                 MINT_IN_CASE(MINT_CONV_I2_I8)
2515                         sp [-1].data.i = (gint16)sp [-1].data.l;
2516                         ++ip;
2517                         MINT_IN_BREAK;
2518                 MINT_IN_CASE(MINT_CONV_I2_R8)
2519                         sp [-1].data.i = (gint16)sp [-1].data.f;
2520                         ++ip;
2521                         MINT_IN_BREAK;
2522                 MINT_IN_CASE(MINT_CONV_U2_I4)
2523                         sp [-1].data.i = (guint16)sp [-1].data.i;
2524                         ++ip;
2525                         MINT_IN_BREAK;
2526                 MINT_IN_CASE(MINT_CONV_U2_I8)
2527                         sp [-1].data.i = (guint16)sp [-1].data.l;
2528                         ++ip;
2529                         MINT_IN_BREAK;
2530                 MINT_IN_CASE(MINT_CONV_U2_R8)
2531                         sp [-1].data.i = (guint16)sp [-1].data.f;
2532                         ++ip;
2533                         MINT_IN_BREAK;
2534                 MINT_IN_CASE(MINT_CONV_I4_R8)
2535                         sp [-1].data.i = (gint32)sp [-1].data.f;
2536                         ++ip;
2537                         MINT_IN_BREAK;
2538                 MINT_IN_CASE(MINT_CONV_U4_I8)
2539                 MINT_IN_CASE(MINT_CONV_I4_I8)
2540                         sp [-1].data.i = (gint32)sp [-1].data.l;
2541                         ++ip;
2542                         MINT_IN_BREAK;
2543                 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2544                         sp [-2].data.i = (gint32)sp [-2].data.l;
2545                         ++ip;
2546                         MINT_IN_BREAK;
2547                 MINT_IN_CASE(MINT_CONV_U4_R8)
2548                         sp [-1].data.i = (guint32)sp [-1].data.f;
2549                         ++ip;
2550                         MINT_IN_BREAK;
2551                 MINT_IN_CASE(MINT_CONV_I8_I4)
2552                         sp [-1].data.l = sp [-1].data.i;
2553                         ++ip;
2554                         MINT_IN_BREAK;
2555                 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2556                         sp [-2].data.l = sp [-2].data.i;
2557                         ++ip;
2558                         MINT_IN_BREAK;
2559                 MINT_IN_CASE(MINT_CONV_I8_U4)
2560                         sp [-1].data.l = (guint32)sp [-1].data.i;
2561                         ++ip;
2562                         MINT_IN_BREAK;
2563                 MINT_IN_CASE(MINT_CONV_I8_R8)
2564                         sp [-1].data.l = (gint64)sp [-1].data.f;
2565                         ++ip;
2566                         MINT_IN_BREAK;
2567                 MINT_IN_CASE(MINT_CONV_R4_I4)
2568                         sp [-1].data.f = (float)sp [-1].data.i;
2569                         ++ip;
2570                         MINT_IN_BREAK;
2571                 MINT_IN_CASE(MINT_CONV_R4_I8)
2572                         sp [-1].data.f = (float)sp [-1].data.l;
2573                         ++ip;
2574                         MINT_IN_BREAK;
2575                 MINT_IN_CASE(MINT_CONV_R4_R8)
2576                         sp [-1].data.f = (float)sp [-1].data.f;
2577                         ++ip;
2578                         MINT_IN_BREAK;
2579                 MINT_IN_CASE(MINT_CONV_R8_I4)
2580                         sp [-1].data.f = (double)sp [-1].data.i;
2581                         ++ip;
2582                         MINT_IN_BREAK;
2583                 MINT_IN_CASE(MINT_CONV_R8_I8)
2584                         sp [-1].data.f = (double)sp [-1].data.l;
2585                         ++ip;
2586                         MINT_IN_BREAK;
2587                 MINT_IN_CASE(MINT_CONV_U8_I4)
2588                         sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2589                         ++ip;
2590                         MINT_IN_BREAK;
2591                 MINT_IN_CASE(MINT_CONV_U8_R8)
2592                         sp [-1].data.l = (guint64)sp [-1].data.f;
2593                         ++ip;
2594                         MINT_IN_BREAK;
2595 #if 0
2596                 MINT_IN_CASE(MINT_CPOBJ) {
2597                         MonoClass *vtklass;
2598                         ++ip;
2599                         vtklass = rtm->data_items[READ32 (ip)];
2600                         ip += 2;
2601                         sp -= 2;
2602                         memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2603                         MINT_IN_BREAK;
2604                 }
2605 #endif
2606                 MINT_IN_CASE(MINT_LDOBJ) {
2607                         int size;
2608                         void *p;
2609                         c = rtm->data_items[* (guint16 *)(ip + 1)];
2610                         ip += 2;
2611                         if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2612                                 p = sp [-1].data.p;
2613                                 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2614                         } else {
2615                                 size = mono_class_value_size (c, NULL);
2616                                 p = sp [-1].data.p;
2617                                 sp [-1].data.p = vt_sp;
2618                                 memcpy(vt_sp, p, size);
2619                                 vt_sp += (size + 7) & ~7;
2620                         }
2621                         MINT_IN_BREAK;
2622                 }
2623                 MINT_IN_CASE(MINT_LDSTR)
2624                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2625                         ++sp;
2626                         ip += 2;
2627                         MINT_IN_BREAK;
2628                 MINT_IN_CASE(MINT_NEWOBJ) {
2629                         MonoClass *newobj_class;
2630                         MonoMethodSignature *csig;
2631                         stackval valuetype_this;
2632                         guint32 token;
2633                         stackval retval;
2634
2635                         frame->ip = ip;
2636
2637                         token = * (guint16 *)(ip + 1);
2638                         ip += 2;
2639
2640                         child_frame.runtime_method = rtm->data_items [token];
2641                         csig = mono_method_signature (child_frame.runtime_method->method);
2642                         newobj_class = child_frame.runtime_method->method->klass;
2643                         /*if (profiling_classes) {
2644                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2645                                 count++;
2646                                 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2647                         }*/
2648                                 
2649
2650                         if (newobj_class->parent == mono_defaults.array_class) {
2651                                 sp -= csig->param_count;
2652                                 o = ves_array_create (context->domain, newobj_class, csig, sp);
2653                                 goto array_constructed;
2654                         }
2655
2656                         /*
2657                          * First arg is the object.
2658                          */
2659                         if (newobj_class->valuetype) {
2660                                 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2661                                         child_frame.obj = vt_sp;
2662                                         valuetype_this.data.p = vt_sp;
2663                                 } else {
2664                                         memset (&valuetype_this, 0, sizeof (stackval));
2665                                         child_frame.obj = &valuetype_this;
2666                                 }
2667                         } else {
2668                                 if (newobj_class != mono_defaults.string_class) {
2669                                         context->managed_code = 0;
2670                                         o = mono_object_new_checked (context->domain, newobj_class, &error);
2671                                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2672                                         context->managed_code = 1;
2673                                         if (*abort_requested)
2674                                                 mono_thread_interruption_checkpoint ();
2675                                         child_frame.obj = o;
2676                                 } else {
2677                                         child_frame.retval = &retval;
2678                                 }
2679                         }
2680
2681                         if (csig->param_count) {
2682                                 sp -= csig->param_count;
2683                                 child_frame.stack_args = sp;
2684                         } else {
2685                                 child_frame.stack_args = NULL;
2686                         }
2687
2688                         g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2689
2690                         child_frame.ip = NULL;
2691                         child_frame.ex = NULL;
2692
2693                         ves_exec_method_with_context (&child_frame, context);
2694
2695                         context->current_frame = frame;
2696
2697                         if (child_frame.ex) {
2698                                 /*
2699                                  * An exception occurred, need to run finally, fault and catch handlers..
2700                                  */
2701                                 frame->ex = child_frame.ex;
2702                                 goto handle_finally;
2703                         }
2704                         /*
2705                          * a constructor returns void, but we need to return the object we created
2706                          */
2707 array_constructed:
2708                         if (newobj_class->valuetype && !newobj_class->enumtype) {
2709                                 *sp = valuetype_this;
2710                         } else if (newobj_class == mono_defaults.string_class) {
2711                                 *sp = retval;
2712                         } else {
2713                                 sp->data.p = o;
2714                         }
2715                         ++sp;
2716                         MINT_IN_BREAK;
2717                 }
2718                 MINT_IN_CASE(MINT_CASTCLASS)
2719                         c = rtm->data_items [*(guint16 *)(ip + 1)];
2720                         if ((o = sp [-1].data.p)) {
2721                                 if (c->marshalbyref) {
2722                                         MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2723                                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2724                                         if (!isinst_obj)
2725                                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
2726                                 } else {
2727                                         MonoVTable *vt = o->vtable;
2728                                         MonoClass *oklass = vt->klass;
2729                                         if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2730                                                 g_error ("FIXME: interface method lookup");
2731                                                 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2732                                                         THROW_EX (mono_get_exception_invalid_cast (), ip);
2733                                                 }
2734                                         } else if (c->rank) {
2735                                                 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2736                                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2737                                                 if (!isinst_obj)
2738                                                         THROW_EX (mono_get_exception_invalid_cast (), ip);
2739                                         } else if (!mono_class_has_parent (oklass, c)) {
2740                                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
2741                                         }
2742                                 }
2743                         }
2744                         ip += 2;
2745                         MINT_IN_BREAK;
2746                 MINT_IN_CASE(MINT_ISINST)
2747                         c = rtm->data_items [*(guint16 *)(ip + 1)];
2748                         if ((o = sp [-1].data.p)) {
2749                                 if (c->marshalbyref) {
2750                                         MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2751                                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2752                                         if (!isinst_obj)
2753                                                 sp [-1].data.p = NULL;
2754                                 } else {
2755                                         MonoVTable *vt = o->vtable;
2756                                         MonoClass *oklass = vt->klass;
2757                                         if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2758                                                 g_error ("FIXME: interface method lookup");
2759                                                 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2760                                                         sp [-1].data.p = NULL;
2761                                                 }
2762                                         } else if (c->rank) {
2763                                                 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2764                                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2765                                                 if (!isinst_obj)
2766                                                         sp [-1].data.p = NULL;
2767                                         } else if (!mono_class_has_parent (oklass, c)) {
2768                                                 sp [-1].data.p = NULL;
2769                                         }
2770                                 }
2771                         }
2772                         ip += 2;
2773                         MINT_IN_BREAK;
2774                 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2775                         sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2776                         ++ip;
2777                         MINT_IN_BREAK;
2778                 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2779                         sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2780                         ++ip;
2781                         MINT_IN_BREAK;
2782                 MINT_IN_CASE(MINT_UNBOX)
2783                         c = rtm->data_items[*(guint16 *)(ip + 1)];
2784                         
2785                         o = sp [-1].data.p;
2786                         if (!o)
2787                                 THROW_EX (mono_get_exception_null_reference(), ip);
2788
2789                         MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2790                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2791                         if (!(isinst_obj ||
2792                                   ((o->vtable->klass->rank == 0) && 
2793                                    (o->vtable->klass->element_class == c->element_class))))
2794                                 THROW_EX (mono_get_exception_invalid_cast (), ip);
2795
2796                         sp [-1].data.p = (char *)o + sizeof (MonoObject);
2797                         ip += 2;
2798                         MINT_IN_BREAK;
2799                 MINT_IN_CASE(MINT_THROW)
2800                         --sp;
2801                         frame->ex_handler = NULL;
2802                         if (!sp->data.p)
2803                                 sp->data.p = mono_get_exception_null_reference ();
2804                         THROW_EX ((MonoException *)sp->data.p, ip);
2805                         MINT_IN_BREAK;
2806                 MINT_IN_CASE(MINT_LDFLDA)
2807                         o = sp [-1].data.p;
2808                         if (!o)
2809                                 THROW_EX (mono_get_exception_null_reference (), ip);
2810                         sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2811                         ip += 2;
2812                         MINT_IN_BREAK;
2813                 MINT_IN_CASE(MINT_CKNULL)
2814                         o = sp [-1].data.p;
2815                         if (!o)
2816                                 THROW_EX (mono_get_exception_null_reference (), ip);
2817                         ++ip;
2818                         MINT_IN_BREAK;
2819
2820 #define LDFLD(datamem, fieldtype) \
2821         o = sp [-1].data.p; \
2822         if (!o) \
2823                 THROW_EX (mono_get_exception_null_reference (), ip); \
2824         sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2825         ip += 2;
2826
2827                 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2828                 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2829                 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2830                 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2831                 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2832                 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2833                 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2834                 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2835                 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2836                 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2837
2838                 MINT_IN_CASE(MINT_LDFLD_VT)
2839                         o = sp [-1].data.p;
2840                         if (!o)
2841                                 THROW_EX (mono_get_exception_null_reference (), ip);
2842                         i32 = READ32(ip + 2);
2843                         sp [-1].data.p = vt_sp;
2844                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2845                         vt_sp += (i32 + 7) & ~7;
2846                         ip += 4;
2847                         MINT_IN_BREAK;
2848
2849                 MINT_IN_CASE(MINT_LDRMFLD) {
2850                         gpointer tmp;
2851                         MonoClassField *field;
2852                         char *addr;
2853
2854                         o = sp [-1].data.p;
2855                         if (!o)
2856                                 THROW_EX (mono_get_exception_null_reference (), ip);
2857                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2858                         ip += 2;
2859                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2860                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2861
2862                                 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2863                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2864                         } else {
2865                                 addr = (char*)o + field->offset;
2866                         }                               
2867
2868                         stackval_from_data (field->type, &sp [-1], addr, FALSE);
2869                         MINT_IN_BREAK;
2870                 }
2871
2872                 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2873                         MonoClassField *field;
2874                         char *addr;
2875                         gpointer tmp;
2876
2877                         o = sp [-1].data.p;
2878                         if (!o)
2879                                 THROW_EX (mono_get_exception_null_reference (), ip);
2880                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2881                         i32 = READ32(ip + 2);
2882                         ip += 4;
2883                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2884                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2885                                 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2886                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2887                         } else {
2888                                 addr = (char*)o + field->offset;
2889                         }                               
2890
2891                         sp [-1].data.p = vt_sp;
2892                         memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2893                         vt_sp += (i32 + 7) & ~7;
2894                         memcpy(sp [-1].data.p, addr, i32);
2895                         MINT_IN_BREAK;
2896                 }
2897
2898 #define STFLD(datamem, fieldtype) \
2899         o = sp [-2].data.p; \
2900         if (!o) \
2901                 THROW_EX (mono_get_exception_null_reference (), ip); \
2902         sp -= 2; \
2903         * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2904         ip += 2;
2905
2906                 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2907                 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2908                 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2909                 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2910                 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2911                 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2912                 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2913                 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2914                 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2915                 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2916
2917                 MINT_IN_CASE(MINT_STFLD_VT)
2918                         o = sp [-2].data.p;
2919                         if (!o)
2920                                 THROW_EX (mono_get_exception_null_reference (), ip);
2921                         i32 = READ32(ip + 2);
2922                         sp -= 2;
2923                         memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2924                         vt_sp -= (i32 + 7) & ~7;
2925                         ip += 4;
2926                         MINT_IN_BREAK;
2927
2928                 MINT_IN_CASE(MINT_STRMFLD) {
2929                         MonoClassField *field;
2930
2931                         o = sp [-2].data.p;
2932                         if (!o)
2933                                 THROW_EX (mono_get_exception_null_reference (), ip);
2934                         
2935                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2936                         ip += 2;
2937
2938                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2939                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2940                                 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2941                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2942                         } else
2943                                 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
2944
2945                         sp -= 2;
2946                         MINT_IN_BREAK;
2947                 }
2948                 MINT_IN_CASE(MINT_STRMFLD_VT) {
2949                         MonoClassField *field;
2950
2951                         o = sp [-2].data.p;
2952                         if (!o)
2953                                 THROW_EX (mono_get_exception_null_reference (), ip);
2954                         field = rtm->data_items[* (guint16 *)(ip + 1)];
2955                         i32 = READ32(ip + 2);
2956                         ip += 4;
2957
2958                         if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2959                                 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2960                                 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2961                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2962                         } else
2963                                 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
2964
2965                         sp -= 2;
2966                         vt_sp -= (i32 + 7) & ~7;
2967                         MINT_IN_BREAK;
2968                 }
2969                 MINT_IN_CASE(MINT_LDSFLDA) {
2970                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2971                         MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
2972                         gpointer addr;
2973
2974                         if (!vt->initialized) {
2975                                 frame->ip = ip;
2976                                 mono_runtime_class_init_full (vt, &error);
2977                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2978                         }
2979                         ip += 2;
2980
2981                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2982                                 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2983                         else
2984                                 sp->data.p = (char*)(vt->vtable) + field->offset;
2985                         ++sp;
2986                         MINT_IN_BREAK;
2987                 }
2988                 MINT_IN_CASE(MINT_LDSFLD) {
2989                         MonoVTable *vt;
2990                         MonoClassField *field;
2991                         gpointer addr;
2992
2993                         field = rtm->data_items[*(guint16 *)(ip + 1)];
2994                         vt = rtm->data_items [*(guint16 *)(ip + 2)];
2995                         if (!vt->initialized) {
2996                                 frame->ip = ip;
2997                                 mono_runtime_class_init_full (vt, &error);
2998                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2999                         }
3000                         ip += 3;
3001                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3002                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3003                         else
3004                                 addr = (char*)(vt->vtable) + field->offset;
3005
3006                         stackval_from_data (field->type, sp, addr, FALSE);
3007                         ++sp;
3008                         MINT_IN_BREAK;
3009                 }
3010                 MINT_IN_CASE(MINT_LDSFLD_I4) {
3011                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3012                         MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3013                         if (!vt->initialized) {
3014                                 frame->ip = ip;
3015                                 mono_runtime_class_init_full (vt, &error);
3016                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3017                         }
3018                         ip += 3;
3019                         sp->data.i = * (gint32 *)((char*)(vt->vtable) + field->offset);
3020                         ++sp;
3021                         MINT_IN_BREAK;
3022                 }
3023                 MINT_IN_CASE(MINT_LDSFLD_O) {
3024                         MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3025                         MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3026                         if (!vt->initialized) {
3027                                 frame->ip = ip;
3028                                 mono_runtime_class_init_full (vt, &error);
3029                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3030                         }
3031                         ip += 3;
3032                         sp->data.p = * (gpointer *)((char*)(vt->vtable) + field->offset);
3033                         ++sp;
3034                         MINT_IN_BREAK;
3035                 }
3036                 MINT_IN_CASE(MINT_LDSFLD_VT) {
3037                         MonoVTable *vt;
3038                         MonoClassField *field;
3039                         guint32 token;
3040                         gpointer addr;
3041                         int size;
3042
3043                         token = * (guint16 *)(ip + 1);
3044                         size = READ32(ip + 2);
3045                         field = rtm->data_items[token];
3046                         ip += 4;
3047                                                 
3048                         vt = mono_class_vtable (context->domain, field->parent);
3049                         if (!vt->initialized) {
3050                                 frame->ip = ip - 2;
3051                                 mono_runtime_class_init_full (vt, &error);
3052                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3053                         }
3054                         
3055                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3056                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3057                         else
3058                                 addr = (char*)(vt->vtable) + field->offset;
3059
3060                         sp->data.p = vt_sp;
3061                         vt_sp += (size + 7) & ~7;
3062                         stackval_from_data (field->type, sp, addr, FALSE);
3063                         ++sp;
3064                         MINT_IN_BREAK;
3065                 }
3066                 MINT_IN_CASE(MINT_STSFLD) {
3067                         MonoVTable *vt;
3068                         MonoClassField *field;
3069                         guint32 token;
3070                         gpointer addr;
3071
3072                         token = * (guint16 *)(ip + 1);
3073                         field = rtm->data_items[token];
3074                         ip += 2;
3075                         --sp;
3076
3077                         vt = mono_class_vtable (context->domain, field->parent);
3078                         if (!vt->initialized) {
3079                                 frame->ip = ip - 2;
3080                                 mono_runtime_class_init_full (vt, &error);
3081                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3082                         }
3083                         
3084                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3085                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3086                         else
3087                                 addr = (char*)(vt->vtable) + field->offset;
3088
3089                         stackval_to_data (field->type, sp, addr, FALSE);
3090                         MINT_IN_BREAK;
3091                 }
3092                 MINT_IN_CASE(MINT_STSFLD_VT) {
3093                         MonoVTable *vt;
3094                         MonoClassField *field;
3095                         guint32 token;
3096                         gpointer addr;
3097                         int size;
3098
3099                         token = * (guint16 *)(ip + 1);
3100                         size = READ32(ip + 2);
3101                         field = rtm->data_items[token];
3102                         ip += 4;
3103                                                 
3104                         vt = mono_class_vtable (context->domain, field->parent);
3105                         if (!vt->initialized) {
3106                                 frame->ip = ip - 2;
3107                                 mono_runtime_class_init_full (vt, &error);
3108                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3109                         }
3110                         
3111                         if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3112                                 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3113                         else
3114                                 addr = (char*)(vt->vtable) + field->offset;
3115                         --sp;
3116                         stackval_to_data (field->type, sp, addr, FALSE);
3117                         vt_sp -= (size + 7) & ~7;
3118                         MINT_IN_BREAK;
3119                 }
3120                 MINT_IN_CASE(MINT_STOBJ_VT) {
3121                         int size;
3122                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3123                         ip += 2;
3124                         size = mono_class_value_size (c, NULL);
3125                         memcpy(sp [-2].data.p, sp [-1].data.p, size);
3126                         vt_sp -= (size + 7) & ~7;
3127                         sp -= 2;
3128                         MINT_IN_BREAK;
3129                 }
3130                 MINT_IN_CASE(MINT_STOBJ) {
3131                         int size;
3132                         c = rtm->data_items[* (guint16 *)(ip + 1)];
3133                         ip += 2;
3134                         size = mono_class_value_size (c, NULL);
3135                         memcpy(sp [-2].data.p, &sp [-1].data, size);
3136                         sp -= 2;
3137                         MINT_IN_BREAK;
3138                 }
3139                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3140                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3141                                 THROW_EX (mono_get_exception_overflow (), ip);
3142                         sp [-1].data.i = (guint32)sp [-1].data.f;
3143                         ++ip;
3144                         MINT_IN_BREAK;
3145                 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3146                         if (sp [-1].data.i < 0)
3147                                 THROW_EX (mono_get_exception_overflow (), ip);
3148                         sp [-1].data.l = sp [-1].data.i;
3149                         ++ip;
3150                         MINT_IN_BREAK;
3151                 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3152                 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3153                         if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3154                                 THROW_EX (mono_get_exception_overflow (), ip);
3155                         sp [-1].data.l = (guint64)sp [-1].data.f;
3156                         ++ip;
3157                         MINT_IN_BREAK;
3158                 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3159                         if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3160                                 THROW_EX (mono_get_exception_overflow (), ip);
3161                         sp [-1].data.l = (gint64)sp [-1].data.f;
3162                         ++ip;
3163                         MINT_IN_BREAK;
3164                 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3165                         if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3166                                 THROW_EX (mono_get_exception_overflow (), ip);
3167                         sp [-1].data.i = (mono_u)sp [-1].data.l;
3168                         ++ip;
3169                         MINT_IN_BREAK;
3170                 MINT_IN_CASE(MINT_BOX)
3171                         c = rtm->data_items [* (guint16 *)(ip + 1)];
3172
3173                         if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3174                                 int size = mono_class_value_size (c, NULL);
3175                                 sp [-1].data.p = mono_value_box_checked (context->domain, c, sp [-1].data.p, &error);
3176                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3177                                 size = (size + 7) & ~7;
3178                                 vt_sp -= size;
3179                         }                               
3180                         else {
3181                                 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3182                                 sp [-1].data.p = mono_value_box_checked (context->domain, c, &sp [-1], &error);
3183                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3184                         }
3185                         ip += 2;
3186                         MINT_IN_BREAK;
3187                 MINT_IN_CASE(MINT_NEWARR)
3188                         sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3189                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3190                         ip += 2;
3191                         /*if (profiling_classes) {
3192                                 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3193                                 count++;
3194                                 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3195                         }*/
3196
3197                         MINT_IN_BREAK;
3198                 MINT_IN_CASE(MINT_LDLEN)
3199                         o = sp [-1].data.p;
3200                         if (!o)
3201                                 THROW_EX (mono_get_exception_null_reference (), ip);
3202                         sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3203                         ++ip;
3204                         MINT_IN_BREAK;
3205                 MINT_IN_CASE(MINT_GETCHR) {
3206                         MonoString *s;
3207                         s = sp [-2].data.p;
3208                         if (!s)
3209                                 THROW_EX (mono_get_exception_null_reference (), ip);
3210                         i32 = sp [-1].data.i;
3211                         if (i32 < 0 || i32 >= mono_string_length (s))
3212                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3213                         --sp;
3214                         sp [-1].data.i = mono_string_chars(s)[i32];
3215                         ++ip;
3216                         MINT_IN_BREAK;
3217                 }
3218                 MINT_IN_CASE(MINT_STRLEN)
3219                         ++ip;
3220                         sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3221                         MINT_IN_BREAK;
3222                 MINT_IN_CASE(MINT_ARRAY_RANK)
3223                         o = sp [-1].data.p;
3224                         if (!o)
3225                                 THROW_EX (mono_get_exception_null_reference (), ip);
3226                         sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3227                         ip++;
3228                         MINT_IN_BREAK;
3229                 MINT_IN_CASE(MINT_LDELEMA) {
3230                         guint32 esize;
3231                         mono_u aindex;
3232                         
3233                         /*token = READ32 (ip)*/;
3234                         ip += 2;
3235                         sp -= 2;
3236
3237                         o = sp [0].data.p;
3238
3239                         aindex = sp [1].data.i;
3240                         if (aindex >= mono_array_length ((MonoArray *) o))
3241                                 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3242
3243                         /* check the array element corresponds to token */
3244                         esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3245                         
3246                         sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3247                         ++sp;
3248                         MINT_IN_BREAK;
3249                 }
3250                 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3251                 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3252                 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3253                 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3254                 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3255                 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3256                 MINT_IN_CASE(MINT_LDELEM_I8)  /* fall through */
3257                 MINT_IN_CASE(MINT_LDELEM_I)  /* fall through */
3258                 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3259                 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3260                 MINT_IN_CASE(MINT_LDELEM_REF) {
3261                         MonoArray *o;
3262                         mono_u aindex;
3263
3264                         sp -= 2;
3265
3266                         o = sp [0].data.p;
3267                         if (!o)
3268                                 THROW_EX (mono_get_exception_null_reference (), ip);
3269
3270                         aindex = sp [1].data.i;
3271                         if (aindex >= mono_array_length (o))
3272                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3273
3274                         /*
3275                          * FIXME: throw mono_get_exception_array_type_mismatch () if needed 
3276                          */
3277                         switch (*ip) {
3278                         case MINT_LDELEM_I1:
3279                                 sp [0].data.i = mono_array_get (o, gint8, aindex);
3280                                 break;
3281                         case MINT_LDELEM_U1:
3282                                 sp [0].data.i = mono_array_get (o, guint8, aindex);
3283                                 break;
3284                         case MINT_LDELEM_I2:
3285                                 sp [0].data.i = mono_array_get (o, gint16, aindex);
3286                                 break;
3287                         case MINT_LDELEM_U2:
3288                                 sp [0].data.i = mono_array_get (o, guint16, aindex);
3289                                 break;
3290                         case MINT_LDELEM_I:
3291                                 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3292                                 break;
3293                         case MINT_LDELEM_I4:
3294                                 sp [0].data.i = mono_array_get (o, gint32, aindex);
3295                                 break;
3296                         case MINT_LDELEM_U4:
3297                                 sp [0].data.i = mono_array_get (o, guint32, aindex);
3298                                 break;
3299                         case MINT_LDELEM_I8:
3300                                 sp [0].data.l = mono_array_get (o, guint64, aindex);
3301                                 break;
3302                         case MINT_LDELEM_R4:
3303                                 sp [0].data.f = mono_array_get (o, float, aindex);
3304                                 break;
3305                         case MINT_LDELEM_R8:
3306                                 sp [0].data.f = mono_array_get (o, double, aindex);
3307                                 break;
3308                         case MINT_LDELEM_REF:
3309                                 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3310                                 break;
3311                         default:
3312                                 ves_abort();
3313                         }
3314
3315                         ++ip;
3316                         ++sp;
3317                         MINT_IN_BREAK;
3318                 }
3319                 MINT_IN_CASE(MINT_STELEM_I)  /* fall through */
3320                 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */ 
3321                 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3322                 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3323                 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3324                 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3325                 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3326                 MINT_IN_CASE(MINT_STELEM_REF) {
3327                         mono_u aindex;
3328
3329                         sp -= 3;
3330
3331                         o = sp [0].data.p;
3332                         if (!o)
3333                                 THROW_EX (mono_get_exception_null_reference (), ip);
3334
3335                         aindex = sp [1].data.i;
3336                         if (aindex >= mono_array_length ((MonoArray *)o))
3337                                 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3338
3339                         switch (*ip) {
3340                         case MINT_STELEM_I:
3341                                 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3342                                 break;
3343                         case MINT_STELEM_I1:
3344                                 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3345                                 break;
3346                         case MINT_STELEM_I2:
3347                                 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3348                                 break;
3349                         case MINT_STELEM_I4:
3350                                 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3351                                 break;
3352                         case MINT_STELEM_I8:
3353                                 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3354                                 break;
3355                         case MINT_STELEM_R4:
3356                                 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3357                                 break;
3358                         case MINT_STELEM_R8:
3359                                 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3360                                 break;
3361                         case MINT_STELEM_REF: {
3362                                 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3363                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3364                                 if (sp [2].data.p && !isinst_obj)
3365                                         THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3366                                 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3367                         } break;
3368                         default:
3369                                 ves_abort();
3370                         }
3371
3372                         ++ip;
3373                         MINT_IN_BREAK;
3374                 }
3375                 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3376                         if (sp [-1].data.i < 0)
3377                                 THROW_EX (mono_get_exception_overflow (), ip);
3378                         ++ip;
3379                         MINT_IN_BREAK;
3380                 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3381                         if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3382                                 THROW_EX (mono_get_exception_overflow (), ip);
3383                         sp [-1].data.i = (gint32) sp [-1].data.l;
3384                         ++ip;
3385                         MINT_IN_BREAK;
3386                 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3387                         if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3388                                 THROW_EX (mono_get_exception_overflow (), ip);
3389                         sp [-1].data.i = (gint32) sp [-1].data.f;
3390                         ++ip;
3391                         MINT_IN_BREAK;
3392                 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3393                         if (sp [-1].data.i < 0)
3394                                 THROW_EX (mono_get_exception_overflow (), ip);
3395                         ++ip;
3396                         MINT_IN_BREAK;
3397                 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3398                         if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3399                                 THROW_EX (mono_get_exception_overflow (), ip);
3400                         sp [-1].data.i = (guint32) sp [-1].data.l;
3401                         ++ip;
3402                         MINT_IN_BREAK;
3403                 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3404                         if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3405                                 THROW_EX (mono_get_exception_overflow (), ip);
3406                         sp [-1].data.i = (guint32) sp [-1].data.f;
3407                         ++ip;
3408                         MINT_IN_BREAK;
3409                 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3410                         if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3411                                 THROW_EX (mono_get_exception_overflow (), ip);
3412                         ++ip;
3413                         MINT_IN_BREAK;
3414                 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3415                         if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3416                                 THROW_EX (mono_get_exception_overflow (), ip);
3417                         sp [-1].data.i = (gint16) sp [-1].data.l;
3418                         ++ip;
3419                         MINT_IN_BREAK;
3420                 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3421                         if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3422                                 THROW_EX (mono_get_exception_overflow (), ip);
3423                         sp [-1].data.i = (gint16) sp [-1].data.f;
3424                         ++ip;
3425                         MINT_IN_BREAK;
3426                 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3427                         if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3428                                 THROW_EX (mono_get_exception_overflow (), ip);
3429                         ++ip;
3430                         MINT_IN_BREAK;
3431                 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3432                         if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3433                                 THROW_EX (mono_get_exception_overflow (), ip);
3434                         sp [-1].data.i = (guint16) sp [-1].data.l;
3435                         ++ip;
3436                         MINT_IN_BREAK;
3437                 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3438                         if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3439                                 THROW_EX (mono_get_exception_overflow (), ip);
3440                         sp [-1].data.i = (guint16) sp [-1].data.f;
3441                         ++ip;
3442                         MINT_IN_BREAK;
3443                 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3444                         if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3445                                 THROW_EX (mono_get_exception_overflow (), ip);
3446                         ++ip;
3447                         MINT_IN_BREAK;
3448                 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3449                         if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3450                                 THROW_EX (mono_get_exception_overflow (), ip);
3451                         sp [-1].data.i = (gint8) sp [-1].data.l;
3452                         ++ip;
3453                         MINT_IN_BREAK;
3454                 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3455                         if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3456                                 THROW_EX (mono_get_exception_overflow (), ip);
3457                         sp [-1].data.i = (gint8) sp [-1].data.f;
3458                         ++ip;
3459                         MINT_IN_BREAK;
3460                 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3461                         if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3462                                 THROW_EX (mono_get_exception_overflow (), ip);
3463                         ++ip;
3464                         MINT_IN_BREAK;
3465                 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3466                         if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3467                                 THROW_EX (mono_get_exception_overflow (), ip);
3468                         sp [-1].data.i = (guint8) sp [-1].data.l;
3469                         ++ip;
3470                         MINT_IN_BREAK;
3471                 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3472                         if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3473                                 THROW_EX (mono_get_exception_overflow (), ip);
3474                         sp [-1].data.i = (guint8) sp [-1].data.f;
3475                         ++ip;
3476                         MINT_IN_BREAK;
3477 #if 0
3478                 MINT_IN_CASE(MINT_LDELEM) 
3479                 MINT_IN_CASE(MINT_STELEM) 
3480                 MINT_IN_CASE(MINT_UNBOX_ANY) 
3481
3482                 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3483 #endif
3484                 MINT_IN_CASE(MINT_CKFINITE)
3485                         if (!isfinite(sp [-1].data.f))
3486                                 THROW_EX (mono_get_exception_arithmetic (), ip);
3487                         ++ip;
3488                         MINT_IN_BREAK;
3489 #if 0
3490                 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3491 #endif
3492                 MINT_IN_CASE(MINT_LDTOKEN)
3493                         sp->data.p = vt_sp;
3494                         vt_sp += 8;
3495                         * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3496                         ip += 2;
3497                         ++sp;
3498                         MINT_IN_BREAK;
3499                 MINT_IN_CASE(MINT_ADD_OVF_I4)
3500                         if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3501                                 THROW_EX (mono_get_exception_overflow (), ip);
3502                         BINOP(i, +);
3503                         MINT_IN_BREAK;
3504                 MINT_IN_CASE(MINT_ADD_OVF_I8)
3505                         if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3506                                 THROW_EX (mono_get_exception_overflow (), ip);
3507                         BINOP(l, +);
3508                         MINT_IN_BREAK;
3509                 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3510                         if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3511                                 THROW_EX (mono_get_exception_overflow (), ip);
3512                         BINOP_CAST(i, +, guint32);
3513                         MINT_IN_BREAK;
3514                 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3515                         if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3516                                 THROW_EX (mono_get_exception_overflow (), ip);
3517                         BINOP_CAST(l, +, guint64);
3518                         MINT_IN_BREAK;
3519                 MINT_IN_CASE(MINT_MUL_OVF_I4)
3520                         if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3521                                 THROW_EX (mono_get_exception_overflow (), ip);
3522                         BINOP(i, *);
3523                         MINT_IN_BREAK;
3524                 MINT_IN_CASE(MINT_MUL_OVF_I8)
3525                         if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3526                                 THROW_EX (mono_get_exception_overflow (), ip);
3527                         BINOP(l, *);
3528                         MINT_IN_BREAK;
3529                 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3530                         if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3531                                 THROW_EX (mono_get_exception_overflow (), ip);
3532                         BINOP_CAST(i, *, guint32);
3533                         MINT_IN_BREAK;
3534                 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3535                         if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3536                                 THROW_EX (mono_get_exception_overflow (), ip);
3537                         BINOP_CAST(l, *, guint64);
3538                         MINT_IN_BREAK;
3539                 MINT_IN_CASE(MINT_SUB_OVF_I4)
3540                         if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3541                                 THROW_EX (mono_get_exception_overflow (), ip);
3542                         BINOP(i, -);
3543                         MINT_IN_BREAK;
3544                 MINT_IN_CASE(MINT_SUB_OVF_I8)
3545                         if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3546                                 THROW_EX (mono_get_exception_overflow (), ip);
3547                         BINOP(l, -);
3548                         MINT_IN_BREAK;
3549                 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3550                         if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3551                                 THROW_EX (mono_get_exception_overflow (), ip);
3552                         BINOP_CAST(i, -, guint32);
3553                         MINT_IN_BREAK;
3554                 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3555                         if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3556                                 THROW_EX (mono_get_exception_overflow (), ip);
3557                         BINOP_CAST(l, -, guint64);
3558                         MINT_IN_BREAK;
3559                 MINT_IN_CASE(MINT_ENDFINALLY)
3560                         if (finally_ips) {
3561                                 ip = finally_ips->data;
3562                                 finally_ips = g_slist_remove (finally_ips, ip);
3563                                 goto main_loop;
3564                         }
3565                         if (frame->ex)
3566                                 goto handle_fault;
3567                         ves_abort();
3568                         MINT_IN_BREAK;
3569                 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3570                 MINT_IN_CASE(MINT_LEAVE_S)
3571                         while (sp > frame->stack) {
3572                                 --sp;
3573                         }
3574                         frame->ip = ip;
3575                         if (*ip == MINT_LEAVE_S) {
3576                                 ip += (short) *(ip + 1);
3577                         } else {
3578                                 ip += (gint32) READ32 (ip + 1);
3579                         }
3580                         endfinally_ip = ip;
3581                         if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3582                                 frame->ex_handler = NULL;
3583                                 frame->ex = NULL;
3584                         }
3585                         goto handle_finally;
3586                         MINT_IN_BREAK;
3587                 MINT_IN_CASE(MINT_ICALL_V_V) 
3588                 MINT_IN_CASE(MINT_ICALL_P_V) 
3589                 MINT_IN_CASE(MINT_ICALL_P_P)
3590                 MINT_IN_CASE(MINT_ICALL_PP_V)
3591                 MINT_IN_CASE(MINT_ICALL_PI_V)
3592                 MINT_IN_CASE(MINT_ICALL_PP_P)
3593                 MINT_IN_CASE(MINT_ICALL_PI_P)
3594                 MINT_IN_CASE(MINT_ICALL_PPP_V)
3595                 MINT_IN_CASE(MINT_ICALL_PPI_V)
3596                         sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3597                         if (frame->ex != NULL)
3598                                 goto handle_exception;
3599                         ip += 2;
3600                         MINT_IN_BREAK;
3601                 MINT_IN_CASE(MINT_MONO_LDPTR) 
3602                         sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3603                         ip += 2;
3604                         ++sp;
3605                         MINT_IN_BREAK;
3606                 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3607                         sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3608                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3609                         ip += 2;
3610                         sp++;
3611                         MINT_IN_BREAK;
3612                 MINT_IN_CASE(MINT_MONO_FREE)
3613                         ++ip;
3614                         --sp;
3615                         g_error ("that doesn't seem right");
3616                         g_free (sp->data.p);
3617                         MINT_IN_BREAK;
3618                 MINT_IN_CASE(MINT_MONO_RETOBJ)
3619                         ++ip;
3620                         sp--;
3621                         stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3622                              mono_method_signature (frame->runtime_method->method)->pinvoke);
3623                         if (sp > frame->stack)
3624                                 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3625                         goto exit_frame;
3626
3627 #define RELOP(datamem, op) \
3628         --sp; \
3629         sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3630         ++ip;
3631                 MINT_IN_CASE(MINT_CEQ_I4)
3632                         RELOP(i, ==);
3633                         MINT_IN_BREAK;
3634                 MINT_IN_CASE(MINT_CEQ0_I4)
3635                         sp [-1].data.i = (sp [-1].data.i == 0);
3636                         ++ip;
3637                         MINT_IN_BREAK;
3638                 MINT_IN_CASE(MINT_CEQ_I8)
3639                         RELOP(l, ==);
3640                         MINT_IN_BREAK;
3641                 MINT_IN_CASE(MINT_CEQ_R8)
3642                         --sp; 
3643                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3644                                 sp [-1].data.i = 0;
3645                         else
3646                                 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3647                         ++ip;
3648                         MINT_IN_BREAK;
3649                 MINT_IN_CASE(MINT_CGT_I4)
3650                         RELOP(i, >);
3651                         MINT_IN_BREAK;
3652                 MINT_IN_CASE(MINT_CGT_I8)
3653                         RELOP(l, >);
3654                         MINT_IN_BREAK;
3655                 MINT_IN_CASE(MINT_CGT_R8)
3656                         --sp; 
3657                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3658                                 sp [-1].data.i = 0;
3659                         else
3660                                 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3661                         ++ip;
3662                         MINT_IN_BREAK;
3663
3664 #define RELOP_CAST(datamem, op, type) \
3665         --sp; \
3666         sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3667         ++ip;
3668
3669                 MINT_IN_CASE(MINT_CGT_UN_I4)
3670                         RELOP_CAST(i, >, guint32);
3671                         MINT_IN_BREAK;
3672                 MINT_IN_CASE(MINT_CGT_UN_I8)
3673                         RELOP_CAST(l, >, guint64);
3674                         MINT_IN_BREAK;
3675                 MINT_IN_CASE(MINT_CGT_UN_R8)
3676                         --sp; 
3677                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3678                                 sp [-1].data.i = 1;
3679                         else
3680                                 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3681                         ++ip;
3682                         MINT_IN_BREAK;
3683                 MINT_IN_CASE(MINT_CLT_I4)
3684                         RELOP(i, <);
3685                         MINT_IN_BREAK;
3686                 MINT_IN_CASE(MINT_CLT_I8)
3687                         RELOP(l, <);
3688                         MINT_IN_BREAK;
3689                 MINT_IN_CASE(MINT_CLT_R8)
3690                         --sp; 
3691                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3692                                 sp [-1].data.i = 0;
3693                         else
3694                                 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3695                         ++ip;
3696                         MINT_IN_BREAK;
3697                 MINT_IN_CASE(MINT_CLT_UN_I4)
3698                         RELOP_CAST(i, <, guint32);
3699                         MINT_IN_BREAK;
3700                 MINT_IN_CASE(MINT_CLT_UN_I8)
3701                         RELOP_CAST(l, <, guint64);
3702                         MINT_IN_BREAK;
3703                 MINT_IN_CASE(MINT_CLT_UN_R8)
3704                         --sp; 
3705                         if (isunordered (sp [-1].data.f, sp [0].data.f))
3706                                 sp [-1].data.i = 1;
3707                         else
3708                                 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3709                         ++ip;
3710                         MINT_IN_BREAK;
3711                 MINT_IN_CASE(MINT_LDFTN) {
3712                         sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3713                         ++sp;
3714                         ip += 2;
3715                         MINT_IN_BREAK;
3716                 }
3717                 MINT_IN_CASE(MINT_LDVIRTFTN) {
3718                         RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3719                         ip += 2;
3720                         --sp;
3721                         if (!sp->data.p)
3722                                 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3723                                 
3724                         sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3725                         ++sp;
3726                         MINT_IN_BREAK;
3727                 }
3728
3729                 MINT_IN_CASE(MINT_LDTHIS)
3730                         sp->data.p = frame->obj;
3731                         ++ip;
3732                         ++sp; 
3733                         MINT_IN_BREAK;
3734                 MINT_IN_CASE(MINT_STTHIS)
3735                         --sp; 
3736                         frame->obj = sp->data.p;
3737                         ++ip;
3738                         MINT_IN_BREAK;
3739                 MINT_IN_CASE(MINT_LDTHISA)
3740                         sp->data.p = &frame->obj;
3741                         ++ip;
3742                         ++sp; 
3743                         MINT_IN_BREAK;
3744
3745 #define LDARG(datamem, argtype) \
3746         sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3747         ip += 2; \
3748         ++sp; 
3749         
3750                 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3751                 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3752                 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3753                 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3754                 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3755                 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3756                 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3757                 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3758                 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3759                 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3760
3761                 MINT_IN_CASE(MINT_LDARG_VT)
3762                         sp->data.p = vt_sp;
3763                         i32 = READ32(ip + 2);
3764                         memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3765                         vt_sp += (i32 + 7) & ~7;
3766                         ip += 4;
3767                         ++sp;
3768                         MINT_IN_BREAK;
3769
3770 #define STARG(datamem, argtype) \
3771         --sp; \
3772         * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3773         ip += 2; \
3774         
3775                 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3776                 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3777                 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3778                 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3779                 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3780                 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3781                 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3782                 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3783                 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3784                 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3785
3786                 MINT_IN_CASE(MINT_STARG_VT) 
3787                         i32 = READ32(ip + 2);
3788                         --sp;
3789                         memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3790                         vt_sp -= (i32 + 7) & ~7;
3791                         ip += 4;
3792                         MINT_IN_BREAK;
3793
3794 #define STINARG(datamem, argtype) \
3795         do { \
3796                 int n = * (guint16 *)(ip + 1); \
3797                 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3798                 ip += 2; \
3799         } while (0)
3800         
3801                 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3802                 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3803                 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3804                 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3805                 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3806                 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3807                 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3808                 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3809                 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3810                 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3811
3812                 MINT_IN_CASE(MINT_STINARG_VT) {
3813                         int n = * (guint16 *)(ip + 1);
3814                         i32 = READ32(ip + 2);
3815                         memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3816                         ip += 4;
3817                         MINT_IN_BREAK;
3818                 }
3819
3820                 MINT_IN_CASE(MINT_LDARGA)
3821                         sp->data.p = frame->args + * (guint16 *)(ip + 1);
3822                         ip += 2;
3823                         ++sp;
3824                         MINT_IN_BREAK;
3825
3826 #define LDLOC(datamem, argtype) \
3827         sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3828         ip += 2; \
3829         ++sp; 
3830         
3831                 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3832                 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3833                 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3834                 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3835                 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3836                 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3837                 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3838                 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3839                 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3840                 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3841
3842                 MINT_IN_CASE(MINT_LDLOC_VT)
3843                         sp->data.p = vt_sp;
3844                         i32 = READ32(ip + 2);
3845                         memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3846                         vt_sp += (i32 + 7) & ~7;
3847                         ip += 4;
3848                         ++sp;
3849                         MINT_IN_BREAK;
3850
3851                 MINT_IN_CASE(MINT_LDLOCA_S)
3852                         sp->data.p = locals + * (guint16 *)(ip + 1);
3853                         ip += 2;
3854                         ++sp;
3855                         MINT_IN_BREAK;
3856
3857 #define STLOC(datamem, argtype) \
3858         --sp; \
3859         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3860         ip += 2;
3861         
3862                 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3863                 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3864                 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3865                 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3866                 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3867                 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3868                 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3869                 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3870                 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3871                 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3872
3873 #define STLOC_NP(datamem, argtype) \
3874         * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3875         ip += 2;
3876
3877                 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3878                 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3879
3880                 MINT_IN_CASE(MINT_STLOC_VT)
3881                         i32 = READ32(ip + 2);
3882                         --sp;
3883                         memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3884                         vt_sp -= (i32 + 7) & ~7;
3885                         ip += 4;
3886                         MINT_IN_BREAK;
3887
3888                 MINT_IN_CASE(MINT_LOCALLOC)
3889                         if (sp != frame->stack + 1) /*FIX?*/
3890                                 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3891                         sp [-1].data.p = alloca (sp [-1].data.i);
3892                         ++ip;
3893                         MINT_IN_BREAK;
3894 #if 0
3895                 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3896 #endif
3897                 MINT_IN_CASE(MINT_INITOBJ)
3898                         --sp;
3899                         memset (sp->data.vt, 0, READ32(ip + 1));
3900                         ip += 3;
3901                         MINT_IN_BREAK;
3902                 MINT_IN_CASE(MINT_CPBLK)
3903                         sp -= 3;
3904                         if (!sp [0].data.p || !sp [1].data.p)
3905                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3906                         ++ip;
3907                         /* FIXME: value and size may be int64... */
3908                         memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3909                         MINT_IN_BREAK;
3910 #if 0
3911                 MINT_IN_CASE(MINT_CONSTRAINED_) {
3912                         guint32 token;
3913                         /* FIXME: implement */
3914                         ++ip;
3915                         token = READ32 (ip);
3916                         ip += 2;
3917                         MINT_IN_BREAK;
3918                 }
3919 #endif
3920                 MINT_IN_CASE(MINT_INITBLK)
3921                         sp -= 3;
3922                         if (!sp [0].data.p)
3923                                 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3924                         ++ip;
3925                         /* FIXME: value and size may be int64... */
3926                         memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3927                         MINT_IN_BREAK;
3928 #if 0
3929                 MINT_IN_CASE(MINT_NO_)
3930                         /* FIXME: implement */
3931                         ip += 2;
3932                         MINT_IN_BREAK;
3933 #endif
3934                 MINT_IN_CASE(MINT_RETHROW)
3935                         /* 
3936                          * need to clarify what this should actually do:
3937                          * start the search from the last found handler in
3938                          * this method or continue in the caller or what.
3939                          * Also, do we need to run finally/fault handlers after a retrow?
3940                          * Well, this implementation will follow the usual search
3941                          * for an handler, considering the current ip as throw spot.
3942                          * We need to NULL frame->ex_handler for the later code to
3943                          * actually run the new found handler.
3944                          */
3945                         frame->ex_handler = NULL;
3946                         THROW_EX (frame->ex, ip - 1);
3947                         MINT_IN_BREAK;
3948                 MINT_IN_DEFAULT
3949                         g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3950                         THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3951                 }
3952         }
3953
3954         g_assert_not_reached ();
3955         /*
3956          * Exception handling code.
3957          * The exception object is stored in frame->ex.
3958          */
3959
3960         handle_exception:
3961         {
3962                 int i;
3963                 guint32 ip_offset;
3964                 MonoInvocation *inv;
3965                 MonoExceptionClause *clause;
3966                 /*char *message;*/
3967                 MonoObject *ex_obj;
3968
3969 #if DEBUG_INTERP
3970                 if (tracing)
3971                         g_print ("* Handling exception '%s' at IL_%04x\n", 
3972                                 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name, 
3973                                 rtm == NULL ? 0 : frame->ip - rtm->code);
3974 #endif
3975                 if (die_on_exception)
3976                         goto die_on_ex;
3977
3978                 for (inv = frame; inv; inv = inv->parent) {
3979                         MonoMethod *method;
3980                         if (inv->runtime_method == NULL)
3981                                 continue;
3982                         method = inv->runtime_method->method;
3983                         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3984                                 continue;
3985                         if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3986                                 continue;
3987                         if (inv->ip == NULL)
3988                                 continue;
3989                         ip_offset = inv->ip - inv->runtime_method->code;
3990                         inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one  - this one wins */
3991                         for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3992                                 clause = &inv->runtime_method->clauses [i];
3993                                 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3994                                         if (!clause->flags) {
3995                                                 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
3996                                                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3997                                                 if (isinst_obj) {
3998                                                         /* 
3999                                                          * OK, we found an handler, now we need to execute the finally
4000                                                          * and fault blocks before branching to the handler code.
4001                                                          */
4002                                                         inv->ex_handler = clause;
4003 #if DEBUG_INTERP
4004                                                         if (tracing)
4005                                                                 g_print ("* Found handler at '%s'\n", method->name);
4006 #endif
4007                                                         goto handle_finally;
4008                                                 }
4009                                         } else {
4010                                                 /* FIXME: handle filter clauses */
4011                                                 g_assert (0);
4012                                         }
4013                                 }
4014                         }
4015                 }
4016                 /*
4017                  * If we get here, no handler was found: print a stack trace.
4018                  */
4019                 for (inv = frame; inv; inv = inv->parent) {
4020                         if (inv->invoke_trap)
4021                                 goto handle_finally;
4022                 }
4023 die_on_ex:
4024                 ex_obj = (MonoObject*)frame->ex;
4025                 mono_unhandled_exception (ex_obj);
4026                 exit (1);
4027         }
4028         handle_finally:
4029         {
4030                 int i;
4031                 guint32 ip_offset;
4032                 MonoExceptionClause *clause;
4033                 GSList *old_list = finally_ips;
4034                 MonoMethod *method = frame->runtime_method->method;
4035                 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4036                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4037                 
4038 #if DEBUG_INTERP
4039                 if (tracing)
4040                         g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4041 #endif
4042                 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) 
4043                                 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4044                         goto exit_frame;
4045                 }
4046                 ip_offset = frame->ip - rtm->code;
4047
4048                 if (endfinally_ip != NULL)
4049                         finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4050                 for (i = 0; i < header->num_clauses; ++i)
4051                         if (frame->ex_handler == &rtm->clauses [i])
4052                                 break;
4053                 while (i > 0) {
4054                         --i;
4055                         clause = &rtm->clauses [i];
4056                         if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4057                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4058                                         ip = rtm->code + clause->handler_offset;
4059                                         finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4060 #if DEBUG_INTERP
4061                                         if (tracing)
4062                                                 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4063 #endif
4064                                 }
4065                         }
4066                 }
4067
4068                 endfinally_ip = NULL;
4069
4070                 if (old_list != finally_ips && finally_ips) {
4071                         ip = finally_ips->data;
4072                         finally_ips = g_slist_remove (finally_ips, ip);
4073                         sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4074                         goto main_loop;
4075                 }
4076
4077                 /*
4078                  * If an exception is set, we need to execute the fault handler, too,
4079                  * otherwise, we continue normally.
4080                  */
4081                 if (frame->ex)
4082                         goto handle_fault;
4083                 ves_abort();
4084         }
4085         handle_fault:
4086         {
4087                 int i;
4088                 guint32 ip_offset;
4089                 MonoExceptionClause *clause;
4090                 MonoMethod *method = frame->runtime_method->method;
4091                 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4092                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4093                 
4094 #if DEBUG_INTERP
4095                 if (tracing)
4096                         g_print ("* Handle fault\n");
4097 #endif
4098                 ip_offset = frame->ip - rtm->code;
4099                 for (i = 0; i < header->num_clauses; ++i) {
4100                         clause = &rtm->clauses [i];
4101                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4102                                 ip = rtm->code + clause->handler_offset;
4103 #if DEBUG_INTERP
4104                                 if (tracing)
4105                                         g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4106 #endif
4107                                 goto main_loop;
4108                         }
4109                 }
4110                 /*
4111                  * If the handler for the exception was found in this method, we jump
4112                  * to it right away, otherwise we return and let the caller run
4113                  * the finally, fault and catch blocks.
4114                  * This same code should be present in the endfault opcode, but it
4115                  * is corrently not assigned in the ECMA specs: LAMESPEC.
4116                  */
4117                 if (frame->ex_handler) {
4118 #if DEBUG_INTERP
4119                         if (tracing)
4120                                 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4121 #endif
4122                         ip = rtm->code + frame->ex_handler->handler_offset;
4123                         sp = frame->stack;
4124                         vt_sp = (unsigned char *) sp + rtm->stack_size;
4125                         sp->data.p = frame->ex;
4126                         ++sp;
4127                         goto main_loop;
4128                 }
4129                 goto exit_frame;
4130         }
4131 exit_frame:
4132         DEBUG_LEAVE ();
4133 }
4134
4135 void
4136 ves_exec_method (MonoInvocation *frame)
4137 {
4138         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4139         ThreadContext context_struct;
4140         MonoError error;
4141         jmp_buf env;
4142
4143         frame->ex = NULL;
4144
4145         if (setjmp(env)) {
4146                 mono_unhandled_exception ((MonoObject*)frame->ex);
4147                 return;
4148         }
4149         if (context == NULL) {
4150                 context = &context_struct;
4151                 context_struct.domain = mono_domain_get ();
4152                 context_struct.base_frame = frame;
4153                 context_struct.current_frame = NULL;
4154                 context_struct.env_frame = frame;
4155                 context_struct.current_env = &env;
4156                 context_struct.search_for_handler = 0;
4157                 context_struct.managed_code = 0;
4158                 mono_native_tls_set_value (thread_context_id, context);
4159         }
4160         frame->ip = NULL;
4161         frame->parent = context->current_frame;
4162         frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4163         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4164         context->managed_code = 1;
4165         ves_exec_method_with_context (frame, context);
4166         context->managed_code = 0;
4167         if (frame->ex) {
4168                 if (context != &context_struct && context->current_env) {
4169                         context->env_frame->ex = frame->ex;
4170                         longjmp (*context->current_env, 1);
4171                 }
4172                 else
4173                         mono_unhandled_exception ((MonoObject*)frame->ex);
4174         }
4175         if (context->base_frame == frame)
4176                 mono_native_tls_set_value (thread_context_id, NULL);
4177         else
4178                 context->current_frame = frame->parent;
4179 }
4180
4181 static int 
4182 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4183 {
4184         MonoImage *image = mono_assembly_get_image (assembly);
4185         MonoMethod *method;
4186         MonoError error;
4187         int rval;
4188
4189         method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4190         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4191
4192         if (!method)
4193                 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4194
4195         rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4196         return_val_and_set_pending_if_nok (&error, rval);
4197 }
4198
4199 static void
4200 usage (void)
4201 {
4202         fprintf (stderr,
4203                  "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4204                  "Usage is: mint [options] executable args...\n\n", VERSION);
4205         fprintf (stderr,
4206                  "Runtime Debugging:\n"
4207 #ifdef DEBUG_INTERP
4208                  "   --debug\n"
4209 #endif
4210                  "   --dieonex\n"
4211                  "   --noptr\t\t\tdon't print pointer addresses in trace output\n"
4212                  "   --opcode-count\n"
4213                  "   --print-vtable\n"
4214                  "   --traceclassinit\n"
4215                  "\n"
4216                  "Development:\n"
4217                  "   --debug method_name\n"
4218                  "   --profile\n"
4219                  "   --trace\n"
4220                  "   --traceops\n"
4221                  "   --regression\n"
4222                  "\n"
4223                  "Runtime:\n"
4224                  "   --config filename  load the specified config file instead of the default\n"
4225                  "   --workers n        maximum number of worker threads\n"
4226                 );
4227         exit (1);
4228 }
4229
4230 static void
4231 add_signal_handler (int signo, void (*handler)(int))
4232 {
4233 #ifdef HOST_WIN32
4234         signal (signo, handler);
4235 #else
4236         struct sigaction sa;
4237
4238         sa.sa_handler = handler;
4239         sigemptyset (&sa.sa_mask);
4240         sa.sa_flags = 0;
4241
4242         g_assert (sigaction (signo, &sa, NULL) != -1);
4243 #endif
4244 }
4245
4246 static void
4247 segv_handler (int signum)
4248 {
4249         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4250         MonoException *segv_exception;
4251
4252         if (context == NULL)
4253                 return;
4254         segv_exception = mono_get_exception_null_reference ();
4255         segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4256         mono_raise_exception (segv_exception);
4257 }
4258
4259
4260 static void
4261 quit_handler (int signum)
4262 {
4263         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4264         MonoException *quit_exception;
4265
4266         if (context == NULL)
4267                 return;
4268         quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4269         mono_raise_exception (quit_exception);
4270 }
4271
4272 static void
4273 abrt_handler (int signum)
4274 {
4275         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4276         MonoException *abrt_exception;
4277
4278         if (context == NULL)
4279                 return;
4280         abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4281         mono_raise_exception (abrt_exception);
4282 }
4283
4284 #if 0
4285 static void
4286 thread_abort_handler (int signum)
4287 {
4288         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4289         MonoException *exc;
4290
4291         if (context == NULL)
4292                 return;
4293
4294         exc = mono_thread_request_interruption (context->managed_code); 
4295         if (exc) mono_raise_exception (exc);
4296 }
4297 #endif
4298
4299 static MonoBoolean
4300 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info, 
4301                           MonoReflectionMethod **method, 
4302                           gint32 *iloffset, gint32 *native_offset,
4303                           MonoString **file, gint32 *line, gint32 *column)
4304 {
4305         ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4306         MonoInvocation *inv = context->current_frame;
4307         MonoError error;
4308         int i;
4309
4310         for (i = 0; inv && i < skip; inv = inv->parent)
4311                 if (inv->runtime_method != NULL)
4312                         ++i;
4313
4314         if (iloffset)
4315                 *iloffset = 0;
4316         if (native_offset)
4317                 *native_offset = 0;
4318         if (method) {
4319                 if (inv == NULL) {
4320                         *method = NULL;
4321                 } else {
4322                         *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4323                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4324                 }
4325         }
4326         if (line)
4327                 *line = 0;
4328         if (need_file_info) {
4329                 if (column)
4330                         *column = 0;
4331                 if (file)
4332                         *file = mono_string_new (mono_domain_get (), "unknown");
4333         }
4334
4335         return TRUE;
4336 }
4337
4338 static MonoArray *
4339 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4340 {
4341         MonoDomain *domain = mono_domain_get ();
4342         MonoArray *res;
4343         MonoArray *ta = exc->trace_ips;
4344         MonoError error;
4345         int i, len;
4346
4347         if (ta == NULL) {
4348                 /* Exception is not thrown yet */
4349                 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4350                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4351                 return array;
4352         }
4353         
4354         len = mono_array_length (ta);
4355
4356         res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4357         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4358
4359         for (i = skip; i < len / 2; i++) {
4360                 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4361                 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4362                 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4363                 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4364
4365                 if (rtm != NULL) {
4366                         sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4367                         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4368                         sf->native_offset = ip - rtm->code;
4369                 }
4370
4371 #if 0
4372                 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4373
4374                 if (need_file_info) {
4375                         gchar *filename;
4376                         
4377                         filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4378
4379                         sf->filename = filename? mono_string_new (domain, filename): NULL;
4380                         sf->column = 0;
4381
4382                         g_free (filename);
4383                 }
4384 #endif
4385
4386                 mono_array_set (res, gpointer, i, sf);
4387         }
4388
4389         return res;
4390 }
4391
4392 static MonoObject *
4393 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4394 {
4395         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4396         MonoObject *delegate;
4397         MonoError error;
4398
4399         g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4400
4401         delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4402         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4403
4404         interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4405         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4406
4407         return delegate;
4408 }
4409
4410
4411 typedef struct
4412 {
4413         MonoDomain *domain;
4414         int enable_debugging;
4415         char *file;
4416         int argc;
4417         char **argv;
4418 } MainThreadArgs;
4419
4420 static void main_thread_handler (gpointer user_data)
4421 {
4422         MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4423         MonoAssembly *assembly;
4424
4425         if (main_args->enable_debugging) {
4426                 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4427         }
4428
4429         assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
4430
4431         if (!assembly){
4432                 fprintf (stderr, "Can not open image %s\n", main_args->file);
4433                 exit (1);
4434         }
4435
4436         ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4437 }
4438
4439 static void
4440 mono_runtime_install_handlers (void)
4441 {
4442         add_signal_handler (SIGSEGV, segv_handler);
4443         add_signal_handler (SIGINT, quit_handler);
4444         add_signal_handler (SIGABRT, abrt_handler);
4445 #if 0
4446         add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4447 #endif
4448 }
4449
4450 static void
4451 quit_function (MonoDomain *domain, gpointer user_data)
4452 {
4453         mono_profiler_shutdown ();
4454         
4455         mono_runtime_cleanup (domain);
4456         mono_domain_free (domain, TRUE);
4457
4458 }
4459
4460 void
4461 mono_interp_cleanup(MonoDomain *domain)
4462 {
4463         quit_function (domain, NULL);
4464 }
4465
4466 int
4467 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4468 {
4469         return ves_exec (domain, assembly, argc, argv);
4470 }
4471
4472 gpointer*
4473 interp_get_imt_trampoline (MonoVTable *vtable, int imt_slot_index)
4474 {
4475         // FIXME: implement me
4476         return NULL;
4477 }
4478
4479 static gpointer
4480 interp_create_ftnptr (MonoDomain *domain, gpointer addr)
4481 {
4482         // FIXME: true on all arch?
4483         return addr;
4484 }
4485
4486 MonoDomain *
4487 mono_interp_init(const char *file)
4488 {
4489         MonoDomain *domain;
4490         MonoRuntimeCallbacks callbacks;
4491         MonoRuntimeExceptionHandlingCallbacks ecallbacks;
4492         MonoError error;
4493
4494         g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4495         g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4496
4497         mono_native_tls_alloc (&thread_context_id, NULL);
4498     mono_native_tls_set_value (thread_context_id, NULL);
4499         mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4500         mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4501
4502         // TODO: use callbacks?
4503         mono_runtime_install_handlers ();
4504         mono_interp_transform_init ();
4505
4506         memset (&callbacks, 0, sizeof (callbacks));
4507         // TODO: replace with `mono_install_callbacks`
4508         callbacks.compile_method = mono_create_method_pointer;
4509         callbacks.runtime_invoke = interp_mono_runtime_invoke;
4510         callbacks.get_imt_trampoline = interp_get_imt_trampoline;
4511         callbacks.create_ftnptr = interp_create_ftnptr;
4512 #ifndef DISABLE_REMOTING
4513         mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4514 #endif
4515         callbacks.create_jit_trampoline = interp_create_trampoline;
4516         mono_install_callbacks (&callbacks);
4517
4518
4519         memset (&ecallbacks, 0, sizeof (ecallbacks));
4520         ecallbacks.mono_raise_exception = interp_ex_handler;
4521 #if 0
4522         // FIXME: ...
4523         mono_install_stack_walk (interp_walk_stack);
4524 #endif
4525         mono_install_eh_callbacks (&ecallbacks);
4526
4527         mono_install_runtime_cleanup (quit_function);
4528
4529         abort_requested = mono_thread_interruption_request_flag ();
4530
4531         domain = mono_init_from_assembly (file, file);
4532 #ifdef __hpux /* generates very big stack frames */
4533         mono_threads_set_default_stacksize(32*1024*1024);
4534 #endif
4535         mono_icall_init ();
4536         mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4537         mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4538         mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4539         mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4540
4541         mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", mono_create_icall_signature ("void"), FALSE);
4542
4543         mono_runtime_init_checked (domain, NULL, NULL, &error);
4544         mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4545
4546         mono_thread_attach (domain);
4547         return domain;
4548 }
4549
4550 typedef int (*TestMethod) (void);
4551
4552 static void
4553 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4554 {
4555         int result, expected, failed, cfailed, run;
4556         TestMethod func;
4557         double elapsed, transform_time, start_time;
4558         int i;
4559         MonoObject *result_obj;
4560
4561         g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4562         cfailed = failed = run = 0;
4563         transform_time = elapsed = 0.0;
4564
4565 #if 0
4566         /* fixme: ugly hack - delete all previously compiled methods */
4567         if (domain_jit_info (domain)) {
4568                 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4569                 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4570                 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4571                 mono_jit_code_hash_init (&(domain->jit_code_hash));
4572         }
4573 #endif
4574
4575         g_timer_start (timer);
4576         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4577                 MonoError error;
4578                 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4579                 if (!method) {
4580                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
4581                         continue;
4582                 }
4583                 if (strncmp (method->name, "test_", 5) == 0) {
4584                         MonoError interp_error;
4585                         RuntimeMethod *runtime_method;
4586                         MonoInvocation frame;
4587                         ThreadContext context;
4588                         stackval frame_result;
4589
4590                         result_obj = interp_mono_runtime_invoke (method, NULL, NULL, NULL, &interp_error);
4591                         if (!mono_error_ok (&interp_error)) {
4592                                 cfailed++;
4593                                 g_print ("Test '%s' execution failed.\n", method->name);
4594                         } else {
4595                                 result = *(gint32 *) mono_object_unbox (result_obj);
4596                                 expected = atoi (method->name + 5);  // FIXME: oh no.
4597                                 run++;
4598
4599                                 if (result != expected) {
4600                                         failed++;
4601                                         g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4602                                 }
4603                         }
4604                 }
4605         }
4606         g_timer_stop (timer);
4607         elapsed = g_timer_elapsed (timer, NULL);
4608         if (failed > 0 || cfailed > 0){
4609                 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4610                                 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4611         } else {
4612                 g_print ("Results: total tests: %d, all pass \n",  run);
4613         }
4614
4615         g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4616                         elapsed - transform_time, transform_time);
4617         *total += failed + cfailed;
4618         *total_run += run;
4619 }
4620 static int
4621 interp_regression (MonoImage *image, int verbose, int *total_run)
4622 {
4623         MonoMethod *method;
4624         char *n;
4625         GTimer *timer = g_timer_new ();
4626         MonoDomain *domain = mono_domain_get ();
4627         guint32 i, exclude = 0;
4628         int total;
4629
4630         /* load the metadata */
4631         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4632                 MonoError error;
4633                 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4634                 if (!method) {
4635                         mono_error_cleanup (&error);
4636                         continue;
4637                 }
4638                 mono_class_init (method->klass);
4639         }
4640
4641         total = 0;
4642         *total_run = 0;
4643         interp_regression_step (image, verbose, total_run, &total, timer, domain);
4644
4645         g_timer_destroy (timer);
4646         return total;
4647 }
4648
4649 int
4650 interp_regression_list (int verbose, int count, char *images [])
4651 {
4652         int i, total, total_run, run;
4653         
4654         total_run = total = 0;
4655         for (i = 0; i < count; ++i) {
4656                 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4657                 if (!ass) {
4658                         g_warning ("failed to load assembly: %s", images [i]);
4659                         continue;
4660                 }
4661                 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4662                 total_run += run;
4663         }
4664         if (total > 0) {
4665                 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4666         } else {
4667                 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);
4668         }
4669         
4670         return total;
4671 }
4672