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