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