2 * PLEASE NOTE: This is a research prototype.
5 * interp.c: Interpreter for CIL byte codes
8 * Paolo Molaro (lupus@ximian.com)
9 * Miguel de Icaza (miguel@ximian.com)
10 * Dietmar Maurer (dietmar@ximian.com)
12 * (C) 2001, 2002 Ximian, Inc.
25 #include <mono/os/gc_wrapper.h>
31 # define alloca __builtin_alloca
35 /* trim excessive headers */
36 #include <mono/metadata/image.h>
37 #include <mono/metadata/assembly.h>
38 #include <mono/metadata/cil-coff.h>
39 #include <mono/metadata/mono-endian.h>
40 #include <mono/metadata/tabledefs.h>
41 #include <mono/metadata/blob.h>
42 #include <mono/metadata/tokentype.h>
43 #include <mono/metadata/loader.h>
44 #include <mono/metadata/threads.h>
45 #include <mono/metadata/threadpool.h>
46 #include <mono/metadata/profiler-private.h>
47 #include <mono/metadata/appdomain.h>
48 #include <mono/metadata/reflection.h>
49 #include <mono/metadata/exception.h>
50 #include <mono/metadata/verify.h>
51 #include <mono/metadata/opcodes.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/io-layer/io-layer.h>
54 #include <mono/metadata/socket-io.h>
55 #include <mono/metadata/mono-config.h>
56 #include <mono/metadata/marshal.h>
57 #include <mono/metadata/environment.h>
58 #include <mono/metadata/mono-debug.h>
59 #include <mono/os/util.h>
61 /*#include <mono/cli/types.h>*/
65 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
68 #define finite _finite
71 /* If true, then we output the opcodes as we interpret them */
72 static int global_tracing = 0;
73 static int global_no_pointers = 0;
75 static int debug_indent_level = 0;
78 * Pull the list of opcodes
80 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
84 #include "mono/cil/opcode.def"
89 #define GET_NATI(sp) ((sp).data.nati)
90 #define CSIZE(x) (sizeof (x) / 4)
92 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,mono_method) \
94 (frame)->parent = (parent_frame); \
95 (frame)->obj = (obj_this); \
96 (frame)->stack_args = (method_args); \
97 (frame)->retval = (method_retval); \
98 (frame)->method = (mono_method); \
99 (frame)->ex_handler = NULL; \
100 (frame)->ex = NULL; \
101 (frame)->child = NULL; \
102 (frame)->invoke_trap = 0; \
105 void ves_exec_method (MonoInvocation *frame);
107 typedef void (*ICallMethod) (MonoInvocation *frame);
109 static guint32 die_on_exception = 0;
110 static guint32 frame_thread_id = 0;
112 #define DEBUG_INTERP 1
115 static unsigned long opcode_count = 0;
116 static unsigned long fcall_count = 0;
117 static int break_on_method = 0;
118 static GList *db_methods = NULL;
125 for (h = 0; h < debug_indent_level; h++)
130 db_match_method (gpointer data, gpointer user_data)
132 MonoMethod *m = (MonoMethod*)user_data;
133 MonoMethodDesc *desc = data;
135 if (mono_method_desc_full_match (desc, m))
139 #define DEBUG_ENTER() \
141 g_list_foreach (db_methods, db_match_method, (gpointer)frame->method); \
142 if (break_on_method) tracing=2; \
143 break_on_method = 0; \
145 char *mn, *args = dump_stack (frame->stack_args, frame->stack_args+signature->param_count); \
146 debug_indent_level++; \
148 mn = mono_method_full_name (frame->method, FALSE); \
149 g_print ("(%d) Entering %s (", GetCurrentThreadId(), mn); \
151 if (signature->hasthis) { \
152 if (global_no_pointers) { \
153 g_print ("this%s ", frame->obj ? "" : "=null"); \
155 g_print ("%p ", frame->obj); } \
157 g_print ("%s)\n", args); \
160 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
161 mono_profiler_method_enter (frame->method);
163 #define DEBUG_LEAVE() \
166 if (signature->ret->type != MONO_TYPE_VOID) \
167 args = dump_stack (frame->retval, frame->retval + 1); \
169 args = g_strdup (""); \
171 mn = mono_method_full_name (frame->method, FALSE); \
172 g_print ("(%d) Leaving %s", GetCurrentThreadId(), mn); \
174 g_print (" => %s\n", args); \
176 debug_indent_level--; \
178 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
179 mono_profiler_method_leave (frame->method);
183 #define DEBUG_ENTER()
184 #define DEBUG_LEAVE()
189 interp_ex_handler (MonoException *ex) {
190 MonoInvocation *frame = TlsGetValue (frame_thread_id);
192 longjmp (*(jmp_buf*)frame->locals, 1);
196 ves_real_abort (int line, MonoMethod *mh,
197 const unsigned char *ip, stackval *stack, stackval *sp)
199 MonoMethodNormal *mm = (MonoMethodNormal *)mh;
200 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
201 fprintf (stderr, "Line=%d IP=0x%04x, Aborted execution\n", line,
202 ip-mm->header->code);
203 g_print ("0x%04x %02x\n",
204 ip-mm->header->code, *ip);
206 printf ("\t[%d] %d 0x%08x %0.5f\n", sp-stack, sp[-1].type, sp[-1].data.i, sp[-1].data.f);
208 #define ves_abort() do {ves_real_abort(__LINE__, frame->method, ip, frame->stack, sp); THROW_EX (mono_get_exception_execution_engine (NULL), ip);} while (0);
211 interp_create_remoting_trampoline (MonoMethod *method)
213 return mono_marshal_get_remoting_invoke (method);
217 get_virtual_method (MonoDomain *domain, MonoMethod *m, stackval *objs)
222 gboolean is_proxy = FALSE;
225 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL))
229 if ((klass = obj->vtable->klass) == mono_defaults.transparent_proxy_class) {
230 klass = ((MonoTransparentProxy *)obj)->klass;
233 vtable = (MonoMethod **)klass->vtable;
235 if (m->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
236 res = ((MonoMethod **)obj->vtable->interface_offsets [m->klass->interface_id]) [m->slot];
238 res = vtable [m->slot];
243 return mono_marshal_get_remoting_invoke (res);
249 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
252 switch (type->type) {
253 case MONO_TYPE_OBJECT:
254 case MONO_TYPE_CLASS:
255 case MONO_TYPE_STRING:
256 case MONO_TYPE_ARRAY:
257 case MONO_TYPE_SZARRAY:
258 result->type = VAL_OBJ;
261 result->type = VAL_MP;
264 result->data.p = *(gpointer*)data;
265 result->data.vt.klass = mono_class_from_mono_type (type);
268 switch (type->type) {
272 result->type = VAL_I32;
273 result->data.i = *(gint8*)data;
276 case MONO_TYPE_BOOLEAN:
277 result->type = VAL_I32;
278 result->data.i = *(guint8*)data;
281 result->type = VAL_I32;
282 result->data.i = *(gint16*)data;
286 result->type = VAL_I32;
287 result->data.i = *(guint16*)data;
290 result->type = VAL_I32;
291 result->data.i = *(gint32*)data;
296 result->type = VAL_TP;
297 result->data.p = *(gpointer*)data;
300 result->type = VAL_I32;
301 result->data.i = *(guint32*)data;
304 result->type = VAL_DOUBLE;
305 result->data.f = *(float*)data;
309 result->type = VAL_I64;
310 result->data.l = *(gint64*)data;
313 result->type = VAL_DOUBLE;
314 result->data.f = *(double*)data;
316 case MONO_TYPE_STRING:
317 case MONO_TYPE_SZARRAY:
318 case MONO_TYPE_CLASS:
319 case MONO_TYPE_OBJECT:
320 case MONO_TYPE_ARRAY:
321 result->type = VAL_OBJ;
322 result->data.p = *(gpointer*)data;
323 result->data.vt.klass = mono_class_from_mono_type (type);
325 case MONO_TYPE_VALUETYPE:
326 if (type->data.klass->enumtype) {
327 return stackval_from_data (type->data.klass->enum_basetype, result, data, pinvoke);
330 result->type = VAL_VALUET;
331 result->data.vt.klass = type->data.klass;
334 size = mono_class_native_size (type->data.klass, NULL);
336 size = mono_class_value_size (type->data.klass, NULL);
337 memcpy (result->data.vt.vt, data, size);
341 g_warning ("got type 0x%02x", type->type);
342 g_assert_not_reached ();
347 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
350 gpointer *p = (gpointer*)data;
354 //printf ("TODAT0 %p\n", data);
355 switch (type->type) {
358 guint8 *p = (guint8*)data;
362 case MONO_TYPE_BOOLEAN: {
363 guint8 *p = (guint8*)data;
364 *p = (val->data.i != 0);
369 case MONO_TYPE_CHAR: {
370 guint16 *p = (guint16*)data;
374 #if SIZEOF_VOID_P == 4
380 gint32 *p = (gint32*)data;
384 #if SIZEOF_VOID_P == 8
390 gint64 *p = (gint64*)data;
395 float *p = (float*)data;
400 double *p = (double*)data;
404 case MONO_TYPE_STRING:
405 case MONO_TYPE_SZARRAY:
406 case MONO_TYPE_CLASS:
407 case MONO_TYPE_OBJECT:
408 case MONO_TYPE_ARRAY:
409 case MONO_TYPE_PTR: {
410 gpointer *p = (gpointer*)data;
414 case MONO_TYPE_VALUETYPE:
415 if (type->data.klass->enumtype) {
416 return stackval_to_data (type->data.klass->enum_basetype, val, data, pinvoke);
421 size = mono_class_native_size (type->data.klass, NULL);
423 size = mono_class_value_size (type->data.klass, NULL);
425 memcpy (data, val->data.vt.vt, size);
429 g_warning ("got type %x", type->type);
430 g_assert_not_reached ();
435 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
438 guint32 *lower_bounds;
441 lengths = alloca (sizeof (guint32) * klass->rank * 2);
442 for (i = 0; i < sig->param_count; ++i) {
443 lengths [i] = values->data.i;
446 if (klass->rank == sig->param_count) {
447 /* Only lengths provided. */
450 /* lower bounds are first. */
451 lower_bounds = lengths;
452 lengths += klass->rank;
454 return (MonoObject*)mono_array_new_full (domain, klass, lengths, lower_bounds);
458 ves_array_set (MonoInvocation *frame)
460 stackval *sp = frame->stack_args;
464 gint32 i, t, pos, esize;
470 ac = o->vtable->klass;
472 g_assert (ac->rank >= 1);
475 if (ao->bounds != NULL) {
476 pos -= ao->bounds [0].lower_bound;
477 for (i = 1; i < ac->rank; i++) {
478 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
479 ao->bounds [i].length) {
480 frame->ex = mono_get_exception_index_out_of_range ();
483 pos = pos*ao->bounds [i].length + sp [i].data.i -
484 ao->bounds [i].lower_bound;
488 esize = mono_array_element_size (ac);
489 ea = mono_array_addr_with_size (ao, esize, pos);
491 mt = frame->method->signature->params [ac->rank];
492 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
496 ves_array_get (MonoInvocation *frame)
498 stackval *sp = frame->stack_args;
502 gint32 i, t, pos, esize;
508 ac = o->vtable->klass;
510 g_assert (ac->rank >= 1);
513 if (ao->bounds != NULL) {
514 pos -= ao->bounds [0].lower_bound;
515 for (i = 1; i < ac->rank; i++) {
517 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
518 ao->bounds [i].length) {
519 frame->ex = mono_get_exception_index_out_of_range ();
523 pos = pos*ao->bounds [i].length + sp [i].data.i -
524 ao->bounds [i].lower_bound;
528 esize = mono_array_element_size (ac);
529 ea = mono_array_addr_with_size (ao, esize, pos);
531 mt = frame->method->signature->ret;
532 stackval_from_data (mt, frame->retval, ea, FALSE);
536 ves_array_element_address (MonoInvocation *frame)
538 stackval *sp = frame->stack_args;
542 gint32 i, pos, esize;
547 ac = o->vtable->klass;
549 g_assert (ac->rank >= 1);
552 if (ao->bounds != NULL) {
553 pos -= ao->bounds [0].lower_bound;
554 for (i = 1; i < ac->rank; i++)
555 pos = pos*ao->bounds [i].length + sp [i].data.i -
556 ao->bounds [i].lower_bound;
559 esize = mono_array_element_size (ac);
560 ea = mono_array_addr_with_size (ao, esize, pos);
562 frame->retval->type = VAL_MP;
563 frame->retval->data.p = ea;
567 interp_walk_stack (MonoStackWalk func, gpointer user_data)
569 MonoInvocation *frame = TlsGetValue (frame_thread_id);
571 MonoMethodHeader *hd;
574 gboolean managed = FALSE;
575 if (!frame->method || (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
576 (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
579 hd = ((MonoMethodNormal*)frame->method)->header;
580 il_offset = frame->ip - hd->code;
581 if (!frame->method->wrapper_type)
584 if (func (frame->method, -1, il_offset, managed, user_data))
586 frame = frame->parent;
591 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFunc addr, gboolean string_ctor)
597 TlsSetValue (frame_thread_id, frame->args);
602 if (!frame->method->info) {
603 func = frame->method->info = mono_create_trampoline (sig, string_ctor);
605 func = (MonoPIFunc)frame->method->info;
608 func = mono_create_trampoline (sig, string_ctor);
612 * frame->locals and args are unused for P/Invoke methods, so we reuse them.
613 * locals will point to the jmp_buf, while args will point to the previous
614 * MonoInvocation frame: this is needed to make exception searching work across
615 * managed/unmanaged boundaries.
617 frame->locals = (char*)&env;
618 frame->args = (char*)TlsGetValue (frame_thread_id);
619 TlsSetValue (frame_thread_id, frame);
621 func (addr, &frame->retval->data.p, frame->obj, frame->stack_args);
624 stackval_from_data (&mono_defaults.string_class->byval_arg,
625 frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
626 } else if (!MONO_TYPE_ISSTRUCT (sig->ret))
627 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
629 TlsSetValue (frame_thread_id, frame->args);
634 * runtime specifies that the implementation of the method is automatically
635 * provided by the runtime and is primarily used for the methods of delegates.
638 ves_runtime_method (MonoInvocation *frame)
640 const char *name = frame->method->name;
641 MonoObject *obj = (MonoObject*)frame->obj;
642 MonoMulticastDelegate *delegate = (MonoMulticastDelegate*)frame->obj;
647 mono_class_init (frame->method->klass);
649 if (obj && mono_object_isinst (obj, mono_defaults.multicastdelegate_class)) {
650 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
651 mono_delegate_ctor (obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
654 if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
661 code = (guchar*)delegate->delegate.method_ptr;
662 if ((ji = mono_jit_info_table_find (mono_root_domain, code))) {
664 INIT_FRAME(&call,frame,delegate->delegate.target,frame->stack_args,frame->retval,method);
665 ves_exec_method (&call);
670 g_assert_not_reached ();
673 delegate = delegate->prev;
677 if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
678 nm = mono_marshal_get_delegate_begin_invoke (frame->method);
679 INIT_FRAME(&call,frame,obj,frame->stack_args,frame->retval,nm);
680 ves_exec_method (&call);
684 if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
685 nm = mono_marshal_get_delegate_end_invoke (frame->method);
686 INIT_FRAME(&call,frame,obj,frame->stack_args,frame->retval,nm);
687 ves_exec_method (&call);
693 if (obj && mono_object_isinst (obj, mono_defaults.array_class)) {
694 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
695 ves_array_set (frame);
698 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
699 ves_array_get (frame);
702 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
703 ves_array_element_address (frame);
708 g_error ("Don't know how to exec runtime method %s.%s::%s",
709 frame->method->klass->name_space, frame->method->klass->name,
710 frame->method->name);
714 dump_stack (stackval *stack, stackval *sp)
717 GString *str = g_string_new ("");
720 return g_string_free (str, FALSE);
724 case VAL_I32: g_string_sprintfa (str, "[%d] ", s->data.i); break;
725 case VAL_I64: g_string_sprintfa (str, "[%lld] ", s->data.l); break;
726 case VAL_DOUBLE: g_string_sprintfa (str, "[%0.5f] ", s->data.f); break;
728 if (!global_no_pointers)
729 g_string_sprintfa (str, "[vt: %p] ", s->data.vt.vt);
731 g_string_sprintfa (str, "[vt%s] ", s->data.vt.vt ? "" : "=null");
734 MonoObject *obj = s->data.p;
735 if (global_no_pointers && obj && obj->vtable) {
736 MonoClass *klass = mono_object_class (obj);
737 if (klass == mono_defaults.string_class) {
738 char *utf8 = mono_string_to_utf8 ((MonoString*) obj);
739 g_string_sprintfa (str, "[str:%s] ", utf8);
742 } else if (klass == mono_defaults.sbyte_class) {
743 g_string_sprintfa (str, "[b:%d] ",
744 *(gint8 *)((guint8 *) obj + sizeof (MonoObject)));
746 } else if (klass == mono_defaults.int16_class) {
747 g_string_sprintfa (str, "[b:%d] ",
748 *(gint16 *)((guint8 *) obj + sizeof (MonoObject)));
750 } else if (klass == mono_defaults.int32_class) {
751 g_string_sprintfa (str, "[b:%d] ",
752 *(gint32 *)((guint8 *) obj + sizeof (MonoObject)));
754 } else if (klass == mono_defaults.byte_class) {
755 g_string_sprintfa (str, "[b:%u] ",
756 *(guint8 *)((guint8 *) obj + sizeof (MonoObject)));
758 } else if (klass == mono_defaults.char_class
759 || klass == mono_defaults.uint16_class) {
760 g_string_sprintfa (str, "[b:%u] ",
761 *(guint16 *)((guint8 *) obj + sizeof (MonoObject)));
763 } else if (klass == mono_defaults.uint32_class) {
764 g_string_sprintfa (str, "[b:%u] ",
765 *(guint32 *)((guint8 *) obj + sizeof (MonoObject)));
767 } else if (klass == mono_defaults.int64_class) {
768 g_string_sprintfa (str, "[b:%lld] ",
769 *(gint64 *)((guint8 *) obj + sizeof (MonoObject)));
771 } else if (klass == mono_defaults.uint64_class) {
772 g_string_sprintfa (str, "[b:%llu] ",
773 *(guint64 *)((guint8 *) obj + sizeof (MonoObject)));
775 } else if (klass == mono_defaults.double_class) {
776 g_string_sprintfa (str, "[b:%0.5f] ",
777 *(gdouble *)((guint8 *) obj + sizeof (MonoObject)));
779 } else if (klass == mono_defaults.single_class) {
780 g_string_sprintfa (str, "[b:%0.5f] ",
781 *(gfloat *)((guint8 *) obj + sizeof (MonoObject)));
783 } else if (klass == mono_defaults.boolean_class) {
784 g_string_sprintfa (str, "[b:%s] ",
785 *(gboolean *)((guint8 *) obj + sizeof (MonoObject))
793 if (!global_no_pointers)
794 g_string_sprintfa (str, "[%p] ", s->data.p);
796 g_string_sprintfa (str, s->data.p ? "[ptr] " : "[null] ");
801 return g_string_free (str, FALSE);
805 dump_frame (MonoInvocation *inv)
807 GString *str = g_string_new ("");
810 for (i = 0; inv; inv = inv->parent, ++i) {
811 MonoClass *k = inv->method->klass;
814 gchar *source = NULL;
815 if (inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL ||
816 inv->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
820 MonoMethodHeader *hd = ((MonoMethodNormal *)inv->method)->header;
823 codep = *(inv->ip) == 0xfe? inv->ip [1] + 256: *(inv->ip);
826 opname = mono_opcode_names [codep];
827 codep = inv->ip - hd->code;
829 source = mono_debug_source_location_from_il_offset (inv->method, codep, NULL);
831 args = dump_stack (inv->stack_args, inv->stack_args + inv->method->signature->param_count);
833 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s.%s::%s (%s) at %s\n", i, codep, opname,
834 k->name_space, k->name, inv->method->name, args, source);
836 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s.%s::%s (%s)\n", i, codep, opname,
837 k->name_space, k->name, inv->method->name, args);
841 return g_string_free (str, FALSE);
845 INLINE_STRING_LENGTH = 1,
848 INLINE_TYPE_ELEMENT_TYPE
852 calc_offsets (MonoImage *image, MonoMethod *method)
854 int i, align, size, offset = 0;
855 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
856 MonoMethodSignature *signature = method->signature;
857 int hasthis = signature->hasthis;
858 register const unsigned char *ip, *end;
859 const MonoOpcode *opcode;
863 MonoDomain *domain = mono_domain_get ();
866 mono_profiler_method_jit (method); /* sort of... */
867 offsets = g_new0 (guint32, 2 + header->num_locals + signature->param_count + signature->hasthis);
868 for (i = 0; i < header->num_locals; ++i) {
869 size = mono_type_size (header->locals [i], &align);
871 offset &= ~(align - 1);
872 offsets [2 + i] = offset;
875 offsets [0] = offset;
878 offset += sizeof (gpointer) - 1;
879 offset &= ~(sizeof (gpointer) - 1);
880 offsets [2 + header->num_locals] = offset;
881 offset += sizeof (gpointer);
883 for (i = 0; i < signature->param_count; ++i) {
884 if (signature->pinvoke)
885 size = mono_type_native_stack_size (signature->params [i], &align);
887 size = mono_type_stack_size (signature->params [i], &align);
889 offset &= ~(align - 1);
890 offsets [2 + hasthis + header->num_locals + i] = offset;
893 offsets [1] = offset;
895 /* FIXME: This might cause a deadlock with domain->lock */
896 EnterCriticalSection (metadata_section);
897 /* intern the strings in the method. */
899 end = ip + header->code_size;
906 opcode = &mono_opcodes [i];
907 switch (opcode->argument) {
911 case MonoInlineString:
912 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
916 if (method->wrapper_type == MONO_WRAPPER_NONE) {
917 class = mono_class_get (image, read32 (ip + 1));
918 mono_class_init (class);
919 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE))
920 mono_class_vtable (domain, class);
924 case MonoInlineField:
925 token = read32 (ip + 1);
926 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
927 mono_field_from_memberref (image, token, &class);
929 class = mono_class_get (image,
930 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
932 mono_class_init (class);
933 mono_class_vtable (domain, class);
936 case MonoInlineMethod:
937 if (method->wrapper_type == MONO_WRAPPER_NONE) {
938 m = mono_get_method (image, read32 (ip + 1), NULL);
939 mono_class_init (m->klass);
940 if (!(m->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
941 mono_class_vtable (domain, m->klass);
947 case MonoShortInlineR:
949 case MonoInlineBrTarget:
955 case MonoShortInlineVar:
956 case MonoShortInlineI:
957 case MonoShortInlineBrTarget:
960 case MonoInlineSwitch: {
973 g_assert_not_reached ();
977 method->info = offsets;
980 * We store the inline info in addr, since it's unused for IL methods.
982 if (method->klass == mono_defaults.string_class) {
983 if (strcmp (method->name, "get_Length") == 0)
984 method->addr = GUINT_TO_POINTER (INLINE_STRING_LENGTH);
985 } else if (method->klass == mono_defaults.array_class) {
986 if (strcmp (method->name, "get_Length") == 0)
987 method->addr = GUINT_TO_POINTER (INLINE_ARRAY_LENGTH);
988 else if (strcmp (method->name, "get_Rank") == 0 || strcmp (method->name, "GetRank") == 0)
989 method->addr = GUINT_TO_POINTER (INLINE_ARRAY_RANK);
990 } else if (method->klass == mono_defaults.monotype_class) {
991 if (strcmp (method->name, "GetElementType") == 0)
992 method->addr = GUINT_TO_POINTER (INLINE_TYPE_ELEMENT_TYPE);
994 LeaveCriticalSection (metadata_section);
995 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
998 #define LOCAL_POS(n) (frame->locals + offsets [2 + (n)])
999 #define LOCAL_TYPE(header, n) ((header)->locals [(n)])
1001 #define ARG_POS(n) (args_pointers [(n)])
1002 #define ARG_TYPE(sig, n) ((n) ? (sig)->params [(n) - (sig)->hasthis] : \
1003 (sig)->hasthis ? &frame->method->klass->this_arg: (sig)->params [(0)])
1005 #define THROW_EX(exception,ex_ip) \
1007 char *stack_trace; \
1008 frame->ip = (ex_ip); \
1009 stack_trace = dump_frame (frame); \
1010 frame->ex = (MonoException*)(exception); \
1011 frame->ex->stack_trace = mono_string_new (domain, stack_trace); \
1012 g_free (stack_trace); \
1013 goto handle_exception; \
1016 typedef struct _vtallocation vtallocation;
1018 struct _vtallocation {
1021 char data [MONO_ZERO_LEN_ARRAY];
1024 #define vt_alloc(vtype,sp,native) \
1025 if ((vtype)->type == MONO_TYPE_VALUETYPE && !(vtype)->data.klass->enumtype) { \
1026 if (!(vtype)->byref) { \
1029 vtallocation *tmp, *prev; \
1030 if (native) size = mono_class_native_size ((vtype)->data.klass, &align); \
1031 else size = mono_class_value_size ((vtype)->data.klass, &align); \
1034 while (tmp && (tmp->size < size)) { \
1039 tmp = alloca (sizeof (vtallocation) + size); \
1041 g_assert (size < 10000); \
1045 prev->next = tmp->next; \
1047 vtalloc = tmp->next; \
1048 (sp)->data.vt.vt = tmp->data; \
1050 (sp)->data.vt.klass = (vtype)->data.klass; \
1054 #define vt_free(sp) \
1056 if ((sp)->type == VAL_VALUET) { \
1057 vtallocation *tmp = (vtallocation*)(((char*)(sp)->data.vt.vt) - G_STRUCT_OFFSET (vtallocation, data)); \
1058 tmp->next = vtalloc; \
1065 verify_method (MonoMethod *m)
1067 GSList *errors, *tmp;
1068 MonoVerifyInfo *info;
1070 errors = mono_method_verify (m, MONO_VERIFY_ALL);
1072 g_print ("Method %s.%s::%s has invalid IL.\n", m->klass->name_space, m->klass->name, m->name);
1073 for (tmp = errors; tmp; tmp = tmp->next) {
1075 g_print ("%s\n", info->message);
1079 mono_free_verify_list (errors);
1083 #define MYGUINT64_MAX 18446744073709551615ULL
1084 #define MYGINT64_MAX 9223372036854775807LL
1085 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1087 #define MYGUINT32_MAX 4294967295U
1088 #define MYGINT32_MAX 2147483647
1089 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1091 #define CHECK_ADD_OVERFLOW(a,b) \
1092 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1093 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1095 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1096 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1098 #define CHECK_ADD_OVERFLOW64(a,b) \
1099 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1100 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1102 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1103 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1105 /* Resolves to TRUE if the operands would overflow */
1106 #define CHECK_MUL_OVERFLOW(a,b) \
1107 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1108 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1109 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1110 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1111 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1112 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1113 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1115 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1116 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1117 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1119 #define CHECK_MUL_OVERFLOW64(a,b) \
1120 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1121 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1122 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1123 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1124 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1125 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1126 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1128 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1129 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1130 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1133 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1135 MonoInvocation frame, *parent;
1136 MonoObject *retval = NULL;
1137 MonoMethodSignature *sig = method->signature;
1138 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1139 int i, type, isobject = 0;
1142 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1144 /* FIXME: Set frame for execption handling. */
1146 switch (sig->ret->type) {
1147 case MONO_TYPE_VOID:
1149 case MONO_TYPE_STRING:
1150 case MONO_TYPE_OBJECT:
1151 case MONO_TYPE_CLASS:
1152 case MONO_TYPE_ARRAY:
1153 case MONO_TYPE_SZARRAY:
1156 case MONO_TYPE_VALUETYPE:
1157 retval = mono_object_new (mono_domain_get (), klass);
1158 ret = ((char*)retval) + sizeof (MonoObject);
1159 if (!sig->ret->data.klass->enumtype)
1160 result.data.vt.vt = ret;
1163 retval = mono_object_new (mono_domain_get (), klass);
1164 ret = ((char*)retval) + sizeof (MonoObject);
1168 for (i = 0; i < sig->param_count; ++i) {
1169 if (sig->params [i]->byref) {
1170 args [i].type = VAL_POINTER;
1171 args [i].data.p = params [i];
1174 type = sig->params [i]->type;
1179 case MONO_TYPE_BOOLEAN:
1180 args [i].type = VAL_I32;
1181 args [i].data.i = *(MonoBoolean*)params [i];
1182 args [i].data.vt.klass = NULL;
1186 case MONO_TYPE_CHAR:
1187 args [i].type = VAL_I32;
1188 args [i].data.i = *(gint16*)params [i];
1189 args [i].data.vt.klass = NULL;
1191 #if SIZEOF_VOID_P == 4
1192 case MONO_TYPE_U: /* use VAL_POINTER? */
1197 args [i].type = VAL_I32;
1198 args [i].data.i = *(gint32*)params [i];
1199 args [i].data.vt.klass = NULL;
1201 #if SIZEOF_VOID_P == 8
1207 args [i].type = VAL_I64;
1208 args [i].data.l = *(gint64*)params [i];
1209 args [i].data.vt.klass = NULL;
1211 case MONO_TYPE_VALUETYPE:
1212 if (sig->params [i]->data.klass->enumtype) {
1213 type = sig->params [i]->data.klass->enum_basetype->type;
1216 args [i].type = VAL_POINTER;
1217 args [i].data.p = params [i];
1218 args [i].data.vt.klass = NULL;
1221 case MONO_TYPE_STRING:
1222 case MONO_TYPE_CLASS:
1223 case MONO_TYPE_ARRAY:
1224 case MONO_TYPE_SZARRAY:
1225 case MONO_TYPE_OBJECT:
1226 args [i].type = VAL_OBJ;
1227 args [i].data.p = params [i];
1228 args [i].data.vt.klass = NULL;
1231 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1235 if (method->klass->valuetype)
1236 /* Unbox the instance, since valuetype methods expect an interior pointer. */
1237 obj = mono_object_unbox (obj);
1239 /* chain with managed parent if any */
1240 parent = TlsGetValue (frame_thread_id);
1241 INIT_FRAME(&frame,parent,obj,args,&result,method);
1243 frame.invoke_trap = 1;
1244 ves_exec_method (&frame);
1245 if (exc && frame.ex) {
1246 *exc = (MonoObject*) frame.ex;
1249 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1251 if (isobject || method->string_ctor)
1252 return result.data.p;
1253 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1258 * Need to optimize ALU ops when natural int == int32
1260 * IDEA: if we maintain a stack of ip, sp to be checked
1261 * in the return opcode, we could inline simple methods that don't
1262 * use the stack or local variables....
1264 * The {,.S} versions of many opcodes can/should be merged to reduce code
1269 ves_exec_method (MonoInvocation *frame)
1271 MonoDomain *domain = mono_domain_get ();
1272 MonoInvocation child_frame;
1273 MonoMethodHeader *header;
1274 MonoMethodSignature *signature;
1276 GSList *finally_ips = NULL;
1277 const unsigned char *endfinally_ip;
1278 register const unsigned char *ip;
1279 register stackval *sp = NULL;
1280 void **args_pointers;
1282 gint il_ins_count = -1;
1283 gint tracing = global_tracing;
1284 unsigned char tail_recursion = 0;
1285 unsigned char unaligned_address = 0;
1286 unsigned char volatile_address = 0;
1287 vtallocation *vtalloc = NULL;
1288 MonoVTable *method_class_vt;
1291 if (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1292 frame->method = mono_marshal_get_native_wrapper (frame->method);
1294 method_class_vt = mono_class_vtable (domain, frame->method->klass);
1295 if (!method_class_vt->initialized)
1296 mono_runtime_class_init (method_class_vt);
1297 signature = frame->method->signature;
1301 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1302 if (!frame->method->addr) {
1303 /* ugly, but needed by the iflags setting in loader.c */
1304 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1305 ves_runtime_method (frame);
1307 goto handle_exception;
1311 if (frame->method->addr) {
1312 frame->ex = (MonoException*)mono_get_exception_missing_method ();
1313 goto handle_exception;
1316 ves_pinvoke_method (frame, frame->method->signature, frame->method->addr,
1317 frame->method->string_ctor);
1319 goto handle_exception;
1324 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1325 ves_runtime_method (frame);
1327 goto handle_exception;
1332 /*verify_method (frame->method);*/
1334 header = ((MonoMethodNormal *)frame->method)->header;
1335 image = frame->method->klass->image;
1337 if (!frame->method->info)
1338 calc_offsets (image, frame->method);
1339 offsets = frame->method->info;
1342 * with alloca we get the expected huge performance gain
1343 * stackval *stack = g_new0(stackval, header->max_stack);
1345 g_assert (header->max_stack < 10000);
1346 sp = frame->stack = alloca (sizeof (stackval) * header->max_stack);
1348 if (header->num_locals) {
1349 g_assert (offsets [0] < 10000);
1350 frame->locals = alloca (offsets [0]);
1352 * yes, we do it unconditionally, because it needs to be done for
1353 * some cases anyway and checking for that would be even slower.
1355 memset (frame->locals, 0, offsets [0]);
1358 * Copy args from stack_args to args.
1360 if (signature->param_count || signature->hasthis) {
1362 int has_this = signature->hasthis;
1364 g_assert (offsets [1] < 10000);
1365 frame->args = alloca (offsets [1]);
1366 g_assert ((signature->param_count + has_this) < 1000);
1367 args_pointers = alloca (sizeof(void*) * (signature->param_count + has_this));
1370 this_arg = args_pointers [0] = frame->args;
1371 *this_arg = frame->obj;
1373 for (i = 0; i < signature->param_count; ++i) {
1374 args_pointers [i + has_this] = frame->args + offsets [2 + header->num_locals + has_this + i];
1375 stackval_to_data (signature->params [i], frame->stack_args + i, args_pointers [i + has_this], signature->pinvoke);
1379 child_frame.parent = frame;
1380 frame->child = &child_frame;
1387 * using while (ip < end) may result in a 15% performance drop,
1388 * but it may be useful for debug
1392 /*g_assert (sp >= stack);*/
1396 char *ins, *discode;
1397 if (sp > frame->stack) {
1398 ins = dump_stack (frame->stack, sp);
1400 ins = g_strdup ("");
1403 discode = mono_disasm_code_one (NULL, frame->method, ip, NULL);
1404 discode [strlen (discode) - 1] = 0; /* no \n */
1405 g_print ("(%d) %-29s %s\n", GetCurrentThreadId(), discode, ins);
1409 if (il_ins_count > 0)
1410 if (!(--il_ins_count))
1420 G_BREAKPOINT (); /* this is not portable... */
1425 CASE (CEE_LDARG_3) {
1426 int n = (*ip)-CEE_LDARG_0;
1428 vt_alloc (ARG_TYPE (signature, n), sp, signature->pinvoke);
1429 stackval_from_data (ARG_TYPE (signature, n), sp, ARG_POS (n), signature->pinvoke);
1436 CASE (CEE_LDLOC_3) {
1437 int n = (*ip)-CEE_LDLOC_0;
1439 if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1441 sp->data.i = *(gint32*) LOCAL_POS (n);
1445 vt_alloc (LOCAL_TYPE (header, n), sp, FALSE);
1446 stackval_from_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1454 CASE (CEE_STLOC_3) {
1455 int n = (*ip)-CEE_STLOC_0;
1458 if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1459 gint32 *p = (gint32*)LOCAL_POS (n);
1463 stackval_to_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1470 vt_alloc (ARG_TYPE (signature, *ip), sp, signature->pinvoke);
1471 stackval_from_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1475 CASE (CEE_LDARGA_S) {
1480 t = ARG_TYPE (signature, *ip);
1481 c = mono_class_from_mono_type (t);
1482 sp->data.vt.klass = c;
1483 sp->data.vt.vt = ARG_POS (*ip);
1494 stackval_to_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1500 vt_alloc (LOCAL_TYPE (header, *ip), sp, FALSE);
1501 stackval_from_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1505 CASE (CEE_LDLOCA_S) {
1510 t = LOCAL_TYPE (header, *ip);
1511 c = mono_class_from_mono_type (t);
1512 sp->data.vt.klass = c;
1513 sp->data.p = LOCAL_POS (*ip);
1524 stackval_to_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1532 sp->data.vt.klass = NULL;
1535 CASE (CEE_LDC_I4_M1)
1551 sp->data.i = (*ip) - CEE_LDC_I4_0;
1558 sp->data.i = *(const gint8 *)ip;
1565 sp->data.i = read32 (ip);
1572 sp->data.l = read64 (ip);
1579 sp->type = VAL_DOUBLE;
1588 sp->type = VAL_DOUBLE;
1589 readr8(ip, &sp->data.f);
1593 CASE (CEE_UNUSED99) ves_abort (); BREAK;
1595 if (sp [-1].type == VAL_VALUET) {
1596 MonoClass *c = sp [-1].data.vt.klass;
1597 vt_alloc (&c->byval_arg, sp, FALSE);
1598 stackval_from_data (&c->byval_arg, sp, sp [-1].data.vt.vt, FALSE);
1616 token = read32 (ip);
1619 child_frame.method = mono_get_method (image, token, NULL);
1620 if (!child_frame.method)
1621 THROW_EX (mono_get_exception_missing_method (), ip -5);
1627 CASE (CEE_CALLVIRT) /* Fall through */
1628 CASE (CEE_CALLI) /* Fall through */
1630 MonoMethodSignature *csignature;
1632 stackval *endsp = sp;
1634 int virtual = *ip == CEE_CALLVIRT;
1635 int calli = *ip == CEE_CALLI;
1636 unsigned char *code = NULL;
1639 * We ignore tail recursion for now.
1646 token = read32 (ip);
1652 if (frame->method->wrapper_type != MONO_WRAPPER_NONE) {
1653 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (frame->method, token);
1654 child_frame.method = NULL;
1656 csignature = mono_metadata_parse_signature (image, token);
1657 if ((ji = mono_jit_info_table_find (mono_root_domain, code))) {
1658 child_frame.method = ji->method;
1660 g_assert_not_reached ();
1665 child_frame.method = mono_get_method (image, token, NULL);
1666 if (!child_frame.method)
1667 THROW_EX (mono_get_exception_missing_method (), ip -5);
1668 csignature = child_frame.method->signature;
1670 stackval *this_arg = &sp [-csignature->param_count-1];
1671 if (!this_arg->data.p)
1672 THROW_EX (mono_get_exception_null_reference(), ip - 5);
1673 child_frame.method = get_virtual_method (domain, child_frame.method, this_arg);
1674 if (!child_frame.method)
1675 THROW_EX (mono_get_exception_missing_method (), ip -5);
1679 if (frame->method->wrapper_type == MONO_WRAPPER_NONE)
1680 if (child_frame.method && child_frame.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1681 child_frame.method = mono_marshal_get_synchronized_wrapper (child_frame.method);
1683 g_assert (csignature->call_convention == MONO_CALL_DEFAULT);
1684 /* decrement by the actual number of args */
1685 if (csignature->param_count) {
1686 sp -= csignature->param_count;
1687 child_frame.stack_args = sp;
1689 child_frame.stack_args = NULL;
1691 if (csignature->hasthis) {
1692 g_assert (sp >= frame->stack);
1695 * It may also be a TP from LD(S)FLDA
1696 * g_assert (sp->type == VAL_OBJ || sp->type == VAL_MP);
1698 if (sp->type == VAL_OBJ && child_frame.method &&
1699 child_frame.method->klass->valuetype) /* unbox it */
1700 child_frame.obj = (char*)sp->data.p + sizeof (MonoObject);
1702 child_frame.obj = sp->data.p;
1704 child_frame.obj = NULL;
1706 if (csignature->ret->type != MONO_TYPE_VOID) {
1707 vt_alloc (csignature->ret, &retval, csignature->pinvoke);
1708 child_frame.retval = &retval;
1710 child_frame.retval = NULL;
1713 child_frame.ex = NULL;
1714 child_frame.ex_handler = NULL;
1716 if (!child_frame.method) {
1718 ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE);
1719 if (child_frame.ex) {
1720 frame->ex = child_frame.ex;
1721 goto handle_exception;
1723 } else if (csignature->hasthis && sp->type == VAL_OBJ &&
1724 ((MonoObject *)sp->data.p)->vtable->klass == mono_defaults.transparent_proxy_class) {
1725 g_assert (child_frame.method);
1726 child_frame.method = mono_marshal_get_remoting_invoke (child_frame.method);
1727 ves_exec_method (&child_frame);
1729 switch (GPOINTER_TO_UINT (child_frame.method->addr)) {
1730 case INLINE_STRING_LENGTH:
1731 retval.type = VAL_I32;
1732 retval.data.i = ((MonoString*)sp->data.p)->length;
1733 /*g_print ("length of '%s' is %d\n", mono_string_to_utf8 (sp->data.p), retval.data.i);*/
1735 case INLINE_ARRAY_LENGTH:
1736 retval.type = VAL_I32;
1737 retval.data.i = mono_array_length ((MonoArray*)sp->data.p);
1739 case INLINE_ARRAY_RANK:
1740 retval.type = VAL_I32;
1741 retval.data.i = mono_object_class (sp->data.p)->rank;
1743 case INLINE_TYPE_ELEMENT_TYPE:
1744 retval.type = VAL_OBJ;
1746 MonoClass *c = mono_class_from_mono_type (((MonoReflectionType*)sp->data.p)->type);
1747 retval.data.vt.klass = NULL;
1748 if (c->enumtype && c->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1749 retval.data.p = mono_type_get_object (domain, c->enum_basetype);
1750 else if (c->element_class)
1751 retval.data.p = mono_type_get_object (domain, &c->element_class->byval_arg);
1753 retval.data.p = NULL;
1757 ves_exec_method (&child_frame);
1761 while (endsp > sp) {
1766 if (child_frame.ex) {
1768 * An exception occurred, need to run finally, fault and catch handlers..
1770 frame->ex = child_frame.ex;
1771 goto handle_finally;
1774 /* need to handle typedbyref ... */
1775 if (csignature->ret->type != MONO_TYPE_VOID) {
1782 if (signature->ret->type != MONO_TYPE_VOID) {
1784 if (sp->type == VAL_VALUET) {
1785 /* the caller has already allocated the memory */
1786 stackval_from_data (signature->ret, frame->retval, sp->data.vt.vt, signature->pinvoke);
1789 *frame->retval = *sp;
1792 if (sp > frame->stack)
1793 g_warning ("more values on stack: %d", sp-frame->stack);
1797 CASE (CEE_BR_S) /* Fall through */
1799 if (*ip == CEE_BR) {
1801 ip += (gint32) read32(ip);
1805 ip += (signed char) *ip;
1809 CASE (CEE_BRFALSE) /* Fall through */
1810 CASE (CEE_BRFALSE_S) {
1813 if (*ip == CEE_BRFALSE_S) {
1814 broffset = (signed char)ip [1];
1817 broffset = (gint32) read32 (ip + 1);
1822 case VAL_I32: result = sp->data.i == 0; break;
1823 case VAL_I64: result = sp->data.l == 0; break;
1824 case VAL_DOUBLE: result = sp->data.f ? 0: 1; break;
1825 default: result = sp->data.p == NULL; break;
1831 CASE (CEE_BRTRUE) /* Fall through */
1832 CASE (CEE_BRTRUE_S) {
1835 if (*ip == CEE_BRTRUE_S) {
1836 broffset = (signed char)ip [1];
1839 broffset = (gint32) read32 (ip + 1);
1844 case VAL_I32: result = sp->data.i != 0; break;
1845 case VAL_I64: result = sp->data.l != 0; break;
1846 case VAL_DOUBLE: result = sp->data.f ? 1 : 0; break;
1847 default: result = sp->data.p != NULL; break;
1853 CASE (CEE_BEQ) /* Fall through */
1857 if (*ip == CEE_BEQ_S) {
1858 broffset = (signed char)ip [1];
1861 broffset = (gint32) read32 (ip + 1);
1865 if (sp->type == VAL_I32)
1866 result = sp [0].data.i == (gint)GET_NATI (sp [1]);
1867 else if (sp->type == VAL_I64)
1868 result = sp [0].data.l == sp [1].data.l;
1869 else if (sp->type == VAL_DOUBLE)
1870 result = sp [0].data.f == sp [1].data.f;
1872 result = (gint)GET_NATI (sp [0]) == (gint)GET_NATI (sp [1]);
1877 CASE (CEE_BGE) /* Fall through */
1881 if (*ip == CEE_BGE_S) {
1882 broffset = (signed char)ip [1];
1885 broffset = (gint32) read32 (ip + 1);
1889 if (sp->type == VAL_I32)
1890 result = sp [0].data.i >= (gint)GET_NATI (sp [1]);
1891 else if (sp->type == VAL_I64)
1892 result = sp [0].data.l >= sp [1].data.l;
1893 else if (sp->type == VAL_DOUBLE)
1894 result = sp [0].data.f >= sp [1].data.f;
1896 result = (gint)GET_NATI (sp [0]) >= (gint)GET_NATI (sp [1]);
1901 CASE (CEE_BGT) /* Fall through */
1905 if (*ip == CEE_BGT_S) {
1906 broffset = (signed char)ip [1];
1909 broffset = (gint32) read32 (ip + 1);
1913 if (sp->type == VAL_I32)
1914 result = sp [0].data.i > (gint)GET_NATI (sp [1]);
1915 else if (sp->type == VAL_I64)
1916 result = sp [0].data.l > sp [1].data.l;
1917 else if (sp->type == VAL_DOUBLE)
1918 result = sp [0].data.f > sp [1].data.f;
1920 result = (gint)GET_NATI (sp [0]) > (gint)GET_NATI (sp [1]);
1925 CASE (CEE_BLT) /* Fall through */
1929 if (*ip == CEE_BLT_S) {
1930 broffset = (signed char)ip [1];
1933 broffset = (gint32) read32 (ip + 1);
1937 if (sp->type == VAL_I32)
1938 result = sp[0].data.i < (gint)GET_NATI(sp[1]);
1939 else if (sp->type == VAL_I64)
1940 result = sp[0].data.l < sp[1].data.l;
1941 else if (sp->type == VAL_DOUBLE)
1942 result = sp[0].data.f < sp[1].data.f;
1944 result = (gint)GET_NATI(sp[0]) < (gint)GET_NATI(sp[1]);
1949 CASE (CEE_BLE) /* fall through */
1953 if (*ip == CEE_BLE_S) {
1954 broffset = (signed char)ip [1];
1957 broffset = (gint32) read32 (ip + 1);
1962 if (sp->type == VAL_I32)
1963 result = sp [0].data.i <= (gint)GET_NATI (sp [1]);
1964 else if (sp->type == VAL_I64)
1965 result = sp [0].data.l <= sp [1].data.l;
1966 else if (sp->type == VAL_DOUBLE)
1967 result = sp [0].data.f <= sp [1].data.f;
1970 * FIXME: here and in other places GET_NATI on the left side
1971 * _will_ be wrong when we change the macro to work on 64 bits
1974 result = (gint)GET_NATI (sp [0]) <= (gint)GET_NATI (sp [1]);
1980 CASE (CEE_BNE_UN) /* Fall through */
1981 CASE (CEE_BNE_UN_S) {
1984 if (*ip == CEE_BNE_UN_S) {
1985 broffset = (signed char)ip [1];
1988 broffset = (gint32) read32 (ip + 1);
1992 if (sp->type == VAL_I32)
1993 result = (guint32)sp [0].data.i != (guint32)GET_NATI (sp [1]);
1994 else if (sp->type == VAL_I64)
1995 result = (guint64)sp [0].data.l != (guint64)sp [1].data.l;
1996 else if (sp->type == VAL_DOUBLE)
1997 result = isunordered (sp [0].data.f, sp [1].data.f) ||
1998 (sp [0].data.f != sp [1].data.f);
2000 result = GET_NATI (sp [0]) != GET_NATI (sp [1]);
2005 CASE (CEE_BGE_UN) /* Fall through */
2006 CASE (CEE_BGE_UN_S) {
2009 if (*ip == CEE_BGE_UN_S) {
2010 broffset = (signed char)ip [1];
2013 broffset = (gint32) read32 (ip + 1);
2017 if (sp->type == VAL_I32)
2018 result = (guint32)sp [0].data.i >= (guint32)GET_NATI (sp [1]);
2019 else if (sp->type == VAL_I64)
2020 result = (guint64)sp [0].data.l >= (guint64)sp [1].data.l;
2021 else if (sp->type == VAL_DOUBLE)
2022 result = !isless (sp [0].data.f,sp [1].data.f);
2024 result = GET_NATI (sp [0]) >= GET_NATI (sp [1]);
2029 CASE (CEE_BGT_UN) /* Fall through */
2030 CASE (CEE_BGT_UN_S) {
2033 if (*ip == CEE_BGT_UN_S) {
2034 broffset = (signed char)ip [1];
2037 broffset = (gint32) read32 (ip + 1);
2041 if (sp->type == VAL_I32)
2042 result = (guint32)sp [0].data.i > (guint32)GET_NATI (sp [1]);
2043 else if (sp->type == VAL_I64)
2044 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
2045 else if (sp->type == VAL_DOUBLE)
2046 result = isgreater (sp [0].data.f, sp [1].data.f);
2048 result = GET_NATI (sp [0]) > GET_NATI (sp [1]);
2053 CASE (CEE_BLE_UN) /* Fall through */
2054 CASE (CEE_BLE_UN_S) {
2057 if (*ip == CEE_BLE_UN_S) {
2058 broffset = (signed char)ip [1];
2061 broffset = (gint32) read32 (ip + 1);
2065 if (sp->type == VAL_I32)
2066 result = (guint32)sp [0].data.i <= (guint32)GET_NATI (sp [1]);
2067 else if (sp->type == VAL_I64)
2068 result = (guint64)sp [0].data.l <= (guint64)sp [1].data.l;
2069 else if (sp->type == VAL_DOUBLE)
2070 result = islessequal (sp [0].data.f, sp [1].data.f);
2072 result = GET_NATI (sp [0]) <= GET_NATI (sp [1]);
2077 CASE (CEE_BLT_UN) /* Fall through */
2078 CASE (CEE_BLT_UN_S) {
2081 if (*ip == CEE_BLT_UN_S) {
2082 broffset = (signed char)ip [1];
2085 broffset = (gint32) read32 (ip + 1);
2089 if (sp->type == VAL_I32)
2090 result = (guint32)sp[0].data.i < (guint32)GET_NATI(sp[1]);
2091 else if (sp->type == VAL_I64)
2092 result = (guint64)sp[0].data.l < (guint64)sp[1].data.l;
2093 else if (sp->type == VAL_DOUBLE)
2094 result = isunordered (sp [0].data.f, sp [1].data.f) ||
2095 (sp [0].data.f < sp [1].data.f);
2097 result = GET_NATI(sp[0]) < GET_NATI(sp[1]);
2104 const unsigned char *st;
2108 st = ip + sizeof (gint32) * n;
2110 if ((guint32)sp->data.i < n) {
2112 ip += sizeof (gint32) * (guint32)sp->data.i;
2113 offset = read32 (ip);
2122 sp[-1].type = VAL_I32;
2123 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2127 sp[-1].type = VAL_I32;
2128 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2132 sp[-1].type = VAL_I32;
2133 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2137 sp[-1].type = VAL_I32;
2138 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2140 CASE (CEE_LDIND_I4) /* Fall through */
2143 sp[-1].type = VAL_I32;
2144 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2148 sp[-1].type = VAL_I64;
2149 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2153 sp[-1].type = VAL_NATI;
2154 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2158 sp[-1].type = VAL_DOUBLE;
2159 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2163 sp[-1].type = VAL_DOUBLE;
2164 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2166 CASE (CEE_LDIND_REF)
2168 sp[-1].type = VAL_OBJ;
2169 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2170 sp[-1].data.vt.klass = NULL;
2172 CASE (CEE_STIND_REF) {
2180 CASE (CEE_STIND_I1) {
2185 *p = (gint8)sp[1].data.i;
2188 CASE (CEE_STIND_I2) {
2193 *p = (gint16)sp[1].data.i;
2196 CASE (CEE_STIND_I4) {
2204 CASE (CEE_STIND_I) {
2209 *p = (mono_i)sp[1].data.p;
2212 CASE (CEE_STIND_I8) {
2220 CASE (CEE_STIND_R4) {
2225 *p = (gfloat)sp[1].data.f;
2228 CASE (CEE_STIND_R8) {
2239 /* should probably consider the pointers as unsigned */
2240 if (sp->type == VAL_I32)
2241 sp [-1].data.i += GET_NATI (sp [0]);
2242 else if (sp->type == VAL_I64)
2243 sp [-1].data.l += sp [0].data.l;
2244 else if (sp->type == VAL_DOUBLE)
2245 sp [-1].data.f += sp [0].data.f;
2247 char *p = sp [-1].data.p;
2248 p += GET_NATI (sp [0]);
2255 /* should probably consider the pointers as unsigned */
2256 if (sp->type == VAL_I32)
2257 sp [-1].data.i -= GET_NATI (sp [0]);
2258 else if (sp->type == VAL_I64)
2259 sp [-1].data.l -= sp [0].data.l;
2260 else if (sp->type == VAL_DOUBLE)
2261 sp [-1].data.f -= sp [0].data.f;
2263 char *p = sp [-1].data.p;
2264 p -= GET_NATI (sp [0]);
2271 if (sp->type == VAL_I32)
2272 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
2273 else if (sp->type == VAL_I64)
2274 sp [-1].data.l *= sp [0].data.l;
2275 else if (sp->type == VAL_DOUBLE)
2276 sp [-1].data.f *= sp [0].data.f;
2281 if (sp->type == VAL_I32) {
2282 if (GET_NATI (sp [0]) == 0)
2283 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2284 sp [-1].data.i /= (gint)GET_NATI (sp [0]);
2285 } else if (sp->type == VAL_I64) {
2286 if (sp [0].data.l == 0)
2287 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2288 sp [-1].data.l /= sp [0].data.l;
2289 } else if (sp->type == VAL_DOUBLE) {
2290 /* set NaN is divisor is 0.0 */
2291 sp [-1].data.f /= sp [0].data.f;
2297 if (sp->type == VAL_I32) {
2299 if (GET_NATI (sp [0]) == 0)
2300 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2301 val = sp [-1].data.i;
2302 val /= (guint32)GET_NATI (sp [0]);
2303 sp [-1].data.i = val;
2304 } else if (sp->type == VAL_I64) {
2306 if (sp [0].data.l == 0)
2307 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2308 val = sp [-1].data.l;
2309 val /= (guint64)sp [0].data.l;
2310 sp [-1].data.l = val;
2311 } else if (sp->type == VAL_NATI) {
2313 if (GET_NATI (sp [0]) == 0)
2314 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2315 val = (mono_u)sp [-1].data.p;
2316 val /= (mono_u)sp [0].data.p;
2317 sp [-1].data.p = (gpointer)val;
2323 if (sp->type == VAL_I32) {
2324 if (GET_NATI (sp [0]) == 0)
2325 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2326 sp [-1].data.i %= (gint)GET_NATI (sp [0]);
2327 } else if (sp->type == VAL_I64) {
2328 if (sp [0].data.l == 0)
2329 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2330 sp [-1].data.l %= sp [0].data.l;
2331 } else if (sp->type == VAL_DOUBLE) {
2332 /* FIXME: what do we actually do here? */
2333 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2335 if (GET_NATI (sp [0]) == 0)
2336 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2337 (gint)GET_NATI (sp [-1]) %= (gint)GET_NATI (sp [0]);
2343 if (sp->type == VAL_I32) {
2344 if (GET_NATI (sp [0]) == 0)
2345 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2346 (guint)sp [-1].data.i %= (guint)GET_NATI (sp [0]);
2347 } else if (sp->type == VAL_I64) {
2348 if (sp [0].data.l == 0)
2349 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2350 (guint64)sp [-1].data.l %= (guint64)sp [0].data.l;
2351 } else if (sp->type == VAL_DOUBLE) {
2352 /* unspecified behaviour according to the spec */
2354 if (GET_NATI (sp [0]) == 0)
2355 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2356 (guint64)GET_NATI (sp [-1]) %= (guint64)GET_NATI (sp [0]);
2362 if (sp->type == VAL_I32)
2363 sp [-1].data.i &= GET_NATI (sp [0]);
2364 else if (sp->type == VAL_I64)
2365 sp [-1].data.l &= sp [0].data.l;
2367 GET_NATI (sp [-1]) &= GET_NATI (sp [0]);
2372 if (sp->type == VAL_I32)
2373 sp [-1].data.i |= GET_NATI (sp [0]);
2374 else if (sp->type == VAL_I64)
2375 sp [-1].data.l |= sp [0].data.l;
2377 GET_NATI (sp [-1]) |= GET_NATI (sp [0]);
2382 if (sp->type == VAL_I32)
2383 sp [-1].data.i ^= GET_NATI (sp [0]);
2384 else if (sp->type == VAL_I64)
2385 sp [-1].data.l ^= sp [0].data.l;
2387 GET_NATI (sp [-1]) ^= GET_NATI (sp [0]);
2392 if (sp [-1].type == VAL_I32)
2393 sp [-1].data.i <<= GET_NATI (sp [0]);
2394 else if (sp [-1].type == VAL_I64)
2395 sp [-1].data.l <<= GET_NATI (sp [0]);
2397 GET_NATI (sp [-1]) <<= GET_NATI (sp [0]);
2402 if (sp [-1].type == VAL_I32)
2403 sp [-1].data.i >>= GET_NATI (sp [0]);
2404 else if (sp [-1].type == VAL_I64)
2405 sp [-1].data.l >>= GET_NATI (sp [0]);
2407 (gint)GET_NATI (sp [-1]) >>= GET_NATI (sp [0]);
2412 if (sp [-1].type == VAL_I32)
2413 (guint)sp [-1].data.i >>= GET_NATI (sp [0]);
2414 else if (sp [-1].type == VAL_I64)
2415 (guint64)sp [-1].data.l >>= GET_NATI (sp [0]);
2417 (guint64)GET_NATI (sp [-1]) >>= GET_NATI (sp [0]);
2422 if (sp->type == VAL_I32)
2423 sp->data.i = - sp->data.i;
2424 else if (sp->type == VAL_I64)
2425 sp->data.l = - sp->data.l;
2426 else if (sp->type == VAL_DOUBLE)
2427 sp->data.f = - sp->data.f;
2428 else if (sp->type == VAL_NATI)
2429 sp->data.p = (gpointer)(- (mono_i)sp->data.p);
2435 if (sp->type == VAL_I32)
2436 sp->data.i = ~ sp->data.i;
2437 else if (sp->type == VAL_I64)
2438 sp->data.l = ~ sp->data.l;
2439 else if (sp->type == VAL_NATI)
2440 sp->data.p = (gpointer)(~ (mono_i)sp->data.p);
2443 CASE (CEE_CONV_U1) /* fall through */
2444 CASE (CEE_CONV_I1) {
2446 switch (sp [-1].type) {
2448 sp [-1].data.i = (gint8)sp [-1].data.f;
2451 sp [-1].data.i = (gint8)sp [-1].data.l;
2456 sp [-1].data.i = (gint8)sp [-1].data.i;
2459 sp [-1].data.i = (gint8)sp [-1].data.nati;
2462 sp [-1].type = VAL_I32;
2465 CASE (CEE_CONV_U2) /* fall through */
2466 CASE (CEE_CONV_I2) {
2468 switch (sp [-1].type) {
2470 sp [-1].data.i = (gint16)sp [-1].data.f;
2473 sp [-1].data.i = (gint16)sp [-1].data.l;
2478 sp [-1].data.i = (gint16)sp [-1].data.i;
2481 sp [-1].data.i = (gint16)sp [-1].data.nati;
2484 sp [-1].type = VAL_I32;
2487 CASE (CEE_CONV_U4) /* Fall through */
2488 #if SIZEOF_VOID_P == 4
2489 CASE (CEE_CONV_I) /* Fall through */
2490 CASE (CEE_CONV_U) /* Fall through */
2492 CASE (CEE_CONV_I4) {
2494 switch (sp [-1].type) {
2496 sp [-1].data.i = (gint32)sp [-1].data.f;
2499 sp [-1].data.i = (gint32)sp [-1].data.l;
2506 sp [-1].data.i = (gint32)sp [-1].data.p;
2509 sp [-1].type = VAL_I32;
2512 #if SIZEOF_VOID_P == 8
2513 CASE (CEE_CONV_I) /* Fall through */
2517 switch (sp [-1].type) {
2519 sp [-1].data.l = (gint64)sp [-1].data.f;
2526 sp [-1].data.l = (gint64)sp [-1].data.i;
2529 sp [-1].data.l = (gint64)sp [-1].data.nati;
2532 sp [-1].type = VAL_I64;
2534 CASE (CEE_CONV_R4) {
2536 switch (sp [-1].type) {
2538 sp [-1].data.f = (float)sp [-1].data.f;
2541 sp [-1].data.f = (float)sp [-1].data.l;
2546 sp [-1].data.f = (float)sp [-1].data.i;
2549 sp [-1].data.f = (float)sp [-1].data.nati;
2552 sp [-1].type = VAL_DOUBLE;
2555 CASE (CEE_CONV_R8) {
2557 switch (sp [-1].type) {
2559 sp [-1].data.f = (double)sp [-1].data.f;
2562 sp [-1].data.f = (double)sp [-1].data.l;
2567 sp [-1].data.f = (double)sp [-1].data.i;
2570 sp [-1].data.f = (double)sp [-1].data.nati;
2573 sp [-1].type = VAL_DOUBLE;
2576 #if SIZEOF_VOID_P == 8
2577 CASE (CEE_CONV_U) /* Fall through */
2582 switch (sp [-1].type){
2584 sp [-1].data.l = (guint64)sp [-1].data.f;
2591 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2594 sp [-1].data.l = (guint64) sp [-1].data.nati;
2597 sp [-1].type = VAL_I64;
2602 vtklass = mono_class_get (image, read32 (ip));
2605 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2614 token = read32 (ip);
2617 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
2618 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
2620 c = mono_class_get (image, token);
2622 addr = sp [-1].data.vt.vt;
2623 vt_alloc (&c->byval_arg, &sp [-1], FALSE);
2624 stackval_from_data (&c->byval_arg, &sp [-1], addr, FALSE);
2632 str_index = mono_metadata_token_index (read32 (ip));
2635 o = (MonoObject*)mono_ldstr (domain, image, str_index);
2638 sp->data.vt.klass = NULL;
2645 MonoClass *newobj_class;
2646 MonoMethodSignature *csig;
2647 stackval valuetype_this;
2648 stackval *endsp = sp;
2655 token = read32 (ip);
2658 if (!(child_frame.method = mono_get_method (image, token, NULL)))
2659 THROW_EX (mono_get_exception_missing_method (), ip -5);
2661 csig = child_frame.method->signature;
2662 newobj_class = child_frame.method->klass;
2663 /*if (profiling_classes) {
2664 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2666 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2670 if (newobj_class->parent == mono_defaults.array_class) {
2671 sp -= csig->param_count;
2672 o = ves_array_create (domain, newobj_class, csig, sp);
2673 goto array_constructed;
2677 * First arg is the object.
2679 if (newobj_class->valuetype) {
2681 vt_alloc (&newobj_class->byval_arg, &valuetype_this, csig->pinvoke);
2682 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2683 zero = valuetype_this.data.vt.vt;
2684 child_frame.obj = valuetype_this.data.vt.vt;
2686 memset (&valuetype_this, 0, sizeof (stackval));
2687 zero = &valuetype_this;
2688 child_frame.obj = &valuetype_this;
2690 stackval_from_data (&newobj_class->byval_arg, &valuetype_this, zero, csig->pinvoke);
2692 if (newobj_class != mono_defaults.string_class) {
2693 o = mono_object_new (domain, newobj_class);
2694 child_frame.obj = o;
2696 child_frame.retval = &retval;
2700 if (csig->param_count) {
2701 sp -= csig->param_count;
2702 child_frame.stack_args = sp;
2704 child_frame.stack_args = NULL;
2707 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2709 child_frame.ex = NULL;
2710 child_frame.ex_handler = NULL;
2712 ves_exec_method (&child_frame);
2714 while (endsp > sp) {
2719 if (child_frame.ex) {
2721 * An exception occurred, need to run finally, fault and catch handlers..
2723 frame->ex = child_frame.ex;
2724 goto handle_finally;
2727 * a constructor returns void, but we need to return the object we created
2730 if (newobj_class->valuetype && !newobj_class->enumtype) {
2731 *sp = valuetype_this;
2732 } else if (newobj_class == mono_defaults.string_class) {
2737 sp->data.vt.klass = newobj_class;
2742 CASE (CEE_CASTCLASS) /* Fall through */
2746 MonoClass *c , *oclass;
2748 int do_isinst = *ip == CEE_ISINST;
2751 token = read32 (ip);
2752 c = mono_class_get (image, token);
2754 g_assert (sp [-1].type == VAL_OBJ);
2756 if ((o = sp [-1].data.p)) {
2760 if (mono_object_isinst (o, c)) {
2761 sp [-1].data.vt.klass = c;
2765 sp [-1].data.p = NULL;
2766 sp [-1].data.vt.klass = NULL;
2768 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
2774 CASE (CEE_CONV_R_UN)
2776 switch (sp [-1].type) {
2780 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2785 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2788 sp [-1].data.f = (double)(guint64)sp [-1].data.nati;
2791 sp [-1].type = VAL_DOUBLE;
2794 CASE (CEE_UNUSED1) ves_abort(); BREAK;
2801 token = read32 (ip);
2803 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
2804 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
2806 c = mono_class_get (image, token);
2810 THROW_EX (mono_get_exception_null_reference(), ip - 1);
2812 if (o->vtable->klass->element_class->type_token != c->element_class->type_token)
2813 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
2815 sp [-1].type = VAL_MP;
2816 sp [-1].data.p = (char *)o + sizeof (MonoObject);
2823 frame->ex_handler = NULL;
2825 sp->data.p = mono_get_exception_null_reference ();
2826 THROW_EX (sp->data.p, ip);
2828 CASE (CEE_LDFLDA) /* Fall through */
2831 MonoClassField *field;
2833 int load_addr = *ip == CEE_LDFLDA;
2836 if (!sp [-1].data.p)
2837 THROW_EX (mono_get_exception_null_reference (), ip);
2840 token = read32 (ip);
2843 if (sp [-1].type == VAL_OBJ) {
2844 obj = sp [-1].data.p;
2846 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) {
2847 MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
2848 field = mono_class_get_field (klass, token);
2849 addr = mono_load_remote_field (obj, klass, field, NULL);
2851 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
2852 field = mono_field_from_memberref (image, token, NULL);
2854 field = mono_class_get_field (obj->vtable->klass, token);
2855 addr = (char*)obj + field->offset;
2858 obj = sp [-1].data.vt.vt;
2859 field = mono_class_get_field (sp [-1].data.vt.klass, token);
2860 addr = (char*)obj + field->offset - sizeof (MonoObject);
2864 sp [-1].type = VAL_MP;
2865 sp [-1].data.p = addr;
2866 sp [-1].data.vt.klass = mono_class_from_mono_type (field->type);
2868 vt_alloc (field->type, &sp [-1], FALSE);
2869 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2876 MonoClassField *field;
2877 guint32 token, offset;
2882 THROW_EX (mono_get_exception_null_reference (), ip);
2885 token = read32 (ip);
2888 if (sp [0].type == VAL_OBJ) {
2889 obj = sp [0].data.p;
2891 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) {
2892 MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
2893 field = mono_class_get_field (klass, token);
2895 mono_store_remote_field (obj, klass, field, &sp [1].data);
2896 offset = field->offset;
2898 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
2899 field = mono_field_from_memberref (image, token, NULL);
2901 field = mono_class_get_field (obj->vtable->klass, token);
2902 offset = field->offset;
2903 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
2907 obj = sp [0].data.vt.vt;
2908 field = mono_class_get_field (sp [0].data.vt.klass, token);
2909 offset = field->offset - sizeof (MonoObject);
2910 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
2916 CASE (CEE_LDSFLD) /* Fall through */
2917 CASE (CEE_LDSFLDA) {
2920 MonoClassField *field;
2922 int load_addr = *ip == CEE_LDSFLDA;
2926 token = read32 (ip);
2929 /* need to handle fieldrefs */
2930 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
2931 field = mono_field_from_memberref (image, token, &klass);
2933 klass = mono_class_get (image,
2934 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
2935 field = mono_class_get_field (klass, token);
2939 vt = mono_class_vtable (domain, klass);
2940 if (!vt->initialized)
2941 mono_runtime_class_init (vt);
2942 if (!domain->thread_static_fields || !(addr = g_hash_table_lookup (domain->thread_static_fields, field)))
2943 addr = (char*)(vt->data) + field->offset;
2945 addr = mono_threads_get_static_data (GPOINTER_TO_UINT (addr));
2950 sp->data.vt.klass = mono_class_from_mono_type (field->type);
2952 vt_alloc (field->type, sp, FALSE);
2953 stackval_from_data (field->type, sp, addr, FALSE);
2961 MonoClassField *field;
2966 token = read32 (ip);
2970 /* need to handle fieldrefs */
2971 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
2972 field = mono_field_from_memberref (image, token, &klass);
2974 klass = mono_class_get (image,
2975 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
2976 field = mono_class_get_field (klass, token);
2980 vt = mono_class_vtable (domain, klass);
2981 if (!vt->initialized)
2982 mono_runtime_class_init (vt);
2983 if (!domain->thread_static_fields || !(addr = g_hash_table_lookup (domain->thread_static_fields, field)))
2984 addr = (char*)(vt->data) + field->offset;
2986 addr = mono_threads_get_static_data (GPOINTER_TO_UINT (addr));
2988 stackval_to_data (field->type, sp, addr, FALSE);
2995 vtklass = mono_class_get (image, read32 (ip));
3000 * LAMESPEC: According to the spec, the stack should contain a
3001 * pointer to a value type. In reality, it can contain anything.
3003 if (sp [1].type == VAL_VALUET)
3004 memcpy (sp [0].data.p, sp [1].data.vt.vt, mono_class_value_size (vtklass, NULL));
3006 memcpy (sp [0].data.p, &sp [1].data, mono_class_value_size (vtklass, NULL));
3009 #if SIZEOF_VOID_P == 8
3010 CASE (CEE_CONV_OVF_I_UN)
3012 CASE (CEE_CONV_OVF_I8_UN) {
3013 switch (sp [-1].type) {
3015 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3016 THROW_EX (mono_get_exception_overflow (), ip);
3017 sp [-1].data.l = (guint64)sp [-1].data.f;
3024 /* Can't overflow */
3025 sp [-1].data.l = (guint64)sp [-1].data.i;
3028 sp [-1].data.l = (guint64)sp [-1].data.nati;
3031 sp [-1].type = VAL_I64;
3035 #if SIZEOF_VOID_P == 8
3036 CASE (CEE_CONV_OVF_U_UN)
3038 CASE (CEE_CONV_OVF_U8_UN) {
3039 switch (sp [-1].type) {
3041 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT64_MAX)
3042 THROW_EX (mono_get_exception_overflow (), ip);
3043 sp [-1].data.l = (guint64)sp [-1].data.f;
3051 /* Can't overflow */
3052 sp [-1].data.l = (guint64)sp [-1].data.i;
3055 /* Can't overflow */
3056 sp [-1].data.l = (guint64)sp [-1].data.nati;
3059 sp [-1].type = VAL_I64;
3063 #if SIZEOF_VOID_P == 4
3064 CASE (CEE_CONV_OVF_I_UN)
3065 CASE (CEE_CONV_OVF_U_UN)
3067 CASE (CEE_CONV_OVF_I1_UN)
3068 CASE (CEE_CONV_OVF_I2_UN)
3069 CASE (CEE_CONV_OVF_I4_UN)
3070 CASE (CEE_CONV_OVF_U1_UN)
3071 CASE (CEE_CONV_OVF_U2_UN)
3072 CASE (CEE_CONV_OVF_U4_UN) {
3074 switch (sp [-1].type) {
3076 value = (guint64)sp [-1].data.f;
3079 value = (guint64)sp [-1].data.l;
3084 value = (guint64)sp [-1].data.i;
3087 value = (guint64)sp [-1].data.nati;
3091 case CEE_CONV_OVF_I1_UN:
3093 THROW_EX (mono_get_exception_overflow (), ip);
3094 sp [-1].data.i = value;
3095 sp [-1].type = VAL_I32;
3097 case CEE_CONV_OVF_I2_UN:
3099 THROW_EX (mono_get_exception_overflow (), ip);
3100 sp [-1].data.i = value;
3101 sp [-1].type = VAL_I32;
3103 #if SIZEOF_VOID_P == 4
3104 case CEE_CONV_OVF_I_UN: /* Fall through */
3106 case CEE_CONV_OVF_I4_UN:
3107 if (value > MYGUINT32_MAX)
3108 THROW_EX (mono_get_exception_overflow (), ip);
3109 sp [-1].data.i = value;
3110 sp [-1].type = VAL_I32;
3112 case CEE_CONV_OVF_U1_UN:
3114 THROW_EX (mono_get_exception_overflow (), ip);
3115 sp [-1].data.i = value;
3116 sp [-1].type = VAL_I32;
3118 case CEE_CONV_OVF_U2_UN:
3120 THROW_EX (mono_get_exception_overflow (), ip);
3121 sp [-1].data.i = value;
3122 sp [-1].type = VAL_I32;
3124 #if SIZEOF_VOID_P == 4
3125 case CEE_CONV_OVF_U_UN: /* Fall through */
3127 case CEE_CONV_OVF_U4_UN:
3128 if (value > 4294967295U)
3129 THROW_EX (mono_get_exception_overflow (), ip);
3130 sp [-1].data.i = value;
3131 sp [-1].type = VAL_I32;
3134 g_assert_not_reached ();
3144 token = read32 (ip);
3146 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3147 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3149 class = mono_class_get (image, token);
3150 g_assert (class != NULL);
3152 sp [-1].type = VAL_OBJ;
3153 if (class->byval_arg.type == MONO_TYPE_VALUETYPE && !class->enumtype)
3154 sp [-1].data.p = mono_value_box (domain, class, sp [-1].data.p);
3156 stackval_to_data (&class->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3157 sp [-1].data.p = mono_value_box (domain, class, &sp [-1]);
3159 /* need to vt_free (sp); */
3171 token = read32 (ip);
3173 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3174 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3176 class = mono_class_get (image, token);
3178 o = (MonoObject*) mono_array_new (domain, class, sp [-1].data.i);
3181 sp [-1].type = VAL_OBJ;
3183 /*if (profiling_classes) {
3184 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3186 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3196 g_assert (sp [-1].type == VAL_OBJ);
3200 THROW_EX (mono_get_exception_null_reference (), ip - 1);
3202 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3204 sp [-1].type = VAL_I32;
3205 sp [-1].data.i = mono_array_length (o);
3209 CASE (CEE_LDELEMA) {
3211 guint32 esize, token;
3215 token = read32 (ip);
3219 g_assert (sp [0].type == VAL_OBJ);
3222 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3224 aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3225 if (aindex >= mono_array_length (o))
3226 THROW_EX (mono_get_exception_index_out_of_range (), ip - 5);
3228 /* check the array element corresponds to token */
3229 esize = mono_array_element_size (o->obj.vtable->klass);
3232 sp->data.p = mono_array_addr_with_size (o, esize, aindex);
3233 sp->data.vt.klass = o->obj.vtable->klass->element_class;
3238 CASE (CEE_LDELEM_I1) /* fall through */
3239 CASE (CEE_LDELEM_U1) /* fall through */
3240 CASE (CEE_LDELEM_I2) /* fall through */
3241 CASE (CEE_LDELEM_U2) /* fall through */
3242 CASE (CEE_LDELEM_I4) /* fall through */
3243 CASE (CEE_LDELEM_U4) /* fall through */
3244 CASE (CEE_LDELEM_I8) /* fall through */
3245 CASE (CEE_LDELEM_I) /* fall through */
3246 CASE (CEE_LDELEM_R4) /* fall through */
3247 CASE (CEE_LDELEM_R8) /* fall through */
3248 CASE (CEE_LDELEM_REF) {
3254 g_assert (sp [0].type == VAL_OBJ);
3257 THROW_EX (mono_get_exception_null_reference (), ip);
3259 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3261 aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3262 if (aindex >= mono_array_length (o))
3263 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3266 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3270 sp [0].data.i = mono_array_get (o, gint8, aindex);
3271 sp [0].type = VAL_I32;
3274 sp [0].data.i = mono_array_get (o, guint8, aindex);
3275 sp [0].type = VAL_I32;
3278 sp [0].data.i = mono_array_get (o, gint16, aindex);
3279 sp [0].type = VAL_I32;
3282 sp [0].data.i = mono_array_get (o, guint16, aindex);
3283 sp [0].type = VAL_I32;
3286 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3287 sp [0].type = VAL_NATI;
3290 sp [0].data.i = mono_array_get (o, gint32, aindex);
3291 sp [0].type = VAL_I32;
3294 sp [0].data.i = mono_array_get (o, guint32, aindex);
3295 sp [0].type = VAL_I32;
3298 sp [0].data.l = mono_array_get (o, guint64, aindex);
3299 sp [0].type = VAL_I64;
3302 sp [0].data.f = mono_array_get (o, float, aindex);
3303 sp [0].type = VAL_DOUBLE;
3306 sp [0].data.f = mono_array_get (o, double, aindex);
3307 sp [0].type = VAL_DOUBLE;
3309 case CEE_LDELEM_REF:
3310 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3311 sp [0].data.vt.klass = NULL;
3312 sp [0].type = VAL_OBJ;
3322 CASE (CEE_STELEM_I) /* fall through */
3323 CASE (CEE_STELEM_I1) /* fall through */
3324 CASE (CEE_STELEM_I2) /* fall through */
3325 CASE (CEE_STELEM_I4) /* fall through */
3326 CASE (CEE_STELEM_I8) /* fall through */
3327 CASE (CEE_STELEM_R4) /* fall through */
3328 CASE (CEE_STELEM_R8) /* fall through */
3329 CASE (CEE_STELEM_REF) {
3336 g_assert (sp [0].type == VAL_OBJ);
3339 THROW_EX (mono_get_exception_null_reference (), ip);
3341 ac = o->obj.vtable->klass;
3342 g_assert (MONO_CLASS_IS_ARRAY (ac));
3344 aindex = sp [1].type == VAL_I32? sp [1].data.i: sp [1].data.nati;
3345 if (aindex >= mono_array_length (o))
3346 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3349 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3353 mono_array_set (o, mono_i, aindex, sp [2].data.nati);
3356 mono_array_set (o, gint8, aindex, sp [2].data.i);
3359 mono_array_set (o, gint16, aindex, sp [2].data.i);
3362 mono_array_set (o, gint32, aindex, sp [2].data.i);
3365 mono_array_set (o, gint64, aindex, sp [2].data.l);
3368 mono_array_set (o, float, aindex, sp [2].data.f);
3371 mono_array_set (o, double, aindex, sp [2].data.f);
3373 case CEE_STELEM_REF:
3374 g_assert (sp [2].type == VAL_OBJ);
3375 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3376 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3377 mono_array_set (o, gpointer, aindex, sp [2].data.p);
3388 CASE (CEE_UNBOX_ANY)
3401 CASE (CEE_UNUSED17) ves_abort(); BREAK;
3403 #if SIZEOF_VOID_P == 4
3404 CASE (CEE_CONV_OVF_I)
3405 CASE (CEE_CONV_OVF_U)
3407 CASE (CEE_CONV_OVF_I1)
3408 CASE (CEE_CONV_OVF_I2)
3409 CASE (CEE_CONV_OVF_I4)
3410 CASE (CEE_CONV_OVF_U1)
3411 CASE (CEE_CONV_OVF_U2)
3412 CASE (CEE_CONV_OVF_U4) {
3414 switch (sp [-1].type) {
3416 value = (gint64)sp [-1].data.f;
3419 value = (gint64)sp [-1].data.l;
3424 value = (gint64)sp [-1].data.i;
3427 value = (gint64)sp [-1].data.nati;
3431 case CEE_CONV_OVF_I1:
3432 if (value < -128 || value > 127)
3433 THROW_EX (mono_get_exception_overflow (), ip);
3434 sp [-1].data.i = value;
3435 sp [-1].type = VAL_I32;
3437 case CEE_CONV_OVF_I2:
3438 if (value < -32768 || value > 32767)
3439 THROW_EX (mono_get_exception_overflow (), ip);
3440 sp [-1].data.i = value;
3441 sp [-1].type = VAL_I32;
3443 #if SIZEOF_VOID_P == 4
3444 case CEE_CONV_OVF_I: /* Fall through */
3446 case CEE_CONV_OVF_I4:
3447 if (value < MYGINT32_MIN || value > MYGINT32_MAX)
3448 THROW_EX (mono_get_exception_overflow (), ip);
3449 sp [-1].data.i = value;
3450 sp [-1].type = VAL_I32;
3452 case CEE_CONV_OVF_U1:
3453 if (value < 0 || value > 255)
3454 THROW_EX (mono_get_exception_overflow (), ip);
3455 sp [-1].data.i = value;
3456 sp [-1].type = VAL_I32;
3458 case CEE_CONV_OVF_U2:
3459 if (value < 0 || value > 65535)
3460 THROW_EX (mono_get_exception_overflow (), ip);
3461 sp [-1].data.i = value;
3462 sp [-1].type = VAL_I32;
3464 #if SIZEOF_VOID_P == 4
3465 case CEE_CONV_OVF_U: /* Fall through */
3467 case CEE_CONV_OVF_U4:
3468 if (value < 0 || value > MYGUINT32_MAX)
3469 THROW_EX (mono_get_exception_overflow (), ip);
3470 sp [-1].data.i = value;
3471 sp [-1].type = VAL_I32;
3474 g_assert_not_reached ();
3480 #if SIZEOF_VOID_P == 8
3481 CASE (CEE_CONV_OVF_I)
3483 CASE (CEE_CONV_OVF_I8)
3484 /* FIXME: handle other cases */
3485 if (sp [-1].type == VAL_I32) {
3486 sp [-1].data.l = (guint64)sp [-1].data.i;
3487 sp [-1].type = VAL_I64;
3488 } else if(sp [-1].type == VAL_I64) {
3489 /* defined as NOP */
3496 #if SIZEOF_VOID_P == 8
3497 CASE (CEE_CONV_OVF_U)
3499 CASE (CEE_CONV_OVF_U8)
3500 /* FIXME: handle other cases */
3501 if (sp [-1].type == VAL_I32) {
3502 sp [-1].data.l = (guint64) sp [-1].data.i;
3503 sp [-1].type = VAL_I64;
3504 } else if(sp [-1].type == VAL_I64) {
3505 /* defined as NOP */
3517 CASE (CEE_UNUSED23) ves_abort(); BREAK;
3518 CASE (CEE_REFANYVAL) ves_abort(); BREAK;
3520 if (!finite(sp [-1].data.f))
3521 THROW_EX (mono_get_exception_arithmetic (), ip);
3524 CASE (CEE_UNUSED24) ves_abort(); BREAK;
3525 CASE (CEE_UNUSED25) ves_abort(); BREAK;
3526 CASE (CEE_MKREFANY) ves_abort(); BREAK;
3535 CASE (CEE_UNUSED67) ves_abort(); BREAK;
3536 CASE (CEE_LDTOKEN) {
3538 MonoClass *handle_class;
3540 handle = mono_ldtoken (image, read32 (ip), &handle_class);
3542 vt_alloc (&handle_class->byval_arg, sp, FALSE);
3543 stackval_from_data (&handle_class->byval_arg, sp, (char*)&handle, FALSE);
3549 /* FIXME: check overflow */
3550 if (sp->type == VAL_I32) {
3551 if (CHECK_ADD_OVERFLOW (sp [-1].data.i, GET_NATI (sp [0])))
3552 THROW_EX (mono_get_exception_overflow (), ip);
3553 sp [-1].data.i = (gint32)sp [-1].data.i + (gint32)GET_NATI (sp [0]);
3554 } else if (sp->type == VAL_I64) {
3555 if (CHECK_ADD_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3556 THROW_EX (mono_get_exception_overflow (), ip);
3557 sp [-1].data.l = (gint64)sp [-1].data.l + (gint64)sp [0].data.l;
3558 } else if (sp->type == VAL_DOUBLE)
3559 sp [-1].data.f += sp [0].data.f;
3561 char *p = sp [-1].data.p;
3562 p += GET_NATI (sp [0]);
3567 CASE (CEE_ADD_OVF_UN)
3569 /* FIXME: check overflow, make unsigned */
3570 if (sp->type == VAL_I32) {
3571 if (CHECK_ADD_OVERFLOW_UN (sp [-1].data.i, GET_NATI (sp [0])))
3572 THROW_EX (mono_get_exception_overflow (), ip);
3573 sp [-1].data.i = (guint32)sp [-1].data.i + (guint32)GET_NATI (sp [0]);
3574 } else if (sp->type == VAL_I64) {
3575 if (CHECK_ADD_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3576 THROW_EX (mono_get_exception_overflow (), ip);
3577 sp [-1].data.l = (guint64)sp [-1].data.l + (guint64)sp [0].data.l;
3578 } else if (sp->type == VAL_DOUBLE)
3579 sp [-1].data.f += sp [0].data.f;
3581 char *p = sp [-1].data.p;
3582 p += GET_NATI (sp [0]);
3590 if (sp->type == VAL_I32) {
3591 if (CHECK_MUL_OVERFLOW (sp [-1].data.i, GET_NATI (sp [0])))
3592 THROW_EX (mono_get_exception_overflow (), ip);
3593 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
3595 else if (sp->type == VAL_I64) {
3596 if (CHECK_MUL_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3597 THROW_EX (mono_get_exception_overflow (), ip);
3598 sp [-1].data.l *= sp [0].data.l;
3600 else if (sp->type == VAL_DOUBLE)
3601 sp [-1].data.f *= sp [0].data.f;
3603 CASE (CEE_MUL_OVF_UN)
3606 /* FIXME: check overflow, make unsigned */
3607 if (sp->type == VAL_I32) {
3608 if (CHECK_MUL_OVERFLOW_UN (sp [-1].data.i, GET_NATI (sp [0])))
3609 THROW_EX (mono_get_exception_overflow (), ip);
3610 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
3612 else if (sp->type == VAL_I64) {
3613 if (CHECK_MUL_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3614 THROW_EX (mono_get_exception_overflow (), ip);
3615 sp [-1].data.l *= sp [0].data.l;
3617 else if (sp->type == VAL_DOUBLE)
3618 sp [-1].data.f *= sp [0].data.f;
3621 CASE (CEE_SUB_OVF_UN)
3624 /* FIXME: handle undeflow/unsigned */
3625 /* should probably consider the pointers as unsigned */
3626 if (sp->type == VAL_I32)
3627 sp [-1].data.i -= GET_NATI (sp [0]);
3628 else if (sp->type == VAL_I64)
3629 sp [-1].data.l -= sp [0].data.l;
3630 else if (sp->type == VAL_DOUBLE)
3631 sp [-1].data.f -= sp [0].data.f;
3633 char *p = sp [-1].data.p;
3634 p -= GET_NATI (sp [0]);
3638 CASE (CEE_ENDFINALLY)
3640 ip = finally_ips->data;
3641 finally_ips = g_slist_remove (finally_ips, ip);
3647 * There was no exception, we continue normally at the target address.
3651 CASE (CEE_LEAVE) /* Fall through */
3653 sp = frame->stack; /* empty the stack */
3655 if (*ip == CEE_LEAVE_S) {
3657 ip += (signed char) *ip;
3661 ip += (gint32) read32 (ip);
3666 * We may be either inside a try block or inside an handler.
3667 * In the first case there was no exception and we go on
3668 * executing the finally handlers and after that resume control
3670 * In the second case we need to clear the exception and
3671 * continue directly at the target ip.
3675 goto handle_finally;
3678 frame->ex_handler = NULL;
3682 frame->ex_handler = NULL;
3684 goto handle_finally;
3690 case CEE_MONO_FUNC1: {
3691 MonoMarshalConv conv;
3701 sp->data.vt.klass = NULL;
3704 case MONO_MARSHAL_CONV_STR_LPWSTR:
3705 sp->data.p = mono_string_to_utf16 (sp->data.p);
3707 case MONO_MARSHAL_CONV_LPSTR_STR:
3708 sp->data.p = mono_string_new_wrapper (sp->data.p);
3710 case MONO_MARSHAL_CONV_STR_LPTSTR:
3711 case MONO_MARSHAL_CONV_STR_LPSTR:
3712 sp->data.p = mono_string_to_utf8 (sp->data.p);
3714 case MONO_MARSHAL_CONV_STR_BSTR:
3715 sp->data.p = mono_string_to_bstr (sp->data.p);
3717 case MONO_MARSHAL_CONV_STR_TBSTR:
3718 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3719 sp->data.p = mono_string_to_ansibstr (sp->data.p);
3721 case MONO_MARSHAL_CONV_SB_LPSTR:
3722 sp->data.p = mono_string_builder_to_utf8 (sp->data.p);
3724 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3725 sp->data.p = mono_array_to_savearray (sp->data.p);
3727 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3728 sp->data.p = mono_array_to_lparray (sp->data.p);
3730 case MONO_MARSHAL_CONV_DEL_FTN:
3731 sp->data.p = mono_delegate_to_ftnptr (sp->data.p);
3733 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3734 sp->data.p = mono_marshal_string_array (sp->data.p);
3737 g_assert_not_reached ();
3742 case CEE_MONO_PROC2: {
3743 MonoMarshalConv conv;
3751 case MONO_MARSHAL_CONV_LPSTR_SB:
3752 mono_string_utf8_to_builder (sp [0].data.p, sp [1].data.p);
3754 case MONO_MARSHAL_FREE_ARRAY:
3755 mono_marshal_free_array (sp [0].data.p, sp [1].data.i);
3758 g_assert_not_reached ();
3762 case CEE_MONO_PROC3: {
3763 MonoMarshalConv conv;
3771 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3772 mono_string_to_byvalstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3774 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3775 mono_string_to_byvalwstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3778 g_assert_not_reached ();
3782 case CEE_MONO_VTADDR: {
3789 case CEE_MONO_LDPTR: {
3793 token = read32 (ip);
3797 sp->data.p = mono_method_get_wrapper_data (frame->method, token);
3798 sp->data.vt.klass = NULL;
3802 case CEE_MONO_FREE: {
3806 g_free (sp->data.p);
3809 case CEE_MONO_OBJADDR: {
3816 case CEE_MONO_NEWOBJ: {
3821 token = read32 (ip);
3824 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3825 sp->data.p = mono_object_new (domain, class);
3829 case CEE_MONO_RETOBJ: {
3834 token = read32 (ip);
3839 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3841 stackval_from_data (signature->ret, frame->retval, sp->data.vt.vt, signature->pinvoke);
3843 if (sp > frame->stack)
3844 g_warning ("more values on stack: %d", sp-frame->stack);
3849 g_error ("Unimplemented opcode: 0xF0 %02x at 0x%x\n", *ip, ip-header->code);
3880 CASE (CEE_PREFIXREF) ves_abort(); BREAK;
3882 * Note: Exceptions thrown when executing a prefixed opcode need
3883 * to take into account the number of prefix bytes (usually the
3884 * throw point is just (ip - n_prefix_bytes).
3889 case CEE_ARGLIST: ves_abort(); break;
3895 if (sp->type == VAL_I32)
3896 result = sp [0].data.i == (gint)GET_NATI (sp [1]);
3897 else if (sp->type == VAL_I64)
3898 result = sp [0].data.l == sp [1].data.l;
3899 else if (sp->type == VAL_DOUBLE) {
3900 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3903 result = sp [0].data.f == sp [1].data.f;
3905 result = GET_NATI (sp [0]) == GET_NATI (sp [1]);
3907 sp->data.i = result;
3917 if (sp->type == VAL_I32)
3918 result = sp [0].data.i > (gint)GET_NATI (sp [1]);
3919 else if (sp->type == VAL_I64)
3920 result = sp [0].data.l > sp [1].data.l;
3921 else if (sp->type == VAL_DOUBLE) {
3922 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3925 result = sp [0].data.f > sp [1].data.f;
3927 result = (gint)GET_NATI (sp [0]) > (gint)GET_NATI (sp [1]);
3929 sp->data.i = result;
3939 if (sp->type == VAL_I32)
3940 result = (guint32)sp [0].data.i > (mono_u)GET_NATI (sp [1]);
3941 else if (sp->type == VAL_I64)
3942 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
3943 else if (sp->type == VAL_DOUBLE)
3944 result = isnan (sp [0].data.f) || isnan (sp [1].data.f);
3946 result = (mono_u)GET_NATI (sp [0]) > (mono_u)GET_NATI (sp [1]);
3948 sp->data.i = result;
3958 if (sp->type == VAL_I32)
3959 result = sp [0].data.i < (gint)GET_NATI (sp [1]);
3960 else if (sp->type == VAL_I64)
3961 result = sp [0].data.l < sp [1].data.l;
3962 else if (sp->type == VAL_DOUBLE) {
3963 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3966 result = sp [0].data.f < sp [1].data.f;
3968 result = (gint)GET_NATI (sp [0]) < (gint)GET_NATI (sp [1]);
3970 sp->data.i = result;
3980 if (sp->type == VAL_I32)
3981 result = (guint32)sp [0].data.i < (mono_u)GET_NATI (sp [1]);
3982 else if (sp->type == VAL_I64)
3983 result = (guint64)sp [0].data.l < (guint64)sp [1].data.l;
3984 else if (sp->type == VAL_DOUBLE)
3985 result = isnan (sp [0].data.f) || isnan (sp [1].data.f);
3987 result = (mono_u)GET_NATI (sp [0]) < (mono_u)GET_NATI (sp [1]);
3989 sp->data.i = result;
3995 case CEE_LDVIRTFTN: {
3996 int virtual = *ip == CEE_LDVIRTFTN;
4000 token = read32 (ip);
4003 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
4004 m = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
4006 m = mono_get_method (image, token, NULL);
4009 THROW_EX (mono_get_exception_missing_method (), ip - 5);
4013 THROW_EX (mono_get_exception_null_reference (), ip - 5);
4015 m = get_virtual_method (domain, m, sp);
4019 * This prevents infinite cycles since the wrapper contains
4022 if (frame->method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
4023 if (m && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4024 m = mono_marshal_get_synchronized_wrapper (m);
4026 sp->type = VAL_NATI;
4027 sp->data.p = mono_create_method_pointer (m);
4028 sp->data.vt.klass = NULL;
4032 case CEE_UNUSED56: ves_abort(); break;
4036 arg_pos = read16 (ip);
4038 vt_alloc (ARG_TYPE (signature, arg_pos), sp, signature->pinvoke);
4039 stackval_from_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4051 t = ARG_TYPE (signature, anum);
4052 c = mono_class_from_mono_type (t);
4053 sp->data.vt.klass = c;
4054 sp->data.vt.vt = ARG_POS (anum);
4064 arg_pos = read16 (ip);
4067 stackval_to_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4074 loc_pos = read16 (ip);
4076 vt_alloc (LOCAL_TYPE (header, loc_pos), sp, FALSE);
4077 stackval_from_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4087 loc_pos = read16 (ip);
4089 t = LOCAL_TYPE (header, loc_pos);
4090 c = mono_class_from_mono_type (t);
4091 sp->data.vt.vt = LOCAL_POS (loc_pos);
4092 sp->data.vt.klass = c;
4102 loc_pos = read16 (ip);
4105 stackval_to_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4111 if (sp != frame->stack)
4112 THROW_EX (mono_get_exception_execution_engine (NULL), ip - 1);
4114 sp->data.p = alloca (sp->data.i);
4118 case CEE_UNUSED57: ves_abort(); break;
4119 case CEE_ENDFILTER: ves_abort(); break;
4120 case CEE_UNALIGNED_:
4122 unaligned_address = 1;
4126 volatile_address = 1;
4137 token = read32 (ip);
4140 class = mono_class_get (image, token);
4143 g_assert (sp->type == VAL_TP || sp->type == VAL_MP);
4144 memset (sp->data.vt.vt, 0, mono_class_value_size (class, NULL));
4147 case CEE_CONSTRAINED_: {
4149 /* FIXME: implement */
4151 token = read32 (ip);
4157 if (!sp [0].data.p || !sp [1].data.p)
4158 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4160 /* FIXME: value and size may be int64... */
4161 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4166 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4168 /* FIXME: value and size may be int64... */
4169 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4172 /* FIXME: implement */
4177 * need to clarify what this should actually do:
4178 * start the search from the last found handler in
4179 * this method or continue in the caller or what.
4180 * Also, do we need to run finally/fault handlers after a retrow?
4181 * Well, this implementation will follow the usual search
4182 * for an handler, considering the current ip as throw spot.
4183 * We need to NULL frame->ex_handler for the later code to
4184 * actually run the new found handler.
4186 frame->ex_handler = NULL;
4187 THROW_EX (frame->ex, ip - 1);
4189 case CEE_UNUSED: ves_abort(); break;
4194 token = read32 (ip);
4196 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4197 MonoType *type = mono_type_create_from_typespec (image, token);
4198 sp->data.i = mono_type_size (type, &align);
4199 mono_metadata_free_type (type);
4201 MonoClass *szclass = mono_class_get (image, token);
4202 mono_class_init (szclass);
4203 if (!szclass->valuetype)
4204 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
4205 sp->data.i = mono_class_value_size (szclass, &align);
4211 case CEE_REFANYTYPE: ves_abort(); break;
4213 g_error ("Unimplemented opcode: 0xFE %02x at 0x%x\n", *ip, ip-header->code);
4220 g_assert_not_reached ();
4222 * Exception handling code.
4223 * The exception object is stored in frame->ex.
4230 MonoInvocation *inv;
4231 MonoMethodHeader *hd;
4232 MonoExceptionClause *clause;
4238 g_print ("* Handling exception '%s' at IL_%04x\n", mono_object_class (frame->ex)->name, frame->ip - header->code);
4240 if (die_on_exception)
4243 for (inv = frame; inv; inv = inv->parent) {
4244 if (inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4246 if (inv->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4248 hd = ((MonoMethodNormal*)inv->method)->header;
4249 ip_offset = inv->ip - hd->code;
4250 for (i = 0; i < hd->num_clauses; ++i) {
4251 clause = &hd->clauses [i];
4252 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4253 if (!clause->flags) {
4254 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (inv->method->klass->image, clause->token_or_filter))) {
4256 * OK, we found an handler, now we need to execute the finally
4257 * and fault blocks before branching to the handler code.
4259 inv->ex_handler = clause;
4262 g_print ("* Found handler at '%s'\n", inv->method->name);
4266 * It seems that if the catch handler is found in the same method,
4267 * it gets executed before the finally handler.
4272 goto handle_finally;
4275 /* FIXME: handle filter clauses */
4282 * If we get here, no handler was found: print a stack trace.
4284 for (inv = frame; inv; inv = inv->parent) {
4285 if (inv->invoke_trap)
4286 goto handle_finally;
4289 ex_obj = (MonoObject*)frame->ex;
4290 mono_unhandled_exception (ex_obj);
4297 MonoExceptionClause *clause;
4301 g_print ("* Handle finally\n");
4303 if ((frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4304 || (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4308 ip_offset = frame->ip - header->code;
4310 for (i = 0; i < header->num_clauses; ++i) {
4311 clause = &header->clauses [i];
4312 if (clause == frame->ex_handler)
4314 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - header->code))) {
4315 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4316 ip = header->code + clause->handler_offset;
4317 finally_ips = g_slist_append (finally_ips, (gpointer) ip);
4320 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4326 ip = finally_ips->data;
4327 finally_ips = g_slist_remove (finally_ips, ip);
4332 * If an exception is set, we need to execute the fault handler, too,
4333 * otherwise, we continue normally.
4344 MonoExceptionClause *clause;
4348 g_print ("* Handle fault\n");
4350 ip_offset = frame->ip - header->code;
4351 for (i = 0; i < header->num_clauses; ++i) {
4352 clause = &header->clauses [i];
4353 if (clause->flags == 3 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4354 ip = header->code + clause->handler_offset;
4357 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4363 * If the handler for the exception was found in this method, we jump
4364 * to it right away, otherwise we return and let the caller run
4365 * the finally, fault and catch blocks.
4366 * This same code should be present in the endfault opcode, but it
4367 * is corrently not assigned in the ECMA specs: LAMESPEC.
4369 if (frame->ex_handler) {
4372 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4374 ip = header->code + frame->ex_handler->handler_offset;
4377 sp->data.p = frame->ex;
4381 if (!frame->parent) {
4382 /* Pass the exception though the managed barrier if possible */
4383 MonoInvocation *parent = TlsGetValue (frame_thread_id);
4385 parent->ex = frame->ex;
4386 longjmp (*(jmp_buf*)parent->locals, 1);
4397 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4399 MonoImage *image = assembly->image;
4400 MonoCLIImageInfo *iinfo;
4402 MonoObject *exc = NULL;
4405 iinfo = image->image_info;
4406 method = mono_get_method (image, iinfo->cli_cli_header.ch_entry_point, NULL);
4408 g_error ("No entry point method found in %s", image->name);
4410 rval = mono_runtime_run_main (method, argc, argv, &exc);
4419 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4420 "Usage is: mint [options] executable args...\n\n", VERSION);
4422 "Runtime Debugging:\n"
4427 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4430 " --traceclassinit\n"
4433 " --debug method_name\n"
4439 " --config filename load the specified config file instead of the default\n"
4440 " --workers n maximum number of worker threads\n"
4447 test_load_class (MonoImage* image)
4449 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
4453 for (i = 1; i <= t->rows; ++i) {
4454 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4455 mono_class_init (klass);
4460 static MonoException * segv_exception = NULL;
4463 segv_handler (int signum)
4465 signal (signum, segv_handler);
4466 mono_raise_exception (segv_exception);
4470 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4471 MonoReflectionMethod **method,
4472 gint32 *iloffset, gint32 *native_offset,
4473 MonoString **file, gint32 *line, gint32 *column)
4486 *file = mono_string_new (mono_domain_get (), "unknown");
4492 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4500 int enable_debugging;
4506 static void main_thread_handler (gpointer user_data)
4508 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4509 MonoAssembly *assembly;
4512 if (main_args->enable_debugging)
4513 mono_debug_init (main_args->domain, MONO_DEBUG_FORMAT_MONO);
4515 assembly = mono_domain_assembly_open (main_args->domain,
4519 fprintf (stderr, "Can not open image %s\n", main_args->file);
4523 if (main_args->enable_debugging)
4524 mono_debug_init_2 (assembly);
4527 test_load_class (assembly->image);
4529 error = mono_verify_corlib ();
4531 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
4534 segv_exception = mono_get_exception_null_reference ();
4535 segv_exception->message = mono_string_new (main_args->domain, "Segmentation fault");
4536 signal (SIGSEGV, segv_handler);
4538 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4543 mono_runtime_install_handlers (void)
4545 /* FIXME: anything to do here? */
4549 quit_function (MonoDomain *domain, gpointer user_data)
4551 mono_profiler_shutdown ();
4553 mono_runtime_cleanup (domain);
4554 mono_domain_unload (domain, TRUE);
4558 static CRITICAL_SECTION ms;
4561 main (int argc, char *argv [])
4564 int retval = 0, i, ocount = 0;
4565 char *file, *config_file = NULL;
4566 int enable_debugging = FALSE;
4567 MainThreadArgs main_args;
4572 for (i = 1; i < argc && argv [i][0] == '-'; i++){
4573 if (strcmp (argv [i], "--trace") == 0)
4575 if (strcmp (argv [i], "--noptr") == 0)
4576 global_no_pointers = 1;
4577 if (strcmp (argv [i], "--traceops") == 0)
4579 if (strcmp (argv [i], "--dieonex") == 0) {
4580 die_on_exception = 1;
4581 enable_debugging = 1;
4583 if (strcmp (argv [i], "--print-vtable") == 0)
4584 mono_print_vtable = TRUE;
4585 if (strcmp (argv [i], "--profile") == 0)
4586 mono_profiler_load (NULL);
4587 if (strcmp (argv [i], "--opcode-count") == 0)
4589 if (strcmp (argv [i], "--config") == 0)
4590 config_file = argv [++i];
4591 if (strcmp (argv [i], "--workers") == 0) {
4592 mono_max_worker_threads = atoi (argv [++i]);
4593 if (mono_max_worker_threads < 1)
4594 mono_max_worker_threads = 1;
4596 if (strcmp (argv [i], "--help") == 0)
4599 if (strcmp (argv [i], "--debug") == 0) {
4600 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
4602 g_error ("Invalid method name '%s'", argv [i]);
4603 db_methods = g_list_append (db_methods, desc);
4613 g_set_prgname (file);
4614 mono_set_rootdir ();
4615 mono_config_parse (config_file);
4617 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4618 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4621 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4622 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4623 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4625 frame_thread_id = TlsAlloc ();
4626 TlsSetValue (frame_thread_id, NULL);
4628 mono_install_compile_method (mono_create_method_pointer);
4629 mono_install_runtime_invoke (interp_mono_runtime_invoke);
4630 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4632 mono_install_handler (interp_ex_handler);
4633 mono_install_stack_walk (interp_walk_stack);
4634 mono_runtime_install_cleanup (quit_function);
4636 metadata_section = &ms;
4637 InitializeCriticalSection (metadata_section);
4638 domain = mono_init (file);
4639 mono_runtime_init (domain, NULL, NULL);
4641 main_args.domain=domain;
4642 main_args.file=file;
4643 main_args.argc=argc-i;
4644 main_args.argv=argv+i;
4645 main_args.enable_debugging=enable_debugging;
4647 mono_runtime_exec_managed_code (domain, main_thread_handler,
4650 quit_function (domain, NULL);
4652 /* Get the return value from System.Environment.ExitCode */
4653 retval=mono_environment_exitcode_get ();
4657 fprintf (stderr, "opcode count: %ld\n", opcode_count);
4658 fprintf (stderr, "fcall count: %ld\n", fcall_count);