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 * volatile 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 if (context != &context_struct) {
1069 context->domain = mono_domain_get ();
1070 context->current_frame = old_frame;
1071 context->managed_code = 0;
1073 TlsSetValue (thread_context_id, NULL);
1075 *exc = (MonoObject *)frame.ex;
1079 if (context == NULL) {
1080 context = &context_struct;
1081 context_struct.base_frame = &frame;
1082 context_struct.current_frame = NULL;
1083 context_struct.env_frame = &frame;
1084 context_struct.current_env = &env;
1085 context_struct.search_for_handler = 0;
1086 context_struct.managed_code = 0;
1087 context_struct.abort_thread = 0;
1088 TlsSetValue (thread_context_id, context);
1091 old_frame = context->current_frame;
1093 context->domain = mono_domain_get ();
1095 switch (sig->ret->type) {
1096 case MONO_TYPE_VOID:
1098 case MONO_TYPE_STRING:
1099 case MONO_TYPE_OBJECT:
1100 case MONO_TYPE_CLASS:
1101 case MONO_TYPE_ARRAY:
1102 case MONO_TYPE_SZARRAY:
1105 case MONO_TYPE_VALUETYPE:
1106 retval = mono_object_new (context->domain, klass);
1107 ret = ((char*)retval) + sizeof (MonoObject);
1108 if (!sig->ret->data.klass->enumtype)
1109 result.data.vt = ret;
1112 retval = mono_object_new (context->domain, klass);
1113 ret = ((char*)retval) + sizeof (MonoObject);
1117 for (i = 0; i < sig->param_count; ++i) {
1118 if (sig->params [i]->byref) {
1119 args [i].data.p = params [i];
1122 type = sig->params [i]->type;
1127 case MONO_TYPE_BOOLEAN:
1128 args [i].data.i = *(MonoBoolean*)params [i];
1132 case MONO_TYPE_CHAR:
1133 args [i].data.i = *(gint16*)params [i];
1135 #if SIZEOF_VOID_P == 4
1136 case MONO_TYPE_U: /* use VAL_POINTER? */
1141 args [i].data.i = *(gint32*)params [i];
1143 #if SIZEOF_VOID_P == 8
1149 args [i].data.l = *(gint64*)params [i];
1151 case MONO_TYPE_VALUETYPE:
1152 if (sig->params [i]->data.klass->enumtype) {
1153 type = sig->params [i]->data.klass->enum_basetype->type;
1156 args [i].data.p = params [i];
1159 case MONO_TYPE_STRING:
1160 case MONO_TYPE_CLASS:
1161 case MONO_TYPE_ARRAY:
1162 case MONO_TYPE_SZARRAY:
1163 case MONO_TYPE_OBJECT:
1164 args [i].data.p = params [i];
1167 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1171 if (method->klass->valuetype)
1172 /* Unbox the instance, since valuetype methods expect an interior pointer. */
1173 obj = mono_object_unbox (obj);
1175 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1176 method = mono_marshal_get_native_wrapper (method);
1177 INIT_FRAME(&frame,context->current_frame,obj,args,&result,method);
1179 frame.invoke_trap = 1;
1180 context->managed_code = 1;
1181 ves_exec_method_with_context (&frame, context);
1182 context->managed_code = 0;
1183 if (context == &context_struct)
1184 TlsSetValue (thread_context_id, NULL);
1186 context->current_frame = old_frame;
1187 if (frame.ex != NULL) {
1189 *exc = (MonoObject*) frame.ex;
1192 if (context->current_env != NULL) {
1193 context->env_frame->ex = frame.ex;
1194 longjmp(*context->current_env, 1);
1197 printf("dropped exception...\n");
1199 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1201 if (isobject || method->string_ctor)
1202 return result.data.p;
1203 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1207 static CRITICAL_SECTION create_method_pointer_mutex;
1209 static MonoGHashTable *method_pointer_hash = NULL;
1212 mono_create_method_pointer (MonoMethod *method)
1217 EnterCriticalSection (&create_method_pointer_mutex);
1218 if (!method_pointer_hash) {
1219 method_pointer_hash = mono_g_hash_table_new (NULL, NULL);
1221 addr = mono_g_hash_table_lookup (method_pointer_hash, method);
1223 LeaveCriticalSection (&create_method_pointer_mutex);
1228 * If it is a static P/Invoke method, we can just return the pointer
1229 * to the method implementation.
1231 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && method->addr) {
1232 ji = g_new0 (MonoJitInfo, 1);
1233 ji->method = method;
1235 ji->code_start = method->addr;
1237 mono_jit_info_table_add (mono_root_domain, ji);
1239 addr = method->addr;
1242 addr = mono_arch_create_method_pointer (method);
1244 mono_g_hash_table_insert (method_pointer_hash, method, addr);
1245 LeaveCriticalSection (&create_method_pointer_mutex);
1251 static int opcode_counts[512];
1253 #define COUNT_OP(op) opcode_counts[op]++
1255 #define COUNT_OP(op)
1259 #define DUMP_INSTR() \
1260 if (tracing > 1) { \
1262 if (sp > frame->stack) { \
1263 ins = dump_stack (frame->stack, sp); \
1265 ins = g_strdup (""); \
1269 g_print ("(%u) ", GetCurrentThreadId()); \
1270 mono_interp_dis_mintop(rtm->code, ip); \
1271 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1275 #define DUMP_INSTR()
1279 #define USE_COMPUTED_GOTO 1
1281 #if USE_COMPUTED_GOTO
1282 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1283 #define MINT_IN_CASE(x) LAB_ ## x:
1285 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1287 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1289 #define MINT_IN_DEFAULT mint_default:
1291 #define MINT_IN_SWITCH(op) switch (op)
1292 #define MINT_IN_CASE(x) case x:
1293 #define MINT_IN_BREAK break
1294 #define MINT_IN_DEFAULT default:
1298 * Defining this causes register allocation errors in some versions of gcc:
1299 * error: unable to find a register to spill in class `SIREG'
1301 /* #define MINT_USE_DEDICATED_IP_REG */
1304 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1306 MonoInvocation child_frame;
1307 GSList *finally_ips = NULL;
1308 const unsigned short *endfinally_ip = NULL;
1309 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1310 register const unsigned short *ip asm ("%esi");
1312 register const unsigned short *ip;
1314 register stackval *sp;
1317 gint tracing = global_tracing;
1318 unsigned char *vtalloc;
1321 unsigned char *vt_sp;
1323 MonoObject *o = NULL;
1325 #if USE_COMPUTED_GOTO
1326 static void *in_labels[] = {
1327 #define OPDEF(a,b,c,d) \
1329 #include "mintops.def"
1334 frame->ex_handler = NULL;
1336 context->current_frame = frame;
1340 if (!frame->runtime_method->transformed) {
1341 context->managed_code = 0;
1342 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1343 context->managed_code = 1;
1351 rtm = frame->runtime_method;
1352 frame->args = alloca (rtm->alloca_size);
1353 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1356 memset(sp, 0, rtm->stack_size);
1358 vt_sp = (char *)sp + rtm->stack_size;
1362 locals = vt_sp + rtm->vt_stack_size;
1364 child_frame.parent = frame;
1370 * using while (ip < end) may result in a 15% performance drop,
1371 * but it may be useful for debug
1375 /* g_assert (sp >= frame->stack); */
1376 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1378 MINT_IN_SWITCH (*ip) {
1379 MINT_IN_CASE(MINT_INITLOCALS)
1380 memset (locals, 0, rtm->locals_size);
1383 MINT_IN_CASE(MINT_NOP)
1386 MINT_IN_CASE(MINT_BREAK)
1388 G_BREAKPOINT (); /* this is not portable... */
1390 MINT_IN_CASE(MINT_LDNULL)
1395 MINT_IN_CASE(MINT_VTRESULT) {
1396 int ret_size = * (guint16 *)(ip + 1);
1397 char *ret_vt_sp = vt_sp;
1398 vt_sp -= READ32(ip + 2);
1400 memmove (vt_sp, ret_vt_sp, ret_size);
1401 vt_sp += (ret_size + 7) & ~7;
1406 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1407 MINT_IN_CASE(MINT_LDC_I4_M1)
1410 MINT_IN_CASE(MINT_LDC_I4_0)
1413 MINT_IN_CASE(MINT_LDC_I4_1)
1416 MINT_IN_CASE(MINT_LDC_I4_2)
1419 MINT_IN_CASE(MINT_LDC_I4_3)
1422 MINT_IN_CASE(MINT_LDC_I4_4)
1425 MINT_IN_CASE(MINT_LDC_I4_5)
1428 MINT_IN_CASE(MINT_LDC_I4_6)
1431 MINT_IN_CASE(MINT_LDC_I4_7)
1434 MINT_IN_CASE(MINT_LDC_I4_8)
1437 MINT_IN_CASE(MINT_LDC_I4_S)
1438 sp->data.i = *(const short *)(ip + 1);
1442 MINT_IN_CASE(MINT_LDC_I4)
1444 sp->data.i = READ32 (ip);
1448 MINT_IN_CASE(MINT_LDC_I8)
1450 sp->data.l = READ64 (ip);
1454 MINT_IN_CASE(MINT_LDC_R4) {
1458 sp->data.f = * (float *)&val;
1463 MINT_IN_CASE(MINT_LDC_R8)
1464 sp->data.l = READ64 (ip + 1); /* note union usage */
1468 MINT_IN_CASE(MINT_DUP)
1473 MINT_IN_CASE(MINT_POP)
1477 MINT_IN_CASE(MINT_JMP) {
1478 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1479 if (!new_method->transformed) {
1481 frame->ex = mono_interp_transform_method (new_method, context);
1486 if (new_method->alloca_size > rtm->alloca_size)
1487 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1488 rtm = frame->runtime_method = new_method;
1489 vt_sp = (char *)sp + rtm->stack_size;
1493 locals = vt_sp + rtm->vt_stack_size;
1494 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1497 MINT_IN_CASE(MINT_CALLI) {
1498 MonoMethodSignature *csignature;
1499 stackval *endsp = sp;
1503 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1507 child_frame.runtime_method = sp->data.p;
1510 child_frame.retval = sp;
1511 /* decrement by the actual number of args */
1512 sp -= csignature->param_count;
1513 child_frame.stack_args = sp;
1514 if (csignature->hasthis) {
1516 child_frame.obj = sp->data.p;
1518 child_frame.obj = NULL;
1520 if (csignature->hasthis &&
1521 ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1522 child_frame.runtime_method = mono_interp_get_runtime_method (
1523 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1524 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1525 child_frame.runtime_method = mono_interp_get_runtime_method (
1526 mono_marshal_get_native_wrapper (child_frame.runtime_method->method));
1528 ves_exec_method_with_context (&child_frame, context);
1530 context->current_frame = frame;
1532 if (child_frame.ex) {
1534 * An exception occurred, need to run finally, fault and catch handlers..
1536 frame->ex = child_frame.ex;
1537 goto handle_finally;
1540 /* need to handle typedbyref ... */
1541 if (csignature->ret->type != MONO_TYPE_VOID) {
1547 MINT_IN_CASE(MINT_CALLI_NAT) {
1548 MonoMethodSignature *csignature;
1549 stackval *endsp = sp;
1550 unsigned char *code = NULL;
1554 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1559 child_frame.runtime_method = NULL;
1562 child_frame.retval = sp;
1563 /* decrement by the actual number of args */
1564 sp -= csignature->param_count;
1565 child_frame.stack_args = sp;
1566 if (csignature->hasthis) {
1568 child_frame.obj = sp->data.p;
1570 child_frame.obj = NULL;
1572 ves_pinvoke_method (&child_frame, csignature, (MonoFunc) code, FALSE, context);
1574 context->current_frame = frame;
1576 if (child_frame.ex) {
1578 * An exception occurred, need to run finally, fault and catch handlers..
1580 frame->ex = child_frame.ex;
1581 if (context->search_for_handler) {
1582 context->search_for_handler = 0;
1583 goto handle_exception;
1585 goto handle_finally;
1588 /* need to handle typedbyref ... */
1589 if (csignature->ret->type != MONO_TYPE_VOID) {
1595 MINT_IN_CASE(MINT_CALL) {
1596 stackval *endsp = sp;
1600 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1603 child_frame.retval = sp;
1604 /* decrement by the actual number of args */
1605 sp -= child_frame.runtime_method->param_count;
1606 child_frame.stack_args = sp;
1607 if (child_frame.runtime_method->hasthis) {
1609 child_frame.obj = sp->data.p;
1611 child_frame.obj = NULL;
1613 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype &&
1614 ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1615 child_frame.runtime_method = mono_interp_get_runtime_method (
1616 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1618 ves_exec_method_with_context (&child_frame, context);
1620 context->current_frame = frame;
1622 if (child_frame.ex) {
1624 * An exception occurred, need to run finally, fault and catch handlers..
1626 frame->ex = child_frame.ex;
1627 goto handle_finally;
1630 /* need to handle typedbyref ... */
1635 MINT_IN_CASE(MINT_VCALL) {
1638 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1642 child_frame.retval = sp;
1643 /* decrement by the actual number of args */
1644 sp -= child_frame.runtime_method->param_count;
1645 child_frame.stack_args = sp;
1646 if (child_frame.runtime_method->hasthis) {
1648 child_frame.obj = sp->data.p;
1650 child_frame.obj = NULL;
1653 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype &&
1654 ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1655 child_frame.runtime_method = mono_interp_get_runtime_method (
1656 mono_marshal_get_remoting_invoke (child_frame.runtime_method->method));
1659 ves_exec_method_with_context (&child_frame, context);
1661 context->current_frame = frame;
1663 if (child_frame.ex) {
1665 * An exception occurred, need to run finally, fault and catch handlers..
1667 frame->ex = child_frame.ex;
1668 goto handle_finally;
1672 MINT_IN_CASE(MINT_CALLVIRT) {
1673 stackval *endsp = sp;
1674 MonoObject *this_arg;
1679 token = * (unsigned short *)(ip + 1);
1681 child_frame.runtime_method = rtm->data_items [token];
1683 child_frame.retval = sp;
1685 /* decrement by the actual number of args */
1686 sp -= child_frame.runtime_method->param_count;
1687 child_frame.stack_args = sp;
1689 child_frame.obj = this_arg = sp->data.p;
1691 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1692 child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
1694 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1695 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1698 ves_exec_method_with_context (&child_frame, context);
1700 context->current_frame = frame;
1702 if (child_frame.ex) {
1704 * An exception occurred, need to run finally, fault and catch handlers..
1706 frame->ex = child_frame.ex;
1707 if (context->search_for_handler) {
1708 context->search_for_handler = 0;
1709 goto handle_exception;
1711 goto handle_finally;
1714 /* need to handle typedbyref ... */
1719 MINT_IN_CASE(MINT_VCALLVIRT) {
1720 MonoObject *this_arg;
1725 token = * (unsigned short *)(ip + 1);
1727 child_frame.runtime_method = rtm->data_items [token];
1729 child_frame.retval = sp;
1731 /* decrement by the actual number of args */
1732 sp -= child_frame.runtime_method->param_count;
1733 child_frame.stack_args = sp;
1735 child_frame.obj = this_arg = sp->data.p;
1737 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1738 child_frame.runtime_method = get_virtual_method (child_frame.runtime_method, this_arg);
1740 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1741 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1744 ves_exec_method_with_context (&child_frame, context);
1746 context->current_frame = frame;
1748 if (child_frame.ex) {
1750 * An exception occurred, need to run finally, fault and catch handlers..
1752 frame->ex = child_frame.ex;
1753 if (context->search_for_handler) {
1754 context->search_for_handler = 0;
1755 goto handle_exception;
1757 goto handle_finally;
1761 MINT_IN_CASE(MINT_CALLINT)
1762 ves_pinvoke_method (frame, frame->runtime_method->method->signature, frame->runtime_method->method->addr,
1763 frame->runtime_method->method->string_ctor, context);
1766 goto handle_exception;
1769 MINT_IN_CASE(MINT_CALLRUN)
1770 ves_runtime_method (frame, context);
1773 goto handle_exception;
1776 MINT_IN_CASE(MINT_RET)
1778 *frame->retval = *sp;
1779 if (sp > frame->stack)
1780 g_warning ("ret: more values on stack: %d", sp-frame->stack);
1782 MINT_IN_CASE(MINT_RET_VOID)
1783 if (sp > frame->stack)
1784 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
1786 MINT_IN_CASE(MINT_RET_VT)
1787 i32 = READ32(ip + 1);
1789 memcpy(frame->retval->data.p, sp->data.p, i32);
1790 if (sp > frame->stack)
1791 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
1793 MINT_IN_CASE(MINT_BR_S)
1794 ip += (short) *(ip + 1);
1796 MINT_IN_CASE(MINT_BR)
1797 ip += (gint32) READ32(ip + 1);
1799 #define ZEROP_S(datamem, op) \
1801 if (sp->data.datamem op 0) \
1802 ip += * (gint16 *)(ip + 1); \
1806 #define ZEROP(datamem, op) \
1808 if (sp->data.datamem op 0) \
1809 ip += READ32(ip + 1); \
1813 MINT_IN_CASE(MINT_BRFALSE_I4_S)
1816 MINT_IN_CASE(MINT_BRFALSE_I8_S)
1819 MINT_IN_CASE(MINT_BRFALSE_R8_S)
1822 MINT_IN_CASE(MINT_BRFALSE_I4)
1825 MINT_IN_CASE(MINT_BRFALSE_I8)
1828 MINT_IN_CASE(MINT_BRFALSE_R8)
1831 MINT_IN_CASE(MINT_BRTRUE_I4_S)
1834 MINT_IN_CASE(MINT_BRTRUE_I8_S)
1837 MINT_IN_CASE(MINT_BRTRUE_R8_S)
1840 MINT_IN_CASE(MINT_BRTRUE_I4)
1843 MINT_IN_CASE(MINT_BRTRUE_I8)
1846 MINT_IN_CASE(MINT_BRTRUE_R8)
1849 #define CONDBR_S(cond) \
1852 ip += * (gint16 *)(ip + 1); \
1855 #define BRELOP_S(datamem, op) \
1856 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
1858 #define CONDBR(cond) \
1861 ip += READ32(ip + 1); \
1865 #define BRELOP(datamem, op) \
1866 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
1868 MINT_IN_CASE(MINT_BEQ_I4_S)
1871 MINT_IN_CASE(MINT_BEQ_I8_S)
1874 MINT_IN_CASE(MINT_BEQ_R8_S)
1875 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
1877 MINT_IN_CASE(MINT_BEQ_I4)
1880 MINT_IN_CASE(MINT_BEQ_I8)
1883 MINT_IN_CASE(MINT_BEQ_R8)
1884 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
1886 MINT_IN_CASE(MINT_BGE_I4_S)
1889 MINT_IN_CASE(MINT_BGE_I8_S)
1892 MINT_IN_CASE(MINT_BGE_R8_S)
1893 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
1895 MINT_IN_CASE(MINT_BGE_I4)
1898 MINT_IN_CASE(MINT_BGE_I8)
1901 MINT_IN_CASE(MINT_BGE_R8)
1902 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
1904 MINT_IN_CASE(MINT_BGT_I4_S)
1907 MINT_IN_CASE(MINT_BGT_I8_S)
1910 MINT_IN_CASE(MINT_BGT_R8_S)
1911 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
1913 MINT_IN_CASE(MINT_BGT_I4)
1916 MINT_IN_CASE(MINT_BGT_I8)
1919 MINT_IN_CASE(MINT_BGT_R8)
1920 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
1922 MINT_IN_CASE(MINT_BLT_I4_S)
1925 MINT_IN_CASE(MINT_BLT_I8_S)
1928 MINT_IN_CASE(MINT_BLT_R8_S)
1929 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
1931 MINT_IN_CASE(MINT_BLT_I4)
1934 MINT_IN_CASE(MINT_BLT_I8)
1937 MINT_IN_CASE(MINT_BLT_R8)
1938 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
1940 MINT_IN_CASE(MINT_BLE_I4_S)
1943 MINT_IN_CASE(MINT_BLE_I8_S)
1946 MINT_IN_CASE(MINT_BLE_R8_S)
1947 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
1949 MINT_IN_CASE(MINT_BLE_I4)
1952 MINT_IN_CASE(MINT_BLE_I8)
1955 MINT_IN_CASE(MINT_BLE_R8)
1956 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
1958 MINT_IN_CASE(MINT_BNE_UN_I4_S)
1961 MINT_IN_CASE(MINT_BNE_UN_I8_S)
1964 MINT_IN_CASE(MINT_BNE_UN_R8_S)
1965 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
1967 MINT_IN_CASE(MINT_BNE_UN_I4)
1970 MINT_IN_CASE(MINT_BNE_UN_I8)
1973 MINT_IN_CASE(MINT_BNE_UN_R8)
1974 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
1977 #define BRELOP_S_CAST(datamem, op, type) \
1979 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
1980 ip += * (gint16 *)(ip + 1); \
1984 #define BRELOP_CAST(datamem, op, type) \
1986 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
1987 ip += READ32(ip + 1); \
1991 MINT_IN_CASE(MINT_BGE_UN_I4_S)
1992 BRELOP_S_CAST(i, >=, guint32);
1994 MINT_IN_CASE(MINT_BGE_UN_I8_S)
1995 BRELOP_S_CAST(l, >=, guint64);
1997 MINT_IN_CASE(MINT_BGE_UN_R8_S)
1998 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2000 MINT_IN_CASE(MINT_BGE_UN_I4)
2001 BRELOP_CAST(i, >=, guint32);
2003 MINT_IN_CASE(MINT_BGE_UN_I8)
2004 BRELOP_CAST(l, >=, guint64);
2006 MINT_IN_CASE(MINT_BGE_UN_R8)
2007 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2009 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2010 BRELOP_S_CAST(i, >, guint32);
2012 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2013 BRELOP_S_CAST(l, >, guint64);
2015 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2016 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2018 MINT_IN_CASE(MINT_BGT_UN_I4)
2019 BRELOP_CAST(i, >, guint32);
2021 MINT_IN_CASE(MINT_BGT_UN_I8)
2022 BRELOP_CAST(l, >, guint64);
2024 MINT_IN_CASE(MINT_BGT_UN_R8)
2025 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2027 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2028 BRELOP_S_CAST(i, <=, guint32);
2030 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2031 BRELOP_S_CAST(l, <=, guint64);
2033 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2034 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2036 MINT_IN_CASE(MINT_BLE_UN_I4)
2037 BRELOP_CAST(i, <=, guint32);
2039 MINT_IN_CASE(MINT_BLE_UN_I8)
2040 BRELOP_CAST(l, <=, guint64);
2042 MINT_IN_CASE(MINT_BLE_UN_R8)
2043 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2045 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2046 BRELOP_S_CAST(i, <, guint32);
2048 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2049 BRELOP_S_CAST(l, <, guint64);
2051 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2052 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2054 MINT_IN_CASE(MINT_BLT_UN_I4)
2055 BRELOP_CAST(i, <, guint32);
2057 MINT_IN_CASE(MINT_BLT_UN_I8)
2058 BRELOP_CAST(l, <, guint64);
2060 MINT_IN_CASE(MINT_BLT_UN_R8)
2061 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2063 MINT_IN_CASE(MINT_SWITCH) {
2065 const unsigned short *st;
2071 if ((guint32)sp->data.i < n) {
2073 ip += 2 * (guint32)sp->data.i;
2074 offset = READ32 (ip);
2081 MINT_IN_CASE(MINT_LDIND_I1)
2083 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2085 MINT_IN_CASE(MINT_LDIND_U1)
2087 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2089 MINT_IN_CASE(MINT_LDIND_I2)
2091 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2093 MINT_IN_CASE(MINT_LDIND_U2)
2095 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2097 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2098 MINT_IN_CASE(MINT_LDIND_U4)
2100 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2102 MINT_IN_CASE(MINT_LDIND_I8)
2104 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2106 MINT_IN_CASE(MINT_LDIND_I)
2108 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2110 MINT_IN_CASE(MINT_LDIND_R4)
2112 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2114 MINT_IN_CASE(MINT_LDIND_R8)
2116 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2118 MINT_IN_CASE(MINT_LDIND_REF)
2120 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2122 MINT_IN_CASE(MINT_STIND_REF)
2125 * (gpointer *) sp->data.p = sp[1].data.p;
2127 MINT_IN_CASE(MINT_STIND_I1)
2130 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2132 MINT_IN_CASE(MINT_STIND_I2)
2135 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2137 MINT_IN_CASE(MINT_STIND_I4)
2140 * (gint32 *) sp->data.p = sp[1].data.i;
2142 MINT_IN_CASE(MINT_STIND_I)
2145 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2147 MINT_IN_CASE(MINT_STIND_I8)
2150 * (gint64 *) sp->data.p = sp[1].data.l;
2152 MINT_IN_CASE(MINT_STIND_R4)
2155 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2157 MINT_IN_CASE(MINT_STIND_R8)
2160 * (double *) sp->data.p = sp[1].data.f;
2162 #define BINOP(datamem, op) \
2164 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2166 MINT_IN_CASE(MINT_ADD_I4)
2169 MINT_IN_CASE(MINT_ADD_I8)
2172 MINT_IN_CASE(MINT_ADD_R8)
2175 MINT_IN_CASE(MINT_ADD1_I4)
2179 MINT_IN_CASE(MINT_SUB_I4)
2182 MINT_IN_CASE(MINT_SUB_I8)
2185 MINT_IN_CASE(MINT_SUB_R8)
2188 MINT_IN_CASE(MINT_SUB1_I4)
2192 MINT_IN_CASE(MINT_MUL_I4)
2195 MINT_IN_CASE(MINT_MUL_I8)
2198 MINT_IN_CASE(MINT_MUL_R8)
2201 MINT_IN_CASE(MINT_DIV_I4)
2202 if (sp [-1].data.i == 0)
2203 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2206 MINT_IN_CASE(MINT_DIV_I8)
2207 if (sp [-1].data.l == 0)
2208 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2211 MINT_IN_CASE(MINT_DIV_R8)
2215 #define BINOP_CAST(datamem, op, type) \
2217 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2219 MINT_IN_CASE(MINT_DIV_UN_I4)
2220 if (sp [-1].data.i == 0)
2221 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2222 BINOP_CAST(i, /, guint32);
2224 MINT_IN_CASE(MINT_DIV_UN_I8)
2225 if (sp [-1].data.l == 0)
2226 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2227 BINOP_CAST(l, /, guint64);
2229 MINT_IN_CASE(MINT_REM_I4)
2230 if (sp [-1].data.i == 0)
2231 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2234 MINT_IN_CASE(MINT_REM_I8)
2235 if (sp [-1].data.l == 0)
2236 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2239 MINT_IN_CASE(MINT_REM_R8)
2240 /* FIXME: what do we actually do here? */
2242 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2245 MINT_IN_CASE(MINT_REM_UN_I4)
2246 if (sp [-1].data.i == 0)
2247 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2248 BINOP_CAST(i, %, guint32);
2250 MINT_IN_CASE(MINT_REM_UN_I8)
2251 if (sp [-1].data.l == 0)
2252 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2253 BINOP_CAST(l, %, guint64);
2255 MINT_IN_CASE(MINT_AND_I4)
2258 MINT_IN_CASE(MINT_AND_I8)
2261 MINT_IN_CASE(MINT_OR_I4)
2264 MINT_IN_CASE(MINT_OR_I8)
2267 MINT_IN_CASE(MINT_XOR_I4)
2270 MINT_IN_CASE(MINT_XOR_I8)
2274 #define SHIFTOP(datamem, op) \
2276 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2279 MINT_IN_CASE(MINT_SHL_I4)
2282 MINT_IN_CASE(MINT_SHL_I8)
2285 MINT_IN_CASE(MINT_SHR_I4)
2288 MINT_IN_CASE(MINT_SHR_I8)
2291 MINT_IN_CASE(MINT_SHR_UN_I4)
2293 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2296 MINT_IN_CASE(MINT_SHR_UN_I8)
2298 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2301 MINT_IN_CASE(MINT_NEG_I4)
2302 sp [-1].data.i = - sp [-1].data.i;
2305 MINT_IN_CASE(MINT_NEG_I8)
2306 sp [-1].data.l = - sp [-1].data.l;
2309 MINT_IN_CASE(MINT_NEG_R8)
2310 sp [-1].data.f = - sp [-1].data.f;
2313 MINT_IN_CASE(MINT_NOT_I4)
2314 sp [-1].data.i = ~ sp [-1].data.i;
2317 MINT_IN_CASE(MINT_NOT_I8)
2318 sp [-1].data.l = ~ sp [-1].data.l;
2321 MINT_IN_CASE(MINT_CONV_I1_I4)
2322 sp [-1].data.i = (gint8)sp [-1].data.i;
2325 MINT_IN_CASE(MINT_CONV_I1_I8)
2326 sp [-1].data.i = (gint8)sp [-1].data.l;
2329 MINT_IN_CASE(MINT_CONV_I1_R8)
2330 sp [-1].data.i = (gint8)sp [-1].data.f;
2333 MINT_IN_CASE(MINT_CONV_U1_I4)
2334 sp [-1].data.i = (guint8)sp [-1].data.i;
2337 MINT_IN_CASE(MINT_CONV_U1_I8)
2338 sp [-1].data.i = (guint8)sp [-1].data.l;
2341 MINT_IN_CASE(MINT_CONV_U1_R8)
2342 sp [-1].data.i = (guint8)sp [-1].data.f;
2345 MINT_IN_CASE(MINT_CONV_I2_I4)
2346 sp [-1].data.i = (gint16)sp [-1].data.i;
2349 MINT_IN_CASE(MINT_CONV_I2_I8)
2350 sp [-1].data.i = (gint16)sp [-1].data.l;
2353 MINT_IN_CASE(MINT_CONV_I2_R8)
2354 sp [-1].data.i = (gint16)sp [-1].data.f;
2357 MINT_IN_CASE(MINT_CONV_U2_I4)
2358 sp [-1].data.i = (guint16)sp [-1].data.i;
2361 MINT_IN_CASE(MINT_CONV_U2_I8)
2362 sp [-1].data.i = (guint16)sp [-1].data.l;
2365 MINT_IN_CASE(MINT_CONV_U2_R8)
2366 sp [-1].data.i = (guint16)sp [-1].data.f;
2369 MINT_IN_CASE(MINT_CONV_I4_R8)
2370 sp [-1].data.i = (gint32)sp [-1].data.f;
2373 MINT_IN_CASE(MINT_CONV_U4_I8)
2374 MINT_IN_CASE(MINT_CONV_I4_I8)
2375 sp [-1].data.i = (gint32)sp [-1].data.l;
2378 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2379 sp [-2].data.i = (gint32)sp [-2].data.l;
2382 MINT_IN_CASE(MINT_CONV_U4_R8)
2383 sp [-1].data.i = (guint32)sp [-1].data.f;
2386 MINT_IN_CASE(MINT_CONV_I8_I4)
2387 sp [-1].data.l = sp [-1].data.i;
2390 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2391 sp [-2].data.l = sp [-2].data.i;
2394 MINT_IN_CASE(MINT_CONV_I8_U4)
2395 sp [-1].data.l = (guint32)sp [-1].data.i;
2398 MINT_IN_CASE(MINT_CONV_I8_R8)
2399 sp [-1].data.l = (gint64)sp [-1].data.f;
2402 MINT_IN_CASE(MINT_CONV_R4_I4)
2403 sp [-1].data.f = (float)sp [-1].data.i;
2406 MINT_IN_CASE(MINT_CONV_R4_I8)
2407 sp [-1].data.f = (float)sp [-1].data.l;
2410 MINT_IN_CASE(MINT_CONV_R4_R8)
2411 sp [-1].data.f = (float)sp [-1].data.f;
2414 MINT_IN_CASE(MINT_CONV_R8_I4)
2415 sp [-1].data.f = (double)sp [-1].data.i;
2418 MINT_IN_CASE(MINT_CONV_R8_I8)
2419 sp [-1].data.f = (double)sp [-1].data.l;
2422 MINT_IN_CASE(MINT_CONV_U8_I4)
2423 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2426 MINT_IN_CASE(MINT_CONV_U8_R8)
2427 sp [-1].data.l = (guint64)sp [-1].data.f;
2431 MINT_IN_CASE(MINT_CPOBJ) {
2434 vtklass = rtm->data_items[READ32 (ip)];
2437 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2441 MINT_IN_CASE(MINT_LDOBJ) {
2444 c = rtm->data_items[* (guint16 *)(ip + 1)];
2446 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2448 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2450 size = mono_class_value_size (c, NULL);
2452 sp [-1].data.p = vt_sp;
2453 memcpy(vt_sp, p, size);
2454 vt_sp += (size + 7) & ~7;
2458 MINT_IN_CASE(MINT_LDSTR)
2459 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2463 MINT_IN_CASE(MINT_NEWOBJ) {
2464 MonoClass *newobj_class;
2465 MonoMethodSignature *csig;
2466 stackval valuetype_this;
2472 token = * (guint16 *)(ip + 1);
2475 child_frame.runtime_method = rtm->data_items [token];
2476 csig = child_frame.runtime_method->method->signature;
2477 newobj_class = child_frame.runtime_method->method->klass;
2478 /*if (profiling_classes) {
2479 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2481 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2485 if (newobj_class->parent == mono_defaults.array_class) {
2486 sp -= csig->param_count;
2487 o = ves_array_create (context->domain, newobj_class, csig, sp);
2488 goto array_constructed;
2492 * First arg is the object.
2494 if (newobj_class->valuetype) {
2495 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2496 child_frame.obj = vt_sp;
2497 valuetype_this.data.p = vt_sp;
2499 memset (&valuetype_this, 0, sizeof (stackval));
2500 child_frame.obj = &valuetype_this;
2503 if (newobj_class != mono_defaults.string_class) {
2504 context->managed_code = 0;
2505 o = mono_object_new (context->domain, newobj_class);
2506 context->managed_code = 1;
2507 if (context->abort_thread) {
2508 MonoThread *thread = mono_thread_current ();
2509 context->abort_thread = 0;
2510 thread->abort_exc = mono_get_exception_thread_abort ();
2511 frame->ex = thread->abort_exc;
2512 goto handle_finally;
2514 child_frame.obj = o;
2516 child_frame.retval = &retval;
2520 if (csig->param_count) {
2521 sp -= csig->param_count;
2522 child_frame.stack_args = sp;
2524 child_frame.stack_args = NULL;
2527 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2529 child_frame.ip = NULL;
2530 child_frame.ex = NULL;
2532 ves_exec_method_with_context (&child_frame, context);
2534 context->current_frame = frame;
2536 if (child_frame.ex) {
2538 * An exception occurred, need to run finally, fault and catch handlers..
2540 frame->ex = child_frame.ex;
2541 goto handle_finally;
2544 * a constructor returns void, but we need to return the object we created
2547 if (newobj_class->valuetype && !newobj_class->enumtype) {
2548 *sp = valuetype_this;
2549 } else if (newobj_class == mono_defaults.string_class) {
2557 MINT_IN_CASE(MINT_CASTCLASS)
2558 c = rtm->data_items [*(guint16 *)(ip + 1)];
2559 if ((o = sp [-1].data.p)) {
2560 if (c->marshalbyref) {
2561 if (!mono_object_isinst_mbyref (o, c))
2562 THROW_EX (mono_get_exception_invalid_cast (), ip);
2564 MonoVTable *vt = o->vtable;
2565 MonoClass *oklass = vt->klass;
2566 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2567 if (c->interface_id > vt->max_interface_id ||
2568 vt->interface_offsets [c->interface_id] == 0) {
2569 THROW_EX (mono_get_exception_invalid_cast (), ip);
2571 } else if (c->rank) {
2572 if (!mono_object_isinst (o, c))
2573 THROW_EX (mono_get_exception_invalid_cast (), ip);
2574 } else if (!mono_class_has_parent (oklass, c)) {
2575 THROW_EX (mono_get_exception_invalid_cast (), ip);
2581 MINT_IN_CASE(MINT_ISINST)
2582 c = rtm->data_items [*(guint16 *)(ip + 1)];
2583 if ((o = sp [-1].data.p)) {
2584 if (c->marshalbyref) {
2585 if (!mono_object_isinst_mbyref (o, c))
2586 sp [-1].data.p = NULL;
2588 MonoVTable *vt = o->vtable;
2589 MonoClass *oklass = vt->klass;
2590 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2591 if (c->interface_id > vt->max_interface_id ||
2592 vt->interface_offsets [c->interface_id] == 0) {
2593 sp [-1].data.p = NULL;
2595 } else if (c->rank) {
2596 if (!mono_object_isinst (o, c))
2597 sp [-1].data.p = NULL;
2598 } else if (!mono_class_has_parent (oklass, c)) {
2599 sp [-1].data.p = NULL;
2605 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2606 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2609 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2610 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2613 MINT_IN_CASE(MINT_UNBOX)
2614 c = rtm->data_items[*(guint16 *)(ip + 1)];
2618 THROW_EX (mono_get_exception_null_reference(), ip);
2620 if (!(mono_object_isinst (o, c) ||
2621 ((o->vtable->klass->rank == 0) &&
2622 (o->vtable->klass->element_class == c->element_class))))
2623 THROW_EX (mono_get_exception_invalid_cast (), ip);
2625 sp [-1].data.p = (char *)o + sizeof (MonoObject);
2628 MINT_IN_CASE(MINT_THROW)
2630 frame->ex_handler = NULL;
2632 sp->data.p = mono_get_exception_null_reference ();
2633 THROW_EX ((MonoException *)sp->data.p, ip);
2635 MINT_IN_CASE(MINT_LDFLDA)
2638 THROW_EX (mono_get_exception_null_reference (), ip);
2639 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2642 MINT_IN_CASE(MINT_CKNULL)
2645 THROW_EX (mono_get_exception_null_reference (), ip);
2649 #define LDFLD(datamem, fieldtype) \
2650 o = sp [-1].data.p; \
2652 THROW_EX (mono_get_exception_null_reference (), ip); \
2653 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2656 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2657 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2658 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2659 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2660 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2661 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2662 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2663 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2664 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2665 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2667 MINT_IN_CASE(MINT_LDFLD_VT)
2670 THROW_EX (mono_get_exception_null_reference (), ip);
2671 i32 = READ32(ip + 2);
2672 sp [-1].data.p = vt_sp;
2673 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2674 vt_sp += (i32 + 7) & ~7;
2678 MINT_IN_CASE(MINT_LDRMFLD) {
2679 MonoClassField *field;
2684 THROW_EX (mono_get_exception_null_reference (), ip);
2685 field = rtm->data_items[* (guint16 *)(ip + 1)];
2687 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2688 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2689 addr = mono_load_remote_field (o, klass, field, NULL);
2691 addr = (char*)o + field->offset;
2694 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2698 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2699 MonoClassField *field;
2704 THROW_EX (mono_get_exception_null_reference (), ip);
2705 field = rtm->data_items[* (guint16 *)(ip + 1)];
2706 i32 = READ32(ip + 2);
2708 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2709 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2710 addr = mono_load_remote_field (o, klass, field, NULL);
2712 addr = (char*)o + field->offset;
2715 sp [-1].data.p = vt_sp;
2716 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2717 vt_sp += (i32 + 7) & ~7;
2718 memcpy(sp [-1].data.p, addr, i32);
2722 #define STFLD(datamem, fieldtype) \
2723 o = sp [-2].data.p; \
2725 THROW_EX (mono_get_exception_null_reference (), ip); \
2727 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2730 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2731 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2732 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2733 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2734 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2735 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2736 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2737 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2738 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2739 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2741 MINT_IN_CASE(MINT_STFLD_VT)
2744 THROW_EX (mono_get_exception_null_reference (), ip);
2745 i32 = READ32(ip + 2);
2747 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2748 vt_sp -= (i32 + 7) & ~7;
2752 MINT_IN_CASE(MINT_STRMFLD) {
2753 MonoClassField *field;
2757 THROW_EX (mono_get_exception_null_reference (), ip);
2759 field = rtm->data_items[* (guint16 *)(ip + 1)];
2762 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2763 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2764 mono_store_remote_field (o, klass, field, &sp [-1].data);
2766 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
2771 MINT_IN_CASE(MINT_STRMFLD_VT) {
2772 MonoClassField *field;
2776 THROW_EX (mono_get_exception_null_reference (), ip);
2777 field = rtm->data_items[* (guint16 *)(ip + 1)];
2778 i32 = READ32(ip + 2);
2781 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
2782 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2783 mono_store_remote_field (o, klass, field, &sp [-1].data);
2785 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
2788 vt_sp -= (i32 + 7) & ~7;
2791 MINT_IN_CASE(MINT_LDSFLDA) {
2792 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2793 MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
2796 if (!vt->initialized) {
2798 mono_runtime_class_init (vt);
2802 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2803 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2805 sp->data.p = (char*)(vt->data) + field->offset;
2809 MINT_IN_CASE(MINT_LDSFLD) {
2811 MonoClassField *field;
2814 field = rtm->data_items[*(guint16 *)(ip + 1)];
2815 vt = rtm->data_items [*(guint16 *)(ip + 2)];
2816 if (!vt->initialized) {
2818 mono_runtime_class_init (vt);
2821 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2822 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2824 addr = (char*)(vt->data) + field->offset;
2826 stackval_from_data (field->type, sp, addr, FALSE);
2830 MINT_IN_CASE(MINT_LDSFLD_I4) {
2831 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2832 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
2833 if (!vt->initialized) {
2835 mono_runtime_class_init (vt);
2838 sp->data.i = * (gint32 *)((char*)(vt->data) + field->offset);
2842 MINT_IN_CASE(MINT_LDSFLD_O) {
2843 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
2844 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
2845 if (!vt->initialized) {
2847 mono_runtime_class_init (vt);
2850 sp->data.p = * (gpointer *)((char*)(vt->data) + field->offset);
2854 MINT_IN_CASE(MINT_LDSFLD_VT) {
2856 MonoClassField *field;
2861 token = * (guint16 *)(ip + 1);
2862 size = READ32(ip + 2);
2863 field = rtm->data_items[token];
2866 vt = mono_class_vtable (context->domain, field->parent);
2867 if (!vt->initialized) {
2869 mono_runtime_class_init (vt);
2872 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2873 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2875 addr = (char*)(vt->data) + field->offset;
2878 vt_sp += (size + 7) & ~7;
2879 stackval_from_data (field->type, sp, addr, FALSE);
2883 MINT_IN_CASE(MINT_STSFLD) {
2885 MonoClassField *field;
2889 token = * (guint16 *)(ip + 1);
2890 field = rtm->data_items[token];
2894 vt = mono_class_vtable (context->domain, field->parent);
2895 if (!vt->initialized) {
2897 mono_runtime_class_init (vt);
2900 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2901 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2903 addr = (char*)(vt->data) + field->offset;
2905 stackval_to_data (field->type, sp, addr, FALSE);
2908 MINT_IN_CASE(MINT_STSFLD_VT) {
2910 MonoClassField *field;
2915 token = * (guint16 *)(ip + 1);
2916 size = READ32(ip + 2);
2917 field = rtm->data_items[token];
2920 vt = mono_class_vtable (context->domain, field->parent);
2921 if (!vt->initialized) {
2923 mono_runtime_class_init (vt);
2926 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
2927 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
2929 addr = (char*)(vt->data) + field->offset;
2931 stackval_to_data (field->type, sp, addr, FALSE);
2932 vt_sp -= (size + 7) & ~7;
2935 MINT_IN_CASE(MINT_STOBJ_VT) {
2937 c = rtm->data_items[* (guint16 *)(ip + 1)];
2939 size = mono_class_value_size (c, NULL);
2940 memcpy(sp [-2].data.p, sp [-1].data.p, size);
2941 vt_sp -= (size + 7) & ~7;
2945 MINT_IN_CASE(MINT_STOBJ) {
2947 c = rtm->data_items[* (guint16 *)(ip + 1)];
2949 size = mono_class_value_size (c, NULL);
2950 memcpy(sp [-2].data.p, &sp [-1].data, size);
2954 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
2955 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
2956 THROW_EX (mono_get_exception_overflow (), ip);
2957 sp [-1].data.i = (guint32)sp [-1].data.f;
2960 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
2961 if (sp [-1].data.i < 0)
2962 THROW_EX (mono_get_exception_overflow (), ip);
2963 sp [-1].data.l = sp [-1].data.i;
2966 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
2967 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
2968 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
2969 THROW_EX (mono_get_exception_overflow (), ip);
2970 sp [-1].data.l = (guint64)sp [-1].data.f;
2973 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
2974 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
2975 THROW_EX (mono_get_exception_overflow (), ip);
2976 sp [-1].data.l = (gint64)sp [-1].data.f;
2979 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
2980 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
2981 THROW_EX (mono_get_exception_overflow (), ip);
2982 sp [-1].data.i = (mono_u)sp [-1].data.l;
2985 MINT_IN_CASE(MINT_BOX)
2986 c = rtm->data_items [* (guint16 *)(ip + 1)];
2988 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2989 int size = mono_class_value_size (c, NULL);
2990 sp [-1].data.p = mono_value_box (context->domain, c, sp [-1].data.p);
2991 size = (size + 7) & ~7;
2995 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
2996 sp [-1].data.p = mono_value_box (context->domain, c, &sp [-1]);
3000 MINT_IN_CASE(MINT_NEWARR)
3001 sp [-1].data.p = (MonoObject*) mono_array_new (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i);
3003 /*if (profiling_classes) {
3004 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3006 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3010 MINT_IN_CASE(MINT_LDLEN)
3013 THROW_EX (mono_get_exception_null_reference (), ip);
3014 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3017 MINT_IN_CASE(MINT_GETCHR) {
3021 THROW_EX (mono_get_exception_null_reference (), ip);
3022 i32 = sp [-1].data.i;
3023 if (i32 < 0 || i32 >= mono_string_length (s))
3024 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3026 sp [-1].data.i = mono_string_chars(s)[i32];
3030 MINT_IN_CASE(MINT_STRLEN)
3032 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3034 MINT_IN_CASE(MINT_ARRAY_RANK)
3037 THROW_EX (mono_get_exception_null_reference (), ip);
3038 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3041 MINT_IN_CASE(MINT_LDELEMA) {
3045 /*token = READ32 (ip)*/;
3051 aindex = sp [1].data.i;
3052 if (aindex >= mono_array_length ((MonoArray *) o))
3053 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3055 /* check the array element corresponds to token */
3056 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3058 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3062 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3063 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3064 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3065 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3066 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3067 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3068 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3069 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3070 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3071 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3072 MINT_IN_CASE(MINT_LDELEM_REF) {
3080 THROW_EX (mono_get_exception_null_reference (), ip);
3082 aindex = sp [1].data.i;
3083 if (aindex >= mono_array_length (o))
3084 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3087 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3090 case MINT_LDELEM_I1:
3091 sp [0].data.i = mono_array_get (o, gint8, aindex);
3093 case MINT_LDELEM_U1:
3094 sp [0].data.i = mono_array_get (o, guint8, aindex);
3096 case MINT_LDELEM_I2:
3097 sp [0].data.i = mono_array_get (o, gint16, aindex);
3099 case MINT_LDELEM_U2:
3100 sp [0].data.i = mono_array_get (o, guint16, aindex);
3103 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3105 case MINT_LDELEM_I4:
3106 sp [0].data.i = mono_array_get (o, gint32, aindex);
3108 case MINT_LDELEM_U4:
3109 sp [0].data.i = mono_array_get (o, guint32, aindex);
3111 case MINT_LDELEM_I8:
3112 sp [0].data.l = mono_array_get (o, guint64, aindex);
3114 case MINT_LDELEM_R4:
3115 sp [0].data.f = mono_array_get (o, float, aindex);
3117 case MINT_LDELEM_R8:
3118 sp [0].data.f = mono_array_get (o, double, aindex);
3120 case MINT_LDELEM_REF:
3121 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3131 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3132 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3133 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3134 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3135 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3136 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3137 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3138 MINT_IN_CASE(MINT_STELEM_REF) {
3145 THROW_EX (mono_get_exception_null_reference (), ip);
3147 aindex = sp [1].data.i;
3148 if (aindex >= mono_array_length ((MonoArray *)o))
3149 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3153 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3155 case MINT_STELEM_I1:
3156 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3158 case MINT_STELEM_I2:
3159 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3161 case MINT_STELEM_I4:
3162 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3164 case MINT_STELEM_I8:
3165 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3167 case MINT_STELEM_R4:
3168 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3170 case MINT_STELEM_R8:
3171 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3173 case MINT_STELEM_REF:
3174 if (sp [2].data.p && !mono_object_isinst (sp [2].data.p, mono_object_class (o)->element_class))
3175 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3176 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3185 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3186 if (sp [-1].data.i < 0)
3187 THROW_EX (mono_get_exception_overflow (), ip);
3190 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3191 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3192 THROW_EX (mono_get_exception_overflow (), ip);
3193 sp [-1].data.i = (gint32) sp [-1].data.l;
3196 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3197 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3198 THROW_EX (mono_get_exception_overflow (), ip);
3199 sp [-1].data.i = (gint32) sp [-1].data.f;
3202 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3203 if (sp [-1].data.i < 0)
3204 THROW_EX (mono_get_exception_overflow (), ip);
3207 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3208 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3209 THROW_EX (mono_get_exception_overflow (), ip);
3210 sp [-1].data.i = (guint32) sp [-1].data.l;
3213 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3214 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3215 THROW_EX (mono_get_exception_overflow (), ip);
3216 sp [-1].data.i = (guint32) sp [-1].data.f;
3219 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3220 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3221 THROW_EX (mono_get_exception_overflow (), ip);
3224 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3225 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3226 THROW_EX (mono_get_exception_overflow (), ip);
3227 sp [-1].data.i = (gint16) sp [-1].data.l;
3230 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3231 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3232 THROW_EX (mono_get_exception_overflow (), ip);
3233 sp [-1].data.i = (gint16) sp [-1].data.f;
3236 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3237 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3238 THROW_EX (mono_get_exception_overflow (), ip);
3241 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3242 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3243 THROW_EX (mono_get_exception_overflow (), ip);
3244 sp [-1].data.i = (guint16) sp [-1].data.l;
3247 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3248 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3249 THROW_EX (mono_get_exception_overflow (), ip);
3250 sp [-1].data.i = (guint16) sp [-1].data.f;
3253 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3254 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3255 THROW_EX (mono_get_exception_overflow (), ip);
3258 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3259 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3260 THROW_EX (mono_get_exception_overflow (), ip);
3261 sp [-1].data.i = (gint8) sp [-1].data.l;
3264 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3265 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3266 THROW_EX (mono_get_exception_overflow (), ip);
3267 sp [-1].data.i = (gint8) sp [-1].data.f;
3270 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3271 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3272 THROW_EX (mono_get_exception_overflow (), ip);
3275 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3276 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3277 THROW_EX (mono_get_exception_overflow (), ip);
3278 sp [-1].data.i = (guint8) sp [-1].data.l;
3281 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3282 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3283 THROW_EX (mono_get_exception_overflow (), ip);
3284 sp [-1].data.i = (guint8) sp [-1].data.f;
3288 MINT_IN_CASE(MINT_LDELEM)
3289 MINT_IN_CASE(MINT_STELEM)
3290 MINT_IN_CASE(MINT_UNBOX_ANY)
3292 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3294 MINT_IN_CASE(MINT_CKFINITE)
3295 if (!finite(sp [-1].data.f))
3296 THROW_EX (mono_get_exception_arithmetic (), ip);
3300 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3302 MINT_IN_CASE(MINT_LDTOKEN)
3305 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3309 MINT_IN_CASE(MINT_ADD_OVF_I4)
3310 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3311 THROW_EX (mono_get_exception_overflow (), ip);
3314 MINT_IN_CASE(MINT_ADD_OVF_I8)
3315 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3316 THROW_EX (mono_get_exception_overflow (), ip);
3319 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3320 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3321 THROW_EX (mono_get_exception_overflow (), ip);
3322 BINOP_CAST(i, +, guint32);
3324 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3325 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3326 THROW_EX (mono_get_exception_overflow (), ip);
3327 BINOP_CAST(l, +, guint64);
3329 MINT_IN_CASE(MINT_MUL_OVF_I4)
3330 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3331 THROW_EX (mono_get_exception_overflow (), ip);
3334 MINT_IN_CASE(MINT_MUL_OVF_I8)
3335 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3336 THROW_EX (mono_get_exception_overflow (), ip);
3339 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3340 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3341 THROW_EX (mono_get_exception_overflow (), ip);
3342 BINOP_CAST(i, *, guint32);
3344 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3345 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3346 THROW_EX (mono_get_exception_overflow (), ip);
3347 BINOP_CAST(l, *, guint64);
3349 MINT_IN_CASE(MINT_SUB_OVF_I4)
3350 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3351 THROW_EX (mono_get_exception_overflow (), ip);
3354 MINT_IN_CASE(MINT_SUB_OVF_I8)
3355 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3356 THROW_EX (mono_get_exception_overflow (), ip);
3359 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3360 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3361 THROW_EX (mono_get_exception_overflow (), ip);
3362 BINOP_CAST(i, -, guint32);
3364 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3365 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3366 THROW_EX (mono_get_exception_overflow (), ip);
3367 BINOP_CAST(l, -, guint64);
3369 MINT_IN_CASE(MINT_ENDFINALLY)
3371 ip = finally_ips->data;
3372 finally_ips = g_slist_remove (finally_ips, ip);
3379 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3380 MINT_IN_CASE(MINT_LEAVE_S)
3381 while (sp > frame->stack) {
3385 if (*ip == MINT_LEAVE_S) {
3386 ip += (short) *(ip + 1);
3388 ip += (gint32) READ32 (ip + 1);
3391 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3392 frame->ex_handler = NULL;
3395 goto handle_finally;
3397 MINT_IN_CASE(MINT_MONO_CONV1)
3400 case MONO_MARSHAL_CONV_STR_LPWSTR:
3401 sp [-1].data.p = mono_string_to_utf16 (sp [-1].data.p);
3403 case MONO_MARSHAL_CONV_LPSTR_STR:
3404 sp [-1].data.p = mono_string_new_wrapper (sp [-1].data.p);
3406 case MONO_MARSHAL_CONV_STR_LPTSTR:
3407 case MONO_MARSHAL_CONV_STR_LPSTR:
3408 sp [-1].data.p = mono_string_to_utf8 (sp [-1].data.p);
3410 case MONO_MARSHAL_CONV_STR_BSTR:
3411 sp [-1].data.p = mono_string_to_bstr (sp [-1].data.p);
3413 case MONO_MARSHAL_CONV_STR_TBSTR:
3414 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3415 sp [-1].data.p = mono_string_to_ansibstr (sp [-1].data.p);
3417 case MONO_MARSHAL_CONV_SB_LPSTR:
3418 case MONO_MARSHAL_CONV_SB_LPTSTR:
3419 sp [-1].data.p = mono_string_builder_to_utf8 (sp [-1].data.p);
3421 case MONO_MARSHAL_CONV_SB_LPWSTR:
3422 sp [-1].data.p = mono_string_builder_to_utf16 (sp [-1].data.p);
3424 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3425 sp [-1].data.p = mono_array_to_savearray (sp [-1].data.p);
3427 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3428 sp [-1].data.p = mono_array_to_lparray (sp [-1].data.p);
3430 case MONO_MARSHAL_CONV_DEL_FTN:
3431 sp [-1].data.p = mono_delegate_to_ftnptr (sp [-1].data.p);
3433 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3434 sp [-1].data.p = mono_marshal_string_array (sp [-1].data.p);
3436 case MONO_MARSHAL_CONV_STRARRAY_STRWLPARRAY:
3437 sp [-1].data.p = mono_marshal_string_array_to_unicode (sp [-1].data.p);
3439 case MONO_MARSHAL_CONV_LPWSTR_STR:
3440 sp [-1].data.p = mono_string_from_utf16 (sp [-1].data.p);
3443 fprintf(stderr, "MINT_MONO_CONV1 %d", ip[-1]);
3444 g_assert_not_reached ();
3447 MINT_IN_CASE(MINT_MONO_CONV2)
3451 case MONO_MARSHAL_CONV_LPSTR_SB:
3452 case MONO_MARSHAL_CONV_LPTSTR_SB:
3453 mono_string_utf8_to_builder (sp [0].data.p, sp [1].data.p);
3455 case MONO_MARSHAL_CONV_LPWSTR_SB:
3456 mono_string_utf16_to_builder (sp [0].data.p, sp [1].data.p);
3458 case MONO_MARSHAL_FREE_ARRAY:
3459 mono_marshal_free_array (sp [0].data.p, sp [1].data.i);
3462 g_assert_not_reached ();
3465 MINT_IN_CASE(MINT_MONO_CONV3)
3469 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3470 mono_string_to_byvalstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3472 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3473 mono_string_to_byvalwstr (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3476 g_assert_not_reached ();
3479 MINT_IN_CASE(MINT_MONO_LDPTR)
3480 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3484 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3485 sp->data.p = mono_object_new (context->domain, rtm->data_items [*(guint16 *)(ip + 1)]);
3489 MINT_IN_CASE(MINT_MONO_FREE)
3492 g_free (sp->data.p);
3494 MINT_IN_CASE(MINT_MONO_RETOBJ)
3497 stackval_from_data (frame->runtime_method->method->signature->ret, frame->retval, sp->data.p, frame->runtime_method->method->signature->pinvoke);
3498 if (sp > frame->stack)
3499 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3502 #define RELOP(datamem, op) \
3504 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3506 MINT_IN_CASE(MINT_CEQ_I4)
3509 MINT_IN_CASE(MINT_CEQ0_I4)
3510 sp [-1].data.i = (sp [-1].data.i == 0);
3513 MINT_IN_CASE(MINT_CEQ_I8)
3516 MINT_IN_CASE(MINT_CEQ_R8)
3518 if (isunordered (sp [-1].data.f, sp [0].data.f))
3521 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3524 MINT_IN_CASE(MINT_CGT_I4)
3527 MINT_IN_CASE(MINT_CGT_I8)
3530 MINT_IN_CASE(MINT_CGT_R8)
3532 if (isunordered (sp [-1].data.f, sp [0].data.f))
3535 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3539 #define RELOP_CAST(datamem, op, type) \
3541 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3544 MINT_IN_CASE(MINT_CGT_UN_I4)
3545 RELOP_CAST(i, >, guint32);
3547 MINT_IN_CASE(MINT_CGT_UN_I8)
3548 RELOP_CAST(l, >, guint64);
3550 MINT_IN_CASE(MINT_CGT_UN_R8)
3552 if (isunordered (sp [-1].data.f, sp [0].data.f))
3555 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3558 MINT_IN_CASE(MINT_CLT_I4)
3561 MINT_IN_CASE(MINT_CLT_I8)
3564 MINT_IN_CASE(MINT_CLT_R8)
3566 if (isunordered (sp [-1].data.f, sp [0].data.f))
3569 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3572 MINT_IN_CASE(MINT_CLT_UN_I4)
3573 RELOP_CAST(i, <, guint32);
3575 MINT_IN_CASE(MINT_CLT_UN_I8)
3576 RELOP_CAST(l, <, guint64);
3578 MINT_IN_CASE(MINT_CLT_UN_R8)
3580 if (isunordered (sp [-1].data.f, sp [0].data.f))
3583 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3586 MINT_IN_CASE(MINT_LDFTN) {
3587 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3592 MINT_IN_CASE(MINT_LDVIRTFTN) {
3593 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3597 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3599 sp->data.p = get_virtual_method (m, sp->data.p);
3604 MINT_IN_CASE(MINT_LDTHIS)
3605 sp->data.p = frame->obj;
3609 MINT_IN_CASE(MINT_STTHIS)
3611 frame->obj = sp->data.p;
3614 MINT_IN_CASE(MINT_LDTHISA)
3615 sp->data.p = &frame->obj;
3620 #define LDARG(datamem, argtype) \
3621 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3625 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3626 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3627 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3628 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3629 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3630 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3631 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3632 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3633 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3634 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3636 MINT_IN_CASE(MINT_LDARG_VT)
3638 i32 = READ32(ip + 2);
3639 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3640 vt_sp += (i32 + 7) & ~7;
3645 #define STARG(datamem, argtype) \
3647 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3650 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3651 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3652 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3653 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3654 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3655 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3656 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3657 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3658 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3659 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3661 MINT_IN_CASE(MINT_STARG_VT)
3662 i32 = READ32(ip + 2);
3664 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3665 vt_sp -= (i32 + 7) & ~7;
3669 #define STINARG(datamem, argtype) \
3671 int n = * (guint16 *)(ip + 1); \
3672 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3676 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3677 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3678 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3679 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3680 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3681 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3682 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3683 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3684 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3685 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3687 MINT_IN_CASE(MINT_STINARG_VT) {
3688 int n = * (guint16 *)(ip + 1);
3689 i32 = READ32(ip + 2);
3690 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3695 MINT_IN_CASE(MINT_LDARGA)
3696 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3701 #define LDLOC(datamem, argtype) \
3702 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3706 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3707 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3708 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3709 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3710 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3711 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3712 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3713 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3714 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3715 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3717 MINT_IN_CASE(MINT_LDLOC_VT)
3719 i32 = READ32(ip + 2);
3720 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3721 vt_sp += (i32 + 7) & ~7;
3726 MINT_IN_CASE(MINT_LDLOCA_S)
3727 sp->data.p = locals + * (guint16 *)(ip + 1);
3732 #define STLOC(datamem, argtype) \
3734 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3737 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3738 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3739 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3740 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3741 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3742 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3743 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3744 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3745 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3746 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3748 #define STLOC_NP(datamem, argtype) \
3749 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3752 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3753 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3755 MINT_IN_CASE(MINT_STLOC_VT)
3756 i32 = READ32(ip + 2);
3758 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3759 vt_sp -= (i32 + 7) & ~7;
3763 MINT_IN_CASE(MINT_LOCALLOC)
3764 if (sp != frame->stack + 1) /*FIX?*/
3765 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3766 sp [-1].data.p = alloca (sp [-1].data.i);
3770 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3772 MINT_IN_CASE(MINT_INITOBJ)
3774 memset (sp->data.vt, 0, READ32(ip + 1));
3777 MINT_IN_CASE(MINT_CPBLK)
3779 if (!sp [0].data.p || !sp [1].data.p)
3780 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3782 /* FIXME: value and size may be int64... */
3783 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3786 MINT_IN_CASE(MINT_CONSTRAINED_) {
3788 /* FIXME: implement */
3790 token = READ32 (ip);
3795 MINT_IN_CASE(MINT_INITBLK)
3798 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3800 /* FIXME: value and size may be int64... */
3801 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3804 MINT_IN_CASE(MINT_NO_)
3805 /* FIXME: implement */
3809 MINT_IN_CASE(MINT_RETHROW)
3811 * need to clarify what this should actually do:
3812 * start the search from the last found handler in
3813 * this method or continue in the caller or what.
3814 * Also, do we need to run finally/fault handlers after a retrow?
3815 * Well, this implementation will follow the usual search
3816 * for an handler, considering the current ip as throw spot.
3817 * We need to NULL frame->ex_handler for the later code to
3818 * actually run the new found handler.
3820 frame->ex_handler = NULL;
3821 THROW_EX (frame->ex, ip - 1);
3824 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3825 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3829 g_assert_not_reached ();
3831 * Exception handling code.
3832 * The exception object is stored in frame->ex.
3839 MonoInvocation *inv;
3840 MonoExceptionClause *clause;
3846 g_print ("* Handling exception '%s' at IL_%04x\n",
3847 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
3848 rtm == NULL ? 0 : frame->ip - rtm->code);
3850 if (die_on_exception)
3853 for (inv = frame; inv; inv = inv->parent) {
3855 if (inv->runtime_method == NULL)
3857 method = inv->runtime_method->method;
3858 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3860 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3862 if (inv->ip == NULL)
3864 ip_offset = inv->ip - inv->runtime_method->code;
3865 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
3866 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3867 clause = &inv->runtime_method->clauses [i];
3868 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3869 if (!clause->flags) {
3870 if (mono_object_isinst ((MonoObject*)frame->ex, mono_class_get (method->klass->image, clause->token_or_filter))) {
3872 * OK, we found an handler, now we need to execute the finally
3873 * and fault blocks before branching to the handler code.
3875 inv->ex_handler = clause;
3878 g_print ("* Found handler at '%s'\n", method->name);
3880 goto handle_finally;
3883 /* FIXME: handle filter clauses */
3890 * If we get here, no handler was found: print a stack trace.
3892 for (inv = frame; inv; inv = inv->parent) {
3893 if (inv->invoke_trap)
3894 goto handle_finally;
3897 ex_obj = (MonoObject*)frame->ex;
3898 mono_unhandled_exception (ex_obj);
3905 MonoExceptionClause *clause;
3906 GSList *old_list = finally_ips;
3907 MonoMethod *method = frame->runtime_method->method;
3908 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
3912 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
3914 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3915 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
3918 ip_offset = frame->ip - rtm->code;
3920 if (endfinally_ip != NULL)
3921 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
3922 for (i = 0; i < header->num_clauses; ++i)
3923 if (frame->ex_handler == &rtm->clauses [i])
3927 clause = &rtm->clauses [i];
3928 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
3929 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
3930 ip = rtm->code + clause->handler_offset;
3931 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
3934 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
3940 endfinally_ip = NULL;
3942 if (old_list != finally_ips && finally_ips) {
3943 ip = finally_ips->data;
3944 finally_ips = g_slist_remove (finally_ips, ip);
3945 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
3950 * If an exception is set, we need to execute the fault handler, too,
3951 * otherwise, we continue normally.
3961 MonoExceptionClause *clause;
3962 MonoMethodHeader *header = ((MonoMethodNormal *)frame->runtime_method->method)->header;
3966 g_print ("* Handle fault\n");
3968 ip_offset = frame->ip - rtm->code;
3969 for (i = 0; i < header->num_clauses; ++i) {
3970 clause = &rtm->clauses [i];
3971 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3972 ip = rtm->code + clause->handler_offset;
3975 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
3981 * If the handler for the exception was found in this method, we jump
3982 * to it right away, otherwise we return and let the caller run
3983 * the finally, fault and catch blocks.
3984 * This same code should be present in the endfault opcode, but it
3985 * is corrently not assigned in the ECMA specs: LAMESPEC.
3987 if (frame->ex_handler) {
3990 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
3992 ip = rtm->code + frame->ex_handler->handler_offset;
3994 vt_sp = (char *)sp + rtm->stack_size;
3995 sp->data.p = frame->ex;
4006 ves_exec_method (MonoInvocation *frame)
4008 ThreadContext *context = TlsGetValue (thread_context_id);
4009 ThreadContext context_struct;
4015 mono_unhandled_exception ((MonoObject*)frame->ex);
4018 if (context == NULL) {
4019 context = &context_struct;
4020 context_struct.domain = mono_domain_get ();
4021 context_struct.base_frame = frame;
4022 context_struct.current_frame = NULL;
4023 context_struct.env_frame = frame;
4024 context_struct.current_env = &env;
4025 context_struct.search_for_handler = 0;
4026 context_struct.managed_code = 0;
4027 context_struct.abort_thread = 0;
4028 TlsSetValue (thread_context_id, context);
4031 frame->parent = context->current_frame;
4032 frame->runtime_method = mono_interp_get_runtime_method (frame->method);
4033 context->managed_code = 1;
4034 ves_exec_method_with_context(frame, context);
4035 context->managed_code = 0;
4037 if (context != &context_struct && context->current_env) {
4038 context->env_frame->ex = frame->ex;
4039 longjmp (*context->current_env, 1);
4042 mono_unhandled_exception ((MonoObject*)frame->ex);
4044 if (context->base_frame == frame)
4045 TlsSetValue (thread_context_id, NULL);
4047 context->current_frame = frame->parent;
4051 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4053 MonoImage *image = assembly->image;
4054 MonoCLIImageInfo *iinfo;
4056 MonoObject *exc = NULL;
4059 iinfo = image->image_info;
4060 method = mono_get_method (image, iinfo->cli_cli_header.ch_entry_point, NULL);
4062 g_error ("No entry point method found in %s", image->name);
4064 rval = mono_runtime_run_main (method, argc, argv, &exc);
4066 mono_unhandled_exception (exc);
4075 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4076 "Usage is: mint [options] executable args...\n\n", VERSION);
4078 "Runtime Debugging:\n"
4083 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4086 " --traceclassinit\n"
4089 " --debug method_name\n"
4095 " --config filename load the specified config file instead of the default\n"
4096 " --workers n maximum number of worker threads\n"
4103 test_load_class (MonoImage* image)
4105 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
4109 for (i = 1; i <= t->rows; ++i) {
4110 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4111 mono_class_init (klass);
4117 add_signal_handler (int signo, void (*handler)(int))
4119 #ifdef PLATFORM_WIN32
4120 signal (signo, handler);
4122 struct sigaction sa;
4124 sa.sa_handler = handler;
4125 sigemptyset (&sa.sa_mask);
4128 g_assert (sigaction (signo, &sa, NULL) != -1);
4133 segv_handler (int signum)
4135 ThreadContext *context = TlsGetValue (thread_context_id);
4136 MonoException *segv_exception;
4138 if (context == NULL)
4140 segv_exception = mono_get_exception_null_reference ();
4141 segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4142 mono_raise_exception (segv_exception);
4147 quit_handler (int signum)
4149 ThreadContext *context = TlsGetValue (thread_context_id);
4150 MonoException *quit_exception;
4152 if (context == NULL)
4154 quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4155 mono_raise_exception (quit_exception);
4159 abrt_handler (int signum)
4161 ThreadContext *context = TlsGetValue (thread_context_id);
4162 MonoException *abrt_exception;
4164 if (context == NULL)
4166 abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4167 mono_raise_exception (abrt_exception);
4171 thread_abort_handler (int signum)
4173 ThreadContext *context = TlsGetValue (thread_context_id);
4176 if (context == NULL)
4179 if (context->managed_code) {
4180 thread = mono_thread_current ();
4181 thread->abort_exc = mono_get_exception_thread_abort ();
4182 mono_raise_exception (thread->abort_exc);
4184 context->abort_thread = 1;
4188 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4189 MonoReflectionMethod **method,
4190 gint32 *iloffset, gint32 *native_offset,
4191 MonoString **file, gint32 *line, gint32 *column)
4193 ThreadContext *context = TlsGetValue (thread_context_id);
4194 MonoInvocation *inv = context->current_frame;
4197 for (i = 0; inv && i < skip; inv = inv->parent)
4198 if (inv->runtime_method != NULL)
4206 *method = inv == NULL ? NULL : mono_method_get_object (context->domain, inv->runtime_method->method, NULL);
4209 if (need_file_info) {
4213 *file = mono_string_new (mono_domain_get (), "unknown");
4220 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4222 MonoDomain *domain = mono_domain_get ();
4224 MonoArray *ta = exc->trace_ips;
4228 /* Exception is not thrown yet */
4229 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
4232 len = mono_array_length (ta);
4234 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
4236 for (i = skip; i < len / 2; i++) {
4237 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
4238 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4239 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4242 sf->method = mono_method_get_object (domain, rtm->method, NULL);
4243 sf->native_offset = ip - rtm->code;
4247 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4249 if (need_file_info) {
4252 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4254 sf->filename = filename? mono_string_new (domain, filename): NULL;
4261 mono_array_set (res, gpointer, i, sf);
4268 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
4269 MonoReflectionMethod *info)
4271 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4272 MonoObject *delegate;
4274 mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4276 delegate = mono_object_new (mono_object_domain (type), delegate_class);
4278 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (info->method));
4287 int enable_debugging;
4293 static void main_thread_handler (gpointer user_data)
4295 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4296 MonoAssembly *assembly;
4298 if (main_args->enable_debugging)
4299 mono_debug_init (main_args->domain, MONO_DEBUG_FORMAT_MONO);
4301 assembly = mono_domain_assembly_open (main_args->domain,
4305 fprintf (stderr, "Can not open image %s\n", main_args->file);
4309 if (main_args->enable_debugging)
4310 mono_debug_init_2 (assembly);
4313 test_load_class (assembly->image);
4316 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4321 mono_runtime_install_handlers (void)
4323 add_signal_handler (SIGSEGV, segv_handler);
4324 add_signal_handler (SIGINT, quit_handler);
4325 add_signal_handler (SIGABRT, abrt_handler);
4326 add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4330 quit_function (MonoDomain *domain, gpointer user_data)
4332 mono_profiler_shutdown ();
4334 mono_runtime_cleanup (domain);
4335 mono_domain_free (domain, TRUE);
4340 mono_interp_cleanup(MonoDomain *domain)
4342 quit_function (domain, NULL);
4346 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4348 return ves_exec (domain, assembly, argc, argv);
4352 mono_interp_init(const char *file)
4356 g_set_prgname (file);
4357 mono_set_rootdir ();
4359 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4360 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4362 g_thread_init (NULL);
4364 thread_context_id = TlsAlloc ();
4365 TlsSetValue (thread_context_id, NULL);
4366 InitializeCriticalSection (&runtime_method_lookup_section);
4367 InitializeCriticalSection (&create_method_pointer_mutex);
4369 mono_runtime_install_handlers ();
4370 mono_interp_transform_init ();
4371 mono_install_compile_method (mono_create_method_pointer);
4372 mono_install_runtime_invoke (interp_mono_runtime_invoke);
4373 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4374 mono_install_trampoline (interp_create_trampoline);
4376 mono_install_handler (interp_ex_handler);
4377 mono_install_stack_walk (interp_walk_stack);
4378 mono_runtime_install_cleanup (quit_function);
4380 domain = mono_init (file);
4381 #ifdef __hpux /* generates very big stack frames */
4382 mono_threads_set_default_stacksize(32*1024*1024);
4385 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4386 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4387 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4388 mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4390 mono_runtime_init (domain, NULL, NULL);
4396 mono_main (int argc, char *argv [])
4400 char *file, *config_file = NULL;
4401 int enable_debugging = FALSE;
4402 MainThreadArgs main_args;
4405 setlocale (LC_ALL, "");
4409 for (i = 1; i < argc && argv [i][0] == '-'; i++){
4410 if (strcmp (argv [i], "--trace") == 0)
4412 if (strcmp (argv [i], "--noptr") == 0)
4413 global_no_pointers = 1;
4414 if (strcmp (argv [i], "--traceops") == 0)
4416 if (strcmp (argv [i], "--traceopt") == 0)
4417 ++mono_interp_traceopt;
4418 if (strcmp (argv [i], "--dieonex") == 0) {
4419 die_on_exception = 1;
4420 enable_debugging = 1;
4422 if (strcmp (argv [i], "--print-vtable") == 0)
4423 mono_print_vtable = TRUE;
4424 if (strcmp (argv [i], "--profile") == 0)
4425 mono_profiler_load (NULL);
4426 if (strcmp (argv [i], "--config") == 0)
4427 config_file = argv [++i];
4428 if (strcmp (argv [i], "--workers") == 0) {
4429 mono_max_worker_threads = atoi (argv [++i]);
4430 if (mono_max_worker_threads < 1)
4431 mono_max_worker_threads = 1;
4433 if (strcmp (argv [i], "--help") == 0)
4436 if (strcmp (argv [i], "--debug") == 0) {
4437 MonoMethodDesc *desc = mono_method_desc_new (argv [++i], FALSE);
4439 g_error ("Invalid method name '%s'", argv [i]);
4440 db_methods = g_list_append (db_methods, desc);
4442 if (strcmp (argv [i], "--nested") == 0)
4452 domain = mono_interp_init(file);
4453 mono_config_parse (config_file);
4455 error = mono_check_corlib_version ();
4457 fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
4458 fprintf (stderr, "Download a newer corlib at http://www.go-mono.com/daily.\n");
4462 main_args.domain=domain;
4463 main_args.file=file;
4464 main_args.argc=argc-i;
4465 main_args.argv=argv+i;
4466 main_args.enable_debugging=enable_debugging;
4468 mono_runtime_exec_managed_code (domain, main_thread_handler,
4471 quit_function (domain, NULL);
4473 /* Get the return value from System.Environment.ExitCode */
4474 retval=mono_environment_exitcode_get ();
4477 for (i = 0; i < 512; i++)
4478 if (opcode_counts[i] != 0)
4479 printf("%s %d\n", mono_interp_opname[i], opcode_counts[i]);