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.
27 #include <mono/os/gc_wrapper.h>
33 # define alloca __builtin_alloca
37 /* trim excessive headers */
38 #include <mono/metadata/image.h>
39 #include <mono/metadata/assembly.h>
40 #include <mono/metadata/cil-coff.h>
41 #include <mono/metadata/mono-endian.h>
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/tokentype.h>
44 #include <mono/metadata/loader.h>
45 #include <mono/metadata/threads.h>
46 #include <mono/metadata/threadpool.h>
47 #include <mono/metadata/profiler-private.h>
48 #include <mono/metadata/appdomain.h>
49 #include <mono/metadata/reflection.h>
50 #include <mono/metadata/exception.h>
51 #include <mono/metadata/verify.h>
52 #include <mono/metadata/opcodes.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/metadata/mono-config.h>
55 #include <mono/metadata/marshal.h>
56 #include <mono/metadata/environment.h>
57 #include <mono/metadata/mono-debug.h>
58 #include <mono/os/util.h>
65 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
69 #include "mono/cil/opcode.def"
74 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
77 #define finite _finite
81 #define finite isfinite
85 /* If true, then we output the opcodes as we interpret them */
86 static int global_tracing = 0;
87 static int global_no_pointers = 0;
89 int mono_interp_traceopt = 0;
91 static int debug_indent_level = 0;
93 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,mono_method) \
95 (frame)->parent = (parent_frame); \
96 (frame)->obj = (obj_this); \
97 (frame)->stack_args = (method_args); \
98 (frame)->retval = (method_retval); \
99 (frame)->runtime_method = mono_interp_get_runtime_method (mono_method); \
100 (frame)->ex = NULL; \
101 (frame)->ip = NULL; \
102 (frame)->invoke_trap = 0; \
105 void ves_exec_method (MonoInvocation *frame);
107 static char* dump_stack (stackval *stack, stackval *sp);
108 static char* dump_frame (MonoInvocation *inv);
109 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
110 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
112 typedef void (*ICallMethod) (MonoInvocation *frame);
114 static guint32 die_on_exception = 0;
115 static guint32 thread_context_id = 0;
117 #define DEBUG_INTERP 1
121 static int break_on_method = 0;
122 static int nested_trace = 0;
123 static GList *db_methods = NULL;
130 for (h = 0; h < debug_indent_level; h++)
135 db_match_method (gpointer data, gpointer user_data)
137 MonoMethod *m = (MonoMethod*)user_data;
138 MonoMethodDesc *desc = data;
140 if (mono_method_desc_full_match (desc, m))
144 #define DEBUG_ENTER() \
146 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method); \
147 if (break_on_method) tracing=nested_trace ? (global_tracing = 2, 3) : 2; \
148 break_on_method = 0; \
151 MonoMethod *method = frame->runtime_method->method ;\
152 char *mn, *args = dump_args (frame); \
153 debug_indent_level++; \
155 mn = mono_method_full_name (method, FALSE); \
156 g_print ("(%u) Entering %s (", GetCurrentThreadId(), mn); \
158 g_print ("%s)\n", args); \
161 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
162 mono_profiler_method_enter (frame->runtime_method->method);
164 #define DEBUG_LEAVE() \
167 args = dump_retval (frame); \
169 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
170 g_print ("(%u) Leaving %s", GetCurrentThreadId(), mn); \
172 g_print (" => %s\n", args); \
174 debug_indent_level--; \
175 if (tracing == 3) global_tracing = 0; \
177 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
178 mono_profiler_method_leave (frame->runtime_method->method);
182 #define DEBUG_ENTER()
183 #define DEBUG_LEAVE()
188 interp_ex_handler (MonoException *ex) {
189 ThreadContext *context = TlsGetValue (thread_context_id);
193 stack_trace = dump_frame (context->current_frame);
194 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
195 g_free (stack_trace);
196 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
197 char *strace = mono_string_to_utf8 (ex->stack_trace);
198 fprintf(stderr, "Nothing can catch this exception: ");
199 fprintf(stderr, "%s", ex->object.vtable->klass->name);
200 if (ex->message != NULL) {
201 char *m = mono_string_to_utf8 (ex->message);
202 fprintf(stderr, ": %s", m);
205 fprintf(stderr, "\n");
206 fprintf(stderr, "%s\n", strace);
208 if (ex->inner_ex != NULL) {
209 ex = (MonoException *)ex->inner_ex;
210 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
211 if (ex->message != NULL) {
212 char *m = mono_string_to_utf8 (ex->message);
213 fprintf(stderr, ": %s", m);
216 strace = mono_string_to_utf8 (ex->stack_trace);
217 fprintf(stderr, "\n");
218 fprintf(stderr, "%s\n", strace);
221 /* wait for other threads to also collapse */
225 context->env_frame->ex = ex;
226 context->search_for_handler = 1;
227 longjmp (*context->current_env, 1);
231 ves_real_abort (int line, MonoMethod *mh,
232 const unsigned short *ip, stackval *stack, stackval *sp)
234 MonoMethodNormal *mm = (MonoMethodNormal *)mh;
235 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
236 fprintf (stderr, "Line=%d IP=0x%04x, Aborted execution\n", line,
237 ip-(const unsigned short *)mm->header->code);
238 g_print ("0x%04x %02x\n",
239 ip-(const unsigned short *)mm->header->code, *ip);
241 printf ("\t[%d] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
244 #define ves_abort() \
246 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
247 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
251 interp_create_remoting_trampoline (MonoMethod *method)
253 return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke (method));
256 static CRITICAL_SECTION runtime_method_lookup_section;
259 mono_interp_get_runtime_method (MonoMethod *method)
261 MonoDomain *domain = mono_domain_get ();
264 EnterCriticalSection (&runtime_method_lookup_section);
265 if ((rtm = g_hash_table_lookup (domain->jit_code_hash, method))) {
266 LeaveCriticalSection (&runtime_method_lookup_section);
269 rtm = mono_mempool_alloc (domain->mp, sizeof (RuntimeMethod));
270 memset (rtm, 0, sizeof (*rtm));
271 rtm->method = method;
272 rtm->param_count = method->signature->param_count;
273 rtm->hasthis = method->signature->hasthis;
274 rtm->valuetype = method->klass->valuetype;
275 g_hash_table_insert (domain->jit_code_hash, method, rtm);
276 LeaveCriticalSection (&runtime_method_lookup_section);
282 interp_create_trampoline (MonoMethod *method)
284 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
285 method = mono_marshal_get_synchronized_wrapper (method);
286 return mono_interp_get_runtime_method (method);
289 static inline RuntimeMethod*
290 get_virtual_method (RuntimeMethod *runtime_method, MonoObject *obj)
292 MonoMethod *m = runtime_method->method;
294 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
295 if (obj->vtable->klass == mono_defaults.transparent_proxy_class)
296 return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke (m));
297 else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
298 return mono_interp_get_runtime_method (mono_marshal_get_synchronized_wrapper (m));
300 return runtime_method;
303 if (m->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
304 return ((RuntimeMethod **)obj->vtable->interface_offsets [m->klass->interface_id]) [m->slot];
306 return ((RuntimeMethod **)obj->vtable->vtable) [m->slot];
311 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
314 switch (type->type) {
315 case MONO_TYPE_OBJECT:
316 case MONO_TYPE_CLASS:
317 case MONO_TYPE_STRING:
318 case MONO_TYPE_ARRAY:
319 case MONO_TYPE_SZARRAY:
324 result->data.p = *(gpointer*)data;
327 switch (type->type) {
331 result->data.i = *(gint8*)data;
334 case MONO_TYPE_BOOLEAN:
335 result->data.i = *(guint8*)data;
338 result->data.i = *(gint16*)data;
342 result->data.i = *(guint16*)data;
345 result->data.i = *(gint32*)data;
349 result->data.nati = *(mono_i*)data;
352 result->data.p = *(gpointer*)data;
355 result->data.i = *(guint32*)data;
358 result->data.f = *(float*)data;
362 result->data.l = *(gint64*)data;
365 result->data.f = *(double*)data;
367 case MONO_TYPE_STRING:
368 case MONO_TYPE_SZARRAY:
369 case MONO_TYPE_CLASS:
370 case MONO_TYPE_OBJECT:
371 case MONO_TYPE_ARRAY:
372 result->data.p = *(gpointer*)data;
374 case MONO_TYPE_VALUETYPE:
375 if (type->data.klass->enumtype) {
376 stackval_from_data (type->data.klass->enum_basetype, result, data, pinvoke);
382 size = mono_class_native_size (type->data.klass, NULL);
384 size = mono_class_value_size (type->data.klass, NULL);
385 memcpy (result->data.vt, data, size);
389 g_warning ("got type 0x%02x", type->type);
390 g_assert_not_reached ();
395 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
398 gpointer *p = (gpointer*)data;
402 //printf ("TODAT0 %p\n", data);
403 switch (type->type) {
406 guint8 *p = (guint8*)data;
410 case MONO_TYPE_BOOLEAN: {
411 guint8 *p = (guint8*)data;
412 *p = (val->data.i != 0);
417 case MONO_TYPE_CHAR: {
418 guint16 *p = (guint16*)data;
423 mono_i *p = (mono_i*)data;
424 /* In theory the value used by stloc should match the local var type
425 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
426 a native int - both by csc and mcs). Not sure what to do about sign extension
427 as it is outside the spec... doing the obvious */
428 *p = (mono_i)val->data.nati;
432 mono_u *p = (mono_u*)data;
434 *p = (mono_u)val->data.nati;
439 gint32 *p = (gint32*)data;
445 gint64 *p = (gint64*)data;
450 float *p = (float*)data;
455 double *p = (double*)data;
459 case MONO_TYPE_STRING:
460 case MONO_TYPE_SZARRAY:
461 case MONO_TYPE_CLASS:
462 case MONO_TYPE_OBJECT:
463 case MONO_TYPE_ARRAY:
464 case MONO_TYPE_PTR: {
465 gpointer *p = (gpointer*)data;
469 case MONO_TYPE_VALUETYPE:
470 if (type->data.klass->enumtype) {
471 stackval_to_data (type->data.klass->enum_basetype, val, data, pinvoke);
477 size = mono_class_native_size (type->data.klass, NULL);
479 size = mono_class_value_size (type->data.klass, NULL);
481 memcpy (data, val->data.p, size);
485 g_warning ("got type %x", type->type);
486 g_assert_not_reached ();
491 fill_in_trace (MonoException *exception, MonoInvocation *frame)
493 char *stack_trace = dump_frame (frame);
494 MonoDomain *domain = mono_domain_get();
495 (exception)->stack_trace = mono_string_new (domain, stack_trace);
496 (exception)->trace_ips = get_trace_ips (domain, frame);
497 g_free (stack_trace);
500 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
502 #define THROW_EX(exception,ex_ip) \
504 frame->ip = (ex_ip); \
505 frame->ex = (MonoException*)(exception); \
506 FILL_IN_TRACE(frame->ex, frame); \
507 goto handle_exception; \
511 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
514 guint32 *lower_bounds;
517 lengths = alloca (sizeof (guint32) * klass->rank * 2);
518 for (i = 0; i < sig->param_count; ++i) {
519 lengths [i] = values->data.i;
522 if (klass->rank == sig->param_count) {
523 /* Only lengths provided. */
526 /* lower bounds are first. */
527 lower_bounds = lengths;
528 lengths += klass->rank;
530 return (MonoObject*)mono_array_new_full (domain, klass, lengths, lower_bounds);
534 ves_array_set (MonoInvocation *frame)
536 stackval *sp = frame->stack_args;
540 gint32 i, t, pos, esize;
546 ac = o->vtable->klass;
548 g_assert (ac->rank >= 1);
551 if (ao->bounds != NULL) {
552 pos -= ao->bounds [0].lower_bound;
553 for (i = 1; i < ac->rank; i++) {
554 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
555 ao->bounds [i].length) {
556 frame->ex = mono_get_exception_index_out_of_range ();
557 FILL_IN_TRACE(frame->ex, frame);
560 pos = pos*ao->bounds [i].length + sp [i].data.i -
561 ao->bounds [i].lower_bound;
563 } else if (pos >= ao->max_length) {
564 frame->ex = mono_get_exception_index_out_of_range ();
565 FILL_IN_TRACE(frame->ex, frame);
570 if (sp [ac->rank].data.p && !mono_object_isinst (sp [ac->rank].data.p, mono_object_class (o)->element_class)) {
571 frame->ex = mono_get_exception_array_type_mismatch ();
572 FILL_IN_TRACE (frame->ex, frame);
577 esize = mono_array_element_size (ac);
578 ea = mono_array_addr_with_size (ao, esize, pos);
580 mt = frame->runtime_method->method->signature->params [ac->rank];
581 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
585 ves_array_get (MonoInvocation *frame)
587 stackval *sp = frame->stack_args;
591 gint32 i, t, pos, esize;
597 ac = o->vtable->klass;
599 g_assert (ac->rank >= 1);
602 if (ao->bounds != NULL) {
603 pos -= ao->bounds [0].lower_bound;
604 for (i = 1; i < ac->rank; i++) {
605 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
606 ao->bounds [i].length) {
607 frame->ex = mono_get_exception_index_out_of_range ();
608 FILL_IN_TRACE(frame->ex, frame);
612 pos = pos*ao->bounds [i].length + sp [i].data.i -
613 ao->bounds [i].lower_bound;
615 } else if (pos >= ao->max_length) {
616 frame->ex = mono_get_exception_index_out_of_range ();
617 FILL_IN_TRACE(frame->ex, frame);
621 esize = mono_array_element_size (ac);
622 ea = mono_array_addr_with_size (ao, esize, pos);
624 mt = frame->runtime_method->method->signature->ret;
625 stackval_from_data (mt, frame->retval, ea, FALSE);
629 ves_array_element_address (MonoInvocation *frame)
631 stackval *sp = frame->stack_args;
635 gint32 i, t, pos, esize;
640 ac = o->vtable->klass;
642 g_assert (ac->rank >= 1);
645 if (ao->bounds != NULL) {
646 pos -= ao->bounds [0].lower_bound;
647 for (i = 1; i < ac->rank; i++) {
648 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
649 ao->bounds [i].length) {
650 frame->ex = mono_get_exception_index_out_of_range ();
651 FILL_IN_TRACE(frame->ex, frame);
654 pos = pos*ao->bounds [i].length + sp [i].data.i -
655 ao->bounds [i].lower_bound;
657 } else if (pos >= ao->max_length) {
658 frame->ex = mono_get_exception_index_out_of_range ();
659 FILL_IN_TRACE(frame->ex, frame);
663 esize = mono_array_element_size (ac);
664 ea = mono_array_addr_with_size (ao, esize, pos);
666 frame->retval->data.p = ea;
670 interp_walk_stack (MonoStackWalk func, gpointer user_data)
672 ThreadContext *context = TlsGetValue (thread_context_id);
673 MonoInvocation *frame = context->current_frame;
675 MonoMethodHeader *hd;
678 gboolean managed = FALSE;
679 MonoMethod *method = frame->runtime_method->method;
680 if (!method || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
681 (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
684 hd = ((MonoMethodNormal*)method)->header;
685 il_offset = frame->ip - (const unsigned short *)hd->code;
686 if (!method->wrapper_type)
689 if (func (method, -1, il_offset, managed, user_data))
691 frame = frame->parent;
696 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFunc addr, gboolean string_ctor, ThreadContext *context)
700 MonoInvocation *old_frame = context->current_frame;
701 MonoInvocation *old_env_frame = context->env_frame;
702 jmp_buf *old_env = context->current_env;
705 context->current_frame = old_frame;
706 context->env_frame = old_env_frame;
707 context->current_env = old_env;
708 context->managed_code = 1;
713 context->env_frame = frame;
714 context->current_env = &env;
716 if (frame->runtime_method) {
717 func = frame->runtime_method->func;
719 func = mono_arch_create_trampoline (sig, string_ctor);
722 context->current_frame = frame;
723 context->managed_code = 0;
725 func (addr, &frame->retval->data.p, frame->obj, frame->stack_args);
727 context->managed_code = 1;
728 /* domain can only be changed by native code */
729 context->domain = mono_domain_get ();
731 if (context->abort_thread) {
732 MonoThread *thread = mono_thread_current ();
733 context->abort_thread = 0;
734 thread->abort_exc = mono_get_exception_thread_abort ();
735 frame->ex = thread->abort_exc;
740 stackval_from_data (&mono_defaults.string_class->byval_arg,
741 frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
742 } else if (!MONO_TYPE_ISSTRUCT (sig->ret))
743 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
745 context->current_frame = old_frame;
746 context->env_frame = old_env_frame;
747 context->current_env = old_env;
751 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
753 MonoDelegate *delegate = (MonoDelegate *)this;
755 delegate->method_info = mono_method_get_object (domain, runtime_method->method, NULL);
756 delegate->target = target;
758 if (target && target->vtable->klass == mono_defaults.transparent_proxy_class) {
759 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
760 delegate->method_ptr = mono_interp_get_runtime_method (method);
762 delegate->method_ptr = runtime_method;
768 * runtime specifies that the implementation of the method is automatically
769 * provided by the runtime and is primarily used for the methods of delegates.
772 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
774 MonoMethod *method = frame->runtime_method->method;
775 const char *name = method->name;
776 MonoObject *obj = (MonoObject*)frame->obj;
778 mono_class_init (method->klass);
780 if (obj && mono_object_isinst (obj, mono_defaults.multicastdelegate_class)) {
781 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
782 interp_delegate_ctor (context->domain, obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
787 if (obj && mono_object_isinst (obj, mono_defaults.array_class)) {
788 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
789 ves_array_set (frame);
792 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
793 ves_array_get (frame);
796 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
797 ves_array_element_address (frame);
802 g_error ("Don't know how to exec runtime method %s.%s::%s",
803 method->klass->name_space, method->klass->name,
808 dump_stack (stackval *stack, stackval *sp)
811 GString *str = g_string_new ("");
814 return g_string_free (str, FALSE);
817 g_string_sprintfa (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
820 return g_string_free (str, FALSE);
824 dump_stackval (GString *str, stackval *s, MonoType *type)
826 switch (type->type) {
834 case MONO_TYPE_BOOLEAN:
835 g_string_sprintfa (str, "[%d] ", s->data.i);
837 case MONO_TYPE_STRING:
838 case MONO_TYPE_SZARRAY:
839 case MONO_TYPE_CLASS:
840 case MONO_TYPE_OBJECT:
841 case MONO_TYPE_ARRAY:
845 g_string_sprintfa (str, "[%p] ", s->data.p);
847 case MONO_TYPE_VALUETYPE:
848 if (type->data.klass->enumtype)
849 g_string_sprintfa (str, "[%d] ", s->data.i);
851 g_string_sprintfa (str, "[vt:%p] ", s->data.p);
855 g_string_sprintfa (str, "[%g] ", s->data.f);
860 g_string_sprintfa (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
866 dump_args (MonoInvocation *inv)
868 GString *str = g_string_new ("");
870 MonoMethodSignature *signature = inv->runtime_method->method->signature;
872 if (signature->param_count == 0)
873 return g_string_free (str, FALSE);
875 if (signature->hasthis)
876 g_string_sprintfa (str, "%p ", inv->obj);
878 for (i = 0; i < signature->param_count; ++i)
879 dump_stackval (str, inv->stack_args + i, signature->params [i]);
881 return g_string_free (str, FALSE);
885 dump_retval (MonoInvocation *inv)
887 GString *str = g_string_new ("");
888 MonoType *ret = inv->runtime_method->method->signature->ret;
890 if (ret->type != MONO_TYPE_VOID)
891 dump_stackval (str, inv->retval, ret);
893 return g_string_free (str, FALSE);
897 dump_frame (MonoInvocation *inv)
899 GString *str = g_string_new ("");
902 for (i = 0; inv; inv = inv->parent) {
903 if (inv->runtime_method != NULL) {
904 MonoMethod *method = inv->runtime_method->method;
908 const char * opname = "";
910 gchar *source = NULL;
914 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
915 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
916 MonoMethodHeader *hd = ((MonoMethodNormal *)method)->header;
920 opname = mono_interp_opname [*inv->ip];
921 codep = inv->ip - inv->runtime_method->code;
925 source = mono_debug_source_location_from_il_offset (method, codep, NULL);
928 args = dump_args (inv);
929 name = mono_method_full_name (method, TRUE);
931 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname,
934 g_string_sprintfa (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname,
942 return g_string_free (str, FALSE);
946 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
952 for (i = 0, inv = top; inv; inv = inv->parent)
953 if (inv->runtime_method != NULL)
956 res = mono_array_new (domain, mono_defaults.int_class, 2 * i);
958 for (i = 0, inv = top; inv; inv = inv->parent)
959 if (inv->runtime_method != NULL) {
960 mono_array_set (res, gpointer, i, inv->runtime_method);
962 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
970 #define MYGUINT64_MAX 18446744073709551615ULL
971 #define MYGINT64_MAX 9223372036854775807LL
972 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
974 #define MYGUINT32_MAX 4294967295U
975 #define MYGINT32_MAX 2147483647
976 #define MYGINT32_MIN (-MYGINT32_MAX -1)
978 #define CHECK_ADD_OVERFLOW(a,b) \
979 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
980 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
982 #define CHECK_SUB_OVERFLOW(a,b) \
983 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
984 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
986 #define CHECK_ADD_OVERFLOW_UN(a,b) \
987 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
989 #define CHECK_SUB_OVERFLOW_UN(a,b) \
990 (guint32)(a) < (guint32)(b) ? -1 : 0
992 #define CHECK_ADD_OVERFLOW64(a,b) \
993 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
994 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
996 #define CHECK_SUB_OVERFLOW64(a,b) \
997 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
998 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1000 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1001 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1003 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1004 (guint64)(a) < (guint64)(b) ? -1 : 0
1006 #if SIZEOF_VOID_P == 4
1007 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1008 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1010 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1011 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1014 /* Resolves to TRUE if the operands would overflow */
1015 #define CHECK_MUL_OVERFLOW(a,b) \
1016 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1017 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1018 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1019 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1020 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1021 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1022 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1024 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1025 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1026 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1028 #define CHECK_MUL_OVERFLOW64(a,b) \
1029 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1030 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1031 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1032 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1033 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1034 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1035 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1037 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1038 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1039 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1041 #if SIZEOF_VOID_P == 4
1042 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1043 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1045 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1046 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1050 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
1052 MonoInvocation frame;
1053 ThreadContext *context = TlsGetValue (thread_context_id);
1054 MonoObject *retval = NULL;
1055 MonoMethodSignature *sig = method->signature;
1056 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1057 int i, type, isobject = 0;
1060 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1061 ThreadContext context_struct;
1062 MonoInvocation *old_frame = NULL;
1068 g_warning ("caught it");
1070 *exc = (MonoObject *)frame.ex;
1074 if (context == NULL) {
1075 context = &context_struct;
1076 context_struct.base_frame = &frame;
1077 context_struct.current_frame = NULL;
1078 context_struct.env_frame = &frame;
1079 context_struct.current_env = &env;
1080 context_struct.search_for_handler = 0;
1081 context_struct.managed_code = 0;
1082 context_struct.abort_thread = 0;
1083 TlsSetValue (thread_context_id, context);
1086 old_frame = context->current_frame;
1088 context->domain = mono_domain_get ();
1090 switch (sig->ret->type) {
1091 case MONO_TYPE_VOID:
1093 case MONO_TYPE_STRING:
1094 case MONO_TYPE_OBJECT:
1095 case MONO_TYPE_CLASS:
1096 case MONO_TYPE_ARRAY:
1097 case MONO_TYPE_SZARRAY:
1100 case MONO_TYPE_VALUETYPE:
1101 retval = mono_object_new (context->domain, klass);
1102 ret = ((char*)retval) + sizeof (MonoObject);
1103 if (!sig->ret->data.klass->enumtype)
1104 result.data.vt = ret;
1107 retval = mono_object_new (context->domain, klass);
1108 ret = ((char*)retval) + sizeof (MonoObject);
1112 for (i = 0; i < sig->param_count; ++i) {
1113 if (sig->params [i]->byref) {
1114 args [i].data.p = params [i];
1117 type = sig->params [i]->type;
1122 case MONO_TYPE_BOOLEAN:
1123 args [i].data.i = *(MonoBoolean*)params [i];
1127 case MONO_TYPE_CHAR:
1128 args [i].data.i = *(gint16*)params [i];
1130 #if SIZEOF_VOID_P == 4
1131 case MONO_TYPE_U: /* use VAL_POINTER? */
1136 args [i].data.i = *(gint32*)params [i];
1138 #if SIZEOF_VOID_P == 8
1144 args [i].data.l = *(gint64*)params [i];
1146 case MONO_TYPE_VALUETYPE:
1147 if (sig->params [i]->data.klass->enumtype) {
1148 type = sig->params [i]->data.klass->enum_basetype->type;
1151 args [i].data.p = params [i];
1154 case MONO_TYPE_STRING:
1155 case MONO_TYPE_CLASS:
1156 case MONO_TYPE_ARRAY:
1157 case MONO_TYPE_SZARRAY:
1158 case MONO_TYPE_OBJECT:
1159 args [i].data.p = params [i];
1162 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1166 if (method->klass->valuetype)
1167 /* Unbox the instance, since valuetype methods expect an interior pointer. */
1168 obj = mono_object_unbox (obj);
1170 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1171 method = mono_marshal_get_native_wrapper (method);
1172 INIT_FRAME(&frame,context->current_frame,obj,args,&result,method);
1174 frame.invoke_trap = 1;
1175 context->managed_code = 1;
1176 ves_exec_method_with_context (&frame, context);
1177 context->managed_code = 0;
1178 if (context == &context_struct)
1179 TlsSetValue (thread_context_id, NULL);
1181 context->current_frame = old_frame;
1182 if (frame.ex != NULL) {
1184 *exc = (MonoObject*) frame.ex;
1187 if (context->current_env != NULL) {
1188 context->env_frame->ex = frame.ex;
1189 longjmp(*context->current_env, 1);
1192 printf("dropped exception...\n");
1194 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1196 if (isobject || method->string_ctor)
1197 return result.data.p;
1198 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1202 static CRITICAL_SECTION create_method_pointer_mutex;
1204 static MonoGHashTable *method_pointer_hash = NULL;
1207 mono_create_method_pointer (MonoMethod *method)
1212 EnterCriticalSection (&create_method_pointer_mutex);
1213 if (!method_pointer_hash) {
1214 method_pointer_hash = mono_g_hash_table_new (NULL, NULL);
1216 addr = mono_g_hash_table_lookup (method_pointer_hash, method);
1218 LeaveCriticalSection (&create_method_pointer_mutex);
1223 * If it is a static P/Invoke method, we can just return the pointer
1224 * to the method implementation.
1226 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && method->addr) {
1227 ji = g_new0 (MonoJitInfo, 1);
1228 ji->method = method;
1230 ji->code_start = method->addr;
1232 mono_jit_info_table_add (mono_root_domain, ji);
1234 addr = method->addr;
1237 addr = mono_arch_create_method_pointer (method);
1239 mono_g_hash_table_insert (method_pointer_hash, method, addr);
1240 LeaveCriticalSection (&create_method_pointer_mutex);
1246 static int opcode_counts[512];
1248 #define COUNT_OP(op) opcode_counts[op]++
1250 #define COUNT_OP(op)
1254 #define DUMP_INSTR() \
1255 if (tracing > 1) { \
1257 if (sp > frame->stack) { \
1258 ins = dump_stack (frame->stack, sp); \
1260 ins = g_strdup (""); \
1264 g_print ("(%u) ", GetCurrentThreadId()); \
1265 mono_interp_dis_mintop(rtm->code, ip); \
1266 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1270 #define DUMP_INSTR()
1274 #define USE_COMPUTED_GOTO 1
1276 #if USE_COMPUTED_GOTO
1277 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1278 #define MINT_IN_CASE(x) LAB_ ## x:
1280 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1282 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1284 #define MINT_IN_DEFAULT mint_default:
1286 #define MINT_IN_SWITCH(op) switch (op)
1287 #define MINT_IN_CASE(x) case x:
1288 #define MINT_IN_BREAK break
1289 #define MINT_IN_DEFAULT default:
1293 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1295 MonoInvocation child_frame;
1296 GSList *finally_ips = NULL;
1297 const unsigned short *endfinally_ip = NULL;
1298 #if defined(__GNUC__) && defined (i386)
1299 register const unsigned short *ip asm ("%esi");
1301 register const unsigned short *ip;
1303 register stackval *sp;
1306 gint tracing = global_tracing;
1307 unsigned char *vtalloc;
1310 unsigned char *vt_sp;
1314 #if USE_COMPUTED_GOTO
1315 static void *in_labels[] = {
1316 #define OPDEF(a,b,c,d) \
1318 #include "mintops.def"
1323 frame->ex_handler = NULL;
1325 context->current_frame = frame;
1329 if (!frame->runtime_method->transformed) {
1330 context->managed_code = 0;
1331 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1332 context->managed_code = 1;
1340 rtm = frame->runtime_method;
1341 frame->args = alloca (rtm->alloca_size);
1342 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1345 memset(sp, 0, rtm->stack_size);
1347 vt_sp = (char *)sp + rtm->stack_size;
1351 locals = vt_sp + rtm->vt_stack_size;
1353 child_frame.parent = frame;
1359 * using while (ip < end) may result in a 15% performance drop,
1360 * but it may be useful for debug
1364 /* g_assert (sp >= frame->stack); */
1365 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1367 MINT_IN_SWITCH (*ip) {
1368 MINT_IN_CASE(MINT_INITLOCALS)
1369 memset (locals, 0, rtm->locals_size);
1372 MINT_IN_CASE(MINT_NOP)
1375 MINT_IN_CASE(MINT_BREAK)
1377 G_BREAKPOINT (); /* this is not portable... */
1379 MINT_IN_CASE(MINT_LDNULL)
1384 MINT_IN_CASE(MINT_VTRESULT) {
1385 int ret_size = * (guint16 *)(ip + 1);
1386 char *ret_vt_sp = vt_sp;
1387 vt_sp -= READ32(ip + 2);
1389 memmove (vt_sp, ret_vt_sp, ret_size);
1390 vt_sp += (ret_size + 7) & ~7;
1395 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1396 MINT_IN_CASE(MINT_LDC_I4_M1)
1399 MINT_IN_CASE(MINT_LDC_I4_0)
1402 MINT_IN_CASE(MINT_LDC_I4_1)
1405 MINT_IN_CASE(MINT_LDC_I4_2)
1408 MINT_IN_CASE(MINT_LDC_I4_3)
1411 MINT_IN_CASE(MINT_LDC_I4_4)
1414 MINT_IN_CASE(MINT_LDC_I4_5)
1417 MINT_IN_CASE(MINT_LDC_I4_6)
1420 MINT_IN_CASE(MINT_LDC_I4_7)
1423 MINT_IN_CASE(MINT_LDC_I4_8)
1426 MINT_IN_CASE(MINT_LDC_I4_S)
1427 sp->data.i = *(const short *)(ip + 1);
1431 MINT_IN_CASE(MINT_LDC_I4)
1433 sp->data.i = READ32 (ip);
1437 MINT_IN_CASE(MINT_LDC_I8)
1439 sp->data.l = READ64 (ip);
1443 MINT_IN_CASE(MINT_LDC_R4) {
1447 sp->data.f = * (float *)&val;
1452 MINT_IN_CASE(MINT_LDC_R8)
1453 sp->data.l = READ64 (ip + 1); /* note union usage */
1457 MINT_IN_CASE(MINT_DUP)
1462 MINT_IN_CASE(MINT_POP)
1466 MINT_IN_CASE(MINT_JMP) {
1467 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1468 if (!new_method->transformed) {
1470 frame->ex = mono_interp_transform_method (new_method, context);
1475 if (new_method->alloca_size > rtm->alloca_size)
1476 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1477 rtm = frame->runtime_method = new_method;
1478 vt_sp = (char *)sp + rtm->stack_size;
1482 locals = vt_sp + rtm->vt_stack_size;
1483 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1486 MINT_IN_CASE(MINT_CALLI) {
1487 MonoMethodSignature *csignature;
1488 stackval *endsp = sp;
1492 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1496 child_frame.runtime_method = sp->data.p;
1499 child_frame.retval = sp;
1500 /* decrement by the actual number of args */
1501 sp -= csignature->param_count;
1502 child_frame.stack_args = sp;
1503 if (csignature->hasthis) {
1505 child_frame.obj = sp->data.p;
1507 child_frame.obj = NULL;
1509 if (csignature->hasthis &&
1510 ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1511 child_frame.runtime_method = mono_interp_get_runtime_method (
1512 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1513 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1514 child_frame.runtime_method = mono_interp_get_runtime_method (
1515 mono_marshal_get_native_wrapper (child_frame.runtime_method->method));
1517 ves_exec_method_with_context (&child_frame, context);
1519 context->current_frame = frame;
1521 if (child_frame.ex) {
1523 * An exception occurred, need to run finally, fault and catch handlers..
1525 frame->ex = child_frame.ex;
1526 goto handle_finally;
1529 /* need to handle typedbyref ... */
1530 if (csignature->ret->type != MONO_TYPE_VOID) {
1536 MINT_IN_CASE(MINT_CALLI_NAT) {
1537 MonoMethodSignature *csignature;
1538 stackval *endsp = sp;
1539 unsigned char *code = NULL;
1543 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1548 child_frame.runtime_method = NULL;
1551 child_frame.retval = sp;
1552 /* decrement by the actual number of args */
1553 sp -= csignature->param_count;
1554 child_frame.stack_args = sp;
1555 if (csignature->hasthis) {
1557 child_frame.obj = sp->data.p;
1559 child_frame.obj = NULL;
1561 ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE, context);
1563 context->current_frame = frame;
1565 if (child_frame.ex) {
1567 * An exception occurred, need to run finally, fault and catch handlers..
1569 frame->ex = child_frame.ex;
1570 if (context->search_for_handler) {
1571 context->search_for_handler = 0;
1572 goto handle_exception;
1574 goto handle_finally;
1577 /* need to handle typedbyref ... */
1578 if (csignature->ret->type != MONO_TYPE_VOID) {
1584 MINT_IN_CASE(MINT_CALL) {
1585 stackval *endsp = sp;
1589 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1592 child_frame.retval = sp;
1593 /* decrement by the actual number of args */
1594 sp -= child_frame.runtime_method->param_count;
1595 child_frame.stack_args = sp;
1596 if (child_frame.runtime_method->hasthis) {
1598 child_frame.obj = sp->data.p;
1600 child_frame.obj = NULL;
1602 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype &&
1603 ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1604 child_frame.runtime_method = mono_interp_get_runtime_method (
1605 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1607 ves_exec_method_with_context (&child_frame, context);
1609 context->current_frame = frame;
1611 if (child_frame.ex) {
1613 * An exception occurred, need to run finally, fault and catch handlers..
1615 frame->ex = child_frame.ex;
1616 goto handle_finally;
1619 /* need to handle typedbyref ... */
1624 MINT_IN_CASE(MINT_VCALL) {
1627 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1631 child_frame.retval = sp;
1632 /* decrement by the actual number of args */
1633 sp -= child_frame.runtime_method->param_count;
1634 child_frame.stack_args = sp;
1635 if (child_frame.runtime_method->hasthis) {
1637 child_frame.obj = sp->data.p;
1639 child_frame.obj = NULL;
1642 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype &&
1643 ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1644 child_frame.runtime_method = mono_interp_get_runtime_method (
1645 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1648 ves_exec_method_with_context (&child_frame, context);
1650 context->current_frame = frame;
1652 if (child_frame.ex) {
1654 * An exception occurred, need to run finally, fault and catch handlers..
1656 frame->ex = child_frame.ex;
1657 goto handle_finally;
1661 MINT_IN_CASE(MINT_CALLVIRT) {
1662 stackval *endsp = sp;
1663 MonoObject *this_arg;
1668 token = * (unsigned short *)(ip + 1);
1670 child_frame.runtime_method = rtm->data_items [token];
1672 child_frame.retval = sp;
1674 /* decrement by the actual number of args */
1675 sp -= child_frame.runtime_method->param_count;
1676 child_frame.stack_args = sp;
1678 child_frame.obj = this_arg = sp->data.p;
1680 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1681 child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
1683 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1684 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1687 ves_exec_method_with_context (&child_frame, context);
1689 context->current_frame = frame;
1691 if (child_frame.ex) {
1693 * An exception occurred, need to run finally, fault and catch handlers..
1695 frame->ex = child_frame.ex;
1696 if (context->search_for_handler) {
1697 context->search_for_handler = 0;
1698 goto handle_exception;
1700 goto handle_finally;
1703 /* need to handle typedbyref ... */
1708 MINT_IN_CASE(MINT_VCALLVIRT) {
1709 MonoObject *this_arg;
1714 token = * (unsigned short *)(ip + 1);
1716 child_frame.runtime_method = rtm->data_items [token];
1718 child_frame.retval = sp;
1720 /* decrement by the actual number of args */
1721 sp -= child_frame.runtime_method->param_count;
1722 child_frame.stack_args = sp;
1724 child_frame.obj = this_arg = sp->data.p;
1726 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1727 child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
1729 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1730 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1733 ves_exec_method_with_context (&child_frame, context);
1735 context->current_frame = frame;
1737 if (child_frame.ex) {
1739 * An exception occurred, need to run finally, fault and catch handlers..
1741 frame->ex = child_frame.ex;
1742 if (context->search_for_handler) {
1743 context->search_for_handler = 0;
1744 goto handle_exception;
1746 goto handle_finally;
1750 MINT_IN_CASE(MINT_CALLINT)
1751 ves_pinvoke_method (frame, frame->runtime_method->method->signature, frame->runtime_method->method->addr,
1752 frame->runtime_method->method->string_ctor, context);
1755 goto handle_exception;
1758 MINT_IN_CASE(MINT_CALLRUN)
1759 ves_runtime_method (frame, context);
1762 goto handle_exception;
1765 MINT_IN_CASE(MINT_RET)
1767 *frame->retval = *sp;
1768 if (sp > frame->stack)
1769 g_warning ("ret: more values on stack: %d", sp-frame->stack);
1771 MINT_IN_CASE(MINT_RET_VOID)
1772 if (sp > frame->stack)
1773 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
1775 MINT_IN_CASE(MINT_RET_VT)
1776 i32 = READ32(ip + 1);
1778 memcpy(frame->retval->data.p, sp->data.p, i32);
1779 if (sp > frame->stack)
1780 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
1782 MINT_IN_CASE(MINT_BR_S)
1783 ip += (short) *(ip + 1);
1785 MINT_IN_CASE(MINT_BR)
1786 ip += (gint32) READ32(ip + 1);
1788 #define ZEROP_S(datamem, op) \
1790 if (sp->data.datamem op 0) \
1791 ip += * (gint16 *)(ip + 1); \
1795 #define ZEROP(datamem, op) \
1797 if (sp->data.datamem op 0) \
1798 ip += READ32(ip + 1); \
1802 MINT_IN_CASE(MINT_BRFALSE_I4_S)
1805 MINT_IN_CASE(MINT_BRFALSE_I8_S)
1808 MINT_IN_CASE(MINT_BRFALSE_R8_S)
1811 MINT_IN_CASE(MINT_BRFALSE_I4)
1814 MINT_IN_CASE(MINT_BRFALSE_I8)
1817 MINT_IN_CASE(MINT_BRFALSE_R8)
1820 MINT_IN_CASE(MINT_BRTRUE_I4_S)
1823 MINT_IN_CASE(MINT_BRTRUE_I8_S)
1826 MINT_IN_CASE(MINT_BRTRUE_R8_S)
1829 MINT_IN_CASE(MINT_BRTRUE_I4)
1832 MINT_IN_CASE(MINT_BRTRUE_I8)
1835 MINT_IN_CASE(MINT_BRTRUE_R8)
1838 #define CONDBR_S(cond) \
1841 ip += * (gint16 *)(ip + 1); \
1844 #define BRELOP_S(datamem, op) \
1845 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
1847 #define CONDBR(cond) \
1850 ip += READ32(ip + 1); \
1854 #define BRELOP(datamem, op) \
1855 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
1857 MINT_IN_CASE(MINT_BEQ_I4_S)
1860 MINT_IN_CASE(MINT_BEQ_I8_S)
1863 MINT_IN_CASE(MINT_BEQ_R8_S)
1864 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
1866 MINT_IN_CASE(MINT_BEQ_I4)
1869 MINT_IN_CASE(MINT_BEQ_I8)
1872 MINT_IN_CASE(MINT_BEQ_R8)
1873 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
1875 MINT_IN_CASE(MINT_BGE_I4_S)
1878 MINT_IN_CASE(MINT_BGE_I8_S)
1881 MINT_IN_CASE(MINT_BGE_R8_S)
1882 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
1884 MINT_IN_CASE(MINT_BGE_I4)
1887 MINT_IN_CASE(MINT_BGE_I8)
1890 MINT_IN_CASE(MINT_BGE_R8)
1891 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
1893 MINT_IN_CASE(MINT_BGT_I4_S)
1896 MINT_IN_CASE(MINT_BGT_I8_S)
1899 MINT_IN_CASE(MINT_BGT_R8_S)
1900 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
1902 MINT_IN_CASE(MINT_BGT_I4)
1905 MINT_IN_CASE(MINT_BGT_I8)
1908 MINT_IN_CASE(MINT_BGT_R8)
1909 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
1911 MINT_IN_CASE(MINT_BLT_I4_S)
1914 MINT_IN_CASE(MINT_BLT_I8_S)
1917 MINT_IN_CASE(MINT_BLT_R8_S)
1918 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
1920 MINT_IN_CASE(MINT_BLT_I4)
1923 MINT_IN_CASE(MINT_BLT_I8)
1926 MINT_IN_CASE(MINT_BLT_R8)
1927 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
1929 MINT_IN_CASE(MINT_BLE_I4_S)
1932 MINT_IN_CASE(MINT_BLE_I8_S)
1935 MINT_IN_CASE(MINT_BLE_R8_S)
1936 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
1938 MINT_IN_CASE(MINT_BLE_I4)
1941 MINT_IN_CASE(MINT_BLE_I8)
1944 MINT_IN_CASE(MINT_BLE_R8)
1945 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
1947 MINT_IN_CASE(MINT_BNE_UN_I4_S)
1950 MINT_IN_CASE(MINT_BNE_UN_I8_S)
1953 MINT_IN_CASE(MINT_BNE_UN_R8_S)
1954 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
1956 MINT_IN_CASE(MINT_BNE_UN_I4)
1959 MINT_IN_CASE(MINT_BNE_UN_I8)
1962 MINT_IN_CASE(MINT_BNE_UN_R8)
1963 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
1966 #define BRELOP_S_CAST(datamem, op, type) \
1968 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
1969 ip += * (gint16 *)(ip + 1); \
1973 #define BRELOP_CAST(datamem, op, type) \
1975 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
1976 ip += READ32(ip + 1); \
1980 MINT_IN_CASE(MINT_BGE_UN_I4_S)
1981 BRELOP_S_CAST(i, >=, guint32);
1983 MINT_IN_CASE(MINT_BGE_UN_I8_S)
1984 BRELOP_S_CAST(l, >=, guint64);
1986 MINT_IN_CASE(MINT_BGE_UN_R8_S)
1987 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
1989 MINT_IN_CASE(MINT_BGE_UN_I4)
1990 BRELOP_CAST(i, >=, guint32);
1992 MINT_IN_CASE(MINT_BGE_UN_I8)
1993 BRELOP_CAST(l, >=, guint64);
1995 MINT_IN_CASE(MINT_BGE_UN_R8)
1996 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
1998 MINT_IN_CASE(MINT_BGT_UN_I4_S)
1999 BRELOP_S_CAST(i, >, guint32);
2001 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2002 BRELOP_S_CAST(l, >, guint64);
2004 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2005 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2007 MINT_IN_CASE(MINT_BGT_UN_I4)
2008 BRELOP_CAST(i, >, guint32);
2010 MINT_IN_CASE(MINT_BGT_UN_I8)
2011 BRELOP_CAST(l, >, guint64);
2013 MINT_IN_CASE(MINT_BGT_UN_R8)
2014 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2016 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2017 BRELOP_S_CAST(i, <=, guint32);
2019 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2020 BRELOP_S_CAST(l, <=, guint64);
2022 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2023 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2025 MINT_IN_CASE(MINT_BLE_UN_I4)
2026 BRELOP_CAST(i, <=, guint32);
2028 MINT_IN_CASE(MINT_BLE_UN_I8)
2029 BRELOP_CAST(l, <=, guint64);
2031 MINT_IN_CASE(MINT_BLE_UN_R8)
2032 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2034 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2035 BRELOP_S_CAST(i, <, guint32);
2037 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2038 BRELOP_S_CAST(l, <, guint64);
2040 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2041 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2043 MINT_IN_CASE(MINT_BLT_UN_I4)
2044 BRELOP_CAST(i, <, guint32);
2046 MINT_IN_CASE(MINT_BLT_UN_I8)
2047 BRELOP_CAST(l, <, guint64);
2049 MINT_IN_CASE(MINT_BLT_UN_R8)
2050 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2052 MINT_IN_CASE(MINT_SWITCH) {
2054 const unsigned short *st;
2060 if ((guint32)sp->data.i < n) {
2062 ip += 2 * (guint32)sp->data.i;
2063 offset = READ32 (ip);
2070 MINT_IN_CASE(MINT_LDIND_I1)
2072 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2074 MINT_IN_CASE(MINT_LDIND_U1)
2076 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2078 MINT_IN_CASE(MINT_LDIND_I2)
2080 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2082 MINT_IN_CASE(MINT_LDIND_U2)
2084 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2086 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2087 MINT_IN_CASE(MINT_LDIND_U4)
2089 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2091 MINT_IN_CASE(MINT_LDIND_I8)
2093 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2095 MINT_IN_CASE(MINT_LDIND_I)
2097 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2099 MINT_IN_CASE(MINT_LDIND_R4)
2101 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2103 MINT_IN_CASE(MINT_LDIND_R8)
2105 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2107 MINT_IN_CASE(MINT_LDIND_REF)
2109 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2111 MINT_IN_CASE(MINT_STIND_REF)
2114 * (gpointer *) sp->data.p = sp[1].data.p;
2116 MINT_IN_CASE(MINT_STIND_I1)
2119 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2121 MINT_IN_CASE(MINT_STIND_I2)
2124 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2126 MINT_IN_CASE(MINT_STIND_I4)
2129 * (gint32 *) sp->data.p = sp[1].data.i;
2131 MINT_IN_CASE(MINT_STIND_I)
2134 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2136 MINT_IN_CASE(MINT_STIND_I8)
2139 * (gint64 *) sp->data.p = sp[1].data.l;
2141 MINT_IN_CASE(MINT_STIND_R4)
2144 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2146 MINT_IN_CASE(MINT_STIND_R8)
2149 * (double *) sp->data.p = sp[1].data.f;
2151 #define BINOP(datamem, op) \
2153 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2155 MINT_IN_CASE(MINT_ADD_I4)
2158 MINT_IN_CASE(MINT_ADD_I8)
2161 MINT_IN_CASE(MINT_ADD_R8)
2164 MINT_IN_CASE(MINT_ADD1_I4)
2168 MINT_IN_CASE(MINT_SUB_I4)
2171 MINT_IN_CASE(MINT_SUB_I8)
2174 MINT_IN_CASE(MINT_SUB_R8)
2177 MINT_IN_CASE(MINT_SUB1_I4)
2181 MINT_IN_CASE(MINT_MUL_I4)
2184 MINT_IN_CASE(MINT_MUL_I8)
2187 MINT_IN_CASE(MINT_MUL_R8)
2190 MINT_IN_CASE(MINT_DIV_I4)
2191 if (sp [-1].data.i == 0)
2192 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2195 MINT_IN_CASE(MINT_DIV_I8)
2196 if (sp [-1].data.l == 0)
2197 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2200 MINT_IN_CASE(MINT_DIV_R8)
2204 #define BINOP_CAST(datamem, op, type) \
2206 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2208 MINT_IN_CASE(MINT_DIV_UN_I4)
2209 if (sp [-1].data.i == 0)
2210 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2211 BINOP_CAST(i, /, guint32);
2213 MINT_IN_CASE(MINT_DIV_UN_I8)
2214 if (sp [-1].data.l == 0)
2215 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2216 BINOP_CAST(l, /, guint64);
2218 MINT_IN_CASE(MINT_REM_I4)
2219 if (sp [-1].data.i == 0)
2220 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2223 MINT_IN_CASE(MINT_REM_I8)
2224 if (sp [-1].data.l == 0)
2225 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2228 MINT_IN_CASE(MINT_REM_R8)
2229 /* FIXME: what do we actually do here? */
2231 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2234 MINT_IN_CASE(MINT_REM_UN_I4)
2235 if (sp [-1].data.i == 0)
2236 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2237 BINOP_CAST(i, %, guint32);
2239 MINT_IN_CASE(MINT_REM_UN_I8)
2240 if (sp [-1].data.l == 0)
2241 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2242 BINOP_CAST(l, %, guint64);
2244 MINT_IN_CASE(MINT_AND_I4)
2247 MINT_IN_CASE(MINT_AND_I8)
2250 MINT_IN_CASE(MINT_OR_I4)
2253 MINT_IN_CASE(MINT_OR_I8)
2256 MINT_IN_CASE(MINT_XOR_I4)
2259 MINT_IN_CASE(MINT_XOR_I8)
2263 #define SHIFTOP(datamem, op) \
2265 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2268 MINT_IN_CASE(MINT_SHL_I4)
2271 MINT_IN_CASE(MINT_SHL_I8)
2274 MINT_IN_CASE(MINT_SHR_I4)
2277 MINT_IN_CASE(MINT_SHR_I8)
2280 MINT_IN_CASE(MINT_SHR_UN_I4)
2282 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2285 MINT_IN_CASE(MINT_SHR_UN_I8)
2287 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2290 MINT_IN_CASE(MINT_NEG_I4)
2291 sp [-1].data.i = - sp [-1].data.i;
2294 MINT_IN_CASE(MINT_NEG_I8)
2295 sp [-1].data.l = - sp [-1].data.l;
2298 MINT_IN_CASE(MINT_NEG_R8)
2299 sp [-1].data.f = - sp [-1].data.f;
2302 MINT_IN_CASE(MINT_NOT_I4)
2303 sp [-1].data.i = ~ sp [-1].data.i;
2306 MINT_IN_CASE(MINT_NOT_I8)
2307 sp [-1].data.l = ~ sp [-1].data.l;
2310 MINT_IN_CASE(MINT_CONV_I1_I4)
2311 sp [-1].data.i = (gint8)sp [-1].data.i;
2314 MINT_IN_CASE(MINT_CONV_I1_I8)
2315 sp [-1].data.i = (gint8)sp [-1].data.l;
2318 MINT_IN_CASE(MINT_CONV_I1_R8)
2319 sp [-1].data.i = (gint8)sp [-1].data.f;
2322 MINT_IN_CASE(MINT_CONV_U1_I4)
2323 sp [-1].data.i = (guint8)sp [-1].data.i;
2326 MINT_IN_CASE(MINT_CONV_U1_I8)
2327 sp [-1].data.i = (guint8)sp [-1].data.l;
2330 MINT_IN_CASE(MINT_CONV_U1_R8)
2331 sp [-1].data.i = (guint8)sp [-1].data.f;
2334 MINT_IN_CASE(MINT_CONV_I2_I4)
2335 sp [-1].data.i = (gint16)sp [-1].data.i;
2338 MINT_IN_CASE(MINT_CONV_I2_I8)
2339 sp [-1].data.i = (gint16)sp [-1].data.l;
2342 MINT_IN_CASE(MINT_CONV_I2_R8)
2343 sp [-1].data.i = (gint16)sp [-1].data.f;
2346 MINT_IN_CASE(MINT_CONV_U2_I4)
2347 sp [-1].data.i = (guint16)sp [-1].data.i;
2350 MINT_IN_CASE(MINT_CONV_U2_I8)
2351 sp [-1].data.i = (guint16)sp [-1].data.l;
2354 MINT_IN_CASE(MINT_CONV_U2_R8)
2355 sp [-1].data.i = (guint16)sp [-1].data.f;
2358 MINT_IN_CASE(MINT_CONV_I4_R8)
2359 sp [-1].data.i = (gint32)sp [-1].data.f;
2362 MINT_IN_CASE(MINT_CONV_U4_I8)
2363 MINT_IN_CASE(MINT_CONV_I4_I8)
2364 sp [-1].data.i = (gint32)sp [-1].data.l;
2367 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2368 sp [-2].data.i = (gint32)sp [-2].data.l;
2371 MINT_IN_CASE(MINT_CONV_U4_R8)
2372 sp [-1].data.i = (guint32)sp [-1].data.f;
2375 MINT_IN_CASE(MINT_CONV_I8_I4)
2376 sp [-1].data.l = sp [-1].data.i;
2379 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2380 sp [-2].data.l = sp [-2].data.i;
2383 MINT_IN_CASE(MINT_CONV_I8_U4)
2384 sp [-1].data.l = (guint32)sp [-1].data.i;
2387 MINT_IN_CASE(MINT_CONV_I8_R8)
2388 sp [-1].data.l = (gint64)sp [-1].data.f;
2391 MINT_IN_CASE(MINT_CONV_R4_I4)
2392 sp [-1].data.f = (float)sp [-1].data.i;
2395 MINT_IN_CASE(MINT_CONV_R4_I8)
2396 sp [-1].data.f = (float)sp [-1].data.l;
2399 MINT_IN_CASE(MINT_CONV_R4_R8)
2400 sp [-1].data.f = (float)sp [-1].data.f;
2403 MINT_IN_CASE(MINT_CONV_R8_I4)
2404 sp [-1].data.f = (double)sp [-1].data.i;
2407 MINT_IN_CASE(MINT_CONV_R8_I8)
2408 sp [-1].data.f = (double)sp [-1].data.l;
2411 MINT_IN_CASE(MINT_CONV_U8_I4)
2412 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2415 MINT_IN_CASE(MINT_CONV_U8_R8)
2416 sp [-1].data.l = (guint64)sp [-1].data.f;
2420 MINT_IN_CASE(MINT_CPOBJ) {
2423 vtklass = rtm->data_items[READ32 (ip)];
2426 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2430 MINT_IN_CASE(MINT_LDOBJ) {
2433 c = rtm->data_items[* (guint16 *)(ip + 1)];
2435 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2437 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2439 size = mono_class_value_size (c, NULL);
2441 sp [-1].data.p = vt_sp;
2442 memcpy(vt_sp, p, size);
2443 vt_sp += (size + 7) & ~7;
2447 MINT_IN_CASE(MINT_LDSTR)
2448 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2452 MINT_IN_CASE(MINT_NEWOBJ) {
2453 MonoClass *newobj_class;
2454 MonoMethodSignature *csig;
2455 stackval valuetype_this;
2461 token = * (guint16 *)(ip + 1);
2464 child_frame.runtime_method = rtm->data_items [token];
2465 csig = child_frame.runtime_method->method->signature;
2466 newobj_class = child_frame.runtime_method->method->klass;
2467 /*if (profiling_classes) {
2468 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2470 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2474 if (newobj_class->parent == mono_defaults.array_class) {
2475 sp -= csig->param_count;
2476 o = ves_array_create (context->domain, newobj_class, csig, sp);
2477 goto array_constructed;
2481 * First arg is the object.
2483 if (newobj_class->valuetype) {
2484 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2485 child_frame.obj = vt_sp;
2486 valuetype_this.data.p = vt_sp;
2488 memset (&valuetype_this, 0, sizeof (stackval));
2489 child_frame.obj = &valuetype_this;
2492 if (newobj_class != mono_defaults.string_class) {
2493 context->managed_code = 0;
2494 o = mono_object_new (context->domain, newobj_class);
2495 context->managed_code = 1;
2496 if (context->abort_thread) {
2497 MonoThread *thread = mono_thread_current ();
2498 context->abort_thread = 0;
2499 thread->abort_exc = mono_get_exception_thread_abort ();
2500 frame->ex = thread->abort_exc;
2501 goto handle_finally;
2503 child_frame.obj = o;
2505 child_frame.retval = &retval;
2509 if (csig->param_count) {
2510 sp -= csig->param_count;
2511 child_frame.stack_args = sp;
2513 child_frame.stack_args = NULL;
2516 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2518 child_frame.ip = NULL;
2519 child_frame.ex = NULL;
2521 ves_exec_method_with_context (&child_frame, context);
2523 context->current_frame = frame;
2525 if (child_frame.ex) {
2527 * An exception occurred, need to run finally, fault and catch handlers..
2529 frame->ex = child_frame.ex;
2530 goto handle_finally;
2533 * a constructor returns void, but we need to return the object we created
2536 if (newobj_class->valuetype && !newobj_class->enumtype) {
2537 *sp = valuetype_this;
2538 } else if (newobj_class == mono_defaults.string_class) {
2546 MINT_IN_CASE(MINT_CASTCLASS)
2547 c = rtm->data_items [*(guint16 *)(ip + 1)];
2548 if ((o = sp [-1].data.p)) {
2549 if (c->marshalbyref) {
2550 if (!mono_object_isinst_mbyref (o, c))
2551 THROW_EX (mono_get_exception_invalid_cast (), ip);
2553 MonoVTable *vt = o->vtable;
2554 MonoClass *oklass = vt->klass;
2555 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2556 if (c->interface_id > vt->max_interface_id ||
2557 vt->interface_offsets [c->interface_id] == 0) {
2558 THROW_EX (mono_get_exception_invalid_cast (), ip);
2560 } else if (c->rank) {
2561 if (!mono_object_isinst (o, c))
2562 THROW_EX (mono_get_exception_invalid_cast (), ip);
2563 } else if (!mono_class_has_parent (oklass, c)) {
2564 THROW_EX (mono_get_exception_invalid_cast (), ip);
2570 MINT_IN_CASE(MINT_ISINST)
2571 c = rtm->data_items [*(guint16 *)(ip + 1)];
2572 if ((o = sp [-1].data.p)) {
2573 if (c->marshalbyref) {
2574 if (!mono_object_isinst_mbyref (o, c))
2575 sp [-1].data.p = NULL;
2577 MonoVTable *vt = o->vtable;
2578 MonoClass *oklass = vt->klass;
2579 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2580 if (c->interface_id > vt->max_interface_id ||
2581 vt->interface_offsets [c->interface_id] == 0) {
2582 sp [-1].data.p = NULL;
2584 } else if (c->rank) {
2585 if (!mono_object_isinst (o, c))
2586 sp [-1].data.p = NULL;
2587 } else if (!mono_class_has_parent (oklass, c)) {
2588 sp [-1].data.p = NULL;
2594 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2595 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2598 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2599 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2602 MINT_IN_CASE(MINT_UNBOX)
2603 c = rtm->data_items[*(guint16 *)(ip + 1)];
2607 THROW_EX (mono_get_exception_null_reference(), ip);
2609 if (!(mono_object_isinst (o, c) ||
2610 ((o->vtable->klass->rank == 0) &&
2611 (o->vtable->klass->element_class == c->element_class))))
2612 THROW_EX (mono_get_exception_invalid_cast (), ip);
2614 sp [-1].data.p = (char *)o + sizeof (MonoObject);
2617 MINT_IN_CASE(MINT_THROW)
2619 frame->ex_handler = NULL;
2621 sp->data.p = mono_get_exception_null_reference ();
2622 THROW_EX ((MonoException *)sp->data.p, ip);
2624 MINT_IN_CASE(MINT_LDFLDA)
2627 THROW_EX (mono_get_exception_null_reference (), ip);
2628 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2631 MINT_IN_CASE(MINT_CKNULL)
2634 THROW_EX (mono_get_exception_null_reference (), ip);
2638 #define LDFLD(datamem, fieldtype) \
2639 o = sp [-1].data.p; \
2641 THROW_EX (mono_get_exception_null_reference (), ip); \
2642 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2645 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2646 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2647 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2648 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2649 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2650 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2651 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2652 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2653 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2654 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2656 MINT_IN_CASE(MINT_LDFLD_VT)
2659 THROW_EX (mono_get_exception_null_reference (), ip);
2660 i32 = READ32(ip + 2);
2661 sp [-1].data.p = vt_sp;
2662 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2663 vt_sp += (i32 + 7) & ~7;
2667 MINT_IN_CASE(MINT_LDRMFLD) {
2668 MonoClassField *field;
2673 THROW_EX (mono_get_exception_null_reference (), ip);
2674 field = rtm->data_items[* (guint16 *)(ip + 1)];
2676 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2677 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2678 addr = mono_load_remote_field (o, klass, field, NULL);
2680 addr = (char*)o + field->offset;
2683 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2687 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2688 MonoClassField *field;
2693 THROW_EX (mono_get_exception_null_reference (), ip);
2694 field = rtm->data_items[* (guint16 *)(ip + 1)];
2695 i32 = READ32(ip + 2);
2697 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2698 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2699 addr = mono_load_remote_field (o, klass, field, NULL);
2701 addr = (char*)o + field->offset;
2704 sp [-1].data.p = vt_sp;
2705 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2706 vt_sp += (i32 + 7) & ~7;
2707 memcpy(sp [-1].data.p, addr, i32);
2711 #define STFLD(datamem, fieldtype) \
2712 o = sp [-2].data.p; \
2714 THROW_EX (mono_get_exception_null_reference (), ip); \
2716 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2719 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2720 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2721 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2722 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2723 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2724 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2725 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2726 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2727 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2728 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2730 MINT_IN_CASE(MINT_STFLD_VT)
2733 THROW_EX (mono_get_exception_null_reference (), ip);
2734 i32 = READ32(ip + 2);
2736 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2737 vt_sp -= (i32 + 7) & ~7;
2741 MINT_IN_CASE(MINT_STRMFLD) {
2742 MonoClassField *field;
2746 THROW_EX (mono_get_exception_null_reference (), ip);
2748 field = rtm->data_items[* (guint16 *)(ip + 1)];
2751 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2752 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2753 mono_store_remote_field (o, klass, field, &sp [-1].data);
2755 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
2760 MINT_IN_CASE(MINT_STRMFLD_VT) {
2761 MonoClassField *field;
2765 THROW_EX (mono_get_exception_null_reference (), ip);
2766 field = rtm->data_items[* (guint16 *)(ip + 1)];
2767 i32 = READ32(ip + 2);
2770 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2771 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2772 mono_store_remote_field (o, klass, field, &sp [-1].data);
2774 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
2777 vt_sp -= (i32 + 7) & ~7;
2780 MINT_IN_CASE(MINT_LDSFLDA) {
2781 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2782 MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
2785 if (!vt->initialized) {
2787 mono_runtime_class_init (vt);
2791 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2792 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2794 sp->data.p = (char*)(vt->data) + field->offset;
2798 MINT_IN_CASE(MINT_LDSFLD) {
2800 MonoClassField *field;
2803 field = rtm->data_items[*(guint16 *)(ip + 1)];
2804 vt = rtm->data_items [*(guint16 *)(ip + 2)];
2805 if (!vt->initialized) {
2807 mono_runtime_class_init (vt);
2810 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2811 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2813 addr = (char*)(vt->data) + field->offset;
2815 stackval_from_data (field->type, sp, addr, FALSE);
2819 MINT_IN_CASE(MINT_LDSFLD_I4) {
2820 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2821 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
2822 if (!vt->initialized) {
2824 mono_runtime_class_init (vt);
2827 sp->data.i = * (gint32 *)((char*)(vt->data) + field->offset);
2831 MINT_IN_CASE(MINT_LDSFLD_O) {
2832 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2833 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
2834 if (!vt->initialized) {
2836 mono_runtime_class_init (vt);
2839 sp->data.p = * (gpointer *)((char*)(vt->data) + field->offset);
2843 MINT_IN_CASE(MINT_LDSFLD_VT) {
2845 MonoClassField *field;
2850 token = * (guint16 *)(ip + 1);
2851 size = READ32(ip + 2);
2852 field = rtm->data_items[token];
2855 vt = mono_class_vtable (context->domain, field->parent);
2856 if (!vt->initialized) {
2858 mono_runtime_class_init (vt);
2861 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2862 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2864 addr = (char*)(vt->data) + field->offset;
2867 vt_sp += (size + 7) & ~7;
2868 stackval_from_data (field->type, sp, addr, FALSE);
2872 MINT_IN_CASE(MINT_STSFLD) {
2874 MonoClassField *field;
2878 token = * (guint16 *)(ip + 1);
2879 field = rtm->data_items[token];
2883 vt = mono_class_vtable (context->domain, field->parent);
2884 if (!vt->initialized) {
2886 mono_runtime_class_init (vt);
2889 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2890 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2892 addr = (char*)(vt->data) + field->offset;
2894 stackval_to_data (field->type, sp, addr, FALSE);
2897 MINT_IN_CASE(MINT_STSFLD_VT) {
2899 MonoClassField *field;
2904 token = * (guint16 *)(ip + 1);
2905 size = READ32(ip + 2);
2906 field = rtm->data_items[token];
2909 vt = mono_class_vtable (context->domain, field->parent);
2910 if (!vt->initialized) {
2912 mono_runtime_class_init (vt);
2915 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2916 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2918 addr = (char*)(vt->data) + field->offset;
2920 stackval_to_data (field->type, sp, addr, FALSE);
2921 vt_sp -= (size + 7) & ~7;
2924 MINT_IN_CASE(MINT_STOBJ_VT) {
2926 c = rtm->data_items[* (guint16 *)(ip + 1)];
2928 size = mono_class_value_size (c, NULL);
2929 memcpy(sp [-2].data.p, sp [-1].data.p, size);
2930 vt_sp -= (size + 7) & ~7;
2934 MINT_IN_CASE(MINT_STOBJ) {
2936 c = rtm->data_items[* (guint16 *)(ip + 1)];
2938 size = mono_class_value_size (c, NULL);
2939 memcpy(sp [-2].data.p, &sp [-1].data, size);
2943 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
2944 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
2945 THROW_EX (mono_get_exception_overflow (), ip);
2946 sp [-1].data.i = (guint32)sp [-1].data.f;
2949 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
2950 if (sp [-1].data.i < 0)
2951 THROW_EX (mono_get_exception_overflow (), ip);
2952 sp [-1].data.l = sp [-1].data.i;
2955 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
2956 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
2957 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
2958 THROW_EX (mono_get_exception_overflow (), ip);
2959 sp [-1].data.l = (guint64)sp [-1].data.f;
2962 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
2963 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
2964 THROW_EX (mono_get_exception_overflow (), ip);
2965 sp [-1].data.l = (gint64)sp [-1].data.f;
2968 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
2969 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
2970 THROW_EX (mono_get_exception_overflow (), ip);
2971 sp [-1].data.i = (mono_u)sp [-1].data.l;
2974 MINT_IN_CASE(MINT_BOX)
2975 c = rtm->data_items [* (guint16 *)(ip + 1)];
2977 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2978 int size = mono_class_value_size (c, NULL);
2979 sp [-1].data.p = mono_value_box (context->domain, c, sp [-1].data.p);
2980 size = (size + 7) & ~7;
2984 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
2985 sp [-1].data.p = mono_value_box (context->domain, c, &sp [-1]);
2989 MINT_IN_CASE(MINT_NEWARR)
2990 sp [-1].data.p = (MonoObject*) mono_array_new (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i);
2992 /*if (profiling_classes) {
2993 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
2995 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
2999 MINT_IN_CASE(MINT_LDLEN)
3002 THROW_EX (mono_get_exception_null_reference (), ip);
3003 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3006 MINT_IN_CASE(MINT_GETCHR) {
3010 THROW_EX (mono_get_exception_null_reference (), ip);
3011 i32 = sp [-1].data.i;
3012 if (i32 < 0 || i32 >= mono_string_length (s))
3013 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3015 sp [-1].data.i = mono_string_chars(s)[i32];
3019 MINT_IN_CASE(MINT_STRLEN)
3021 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3023 MINT_IN_CASE(MINT_ARRAY_RANK)
3026 THROW_EX (mono_get_exception_null_reference (), ip);
3027 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3030 MINT_IN_CASE(MINT_LDELEMA) {
3034 /*token = READ32 (ip)*/;
3040 aindex = sp [1].data.i;
3041 if (aindex >= mono_array_length ((MonoArray *) o))
3042 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3044 /* check the array element corresponds to token */
3045 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3047 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3051 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3052 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3053 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3054 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3055 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3056 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3057 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3058 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3059 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3060 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3061 MINT_IN_CASE(MINT_LDELEM_REF) {
3069 THROW_EX (mono_get_exception_null_reference (), ip);
3071 aindex = sp [1].data.i;
3072 if (aindex >= mono_array_length (o))
3073 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3076 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3079 case MINT_LDELEM_I1:
3080 sp [0].data.i = mono_array_get (o, gint8, aindex);
3082 case MINT_LDELEM_U1:
3083 sp [0].data.i = mono_array_get (o, guint8, aindex);
3085 case MINT_LDELEM_I2:
3086 sp [0].data.i = mono_array_get (o, gint16, aindex);
3088 case MINT_LDELEM_U2:
3089 sp [0].data.i = mono_array_get (o, guint16, aindex);
3092 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3094 case MINT_LDELEM_I4:
3095 sp [0].data.i = mono_array_get (o, gint32, aindex);
3097 case MINT_LDELEM_U4:
3098 sp [0].data.i = mono_array_get (o, guint32, aindex);
3100 case MINT_LDELEM_I8:
3101 sp [0].data.l = mono_array_get (o, guint64, aindex);
3103 case MINT_LDELEM_R4:
3104 sp [0].data.f = mono_array_get (o, float, aindex);
3106 case MINT_LDELEM_R8:
3107 sp [0].data.f = mono_array_get (o, double, aindex);
3109 case MINT_LDELEM_REF:
3110 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3120 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3121 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3122 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3123 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3124 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3125 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3126 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3127 MINT_IN_CASE(MINT_STELEM_REF) {
3134 THROW_EX (mono_get_exception_null_reference (), ip);
3136 aindex = sp [1].data.i;
3137 if (aindex >= mono_array_length ((MonoArray *)o))
3138 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3142 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3144 case MINT_STELEM_I1:
3145 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3147 case MINT_STELEM_I2:
3148 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3150 case MINT_STELEM_I4:
3151 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3153 case MINT_STELEM_I8:
3154 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3156 case MINT_STELEM_R4:
3157 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3159 case MINT_STELEM_R8:
3160 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3162 case MINT_STELEM_REF:
3163 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3164 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3165 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3174 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3175 if (sp [-1].data.i < 0)
3176 THROW_EX (mono_get_exception_overflow (), ip);
3179 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3180 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3181 THROW_EX (mono_get_exception_overflow (), ip);
3182 sp [-1].data.i = (gint32) sp [-1].data.l;
3185 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3186 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3187 THROW_EX (mono_get_exception_overflow (), ip);
3188 sp [-1].data.i = (gint32) sp [-1].data.f;
3191 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3192 if (sp [-1].data.i < 0)
3193 THROW_EX (mono_get_exception_overflow (), ip);
3196 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3197 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3198 THROW_EX (mono_get_exception_overflow (), ip);
3199 sp [-1].data.i = (guint32) sp [-1].data.l;
3202 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3203 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3204 THROW_EX (mono_get_exception_overflow (), ip);
3205 sp [-1].data.i = (guint32) sp [-1].data.f;
3208 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3209 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3210 THROW_EX (mono_get_exception_overflow (), ip);
3213 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3214 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3215 THROW_EX (mono_get_exception_overflow (), ip);
3216 sp [-1].data.i = (gint16) sp [-1].data.l;
3219 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3220 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3221 THROW_EX (mono_get_exception_overflow (), ip);
3222 sp [-1].data.i = (gint16) sp [-1].data.f;
3225 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3226 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3227 THROW_EX (mono_get_exception_overflow (), ip);
3230 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3231 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3232 THROW_EX (mono_get_exception_overflow (), ip);
3233 sp [-1].data.i = (guint16) sp [-1].data.l;
3236 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3237 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3238 THROW_EX (mono_get_exception_overflow (), ip);
3239 sp [-1].data.i = (guint16) sp [-1].data.f;
3242 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3243 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3244 THROW_EX (mono_get_exception_overflow (), ip);
3247 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3248 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3249 THROW_EX (mono_get_exception_overflow (), ip);
3250 sp [-1].data.i = (gint8) sp [-1].data.l;
3253 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3254 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3255 THROW_EX (mono_get_exception_overflow (), ip);
3256 sp [-1].data.i = (gint8) sp [-1].data.f;
3259 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3260 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3261 THROW_EX (mono_get_exception_overflow (), ip);
3264 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3265 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3266 THROW_EX (mono_get_exception_overflow (), ip);
3267 sp [-1].data.i = (guint8) sp [-1].data.l;
3270 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3271 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3272 THROW_EX (mono_get_exception_overflow (), ip);
3273 sp [-1].data.i = (guint8) sp [-1].data.f;
3277 MINT_IN_CASE(MINT_LDELEM)
3278 MINT_IN_CASE(MINT_STELEM)
3279 MINT_IN_CASE(MINT_UNBOX_ANY)
3281 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3283 MINT_IN_CASE(MINT_CKFINITE)
3284 if (!finite(sp [-1].data.f))
3285 THROW_EX (mono_get_exception_arithmetic (), ip);
3289 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3291 MINT_IN_CASE(MINT_LDTOKEN)
3294 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3298 MINT_IN_CASE(MINT_ADD_OVF_I4)
3299 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3300 THROW_EX (mono_get_exception_overflow (), ip);
3303 MINT_IN_CASE(MINT_ADD_OVF_I8)
3304 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3305 THROW_EX (mono_get_exception_overflow (), ip);
3308 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3309 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3310 THROW_EX (mono_get_exception_overflow (), ip);
3311 BINOP_CAST(i, +, guint32);
3313 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3314 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3315 THROW_EX (mono_get_exception_overflow (), ip);
3316 BINOP_CAST(l, +, guint64);
3318 MINT_IN_CASE(MINT_MUL_OVF_I4)
3319 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3320 THROW_EX (mono_get_exception_overflow (), ip);
3323 MINT_IN_CASE(MINT_MUL_OVF_I8)
3324 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3325 THROW_EX (mono_get_exception_overflow (), ip);
3328 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3329 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3330 THROW_EX (mono_get_exception_overflow (), ip);
3331 BINOP_CAST(i, *, guint32);
3333 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3334 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3335 THROW_EX (mono_get_exception_overflow (), ip);
3336 BINOP_CAST(l, *, guint64);
3338 MINT_IN_CASE(MINT_SUB_OVF_I4)
3339 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3340 THROW_EX (mono_get_exception_overflow (), ip);
3343 MINT_IN_CASE(MINT_SUB_OVF_I8)
3344 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3345 THROW_EX (mono_get_exception_overflow (), ip);
3348 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3349 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3350 THROW_EX (mono_get_exception_overflow (), ip);
3351 BINOP_CAST(i, -, guint32);
3353 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3354 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3355 THROW_EX (mono_get_exception_overflow (), ip);
3356 BINOP_CAST(l, -, guint64);
3358 MINT_IN_CASE(MINT_ENDFINALLY)
3360 ip = finally_ips->data;
3361 finally_ips = g_slist_remove (finally_ips, ip);
3368 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3369 MINT_IN_CASE(MINT_LEAVE_S)
3370 while (sp > frame->stack) {
3374 if (*ip == MINT_LEAVE_S) {
3375 ip += (short) *(ip + 1);
3377 ip += (gint32) READ32 (ip + 1);
3380 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3381 frame->ex_handler = NULL;
3384 goto handle_finally;
3386 MINT_IN_CASE(MINT_MONO_CONV1)
3389 case MONO_MARSHAL_CONV_STR_LPWSTR:
3390 sp [-1].data.p = mono_string_to_utf16 (sp [-1].data.p);
3392 case MONO_MARSHAL_CONV_LPSTR_STR:
3393 sp [-1].data.p = mono_string_new_wrapper (sp [-1].data.p);
3395 case MONO_MARSHAL_CONV_STR_LPTSTR:
3396 case MONO_MARSHAL_CONV_STR_LPSTR:
3397 sp [-1].data.p = mono_string_to_utf8 (sp [-1].data.p);
3399 case MONO_MARSHAL_CONV_STR_BSTR:
3400 sp [-1].data.p = mono_string_to_bstr (sp [-1].data.p);
3402 case MONO_MARSHAL_CONV_STR_TBSTR:
3403 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3404 sp [-1].data.p = mono_string_to_ansibstr (sp [-1].data.p);
3406 case MONO_MARSHAL_CONV_SB_LPSTR:
3407 case MONO_MARSHAL_CONV_SB_LPTSTR:
3408 sp [-1].data.p = mono_string_builder_to_utf8 (sp [-1].data.p);
3410 case MONO_MARSHAL_CONV_SB_LPWSTR:
3411 sp [-1].data.p = mono_string_builder_to_utf16 (sp [-1].data.p);
3413 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3414 sp [-1].data.p = mono_array_to_savearray (sp [-1].data.p);
3416 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3417 sp [-1].data.p = mono_array_to_lparray (sp [-1].data.p);
3419 case MONO_MARSHAL_CONV_DEL_FTN:
3420 sp [-1].data.p = mono_delegate_to_ftnptr (sp [-1].data.p);
3422 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3423 sp [-1].data.p = mono_marshal_string_array (sp [-1].data.p);
3425 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
3426 sp [-1].data.p = mono_marshal_string_array_to_unicode (sp [-1].data.p);
3428 case MONO_MARSHAL_CONV_LPWSTR_STR:
3429 sp [-1].data.p = mono_string_from_utf16 (sp [-1].data.p);
3432 fprintf(stderr, "MINT_MONO_CONV1 %d", ip[-1]);
3433 g_assert_not_reached ();
3436 MINT_IN_CASE(MINT_MONO_CONV2)
3440 case MONO_MARSHAL_CONV_LPSTR_SB:
3441 case MONO_MARSHAL_CONV_LPTSTR_SB:
3442 mono_string_utf8_to_builder (sp [0].data.p, sp [1].data.p);
3444 case MONO_MARSHAL_CONV_LPWSTR_SB:
3445 mono_string_utf16_to_builder (sp [0].data.p, sp [1].data.p);
3447 case MONO_MARSHAL_FREE_ARRAY:
3448 mono_marshal_free_array (sp [0].data.p, sp [1].data.i);
3451 g_assert_not_reached ();
3454 MINT_IN_CASE(MINT_MONO_CONV3)
3458 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3459 mono_string_to_byvalstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3461 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3462 mono_string_to_byvalwstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3465 g_assert_not_reached ();
3468 MINT_IN_CASE(MINT_MONO_LDPTR)
3469 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3473 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3474 sp->data.p = mono_object_new (context->domain, rtm->data_items [*(guint16 *)(ip + 1)]);
3478 MINT_IN_CASE(MINT_MONO_FREE)
3481 g_free (sp->data.p);
3483 MINT_IN_CASE(MINT_MONO_RETOBJ)
3486 stackval_from_data (frame->runtime_method->method->signature->ret, frame->retval, sp->data.p, frame->runtime_method->method->signature->pinvoke);
3487 if (sp > frame->stack)
3488 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3491 #define RELOP(datamem, op) \
3493 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3495 MINT_IN_CASE(MINT_CEQ_I4)
3498 MINT_IN_CASE(MINT_CEQ0_I4)
3499 sp [-1].data.i = (sp [-1].data.i == 0);
3502 MINT_IN_CASE(MINT_CEQ_I8)
3505 MINT_IN_CASE(MINT_CEQ_R8)
3507 if (isunordered (sp [-1].data.f, sp [0].data.f))
3510 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3513 MINT_IN_CASE(MINT_CGT_I4)
3516 MINT_IN_CASE(MINT_CGT_I8)
3519 MINT_IN_CASE(MINT_CGT_R8)
3521 if (isunordered (sp [-1].data.f, sp [0].data.f))
3524 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3528 #define RELOP_CAST(datamem, op, type) \
3530 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3533 MINT_IN_CASE(MINT_CGT_UN_I4)
3534 RELOP_CAST(i, >, guint32);
3536 MINT_IN_CASE(MINT_CGT_UN_I8)
3537 RELOP_CAST(l, >, guint64);
3539 MINT_IN_CASE(MINT_CGT_UN_R8)
3541 if (isunordered (sp [-1].data.f, sp [0].data.f))
3544 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3547 MINT_IN_CASE(MINT_CLT_I4)
3550 MINT_IN_CASE(MINT_CLT_I8)
3553 MINT_IN_CASE(MINT_CLT_R8)
3555 if (isunordered (sp [-1].data.f, sp [0].data.f))
3558 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3561 MINT_IN_CASE(MINT_CLT_UN_I4)
3562 RELOP_CAST(i, <, guint32);
3564 MINT_IN_CASE(MINT_CLT_UN_I8)
3565 RELOP_CAST(l, <, guint64);
3567 MINT_IN_CASE(MINT_CLT_UN_R8)
3569 if (isunordered (sp [-1].data.f, sp [0].data.f))
3572 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3575 MINT_IN_CASE(MINT_LDFTN) {
3576 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3581 MINT_IN_CASE(MINT_LDVIRTFTN) {
3582 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3586 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3588 sp->data.p = get_virtual_method (m, sp->data.p);
3593 MINT_IN_CASE(MINT_LDTHIS)
3594 sp->data.p = frame->obj;
3598 MINT_IN_CASE(MINT_STTHIS)
3600 frame->obj = sp->data.p;
3603 MINT_IN_CASE(MINT_LDTHISA)
3604 sp->data.p = &frame->obj;
3609 #define LDARG(datamem, argtype) \
3610 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3614 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3615 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3616 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3617 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3618 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3619 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3620 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3621 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3622 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3623 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3625 MINT_IN_CASE(MINT_LDARG_VT)
3627 i32 = READ32(ip + 2);
3628 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3629 vt_sp += (i32 + 7) & ~7;
3634 #define STARG(datamem, argtype) \
3636 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3639 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3640 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3641 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3642 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3643 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3644 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3645 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3646 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3647 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3648 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3650 MINT_IN_CASE(MINT_STARG_VT)
3651 i32 = READ32(ip + 2);
3653 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3654 vt_sp -= (i32 + 7) & ~7;
3658 #define STINARG(datamem, argtype) \
3660 int n = * (guint16 *)(ip + 1); \
3661 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3665 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3666 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3667 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3668 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3669 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3670 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3671 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3672 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3673 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3674 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3676 MINT_IN_CASE(MINT_STINARG_VT) {
3677 int n = * (guint16 *)(ip + 1);
3678 i32 = READ32(ip + 2);
3679 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3684 MINT_IN_CASE(MINT_LDARGA)
3685 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3690 #define LDLOC(datamem, argtype) \
3691 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3695 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3696 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3697 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3698 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3699 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3700 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3701 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3702 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3703 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3704 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3706 MINT_IN_CASE(MINT_LDLOC_VT)
3708 i32 = READ32(ip + 2);
3709 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3710 vt_sp += (i32 + 7) & ~7;
3715 MINT_IN_CASE(MINT_LDLOCA_S)
3716 sp->data.p = locals + * (guint16 *)(ip + 1);
3721 #define STLOC(datamem, argtype) \
3723 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3726 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3727 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3728 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3729 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3730 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3731 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3732 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3733 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3734 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3735 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3737 #define STLOC_NP(datamem, argtype) \
3738 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3741 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3742 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3744 MINT_IN_CASE(MINT_STLOC_VT)
3745 i32 = READ32(ip + 2);
3747 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3748 vt_sp -= (i32 + 7) & ~7;
3752 MINT_IN_CASE(MINT_LOCALLOC)
3753 if (sp != frame->stack + 1) /*FIX?*/
3754 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3755 sp [-1].data.p = alloca (sp [-1].data.i);
3759 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3761 MINT_IN_CASE(MINT_INITOBJ)
3763 memset (sp->data.vt, 0, READ32(ip + 1));
3766 MINT_IN_CASE(MINT_CPBLK)
3768 if (!sp [0].data.p || !sp [1].data.p)
3769 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3771 /* FIXME: value and size may be int64... */
3772 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3775 MINT_IN_CASE(MINT_CONSTRAINED_) {
3777 /* FIXME: implement */
3779 token = READ32 (ip);
3784 MINT_IN_CASE(MINT_INITBLK)
3787 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3789 /* FIXME: value and size may be int64... */
3790 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3793 MINT_IN_CASE(MINT_NO_)
3794 /* FIXME: implement */
3798 MINT_IN_CASE(MINT_RETHROW)
3800 * need to clarify what this should actually do:
3801 * start the search from the last found handler in
3802 * this method or continue in the caller or what.
3803 * Also, do we need to run finally/fault handlers after a retrow?
3804 * Well, this implementation will follow the usual search
3805 * for an handler, considering the current ip as throw spot.
3806 * We need to NULL frame->ex_handler for the later code to
3807 * actually run the new found handler.
3809 frame->ex_handler = NULL;
3810 THROW_EX (frame->ex, ip - 1);
3813 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3814 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3818 g_assert_not_reached ();
3820 * Exception handling code.
3821 * The exception object is stored in frame->ex.
3828 MonoInvocation *inv;
3829 MonoExceptionClause *clause;
3835 g_print ("* Handling exception '%s' at IL_%04x\n",
3836 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
3837 rtm == NULL ? 0 : frame->ip - rtm->code);
3839 if (die_on_exception)
3842 for (inv = frame; inv; inv = inv->parent) {
3844 if (inv->runtime_method == NULL)
3846 method = inv->runtime_method->method;
3847 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3849 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3851 if (inv->ip == NULL)
3853 ip_offset = inv->ip - inv->runtime_method->code;
3854 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
3855 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3856 clause = &inv->runtime_method->clauses [i];
3857 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3858 if (!clause->flags) {
3859 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (method->klass->image, clause->token_or_filter))) {
3861 * OK, we found an handler, now we need to execute the finally
3862 * and fault blocks before branching to the handler code.
3864 inv->ex_handler = clause;
3867 g_print ("* Found handler at '%s'\n", method->name);
3869 goto handle_finally;
3872 /* FIXME: handle filter clauses */
3879 * If we get here, no handler was found: print a stack trace.
3881 for (inv = frame; inv; inv = inv->parent) {
3882 if (inv->invoke_trap)
3883 goto handle_finally;
3886 ex_obj = (MonoObject*)frame->ex;
3887 mono_unhandled_exception (ex_obj);
3894 MonoExceptionClause *clause;
3895 GSList *old_list = finally_ips;
3896 MonoMethod *method = frame->runtime_method->method;
3897 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
3901 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
3903 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3904 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
3907 ip_offset = frame->ip - rtm->code;
3909 if (endfinally_ip != NULL)
3910 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
3911 for (i = 0; i < header->num_clauses; ++i)
3912 if (frame->ex_handler == &rtm->clauses [i])
3916 clause = &rtm->clauses [i];
3917 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
3918 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
3919 ip = rtm->code + clause->handler_offset;
3920 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
3923 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
3929 endfinally_ip = NULL;
3931 if (old_list != finally_ips && finally_ips) {
3932 ip = finally_ips->data;
3933 finally_ips = g_slist_remove (finally_ips, ip);
3934 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
3939 * If an exception is set, we need to execute the fault handler, too,
3940 * otherwise, we continue normally.
3950 MonoExceptionClause *clause;
3951 MonoMethodHeader *header = ((MonoMethodNormal *)frame->runtime_method->method)->header;
3955 g_print ("* Handle fault\n");
3957 ip_offset = frame->ip - rtm->code;
3958 for (i = 0; i < header->num_clauses; ++i) {
3959 clause = &rtm->clauses [i];
3960 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3961 ip = rtm->code + clause->handler_offset;
3964 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
3970 * If the handler for the exception was found in this method, we jump
3971 * to it right away, otherwise we return and let the caller run
3972 * the finally, fault and catch blocks.
3973 * This same code should be present in the endfault opcode, but it
3974 * is corrently not assigned in the ECMA specs: LAMESPEC.
3976 if (frame->ex_handler) {
3979 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
3981 ip = rtm->code + frame->ex_handler->handler_offset;
3983 vt_sp = (char *)sp + rtm->stack_size;
3984 sp->data.p = frame->ex;
3995 ves_exec_method (MonoInvocation *frame)
3997 ThreadContext *context = TlsGetValue (thread_context_id);
3998 ThreadContext context_struct;
4004 mono_unhandled_exception ((MonoObject*)frame->ex);
4007 if (context == NULL) {
4008 context = &context_struct;
4009 context_struct.domain = mono_domain_get ();
4010 context_struct.base_frame = frame;
4011 context_struct.current_frame = NULL;
4012 context_struct.env_frame = frame;
4013 context_struct.current_env = &env;
4014 context_struct.search_for_handler = 0;
4015 context_struct.managed_code = 0;
4016 context_struct.abort_thread = 0;
4017 TlsSetValue (thread_context_id, context);
4020 frame->parent = context->current_frame;
4021 frame->runtime_method = mono_interp_get_runtime_method (frame->method);
4022 context->managed_code = 1;
4023 ves_exec_method_with_context(frame, context);
4024 context->managed_code = 0;
4026 if (context != &context_struct && context->current_env) {
4027 context->env_frame->ex = frame->ex;
4028 longjmp (*context->current_env, 1);
4031 mono_unhandled_exception ((MonoObject*)frame->ex);
4033 if (context->base_frame == frame)
4034 TlsSetValue (thread_context_id, NULL);
4036 context->current_frame = frame->parent;
4040 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4042 MonoImage *image = assembly->image;
4043 MonoCLIImageInfo *iinfo;
4045 MonoObject *exc = NULL;
4048 iinfo = image->image_info;
4049 method = mono_get_method (image, iinfo->cli_cli_header.ch_entry_point, NULL);
4051 g_error ("No entry point method found in %s", image->name);
4053 rval = mono_runtime_run_main (method, argc, argv, &exc);
4055 mono_unhandled_exception (exc);
4064 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4065 "Usage is: mint [options] executable args...\n\n", VERSION);
4067 "Runtime Debugging:\n"
4072 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4075 " --traceclassinit\n"
4078 " --debug method_name\n"
4084 " --config filename load the specified config file instead of the default\n"
4085 " --workers n maximum number of worker threads\n"
4092 test_load_class (MonoImage* image)
4094 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
4098 for (i = 1; i <= t->rows; ++i) {
4099 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4100 mono_class_init (klass);
4106 add_signal_handler (int signo, void (*handler)(int))
4108 #ifdef PLATFORM_WIN32
4109 signal (signo, handler);
4111 struct sigaction sa;
4113 sa.sa_handler = handler;
4114 sigemptyset (&sa.sa_mask);
4117 g_assert (sigaction (signo, &sa, NULL) != -1);
4122 segv_handler (int signum)
4124 ThreadContext *context = TlsGetValue (thread_context_id);
4125 MonoException *segv_exception;
4127 if (context == NULL)
4129 segv_exception = mono_get_exception_null_reference ();
4130 segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4131 mono_raise_exception (segv_exception);
4136 quit_handler (int signum)
4138 ThreadContext *context = TlsGetValue (thread_context_id);
4139 MonoException *quit_exception;
4141 if (context == NULL)
4143 quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4144 mono_raise_exception (quit_exception);
4148 abrt_handler (int signum)
4150 ThreadContext *context = TlsGetValue (thread_context_id);
4151 MonoException *abrt_exception;
4153 if (context == NULL)
4155 abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4156 mono_raise_exception (abrt_exception);
4160 thread_abort_handler (int signum)
4162 ThreadContext *context = TlsGetValue (thread_context_id);
4165 if (context == NULL)
4168 if (context->managed_code) {
4169 thread = mono_thread_current ();
4170 thread->abort_exc = mono_get_exception_thread_abort ();
4171 mono_raise_exception (thread->abort_exc);
4173 context->abort_thread = 1;
4177 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4178 MonoReflectionMethod **method,
4179 gint32 *iloffset, gint32 *native_offset,
4180 MonoString **file, gint32 *line, gint32 *column)
4182 ThreadContext *context = TlsGetValue (thread_context_id);
4183 MonoInvocation *inv = context->current_frame;
4186 for (i = 0; inv && i < skip; inv = inv->parent)
4187 if (inv->runtime_method != NULL)
4195 *method = inv == NULL ? NULL : mono_method_get_object (context->domain, inv->runtime_method->method, NULL);
4198 if (need_file_info) {
4202 *file = mono_string_new (mono_domain_get (), "unknown");
4209 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4211 MonoDomain *domain = mono_domain_get ();
4213 MonoArray *ta = exc->trace_ips;
4217 /* Exception is not thrown yet */
4218 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
4221 len = mono_array_length (ta);
4223 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
4225 for (i = skip; i < len / 2; i++) {
4226 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
4227 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4228 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4231 sf->method = mono_method_get_object (domain, rtm->method, NULL);
4232 sf->native_offset = ip - rtm->code;
4236 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4238 if (need_file_info) {
4241 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4243 sf->filename = filename? mono_string_new (domain, filename): NULL;
4250 mono_array_set (res, gpointer, i, sf);
4257 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
4258 MonoReflectionMethod *info)
4260 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4261 MonoObject *delegate;
4263 mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4265 delegate = mono_object_new (mono_object_domain (type), delegate_class);
4267 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (info->method));
4276 int enable_debugging;
4282 static void main_thread_handler (gpointer user_data)
4284 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4285 MonoAssembly *assembly;
4287 if (main_args->enable_debugging)
4288 mono_debug_init (main_args->domain, MONO_DEBUG_FORMAT_MONO);
4290 assembly = mono_domain_assembly_open (main_args->domain,
4294 fprintf (stderr, "Can not open image %s\n", main_args->file);
4298 if (main_args->enable_debugging)
4299 mono_debug_init_2 (assembly);
4302 test_load_class (assembly->image);
4305 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4310 mono_runtime_install_handlers (void)
4312 add_signal_handler (SIGSEGV, segv_handler);
4313 add_signal_handler (SIGINT, quit_handler);
4314 add_signal_handler (SIGABRT, abrt_handler);
4315 add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4319 quit_function (MonoDomain *domain, gpointer user_data)
4321 mono_profiler_shutdown ();
4323 mono_runtime_cleanup (domain);
4324 mono_domain_free (domain, TRUE);
4329 mono_interp_cleanup(MonoDomain *domain)
4331 quit_function (domain, NULL);
4335 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4337 return ves_exec (domain, assembly, argc, argv);
4341 mono_interp_init(const char *file)
4345 g_set_prgname (file);
4346 mono_set_rootdir ();
4348 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4349 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4351 g_thread_init (NULL);
4353 thread_context_id = TlsAlloc ();
4354 TlsSetValue (thread_context_id, NULL);
4355 InitializeCriticalSection (&runtime_method_lookup_section);
4356 InitializeCriticalSection (&create_method_pointer_mutex);
4358 mono_runtime_install_handlers ();
4359 mono_interp_transform_init ();
4360 mono_install_compile_method (mono_create_method_pointer);
4361 mono_install_runtime_invoke (interp_mono_runtime_invoke);
4362 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4363 mono_install_trampoline (interp_create_trampoline);
4365 mono_install_handler (interp_ex_handler);
4366 mono_install_stack_walk (interp_walk_stack);
4367 mono_runtime_install_cleanup (quit_function);
4369 domain = mono_init (file);
4370 #ifdef __hpux /* generates very big stack frames */
4371 mono_threads_set_default_stacksize(32*1024*1024);
4374 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4375 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4376 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4377 mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4379 mono_runtime_init (domain, NULL, NULL);
4385 mono_main (int argc, char *argv [])
4389 char *file, *config_file = NULL;
4390 int enable_debugging = FALSE;
4391 MainThreadArgs main_args;
4394 setlocale (LC_ALL, "");
4398 for (i = 1; i < argc && argv [i][0] == '-'; i++){
4399 if (strcmp (argv [i], "--trace") == 0)
4401 if (strcmp (argv [i], "--noptr") == 0)
4402 global_no_pointers = 1;
4403 if (strcmp (argv [i], "--traceops") == 0)
4405 if (strcmp (argv [i], "--traceopt") == 0)
4406 ++mono_interp_traceopt;
4407 if (strcmp (argv [i], "--dieonex") == 0) {
4408 die_on_exception = 1;
4409 enable_debugging = 1;
4411 if (strcmp (argv [i], "--print-vtable") == 0)
4412 mono_print_vtable = TRUE;
4413 if (strcmp (argv [i], "--profile") == 0)
4414 mono_profiler_load (NULL);
4415 if (strcmp (argv [i], "--config") == 0)
4416 config_file = argv [++i];
4417 if (strcmp (argv [i], "--workers") == 0) {
4418 mono_max_worker_threads = atoi (argv [++i]);
4419 if (mono_max_worker_threads < 1)
4420 mono_max_worker_threads = 1;
4422 if (strcmp (argv [i], "--help") == 0)
4425 if (strcmp (argv [i], "--debug") == 0) {
4426 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
4428 g_error ("Invalid method name '%s'", argv [i]);
4429 db_methods = g_list_append (db_methods, desc);
4431 if (strcmp (argv [i], "--nested") == 0)
4441 domain = mono_interp_init(file);
4442 mono_config_parse (config_file);
4444 error = mono_check_corlib_version ();
4446 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
4447 fprintf (stderr, "Download a newer corlib at http://www.go-mono.com/daily.\n");
4451 main_args.domain=domain;
4452 main_args.file=file;
4453 main_args.argc=argc-i;
4454 main_args.argv=argv+i;
4455 main_args.enable_debugging=enable_debugging;
4457 mono_runtime_exec_managed_code (domain, main_thread_handler,
4460 quit_function (domain, NULL);
4462 /* Get the return value from System.Environment.ExitCode */
4463 retval=mono_environment_exitcode_get ();
4466 for (i = 0; i < 512; i++)
4467 if (opcode_counts[i] != 0)
4468 printf("%s %d\n", mono_interp_opname[i], opcode_counts[i]);