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**)((char*)obj->vtable->interface_offsets [m->klass->interface_id] + (m->slot<<2));
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_VALUETA;
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 g_warning ("wrong array index");
481 g_assert_not_reached ();
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, 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++)
516 pos = pos*ao->bounds [i].length + sp [i].data.i -
517 ao->bounds [i].lower_bound;
520 esize = mono_array_element_size (ac);
521 ea = mono_array_addr_with_size (ao, esize, pos);
523 mt = frame->method->signature->ret;
524 stackval_from_data (mt, frame->retval, ea, FALSE);
528 ves_array_element_address (MonoInvocation *frame)
530 stackval *sp = frame->stack_args;
534 gint32 i, pos, esize;
539 ac = o->vtable->klass;
541 g_assert (ac->rank >= 1);
544 if (ao->bounds != NULL) {
545 pos -= ao->bounds [0].lower_bound;
546 for (i = 1; i < ac->rank; i++)
547 pos = pos*ao->bounds [i].length + sp [i].data.i -
548 ao->bounds [i].lower_bound;
551 esize = mono_array_element_size (ac);
552 ea = mono_array_addr_with_size (ao, esize, pos);
554 frame->retval->type = VAL_TP;
555 frame->retval->data.p = ea;
559 interp_walk_stack (MonoStackWalk func, gpointer user_data)
561 MonoInvocation *frame = TlsGetValue (frame_thread_id);
563 MonoMethodHeader *hd;
566 gboolean managed = FALSE;
567 if (!frame->method || (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
568 (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
571 hd = ((MonoMethodNormal*)frame->method)->header;
572 il_offset = frame->ip - hd->code;
573 if (!frame->method->wrapper_type)
576 if (func (frame->method, -1, il_offset, managed, user_data))
578 frame = frame->parent;
583 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFunc addr, gboolean string_ctor)
589 TlsSetValue (frame_thread_id, frame->args);
594 if (!frame->method->info) {
595 func = frame->method->info = mono_create_trampoline (sig, string_ctor);
597 func = (MonoPIFunc)frame->method->info;
600 func = mono_create_trampoline (sig, string_ctor);
604 * frame->locals and args are unused for P/Invoke methods, so we reuse them.
605 * locals will point to the jmp_buf, while args will point to the previous
606 * MonoInvocation frame: this is needed to make exception searching work across
607 * managed/unmanaged boundaries.
609 frame->locals = (char*)&env;
610 frame->args = (char*)TlsGetValue (frame_thread_id);
611 TlsSetValue (frame_thread_id, frame);
613 func (addr, &frame->retval->data.p, frame->obj, frame->stack_args);
616 stackval_from_data (&mono_defaults.string_class->byval_arg,
617 frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
618 } else if (!MONO_TYPE_ISSTRUCT (sig->ret))
619 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
621 TlsSetValue (frame_thread_id, frame->args);
626 * runtime specifies that the implementation of the method is automatically
627 * provided by the runtime and is primarily used for the methods of delegates.
630 ves_runtime_method (MonoInvocation *frame)
632 const char *name = frame->method->name;
633 MonoObject *obj = (MonoObject*)frame->obj;
634 MonoMulticastDelegate *delegate = (MonoMulticastDelegate*)frame->obj;
639 mono_class_init (frame->method->klass);
641 if (obj && mono_object_isinst (obj, mono_defaults.multicastdelegate_class)) {
642 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
643 mono_delegate_ctor (obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
646 if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
653 code = (guchar*)delegate->delegate.method_ptr;
654 if ((ji = mono_jit_info_table_find (mono_root_domain, code))) {
656 INIT_FRAME(&call,frame,delegate->delegate.target,frame->stack_args,frame->retval,method);
657 ves_exec_method (&call);
662 g_assert_not_reached ();
665 delegate = delegate->prev;
669 if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
670 nm = mono_marshal_get_delegate_begin_invoke (frame->method);
671 INIT_FRAME(&call,frame,obj,frame->stack_args,frame->retval,nm);
672 ves_exec_method (&call);
676 if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
677 nm = mono_marshal_get_delegate_end_invoke (frame->method);
678 INIT_FRAME(&call,frame,obj,frame->stack_args,frame->retval,nm);
679 ves_exec_method (&call);
685 if (obj && mono_object_isinst (obj, mono_defaults.array_class)) {
686 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
687 ves_array_set (frame);
690 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
691 ves_array_get (frame);
694 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
695 ves_array_element_address (frame);
700 g_error ("Don't know how to exec runtime method %s.%s::%s",
701 frame->method->klass->name_space, frame->method->klass->name,
702 frame->method->name);
706 dump_stack (stackval *stack, stackval *sp)
709 GString *str = g_string_new ("");
712 return g_string_free (str, FALSE);
716 case VAL_I32: g_string_sprintfa (str, "[%d] ", s->data.i); break;
717 case VAL_I64: g_string_sprintfa (str, "[%lld] ", s->data.l); break;
718 case VAL_DOUBLE: g_string_sprintfa (str, "[%0.5f] ", s->data.f); break;
720 if (!global_no_pointers)
721 g_string_sprintfa (str, "[vt: %p] ", s->data.vt.vt);
723 g_string_sprintfa (str, "[vt%s] ", s->data.vt.vt ? "" : "=null");
726 MonoObject *obj = s->data.p;
727 if (global_no_pointers && obj && obj->vtable) {
728 MonoClass *klass = mono_object_class (obj);
729 if (klass == mono_defaults.string_class) {
730 char *utf8 = mono_string_to_utf8 ((MonoString*) obj);
731 g_string_sprintfa (str, "[str:%s] ", utf8);
734 } else if (klass == mono_defaults.sbyte_class) {
735 g_string_sprintfa (str, "[b:%d] ",
736 *(gint8 *)((guint8 *) obj + sizeof (MonoObject)));
738 } else if (klass == mono_defaults.int16_class) {
739 g_string_sprintfa (str, "[b:%d] ",
740 *(gint16 *)((guint8 *) obj + sizeof (MonoObject)));
742 } else if (klass == mono_defaults.int32_class) {
743 g_string_sprintfa (str, "[b:%d] ",
744 *(gint32 *)((guint8 *) obj + sizeof (MonoObject)));
746 } else if (klass == mono_defaults.byte_class) {
747 g_string_sprintfa (str, "[b:%u] ",
748 *(guint8 *)((guint8 *) obj + sizeof (MonoObject)));
750 } else if (klass == mono_defaults.char_class
751 || klass == mono_defaults.uint16_class) {
752 g_string_sprintfa (str, "[b:%u] ",
753 *(guint16 *)((guint8 *) obj + sizeof (MonoObject)));
755 } else if (klass == mono_defaults.uint32_class) {
756 g_string_sprintfa (str, "[b:%u] ",
757 *(guint32 *)((guint8 *) obj + sizeof (MonoObject)));
759 } else if (klass == mono_defaults.int64_class) {
760 g_string_sprintfa (str, "[b:%lld] ",
761 *(gint64 *)((guint8 *) obj + sizeof (MonoObject)));
763 } else if (klass == mono_defaults.uint64_class) {
764 g_string_sprintfa (str, "[b:%llu] ",
765 *(guint64 *)((guint8 *) obj + sizeof (MonoObject)));
767 } else if (klass == mono_defaults.double_class) {
768 g_string_sprintfa (str, "[b:%0.5f] ",
769 *(gdouble *)((guint8 *) obj + sizeof (MonoObject)));
771 } else if (klass == mono_defaults.single_class) {
772 g_string_sprintfa (str, "[b:%0.5f] ",
773 *(gfloat *)((guint8 *) obj + sizeof (MonoObject)));
775 } else if (klass == mono_defaults.boolean_class) {
776 g_string_sprintfa (str, "[b:%s] ",
777 *(gboolean *)((guint8 *) obj + sizeof (MonoObject))
785 if (!global_no_pointers)
786 g_string_sprintfa (str, "[%p] ", s->data.p);
788 g_string_sprintfa (str, s->data.p ? "[ptr] " : "[null] ");
793 return g_string_free (str, FALSE);
797 dump_frame (MonoInvocation *inv)
799 GString *str = g_string_new ("");
802 for (i = 0; inv; inv = inv->parent, ++i) {
803 MonoClass *k = inv->method->klass;
806 gchar *source = NULL;
807 if (inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL ||
808 inv->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
812 MonoMethodHeader *hd = ((MonoMethodNormal *)inv->method)->header;
815 codep = *(inv->ip) == 0xfe? inv->ip [1] + 256: *(inv->ip);
818 opname = mono_opcode_names [codep];
819 codep = inv->ip - hd->code;
821 source = mono_debug_source_location_from_il_offset (inv->method, codep, NULL);
823 args = dump_stack (inv->stack_args, inv->stack_args + inv->method->signature->param_count);
825 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s.%s::%s (%s) at %s\n", i, codep, opname,
826 k->name_space, k->name, inv->method->name, args, source);
828 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s.%s::%s (%s)\n", i, codep, opname,
829 k->name_space, k->name, inv->method->name, args);
833 return g_string_free (str, FALSE);
836 static CRITICAL_SECTION metadata_lock;
839 INLINE_STRING_LENGTH = 1,
842 INLINE_TYPE_ELEMENT_TYPE
846 calc_offsets (MonoImage *image, MonoMethod *method)
848 int i, align, size, offset = 0;
849 MonoMethodHeader *header = ((MonoMethodNormal*)method)->header;
850 MonoMethodSignature *signature = method->signature;
851 int hasthis = signature->hasthis;
852 register const unsigned char *ip, *end;
853 const MonoOpcode *opcode;
857 MonoDomain *domain = mono_domain_get ();
860 mono_profiler_method_jit (method); /* sort of... */
861 offsets = g_new0 (guint32, 2 + header->num_locals + signature->param_count + signature->hasthis);
862 for (i = 0; i < header->num_locals; ++i) {
863 size = mono_type_size (header->locals [i], &align);
865 offset &= ~(align - 1);
866 offsets [2 + i] = offset;
869 offsets [0] = offset;
872 offset += sizeof (gpointer) - 1;
873 offset &= ~(sizeof (gpointer) - 1);
874 offsets [2 + header->num_locals] = offset;
875 offset += sizeof (gpointer);
877 for (i = 0; i < signature->param_count; ++i) {
878 if (signature->pinvoke)
879 size = mono_type_native_stack_size (signature->params [i], &align);
881 size = mono_type_stack_size (signature->params [i], &align);
883 offset &= ~(align - 1);
884 offsets [2 + hasthis + header->num_locals + i] = offset;
887 offsets [1] = offset;
889 EnterCriticalSection (&metadata_lock);
890 /* intern the strings in the method. */
892 end = ip + header->code_size;
899 opcode = &mono_opcodes [i];
900 switch (opcode->argument) {
904 case MonoInlineString:
905 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
909 if (method->wrapper_type == MONO_WRAPPER_NONE) {
910 class = mono_class_get (image, read32 (ip + 1));
911 mono_class_init (class);
912 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE))
913 mono_class_vtable (domain, class);
917 case MonoInlineField:
918 token = read32 (ip + 1);
919 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
920 mono_field_from_memberref (image, token, &class);
922 class = mono_class_get (image,
923 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
925 mono_class_init (class);
926 mono_class_vtable (domain, class);
929 case MonoInlineMethod:
930 if (method->wrapper_type == MONO_WRAPPER_NONE) {
931 m = mono_get_method (image, read32 (ip + 1), NULL);
932 mono_class_init (m->klass);
933 if (!(m->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
934 mono_class_vtable (domain, m->klass);
940 case MonoShortInlineR:
942 case MonoInlineBrTarget:
948 case MonoShortInlineVar:
949 case MonoShortInlineI:
950 case MonoShortInlineBrTarget:
953 case MonoInlineSwitch: {
966 g_assert_not_reached ();
970 method->info = offsets;
973 * We store the inline info in addr, since it's unused for IL methods.
975 if (method->klass == mono_defaults.string_class) {
976 if (strcmp (method->name, "get_Length") == 0)
977 method->addr = GUINT_TO_POINTER (INLINE_STRING_LENGTH);
978 } else if (method->klass == mono_defaults.array_class) {
979 if (strcmp (method->name, "get_Length") == 0)
980 method->addr = GUINT_TO_POINTER (INLINE_ARRAY_LENGTH);
981 else if (strcmp (method->name, "get_Rank") == 0 || strcmp (method->name, "GetRank") == 0)
982 method->addr = GUINT_TO_POINTER (INLINE_ARRAY_RANK);
983 } else if (method->klass == mono_defaults.monotype_class) {
984 if (strcmp (method->name, "GetElementType") == 0)
985 method->addr = GUINT_TO_POINTER (INLINE_TYPE_ELEMENT_TYPE);
987 LeaveCriticalSection (&metadata_lock);
988 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
991 #define LOCAL_POS(n) (frame->locals + offsets [2 + (n)])
992 #define LOCAL_TYPE(header, n) ((header)->locals [(n)])
994 #define ARG_POS(n) (args_pointers [(n)])
995 #define ARG_TYPE(sig, n) ((n) ? (sig)->params [(n) - (sig)->hasthis] : \
996 (sig)->hasthis ? &frame->method->klass->this_arg: (sig)->params [(0)])
998 #define THROW_EX(exception,ex_ip) \
1000 char *stack_trace; \
1001 frame->ip = (ex_ip); \
1002 stack_trace = dump_frame (frame); \
1003 frame->ex = (MonoException*)(exception); \
1004 frame->ex->stack_trace = mono_string_new (domain, stack_trace); \
1005 g_free (stack_trace); \
1006 goto handle_exception; \
1009 typedef struct _vtallocation vtallocation;
1011 struct _vtallocation {
1014 char data [MONO_ZERO_LEN_ARRAY];
1018 * we don't use vtallocation->next, yet
1020 #define vt_alloc(vtype,sp,native) \
1021 if ((vtype)->type == MONO_TYPE_VALUETYPE && !(vtype)->data.klass->enumtype) { \
1022 if (!(vtype)->byref) { \
1025 if (native) size = mono_class_native_size ((vtype)->data.klass, &align); \
1026 else size = mono_class_value_size ((vtype)->data.klass, &align); \
1027 if (!vtalloc || vtalloc->size <= size) { \
1028 vtalloc = alloca (sizeof (vtallocation) + size); \
1029 vtalloc->size = size; \
1030 g_assert (size < 10000); \
1032 (sp)->data.vt.vt = vtalloc->data; \
1035 (sp)->data.vt.klass = (vtype)->data.klass; \
1039 #define vt_free(sp) \
1041 if ((sp)->type == VAL_VALUET) { \
1042 vtalloc = (vtallocation*)(((char*)(sp)->data.vt.vt) - G_STRUCT_OFFSET (vtallocation, data)); \
1047 verify_method (MonoMethod *m)
1049 GSList *errors, *tmp;
1050 MonoVerifyInfo *info;
1052 errors = mono_method_verify (m, MONO_VERIFY_ALL);
1054 g_print ("Method %s.%s::%s has invalid IL.\n", m->klass->name_space, m->klass->name, m->name);
1055 for (tmp = errors; tmp; tmp = tmp->next) {
1057 g_print ("%s\n", info->message);
1061 mono_free_verify_list (errors);
1064 #define MYGUINT64_MAX 18446744073709551615UL
1065 #define MYGINT64_MAX 9223372036854775807LL
1066 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1068 #define MYGUINT32_MAX 4294967295U
1069 #define MYGINT32_MAX 2147483647
1070 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1072 #define CHECK_ADD_OVERFLOW(a,b) \
1073 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1074 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1076 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1077 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1079 #define CHECK_ADD_OVERFLOW64(a,b) \
1080 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1081 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1083 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1084 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1086 /* Resolves to TRUE if the operands would overflow */
1087 #define CHECK_MUL_OVERFLOW(a,b) \
1088 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1089 (((gint32)(a) > 0) && ((gint32)(b) > 0)) || (((gint32)(a) < 0) && ((gint32)(b) < 0)) ? \
1090 (gint32)(b) > ((MYGINT32_MAX) / (gint32)(a)) : \
1091 (gint32)(b) < ((MYGINT32_MIN) / (gint32)(a))
1093 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1094 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1095 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1097 #define CHECK_MUL_OVERFLOW64(a,b) \
1098 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1099 (((gint64)(a) > 0) && ((gint64)(b) > 0)) || (((gint64)(a) < 0) && ((gint64)(b) < 0)) ? \
1100 (gint64)(b) > ((MYGINT64_MAX) / (gint64)(a)) : \
1101 (gint64)(b) < ((MYGINT64_MIN) / (gint64)(a))
1103 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1104 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1105 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1108 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1110 MonoInvocation frame, *parent;
1111 MonoObject *retval = NULL;
1112 MonoMethodSignature *sig = method->signature;
1113 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1114 int i, type, isobject = 0;
1117 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1119 /* FIXME: Set frame for execption handling. */
1121 switch (sig->ret->type) {
1122 case MONO_TYPE_VOID:
1124 case MONO_TYPE_STRING:
1125 case MONO_TYPE_OBJECT:
1126 case MONO_TYPE_CLASS:
1127 case MONO_TYPE_ARRAY:
1128 case MONO_TYPE_SZARRAY:
1131 case MONO_TYPE_VALUETYPE:
1132 retval = mono_object_new (mono_domain_get (), klass);
1133 ret = ((char*)retval) + sizeof (MonoObject);
1134 if (!sig->ret->data.klass->enumtype)
1135 result.data.vt.vt = ret;
1138 retval = mono_object_new (mono_domain_get (), klass);
1139 ret = ((char*)retval) + sizeof (MonoObject);
1143 for (i = 0; i < sig->param_count; ++i) {
1144 if (sig->params [i]->byref) {
1145 args [i].type = VAL_POINTER;
1146 args [i].data.p = params [i];
1149 type = sig->params [i]->type;
1154 case MONO_TYPE_BOOLEAN:
1155 args [i].type = VAL_I32;
1156 args [i].data.i = *(MonoBoolean*)params [i];
1157 args [i].data.vt.klass = NULL;
1161 case MONO_TYPE_CHAR:
1162 args [i].type = VAL_I32;
1163 args [i].data.i = *(gint16*)params [i];
1164 args [i].data.vt.klass = NULL;
1166 #if SIZEOF_VOID_P == 4
1167 case MONO_TYPE_U: /* use VAL_POINTER? */
1172 args [i].type = VAL_I32;
1173 args [i].data.i = *(gint32*)params [i];
1174 args [i].data.vt.klass = NULL;
1176 #if SIZEOF_VOID_P == 8
1182 args [i].type = VAL_I64;
1183 args [i].data.l = *(gint64*)params [i];
1184 args [i].data.vt.klass = NULL;
1186 case MONO_TYPE_VALUETYPE:
1187 if (sig->params [i]->data.klass->enumtype) {
1188 type = sig->params [i]->data.klass->enum_basetype->type;
1191 args [i].type = VAL_POINTER;
1192 args [i].data.p = params [i];
1193 args [i].data.vt.klass = NULL;
1196 case MONO_TYPE_STRING:
1197 case MONO_TYPE_CLASS:
1198 case MONO_TYPE_ARRAY:
1199 case MONO_TYPE_SZARRAY:
1200 case MONO_TYPE_OBJECT:
1201 args [i].type = VAL_OBJ;
1202 args [i].data.p = params [i];
1203 args [i].data.vt.klass = NULL;
1206 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1210 /* chain with managed parent if any */
1211 parent = TlsGetValue (frame_thread_id);
1212 INIT_FRAME(&frame,parent,obj,args,&result,method);
1214 frame.invoke_trap = 1;
1215 ves_exec_method (&frame);
1216 if (exc && frame.ex) {
1217 *exc = (MonoObject*) frame.ex;
1220 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1222 if (isobject || method->string_ctor)
1223 return result.data.p;
1224 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1229 * Need to optimize ALU ops when natural int == int32
1231 * IDEA: if we maintain a stack of ip, sp to be checked
1232 * in the return opcode, we could inline simple methods that don't
1233 * use the stack or local variables....
1235 * The {,.S} versions of many opcodes can/should be merged to reduce code
1240 ves_exec_method (MonoInvocation *frame)
1242 MonoDomain *domain = mono_domain_get ();
1243 MonoInvocation child_frame;
1244 MonoMethodHeader *header;
1245 MonoMethodSignature *signature;
1247 GSList *finally_ips = NULL;
1248 const unsigned char *endfinally_ip;
1249 register const unsigned char *ip;
1250 register stackval *sp;
1251 void **args_pointers;
1253 gint il_ins_count = -1;
1254 gint tracing = global_tracing;
1255 unsigned char tail_recursion = 0;
1256 unsigned char unaligned_address = 0;
1257 unsigned char volatile_address = 0;
1258 vtallocation *vtalloc = NULL;
1261 if (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1262 frame->method = mono_marshal_get_native_wrapper (frame->method);
1264 signature = frame->method->signature;
1268 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1269 if (!frame->method->addr) {
1270 /* ugly, but needed by the iflags setting in loader.c */
1271 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1272 ves_runtime_method (frame);
1274 goto handle_exception;
1278 if (frame->method->addr) {
1279 frame->ex = (MonoException*)mono_get_exception_missing_method ();
1280 goto handle_exception;
1283 ves_pinvoke_method (frame, frame->method->signature, frame->method->addr,
1284 frame->method->string_ctor);
1286 goto handle_exception;
1291 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1292 ves_runtime_method (frame);
1294 goto handle_exception;
1299 /*verify_method (frame->method);*/
1301 header = ((MonoMethodNormal *)frame->method)->header;
1302 image = frame->method->klass->image;
1304 if (!frame->method->info)
1305 calc_offsets (image, frame->method);
1306 offsets = frame->method->info;
1309 * with alloca we get the expected huge performance gain
1310 * stackval *stack = g_new0(stackval, header->max_stack);
1312 g_assert (header->max_stack < 10000);
1313 sp = frame->stack = alloca (sizeof (stackval) * header->max_stack);
1315 if (header->num_locals) {
1316 g_assert (offsets [0] < 10000);
1317 frame->locals = alloca (offsets [0]);
1319 * yes, we do it unconditionally, because it needs to be done for
1320 * some cases anyway and checking for that would be even slower.
1322 memset (frame->locals, 0, offsets [0]);
1325 * Copy args from stack_args to args.
1327 if (signature->param_count || signature->hasthis) {
1329 int has_this = signature->hasthis;
1331 g_assert (offsets [1] < 10000);
1332 frame->args = alloca (offsets [1]);
1333 g_assert ((signature->param_count + has_this) < 1000);
1334 args_pointers = alloca (sizeof(void*) * (signature->param_count + has_this));
1337 this_arg = args_pointers [0] = frame->args;
1338 *this_arg = frame->obj;
1340 for (i = 0; i < signature->param_count; ++i) {
1341 args_pointers [i + has_this] = frame->args + offsets [2 + header->num_locals + has_this + i];
1342 stackval_to_data (signature->params [i], frame->stack_args + i, args_pointers [i + has_this], signature->pinvoke);
1346 child_frame.parent = frame;
1347 frame->child = &child_frame;
1354 * using while (ip < end) may result in a 15% performance drop,
1355 * but it may be useful for debug
1359 /*g_assert (sp >= stack);*/
1363 char *ins, *discode;
1364 if (sp > frame->stack) {
1365 ins = dump_stack (frame->stack, sp);
1367 ins = g_strdup ("");
1370 discode = mono_disasm_code_one (NULL, frame->method, ip, NULL);
1371 discode [strlen (discode) - 1] = 0; /* no \n */
1372 g_print ("(%d) %-29s %s\n", GetCurrentThreadId(), discode, ins);
1376 if (il_ins_count > 0)
1377 if (!(--il_ins_count))
1387 G_BREAKPOINT (); /* this is not portable... */
1392 CASE (CEE_LDARG_3) {
1393 int n = (*ip)-CEE_LDARG_0;
1395 vt_alloc (ARG_TYPE (signature, n), sp, signature->pinvoke);
1396 stackval_from_data (ARG_TYPE (signature, n), sp, ARG_POS (n), signature->pinvoke);
1403 CASE (CEE_LDLOC_3) {
1404 int n = (*ip)-CEE_LDLOC_0;
1406 if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1408 sp->data.i = *(gint32*) LOCAL_POS (n);
1412 vt_alloc (LOCAL_TYPE (header, n), sp, FALSE);
1413 stackval_from_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1421 CASE (CEE_STLOC_3) {
1422 int n = (*ip)-CEE_STLOC_0;
1425 if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1426 gint32 *p = (gint32*)LOCAL_POS (n);
1430 stackval_to_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1437 vt_alloc (ARG_TYPE (signature, *ip), sp, signature->pinvoke);
1438 stackval_from_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1442 CASE (CEE_LDARGA_S) {
1447 t = ARG_TYPE (signature, *ip);
1448 c = mono_class_from_mono_type (t);
1449 sp->data.vt.klass = c;
1450 sp->data.vt.vt = ARG_POS (*ip);
1453 sp->type = VAL_VALUETA;
1464 stackval_to_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1470 vt_alloc (LOCAL_TYPE (header, *ip), sp, FALSE);
1471 stackval_from_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1475 CASE (CEE_LDLOCA_S) {
1480 t = LOCAL_TYPE (header, *ip);
1481 c = mono_class_from_mono_type (t);
1482 sp->data.vt.klass = c;
1483 sp->data.p = LOCAL_POS (*ip);
1486 sp->type = VAL_VALUETA;
1497 stackval_to_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1505 sp->data.vt.klass = NULL;
1508 CASE (CEE_LDC_I4_M1)
1524 sp->data.i = (*ip) - CEE_LDC_I4_0;
1531 sp->data.i = *(const gint8 *)ip;
1538 sp->data.i = read32 (ip);
1545 sp->data.l = read64 (ip);
1552 sp->type = VAL_DOUBLE;
1561 sp->type = VAL_DOUBLE;
1562 readr8(ip, &sp->data.f);
1566 CASE (CEE_UNUSED99) ves_abort (); BREAK;
1568 if (sp [-1].type == VAL_VALUET) {
1569 MonoClass *c = sp [-1].data.vt.klass;
1570 vt_alloc (&c->byval_arg, sp, FALSE);
1571 stackval_from_data (&c->byval_arg, sp, sp [-1].data.vt.vt, FALSE);
1583 CASE (CEE_JMP) ves_abort(); BREAK;
1584 CASE (CEE_CALLVIRT) /* Fall through */
1585 CASE (CEE_CALLI) /* Fall through */
1587 MonoMethodSignature *csignature;
1589 stackval *endsp = sp;
1591 int virtual = *ip == CEE_CALLVIRT;
1592 int calli = *ip == CEE_CALLI;
1593 unsigned char *code = NULL;
1596 * We ignore tail recursion for now.
1603 token = read32 (ip);
1609 if (frame->method->wrapper_type != MONO_WRAPPER_NONE) {
1610 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (frame->method, token);
1611 child_frame.method = NULL;
1612 } else if ((ji = mono_jit_info_table_find (mono_root_domain, code))) {
1613 child_frame.method = ji->method;
1615 g_assert_not_reached ();
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);
1622 csignature = child_frame.method->signature;
1624 stackval *this_arg = &sp [-csignature->param_count-1];
1625 if (!this_arg->data.p)
1626 THROW_EX (mono_get_exception_null_reference(), ip - 5);
1627 child_frame.method = get_virtual_method (domain, child_frame.method, this_arg);
1628 if (!child_frame.method)
1629 THROW_EX (mono_get_exception_missing_method (), ip -5);
1633 g_assert (csignature->call_convention == MONO_CALL_DEFAULT);
1634 /* decrement by the actual number of args */
1635 if (csignature->param_count) {
1636 sp -= csignature->param_count;
1637 child_frame.stack_args = sp;
1639 child_frame.stack_args = NULL;
1641 if (csignature->hasthis) {
1642 g_assert (sp >= frame->stack);
1645 * It may also be a TP from LD(S)FLDA
1646 * g_assert (sp->type == VAL_OBJ || sp->type == VAL_VALUETA);
1648 if (sp->type == VAL_OBJ && child_frame.method &&
1649 child_frame.method->klass->valuetype) /* unbox it */
1650 child_frame.obj = (char*)sp->data.p + sizeof (MonoObject);
1652 child_frame.obj = sp->data.p;
1654 child_frame.obj = NULL;
1656 if (csignature->ret->type != MONO_TYPE_VOID) {
1657 vt_alloc (csignature->ret, &retval, csignature->pinvoke);
1658 child_frame.retval = &retval;
1660 child_frame.retval = NULL;
1663 child_frame.ex = NULL;
1664 child_frame.ex_handler = NULL;
1666 if (!child_frame.method) {
1668 ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE);
1669 } else if (csignature->hasthis && sp->type == VAL_OBJ &&
1670 ((MonoObject *)sp->data.p)->vtable->klass == mono_defaults.transparent_proxy_class) {
1671 g_assert (child_frame.method);
1672 child_frame.method = mono_marshal_get_remoting_invoke (child_frame.method);
1673 ves_exec_method (&child_frame);
1675 switch (GPOINTER_TO_UINT (child_frame.method->addr)) {
1676 case INLINE_STRING_LENGTH:
1677 retval.type = VAL_I32;
1678 retval.data.i = ((MonoString*)sp->data.p)->length;
1679 /*g_print ("length of '%s' is %d\n", mono_string_to_utf8 (sp->data.p), retval.data.i);*/
1681 case INLINE_ARRAY_LENGTH:
1682 retval.type = VAL_I32;
1683 retval.data.i = mono_array_length ((MonoArray*)sp->data.p);
1685 case INLINE_ARRAY_RANK:
1686 retval.type = VAL_I32;
1687 retval.data.i = mono_object_class (sp->data.p)->rank;
1689 case INLINE_TYPE_ELEMENT_TYPE:
1690 retval.type = VAL_OBJ;
1692 MonoClass *c = mono_class_from_mono_type (((MonoReflectionType*)sp->data.p)->type);
1693 retval.data.vt.klass = NULL;
1694 if (c->enumtype && c->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1695 retval.data.p = mono_type_get_object (domain, c->enum_basetype);
1696 else if (c->element_class)
1697 retval.data.p = mono_type_get_object (domain, &c->element_class->byval_arg);
1699 retval.data.p = NULL;
1703 ves_exec_method (&child_frame);
1707 while (endsp > sp) {
1712 if (child_frame.ex) {
1714 * An exception occurred, need to run finally, fault and catch handlers..
1716 frame->ex = child_frame.ex;
1717 goto handle_finally;
1720 /* need to handle typedbyref ... */
1721 if (csignature->ret->type != MONO_TYPE_VOID) {
1728 if (signature->ret->type != MONO_TYPE_VOID) {
1730 if (sp->type == VAL_VALUET) {
1731 /* the caller has already allocated the memory */
1732 stackval_from_data (signature->ret, frame->retval, sp->data.vt.vt, signature->pinvoke);
1735 *frame->retval = *sp;
1738 if (sp > frame->stack)
1739 g_warning ("more values on stack: %d", sp-frame->stack);
1743 CASE (CEE_BR_S) /* Fall through */
1745 if (*ip == CEE_BR) {
1747 ip += (gint32) read32(ip);
1751 ip += (signed char) *ip;
1755 CASE (CEE_BRFALSE) /* Fall through */
1756 CASE (CEE_BRFALSE_S) {
1759 if (*ip == CEE_BRFALSE_S) {
1760 broffset = (signed char)ip [1];
1763 broffset = (gint32) read32 (ip + 1);
1768 case VAL_I32: result = sp->data.i == 0; break;
1769 case VAL_I64: result = sp->data.l == 0; break;
1770 case VAL_DOUBLE: result = sp->data.f ? 0: 1; break;
1771 default: result = sp->data.p == NULL; break;
1777 CASE (CEE_BRTRUE) /* Fall through */
1778 CASE (CEE_BRTRUE_S) {
1781 if (*ip == CEE_BRTRUE_S) {
1782 broffset = (signed char)ip [1];
1785 broffset = (gint32) read32 (ip + 1);
1790 case VAL_I32: result = sp->data.i != 0; break;
1791 case VAL_I64: result = sp->data.l != 0; break;
1792 case VAL_DOUBLE: result = sp->data.f ? 1 : 0; break;
1793 default: result = sp->data.p != NULL; break;
1799 CASE (CEE_BEQ) /* Fall through */
1803 if (*ip == CEE_BEQ_S) {
1804 broffset = (signed char)ip [1];
1807 broffset = (gint32) read32 (ip + 1);
1811 if (sp->type == VAL_I32)
1812 result = sp [0].data.i == (gint)GET_NATI (sp [1]);
1813 else if (sp->type == VAL_I64)
1814 result = sp [0].data.l == sp [1].data.l;
1815 else if (sp->type == VAL_DOUBLE)
1816 result = sp [0].data.f == sp [1].data.f;
1818 result = (gint)GET_NATI (sp [0]) == (gint)GET_NATI (sp [1]);
1823 CASE (CEE_BGE) /* Fall through */
1827 if (*ip == CEE_BGE_S) {
1828 broffset = (signed char)ip [1];
1831 broffset = (gint32) read32 (ip + 1);
1835 if (sp->type == VAL_I32)
1836 result = sp [0].data.i >= (gint)GET_NATI (sp [1]);
1837 else if (sp->type == VAL_I64)
1838 result = sp [0].data.l >= sp [1].data.l;
1839 else if (sp->type == VAL_DOUBLE)
1840 result = sp [0].data.f >= sp [1].data.f;
1842 result = (gint)GET_NATI (sp [0]) >= (gint)GET_NATI (sp [1]);
1847 CASE (CEE_BGT) /* Fall through */
1851 if (*ip == CEE_BGT_S) {
1852 broffset = (signed char)ip [1];
1855 broffset = (gint32) read32 (ip + 1);
1859 if (sp->type == VAL_I32)
1860 result = sp [0].data.i > (gint)GET_NATI (sp [1]);
1861 else if (sp->type == VAL_I64)
1862 result = sp [0].data.l > sp [1].data.l;
1863 else if (sp->type == VAL_DOUBLE)
1864 result = sp [0].data.f > sp [1].data.f;
1866 result = (gint)GET_NATI (sp [0]) > (gint)GET_NATI (sp [1]);
1871 CASE (CEE_BLT) /* Fall through */
1875 if (*ip == CEE_BLT_S) {
1876 broffset = (signed char)ip [1];
1879 broffset = (gint32) read32 (ip + 1);
1883 if (sp->type == VAL_I32)
1884 result = sp[0].data.i < (gint)GET_NATI(sp[1]);
1885 else if (sp->type == VAL_I64)
1886 result = sp[0].data.l < sp[1].data.l;
1887 else if (sp->type == VAL_DOUBLE)
1888 result = sp[0].data.f < sp[1].data.f;
1890 result = (gint)GET_NATI(sp[0]) < (gint)GET_NATI(sp[1]);
1895 CASE (CEE_BLE) /* fall through */
1899 if (*ip == CEE_BLE_S) {
1900 broffset = (signed char)ip [1];
1903 broffset = (gint32) read32 (ip + 1);
1908 if (sp->type == VAL_I32)
1909 result = sp [0].data.i <= (gint)GET_NATI (sp [1]);
1910 else if (sp->type == VAL_I64)
1911 result = sp [0].data.l <= sp [1].data.l;
1912 else if (sp->type == VAL_DOUBLE)
1913 result = sp [0].data.f <= sp [1].data.f;
1916 * FIXME: here and in other places GET_NATI on the left side
1917 * _will_ be wrong when we change the macro to work on 64 bits
1920 result = (gint)GET_NATI (sp [0]) <= (gint)GET_NATI (sp [1]);
1926 CASE (CEE_BNE_UN) /* Fall through */
1927 CASE (CEE_BNE_UN_S) {
1930 if (*ip == CEE_BNE_UN_S) {
1931 broffset = (signed char)ip [1];
1934 broffset = (gint32) read32 (ip + 1);
1938 if (sp->type == VAL_I32)
1939 result = (guint32)sp [0].data.i != (guint32)GET_NATI (sp [1]);
1940 else if (sp->type == VAL_I64)
1941 result = (guint64)sp [0].data.l != (guint64)sp [1].data.l;
1942 else if (sp->type == VAL_DOUBLE)
1943 result = isunordered (sp [0].data.f, sp [1].data.f) ||
1944 (sp [0].data.f != sp [1].data.f);
1946 result = GET_NATI (sp [0]) != GET_NATI (sp [1]);
1951 CASE (CEE_BGE_UN) /* Fall through */
1952 CASE (CEE_BGE_UN_S) {
1955 if (*ip == CEE_BGE_UN_S) {
1956 broffset = (signed char)ip [1];
1959 broffset = (gint32) read32 (ip + 1);
1963 if (sp->type == VAL_I32)
1964 result = (guint32)sp [0].data.i >= (guint32)GET_NATI (sp [1]);
1965 else if (sp->type == VAL_I64)
1966 result = (guint64)sp [0].data.l >= (guint64)sp [1].data.l;
1967 else if (sp->type == VAL_DOUBLE)
1968 result = !isless (sp [0].data.f,sp [1].data.f);
1970 result = GET_NATI (sp [0]) >= GET_NATI (sp [1]);
1975 CASE (CEE_BGT_UN) /* Fall through */
1976 CASE (CEE_BGT_UN_S) {
1979 if (*ip == CEE_BGT_UN_S) {
1980 broffset = (signed char)ip [1];
1983 broffset = (gint32) read32 (ip + 1);
1987 if (sp->type == VAL_I32)
1988 result = (guint32)sp [0].data.i > (guint32)GET_NATI (sp [1]);
1989 else if (sp->type == VAL_I64)
1990 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
1991 else if (sp->type == VAL_DOUBLE)
1992 result = isgreater (sp [0].data.f, sp [1].data.f);
1994 result = GET_NATI (sp [0]) > GET_NATI (sp [1]);
1999 CASE (CEE_BLE_UN) /* Fall through */
2000 CASE (CEE_BLE_UN_S) {
2003 if (*ip == CEE_BLE_UN_S) {
2004 broffset = (signed char)ip [1];
2007 broffset = (gint32) read32 (ip + 1);
2011 if (sp->type == VAL_I32)
2012 result = (guint32)sp [0].data.i <= (guint32)GET_NATI (sp [1]);
2013 else if (sp->type == VAL_I64)
2014 result = (guint64)sp [0].data.l <= (guint64)sp [1].data.l;
2015 else if (sp->type == VAL_DOUBLE)
2016 result = islessequal (sp [0].data.f, sp [1].data.f);
2018 result = GET_NATI (sp [0]) <= GET_NATI (sp [1]);
2023 CASE (CEE_BLT_UN) /* Fall through */
2024 CASE (CEE_BLT_UN_S) {
2027 if (*ip == CEE_BLT_UN_S) {
2028 broffset = (signed char)ip [1];
2031 broffset = (gint32) read32 (ip + 1);
2035 if (sp->type == VAL_I32)
2036 result = (guint32)sp[0].data.i < (guint32)GET_NATI(sp[1]);
2037 else if (sp->type == VAL_I64)
2038 result = (guint64)sp[0].data.l < (guint64)sp[1].data.l;
2039 else if (sp->type == VAL_DOUBLE)
2040 result = isunordered (sp [0].data.f, sp [1].data.f) ||
2041 (sp [0].data.f < sp [1].data.f);
2043 result = GET_NATI(sp[0]) < GET_NATI(sp[1]);
2050 const unsigned char *st;
2054 st = ip + sizeof (gint32) * n;
2056 if ((guint32)sp->data.i < n) {
2058 ip += sizeof (gint32) * (guint32)sp->data.i;
2059 offset = read32 (ip);
2068 sp[-1].type = VAL_I32;
2069 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2073 sp[-1].type = VAL_I32;
2074 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2078 sp[-1].type = VAL_I32;
2079 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2083 sp[-1].type = VAL_I32;
2084 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2086 CASE (CEE_LDIND_I4) /* Fall through */
2089 sp[-1].type = VAL_I32;
2090 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2094 sp[-1].type = VAL_I64;
2095 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2099 sp[-1].type = VAL_NATI;
2100 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2104 sp[-1].type = VAL_DOUBLE;
2105 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2109 sp[-1].type = VAL_DOUBLE;
2110 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2112 CASE (CEE_LDIND_REF)
2114 sp[-1].type = VAL_OBJ;
2115 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2116 sp[-1].data.vt.klass = NULL;
2118 CASE (CEE_STIND_REF) {
2126 CASE (CEE_STIND_I1) {
2131 *p = (gint8)sp[1].data.i;
2134 CASE (CEE_STIND_I2) {
2139 *p = (gint16)sp[1].data.i;
2142 CASE (CEE_STIND_I4) {
2150 CASE (CEE_STIND_I) {
2155 *p = (mono_i)sp[1].data.p;
2158 CASE (CEE_STIND_I8) {
2166 CASE (CEE_STIND_R4) {
2171 *p = (gfloat)sp[1].data.f;
2174 CASE (CEE_STIND_R8) {
2185 /* should probably consider the pointers as unsigned */
2186 if (sp->type == VAL_I32)
2187 sp [-1].data.i += GET_NATI (sp [0]);
2188 else if (sp->type == VAL_I64)
2189 sp [-1].data.l += sp [0].data.l;
2190 else if (sp->type == VAL_DOUBLE)
2191 sp [-1].data.f += sp [0].data.f;
2193 char *p = sp [-1].data.p;
2194 p += GET_NATI (sp [0]);
2201 /* should probably consider the pointers as unsigned */
2202 if (sp->type == VAL_I32)
2203 sp [-1].data.i -= GET_NATI (sp [0]);
2204 else if (sp->type == VAL_I64)
2205 sp [-1].data.l -= sp [0].data.l;
2206 else if (sp->type == VAL_DOUBLE)
2207 sp [-1].data.f -= sp [0].data.f;
2209 char *p = sp [-1].data.p;
2210 p -= GET_NATI (sp [0]);
2217 if (sp->type == VAL_I32)
2218 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
2219 else if (sp->type == VAL_I64)
2220 sp [-1].data.l *= sp [0].data.l;
2221 else if (sp->type == VAL_DOUBLE)
2222 sp [-1].data.f *= sp [0].data.f;
2227 if (sp->type == VAL_I32) {
2228 if (GET_NATI (sp [0]) == 0)
2229 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2230 sp [-1].data.i /= (gint)GET_NATI (sp [0]);
2231 } else if (sp->type == VAL_I64) {
2232 if (sp [0].data.l == 0)
2233 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2234 sp [-1].data.l /= sp [0].data.l;
2235 } else if (sp->type == VAL_DOUBLE) {
2236 /* set NaN is divisor is 0.0 */
2237 sp [-1].data.f /= sp [0].data.f;
2243 if (sp->type == VAL_I32) {
2245 if (GET_NATI (sp [0]) == 0)
2246 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2247 val = sp [-1].data.i;
2248 val /= (guint32)GET_NATI (sp [0]);
2249 sp [-1].data.i = val;
2250 } else if (sp->type == VAL_I64) {
2252 if (sp [0].data.l == 0)
2253 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2254 val = sp [-1].data.l;
2255 val /= (guint64)sp [0].data.l;
2256 sp [-1].data.l = val;
2257 } else if (sp->type == VAL_NATI) {
2259 if (GET_NATI (sp [0]) == 0)
2260 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2261 val = (mono_u)sp [-1].data.p;
2262 val /= (mono_u)sp [0].data.p;
2263 sp [-1].data.p = (gpointer)val;
2269 if (sp->type == VAL_I32) {
2270 if (GET_NATI (sp [0]) == 0)
2271 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2272 sp [-1].data.i %= (gint)GET_NATI (sp [0]);
2273 } else if (sp->type == VAL_I64) {
2274 if (sp [0].data.l == 0)
2275 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2276 sp [-1].data.l %= sp [0].data.l;
2277 } else if (sp->type == VAL_DOUBLE) {
2278 /* FIXME: what do we actually do here? */
2279 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2281 if (GET_NATI (sp [0]) == 0)
2282 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2283 (gint)GET_NATI (sp [-1]) %= (gint)GET_NATI (sp [0]);
2289 if (sp->type == VAL_I32) {
2290 if (GET_NATI (sp [0]) == 0)
2291 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2292 (guint)sp [-1].data.i %= (guint)GET_NATI (sp [0]);
2293 } else if (sp->type == VAL_I64) {
2294 if (sp [0].data.l == 0)
2295 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2296 (guint64)sp [-1].data.l %= (guint64)sp [0].data.l;
2297 } else if (sp->type == VAL_DOUBLE) {
2298 /* unspecified behaviour according to the spec */
2300 if (GET_NATI (sp [0]) == 0)
2301 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2302 (guint64)GET_NATI (sp [-1]) %= (guint64)GET_NATI (sp [0]);
2308 if (sp->type == VAL_I32)
2309 sp [-1].data.i &= GET_NATI (sp [0]);
2310 else if (sp->type == VAL_I64)
2311 sp [-1].data.l &= sp [0].data.l;
2313 GET_NATI (sp [-1]) &= GET_NATI (sp [0]);
2318 if (sp->type == VAL_I32)
2319 sp [-1].data.i |= GET_NATI (sp [0]);
2320 else if (sp->type == VAL_I64)
2321 sp [-1].data.l |= sp [0].data.l;
2323 GET_NATI (sp [-1]) |= GET_NATI (sp [0]);
2328 if (sp->type == VAL_I32)
2329 sp [-1].data.i ^= GET_NATI (sp [0]);
2330 else if (sp->type == VAL_I64)
2331 sp [-1].data.l ^= sp [0].data.l;
2333 GET_NATI (sp [-1]) ^= GET_NATI (sp [0]);
2338 if (sp [-1].type == VAL_I32)
2339 sp [-1].data.i <<= GET_NATI (sp [0]);
2340 else if (sp [-1].type == VAL_I64)
2341 sp [-1].data.l <<= GET_NATI (sp [0]);
2343 GET_NATI (sp [-1]) <<= GET_NATI (sp [0]);
2348 if (sp [-1].type == VAL_I32)
2349 sp [-1].data.i >>= GET_NATI (sp [0]);
2350 else if (sp [-1].type == VAL_I64)
2351 sp [-1].data.l >>= GET_NATI (sp [0]);
2353 (gint)GET_NATI (sp [-1]) >>= GET_NATI (sp [0]);
2358 if (sp [-1].type == VAL_I32)
2359 (guint)sp [-1].data.i >>= GET_NATI (sp [0]);
2360 else if (sp [-1].type == VAL_I64)
2361 (guint64)sp [-1].data.l >>= GET_NATI (sp [0]);
2363 (guint64)GET_NATI (sp [-1]) >>= GET_NATI (sp [0]);
2368 if (sp->type == VAL_I32)
2369 sp->data.i = - sp->data.i;
2370 else if (sp->type == VAL_I64)
2371 sp->data.l = - sp->data.l;
2372 else if (sp->type == VAL_DOUBLE)
2373 sp->data.f = - sp->data.f;
2374 else if (sp->type == VAL_NATI)
2375 sp->data.p = (gpointer)(- (mono_i)sp->data.p);
2381 if (sp->type == VAL_I32)
2382 sp->data.i = ~ sp->data.i;
2383 else if (sp->type == VAL_I64)
2384 sp->data.l = ~ sp->data.l;
2385 else if (sp->type == VAL_NATI)
2386 sp->data.p = (gpointer)(~ (mono_i)sp->data.p);
2389 CASE (CEE_CONV_U1) /* fall through */
2390 CASE (CEE_CONV_I1) {
2392 switch (sp [-1].type) {
2394 sp [-1].data.i = (gint8)sp [-1].data.f;
2397 sp [-1].data.i = (gint8)sp [-1].data.l;
2402 sp [-1].data.i = (gint8)sp [-1].data.i;
2405 sp [-1].data.i = (gint8)sp [-1].data.nati;
2408 sp [-1].type = VAL_I32;
2411 CASE (CEE_CONV_U2) /* fall through */
2412 CASE (CEE_CONV_I2) {
2414 switch (sp [-1].type) {
2416 sp [-1].data.i = (gint16)sp [-1].data.f;
2419 sp [-1].data.i = (gint16)sp [-1].data.l;
2424 sp [-1].data.i = (gint16)sp [-1].data.i;
2427 sp [-1].data.i = (gint16)sp [-1].data.nati;
2430 sp [-1].type = VAL_I32;
2433 CASE (CEE_CONV_U4) /* Fall through */
2434 #if SIZEOF_VOID_P == 4
2435 CASE (CEE_CONV_I) /* Fall through */
2436 CASE (CEE_CONV_U) /* Fall through */
2438 CASE (CEE_CONV_I4) {
2440 switch (sp [-1].type) {
2442 sp [-1].data.i = (gint32)sp [-1].data.f;
2445 sp [-1].data.i = (gint32)sp [-1].data.l;
2452 sp [-1].data.i = (gint32)sp [-1].data.p;
2455 sp [-1].type = VAL_I32;
2458 #if SIZEOF_VOID_P == 8
2459 CASE (CEE_CONV_I) /* Fall through */
2463 switch (sp [-1].type) {
2465 sp [-1].data.l = (gint64)sp [-1].data.f;
2472 sp [-1].data.l = (gint64)sp [-1].data.i;
2475 sp [-1].data.l = (gint64)sp [-1].data.nati;
2478 sp [-1].type = VAL_I64;
2480 CASE (CEE_CONV_R4) {
2482 switch (sp [-1].type) {
2484 sp [-1].data.f = (float)sp [-1].data.f;
2487 sp [-1].data.f = (float)sp [-1].data.l;
2492 sp [-1].data.f = (float)sp [-1].data.i;
2495 sp [-1].data.f = (float)sp [-1].data.nati;
2498 sp [-1].type = VAL_DOUBLE;
2501 CASE (CEE_CONV_R8) {
2503 switch (sp [-1].type) {
2505 sp [-1].data.f = (double)sp [-1].data.f;
2508 sp [-1].data.f = (double)sp [-1].data.l;
2513 sp [-1].data.f = (double)sp [-1].data.i;
2516 sp [-1].data.f = (double)sp [-1].data.nati;
2519 sp [-1].type = VAL_DOUBLE;
2522 #if SIZEOF_VOID_P == 8
2523 CASE (CEE_CONV_U) /* Fall through */
2528 switch (sp [-1].type){
2530 sp [-1].data.l = (guint64)sp [-1].data.f;
2537 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2540 sp [-1].data.l = (guint64) sp [-1].data.nati;
2543 sp [-1].type = VAL_I64;
2548 vtklass = mono_class_get (image, read32 (ip));
2551 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2560 token = read32 (ip);
2563 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
2564 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
2566 c = mono_class_get (image, token);
2568 addr = sp [-1].data.vt.vt;
2569 vt_alloc (&c->byval_arg, &sp [-1], FALSE);
2570 stackval_from_data (&c->byval_arg, &sp [-1], addr, FALSE);
2578 str_index = mono_metadata_token_index (read32 (ip));
2581 o = (MonoObject*)mono_ldstr (domain, image, str_index);
2584 sp->data.vt.klass = NULL;
2591 MonoClass *newobj_class;
2592 MonoMethodSignature *csig;
2593 stackval valuetype_this;
2594 stackval *endsp = sp;
2601 token = read32 (ip);
2604 if (!(child_frame.method = mono_get_method (image, token, NULL)))
2605 THROW_EX (mono_get_exception_missing_method (), ip -5);
2607 csig = child_frame.method->signature;
2608 newobj_class = child_frame.method->klass;
2609 /*if (profiling_classes) {
2610 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2612 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2616 if (newobj_class->parent == mono_defaults.array_class) {
2617 sp -= csig->param_count;
2618 o = ves_array_create (domain, newobj_class, csig, sp);
2619 goto array_constructed;
2623 * First arg is the object.
2625 if (newobj_class->valuetype) {
2627 vt_alloc (&newobj_class->byval_arg, &valuetype_this, csig->pinvoke);
2628 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2629 zero = valuetype_this.data.vt.vt;
2630 child_frame.obj = valuetype_this.data.vt.vt;
2632 memset (&valuetype_this, 0, sizeof (stackval));
2633 zero = &valuetype_this;
2634 child_frame.obj = &valuetype_this;
2636 stackval_from_data (&newobj_class->byval_arg, &valuetype_this, zero, csig->pinvoke);
2638 if (newobj_class != mono_defaults.string_class) {
2639 o = mono_object_new (domain, newobj_class);
2640 child_frame.obj = o;
2642 child_frame.retval = &retval;
2646 if (csig->param_count) {
2647 sp -= csig->param_count;
2648 child_frame.stack_args = sp;
2650 child_frame.stack_args = NULL;
2653 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2655 child_frame.ex = NULL;
2656 child_frame.ex_handler = NULL;
2658 ves_exec_method (&child_frame);
2660 while (endsp > sp) {
2665 if (child_frame.ex) {
2667 * An exception occurred, need to run finally, fault and catch handlers..
2669 frame->ex = child_frame.ex;
2670 goto handle_finally;
2673 * a constructor returns void, but we need to return the object we created
2676 if (newobj_class->valuetype && !newobj_class->enumtype) {
2677 *sp = valuetype_this;
2678 } else if (newobj_class == mono_defaults.string_class) {
2683 sp->data.vt.klass = newobj_class;
2688 CASE (CEE_CASTCLASS) /* Fall through */
2692 MonoClass *c , *oclass;
2694 int do_isinst = *ip == CEE_ISINST;
2695 gboolean found = FALSE;
2698 token = read32 (ip);
2699 c = mono_class_get (image, token);
2701 g_assert (sp [-1].type == VAL_OBJ);
2703 if ((o = sp [-1].data.p)) {
2708 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2709 if ((c->interface_id <= oclass->max_interface_id) &&
2710 vt->interface_offsets [c->interface_id])
2713 if (oclass == mono_defaults.transparent_proxy_class) {
2714 /* fixme: add check for IRemotingTypeInfo */
2715 MonoRealProxy *rp = ((MonoTransparentProxy *)o)->rp;
2717 type = rp->class_to_proxy->type;
2718 oclass = mono_class_from_mono_type (type);
2720 /* handle array casts */
2722 if ((oclass->rank == c->rank) &&
2723 (oclass->cast_class->baseval - c->cast_class->baseval) <=
2724 c->cast_class->diffval) {
2725 sp [-1].data.vt.klass = c;
2728 } else if ((oclass->baseval - c->baseval) <= c->diffval) {
2729 sp [-1].data.vt.klass = c;
2736 sp [-1].data.p = NULL;
2737 sp [-1].data.vt.klass = NULL;
2739 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
2745 CASE (CEE_CONV_R_UN)
2747 switch (sp [-1].type) {
2751 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2756 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2759 sp [-1].data.f = (double)(guint64)sp [-1].data.nati;
2762 sp [-1].type = VAL_DOUBLE;
2765 CASE (CEE_UNUSED1) ves_abort(); BREAK;
2772 token = read32 (ip);
2774 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
2775 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
2777 c = mono_class_get (image, token);
2781 THROW_EX (mono_get_exception_null_reference(), ip - 1);
2783 if (o->vtable->klass->element_class->type_token != c->element_class->type_token)
2784 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
2786 sp [-1].type = VAL_MP;
2787 sp [-1].data.p = (char *)o + sizeof (MonoObject);
2794 frame->ex_handler = NULL;
2796 sp->data.p = mono_get_exception_null_reference ();
2797 THROW_EX (sp->data.p, ip);
2799 CASE (CEE_LDFLDA) /* Fall through */
2802 MonoClassField *field;
2804 int load_addr = *ip == CEE_LDFLDA;
2807 if (!sp [-1].data.p)
2808 THROW_EX (mono_get_exception_null_reference (), ip);
2811 token = read32 (ip);
2814 if (sp [-1].type == VAL_OBJ) {
2815 obj = sp [-1].data.p;
2817 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) {
2818 MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
2819 field = mono_class_get_field (klass, token);
2820 addr = mono_load_remote_field (obj, klass, field, NULL);
2822 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
2823 field = mono_field_from_memberref (image, token, NULL);
2825 field = mono_class_get_field (obj->vtable->klass, token);
2826 addr = (char*)obj + field->offset;
2829 obj = sp [-1].data.vt.vt;
2830 field = mono_class_get_field (sp [-1].data.vt.klass, token);
2831 addr = (char*)obj + field->offset - sizeof (MonoObject);
2835 sp [-1].type = VAL_TP;
2836 sp [-1].data.p = addr;
2837 sp [-1].data.vt.klass = mono_class_from_mono_type (field->type);
2839 vt_alloc (field->type, &sp [-1], FALSE);
2840 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2847 MonoClassField *field;
2848 guint32 token, offset;
2853 THROW_EX (mono_get_exception_null_reference (), ip);
2856 token = read32 (ip);
2859 if (sp [0].type == VAL_OBJ) {
2860 obj = sp [0].data.p;
2862 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) {
2863 MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
2864 field = mono_class_get_field (klass, token);
2866 mono_store_remote_field (obj, klass, field, &sp [1].data);
2867 offset = field->offset;
2869 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
2870 field = mono_field_from_memberref (image, token, NULL);
2872 field = mono_class_get_field (obj->vtable->klass, token);
2873 offset = field->offset;
2874 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
2878 obj = sp [0].data.vt.vt;
2879 field = mono_class_get_field (sp [0].data.vt.klass, token);
2880 offset = field->offset - sizeof (MonoObject);
2881 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
2887 CASE (CEE_LDSFLD) /* Fall through */
2888 CASE (CEE_LDSFLDA) {
2891 MonoClassField *field;
2893 int load_addr = *ip == CEE_LDSFLDA;
2897 token = read32 (ip);
2900 /* need to handle fieldrefs */
2901 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
2902 field = mono_field_from_memberref (image, token, &klass);
2904 klass = mono_class_get (image,
2905 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
2906 field = mono_class_get_field (klass, token);
2910 vt = mono_class_vtable (domain, klass);
2911 addr = (char*)(vt->data) + field->offset;
2916 sp->data.vt.klass = mono_class_from_mono_type (field->type);
2918 vt_alloc (field->type, sp, FALSE);
2919 stackval_from_data (field->type, sp, addr, FALSE);
2927 MonoClassField *field;
2932 token = read32 (ip);
2936 /* need to handle fieldrefs */
2937 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
2938 field = mono_field_from_memberref (image, token, &klass);
2940 klass = mono_class_get (image,
2941 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
2942 field = mono_class_get_field (klass, token);
2946 vt = mono_class_vtable (domain, klass);
2947 addr = (char*)(vt->data) + field->offset;
2949 stackval_to_data (field->type, sp, addr, FALSE);
2956 vtklass = mono_class_get (image, read32 (ip));
2959 memcpy (sp [0].data.p, sp [1].data.vt.vt, mono_class_value_size (vtklass, NULL));
2962 #if SIZEOF_VOID_P == 8
2963 CASE (CEE_CONV_OVF_I_UN)
2965 CASE (CEE_CONV_OVF_I8_UN) {
2966 switch (sp [-1].type) {
2968 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807L)
2969 THROW_EX (mono_get_exception_overflow (), ip);
2970 sp [-1].data.l = (guint64)sp [-1].data.f;
2977 /* Can't overflow */
2978 sp [-1].data.l = (guint64)sp [-1].data.i;
2981 sp [-1].data.l = (guint64)sp [-1].data.nati;
2984 sp [-1].type = VAL_I64;
2988 #if SIZEOF_VOID_P == 8
2989 CASE (CEE_CONV_OVF_U_UN)
2991 CASE (CEE_CONV_OVF_U8_UN) {
2992 switch (sp [-1].type) {
2994 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT64_MAX)
2995 THROW_EX (mono_get_exception_overflow (), ip);
2996 sp [-1].data.l = (guint64)sp [-1].data.f;
3004 /* Can't overflow */
3005 sp [-1].data.l = (guint64)sp [-1].data.i;
3008 /* Can't overflow */
3009 sp [-1].data.l = (guint64)sp [-1].data.nati;
3012 sp [-1].type = VAL_I64;
3016 #if SIZEOF_VOID_P == 4
3017 CASE (CEE_CONV_OVF_I_UN)
3018 CASE (CEE_CONV_OVF_U_UN)
3020 CASE (CEE_CONV_OVF_I1_UN)
3021 CASE (CEE_CONV_OVF_I2_UN)
3022 CASE (CEE_CONV_OVF_I4_UN)
3023 CASE (CEE_CONV_OVF_U1_UN)
3024 CASE (CEE_CONV_OVF_U2_UN)
3025 CASE (CEE_CONV_OVF_U4_UN) {
3027 switch (sp [-1].type) {
3029 value = (guint64)sp [-1].data.f;
3032 value = (guint64)sp [-1].data.l;
3037 value = (guint64)sp [-1].data.i;
3040 value = (guint64)sp [-1].data.nati;
3044 case CEE_CONV_OVF_I1_UN:
3046 THROW_EX (mono_get_exception_overflow (), ip);
3047 sp [-1].data.i = value;
3048 sp [-1].type = VAL_I32;
3050 case CEE_CONV_OVF_I2_UN:
3052 THROW_EX (mono_get_exception_overflow (), ip);
3053 sp [-1].data.i = value;
3054 sp [-1].type = VAL_I32;
3056 #if SIZEOF_VOID_P == 4
3057 case CEE_CONV_OVF_I_UN: /* Fall through */
3059 case CEE_CONV_OVF_I4_UN:
3060 if (value > 2147483647)
3061 THROW_EX (mono_get_exception_overflow (), ip);
3062 sp [-1].data.i = value;
3063 sp [-1].type = VAL_I32;
3065 case CEE_CONV_OVF_U1_UN:
3067 THROW_EX (mono_get_exception_overflow (), ip);
3068 sp [-1].data.i = value;
3069 sp [-1].type = VAL_I32;
3071 case CEE_CONV_OVF_U2_UN:
3073 THROW_EX (mono_get_exception_overflow (), ip);
3074 sp [-1].data.i = value;
3075 sp [-1].type = VAL_I32;
3077 #if SIZEOF_VOID_P == 4
3078 case CEE_CONV_OVF_U_UN: /* Fall through */
3080 case CEE_CONV_OVF_U4_UN:
3081 if (value > 4294967295U)
3082 THROW_EX (mono_get_exception_overflow (), ip);
3083 sp [-1].data.i = value;
3084 sp [-1].type = VAL_I32;
3087 g_assert_not_reached ();
3097 token = read32 (ip);
3099 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3100 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3102 class = mono_class_get (image, token);
3103 g_assert (class != NULL);
3105 sp [-1].type = VAL_OBJ;
3106 if (class->byval_arg.type == MONO_TYPE_VALUETYPE && !class->enumtype)
3107 sp [-1].data.p = mono_value_box (domain, class, sp [-1].data.p);
3109 stackval_to_data (&class->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3110 sp [-1].data.p = mono_value_box (domain, class, &sp [-1]);
3112 /* need to vt_free (sp); */
3124 token = read32 (ip);
3126 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3127 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3129 class = mono_class_get (image, token);
3131 o = (MonoObject*) mono_array_new (domain, class, sp [-1].data.i);
3134 sp [-1].type = VAL_OBJ;
3136 /*if (profiling_classes) {
3137 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3139 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3149 g_assert (sp [-1].type == VAL_OBJ);
3153 THROW_EX (mono_get_exception_null_reference (), ip - 1);
3155 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3157 sp [-1].type = VAL_I32;
3158 sp [-1].data.i = mono_array_length (o);
3162 CASE (CEE_LDELEMA) {
3164 guint32 esize, token;
3167 token = read32 (ip);
3171 g_assert (sp [0].type == VAL_OBJ);
3174 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3176 if (sp [1].data.nati >= mono_array_length (o))
3177 THROW_EX (mono_get_exception_index_out_of_range (), ip - 5);
3179 /* check the array element corresponds to token */
3180 esize = mono_array_element_size (o->obj.vtable->klass);
3183 sp->data.p = mono_array_addr_with_size (o, esize, sp [1].data.i);
3184 sp->data.vt.klass = o->obj.vtable->klass->element_class;
3189 CASE (CEE_LDELEM_I1) /* fall through */
3190 CASE (CEE_LDELEM_U1) /* fall through */
3191 CASE (CEE_LDELEM_I2) /* fall through */
3192 CASE (CEE_LDELEM_U2) /* fall through */
3193 CASE (CEE_LDELEM_I4) /* fall through */
3194 CASE (CEE_LDELEM_U4) /* fall through */
3195 CASE (CEE_LDELEM_I8) /* fall through */
3196 CASE (CEE_LDELEM_I) /* fall through */
3197 CASE (CEE_LDELEM_R4) /* fall through */
3198 CASE (CEE_LDELEM_R8) /* fall through */
3199 CASE (CEE_LDELEM_REF) {
3205 g_assert (sp [0].type == VAL_OBJ);
3208 THROW_EX (mono_get_exception_null_reference (), ip);
3210 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3212 aindex = sp [1].data.nati;
3213 if (aindex >= mono_array_length (o))
3214 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3217 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3221 sp [0].data.i = mono_array_get (o, gint8, aindex);
3222 sp [0].type = VAL_I32;
3225 sp [0].data.i = mono_array_get (o, guint8, aindex);
3226 sp [0].type = VAL_I32;
3229 sp [0].data.i = mono_array_get (o, gint16, aindex);
3230 sp [0].type = VAL_I32;
3233 sp [0].data.i = mono_array_get (o, guint16, aindex);
3234 sp [0].type = VAL_I32;
3237 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3238 sp [0].type = VAL_NATI;
3241 sp [0].data.i = mono_array_get (o, gint32, aindex);
3242 sp [0].type = VAL_I32;
3245 sp [0].data.i = mono_array_get (o, guint32, aindex);
3246 sp [0].type = VAL_I32;
3249 sp [0].data.l = mono_array_get (o, guint64, aindex);
3250 sp [0].type = VAL_I64;
3253 sp [0].data.f = mono_array_get (o, float, aindex);
3254 sp [0].type = VAL_DOUBLE;
3257 sp [0].data.f = mono_array_get (o, double, aindex);
3258 sp [0].type = VAL_DOUBLE;
3260 case CEE_LDELEM_REF:
3261 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3262 sp [0].data.vt.klass = NULL;
3263 sp [0].type = VAL_OBJ;
3273 CASE (CEE_STELEM_I) /* fall through */
3274 CASE (CEE_STELEM_I1) /* fall through */
3275 CASE (CEE_STELEM_I2) /* fall through */
3276 CASE (CEE_STELEM_I4) /* fall through */
3277 CASE (CEE_STELEM_I8) /* fall through */
3278 CASE (CEE_STELEM_R4) /* fall through */
3279 CASE (CEE_STELEM_R8) /* fall through */
3280 CASE (CEE_STELEM_REF) {
3287 g_assert (sp [0].type == VAL_OBJ);
3290 THROW_EX (mono_get_exception_null_reference (), ip);
3292 ac = o->obj.vtable->klass;
3293 g_assert (MONO_CLASS_IS_ARRAY (ac));
3295 aindex = sp [1].data.nati;
3296 if (aindex >= mono_array_length (o))
3297 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3300 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3304 mono_array_set (o, mono_i, aindex, sp [2].data.nati);
3307 mono_array_set (o, gint8, aindex, sp [2].data.i);
3310 mono_array_set (o, gint16, aindex, sp [2].data.i);
3313 mono_array_set (o, gint32, aindex, sp [2].data.i);
3316 mono_array_set (o, gint64, aindex, sp [2].data.l);
3319 mono_array_set (o, float, aindex, sp [2].data.f);
3322 mono_array_set (o, double, aindex, sp [2].data.f);
3324 case CEE_STELEM_REF:
3325 g_assert (sp [2].type == VAL_OBJ);
3326 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3327 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3328 mono_array_set (o, gpointer, aindex, sp [2].data.p);
3352 CASE (CEE_UNUSED17) ves_abort(); BREAK;
3353 CASE (CEE_CONV_OVF_I1)
3354 if (sp [-1].type == VAL_I32) {
3355 if (sp [-1].data.i < 128 || sp [-1].data.i > 127)
3356 THROW_EX (mono_get_exception_overflow (), ip);
3357 sp [-1].data.i = (gint8)sp [-1].data.i;
3358 } else if (sp [-1].type == VAL_I64) {
3359 if (sp [-1].data.l < 128 || sp [-1].data.l > 127)
3360 THROW_EX (mono_get_exception_overflow (), ip);
3361 sp [-1].data.i = (gint8)sp [-1].data.l;
3367 CASE (CEE_CONV_OVF_U1)
3368 if (sp [-1].type == VAL_I32) {
3369 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3370 THROW_EX (mono_get_exception_overflow (), ip);
3371 sp [-1].data.i = (gint8)sp [-1].data.i;
3372 } else if (sp [-1].type == VAL_I64) {
3373 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3374 THROW_EX (mono_get_exception_overflow (), ip);
3375 sp [-1].data.i = (gint8)sp [-1].data.l;
3381 CASE (CEE_CONV_OVF_I2)
3382 CASE (CEE_CONV_OVF_U2)
3384 /* FIXME: handle other cases */
3385 if (sp [-1].type == VAL_I32) {
3386 /* defined as NOP */
3391 CASE (CEE_CONV_OVF_I4)
3392 /* FIXME: handle other cases */
3393 if (sp [-1].type == VAL_I32) {
3394 /* defined as NOP */
3395 } else if(sp [-1].type == VAL_I64) {
3396 sp [-1].data.i = (gint32)sp [-1].data.l;
3397 sp [-1].type = VAL_I32;
3403 CASE (CEE_CONV_OVF_U4)
3404 /* FIXME: handle other cases */
3405 if (sp [-1].type == VAL_I32) {
3406 /* defined as NOP */
3407 } else if(sp [-1].type == VAL_I64) {
3408 sp [-1].data.i = (guint32)sp [-1].data.l;
3409 sp [-1].type = VAL_I32;
3415 CASE (CEE_CONV_OVF_I8)
3416 /* FIXME: handle other cases */
3417 if (sp [-1].type == VAL_I32) {
3418 sp [-1].data.l = (guint64)sp [-1].data.i;
3419 sp [-1].type = VAL_I64;
3420 } else if(sp [-1].type == VAL_I64) {
3421 /* defined as NOP */
3427 CASE (CEE_CONV_OVF_U8)
3428 /* FIXME: handle other cases */
3429 if (sp [-1].type == VAL_I32) {
3430 sp [-1].data.l = (guint64) sp [-1].data.i;
3431 sp [-1].type = VAL_I64;
3432 } else if(sp [-1].type == VAL_I64) {
3433 /* defined as NOP */
3445 CASE (CEE_UNUSED23) ves_abort(); BREAK;
3446 CASE (CEE_REFANYVAL) ves_abort(); BREAK;
3448 if (!finite(sp [-1].data.f))
3449 THROW_EX (mono_get_exception_arithmetic (), ip);
3452 CASE (CEE_UNUSED24) ves_abort(); BREAK;
3453 CASE (CEE_UNUSED25) ves_abort(); BREAK;
3454 CASE (CEE_MKREFANY) ves_abort(); BREAK;
3463 CASE (CEE_UNUSED67) ves_abort(); BREAK;
3464 CASE (CEE_LDTOKEN) {
3466 MonoClass *handle_class;
3468 handle = mono_ldtoken (image, read32 (ip), &handle_class);
3470 vt_alloc (&handle_class->byval_arg, sp, FALSE);
3471 stackval_from_data (&handle_class->byval_arg, sp, (char*)&handle, FALSE);
3475 CASE (CEE_CONV_OVF_I)
3478 /* FIXME: check overflow. */
3481 sp->data.p = (gpointer)(mono_i) sp->data.i;
3484 sp->data.p = (gpointer)(mono_i) sp->data.l;
3489 sp->data.p = (gpointer)(mono_i) sp->data.f;
3494 sp->type = VAL_NATI;
3497 CASE (CEE_CONV_OVF_U) ves_abort(); BREAK;
3500 /* FIXME: check overflow */
3501 if (sp->type == VAL_I32) {
3502 if (CHECK_ADD_OVERFLOW (sp [-1].data.i, GET_NATI (sp [0])))
3503 THROW_EX (mono_get_exception_overflow (), ip);
3504 sp [-1].data.i = (gint32)sp [-1].data.i + (gint32)GET_NATI (sp [0]);
3505 } else if (sp->type == VAL_I64) {
3506 if (CHECK_ADD_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3507 THROW_EX (mono_get_exception_overflow (), ip);
3508 sp [-1].data.l = (gint64)sp [-1].data.l + (gint64)sp [0].data.l;
3509 } else if (sp->type == VAL_DOUBLE)
3510 sp [-1].data.f += sp [0].data.f;
3512 char *p = sp [-1].data.p;
3513 p += GET_NATI (sp [0]);
3518 CASE (CEE_ADD_OVF_UN)
3520 /* FIXME: check overflow, make unsigned */
3521 if (sp->type == VAL_I32) {
3522 if (CHECK_ADD_OVERFLOW_UN (sp [-1].data.i, GET_NATI (sp [0])))
3523 THROW_EX (mono_get_exception_overflow (), ip);
3524 sp [-1].data.i = (guint32)sp [-1].data.i + (guint32)GET_NATI (sp [0]);
3525 } else if (sp->type == VAL_I64) {
3526 if (CHECK_ADD_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3527 THROW_EX (mono_get_exception_overflow (), ip);
3528 sp [-1].data.l = (guint64)sp [-1].data.l + (guint64)sp [0].data.l;
3529 } else if (sp->type == VAL_DOUBLE)
3530 sp [-1].data.f += sp [0].data.f;
3532 char *p = sp [-1].data.p;
3533 p += GET_NATI (sp [0]);
3541 /* FIXME: check overflow */
3542 if (sp->type == VAL_I32) {
3543 if (CHECK_MUL_OVERFLOW (sp [-1].data.i, GET_NATI (sp [0])))
3544 THROW_EX (mono_get_exception_overflow (), ip);
3545 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
3547 else if (sp->type == VAL_I64) {
3548 if (CHECK_MUL_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3549 THROW_EX (mono_get_exception_overflow (), ip);
3550 sp [-1].data.l *= sp [0].data.l;
3552 else if (sp->type == VAL_DOUBLE)
3553 sp [-1].data.f *= sp [0].data.f;
3555 CASE (CEE_MUL_OVF_UN)
3558 /* FIXME: check overflow, make unsigned */
3559 if (sp->type == VAL_I32) {
3560 if (CHECK_MUL_OVERFLOW_UN (sp [-1].data.i, GET_NATI (sp [0])))
3561 THROW_EX (mono_get_exception_overflow (), ip);
3562 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
3564 else if (sp->type == VAL_I64) {
3565 if (CHECK_MUL_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3566 THROW_EX (mono_get_exception_overflow (), ip);
3567 sp [-1].data.l *= sp [0].data.l;
3569 else if (sp->type == VAL_DOUBLE)
3570 sp [-1].data.f *= sp [0].data.f;
3573 CASE (CEE_SUB_OVF_UN)
3576 /* FIXME: handle undeflow/unsigned */
3577 /* should probably consider the pointers as unsigned */
3578 if (sp->type == VAL_I32)
3579 sp [-1].data.i -= GET_NATI (sp [0]);
3580 else if (sp->type == VAL_I64)
3581 sp [-1].data.l -= sp [0].data.l;
3582 else if (sp->type == VAL_DOUBLE)
3583 sp [-1].data.f -= sp [0].data.f;
3585 char *p = sp [-1].data.p;
3586 p -= GET_NATI (sp [0]);
3590 CASE (CEE_ENDFINALLY)
3592 ip = finally_ips->data;
3593 finally_ips = g_slist_remove (finally_ips, ip);
3599 * There was no exception, we continue normally at the target address.
3603 CASE (CEE_LEAVE) /* Fall through */
3605 sp = frame->stack; /* empty the stack */
3607 if (*ip == CEE_LEAVE_S) {
3609 ip += (signed char) *ip;
3613 ip += (gint32) read32 (ip);
3618 * We may be either inside a try block or inside an handler.
3619 * In the first case there was no exception and we go on
3620 * executing the finally handlers and after that resume control
3622 * In the second case we need to clear the exception and
3623 * continue directly at the target ip.
3627 goto handle_finally;
3630 frame->ex_handler = NULL;
3634 frame->ex_handler = NULL;
3636 goto handle_finally;
3642 case CEE_MONO_FUNC1: {
3643 MonoMarshalConv conv;
3653 sp->data.vt.klass = NULL;
3656 case MONO_MARSHAL_CONV_STR_LPWSTR:
3657 sp->data.p = mono_string_to_utf16 (sp->data.p);
3659 case MONO_MARSHAL_CONV_LPSTR_STR:
3660 sp->data.p = mono_string_new_wrapper (sp->data.p);
3662 case MONO_MARSHAL_CONV_STR_LPTSTR:
3663 case MONO_MARSHAL_CONV_STR_LPSTR:
3664 sp->data.p = mono_string_to_utf8 (sp->data.p);
3666 case MONO_MARSHAL_CONV_STR_BSTR:
3667 sp->data.p = mono_string_to_bstr (sp->data.p);
3669 case MONO_MARSHAL_CONV_STR_TBSTR:
3670 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3671 sp->data.p = mono_string_to_ansibstr (sp->data.p);
3673 case MONO_MARSHAL_CONV_SB_LPSTR:
3674 sp->data.p = mono_string_builder_to_utf8 (sp->data.p);
3676 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3677 sp->data.p = mono_array_to_savearray (sp->data.p);
3679 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3680 sp->data.p = mono_array_to_lparray (sp->data.p);
3682 case MONO_MARSHAL_CONV_DEL_FTN:
3683 sp->data.p = mono_delegate_to_ftnptr (sp->data.p);
3685 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3686 sp->data.p = mono_marshal_string_array (sp->data.p);
3689 g_assert_not_reached ();
3694 case CEE_MONO_PROC2: {
3695 MonoMarshalConv conv;
3703 case MONO_MARSHAL_CONV_LPSTR_SB:
3704 mono_string_utf8_to_builder (sp [0].data.p, sp [1].data.p);
3706 case MONO_MARSHAL_FREE_ARRAY:
3707 mono_marshal_free_array (sp [0].data.p, sp [1].data.i);
3710 g_assert_not_reached ();
3714 case CEE_MONO_PROC3: {
3715 MonoMarshalConv conv;
3723 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3724 mono_string_to_byvalstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3726 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3727 mono_string_to_byvalwstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3730 g_assert_not_reached ();
3734 case CEE_MONO_VTADDR: {
3737 sp->type = VAL_VALUETA;
3741 case CEE_MONO_LDPTR: {
3745 token = read32 (ip);
3749 sp->data.p = mono_method_get_wrapper_data (frame->method, token);
3750 sp->data.vt.klass = NULL;
3754 case CEE_MONO_FREE: {
3758 g_free (sp->data.p);
3761 case CEE_MONO_OBJADDR: {
3768 case CEE_MONO_NEWOBJ: {
3773 token = read32 (ip);
3776 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3777 sp->data.p = mono_object_new (domain, class);
3781 case CEE_MONO_RETOBJ: {
3786 token = read32 (ip);
3791 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3793 stackval_from_data (signature->ret, frame->retval, sp->data.vt.vt, signature->pinvoke);
3795 if (sp > frame->stack)
3796 g_warning ("more values on stack: %d", sp-frame->stack);
3801 g_error ("Unimplemented opcode: 0xF0 %02x at 0x%x\n", *ip, ip-header->code);
3832 CASE (CEE_PREFIXREF) ves_abort(); BREAK;
3834 * Note: Exceptions thrown when executing a prefixed opcode need
3835 * to take into account the number of prefix bytes (usually the
3836 * throw point is just (ip - n_prefix_bytes).
3841 case CEE_ARGLIST: ves_abort(); break;
3847 if (sp->type == VAL_I32)
3848 result = sp [0].data.i == (gint)GET_NATI (sp [1]);
3849 else if (sp->type == VAL_I64)
3850 result = sp [0].data.l == sp [1].data.l;
3851 else if (sp->type == VAL_DOUBLE) {
3852 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3855 result = sp [0].data.f == sp [1].data.f;
3857 result = GET_NATI (sp [0]) == GET_NATI (sp [1]);
3859 sp->data.i = result;
3869 if (sp->type == VAL_I32)
3870 result = sp [0].data.i > (gint)GET_NATI (sp [1]);
3871 else if (sp->type == VAL_I64)
3872 result = sp [0].data.l > sp [1].data.l;
3873 else if (sp->type == VAL_DOUBLE) {
3874 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3877 result = sp [0].data.f > sp [1].data.f;
3879 result = (gint)GET_NATI (sp [0]) > (gint)GET_NATI (sp [1]);
3881 sp->data.i = result;
3891 if (sp->type == VAL_I32)
3892 result = (guint32)sp [0].data.i > (mono_u)GET_NATI (sp [1]);
3893 else if (sp->type == VAL_I64)
3894 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
3895 else if (sp->type == VAL_DOUBLE)
3896 result = isnan (sp [0].data.f) || isnan (sp [1].data.f);
3898 result = (mono_u)GET_NATI (sp [0]) > (mono_u)GET_NATI (sp [1]);
3900 sp->data.i = result;
3910 if (sp->type == VAL_I32)
3911 result = sp [0].data.i < (gint)GET_NATI (sp [1]);
3912 else if (sp->type == VAL_I64)
3913 result = sp [0].data.l < sp [1].data.l;
3914 else if (sp->type == VAL_DOUBLE) {
3915 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3918 result = sp [0].data.f < sp [1].data.f;
3920 result = (gint)GET_NATI (sp [0]) < (gint)GET_NATI (sp [1]);
3922 sp->data.i = result;
3932 if (sp->type == VAL_I32)
3933 result = (guint32)sp [0].data.i < (mono_u)GET_NATI (sp [1]);
3934 else if (sp->type == VAL_I64)
3935 result = (guint64)sp [0].data.l < (guint64)sp [1].data.l;
3936 else if (sp->type == VAL_DOUBLE)
3937 result = isnan (sp [0].data.f) || isnan (sp [1].data.f);
3939 result = (mono_u)GET_NATI (sp [0]) < (mono_u)GET_NATI (sp [1]);
3941 sp->data.i = result;
3947 case CEE_LDVIRTFTN: {
3948 int virtual = *ip == CEE_LDVIRTFTN;
3952 token = read32 (ip);
3955 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3956 m = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
3958 m = mono_get_method (image, token, NULL);
3961 THROW_EX (mono_get_exception_missing_method (), ip - 5);
3965 THROW_EX (mono_get_exception_null_reference (), ip - 5);
3967 m = get_virtual_method (domain, m, sp);
3969 sp->type = VAL_NATI;
3970 sp->data.p = mono_create_method_pointer (m);
3971 sp->data.vt.klass = NULL;
3975 case CEE_UNUSED56: ves_abort(); break;
3979 arg_pos = read16 (ip);
3981 vt_alloc (ARG_TYPE (signature, arg_pos), sp, signature->pinvoke);
3982 stackval_from_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
3994 t = ARG_TYPE (signature, anum);
3995 c = mono_class_from_mono_type (t);
3996 sp->data.vt.klass = c;
3997 sp->data.vt.vt = ARG_POS (anum);
4000 sp->type = VAL_VALUETA;
4010 arg_pos = read16 (ip);
4013 stackval_to_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4020 loc_pos = read16 (ip);
4022 vt_alloc (LOCAL_TYPE (header, loc_pos), sp, FALSE);
4023 stackval_from_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4033 loc_pos = read16 (ip);
4035 t = LOCAL_TYPE (header, loc_pos);
4036 c = mono_class_from_mono_type (t);
4037 sp->data.vt.vt = LOCAL_POS (loc_pos);
4038 sp->data.vt.klass = c;
4041 sp->type = VAL_VALUETA;
4051 loc_pos = read16 (ip);
4054 stackval_to_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4060 if (sp != frame->stack)
4061 THROW_EX (mono_get_exception_execution_engine (NULL), ip - 1);
4063 sp->data.p = alloca (sp->data.i);
4067 case CEE_UNUSED57: ves_abort(); break;
4068 case CEE_ENDFILTER: ves_abort(); break;
4069 case CEE_UNALIGNED_:
4071 unaligned_address = 1;
4075 volatile_address = 1;
4084 token = read32 (ip);
4087 * we ignore the value of token (I think we can as unspecified
4088 * behavior described in Partition II, 3.5).
4091 g_assert (sp->type == VAL_VALUETA || sp->type == VAL_TP);
4092 memset (sp->data.vt.vt, 0, mono_class_value_size (sp->data.vt.klass, NULL));
4095 case CEE_UNUSED68: ves_abort(); break;
4098 if (!sp [0].data.p || !sp [1].data.p)
4099 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4101 /* FIXME: value and size may be int64... */
4102 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4107 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4109 /* FIXME: value and size may be int64... */
4110 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4112 case CEE_UNUSED69: ves_abort(); break;
4115 * need to clarify what this should actually do:
4116 * start the search from the last found handler in
4117 * this method or continue in the caller or what.
4118 * Also, do we need to run finally/fault handlers after a retrow?
4119 * Well, this implementation will follow the usual search
4120 * for an handler, considering the current ip as throw spot.
4121 * We need to NULL frame->ex_handler for the later code to
4122 * actually run the new found handler.
4124 frame->ex_handler = NULL;
4125 THROW_EX (frame->ex, ip - 1);
4127 case CEE_UNUSED: ves_abort(); break;
4132 token = read32 (ip);
4134 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4135 MonoType *type = mono_type_create_from_typespec (image, token);
4136 sp->data.i = mono_type_size (type, &align);
4137 mono_metadata_free_type (type);
4139 MonoClass *szclass = mono_class_get (image, token);
4140 mono_class_init (szclass);
4141 if (!szclass->valuetype)
4142 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
4143 sp->data.i = mono_class_value_size (szclass, &align);
4149 case CEE_REFANYTYPE: ves_abort(); break;
4156 g_error ("Unimplemented opcode: 0xFE %02x at 0x%x\n", *ip, ip-header->code);
4163 g_assert_not_reached ();
4165 * Exception handling code.
4166 * The exception object is stored in frame->ex.
4173 MonoInvocation *inv;
4174 MonoMethodHeader *hd;
4175 MonoExceptionClause *clause;
4181 g_print ("* Handling exception '%s' at IL_%04x\n", mono_object_class (frame->ex)->name, frame->ip - header->code);
4183 if (die_on_exception)
4186 for (inv = frame; inv; inv = inv->parent) {
4187 if (inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4189 if (inv->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4191 hd = ((MonoMethodNormal*)inv->method)->header;
4192 ip_offset = inv->ip - hd->code;
4193 for (i = 0; i < hd->num_clauses; ++i) {
4194 clause = &hd->clauses [i];
4195 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4196 if (!clause->flags) {
4197 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (inv->method->klass->image, clause->token_or_filter))) {
4199 * OK, we found an handler, now we need to execute the finally
4200 * and fault blocks before branching to the handler code.
4202 inv->ex_handler = clause;
4205 g_print ("* Found handler at '%s'\n", inv->method->name);
4208 * It seems that if the catch handler is found in the same method,
4209 * it gets executed before the finally handler.
4214 goto handle_finally;
4217 /* FIXME: handle filter clauses */
4224 * If we get here, no handler was found: print a stack trace.
4226 for (inv = frame; inv; inv = inv->parent) {
4227 if (inv->invoke_trap)
4228 goto handle_finally;
4231 ex_obj = (MonoObject*)frame->ex;
4232 mono_unhandled_exception (ex_obj);
4239 MonoExceptionClause *clause;
4243 g_print ("* Handle finally\n");
4245 if ((frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4246 || (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4250 ip_offset = frame->ip - header->code;
4252 for (i = 0; i < header->num_clauses; ++i) {
4253 clause = &header->clauses [i];
4254 if (clause == frame->ex_handler)
4256 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - header->code))) {
4257 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4258 ip = header->code + clause->handler_offset;
4259 finally_ips = g_slist_append (finally_ips, (gpointer) ip);
4262 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4268 ip = finally_ips->data;
4269 finally_ips = g_slist_remove (finally_ips, ip);
4274 * If an exception is set, we need to execute the fault handler, too,
4275 * otherwise, we continue normally.
4286 MonoExceptionClause *clause;
4290 g_print ("* Handle fault\n");
4292 ip_offset = frame->ip - header->code;
4293 for (i = 0; i < header->num_clauses; ++i) {
4294 clause = &header->clauses [i];
4295 if (clause->flags == 3 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4296 ip = header->code + clause->handler_offset;
4299 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4305 * If the handler for the exception was found in this method, we jump
4306 * to it right away, otherwise we return and let the caller run
4307 * the finally, fault and catch blocks.
4308 * This same code should be present in the endfault opcode, but it
4309 * is corrently not assigned in the ECMA specs: LAMESPEC.
4311 if (frame->ex_handler) {
4314 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4316 ip = header->code + frame->ex_handler->handler_offset;
4319 sp->data.p = frame->ex;
4330 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4332 MonoImage *image = assembly->image;
4333 MonoCLIImageInfo *iinfo;
4335 MonoObject *exc = NULL;
4338 iinfo = image->image_info;
4339 method = mono_get_method (image, iinfo->cli_cli_header.ch_entry_point, NULL);
4341 g_error ("No entry point method found in %s", image->name);
4343 rval = mono_runtime_run_main (method, argc, argv, &exc);
4352 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4353 "Usage is: mint [options] executable args...\n\n", VERSION);
4355 "Runtime Debugging:\n"
4360 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4363 " --traceclassinit\n"
4366 " --debug method_name\n"
4372 " --config filename load the specified config file instead of the default\n"
4373 " --workers n maximum number of worker threads\n"
4380 test_load_class (MonoImage* image)
4382 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
4386 for (i = 1; i <= t->rows; ++i) {
4387 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4388 mono_class_init (klass);
4393 static MonoException * segv_exception = NULL;
4396 segv_handler (int signum)
4398 signal (signum, segv_handler);
4399 mono_raise_exception (segv_exception);
4403 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4404 MonoReflectionMethod **method,
4405 gint32 *iloffset, gint32 *native_offset,
4406 MonoString **file, gint32 *line, gint32 *column)
4419 *file = mono_string_new (mono_domain_get (), "unknown");
4425 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4433 int enable_debugging;
4439 static void main_thread_handler (gpointer user_data)
4441 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4442 MonoAssembly *assembly;
4445 if (main_args->enable_debugging)
4446 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4448 assembly = mono_domain_assembly_open (main_args->domain,
4452 fprintf (stderr, "Can not open image %s\n", main_args->file);
4456 if (main_args->enable_debugging)
4457 mono_debug_init_2 (assembly);
4460 test_load_class (assembly->image);
4462 error = mono_verify_corlib ();
4464 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
4467 segv_exception = mono_get_exception_null_reference ();
4468 segv_exception->message = mono_string_new (main_args->domain, "Segmentation fault");
4469 signal (SIGSEGV, segv_handler);
4471 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4476 mono_runtime_install_handlers (void)
4478 /* FIXME: anything to do here? */
4482 main (int argc, char *argv [])
4485 int retval = 0, i, ocount = 0;
4486 char *file, *config_file = NULL;
4487 int enable_debugging = FALSE;
4488 MainThreadArgs main_args;
4493 for (i = 1; i < argc && argv [i][0] == '-'; i++){
4494 if (strcmp (argv [i], "--trace") == 0)
4496 if (strcmp (argv [i], "--noptr") == 0)
4497 global_no_pointers = 1;
4498 if (strcmp (argv [i], "--traceops") == 0)
4500 if (strcmp (argv [i], "--dieonex") == 0) {
4501 die_on_exception = 1;
4502 enable_debugging = 1;
4504 if (strcmp (argv [i], "--print-vtable") == 0)
4505 mono_print_vtable = TRUE;
4506 if (strcmp (argv [i], "--profile") == 0)
4507 mono_profiler_install_simple ();
4508 if (strcmp (argv [i], "--opcode-count") == 0)
4510 if (strcmp (argv [i], "--config") == 0)
4511 config_file = argv [++i];
4512 if (strcmp (argv [i], "--workers") == 0) {
4513 mono_worker_threads = atoi (argv [++i]);
4514 if (mono_worker_threads < 1)
4515 mono_worker_threads = 1;
4517 if (strcmp (argv [i], "--help") == 0)
4520 if (strcmp (argv [i], "--debug") == 0) {
4521 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
4523 g_error ("Invalid method name '%s'", argv [i]);
4524 db_methods = g_list_append (db_methods, desc);
4534 g_set_prgname (file);
4535 mono_set_rootdir ();
4536 mono_config_parse (config_file);
4538 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4539 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4542 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4543 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4544 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4546 frame_thread_id = TlsAlloc ();
4547 TlsSetValue (frame_thread_id, NULL);
4549 mono_install_compile_method (mono_create_method_pointer);
4550 mono_install_runtime_invoke (interp_mono_runtime_invoke);
4551 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4553 mono_install_handler (interp_ex_handler);
4554 mono_install_stack_walk (interp_walk_stack);
4556 InitializeCriticalSection (&metadata_lock);
4557 domain = mono_init (file);
4558 mono_runtime_init (domain, NULL, NULL);
4560 main_args.domain=domain;
4561 main_args.file=file;
4562 main_args.argc=argc-i;
4563 main_args.argv=argv+i;
4564 main_args.enable_debugging=enable_debugging;
4566 mono_runtime_exec_managed_code (domain, main_thread_handler,
4569 mono_profiler_shutdown ();
4571 mono_runtime_cleanup (domain);
4572 mono_domain_unload (domain, TRUE);
4574 /* Get the return value from System.Environment.ExitCode */
4575 retval=mono_environment_exitcode_get ();
4579 fprintf (stderr, "opcode count: %ld\n", opcode_count);
4580 fprintf (stderr, "fcall count: %ld\n", fcall_count);