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/utils/gc_wrapper.h>
33 # define alloca __builtin_alloca
37 /* trim excessive headers */
38 #include <mono/metadata/image.h>
39 #include <mono/metadata/assembly-internals.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/reflection-internals.h>
51 #include <mono/metadata/exception.h>
52 #include <mono/metadata/verify.h>
53 #include <mono/metadata/opcodes.h>
54 #include <mono/metadata/debug-helpers.h>
55 #include <mono/metadata/mono-config.h>
56 #include <mono/metadata/marshal.h>
57 #include <mono/metadata/environment.h>
58 #include <mono/metadata/mono-debug.h>
61 #include "interp-internals.h"
65 #include <mono/mini/mini.h>
66 #include <mono/mini/jit-icalls.h>
69 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
72 #define finite _finite
76 #define finite isfinite
80 #define INIT_FRAME(frame,parent_frame,method_args,method_retval,domain,mono_method,error) \
82 (frame)->parent = (parent_frame); \
83 (frame)->stack_args = (method_args); \
84 (frame)->retval = (method_retval); \
85 (frame)->runtime_method = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
88 (frame)->invoke_trap = 0; \
91 void ves_exec_method (MonoInvocation *frame);
93 static char* dump_stack (stackval *stack, stackval *sp);
94 static char* dump_frame (MonoInvocation *inv);
95 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
96 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
98 typedef void (*ICallMethod) (MonoInvocation *frame);
100 static guint32 die_on_exception = 0;
101 static MonoNativeTlsKey thread_context_id;
103 static char* dump_args (MonoInvocation *inv);
105 #define DEBUG_INTERP 0
108 int mono_interp_traceopt = 2;
109 /* If true, then we output the opcodes as we interpret them */
110 static int global_tracing = 2;
112 static int debug_indent_level = 0;
114 static int break_on_method = 0;
115 static int nested_trace = 0;
116 static GList *db_methods = NULL;
123 for (h = 0; h < debug_indent_level; h++)
128 db_match_method (gpointer data, gpointer user_data)
130 MonoMethod *m = (MonoMethod*)user_data;
131 MonoMethodDesc *desc = data;
133 if (mono_method_desc_full_match (desc, m))
137 static void debug_enter (MonoInvocation *frame, int *tracing)
140 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
142 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
146 MonoMethod *method = frame->runtime_method->method;
147 char *mn, *args = dump_args (frame);
148 debug_indent_level++;
150 mn = mono_method_full_name (method, FALSE);
151 g_print ("(%p) Entering %s (", mono_thread_internal_current (), mn);
153 g_print ("%s)\n", args);
156 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
157 mono_profiler_method_enter (frame->runtime_method->method);
161 #define DEBUG_LEAVE() \
164 args = dump_retval (frame); \
166 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
167 g_print ("(%p) Leaving %s", mono_thread_internal_current (), mn); \
169 g_print (" => %s\n", args); \
171 debug_indent_level--; \
172 if (tracing == 3) global_tracing = 0; \
174 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
175 mono_profiler_method_leave (frame->runtime_method->method);
179 int mono_interp_traceopt = 0;
180 static void debug_enter (MonoInvocation *frame, int *tracing)
183 #define DEBUG_LEAVE()
188 interp_ex_handler (MonoException *ex) {
190 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
194 stack_trace = dump_frame (context->current_frame);
195 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
196 g_free (stack_trace);
197 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
198 char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
199 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
200 fprintf(stderr, "Nothing can catch this exception: ");
201 fprintf(stderr, "%s", ex->object.vtable->klass->name);
202 if (ex->message != NULL) {
203 char *m = mono_string_to_utf8_checked (ex->message, &error);
204 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
205 fprintf(stderr, ": %s", m);
208 fprintf(stderr, "\n%s\n", strace);
210 if (ex->inner_ex != NULL) {
211 ex = (MonoException *)ex->inner_ex;
212 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
213 if (ex->message != NULL) {
214 char *m = mono_string_to_utf8_checked (ex->message, &error);
215 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
216 fprintf(stderr, ": %s", m);
219 strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
220 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
221 fprintf(stderr, "\n");
222 fprintf(stderr, "%s\n", strace);
225 /* wait for other threads to also collapse */
226 // Sleep(1000); // TODO: proper sleep
229 context->env_frame->ex = ex;
230 context->search_for_handler = 1;
231 longjmp (*context->current_env, 1);
235 ves_real_abort (int line, MonoMethod *mh,
236 const unsigned short *ip, stackval *stack, stackval *sp)
239 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
240 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
241 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
242 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
243 g_print ("0x%04x %02x\n",
244 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
245 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
247 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
250 #define ves_abort() \
252 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
253 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
257 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
262 mono_domain_jit_code_hash_lock (domain);
263 if ((rtm = mono_internal_hash_table_lookup (&domain->jit_code_hash, method))) {
264 mono_domain_jit_code_hash_unlock (domain);
267 rtm = mono_domain_alloc0 (domain, sizeof (RuntimeMethod));
268 rtm->method = method;
269 rtm->param_count = mono_method_signature (method)->param_count;
270 rtm->hasthis = mono_method_signature (method)->hasthis;
271 mono_internal_hash_table_insert (&domain->jit_code_hash, method, rtm);
272 mono_domain_jit_code_hash_unlock (domain);
278 mono_interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
280 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
281 method = mono_marshal_get_synchronized_wrapper (method);
282 return mono_interp_get_runtime_method (domain, method, error);
285 static inline RuntimeMethod*
286 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
288 MonoMethod *m = runtime_method->method;
291 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
292 RuntimeMethod *ret = NULL;
293 if (mono_object_is_transparent_proxy (obj)) {
294 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
295 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
296 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
297 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
298 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
300 ret = runtime_method;
305 mono_class_setup_vtable (obj->vtable->klass);
307 int slot = mono_method_get_vtable_slot (m);
308 if (mono_class_is_interface (m->klass)) {
309 g_assert (obj->vtable->klass != m->klass);
310 /* TODO: interface offset lookup is slow, go through IMT instead */
311 gboolean non_exact_match;
312 slot += mono_class_interface_offset_with_variance (obj->vtable->klass, m->klass, &non_exact_match);
315 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
316 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
317 MonoGenericContext context = { NULL, NULL };
319 if (mono_class_is_ginst (virtual_method->klass))
320 context.class_inst = mono_class_get_generic_class (virtual_method->klass)->context.class_inst;
321 else if (mono_class_is_gtd (virtual_method->klass))
322 context.class_inst = mono_class_get_generic_container (virtual_method->klass)->context.class_inst;
323 context.method_inst = mono_method_get_context (m)->method_inst;
325 virtual_method = mono_class_inflate_generic_method_checked (virtual_method, &context, &error);
326 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
329 if (virtual_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
330 virtual_method = mono_marshal_get_synchronized_wrapper (virtual_method);
333 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
334 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
335 return virtual_runtime_method;
339 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
342 switch (type->type) {
343 case MONO_TYPE_OBJECT:
344 case MONO_TYPE_CLASS:
345 case MONO_TYPE_STRING:
346 case MONO_TYPE_ARRAY:
347 case MONO_TYPE_SZARRAY:
352 result->data.p = *(gpointer*)data;
355 switch (type->type) {
359 result->data.i = *(gint8*)data;
362 case MONO_TYPE_BOOLEAN:
363 result->data.i = *(guint8*)data;
366 result->data.i = *(gint16*)data;
370 result->data.i = *(guint16*)data;
373 result->data.i = *(gint32*)data;
377 result->data.nati = *(mono_i*)data;
380 result->data.p = *(gpointer*)data;
383 result->data.i = *(guint32*)data;
386 result->data.f = *(float*)data;
390 result->data.l = *(gint64*)data;
393 result->data.f = *(double*)data;
395 case MONO_TYPE_STRING:
396 case MONO_TYPE_SZARRAY:
397 case MONO_TYPE_CLASS:
398 case MONO_TYPE_OBJECT:
399 case MONO_TYPE_ARRAY:
400 result->data.p = *(gpointer*)data;
402 case MONO_TYPE_VALUETYPE:
403 if (type->data.klass->enumtype) {
404 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
407 mono_value_copy (result->data.vt, data, type->data.klass);
409 case MONO_TYPE_GENERICINST:
410 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
413 g_warning ("got type 0x%02x", type->type);
414 g_assert_not_reached ();
419 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
422 gpointer *p = (gpointer*)data;
426 /* printf ("TODAT0 %p\n", data); */
427 switch (type->type) {
430 guint8 *p = (guint8*)data;
434 case MONO_TYPE_BOOLEAN: {
435 guint8 *p = (guint8*)data;
436 *p = (val->data.i != 0);
441 case MONO_TYPE_CHAR: {
442 guint16 *p = (guint16*)data;
447 mono_i *p = (mono_i*)data;
448 /* In theory the value used by stloc should match the local var type
449 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
450 a native int - both by csc and mcs). Not sure what to do about sign extension
451 as it is outside the spec... doing the obvious */
452 *p = (mono_i)val->data.nati;
456 mono_u *p = (mono_u*)data;
458 *p = (mono_u)val->data.nati;
463 gint32 *p = (gint32*)data;
469 gint64 *p = (gint64*)data;
474 float *p = (float*)data;
479 double *p = (double*)data;
483 case MONO_TYPE_STRING:
484 case MONO_TYPE_SZARRAY:
485 case MONO_TYPE_CLASS:
486 case MONO_TYPE_OBJECT:
487 case MONO_TYPE_ARRAY:
488 case MONO_TYPE_PTR: {
489 gpointer *p = (gpointer*)data;
493 case MONO_TYPE_VALUETYPE:
494 if (type->data.klass->enumtype) {
495 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
498 mono_value_copy (data, val->data.vt, type->data.klass);
500 case MONO_TYPE_GENERICINST:
501 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
504 g_warning ("got type %x", type->type);
505 g_assert_not_reached ();
510 fill_in_trace (MonoException *exception, MonoInvocation *frame)
512 char *stack_trace = dump_frame (frame);
513 MonoDomain *domain = mono_domain_get();
514 (exception)->stack_trace = mono_string_new (domain, stack_trace);
515 (exception)->trace_ips = get_trace_ips (domain, frame);
516 g_free (stack_trace);
519 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
521 #define THROW_EX(exception,ex_ip) \
523 frame->ip = (ex_ip); \
524 frame->ex = (MonoException*)(exception); \
525 FILL_IN_TRACE(frame->ex, frame); \
526 goto handle_exception; \
530 ves_array_create (MonoInvocation *frame, MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
533 intptr_t *lower_bounds;
538 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
539 for (i = 0; i < sig->param_count; ++i) {
540 lengths [i] = values->data.i;
543 if (klass->rank == sig->param_count) {
544 /* Only lengths provided. */
547 /* lower bounds are first. */
548 lower_bounds = (intptr_t *) lengths;
549 lengths += klass->rank;
551 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
552 if (!mono_error_ok (&error)) {
553 frame->ex = mono_error_convert_to_exception (&error);
554 FILL_IN_TRACE (frame->ex, frame);
556 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
561 ves_array_calculate_index (MonoArray *ao, stackval *sp, MonoInvocation *frame)
563 g_assert (!frame->ex);
564 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
568 for (gint32 i = 0; i < ac->rank; i++) {
569 guint32 idx = sp [i].data.i;
570 guint32 lower = ao->bounds [i].lower_bound;
571 guint32 len = ao->bounds [i].length;
572 if (idx < lower || (idx - lower) >= len) {
573 frame->ex = mono_get_exception_index_out_of_range ();
574 FILL_IN_TRACE (frame->ex, frame);
577 pos = (pos * len) + idx - lower;
581 if (pos >= ao->max_length) {
582 frame->ex = mono_get_exception_index_out_of_range ();
583 FILL_IN_TRACE (frame->ex, frame);
591 ves_array_set (MonoInvocation *frame)
593 stackval *sp = frame->stack_args + 1;
595 MonoObject *o = frame->stack_args->data.p;
596 MonoArray *ao = (MonoArray *) o;
597 MonoClass *ac = o->vtable->klass;
599 g_assert (ac->rank >= 1);
601 gint32 pos = ves_array_calculate_index (ao, sp, frame);
605 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
607 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
608 mono_error_cleanup (&error);
610 frame->ex = mono_get_exception_array_type_mismatch ();
611 FILL_IN_TRACE (frame->ex, frame);
616 gint32 esize = mono_array_element_size (ac);
617 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
619 MonoType *mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
620 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
624 ves_array_get (MonoInvocation *frame)
626 stackval *sp = frame->stack_args + 1;
628 MonoObject *o = frame->stack_args->data.p;
629 MonoArray *ao = (MonoArray *) o;
630 MonoClass *ac = o->vtable->klass;
632 g_assert (ac->rank >= 1);
634 gint32 pos = ves_array_calculate_index (ao, sp, frame);
638 gint32 esize = mono_array_element_size (ac);
639 gpointer ea = mono_array_addr_with_size (ao, esize, pos);
641 MonoType *mt = mono_method_signature (frame->runtime_method->method)->ret;
642 stackval_from_data (mt, frame->retval, ea, FALSE);
646 ves_array_element_address (MonoInvocation *frame, MonoClass *required_type, MonoArray *ao, stackval *sp, gboolean needs_typecheck)
648 MonoClass *ac = ((MonoObject *) ao)->vtable->klass;
650 g_assert (ac->rank >= 1);
652 gint32 pos = ves_array_calculate_index (ao, sp, frame);
656 if (needs_typecheck && !mono_class_is_assignable_from (mono_object_class ((MonoObject *) ao)->element_class, required_type->element_class)) {
657 frame->ex = mono_get_exception_array_type_mismatch ();
658 FILL_IN_TRACE (frame->ex, frame);
661 gint32 esize = mono_array_element_size (ac);
662 return mono_array_addr_with_size (ao, esize, pos);
666 interp_walk_stack_with_ctx (MonoInternalStackWalk func, MonoContext *ctx, MonoUnwindOptions options, void *user_data)
669 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
671 MonoInvocation *frame = context->current_frame;
674 MonoStackFrameInfo fi;
675 memset (&fi, 0, sizeof (MonoStackFrameInfo));
677 /* TODO: hack to make some asserts happy. */
678 fi.ji = (MonoJitInfo *) frame->runtime_method;
680 if (frame->runtime_method)
681 fi.method = fi.actual_method = frame->runtime_method->method;
683 if (!fi.method || (fi.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (fi.method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
685 fi.type = FRAME_TYPE_MANAGED_TO_NATIVE;
687 MonoMethodHeader *hd = mono_method_get_header_checked (fi.method, &error);
688 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
689 fi.type = FRAME_TYPE_MANAGED;
690 fi.il_offset = frame->ip - (const unsigned short *) hd->code;
691 if (!fi.method->wrapper_type)
695 if (func (&fi, ctx, user_data))
697 frame = frame->parent;
701 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
703 struct _MethodArguments {
712 typedef struct _MethodArguments MethodArguments;
714 // TODO: this function is also arch dependent (register width).
715 static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
717 // TODO: don't malloc this data structure.
718 MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
723 for (int i = 0; i < sig->param_count; i++) {
724 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
726 case MONO_TYPE_BOOLEAN:
737 case MONO_TYPE_SZARRAY:
738 case MONO_TYPE_CLASS:
739 case MONO_TYPE_OBJECT:
740 case MONO_TYPE_STRING:
742 case MONO_TYPE_VALUETYPE:
743 case MONO_TYPE_GENERICINST:
751 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
756 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
759 margs->fargs = g_malloc0 (sizeof (double) * margs->flen);
762 g_error ("build_args_from_sig: TODO, allocate gregs: %d\n", margs->ilen);
765 g_error ("build_args_from_sig: TODO, allocate fregs: %d\n", margs->flen);
772 margs->iargs [0] = frame->stack_args->data.p;
776 for (int i = 0; i < sig->param_count; i++) {
777 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
779 case MONO_TYPE_BOOLEAN:
790 case MONO_TYPE_SZARRAY:
791 case MONO_TYPE_CLASS:
792 case MONO_TYPE_OBJECT:
793 case MONO_TYPE_STRING:
795 case MONO_TYPE_VALUETYPE:
796 case MONO_TYPE_GENERICINST:
797 margs->iargs [int_i] = frame->stack_args [i].data.p;
799 g_print ("build_args_from_sig: margs->iargs [%d]: %p (frame @ %d)\n", int_i, margs->iargs [int_i], i);
805 if (ptype == MONO_TYPE_R4)
806 * (float *) &(margs->fargs [int_f]) = (float) frame->stack_args [i].data.f;
808 margs->fargs [int_f] = frame->stack_args [i].data.f;
810 g_print ("build_args_from_sig: margs->fargs [%d]: %p (%f) (frame @ %d)\n", int_f, margs->fargs [int_f], margs->fargs [int_f], i);
815 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
819 switch (sig->ret->type) {
820 case MONO_TYPE_BOOLEAN:
831 case MONO_TYPE_SZARRAY:
832 case MONO_TYPE_CLASS:
833 case MONO_TYPE_OBJECT:
834 case MONO_TYPE_STRING:
836 case MONO_TYPE_VALUETYPE:
837 case MONO_TYPE_GENERICINST:
838 margs->retval = &(frame->retval->data.p);
842 margs->retval = &(frame->retval->data.p);
843 margs->is_float_ret = 1;
846 margs->retval = NULL;
849 g_error ("build_args_from_sig: ret type not implemented yet: 0x%x\n", sig->ret->type);
856 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
859 MonoInvocation *old_frame = context->current_frame;
860 MonoInvocation *old_env_frame = context->env_frame;
861 jmp_buf *old_env = context->current_env;
864 context->current_frame = old_frame;
865 context->env_frame = old_env_frame;
866 context->current_env = old_env;
867 context->managed_code = 1;
872 context->env_frame = frame;
873 context->current_env = &env;
875 g_assert (!frame->runtime_method);
876 if (!mono_interp_enter_icall_trampoline) {
878 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
880 // mono_tramp_info_register (info, NULL);
883 MethodArguments *margs = build_args_from_sig (sig, frame);
885 g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
886 g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
889 context->current_frame = frame;
890 context->managed_code = 0;
892 mono_interp_enter_icall_trampoline (addr, margs);
894 context->managed_code = 1;
895 /* domain can only be changed by native code */
896 context->domain = mono_domain_get ();
898 if (*mono_thread_interruption_request_flag ()) {
899 MonoException *exc = mono_thread_interruption_checkpoint ();
902 context->search_for_handler = 1;
906 if (!frame->ex && !MONO_TYPE_ISSTRUCT (sig->ret))
907 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
909 context->current_frame = old_frame;
910 context->env_frame = old_env_frame;
911 context->current_env = old_env;
913 g_free (margs->iargs);
914 g_free (margs->fargs);
919 mono_interp_init_delegate (MonoDelegate *del)
922 del->method = ((RuntimeMethod *) del->method_ptr)->method;
927 * runtime specifies that the implementation of the method is automatically
928 * provided by the runtime and is primarily used for the methods of delegates.
931 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
933 MonoMethod *method = frame->runtime_method->method;
934 const char *name = method->name;
935 MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
936 MonoObject *isinst_obj;
939 mono_class_init (method->klass);
941 if (method->klass == mono_defaults.array_class) {
942 if (!strcmp (method->name, "UnsafeMov")) {
943 /* TODO: layout checks */
944 MonoType *mt = mono_method_signature (method)->ret;
945 stackval_from_data (mt, frame->retval, (char *) frame->stack_args, FALSE);
950 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
951 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
952 if (obj && isinst_obj) {
953 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
954 ves_array_set (frame);
957 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
958 ves_array_get (frame);
963 g_error ("Don't know how to exec runtime method %s.%s::%s",
964 method->klass->name_space, method->klass->name,
969 dump_stack (stackval *stack, stackval *sp)
972 GString *str = g_string_new ("");
975 return g_string_free (str, FALSE);
978 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
981 return g_string_free (str, FALSE);
985 dump_stackval (GString *str, stackval *s, MonoType *type)
987 switch (type->type) {
995 case MONO_TYPE_BOOLEAN:
996 g_string_append_printf (str, "[%d] ", s->data.i);
998 case MONO_TYPE_STRING:
999 case MONO_TYPE_SZARRAY:
1000 case MONO_TYPE_CLASS:
1001 case MONO_TYPE_OBJECT:
1002 case MONO_TYPE_ARRAY:
1006 g_string_append_printf (str, "[%p] ", s->data.p);
1008 case MONO_TYPE_VALUETYPE:
1009 if (type->data.klass->enumtype)
1010 g_string_append_printf (str, "[%d] ", s->data.i);
1012 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1016 g_string_append_printf (str, "[%g] ", s->data.f);
1021 GString *res = g_string_new ("");
1022 mono_type_get_desc (res, type, TRUE);
1023 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1024 g_string_free (res, TRUE);
1031 dump_args (MonoInvocation *inv)
1033 GString *str = g_string_new ("");
1035 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1037 if (signature->param_count == 0 && !signature->hasthis)
1038 return g_string_free (str, FALSE);
1040 if (signature->hasthis) {
1041 MonoMethod *method = inv->runtime_method->method;
1042 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1045 for (i = 0; i < signature->param_count; ++i)
1046 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1048 return g_string_free (str, FALSE);
1052 dump_retval (MonoInvocation *inv)
1054 GString *str = g_string_new ("");
1055 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1057 if (ret->type != MONO_TYPE_VOID)
1058 dump_stackval (str, inv->retval, ret);
1060 return g_string_free (str, FALSE);
1064 dump_frame (MonoInvocation *inv)
1066 GString *str = g_string_new ("");
1071 for (i = 0; inv; inv = inv->parent) {
1072 if (inv->runtime_method != NULL) {
1073 MonoMethod *method = inv->runtime_method->method;
1077 const char * opname = "";
1079 gchar *source = NULL;
1083 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1084 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1085 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1086 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1090 opname = mono_interp_opname [*inv->ip];
1091 codep = inv->ip - inv->runtime_method->code;
1092 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1097 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1098 source = mono_debug_method_lookup_location (minfo, codep);
1102 args = dump_args (inv);
1103 name = mono_method_full_name (method, TRUE);
1105 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1107 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1114 return g_string_free (str, FALSE);
1118 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1122 MonoInvocation *inv;
1125 for (i = 0, inv = top; inv; inv = inv->parent)
1126 if (inv->runtime_method != NULL)
1129 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1130 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1132 for (i = 0, inv = top; inv; inv = inv->parent)
1133 if (inv->runtime_method != NULL) {
1134 mono_array_set (res, gpointer, i, inv->runtime_method);
1136 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1144 #define MYGUINT64_MAX 18446744073709551615ULL
1145 #define MYGINT64_MAX 9223372036854775807LL
1146 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1148 #define MYGUINT32_MAX 4294967295U
1149 #define MYGINT32_MAX 2147483647
1150 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1152 #define CHECK_ADD_OVERFLOW(a,b) \
1153 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1154 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1156 #define CHECK_SUB_OVERFLOW(a,b) \
1157 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1158 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1160 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1161 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1163 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1164 (guint32)(a) < (guint32)(b) ? -1 : 0
1166 #define CHECK_ADD_OVERFLOW64(a,b) \
1167 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1168 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1170 #define CHECK_SUB_OVERFLOW64(a,b) \
1171 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1172 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1174 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1175 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1177 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1178 (guint64)(a) < (guint64)(b) ? -1 : 0
1180 #if SIZEOF_VOID_P == 4
1181 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1182 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1184 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1185 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1188 /* Resolves to TRUE if the operands would overflow */
1189 #define CHECK_MUL_OVERFLOW(a,b) \
1190 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1191 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1192 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1193 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1194 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1195 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1196 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1198 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1199 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1200 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1202 #define CHECK_MUL_OVERFLOW64(a,b) \
1203 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1204 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1205 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1206 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1207 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1208 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1209 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1211 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1212 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1213 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1215 #if SIZEOF_VOID_P == 4
1216 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1217 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1219 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1220 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1224 mono_interp_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1226 MonoInvocation frame;
1227 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1228 MonoObject *retval = NULL;
1229 MonoMethodSignature *sig = mono_method_signature (method);
1230 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1231 int i, type, isobject = 0;
1234 stackval *args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
1235 ThreadContext context_struct;
1236 MonoInvocation *old_frame = NULL;
1246 if (context != &context_struct) {
1247 context->domain = mono_domain_get ();
1248 context->current_frame = old_frame;
1249 context->managed_code = 0;
1251 mono_native_tls_set_value (thread_context_id, NULL);
1253 *exc = (MonoObject *)frame.ex;
1257 if (context == NULL) {
1258 context = &context_struct;
1259 context_struct.base_frame = &frame;
1260 context_struct.current_frame = NULL;
1261 context_struct.env_frame = &frame;
1262 context_struct.current_env = &env;
1263 context_struct.search_for_handler = 0;
1264 context_struct.managed_code = 0;
1265 mono_native_tls_set_value (thread_context_id, context);
1268 old_frame = context->current_frame;
1270 context->domain = mono_domain_get ();
1272 switch (sig->ret->type) {
1273 case MONO_TYPE_VOID:
1275 case MONO_TYPE_STRING:
1276 case MONO_TYPE_OBJECT:
1277 case MONO_TYPE_CLASS:
1278 case MONO_TYPE_ARRAY:
1279 case MONO_TYPE_SZARRAY:
1282 case MONO_TYPE_VALUETYPE:
1283 retval = mono_object_new_checked (context->domain, klass, error);
1284 ret = mono_object_unbox (retval);
1285 if (!sig->ret->data.klass->enumtype)
1286 result.data.vt = ret;
1289 retval = mono_object_new_checked (context->domain, mono_defaults.int_class, error);
1290 ret = mono_object_unbox (retval);
1293 retval = mono_object_new_checked (context->domain, klass, error);
1294 ret = ((char*)retval) + sizeof (MonoObject);
1299 args [0].data.p = obj;
1301 for (i = 0; i < sig->param_count; ++i) {
1302 int a_index = i + !!sig->hasthis;
1303 if (sig->params [i]->byref) {
1304 args [a_index].data.p = params [i];
1307 type = sig->params [i]->type;
1312 case MONO_TYPE_BOOLEAN:
1313 args [a_index].data.i = *(MonoBoolean*)params [i];
1317 case MONO_TYPE_CHAR:
1318 args [a_index].data.i = *(gint16*)params [i];
1320 #if SIZEOF_VOID_P == 4
1321 case MONO_TYPE_U: /* use VAL_POINTER? */
1326 args [a_index].data.i = *(gint32*)params [i];
1328 #if SIZEOF_VOID_P == 8
1334 args [a_index].data.l = *(gint64*)params [i];
1336 case MONO_TYPE_VALUETYPE:
1337 if (sig->params [i]->data.klass->enumtype) {
1338 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1341 args [a_index].data.p = params [i];
1344 case MONO_TYPE_STRING:
1346 case MONO_TYPE_CLASS:
1347 case MONO_TYPE_ARRAY:
1348 case MONO_TYPE_SZARRAY:
1349 case MONO_TYPE_OBJECT:
1350 case MONO_TYPE_GENERICINST:
1351 args [a_index].data.p = params [i];
1354 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1358 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1359 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1360 INIT_FRAME (&frame,context->current_frame,args,&result,mono_get_root_domain (),method,error);
1362 frame.invoke_trap = 1;
1363 context->managed_code = 1;
1364 ves_exec_method_with_context (&frame, context);
1365 context->managed_code = 0;
1366 if (context == &context_struct)
1367 mono_native_tls_set_value (thread_context_id, NULL);
1369 context->current_frame = old_frame;
1370 if (frame.ex != NULL) {
1372 *exc = (MonoObject*) frame.ex;
1375 if (context->current_env != NULL) {
1376 context->env_frame->ex = frame.ex;
1377 longjmp(*context->current_env, 1);
1380 printf("dropped exception...\n");
1382 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1384 if (isobject || method->string_ctor)
1385 return result.data.p;
1386 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1391 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1393 MonoInvocation *old_frame = context->current_frame;
1394 MonoInvocation *old_env_frame = context->env_frame;
1395 jmp_buf *old_env = context->current_env;
1399 context->current_frame = old_frame;
1400 context->env_frame = old_env_frame;
1401 context->current_env = old_env;
1402 context->managed_code = 1;
1406 context->env_frame = context->current_frame;
1407 context->current_env = &env;
1408 context->managed_code = 0;
1411 case MINT_ICALL_V_V: {
1412 void (*func)() = ptr;
1416 case MINT_ICALL_V_P: {
1417 gpointer (*func)() = ptr;
1419 sp [-1].data.p = func ();
1422 case MINT_ICALL_P_V: {
1423 void (*func)(gpointer) = ptr;
1424 func (sp [-1].data.p);
1428 case MINT_ICALL_P_P: {
1429 gpointer (*func)(gpointer) = ptr;
1430 sp [-1].data.p = func (sp [-1].data.p);
1433 case MINT_ICALL_PP_V: {
1434 void (*func)(gpointer,gpointer) = ptr;
1436 func (sp [0].data.p, sp [1].data.p);
1439 case MINT_ICALL_PI_V: {
1440 void (*func)(gpointer,int) = ptr;
1442 func (sp [0].data.p, sp [1].data.i);
1445 case MINT_ICALL_PP_P: {
1446 gpointer (*func)(gpointer,gpointer) = ptr;
1448 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1451 case MINT_ICALL_PI_P: {
1452 gpointer (*func)(gpointer,int) = ptr;
1454 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1457 case MINT_ICALL_PPP_V: {
1458 void (*func)(gpointer,gpointer,gpointer) = ptr;
1460 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1463 case MINT_ICALL_PPI_V: {
1464 void (*func)(gpointer,gpointer,int) = ptr;
1466 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1470 g_assert_not_reached ();
1473 context->env_frame = old_env_frame;
1474 context->current_env = old_env;
1479 static mono_mutex_t create_method_pointer_mutex;
1481 static GHashTable *method_pointer_hash = NULL;
1483 #define TRAMPS_USED 8
1485 static MonoMethod *method_pointers [TRAMPS_USED] = {0};
1487 #define GEN_METHOD_PTR_TRAMP(num) \
1488 static MonoObject * mp_tramp_ ## num (MonoObject *this_obj, void **params, MonoObject **exc, void *compiled_method) { \
1490 void *params_real[] = {this_obj, ¶ms, &exc, &compiled_method}; \
1491 MonoObject *ret = mono_interp_runtime_invoke (method_pointers [num], NULL, params_real, NULL, &error); \
1492 mono_error_cleanup (&error); \
1497 GEN_METHOD_PTR_TRAMP (0);
1498 GEN_METHOD_PTR_TRAMP (1);
1499 GEN_METHOD_PTR_TRAMP (2);
1500 GEN_METHOD_PTR_TRAMP (3);
1501 GEN_METHOD_PTR_TRAMP (4);
1502 GEN_METHOD_PTR_TRAMP (5);
1503 GEN_METHOD_PTR_TRAMP (6);
1504 GEN_METHOD_PTR_TRAMP (7);
1506 #undef GEN_METHOD_PTR_TRAMP
1508 gpointer *mp_tramps[TRAMPS_USED] = {
1509 (gpointer) mp_tramp_0, (gpointer) mp_tramp_1, (gpointer) mp_tramp_2, (gpointer) mp_tramp_3,
1510 (gpointer) mp_tramp_4, (gpointer) mp_tramp_5, (gpointer) mp_tramp_6, (gpointer) mp_tramp_7
1513 static int tramps_used = 0;
1516 mono_interp_create_method_pointer (MonoMethod *method, MonoError *error)
1521 mono_os_mutex_lock (&create_method_pointer_mutex);
1522 if (!method_pointer_hash) {
1523 // FIXME: is registering method table as GC root really necessary?
1524 // MONO_GC_REGISTER_ROOT_FIXED (method_pointer_hash);
1525 method_pointer_hash = g_hash_table_new (NULL, NULL);
1527 addr = g_hash_table_lookup (method_pointer_hash, method);
1529 mono_os_mutex_unlock (&create_method_pointer_mutex);
1534 * If it is a static P/Invoke method, we can just return the pointer
1535 * to the method implementation.
1537 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && ((MonoMethodPInvoke*) method)->addr) {
1538 ji = g_new0 (MonoJitInfo, 1);
1539 ji->d.method = method;
1541 ji->code_start = addr = ((MonoMethodPInvoke*) method)->addr;
1543 mono_jit_info_table_add (mono_get_root_domain (), ji);
1546 g_assert (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE);
1547 g_assert (tramps_used < TRAMPS_USED);
1549 /* FIXME: needs locking */
1550 method_pointers [tramps_used] = method;
1551 addr = mp_tramps [tramps_used];
1555 g_hash_table_insert (method_pointer_hash, method, addr);
1556 mono_os_mutex_unlock (&create_method_pointer_mutex);
1562 static int opcode_counts[512];
1564 #define COUNT_OP(op) opcode_counts[op]++
1566 #define COUNT_OP(op)
1570 #define DUMP_INSTR() \
1571 if (tracing > 1) { \
1573 if (sp > frame->stack) { \
1574 ins = dump_stack (frame->stack, sp); \
1576 ins = g_strdup (""); \
1580 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
1581 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
1583 mono_interp_dis_mintop(rtm->code, ip); \
1584 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1588 #define DUMP_INSTR()
1592 #define USE_COMPUTED_GOTO 1
1594 #if USE_COMPUTED_GOTO
1595 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1596 #define MINT_IN_CASE(x) LAB_ ## x:
1598 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1600 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1602 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
1604 #define MINT_IN_SWITCH(op) switch (op)
1605 #define MINT_IN_CASE(x) case x:
1606 #define MINT_IN_BREAK break
1607 #define MINT_IN_DEFAULT default:
1611 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1613 MonoInvocation child_frame;
1614 GSList *finally_ips = NULL;
1615 const unsigned short *endfinally_ip = NULL;
1616 const unsigned short *ip = NULL;
1617 register stackval *sp;
1620 gint tracing = global_tracing;
1621 unsigned char *vtalloc;
1624 unsigned char *vt_sp;
1625 unsigned char *locals;
1627 MonoObject *o = NULL;
1629 #if USE_COMPUTED_GOTO
1630 static void *in_labels[] = {
1631 #define OPDEF(a,b,c,d) \
1633 #include "mintops.def"
1638 frame->ex_handler = NULL;
1640 context->current_frame = frame;
1643 debug_enter (frame, &tracing);
1646 if (!frame->runtime_method->transformed) {
1647 context->managed_code = 0;
1649 char *mn = mono_method_full_name (frame->runtime_method->method, TRUE);
1650 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
1653 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1654 context->managed_code = 1;
1662 rtm = frame->runtime_method;
1663 frame->args = alloca (rtm->alloca_size);
1664 memset (frame->args, 0, rtm->alloca_size);
1666 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1667 memset (sp, 0, rtm->stack_size);
1669 vt_sp = (unsigned char *) sp + rtm->stack_size;
1670 memset (vt_sp, 0, rtm->vt_stack_size);
1675 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1676 memset (vt_sp, 0, rtm->locals_size);
1678 child_frame.parent = frame;
1684 * using while (ip < end) may result in a 15% performance drop,
1685 * but it may be useful for debug
1689 /* g_assert (sp >= frame->stack); */
1690 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1692 MINT_IN_SWITCH (*ip) {
1693 MINT_IN_CASE(MINT_INITLOCALS)
1694 memset (locals, 0, rtm->locals_size);
1697 MINT_IN_CASE(MINT_NOP)
1700 MINT_IN_CASE(MINT_BREAK)
1702 G_BREAKPOINT (); /* this is not portable... */
1704 MINT_IN_CASE(MINT_LDNULL)
1709 MINT_IN_CASE(MINT_VTRESULT) {
1710 int ret_size = * (guint16 *)(ip + 1);
1711 unsigned char *ret_vt_sp = vt_sp;
1712 vt_sp -= READ32(ip + 2);
1714 memmove (vt_sp, ret_vt_sp, ret_size);
1715 sp [-1].data.p = vt_sp;
1716 vt_sp += (ret_size + 7) & ~7;
1721 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1722 MINT_IN_CASE(MINT_LDC_I4_M1)
1725 MINT_IN_CASE(MINT_LDC_I4_0)
1728 MINT_IN_CASE(MINT_LDC_I4_1)
1731 MINT_IN_CASE(MINT_LDC_I4_2)
1734 MINT_IN_CASE(MINT_LDC_I4_3)
1737 MINT_IN_CASE(MINT_LDC_I4_4)
1740 MINT_IN_CASE(MINT_LDC_I4_5)
1743 MINT_IN_CASE(MINT_LDC_I4_6)
1746 MINT_IN_CASE(MINT_LDC_I4_7)
1749 MINT_IN_CASE(MINT_LDC_I4_8)
1752 MINT_IN_CASE(MINT_LDC_I4_S)
1753 sp->data.i = *(const short *)(ip + 1);
1757 MINT_IN_CASE(MINT_LDC_I4)
1759 sp->data.i = READ32 (ip);
1763 MINT_IN_CASE(MINT_LDC_I8)
1765 sp->data.l = READ64 (ip);
1769 MINT_IN_CASE(MINT_LDC_R4) {
1773 sp->data.f = * (float *)&val;
1778 MINT_IN_CASE(MINT_LDC_R8)
1779 sp->data.l = READ64 (ip + 1); /* note union usage */
1783 MINT_IN_CASE(MINT_DUP)
1788 MINT_IN_CASE(MINT_DUP_VT)
1789 i32 = READ32 (ip + 1);
1791 memcpy(sp->data.p, sp [-1].data.p, i32);
1792 vt_sp += (i32 + 7) & ~7;
1796 MINT_IN_CASE(MINT_POP) {
1797 guint16 u16 = (* (guint16 *)(ip + 1)) + 1;
1799 memmove (sp - u16, sp - 1, (u16 - 1) * sizeof (stackval));
1804 MINT_IN_CASE(MINT_JMP) {
1805 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1806 if (!new_method->transformed) {
1808 frame->ex = mono_interp_transform_method (new_method, context);
1813 if (new_method->alloca_size > rtm->alloca_size)
1814 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1815 rtm = frame->runtime_method = new_method;
1816 vt_sp = (unsigned char *) sp + rtm->stack_size;
1820 locals = vt_sp + rtm->vt_stack_size;
1821 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1824 MINT_IN_CASE(MINT_CALLI) {
1825 MonoMethodSignature *csignature;
1826 stackval *endsp = sp;
1830 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1834 child_frame.runtime_method = sp->data.p;
1837 child_frame.retval = sp;
1838 /* decrement by the actual number of args */
1839 sp -= csignature->param_count;
1840 if (csignature->hasthis)
1842 child_frame.stack_args = sp;
1844 /* `this' can be NULL for string:.ctor */
1845 if (csignature->hasthis && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
1846 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1847 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1848 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1849 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1850 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1853 if (csignature->hasthis) {
1854 MonoObject *this_arg = sp->data.p;
1856 if (this_arg->vtable->klass->valuetype) {
1857 gpointer *unboxed = mono_object_unbox (this_arg);
1858 sp [0].data.p = unboxed;
1862 ves_exec_method_with_context (&child_frame, context);
1864 context->current_frame = frame;
1866 if (child_frame.ex) {
1868 * An exception occurred, need to run finally, fault and catch handlers..
1870 frame->ex = child_frame.ex;
1871 goto handle_finally;
1874 /* need to handle typedbyref ... */
1875 if (csignature->ret->type != MONO_TYPE_VOID) {
1881 MINT_IN_CASE(MINT_CALLI_NAT) {
1882 MonoMethodSignature *csignature;
1883 stackval *endsp = sp;
1884 unsigned char *code = NULL;
1888 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1893 child_frame.runtime_method = NULL;
1896 child_frame.retval = sp;
1897 /* decrement by the actual number of args */
1898 sp -= csignature->param_count;
1899 if (csignature->hasthis)
1901 child_frame.stack_args = sp;
1902 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1904 context->current_frame = frame;
1906 if (child_frame.ex) {
1908 * An exception occurred, need to run finally, fault and catch handlers..
1910 frame->ex = child_frame.ex;
1911 if (context->search_for_handler) {
1912 context->search_for_handler = 0;
1913 goto handle_exception;
1915 goto handle_finally;
1918 /* need to handle typedbyref ... */
1919 if (csignature->ret->type != MONO_TYPE_VOID) {
1925 MINT_IN_CASE(MINT_CALL) {
1926 stackval *endsp = sp;
1930 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1933 child_frame.retval = sp;
1934 /* decrement by the actual number of args */
1935 sp -= child_frame.runtime_method->param_count;
1936 if (child_frame.runtime_method->hasthis)
1938 child_frame.stack_args = sp;
1940 /* `this' can be NULL for string:.ctor */
1941 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && sp->data.p && mono_object_is_transparent_proxy (sp->data.p)) {
1942 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1943 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1945 ves_exec_method_with_context (&child_frame, context);
1947 context->current_frame = frame;
1949 if (child_frame.ex) {
1951 * An exception occurred, need to run finally, fault and catch handlers..
1953 frame->ex = child_frame.ex;
1954 goto handle_exception;;
1957 /* need to handle typedbyref ... */
1962 MINT_IN_CASE(MINT_VCALL) {
1965 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1969 child_frame.retval = sp;
1970 /* decrement by the actual number of args */
1971 sp -= child_frame.runtime_method->param_count;
1972 if (child_frame.runtime_method->hasthis) {
1974 MonoObject *this_arg = sp->data.p;
1976 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1978 child_frame.stack_args = sp;
1980 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->method->klass->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
1981 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1982 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1985 ves_exec_method_with_context (&child_frame, context);
1987 context->current_frame = frame;
1989 if (child_frame.ex) {
1991 * An exception occurred, need to run finally, fault and catch handlers..
1993 frame->ex = child_frame.ex;
1994 goto handle_finally;
1998 MINT_IN_CASE(MINT_CALLVIRT) {
1999 stackval *endsp = sp;
2000 MonoObject *this_arg;
2005 token = * (unsigned short *)(ip + 1);
2007 child_frame.runtime_method = rtm->data_items [token];
2009 child_frame.retval = sp;
2011 /* decrement by the actual number of args */
2012 sp -= child_frame.runtime_method->param_count + 1;
2013 child_frame.stack_args = sp;
2014 this_arg = sp->data.p;
2016 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2017 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2019 MonoClass *this_class = this_arg->vtable->klass;
2020 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2022 gpointer *unboxed = mono_object_unbox (this_arg);
2023 sp [0].data.p = unboxed;
2026 ves_exec_method_with_context (&child_frame, context);
2028 context->current_frame = frame;
2030 if (child_frame.ex) {
2032 * An exception occurred, need to run finally, fault and catch handlers..
2034 frame->ex = child_frame.ex;
2035 if (context->search_for_handler) {
2036 context->search_for_handler = 0;
2037 goto handle_exception;
2039 goto handle_finally;
2042 /* need to handle typedbyref ... */
2047 MINT_IN_CASE(MINT_VCALLVIRT) {
2048 MonoObject *this_arg;
2053 token = * (unsigned short *)(ip + 1);
2055 child_frame.runtime_method = rtm->data_items [token];
2057 child_frame.retval = sp;
2059 /* decrement by the actual number of args */
2060 sp -= child_frame.runtime_method->param_count + 1;
2061 child_frame.stack_args = sp;
2062 this_arg = sp->data.p;
2064 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2065 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2067 MonoClass *this_class = this_arg->vtable->klass;
2068 if (this_class->valuetype && child_frame.runtime_method->method->klass->valuetype) {
2069 gpointer *unboxed = mono_object_unbox (this_arg);
2070 sp [0].data.p = unboxed;
2073 ves_exec_method_with_context (&child_frame, context);
2075 context->current_frame = frame;
2077 if (child_frame.ex) {
2079 * An exception occurred, need to run finally, fault and catch handlers..
2081 frame->ex = child_frame.ex;
2082 if (context->search_for_handler) {
2083 context->search_for_handler = 0;
2084 goto handle_exception;
2086 goto handle_finally;
2090 MINT_IN_CASE(MINT_CALLRUN)
2091 ves_runtime_method (frame, context);
2094 goto handle_exception;
2097 MINT_IN_CASE(MINT_RET)
2099 *frame->retval = *sp;
2100 if (sp > frame->stack)
2101 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2103 MINT_IN_CASE(MINT_RET_VOID)
2104 if (sp > frame->stack)
2105 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
2107 MINT_IN_CASE(MINT_RET_VT)
2108 i32 = READ32(ip + 1);
2110 memcpy(frame->retval->data.p, sp->data.p, i32);
2111 if (sp > frame->stack)
2112 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2114 MINT_IN_CASE(MINT_BR_S)
2115 ip += (short) *(ip + 1);
2117 MINT_IN_CASE(MINT_BR)
2118 ip += (gint32) READ32(ip + 1);
2120 #define ZEROP_S(datamem, op) \
2122 if (sp->data.datamem op 0) \
2123 ip += * (gint16 *)(ip + 1); \
2127 #define ZEROP(datamem, op) \
2129 if (sp->data.datamem op 0) \
2130 ip += READ32(ip + 1); \
2134 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2137 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2140 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2143 MINT_IN_CASE(MINT_BRFALSE_I4)
2146 MINT_IN_CASE(MINT_BRFALSE_I8)
2149 MINT_IN_CASE(MINT_BRFALSE_R8)
2152 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2155 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2158 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2161 MINT_IN_CASE(MINT_BRTRUE_I4)
2164 MINT_IN_CASE(MINT_BRTRUE_I8)
2167 MINT_IN_CASE(MINT_BRTRUE_R8)
2170 #define CONDBR_S(cond) \
2173 ip += * (gint16 *)(ip + 1); \
2176 #define BRELOP_S(datamem, op) \
2177 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2179 #define CONDBR(cond) \
2182 ip += READ32(ip + 1); \
2186 #define BRELOP(datamem, op) \
2187 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2189 MINT_IN_CASE(MINT_BEQ_I4_S)
2192 MINT_IN_CASE(MINT_BEQ_I8_S)
2195 MINT_IN_CASE(MINT_BEQ_R8_S)
2196 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2198 MINT_IN_CASE(MINT_BEQ_I4)
2201 MINT_IN_CASE(MINT_BEQ_I8)
2204 MINT_IN_CASE(MINT_BEQ_R8)
2205 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2207 MINT_IN_CASE(MINT_BGE_I4_S)
2210 MINT_IN_CASE(MINT_BGE_I8_S)
2213 MINT_IN_CASE(MINT_BGE_R8_S)
2214 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2216 MINT_IN_CASE(MINT_BGE_I4)
2219 MINT_IN_CASE(MINT_BGE_I8)
2222 MINT_IN_CASE(MINT_BGE_R8)
2223 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2225 MINT_IN_CASE(MINT_BGT_I4_S)
2228 MINT_IN_CASE(MINT_BGT_I8_S)
2231 MINT_IN_CASE(MINT_BGT_R8_S)
2232 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2234 MINT_IN_CASE(MINT_BGT_I4)
2237 MINT_IN_CASE(MINT_BGT_I8)
2240 MINT_IN_CASE(MINT_BGT_R8)
2241 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2243 MINT_IN_CASE(MINT_BLT_I4_S)
2246 MINT_IN_CASE(MINT_BLT_I8_S)
2249 MINT_IN_CASE(MINT_BLT_R8_S)
2250 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2252 MINT_IN_CASE(MINT_BLT_I4)
2255 MINT_IN_CASE(MINT_BLT_I8)
2258 MINT_IN_CASE(MINT_BLT_R8)
2259 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2261 MINT_IN_CASE(MINT_BLE_I4_S)
2264 MINT_IN_CASE(MINT_BLE_I8_S)
2267 MINT_IN_CASE(MINT_BLE_R8_S)
2268 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2270 MINT_IN_CASE(MINT_BLE_I4)
2273 MINT_IN_CASE(MINT_BLE_I8)
2276 MINT_IN_CASE(MINT_BLE_R8)
2277 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2279 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2282 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2285 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2286 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2288 MINT_IN_CASE(MINT_BNE_UN_I4)
2291 MINT_IN_CASE(MINT_BNE_UN_I8)
2294 MINT_IN_CASE(MINT_BNE_UN_R8)
2295 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2298 #define BRELOP_S_CAST(datamem, op, type) \
2300 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2301 ip += * (gint16 *)(ip + 1); \
2305 #define BRELOP_CAST(datamem, op, type) \
2307 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2308 ip += READ32(ip + 1); \
2312 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2313 BRELOP_S_CAST(i, >=, guint32);
2315 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2316 BRELOP_S_CAST(l, >=, guint64);
2318 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2319 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2321 MINT_IN_CASE(MINT_BGE_UN_I4)
2322 BRELOP_CAST(i, >=, guint32);
2324 MINT_IN_CASE(MINT_BGE_UN_I8)
2325 BRELOP_CAST(l, >=, guint64);
2327 MINT_IN_CASE(MINT_BGE_UN_R8)
2328 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2330 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2331 BRELOP_S_CAST(i, >, guint32);
2333 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2334 BRELOP_S_CAST(l, >, guint64);
2336 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2337 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2339 MINT_IN_CASE(MINT_BGT_UN_I4)
2340 BRELOP_CAST(i, >, guint32);
2342 MINT_IN_CASE(MINT_BGT_UN_I8)
2343 BRELOP_CAST(l, >, guint64);
2345 MINT_IN_CASE(MINT_BGT_UN_R8)
2346 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2348 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2349 BRELOP_S_CAST(i, <=, guint32);
2351 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2352 BRELOP_S_CAST(l, <=, guint64);
2354 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2355 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2357 MINT_IN_CASE(MINT_BLE_UN_I4)
2358 BRELOP_CAST(i, <=, guint32);
2360 MINT_IN_CASE(MINT_BLE_UN_I8)
2361 BRELOP_CAST(l, <=, guint64);
2363 MINT_IN_CASE(MINT_BLE_UN_R8)
2364 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2366 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2367 BRELOP_S_CAST(i, <, guint32);
2369 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2370 BRELOP_S_CAST(l, <, guint64);
2372 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2373 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2375 MINT_IN_CASE(MINT_BLT_UN_I4)
2376 BRELOP_CAST(i, <, guint32);
2378 MINT_IN_CASE(MINT_BLT_UN_I8)
2379 BRELOP_CAST(l, <, guint64);
2381 MINT_IN_CASE(MINT_BLT_UN_R8)
2382 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2384 MINT_IN_CASE(MINT_SWITCH) {
2386 const unsigned short *st;
2392 if ((guint32)sp->data.i < n) {
2394 ip += 2 * (guint32)sp->data.i;
2395 offset = READ32 (ip);
2402 MINT_IN_CASE(MINT_LDIND_I1)
2404 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2406 MINT_IN_CASE(MINT_LDIND_U1)
2408 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2410 MINT_IN_CASE(MINT_LDIND_I2)
2412 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2414 MINT_IN_CASE(MINT_LDIND_U2)
2416 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2418 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2419 MINT_IN_CASE(MINT_LDIND_U4)
2421 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2423 MINT_IN_CASE(MINT_LDIND_I8)
2425 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2427 MINT_IN_CASE(MINT_LDIND_I) {
2428 guint16 offset = * (guint16 *)(ip + 1);
2429 sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
2433 MINT_IN_CASE(MINT_LDIND_R4)
2435 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2437 MINT_IN_CASE(MINT_LDIND_R8)
2439 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2441 MINT_IN_CASE(MINT_LDIND_REF)
2443 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2445 MINT_IN_CASE(MINT_STIND_REF)
2448 * (gpointer *) sp->data.p = sp[1].data.p;
2450 MINT_IN_CASE(MINT_STIND_I1)
2453 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2455 MINT_IN_CASE(MINT_STIND_I2)
2458 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2460 MINT_IN_CASE(MINT_STIND_I4)
2463 * (gint32 *) sp->data.p = sp[1].data.i;
2465 MINT_IN_CASE(MINT_STIND_I)
2468 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2470 MINT_IN_CASE(MINT_STIND_I8)
2473 * (gint64 *) sp->data.p = sp[1].data.l;
2475 MINT_IN_CASE(MINT_STIND_R4)
2478 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2480 MINT_IN_CASE(MINT_STIND_R8)
2483 * (double *) sp->data.p = sp[1].data.f;
2485 #define BINOP(datamem, op) \
2487 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2489 MINT_IN_CASE(MINT_ADD_I4)
2492 MINT_IN_CASE(MINT_ADD_I8)
2495 MINT_IN_CASE(MINT_ADD_R8)
2498 MINT_IN_CASE(MINT_ADD1_I4)
2502 MINT_IN_CASE(MINT_SUB_I4)
2505 MINT_IN_CASE(MINT_SUB_I8)
2508 MINT_IN_CASE(MINT_SUB_R8)
2511 MINT_IN_CASE(MINT_SUB1_I4)
2515 MINT_IN_CASE(MINT_MUL_I4)
2518 MINT_IN_CASE(MINT_MUL_I8)
2521 MINT_IN_CASE(MINT_MUL_R8)
2524 MINT_IN_CASE(MINT_DIV_I4)
2525 if (sp [-1].data.i == 0)
2526 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2527 if (sp [-1].data.i == (-1))
2528 THROW_EX (mono_get_exception_overflow (), ip);
2531 MINT_IN_CASE(MINT_DIV_I8)
2532 if (sp [-1].data.l == 0)
2533 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2534 if (sp [-1].data.l == (-1))
2535 THROW_EX (mono_get_exception_overflow (), ip);
2538 MINT_IN_CASE(MINT_DIV_R8)
2542 #define BINOP_CAST(datamem, op, type) \
2544 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2546 MINT_IN_CASE(MINT_DIV_UN_I4)
2547 if (sp [-1].data.i == 0)
2548 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2549 BINOP_CAST(i, /, guint32);
2551 MINT_IN_CASE(MINT_DIV_UN_I8)
2552 if (sp [-1].data.l == 0)
2553 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2554 BINOP_CAST(l, /, guint64);
2556 MINT_IN_CASE(MINT_REM_I4)
2557 if (sp [-1].data.i == 0)
2558 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2559 if (sp [-1].data.i == (-1))
2560 THROW_EX (mono_get_exception_overflow (), ip);
2563 MINT_IN_CASE(MINT_REM_I8)
2564 if (sp [-1].data.l == 0)
2565 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2566 if (sp [-1].data.l == (-1))
2567 THROW_EX (mono_get_exception_overflow (), ip);
2570 MINT_IN_CASE(MINT_REM_R8)
2571 /* FIXME: what do we actually do here? */
2573 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2576 MINT_IN_CASE(MINT_REM_UN_I4)
2577 if (sp [-1].data.i == 0)
2578 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2579 BINOP_CAST(i, %, guint32);
2581 MINT_IN_CASE(MINT_REM_UN_I8)
2582 if (sp [-1].data.l == 0)
2583 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2584 BINOP_CAST(l, %, guint64);
2586 MINT_IN_CASE(MINT_AND_I4)
2589 MINT_IN_CASE(MINT_AND_I8)
2592 MINT_IN_CASE(MINT_OR_I4)
2595 MINT_IN_CASE(MINT_OR_I8)
2598 MINT_IN_CASE(MINT_XOR_I4)
2601 MINT_IN_CASE(MINT_XOR_I8)
2605 #define SHIFTOP(datamem, op) \
2607 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2610 MINT_IN_CASE(MINT_SHL_I4)
2613 MINT_IN_CASE(MINT_SHL_I8)
2616 MINT_IN_CASE(MINT_SHR_I4)
2619 MINT_IN_CASE(MINT_SHR_I8)
2622 MINT_IN_CASE(MINT_SHR_UN_I4)
2624 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2627 MINT_IN_CASE(MINT_SHR_UN_I8)
2629 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2632 MINT_IN_CASE(MINT_NEG_I4)
2633 sp [-1].data.i = - sp [-1].data.i;
2636 MINT_IN_CASE(MINT_NEG_I8)
2637 sp [-1].data.l = - sp [-1].data.l;
2640 MINT_IN_CASE(MINT_NEG_R8)
2641 sp [-1].data.f = - sp [-1].data.f;
2644 MINT_IN_CASE(MINT_NOT_I4)
2645 sp [-1].data.i = ~ sp [-1].data.i;
2648 MINT_IN_CASE(MINT_NOT_I8)
2649 sp [-1].data.l = ~ sp [-1].data.l;
2652 MINT_IN_CASE(MINT_CONV_I1_I4)
2653 sp [-1].data.i = (gint8)sp [-1].data.i;
2656 MINT_IN_CASE(MINT_CONV_I1_I8)
2657 sp [-1].data.i = (gint8)sp [-1].data.l;
2660 MINT_IN_CASE(MINT_CONV_I1_R8)
2661 sp [-1].data.i = (gint8)sp [-1].data.f;
2664 MINT_IN_CASE(MINT_CONV_U1_I4)
2665 sp [-1].data.i = (guint8)sp [-1].data.i;
2668 MINT_IN_CASE(MINT_CONV_U1_I8)
2669 sp [-1].data.i = (guint8)sp [-1].data.l;
2672 MINT_IN_CASE(MINT_CONV_U1_R8)
2673 sp [-1].data.i = (guint8)sp [-1].data.f;
2676 MINT_IN_CASE(MINT_CONV_I2_I4)
2677 sp [-1].data.i = (gint16)sp [-1].data.i;
2680 MINT_IN_CASE(MINT_CONV_I2_I8)
2681 sp [-1].data.i = (gint16)sp [-1].data.l;
2684 MINT_IN_CASE(MINT_CONV_I2_R8)
2685 sp [-1].data.i = (gint16)sp [-1].data.f;
2688 MINT_IN_CASE(MINT_CONV_U2_I4)
2689 sp [-1].data.i = (guint16)sp [-1].data.i;
2692 MINT_IN_CASE(MINT_CONV_U2_I8)
2693 sp [-1].data.i = (guint16)sp [-1].data.l;
2696 MINT_IN_CASE(MINT_CONV_U2_R8)
2697 sp [-1].data.i = (guint16)sp [-1].data.f;
2700 MINT_IN_CASE(MINT_CONV_I4_R8)
2701 sp [-1].data.i = (gint32)sp [-1].data.f;
2704 MINT_IN_CASE(MINT_CONV_U4_I8)
2705 MINT_IN_CASE(MINT_CONV_I4_I8)
2706 sp [-1].data.i = (gint32)sp [-1].data.l;
2709 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2710 sp [-2].data.i = (gint32)sp [-2].data.l;
2713 MINT_IN_CASE(MINT_CONV_U4_R8)
2714 sp [-1].data.i = (guint32)sp [-1].data.f;
2717 MINT_IN_CASE(MINT_CONV_I8_I4)
2718 sp [-1].data.l = sp [-1].data.i;
2721 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2722 sp [-2].data.l = sp [-2].data.i;
2725 MINT_IN_CASE(MINT_CONV_I8_U4)
2726 sp [-1].data.l = (guint32)sp [-1].data.i;
2729 MINT_IN_CASE(MINT_CONV_I8_R8)
2730 sp [-1].data.l = (gint64)sp [-1].data.f;
2733 MINT_IN_CASE(MINT_CONV_R4_I4)
2734 sp [-1].data.f = (float)sp [-1].data.i;
2737 MINT_IN_CASE(MINT_CONV_R4_I8)
2738 sp [-1].data.f = (float)sp [-1].data.l;
2741 MINT_IN_CASE(MINT_CONV_R4_R8)
2742 sp [-1].data.f = (float)sp [-1].data.f;
2745 MINT_IN_CASE(MINT_CONV_R8_I4)
2746 sp [-1].data.f = (double)sp [-1].data.i;
2749 MINT_IN_CASE(MINT_CONV_R8_I8)
2750 sp [-1].data.f = (double)sp [-1].data.l;
2753 MINT_IN_CASE(MINT_CONV_U8_I4)
2754 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2757 MINT_IN_CASE(MINT_CONV_U8_R8)
2758 sp [-1].data.l = (guint64)sp [-1].data.f;
2761 MINT_IN_CASE(MINT_CPOBJ) {
2762 c = rtm->data_items[* (guint16 *)(ip + 1)];
2763 g_assert (c->byval_arg.type == MONO_TYPE_VALUETYPE);
2764 stackval_from_data (&c->byval_arg, &sp [-2], sp [-1].data.p, FALSE);
2769 MINT_IN_CASE(MINT_LDOBJ) {
2771 c = rtm->data_items[* (guint16 *)(ip + 1)];
2774 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2775 int size = mono_class_value_size (c, NULL);
2776 sp [-1].data.p = vt_sp;
2777 vt_sp += (size + 7) & ~7;
2779 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2782 MINT_IN_CASE(MINT_LDSTR)
2783 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2787 MINT_IN_CASE(MINT_NEWOBJ) {
2788 MonoClass *newobj_class;
2789 MonoMethodSignature *csig;
2790 stackval valuetype_this;
2796 token = * (guint16 *)(ip + 1);
2799 child_frame.ip = NULL;
2800 child_frame.ex = NULL;
2802 child_frame.runtime_method = rtm->data_items [token];
2803 csig = mono_method_signature (child_frame.runtime_method->method);
2804 newobj_class = child_frame.runtime_method->method->klass;
2805 /*if (profiling_classes) {
2806 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2808 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2811 if (newobj_class->parent == mono_defaults.array_class) {
2812 sp -= csig->param_count;
2813 child_frame.stack_args = sp;
2814 o = ves_array_create (&child_frame, context->domain, newobj_class, csig, sp);
2816 THROW_EX (child_frame.ex, ip);
2817 goto array_constructed;
2820 g_assert (csig->hasthis);
2821 if (csig->param_count) {
2822 sp -= csig->param_count;
2823 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
2825 child_frame.stack_args = sp;
2828 * First arg is the object.
2830 if (newobj_class->valuetype) {
2831 MonoType *t = &newobj_class->byval_arg;
2832 memset (&valuetype_this, 0, sizeof (stackval));
2833 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
2835 valuetype_this.data.p = vt_sp;
2837 sp->data.p = &valuetype_this;
2840 if (newobj_class != mono_defaults.string_class) {
2841 context->managed_code = 0;
2842 o = mono_object_new_checked (context->domain, newobj_class, &error);
2843 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2844 context->managed_code = 1;
2845 if (*mono_thread_interruption_request_flag ())
2846 mono_thread_interruption_checkpoint ();
2850 child_frame.retval = &retval;
2854 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2856 ves_exec_method_with_context (&child_frame, context);
2858 context->current_frame = frame;
2860 if (child_frame.ex) {
2862 * An exception occurred, need to run finally, fault and catch handlers..
2864 frame->ex = child_frame.ex;
2865 goto handle_finally;
2868 * a constructor returns void, but we need to return the object we created
2871 if (newobj_class->valuetype && !newobj_class->enumtype) {
2872 *sp = valuetype_this;
2873 } else if (newobj_class == mono_defaults.string_class) {
2881 MINT_IN_CASE(MINT_CASTCLASS)
2882 c = rtm->data_items [*(guint16 *)(ip + 1)];
2883 if ((o = sp [-1].data.p)) {
2884 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2885 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2887 THROW_EX (mono_get_exception_invalid_cast (), ip);
2891 MINT_IN_CASE(MINT_ISINST)
2892 c = rtm->data_items [*(guint16 *)(ip + 1)];
2893 if ((o = sp [-1].data.p)) {
2894 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2895 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2897 sp [-1].data.p = NULL;
2901 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2902 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2905 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2906 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2909 MINT_IN_CASE(MINT_UNBOX)
2910 c = rtm->data_items[*(guint16 *)(ip + 1)];
2914 THROW_EX (mono_get_exception_null_reference (), ip);
2916 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2917 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2918 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
2919 THROW_EX (mono_get_exception_invalid_cast (), ip);
2921 sp [-1].data.p = mono_object_unbox (o);
2924 MINT_IN_CASE(MINT_THROW)
2926 frame->ex_handler = NULL;
2928 sp->data.p = mono_get_exception_null_reference ();
2929 THROW_EX ((MonoException *)sp->data.p, ip);
2931 MINT_IN_CASE(MINT_LDFLDA_UNSAFE)
2933 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2936 MINT_IN_CASE(MINT_LDFLDA)
2939 THROW_EX (mono_get_exception_null_reference (), ip);
2940 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2943 MINT_IN_CASE(MINT_CKNULL)
2946 THROW_EX (mono_get_exception_null_reference (), ip);
2950 #define LDFLD(datamem, fieldtype) \
2951 o = sp [-1].data.p; \
2953 THROW_EX (mono_get_exception_null_reference (), ip); \
2954 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2957 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2958 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2959 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2960 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2961 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2962 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2963 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2964 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2965 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2966 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2968 MINT_IN_CASE(MINT_LDFLD_VT)
2971 THROW_EX (mono_get_exception_null_reference (), ip);
2972 i32 = READ32(ip + 2);
2973 sp [-1].data.p = vt_sp;
2974 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2975 vt_sp += (i32 + 7) & ~7;
2979 MINT_IN_CASE(MINT_LDRMFLD) {
2981 MonoClassField *field;
2986 THROW_EX (mono_get_exception_null_reference (), ip);
2987 field = rtm->data_items[* (guint16 *)(ip + 1)];
2989 if (mono_object_is_transparent_proxy (o)) {
2990 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2992 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2993 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2995 addr = (char*)o + field->offset;
2998 stackval_from_data (field->type, &sp [-1], addr, FALSE);
3002 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3003 MonoClassField *field;
3009 THROW_EX (mono_get_exception_null_reference (), ip);
3010 field = rtm->data_items[* (guint16 *)(ip + 1)];
3011 i32 = READ32(ip + 2);
3013 if (mono_object_is_transparent_proxy (o)) {
3014 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3015 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3016 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3018 addr = (char*)o + field->offset;
3021 sp [-1].data.p = vt_sp;
3022 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3023 vt_sp += (i32 + 7) & ~7;
3024 memcpy(sp [-1].data.p, addr, i32);
3028 #define STFLD(datamem, fieldtype) \
3029 o = sp [-2].data.p; \
3031 THROW_EX (mono_get_exception_null_reference (), ip); \
3033 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3036 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3037 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3038 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3039 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3040 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3041 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3042 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3043 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3044 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
3045 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3047 MINT_IN_CASE(MINT_STFLD_VT)
3050 THROW_EX (mono_get_exception_null_reference (), ip);
3051 i32 = READ32(ip + 2);
3053 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3054 vt_sp -= (i32 + 7) & ~7;
3058 MINT_IN_CASE(MINT_STRMFLD) {
3059 MonoClassField *field;
3063 THROW_EX (mono_get_exception_null_reference (), ip);
3065 field = rtm->data_items[* (guint16 *)(ip + 1)];
3068 if (mono_object_is_transparent_proxy (o)) {
3069 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3070 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3071 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3073 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3078 MINT_IN_CASE(MINT_STRMFLD_VT) {
3079 MonoClassField *field;
3083 THROW_EX (mono_get_exception_null_reference (), ip);
3084 field = rtm->data_items[* (guint16 *)(ip + 1)];
3085 i32 = READ32(ip + 2);
3088 if (mono_object_is_transparent_proxy (o)) {
3089 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3090 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3091 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3093 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3096 vt_sp -= (i32 + 7) & ~7;
3099 MINT_IN_CASE(MINT_LDSFLDA) {
3100 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3101 sp->data.p = mono_class_static_field_address (context->domain, field);
3106 MINT_IN_CASE(MINT_LDSFLD) {
3107 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3108 gpointer addr = mono_class_static_field_address (context->domain, field);
3109 stackval_from_data (field->type, sp, addr, FALSE);
3114 MINT_IN_CASE(MINT_LDSFLD_VT) {
3115 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3116 gpointer addr = mono_class_static_field_address (context->domain, field);
3117 int size = READ32 (ip + 2);
3121 vt_sp += (size + 7) & ~7;
3122 stackval_from_data (field->type, sp, addr, FALSE);
3126 MINT_IN_CASE(MINT_STSFLD) {
3127 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3128 gpointer addr = mono_class_static_field_address (context->domain, field);
3131 stackval_to_data (field->type, sp, addr, FALSE);
3134 MINT_IN_CASE(MINT_STSFLD_VT) {
3135 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3136 gpointer addr = mono_class_static_field_address (context->domain, field);
3137 int size = READ32 (ip + 2);
3141 stackval_to_data (field->type, sp, addr, FALSE);
3142 vt_sp -= (size + 7) & ~7;
3145 MINT_IN_CASE(MINT_STOBJ_VT) {
3147 c = rtm->data_items[* (guint16 *)(ip + 1)];
3149 size = mono_class_value_size (c, NULL);
3150 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3151 vt_sp -= (size + 7) & ~7;
3155 MINT_IN_CASE(MINT_STOBJ) {
3157 c = rtm->data_items[* (guint16 *)(ip + 1)];
3159 size = mono_class_value_size (c, NULL);
3160 memcpy(sp [-2].data.p, &sp [-1].data, size);
3164 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3165 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3166 THROW_EX (mono_get_exception_overflow (), ip);
3167 sp [-1].data.i = (guint32)sp [-1].data.f;
3170 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3171 if (sp [-1].data.i < 0)
3172 THROW_EX (mono_get_exception_overflow (), ip);
3173 sp [-1].data.l = sp [-1].data.i;
3176 MINT_IN_CASE(MINT_CONV_OVF_U8_I8)
3177 if (sp [-1].data.l < 0)
3178 THROW_EX (mono_get_exception_overflow (), ip);
3181 MINT_IN_CASE(MINT_CONV_OVF_I8_U8)
3182 if ((guint64) sp [-1].data.l > MYGINT64_MAX)
3183 THROW_EX (mono_get_exception_overflow (), ip);
3186 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3187 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3188 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGINT64_MAX)
3189 THROW_EX (mono_get_exception_overflow (), ip);
3190 sp [-1].data.l = (guint64)sp [-1].data.f;
3193 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3194 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3195 THROW_EX (mono_get_exception_overflow (), ip);
3196 sp [-1].data.l = (gint64)sp [-1].data.f;
3199 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3200 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3201 THROW_EX (mono_get_exception_overflow (), ip);
3202 sp [-1].data.i = (mono_u)sp [-1].data.l;
3205 MINT_IN_CASE(MINT_BOX) {
3206 c = rtm->data_items [* (guint16 *)(ip + 1)];
3207 guint16 offset = * (guint16 *)(ip + 2);
3209 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3210 int size = mono_class_value_size (c, NULL);
3211 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, sp [-1 - offset].data.p, &error);
3212 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3213 size = (size + 7) & ~7;
3216 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
3217 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, &sp [-1 - offset], &error);
3218 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3223 MINT_IN_CASE(MINT_NEWARR)
3224 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3225 if (!mono_error_ok (&error)) {
3226 THROW_EX (mono_error_convert_to_exception (&error), ip);
3228 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3230 /*if (profiling_classes) {
3231 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3233 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3237 MINT_IN_CASE(MINT_LDLEN)
3240 THROW_EX (mono_get_exception_null_reference (), ip);
3241 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3244 MINT_IN_CASE(MINT_GETCHR) {
3248 THROW_EX (mono_get_exception_null_reference (), ip);
3249 i32 = sp [-1].data.i;
3250 if (i32 < 0 || i32 >= mono_string_length (s))
3251 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3253 sp [-1].data.i = mono_string_chars(s)[i32];
3257 MINT_IN_CASE(MINT_STRLEN)
3259 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3261 MINT_IN_CASE(MINT_ARRAY_RANK)
3264 THROW_EX (mono_get_exception_null_reference (), ip);
3265 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3268 MINT_IN_CASE(MINT_LDELEMA)
3269 MINT_IN_CASE(MINT_LDELEMA_TC) {
3270 gboolean needs_typecheck = *ip == MINT_LDELEMA_TC;
3272 MonoClass *klass = rtm->data_items [*(guint16 *) (ip + 1)];
3273 guint16 numargs = *(guint16 *) (ip + 2);
3278 sp->data.p = ves_array_element_address (frame, klass, (MonoArray *) o, &sp [1], needs_typecheck);
3280 THROW_EX (frame->ex, ip);
3285 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3286 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3287 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3288 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3289 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3290 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3291 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3292 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3293 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3294 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3295 MINT_IN_CASE(MINT_LDELEM_REF) /* fall through */
3296 MINT_IN_CASE(MINT_LDELEM_VT) {
3304 THROW_EX (mono_get_exception_null_reference (), ip);
3306 aindex = sp [1].data.i;
3307 if (aindex >= mono_array_length (o))
3308 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3311 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3314 case MINT_LDELEM_I1:
3315 sp [0].data.i = mono_array_get (o, gint8, aindex);
3317 case MINT_LDELEM_U1:
3318 sp [0].data.i = mono_array_get (o, guint8, aindex);
3320 case MINT_LDELEM_I2:
3321 sp [0].data.i = mono_array_get (o, gint16, aindex);
3323 case MINT_LDELEM_U2:
3324 sp [0].data.i = mono_array_get (o, guint16, aindex);
3327 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3329 case MINT_LDELEM_I4:
3330 sp [0].data.i = mono_array_get (o, gint32, aindex);
3332 case MINT_LDELEM_U4:
3333 sp [0].data.i = mono_array_get (o, guint32, aindex);
3335 case MINT_LDELEM_I8:
3336 sp [0].data.l = mono_array_get (o, guint64, aindex);
3338 case MINT_LDELEM_R4:
3339 sp [0].data.f = mono_array_get (o, float, aindex);
3341 case MINT_LDELEM_R8:
3342 sp [0].data.f = mono_array_get (o, double, aindex);
3344 case MINT_LDELEM_REF:
3345 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3347 case MINT_LDELEM_VT: {
3348 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
3349 i32 = READ32 (ip + 2);
3350 char *src_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
3351 sp [0].data.vt = vt_sp;
3352 stackval_from_data (&klass_vt->byval_arg, sp, src_addr, FALSE);
3353 vt_sp += (i32 + 7) & ~7;
3365 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3366 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3367 MINT_IN_CASE(MINT_STELEM_U1) /* fall through */
3368 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3369 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3370 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3371 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3372 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3373 MINT_IN_CASE(MINT_STELEM_REF) /* fall through */
3374 MINT_IN_CASE(MINT_STELEM_VT) {
3381 THROW_EX (mono_get_exception_null_reference (), ip);
3383 aindex = sp [1].data.i;
3384 if (aindex >= mono_array_length ((MonoArray *)o))
3385 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3389 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3391 case MINT_STELEM_I1:
3392 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3394 case MINT_STELEM_U1:
3395 mono_array_set ((MonoArray *) o, guint8, aindex, sp [2].data.i);
3397 case MINT_STELEM_I2:
3398 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3400 case MINT_STELEM_I4:
3401 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3403 case MINT_STELEM_I8:
3404 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3406 case MINT_STELEM_R4:
3407 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3409 case MINT_STELEM_R8:
3410 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3412 case MINT_STELEM_REF: {
3413 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3414 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3415 if (sp [2].data.p && !isinst_obj)
3416 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3417 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3420 case MINT_STELEM_VT: {
3421 MonoClass *klass_vt = rtm->data_items [*(guint16 *) (ip + 1)];
3422 i32 = READ32 (ip + 2);
3423 char *dst_addr = mono_array_addr_with_size ((MonoArray *) o, i32, aindex);
3425 stackval_to_data (&klass_vt->byval_arg, &sp [2], dst_addr, FALSE);
3426 vt_sp -= (i32 + 7) & ~7;
3437 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3438 if (sp [-1].data.i < 0)
3439 THROW_EX (mono_get_exception_overflow (), ip);
3442 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3443 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3444 THROW_EX (mono_get_exception_overflow (), ip);
3445 sp [-1].data.i = (gint32) sp [-1].data.l;
3448 MINT_IN_CASE(MINT_CONV_OVF_I4_U8)
3449 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGINT32_MAX)
3450 THROW_EX (mono_get_exception_overflow (), ip);
3451 sp [-1].data.i = (gint32) sp [-1].data.l;
3454 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3455 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3456 THROW_EX (mono_get_exception_overflow (), ip);
3457 sp [-1].data.i = (gint32) sp [-1].data.f;
3460 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3461 if (sp [-1].data.i < 0)
3462 THROW_EX (mono_get_exception_overflow (), ip);
3465 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3466 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3467 THROW_EX (mono_get_exception_overflow (), ip);
3468 sp [-1].data.i = (guint32) sp [-1].data.l;
3471 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3472 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3473 THROW_EX (mono_get_exception_overflow (), ip);
3474 sp [-1].data.i = (guint32) sp [-1].data.f;
3477 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3478 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3479 THROW_EX (mono_get_exception_overflow (), ip);
3482 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3483 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3484 THROW_EX (mono_get_exception_overflow (), ip);
3485 sp [-1].data.i = (gint16) sp [-1].data.l;
3488 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3489 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3490 THROW_EX (mono_get_exception_overflow (), ip);
3491 sp [-1].data.i = (gint16) sp [-1].data.f;
3494 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3495 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3496 THROW_EX (mono_get_exception_overflow (), ip);
3499 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3500 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3501 THROW_EX (mono_get_exception_overflow (), ip);
3502 sp [-1].data.i = (guint16) sp [-1].data.l;
3505 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3506 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3507 THROW_EX (mono_get_exception_overflow (), ip);
3508 sp [-1].data.i = (guint16) sp [-1].data.f;
3511 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3512 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3513 THROW_EX (mono_get_exception_overflow (), ip);
3516 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3517 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3518 THROW_EX (mono_get_exception_overflow (), ip);
3519 sp [-1].data.i = (gint8) sp [-1].data.l;
3522 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3523 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3524 THROW_EX (mono_get_exception_overflow (), ip);
3525 sp [-1].data.i = (gint8) sp [-1].data.f;
3528 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3529 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3530 THROW_EX (mono_get_exception_overflow (), ip);
3533 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3534 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3535 THROW_EX (mono_get_exception_overflow (), ip);
3536 sp [-1].data.i = (guint8) sp [-1].data.l;
3539 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3540 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3541 THROW_EX (mono_get_exception_overflow (), ip);
3542 sp [-1].data.i = (guint8) sp [-1].data.f;
3546 MINT_IN_CASE(MINT_LDELEM)
3547 MINT_IN_CASE(MINT_STELEM)
3548 MINT_IN_CASE(MINT_UNBOX_ANY)
3550 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3552 MINT_IN_CASE(MINT_CKFINITE)
3553 if (!isfinite(sp [-1].data.f))
3554 THROW_EX (mono_get_exception_arithmetic (), ip);
3558 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3560 MINT_IN_CASE(MINT_LDTOKEN)
3563 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3567 MINT_IN_CASE(MINT_ADD_OVF_I4)
3568 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3569 THROW_EX (mono_get_exception_overflow (), ip);
3572 MINT_IN_CASE(MINT_ADD_OVF_I8)
3573 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3574 THROW_EX (mono_get_exception_overflow (), ip);
3577 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3578 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3579 THROW_EX (mono_get_exception_overflow (), ip);
3580 BINOP_CAST(i, +, guint32);
3582 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3583 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3584 THROW_EX (mono_get_exception_overflow (), ip);
3585 BINOP_CAST(l, +, guint64);
3587 MINT_IN_CASE(MINT_MUL_OVF_I4)
3588 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3589 THROW_EX (mono_get_exception_overflow (), ip);
3592 MINT_IN_CASE(MINT_MUL_OVF_I8)
3593 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3594 THROW_EX (mono_get_exception_overflow (), ip);
3597 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3598 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3599 THROW_EX (mono_get_exception_overflow (), ip);
3600 BINOP_CAST(i, *, guint32);
3602 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3603 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3604 THROW_EX (mono_get_exception_overflow (), ip);
3605 BINOP_CAST(l, *, guint64);
3607 MINT_IN_CASE(MINT_SUB_OVF_I4)
3608 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3609 THROW_EX (mono_get_exception_overflow (), ip);
3612 MINT_IN_CASE(MINT_SUB_OVF_I8)
3613 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3614 THROW_EX (mono_get_exception_overflow (), ip);
3617 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3618 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3619 THROW_EX (mono_get_exception_overflow (), ip);
3620 BINOP_CAST(i, -, guint32);
3622 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3623 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3624 THROW_EX (mono_get_exception_overflow (), ip);
3625 BINOP_CAST(l, -, guint64);
3627 MINT_IN_CASE(MINT_ENDFINALLY)
3629 ip = finally_ips->data;
3630 finally_ips = g_slist_remove (finally_ips, ip);
3637 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3638 MINT_IN_CASE(MINT_LEAVE_S)
3639 while (sp > frame->stack) {
3643 if (*ip == MINT_LEAVE_S) {
3644 ip += (short) *(ip + 1);
3646 ip += (gint32) READ32 (ip + 1);
3649 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3650 frame->ex_handler = NULL;
3653 goto handle_finally;
3655 MINT_IN_CASE(MINT_ICALL_V_V)
3656 MINT_IN_CASE(MINT_ICALL_V_P)
3657 MINT_IN_CASE(MINT_ICALL_P_V)
3658 MINT_IN_CASE(MINT_ICALL_P_P)
3659 MINT_IN_CASE(MINT_ICALL_PP_V)
3660 MINT_IN_CASE(MINT_ICALL_PI_V)
3661 MINT_IN_CASE(MINT_ICALL_PP_P)
3662 MINT_IN_CASE(MINT_ICALL_PI_P)
3663 MINT_IN_CASE(MINT_ICALL_PPP_V)
3664 MINT_IN_CASE(MINT_ICALL_PPI_V)
3665 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3666 if (frame->ex != NULL)
3667 goto handle_exception;
3670 MINT_IN_CASE(MINT_MONO_LDPTR)
3671 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3675 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3676 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3677 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3681 MINT_IN_CASE(MINT_MONO_FREE)
3684 g_error ("that doesn't seem right");
3685 g_free (sp->data.p);
3687 MINT_IN_CASE(MINT_MONO_RETOBJ)
3690 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3691 mono_method_signature (frame->runtime_method->method)->pinvoke);
3692 if (sp > frame->stack)
3693 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3696 #define RELOP(datamem, op) \
3698 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3700 MINT_IN_CASE(MINT_CEQ_I4)
3703 MINT_IN_CASE(MINT_CEQ0_I4)
3704 sp [-1].data.i = (sp [-1].data.i == 0);
3707 MINT_IN_CASE(MINT_CEQ_I8)
3710 MINT_IN_CASE(MINT_CEQ_R8)
3712 if (isunordered (sp [-1].data.f, sp [0].data.f))
3715 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3718 MINT_IN_CASE(MINT_CGT_I4)
3721 MINT_IN_CASE(MINT_CGT_I8)
3724 MINT_IN_CASE(MINT_CGT_R8)
3726 if (isunordered (sp [-1].data.f, sp [0].data.f))
3729 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3733 #define RELOP_CAST(datamem, op, type) \
3735 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3738 MINT_IN_CASE(MINT_CGT_UN_I4)
3739 RELOP_CAST(i, >, guint32);
3741 MINT_IN_CASE(MINT_CGT_UN_I8)
3742 RELOP_CAST(l, >, guint64);
3744 MINT_IN_CASE(MINT_CGT_UN_R8)
3746 if (isunordered (sp [-1].data.f, sp [0].data.f))
3749 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3752 MINT_IN_CASE(MINT_CLT_I4)
3755 MINT_IN_CASE(MINT_CLT_I8)
3758 MINT_IN_CASE(MINT_CLT_R8)
3760 if (isunordered (sp [-1].data.f, sp [0].data.f))
3763 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3766 MINT_IN_CASE(MINT_CLT_UN_I4)
3767 RELOP_CAST(i, <, guint32);
3769 MINT_IN_CASE(MINT_CLT_UN_I8)
3770 RELOP_CAST(l, <, guint64);
3772 MINT_IN_CASE(MINT_CLT_UN_R8)
3774 if (isunordered (sp [-1].data.f, sp [0].data.f))
3777 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3780 MINT_IN_CASE(MINT_LDFTN) {
3781 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3786 MINT_IN_CASE(MINT_LDVIRTFTN) {
3787 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3791 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3793 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3798 #define LDARG(datamem, argtype) \
3799 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3803 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3804 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3805 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3806 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3807 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3808 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3809 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3810 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3811 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3812 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3814 MINT_IN_CASE(MINT_LDARG_VT)
3816 i32 = READ32(ip + 2);
3817 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3818 vt_sp += (i32 + 7) & ~7;
3823 #define STARG(datamem, argtype) \
3825 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3828 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3829 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3830 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3831 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3832 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3833 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3834 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3835 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3836 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3837 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3839 MINT_IN_CASE(MINT_STARG_VT)
3840 i32 = READ32(ip + 2);
3842 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3843 vt_sp -= (i32 + 7) & ~7;
3847 #define STINARG(datamem, argtype) \
3849 int n = * (guint16 *)(ip + 1); \
3850 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3854 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3855 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3856 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3857 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3858 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3859 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3860 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3861 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3862 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3863 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3865 MINT_IN_CASE(MINT_STINARG_VT) {
3866 int n = * (guint16 *)(ip + 1);
3867 i32 = READ32(ip + 2);
3868 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3873 MINT_IN_CASE(MINT_LDARGA)
3874 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3879 #define LDLOC(datamem, argtype) \
3880 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3884 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3885 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3886 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3887 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3888 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3889 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3890 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3891 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3892 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3893 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3895 MINT_IN_CASE(MINT_LDLOC_VT)
3897 i32 = READ32(ip + 2);
3898 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3899 vt_sp += (i32 + 7) & ~7;
3904 MINT_IN_CASE(MINT_LDLOCA_S)
3905 sp->data.p = locals + * (guint16 *)(ip + 1);
3910 #define STLOC(datamem, argtype) \
3912 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3915 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3916 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3917 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3918 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3919 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3920 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3921 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3922 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3923 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3924 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3926 #define STLOC_NP(datamem, argtype) \
3927 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3930 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3931 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3933 MINT_IN_CASE(MINT_STLOC_VT)
3934 i32 = READ32(ip + 2);
3936 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3937 vt_sp -= (i32 + 7) & ~7;
3941 MINT_IN_CASE(MINT_LOCALLOC) {
3942 if (sp != frame->stack + 1) /*FIX?*/
3943 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3945 int len = sp [-1].data.i;
3946 sp [-1].data.p = alloca (len);
3947 MonoMethodHeader *header = mono_method_get_header_checked (frame->runtime_method->method, &error);
3948 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3949 if (header->init_locals)
3950 memset (sp [-1].data.p, 0, len);
3955 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3957 MINT_IN_CASE(MINT_INITOBJ)
3959 memset (sp->data.vt, 0, READ32(ip + 1));
3962 MINT_IN_CASE(MINT_CPBLK)
3964 if (!sp [0].data.p || !sp [1].data.p)
3965 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3967 /* FIXME: value and size may be int64... */
3968 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3971 MINT_IN_CASE(MINT_CONSTRAINED_) {
3973 /* FIXME: implement */
3975 token = READ32 (ip);
3980 MINT_IN_CASE(MINT_INITBLK)
3983 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3985 /* FIXME: value and size may be int64... */
3986 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3989 MINT_IN_CASE(MINT_NO_)
3990 /* FIXME: implement */
3994 MINT_IN_CASE(MINT_RETHROW)
3996 * need to clarify what this should actually do:
3997 * start the search from the last found handler in
3998 * this method or continue in the caller or what.
3999 * Also, do we need to run finally/fault handlers after a retrow?
4000 * Well, this implementation will follow the usual search
4001 * for an handler, considering the current ip as throw spot.
4002 * We need to NULL frame->ex_handler for the later code to
4003 * actually run the new found handler.
4005 frame->ex_handler = NULL;
4006 THROW_EX (frame->ex, ip - 1);
4009 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4010 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4014 g_assert_not_reached ();
4016 * Exception handling code.
4017 * The exception object is stored in frame->ex.
4024 MonoInvocation *inv;
4025 MonoExceptionClause *clause;
4031 g_print ("* Handling exception '%s' at IL_%04x\n",
4032 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
4033 rtm == NULL ? 0 : frame->ip - rtm->code);
4035 if (die_on_exception)
4038 for (inv = frame; inv; inv = inv->parent) {
4040 if (inv->runtime_method == NULL)
4042 method = inv->runtime_method->method;
4043 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4045 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4047 if (inv->ip == NULL)
4049 ip_offset = inv->ip - inv->runtime_method->code;
4050 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
4051 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
4052 clause = &inv->runtime_method->clauses [i];
4053 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4054 if (!clause->flags) {
4055 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4056 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4059 * OK, we found an handler, now we need to execute the finally
4060 * and fault blocks before branching to the handler code.
4062 inv->ex_handler = clause;
4065 g_print ("* Found handler at '%s'\n", method->name);
4067 goto handle_finally;
4070 g_error ("FIXME: handle filter clause");
4076 * If we get here, no handler was found: print a stack trace.
4078 for (inv = frame; inv; inv = inv->parent) {
4079 if (inv->invoke_trap)
4080 goto handle_finally;
4083 ex_obj = (MonoObject*)frame->ex;
4084 MonoJitTlsData *jit_tls = (MonoJitTlsData *) mono_tls_get_jit_tls ();
4085 jit_tls->abort_func (ex_obj);
4086 g_assert_not_reached ();
4092 MonoExceptionClause *clause;
4093 GSList *old_list = finally_ips;
4094 MonoMethod *method = frame->runtime_method->method;
4095 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4096 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4100 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4102 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4103 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4106 ip_offset = frame->ip - rtm->code;
4108 if (endfinally_ip != NULL)
4109 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4110 for (i = 0; i < header->num_clauses; ++i)
4111 if (frame->ex_handler == &rtm->clauses [i])
4115 clause = &rtm->clauses [i];
4116 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4117 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4118 ip = rtm->code + clause->handler_offset;
4119 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4122 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4128 endfinally_ip = NULL;
4130 if (old_list != finally_ips && finally_ips) {
4131 ip = finally_ips->data;
4132 finally_ips = g_slist_remove (finally_ips, ip);
4133 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4138 * If an exception is set, we need to execute the fault handler, too,
4139 * otherwise, we continue normally.
4149 MonoExceptionClause *clause;
4150 MonoMethod *method = frame->runtime_method->method;
4151 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4152 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4156 g_print ("* Handle fault\n");
4158 ip_offset = frame->ip - rtm->code;
4159 for (i = 0; i < header->num_clauses; ++i) {
4160 clause = &rtm->clauses [i];
4161 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4162 ip = rtm->code + clause->handler_offset;
4165 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4171 * If the handler for the exception was found in this method, we jump
4172 * to it right away, otherwise we return and let the caller run
4173 * the finally, fault and catch blocks.
4174 * This same code should be present in the endfault opcode, but it
4175 * is corrently not assigned in the ECMA specs: LAMESPEC.
4177 if (frame->ex_handler) {
4180 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4182 ip = rtm->code + frame->ex_handler->handler_offset;
4184 vt_sp = (unsigned char *) sp + rtm->stack_size;
4185 sp->data.p = frame->ex;
4196 ves_exec_method (MonoInvocation *frame)
4198 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4199 ThreadContext context_struct;
4206 mono_unhandled_exception ((MonoObject*)frame->ex);
4209 if (context == NULL) {
4210 context = &context_struct;
4211 context_struct.domain = mono_domain_get ();
4212 context_struct.base_frame = frame;
4213 context_struct.current_frame = NULL;
4214 context_struct.env_frame = frame;
4215 context_struct.current_env = &env;
4216 context_struct.search_for_handler = 0;
4217 context_struct.managed_code = 0;
4218 mono_native_tls_set_value (thread_context_id, context);
4221 frame->parent = context->current_frame;
4222 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4223 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4224 context->managed_code = 1;
4225 ves_exec_method_with_context (frame, context);
4226 context->managed_code = 0;
4228 if (context != &context_struct && context->current_env) {
4229 context->env_frame->ex = frame->ex;
4230 longjmp (*context->current_env, 1);
4233 mono_unhandled_exception ((MonoObject*)frame->ex);
4235 if (context->base_frame == frame)
4236 mono_native_tls_set_value (thread_context_id, NULL);
4238 context->current_frame = frame->parent;
4242 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4244 MonoImage *image = mono_assembly_get_image (assembly);
4249 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4250 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4253 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4255 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4256 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4264 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4265 "Usage is: mint [options] executable args...\n\n", VERSION);
4267 "Runtime Debugging:\n"
4272 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4275 " --traceclassinit\n"
4278 " --debug method_name\n"
4285 " --config filename load the specified config file instead of the default\n"
4286 " --workers n maximum number of worker threads\n"
4292 interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4293 MonoReflectionMethod **method,
4294 gint32 *iloffset, gint32 *native_offset,
4295 MonoString **file, gint32 *line, gint32 *column)
4297 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4298 MonoInvocation *inv = context->current_frame;
4302 for (i = 0; inv && i < skip; inv = inv->parent)
4303 if (inv->runtime_method != NULL)
4314 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4315 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4320 if (need_file_info) {
4324 *file = mono_string_new (mono_domain_get (), "unknown");
4331 interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4333 MonoDomain *domain = mono_domain_get ();
4335 MonoArray *ta = exc->trace_ips;
4340 /* Exception is not thrown yet */
4341 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4342 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4346 len = mono_array_length (ta);
4348 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4349 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4351 for (i = skip; i < len / 2; i++) {
4352 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4353 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4354 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4355 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4358 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4359 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4360 sf->native_offset = ip - rtm->code;
4364 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4366 if (need_file_info) {
4369 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4371 sf->filename = filename? mono_string_new (domain, filename): NULL;
4378 mono_array_set (res, gpointer, i, sf);
4387 mono_native_tls_alloc (&thread_context_id, NULL);
4388 mono_native_tls_set_value (thread_context_id, NULL);
4389 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4391 mono_interp_transform_init ();
4394 typedef int (*TestMethod) (void);
4397 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4399 int result, expected, failed, cfailed, run;
4400 double elapsed, transform_time;
4402 MonoObject *result_obj;
4403 static gboolean filter_method_init = FALSE;
4404 static const char *filter_method = NULL;
4406 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4407 cfailed = failed = run = 0;
4408 transform_time = elapsed = 0.0;
4410 g_timer_start (timer);
4411 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4412 MonoObject *exc = NULL;
4414 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4416 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4420 if (!filter_method_init) {
4421 filter_method = g_getenv ("INTERP_FILTER_METHOD");
4422 filter_method_init = TRUE;
4424 gboolean filter = FALSE;
4425 if (filter_method) {
4426 const char *name = filter_method;
4428 if ((strchr (name, '.') > name) || strchr (name, ':')) {
4429 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
4430 filter = mono_method_desc_full_match (desc, method);
4431 mono_method_desc_free (desc);
4433 filter = strcmp (method->name, name) == 0;
4435 } else { /* no filter, check for `Category' attribute on method */
4437 MonoCustomAttrInfo* ainfo = mono_custom_attrs_from_method_checked (method, &error);
4438 mono_error_cleanup (&error);
4442 for (j = 0; j < ainfo->num_attrs && filter; ++j) {
4443 MonoCustomAttrEntry *centry = &ainfo->attrs [j];
4444 if (centry->ctor == NULL)
4447 MonoClass *klass = centry->ctor->klass;
4448 if (strcmp (klass->name, "CategoryAttribute"))
4451 MonoObject *obj = mono_custom_attrs_get_attr_checked (ainfo, klass, &error);
4452 /* FIXME: there is an ordering problem if there're multiple attributes, do this instead:
4453 * MonoObject *obj = create_custom_attr (ainfo->image, centry->ctor, centry->data, centry->data_size, &error); */
4454 mono_error_cleanup (&error);
4455 MonoMethod *getter = mono_class_get_method_from_name (klass, "get_Category", -1);
4456 MonoObject *str = mono_interp_runtime_invoke (getter, obj, NULL, &exc, &error);
4457 mono_error_cleanup (&error);
4458 char *utf8_str = mono_string_to_utf8_checked ((MonoString *) str, &error);
4459 mono_error_cleanup (&error);
4460 if (!strcmp (utf8_str, "!INTERPRETER")) {
4461 g_print ("skip %s...\n", method->name);
4467 if (strncmp (method->name, "test_", 5) == 0 && filter) {
4468 MonoError interp_error;
4469 MonoObject *exc = NULL;
4471 result_obj = mono_interp_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
4472 if (!mono_error_ok (&interp_error)) {
4474 g_print ("Test '%s' execution failed.\n", method->name);
4475 } else if (exc != NULL) {
4476 g_print ("Exception in Test '%s' occured:\n", method->name);
4477 mono_object_describe (exc);
4481 result = *(gint32 *) mono_object_unbox (result_obj);
4482 expected = atoi (method->name + 5); // FIXME: oh no.
4485 if (result != expected) {
4487 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4492 g_timer_stop (timer);
4493 elapsed = g_timer_elapsed (timer, NULL);
4494 if (failed > 0 || cfailed > 0){
4495 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4496 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4498 g_print ("Results: total tests: %d, all pass \n", run);
4501 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4502 elapsed - transform_time, transform_time);
4503 *total += failed + cfailed;
4507 interp_regression (MonoImage *image, int verbose, int *total_run)
4510 GTimer *timer = g_timer_new ();
4511 MonoDomain *domain = mono_domain_get ();
4515 /* load the metadata */
4516 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4518 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4520 mono_error_cleanup (&error);
4523 mono_class_init (method->klass);
4528 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4530 g_timer_destroy (timer);
4535 mono_interp_regression_list (int verbose, int count, char *images [])
4537 int i, total, total_run, run;
4539 total_run = total = 0;
4540 for (i = 0; i < count; ++i) {
4541 MonoAssembly *ass = mono_assembly_open_predicate (images [i], FALSE, FALSE, NULL, NULL, NULL);
4543 g_warning ("failed to load assembly: %s", images [i]);
4546 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4550 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4552 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);