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.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>
64 #include <mono/mini/mini.h>
65 #include <mono/mini/jit-icalls.h>
68 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
71 #define finite _finite
75 #define finite isfinite
79 #define INIT_FRAME(frame,parent_frame,method_args,method_retval,domain,mono_method,error) \
81 (frame)->parent = (parent_frame); \
82 (frame)->stack_args = (method_args); \
83 (frame)->retval = (method_retval); \
84 (frame)->runtime_method = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
87 (frame)->invoke_trap = 0; \
90 void ves_exec_method (MonoInvocation *frame);
92 static char* dump_stack (stackval *stack, stackval *sp);
93 static char* dump_frame (MonoInvocation *inv);
94 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
95 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
97 typedef void (*ICallMethod) (MonoInvocation *frame);
99 static guint32 die_on_exception = 0;
100 static MonoNativeTlsKey thread_context_id;
102 static char* dump_args (MonoInvocation *inv);
104 #define DEBUG_INTERP 0
107 int mono_interp_traceopt = 2;
108 /* If true, then we output the opcodes as we interpret them */
109 static int global_tracing = 2;
111 static int debug_indent_level = 0;
113 static int break_on_method = 0;
114 static int nested_trace = 0;
115 static GList *db_methods = NULL;
122 for (h = 0; h < debug_indent_level; h++)
127 db_match_method (gpointer data, gpointer user_data)
129 MonoMethod *m = (MonoMethod*)user_data;
130 MonoMethodDesc *desc = data;
132 if (mono_method_desc_full_match (desc, m))
136 static void debug_enter (MonoInvocation *frame, int *tracing)
139 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
141 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
145 MonoMethod *method = frame->runtime_method->method;
146 char *mn, *args = dump_args (frame);
147 debug_indent_level++;
149 mn = mono_method_full_name (method, FALSE);
150 g_print ("(%p) Entering %s (", mono_thread_internal_current (), mn);
152 g_print ("%s)\n", args);
155 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
156 mono_profiler_method_enter (frame->runtime_method->method);
160 #define DEBUG_LEAVE() \
163 args = dump_retval (frame); \
165 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
166 g_print ("(%p) Leaving %s", mono_thread_internal_current (), mn); \
168 g_print (" => %s\n", args); \
170 debug_indent_level--; \
171 if (tracing == 3) global_tracing = 0; \
173 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
174 mono_profiler_method_leave (frame->runtime_method->method);
178 int mono_interp_traceopt = 0;
179 static void debug_enter (MonoInvocation *frame, int *tracing)
182 #define DEBUG_LEAVE()
187 interp_ex_handler (MonoException *ex) {
189 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
193 stack_trace = dump_frame (context->current_frame);
194 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
195 g_free (stack_trace);
196 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
197 char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
198 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
199 fprintf(stderr, "Nothing can catch this exception: ");
200 fprintf(stderr, "%s", ex->object.vtable->klass->name);
201 if (ex->message != NULL) {
202 char *m = mono_string_to_utf8_checked (ex->message, &error);
203 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
204 fprintf(stderr, ": %s", m);
207 fprintf(stderr, "\n%s\n", strace);
209 if (ex->inner_ex != NULL) {
210 ex = (MonoException *)ex->inner_ex;
211 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
212 if (ex->message != NULL) {
213 char *m = mono_string_to_utf8_checked (ex->message, &error);
214 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
215 fprintf(stderr, ": %s", m);
218 strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
219 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
220 fprintf(stderr, "\n");
221 fprintf(stderr, "%s\n", strace);
224 /* wait for other threads to also collapse */
225 // Sleep(1000); // TODO: proper sleep
228 context->env_frame->ex = ex;
229 context->search_for_handler = 1;
230 longjmp (*context->current_env, 1);
234 ves_real_abort (int line, MonoMethod *mh,
235 const unsigned short *ip, stackval *stack, stackval *sp)
238 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
239 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
240 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
241 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
242 g_print ("0x%04x %02x\n",
243 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
244 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
246 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
249 #define ves_abort() \
251 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
252 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
255 static mono_mutex_t runtime_method_lookup_section;
258 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
261 mono_error_init (error);
263 mono_os_mutex_lock (&runtime_method_lookup_section);
264 if ((rtm = mono_internal_hash_table_lookup (&domain->jit_code_hash, method))) {
265 mono_os_mutex_unlock (&runtime_method_lookup_section);
268 rtm = mono_mempool_alloc (domain->mp, sizeof (RuntimeMethod));
269 memset (rtm, 0, sizeof (*rtm));
270 rtm->method = method;
271 rtm->param_count = mono_method_signature (method)->param_count;
272 rtm->hasthis = mono_method_signature (method)->hasthis;
273 rtm->valuetype = method->klass->valuetype;
274 mono_internal_hash_table_insert (&domain->jit_code_hash, method, rtm);
275 mono_os_mutex_unlock (&runtime_method_lookup_section);
281 interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
283 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
284 method = mono_marshal_get_synchronized_wrapper (method);
285 return mono_interp_get_runtime_method (domain, method, error);
288 static inline RuntimeMethod*
289 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
291 MonoMethod *m = runtime_method->method;
294 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
295 RuntimeMethod *ret = NULL;
296 if (mono_object_is_transparent_proxy (obj)) {
297 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
298 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
299 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
300 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
301 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
303 ret = runtime_method;
308 int slot = mono_method_get_vtable_slot (m);
309 if (mono_class_is_interface (m->klass)) {
310 g_assert (obj->vtable->klass != m->klass);
311 /* TODO: interface offset lookup is slow, go through IMT instead */
312 slot += mono_class_interface_offset (obj->vtable->klass, m->klass);
314 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
315 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
316 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
317 return virtual_runtime_method;
321 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
324 switch (type->type) {
325 case MONO_TYPE_OBJECT:
326 case MONO_TYPE_CLASS:
327 case MONO_TYPE_STRING:
328 case MONO_TYPE_ARRAY:
329 case MONO_TYPE_SZARRAY:
334 result->data.p = *(gpointer*)data;
337 switch (type->type) {
341 result->data.i = *(gint8*)data;
344 case MONO_TYPE_BOOLEAN:
345 result->data.i = *(guint8*)data;
348 result->data.i = *(gint16*)data;
352 result->data.i = *(guint16*)data;
355 result->data.i = *(gint32*)data;
359 result->data.nati = *(mono_i*)data;
362 result->data.p = *(gpointer*)data;
365 result->data.i = *(guint32*)data;
368 result->data.f = *(float*)data;
372 result->data.l = *(gint64*)data;
375 result->data.f = *(double*)data;
377 case MONO_TYPE_STRING:
378 case MONO_TYPE_SZARRAY:
379 case MONO_TYPE_CLASS:
380 case MONO_TYPE_OBJECT:
381 case MONO_TYPE_ARRAY:
382 result->data.p = *(gpointer*)data;
384 case MONO_TYPE_VALUETYPE:
385 if (type->data.klass->enumtype) {
386 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
389 mono_value_copy (result->data.vt, data, type->data.klass);
391 case MONO_TYPE_GENERICINST:
392 stackval_from_data (&type->data.generic_class->container_class->byval_arg, result, data, pinvoke);
395 g_warning ("got type 0x%02x", type->type);
396 g_assert_not_reached ();
401 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
404 gpointer *p = (gpointer*)data;
408 /* printf ("TODAT0 %p\n", data); */
409 switch (type->type) {
412 guint8 *p = (guint8*)data;
416 case MONO_TYPE_BOOLEAN: {
417 guint8 *p = (guint8*)data;
418 *p = (val->data.i != 0);
423 case MONO_TYPE_CHAR: {
424 guint16 *p = (guint16*)data;
429 mono_i *p = (mono_i*)data;
430 /* In theory the value used by stloc should match the local var type
431 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
432 a native int - both by csc and mcs). Not sure what to do about sign extension
433 as it is outside the spec... doing the obvious */
434 *p = (mono_i)val->data.nati;
438 mono_u *p = (mono_u*)data;
440 *p = (mono_u)val->data.nati;
445 gint32 *p = (gint32*)data;
451 gint64 *p = (gint64*)data;
456 float *p = (float*)data;
461 double *p = (double*)data;
465 case MONO_TYPE_STRING:
466 case MONO_TYPE_SZARRAY:
467 case MONO_TYPE_CLASS:
468 case MONO_TYPE_OBJECT:
469 case MONO_TYPE_ARRAY:
470 case MONO_TYPE_PTR: {
471 gpointer *p = (gpointer*)data;
475 case MONO_TYPE_VALUETYPE:
476 if (type->data.klass->enumtype) {
477 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
480 mono_value_copy (data, val->data.vt, type->data.klass);
482 case MONO_TYPE_GENERICINST:
483 stackval_to_data (&type->data.generic_class->container_class->byval_arg, val, data, pinvoke);
486 g_warning ("got type %x", type->type);
487 g_assert_not_reached ();
492 fill_in_trace (MonoException *exception, MonoInvocation *frame)
494 char *stack_trace = dump_frame (frame);
495 MonoDomain *domain = mono_domain_get();
496 (exception)->stack_trace = mono_string_new (domain, stack_trace);
497 (exception)->trace_ips = get_trace_ips (domain, frame);
498 g_free (stack_trace);
501 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
503 #define THROW_EX(exception,ex_ip) \
505 frame->ip = (ex_ip); \
506 frame->ex = (MonoException*)(exception); \
507 FILL_IN_TRACE(frame->ex, frame); \
508 goto handle_exception; \
512 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
515 intptr_t *lower_bounds;
520 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
521 for (i = 0; i < sig->param_count; ++i) {
522 lengths [i] = values->data.i;
525 if (klass->rank == sig->param_count) {
526 /* Only lengths provided. */
529 /* lower bounds are first. */
530 lower_bounds = (intptr_t *) lengths;
531 lengths += klass->rank;
533 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
534 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
539 ves_array_set (MonoInvocation *frame)
541 stackval *sp = frame->stack_args;
545 gint32 i, t, pos, esize;
549 o = frame->stack_args->data.p;
551 ac = o->vtable->klass;
553 g_assert (ac->rank >= 1);
556 if (ao->bounds != NULL) {
557 pos -= ao->bounds [0].lower_bound;
558 for (i = 1; i < ac->rank; i++) {
559 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
560 ao->bounds [i].length) {
561 frame->ex = mono_get_exception_index_out_of_range ();
562 FILL_IN_TRACE(frame->ex, frame);
565 pos = pos*ao->bounds [i].length + sp [i].data.i -
566 ao->bounds [i].lower_bound;
568 } else if (pos >= ao->max_length) {
569 frame->ex = mono_get_exception_index_out_of_range ();
570 FILL_IN_TRACE(frame->ex, frame);
574 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
576 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
577 mono_error_cleanup (&error);
579 frame->ex = mono_get_exception_array_type_mismatch ();
580 FILL_IN_TRACE (frame->ex, frame);
585 esize = mono_array_element_size (ac);
586 ea = mono_array_addr_with_size (ao, esize, pos);
588 mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
589 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
593 ves_array_get (MonoInvocation *frame)
595 stackval *sp = frame->stack_args;
599 gint32 i, t, pos, esize;
603 o = frame->stack_args->data.p;
605 ac = o->vtable->klass;
607 g_assert (ac->rank >= 1);
610 if (ao->bounds != NULL) {
611 pos -= ao->bounds [0].lower_bound;
612 for (i = 1; i < ac->rank; i++) {
613 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
614 ao->bounds [i].length) {
615 frame->ex = mono_get_exception_index_out_of_range ();
616 FILL_IN_TRACE(frame->ex, frame);
620 pos = pos*ao->bounds [i].length + sp [i].data.i -
621 ao->bounds [i].lower_bound;
623 } else if (pos >= ao->max_length) {
624 frame->ex = mono_get_exception_index_out_of_range ();
625 FILL_IN_TRACE(frame->ex, frame);
629 esize = mono_array_element_size (ac);
630 ea = mono_array_addr_with_size (ao, esize, pos);
632 mt = mono_method_signature (frame->runtime_method->method)->ret;
633 stackval_from_data (mt, frame->retval, ea, FALSE);
637 ves_array_element_address (MonoInvocation *frame)
639 stackval *sp = frame->stack_args;
643 gint32 i, t, pos, esize;
646 o = frame->stack_args->data.p;
648 ac = o->vtable->klass;
650 g_assert (ac->rank >= 1);
653 if (ao->bounds != NULL) {
654 pos -= ao->bounds [0].lower_bound;
655 for (i = 1; i < ac->rank; i++) {
656 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
657 ao->bounds [i].length) {
658 frame->ex = mono_get_exception_index_out_of_range ();
659 FILL_IN_TRACE(frame->ex, frame);
662 pos = pos*ao->bounds [i].length + sp [i].data.i -
663 ao->bounds [i].lower_bound;
665 } else if (pos >= ao->max_length) {
666 frame->ex = mono_get_exception_index_out_of_range ();
667 FILL_IN_TRACE(frame->ex, frame);
671 esize = mono_array_element_size (ac);
672 ea = mono_array_addr_with_size (ao, esize, pos);
674 frame->retval->data.p = ea;
678 interp_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
680 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
681 MonoInvocation *frame;
683 MonoMethodHeader *hd;
686 if (!context) return;
688 frame = context->current_frame;
691 gboolean managed = FALSE;
692 MonoMethod *method = frame->runtime_method->method;
693 if (!method || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
696 hd = mono_method_get_header_checked (method, &error);
697 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
698 il_offset = frame->ip - (const unsigned short *)hd->code;
699 if (!method->wrapper_type)
702 if (func (method, -1, il_offset, managed, user_data))
704 frame = frame->parent;
708 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
710 struct _MethodArguments {
718 typedef struct _MethodArguments MethodArguments;
720 // TODO: this function is also arch dependent (register width).
721 static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
723 // TODO: don't malloc this data structure.
724 MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
729 for (int i = 0; i < sig->param_count; i++) {
730 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
732 case MONO_TYPE_BOOLEAN:
743 case MONO_TYPE_SZARRAY:
744 case MONO_TYPE_CLASS:
745 case MONO_TYPE_OBJECT:
746 case MONO_TYPE_STRING:
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 g_error ("build_args_from_sig: TODO, more than two iregs: %d\n", margs->ilen);
762 g_error ("build_args_from_sig: TODO, allocate floats: %d\n", margs->flen);
768 margs->iargs [0] = frame->stack_args->data.p;
772 for (int i = 0; i < sig->param_count; i++) {
773 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
775 case MONO_TYPE_BOOLEAN:
786 case MONO_TYPE_SZARRAY:
787 case MONO_TYPE_CLASS:
788 case MONO_TYPE_OBJECT:
789 case MONO_TYPE_STRING:
791 margs->iargs [int_i] = frame->stack_args [i].data.p;
793 g_print ("build_args_from_sig: margs->iargs[%d]: %p (frame @ %d)\n", int_i, margs->iargs[int_i], i);
798 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
802 if (sig->ret->type != MONO_TYPE_VOID) {
803 margs->retval = &(frame->retval->data.p);
805 margs->retval = NULL;
812 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
815 MonoInvocation *old_frame = context->current_frame;
816 MonoInvocation *old_env_frame = context->env_frame;
817 jmp_buf *old_env = context->current_env;
820 context->current_frame = old_frame;
821 context->env_frame = old_env_frame;
822 context->current_env = old_env;
823 context->managed_code = 1;
828 context->env_frame = frame;
829 context->current_env = &env;
831 g_assert (!frame->runtime_method);
832 if (!mono_interp_enter_icall_trampoline) {
834 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
836 // mono_tramp_info_register (info, NULL);
839 MethodArguments *margs = build_args_from_sig (sig, frame);
841 g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
842 g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
845 context->current_frame = frame;
846 context->managed_code = 0;
848 mono_interp_enter_icall_trampoline (addr, margs);
850 context->managed_code = 1;
851 /* domain can only be changed by native code */
852 context->domain = mono_domain_get ();
854 if (*mono_thread_interruption_request_flag ())
855 mono_thread_interruption_checkpoint ();
857 if (!MONO_TYPE_ISSTRUCT (sig->ret))
858 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
860 context->current_frame = old_frame;
861 context->env_frame = old_env_frame;
862 context->current_env = old_env;
864 g_free (margs->iargs);
865 g_free (margs->fargs);
870 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
872 MonoDelegate *delegate = (MonoDelegate *)this;
875 delegate->method_info = mono_method_get_object_checked (domain, runtime_method->method, NULL, &error);
876 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
877 delegate->target = target;
879 if (target && mono_object_is_transparent_proxy (target)) {
880 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
881 delegate->method_ptr = mono_interp_get_runtime_method (domain, method, &error);
882 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
884 delegate->method_ptr = runtime_method;
889 mono_interp_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
893 MonoDomain *domain = mono_domain_get ();
896 d = (MonoDelegate*)mono_object_new_checked (domain, klass, &error);
897 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
899 ji = mono_jit_info_table_find (domain, ftn);
901 mono_raise_exception (mono_get_exception_argument ("", "Function pointer was not created by a Delegate."));
903 /* FIXME: discard the wrapper and call the original method */
904 interp_delegate_ctor (domain, (MonoObject*)d, NULL, mono_interp_get_runtime_method (domain, ji->d.method, &error));
905 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
912 * runtime specifies that the implementation of the method is automatically
913 * provided by the runtime and is primarily used for the methods of delegates.
916 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
918 MonoMethod *method = frame->runtime_method->method;
919 const char *name = method->name;
920 MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
921 MonoObject *isinst_obj;
924 mono_class_init (method->klass);
926 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.multicastdelegate_class, &error);
927 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
928 if (obj && isinst_obj) {
929 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
930 interp_delegate_ctor (context->domain, obj, frame->stack_args [1].data.p, frame->stack_args[2].data.p);
935 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
936 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
937 if (obj && isinst_obj) {
938 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
939 ves_array_set (frame);
942 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
943 ves_array_get (frame);
946 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
947 ves_array_element_address (frame);
952 g_error ("Don't know how to exec runtime method %s.%s::%s",
953 method->klass->name_space, method->klass->name,
958 dump_stack (stackval *stack, stackval *sp)
961 GString *str = g_string_new ("");
964 return g_string_free (str, FALSE);
967 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
970 return g_string_free (str, FALSE);
974 dump_stackval (GString *str, stackval *s, MonoType *type)
976 switch (type->type) {
984 case MONO_TYPE_BOOLEAN:
985 g_string_append_printf (str, "[%d] ", s->data.i);
987 case MONO_TYPE_STRING:
988 case MONO_TYPE_SZARRAY:
989 case MONO_TYPE_CLASS:
990 case MONO_TYPE_OBJECT:
991 case MONO_TYPE_ARRAY:
995 g_string_append_printf (str, "[%p] ", s->data.p);
997 case MONO_TYPE_VALUETYPE:
998 if (type->data.klass->enumtype)
999 g_string_append_printf (str, "[%d] ", s->data.i);
1001 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1005 g_string_append_printf (str, "[%g] ", s->data.f);
1010 GString *res = g_string_new ("");
1011 mono_type_get_desc (res, type, TRUE);
1012 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1013 g_string_free (res, TRUE);
1020 dump_args (MonoInvocation *inv)
1022 GString *str = g_string_new ("");
1024 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1026 if (signature->param_count == 0 && !signature->hasthis)
1027 return g_string_free (str, FALSE);
1029 if (signature->hasthis) {
1030 MonoMethod *method = inv->runtime_method->method;
1031 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1034 for (i = 0; i < signature->param_count; ++i)
1035 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1037 return g_string_free (str, FALSE);
1041 dump_retval (MonoInvocation *inv)
1043 GString *str = g_string_new ("");
1044 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1046 if (ret->type != MONO_TYPE_VOID)
1047 dump_stackval (str, inv->retval, ret);
1049 return g_string_free (str, FALSE);
1053 dump_frame (MonoInvocation *inv)
1055 GString *str = g_string_new ("");
1060 for (i = 0; inv; inv = inv->parent) {
1061 if (inv->runtime_method != NULL) {
1062 MonoMethod *method = inv->runtime_method->method;
1066 const char * opname = "";
1068 gchar *source = NULL;
1072 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1073 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1074 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1075 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1079 opname = mono_interp_opname [*inv->ip];
1080 codep = inv->ip - inv->runtime_method->code;
1081 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1086 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1087 source = mono_debug_method_lookup_location (minfo, codep);
1091 args = dump_args (inv);
1092 name = mono_method_full_name (method, TRUE);
1094 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1096 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1103 return g_string_free (str, FALSE);
1107 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1111 MonoInvocation *inv;
1114 for (i = 0, inv = top; inv; inv = inv->parent)
1115 if (inv->runtime_method != NULL)
1118 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1119 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1121 for (i = 0, inv = top; inv; inv = inv->parent)
1122 if (inv->runtime_method != NULL) {
1123 mono_array_set (res, gpointer, i, inv->runtime_method);
1125 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1133 #define MYGUINT64_MAX 18446744073709551615ULL
1134 #define MYGINT64_MAX 9223372036854775807LL
1135 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1137 #define MYGUINT32_MAX 4294967295U
1138 #define MYGINT32_MAX 2147483647
1139 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1141 #define CHECK_ADD_OVERFLOW(a,b) \
1142 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1143 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1145 #define CHECK_SUB_OVERFLOW(a,b) \
1146 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1147 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1149 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1150 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1152 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1153 (guint32)(a) < (guint32)(b) ? -1 : 0
1155 #define CHECK_ADD_OVERFLOW64(a,b) \
1156 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1157 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1159 #define CHECK_SUB_OVERFLOW64(a,b) \
1160 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1161 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1163 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1164 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1166 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1167 (guint64)(a) < (guint64)(b) ? -1 : 0
1169 #if SIZEOF_VOID_P == 4
1170 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1171 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1173 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1174 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1177 /* Resolves to TRUE if the operands would overflow */
1178 #define CHECK_MUL_OVERFLOW(a,b) \
1179 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1180 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1181 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1182 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1183 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1184 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1185 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1187 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1188 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1189 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1191 #define CHECK_MUL_OVERFLOW64(a,b) \
1192 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1193 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1194 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1195 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1196 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1197 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1198 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1200 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1201 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1202 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1204 #if SIZEOF_VOID_P == 4
1205 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1206 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1208 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1209 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1213 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1215 MonoInvocation frame;
1216 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1217 MonoObject *retval = NULL;
1218 MonoMethodSignature *sig = mono_method_signature (method);
1219 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1220 int i, type, isobject = 0;
1223 stackval *args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
1224 ThreadContext context_struct;
1225 MonoInvocation *old_frame = NULL;
1228 mono_error_init (error);
1233 if (context != &context_struct) {
1234 context->domain = mono_domain_get ();
1235 context->current_frame = old_frame;
1236 context->managed_code = 0;
1238 mono_native_tls_set_value (thread_context_id, NULL);
1240 *exc = (MonoObject *)frame.ex;
1244 if (context == NULL) {
1245 context = &context_struct;
1246 context_struct.base_frame = &frame;
1247 context_struct.current_frame = NULL;
1248 context_struct.env_frame = &frame;
1249 context_struct.current_env = &env;
1250 context_struct.search_for_handler = 0;
1251 context_struct.managed_code = 0;
1252 mono_native_tls_set_value (thread_context_id, context);
1255 old_frame = context->current_frame;
1257 context->domain = mono_domain_get ();
1259 switch (sig->ret->type) {
1260 case MONO_TYPE_VOID:
1262 case MONO_TYPE_STRING:
1263 case MONO_TYPE_OBJECT:
1264 case MONO_TYPE_CLASS:
1265 case MONO_TYPE_ARRAY:
1266 case MONO_TYPE_SZARRAY:
1269 case MONO_TYPE_VALUETYPE:
1270 retval = mono_object_new_checked (context->domain, klass, error);
1271 ret = ((char*)retval) + sizeof (MonoObject);
1272 if (!sig->ret->data.klass->enumtype)
1273 result.data.vt = ret;
1276 retval = mono_object_new_checked (context->domain, klass, error);
1277 ret = ((char*)retval) + sizeof (MonoObject);
1282 args [0].data.p = obj;
1284 for (i = 0; i < sig->param_count; ++i) {
1285 int a_index = i + !!sig->hasthis;
1286 if (sig->params [i]->byref) {
1287 args [a_index].data.p = params [i];
1290 type = sig->params [i]->type;
1295 case MONO_TYPE_BOOLEAN:
1296 args [a_index].data.i = *(MonoBoolean*)params [i];
1300 case MONO_TYPE_CHAR:
1301 args [a_index].data.i = *(gint16*)params [i];
1303 #if SIZEOF_VOID_P == 4
1304 case MONO_TYPE_U: /* use VAL_POINTER? */
1309 args [a_index].data.i = *(gint32*)params [i];
1311 #if SIZEOF_VOID_P == 8
1317 args [a_index].data.l = *(gint64*)params [i];
1319 case MONO_TYPE_VALUETYPE:
1320 if (sig->params [i]->data.klass->enumtype) {
1321 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1324 args [a_index].data.p = params [i];
1327 case MONO_TYPE_STRING:
1328 case MONO_TYPE_CLASS:
1329 case MONO_TYPE_ARRAY:
1330 case MONO_TYPE_SZARRAY:
1331 case MONO_TYPE_OBJECT:
1332 args [a_index].data.p = params [i];
1335 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1339 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1340 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1341 INIT_FRAME (&frame,context->current_frame,args,&result,mono_get_root_domain (),method,error);
1343 frame.invoke_trap = 1;
1344 context->managed_code = 1;
1345 ves_exec_method_with_context (&frame, context);
1346 context->managed_code = 0;
1347 if (context == &context_struct)
1348 mono_native_tls_set_value (thread_context_id, NULL);
1350 context->current_frame = old_frame;
1351 if (frame.ex != NULL) {
1353 *exc = (MonoObject*) frame.ex;
1356 if (context->current_env != NULL) {
1357 context->env_frame->ex = frame.ex;
1358 longjmp(*context->current_env, 1);
1361 printf("dropped exception...\n");
1363 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1365 if (isobject || method->string_ctor)
1366 return result.data.p;
1367 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1372 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1374 MonoInvocation *old_frame = context->current_frame;
1375 MonoInvocation *old_env_frame = context->env_frame;
1376 jmp_buf *old_env = context->current_env;
1380 context->current_frame = old_frame;
1381 context->env_frame = old_env_frame;
1382 context->current_env = old_env;
1383 context->managed_code = 1;
1387 context->env_frame = context->current_frame;
1388 context->current_env = &env;
1389 context->managed_code = 0;
1392 case MINT_ICALL_V_V: {
1393 void (*func)() = ptr;
1397 case MINT_ICALL_V_P: {
1398 gpointer (*func)() = ptr;
1400 sp [-1].data.p = func ();
1403 case MINT_ICALL_P_V: {
1404 void (*func)(gpointer) = ptr;
1405 func (sp [-1].data.p);
1409 case MINT_ICALL_P_P: {
1410 gpointer (*func)(gpointer) = ptr;
1411 sp [-1].data.p = func (sp [-1].data.p);
1414 case MINT_ICALL_PP_V: {
1415 void (*func)(gpointer,gpointer) = ptr;
1417 func (sp [0].data.p, sp [1].data.p);
1420 case MINT_ICALL_PI_V: {
1421 void (*func)(gpointer,int) = ptr;
1423 func (sp [0].data.p, sp [1].data.i);
1426 case MINT_ICALL_PP_P: {
1427 gpointer (*func)(gpointer,gpointer) = ptr;
1429 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1432 case MINT_ICALL_PI_P: {
1433 gpointer (*func)(gpointer,int) = ptr;
1435 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1438 case MINT_ICALL_PPP_V: {
1439 void (*func)(gpointer,gpointer,gpointer) = ptr;
1441 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1444 case MINT_ICALL_PPI_V: {
1445 void (*func)(gpointer,gpointer,int) = ptr;
1447 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1451 g_assert_not_reached ();
1454 context->env_frame = old_env_frame;
1455 context->current_env = old_env;
1460 static mono_mutex_t create_method_pointer_mutex;
1462 static GHashTable *method_pointer_hash = NULL;
1465 interp_create_method_pointer (MonoMethod *method, MonoError *error)
1470 mono_os_mutex_lock (&create_method_pointer_mutex);
1471 if (!method_pointer_hash) {
1472 // FIXME: is registering method table as GC root really necessary?
1473 // MONO_GC_REGISTER_ROOT_FIXED (method_pointer_hash);
1474 method_pointer_hash = g_hash_table_new (NULL, NULL);
1476 addr = g_hash_table_lookup (method_pointer_hash, method);
1478 mono_os_mutex_unlock (&create_method_pointer_mutex);
1483 * If it is a static P/Invoke method, we can just return the pointer
1484 * to the method implementation.
1486 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && ((MonoMethodPInvoke*) method)->addr) {
1487 ji = g_new0 (MonoJitInfo, 1);
1488 ji->d.method = method;
1490 ji->code_start = addr = ((MonoMethodPInvoke*) method)->addr;
1492 mono_jit_info_table_add (mono_get_root_domain (), ji);
1495 g_error ("FIXME: not available? figure out new API");
1497 addr = mono_arch_create_method_pointer (method);
1501 g_hash_table_insert (method_pointer_hash, method, addr);
1502 mono_os_mutex_unlock (&create_method_pointer_mutex);
1508 static int opcode_counts[512];
1510 #define COUNT_OP(op) opcode_counts[op]++
1512 #define COUNT_OP(op)
1516 #define DUMP_INSTR() \
1517 if (tracing > 1) { \
1519 if (sp > frame->stack) { \
1520 ins = dump_stack (frame->stack, sp); \
1522 ins = g_strdup (""); \
1526 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
1527 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
1529 mono_interp_dis_mintop(rtm->code, ip); \
1530 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1534 #define DUMP_INSTR()
1538 #define USE_COMPUTED_GOTO 1
1540 #if USE_COMPUTED_GOTO
1541 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1542 #define MINT_IN_CASE(x) LAB_ ## x:
1544 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1546 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1548 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
1550 #define MINT_IN_SWITCH(op) switch (op)
1551 #define MINT_IN_CASE(x) case x:
1552 #define MINT_IN_BREAK break
1553 #define MINT_IN_DEFAULT default:
1557 * Defining this causes register allocation errors in some versions of gcc:
1558 * error: unable to find a register to spill in class `SIREG'
1560 /* #define MINT_USE_DEDICATED_IP_REG */
1563 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1565 MonoInvocation child_frame;
1566 GSList *finally_ips = NULL;
1567 const unsigned short *endfinally_ip = NULL;
1568 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1569 register const unsigned short *ip asm ("%esi");
1571 register const unsigned short *ip;
1573 register stackval *sp;
1576 gint tracing = global_tracing;
1577 unsigned char *vtalloc;
1580 unsigned char *vt_sp;
1581 unsigned char *locals;
1583 MonoObject *o = NULL;
1585 #if USE_COMPUTED_GOTO
1586 static void *in_labels[] = {
1587 #define OPDEF(a,b,c,d) \
1589 #include "mintops.def"
1594 frame->ex_handler = NULL;
1596 context->current_frame = frame;
1599 debug_enter (frame, &tracing);
1602 if (!frame->runtime_method->transformed) {
1603 context->managed_code = 0;
1605 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE);
1606 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
1609 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1610 context->managed_code = 1;
1618 rtm = frame->runtime_method;
1619 frame->args = alloca (rtm->alloca_size);
1620 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1623 memset(sp, 0, rtm->stack_size);
1625 vt_sp = (unsigned char *) sp + rtm->stack_size;
1629 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1631 child_frame.parent = frame;
1637 * using while (ip < end) may result in a 15% performance drop,
1638 * but it may be useful for debug
1642 /* g_assert (sp >= frame->stack); */
1643 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1645 MINT_IN_SWITCH (*ip) {
1646 MINT_IN_CASE(MINT_INITLOCALS)
1647 memset (locals, 0, rtm->locals_size);
1650 MINT_IN_CASE(MINT_NOP)
1653 MINT_IN_CASE(MINT_BREAK)
1655 G_BREAKPOINT (); /* this is not portable... */
1657 MINT_IN_CASE(MINT_LDNULL)
1662 MINT_IN_CASE(MINT_VTRESULT) {
1663 int ret_size = * (guint16 *)(ip + 1);
1664 unsigned char *ret_vt_sp = vt_sp;
1665 vt_sp -= READ32(ip + 2);
1667 memmove (vt_sp, ret_vt_sp, ret_size);
1668 sp [-1].data.p = vt_sp;
1669 vt_sp += (ret_size + 7) & ~7;
1674 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1675 MINT_IN_CASE(MINT_LDC_I4_M1)
1678 MINT_IN_CASE(MINT_LDC_I4_0)
1681 MINT_IN_CASE(MINT_LDC_I4_1)
1684 MINT_IN_CASE(MINT_LDC_I4_2)
1687 MINT_IN_CASE(MINT_LDC_I4_3)
1690 MINT_IN_CASE(MINT_LDC_I4_4)
1693 MINT_IN_CASE(MINT_LDC_I4_5)
1696 MINT_IN_CASE(MINT_LDC_I4_6)
1699 MINT_IN_CASE(MINT_LDC_I4_7)
1702 MINT_IN_CASE(MINT_LDC_I4_8)
1705 MINT_IN_CASE(MINT_LDC_I4_S)
1706 sp->data.i = *(const short *)(ip + 1);
1710 MINT_IN_CASE(MINT_LDC_I4)
1712 sp->data.i = READ32 (ip);
1716 MINT_IN_CASE(MINT_LDC_I8)
1718 sp->data.l = READ64 (ip);
1722 MINT_IN_CASE(MINT_LDC_R4) {
1726 sp->data.f = * (float *)&val;
1731 MINT_IN_CASE(MINT_LDC_R8)
1732 sp->data.l = READ64 (ip + 1); /* note union usage */
1736 MINT_IN_CASE(MINT_DUP)
1741 MINT_IN_CASE(MINT_DUP_VT)
1742 i32 = READ32 (ip + 1);
1744 memcpy(sp->data.p, sp [-1].data.p, i32);
1745 vt_sp += (i32 + 7) & ~7;
1749 MINT_IN_CASE(MINT_POP)
1753 MINT_IN_CASE(MINT_JMP) {
1754 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1755 if (!new_method->transformed) {
1757 frame->ex = mono_interp_transform_method (new_method, context);
1762 if (new_method->alloca_size > rtm->alloca_size)
1763 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1764 rtm = frame->runtime_method = new_method;
1765 vt_sp = (unsigned char *) sp + rtm->stack_size;
1769 locals = vt_sp + rtm->vt_stack_size;
1770 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1773 MINT_IN_CASE(MINT_CALLI) {
1774 MonoMethodSignature *csignature;
1775 stackval *endsp = sp;
1779 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1783 child_frame.runtime_method = sp->data.p;
1786 child_frame.retval = sp;
1787 /* decrement by the actual number of args */
1788 sp -= csignature->param_count;
1789 if (csignature->hasthis)
1791 child_frame.stack_args = sp;
1792 if (csignature->hasthis && mono_object_is_transparent_proxy (sp->data.p)) {
1793 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1794 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1795 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1796 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1797 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1800 ves_exec_method_with_context (&child_frame, context);
1802 context->current_frame = frame;
1804 if (child_frame.ex) {
1806 * An exception occurred, need to run finally, fault and catch handlers..
1808 frame->ex = child_frame.ex;
1809 goto handle_finally;
1812 /* need to handle typedbyref ... */
1813 if (csignature->ret->type != MONO_TYPE_VOID) {
1819 MINT_IN_CASE(MINT_CALLI_NAT) {
1820 MonoMethodSignature *csignature;
1821 stackval *endsp = sp;
1822 unsigned char *code = NULL;
1826 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1831 child_frame.runtime_method = NULL;
1834 child_frame.retval = sp;
1835 /* decrement by the actual number of args */
1836 sp -= csignature->param_count;
1837 if (csignature->hasthis)
1839 child_frame.stack_args = sp;
1840 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1842 context->current_frame = frame;
1844 if (child_frame.ex) {
1846 * An exception occurred, need to run finally, fault and catch handlers..
1848 frame->ex = child_frame.ex;
1849 if (context->search_for_handler) {
1850 context->search_for_handler = 0;
1851 goto handle_exception;
1853 goto handle_finally;
1856 /* need to handle typedbyref ... */
1857 if (csignature->ret->type != MONO_TYPE_VOID) {
1863 MINT_IN_CASE(MINT_CALL) {
1864 stackval *endsp = sp;
1868 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1871 child_frame.retval = sp;
1872 /* decrement by the actual number of args */
1873 sp -= child_frame.runtime_method->param_count;
1874 if (child_frame.runtime_method->hasthis)
1876 child_frame.stack_args = sp;
1877 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
1878 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1879 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1881 ves_exec_method_with_context (&child_frame, context);
1883 context->current_frame = frame;
1885 if (child_frame.ex) {
1887 * An exception occurred, need to run finally, fault and catch handlers..
1889 frame->ex = child_frame.ex;
1890 goto handle_finally;
1893 /* need to handle typedbyref ... */
1898 MINT_IN_CASE(MINT_VCALL) {
1901 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1905 child_frame.retval = sp;
1906 /* decrement by the actual number of args */
1907 sp -= child_frame.runtime_method->param_count;
1908 if (child_frame.runtime_method->hasthis)
1910 child_frame.stack_args = sp;
1912 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
1913 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1914 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1917 ves_exec_method_with_context (&child_frame, context);
1919 context->current_frame = frame;
1921 if (child_frame.ex) {
1923 * An exception occurred, need to run finally, fault and catch handlers..
1925 frame->ex = child_frame.ex;
1926 goto handle_finally;
1930 MINT_IN_CASE(MINT_CALLVIRT) {
1931 stackval *endsp = sp;
1932 MonoObject *this_arg;
1937 token = * (unsigned short *)(ip + 1);
1939 child_frame.runtime_method = rtm->data_items [token];
1941 child_frame.retval = sp;
1943 /* decrement by the actual number of args */
1944 sp -= child_frame.runtime_method->param_count + 1;
1945 child_frame.stack_args = sp;
1946 this_arg = sp->data.p;
1948 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1949 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1951 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1952 sp->data.p = (char *) this_arg + sizeof (MonoObject);
1955 ves_exec_method_with_context (&child_frame, context);
1957 context->current_frame = frame;
1959 if (child_frame.ex) {
1961 * An exception occurred, need to run finally, fault and catch handlers..
1963 frame->ex = child_frame.ex;
1964 if (context->search_for_handler) {
1965 context->search_for_handler = 0;
1966 goto handle_exception;
1968 goto handle_finally;
1971 /* need to handle typedbyref ... */
1976 MINT_IN_CASE(MINT_VCALLVIRT) {
1977 MonoObject *this_arg;
1982 token = * (unsigned short *)(ip + 1);
1984 child_frame.runtime_method = rtm->data_items [token];
1986 child_frame.retval = sp;
1988 /* decrement by the actual number of args */
1989 sp -= child_frame.runtime_method->param_count + 1;
1990 child_frame.stack_args = sp;
1991 this_arg = sp->data.p;
1993 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1994 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1996 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1997 sp->data.p = (char *) this_arg + sizeof (MonoObject);
2000 ves_exec_method_with_context (&child_frame, context);
2002 context->current_frame = frame;
2004 if (child_frame.ex) {
2006 * An exception occurred, need to run finally, fault and catch handlers..
2008 frame->ex = child_frame.ex;
2009 if (context->search_for_handler) {
2010 context->search_for_handler = 0;
2011 goto handle_exception;
2013 goto handle_finally;
2017 MINT_IN_CASE(MINT_CALLRUN)
2018 ves_runtime_method (frame, context);
2021 goto handle_exception;
2024 MINT_IN_CASE(MINT_RET)
2026 *frame->retval = *sp;
2027 if (sp > frame->stack)
2028 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2030 MINT_IN_CASE(MINT_RET_VOID)
2031 if (sp > frame->stack)
2032 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
2034 MINT_IN_CASE(MINT_RET_VT)
2035 i32 = READ32(ip + 1);
2037 memcpy(frame->retval->data.p, sp->data.p, i32);
2038 if (sp > frame->stack)
2039 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2041 MINT_IN_CASE(MINT_BR_S)
2042 ip += (short) *(ip + 1);
2044 MINT_IN_CASE(MINT_BR)
2045 ip += (gint32) READ32(ip + 1);
2047 #define ZEROP_S(datamem, op) \
2049 if (sp->data.datamem op 0) \
2050 ip += * (gint16 *)(ip + 1); \
2054 #define ZEROP(datamem, op) \
2056 if (sp->data.datamem op 0) \
2057 ip += READ32(ip + 1); \
2061 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2064 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2067 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2070 MINT_IN_CASE(MINT_BRFALSE_I4)
2073 MINT_IN_CASE(MINT_BRFALSE_I8)
2076 MINT_IN_CASE(MINT_BRFALSE_R8)
2079 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2082 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2085 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2088 MINT_IN_CASE(MINT_BRTRUE_I4)
2091 MINT_IN_CASE(MINT_BRTRUE_I8)
2094 MINT_IN_CASE(MINT_BRTRUE_R8)
2097 #define CONDBR_S(cond) \
2100 ip += * (gint16 *)(ip + 1); \
2103 #define BRELOP_S(datamem, op) \
2104 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2106 #define CONDBR(cond) \
2109 ip += READ32(ip + 1); \
2113 #define BRELOP(datamem, op) \
2114 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2116 MINT_IN_CASE(MINT_BEQ_I4_S)
2119 MINT_IN_CASE(MINT_BEQ_I8_S)
2122 MINT_IN_CASE(MINT_BEQ_R8_S)
2123 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2125 MINT_IN_CASE(MINT_BEQ_I4)
2128 MINT_IN_CASE(MINT_BEQ_I8)
2131 MINT_IN_CASE(MINT_BEQ_R8)
2132 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2134 MINT_IN_CASE(MINT_BGE_I4_S)
2137 MINT_IN_CASE(MINT_BGE_I8_S)
2140 MINT_IN_CASE(MINT_BGE_R8_S)
2141 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2143 MINT_IN_CASE(MINT_BGE_I4)
2146 MINT_IN_CASE(MINT_BGE_I8)
2149 MINT_IN_CASE(MINT_BGE_R8)
2150 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2152 MINT_IN_CASE(MINT_BGT_I4_S)
2155 MINT_IN_CASE(MINT_BGT_I8_S)
2158 MINT_IN_CASE(MINT_BGT_R8_S)
2159 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2161 MINT_IN_CASE(MINT_BGT_I4)
2164 MINT_IN_CASE(MINT_BGT_I8)
2167 MINT_IN_CASE(MINT_BGT_R8)
2168 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2170 MINT_IN_CASE(MINT_BLT_I4_S)
2173 MINT_IN_CASE(MINT_BLT_I8_S)
2176 MINT_IN_CASE(MINT_BLT_R8_S)
2177 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2179 MINT_IN_CASE(MINT_BLT_I4)
2182 MINT_IN_CASE(MINT_BLT_I8)
2185 MINT_IN_CASE(MINT_BLT_R8)
2186 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2188 MINT_IN_CASE(MINT_BLE_I4_S)
2191 MINT_IN_CASE(MINT_BLE_I8_S)
2194 MINT_IN_CASE(MINT_BLE_R8_S)
2195 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2197 MINT_IN_CASE(MINT_BLE_I4)
2200 MINT_IN_CASE(MINT_BLE_I8)
2203 MINT_IN_CASE(MINT_BLE_R8)
2204 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2206 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2209 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2212 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2213 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2215 MINT_IN_CASE(MINT_BNE_UN_I4)
2218 MINT_IN_CASE(MINT_BNE_UN_I8)
2221 MINT_IN_CASE(MINT_BNE_UN_R8)
2222 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2225 #define BRELOP_S_CAST(datamem, op, type) \
2227 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2228 ip += * (gint16 *)(ip + 1); \
2232 #define BRELOP_CAST(datamem, op, type) \
2234 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2235 ip += READ32(ip + 1); \
2239 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2240 BRELOP_S_CAST(i, >=, guint32);
2242 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2243 BRELOP_S_CAST(l, >=, guint64);
2245 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2246 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2248 MINT_IN_CASE(MINT_BGE_UN_I4)
2249 BRELOP_CAST(i, >=, guint32);
2251 MINT_IN_CASE(MINT_BGE_UN_I8)
2252 BRELOP_CAST(l, >=, guint64);
2254 MINT_IN_CASE(MINT_BGE_UN_R8)
2255 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2257 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2258 BRELOP_S_CAST(i, >, guint32);
2260 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2261 BRELOP_S_CAST(l, >, guint64);
2263 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2264 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2266 MINT_IN_CASE(MINT_BGT_UN_I4)
2267 BRELOP_CAST(i, >, guint32);
2269 MINT_IN_CASE(MINT_BGT_UN_I8)
2270 BRELOP_CAST(l, >, guint64);
2272 MINT_IN_CASE(MINT_BGT_UN_R8)
2273 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2275 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2276 BRELOP_S_CAST(i, <=, guint32);
2278 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2279 BRELOP_S_CAST(l, <=, guint64);
2281 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2282 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2284 MINT_IN_CASE(MINT_BLE_UN_I4)
2285 BRELOP_CAST(i, <=, guint32);
2287 MINT_IN_CASE(MINT_BLE_UN_I8)
2288 BRELOP_CAST(l, <=, guint64);
2290 MINT_IN_CASE(MINT_BLE_UN_R8)
2291 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2293 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2294 BRELOP_S_CAST(i, <, guint32);
2296 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2297 BRELOP_S_CAST(l, <, guint64);
2299 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2300 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2302 MINT_IN_CASE(MINT_BLT_UN_I4)
2303 BRELOP_CAST(i, <, guint32);
2305 MINT_IN_CASE(MINT_BLT_UN_I8)
2306 BRELOP_CAST(l, <, guint64);
2308 MINT_IN_CASE(MINT_BLT_UN_R8)
2309 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2311 MINT_IN_CASE(MINT_SWITCH) {
2313 const unsigned short *st;
2319 if ((guint32)sp->data.i < n) {
2321 ip += 2 * (guint32)sp->data.i;
2322 offset = READ32 (ip);
2329 MINT_IN_CASE(MINT_LDIND_I1)
2331 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2333 MINT_IN_CASE(MINT_LDIND_U1)
2335 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2337 MINT_IN_CASE(MINT_LDIND_I2)
2339 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2341 MINT_IN_CASE(MINT_LDIND_U2)
2343 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2345 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2346 MINT_IN_CASE(MINT_LDIND_U4)
2348 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2350 MINT_IN_CASE(MINT_LDIND_I8)
2352 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2354 MINT_IN_CASE(MINT_LDIND_I)
2356 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2358 MINT_IN_CASE(MINT_LDIND_R4)
2360 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2362 MINT_IN_CASE(MINT_LDIND_R8)
2364 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2366 MINT_IN_CASE(MINT_LDIND_REF)
2368 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2370 MINT_IN_CASE(MINT_STIND_REF)
2373 * (gpointer *) sp->data.p = sp[1].data.p;
2375 MINT_IN_CASE(MINT_STIND_I1)
2378 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2380 MINT_IN_CASE(MINT_STIND_I2)
2383 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2385 MINT_IN_CASE(MINT_STIND_I4)
2388 * (gint32 *) sp->data.p = sp[1].data.i;
2390 MINT_IN_CASE(MINT_STIND_I)
2393 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2395 MINT_IN_CASE(MINT_STIND_I8)
2398 * (gint64 *) sp->data.p = sp[1].data.l;
2400 MINT_IN_CASE(MINT_STIND_R4)
2403 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2405 MINT_IN_CASE(MINT_STIND_R8)
2408 * (double *) sp->data.p = sp[1].data.f;
2410 #define BINOP(datamem, op) \
2412 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2414 MINT_IN_CASE(MINT_ADD_I4)
2417 MINT_IN_CASE(MINT_ADD_I8)
2420 MINT_IN_CASE(MINT_ADD_R8)
2423 MINT_IN_CASE(MINT_ADD1_I4)
2427 MINT_IN_CASE(MINT_SUB_I4)
2430 MINT_IN_CASE(MINT_SUB_I8)
2433 MINT_IN_CASE(MINT_SUB_R8)
2436 MINT_IN_CASE(MINT_SUB1_I4)
2440 MINT_IN_CASE(MINT_MUL_I4)
2443 MINT_IN_CASE(MINT_MUL_I8)
2446 MINT_IN_CASE(MINT_MUL_R8)
2449 MINT_IN_CASE(MINT_DIV_I4)
2450 if (sp [-1].data.i == 0)
2451 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2452 if (sp [-1].data.i == (-1))
2453 THROW_EX (mono_get_exception_overflow (), ip);
2456 MINT_IN_CASE(MINT_DIV_I8)
2457 if (sp [-1].data.l == 0)
2458 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2459 if (sp [-1].data.l == (-1))
2460 THROW_EX (mono_get_exception_overflow (), ip);
2463 MINT_IN_CASE(MINT_DIV_R8)
2467 #define BINOP_CAST(datamem, op, type) \
2469 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2471 MINT_IN_CASE(MINT_DIV_UN_I4)
2472 if (sp [-1].data.i == 0)
2473 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2474 BINOP_CAST(i, /, guint32);
2476 MINT_IN_CASE(MINT_DIV_UN_I8)
2477 if (sp [-1].data.l == 0)
2478 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2479 BINOP_CAST(l, /, guint64);
2481 MINT_IN_CASE(MINT_REM_I4)
2482 if (sp [-1].data.i == 0)
2483 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2486 MINT_IN_CASE(MINT_REM_I8)
2487 if (sp [-1].data.l == 0)
2488 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2491 MINT_IN_CASE(MINT_REM_R8)
2492 /* FIXME: what do we actually do here? */
2494 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2497 MINT_IN_CASE(MINT_REM_UN_I4)
2498 if (sp [-1].data.i == 0)
2499 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2500 BINOP_CAST(i, %, guint32);
2502 MINT_IN_CASE(MINT_REM_UN_I8)
2503 if (sp [-1].data.l == 0)
2504 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2505 BINOP_CAST(l, %, guint64);
2507 MINT_IN_CASE(MINT_AND_I4)
2510 MINT_IN_CASE(MINT_AND_I8)
2513 MINT_IN_CASE(MINT_OR_I4)
2516 MINT_IN_CASE(MINT_OR_I8)
2519 MINT_IN_CASE(MINT_XOR_I4)
2522 MINT_IN_CASE(MINT_XOR_I8)
2526 #define SHIFTOP(datamem, op) \
2528 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2531 MINT_IN_CASE(MINT_SHL_I4)
2534 MINT_IN_CASE(MINT_SHL_I8)
2537 MINT_IN_CASE(MINT_SHR_I4)
2540 MINT_IN_CASE(MINT_SHR_I8)
2543 MINT_IN_CASE(MINT_SHR_UN_I4)
2545 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2548 MINT_IN_CASE(MINT_SHR_UN_I8)
2550 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2553 MINT_IN_CASE(MINT_NEG_I4)
2554 sp [-1].data.i = - sp [-1].data.i;
2557 MINT_IN_CASE(MINT_NEG_I8)
2558 sp [-1].data.l = - sp [-1].data.l;
2561 MINT_IN_CASE(MINT_NEG_R8)
2562 sp [-1].data.f = - sp [-1].data.f;
2565 MINT_IN_CASE(MINT_NOT_I4)
2566 sp [-1].data.i = ~ sp [-1].data.i;
2569 MINT_IN_CASE(MINT_NOT_I8)
2570 sp [-1].data.l = ~ sp [-1].data.l;
2573 MINT_IN_CASE(MINT_CONV_I1_I4)
2574 sp [-1].data.i = (gint8)sp [-1].data.i;
2577 MINT_IN_CASE(MINT_CONV_I1_I8)
2578 sp [-1].data.i = (gint8)sp [-1].data.l;
2581 MINT_IN_CASE(MINT_CONV_I1_R8)
2582 sp [-1].data.i = (gint8)sp [-1].data.f;
2585 MINT_IN_CASE(MINT_CONV_U1_I4)
2586 sp [-1].data.i = (guint8)sp [-1].data.i;
2589 MINT_IN_CASE(MINT_CONV_U1_I8)
2590 sp [-1].data.i = (guint8)sp [-1].data.l;
2593 MINT_IN_CASE(MINT_CONV_U1_R8)
2594 sp [-1].data.i = (guint8)sp [-1].data.f;
2597 MINT_IN_CASE(MINT_CONV_I2_I4)
2598 sp [-1].data.i = (gint16)sp [-1].data.i;
2601 MINT_IN_CASE(MINT_CONV_I2_I8)
2602 sp [-1].data.i = (gint16)sp [-1].data.l;
2605 MINT_IN_CASE(MINT_CONV_I2_R8)
2606 sp [-1].data.i = (gint16)sp [-1].data.f;
2609 MINT_IN_CASE(MINT_CONV_U2_I4)
2610 sp [-1].data.i = (guint16)sp [-1].data.i;
2613 MINT_IN_CASE(MINT_CONV_U2_I8)
2614 sp [-1].data.i = (guint16)sp [-1].data.l;
2617 MINT_IN_CASE(MINT_CONV_U2_R8)
2618 sp [-1].data.i = (guint16)sp [-1].data.f;
2621 MINT_IN_CASE(MINT_CONV_I4_R8)
2622 sp [-1].data.i = (gint32)sp [-1].data.f;
2625 MINT_IN_CASE(MINT_CONV_U4_I8)
2626 MINT_IN_CASE(MINT_CONV_I4_I8)
2627 sp [-1].data.i = (gint32)sp [-1].data.l;
2630 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2631 sp [-2].data.i = (gint32)sp [-2].data.l;
2634 MINT_IN_CASE(MINT_CONV_U4_R8)
2635 sp [-1].data.i = (guint32)sp [-1].data.f;
2638 MINT_IN_CASE(MINT_CONV_I8_I4)
2639 sp [-1].data.l = sp [-1].data.i;
2642 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2643 sp [-2].data.l = sp [-2].data.i;
2646 MINT_IN_CASE(MINT_CONV_I8_U4)
2647 sp [-1].data.l = (guint32)sp [-1].data.i;
2650 MINT_IN_CASE(MINT_CONV_I8_R8)
2651 sp [-1].data.l = (gint64)sp [-1].data.f;
2654 MINT_IN_CASE(MINT_CONV_R4_I4)
2655 sp [-1].data.f = (float)sp [-1].data.i;
2658 MINT_IN_CASE(MINT_CONV_R4_I8)
2659 sp [-1].data.f = (float)sp [-1].data.l;
2662 MINT_IN_CASE(MINT_CONV_R4_R8)
2663 sp [-1].data.f = (float)sp [-1].data.f;
2666 MINT_IN_CASE(MINT_CONV_R8_I4)
2667 sp [-1].data.f = (double)sp [-1].data.i;
2670 MINT_IN_CASE(MINT_CONV_R8_I8)
2671 sp [-1].data.f = (double)sp [-1].data.l;
2674 MINT_IN_CASE(MINT_CONV_U8_I4)
2675 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2678 MINT_IN_CASE(MINT_CONV_U8_R8)
2679 sp [-1].data.l = (guint64)sp [-1].data.f;
2683 MINT_IN_CASE(MINT_CPOBJ) {
2686 vtklass = rtm->data_items[READ32 (ip)];
2689 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2693 MINT_IN_CASE(MINT_LDOBJ) {
2696 c = rtm->data_items[* (guint16 *)(ip + 1)];
2698 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2700 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2702 size = mono_class_value_size (c, NULL);
2704 sp [-1].data.p = vt_sp;
2705 memcpy(vt_sp, p, size);
2706 vt_sp += (size + 7) & ~7;
2710 MINT_IN_CASE(MINT_LDSTR)
2711 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2715 MINT_IN_CASE(MINT_NEWOBJ) {
2716 MonoClass *newobj_class;
2717 MonoMethodSignature *csig;
2718 stackval valuetype_this;
2724 token = * (guint16 *)(ip + 1);
2727 child_frame.runtime_method = rtm->data_items [token];
2728 csig = mono_method_signature (child_frame.runtime_method->method);
2729 newobj_class = child_frame.runtime_method->method->klass;
2730 /*if (profiling_classes) {
2731 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2733 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2736 if (newobj_class->parent == mono_defaults.array_class) {
2737 sp -= csig->param_count;
2738 o = ves_array_create (context->domain, newobj_class, csig, sp);
2739 goto array_constructed;
2742 g_assert (csig->hasthis);
2743 if (csig->param_count) {
2744 sp -= csig->param_count;
2745 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
2747 child_frame.stack_args = sp;
2750 * First arg is the object.
2752 if (newobj_class->valuetype) {
2753 MonoType *t = &newobj_class->byval_arg;
2754 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
2756 valuetype_this.data.p = vt_sp;
2758 memset (&valuetype_this, 0, sizeof (stackval));
2759 sp->data.p = &valuetype_this;
2762 if (newobj_class != mono_defaults.string_class) {
2763 context->managed_code = 0;
2764 o = mono_object_new_checked (context->domain, newobj_class, &error);
2765 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2766 context->managed_code = 1;
2767 if (*mono_thread_interruption_request_flag ())
2768 mono_thread_interruption_checkpoint ();
2771 child_frame.retval = &retval;
2775 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2777 child_frame.ip = NULL;
2778 child_frame.ex = NULL;
2780 ves_exec_method_with_context (&child_frame, context);
2782 context->current_frame = frame;
2784 if (child_frame.ex) {
2786 * An exception occurred, need to run finally, fault and catch handlers..
2788 frame->ex = child_frame.ex;
2789 goto handle_finally;
2792 * a constructor returns void, but we need to return the object we created
2795 if (newobj_class->valuetype && !newobj_class->enumtype) {
2796 *sp = valuetype_this;
2797 } else if (newobj_class == mono_defaults.string_class) {
2805 MINT_IN_CASE(MINT_CASTCLASS)
2806 c = rtm->data_items [*(guint16 *)(ip + 1)];
2807 if ((o = sp [-1].data.p)) {
2808 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2809 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2811 THROW_EX (mono_get_exception_invalid_cast (), ip);
2815 MINT_IN_CASE(MINT_ISINST)
2816 c = rtm->data_items [*(guint16 *)(ip + 1)];
2817 if ((o = sp [-1].data.p)) {
2818 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2819 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2821 sp [-1].data.p = NULL;
2825 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2826 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2829 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2830 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2833 MINT_IN_CASE(MINT_UNBOX)
2834 c = rtm->data_items[*(guint16 *)(ip + 1)];
2838 THROW_EX (mono_get_exception_null_reference (), ip);
2840 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2841 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2842 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
2843 THROW_EX (mono_get_exception_invalid_cast (), ip);
2845 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2846 int size = mono_class_native_size (c, NULL);
2847 sp [-1].data.p = vt_sp;
2848 vt_sp += (size + 7) & ~7;
2850 stackval_from_data (&c->byval_arg, &sp [-1], mono_object_unbox (o), FALSE);
2853 MINT_IN_CASE(MINT_THROW)
2855 frame->ex_handler = NULL;
2857 sp->data.p = mono_get_exception_null_reference ();
2858 THROW_EX ((MonoException *)sp->data.p, ip);
2860 MINT_IN_CASE(MINT_LDFLDA)
2863 THROW_EX (mono_get_exception_null_reference (), ip);
2864 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2867 MINT_IN_CASE(MINT_CKNULL)
2870 THROW_EX (mono_get_exception_null_reference (), ip);
2874 #define LDFLD(datamem, fieldtype) \
2875 o = sp [-1].data.p; \
2877 THROW_EX (mono_get_exception_null_reference (), ip); \
2878 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2881 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2882 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2883 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2884 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2885 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2886 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2887 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2888 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2889 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2890 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2892 MINT_IN_CASE(MINT_LDFLD_VT)
2895 THROW_EX (mono_get_exception_null_reference (), ip);
2896 i32 = READ32(ip + 2);
2897 sp [-1].data.p = vt_sp;
2898 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2899 vt_sp += (i32 + 7) & ~7;
2903 MINT_IN_CASE(MINT_LDRMFLD) {
2905 MonoClassField *field;
2910 THROW_EX (mono_get_exception_null_reference (), ip);
2911 field = rtm->data_items[* (guint16 *)(ip + 1)];
2913 if (mono_object_is_transparent_proxy (o)) {
2914 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2916 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2917 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2919 addr = (char*)o + field->offset;
2922 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2926 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2927 MonoClassField *field;
2933 THROW_EX (mono_get_exception_null_reference (), ip);
2934 field = rtm->data_items[* (guint16 *)(ip + 1)];
2935 i32 = READ32(ip + 2);
2937 if (mono_object_is_transparent_proxy (o)) {
2938 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2939 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2940 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2942 addr = (char*)o + field->offset;
2945 sp [-1].data.p = vt_sp;
2946 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2947 vt_sp += (i32 + 7) & ~7;
2948 memcpy(sp [-1].data.p, addr, i32);
2952 #define STFLD(datamem, fieldtype) \
2953 o = sp [-2].data.p; \
2955 THROW_EX (mono_get_exception_null_reference (), ip); \
2957 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2960 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2961 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2962 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2963 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2964 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2965 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2966 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2967 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2968 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2969 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2971 MINT_IN_CASE(MINT_STFLD_VT)
2974 THROW_EX (mono_get_exception_null_reference (), ip);
2975 i32 = READ32(ip + 2);
2977 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2978 vt_sp -= (i32 + 7) & ~7;
2982 MINT_IN_CASE(MINT_STRMFLD) {
2983 MonoClassField *field;
2987 THROW_EX (mono_get_exception_null_reference (), ip);
2989 field = rtm->data_items[* (guint16 *)(ip + 1)];
2992 if (mono_object_is_transparent_proxy (o)) {
2993 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2994 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2995 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2997 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3002 MINT_IN_CASE(MINT_STRMFLD_VT) {
3003 MonoClassField *field;
3007 THROW_EX (mono_get_exception_null_reference (), ip);
3008 field = rtm->data_items[* (guint16 *)(ip + 1)];
3009 i32 = READ32(ip + 2);
3012 if (mono_object_is_transparent_proxy (o)) {
3013 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3014 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3015 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3017 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3020 vt_sp -= (i32 + 7) & ~7;
3023 MINT_IN_CASE(MINT_LDSFLDA) {
3024 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3025 sp->data.p = mono_class_static_field_address (context->domain, field);
3030 MINT_IN_CASE(MINT_LDSFLD) {
3031 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3032 gpointer addr = mono_class_static_field_address (context->domain, field);
3033 stackval_from_data (field->type, sp, addr, FALSE);
3038 MINT_IN_CASE(MINT_LDSFLD_VT) {
3039 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3040 gpointer addr = mono_class_static_field_address (context->domain, field);
3041 int size = READ32 (ip + 2);
3045 vt_sp += (size + 7) & ~7;
3046 stackval_from_data (field->type, sp, addr, FALSE);
3050 MINT_IN_CASE(MINT_STSFLD) {
3051 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3052 gpointer addr = mono_class_static_field_address (context->domain, field);
3055 stackval_to_data (field->type, sp, addr, FALSE);
3058 MINT_IN_CASE(MINT_STSFLD_VT) {
3059 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3060 gpointer addr = mono_class_static_field_address (context->domain, field);
3061 int size = READ32 (ip + 2);
3065 stackval_to_data (field->type, sp, addr, FALSE);
3066 vt_sp -= (size + 7) & ~7;
3069 MINT_IN_CASE(MINT_STOBJ_VT) {
3071 c = rtm->data_items[* (guint16 *)(ip + 1)];
3073 size = mono_class_value_size (c, NULL);
3074 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3075 vt_sp -= (size + 7) & ~7;
3079 MINT_IN_CASE(MINT_STOBJ) {
3081 c = rtm->data_items[* (guint16 *)(ip + 1)];
3083 size = mono_class_value_size (c, NULL);
3084 memcpy(sp [-2].data.p, &sp [-1].data, size);
3088 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3089 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3090 THROW_EX (mono_get_exception_overflow (), ip);
3091 sp [-1].data.i = (guint32)sp [-1].data.f;
3094 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3095 if (sp [-1].data.i < 0)
3096 THROW_EX (mono_get_exception_overflow (), ip);
3097 sp [-1].data.l = sp [-1].data.i;
3100 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3101 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3102 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3103 THROW_EX (mono_get_exception_overflow (), ip);
3104 sp [-1].data.l = (guint64)sp [-1].data.f;
3107 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3108 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3109 THROW_EX (mono_get_exception_overflow (), ip);
3110 sp [-1].data.l = (gint64)sp [-1].data.f;
3113 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3114 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3115 THROW_EX (mono_get_exception_overflow (), ip);
3116 sp [-1].data.i = (mono_u)sp [-1].data.l;
3119 MINT_IN_CASE(MINT_BOX)
3120 c = rtm->data_items [* (guint16 *)(ip + 1)];
3122 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3123 int size = mono_class_value_size (c, NULL);
3124 sp [-1].data.p = mono_value_box_checked (context->domain, c, sp [-1].data.p, &error);
3125 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3126 size = (size + 7) & ~7;
3130 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3131 sp [-1].data.p = mono_value_box_checked (context->domain, c, &sp [-1], &error);
3132 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3136 MINT_IN_CASE(MINT_NEWARR)
3137 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3138 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3140 /*if (profiling_classes) {
3141 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3143 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3147 MINT_IN_CASE(MINT_LDLEN)
3150 THROW_EX (mono_get_exception_null_reference (), ip);
3151 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3154 MINT_IN_CASE(MINT_GETCHR) {
3158 THROW_EX (mono_get_exception_null_reference (), ip);
3159 i32 = sp [-1].data.i;
3160 if (i32 < 0 || i32 >= mono_string_length (s))
3161 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3163 sp [-1].data.i = mono_string_chars(s)[i32];
3167 MINT_IN_CASE(MINT_STRLEN)
3169 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3171 MINT_IN_CASE(MINT_ARRAY_RANK)
3174 THROW_EX (mono_get_exception_null_reference (), ip);
3175 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3178 MINT_IN_CASE(MINT_LDELEMA) {
3182 /*token = READ32 (ip)*/;
3188 aindex = sp [1].data.i;
3189 if (aindex >= mono_array_length ((MonoArray *) o))
3190 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3192 /* check the array element corresponds to token */
3193 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3195 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3199 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3200 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3201 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3202 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3203 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3204 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3205 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3206 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3207 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3208 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3209 MINT_IN_CASE(MINT_LDELEM_REF) {
3217 THROW_EX (mono_get_exception_null_reference (), ip);
3219 aindex = sp [1].data.i;
3220 if (aindex >= mono_array_length (o))
3221 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3224 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3227 case MINT_LDELEM_I1:
3228 sp [0].data.i = mono_array_get (o, gint8, aindex);
3230 case MINT_LDELEM_U1:
3231 sp [0].data.i = mono_array_get (o, guint8, aindex);
3233 case MINT_LDELEM_I2:
3234 sp [0].data.i = mono_array_get (o, gint16, aindex);
3236 case MINT_LDELEM_U2:
3237 sp [0].data.i = mono_array_get (o, guint16, aindex);
3240 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3242 case MINT_LDELEM_I4:
3243 sp [0].data.i = mono_array_get (o, gint32, aindex);
3245 case MINT_LDELEM_U4:
3246 sp [0].data.i = mono_array_get (o, guint32, aindex);
3248 case MINT_LDELEM_I8:
3249 sp [0].data.l = mono_array_get (o, guint64, aindex);
3251 case MINT_LDELEM_R4:
3252 sp [0].data.f = mono_array_get (o, float, aindex);
3254 case MINT_LDELEM_R8:
3255 sp [0].data.f = mono_array_get (o, double, aindex);
3257 case MINT_LDELEM_REF:
3258 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3268 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3269 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3270 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3271 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3272 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3273 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3274 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3275 MINT_IN_CASE(MINT_STELEM_REF) {
3282 THROW_EX (mono_get_exception_null_reference (), ip);
3284 aindex = sp [1].data.i;
3285 if (aindex >= mono_array_length ((MonoArray *)o))
3286 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3290 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3292 case MINT_STELEM_I1:
3293 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3295 case MINT_STELEM_I2:
3296 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3298 case MINT_STELEM_I4:
3299 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3301 case MINT_STELEM_I8:
3302 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3304 case MINT_STELEM_R4:
3305 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3307 case MINT_STELEM_R8:
3308 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3310 case MINT_STELEM_REF: {
3311 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3312 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3313 if (sp [2].data.p && !isinst_obj)
3314 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3315 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3324 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3325 if (sp [-1].data.i < 0)
3326 THROW_EX (mono_get_exception_overflow (), ip);
3329 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3330 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3331 THROW_EX (mono_get_exception_overflow (), ip);
3332 sp [-1].data.i = (gint32) sp [-1].data.l;
3335 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3336 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3337 THROW_EX (mono_get_exception_overflow (), ip);
3338 sp [-1].data.i = (gint32) sp [-1].data.f;
3341 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3342 if (sp [-1].data.i < 0)
3343 THROW_EX (mono_get_exception_overflow (), ip);
3346 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3347 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3348 THROW_EX (mono_get_exception_overflow (), ip);
3349 sp [-1].data.i = (guint32) sp [-1].data.l;
3352 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3353 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3354 THROW_EX (mono_get_exception_overflow (), ip);
3355 sp [-1].data.i = (guint32) sp [-1].data.f;
3358 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3359 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3360 THROW_EX (mono_get_exception_overflow (), ip);
3363 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3364 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3365 THROW_EX (mono_get_exception_overflow (), ip);
3366 sp [-1].data.i = (gint16) sp [-1].data.l;
3369 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3370 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3371 THROW_EX (mono_get_exception_overflow (), ip);
3372 sp [-1].data.i = (gint16) sp [-1].data.f;
3375 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3376 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3377 THROW_EX (mono_get_exception_overflow (), ip);
3380 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3381 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3382 THROW_EX (mono_get_exception_overflow (), ip);
3383 sp [-1].data.i = (guint16) sp [-1].data.l;
3386 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3387 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3388 THROW_EX (mono_get_exception_overflow (), ip);
3389 sp [-1].data.i = (guint16) sp [-1].data.f;
3392 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3393 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3394 THROW_EX (mono_get_exception_overflow (), ip);
3397 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3398 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3399 THROW_EX (mono_get_exception_overflow (), ip);
3400 sp [-1].data.i = (gint8) sp [-1].data.l;
3403 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3404 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3405 THROW_EX (mono_get_exception_overflow (), ip);
3406 sp [-1].data.i = (gint8) sp [-1].data.f;
3409 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3410 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3411 THROW_EX (mono_get_exception_overflow (), ip);
3414 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3415 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3416 THROW_EX (mono_get_exception_overflow (), ip);
3417 sp [-1].data.i = (guint8) sp [-1].data.l;
3420 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3421 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3422 THROW_EX (mono_get_exception_overflow (), ip);
3423 sp [-1].data.i = (guint8) sp [-1].data.f;
3427 MINT_IN_CASE(MINT_LDELEM)
3428 MINT_IN_CASE(MINT_STELEM)
3429 MINT_IN_CASE(MINT_UNBOX_ANY)
3431 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3433 MINT_IN_CASE(MINT_CKFINITE)
3434 if (!isfinite(sp [-1].data.f))
3435 THROW_EX (mono_get_exception_arithmetic (), ip);
3439 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3441 MINT_IN_CASE(MINT_LDTOKEN)
3444 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3448 MINT_IN_CASE(MINT_ADD_OVF_I4)
3449 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3450 THROW_EX (mono_get_exception_overflow (), ip);
3453 MINT_IN_CASE(MINT_ADD_OVF_I8)
3454 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3455 THROW_EX (mono_get_exception_overflow (), ip);
3458 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3459 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3460 THROW_EX (mono_get_exception_overflow (), ip);
3461 BINOP_CAST(i, +, guint32);
3463 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3464 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3465 THROW_EX (mono_get_exception_overflow (), ip);
3466 BINOP_CAST(l, +, guint64);
3468 MINT_IN_CASE(MINT_MUL_OVF_I4)
3469 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3470 THROW_EX (mono_get_exception_overflow (), ip);
3473 MINT_IN_CASE(MINT_MUL_OVF_I8)
3474 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3475 THROW_EX (mono_get_exception_overflow (), ip);
3478 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3479 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3480 THROW_EX (mono_get_exception_overflow (), ip);
3481 BINOP_CAST(i, *, guint32);
3483 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3484 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3485 THROW_EX (mono_get_exception_overflow (), ip);
3486 BINOP_CAST(l, *, guint64);
3488 MINT_IN_CASE(MINT_SUB_OVF_I4)
3489 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3490 THROW_EX (mono_get_exception_overflow (), ip);
3493 MINT_IN_CASE(MINT_SUB_OVF_I8)
3494 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3495 THROW_EX (mono_get_exception_overflow (), ip);
3498 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3499 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3500 THROW_EX (mono_get_exception_overflow (), ip);
3501 BINOP_CAST(i, -, guint32);
3503 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3504 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3505 THROW_EX (mono_get_exception_overflow (), ip);
3506 BINOP_CAST(l, -, guint64);
3508 MINT_IN_CASE(MINT_ENDFINALLY)
3510 ip = finally_ips->data;
3511 finally_ips = g_slist_remove (finally_ips, ip);
3518 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3519 MINT_IN_CASE(MINT_LEAVE_S)
3520 while (sp > frame->stack) {
3524 if (*ip == MINT_LEAVE_S) {
3525 ip += (short) *(ip + 1);
3527 ip += (gint32) READ32 (ip + 1);
3530 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3531 frame->ex_handler = NULL;
3534 goto handle_finally;
3536 MINT_IN_CASE(MINT_ICALL_V_V)
3537 MINT_IN_CASE(MINT_ICALL_V_P)
3538 MINT_IN_CASE(MINT_ICALL_P_V)
3539 MINT_IN_CASE(MINT_ICALL_P_P)
3540 MINT_IN_CASE(MINT_ICALL_PP_V)
3541 MINT_IN_CASE(MINT_ICALL_PI_V)
3542 MINT_IN_CASE(MINT_ICALL_PP_P)
3543 MINT_IN_CASE(MINT_ICALL_PI_P)
3544 MINT_IN_CASE(MINT_ICALL_PPP_V)
3545 MINT_IN_CASE(MINT_ICALL_PPI_V)
3546 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3547 if (frame->ex != NULL)
3548 goto handle_exception;
3551 MINT_IN_CASE(MINT_MONO_LDPTR)
3552 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3556 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3557 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3558 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3562 MINT_IN_CASE(MINT_MONO_FREE)
3565 g_error ("that doesn't seem right");
3566 g_free (sp->data.p);
3568 MINT_IN_CASE(MINT_MONO_RETOBJ)
3571 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3572 mono_method_signature (frame->runtime_method->method)->pinvoke);
3573 if (sp > frame->stack)
3574 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3577 #define RELOP(datamem, op) \
3579 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3581 MINT_IN_CASE(MINT_CEQ_I4)
3584 MINT_IN_CASE(MINT_CEQ0_I4)
3585 sp [-1].data.i = (sp [-1].data.i == 0);
3588 MINT_IN_CASE(MINT_CEQ_I8)
3591 MINT_IN_CASE(MINT_CEQ_R8)
3593 if (isunordered (sp [-1].data.f, sp [0].data.f))
3596 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3599 MINT_IN_CASE(MINT_CGT_I4)
3602 MINT_IN_CASE(MINT_CGT_I8)
3605 MINT_IN_CASE(MINT_CGT_R8)
3607 if (isunordered (sp [-1].data.f, sp [0].data.f))
3610 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3614 #define RELOP_CAST(datamem, op, type) \
3616 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3619 MINT_IN_CASE(MINT_CGT_UN_I4)
3620 RELOP_CAST(i, >, guint32);
3622 MINT_IN_CASE(MINT_CGT_UN_I8)
3623 RELOP_CAST(l, >, guint64);
3625 MINT_IN_CASE(MINT_CGT_UN_R8)
3627 if (isunordered (sp [-1].data.f, sp [0].data.f))
3630 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3633 MINT_IN_CASE(MINT_CLT_I4)
3636 MINT_IN_CASE(MINT_CLT_I8)
3639 MINT_IN_CASE(MINT_CLT_R8)
3641 if (isunordered (sp [-1].data.f, sp [0].data.f))
3644 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3647 MINT_IN_CASE(MINT_CLT_UN_I4)
3648 RELOP_CAST(i, <, guint32);
3650 MINT_IN_CASE(MINT_CLT_UN_I8)
3651 RELOP_CAST(l, <, guint64);
3653 MINT_IN_CASE(MINT_CLT_UN_R8)
3655 if (isunordered (sp [-1].data.f, sp [0].data.f))
3658 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3661 MINT_IN_CASE(MINT_LDFTN) {
3662 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3667 MINT_IN_CASE(MINT_LDVIRTFTN) {
3668 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3672 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3674 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3679 MINT_IN_CASE(MINT_LDTHISA)
3680 g_error ("should not happen");
3681 // sp->data.p = &frame->obj;
3686 #define LDARG(datamem, argtype) \
3687 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3691 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3692 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3693 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3694 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3695 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3696 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3697 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3698 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3699 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3700 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3702 MINT_IN_CASE(MINT_LDARG_VT)
3704 i32 = READ32(ip + 2);
3705 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3706 vt_sp += (i32 + 7) & ~7;
3711 #define STARG(datamem, argtype) \
3713 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3716 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3717 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3718 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3719 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3720 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3721 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3722 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3723 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3724 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3725 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3727 MINT_IN_CASE(MINT_STARG_VT)
3728 i32 = READ32(ip + 2);
3730 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3731 vt_sp -= (i32 + 7) & ~7;
3735 #define STINARG(datamem, argtype) \
3737 int n = * (guint16 *)(ip + 1); \
3738 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3742 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3743 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3744 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3745 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3746 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3747 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3748 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3749 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3750 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3751 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3753 MINT_IN_CASE(MINT_STINARG_VT) {
3754 int n = * (guint16 *)(ip + 1);
3755 i32 = READ32(ip + 2);
3756 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3761 MINT_IN_CASE(MINT_LDARGA)
3762 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3767 #define LDLOC(datamem, argtype) \
3768 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3772 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3773 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3774 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3775 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3776 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3777 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3778 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3779 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3780 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3781 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3783 MINT_IN_CASE(MINT_LDLOC_VT)
3785 i32 = READ32(ip + 2);
3786 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3787 vt_sp += (i32 + 7) & ~7;
3792 MINT_IN_CASE(MINT_LDLOCA_S)
3793 sp->data.p = locals + * (guint16 *)(ip + 1);
3798 #define STLOC(datamem, argtype) \
3800 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3803 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3804 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3805 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3806 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3807 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3808 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3809 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3810 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3811 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3812 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3814 #define STLOC_NP(datamem, argtype) \
3815 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3818 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3819 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3821 MINT_IN_CASE(MINT_STLOC_VT)
3822 i32 = READ32(ip + 2);
3824 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3825 vt_sp -= (i32 + 7) & ~7;
3829 MINT_IN_CASE(MINT_LOCALLOC)
3830 if (sp != frame->stack + 1) /*FIX?*/
3831 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3832 sp [-1].data.p = alloca (sp [-1].data.i);
3836 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3838 MINT_IN_CASE(MINT_INITOBJ)
3840 memset (sp->data.vt, 0, READ32(ip + 1));
3843 MINT_IN_CASE(MINT_CPBLK)
3845 if (!sp [0].data.p || !sp [1].data.p)
3846 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3848 /* FIXME: value and size may be int64... */
3849 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3852 MINT_IN_CASE(MINT_CONSTRAINED_) {
3854 /* FIXME: implement */
3856 token = READ32 (ip);
3861 MINT_IN_CASE(MINT_INITBLK)
3864 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3866 /* FIXME: value and size may be int64... */
3867 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3870 MINT_IN_CASE(MINT_NO_)
3871 /* FIXME: implement */
3875 MINT_IN_CASE(MINT_RETHROW)
3877 * need to clarify what this should actually do:
3878 * start the search from the last found handler in
3879 * this method or continue in the caller or what.
3880 * Also, do we need to run finally/fault handlers after a retrow?
3881 * Well, this implementation will follow the usual search
3882 * for an handler, considering the current ip as throw spot.
3883 * We need to NULL frame->ex_handler for the later code to
3884 * actually run the new found handler.
3886 frame->ex_handler = NULL;
3887 THROW_EX (frame->ex, ip - 1);
3890 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3891 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3895 g_assert_not_reached ();
3897 * Exception handling code.
3898 * The exception object is stored in frame->ex.
3905 MonoInvocation *inv;
3906 MonoExceptionClause *clause;
3912 g_print ("* Handling exception '%s' at IL_%04x\n",
3913 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
3914 rtm == NULL ? 0 : frame->ip - rtm->code);
3916 if (die_on_exception)
3919 for (inv = frame; inv; inv = inv->parent) {
3921 if (inv->runtime_method == NULL)
3923 method = inv->runtime_method->method;
3924 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3926 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3928 if (inv->ip == NULL)
3930 ip_offset = inv->ip - inv->runtime_method->code;
3931 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
3932 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3933 clause = &inv->runtime_method->clauses [i];
3934 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3935 if (!clause->flags) {
3936 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
3937 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3940 * OK, we found an handler, now we need to execute the finally
3941 * and fault blocks before branching to the handler code.
3943 inv->ex_handler = clause;
3946 g_print ("* Found handler at '%s'\n", method->name);
3948 goto handle_finally;
3951 /* FIXME: handle filter clauses */
3958 * If we get here, no handler was found: print a stack trace.
3960 for (inv = frame; inv; inv = inv->parent) {
3961 if (inv->invoke_trap)
3962 goto handle_finally;
3965 ex_obj = (MonoObject*)frame->ex;
3966 mono_unhandled_exception (ex_obj);
3973 MonoExceptionClause *clause;
3974 GSList *old_list = finally_ips;
3975 MonoMethod *method = frame->runtime_method->method;
3976 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
3977 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3981 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
3983 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3984 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
3987 ip_offset = frame->ip - rtm->code;
3989 if (endfinally_ip != NULL)
3990 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
3991 for (i = 0; i < header->num_clauses; ++i)
3992 if (frame->ex_handler == &rtm->clauses [i])
3996 clause = &rtm->clauses [i];
3997 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
3998 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
3999 ip = rtm->code + clause->handler_offset;
4000 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4003 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4009 endfinally_ip = NULL;
4011 if (old_list != finally_ips && finally_ips) {
4012 ip = finally_ips->data;
4013 finally_ips = g_slist_remove (finally_ips, ip);
4014 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4019 * If an exception is set, we need to execute the fault handler, too,
4020 * otherwise, we continue normally.
4030 MonoExceptionClause *clause;
4031 MonoMethod *method = frame->runtime_method->method;
4032 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4033 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4037 g_print ("* Handle fault\n");
4039 ip_offset = frame->ip - rtm->code;
4040 for (i = 0; i < header->num_clauses; ++i) {
4041 clause = &rtm->clauses [i];
4042 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4043 ip = rtm->code + clause->handler_offset;
4046 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4052 * If the handler for the exception was found in this method, we jump
4053 * to it right away, otherwise we return and let the caller run
4054 * the finally, fault and catch blocks.
4055 * This same code should be present in the endfault opcode, but it
4056 * is corrently not assigned in the ECMA specs: LAMESPEC.
4058 if (frame->ex_handler) {
4061 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4063 ip = rtm->code + frame->ex_handler->handler_offset;
4065 vt_sp = (unsigned char *) sp + rtm->stack_size;
4066 sp->data.p = frame->ex;
4077 ves_exec_method (MonoInvocation *frame)
4079 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4080 ThreadContext context_struct;
4087 mono_unhandled_exception ((MonoObject*)frame->ex);
4090 if (context == NULL) {
4091 context = &context_struct;
4092 context_struct.domain = mono_domain_get ();
4093 context_struct.base_frame = frame;
4094 context_struct.current_frame = NULL;
4095 context_struct.env_frame = frame;
4096 context_struct.current_env = &env;
4097 context_struct.search_for_handler = 0;
4098 context_struct.managed_code = 0;
4099 mono_native_tls_set_value (thread_context_id, context);
4102 frame->parent = context->current_frame;
4103 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4104 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4105 context->managed_code = 1;
4106 ves_exec_method_with_context (frame, context);
4107 context->managed_code = 0;
4109 if (context != &context_struct && context->current_env) {
4110 context->env_frame->ex = frame->ex;
4111 longjmp (*context->current_env, 1);
4114 mono_unhandled_exception ((MonoObject*)frame->ex);
4116 if (context->base_frame == frame)
4117 mono_native_tls_set_value (thread_context_id, NULL);
4119 context->current_frame = frame->parent;
4123 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4125 MonoImage *image = mono_assembly_get_image (assembly);
4130 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4131 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4134 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4136 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4137 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4145 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4146 "Usage is: mint [options] executable args...\n\n", VERSION);
4148 "Runtime Debugging:\n"
4153 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4156 " --traceclassinit\n"
4159 " --debug method_name\n"
4166 " --config filename load the specified config file instead of the default\n"
4167 " --workers n maximum number of worker threads\n"
4173 interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4174 MonoReflectionMethod **method,
4175 gint32 *iloffset, gint32 *native_offset,
4176 MonoString **file, gint32 *line, gint32 *column)
4178 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4179 MonoInvocation *inv = context->current_frame;
4183 for (i = 0; inv && i < skip; inv = inv->parent)
4184 if (inv->runtime_method != NULL)
4195 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4196 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4201 if (need_file_info) {
4205 *file = mono_string_new (mono_domain_get (), "unknown");
4212 interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4214 MonoDomain *domain = mono_domain_get ();
4216 MonoArray *ta = exc->trace_ips;
4221 /* Exception is not thrown yet */
4222 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4223 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4227 len = mono_array_length (ta);
4229 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4230 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4232 for (i = skip; i < len / 2; i++) {
4233 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4234 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4235 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4236 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4239 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4240 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4241 sf->native_offset = ip - rtm->code;
4245 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4247 if (need_file_info) {
4250 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4252 sf->filename = filename? mono_string_new (domain, filename): NULL;
4259 mono_array_set (res, gpointer, i, sf);
4266 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4268 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4269 MonoObject *delegate;
4272 g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4274 delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4275 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4277 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4278 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4286 mono_native_tls_alloc (&thread_context_id, NULL);
4287 mono_native_tls_set_value (thread_context_id, NULL);
4288 mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4289 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4291 mono_interp_transform_init ();
4294 typedef int (*TestMethod) (void);
4297 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4299 int result, expected, failed, cfailed, run;
4300 double elapsed, transform_time;
4302 MonoObject *result_obj;
4303 static gboolean filter_method_init = FALSE;
4304 static const char *filter_method = NULL;
4306 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4307 cfailed = failed = run = 0;
4308 transform_time = elapsed = 0.0;
4311 /* fixme: ugly hack - delete all previously compiled methods */
4312 if (domain_jit_info (domain)) {
4313 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4314 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4315 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4316 mono_jit_code_hash_init (&(domain->jit_code_hash));
4320 g_timer_start (timer);
4321 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4323 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4325 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4329 if (!filter_method_init) {
4330 filter_method = g_getenv ("INTERP_FILTER_METHOD");
4331 filter_method_init = TRUE;
4333 gboolean filter = FALSE;
4334 if (filter_method) {
4335 const char *name = filter_method;
4337 if ((strchr (name, '.') > name) || strchr (name, ':')) {
4338 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
4339 filter = mono_method_desc_full_match (desc, method);
4340 mono_method_desc_free (desc);
4342 filter = strcmp (method->name, name) == 0;
4344 } else { // no filter
4347 if (strncmp (method->name, "test_", 5) == 0 && filter) {
4348 MonoError interp_error;
4349 MonoObject *exc = NULL;
4351 result_obj = interp_mono_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
4352 if (!mono_error_ok (&interp_error)) {
4354 g_print ("Test '%s' execution failed.\n", method->name);
4355 } else if (exc != NULL) {
4356 g_print ("Exception in Test '%s' occured:\n", method->name);
4357 mono_object_describe (exc);
4361 result = *(gint32 *) mono_object_unbox (result_obj);
4362 expected = atoi (method->name + 5); // FIXME: oh no.
4365 if (result != expected) {
4367 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4372 g_timer_stop (timer);
4373 elapsed = g_timer_elapsed (timer, NULL);
4374 if (failed > 0 || cfailed > 0){
4375 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4376 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4378 g_print ("Results: total tests: %d, all pass \n", run);
4381 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4382 elapsed - transform_time, transform_time);
4383 *total += failed + cfailed;
4387 interp_regression (MonoImage *image, int verbose, int *total_run)
4390 GTimer *timer = g_timer_new ();
4391 MonoDomain *domain = mono_domain_get ();
4395 /* load the metadata */
4396 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4398 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4400 mono_error_cleanup (&error);
4403 mono_class_init (method->klass);
4408 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4410 g_timer_destroy (timer);
4415 interp_regression_list (int verbose, int count, char *images [])
4417 int i, total, total_run, run;
4419 total_run = total = 0;
4420 for (i = 0; i < count; ++i) {
4421 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4423 g_warning ("failed to load assembly: %s", images [i]);
4426 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4430 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4432 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);