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 /* FIXME: This might cause a deadlock with domain->lock */
890 EnterCriticalSection (&metadata_lock);
891 /* intern the strings in the method. */
893 end = ip + header->code_size;
900 opcode = &mono_opcodes [i];
901 switch (opcode->argument) {
905 case MonoInlineString:
906 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
910 if (method->wrapper_type == MONO_WRAPPER_NONE) {
911 class = mono_class_get (image, read32 (ip + 1));
912 mono_class_init (class);
913 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE))
914 mono_class_vtable (domain, class);
918 case MonoInlineField:
919 token = read32 (ip + 1);
920 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
921 mono_field_from_memberref (image, token, &class);
923 class = mono_class_get (image,
924 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
926 mono_class_init (class);
927 mono_class_vtable (domain, class);
930 case MonoInlineMethod:
931 if (method->wrapper_type == MONO_WRAPPER_NONE) {
932 m = mono_get_method (image, read32 (ip + 1), NULL);
933 mono_class_init (m->klass);
934 if (!(m->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
935 mono_class_vtable (domain, m->klass);
941 case MonoShortInlineR:
943 case MonoInlineBrTarget:
949 case MonoShortInlineVar:
950 case MonoShortInlineI:
951 case MonoShortInlineBrTarget:
954 case MonoInlineSwitch: {
967 g_assert_not_reached ();
971 method->info = offsets;
974 * We store the inline info in addr, since it's unused for IL methods.
976 if (method->klass == mono_defaults.string_class) {
977 if (strcmp (method->name, "get_Length") == 0)
978 method->addr = GUINT_TO_POINTER (INLINE_STRING_LENGTH);
979 } else if (method->klass == mono_defaults.array_class) {
980 if (strcmp (method->name, "get_Length") == 0)
981 method->addr = GUINT_TO_POINTER (INLINE_ARRAY_LENGTH);
982 else if (strcmp (method->name, "get_Rank") == 0 || strcmp (method->name, "GetRank") == 0)
983 method->addr = GUINT_TO_POINTER (INLINE_ARRAY_RANK);
984 } else if (method->klass == mono_defaults.monotype_class) {
985 if (strcmp (method->name, "GetElementType") == 0)
986 method->addr = GUINT_TO_POINTER (INLINE_TYPE_ELEMENT_TYPE);
988 LeaveCriticalSection (&metadata_lock);
989 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
992 #define LOCAL_POS(n) (frame->locals + offsets [2 + (n)])
993 #define LOCAL_TYPE(header, n) ((header)->locals [(n)])
995 #define ARG_POS(n) (args_pointers [(n)])
996 #define ARG_TYPE(sig, n) ((n) ? (sig)->params [(n) - (sig)->hasthis] : \
997 (sig)->hasthis ? &frame->method->klass->this_arg: (sig)->params [(0)])
999 #define THROW_EX(exception,ex_ip) \
1001 char *stack_trace; \
1002 frame->ip = (ex_ip); \
1003 stack_trace = dump_frame (frame); \
1004 frame->ex = (MonoException*)(exception); \
1005 frame->ex->stack_trace = mono_string_new (domain, stack_trace); \
1006 g_free (stack_trace); \
1007 goto handle_exception; \
1010 typedef struct _vtallocation vtallocation;
1012 struct _vtallocation {
1015 char data [MONO_ZERO_LEN_ARRAY];
1018 #define vt_alloc(vtype,sp,native) \
1019 if ((vtype)->type == MONO_TYPE_VALUETYPE && !(vtype)->data.klass->enumtype) { \
1020 if (!(vtype)->byref) { \
1023 vtallocation *tmp, *prev; \
1024 if (native) size = mono_class_native_size ((vtype)->data.klass, &align); \
1025 else size = mono_class_value_size ((vtype)->data.klass, &align); \
1028 while (tmp && (tmp->size < size)) { \
1033 tmp = alloca (sizeof (vtallocation) + size); \
1035 g_assert (size < 10000); \
1039 prev->next = tmp->next; \
1041 vtalloc = tmp->next; \
1042 (sp)->data.vt.vt = tmp->data; \
1044 (sp)->data.vt.klass = (vtype)->data.klass; \
1048 #define vt_free(sp) \
1050 if ((sp)->type == VAL_VALUET) { \
1051 vtallocation *tmp = (vtallocation*)(((char*)(sp)->data.vt.vt) - G_STRUCT_OFFSET (vtallocation, data)); \
1052 tmp->next = vtalloc; \
1059 verify_method (MonoMethod *m)
1061 GSList *errors, *tmp;
1062 MonoVerifyInfo *info;
1064 errors = mono_method_verify (m, MONO_VERIFY_ALL);
1066 g_print ("Method %s.%s::%s has invalid IL.\n", m->klass->name_space, m->klass->name, m->name);
1067 for (tmp = errors; tmp; tmp = tmp->next) {
1069 g_print ("%s\n", info->message);
1073 mono_free_verify_list (errors);
1077 #define MYGUINT64_MAX 18446744073709551615ULL
1078 #define MYGINT64_MAX 9223372036854775807LL
1079 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1081 #define MYGUINT32_MAX 4294967295U
1082 #define MYGINT32_MAX 2147483647
1083 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1085 #define CHECK_ADD_OVERFLOW(a,b) \
1086 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1087 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1089 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1090 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1092 #define CHECK_ADD_OVERFLOW64(a,b) \
1093 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1094 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1096 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1097 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1099 /* Resolves to TRUE if the operands would overflow */
1100 #define CHECK_MUL_OVERFLOW(a,b) \
1101 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1102 (((gint32)(a) > 0) && ((gint32)(b) > 0)) || (((gint32)(a) < 0) && ((gint32)(b) < 0)) ? \
1103 (gint32)(b) > ((MYGINT32_MAX) / (gint32)(a)) : \
1104 (gint32)(b) < ((MYGINT32_MIN) / (gint32)(a))
1106 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1107 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1108 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1110 #define CHECK_MUL_OVERFLOW64(a,b) \
1111 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1112 (((gint64)(a) > 0) && ((gint64)(b) > 0)) || (((gint64)(a) < 0) && ((gint64)(b) < 0)) ? \
1113 (gint64)(b) > ((MYGINT64_MAX) / (gint64)(a)) : \
1114 (gint64)(b) < ((MYGINT64_MIN) / (gint64)(a))
1116 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1117 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1118 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1121 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1123 MonoInvocation frame, *parent;
1124 MonoObject *retval = NULL;
1125 MonoMethodSignature *sig = method->signature;
1126 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1127 int i, type, isobject = 0;
1130 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1132 /* FIXME: Set frame for execption handling. */
1134 switch (sig->ret->type) {
1135 case MONO_TYPE_VOID:
1137 case MONO_TYPE_STRING:
1138 case MONO_TYPE_OBJECT:
1139 case MONO_TYPE_CLASS:
1140 case MONO_TYPE_ARRAY:
1141 case MONO_TYPE_SZARRAY:
1144 case MONO_TYPE_VALUETYPE:
1145 retval = mono_object_new (mono_domain_get (), klass);
1146 ret = ((char*)retval) + sizeof (MonoObject);
1147 if (!sig->ret->data.klass->enumtype)
1148 result.data.vt.vt = ret;
1151 retval = mono_object_new (mono_domain_get (), klass);
1152 ret = ((char*)retval) + sizeof (MonoObject);
1156 for (i = 0; i < sig->param_count; ++i) {
1157 if (sig->params [i]->byref) {
1158 args [i].type = VAL_POINTER;
1159 args [i].data.p = params [i];
1162 type = sig->params [i]->type;
1167 case MONO_TYPE_BOOLEAN:
1168 args [i].type = VAL_I32;
1169 args [i].data.i = *(MonoBoolean*)params [i];
1170 args [i].data.vt.klass = NULL;
1174 case MONO_TYPE_CHAR:
1175 args [i].type = VAL_I32;
1176 args [i].data.i = *(gint16*)params [i];
1177 args [i].data.vt.klass = NULL;
1179 #if SIZEOF_VOID_P == 4
1180 case MONO_TYPE_U: /* use VAL_POINTER? */
1185 args [i].type = VAL_I32;
1186 args [i].data.i = *(gint32*)params [i];
1187 args [i].data.vt.klass = NULL;
1189 #if SIZEOF_VOID_P == 8
1195 args [i].type = VAL_I64;
1196 args [i].data.l = *(gint64*)params [i];
1197 args [i].data.vt.klass = NULL;
1199 case MONO_TYPE_VALUETYPE:
1200 if (sig->params [i]->data.klass->enumtype) {
1201 type = sig->params [i]->data.klass->enum_basetype->type;
1204 args [i].type = VAL_POINTER;
1205 args [i].data.p = params [i];
1206 args [i].data.vt.klass = NULL;
1209 case MONO_TYPE_STRING:
1210 case MONO_TYPE_CLASS:
1211 case MONO_TYPE_ARRAY:
1212 case MONO_TYPE_SZARRAY:
1213 case MONO_TYPE_OBJECT:
1214 args [i].type = VAL_OBJ;
1215 args [i].data.p = params [i];
1216 args [i].data.vt.klass = NULL;
1219 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1223 if (method->klass->valuetype)
1224 /* Unbox the instance, since valuetype methods expect an interior pointer. */
1225 obj = mono_object_unbox (obj);
1227 /* chain with managed parent if any */
1228 parent = TlsGetValue (frame_thread_id);
1229 INIT_FRAME(&frame,parent,obj,args,&result,method);
1231 frame.invoke_trap = 1;
1232 ves_exec_method (&frame);
1233 if (exc && frame.ex) {
1234 *exc = (MonoObject*) frame.ex;
1237 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1239 if (isobject || method->string_ctor)
1240 return result.data.p;
1241 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1246 * Need to optimize ALU ops when natural int == int32
1248 * IDEA: if we maintain a stack of ip, sp to be checked
1249 * in the return opcode, we could inline simple methods that don't
1250 * use the stack or local variables....
1252 * The {,.S} versions of many opcodes can/should be merged to reduce code
1257 ves_exec_method (MonoInvocation *frame)
1259 MonoDomain *domain = mono_domain_get ();
1260 MonoInvocation child_frame;
1261 MonoMethodHeader *header;
1262 MonoMethodSignature *signature;
1264 GSList *finally_ips = NULL;
1265 const unsigned char *endfinally_ip;
1266 register const unsigned char *ip;
1267 register stackval *sp = NULL;
1268 void **args_pointers;
1270 gint il_ins_count = -1;
1271 gint tracing = global_tracing;
1272 unsigned char tail_recursion = 0;
1273 unsigned char unaligned_address = 0;
1274 unsigned char volatile_address = 0;
1275 vtallocation *vtalloc = NULL;
1276 MonoVTable *method_class_vt;
1279 if (frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1280 frame->method = mono_marshal_get_native_wrapper (frame->method);
1282 method_class_vt = mono_class_vtable (domain, frame->method->klass);
1283 if (!method_class_vt->initialized)
1284 mono_runtime_class_init (method_class_vt);
1285 signature = frame->method->signature;
1289 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
1290 if (!frame->method->addr) {
1291 /* ugly, but needed by the iflags setting in loader.c */
1292 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1293 ves_runtime_method (frame);
1295 goto handle_exception;
1299 if (frame->method->addr) {
1300 frame->ex = (MonoException*)mono_get_exception_missing_method ();
1301 goto handle_exception;
1304 ves_pinvoke_method (frame, frame->method->signature, frame->method->addr,
1305 frame->method->string_ctor);
1307 goto handle_exception;
1312 if (frame->method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
1313 ves_runtime_method (frame);
1315 goto handle_exception;
1320 /*verify_method (frame->method);*/
1322 header = ((MonoMethodNormal *)frame->method)->header;
1323 image = frame->method->klass->image;
1325 if (!frame->method->info)
1326 calc_offsets (image, frame->method);
1327 offsets = frame->method->info;
1330 * with alloca we get the expected huge performance gain
1331 * stackval *stack = g_new0(stackval, header->max_stack);
1333 g_assert (header->max_stack < 10000);
1334 sp = frame->stack = alloca (sizeof (stackval) * header->max_stack);
1336 if (header->num_locals) {
1337 g_assert (offsets [0] < 10000);
1338 frame->locals = alloca (offsets [0]);
1340 * yes, we do it unconditionally, because it needs to be done for
1341 * some cases anyway and checking for that would be even slower.
1343 memset (frame->locals, 0, offsets [0]);
1346 * Copy args from stack_args to args.
1348 if (signature->param_count || signature->hasthis) {
1350 int has_this = signature->hasthis;
1352 g_assert (offsets [1] < 10000);
1353 frame->args = alloca (offsets [1]);
1354 g_assert ((signature->param_count + has_this) < 1000);
1355 args_pointers = alloca (sizeof(void*) * (signature->param_count + has_this));
1358 this_arg = args_pointers [0] = frame->args;
1359 *this_arg = frame->obj;
1361 for (i = 0; i < signature->param_count; ++i) {
1362 args_pointers [i + has_this] = frame->args + offsets [2 + header->num_locals + has_this + i];
1363 stackval_to_data (signature->params [i], frame->stack_args + i, args_pointers [i + has_this], signature->pinvoke);
1367 child_frame.parent = frame;
1368 frame->child = &child_frame;
1375 * using while (ip < end) may result in a 15% performance drop,
1376 * but it may be useful for debug
1380 /*g_assert (sp >= stack);*/
1384 char *ins, *discode;
1385 if (sp > frame->stack) {
1386 ins = dump_stack (frame->stack, sp);
1388 ins = g_strdup ("");
1391 discode = mono_disasm_code_one (NULL, frame->method, ip, NULL);
1392 discode [strlen (discode) - 1] = 0; /* no \n */
1393 g_print ("(%d) %-29s %s\n", GetCurrentThreadId(), discode, ins);
1397 if (il_ins_count > 0)
1398 if (!(--il_ins_count))
1408 G_BREAKPOINT (); /* this is not portable... */
1413 CASE (CEE_LDARG_3) {
1414 int n = (*ip)-CEE_LDARG_0;
1416 vt_alloc (ARG_TYPE (signature, n), sp, signature->pinvoke);
1417 stackval_from_data (ARG_TYPE (signature, n), sp, ARG_POS (n), signature->pinvoke);
1424 CASE (CEE_LDLOC_3) {
1425 int n = (*ip)-CEE_LDLOC_0;
1427 if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1429 sp->data.i = *(gint32*) LOCAL_POS (n);
1433 vt_alloc (LOCAL_TYPE (header, n), sp, FALSE);
1434 stackval_from_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1442 CASE (CEE_STLOC_3) {
1443 int n = (*ip)-CEE_STLOC_0;
1446 if ((LOCAL_TYPE (header, n))->type == MONO_TYPE_I4) {
1447 gint32 *p = (gint32*)LOCAL_POS (n);
1451 stackval_to_data (LOCAL_TYPE (header, n), sp, LOCAL_POS (n), FALSE);
1458 vt_alloc (ARG_TYPE (signature, *ip), sp, signature->pinvoke);
1459 stackval_from_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1463 CASE (CEE_LDARGA_S) {
1468 t = ARG_TYPE (signature, *ip);
1469 c = mono_class_from_mono_type (t);
1470 sp->data.vt.klass = c;
1471 sp->data.vt.vt = ARG_POS (*ip);
1474 sp->type = VAL_VALUETA;
1485 stackval_to_data (ARG_TYPE (signature, *ip), sp, ARG_POS (*ip), signature->pinvoke);
1491 vt_alloc (LOCAL_TYPE (header, *ip), sp, FALSE);
1492 stackval_from_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1496 CASE (CEE_LDLOCA_S) {
1501 t = LOCAL_TYPE (header, *ip);
1502 c = mono_class_from_mono_type (t);
1503 sp->data.vt.klass = c;
1504 sp->data.p = LOCAL_POS (*ip);
1507 sp->type = VAL_VALUETA;
1518 stackval_to_data (LOCAL_TYPE (header, *ip), sp, LOCAL_POS (*ip), FALSE);
1526 sp->data.vt.klass = NULL;
1529 CASE (CEE_LDC_I4_M1)
1545 sp->data.i = (*ip) - CEE_LDC_I4_0;
1552 sp->data.i = *(const gint8 *)ip;
1559 sp->data.i = read32 (ip);
1566 sp->data.l = read64 (ip);
1573 sp->type = VAL_DOUBLE;
1582 sp->type = VAL_DOUBLE;
1583 readr8(ip, &sp->data.f);
1587 CASE (CEE_UNUSED99) ves_abort (); BREAK;
1589 if (sp [-1].type == VAL_VALUET) {
1590 MonoClass *c = sp [-1].data.vt.klass;
1591 vt_alloc (&c->byval_arg, sp, FALSE);
1592 stackval_from_data (&c->byval_arg, sp, sp [-1].data.vt.vt, FALSE);
1604 CASE (CEE_JMP) ves_abort(); BREAK;
1605 CASE (CEE_CALLVIRT) /* Fall through */
1606 CASE (CEE_CALLI) /* Fall through */
1608 MonoMethodSignature *csignature;
1610 stackval *endsp = sp;
1612 int virtual = *ip == CEE_CALLVIRT;
1613 int calli = *ip == CEE_CALLI;
1614 unsigned char *code = NULL;
1617 * We ignore tail recursion for now.
1624 token = read32 (ip);
1630 if (frame->method->wrapper_type != MONO_WRAPPER_NONE) {
1631 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (frame->method, token);
1632 child_frame.method = NULL;
1633 } else if ((ji = mono_jit_info_table_find (mono_root_domain, code))) {
1634 child_frame.method = ji->method;
1636 g_assert_not_reached ();
1640 child_frame.method = mono_get_method (image, token, NULL);
1641 if (!child_frame.method)
1642 THROW_EX (mono_get_exception_missing_method (), ip -5);
1643 csignature = child_frame.method->signature;
1645 stackval *this_arg = &sp [-csignature->param_count-1];
1646 if (!this_arg->data.p)
1647 THROW_EX (mono_get_exception_null_reference(), ip - 5);
1648 child_frame.method = get_virtual_method (domain, child_frame.method, this_arg);
1649 if (!child_frame.method)
1650 THROW_EX (mono_get_exception_missing_method (), ip -5);
1654 g_assert (csignature->call_convention == MONO_CALL_DEFAULT);
1655 /* decrement by the actual number of args */
1656 if (csignature->param_count) {
1657 sp -= csignature->param_count;
1658 child_frame.stack_args = sp;
1660 child_frame.stack_args = NULL;
1662 if (csignature->hasthis) {
1663 g_assert (sp >= frame->stack);
1666 * It may also be a TP from LD(S)FLDA
1667 * g_assert (sp->type == VAL_OBJ || sp->type == VAL_VALUETA);
1669 if (sp->type == VAL_OBJ && child_frame.method &&
1670 child_frame.method->klass->valuetype) /* unbox it */
1671 child_frame.obj = (char*)sp->data.p + sizeof (MonoObject);
1673 child_frame.obj = sp->data.p;
1675 child_frame.obj = NULL;
1677 if (csignature->ret->type != MONO_TYPE_VOID) {
1678 vt_alloc (csignature->ret, &retval, csignature->pinvoke);
1679 child_frame.retval = &retval;
1681 child_frame.retval = NULL;
1684 child_frame.ex = NULL;
1685 child_frame.ex_handler = NULL;
1687 if (!child_frame.method) {
1689 ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE);
1690 } else if (csignature->hasthis && sp->type == VAL_OBJ &&
1691 ((MonoObject *)sp->data.p)->vtable->klass == mono_defaults.transparent_proxy_class) {
1692 g_assert (child_frame.method);
1693 child_frame.method = mono_marshal_get_remoting_invoke (child_frame.method);
1694 ves_exec_method (&child_frame);
1696 switch (GPOINTER_TO_UINT (child_frame.method->addr)) {
1697 case INLINE_STRING_LENGTH:
1698 retval.type = VAL_I32;
1699 retval.data.i = ((MonoString*)sp->data.p)->length;
1700 /*g_print ("length of '%s' is %d\n", mono_string_to_utf8 (sp->data.p), retval.data.i);*/
1702 case INLINE_ARRAY_LENGTH:
1703 retval.type = VAL_I32;
1704 retval.data.i = mono_array_length ((MonoArray*)sp->data.p);
1706 case INLINE_ARRAY_RANK:
1707 retval.type = VAL_I32;
1708 retval.data.i = mono_object_class (sp->data.p)->rank;
1710 case INLINE_TYPE_ELEMENT_TYPE:
1711 retval.type = VAL_OBJ;
1713 MonoClass *c = mono_class_from_mono_type (((MonoReflectionType*)sp->data.p)->type);
1714 retval.data.vt.klass = NULL;
1715 if (c->enumtype && c->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1716 retval.data.p = mono_type_get_object (domain, c->enum_basetype);
1717 else if (c->element_class)
1718 retval.data.p = mono_type_get_object (domain, &c->element_class->byval_arg);
1720 retval.data.p = NULL;
1724 ves_exec_method (&child_frame);
1728 while (endsp > sp) {
1733 if (child_frame.ex) {
1735 * An exception occurred, need to run finally, fault and catch handlers..
1737 frame->ex = child_frame.ex;
1738 goto handle_finally;
1741 /* need to handle typedbyref ... */
1742 if (csignature->ret->type != MONO_TYPE_VOID) {
1749 if (signature->ret->type != MONO_TYPE_VOID) {
1751 if (sp->type == VAL_VALUET) {
1752 /* the caller has already allocated the memory */
1753 stackval_from_data (signature->ret, frame->retval, sp->data.vt.vt, signature->pinvoke);
1756 *frame->retval = *sp;
1759 if (sp > frame->stack)
1760 g_warning ("more values on stack: %d", sp-frame->stack);
1764 CASE (CEE_BR_S) /* Fall through */
1766 if (*ip == CEE_BR) {
1768 ip += (gint32) read32(ip);
1772 ip += (signed char) *ip;
1776 CASE (CEE_BRFALSE) /* Fall through */
1777 CASE (CEE_BRFALSE_S) {
1780 if (*ip == CEE_BRFALSE_S) {
1781 broffset = (signed char)ip [1];
1784 broffset = (gint32) read32 (ip + 1);
1789 case VAL_I32: result = sp->data.i == 0; break;
1790 case VAL_I64: result = sp->data.l == 0; break;
1791 case VAL_DOUBLE: result = sp->data.f ? 0: 1; break;
1792 default: result = sp->data.p == NULL; break;
1798 CASE (CEE_BRTRUE) /* Fall through */
1799 CASE (CEE_BRTRUE_S) {
1802 if (*ip == CEE_BRTRUE_S) {
1803 broffset = (signed char)ip [1];
1806 broffset = (gint32) read32 (ip + 1);
1811 case VAL_I32: result = sp->data.i != 0; break;
1812 case VAL_I64: result = sp->data.l != 0; break;
1813 case VAL_DOUBLE: result = sp->data.f ? 1 : 0; break;
1814 default: result = sp->data.p != NULL; break;
1820 CASE (CEE_BEQ) /* Fall through */
1824 if (*ip == CEE_BEQ_S) {
1825 broffset = (signed char)ip [1];
1828 broffset = (gint32) read32 (ip + 1);
1832 if (sp->type == VAL_I32)
1833 result = sp [0].data.i == (gint)GET_NATI (sp [1]);
1834 else if (sp->type == VAL_I64)
1835 result = sp [0].data.l == sp [1].data.l;
1836 else if (sp->type == VAL_DOUBLE)
1837 result = sp [0].data.f == sp [1].data.f;
1839 result = (gint)GET_NATI (sp [0]) == (gint)GET_NATI (sp [1]);
1844 CASE (CEE_BGE) /* Fall through */
1848 if (*ip == CEE_BGE_S) {
1849 broffset = (signed char)ip [1];
1852 broffset = (gint32) read32 (ip + 1);
1856 if (sp->type == VAL_I32)
1857 result = sp [0].data.i >= (gint)GET_NATI (sp [1]);
1858 else if (sp->type == VAL_I64)
1859 result = sp [0].data.l >= sp [1].data.l;
1860 else if (sp->type == VAL_DOUBLE)
1861 result = sp [0].data.f >= sp [1].data.f;
1863 result = (gint)GET_NATI (sp [0]) >= (gint)GET_NATI (sp [1]);
1868 CASE (CEE_BGT) /* Fall through */
1872 if (*ip == CEE_BGT_S) {
1873 broffset = (signed char)ip [1];
1876 broffset = (gint32) read32 (ip + 1);
1880 if (sp->type == VAL_I32)
1881 result = sp [0].data.i > (gint)GET_NATI (sp [1]);
1882 else if (sp->type == VAL_I64)
1883 result = sp [0].data.l > sp [1].data.l;
1884 else if (sp->type == VAL_DOUBLE)
1885 result = sp [0].data.f > sp [1].data.f;
1887 result = (gint)GET_NATI (sp [0]) > (gint)GET_NATI (sp [1]);
1892 CASE (CEE_BLT) /* Fall through */
1896 if (*ip == CEE_BLT_S) {
1897 broffset = (signed char)ip [1];
1900 broffset = (gint32) read32 (ip + 1);
1904 if (sp->type == VAL_I32)
1905 result = sp[0].data.i < (gint)GET_NATI(sp[1]);
1906 else if (sp->type == VAL_I64)
1907 result = sp[0].data.l < sp[1].data.l;
1908 else if (sp->type == VAL_DOUBLE)
1909 result = sp[0].data.f < sp[1].data.f;
1911 result = (gint)GET_NATI(sp[0]) < (gint)GET_NATI(sp[1]);
1916 CASE (CEE_BLE) /* fall through */
1920 if (*ip == CEE_BLE_S) {
1921 broffset = (signed char)ip [1];
1924 broffset = (gint32) read32 (ip + 1);
1929 if (sp->type == VAL_I32)
1930 result = sp [0].data.i <= (gint)GET_NATI (sp [1]);
1931 else if (sp->type == VAL_I64)
1932 result = sp [0].data.l <= sp [1].data.l;
1933 else if (sp->type == VAL_DOUBLE)
1934 result = sp [0].data.f <= sp [1].data.f;
1937 * FIXME: here and in other places GET_NATI on the left side
1938 * _will_ be wrong when we change the macro to work on 64 bits
1941 result = (gint)GET_NATI (sp [0]) <= (gint)GET_NATI (sp [1]);
1947 CASE (CEE_BNE_UN) /* Fall through */
1948 CASE (CEE_BNE_UN_S) {
1951 if (*ip == CEE_BNE_UN_S) {
1952 broffset = (signed char)ip [1];
1955 broffset = (gint32) read32 (ip + 1);
1959 if (sp->type == VAL_I32)
1960 result = (guint32)sp [0].data.i != (guint32)GET_NATI (sp [1]);
1961 else if (sp->type == VAL_I64)
1962 result = (guint64)sp [0].data.l != (guint64)sp [1].data.l;
1963 else if (sp->type == VAL_DOUBLE)
1964 result = isunordered (sp [0].data.f, sp [1].data.f) ||
1965 (sp [0].data.f != sp [1].data.f);
1967 result = GET_NATI (sp [0]) != GET_NATI (sp [1]);
1972 CASE (CEE_BGE_UN) /* Fall through */
1973 CASE (CEE_BGE_UN_S) {
1976 if (*ip == CEE_BGE_UN_S) {
1977 broffset = (signed char)ip [1];
1980 broffset = (gint32) read32 (ip + 1);
1984 if (sp->type == VAL_I32)
1985 result = (guint32)sp [0].data.i >= (guint32)GET_NATI (sp [1]);
1986 else if (sp->type == VAL_I64)
1987 result = (guint64)sp [0].data.l >= (guint64)sp [1].data.l;
1988 else if (sp->type == VAL_DOUBLE)
1989 result = !isless (sp [0].data.f,sp [1].data.f);
1991 result = GET_NATI (sp [0]) >= GET_NATI (sp [1]);
1996 CASE (CEE_BGT_UN) /* Fall through */
1997 CASE (CEE_BGT_UN_S) {
2000 if (*ip == CEE_BGT_UN_S) {
2001 broffset = (signed char)ip [1];
2004 broffset = (gint32) read32 (ip + 1);
2008 if (sp->type == VAL_I32)
2009 result = (guint32)sp [0].data.i > (guint32)GET_NATI (sp [1]);
2010 else if (sp->type == VAL_I64)
2011 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
2012 else if (sp->type == VAL_DOUBLE)
2013 result = isgreater (sp [0].data.f, sp [1].data.f);
2015 result = GET_NATI (sp [0]) > GET_NATI (sp [1]);
2020 CASE (CEE_BLE_UN) /* Fall through */
2021 CASE (CEE_BLE_UN_S) {
2024 if (*ip == CEE_BLE_UN_S) {
2025 broffset = (signed char)ip [1];
2028 broffset = (gint32) read32 (ip + 1);
2032 if (sp->type == VAL_I32)
2033 result = (guint32)sp [0].data.i <= (guint32)GET_NATI (sp [1]);
2034 else if (sp->type == VAL_I64)
2035 result = (guint64)sp [0].data.l <= (guint64)sp [1].data.l;
2036 else if (sp->type == VAL_DOUBLE)
2037 result = islessequal (sp [0].data.f, sp [1].data.f);
2039 result = GET_NATI (sp [0]) <= GET_NATI (sp [1]);
2044 CASE (CEE_BLT_UN) /* Fall through */
2045 CASE (CEE_BLT_UN_S) {
2048 if (*ip == CEE_BLT_UN_S) {
2049 broffset = (signed char)ip [1];
2052 broffset = (gint32) read32 (ip + 1);
2056 if (sp->type == VAL_I32)
2057 result = (guint32)sp[0].data.i < (guint32)GET_NATI(sp[1]);
2058 else if (sp->type == VAL_I64)
2059 result = (guint64)sp[0].data.l < (guint64)sp[1].data.l;
2060 else if (sp->type == VAL_DOUBLE)
2061 result = isunordered (sp [0].data.f, sp [1].data.f) ||
2062 (sp [0].data.f < sp [1].data.f);
2064 result = GET_NATI(sp[0]) < GET_NATI(sp[1]);
2071 const unsigned char *st;
2075 st = ip + sizeof (gint32) * n;
2077 if ((guint32)sp->data.i < n) {
2079 ip += sizeof (gint32) * (guint32)sp->data.i;
2080 offset = read32 (ip);
2089 sp[-1].type = VAL_I32;
2090 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2094 sp[-1].type = VAL_I32;
2095 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2099 sp[-1].type = VAL_I32;
2100 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2104 sp[-1].type = VAL_I32;
2105 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2107 CASE (CEE_LDIND_I4) /* Fall through */
2110 sp[-1].type = VAL_I32;
2111 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2115 sp[-1].type = VAL_I64;
2116 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2120 sp[-1].type = VAL_NATI;
2121 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2125 sp[-1].type = VAL_DOUBLE;
2126 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2130 sp[-1].type = VAL_DOUBLE;
2131 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2133 CASE (CEE_LDIND_REF)
2135 sp[-1].type = VAL_OBJ;
2136 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2137 sp[-1].data.vt.klass = NULL;
2139 CASE (CEE_STIND_REF) {
2147 CASE (CEE_STIND_I1) {
2152 *p = (gint8)sp[1].data.i;
2155 CASE (CEE_STIND_I2) {
2160 *p = (gint16)sp[1].data.i;
2163 CASE (CEE_STIND_I4) {
2171 CASE (CEE_STIND_I) {
2176 *p = (mono_i)sp[1].data.p;
2179 CASE (CEE_STIND_I8) {
2187 CASE (CEE_STIND_R4) {
2192 *p = (gfloat)sp[1].data.f;
2195 CASE (CEE_STIND_R8) {
2206 /* should probably consider the pointers as unsigned */
2207 if (sp->type == VAL_I32)
2208 sp [-1].data.i += GET_NATI (sp [0]);
2209 else if (sp->type == VAL_I64)
2210 sp [-1].data.l += sp [0].data.l;
2211 else if (sp->type == VAL_DOUBLE)
2212 sp [-1].data.f += sp [0].data.f;
2214 char *p = sp [-1].data.p;
2215 p += GET_NATI (sp [0]);
2222 /* should probably consider the pointers as unsigned */
2223 if (sp->type == VAL_I32)
2224 sp [-1].data.i -= GET_NATI (sp [0]);
2225 else if (sp->type == VAL_I64)
2226 sp [-1].data.l -= sp [0].data.l;
2227 else if (sp->type == VAL_DOUBLE)
2228 sp [-1].data.f -= sp [0].data.f;
2230 char *p = sp [-1].data.p;
2231 p -= GET_NATI (sp [0]);
2238 if (sp->type == VAL_I32)
2239 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
2240 else if (sp->type == VAL_I64)
2241 sp [-1].data.l *= sp [0].data.l;
2242 else if (sp->type == VAL_DOUBLE)
2243 sp [-1].data.f *= sp [0].data.f;
2248 if (sp->type == VAL_I32) {
2249 if (GET_NATI (sp [0]) == 0)
2250 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2251 sp [-1].data.i /= (gint)GET_NATI (sp [0]);
2252 } else if (sp->type == VAL_I64) {
2253 if (sp [0].data.l == 0)
2254 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2255 sp [-1].data.l /= sp [0].data.l;
2256 } else if (sp->type == VAL_DOUBLE) {
2257 /* set NaN is divisor is 0.0 */
2258 sp [-1].data.f /= sp [0].data.f;
2264 if (sp->type == VAL_I32) {
2266 if (GET_NATI (sp [0]) == 0)
2267 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2268 val = sp [-1].data.i;
2269 val /= (guint32)GET_NATI (sp [0]);
2270 sp [-1].data.i = val;
2271 } else if (sp->type == VAL_I64) {
2273 if (sp [0].data.l == 0)
2274 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2275 val = sp [-1].data.l;
2276 val /= (guint64)sp [0].data.l;
2277 sp [-1].data.l = val;
2278 } else if (sp->type == VAL_NATI) {
2280 if (GET_NATI (sp [0]) == 0)
2281 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2282 val = (mono_u)sp [-1].data.p;
2283 val /= (mono_u)sp [0].data.p;
2284 sp [-1].data.p = (gpointer)val;
2290 if (sp->type == VAL_I32) {
2291 if (GET_NATI (sp [0]) == 0)
2292 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2293 sp [-1].data.i %= (gint)GET_NATI (sp [0]);
2294 } else if (sp->type == VAL_I64) {
2295 if (sp [0].data.l == 0)
2296 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2297 sp [-1].data.l %= sp [0].data.l;
2298 } else if (sp->type == VAL_DOUBLE) {
2299 /* FIXME: what do we actually do here? */
2300 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2302 if (GET_NATI (sp [0]) == 0)
2303 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2304 (gint)GET_NATI (sp [-1]) %= (gint)GET_NATI (sp [0]);
2310 if (sp->type == VAL_I32) {
2311 if (GET_NATI (sp [0]) == 0)
2312 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2313 (guint)sp [-1].data.i %= (guint)GET_NATI (sp [0]);
2314 } else if (sp->type == VAL_I64) {
2315 if (sp [0].data.l == 0)
2316 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2317 (guint64)sp [-1].data.l %= (guint64)sp [0].data.l;
2318 } else if (sp->type == VAL_DOUBLE) {
2319 /* unspecified behaviour according to the spec */
2321 if (GET_NATI (sp [0]) == 0)
2322 THROW_EX (mono_get_exception_divide_by_zero (), ip - 1);
2323 (guint64)GET_NATI (sp [-1]) %= (guint64)GET_NATI (sp [0]);
2329 if (sp->type == VAL_I32)
2330 sp [-1].data.i &= GET_NATI (sp [0]);
2331 else if (sp->type == VAL_I64)
2332 sp [-1].data.l &= sp [0].data.l;
2334 GET_NATI (sp [-1]) &= GET_NATI (sp [0]);
2339 if (sp->type == VAL_I32)
2340 sp [-1].data.i |= GET_NATI (sp [0]);
2341 else if (sp->type == VAL_I64)
2342 sp [-1].data.l |= sp [0].data.l;
2344 GET_NATI (sp [-1]) |= GET_NATI (sp [0]);
2349 if (sp->type == VAL_I32)
2350 sp [-1].data.i ^= GET_NATI (sp [0]);
2351 else if (sp->type == VAL_I64)
2352 sp [-1].data.l ^= sp [0].data.l;
2354 GET_NATI (sp [-1]) ^= GET_NATI (sp [0]);
2359 if (sp [-1].type == VAL_I32)
2360 sp [-1].data.i <<= GET_NATI (sp [0]);
2361 else if (sp [-1].type == VAL_I64)
2362 sp [-1].data.l <<= GET_NATI (sp [0]);
2364 GET_NATI (sp [-1]) <<= GET_NATI (sp [0]);
2369 if (sp [-1].type == VAL_I32)
2370 sp [-1].data.i >>= GET_NATI (sp [0]);
2371 else if (sp [-1].type == VAL_I64)
2372 sp [-1].data.l >>= GET_NATI (sp [0]);
2374 (gint)GET_NATI (sp [-1]) >>= GET_NATI (sp [0]);
2379 if (sp [-1].type == VAL_I32)
2380 (guint)sp [-1].data.i >>= GET_NATI (sp [0]);
2381 else if (sp [-1].type == VAL_I64)
2382 (guint64)sp [-1].data.l >>= GET_NATI (sp [0]);
2384 (guint64)GET_NATI (sp [-1]) >>= GET_NATI (sp [0]);
2389 if (sp->type == VAL_I32)
2390 sp->data.i = - sp->data.i;
2391 else if (sp->type == VAL_I64)
2392 sp->data.l = - sp->data.l;
2393 else if (sp->type == VAL_DOUBLE)
2394 sp->data.f = - sp->data.f;
2395 else if (sp->type == VAL_NATI)
2396 sp->data.p = (gpointer)(- (mono_i)sp->data.p);
2402 if (sp->type == VAL_I32)
2403 sp->data.i = ~ sp->data.i;
2404 else if (sp->type == VAL_I64)
2405 sp->data.l = ~ sp->data.l;
2406 else if (sp->type == VAL_NATI)
2407 sp->data.p = (gpointer)(~ (mono_i)sp->data.p);
2410 CASE (CEE_CONV_U1) /* fall through */
2411 CASE (CEE_CONV_I1) {
2413 switch (sp [-1].type) {
2415 sp [-1].data.i = (gint8)sp [-1].data.f;
2418 sp [-1].data.i = (gint8)sp [-1].data.l;
2423 sp [-1].data.i = (gint8)sp [-1].data.i;
2426 sp [-1].data.i = (gint8)sp [-1].data.nati;
2429 sp [-1].type = VAL_I32;
2432 CASE (CEE_CONV_U2) /* fall through */
2433 CASE (CEE_CONV_I2) {
2435 switch (sp [-1].type) {
2437 sp [-1].data.i = (gint16)sp [-1].data.f;
2440 sp [-1].data.i = (gint16)sp [-1].data.l;
2445 sp [-1].data.i = (gint16)sp [-1].data.i;
2448 sp [-1].data.i = (gint16)sp [-1].data.nati;
2451 sp [-1].type = VAL_I32;
2454 CASE (CEE_CONV_U4) /* Fall through */
2455 #if SIZEOF_VOID_P == 4
2456 CASE (CEE_CONV_I) /* Fall through */
2457 CASE (CEE_CONV_U) /* Fall through */
2459 CASE (CEE_CONV_I4) {
2461 switch (sp [-1].type) {
2463 sp [-1].data.i = (gint32)sp [-1].data.f;
2466 sp [-1].data.i = (gint32)sp [-1].data.l;
2473 sp [-1].data.i = (gint32)sp [-1].data.p;
2476 sp [-1].type = VAL_I32;
2479 #if SIZEOF_VOID_P == 8
2480 CASE (CEE_CONV_I) /* Fall through */
2484 switch (sp [-1].type) {
2486 sp [-1].data.l = (gint64)sp [-1].data.f;
2493 sp [-1].data.l = (gint64)sp [-1].data.i;
2496 sp [-1].data.l = (gint64)sp [-1].data.nati;
2499 sp [-1].type = VAL_I64;
2501 CASE (CEE_CONV_R4) {
2503 switch (sp [-1].type) {
2505 sp [-1].data.f = (float)sp [-1].data.f;
2508 sp [-1].data.f = (float)sp [-1].data.l;
2513 sp [-1].data.f = (float)sp [-1].data.i;
2516 sp [-1].data.f = (float)sp [-1].data.nati;
2519 sp [-1].type = VAL_DOUBLE;
2522 CASE (CEE_CONV_R8) {
2524 switch (sp [-1].type) {
2526 sp [-1].data.f = (double)sp [-1].data.f;
2529 sp [-1].data.f = (double)sp [-1].data.l;
2534 sp [-1].data.f = (double)sp [-1].data.i;
2537 sp [-1].data.f = (double)sp [-1].data.nati;
2540 sp [-1].type = VAL_DOUBLE;
2543 #if SIZEOF_VOID_P == 8
2544 CASE (CEE_CONV_U) /* Fall through */
2549 switch (sp [-1].type){
2551 sp [-1].data.l = (guint64)sp [-1].data.f;
2558 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2561 sp [-1].data.l = (guint64) sp [-1].data.nati;
2564 sp [-1].type = VAL_I64;
2569 vtklass = mono_class_get (image, read32 (ip));
2572 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2581 token = read32 (ip);
2584 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
2585 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
2587 c = mono_class_get (image, token);
2589 addr = sp [-1].data.vt.vt;
2590 vt_alloc (&c->byval_arg, &sp [-1], FALSE);
2591 stackval_from_data (&c->byval_arg, &sp [-1], addr, FALSE);
2599 str_index = mono_metadata_token_index (read32 (ip));
2602 o = (MonoObject*)mono_ldstr (domain, image, str_index);
2605 sp->data.vt.klass = NULL;
2612 MonoClass *newobj_class;
2613 MonoMethodSignature *csig;
2614 stackval valuetype_this;
2615 stackval *endsp = sp;
2622 token = read32 (ip);
2625 if (!(child_frame.method = mono_get_method (image, token, NULL)))
2626 THROW_EX (mono_get_exception_missing_method (), ip -5);
2628 csig = child_frame.method->signature;
2629 newobj_class = child_frame.method->klass;
2630 /*if (profiling_classes) {
2631 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2633 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2637 if (newobj_class->parent == mono_defaults.array_class) {
2638 sp -= csig->param_count;
2639 o = ves_array_create (domain, newobj_class, csig, sp);
2640 goto array_constructed;
2644 * First arg is the object.
2646 if (newobj_class->valuetype) {
2648 vt_alloc (&newobj_class->byval_arg, &valuetype_this, csig->pinvoke);
2649 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2650 zero = valuetype_this.data.vt.vt;
2651 child_frame.obj = valuetype_this.data.vt.vt;
2653 memset (&valuetype_this, 0, sizeof (stackval));
2654 zero = &valuetype_this;
2655 child_frame.obj = &valuetype_this;
2657 stackval_from_data (&newobj_class->byval_arg, &valuetype_this, zero, csig->pinvoke);
2659 if (newobj_class != mono_defaults.string_class) {
2660 o = mono_object_new (domain, newobj_class);
2661 child_frame.obj = o;
2663 child_frame.retval = &retval;
2667 if (csig->param_count) {
2668 sp -= csig->param_count;
2669 child_frame.stack_args = sp;
2671 child_frame.stack_args = NULL;
2674 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2676 child_frame.ex = NULL;
2677 child_frame.ex_handler = NULL;
2679 ves_exec_method (&child_frame);
2681 while (endsp > sp) {
2686 if (child_frame.ex) {
2688 * An exception occurred, need to run finally, fault and catch handlers..
2690 frame->ex = child_frame.ex;
2691 goto handle_finally;
2694 * a constructor returns void, but we need to return the object we created
2697 if (newobj_class->valuetype && !newobj_class->enumtype) {
2698 *sp = valuetype_this;
2699 } else if (newobj_class == mono_defaults.string_class) {
2704 sp->data.vt.klass = newobj_class;
2709 CASE (CEE_CASTCLASS) /* Fall through */
2713 MonoClass *c , *oclass;
2715 int do_isinst = *ip == CEE_ISINST;
2718 token = read32 (ip);
2719 c = mono_class_get (image, token);
2721 g_assert (sp [-1].type == VAL_OBJ);
2723 if ((o = sp [-1].data.p)) {
2727 if (mono_object_isinst (o, c)) {
2728 sp [-1].data.vt.klass = c;
2732 sp [-1].data.p = NULL;
2733 sp [-1].data.vt.klass = NULL;
2735 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
2741 CASE (CEE_CONV_R_UN)
2743 switch (sp [-1].type) {
2747 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2752 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2755 sp [-1].data.f = (double)(guint64)sp [-1].data.nati;
2758 sp [-1].type = VAL_DOUBLE;
2761 CASE (CEE_UNUSED1) ves_abort(); BREAK;
2768 token = read32 (ip);
2770 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
2771 c = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
2773 c = mono_class_get (image, token);
2777 THROW_EX (mono_get_exception_null_reference(), ip - 1);
2779 if (o->vtable->klass->element_class->type_token != c->element_class->type_token)
2780 THROW_EX (mono_get_exception_invalid_cast (), ip - 1);
2782 sp [-1].type = VAL_MP;
2783 sp [-1].data.p = (char *)o + sizeof (MonoObject);
2790 frame->ex_handler = NULL;
2792 sp->data.p = mono_get_exception_null_reference ();
2793 THROW_EX (sp->data.p, ip);
2795 CASE (CEE_LDFLDA) /* Fall through */
2798 MonoClassField *field;
2800 int load_addr = *ip == CEE_LDFLDA;
2803 if (!sp [-1].data.p)
2804 THROW_EX (mono_get_exception_null_reference (), ip);
2807 token = read32 (ip);
2810 if (sp [-1].type == VAL_OBJ) {
2811 obj = sp [-1].data.p;
2813 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) {
2814 MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
2815 field = mono_class_get_field (klass, token);
2816 addr = mono_load_remote_field (obj, klass, field, NULL);
2818 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
2819 field = mono_field_from_memberref (image, token, NULL);
2821 field = mono_class_get_field (obj->vtable->klass, token);
2822 addr = (char*)obj + field->offset;
2825 obj = sp [-1].data.vt.vt;
2826 field = mono_class_get_field (sp [-1].data.vt.klass, token);
2827 addr = (char*)obj + field->offset - sizeof (MonoObject);
2831 sp [-1].type = VAL_TP;
2832 sp [-1].data.p = addr;
2833 sp [-1].data.vt.klass = mono_class_from_mono_type (field->type);
2835 vt_alloc (field->type, &sp [-1], FALSE);
2836 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2843 MonoClassField *field;
2844 guint32 token, offset;
2849 THROW_EX (mono_get_exception_null_reference (), ip);
2852 token = read32 (ip);
2855 if (sp [0].type == VAL_OBJ) {
2856 obj = sp [0].data.p;
2858 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) {
2859 MonoClass *klass = ((MonoTransparentProxy*)obj)->klass;
2860 field = mono_class_get_field (klass, token);
2862 mono_store_remote_field (obj, klass, field, &sp [1].data);
2863 offset = field->offset;
2865 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
2866 field = mono_field_from_memberref (image, token, NULL);
2868 field = mono_class_get_field (obj->vtable->klass, token);
2869 offset = field->offset;
2870 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
2874 obj = sp [0].data.vt.vt;
2875 field = mono_class_get_field (sp [0].data.vt.klass, token);
2876 offset = field->offset - sizeof (MonoObject);
2877 stackval_to_data (field->type, &sp [1], (char*)obj + offset, FALSE);
2883 CASE (CEE_LDSFLD) /* Fall through */
2884 CASE (CEE_LDSFLDA) {
2887 MonoClassField *field;
2889 int load_addr = *ip == CEE_LDSFLDA;
2893 token = read32 (ip);
2896 /* need to handle fieldrefs */
2897 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
2898 field = mono_field_from_memberref (image, token, &klass);
2900 klass = mono_class_get (image,
2901 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
2902 field = mono_class_get_field (klass, token);
2906 vt = mono_class_vtable (domain, klass);
2907 if (!vt->initialized)
2908 mono_runtime_class_init (vt);
2909 if (!domain->thread_static_fields || !(addr = g_hash_table_lookup (domain->thread_static_fields, field)))
2910 addr = (char*)(vt->data) + field->offset;
2912 addr = mono_threads_get_static_data (GPOINTER_TO_UINT (addr));
2917 sp->data.vt.klass = mono_class_from_mono_type (field->type);
2919 vt_alloc (field->type, sp, FALSE);
2920 stackval_from_data (field->type, sp, addr, FALSE);
2928 MonoClassField *field;
2933 token = read32 (ip);
2937 /* need to handle fieldrefs */
2938 if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF) {
2939 field = mono_field_from_memberref (image, token, &klass);
2941 klass = mono_class_get (image,
2942 MONO_TOKEN_TYPE_DEF | mono_metadata_typedef_from_field (image, token & 0xffffff));
2943 field = mono_class_get_field (klass, token);
2947 vt = mono_class_vtable (domain, klass);
2948 if (!vt->initialized)
2949 mono_runtime_class_init (vt);
2950 if (!domain->thread_static_fields || !(addr = g_hash_table_lookup (domain->thread_static_fields, field)))
2951 addr = (char*)(vt->data) + field->offset;
2953 addr = mono_threads_get_static_data (GPOINTER_TO_UINT (addr));
2955 stackval_to_data (field->type, sp, addr, FALSE);
2962 vtklass = mono_class_get (image, read32 (ip));
2965 memcpy (sp [0].data.p, sp [1].data.vt.vt, mono_class_value_size (vtklass, NULL));
2968 #if SIZEOF_VOID_P == 8
2969 CASE (CEE_CONV_OVF_I_UN)
2971 CASE (CEE_CONV_OVF_I8_UN) {
2972 switch (sp [-1].type) {
2974 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
2975 THROW_EX (mono_get_exception_overflow (), ip);
2976 sp [-1].data.l = (guint64)sp [-1].data.f;
2983 /* Can't overflow */
2984 sp [-1].data.l = (guint64)sp [-1].data.i;
2987 sp [-1].data.l = (guint64)sp [-1].data.nati;
2990 sp [-1].type = VAL_I64;
2994 #if SIZEOF_VOID_P == 8
2995 CASE (CEE_CONV_OVF_U_UN)
2997 CASE (CEE_CONV_OVF_U8_UN) {
2998 switch (sp [-1].type) {
3000 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT64_MAX)
3001 THROW_EX (mono_get_exception_overflow (), ip);
3002 sp [-1].data.l = (guint64)sp [-1].data.f;
3010 /* Can't overflow */
3011 sp [-1].data.l = (guint64)sp [-1].data.i;
3014 /* Can't overflow */
3015 sp [-1].data.l = (guint64)sp [-1].data.nati;
3018 sp [-1].type = VAL_I64;
3022 #if SIZEOF_VOID_P == 4
3023 CASE (CEE_CONV_OVF_I_UN)
3024 CASE (CEE_CONV_OVF_U_UN)
3026 CASE (CEE_CONV_OVF_I1_UN)
3027 CASE (CEE_CONV_OVF_I2_UN)
3028 CASE (CEE_CONV_OVF_I4_UN)
3029 CASE (CEE_CONV_OVF_U1_UN)
3030 CASE (CEE_CONV_OVF_U2_UN)
3031 CASE (CEE_CONV_OVF_U4_UN) {
3033 switch (sp [-1].type) {
3035 value = (guint64)sp [-1].data.f;
3038 value = (guint64)sp [-1].data.l;
3043 value = (guint64)sp [-1].data.i;
3046 value = (guint64)sp [-1].data.nati;
3050 case CEE_CONV_OVF_I1_UN:
3052 THROW_EX (mono_get_exception_overflow (), ip);
3053 sp [-1].data.i = value;
3054 sp [-1].type = VAL_I32;
3056 case CEE_CONV_OVF_I2_UN:
3058 THROW_EX (mono_get_exception_overflow (), ip);
3059 sp [-1].data.i = value;
3060 sp [-1].type = VAL_I32;
3062 #if SIZEOF_VOID_P == 4
3063 case CEE_CONV_OVF_I_UN: /* Fall through */
3065 case CEE_CONV_OVF_I4_UN:
3066 if (value > 2147483647)
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_U1_UN:
3073 THROW_EX (mono_get_exception_overflow (), ip);
3074 sp [-1].data.i = value;
3075 sp [-1].type = VAL_I32;
3077 case CEE_CONV_OVF_U2_UN:
3079 THROW_EX (mono_get_exception_overflow (), ip);
3080 sp [-1].data.i = value;
3081 sp [-1].type = VAL_I32;
3083 #if SIZEOF_VOID_P == 4
3084 case CEE_CONV_OVF_U_UN: /* Fall through */
3086 case CEE_CONV_OVF_U4_UN:
3087 if (value > 4294967295U)
3088 THROW_EX (mono_get_exception_overflow (), ip);
3089 sp [-1].data.i = value;
3090 sp [-1].type = VAL_I32;
3093 g_assert_not_reached ();
3103 token = read32 (ip);
3105 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3106 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3108 class = mono_class_get (image, token);
3109 g_assert (class != NULL);
3111 sp [-1].type = VAL_OBJ;
3112 if (class->byval_arg.type == MONO_TYPE_VALUETYPE && !class->enumtype)
3113 sp [-1].data.p = mono_value_box (domain, class, sp [-1].data.p);
3115 stackval_to_data (&class->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3116 sp [-1].data.p = mono_value_box (domain, class, &sp [-1]);
3118 /* need to vt_free (sp); */
3130 token = read32 (ip);
3132 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3133 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3135 class = mono_class_get (image, token);
3137 o = (MonoObject*) mono_array_new (domain, class, sp [-1].data.i);
3140 sp [-1].type = VAL_OBJ;
3142 /*if (profiling_classes) {
3143 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3145 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3155 g_assert (sp [-1].type == VAL_OBJ);
3159 THROW_EX (mono_get_exception_null_reference (), ip - 1);
3161 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3163 sp [-1].type = VAL_I32;
3164 sp [-1].data.i = mono_array_length (o);
3168 CASE (CEE_LDELEMA) {
3170 guint32 esize, token;
3173 token = read32 (ip);
3177 g_assert (sp [0].type == VAL_OBJ);
3180 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3182 if (sp [1].data.nati >= mono_array_length (o))
3183 THROW_EX (mono_get_exception_index_out_of_range (), ip - 5);
3185 /* check the array element corresponds to token */
3186 esize = mono_array_element_size (o->obj.vtable->klass);
3189 sp->data.p = mono_array_addr_with_size (o, esize, sp [1].data.i);
3190 sp->data.vt.klass = o->obj.vtable->klass->element_class;
3195 CASE (CEE_LDELEM_I1) /* fall through */
3196 CASE (CEE_LDELEM_U1) /* fall through */
3197 CASE (CEE_LDELEM_I2) /* fall through */
3198 CASE (CEE_LDELEM_U2) /* fall through */
3199 CASE (CEE_LDELEM_I4) /* fall through */
3200 CASE (CEE_LDELEM_U4) /* fall through */
3201 CASE (CEE_LDELEM_I8) /* fall through */
3202 CASE (CEE_LDELEM_I) /* fall through */
3203 CASE (CEE_LDELEM_R4) /* fall through */
3204 CASE (CEE_LDELEM_R8) /* fall through */
3205 CASE (CEE_LDELEM_REF) {
3211 g_assert (sp [0].type == VAL_OBJ);
3214 THROW_EX (mono_get_exception_null_reference (), ip);
3216 g_assert (MONO_CLASS_IS_ARRAY (o->obj.vtable->klass));
3218 aindex = sp [1].data.nati;
3219 if (aindex >= mono_array_length (o))
3220 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3223 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3227 sp [0].data.i = mono_array_get (o, gint8, aindex);
3228 sp [0].type = VAL_I32;
3231 sp [0].data.i = mono_array_get (o, guint8, aindex);
3232 sp [0].type = VAL_I32;
3235 sp [0].data.i = mono_array_get (o, gint16, aindex);
3236 sp [0].type = VAL_I32;
3239 sp [0].data.i = mono_array_get (o, guint16, aindex);
3240 sp [0].type = VAL_I32;
3243 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3244 sp [0].type = VAL_NATI;
3247 sp [0].data.i = mono_array_get (o, gint32, aindex);
3248 sp [0].type = VAL_I32;
3251 sp [0].data.i = mono_array_get (o, guint32, aindex);
3252 sp [0].type = VAL_I32;
3255 sp [0].data.l = mono_array_get (o, guint64, aindex);
3256 sp [0].type = VAL_I64;
3259 sp [0].data.f = mono_array_get (o, float, aindex);
3260 sp [0].type = VAL_DOUBLE;
3263 sp [0].data.f = mono_array_get (o, double, aindex);
3264 sp [0].type = VAL_DOUBLE;
3266 case CEE_LDELEM_REF:
3267 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3268 sp [0].data.vt.klass = NULL;
3269 sp [0].type = VAL_OBJ;
3279 CASE (CEE_STELEM_I) /* fall through */
3280 CASE (CEE_STELEM_I1) /* fall through */
3281 CASE (CEE_STELEM_I2) /* fall through */
3282 CASE (CEE_STELEM_I4) /* fall through */
3283 CASE (CEE_STELEM_I8) /* fall through */
3284 CASE (CEE_STELEM_R4) /* fall through */
3285 CASE (CEE_STELEM_R8) /* fall through */
3286 CASE (CEE_STELEM_REF) {
3293 g_assert (sp [0].type == VAL_OBJ);
3296 THROW_EX (mono_get_exception_null_reference (), ip);
3298 ac = o->obj.vtable->klass;
3299 g_assert (MONO_CLASS_IS_ARRAY (ac));
3301 aindex = sp [1].data.nati;
3302 if (aindex >= mono_array_length (o))
3303 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3306 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3310 mono_array_set (o, mono_i, aindex, sp [2].data.nati);
3313 mono_array_set (o, gint8, aindex, sp [2].data.i);
3316 mono_array_set (o, gint16, aindex, sp [2].data.i);
3319 mono_array_set (o, gint32, aindex, sp [2].data.i);
3322 mono_array_set (o, gint64, aindex, sp [2].data.l);
3325 mono_array_set (o, float, aindex, sp [2].data.f);
3328 mono_array_set (o, double, aindex, sp [2].data.f);
3330 case CEE_STELEM_REF:
3331 g_assert (sp [2].type == VAL_OBJ);
3332 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3333 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3334 mono_array_set (o, gpointer, aindex, sp [2].data.p);
3345 CASE (CEE_UNBOX_ANY)
3358 CASE (CEE_UNUSED17) ves_abort(); BREAK;
3359 CASE (CEE_CONV_OVF_I1)
3360 if (sp [-1].type == VAL_I32) {
3361 if (sp [-1].data.i < 128 || sp [-1].data.i > 127)
3362 THROW_EX (mono_get_exception_overflow (), ip);
3363 sp [-1].data.i = (gint8)sp [-1].data.i;
3364 } else if (sp [-1].type == VAL_I64) {
3365 if (sp [-1].data.l < 128 || sp [-1].data.l > 127)
3366 THROW_EX (mono_get_exception_overflow (), ip);
3367 sp [-1].data.i = (gint8)sp [-1].data.l;
3373 CASE (CEE_CONV_OVF_U1)
3374 if (sp [-1].type == VAL_I32) {
3375 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3376 THROW_EX (mono_get_exception_overflow (), ip);
3377 sp [-1].data.i = (gint8)sp [-1].data.i;
3378 } else if (sp [-1].type == VAL_I64) {
3379 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3380 THROW_EX (mono_get_exception_overflow (), ip);
3381 sp [-1].data.i = (gint8)sp [-1].data.l;
3387 CASE (CEE_CONV_OVF_I2)
3388 CASE (CEE_CONV_OVF_U2)
3390 /* FIXME: handle other cases */
3391 if (sp [-1].type == VAL_I32) {
3392 /* defined as NOP */
3397 CASE (CEE_CONV_OVF_I4)
3398 /* FIXME: handle other cases */
3399 if (sp [-1].type == VAL_I32) {
3400 /* defined as NOP */
3401 } else if(sp [-1].type == VAL_I64) {
3402 sp [-1].data.i = (gint32)sp [-1].data.l;
3403 sp [-1].type = VAL_I32;
3409 CASE (CEE_CONV_OVF_U4)
3410 /* FIXME: handle other cases */
3411 if (sp [-1].type == VAL_I32) {
3412 /* defined as NOP */
3413 } else if(sp [-1].type == VAL_I64) {
3414 sp [-1].data.i = (guint32)sp [-1].data.l;
3415 sp [-1].type = VAL_I32;
3421 CASE (CEE_CONV_OVF_I8)
3422 /* FIXME: handle other cases */
3423 if (sp [-1].type == VAL_I32) {
3424 sp [-1].data.l = (guint64)sp [-1].data.i;
3425 sp [-1].type = VAL_I64;
3426 } else if(sp [-1].type == VAL_I64) {
3427 /* defined as NOP */
3433 CASE (CEE_CONV_OVF_U8)
3434 /* FIXME: handle other cases */
3435 if (sp [-1].type == VAL_I32) {
3436 sp [-1].data.l = (guint64) sp [-1].data.i;
3437 sp [-1].type = VAL_I64;
3438 } else if(sp [-1].type == VAL_I64) {
3439 /* defined as NOP */
3451 CASE (CEE_UNUSED23) ves_abort(); BREAK;
3452 CASE (CEE_REFANYVAL) ves_abort(); BREAK;
3454 if (!finite(sp [-1].data.f))
3455 THROW_EX (mono_get_exception_arithmetic (), ip);
3458 CASE (CEE_UNUSED24) ves_abort(); BREAK;
3459 CASE (CEE_UNUSED25) ves_abort(); BREAK;
3460 CASE (CEE_MKREFANY) ves_abort(); BREAK;
3469 CASE (CEE_UNUSED67) ves_abort(); BREAK;
3470 CASE (CEE_LDTOKEN) {
3472 MonoClass *handle_class;
3474 handle = mono_ldtoken (image, read32 (ip), &handle_class);
3476 vt_alloc (&handle_class->byval_arg, sp, FALSE);
3477 stackval_from_data (&handle_class->byval_arg, sp, (char*)&handle, FALSE);
3481 CASE (CEE_CONV_OVF_I)
3484 /* FIXME: check overflow. */
3487 sp->data.p = (gpointer)(mono_i) sp->data.i;
3490 sp->data.p = (gpointer)(mono_i) sp->data.l;
3495 sp->data.p = (gpointer)(mono_i) sp->data.f;
3500 sp->type = VAL_NATI;
3503 CASE (CEE_CONV_OVF_U) ves_abort(); BREAK;
3506 /* FIXME: check overflow */
3507 if (sp->type == VAL_I32) {
3508 if (CHECK_ADD_OVERFLOW (sp [-1].data.i, GET_NATI (sp [0])))
3509 THROW_EX (mono_get_exception_overflow (), ip);
3510 sp [-1].data.i = (gint32)sp [-1].data.i + (gint32)GET_NATI (sp [0]);
3511 } else if (sp->type == VAL_I64) {
3512 if (CHECK_ADD_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3513 THROW_EX (mono_get_exception_overflow (), ip);
3514 sp [-1].data.l = (gint64)sp [-1].data.l + (gint64)sp [0].data.l;
3515 } else if (sp->type == VAL_DOUBLE)
3516 sp [-1].data.f += sp [0].data.f;
3518 char *p = sp [-1].data.p;
3519 p += GET_NATI (sp [0]);
3524 CASE (CEE_ADD_OVF_UN)
3526 /* FIXME: check overflow, make unsigned */
3527 if (sp->type == VAL_I32) {
3528 if (CHECK_ADD_OVERFLOW_UN (sp [-1].data.i, GET_NATI (sp [0])))
3529 THROW_EX (mono_get_exception_overflow (), ip);
3530 sp [-1].data.i = (guint32)sp [-1].data.i + (guint32)GET_NATI (sp [0]);
3531 } else if (sp->type == VAL_I64) {
3532 if (CHECK_ADD_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3533 THROW_EX (mono_get_exception_overflow (), ip);
3534 sp [-1].data.l = (guint64)sp [-1].data.l + (guint64)sp [0].data.l;
3535 } else if (sp->type == VAL_DOUBLE)
3536 sp [-1].data.f += sp [0].data.f;
3538 char *p = sp [-1].data.p;
3539 p += GET_NATI (sp [0]);
3547 /* FIXME: check overflow */
3548 if (sp->type == VAL_I32) {
3549 if (CHECK_MUL_OVERFLOW (sp [-1].data.i, GET_NATI (sp [0])))
3550 THROW_EX (mono_get_exception_overflow (), ip);
3551 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
3553 else if (sp->type == VAL_I64) {
3554 if (CHECK_MUL_OVERFLOW64 (sp [-1].data.l, sp [0].data.l))
3555 THROW_EX (mono_get_exception_overflow (), ip);
3556 sp [-1].data.l *= sp [0].data.l;
3558 else if (sp->type == VAL_DOUBLE)
3559 sp [-1].data.f *= sp [0].data.f;
3561 CASE (CEE_MUL_OVF_UN)
3564 /* FIXME: check overflow, make unsigned */
3565 if (sp->type == VAL_I32) {
3566 if (CHECK_MUL_OVERFLOW_UN (sp [-1].data.i, GET_NATI (sp [0])))
3567 THROW_EX (mono_get_exception_overflow (), ip);
3568 sp [-1].data.i *= (gint)GET_NATI (sp [0]);
3570 else if (sp->type == VAL_I64) {
3571 if (CHECK_MUL_OVERFLOW64_UN (sp [-1].data.l, sp [0].data.l))
3572 THROW_EX (mono_get_exception_overflow (), ip);
3573 sp [-1].data.l *= sp [0].data.l;
3575 else if (sp->type == VAL_DOUBLE)
3576 sp [-1].data.f *= sp [0].data.f;
3579 CASE (CEE_SUB_OVF_UN)
3582 /* FIXME: handle undeflow/unsigned */
3583 /* should probably consider the pointers as unsigned */
3584 if (sp->type == VAL_I32)
3585 sp [-1].data.i -= GET_NATI (sp [0]);
3586 else if (sp->type == VAL_I64)
3587 sp [-1].data.l -= sp [0].data.l;
3588 else if (sp->type == VAL_DOUBLE)
3589 sp [-1].data.f -= sp [0].data.f;
3591 char *p = sp [-1].data.p;
3592 p -= GET_NATI (sp [0]);
3596 CASE (CEE_ENDFINALLY)
3598 ip = finally_ips->data;
3599 finally_ips = g_slist_remove (finally_ips, ip);
3605 * There was no exception, we continue normally at the target address.
3609 CASE (CEE_LEAVE) /* Fall through */
3611 sp = frame->stack; /* empty the stack */
3613 if (*ip == CEE_LEAVE_S) {
3615 ip += (signed char) *ip;
3619 ip += (gint32) read32 (ip);
3624 * We may be either inside a try block or inside an handler.
3625 * In the first case there was no exception and we go on
3626 * executing the finally handlers and after that resume control
3628 * In the second case we need to clear the exception and
3629 * continue directly at the target ip.
3633 goto handle_finally;
3636 frame->ex_handler = NULL;
3640 frame->ex_handler = NULL;
3642 goto handle_finally;
3648 case CEE_MONO_FUNC1: {
3649 MonoMarshalConv conv;
3659 sp->data.vt.klass = NULL;
3662 case MONO_MARSHAL_CONV_STR_LPWSTR:
3663 sp->data.p = mono_string_to_utf16 (sp->data.p);
3665 case MONO_MARSHAL_CONV_LPSTR_STR:
3666 sp->data.p = mono_string_new_wrapper (sp->data.p);
3668 case MONO_MARSHAL_CONV_STR_LPTSTR:
3669 case MONO_MARSHAL_CONV_STR_LPSTR:
3670 sp->data.p = mono_string_to_utf8 (sp->data.p);
3672 case MONO_MARSHAL_CONV_STR_BSTR:
3673 sp->data.p = mono_string_to_bstr (sp->data.p);
3675 case MONO_MARSHAL_CONV_STR_TBSTR:
3676 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3677 sp->data.p = mono_string_to_ansibstr (sp->data.p);
3679 case MONO_MARSHAL_CONV_SB_LPSTR:
3680 sp->data.p = mono_string_builder_to_utf8 (sp->data.p);
3682 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3683 sp->data.p = mono_array_to_savearray (sp->data.p);
3685 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3686 sp->data.p = mono_array_to_lparray (sp->data.p);
3688 case MONO_MARSHAL_CONV_DEL_FTN:
3689 sp->data.p = mono_delegate_to_ftnptr (sp->data.p);
3691 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3692 sp->data.p = mono_marshal_string_array (sp->data.p);
3695 g_assert_not_reached ();
3700 case CEE_MONO_PROC2: {
3701 MonoMarshalConv conv;
3709 case MONO_MARSHAL_CONV_LPSTR_SB:
3710 mono_string_utf8_to_builder (sp [0].data.p, sp [1].data.p);
3712 case MONO_MARSHAL_FREE_ARRAY:
3713 mono_marshal_free_array (sp [0].data.p, sp [1].data.i);
3716 g_assert_not_reached ();
3720 case CEE_MONO_PROC3: {
3721 MonoMarshalConv conv;
3729 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3730 mono_string_to_byvalstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3732 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3733 mono_string_to_byvalwstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3736 g_assert_not_reached ();
3740 case CEE_MONO_VTADDR: {
3743 sp->type = VAL_VALUETA;
3747 case CEE_MONO_LDPTR: {
3751 token = read32 (ip);
3755 sp->data.p = mono_method_get_wrapper_data (frame->method, token);
3756 sp->data.vt.klass = NULL;
3760 case CEE_MONO_FREE: {
3764 g_free (sp->data.p);
3767 case CEE_MONO_OBJADDR: {
3774 case CEE_MONO_NEWOBJ: {
3779 token = read32 (ip);
3782 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3783 sp->data.p = mono_object_new (domain, class);
3787 case CEE_MONO_RETOBJ: {
3792 token = read32 (ip);
3797 class = (MonoClass *)mono_method_get_wrapper_data (frame->method, token);
3799 stackval_from_data (signature->ret, frame->retval, sp->data.vt.vt, signature->pinvoke);
3801 if (sp > frame->stack)
3802 g_warning ("more values on stack: %d", sp-frame->stack);
3807 g_error ("Unimplemented opcode: 0xF0 %02x at 0x%x\n", *ip, ip-header->code);
3838 CASE (CEE_PREFIXREF) ves_abort(); BREAK;
3840 * Note: Exceptions thrown when executing a prefixed opcode need
3841 * to take into account the number of prefix bytes (usually the
3842 * throw point is just (ip - n_prefix_bytes).
3847 case CEE_ARGLIST: ves_abort(); break;
3853 if (sp->type == VAL_I32)
3854 result = sp [0].data.i == (gint)GET_NATI (sp [1]);
3855 else if (sp->type == VAL_I64)
3856 result = sp [0].data.l == sp [1].data.l;
3857 else if (sp->type == VAL_DOUBLE) {
3858 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3861 result = sp [0].data.f == sp [1].data.f;
3863 result = GET_NATI (sp [0]) == GET_NATI (sp [1]);
3865 sp->data.i = result;
3875 if (sp->type == VAL_I32)
3876 result = sp [0].data.i > (gint)GET_NATI (sp [1]);
3877 else if (sp->type == VAL_I64)
3878 result = sp [0].data.l > sp [1].data.l;
3879 else if (sp->type == VAL_DOUBLE) {
3880 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3883 result = sp [0].data.f > sp [1].data.f;
3885 result = (gint)GET_NATI (sp [0]) > (gint)GET_NATI (sp [1]);
3887 sp->data.i = result;
3897 if (sp->type == VAL_I32)
3898 result = (guint32)sp [0].data.i > (mono_u)GET_NATI (sp [1]);
3899 else if (sp->type == VAL_I64)
3900 result = (guint64)sp [0].data.l > (guint64)sp [1].data.l;
3901 else if (sp->type == VAL_DOUBLE)
3902 result = isnan (sp [0].data.f) || isnan (sp [1].data.f);
3904 result = (mono_u)GET_NATI (sp [0]) > (mono_u)GET_NATI (sp [1]);
3906 sp->data.i = result;
3916 if (sp->type == VAL_I32)
3917 result = sp [0].data.i < (gint)GET_NATI (sp [1]);
3918 else if (sp->type == VAL_I64)
3919 result = sp [0].data.l < sp [1].data.l;
3920 else if (sp->type == VAL_DOUBLE) {
3921 if (isnan (sp [0].data.f) || isnan (sp [1].data.f))
3924 result = sp [0].data.f < sp [1].data.f;
3926 result = (gint)GET_NATI (sp [0]) < (gint)GET_NATI (sp [1]);
3928 sp->data.i = result;
3938 if (sp->type == VAL_I32)
3939 result = (guint32)sp [0].data.i < (mono_u)GET_NATI (sp [1]);
3940 else if (sp->type == VAL_I64)
3941 result = (guint64)sp [0].data.l < (guint64)sp [1].data.l;
3942 else if (sp->type == VAL_DOUBLE)
3943 result = isnan (sp [0].data.f) || isnan (sp [1].data.f);
3945 result = (mono_u)GET_NATI (sp [0]) < (mono_u)GET_NATI (sp [1]);
3947 sp->data.i = result;
3953 case CEE_LDVIRTFTN: {
3954 int virtual = *ip == CEE_LDVIRTFTN;
3958 token = read32 (ip);
3961 if (frame->method->wrapper_type != MONO_WRAPPER_NONE)
3962 m = (MonoMethod *)mono_method_get_wrapper_data (frame->method, token);
3964 m = mono_get_method (image, token, NULL);
3967 THROW_EX (mono_get_exception_missing_method (), ip - 5);
3971 THROW_EX (mono_get_exception_null_reference (), ip - 5);
3973 m = get_virtual_method (domain, m, sp);
3975 sp->type = VAL_NATI;
3976 sp->data.p = mono_create_method_pointer (m);
3977 sp->data.vt.klass = NULL;
3981 case CEE_UNUSED56: ves_abort(); break;
3985 arg_pos = read16 (ip);
3987 vt_alloc (ARG_TYPE (signature, arg_pos), sp, signature->pinvoke);
3988 stackval_from_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4000 t = ARG_TYPE (signature, anum);
4001 c = mono_class_from_mono_type (t);
4002 sp->data.vt.klass = c;
4003 sp->data.vt.vt = ARG_POS (anum);
4006 sp->type = VAL_VALUETA;
4016 arg_pos = read16 (ip);
4019 stackval_to_data (ARG_TYPE (signature, arg_pos), sp, ARG_POS (arg_pos), signature->pinvoke);
4026 loc_pos = read16 (ip);
4028 vt_alloc (LOCAL_TYPE (header, loc_pos), sp, FALSE);
4029 stackval_from_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4039 loc_pos = read16 (ip);
4041 t = LOCAL_TYPE (header, loc_pos);
4042 c = mono_class_from_mono_type (t);
4043 sp->data.vt.vt = LOCAL_POS (loc_pos);
4044 sp->data.vt.klass = c;
4047 sp->type = VAL_VALUETA;
4057 loc_pos = read16 (ip);
4060 stackval_to_data (LOCAL_TYPE (header, loc_pos), sp, LOCAL_POS (loc_pos), FALSE);
4066 if (sp != frame->stack)
4067 THROW_EX (mono_get_exception_execution_engine (NULL), ip - 1);
4069 sp->data.p = alloca (sp->data.i);
4073 case CEE_UNUSED57: ves_abort(); break;
4074 case CEE_ENDFILTER: ves_abort(); break;
4075 case CEE_UNALIGNED_:
4077 unaligned_address = 1;
4081 volatile_address = 1;
4090 token = read32 (ip);
4093 * we ignore the value of token (I think we can as unspecified
4094 * behavior described in Partition II, 3.5).
4097 g_assert (sp->type == VAL_VALUETA || sp->type == VAL_TP);
4098 memset (sp->data.vt.vt, 0, mono_class_value_size (sp->data.vt.klass, NULL));
4101 case CEE_UNUSED68: ves_abort(); break;
4104 if (!sp [0].data.p || !sp [1].data.p)
4105 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4107 /* FIXME: value and size may be int64... */
4108 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4113 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4115 /* FIXME: value and size may be int64... */
4116 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4118 case CEE_UNUSED69: ves_abort(); break;
4121 * need to clarify what this should actually do:
4122 * start the search from the last found handler in
4123 * this method or continue in the caller or what.
4124 * Also, do we need to run finally/fault handlers after a retrow?
4125 * Well, this implementation will follow the usual search
4126 * for an handler, considering the current ip as throw spot.
4127 * We need to NULL frame->ex_handler for the later code to
4128 * actually run the new found handler.
4130 frame->ex_handler = NULL;
4131 THROW_EX (frame->ex, ip - 1);
4133 case CEE_UNUSED: ves_abort(); break;
4138 token = read32 (ip);
4140 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4141 MonoType *type = mono_type_create_from_typespec (image, token);
4142 sp->data.i = mono_type_size (type, &align);
4143 mono_metadata_free_type (type);
4145 MonoClass *szclass = mono_class_get (image, token);
4146 mono_class_init (szclass);
4147 if (!szclass->valuetype)
4148 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
4149 sp->data.i = mono_class_value_size (szclass, &align);
4155 case CEE_REFANYTYPE: ves_abort(); break;
4162 g_error ("Unimplemented opcode: 0xFE %02x at 0x%x\n", *ip, ip-header->code);
4169 g_assert_not_reached ();
4171 * Exception handling code.
4172 * The exception object is stored in frame->ex.
4179 MonoInvocation *inv;
4180 MonoMethodHeader *hd;
4181 MonoExceptionClause *clause;
4187 g_print ("* Handling exception '%s' at IL_%04x\n", mono_object_class (frame->ex)->name, frame->ip - header->code);
4189 if (die_on_exception)
4192 for (inv = frame; inv; inv = inv->parent) {
4193 if (inv->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4195 if (inv->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4197 hd = ((MonoMethodNormal*)inv->method)->header;
4198 ip_offset = inv->ip - hd->code;
4199 for (i = 0; i < hd->num_clauses; ++i) {
4200 clause = &hd->clauses [i];
4201 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4202 if (!clause->flags) {
4203 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (inv->method->klass->image, clause->token_or_filter))) {
4205 * OK, we found an handler, now we need to execute the finally
4206 * and fault blocks before branching to the handler code.
4208 inv->ex_handler = clause;
4211 g_print ("* Found handler at '%s'\n", inv->method->name);
4214 * It seems that if the catch handler is found in the same method,
4215 * it gets executed before the finally handler.
4220 goto handle_finally;
4223 /* FIXME: handle filter clauses */
4230 * If we get here, no handler was found: print a stack trace.
4232 for (inv = frame; inv; inv = inv->parent) {
4233 if (inv->invoke_trap)
4234 goto handle_finally;
4237 ex_obj = (MonoObject*)frame->ex;
4238 mono_unhandled_exception (ex_obj);
4245 MonoExceptionClause *clause;
4249 g_print ("* Handle finally\n");
4251 if ((frame->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4252 || (frame->method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4256 ip_offset = frame->ip - header->code;
4258 for (i = 0; i < header->num_clauses; ++i) {
4259 clause = &header->clauses [i];
4260 if (clause == frame->ex_handler)
4262 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - header->code))) {
4263 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4264 ip = header->code + clause->handler_offset;
4265 finally_ips = g_slist_append (finally_ips, (gpointer) ip);
4268 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4274 ip = finally_ips->data;
4275 finally_ips = g_slist_remove (finally_ips, ip);
4280 * If an exception is set, we need to execute the fault handler, too,
4281 * otherwise, we continue normally.
4292 MonoExceptionClause *clause;
4296 g_print ("* Handle fault\n");
4298 ip_offset = frame->ip - header->code;
4299 for (i = 0; i < header->num_clauses; ++i) {
4300 clause = &header->clauses [i];
4301 if (clause->flags == 3 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4302 ip = header->code + clause->handler_offset;
4305 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4311 * If the handler for the exception was found in this method, we jump
4312 * to it right away, otherwise we return and let the caller run
4313 * the finally, fault and catch blocks.
4314 * This same code should be present in the endfault opcode, but it
4315 * is corrently not assigned in the ECMA specs: LAMESPEC.
4317 if (frame->ex_handler) {
4320 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4322 ip = header->code + frame->ex_handler->handler_offset;
4325 sp->data.p = frame->ex;
4338 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4340 MonoImage *image = assembly->image;
4341 MonoCLIImageInfo *iinfo;
4343 MonoObject *exc = NULL;
4346 iinfo = image->image_info;
4347 method = mono_get_method (image, iinfo->cli_cli_header.ch_entry_point, NULL);
4349 g_error ("No entry point method found in %s", image->name);
4351 rval = mono_runtime_run_main (method, argc, argv, &exc);
4360 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4361 "Usage is: mint [options] executable args...\n\n", VERSION);
4363 "Runtime Debugging:\n"
4368 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4371 " --traceclassinit\n"
4374 " --debug method_name\n"
4380 " --config filename load the specified config file instead of the default\n"
4381 " --workers n maximum number of worker threads\n"
4388 test_load_class (MonoImage* image)
4390 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
4394 for (i = 1; i <= t->rows; ++i) {
4395 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4396 mono_class_init (klass);
4401 static MonoException * segv_exception = NULL;
4404 segv_handler (int signum)
4406 signal (signum, segv_handler);
4407 mono_raise_exception (segv_exception);
4411 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4412 MonoReflectionMethod **method,
4413 gint32 *iloffset, gint32 *native_offset,
4414 MonoString **file, gint32 *line, gint32 *column)
4427 *file = mono_string_new (mono_domain_get (), "unknown");
4433 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4441 int enable_debugging;
4447 static void main_thread_handler (gpointer user_data)
4449 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4450 MonoAssembly *assembly;
4453 if (main_args->enable_debugging)
4454 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4456 assembly = mono_domain_assembly_open (main_args->domain,
4460 fprintf (stderr, "Can not open image %s\n", main_args->file);
4464 if (main_args->enable_debugging)
4465 mono_debug_init_2 (assembly);
4468 test_load_class (assembly->image);
4470 error = mono_verify_corlib ();
4472 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
4475 segv_exception = mono_get_exception_null_reference ();
4476 segv_exception->message = mono_string_new (main_args->domain, "Segmentation fault");
4477 signal (SIGSEGV, segv_handler);
4479 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4484 mono_runtime_install_handlers (void)
4486 /* FIXME: anything to do here? */
4490 main (int argc, char *argv [])
4493 int retval = 0, i, ocount = 0;
4494 char *file, *config_file = NULL;
4495 int enable_debugging = FALSE;
4496 MainThreadArgs main_args;
4501 for (i = 1; i < argc && argv [i][0] == '-'; i++){
4502 if (strcmp (argv [i], "--trace") == 0)
4504 if (strcmp (argv [i], "--noptr") == 0)
4505 global_no_pointers = 1;
4506 if (strcmp (argv [i], "--traceops") == 0)
4508 if (strcmp (argv [i], "--dieonex") == 0) {
4509 die_on_exception = 1;
4510 enable_debugging = 1;
4512 if (strcmp (argv [i], "--print-vtable") == 0)
4513 mono_print_vtable = TRUE;
4514 if (strcmp (argv [i], "--profile") == 0)
4515 mono_profiler_load (NULL);
4516 if (strcmp (argv [i], "--opcode-count") == 0)
4518 if (strcmp (argv [i], "--config") == 0)
4519 config_file = argv [++i];
4520 if (strcmp (argv [i], "--workers") == 0) {
4521 mono_max_worker_threads = atoi (argv [++i]);
4522 if (mono_max_worker_threads < 1)
4523 mono_max_worker_threads = 1;
4525 if (strcmp (argv [i], "--help") == 0)
4528 if (strcmp (argv [i], "--debug") == 0) {
4529 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
4531 g_error ("Invalid method name '%s'", argv [i]);
4532 db_methods = g_list_append (db_methods, desc);
4542 g_set_prgname (file);
4543 mono_set_rootdir ();
4544 mono_config_parse (config_file);
4546 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4547 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4550 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4551 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4552 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4554 frame_thread_id = TlsAlloc ();
4555 TlsSetValue (frame_thread_id, NULL);
4557 mono_install_compile_method (mono_create_method_pointer);
4558 mono_install_runtime_invoke (interp_mono_runtime_invoke);
4559 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4561 mono_install_handler (interp_ex_handler);
4562 mono_install_stack_walk (interp_walk_stack);
4564 InitializeCriticalSection (&metadata_lock);
4565 domain = mono_init (file);
4566 mono_runtime_init (domain, NULL, NULL);
4568 main_args.domain=domain;
4569 main_args.file=file;
4570 main_args.argc=argc-i;
4571 main_args.argv=argv+i;
4572 main_args.enable_debugging=enable_debugging;
4574 mono_runtime_exec_managed_code (domain, main_thread_handler,
4577 mono_profiler_shutdown ();
4579 mono_runtime_cleanup (domain);
4580 mono_domain_unload (domain, TRUE);
4582 /* Get the return value from System.Environment.ExitCode */
4583 retval=mono_environment_exitcode_get ();
4587 fprintf (stderr, "opcode count: %ld\n", opcode_count);
4588 fprintf (stderr, "fcall count: %ld\n", fcall_count);