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:
748 case MONO_TYPE_VALUETYPE:
752 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
757 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
760 g_error ("build_args_from_sig: TODO, more than 6 iregs: %d\n", margs->ilen);
763 g_error ("build_args_from_sig: TODO, allocate floats: %d\n", margs->flen);
769 margs->iargs [0] = frame->stack_args->data.p;
773 for (int i = 0; i < sig->param_count; i++) {
774 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
776 case MONO_TYPE_BOOLEAN:
787 case MONO_TYPE_SZARRAY:
788 case MONO_TYPE_CLASS:
789 case MONO_TYPE_OBJECT:
790 case MONO_TYPE_STRING:
792 case MONO_TYPE_VALUETYPE:
793 margs->iargs [int_i] = frame->stack_args [i].data.p;
795 g_print ("build_args_from_sig: margs->iargs[%d]: %p (frame @ %d)\n", int_i, margs->iargs[int_i], i);
800 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
804 if (sig->ret->type != MONO_TYPE_VOID) {
805 margs->retval = &(frame->retval->data.p);
807 margs->retval = NULL;
814 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
817 MonoInvocation *old_frame = context->current_frame;
818 MonoInvocation *old_env_frame = context->env_frame;
819 jmp_buf *old_env = context->current_env;
822 context->current_frame = old_frame;
823 context->env_frame = old_env_frame;
824 context->current_env = old_env;
825 context->managed_code = 1;
830 context->env_frame = frame;
831 context->current_env = &env;
833 g_assert (!frame->runtime_method);
834 if (!mono_interp_enter_icall_trampoline) {
836 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
838 // mono_tramp_info_register (info, NULL);
841 MethodArguments *margs = build_args_from_sig (sig, frame);
843 g_print ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
844 g_print ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
847 context->current_frame = frame;
848 context->managed_code = 0;
850 mono_interp_enter_icall_trampoline (addr, margs);
852 context->managed_code = 1;
853 /* domain can only be changed by native code */
854 context->domain = mono_domain_get ();
856 if (*mono_thread_interruption_request_flag ())
857 mono_thread_interruption_checkpoint ();
859 if (!MONO_TYPE_ISSTRUCT (sig->ret))
860 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
862 context->current_frame = old_frame;
863 context->env_frame = old_env_frame;
864 context->current_env = old_env;
866 g_free (margs->iargs);
867 g_free (margs->fargs);
872 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
874 MonoDelegate *delegate = (MonoDelegate *)this;
877 delegate->method_info = mono_method_get_object_checked (domain, runtime_method->method, NULL, &error);
878 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
879 delegate->target = target;
881 if (target && mono_object_is_transparent_proxy (target)) {
882 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
883 delegate->method_ptr = mono_interp_get_runtime_method (domain, method, &error);
884 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
886 delegate->method_ptr = runtime_method;
891 mono_interp_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
895 MonoDomain *domain = mono_domain_get ();
898 d = (MonoDelegate*)mono_object_new_checked (domain, klass, &error);
899 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
901 ji = mono_jit_info_table_find (domain, ftn);
903 mono_raise_exception (mono_get_exception_argument ("", "Function pointer was not created by a Delegate."));
905 /* FIXME: discard the wrapper and call the original method */
906 interp_delegate_ctor (domain, (MonoObject*)d, NULL, mono_interp_get_runtime_method (domain, ji->d.method, &error));
907 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
914 * runtime specifies that the implementation of the method is automatically
915 * provided by the runtime and is primarily used for the methods of delegates.
918 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
920 MonoMethod *method = frame->runtime_method->method;
921 const char *name = method->name;
922 MonoObject *obj = (MonoObject*) frame->stack_args->data.p;
923 MonoObject *isinst_obj;
926 mono_class_init (method->klass);
928 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.multicastdelegate_class, &error);
929 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
930 if (obj && isinst_obj) {
931 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
932 interp_delegate_ctor (context->domain, obj, frame->stack_args [1].data.p, frame->stack_args[2].data.p);
937 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
938 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
939 if (obj && isinst_obj) {
940 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
941 ves_array_set (frame);
944 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
945 ves_array_get (frame);
948 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
949 ves_array_element_address (frame);
954 g_error ("Don't know how to exec runtime method %s.%s::%s",
955 method->klass->name_space, method->klass->name,
960 dump_stack (stackval *stack, stackval *sp)
963 GString *str = g_string_new ("");
966 return g_string_free (str, FALSE);
969 g_string_append_printf (str, "[%p (%lld)] ", s->data.l, s->data.l);
972 return g_string_free (str, FALSE);
976 dump_stackval (GString *str, stackval *s, MonoType *type)
978 switch (type->type) {
986 case MONO_TYPE_BOOLEAN:
987 g_string_append_printf (str, "[%d] ", s->data.i);
989 case MONO_TYPE_STRING:
990 case MONO_TYPE_SZARRAY:
991 case MONO_TYPE_CLASS:
992 case MONO_TYPE_OBJECT:
993 case MONO_TYPE_ARRAY:
997 g_string_append_printf (str, "[%p] ", s->data.p);
999 case MONO_TYPE_VALUETYPE:
1000 if (type->data.klass->enumtype)
1001 g_string_append_printf (str, "[%d] ", s->data.i);
1003 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1007 g_string_append_printf (str, "[%g] ", s->data.f);
1012 GString *res = g_string_new ("");
1013 mono_type_get_desc (res, type, TRUE);
1014 g_string_append_printf (str, "[{%s} %lld/0x%0llx] ", res->str, s->data.l, s->data.l);
1015 g_string_free (res, TRUE);
1022 dump_args (MonoInvocation *inv)
1024 GString *str = g_string_new ("");
1026 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1028 if (signature->param_count == 0 && !signature->hasthis)
1029 return g_string_free (str, FALSE);
1031 if (signature->hasthis) {
1032 MonoMethod *method = inv->runtime_method->method;
1033 dump_stackval (str, inv->stack_args, &method->klass->byval_arg);
1036 for (i = 0; i < signature->param_count; ++i)
1037 dump_stackval (str, inv->stack_args + (!!signature->hasthis) + i, signature->params [i]);
1039 return g_string_free (str, FALSE);
1043 dump_retval (MonoInvocation *inv)
1045 GString *str = g_string_new ("");
1046 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1048 if (ret->type != MONO_TYPE_VOID)
1049 dump_stackval (str, inv->retval, ret);
1051 return g_string_free (str, FALSE);
1055 dump_frame (MonoInvocation *inv)
1057 GString *str = g_string_new ("");
1062 for (i = 0; inv; inv = inv->parent) {
1063 if (inv->runtime_method != NULL) {
1064 MonoMethod *method = inv->runtime_method->method;
1068 const char * opname = "";
1070 gchar *source = NULL;
1074 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1075 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1076 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1077 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1081 opname = mono_interp_opname [*inv->ip];
1082 codep = inv->ip - inv->runtime_method->code;
1083 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1088 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1089 source = mono_debug_method_lookup_location (minfo, codep);
1093 args = dump_args (inv);
1094 name = mono_method_full_name (method, TRUE);
1096 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1098 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1105 return g_string_free (str, FALSE);
1109 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1113 MonoInvocation *inv;
1116 for (i = 0, inv = top; inv; inv = inv->parent)
1117 if (inv->runtime_method != NULL)
1120 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1121 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1123 for (i = 0, inv = top; inv; inv = inv->parent)
1124 if (inv->runtime_method != NULL) {
1125 mono_array_set (res, gpointer, i, inv->runtime_method);
1127 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1135 #define MYGUINT64_MAX 18446744073709551615ULL
1136 #define MYGINT64_MAX 9223372036854775807LL
1137 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1139 #define MYGUINT32_MAX 4294967295U
1140 #define MYGINT32_MAX 2147483647
1141 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1143 #define CHECK_ADD_OVERFLOW(a,b) \
1144 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1145 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1147 #define CHECK_SUB_OVERFLOW(a,b) \
1148 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1149 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1151 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1152 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1154 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1155 (guint32)(a) < (guint32)(b) ? -1 : 0
1157 #define CHECK_ADD_OVERFLOW64(a,b) \
1158 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1159 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1161 #define CHECK_SUB_OVERFLOW64(a,b) \
1162 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1163 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1165 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1166 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1168 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1169 (guint64)(a) < (guint64)(b) ? -1 : 0
1171 #if SIZEOF_VOID_P == 4
1172 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1173 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1175 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1176 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1179 /* Resolves to TRUE if the operands would overflow */
1180 #define CHECK_MUL_OVERFLOW(a,b) \
1181 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1182 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1183 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1184 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1185 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1186 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1187 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1189 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1190 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1191 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1193 #define CHECK_MUL_OVERFLOW64(a,b) \
1194 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1195 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1196 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1197 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1198 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1199 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1200 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1202 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1203 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1204 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1206 #if SIZEOF_VOID_P == 4
1207 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1208 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1210 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1211 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1215 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1217 MonoInvocation frame;
1218 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1219 MonoObject *retval = NULL;
1220 MonoMethodSignature *sig = mono_method_signature (method);
1221 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1222 int i, type, isobject = 0;
1225 stackval *args = alloca (sizeof (stackval) * (sig->param_count + !!sig->hasthis));
1226 ThreadContext context_struct;
1227 MonoInvocation *old_frame = NULL;
1230 mono_error_init (error);
1235 if (context != &context_struct) {
1236 context->domain = mono_domain_get ();
1237 context->current_frame = old_frame;
1238 context->managed_code = 0;
1240 mono_native_tls_set_value (thread_context_id, NULL);
1242 *exc = (MonoObject *)frame.ex;
1246 if (context == NULL) {
1247 context = &context_struct;
1248 context_struct.base_frame = &frame;
1249 context_struct.current_frame = NULL;
1250 context_struct.env_frame = &frame;
1251 context_struct.current_env = &env;
1252 context_struct.search_for_handler = 0;
1253 context_struct.managed_code = 0;
1254 mono_native_tls_set_value (thread_context_id, context);
1257 old_frame = context->current_frame;
1259 context->domain = mono_domain_get ();
1261 switch (sig->ret->type) {
1262 case MONO_TYPE_VOID:
1264 case MONO_TYPE_STRING:
1265 case MONO_TYPE_OBJECT:
1266 case MONO_TYPE_CLASS:
1267 case MONO_TYPE_ARRAY:
1268 case MONO_TYPE_SZARRAY:
1271 case MONO_TYPE_VALUETYPE:
1272 retval = mono_object_new_checked (context->domain, klass, error);
1273 ret = ((char*)retval) + sizeof (MonoObject);
1274 if (!sig->ret->data.klass->enumtype)
1275 result.data.vt = ret;
1278 retval = mono_object_new_checked (context->domain, klass, error);
1279 ret = ((char*)retval) + sizeof (MonoObject);
1284 args [0].data.p = obj;
1286 for (i = 0; i < sig->param_count; ++i) {
1287 int a_index = i + !!sig->hasthis;
1288 if (sig->params [i]->byref) {
1289 args [a_index].data.p = params [i];
1292 type = sig->params [i]->type;
1297 case MONO_TYPE_BOOLEAN:
1298 args [a_index].data.i = *(MonoBoolean*)params [i];
1302 case MONO_TYPE_CHAR:
1303 args [a_index].data.i = *(gint16*)params [i];
1305 #if SIZEOF_VOID_P == 4
1306 case MONO_TYPE_U: /* use VAL_POINTER? */
1311 args [a_index].data.i = *(gint32*)params [i];
1313 #if SIZEOF_VOID_P == 8
1319 args [a_index].data.l = *(gint64*)params [i];
1321 case MONO_TYPE_VALUETYPE:
1322 if (sig->params [i]->data.klass->enumtype) {
1323 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1326 args [a_index].data.p = params [i];
1329 case MONO_TYPE_STRING:
1330 case MONO_TYPE_CLASS:
1331 case MONO_TYPE_ARRAY:
1332 case MONO_TYPE_SZARRAY:
1333 case MONO_TYPE_OBJECT:
1334 args [a_index].data.p = params [i];
1337 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1341 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1342 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1343 INIT_FRAME (&frame,context->current_frame,args,&result,mono_get_root_domain (),method,error);
1345 frame.invoke_trap = 1;
1346 context->managed_code = 1;
1347 ves_exec_method_with_context (&frame, context);
1348 context->managed_code = 0;
1349 if (context == &context_struct)
1350 mono_native_tls_set_value (thread_context_id, NULL);
1352 context->current_frame = old_frame;
1353 if (frame.ex != NULL) {
1355 *exc = (MonoObject*) frame.ex;
1358 if (context->current_env != NULL) {
1359 context->env_frame->ex = frame.ex;
1360 longjmp(*context->current_env, 1);
1363 printf("dropped exception...\n");
1365 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1367 if (isobject || method->string_ctor)
1368 return result.data.p;
1369 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1374 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1376 MonoInvocation *old_frame = context->current_frame;
1377 MonoInvocation *old_env_frame = context->env_frame;
1378 jmp_buf *old_env = context->current_env;
1382 context->current_frame = old_frame;
1383 context->env_frame = old_env_frame;
1384 context->current_env = old_env;
1385 context->managed_code = 1;
1389 context->env_frame = context->current_frame;
1390 context->current_env = &env;
1391 context->managed_code = 0;
1394 case MINT_ICALL_V_V: {
1395 void (*func)() = ptr;
1399 case MINT_ICALL_V_P: {
1400 gpointer (*func)() = ptr;
1402 sp [-1].data.p = func ();
1405 case MINT_ICALL_P_V: {
1406 void (*func)(gpointer) = ptr;
1407 func (sp [-1].data.p);
1411 case MINT_ICALL_P_P: {
1412 gpointer (*func)(gpointer) = ptr;
1413 sp [-1].data.p = func (sp [-1].data.p);
1416 case MINT_ICALL_PP_V: {
1417 void (*func)(gpointer,gpointer) = ptr;
1419 func (sp [0].data.p, sp [1].data.p);
1422 case MINT_ICALL_PI_V: {
1423 void (*func)(gpointer,int) = ptr;
1425 func (sp [0].data.p, sp [1].data.i);
1428 case MINT_ICALL_PP_P: {
1429 gpointer (*func)(gpointer,gpointer) = ptr;
1431 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1434 case MINT_ICALL_PI_P: {
1435 gpointer (*func)(gpointer,int) = ptr;
1437 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1440 case MINT_ICALL_PPP_V: {
1441 void (*func)(gpointer,gpointer,gpointer) = ptr;
1443 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1446 case MINT_ICALL_PPI_V: {
1447 void (*func)(gpointer,gpointer,int) = ptr;
1449 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1453 g_assert_not_reached ();
1456 context->env_frame = old_env_frame;
1457 context->current_env = old_env;
1462 static mono_mutex_t create_method_pointer_mutex;
1464 static GHashTable *method_pointer_hash = NULL;
1467 interp_create_method_pointer (MonoMethod *method, MonoError *error)
1472 mono_os_mutex_lock (&create_method_pointer_mutex);
1473 if (!method_pointer_hash) {
1474 // FIXME: is registering method table as GC root really necessary?
1475 // MONO_GC_REGISTER_ROOT_FIXED (method_pointer_hash);
1476 method_pointer_hash = g_hash_table_new (NULL, NULL);
1478 addr = g_hash_table_lookup (method_pointer_hash, method);
1480 mono_os_mutex_unlock (&create_method_pointer_mutex);
1485 * If it is a static P/Invoke method, we can just return the pointer
1486 * to the method implementation.
1488 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && ((MonoMethodPInvoke*) method)->addr) {
1489 ji = g_new0 (MonoJitInfo, 1);
1490 ji->d.method = method;
1492 ji->code_start = addr = ((MonoMethodPInvoke*) method)->addr;
1494 mono_jit_info_table_add (mono_get_root_domain (), ji);
1497 g_error ("FIXME: not available? figure out new API");
1499 addr = mono_arch_create_method_pointer (method);
1503 g_hash_table_insert (method_pointer_hash, method, addr);
1504 mono_os_mutex_unlock (&create_method_pointer_mutex);
1510 static int opcode_counts[512];
1512 #define COUNT_OP(op) opcode_counts[op]++
1514 #define COUNT_OP(op)
1518 #define DUMP_INSTR() \
1519 if (tracing > 1) { \
1521 if (sp > frame->stack) { \
1522 ins = dump_stack (frame->stack, sp); \
1524 ins = g_strdup (""); \
1528 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
1529 g_print ("(%p) %s -> ", mono_thread_internal_current (), mn); \
1531 mono_interp_dis_mintop(rtm->code, ip); \
1532 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1536 #define DUMP_INSTR()
1540 #define USE_COMPUTED_GOTO 1
1542 #if USE_COMPUTED_GOTO
1543 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1544 #define MINT_IN_CASE(x) LAB_ ## x:
1546 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1548 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1550 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
1552 #define MINT_IN_SWITCH(op) switch (op)
1553 #define MINT_IN_CASE(x) case x:
1554 #define MINT_IN_BREAK break
1555 #define MINT_IN_DEFAULT default:
1559 * Defining this causes register allocation errors in some versions of gcc:
1560 * error: unable to find a register to spill in class `SIREG'
1562 /* #define MINT_USE_DEDICATED_IP_REG */
1565 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1567 MonoInvocation child_frame;
1568 GSList *finally_ips = NULL;
1569 const unsigned short *endfinally_ip = NULL;
1570 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1571 register const unsigned short *ip asm ("%esi");
1573 register const unsigned short *ip;
1575 register stackval *sp;
1578 gint tracing = global_tracing;
1579 unsigned char *vtalloc;
1582 unsigned char *vt_sp;
1583 unsigned char *locals;
1585 MonoObject *o = NULL;
1587 #if USE_COMPUTED_GOTO
1588 static void *in_labels[] = {
1589 #define OPDEF(a,b,c,d) \
1591 #include "mintops.def"
1596 frame->ex_handler = NULL;
1598 context->current_frame = frame;
1601 debug_enter (frame, &tracing);
1604 if (!frame->runtime_method->transformed) {
1605 context->managed_code = 0;
1607 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE);
1608 g_print ("(%p) Transforming %s\n", mono_thread_internal_current (), mn);
1611 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1612 context->managed_code = 1;
1620 rtm = frame->runtime_method;
1621 frame->args = alloca (rtm->alloca_size);
1622 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1625 memset(sp, 0, rtm->stack_size);
1627 vt_sp = (unsigned char *) sp + rtm->stack_size;
1631 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1633 child_frame.parent = frame;
1639 * using while (ip < end) may result in a 15% performance drop,
1640 * but it may be useful for debug
1644 /* g_assert (sp >= frame->stack); */
1645 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1647 MINT_IN_SWITCH (*ip) {
1648 MINT_IN_CASE(MINT_INITLOCALS)
1649 memset (locals, 0, rtm->locals_size);
1652 MINT_IN_CASE(MINT_NOP)
1655 MINT_IN_CASE(MINT_BREAK)
1657 G_BREAKPOINT (); /* this is not portable... */
1659 MINT_IN_CASE(MINT_LDNULL)
1664 MINT_IN_CASE(MINT_VTRESULT) {
1665 int ret_size = * (guint16 *)(ip + 1);
1666 unsigned char *ret_vt_sp = vt_sp;
1667 vt_sp -= READ32(ip + 2);
1669 memmove (vt_sp, ret_vt_sp, ret_size);
1670 sp [-1].data.p = vt_sp;
1671 vt_sp += (ret_size + 7) & ~7;
1676 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1677 MINT_IN_CASE(MINT_LDC_I4_M1)
1680 MINT_IN_CASE(MINT_LDC_I4_0)
1683 MINT_IN_CASE(MINT_LDC_I4_1)
1686 MINT_IN_CASE(MINT_LDC_I4_2)
1689 MINT_IN_CASE(MINT_LDC_I4_3)
1692 MINT_IN_CASE(MINT_LDC_I4_4)
1695 MINT_IN_CASE(MINT_LDC_I4_5)
1698 MINT_IN_CASE(MINT_LDC_I4_6)
1701 MINT_IN_CASE(MINT_LDC_I4_7)
1704 MINT_IN_CASE(MINT_LDC_I4_8)
1707 MINT_IN_CASE(MINT_LDC_I4_S)
1708 sp->data.i = *(const short *)(ip + 1);
1712 MINT_IN_CASE(MINT_LDC_I4)
1714 sp->data.i = READ32 (ip);
1718 MINT_IN_CASE(MINT_LDC_I8)
1720 sp->data.l = READ64 (ip);
1724 MINT_IN_CASE(MINT_LDC_R4) {
1728 sp->data.f = * (float *)&val;
1733 MINT_IN_CASE(MINT_LDC_R8)
1734 sp->data.l = READ64 (ip + 1); /* note union usage */
1738 MINT_IN_CASE(MINT_DUP)
1743 MINT_IN_CASE(MINT_DUP_VT)
1744 i32 = READ32 (ip + 1);
1746 memcpy(sp->data.p, sp [-1].data.p, i32);
1747 vt_sp += (i32 + 7) & ~7;
1751 MINT_IN_CASE(MINT_POP)
1755 MINT_IN_CASE(MINT_JMP) {
1756 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1757 if (!new_method->transformed) {
1759 frame->ex = mono_interp_transform_method (new_method, context);
1764 if (new_method->alloca_size > rtm->alloca_size)
1765 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1766 rtm = frame->runtime_method = new_method;
1767 vt_sp = (unsigned char *) sp + rtm->stack_size;
1771 locals = vt_sp + rtm->vt_stack_size;
1772 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1775 MINT_IN_CASE(MINT_CALLI) {
1776 MonoMethodSignature *csignature;
1777 stackval *endsp = sp;
1781 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1785 child_frame.runtime_method = sp->data.p;
1788 child_frame.retval = sp;
1789 /* decrement by the actual number of args */
1790 sp -= csignature->param_count;
1791 if (csignature->hasthis)
1793 child_frame.stack_args = sp;
1794 if (csignature->hasthis && mono_object_is_transparent_proxy (sp->data.p)) {
1795 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1796 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1797 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1798 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1799 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1802 ves_exec_method_with_context (&child_frame, context);
1804 context->current_frame = frame;
1806 if (child_frame.ex) {
1808 * An exception occurred, need to run finally, fault and catch handlers..
1810 frame->ex = child_frame.ex;
1811 goto handle_finally;
1814 /* need to handle typedbyref ... */
1815 if (csignature->ret->type != MONO_TYPE_VOID) {
1821 MINT_IN_CASE(MINT_CALLI_NAT) {
1822 MonoMethodSignature *csignature;
1823 stackval *endsp = sp;
1824 unsigned char *code = NULL;
1828 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1833 child_frame.runtime_method = NULL;
1836 child_frame.retval = sp;
1837 /* decrement by the actual number of args */
1838 sp -= csignature->param_count;
1839 if (csignature->hasthis)
1841 child_frame.stack_args = sp;
1842 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1844 context->current_frame = frame;
1846 if (child_frame.ex) {
1848 * An exception occurred, need to run finally, fault and catch handlers..
1850 frame->ex = child_frame.ex;
1851 if (context->search_for_handler) {
1852 context->search_for_handler = 0;
1853 goto handle_exception;
1855 goto handle_finally;
1858 /* need to handle typedbyref ... */
1859 if (csignature->ret->type != MONO_TYPE_VOID) {
1865 MINT_IN_CASE(MINT_CALL) {
1866 stackval *endsp = sp;
1870 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1873 child_frame.retval = sp;
1874 /* decrement by the actual number of args */
1875 sp -= child_frame.runtime_method->param_count;
1876 if (child_frame.runtime_method->hasthis)
1878 child_frame.stack_args = sp;
1879 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
1880 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1881 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1883 ves_exec_method_with_context (&child_frame, context);
1885 context->current_frame = frame;
1887 if (child_frame.ex) {
1889 * An exception occurred, need to run finally, fault and catch handlers..
1891 frame->ex = child_frame.ex;
1892 goto handle_finally;
1895 /* need to handle typedbyref ... */
1900 MINT_IN_CASE(MINT_VCALL) {
1903 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1907 child_frame.retval = sp;
1908 /* decrement by the actual number of args */
1909 sp -= child_frame.runtime_method->param_count;
1910 if (child_frame.runtime_method->hasthis)
1912 child_frame.stack_args = sp;
1914 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (sp->data.p)) {
1915 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1916 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1919 ves_exec_method_with_context (&child_frame, context);
1921 context->current_frame = frame;
1923 if (child_frame.ex) {
1925 * An exception occurred, need to run finally, fault and catch handlers..
1927 frame->ex = child_frame.ex;
1928 goto handle_finally;
1932 MINT_IN_CASE(MINT_CALLVIRT) {
1933 stackval *endsp = sp;
1934 MonoObject *this_arg;
1939 token = * (unsigned short *)(ip + 1);
1941 child_frame.runtime_method = rtm->data_items [token];
1943 child_frame.retval = sp;
1945 /* decrement by the actual number of args */
1946 sp -= child_frame.runtime_method->param_count + 1;
1947 child_frame.stack_args = sp;
1948 this_arg = sp->data.p;
1950 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1951 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1953 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1954 sp->data.p = (char *) this_arg + sizeof (MonoObject);
1957 ves_exec_method_with_context (&child_frame, context);
1959 context->current_frame = frame;
1961 if (child_frame.ex) {
1963 * An exception occurred, need to run finally, fault and catch handlers..
1965 frame->ex = child_frame.ex;
1966 if (context->search_for_handler) {
1967 context->search_for_handler = 0;
1968 goto handle_exception;
1970 goto handle_finally;
1973 /* need to handle typedbyref ... */
1978 MINT_IN_CASE(MINT_VCALLVIRT) {
1979 MonoObject *this_arg;
1984 token = * (unsigned short *)(ip + 1);
1986 child_frame.runtime_method = rtm->data_items [token];
1988 child_frame.retval = sp;
1990 /* decrement by the actual number of args */
1991 sp -= child_frame.runtime_method->param_count + 1;
1992 child_frame.stack_args = sp;
1993 this_arg = sp->data.p;
1995 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1996 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1998 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1999 sp->data.p = (char *) this_arg + sizeof (MonoObject);
2002 ves_exec_method_with_context (&child_frame, context);
2004 context->current_frame = frame;
2006 if (child_frame.ex) {
2008 * An exception occurred, need to run finally, fault and catch handlers..
2010 frame->ex = child_frame.ex;
2011 if (context->search_for_handler) {
2012 context->search_for_handler = 0;
2013 goto handle_exception;
2015 goto handle_finally;
2019 MINT_IN_CASE(MINT_CALLRUN)
2020 ves_runtime_method (frame, context);
2023 goto handle_exception;
2026 MINT_IN_CASE(MINT_RET)
2028 *frame->retval = *sp;
2029 if (sp > frame->stack)
2030 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2032 MINT_IN_CASE(MINT_RET_VOID)
2033 if (sp > frame->stack)
2034 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
2036 MINT_IN_CASE(MINT_RET_VT)
2037 i32 = READ32(ip + 1);
2039 memcpy(frame->retval->data.p, sp->data.p, i32);
2040 if (sp > frame->stack)
2041 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2043 MINT_IN_CASE(MINT_BR_S)
2044 ip += (short) *(ip + 1);
2046 MINT_IN_CASE(MINT_BR)
2047 ip += (gint32) READ32(ip + 1);
2049 #define ZEROP_S(datamem, op) \
2051 if (sp->data.datamem op 0) \
2052 ip += * (gint16 *)(ip + 1); \
2056 #define ZEROP(datamem, op) \
2058 if (sp->data.datamem op 0) \
2059 ip += READ32(ip + 1); \
2063 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2066 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2069 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2072 MINT_IN_CASE(MINT_BRFALSE_I4)
2075 MINT_IN_CASE(MINT_BRFALSE_I8)
2078 MINT_IN_CASE(MINT_BRFALSE_R8)
2081 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2084 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2087 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2090 MINT_IN_CASE(MINT_BRTRUE_I4)
2093 MINT_IN_CASE(MINT_BRTRUE_I8)
2096 MINT_IN_CASE(MINT_BRTRUE_R8)
2099 #define CONDBR_S(cond) \
2102 ip += * (gint16 *)(ip + 1); \
2105 #define BRELOP_S(datamem, op) \
2106 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2108 #define CONDBR(cond) \
2111 ip += READ32(ip + 1); \
2115 #define BRELOP(datamem, op) \
2116 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2118 MINT_IN_CASE(MINT_BEQ_I4_S)
2121 MINT_IN_CASE(MINT_BEQ_I8_S)
2124 MINT_IN_CASE(MINT_BEQ_R8_S)
2125 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2127 MINT_IN_CASE(MINT_BEQ_I4)
2130 MINT_IN_CASE(MINT_BEQ_I8)
2133 MINT_IN_CASE(MINT_BEQ_R8)
2134 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2136 MINT_IN_CASE(MINT_BGE_I4_S)
2139 MINT_IN_CASE(MINT_BGE_I8_S)
2142 MINT_IN_CASE(MINT_BGE_R8_S)
2143 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2145 MINT_IN_CASE(MINT_BGE_I4)
2148 MINT_IN_CASE(MINT_BGE_I8)
2151 MINT_IN_CASE(MINT_BGE_R8)
2152 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2154 MINT_IN_CASE(MINT_BGT_I4_S)
2157 MINT_IN_CASE(MINT_BGT_I8_S)
2160 MINT_IN_CASE(MINT_BGT_R8_S)
2161 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2163 MINT_IN_CASE(MINT_BGT_I4)
2166 MINT_IN_CASE(MINT_BGT_I8)
2169 MINT_IN_CASE(MINT_BGT_R8)
2170 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2172 MINT_IN_CASE(MINT_BLT_I4_S)
2175 MINT_IN_CASE(MINT_BLT_I8_S)
2178 MINT_IN_CASE(MINT_BLT_R8_S)
2179 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2181 MINT_IN_CASE(MINT_BLT_I4)
2184 MINT_IN_CASE(MINT_BLT_I8)
2187 MINT_IN_CASE(MINT_BLT_R8)
2188 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2190 MINT_IN_CASE(MINT_BLE_I4_S)
2193 MINT_IN_CASE(MINT_BLE_I8_S)
2196 MINT_IN_CASE(MINT_BLE_R8_S)
2197 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2199 MINT_IN_CASE(MINT_BLE_I4)
2202 MINT_IN_CASE(MINT_BLE_I8)
2205 MINT_IN_CASE(MINT_BLE_R8)
2206 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2208 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2211 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2214 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2215 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2217 MINT_IN_CASE(MINT_BNE_UN_I4)
2220 MINT_IN_CASE(MINT_BNE_UN_I8)
2223 MINT_IN_CASE(MINT_BNE_UN_R8)
2224 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2227 #define BRELOP_S_CAST(datamem, op, type) \
2229 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2230 ip += * (gint16 *)(ip + 1); \
2234 #define BRELOP_CAST(datamem, op, type) \
2236 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2237 ip += READ32(ip + 1); \
2241 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2242 BRELOP_S_CAST(i, >=, guint32);
2244 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2245 BRELOP_S_CAST(l, >=, guint64);
2247 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2248 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2250 MINT_IN_CASE(MINT_BGE_UN_I4)
2251 BRELOP_CAST(i, >=, guint32);
2253 MINT_IN_CASE(MINT_BGE_UN_I8)
2254 BRELOP_CAST(l, >=, guint64);
2256 MINT_IN_CASE(MINT_BGE_UN_R8)
2257 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2259 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2260 BRELOP_S_CAST(i, >, guint32);
2262 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2263 BRELOP_S_CAST(l, >, guint64);
2265 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2266 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2268 MINT_IN_CASE(MINT_BGT_UN_I4)
2269 BRELOP_CAST(i, >, guint32);
2271 MINT_IN_CASE(MINT_BGT_UN_I8)
2272 BRELOP_CAST(l, >, guint64);
2274 MINT_IN_CASE(MINT_BGT_UN_R8)
2275 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2277 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2278 BRELOP_S_CAST(i, <=, guint32);
2280 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2281 BRELOP_S_CAST(l, <=, guint64);
2283 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2284 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2286 MINT_IN_CASE(MINT_BLE_UN_I4)
2287 BRELOP_CAST(i, <=, guint32);
2289 MINT_IN_CASE(MINT_BLE_UN_I8)
2290 BRELOP_CAST(l, <=, guint64);
2292 MINT_IN_CASE(MINT_BLE_UN_R8)
2293 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2295 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2296 BRELOP_S_CAST(i, <, guint32);
2298 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2299 BRELOP_S_CAST(l, <, guint64);
2301 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2302 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2304 MINT_IN_CASE(MINT_BLT_UN_I4)
2305 BRELOP_CAST(i, <, guint32);
2307 MINT_IN_CASE(MINT_BLT_UN_I8)
2308 BRELOP_CAST(l, <, guint64);
2310 MINT_IN_CASE(MINT_BLT_UN_R8)
2311 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2313 MINT_IN_CASE(MINT_SWITCH) {
2315 const unsigned short *st;
2321 if ((guint32)sp->data.i < n) {
2323 ip += 2 * (guint32)sp->data.i;
2324 offset = READ32 (ip);
2331 MINT_IN_CASE(MINT_LDIND_I1)
2333 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2335 MINT_IN_CASE(MINT_LDIND_U1)
2337 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2339 MINT_IN_CASE(MINT_LDIND_I2)
2341 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2343 MINT_IN_CASE(MINT_LDIND_U2)
2345 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2347 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2348 MINT_IN_CASE(MINT_LDIND_U4)
2350 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2352 MINT_IN_CASE(MINT_LDIND_I8)
2354 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2356 MINT_IN_CASE(MINT_LDIND_I) {
2357 guint16 offset = * (guint16 *)(ip + 1);
2358 sp[-1 - offset].data.p = *(gpointer*)sp[-1 - offset].data.p;
2362 MINT_IN_CASE(MINT_LDIND_R4)
2364 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2366 MINT_IN_CASE(MINT_LDIND_R8)
2368 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2370 MINT_IN_CASE(MINT_LDIND_REF)
2372 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2374 MINT_IN_CASE(MINT_STIND_REF)
2377 * (gpointer *) sp->data.p = sp[1].data.p;
2379 MINT_IN_CASE(MINT_STIND_I1)
2382 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2384 MINT_IN_CASE(MINT_STIND_I2)
2387 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2389 MINT_IN_CASE(MINT_STIND_I4)
2392 * (gint32 *) sp->data.p = sp[1].data.i;
2394 MINT_IN_CASE(MINT_STIND_I)
2397 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2399 MINT_IN_CASE(MINT_STIND_I8)
2402 * (gint64 *) sp->data.p = sp[1].data.l;
2404 MINT_IN_CASE(MINT_STIND_R4)
2407 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2409 MINT_IN_CASE(MINT_STIND_R8)
2412 * (double *) sp->data.p = sp[1].data.f;
2414 #define BINOP(datamem, op) \
2416 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2418 MINT_IN_CASE(MINT_ADD_I4)
2421 MINT_IN_CASE(MINT_ADD_I8)
2424 MINT_IN_CASE(MINT_ADD_R8)
2427 MINT_IN_CASE(MINT_ADD1_I4)
2431 MINT_IN_CASE(MINT_SUB_I4)
2434 MINT_IN_CASE(MINT_SUB_I8)
2437 MINT_IN_CASE(MINT_SUB_R8)
2440 MINT_IN_CASE(MINT_SUB1_I4)
2444 MINT_IN_CASE(MINT_MUL_I4)
2447 MINT_IN_CASE(MINT_MUL_I8)
2450 MINT_IN_CASE(MINT_MUL_R8)
2453 MINT_IN_CASE(MINT_DIV_I4)
2454 if (sp [-1].data.i == 0)
2455 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2456 if (sp [-1].data.i == (-1))
2457 THROW_EX (mono_get_exception_overflow (), ip);
2460 MINT_IN_CASE(MINT_DIV_I8)
2461 if (sp [-1].data.l == 0)
2462 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2463 if (sp [-1].data.l == (-1))
2464 THROW_EX (mono_get_exception_overflow (), ip);
2467 MINT_IN_CASE(MINT_DIV_R8)
2471 #define BINOP_CAST(datamem, op, type) \
2473 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2475 MINT_IN_CASE(MINT_DIV_UN_I4)
2476 if (sp [-1].data.i == 0)
2477 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2478 BINOP_CAST(i, /, guint32);
2480 MINT_IN_CASE(MINT_DIV_UN_I8)
2481 if (sp [-1].data.l == 0)
2482 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2483 BINOP_CAST(l, /, guint64);
2485 MINT_IN_CASE(MINT_REM_I4)
2486 if (sp [-1].data.i == 0)
2487 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2490 MINT_IN_CASE(MINT_REM_I8)
2491 if (sp [-1].data.l == 0)
2492 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2495 MINT_IN_CASE(MINT_REM_R8)
2496 /* FIXME: what do we actually do here? */
2498 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2501 MINT_IN_CASE(MINT_REM_UN_I4)
2502 if (sp [-1].data.i == 0)
2503 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2504 BINOP_CAST(i, %, guint32);
2506 MINT_IN_CASE(MINT_REM_UN_I8)
2507 if (sp [-1].data.l == 0)
2508 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2509 BINOP_CAST(l, %, guint64);
2511 MINT_IN_CASE(MINT_AND_I4)
2514 MINT_IN_CASE(MINT_AND_I8)
2517 MINT_IN_CASE(MINT_OR_I4)
2520 MINT_IN_CASE(MINT_OR_I8)
2523 MINT_IN_CASE(MINT_XOR_I4)
2526 MINT_IN_CASE(MINT_XOR_I8)
2530 #define SHIFTOP(datamem, op) \
2532 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2535 MINT_IN_CASE(MINT_SHL_I4)
2538 MINT_IN_CASE(MINT_SHL_I8)
2541 MINT_IN_CASE(MINT_SHR_I4)
2544 MINT_IN_CASE(MINT_SHR_I8)
2547 MINT_IN_CASE(MINT_SHR_UN_I4)
2549 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2552 MINT_IN_CASE(MINT_SHR_UN_I8)
2554 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2557 MINT_IN_CASE(MINT_NEG_I4)
2558 sp [-1].data.i = - sp [-1].data.i;
2561 MINT_IN_CASE(MINT_NEG_I8)
2562 sp [-1].data.l = - sp [-1].data.l;
2565 MINT_IN_CASE(MINT_NEG_R8)
2566 sp [-1].data.f = - sp [-1].data.f;
2569 MINT_IN_CASE(MINT_NOT_I4)
2570 sp [-1].data.i = ~ sp [-1].data.i;
2573 MINT_IN_CASE(MINT_NOT_I8)
2574 sp [-1].data.l = ~ sp [-1].data.l;
2577 MINT_IN_CASE(MINT_CONV_I1_I4)
2578 sp [-1].data.i = (gint8)sp [-1].data.i;
2581 MINT_IN_CASE(MINT_CONV_I1_I8)
2582 sp [-1].data.i = (gint8)sp [-1].data.l;
2585 MINT_IN_CASE(MINT_CONV_I1_R8)
2586 sp [-1].data.i = (gint8)sp [-1].data.f;
2589 MINT_IN_CASE(MINT_CONV_U1_I4)
2590 sp [-1].data.i = (guint8)sp [-1].data.i;
2593 MINT_IN_CASE(MINT_CONV_U1_I8)
2594 sp [-1].data.i = (guint8)sp [-1].data.l;
2597 MINT_IN_CASE(MINT_CONV_U1_R8)
2598 sp [-1].data.i = (guint8)sp [-1].data.f;
2601 MINT_IN_CASE(MINT_CONV_I2_I4)
2602 sp [-1].data.i = (gint16)sp [-1].data.i;
2605 MINT_IN_CASE(MINT_CONV_I2_I8)
2606 sp [-1].data.i = (gint16)sp [-1].data.l;
2609 MINT_IN_CASE(MINT_CONV_I2_R8)
2610 sp [-1].data.i = (gint16)sp [-1].data.f;
2613 MINT_IN_CASE(MINT_CONV_U2_I4)
2614 sp [-1].data.i = (guint16)sp [-1].data.i;
2617 MINT_IN_CASE(MINT_CONV_U2_I8)
2618 sp [-1].data.i = (guint16)sp [-1].data.l;
2621 MINT_IN_CASE(MINT_CONV_U2_R8)
2622 sp [-1].data.i = (guint16)sp [-1].data.f;
2625 MINT_IN_CASE(MINT_CONV_I4_R8)
2626 sp [-1].data.i = (gint32)sp [-1].data.f;
2629 MINT_IN_CASE(MINT_CONV_U4_I8)
2630 MINT_IN_CASE(MINT_CONV_I4_I8)
2631 sp [-1].data.i = (gint32)sp [-1].data.l;
2634 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2635 sp [-2].data.i = (gint32)sp [-2].data.l;
2638 MINT_IN_CASE(MINT_CONV_U4_R8)
2639 sp [-1].data.i = (guint32)sp [-1].data.f;
2642 MINT_IN_CASE(MINT_CONV_I8_I4)
2643 sp [-1].data.l = sp [-1].data.i;
2646 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2647 sp [-2].data.l = sp [-2].data.i;
2650 MINT_IN_CASE(MINT_CONV_I8_U4)
2651 sp [-1].data.l = (guint32)sp [-1].data.i;
2654 MINT_IN_CASE(MINT_CONV_I8_R8)
2655 sp [-1].data.l = (gint64)sp [-1].data.f;
2658 MINT_IN_CASE(MINT_CONV_R4_I4)
2659 sp [-1].data.f = (float)sp [-1].data.i;
2662 MINT_IN_CASE(MINT_CONV_R4_I8)
2663 sp [-1].data.f = (float)sp [-1].data.l;
2666 MINT_IN_CASE(MINT_CONV_R4_R8)
2667 sp [-1].data.f = (float)sp [-1].data.f;
2670 MINT_IN_CASE(MINT_CONV_R8_I4)
2671 sp [-1].data.f = (double)sp [-1].data.i;
2674 MINT_IN_CASE(MINT_CONV_R8_I8)
2675 sp [-1].data.f = (double)sp [-1].data.l;
2678 MINT_IN_CASE(MINT_CONV_U8_I4)
2679 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2682 MINT_IN_CASE(MINT_CONV_U8_R8)
2683 sp [-1].data.l = (guint64)sp [-1].data.f;
2687 MINT_IN_CASE(MINT_CPOBJ) {
2690 vtklass = rtm->data_items[READ32 (ip)];
2693 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2697 MINT_IN_CASE(MINT_LDOBJ) {
2700 c = rtm->data_items[* (guint16 *)(ip + 1)];
2702 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2704 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2706 size = mono_class_value_size (c, NULL);
2708 sp [-1].data.p = vt_sp;
2709 memcpy(vt_sp, p, size);
2710 vt_sp += (size + 7) & ~7;
2714 MINT_IN_CASE(MINT_LDSTR)
2715 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2719 MINT_IN_CASE(MINT_NEWOBJ) {
2720 MonoClass *newobj_class;
2721 MonoMethodSignature *csig;
2722 stackval valuetype_this;
2728 token = * (guint16 *)(ip + 1);
2731 child_frame.runtime_method = rtm->data_items [token];
2732 csig = mono_method_signature (child_frame.runtime_method->method);
2733 newobj_class = child_frame.runtime_method->method->klass;
2734 /*if (profiling_classes) {
2735 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2737 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2740 if (newobj_class->parent == mono_defaults.array_class) {
2741 sp -= csig->param_count;
2742 o = ves_array_create (context->domain, newobj_class, csig, sp);
2743 goto array_constructed;
2746 g_assert (csig->hasthis);
2747 if (csig->param_count) {
2748 sp -= csig->param_count;
2749 memmove (sp + 1, sp, csig->param_count * sizeof (stackval));
2751 child_frame.stack_args = sp;
2754 * First arg is the object.
2756 if (newobj_class->valuetype) {
2757 MonoType *t = &newobj_class->byval_arg;
2758 if (!newobj_class->enumtype && (t->type == MONO_TYPE_VALUETYPE || (t->type == MONO_TYPE_GENERICINST && mono_type_generic_inst_is_valuetype (t)))) {
2760 valuetype_this.data.p = vt_sp;
2762 memset (&valuetype_this, 0, sizeof (stackval));
2763 sp->data.p = &valuetype_this;
2766 if (newobj_class != mono_defaults.string_class) {
2767 context->managed_code = 0;
2768 o = mono_object_new_checked (context->domain, newobj_class, &error);
2769 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2770 context->managed_code = 1;
2771 if (*mono_thread_interruption_request_flag ())
2772 mono_thread_interruption_checkpoint ();
2775 child_frame.retval = &retval;
2779 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2781 child_frame.ip = NULL;
2782 child_frame.ex = NULL;
2784 ves_exec_method_with_context (&child_frame, context);
2786 context->current_frame = frame;
2788 if (child_frame.ex) {
2790 * An exception occurred, need to run finally, fault and catch handlers..
2792 frame->ex = child_frame.ex;
2793 goto handle_finally;
2796 * a constructor returns void, but we need to return the object we created
2799 if (newobj_class->valuetype && !newobj_class->enumtype) {
2800 *sp = valuetype_this;
2801 } else if (newobj_class == mono_defaults.string_class) {
2809 MINT_IN_CASE(MINT_CASTCLASS)
2810 c = rtm->data_items [*(guint16 *)(ip + 1)];
2811 if ((o = sp [-1].data.p)) {
2812 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2813 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2815 THROW_EX (mono_get_exception_invalid_cast (), ip);
2819 MINT_IN_CASE(MINT_ISINST)
2820 c = rtm->data_items [*(guint16 *)(ip + 1)];
2821 if ((o = sp [-1].data.p)) {
2822 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2823 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2825 sp [-1].data.p = NULL;
2829 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2830 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2833 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2834 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2837 MINT_IN_CASE(MINT_UNBOX)
2838 c = rtm->data_items[*(guint16 *)(ip + 1)];
2842 THROW_EX (mono_get_exception_null_reference (), ip);
2844 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2845 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2846 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
2847 THROW_EX (mono_get_exception_invalid_cast (), ip);
2849 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2850 int size = mono_class_native_size (c, NULL);
2851 sp [-1].data.p = vt_sp;
2852 vt_sp += (size + 7) & ~7;
2854 stackval_from_data (&c->byval_arg, &sp [-1], mono_object_unbox (o), FALSE);
2857 MINT_IN_CASE(MINT_THROW)
2859 frame->ex_handler = NULL;
2861 sp->data.p = mono_get_exception_null_reference ();
2862 THROW_EX ((MonoException *)sp->data.p, ip);
2864 MINT_IN_CASE(MINT_LDFLDA)
2867 THROW_EX (mono_get_exception_null_reference (), ip);
2868 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2871 MINT_IN_CASE(MINT_CKNULL)
2874 THROW_EX (mono_get_exception_null_reference (), ip);
2878 #define LDFLD(datamem, fieldtype) \
2879 o = sp [-1].data.p; \
2881 THROW_EX (mono_get_exception_null_reference (), ip); \
2882 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2885 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2886 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2887 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2888 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2889 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2890 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2891 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2892 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2893 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2894 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2896 MINT_IN_CASE(MINT_LDFLD_VT)
2899 THROW_EX (mono_get_exception_null_reference (), ip);
2900 i32 = READ32(ip + 2);
2901 sp [-1].data.p = vt_sp;
2902 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2903 vt_sp += (i32 + 7) & ~7;
2907 MINT_IN_CASE(MINT_LDRMFLD) {
2909 MonoClassField *field;
2914 THROW_EX (mono_get_exception_null_reference (), ip);
2915 field = rtm->data_items[* (guint16 *)(ip + 1)];
2917 if (mono_object_is_transparent_proxy (o)) {
2918 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2920 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2921 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2923 addr = (char*)o + field->offset;
2926 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2930 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2931 MonoClassField *field;
2937 THROW_EX (mono_get_exception_null_reference (), ip);
2938 field = rtm->data_items[* (guint16 *)(ip + 1)];
2939 i32 = READ32(ip + 2);
2941 if (mono_object_is_transparent_proxy (o)) {
2942 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2943 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2944 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2946 addr = (char*)o + field->offset;
2949 sp [-1].data.p = vt_sp;
2950 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2951 vt_sp += (i32 + 7) & ~7;
2952 memcpy(sp [-1].data.p, addr, i32);
2956 #define STFLD(datamem, fieldtype) \
2957 o = sp [-2].data.p; \
2959 THROW_EX (mono_get_exception_null_reference (), ip); \
2961 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
2964 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
2965 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
2966 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
2967 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
2968 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
2969 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
2970 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
2971 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
2972 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
2973 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
2975 MINT_IN_CASE(MINT_STFLD_VT)
2978 THROW_EX (mono_get_exception_null_reference (), ip);
2979 i32 = READ32(ip + 2);
2981 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
2982 vt_sp -= (i32 + 7) & ~7;
2986 MINT_IN_CASE(MINT_STRMFLD) {
2987 MonoClassField *field;
2991 THROW_EX (mono_get_exception_null_reference (), ip);
2993 field = rtm->data_items[* (guint16 *)(ip + 1)];
2996 if (mono_object_is_transparent_proxy (o)) {
2997 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2998 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
2999 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3001 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3006 MINT_IN_CASE(MINT_STRMFLD_VT) {
3007 MonoClassField *field;
3011 THROW_EX (mono_get_exception_null_reference (), ip);
3012 field = rtm->data_items[* (guint16 *)(ip + 1)];
3013 i32 = READ32(ip + 2);
3016 if (mono_object_is_transparent_proxy (o)) {
3017 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3018 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3019 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3021 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3024 vt_sp -= (i32 + 7) & ~7;
3027 MINT_IN_CASE(MINT_LDSFLDA) {
3028 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3029 sp->data.p = mono_class_static_field_address (context->domain, field);
3034 MINT_IN_CASE(MINT_LDSFLD) {
3035 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3036 gpointer addr = mono_class_static_field_address (context->domain, field);
3037 stackval_from_data (field->type, sp, addr, FALSE);
3042 MINT_IN_CASE(MINT_LDSFLD_VT) {
3043 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3044 gpointer addr = mono_class_static_field_address (context->domain, field);
3045 int size = READ32 (ip + 2);
3049 vt_sp += (size + 7) & ~7;
3050 stackval_from_data (field->type, sp, addr, FALSE);
3054 MINT_IN_CASE(MINT_STSFLD) {
3055 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3056 gpointer addr = mono_class_static_field_address (context->domain, field);
3059 stackval_to_data (field->type, sp, addr, FALSE);
3062 MINT_IN_CASE(MINT_STSFLD_VT) {
3063 MonoClassField *field = rtm->data_items [* (guint16 *)(ip + 1)];
3064 gpointer addr = mono_class_static_field_address (context->domain, field);
3065 int size = READ32 (ip + 2);
3069 stackval_to_data (field->type, sp, addr, FALSE);
3070 vt_sp -= (size + 7) & ~7;
3073 MINT_IN_CASE(MINT_STOBJ_VT) {
3075 c = rtm->data_items[* (guint16 *)(ip + 1)];
3077 size = mono_class_value_size (c, NULL);
3078 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3079 vt_sp -= (size + 7) & ~7;
3083 MINT_IN_CASE(MINT_STOBJ) {
3085 c = rtm->data_items[* (guint16 *)(ip + 1)];
3087 size = mono_class_value_size (c, NULL);
3088 memcpy(sp [-2].data.p, &sp [-1].data, size);
3092 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3093 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3094 THROW_EX (mono_get_exception_overflow (), ip);
3095 sp [-1].data.i = (guint32)sp [-1].data.f;
3098 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3099 if (sp [-1].data.i < 0)
3100 THROW_EX (mono_get_exception_overflow (), ip);
3101 sp [-1].data.l = sp [-1].data.i;
3104 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3105 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3106 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3107 THROW_EX (mono_get_exception_overflow (), ip);
3108 sp [-1].data.l = (guint64)sp [-1].data.f;
3111 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3112 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3113 THROW_EX (mono_get_exception_overflow (), ip);
3114 sp [-1].data.l = (gint64)sp [-1].data.f;
3117 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3118 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3119 THROW_EX (mono_get_exception_overflow (), ip);
3120 sp [-1].data.i = (mono_u)sp [-1].data.l;
3123 MINT_IN_CASE(MINT_BOX) {
3124 c = rtm->data_items [* (guint16 *)(ip + 1)];
3125 guint16 offset = * (guint16 *)(ip + 2);
3127 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3128 int size = mono_class_value_size (c, NULL);
3129 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, sp [-1 - offset].data.p, &error);
3130 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3131 size = (size + 7) & ~7;
3134 stackval_to_data (&c->byval_arg, &sp [-1 - offset], (char *) &sp [-1 - offset], FALSE);
3135 sp [-1 - offset].data.p = mono_value_box_checked (context->domain, c, &sp [-1 - offset], &error);
3136 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3141 MINT_IN_CASE(MINT_NEWARR)
3142 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3143 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3145 /*if (profiling_classes) {
3146 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3148 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3152 MINT_IN_CASE(MINT_LDLEN)
3155 THROW_EX (mono_get_exception_null_reference (), ip);
3156 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3159 MINT_IN_CASE(MINT_GETCHR) {
3163 THROW_EX (mono_get_exception_null_reference (), ip);
3164 i32 = sp [-1].data.i;
3165 if (i32 < 0 || i32 >= mono_string_length (s))
3166 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3168 sp [-1].data.i = mono_string_chars(s)[i32];
3172 MINT_IN_CASE(MINT_STRLEN)
3174 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3176 MINT_IN_CASE(MINT_ARRAY_RANK)
3179 THROW_EX (mono_get_exception_null_reference (), ip);
3180 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3183 MINT_IN_CASE(MINT_LDELEMA) {
3187 /*token = READ32 (ip)*/;
3193 aindex = sp [1].data.i;
3194 if (aindex >= mono_array_length ((MonoArray *) o))
3195 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3197 /* check the array element corresponds to token */
3198 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3200 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3204 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3205 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3206 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3207 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3208 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3209 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3210 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3211 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3212 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3213 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3214 MINT_IN_CASE(MINT_LDELEM_REF) {
3222 THROW_EX (mono_get_exception_null_reference (), ip);
3224 aindex = sp [1].data.i;
3225 if (aindex >= mono_array_length (o))
3226 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3229 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3232 case MINT_LDELEM_I1:
3233 sp [0].data.i = mono_array_get (o, gint8, aindex);
3235 case MINT_LDELEM_U1:
3236 sp [0].data.i = mono_array_get (o, guint8, aindex);
3238 case MINT_LDELEM_I2:
3239 sp [0].data.i = mono_array_get (o, gint16, aindex);
3241 case MINT_LDELEM_U2:
3242 sp [0].data.i = mono_array_get (o, guint16, aindex);
3245 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3247 case MINT_LDELEM_I4:
3248 sp [0].data.i = mono_array_get (o, gint32, aindex);
3250 case MINT_LDELEM_U4:
3251 sp [0].data.i = mono_array_get (o, guint32, aindex);
3253 case MINT_LDELEM_I8:
3254 sp [0].data.l = mono_array_get (o, guint64, aindex);
3256 case MINT_LDELEM_R4:
3257 sp [0].data.f = mono_array_get (o, float, aindex);
3259 case MINT_LDELEM_R8:
3260 sp [0].data.f = mono_array_get (o, double, aindex);
3262 case MINT_LDELEM_REF:
3263 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3273 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3274 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3275 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3276 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3277 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3278 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3279 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3280 MINT_IN_CASE(MINT_STELEM_REF) {
3287 THROW_EX (mono_get_exception_null_reference (), ip);
3289 aindex = sp [1].data.i;
3290 if (aindex >= mono_array_length ((MonoArray *)o))
3291 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3295 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3297 case MINT_STELEM_I1:
3298 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3300 case MINT_STELEM_I2:
3301 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3303 case MINT_STELEM_I4:
3304 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3306 case MINT_STELEM_I8:
3307 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3309 case MINT_STELEM_R4:
3310 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3312 case MINT_STELEM_R8:
3313 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3315 case MINT_STELEM_REF: {
3316 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3317 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3318 if (sp [2].data.p && !isinst_obj)
3319 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3320 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3329 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3330 if (sp [-1].data.i < 0)
3331 THROW_EX (mono_get_exception_overflow (), ip);
3334 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3335 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3336 THROW_EX (mono_get_exception_overflow (), ip);
3337 sp [-1].data.i = (gint32) sp [-1].data.l;
3340 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3341 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3342 THROW_EX (mono_get_exception_overflow (), ip);
3343 sp [-1].data.i = (gint32) sp [-1].data.f;
3346 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3347 if (sp [-1].data.i < 0)
3348 THROW_EX (mono_get_exception_overflow (), ip);
3351 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3352 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3353 THROW_EX (mono_get_exception_overflow (), ip);
3354 sp [-1].data.i = (guint32) sp [-1].data.l;
3357 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3358 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3359 THROW_EX (mono_get_exception_overflow (), ip);
3360 sp [-1].data.i = (guint32) sp [-1].data.f;
3363 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3364 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3365 THROW_EX (mono_get_exception_overflow (), ip);
3368 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3369 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3370 THROW_EX (mono_get_exception_overflow (), ip);
3371 sp [-1].data.i = (gint16) sp [-1].data.l;
3374 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3375 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3376 THROW_EX (mono_get_exception_overflow (), ip);
3377 sp [-1].data.i = (gint16) sp [-1].data.f;
3380 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3381 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3382 THROW_EX (mono_get_exception_overflow (), ip);
3385 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3386 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3387 THROW_EX (mono_get_exception_overflow (), ip);
3388 sp [-1].data.i = (guint16) sp [-1].data.l;
3391 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3392 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3393 THROW_EX (mono_get_exception_overflow (), ip);
3394 sp [-1].data.i = (guint16) sp [-1].data.f;
3397 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3398 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3399 THROW_EX (mono_get_exception_overflow (), ip);
3402 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3403 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3404 THROW_EX (mono_get_exception_overflow (), ip);
3405 sp [-1].data.i = (gint8) sp [-1].data.l;
3408 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3409 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3410 THROW_EX (mono_get_exception_overflow (), ip);
3411 sp [-1].data.i = (gint8) sp [-1].data.f;
3414 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3415 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3416 THROW_EX (mono_get_exception_overflow (), ip);
3419 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3420 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3421 THROW_EX (mono_get_exception_overflow (), ip);
3422 sp [-1].data.i = (guint8) sp [-1].data.l;
3425 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3426 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3427 THROW_EX (mono_get_exception_overflow (), ip);
3428 sp [-1].data.i = (guint8) sp [-1].data.f;
3432 MINT_IN_CASE(MINT_LDELEM)
3433 MINT_IN_CASE(MINT_STELEM)
3434 MINT_IN_CASE(MINT_UNBOX_ANY)
3436 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3438 MINT_IN_CASE(MINT_CKFINITE)
3439 if (!isfinite(sp [-1].data.f))
3440 THROW_EX (mono_get_exception_arithmetic (), ip);
3444 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3446 MINT_IN_CASE(MINT_LDTOKEN)
3449 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3453 MINT_IN_CASE(MINT_ADD_OVF_I4)
3454 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3455 THROW_EX (mono_get_exception_overflow (), ip);
3458 MINT_IN_CASE(MINT_ADD_OVF_I8)
3459 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3460 THROW_EX (mono_get_exception_overflow (), ip);
3463 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3464 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3465 THROW_EX (mono_get_exception_overflow (), ip);
3466 BINOP_CAST(i, +, guint32);
3468 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3469 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3470 THROW_EX (mono_get_exception_overflow (), ip);
3471 BINOP_CAST(l, +, guint64);
3473 MINT_IN_CASE(MINT_MUL_OVF_I4)
3474 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3475 THROW_EX (mono_get_exception_overflow (), ip);
3478 MINT_IN_CASE(MINT_MUL_OVF_I8)
3479 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3480 THROW_EX (mono_get_exception_overflow (), ip);
3483 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3484 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3485 THROW_EX (mono_get_exception_overflow (), ip);
3486 BINOP_CAST(i, *, guint32);
3488 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3489 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3490 THROW_EX (mono_get_exception_overflow (), ip);
3491 BINOP_CAST(l, *, guint64);
3493 MINT_IN_CASE(MINT_SUB_OVF_I4)
3494 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3495 THROW_EX (mono_get_exception_overflow (), ip);
3498 MINT_IN_CASE(MINT_SUB_OVF_I8)
3499 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3500 THROW_EX (mono_get_exception_overflow (), ip);
3503 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3504 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3505 THROW_EX (mono_get_exception_overflow (), ip);
3506 BINOP_CAST(i, -, guint32);
3508 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3509 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3510 THROW_EX (mono_get_exception_overflow (), ip);
3511 BINOP_CAST(l, -, guint64);
3513 MINT_IN_CASE(MINT_ENDFINALLY)
3515 ip = finally_ips->data;
3516 finally_ips = g_slist_remove (finally_ips, ip);
3523 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3524 MINT_IN_CASE(MINT_LEAVE_S)
3525 while (sp > frame->stack) {
3529 if (*ip == MINT_LEAVE_S) {
3530 ip += (short) *(ip + 1);
3532 ip += (gint32) READ32 (ip + 1);
3535 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3536 frame->ex_handler = NULL;
3539 goto handle_finally;
3541 MINT_IN_CASE(MINT_ICALL_V_V)
3542 MINT_IN_CASE(MINT_ICALL_V_P)
3543 MINT_IN_CASE(MINT_ICALL_P_V)
3544 MINT_IN_CASE(MINT_ICALL_P_P)
3545 MINT_IN_CASE(MINT_ICALL_PP_V)
3546 MINT_IN_CASE(MINT_ICALL_PI_V)
3547 MINT_IN_CASE(MINT_ICALL_PP_P)
3548 MINT_IN_CASE(MINT_ICALL_PI_P)
3549 MINT_IN_CASE(MINT_ICALL_PPP_V)
3550 MINT_IN_CASE(MINT_ICALL_PPI_V)
3551 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3552 if (frame->ex != NULL)
3553 goto handle_exception;
3556 MINT_IN_CASE(MINT_MONO_LDPTR)
3557 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3561 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3562 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3563 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3567 MINT_IN_CASE(MINT_MONO_FREE)
3570 g_error ("that doesn't seem right");
3571 g_free (sp->data.p);
3573 MINT_IN_CASE(MINT_MONO_RETOBJ)
3576 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3577 mono_method_signature (frame->runtime_method->method)->pinvoke);
3578 if (sp > frame->stack)
3579 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3582 #define RELOP(datamem, op) \
3584 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3586 MINT_IN_CASE(MINT_CEQ_I4)
3589 MINT_IN_CASE(MINT_CEQ0_I4)
3590 sp [-1].data.i = (sp [-1].data.i == 0);
3593 MINT_IN_CASE(MINT_CEQ_I8)
3596 MINT_IN_CASE(MINT_CEQ_R8)
3598 if (isunordered (sp [-1].data.f, sp [0].data.f))
3601 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3604 MINT_IN_CASE(MINT_CGT_I4)
3607 MINT_IN_CASE(MINT_CGT_I8)
3610 MINT_IN_CASE(MINT_CGT_R8)
3612 if (isunordered (sp [-1].data.f, sp [0].data.f))
3615 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3619 #define RELOP_CAST(datamem, op, type) \
3621 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3624 MINT_IN_CASE(MINT_CGT_UN_I4)
3625 RELOP_CAST(i, >, guint32);
3627 MINT_IN_CASE(MINT_CGT_UN_I8)
3628 RELOP_CAST(l, >, guint64);
3630 MINT_IN_CASE(MINT_CGT_UN_R8)
3632 if (isunordered (sp [-1].data.f, sp [0].data.f))
3635 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3638 MINT_IN_CASE(MINT_CLT_I4)
3641 MINT_IN_CASE(MINT_CLT_I8)
3644 MINT_IN_CASE(MINT_CLT_R8)
3646 if (isunordered (sp [-1].data.f, sp [0].data.f))
3649 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3652 MINT_IN_CASE(MINT_CLT_UN_I4)
3653 RELOP_CAST(i, <, guint32);
3655 MINT_IN_CASE(MINT_CLT_UN_I8)
3656 RELOP_CAST(l, <, guint64);
3658 MINT_IN_CASE(MINT_CLT_UN_R8)
3660 if (isunordered (sp [-1].data.f, sp [0].data.f))
3663 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3666 MINT_IN_CASE(MINT_LDFTN) {
3667 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3672 MINT_IN_CASE(MINT_LDVIRTFTN) {
3673 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3677 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3679 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3684 MINT_IN_CASE(MINT_LDTHISA)
3685 g_error ("should not happen");
3686 // sp->data.p = &frame->obj;
3691 #define LDARG(datamem, argtype) \
3692 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3696 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3697 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3698 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3699 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3700 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3701 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3702 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3703 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3704 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3705 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3707 MINT_IN_CASE(MINT_LDARG_VT)
3709 i32 = READ32(ip + 2);
3710 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3711 vt_sp += (i32 + 7) & ~7;
3716 #define STARG(datamem, argtype) \
3718 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3721 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3722 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3723 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3724 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3725 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3726 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3727 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3728 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3729 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3730 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3732 MINT_IN_CASE(MINT_STARG_VT)
3733 i32 = READ32(ip + 2);
3735 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3736 vt_sp -= (i32 + 7) & ~7;
3740 #define STINARG(datamem, argtype) \
3742 int n = * (guint16 *)(ip + 1); \
3743 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3747 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3748 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3749 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3750 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3751 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3752 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3753 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3754 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3755 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3756 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3758 MINT_IN_CASE(MINT_STINARG_VT) {
3759 int n = * (guint16 *)(ip + 1);
3760 i32 = READ32(ip + 2);
3761 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3766 MINT_IN_CASE(MINT_LDARGA)
3767 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3772 #define LDLOC(datamem, argtype) \
3773 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3777 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3778 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3779 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3780 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3781 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3782 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3783 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3784 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3785 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3786 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3788 MINT_IN_CASE(MINT_LDLOC_VT)
3790 i32 = READ32(ip + 2);
3791 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3792 vt_sp += (i32 + 7) & ~7;
3797 MINT_IN_CASE(MINT_LDLOCA_S)
3798 sp->data.p = locals + * (guint16 *)(ip + 1);
3803 #define STLOC(datamem, argtype) \
3805 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3808 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3809 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3810 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3811 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3812 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3813 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3814 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3815 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3816 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3817 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3819 #define STLOC_NP(datamem, argtype) \
3820 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3823 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3824 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3826 MINT_IN_CASE(MINT_STLOC_VT)
3827 i32 = READ32(ip + 2);
3829 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3830 vt_sp -= (i32 + 7) & ~7;
3834 MINT_IN_CASE(MINT_LOCALLOC)
3835 if (sp != frame->stack + 1) /*FIX?*/
3836 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
3837 sp [-1].data.p = alloca (sp [-1].data.i);
3841 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
3843 MINT_IN_CASE(MINT_INITOBJ)
3845 memset (sp->data.vt, 0, READ32(ip + 1));
3848 MINT_IN_CASE(MINT_CPBLK)
3850 if (!sp [0].data.p || !sp [1].data.p)
3851 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3853 /* FIXME: value and size may be int64... */
3854 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
3857 MINT_IN_CASE(MINT_CONSTRAINED_) {
3859 /* FIXME: implement */
3861 token = READ32 (ip);
3866 MINT_IN_CASE(MINT_INITBLK)
3869 THROW_EX (mono_get_exception_null_reference(), ip - 1);
3871 /* FIXME: value and size may be int64... */
3872 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
3875 MINT_IN_CASE(MINT_NO_)
3876 /* FIXME: implement */
3880 MINT_IN_CASE(MINT_RETHROW)
3882 * need to clarify what this should actually do:
3883 * start the search from the last found handler in
3884 * this method or continue in the caller or what.
3885 * Also, do we need to run finally/fault handlers after a retrow?
3886 * Well, this implementation will follow the usual search
3887 * for an handler, considering the current ip as throw spot.
3888 * We need to NULL frame->ex_handler for the later code to
3889 * actually run the new found handler.
3891 frame->ex_handler = NULL;
3892 THROW_EX (frame->ex, ip - 1);
3895 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
3896 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
3900 g_assert_not_reached ();
3902 * Exception handling code.
3903 * The exception object is stored in frame->ex.
3910 MonoInvocation *inv;
3911 MonoExceptionClause *clause;
3917 g_print ("* Handling exception '%s' at IL_%04x\n",
3918 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
3919 rtm == NULL ? 0 : frame->ip - rtm->code);
3921 if (die_on_exception)
3924 for (inv = frame; inv; inv = inv->parent) {
3926 if (inv->runtime_method == NULL)
3928 method = inv->runtime_method->method;
3929 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3931 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
3933 if (inv->ip == NULL)
3935 ip_offset = inv->ip - inv->runtime_method->code;
3936 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
3937 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
3938 clause = &inv->runtime_method->clauses [i];
3939 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
3940 if (!clause->flags) {
3941 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
3942 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3945 * OK, we found an handler, now we need to execute the finally
3946 * and fault blocks before branching to the handler code.
3948 inv->ex_handler = clause;
3951 g_print ("* Found handler at '%s'\n", method->name);
3953 goto handle_finally;
3956 /* FIXME: handle filter clauses */
3963 * If we get here, no handler was found: print a stack trace.
3965 for (inv = frame; inv; inv = inv->parent) {
3966 if (inv->invoke_trap)
3967 goto handle_finally;
3970 ex_obj = (MonoObject*)frame->ex;
3971 mono_unhandled_exception (ex_obj);
3978 MonoExceptionClause *clause;
3979 GSList *old_list = finally_ips;
3980 MonoMethod *method = frame->runtime_method->method;
3981 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
3982 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3986 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
3988 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
3989 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
3992 ip_offset = frame->ip - rtm->code;
3994 if (endfinally_ip != NULL)
3995 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
3996 for (i = 0; i < header->num_clauses; ++i)
3997 if (frame->ex_handler == &rtm->clauses [i])
4001 clause = &rtm->clauses [i];
4002 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4003 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4004 ip = rtm->code + clause->handler_offset;
4005 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4008 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4014 endfinally_ip = NULL;
4016 if (old_list != finally_ips && finally_ips) {
4017 ip = finally_ips->data;
4018 finally_ips = g_slist_remove (finally_ips, ip);
4019 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4024 * If an exception is set, we need to execute the fault handler, too,
4025 * otherwise, we continue normally.
4035 MonoExceptionClause *clause;
4036 MonoMethod *method = frame->runtime_method->method;
4037 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4038 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4042 g_print ("* Handle fault\n");
4044 ip_offset = frame->ip - rtm->code;
4045 for (i = 0; i < header->num_clauses; ++i) {
4046 clause = &rtm->clauses [i];
4047 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4048 ip = rtm->code + clause->handler_offset;
4051 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4057 * If the handler for the exception was found in this method, we jump
4058 * to it right away, otherwise we return and let the caller run
4059 * the finally, fault and catch blocks.
4060 * This same code should be present in the endfault opcode, but it
4061 * is corrently not assigned in the ECMA specs: LAMESPEC.
4063 if (frame->ex_handler) {
4066 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4068 ip = rtm->code + frame->ex_handler->handler_offset;
4070 vt_sp = (unsigned char *) sp + rtm->stack_size;
4071 sp->data.p = frame->ex;
4082 ves_exec_method (MonoInvocation *frame)
4084 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4085 ThreadContext context_struct;
4092 mono_unhandled_exception ((MonoObject*)frame->ex);
4095 if (context == NULL) {
4096 context = &context_struct;
4097 context_struct.domain = mono_domain_get ();
4098 context_struct.base_frame = frame;
4099 context_struct.current_frame = NULL;
4100 context_struct.env_frame = frame;
4101 context_struct.current_env = &env;
4102 context_struct.search_for_handler = 0;
4103 context_struct.managed_code = 0;
4104 mono_native_tls_set_value (thread_context_id, context);
4107 frame->parent = context->current_frame;
4108 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4109 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4110 context->managed_code = 1;
4111 ves_exec_method_with_context (frame, context);
4112 context->managed_code = 0;
4114 if (context != &context_struct && context->current_env) {
4115 context->env_frame->ex = frame->ex;
4116 longjmp (*context->current_env, 1);
4119 mono_unhandled_exception ((MonoObject*)frame->ex);
4121 if (context->base_frame == frame)
4122 mono_native_tls_set_value (thread_context_id, NULL);
4124 context->current_frame = frame->parent;
4128 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4130 MonoImage *image = mono_assembly_get_image (assembly);
4135 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4136 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4139 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4141 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4142 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4150 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4151 "Usage is: mint [options] executable args...\n\n", VERSION);
4153 "Runtime Debugging:\n"
4158 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4161 " --traceclassinit\n"
4164 " --debug method_name\n"
4171 " --config filename load the specified config file instead of the default\n"
4172 " --workers n maximum number of worker threads\n"
4178 interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4179 MonoReflectionMethod **method,
4180 gint32 *iloffset, gint32 *native_offset,
4181 MonoString **file, gint32 *line, gint32 *column)
4183 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4184 MonoInvocation *inv = context->current_frame;
4188 for (i = 0; inv && i < skip; inv = inv->parent)
4189 if (inv->runtime_method != NULL)
4200 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4201 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4206 if (need_file_info) {
4210 *file = mono_string_new (mono_domain_get (), "unknown");
4217 interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4219 MonoDomain *domain = mono_domain_get ();
4221 MonoArray *ta = exc->trace_ips;
4226 /* Exception is not thrown yet */
4227 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4228 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4232 len = mono_array_length (ta);
4234 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4235 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4237 for (i = skip; i < len / 2; i++) {
4238 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4239 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4240 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4241 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4244 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4245 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4246 sf->native_offset = ip - rtm->code;
4250 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4252 if (need_file_info) {
4255 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4257 sf->filename = filename? mono_string_new (domain, filename): NULL;
4264 mono_array_set (res, gpointer, i, sf);
4271 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4273 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4274 MonoObject *delegate;
4277 g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4279 delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4280 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4282 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4283 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4291 mono_native_tls_alloc (&thread_context_id, NULL);
4292 mono_native_tls_set_value (thread_context_id, NULL);
4293 mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4294 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4296 mono_interp_transform_init ();
4299 typedef int (*TestMethod) (void);
4302 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4304 int result, expected, failed, cfailed, run;
4305 double elapsed, transform_time;
4307 MonoObject *result_obj;
4308 static gboolean filter_method_init = FALSE;
4309 static const char *filter_method = NULL;
4311 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4312 cfailed = failed = run = 0;
4313 transform_time = elapsed = 0.0;
4316 /* fixme: ugly hack - delete all previously compiled methods */
4317 if (domain_jit_info (domain)) {
4318 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4319 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4320 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4321 mono_jit_code_hash_init (&(domain->jit_code_hash));
4325 g_timer_start (timer);
4326 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4328 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4330 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4334 if (!filter_method_init) {
4335 filter_method = g_getenv ("INTERP_FILTER_METHOD");
4336 filter_method_init = TRUE;
4338 gboolean filter = FALSE;
4339 if (filter_method) {
4340 const char *name = filter_method;
4342 if ((strchr (name, '.') > name) || strchr (name, ':')) {
4343 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
4344 filter = mono_method_desc_full_match (desc, method);
4345 mono_method_desc_free (desc);
4347 filter = strcmp (method->name, name) == 0;
4349 } else { // no filter
4352 if (strncmp (method->name, "test_", 5) == 0 && filter) {
4353 MonoError interp_error;
4354 MonoObject *exc = NULL;
4356 result_obj = interp_mono_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
4357 if (!mono_error_ok (&interp_error)) {
4359 g_print ("Test '%s' execution failed.\n", method->name);
4360 } else if (exc != NULL) {
4361 g_print ("Exception in Test '%s' occured:\n", method->name);
4362 mono_object_describe (exc);
4366 result = *(gint32 *) mono_object_unbox (result_obj);
4367 expected = atoi (method->name + 5); // FIXME: oh no.
4370 if (result != expected) {
4372 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4377 g_timer_stop (timer);
4378 elapsed = g_timer_elapsed (timer, NULL);
4379 if (failed > 0 || cfailed > 0){
4380 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4381 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4383 g_print ("Results: total tests: %d, all pass \n", run);
4386 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4387 elapsed - transform_time, transform_time);
4388 *total += failed + cfailed;
4392 interp_regression (MonoImage *image, int verbose, int *total_run)
4395 GTimer *timer = g_timer_new ();
4396 MonoDomain *domain = mono_domain_get ();
4400 /* load the metadata */
4401 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4403 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4405 mono_error_cleanup (&error);
4408 mono_class_init (method->klass);
4413 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4415 g_timer_destroy (timer);
4420 interp_regression_list (int verbose, int count, char *images [])
4422 int i, total, total_run, run;
4424 total_run = total = 0;
4425 for (i = 0; i < count; ++i) {
4426 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4428 g_warning ("failed to load assembly: %s", images [i]);
4431 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4435 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4437 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);