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>
65 #include <mono/mini/mini.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 static gint *abort_requested;
81 int mono_interp_traceopt = 0;
83 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,domain,mono_method,error) \
85 (frame)->parent = (parent_frame); \
86 (frame)->obj = (obj_this); \
87 (frame)->stack_args = (method_args); \
88 (frame)->retval = (method_retval); \
89 (frame)->runtime_method = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
92 (frame)->invoke_trap = 0; \
95 void ves_exec_method (MonoInvocation *frame);
97 static char* dump_stack (stackval *stack, stackval *sp);
98 static char* dump_frame (MonoInvocation *inv);
99 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
100 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
102 typedef void (*ICallMethod) (MonoInvocation *frame);
104 static guint32 die_on_exception = 0;
105 static MonoNativeTlsKey thread_context_id;
107 static char* dump_args (MonoInvocation *inv);
109 #define DEBUG_INTERP 0
112 /* If true, then we output the opcodes as we interpret them */
113 static int global_tracing = 1;
115 static int debug_indent_level = 0;
117 static int break_on_method = 0;
118 static int nested_trace = 0;
119 static GList *db_methods = NULL;
126 for (h = 0; h < debug_indent_level; h++)
131 db_match_method (gpointer data, gpointer user_data)
133 MonoMethod *m = (MonoMethod*)user_data;
134 MonoMethodDesc *desc = data;
136 if (mono_method_desc_full_match (desc, m))
140 static void debug_enter (MonoInvocation *frame, int *tracing)
143 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
145 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
149 MonoMethod *method = frame->runtime_method->method;
150 char *mn, *args = dump_args (frame);
151 debug_indent_level++;
153 mn = mono_method_full_name (method, FALSE);
154 g_printerr ("(0x%08x) Entering %s (", mono_thread_internal_current (), mn);
156 g_printerr ("%s)\n", args);
159 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
160 mono_profiler_method_enter (frame->runtime_method->method);
164 #define DEBUG_LEAVE() \
167 args = dump_retval (frame); \
169 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
170 g_printerr ("(0x%08x) Leaving %s", mono_thread_internal_current (), mn); \
172 g_printerr (" => %s\n", args); \
174 debug_indent_level--; \
175 if (tracing == 3) global_tracing = 0; \
177 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
178 mono_profiler_method_leave (frame->runtime_method->method);
182 static void debug_enter (MonoInvocation *frame, int *tracing)
185 #define DEBUG_LEAVE()
190 interp_ex_handler (MonoException *ex) {
192 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
196 stack_trace = dump_frame (context->current_frame);
197 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
198 g_free (stack_trace);
199 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
200 char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
201 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
202 fprintf(stderr, "Nothing can catch this exception: ");
203 fprintf(stderr, "%s", ex->object.vtable->klass->name);
204 if (ex->message != NULL) {
205 char *m = mono_string_to_utf8_checked (ex->message, &error);
206 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
207 fprintf(stderr, ": %s", m);
210 fprintf(stderr, "\n%s\n", strace);
212 if (ex->inner_ex != NULL) {
213 ex = (MonoException *)ex->inner_ex;
214 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
215 if (ex->message != NULL) {
216 char *m = mono_string_to_utf8_checked (ex->message, &error);
217 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
218 fprintf(stderr, ": %s", m);
221 strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
222 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
223 fprintf(stderr, "\n");
224 fprintf(stderr, "%s\n", strace);
227 /* wait for other threads to also collapse */
228 // Sleep(1000); // TODO: proper sleep
231 context->env_frame->ex = ex;
232 context->search_for_handler = 1;
233 longjmp (*context->current_env, 1);
237 ves_real_abort (int line, MonoMethod *mh,
238 const unsigned short *ip, stackval *stack, stackval *sp)
241 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
242 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
243 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
244 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
245 g_print ("0x%04x %02x\n",
246 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
247 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
249 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
252 #define ves_abort() \
254 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
255 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
259 interp_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target, MonoError *error)
261 g_error ("FIXME: use domain and error");
262 // return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke_for_target (method, target));
265 static mono_mutex_t runtime_method_lookup_section;
268 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
271 mono_error_init (error);
273 mono_os_mutex_lock (&runtime_method_lookup_section);
274 if ((rtm = mono_internal_hash_table_lookup (&domain->jit_code_hash, method))) {
275 mono_os_mutex_unlock (&runtime_method_lookup_section);
278 rtm = mono_mempool_alloc (domain->mp, sizeof (RuntimeMethod));
279 memset (rtm, 0, sizeof (*rtm));
280 rtm->method = method;
281 rtm->param_count = mono_method_signature (method)->param_count;
282 rtm->hasthis = mono_method_signature (method)->hasthis;
283 rtm->valuetype = method->klass->valuetype;
284 mono_internal_hash_table_insert (&domain->jit_code_hash, method, rtm);
285 mono_os_mutex_unlock (&runtime_method_lookup_section);
291 interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
293 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
294 method = mono_marshal_get_synchronized_wrapper (method);
295 return mono_interp_get_runtime_method (domain, method, error);
298 static inline RuntimeMethod*
299 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
301 MonoMethod *m = runtime_method->method;
304 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
305 RuntimeMethod *ret = NULL;
306 if (mono_object_is_transparent_proxy (obj)) {
307 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
308 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
309 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
310 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
311 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
313 ret = runtime_method;
318 int slot = mono_method_get_vtable_slot (m);
319 if (mono_class_is_interface (m->klass)) {
320 g_assert (obj->vtable->klass != m->klass);
321 /* TODO: interface offset lookup is slow, go through IMT instead */
322 slot += mono_class_interface_offset (obj->vtable->klass, m->klass);
324 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
325 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
326 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
327 return virtual_runtime_method;
331 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
334 switch (type->type) {
335 case MONO_TYPE_OBJECT:
336 case MONO_TYPE_CLASS:
337 case MONO_TYPE_STRING:
338 case MONO_TYPE_ARRAY:
339 case MONO_TYPE_SZARRAY:
344 result->data.p = *(gpointer*)data;
347 switch (type->type) {
351 result->data.i = *(gint8*)data;
354 case MONO_TYPE_BOOLEAN:
355 result->data.i = *(guint8*)data;
358 result->data.i = *(gint16*)data;
362 result->data.i = *(guint16*)data;
365 result->data.i = *(gint32*)data;
369 result->data.nati = *(mono_i*)data;
372 result->data.p = *(gpointer*)data;
375 result->data.i = *(guint32*)data;
378 result->data.f = *(float*)data;
382 result->data.l = *(gint64*)data;
385 result->data.f = *(double*)data;
387 case MONO_TYPE_STRING:
388 case MONO_TYPE_SZARRAY:
389 case MONO_TYPE_CLASS:
390 case MONO_TYPE_OBJECT:
391 case MONO_TYPE_ARRAY:
392 result->data.p = *(gpointer*)data;
394 case MONO_TYPE_VALUETYPE:
395 if (type->data.klass->enumtype) {
396 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
402 size = mono_class_native_size (type->data.klass, NULL);
404 size = mono_class_value_size (type->data.klass, NULL);
405 memcpy (result->data.vt, data, size);
409 g_warning ("got type 0x%02x", type->type);
410 g_assert_not_reached ();
415 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
418 gpointer *p = (gpointer*)data;
422 /* printf ("TODAT0 %p\n", data); */
423 switch (type->type) {
426 guint8 *p = (guint8*)data;
430 case MONO_TYPE_BOOLEAN: {
431 guint8 *p = (guint8*)data;
432 *p = (val->data.i != 0);
437 case MONO_TYPE_CHAR: {
438 guint16 *p = (guint16*)data;
443 mono_i *p = (mono_i*)data;
444 /* In theory the value used by stloc should match the local var type
445 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
446 a native int - both by csc and mcs). Not sure what to do about sign extension
447 as it is outside the spec... doing the obvious */
448 *p = (mono_i)val->data.nati;
452 mono_u *p = (mono_u*)data;
454 *p = (mono_u)val->data.nati;
459 gint32 *p = (gint32*)data;
465 gint64 *p = (gint64*)data;
470 float *p = (float*)data;
475 double *p = (double*)data;
479 case MONO_TYPE_STRING:
480 case MONO_TYPE_SZARRAY:
481 case MONO_TYPE_CLASS:
482 case MONO_TYPE_OBJECT:
483 case MONO_TYPE_ARRAY:
484 case MONO_TYPE_PTR: {
485 gpointer *p = (gpointer*)data;
489 case MONO_TYPE_VALUETYPE:
490 if (type->data.klass->enumtype) {
491 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
497 size = mono_class_native_size (type->data.klass, NULL);
499 size = mono_class_value_size (type->data.klass, NULL);
501 memcpy (data, val->data.p, size);
505 g_warning ("got type %x", type->type);
506 g_assert_not_reached ();
511 fill_in_trace (MonoException *exception, MonoInvocation *frame)
513 char *stack_trace = dump_frame (frame);
514 MonoDomain *domain = mono_domain_get();
515 (exception)->stack_trace = mono_string_new (domain, stack_trace);
516 (exception)->trace_ips = get_trace_ips (domain, frame);
517 g_free (stack_trace);
520 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
522 #define THROW_EX(exception,ex_ip) \
524 frame->ip = (ex_ip); \
525 frame->ex = (MonoException*)(exception); \
526 FILL_IN_TRACE(frame->ex, frame); \
527 goto handle_exception; \
531 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
534 intptr_t *lower_bounds;
539 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
540 for (i = 0; i < sig->param_count; ++i) {
541 lengths [i] = values->data.i;
544 if (klass->rank == sig->param_count) {
545 /* Only lengths provided. */
548 /* lower bounds are first. */
549 lower_bounds = (intptr_t *) lengths;
550 lengths += klass->rank;
552 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
553 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
558 ves_array_set (MonoInvocation *frame)
560 stackval *sp = frame->stack_args;
564 gint32 i, t, pos, esize;
570 ac = o->vtable->klass;
572 g_assert (ac->rank >= 1);
575 if (ao->bounds != NULL) {
576 pos -= ao->bounds [0].lower_bound;
577 for (i = 1; i < ac->rank; i++) {
578 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
579 ao->bounds [i].length) {
580 frame->ex = mono_get_exception_index_out_of_range ();
581 FILL_IN_TRACE(frame->ex, frame);
584 pos = pos*ao->bounds [i].length + sp [i].data.i -
585 ao->bounds [i].lower_bound;
587 } else if (pos >= ao->max_length) {
588 frame->ex = mono_get_exception_index_out_of_range ();
589 FILL_IN_TRACE(frame->ex, frame);
593 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
595 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
596 mono_error_cleanup (&error);
598 frame->ex = mono_get_exception_array_type_mismatch ();
599 FILL_IN_TRACE (frame->ex, frame);
604 esize = mono_array_element_size (ac);
605 ea = mono_array_addr_with_size (ao, esize, pos);
607 mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
608 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
612 ves_array_get (MonoInvocation *frame)
614 stackval *sp = frame->stack_args;
618 gint32 i, t, pos, esize;
624 ac = o->vtable->klass;
626 g_assert (ac->rank >= 1);
629 if (ao->bounds != NULL) {
630 pos -= ao->bounds [0].lower_bound;
631 for (i = 1; i < ac->rank; i++) {
632 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
633 ao->bounds [i].length) {
634 frame->ex = mono_get_exception_index_out_of_range ();
635 FILL_IN_TRACE(frame->ex, frame);
639 pos = pos*ao->bounds [i].length + sp [i].data.i -
640 ao->bounds [i].lower_bound;
642 } else if (pos >= ao->max_length) {
643 frame->ex = mono_get_exception_index_out_of_range ();
644 FILL_IN_TRACE(frame->ex, frame);
648 esize = mono_array_element_size (ac);
649 ea = mono_array_addr_with_size (ao, esize, pos);
651 mt = mono_method_signature (frame->runtime_method->method)->ret;
652 stackval_from_data (mt, frame->retval, ea, FALSE);
656 ves_array_element_address (MonoInvocation *frame)
658 stackval *sp = frame->stack_args;
662 gint32 i, t, pos, esize;
667 ac = o->vtable->klass;
669 g_assert (ac->rank >= 1);
672 if (ao->bounds != NULL) {
673 pos -= ao->bounds [0].lower_bound;
674 for (i = 1; i < ac->rank; i++) {
675 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
676 ao->bounds [i].length) {
677 frame->ex = mono_get_exception_index_out_of_range ();
678 FILL_IN_TRACE(frame->ex, frame);
681 pos = pos*ao->bounds [i].length + sp [i].data.i -
682 ao->bounds [i].lower_bound;
684 } else if (pos >= ao->max_length) {
685 frame->ex = mono_get_exception_index_out_of_range ();
686 FILL_IN_TRACE(frame->ex, frame);
690 esize = mono_array_element_size (ac);
691 ea = mono_array_addr_with_size (ao, esize, pos);
693 frame->retval->data.p = ea;
697 interp_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
699 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
700 MonoInvocation *frame;
702 MonoMethodHeader *hd;
705 if (!context) return;
707 frame = context->current_frame;
710 gboolean managed = FALSE;
711 MonoMethod *method = frame->runtime_method->method;
712 if (!method || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
715 hd = mono_method_get_header_checked (method, &error);
716 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
717 il_offset = frame->ip - (const unsigned short *)hd->code;
718 if (!method->wrapper_type)
721 if (func (method, -1, il_offset, managed, user_data))
723 frame = frame->parent;
727 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
729 struct _MethodArguments {
737 typedef struct _MethodArguments MethodArguments;
739 // TODO: this function is also arch dependent (register width).
740 static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
742 // TODO: don't malloc this data structure.
743 MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
748 for (int i = 0; i < sig->param_count; i++) {
749 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
751 case MONO_TYPE_BOOLEAN:
762 case MONO_TYPE_SZARRAY:
763 case MONO_TYPE_CLASS:
764 case MONO_TYPE_OBJECT:
765 case MONO_TYPE_STRING:
770 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
775 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
778 g_error ("build_args_from_sig: TODO, more than two iregs: %d\n", margs->ilen);
781 g_error ("build_args_from_sig: TODO, allocate floats: %d\n", margs->flen);
787 margs->iargs [0] = frame->obj;
791 for (int i = 0; i < sig->param_count; i++) {
792 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
794 case MONO_TYPE_BOOLEAN:
805 case MONO_TYPE_SZARRAY:
806 case MONO_TYPE_CLASS:
807 case MONO_TYPE_OBJECT:
808 case MONO_TYPE_STRING:
810 margs->iargs [int_i] = frame->stack_args [i].data.p;
812 g_printerr ("build_args_from_sig: margs->iargs[%d]: %p (frame @ %d)\n", int_i, margs->iargs[int_i], i);
817 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
821 if (sig->ret->type != MONO_TYPE_VOID) {
822 margs->retval = &(frame->retval->data.p);
824 margs->retval = NULL;
831 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
834 MonoInvocation *old_frame = context->current_frame;
835 MonoInvocation *old_env_frame = context->env_frame;
836 jmp_buf *old_env = context->current_env;
839 context->current_frame = old_frame;
840 context->env_frame = old_env_frame;
841 context->current_env = old_env;
842 context->managed_code = 1;
847 context->env_frame = frame;
848 context->current_env = &env;
850 g_assert (!frame->runtime_method);
851 if (!mono_interp_enter_icall_trampoline) {
853 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
855 // mono_tramp_info_register (info, NULL);
858 MethodArguments *margs = build_args_from_sig (sig, frame);
860 g_printerr ("ICALL: mono_interp_enter_icall_trampoline = %p, addr = %p\n", mono_interp_enter_icall_trampoline, addr);
861 g_printerr ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
864 context->current_frame = frame;
865 context->managed_code = 0;
867 mono_interp_enter_icall_trampoline (addr, margs);
869 context->managed_code = 1;
870 /* domain can only be changed by native code */
871 context->domain = mono_domain_get ();
873 if (*abort_requested)
874 mono_thread_interruption_checkpoint ();
876 if (!MONO_TYPE_ISSTRUCT (sig->ret))
877 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
879 context->current_frame = old_frame;
880 context->env_frame = old_env_frame;
881 context->current_env = old_env;
883 g_free (margs->iargs);
884 g_free (margs->fargs);
889 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
891 MonoDelegate *delegate = (MonoDelegate *)this;
894 delegate->method_info = mono_method_get_object_checked (domain, runtime_method->method, NULL, &error);
895 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
896 delegate->target = target;
898 if (target && mono_object_is_transparent_proxy (target)) {
899 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
900 delegate->method_ptr = mono_interp_get_runtime_method (domain, method, &error);
901 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
903 delegate->method_ptr = runtime_method;
908 mono_interp_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
912 MonoDomain *domain = mono_domain_get ();
915 d = (MonoDelegate*)mono_object_new_checked (domain, klass, &error);
916 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
918 ji = mono_jit_info_table_find (domain, ftn);
920 mono_raise_exception (mono_get_exception_argument ("", "Function pointer was not created by a Delegate."));
922 /* FIXME: discard the wrapper and call the original method */
923 interp_delegate_ctor (domain, (MonoObject*)d, NULL, mono_interp_get_runtime_method (domain, ji->d.method, &error));
924 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
931 * runtime specifies that the implementation of the method is automatically
932 * provided by the runtime and is primarily used for the methods of delegates.
935 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
937 MonoMethod *method = frame->runtime_method->method;
938 const char *name = method->name;
939 MonoObject *obj = (MonoObject*)frame->obj;
940 MonoObject *isinst_obj;
943 mono_class_init (method->klass);
945 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.multicastdelegate_class, &error);
946 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
947 if (obj && isinst_obj) {
948 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
949 interp_delegate_ctor (context->domain, obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
954 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
955 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
956 if (obj && isinst_obj) {
957 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
958 ves_array_set (frame);
961 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
962 ves_array_get (frame);
965 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
966 ves_array_element_address (frame);
971 g_error ("Don't know how to exec runtime method %s.%s::%s",
972 method->klass->name_space, method->klass->name,
977 dump_stack (stackval *stack, stackval *sp)
980 GString *str = g_string_new ("");
983 return g_string_free (str, FALSE);
986 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
989 return g_string_free (str, FALSE);
993 dump_stackval (GString *str, stackval *s, MonoType *type)
995 switch (type->type) {
1002 case MONO_TYPE_CHAR:
1003 case MONO_TYPE_BOOLEAN:
1004 g_string_append_printf (str, "[%d] ", s->data.i);
1006 case MONO_TYPE_STRING:
1007 case MONO_TYPE_SZARRAY:
1008 case MONO_TYPE_CLASS:
1009 case MONO_TYPE_OBJECT:
1010 case MONO_TYPE_ARRAY:
1014 g_string_append_printf (str, "[%p] ", s->data.p);
1016 case MONO_TYPE_VALUETYPE:
1017 if (type->data.klass->enumtype)
1018 g_string_append_printf (str, "[%d] ", s->data.i);
1020 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1024 g_string_append_printf (str, "[%g] ", s->data.f);
1029 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
1035 dump_args (MonoInvocation *inv)
1037 GString *str = g_string_new ("");
1039 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1041 if (signature->param_count == 0)
1042 return g_string_free (str, FALSE);
1044 if (signature->hasthis)
1045 g_string_append_printf (str, "%p ", inv->obj);
1047 for (i = 0; i < signature->param_count; ++i)
1048 dump_stackval (str, inv->stack_args + i, signature->params [i]);
1050 return g_string_free (str, FALSE);
1054 dump_retval (MonoInvocation *inv)
1056 GString *str = g_string_new ("");
1057 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1059 if (ret->type != MONO_TYPE_VOID)
1060 dump_stackval (str, inv->retval, ret);
1062 return g_string_free (str, FALSE);
1066 dump_frame (MonoInvocation *inv)
1068 GString *str = g_string_new ("");
1073 for (i = 0; inv; inv = inv->parent) {
1074 if (inv->runtime_method != NULL) {
1075 MonoMethod *method = inv->runtime_method->method;
1079 const char * opname = "";
1081 gchar *source = NULL;
1085 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1086 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1087 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1088 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1092 opname = mono_interp_opname [*inv->ip];
1093 codep = inv->ip - inv->runtime_method->code;
1094 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1099 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1100 source = mono_debug_method_lookup_location (minfo, codep);
1104 args = dump_args (inv);
1105 name = mono_method_full_name (method, TRUE);
1107 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1109 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1116 return g_string_free (str, FALSE);
1120 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1124 MonoInvocation *inv;
1127 for (i = 0, inv = top; inv; inv = inv->parent)
1128 if (inv->runtime_method != NULL)
1131 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1132 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1134 for (i = 0, inv = top; inv; inv = inv->parent)
1135 if (inv->runtime_method != NULL) {
1136 mono_array_set (res, gpointer, i, inv->runtime_method);
1138 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1146 #define MYGUINT64_MAX 18446744073709551615ULL
1147 #define MYGINT64_MAX 9223372036854775807LL
1148 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1150 #define MYGUINT32_MAX 4294967295U
1151 #define MYGINT32_MAX 2147483647
1152 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1154 #define CHECK_ADD_OVERFLOW(a,b) \
1155 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1156 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1158 #define CHECK_SUB_OVERFLOW(a,b) \
1159 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1160 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1162 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1163 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1165 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1166 (guint32)(a) < (guint32)(b) ? -1 : 0
1168 #define CHECK_ADD_OVERFLOW64(a,b) \
1169 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1170 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1172 #define CHECK_SUB_OVERFLOW64(a,b) \
1173 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1174 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1176 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1177 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1179 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1180 (guint64)(a) < (guint64)(b) ? -1 : 0
1182 #if SIZEOF_VOID_P == 4
1183 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1184 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1186 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1187 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1190 /* Resolves to TRUE if the operands would overflow */
1191 #define CHECK_MUL_OVERFLOW(a,b) \
1192 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1193 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1194 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1195 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1196 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1197 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1198 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1200 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1201 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1202 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1204 #define CHECK_MUL_OVERFLOW64(a,b) \
1205 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1206 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1207 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1208 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1209 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1210 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1211 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1213 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1214 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1215 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1217 #if SIZEOF_VOID_P == 4
1218 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1219 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1221 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1222 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1226 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1228 MonoInvocation frame;
1229 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1230 MonoObject *retval = NULL;
1231 MonoMethodSignature *sig = mono_method_signature (method);
1232 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1233 int i, type, isobject = 0;
1236 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1237 ThreadContext context_struct;
1238 MonoInvocation *old_frame = NULL;
1241 mono_error_init (error);
1246 if (context != &context_struct) {
1247 context->domain = mono_domain_get ();
1248 context->current_frame = old_frame;
1249 context->managed_code = 0;
1251 mono_native_tls_set_value (thread_context_id, NULL);
1253 *exc = (MonoObject *)frame.ex;
1257 if (context == NULL) {
1258 context = &context_struct;
1259 context_struct.base_frame = &frame;
1260 context_struct.current_frame = NULL;
1261 context_struct.env_frame = &frame;
1262 context_struct.current_env = &env;
1263 context_struct.search_for_handler = 0;
1264 context_struct.managed_code = 0;
1265 mono_native_tls_set_value (thread_context_id, context);
1268 old_frame = context->current_frame;
1270 context->domain = mono_domain_get ();
1272 switch (sig->ret->type) {
1273 case MONO_TYPE_VOID:
1275 case MONO_TYPE_STRING:
1276 case MONO_TYPE_OBJECT:
1277 case MONO_TYPE_CLASS:
1278 case MONO_TYPE_ARRAY:
1279 case MONO_TYPE_SZARRAY:
1282 case MONO_TYPE_VALUETYPE:
1283 retval = mono_object_new_checked (context->domain, klass, error);
1284 ret = ((char*)retval) + sizeof (MonoObject);
1285 if (!sig->ret->data.klass->enumtype)
1286 result.data.vt = ret;
1289 retval = mono_object_new_checked (context->domain, klass, error);
1290 ret = ((char*)retval) + sizeof (MonoObject);
1294 for (i = 0; i < sig->param_count; ++i) {
1295 if (sig->params [i]->byref) {
1296 args [i].data.p = params [i];
1299 type = sig->params [i]->type;
1304 case MONO_TYPE_BOOLEAN:
1305 args [i].data.i = *(MonoBoolean*)params [i];
1309 case MONO_TYPE_CHAR:
1310 args [i].data.i = *(gint16*)params [i];
1312 #if SIZEOF_VOID_P == 4
1313 case MONO_TYPE_U: /* use VAL_POINTER? */
1318 args [i].data.i = *(gint32*)params [i];
1320 #if SIZEOF_VOID_P == 8
1326 args [i].data.l = *(gint64*)params [i];
1328 case MONO_TYPE_VALUETYPE:
1329 if (sig->params [i]->data.klass->enumtype) {
1330 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1333 args [i].data.p = params [i];
1336 case MONO_TYPE_STRING:
1337 case MONO_TYPE_CLASS:
1338 case MONO_TYPE_ARRAY:
1339 case MONO_TYPE_SZARRAY:
1340 case MONO_TYPE_OBJECT:
1341 args [i].data.p = params [i];
1344 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1348 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1349 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1350 INIT_FRAME(&frame,context->current_frame,obj,args,&result,mono_get_root_domain (),method,error);
1352 frame.invoke_trap = 1;
1353 context->managed_code = 1;
1354 ves_exec_method_with_context (&frame, context);
1355 context->managed_code = 0;
1356 if (context == &context_struct)
1357 mono_native_tls_set_value (thread_context_id, NULL);
1359 context->current_frame = old_frame;
1360 if (frame.ex != NULL) {
1362 *exc = (MonoObject*) frame.ex;
1365 if (context->current_env != NULL) {
1366 context->env_frame->ex = frame.ex;
1367 longjmp(*context->current_env, 1);
1370 printf("dropped exception...\n");
1372 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1374 if (isobject || method->string_ctor)
1375 return result.data.p;
1376 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1381 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1383 MonoInvocation *old_frame = context->current_frame;
1384 MonoInvocation *old_env_frame = context->env_frame;
1385 jmp_buf *old_env = context->current_env;
1389 context->current_frame = old_frame;
1390 context->env_frame = old_env_frame;
1391 context->current_env = old_env;
1392 context->managed_code = 1;
1396 context->env_frame = context->current_frame;
1397 context->current_env = &env;
1398 context->managed_code = 0;
1401 case MINT_ICALL_V_V: {
1402 void (*func)() = ptr;
1406 case MINT_ICALL_V_P: {
1407 gpointer (*func)() = ptr;
1409 sp [-1].data.p = func ();
1412 case MINT_ICALL_P_V: {
1413 void (*func)(gpointer) = ptr;
1414 func (sp [-1].data.p);
1418 case MINT_ICALL_P_P: {
1419 gpointer (*func)(gpointer) = ptr;
1420 sp [-1].data.p = func (sp [-1].data.p);
1423 case MINT_ICALL_PP_V: {
1424 void (*func)(gpointer,gpointer) = ptr;
1426 func (sp [0].data.p, sp [1].data.p);
1429 case MINT_ICALL_PI_V: {
1430 void (*func)(gpointer,int) = ptr;
1432 func (sp [0].data.p, sp [1].data.i);
1435 case MINT_ICALL_PP_P: {
1436 gpointer (*func)(gpointer,gpointer) = ptr;
1438 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1441 case MINT_ICALL_PI_P: {
1442 gpointer (*func)(gpointer,int) = ptr;
1444 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1447 case MINT_ICALL_PPP_V: {
1448 void (*func)(gpointer,gpointer,gpointer) = ptr;
1450 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1453 case MINT_ICALL_PPI_V: {
1454 void (*func)(gpointer,gpointer,int) = ptr;
1456 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1460 g_assert_not_reached ();
1463 context->env_frame = old_env_frame;
1464 context->current_env = old_env;
1469 static mono_mutex_t create_method_pointer_mutex;
1471 static GHashTable *method_pointer_hash = NULL;
1474 mono_create_method_pointer (MonoMethod *method, MonoError *error)
1478 mono_error_init (error);
1480 mono_os_mutex_lock (&create_method_pointer_mutex);
1481 if (!method_pointer_hash) {
1482 // FIXME: is registering method table as GC root really necessary?
1483 // MONO_GC_REGISTER_ROOT_FIXED (method_pointer_hash);
1484 method_pointer_hash = g_hash_table_new (NULL, NULL);
1486 addr = g_hash_table_lookup (method_pointer_hash, method);
1488 mono_os_mutex_unlock (&create_method_pointer_mutex);
1493 * If it is a static P/Invoke method, we can just return the pointer
1494 * to the method implementation.
1496 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && ((MonoMethodPInvoke*) method)->addr) {
1497 ji = g_new0 (MonoJitInfo, 1);
1498 ji->d.method = method;
1500 ji->code_start = addr = ((MonoMethodPInvoke*) method)->addr;
1502 mono_jit_info_table_add (mono_get_root_domain (), ji);
1505 g_error ("FIXME: not available? figure out new API");
1507 addr = mono_arch_create_method_pointer (method);
1511 g_hash_table_insert (method_pointer_hash, method, addr);
1512 mono_os_mutex_unlock (&create_method_pointer_mutex);
1518 static int opcode_counts[512];
1520 #define COUNT_OP(op) opcode_counts[op]++
1522 #define COUNT_OP(op)
1526 #define DUMP_INSTR() \
1527 if (tracing > 1) { \
1529 if (sp > frame->stack) { \
1530 ins = dump_stack (frame->stack, sp); \
1532 ins = g_strdup (""); \
1536 g_print ("(%u) ", mono_thread_internal_current ()); \
1537 mono_interp_dis_mintop(rtm->code, ip); \
1538 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1542 #define DUMP_INSTR()
1546 #define USE_COMPUTED_GOTO 1
1548 #if USE_COMPUTED_GOTO
1549 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1550 #define MINT_IN_CASE(x) LAB_ ## x:
1552 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1554 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1556 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
1558 #define MINT_IN_SWITCH(op) switch (op)
1559 #define MINT_IN_CASE(x) case x:
1560 #define MINT_IN_BREAK break
1561 #define MINT_IN_DEFAULT default:
1565 * Defining this causes register allocation errors in some versions of gcc:
1566 * error: unable to find a register to spill in class `SIREG'
1568 /* #define MINT_USE_DEDICATED_IP_REG */
1571 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1573 MonoInvocation child_frame;
1574 GSList *finally_ips = NULL;
1575 const unsigned short *endfinally_ip = NULL;
1576 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1577 register const unsigned short *ip asm ("%esi");
1579 register const unsigned short *ip;
1581 register stackval *sp;
1584 gint tracing = global_tracing;
1585 unsigned char *vtalloc;
1588 unsigned char *vt_sp;
1589 unsigned char *locals;
1591 MonoObject *o = NULL;
1593 #if USE_COMPUTED_GOTO
1594 static void *in_labels[] = {
1595 #define OPDEF(a,b,c,d) \
1597 #include "mintops.def"
1602 frame->ex_handler = NULL;
1604 context->current_frame = frame;
1607 debug_enter (frame, &tracing);
1610 if (!frame->runtime_method->transformed) {
1611 context->managed_code = 0;
1613 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE);
1614 g_printerr ("(0x%08x) Transforming %s\n", mono_thread_internal_current (), mn);
1617 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1618 context->managed_code = 1;
1626 rtm = frame->runtime_method;
1627 frame->args = alloca (rtm->alloca_size);
1628 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1631 memset(sp, 0, rtm->stack_size);
1633 vt_sp = (unsigned char *) sp + rtm->stack_size;
1637 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1639 child_frame.parent = frame;
1645 * using while (ip < end) may result in a 15% performance drop,
1646 * but it may be useful for debug
1650 /* g_assert (sp >= frame->stack); */
1651 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1653 MINT_IN_SWITCH (*ip) {
1654 MINT_IN_CASE(MINT_INITLOCALS)
1655 memset (locals, 0, rtm->locals_size);
1658 MINT_IN_CASE(MINT_NOP)
1661 MINT_IN_CASE(MINT_BREAK)
1663 G_BREAKPOINT (); /* this is not portable... */
1665 MINT_IN_CASE(MINT_LDNULL)
1670 MINT_IN_CASE(MINT_VTRESULT) {
1671 int ret_size = * (guint16 *)(ip + 1);
1672 unsigned char *ret_vt_sp = vt_sp;
1673 vt_sp -= READ32(ip + 2);
1675 memmove (vt_sp, ret_vt_sp, ret_size);
1676 vt_sp += (ret_size + 7) & ~7;
1681 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1682 MINT_IN_CASE(MINT_LDC_I4_M1)
1685 MINT_IN_CASE(MINT_LDC_I4_0)
1688 MINT_IN_CASE(MINT_LDC_I4_1)
1691 MINT_IN_CASE(MINT_LDC_I4_2)
1694 MINT_IN_CASE(MINT_LDC_I4_3)
1697 MINT_IN_CASE(MINT_LDC_I4_4)
1700 MINT_IN_CASE(MINT_LDC_I4_5)
1703 MINT_IN_CASE(MINT_LDC_I4_6)
1706 MINT_IN_CASE(MINT_LDC_I4_7)
1709 MINT_IN_CASE(MINT_LDC_I4_8)
1712 MINT_IN_CASE(MINT_LDC_I4_S)
1713 sp->data.i = *(const short *)(ip + 1);
1717 MINT_IN_CASE(MINT_LDC_I4)
1719 sp->data.i = READ32 (ip);
1723 MINT_IN_CASE(MINT_LDC_I8)
1725 sp->data.l = READ64 (ip);
1729 MINT_IN_CASE(MINT_LDC_R4) {
1733 sp->data.f = * (float *)&val;
1738 MINT_IN_CASE(MINT_LDC_R8)
1739 sp->data.l = READ64 (ip + 1); /* note union usage */
1743 MINT_IN_CASE(MINT_DUP)
1748 MINT_IN_CASE(MINT_DUP_VT)
1749 i32 = READ32 (ip + 1);
1751 memcpy(sp->data.p, sp [-1].data.p, i32);
1752 vt_sp += (i32 + 7) & ~7;
1756 MINT_IN_CASE(MINT_POP)
1760 MINT_IN_CASE(MINT_JMP) {
1761 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1762 if (!new_method->transformed) {
1764 frame->ex = mono_interp_transform_method (new_method, context);
1769 if (new_method->alloca_size > rtm->alloca_size)
1770 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1771 rtm = frame->runtime_method = new_method;
1772 vt_sp = (unsigned char *) sp + rtm->stack_size;
1776 locals = vt_sp + rtm->vt_stack_size;
1777 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1780 MINT_IN_CASE(MINT_CALLI) {
1781 MonoMethodSignature *csignature;
1782 stackval *endsp = sp;
1786 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1790 child_frame.runtime_method = sp->data.p;
1793 child_frame.retval = sp;
1794 /* decrement by the actual number of args */
1795 sp -= csignature->param_count;
1796 child_frame.stack_args = sp;
1797 if (csignature->hasthis) {
1799 child_frame.obj = sp->data.p;
1801 child_frame.obj = NULL;
1803 if (csignature->hasthis && mono_object_is_transparent_proxy (child_frame.obj)) {
1804 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1805 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1806 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1807 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1808 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1811 ves_exec_method_with_context (&child_frame, context);
1813 context->current_frame = frame;
1815 if (child_frame.ex) {
1817 * An exception occurred, need to run finally, fault and catch handlers..
1819 frame->ex = child_frame.ex;
1820 goto handle_finally;
1823 /* need to handle typedbyref ... */
1824 if (csignature->ret->type != MONO_TYPE_VOID) {
1830 MINT_IN_CASE(MINT_CALLI_NAT) {
1831 MonoMethodSignature *csignature;
1832 stackval *endsp = sp;
1833 unsigned char *code = NULL;
1837 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1842 child_frame.runtime_method = NULL;
1845 child_frame.retval = sp;
1846 /* decrement by the actual number of args */
1847 sp -= csignature->param_count;
1848 child_frame.stack_args = sp;
1849 if (csignature->hasthis) {
1851 child_frame.obj = sp->data.p;
1853 child_frame.obj = NULL;
1855 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, context);
1857 context->current_frame = frame;
1859 if (child_frame.ex) {
1861 * An exception occurred, need to run finally, fault and catch handlers..
1863 frame->ex = child_frame.ex;
1864 if (context->search_for_handler) {
1865 context->search_for_handler = 0;
1866 goto handle_exception;
1868 goto handle_finally;
1871 /* need to handle typedbyref ... */
1872 if (csignature->ret->type != MONO_TYPE_VOID) {
1878 MINT_IN_CASE(MINT_CALL) {
1879 stackval *endsp = sp;
1883 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1886 child_frame.retval = sp;
1887 /* decrement by the actual number of args */
1888 sp -= child_frame.runtime_method->param_count;
1889 child_frame.stack_args = sp;
1890 if (child_frame.runtime_method->hasthis) {
1892 child_frame.obj = sp->data.p;
1894 child_frame.obj = NULL;
1896 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (child_frame.obj)) {
1897 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1898 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1900 ves_exec_method_with_context (&child_frame, context);
1902 context->current_frame = frame;
1904 if (child_frame.ex) {
1906 * An exception occurred, need to run finally, fault and catch handlers..
1908 frame->ex = child_frame.ex;
1909 goto handle_finally;
1912 /* need to handle typedbyref ... */
1917 MINT_IN_CASE(MINT_VCALL) {
1920 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1924 child_frame.retval = sp;
1925 /* decrement by the actual number of args */
1926 sp -= child_frame.runtime_method->param_count;
1927 child_frame.stack_args = sp;
1928 if (child_frame.runtime_method->hasthis) {
1930 child_frame.obj = sp->data.p;
1932 child_frame.obj = NULL;
1935 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && mono_object_is_transparent_proxy (child_frame.obj)) {
1936 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1937 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1940 ves_exec_method_with_context (&child_frame, context);
1942 context->current_frame = frame;
1944 if (child_frame.ex) {
1946 * An exception occurred, need to run finally, fault and catch handlers..
1948 frame->ex = child_frame.ex;
1949 goto handle_finally;
1953 MINT_IN_CASE(MINT_CALLVIRT) {
1954 stackval *endsp = sp;
1955 MonoObject *this_arg;
1960 token = * (unsigned short *)(ip + 1);
1962 child_frame.runtime_method = rtm->data_items [token];
1964 child_frame.retval = sp;
1966 /* decrement by the actual number of args */
1967 sp -= child_frame.runtime_method->param_count;
1968 child_frame.stack_args = sp;
1970 child_frame.obj = this_arg = sp->data.p;
1972 THROW_EX (mono_get_exception_null_reference(), ip - 2);
1973 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
1975 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
1976 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
1979 ves_exec_method_with_context (&child_frame, context);
1981 context->current_frame = frame;
1983 if (child_frame.ex) {
1985 * An exception occurred, need to run finally, fault and catch handlers..
1987 frame->ex = child_frame.ex;
1988 if (context->search_for_handler) {
1989 context->search_for_handler = 0;
1990 goto handle_exception;
1992 goto handle_finally;
1995 /* need to handle typedbyref ... */
2000 MINT_IN_CASE(MINT_VCALLVIRT) {
2001 MonoObject *this_arg;
2006 token = * (unsigned short *)(ip + 1);
2008 child_frame.runtime_method = rtm->data_items [token];
2010 child_frame.retval = sp;
2012 /* decrement by the actual number of args */
2013 sp -= child_frame.runtime_method->param_count;
2014 child_frame.stack_args = sp;
2016 child_frame.obj = this_arg = sp->data.p;
2018 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2019 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2021 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
2022 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
2025 ves_exec_method_with_context (&child_frame, context);
2027 context->current_frame = frame;
2029 if (child_frame.ex) {
2031 * An exception occurred, need to run finally, fault and catch handlers..
2033 frame->ex = child_frame.ex;
2034 if (context->search_for_handler) {
2035 context->search_for_handler = 0;
2036 goto handle_exception;
2038 goto handle_finally;
2042 MINT_IN_CASE(MINT_CALLRUN)
2043 ves_runtime_method (frame, context);
2046 goto handle_exception;
2049 MINT_IN_CASE(MINT_RET)
2051 *frame->retval = *sp;
2052 if (sp > frame->stack)
2053 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2055 MINT_IN_CASE(MINT_RET_VOID)
2056 if (sp > frame->stack)
2057 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
2059 MINT_IN_CASE(MINT_RET_VT)
2060 i32 = READ32(ip + 1);
2062 memcpy(frame->retval->data.p, sp->data.p, i32);
2063 if (sp > frame->stack)
2064 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2066 MINT_IN_CASE(MINT_BR_S)
2067 ip += (short) *(ip + 1);
2069 MINT_IN_CASE(MINT_BR)
2070 ip += (gint32) READ32(ip + 1);
2072 #define ZEROP_S(datamem, op) \
2074 if (sp->data.datamem op 0) \
2075 ip += * (gint16 *)(ip + 1); \
2079 #define ZEROP(datamem, op) \
2081 if (sp->data.datamem op 0) \
2082 ip += READ32(ip + 1); \
2086 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2089 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2092 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2095 MINT_IN_CASE(MINT_BRFALSE_I4)
2098 MINT_IN_CASE(MINT_BRFALSE_I8)
2101 MINT_IN_CASE(MINT_BRFALSE_R8)
2104 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2107 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2110 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2113 MINT_IN_CASE(MINT_BRTRUE_I4)
2116 MINT_IN_CASE(MINT_BRTRUE_I8)
2119 MINT_IN_CASE(MINT_BRTRUE_R8)
2122 #define CONDBR_S(cond) \
2125 ip += * (gint16 *)(ip + 1); \
2128 #define BRELOP_S(datamem, op) \
2129 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2131 #define CONDBR(cond) \
2134 ip += READ32(ip + 1); \
2138 #define BRELOP(datamem, op) \
2139 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2141 MINT_IN_CASE(MINT_BEQ_I4_S)
2144 MINT_IN_CASE(MINT_BEQ_I8_S)
2147 MINT_IN_CASE(MINT_BEQ_R8_S)
2148 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2150 MINT_IN_CASE(MINT_BEQ_I4)
2153 MINT_IN_CASE(MINT_BEQ_I8)
2156 MINT_IN_CASE(MINT_BEQ_R8)
2157 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f == sp[1].data.f)
2159 MINT_IN_CASE(MINT_BGE_I4_S)
2162 MINT_IN_CASE(MINT_BGE_I8_S)
2165 MINT_IN_CASE(MINT_BGE_R8_S)
2166 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2168 MINT_IN_CASE(MINT_BGE_I4)
2171 MINT_IN_CASE(MINT_BGE_I8)
2174 MINT_IN_CASE(MINT_BGE_R8)
2175 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f >= sp[1].data.f)
2177 MINT_IN_CASE(MINT_BGT_I4_S)
2180 MINT_IN_CASE(MINT_BGT_I8_S)
2183 MINT_IN_CASE(MINT_BGT_R8_S)
2184 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2186 MINT_IN_CASE(MINT_BGT_I4)
2189 MINT_IN_CASE(MINT_BGT_I8)
2192 MINT_IN_CASE(MINT_BGT_R8)
2193 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2195 MINT_IN_CASE(MINT_BLT_I4_S)
2198 MINT_IN_CASE(MINT_BLT_I8_S)
2201 MINT_IN_CASE(MINT_BLT_R8_S)
2202 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2204 MINT_IN_CASE(MINT_BLT_I4)
2207 MINT_IN_CASE(MINT_BLT_I8)
2210 MINT_IN_CASE(MINT_BLT_R8)
2211 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2213 MINT_IN_CASE(MINT_BLE_I4_S)
2216 MINT_IN_CASE(MINT_BLE_I8_S)
2219 MINT_IN_CASE(MINT_BLE_R8_S)
2220 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2222 MINT_IN_CASE(MINT_BLE_I4)
2225 MINT_IN_CASE(MINT_BLE_I8)
2228 MINT_IN_CASE(MINT_BLE_R8)
2229 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2231 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2234 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2237 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2238 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2240 MINT_IN_CASE(MINT_BNE_UN_I4)
2243 MINT_IN_CASE(MINT_BNE_UN_I8)
2246 MINT_IN_CASE(MINT_BNE_UN_R8)
2247 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2250 #define BRELOP_S_CAST(datamem, op, type) \
2252 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2253 ip += * (gint16 *)(ip + 1); \
2257 #define BRELOP_CAST(datamem, op, type) \
2259 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2260 ip += READ32(ip + 1); \
2264 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2265 BRELOP_S_CAST(i, >=, guint32);
2267 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2268 BRELOP_S_CAST(l, >=, guint64);
2270 MINT_IN_CASE(MINT_BGE_UN_R8_S)
2271 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2273 MINT_IN_CASE(MINT_BGE_UN_I4)
2274 BRELOP_CAST(i, >=, guint32);
2276 MINT_IN_CASE(MINT_BGE_UN_I8)
2277 BRELOP_CAST(l, >=, guint64);
2279 MINT_IN_CASE(MINT_BGE_UN_R8)
2280 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f >= sp[1].data.f)
2282 MINT_IN_CASE(MINT_BGT_UN_I4_S)
2283 BRELOP_S_CAST(i, >, guint32);
2285 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2286 BRELOP_S_CAST(l, >, guint64);
2288 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2289 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2291 MINT_IN_CASE(MINT_BGT_UN_I4)
2292 BRELOP_CAST(i, >, guint32);
2294 MINT_IN_CASE(MINT_BGT_UN_I8)
2295 BRELOP_CAST(l, >, guint64);
2297 MINT_IN_CASE(MINT_BGT_UN_R8)
2298 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2300 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2301 BRELOP_S_CAST(i, <=, guint32);
2303 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2304 BRELOP_S_CAST(l, <=, guint64);
2306 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2307 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2309 MINT_IN_CASE(MINT_BLE_UN_I4)
2310 BRELOP_CAST(i, <=, guint32);
2312 MINT_IN_CASE(MINT_BLE_UN_I8)
2313 BRELOP_CAST(l, <=, guint64);
2315 MINT_IN_CASE(MINT_BLE_UN_R8)
2316 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2318 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2319 BRELOP_S_CAST(i, <, guint32);
2321 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2322 BRELOP_S_CAST(l, <, guint64);
2324 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2325 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2327 MINT_IN_CASE(MINT_BLT_UN_I4)
2328 BRELOP_CAST(i, <, guint32);
2330 MINT_IN_CASE(MINT_BLT_UN_I8)
2331 BRELOP_CAST(l, <, guint64);
2333 MINT_IN_CASE(MINT_BLT_UN_R8)
2334 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2336 MINT_IN_CASE(MINT_SWITCH) {
2338 const unsigned short *st;
2344 if ((guint32)sp->data.i < n) {
2346 ip += 2 * (guint32)sp->data.i;
2347 offset = READ32 (ip);
2354 MINT_IN_CASE(MINT_LDIND_I1)
2356 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2358 MINT_IN_CASE(MINT_LDIND_U1)
2360 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2362 MINT_IN_CASE(MINT_LDIND_I2)
2364 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2366 MINT_IN_CASE(MINT_LDIND_U2)
2368 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2370 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2371 MINT_IN_CASE(MINT_LDIND_U4)
2373 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2375 MINT_IN_CASE(MINT_LDIND_I8)
2377 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2379 MINT_IN_CASE(MINT_LDIND_I)
2381 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2383 MINT_IN_CASE(MINT_LDIND_R4)
2385 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2387 MINT_IN_CASE(MINT_LDIND_R8)
2389 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2391 MINT_IN_CASE(MINT_LDIND_REF)
2393 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2395 MINT_IN_CASE(MINT_STIND_REF)
2398 * (gpointer *) sp->data.p = sp[1].data.p;
2400 MINT_IN_CASE(MINT_STIND_I1)
2403 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2405 MINT_IN_CASE(MINT_STIND_I2)
2408 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2410 MINT_IN_CASE(MINT_STIND_I4)
2413 * (gint32 *) sp->data.p = sp[1].data.i;
2415 MINT_IN_CASE(MINT_STIND_I)
2418 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2420 MINT_IN_CASE(MINT_STIND_I8)
2423 * (gint64 *) sp->data.p = sp[1].data.l;
2425 MINT_IN_CASE(MINT_STIND_R4)
2428 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2430 MINT_IN_CASE(MINT_STIND_R8)
2433 * (double *) sp->data.p = sp[1].data.f;
2435 #define BINOP(datamem, op) \
2437 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2439 MINT_IN_CASE(MINT_ADD_I4)
2442 MINT_IN_CASE(MINT_ADD_I8)
2445 MINT_IN_CASE(MINT_ADD_R8)
2448 MINT_IN_CASE(MINT_ADD1_I4)
2452 MINT_IN_CASE(MINT_SUB_I4)
2455 MINT_IN_CASE(MINT_SUB_I8)
2458 MINT_IN_CASE(MINT_SUB_R8)
2461 MINT_IN_CASE(MINT_SUB1_I4)
2465 MINT_IN_CASE(MINT_MUL_I4)
2468 MINT_IN_CASE(MINT_MUL_I8)
2471 MINT_IN_CASE(MINT_MUL_R8)
2474 MINT_IN_CASE(MINT_DIV_I4)
2475 if (sp [-1].data.i == 0)
2476 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2477 if (sp [-1].data.i == (-1))
2478 THROW_EX (mono_get_exception_overflow (), ip);
2481 MINT_IN_CASE(MINT_DIV_I8)
2482 if (sp [-1].data.l == 0)
2483 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2484 if (sp [-1].data.l == (-1))
2485 THROW_EX (mono_get_exception_overflow (), ip);
2488 MINT_IN_CASE(MINT_DIV_R8)
2492 #define BINOP_CAST(datamem, op, type) \
2494 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2496 MINT_IN_CASE(MINT_DIV_UN_I4)
2497 if (sp [-1].data.i == 0)
2498 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2499 BINOP_CAST(i, /, guint32);
2501 MINT_IN_CASE(MINT_DIV_UN_I8)
2502 if (sp [-1].data.l == 0)
2503 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2504 BINOP_CAST(l, /, guint64);
2506 MINT_IN_CASE(MINT_REM_I4)
2507 if (sp [-1].data.i == 0)
2508 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2511 MINT_IN_CASE(MINT_REM_I8)
2512 if (sp [-1].data.l == 0)
2513 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2516 MINT_IN_CASE(MINT_REM_R8)
2517 /* FIXME: what do we actually do here? */
2519 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2522 MINT_IN_CASE(MINT_REM_UN_I4)
2523 if (sp [-1].data.i == 0)
2524 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2525 BINOP_CAST(i, %, guint32);
2527 MINT_IN_CASE(MINT_REM_UN_I8)
2528 if (sp [-1].data.l == 0)
2529 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2530 BINOP_CAST(l, %, guint64);
2532 MINT_IN_CASE(MINT_AND_I4)
2535 MINT_IN_CASE(MINT_AND_I8)
2538 MINT_IN_CASE(MINT_OR_I4)
2541 MINT_IN_CASE(MINT_OR_I8)
2544 MINT_IN_CASE(MINT_XOR_I4)
2547 MINT_IN_CASE(MINT_XOR_I8)
2551 #define SHIFTOP(datamem, op) \
2553 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2556 MINT_IN_CASE(MINT_SHL_I4)
2559 MINT_IN_CASE(MINT_SHL_I8)
2562 MINT_IN_CASE(MINT_SHR_I4)
2565 MINT_IN_CASE(MINT_SHR_I8)
2568 MINT_IN_CASE(MINT_SHR_UN_I4)
2570 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2573 MINT_IN_CASE(MINT_SHR_UN_I8)
2575 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2578 MINT_IN_CASE(MINT_NEG_I4)
2579 sp [-1].data.i = - sp [-1].data.i;
2582 MINT_IN_CASE(MINT_NEG_I8)
2583 sp [-1].data.l = - sp [-1].data.l;
2586 MINT_IN_CASE(MINT_NEG_R8)
2587 sp [-1].data.f = - sp [-1].data.f;
2590 MINT_IN_CASE(MINT_NOT_I4)
2591 sp [-1].data.i = ~ sp [-1].data.i;
2594 MINT_IN_CASE(MINT_NOT_I8)
2595 sp [-1].data.l = ~ sp [-1].data.l;
2598 MINT_IN_CASE(MINT_CONV_I1_I4)
2599 sp [-1].data.i = (gint8)sp [-1].data.i;
2602 MINT_IN_CASE(MINT_CONV_I1_I8)
2603 sp [-1].data.i = (gint8)sp [-1].data.l;
2606 MINT_IN_CASE(MINT_CONV_I1_R8)
2607 sp [-1].data.i = (gint8)sp [-1].data.f;
2610 MINT_IN_CASE(MINT_CONV_U1_I4)
2611 sp [-1].data.i = (guint8)sp [-1].data.i;
2614 MINT_IN_CASE(MINT_CONV_U1_I8)
2615 sp [-1].data.i = (guint8)sp [-1].data.l;
2618 MINT_IN_CASE(MINT_CONV_U1_R8)
2619 sp [-1].data.i = (guint8)sp [-1].data.f;
2622 MINT_IN_CASE(MINT_CONV_I2_I4)
2623 sp [-1].data.i = (gint16)sp [-1].data.i;
2626 MINT_IN_CASE(MINT_CONV_I2_I8)
2627 sp [-1].data.i = (gint16)sp [-1].data.l;
2630 MINT_IN_CASE(MINT_CONV_I2_R8)
2631 sp [-1].data.i = (gint16)sp [-1].data.f;
2634 MINT_IN_CASE(MINT_CONV_U2_I4)
2635 sp [-1].data.i = (guint16)sp [-1].data.i;
2638 MINT_IN_CASE(MINT_CONV_U2_I8)
2639 sp [-1].data.i = (guint16)sp [-1].data.l;
2642 MINT_IN_CASE(MINT_CONV_U2_R8)
2643 sp [-1].data.i = (guint16)sp [-1].data.f;
2646 MINT_IN_CASE(MINT_CONV_I4_R8)
2647 sp [-1].data.i = (gint32)sp [-1].data.f;
2650 MINT_IN_CASE(MINT_CONV_U4_I8)
2651 MINT_IN_CASE(MINT_CONV_I4_I8)
2652 sp [-1].data.i = (gint32)sp [-1].data.l;
2655 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2656 sp [-2].data.i = (gint32)sp [-2].data.l;
2659 MINT_IN_CASE(MINT_CONV_U4_R8)
2660 sp [-1].data.i = (guint32)sp [-1].data.f;
2663 MINT_IN_CASE(MINT_CONV_I8_I4)
2664 sp [-1].data.l = sp [-1].data.i;
2667 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2668 sp [-2].data.l = sp [-2].data.i;
2671 MINT_IN_CASE(MINT_CONV_I8_U4)
2672 sp [-1].data.l = (guint32)sp [-1].data.i;
2675 MINT_IN_CASE(MINT_CONV_I8_R8)
2676 sp [-1].data.l = (gint64)sp [-1].data.f;
2679 MINT_IN_CASE(MINT_CONV_R4_I4)
2680 sp [-1].data.f = (float)sp [-1].data.i;
2683 MINT_IN_CASE(MINT_CONV_R4_I8)
2684 sp [-1].data.f = (float)sp [-1].data.l;
2687 MINT_IN_CASE(MINT_CONV_R4_R8)
2688 sp [-1].data.f = (float)sp [-1].data.f;
2691 MINT_IN_CASE(MINT_CONV_R8_I4)
2692 sp [-1].data.f = (double)sp [-1].data.i;
2695 MINT_IN_CASE(MINT_CONV_R8_I8)
2696 sp [-1].data.f = (double)sp [-1].data.l;
2699 MINT_IN_CASE(MINT_CONV_U8_I4)
2700 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2703 MINT_IN_CASE(MINT_CONV_U8_R8)
2704 sp [-1].data.l = (guint64)sp [-1].data.f;
2708 MINT_IN_CASE(MINT_CPOBJ) {
2711 vtklass = rtm->data_items[READ32 (ip)];
2714 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2718 MINT_IN_CASE(MINT_LDOBJ) {
2721 c = rtm->data_items[* (guint16 *)(ip + 1)];
2723 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2725 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2727 size = mono_class_value_size (c, NULL);
2729 sp [-1].data.p = vt_sp;
2730 memcpy(vt_sp, p, size);
2731 vt_sp += (size + 7) & ~7;
2735 MINT_IN_CASE(MINT_LDSTR)
2736 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2740 MINT_IN_CASE(MINT_NEWOBJ) {
2741 MonoClass *newobj_class;
2742 MonoMethodSignature *csig;
2743 stackval valuetype_this;
2749 token = * (guint16 *)(ip + 1);
2752 child_frame.runtime_method = rtm->data_items [token];
2753 csig = mono_method_signature (child_frame.runtime_method->method);
2754 newobj_class = child_frame.runtime_method->method->klass;
2755 /*if (profiling_classes) {
2756 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2758 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2762 if (newobj_class->parent == mono_defaults.array_class) {
2763 sp -= csig->param_count;
2764 o = ves_array_create (context->domain, newobj_class, csig, sp);
2765 goto array_constructed;
2769 * First arg is the object.
2771 if (newobj_class->valuetype) {
2772 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2773 child_frame.obj = vt_sp;
2774 valuetype_this.data.p = vt_sp;
2776 memset (&valuetype_this, 0, sizeof (stackval));
2777 child_frame.obj = &valuetype_this;
2780 if (newobj_class != mono_defaults.string_class) {
2781 context->managed_code = 0;
2782 o = mono_object_new_checked (context->domain, newobj_class, &error);
2783 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2784 context->managed_code = 1;
2785 if (*abort_requested)
2786 mono_thread_interruption_checkpoint ();
2787 child_frame.obj = o;
2789 child_frame.retval = &retval;
2793 if (csig->param_count) {
2794 sp -= csig->param_count;
2795 child_frame.stack_args = sp;
2797 child_frame.stack_args = NULL;
2800 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2802 child_frame.ip = NULL;
2803 child_frame.ex = NULL;
2805 ves_exec_method_with_context (&child_frame, context);
2807 context->current_frame = frame;
2809 if (child_frame.ex) {
2811 * An exception occurred, need to run finally, fault and catch handlers..
2813 frame->ex = child_frame.ex;
2814 goto handle_finally;
2817 * a constructor returns void, but we need to return the object we created
2820 if (newobj_class->valuetype && !newobj_class->enumtype) {
2821 *sp = valuetype_this;
2822 } else if (newobj_class == mono_defaults.string_class) {
2830 MINT_IN_CASE(MINT_CASTCLASS)
2831 c = rtm->data_items [*(guint16 *)(ip + 1)];
2832 if ((o = sp [-1].data.p)) {
2833 if (c->marshalbyref) {
2834 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2835 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2837 THROW_EX (mono_get_exception_invalid_cast (), ip);
2839 MonoVTable *vt = o->vtable;
2840 MonoClass *oklass = vt->klass;
2841 if (mono_class_is_interface (c)) {
2842 g_error ("FIXME: interface method lookup");
2843 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2844 THROW_EX (mono_get_exception_invalid_cast (), ip);
2846 } else if (c->rank) {
2847 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2848 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2850 THROW_EX (mono_get_exception_invalid_cast (), ip);
2851 } else if (!mono_class_has_parent (oklass, c)) {
2852 THROW_EX (mono_get_exception_invalid_cast (), ip);
2858 MINT_IN_CASE(MINT_ISINST)
2859 c = rtm->data_items [*(guint16 *)(ip + 1)];
2860 if ((o = sp [-1].data.p)) {
2861 if (c->marshalbyref) {
2862 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2863 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2865 sp [-1].data.p = NULL;
2867 MonoVTable *vt = o->vtable;
2868 MonoClass *oklass = vt->klass;
2869 if (mono_class_is_interface (c)) {
2870 g_error ("FIXME: interface method lookup");
2871 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2872 sp [-1].data.p = NULL;
2874 } else if (c->rank) {
2875 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2876 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2878 sp [-1].data.p = NULL;
2879 } else if (!mono_class_has_parent (oklass, c)) {
2880 sp [-1].data.p = NULL;
2886 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2887 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2890 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2891 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2894 MINT_IN_CASE(MINT_UNBOX)
2895 c = rtm->data_items[*(guint16 *)(ip + 1)];
2899 THROW_EX (mono_get_exception_null_reference (), ip);
2901 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2902 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2903 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
2904 THROW_EX (mono_get_exception_invalid_cast (), ip);
2906 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2907 g_error ("unbox: implement vt");
2909 stackval_from_data (&c->byval_arg, &sp [-1], mono_object_unbox (o), FALSE);
2913 MINT_IN_CASE(MINT_THROW)
2915 frame->ex_handler = NULL;
2917 sp->data.p = mono_get_exception_null_reference ();
2918 THROW_EX ((MonoException *)sp->data.p, ip);
2920 MINT_IN_CASE(MINT_LDFLDA)
2923 THROW_EX (mono_get_exception_null_reference (), ip);
2924 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2927 MINT_IN_CASE(MINT_CKNULL)
2930 THROW_EX (mono_get_exception_null_reference (), ip);
2934 #define LDFLD(datamem, fieldtype) \
2935 o = sp [-1].data.p; \
2937 THROW_EX (mono_get_exception_null_reference (), ip); \
2938 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2941 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2942 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2943 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2944 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2945 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
2946 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
2947 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
2948 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
2949 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
2950 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
2952 MINT_IN_CASE(MINT_LDFLD_VT)
2955 THROW_EX (mono_get_exception_null_reference (), ip);
2956 i32 = READ32(ip + 2);
2957 sp [-1].data.p = vt_sp;
2958 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
2959 vt_sp += (i32 + 7) & ~7;
2963 MINT_IN_CASE(MINT_LDRMFLD) {
2965 MonoClassField *field;
2970 THROW_EX (mono_get_exception_null_reference (), ip);
2971 field = rtm->data_items[* (guint16 *)(ip + 1)];
2973 if (mono_object_is_transparent_proxy (o)) {
2974 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2976 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
2977 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2979 addr = (char*)o + field->offset;
2982 stackval_from_data (field->type, &sp [-1], addr, FALSE);
2986 MINT_IN_CASE(MINT_LDRMFLD_VT) {
2987 MonoClassField *field;
2993 THROW_EX (mono_get_exception_null_reference (), ip);
2994 field = rtm->data_items[* (guint16 *)(ip + 1)];
2995 i32 = READ32(ip + 2);
2997 if (mono_object_is_transparent_proxy (o)) {
2998 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
2999 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3000 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3002 addr = (char*)o + field->offset;
3005 sp [-1].data.p = vt_sp;
3006 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3007 vt_sp += (i32 + 7) & ~7;
3008 memcpy(sp [-1].data.p, addr, i32);
3012 #define STFLD(datamem, fieldtype) \
3013 o = sp [-2].data.p; \
3015 THROW_EX (mono_get_exception_null_reference (), ip); \
3017 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3020 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3021 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3022 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3023 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3024 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3025 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3026 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3027 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3028 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
3029 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3031 MINT_IN_CASE(MINT_STFLD_VT)
3034 THROW_EX (mono_get_exception_null_reference (), ip);
3035 i32 = READ32(ip + 2);
3037 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3038 vt_sp -= (i32 + 7) & ~7;
3042 MINT_IN_CASE(MINT_STRMFLD) {
3043 MonoClassField *field;
3047 THROW_EX (mono_get_exception_null_reference (), ip);
3049 field = rtm->data_items[* (guint16 *)(ip + 1)];
3052 if (mono_object_is_transparent_proxy (o)) {
3053 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3054 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3055 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3057 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3062 MINT_IN_CASE(MINT_STRMFLD_VT) {
3063 MonoClassField *field;
3067 THROW_EX (mono_get_exception_null_reference (), ip);
3068 field = rtm->data_items[* (guint16 *)(ip + 1)];
3069 i32 = READ32(ip + 2);
3072 if (mono_object_is_transparent_proxy (o)) {
3073 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3074 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3075 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3077 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3080 vt_sp -= (i32 + 7) & ~7;
3083 MINT_IN_CASE(MINT_LDSFLDA) {
3084 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3085 MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
3088 if (!vt->initialized) {
3090 mono_runtime_class_init_full (vt, &error);
3091 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3095 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3096 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3098 sp->data.p = (char*)(vt->vtable) + field->offset;
3102 MINT_IN_CASE(MINT_LDSFLD) {
3104 MonoClassField *field;
3107 field = rtm->data_items[*(guint16 *)(ip + 1)];
3108 vt = rtm->data_items [*(guint16 *)(ip + 2)];
3109 if (!vt->initialized) {
3111 mono_runtime_class_init_full (vt, &error);
3112 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3115 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3116 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3118 addr = (char*)(vt->vtable) + field->offset;
3120 stackval_from_data (field->type, sp, addr, FALSE);
3124 MINT_IN_CASE(MINT_LDSFLD_I4) {
3125 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3126 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3127 if (!vt->initialized) {
3129 mono_runtime_class_init_full (vt, &error);
3130 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3133 sp->data.i = * (gint32 *)((char*)(vt->vtable) + field->offset);
3137 MINT_IN_CASE(MINT_LDSFLD_O) {
3138 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3139 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3140 if (!vt->initialized) {
3142 mono_runtime_class_init_full (vt, &error);
3143 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3146 sp->data.p = * (gpointer *)((char*)(vt->vtable) + field->offset);
3150 MINT_IN_CASE(MINT_LDSFLD_VT) {
3152 MonoClassField *field;
3157 token = * (guint16 *)(ip + 1);
3158 size = READ32(ip + 2);
3159 field = rtm->data_items[token];
3162 vt = mono_class_vtable (context->domain, field->parent);
3163 if (!vt->initialized) {
3165 mono_runtime_class_init_full (vt, &error);
3166 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3169 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3170 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3172 addr = (char*)(vt->vtable) + field->offset;
3175 vt_sp += (size + 7) & ~7;
3176 stackval_from_data (field->type, sp, addr, FALSE);
3180 MINT_IN_CASE(MINT_STSFLD) {
3182 MonoClassField *field;
3186 token = * (guint16 *)(ip + 1);
3187 field = rtm->data_items[token];
3191 vt = mono_class_vtable (context->domain, field->parent);
3192 if (!vt->initialized) {
3194 mono_runtime_class_init_full (vt, &error);
3195 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3198 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3199 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3201 addr = (char*)(vt->vtable) + field->offset;
3203 stackval_to_data (field->type, sp, addr, FALSE);
3206 MINT_IN_CASE(MINT_STSFLD_VT) {
3208 MonoClassField *field;
3213 token = * (guint16 *)(ip + 1);
3214 size = READ32(ip + 2);
3215 field = rtm->data_items[token];
3218 vt = mono_class_vtable (context->domain, field->parent);
3219 if (!vt->initialized) {
3221 mono_runtime_class_init_full (vt, &error);
3222 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3225 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3226 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3228 addr = (char*)(vt->vtable) + field->offset;
3230 stackval_to_data (field->type, sp, addr, FALSE);
3231 vt_sp -= (size + 7) & ~7;
3234 MINT_IN_CASE(MINT_STOBJ_VT) {
3236 c = rtm->data_items[* (guint16 *)(ip + 1)];
3238 size = mono_class_value_size (c, NULL);
3239 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3240 vt_sp -= (size + 7) & ~7;
3244 MINT_IN_CASE(MINT_STOBJ) {
3246 c = rtm->data_items[* (guint16 *)(ip + 1)];
3248 size = mono_class_value_size (c, NULL);
3249 memcpy(sp [-2].data.p, &sp [-1].data, size);
3253 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3254 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3255 THROW_EX (mono_get_exception_overflow (), ip);
3256 sp [-1].data.i = (guint32)sp [-1].data.f;
3259 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3260 if (sp [-1].data.i < 0)
3261 THROW_EX (mono_get_exception_overflow (), ip);
3262 sp [-1].data.l = sp [-1].data.i;
3265 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3266 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3267 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3268 THROW_EX (mono_get_exception_overflow (), ip);
3269 sp [-1].data.l = (guint64)sp [-1].data.f;
3272 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3273 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3274 THROW_EX (mono_get_exception_overflow (), ip);
3275 sp [-1].data.l = (gint64)sp [-1].data.f;
3278 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3279 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3280 THROW_EX (mono_get_exception_overflow (), ip);
3281 sp [-1].data.i = (mono_u)sp [-1].data.l;
3284 MINT_IN_CASE(MINT_BOX)
3285 c = rtm->data_items [* (guint16 *)(ip + 1)];
3287 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3288 int size = mono_class_value_size (c, NULL);
3289 sp [-1].data.p = mono_value_box_checked (context->domain, c, sp [-1].data.p, &error);
3290 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3291 size = (size + 7) & ~7;
3295 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3296 sp [-1].data.p = mono_value_box_checked (context->domain, c, &sp [-1], &error);
3297 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3301 MINT_IN_CASE(MINT_NEWARR)
3302 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3303 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3305 /*if (profiling_classes) {
3306 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3308 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3312 MINT_IN_CASE(MINT_LDLEN)
3315 THROW_EX (mono_get_exception_null_reference (), ip);
3316 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3319 MINT_IN_CASE(MINT_GETCHR) {
3323 THROW_EX (mono_get_exception_null_reference (), ip);
3324 i32 = sp [-1].data.i;
3325 if (i32 < 0 || i32 >= mono_string_length (s))
3326 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3328 sp [-1].data.i = mono_string_chars(s)[i32];
3332 MINT_IN_CASE(MINT_STRLEN)
3334 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3336 MINT_IN_CASE(MINT_ARRAY_RANK)
3339 THROW_EX (mono_get_exception_null_reference (), ip);
3340 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3343 MINT_IN_CASE(MINT_LDELEMA) {
3347 /*token = READ32 (ip)*/;
3353 aindex = sp [1].data.i;
3354 if (aindex >= mono_array_length ((MonoArray *) o))
3355 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3357 /* check the array element corresponds to token */
3358 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3360 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3364 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3365 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3366 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3367 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3368 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3369 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3370 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3371 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3372 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3373 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3374 MINT_IN_CASE(MINT_LDELEM_REF) {
3382 THROW_EX (mono_get_exception_null_reference (), ip);
3384 aindex = sp [1].data.i;
3385 if (aindex >= mono_array_length (o))
3386 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3389 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3392 case MINT_LDELEM_I1:
3393 sp [0].data.i = mono_array_get (o, gint8, aindex);
3395 case MINT_LDELEM_U1:
3396 sp [0].data.i = mono_array_get (o, guint8, aindex);
3398 case MINT_LDELEM_I2:
3399 sp [0].data.i = mono_array_get (o, gint16, aindex);
3401 case MINT_LDELEM_U2:
3402 sp [0].data.i = mono_array_get (o, guint16, aindex);
3405 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3407 case MINT_LDELEM_I4:
3408 sp [0].data.i = mono_array_get (o, gint32, aindex);
3410 case MINT_LDELEM_U4:
3411 sp [0].data.i = mono_array_get (o, guint32, aindex);
3413 case MINT_LDELEM_I8:
3414 sp [0].data.l = mono_array_get (o, guint64, aindex);
3416 case MINT_LDELEM_R4:
3417 sp [0].data.f = mono_array_get (o, float, aindex);
3419 case MINT_LDELEM_R8:
3420 sp [0].data.f = mono_array_get (o, double, aindex);
3422 case MINT_LDELEM_REF:
3423 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3433 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3434 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3435 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3436 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3437 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3438 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3439 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3440 MINT_IN_CASE(MINT_STELEM_REF) {
3447 THROW_EX (mono_get_exception_null_reference (), ip);
3449 aindex = sp [1].data.i;
3450 if (aindex >= mono_array_length ((MonoArray *)o))
3451 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3455 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3457 case MINT_STELEM_I1:
3458 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3460 case MINT_STELEM_I2:
3461 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3463 case MINT_STELEM_I4:
3464 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3466 case MINT_STELEM_I8:
3467 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3469 case MINT_STELEM_R4:
3470 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3472 case MINT_STELEM_R8:
3473 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3475 case MINT_STELEM_REF: {
3476 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3477 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3478 if (sp [2].data.p && !isinst_obj)
3479 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3480 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3489 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3490 if (sp [-1].data.i < 0)
3491 THROW_EX (mono_get_exception_overflow (), ip);
3494 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3495 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3496 THROW_EX (mono_get_exception_overflow (), ip);
3497 sp [-1].data.i = (gint32) sp [-1].data.l;
3500 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3501 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3502 THROW_EX (mono_get_exception_overflow (), ip);
3503 sp [-1].data.i = (gint32) sp [-1].data.f;
3506 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3507 if (sp [-1].data.i < 0)
3508 THROW_EX (mono_get_exception_overflow (), ip);
3511 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3512 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3513 THROW_EX (mono_get_exception_overflow (), ip);
3514 sp [-1].data.i = (guint32) sp [-1].data.l;
3517 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3518 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3519 THROW_EX (mono_get_exception_overflow (), ip);
3520 sp [-1].data.i = (guint32) sp [-1].data.f;
3523 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3524 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3525 THROW_EX (mono_get_exception_overflow (), ip);
3528 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3529 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3530 THROW_EX (mono_get_exception_overflow (), ip);
3531 sp [-1].data.i = (gint16) sp [-1].data.l;
3534 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3535 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3536 THROW_EX (mono_get_exception_overflow (), ip);
3537 sp [-1].data.i = (gint16) sp [-1].data.f;
3540 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3541 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3542 THROW_EX (mono_get_exception_overflow (), ip);
3545 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3546 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3547 THROW_EX (mono_get_exception_overflow (), ip);
3548 sp [-1].data.i = (guint16) sp [-1].data.l;
3551 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3552 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3553 THROW_EX (mono_get_exception_overflow (), ip);
3554 sp [-1].data.i = (guint16) sp [-1].data.f;
3557 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3558 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3559 THROW_EX (mono_get_exception_overflow (), ip);
3562 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3563 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3564 THROW_EX (mono_get_exception_overflow (), ip);
3565 sp [-1].data.i = (gint8) sp [-1].data.l;
3568 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3569 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3570 THROW_EX (mono_get_exception_overflow (), ip);
3571 sp [-1].data.i = (gint8) sp [-1].data.f;
3574 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3575 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3576 THROW_EX (mono_get_exception_overflow (), ip);
3579 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3580 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3581 THROW_EX (mono_get_exception_overflow (), ip);
3582 sp [-1].data.i = (guint8) sp [-1].data.l;
3585 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3586 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3587 THROW_EX (mono_get_exception_overflow (), ip);
3588 sp [-1].data.i = (guint8) sp [-1].data.f;
3592 MINT_IN_CASE(MINT_LDELEM)
3593 MINT_IN_CASE(MINT_STELEM)
3594 MINT_IN_CASE(MINT_UNBOX_ANY)
3596 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3598 MINT_IN_CASE(MINT_CKFINITE)
3599 if (!isfinite(sp [-1].data.f))
3600 THROW_EX (mono_get_exception_arithmetic (), ip);
3604 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3606 MINT_IN_CASE(MINT_LDTOKEN)
3609 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3613 MINT_IN_CASE(MINT_ADD_OVF_I4)
3614 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3615 THROW_EX (mono_get_exception_overflow (), ip);
3618 MINT_IN_CASE(MINT_ADD_OVF_I8)
3619 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3620 THROW_EX (mono_get_exception_overflow (), ip);
3623 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3624 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3625 THROW_EX (mono_get_exception_overflow (), ip);
3626 BINOP_CAST(i, +, guint32);
3628 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3629 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3630 THROW_EX (mono_get_exception_overflow (), ip);
3631 BINOP_CAST(l, +, guint64);
3633 MINT_IN_CASE(MINT_MUL_OVF_I4)
3634 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3635 THROW_EX (mono_get_exception_overflow (), ip);
3638 MINT_IN_CASE(MINT_MUL_OVF_I8)
3639 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3640 THROW_EX (mono_get_exception_overflow (), ip);
3643 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3644 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3645 THROW_EX (mono_get_exception_overflow (), ip);
3646 BINOP_CAST(i, *, guint32);
3648 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3649 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3650 THROW_EX (mono_get_exception_overflow (), ip);
3651 BINOP_CAST(l, *, guint64);
3653 MINT_IN_CASE(MINT_SUB_OVF_I4)
3654 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3655 THROW_EX (mono_get_exception_overflow (), ip);
3658 MINT_IN_CASE(MINT_SUB_OVF_I8)
3659 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3660 THROW_EX (mono_get_exception_overflow (), ip);
3663 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3664 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3665 THROW_EX (mono_get_exception_overflow (), ip);
3666 BINOP_CAST(i, -, guint32);
3668 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3669 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3670 THROW_EX (mono_get_exception_overflow (), ip);
3671 BINOP_CAST(l, -, guint64);
3673 MINT_IN_CASE(MINT_ENDFINALLY)
3675 ip = finally_ips->data;
3676 finally_ips = g_slist_remove (finally_ips, ip);
3683 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3684 MINT_IN_CASE(MINT_LEAVE_S)
3685 while (sp > frame->stack) {
3689 if (*ip == MINT_LEAVE_S) {
3690 ip += (short) *(ip + 1);
3692 ip += (gint32) READ32 (ip + 1);
3695 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3696 frame->ex_handler = NULL;
3699 goto handle_finally;
3701 MINT_IN_CASE(MINT_ICALL_V_V)
3702 MINT_IN_CASE(MINT_ICALL_V_P)
3703 MINT_IN_CASE(MINT_ICALL_P_V)
3704 MINT_IN_CASE(MINT_ICALL_P_P)
3705 MINT_IN_CASE(MINT_ICALL_PP_V)
3706 MINT_IN_CASE(MINT_ICALL_PI_V)
3707 MINT_IN_CASE(MINT_ICALL_PP_P)
3708 MINT_IN_CASE(MINT_ICALL_PI_P)
3709 MINT_IN_CASE(MINT_ICALL_PPP_V)
3710 MINT_IN_CASE(MINT_ICALL_PPI_V)
3711 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3712 if (frame->ex != NULL)
3713 goto handle_exception;
3716 MINT_IN_CASE(MINT_MONO_LDPTR)
3717 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3721 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3722 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3723 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3727 MINT_IN_CASE(MINT_MONO_FREE)
3730 g_error ("that doesn't seem right");
3731 g_free (sp->data.p);
3733 MINT_IN_CASE(MINT_MONO_RETOBJ)
3736 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3737 mono_method_signature (frame->runtime_method->method)->pinvoke);
3738 if (sp > frame->stack)
3739 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3742 #define RELOP(datamem, op) \
3744 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3746 MINT_IN_CASE(MINT_CEQ_I4)
3749 MINT_IN_CASE(MINT_CEQ0_I4)
3750 sp [-1].data.i = (sp [-1].data.i == 0);
3753 MINT_IN_CASE(MINT_CEQ_I8)
3756 MINT_IN_CASE(MINT_CEQ_R8)
3758 if (isunordered (sp [-1].data.f, sp [0].data.f))
3761 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3764 MINT_IN_CASE(MINT_CGT_I4)
3767 MINT_IN_CASE(MINT_CGT_I8)
3770 MINT_IN_CASE(MINT_CGT_R8)
3772 if (isunordered (sp [-1].data.f, sp [0].data.f))
3775 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3779 #define RELOP_CAST(datamem, op, type) \
3781 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3784 MINT_IN_CASE(MINT_CGT_UN_I4)
3785 RELOP_CAST(i, >, guint32);
3787 MINT_IN_CASE(MINT_CGT_UN_I8)
3788 RELOP_CAST(l, >, guint64);
3790 MINT_IN_CASE(MINT_CGT_UN_R8)
3792 if (isunordered (sp [-1].data.f, sp [0].data.f))
3795 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3798 MINT_IN_CASE(MINT_CLT_I4)
3801 MINT_IN_CASE(MINT_CLT_I8)
3804 MINT_IN_CASE(MINT_CLT_R8)
3806 if (isunordered (sp [-1].data.f, sp [0].data.f))
3809 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3812 MINT_IN_CASE(MINT_CLT_UN_I4)
3813 RELOP_CAST(i, <, guint32);
3815 MINT_IN_CASE(MINT_CLT_UN_I8)
3816 RELOP_CAST(l, <, guint64);
3818 MINT_IN_CASE(MINT_CLT_UN_R8)
3820 if (isunordered (sp [-1].data.f, sp [0].data.f))
3823 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3826 MINT_IN_CASE(MINT_LDFTN) {
3827 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3832 MINT_IN_CASE(MINT_LDVIRTFTN) {
3833 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3837 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3839 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3844 MINT_IN_CASE(MINT_LDTHIS)
3845 sp->data.p = frame->obj;
3849 MINT_IN_CASE(MINT_STTHIS)
3851 frame->obj = sp->data.p;
3854 MINT_IN_CASE(MINT_LDTHISA)
3855 sp->data.p = &frame->obj;
3860 #define LDARG(datamem, argtype) \
3861 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3865 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3866 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3867 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3868 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3869 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3870 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3871 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3872 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3873 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3874 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3876 MINT_IN_CASE(MINT_LDARG_VT)
3878 i32 = READ32(ip + 2);
3879 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3880 vt_sp += (i32 + 7) & ~7;
3885 #define STARG(datamem, argtype) \
3887 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3890 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3891 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3892 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3893 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3894 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3895 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3896 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3897 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3898 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3899 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3901 MINT_IN_CASE(MINT_STARG_VT)
3902 i32 = READ32(ip + 2);
3904 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3905 vt_sp -= (i32 + 7) & ~7;
3909 #define STINARG(datamem, argtype) \
3911 int n = * (guint16 *)(ip + 1); \
3912 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3916 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3917 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3918 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3919 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3920 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3921 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3922 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3923 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3924 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3925 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3927 MINT_IN_CASE(MINT_STINARG_VT) {
3928 int n = * (guint16 *)(ip + 1);
3929 i32 = READ32(ip + 2);
3930 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3935 MINT_IN_CASE(MINT_LDARGA)
3936 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3941 #define LDLOC(datamem, argtype) \
3942 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3946 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
3947 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
3948 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
3949 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
3950 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
3951 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
3952 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
3953 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
3954 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
3955 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
3957 MINT_IN_CASE(MINT_LDLOC_VT)
3959 i32 = READ32(ip + 2);
3960 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
3961 vt_sp += (i32 + 7) & ~7;
3966 MINT_IN_CASE(MINT_LDLOCA_S)
3967 sp->data.p = locals + * (guint16 *)(ip + 1);
3972 #define STLOC(datamem, argtype) \
3974 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3977 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
3978 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
3979 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
3980 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
3981 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
3982 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
3983 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
3984 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
3985 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
3986 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
3988 #define STLOC_NP(datamem, argtype) \
3989 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
3992 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
3993 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
3995 MINT_IN_CASE(MINT_STLOC_VT)
3996 i32 = READ32(ip + 2);
3998 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
3999 vt_sp -= (i32 + 7) & ~7;
4003 MINT_IN_CASE(MINT_LOCALLOC)
4004 if (sp != frame->stack + 1) /*FIX?*/
4005 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
4006 sp [-1].data.p = alloca (sp [-1].data.i);
4010 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
4012 MINT_IN_CASE(MINT_INITOBJ)
4014 memset (sp->data.vt, 0, READ32(ip + 1));
4017 MINT_IN_CASE(MINT_CPBLK)
4019 if (!sp [0].data.p || !sp [1].data.p)
4020 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4022 /* FIXME: value and size may be int64... */
4023 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4026 MINT_IN_CASE(MINT_CONSTRAINED_) {
4028 /* FIXME: implement */
4030 token = READ32 (ip);
4035 MINT_IN_CASE(MINT_INITBLK)
4038 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4040 /* FIXME: value and size may be int64... */
4041 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4044 MINT_IN_CASE(MINT_NO_)
4045 /* FIXME: implement */
4049 MINT_IN_CASE(MINT_RETHROW)
4051 * need to clarify what this should actually do:
4052 * start the search from the last found handler in
4053 * this method or continue in the caller or what.
4054 * Also, do we need to run finally/fault handlers after a retrow?
4055 * Well, this implementation will follow the usual search
4056 * for an handler, considering the current ip as throw spot.
4057 * We need to NULL frame->ex_handler for the later code to
4058 * actually run the new found handler.
4060 frame->ex_handler = NULL;
4061 THROW_EX (frame->ex, ip - 1);
4064 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4065 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4069 g_assert_not_reached ();
4071 * Exception handling code.
4072 * The exception object is stored in frame->ex.
4079 MonoInvocation *inv;
4080 MonoExceptionClause *clause;
4086 g_print ("* Handling exception '%s' at IL_%04x\n",
4087 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
4088 rtm == NULL ? 0 : frame->ip - rtm->code);
4090 if (die_on_exception)
4093 for (inv = frame; inv; inv = inv->parent) {
4095 if (inv->runtime_method == NULL)
4097 method = inv->runtime_method->method;
4098 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4100 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4102 if (inv->ip == NULL)
4104 ip_offset = inv->ip - inv->runtime_method->code;
4105 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
4106 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
4107 clause = &inv->runtime_method->clauses [i];
4108 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4109 if (!clause->flags) {
4110 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4111 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4114 * OK, we found an handler, now we need to execute the finally
4115 * and fault blocks before branching to the handler code.
4117 inv->ex_handler = clause;
4120 g_print ("* Found handler at '%s'\n", method->name);
4122 goto handle_finally;
4125 /* FIXME: handle filter clauses */
4132 * If we get here, no handler was found: print a stack trace.
4134 for (inv = frame; inv; inv = inv->parent) {
4135 if (inv->invoke_trap)
4136 goto handle_finally;
4139 ex_obj = (MonoObject*)frame->ex;
4140 mono_unhandled_exception (ex_obj);
4147 MonoExceptionClause *clause;
4148 GSList *old_list = finally_ips;
4149 MonoMethod *method = frame->runtime_method->method;
4150 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4151 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4155 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4157 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4158 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4161 ip_offset = frame->ip - rtm->code;
4163 if (endfinally_ip != NULL)
4164 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4165 for (i = 0; i < header->num_clauses; ++i)
4166 if (frame->ex_handler == &rtm->clauses [i])
4170 clause = &rtm->clauses [i];
4171 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4172 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4173 ip = rtm->code + clause->handler_offset;
4174 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4177 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4183 endfinally_ip = NULL;
4185 if (old_list != finally_ips && finally_ips) {
4186 ip = finally_ips->data;
4187 finally_ips = g_slist_remove (finally_ips, ip);
4188 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4193 * If an exception is set, we need to execute the fault handler, too,
4194 * otherwise, we continue normally.
4204 MonoExceptionClause *clause;
4205 MonoMethod *method = frame->runtime_method->method;
4206 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4207 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4211 g_print ("* Handle fault\n");
4213 ip_offset = frame->ip - rtm->code;
4214 for (i = 0; i < header->num_clauses; ++i) {
4215 clause = &rtm->clauses [i];
4216 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4217 ip = rtm->code + clause->handler_offset;
4220 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4226 * If the handler for the exception was found in this method, we jump
4227 * to it right away, otherwise we return and let the caller run
4228 * the finally, fault and catch blocks.
4229 * This same code should be present in the endfault opcode, but it
4230 * is corrently not assigned in the ECMA specs: LAMESPEC.
4232 if (frame->ex_handler) {
4235 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4237 ip = rtm->code + frame->ex_handler->handler_offset;
4239 vt_sp = (unsigned char *) sp + rtm->stack_size;
4240 sp->data.p = frame->ex;
4251 ves_exec_method (MonoInvocation *frame)
4253 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4254 ThreadContext context_struct;
4261 mono_unhandled_exception ((MonoObject*)frame->ex);
4264 if (context == NULL) {
4265 context = &context_struct;
4266 context_struct.domain = mono_domain_get ();
4267 context_struct.base_frame = frame;
4268 context_struct.current_frame = NULL;
4269 context_struct.env_frame = frame;
4270 context_struct.current_env = &env;
4271 context_struct.search_for_handler = 0;
4272 context_struct.managed_code = 0;
4273 mono_native_tls_set_value (thread_context_id, context);
4276 frame->parent = context->current_frame;
4277 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4278 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4279 context->managed_code = 1;
4280 ves_exec_method_with_context (frame, context);
4281 context->managed_code = 0;
4283 if (context != &context_struct && context->current_env) {
4284 context->env_frame->ex = frame->ex;
4285 longjmp (*context->current_env, 1);
4288 mono_unhandled_exception ((MonoObject*)frame->ex);
4290 if (context->base_frame == frame)
4291 mono_native_tls_set_value (thread_context_id, NULL);
4293 context->current_frame = frame->parent;
4297 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4299 MonoImage *image = mono_assembly_get_image (assembly);
4304 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4305 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4308 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4310 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4311 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4319 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4320 "Usage is: mint [options] executable args...\n\n", VERSION);
4322 "Runtime Debugging:\n"
4327 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4330 " --traceclassinit\n"
4333 " --debug method_name\n"
4340 " --config filename load the specified config file instead of the default\n"
4341 " --workers n maximum number of worker threads\n"
4347 add_signal_handler (int signo, void (*handler)(int))
4350 signal (signo, handler);
4352 struct sigaction sa;
4354 sa.sa_handler = handler;
4355 sigemptyset (&sa.sa_mask);
4358 g_assert (sigaction (signo, &sa, NULL) != -1);
4363 segv_handler (int signum)
4365 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4366 MonoException *segv_exception;
4368 if (context == NULL)
4370 segv_exception = mono_get_exception_null_reference ();
4371 segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4372 mono_raise_exception (segv_exception);
4377 quit_handler (int signum)
4379 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4380 MonoException *quit_exception;
4382 if (context == NULL)
4384 quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4385 mono_raise_exception (quit_exception);
4389 abrt_handler (int signum)
4391 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4392 MonoException *abrt_exception;
4394 if (context == NULL)
4396 abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4397 mono_raise_exception (abrt_exception);
4402 thread_abort_handler (int signum)
4404 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4407 if (context == NULL)
4410 exc = mono_thread_request_interruption (context->managed_code);
4411 if (exc) mono_raise_exception (exc);
4416 interp_ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4417 MonoReflectionMethod **method,
4418 gint32 *iloffset, gint32 *native_offset,
4419 MonoString **file, gint32 *line, gint32 *column)
4421 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4422 MonoInvocation *inv = context->current_frame;
4426 for (i = 0; inv && i < skip; inv = inv->parent)
4427 if (inv->runtime_method != NULL)
4438 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4439 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4444 if (need_file_info) {
4448 *file = mono_string_new (mono_domain_get (), "unknown");
4455 interp_ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4457 MonoDomain *domain = mono_domain_get ();
4459 MonoArray *ta = exc->trace_ips;
4464 /* Exception is not thrown yet */
4465 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4466 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4470 len = mono_array_length (ta);
4472 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4473 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4475 for (i = skip; i < len / 2; i++) {
4476 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4477 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4478 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4479 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4482 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4483 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4484 sf->native_offset = ip - rtm->code;
4488 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4490 if (need_file_info) {
4493 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4495 sf->filename = filename? mono_string_new (domain, filename): NULL;
4502 mono_array_set (res, gpointer, i, sf);
4509 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4511 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4512 MonoObject *delegate;
4515 g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4517 delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4518 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4520 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4521 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4530 int enable_debugging;
4536 static void main_thread_handler (gpointer user_data)
4538 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4539 MonoAssembly *assembly;
4541 if (main_args->enable_debugging) {
4542 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4545 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
4548 fprintf (stderr, "Can not open image %s\n", main_args->file);
4552 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4556 interp_mono_runtime_install_handlers (void)
4558 add_signal_handler (SIGSEGV, segv_handler);
4559 add_signal_handler (SIGINT, quit_handler);
4560 add_signal_handler (SIGABRT, abrt_handler);
4562 add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4567 quit_function (MonoDomain *domain, gpointer user_data)
4569 mono_profiler_shutdown ();
4571 mono_runtime_cleanup (domain);
4572 mono_domain_free (domain, TRUE);
4577 mono_interp_cleanup(MonoDomain *domain)
4579 quit_function (domain, NULL);
4583 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4585 return ves_exec (domain, assembly, argc, argv);
4589 interp_get_imt_trampoline (MonoVTable *vtable, int imt_slot_index)
4591 // FIXME: implement me
4596 interp_create_ftnptr (MonoDomain *domain, gpointer addr)
4598 // FIXME: true on all arch?
4604 mono_current_thread_has_handle_block_guard (void)
4611 mono_above_abort_threshold (void)
4617 mono_interp_init(const char *file)
4620 MonoRuntimeCallbacks callbacks;
4621 MonoRuntimeExceptionHandlingCallbacks ecallbacks;
4624 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4625 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4627 mono_native_tls_alloc (&thread_context_id, NULL);
4628 mono_native_tls_set_value (thread_context_id, NULL);
4629 mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4630 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4632 mono_tls_init_runtime_keys ();
4634 // TODO: use callbacks?
4635 interp_mono_runtime_install_handlers ();
4636 mono_interp_transform_init ();
4638 memset (&callbacks, 0, sizeof (callbacks));
4639 // TODO: replace with `mono_install_callbacks`
4640 callbacks.compile_method = mono_create_method_pointer;
4641 callbacks.runtime_invoke = interp_mono_runtime_invoke;
4642 callbacks.get_imt_trampoline = interp_get_imt_trampoline;
4643 callbacks.create_ftnptr = interp_create_ftnptr;
4644 #ifndef DISABLE_REMOTING
4645 callbacks.create_remoting_trampoline = interp_create_remoting_trampoline;
4647 callbacks.create_jit_trampoline = interp_create_trampoline;
4648 mono_install_callbacks (&callbacks);
4651 memset (&ecallbacks, 0, sizeof (ecallbacks));
4652 ecallbacks.mono_raise_exception = interp_ex_handler;
4653 ecallbacks.mono_current_thread_has_handle_block_guard = mono_current_thread_has_handle_block_guard;
4654 ecallbacks.mono_above_abort_threshold = mono_above_abort_threshold;
4657 mono_install_stack_walk (interp_walk_stack);
4659 mono_install_eh_callbacks (&ecallbacks);
4661 mono_install_runtime_cleanup (quit_function);
4663 abort_requested = mono_thread_interruption_request_flag ();
4665 domain = mono_init_from_assembly (file, file);
4666 #ifdef __hpux /* generates very big stack frames */
4667 mono_threads_set_default_stacksize(32*1024*1024);
4670 /* TODO: this should use ves_icall_get_frame from mini-exceptions.c? */
4671 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", interp_ves_icall_get_frame_info);
4672 /* TODO: this should use ves_icall_get_trace from mini-exceptions.c? */
4673 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", interp_ves_icall_get_trace);
4674 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", interp_mono_runtime_install_handlers);
4675 mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4677 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", mono_create_icall_signature ("object"), FALSE);
4679 mono_runtime_init_checked (domain, NULL, NULL, &error);
4680 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4682 mono_thread_attach (domain);
4686 typedef int (*TestMethod) (void);
4689 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4691 int result, expected, failed, cfailed, run;
4692 double elapsed, transform_time;
4694 MonoObject *result_obj;
4695 static gboolean filter_method_init = FALSE;
4696 static const char *filter_method = NULL;
4698 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4699 cfailed = failed = run = 0;
4700 transform_time = elapsed = 0.0;
4703 /* fixme: ugly hack - delete all previously compiled methods */
4704 if (domain_jit_info (domain)) {
4705 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4706 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4707 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4708 mono_jit_code_hash_init (&(domain->jit_code_hash));
4712 g_timer_start (timer);
4713 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4715 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4717 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4721 if (!filter_method_init) {
4722 filter_method = g_getenv ("INTERP_FILTER_METHOD");
4723 filter_method_init = TRUE;
4725 gboolean filter = FALSE;
4726 if (filter_method) {
4727 const char *name = filter_method;
4729 if ((strchr (name, '.') > name) || strchr (name, ':')) {
4730 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
4731 filter = mono_method_desc_full_match (desc, method);
4732 mono_method_desc_free (desc);
4734 filter = strcmp (method->name, name) == 0;
4736 } else { // no filter
4739 if (strncmp (method->name, "test_", 5) == 0 && filter) {
4740 MonoError interp_error;
4741 MonoObject *exc = NULL;
4743 result_obj = interp_mono_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
4744 if (!mono_error_ok (&interp_error)) {
4746 g_print ("Test '%s' execution failed.\n", method->name);
4747 } else if (exc != NULL) {
4748 g_print ("Exception in Test '%s' occured:\n", method->name);
4749 mono_object_describe (exc);
4753 result = *(gint32 *) mono_object_unbox (result_obj);
4754 expected = atoi (method->name + 5); // FIXME: oh no.
4757 if (result != expected) {
4759 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4764 g_timer_stop (timer);
4765 elapsed = g_timer_elapsed (timer, NULL);
4766 if (failed > 0 || cfailed > 0){
4767 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4768 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4770 g_print ("Results: total tests: %d, all pass \n", run);
4773 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4774 elapsed - transform_time, transform_time);
4775 *total += failed + cfailed;
4779 interp_regression (MonoImage *image, int verbose, int *total_run)
4782 GTimer *timer = g_timer_new ();
4783 MonoDomain *domain = mono_domain_get ();
4787 /* load the metadata */
4788 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4790 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4792 mono_error_cleanup (&error);
4795 mono_class_init (method->klass);
4800 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4802 g_timer_destroy (timer);
4807 interp_regression_list (int verbose, int count, char *images [])
4809 int i, total, total_run, run;
4811 total_run = total = 0;
4812 for (i = 0; i < count; ++i) {
4813 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4815 g_warning ("failed to load assembly: %s", images [i]);
4818 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4822 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4824 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);