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-ms.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 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
69 #include "mono/cil/opcode.def"
74 // TODO: should come from mono/mini/mini.h
77 * Information about a trampoline function.
82 * The native code of the trampoline. Not owned by this structure.
87 * The name of the trampoline which can be used in AOT/xdebug. Owned by this
92 * Patches required by the trampoline when aot-ing. Owned by this structure.
96 * Unwind information. Owned by this structure.
101 * Encoded unwind info loaded from AOT images
106 gpointer mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info);
107 void mono_tramp_info_register (MonoTrampInfo *info, MonoDomain *domain);
109 #include <mono/mini/mini.h>
113 /* Mingw 2.1 doesnt need this any more, but leave it in for now for older versions */
116 #define finite _finite
120 #define finite isfinite
124 static gint *abort_requested;
126 /* If true, then we output the opcodes as we interpret them */
127 static int global_tracing = 1;
128 static int global_no_pointers = 0;
130 int mono_interp_traceopt = 0;
132 static int debug_indent_level = 0;
134 #define INIT_FRAME(frame,parent_frame,obj_this,method_args,method_retval,domain,mono_method,error) \
136 (frame)->parent = (parent_frame); \
137 (frame)->obj = (obj_this); \
138 (frame)->stack_args = (method_args); \
139 (frame)->retval = (method_retval); \
140 (frame)->runtime_method = mono_interp_get_runtime_method ((domain), (mono_method), (error)); \
141 (frame)->ex = NULL; \
142 (frame)->ip = NULL; \
143 (frame)->invoke_trap = 0; \
146 void ves_exec_method (MonoInvocation *frame);
148 static char* dump_stack (stackval *stack, stackval *sp);
149 static char* dump_frame (MonoInvocation *inv);
150 static MonoArray *get_trace_ips (MonoDomain *domain, MonoInvocation *top);
151 static void ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context);
153 typedef void (*ICallMethod) (MonoInvocation *frame);
155 static guint32 die_on_exception = 0;
156 static MonoNativeTlsKey thread_context_id;
158 static char* dump_args (MonoInvocation *inv);
160 #define DEBUG_INTERP 0
164 static int break_on_method = 0;
165 static int nested_trace = 0;
166 static GList *db_methods = NULL;
173 for (h = 0; h < debug_indent_level; h++)
178 db_match_method (gpointer data, gpointer user_data)
180 MonoMethod *m = (MonoMethod*)user_data;
181 MonoMethodDesc *desc = data;
183 if (mono_method_desc_full_match (desc, m))
187 static void debug_enter (MonoInvocation *frame, int *tracing)
190 g_list_foreach (db_methods, db_match_method, (gpointer)frame->runtime_method->method);
192 *tracing = nested_trace ? (global_tracing = 2, 3) : 2;
196 MonoMethod *method = frame->runtime_method->method;
197 char *mn, *args = dump_args (frame);
198 debug_indent_level++;
200 mn = mono_method_full_name (method, FALSE);
201 g_printerr ("(0x%08x) Entering %s (", mono_thread_internal_current (), mn);
203 g_printerr ("%s)\n", args);
206 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE)
207 mono_profiler_method_enter (frame->runtime_method->method);
211 #define DEBUG_LEAVE() \
214 args = dump_retval (frame); \
216 mn = mono_method_full_name (frame->runtime_method->method, FALSE); \
217 g_printerr ("(0x%08x) Leaving %s", mono_thread_internal_current (), mn); \
219 g_printerr (" => %s\n", args); \
221 debug_indent_level--; \
222 if (tracing == 3) global_tracing = 0; \
224 if (mono_profiler_events & MONO_PROFILE_ENTER_LEAVE) \
225 mono_profiler_method_leave (frame->runtime_method->method);
229 static void debug_enter (MonoInvocation *frame, int *tracing)
232 #define DEBUG_LEAVE()
237 interp_ex_handler (MonoException *ex) {
239 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
243 stack_trace = dump_frame (context->current_frame);
244 ex->stack_trace = mono_string_new (mono_domain_get(), stack_trace);
245 g_free (stack_trace);
246 if (context->current_env == NULL || strcmp(ex->object.vtable->klass->name, "ExecutionEngineException") == 0) {
247 char *strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
248 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
249 fprintf(stderr, "Nothing can catch this exception: ");
250 fprintf(stderr, "%s", ex->object.vtable->klass->name);
251 if (ex->message != NULL) {
252 char *m = mono_string_to_utf8_checked (ex->message, &error);
253 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
254 fprintf(stderr, ": %s", m);
257 fprintf(stderr, "\n%s\n", strace);
259 if (ex->inner_ex != NULL) {
260 ex = (MonoException *)ex->inner_ex;
261 fprintf(stderr, "Inner exception: %s", ex->object.vtable->klass->name);
262 if (ex->message != NULL) {
263 char *m = mono_string_to_utf8_checked (ex->message, &error);
264 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
265 fprintf(stderr, ": %s", m);
268 strace = mono_string_to_utf8_checked (ex->stack_trace, &error);
269 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
270 fprintf(stderr, "\n");
271 fprintf(stderr, "%s\n", strace);
274 /* wait for other threads to also collapse */
275 // Sleep(1000); // TODO: proper sleep
278 context->env_frame->ex = ex;
279 context->search_for_handler = 1;
280 longjmp (*context->current_env, 1);
284 ves_real_abort (int line, MonoMethod *mh,
285 const unsigned short *ip, stackval *stack, stackval *sp)
288 fprintf (stderr, "Execution aborted in method: %s::%s\n", mh->klass->name, mh->name);
289 fprintf (stderr, "Line=%d IP=0x%04lx, Aborted execution\n", line,
290 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code);
291 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
292 g_print ("0x%04x %02x\n",
293 ip-(const unsigned short *)mono_method_get_header_checked (mh, &error)->code, *ip);
294 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
296 printf ("\t[%ld] 0x%08x %0.5f\n", sp-stack, sp[-1].data.i, sp[-1].data.f);
299 #define ves_abort() \
301 ves_real_abort(__LINE__, frame->runtime_method->method, ip, frame->stack, sp); \
302 THROW_EX (mono_get_exception_execution_engine (NULL), ip); \
306 interp_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target, MonoError *error)
308 g_error ("FIXME: use domain and error");
309 // return mono_interp_get_runtime_method (mono_marshal_get_remoting_invoke_for_target (method, target));
312 static mono_mutex_t runtime_method_lookup_section;
315 mono_interp_get_runtime_method (MonoDomain *domain, MonoMethod *method, MonoError *error)
318 mono_error_init (error);
320 mono_os_mutex_lock (&runtime_method_lookup_section);
321 if ((rtm = mono_internal_hash_table_lookup (&domain->jit_code_hash, method))) {
322 mono_os_mutex_unlock (&runtime_method_lookup_section);
325 rtm = mono_mempool_alloc (domain->mp, sizeof (RuntimeMethod));
326 memset (rtm, 0, sizeof (*rtm));
327 rtm->method = method;
328 rtm->param_count = mono_method_signature (method)->param_count;
329 rtm->hasthis = mono_method_signature (method)->hasthis;
330 rtm->valuetype = method->klass->valuetype;
331 mono_internal_hash_table_insert (&domain->jit_code_hash, method, rtm);
332 mono_os_mutex_unlock (&runtime_method_lookup_section);
338 interp_create_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
340 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
341 method = mono_marshal_get_synchronized_wrapper (method);
342 return mono_interp_get_runtime_method (domain, method, error);
345 static inline RuntimeMethod*
346 get_virtual_method (MonoDomain *domain, RuntimeMethod *runtime_method, MonoObject *obj)
348 MonoMethod *m = runtime_method->method;
351 if ((m->flags & METHOD_ATTRIBUTE_FINAL) || !(m->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
352 RuntimeMethod *ret = NULL;
353 if (obj->vtable->klass == mono_defaults.transparent_proxy_class) {
354 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_remoting_invoke (m), &error);
355 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
356 } else if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
357 ret = mono_interp_get_runtime_method (domain, mono_marshal_get_synchronized_wrapper (m), &error);
358 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
360 ret = runtime_method;
365 int slot = mono_method_get_vtable_slot (m);
366 if (m->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
367 g_assert (obj->vtable->klass != m->klass);
368 /* TODO: interface offset lookup is slow, go through IMT instead */
369 slot += mono_class_interface_offset (obj->vtable->klass, m->klass);
371 MonoMethod *virtual_method = obj->vtable->klass->vtable [slot];
372 RuntimeMethod *virtual_runtime_method = mono_interp_get_runtime_method (domain, virtual_method, &error);
373 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
374 return virtual_runtime_method;
378 stackval_from_data (MonoType *type, stackval *result, char *data, gboolean pinvoke)
381 switch (type->type) {
382 case MONO_TYPE_OBJECT:
383 case MONO_TYPE_CLASS:
384 case MONO_TYPE_STRING:
385 case MONO_TYPE_ARRAY:
386 case MONO_TYPE_SZARRAY:
391 result->data.p = *(gpointer*)data;
394 switch (type->type) {
398 result->data.i = *(gint8*)data;
401 case MONO_TYPE_BOOLEAN:
402 result->data.i = *(guint8*)data;
405 result->data.i = *(gint16*)data;
409 result->data.i = *(guint16*)data;
412 result->data.i = *(gint32*)data;
416 result->data.nati = *(mono_i*)data;
419 result->data.p = *(gpointer*)data;
422 result->data.i = *(guint32*)data;
425 result->data.f = *(float*)data;
429 result->data.l = *(gint64*)data;
432 result->data.f = *(double*)data;
434 case MONO_TYPE_STRING:
435 case MONO_TYPE_SZARRAY:
436 case MONO_TYPE_CLASS:
437 case MONO_TYPE_OBJECT:
438 case MONO_TYPE_ARRAY:
439 result->data.p = *(gpointer*)data;
441 case MONO_TYPE_VALUETYPE:
442 if (type->data.klass->enumtype) {
443 stackval_from_data (mono_class_enum_basetype (type->data.klass), result, data, pinvoke);
449 size = mono_class_native_size (type->data.klass, NULL);
451 size = mono_class_value_size (type->data.klass, NULL);
452 memcpy (result->data.vt, data, size);
456 g_warning ("got type 0x%02x", type->type);
457 g_assert_not_reached ();
462 stackval_to_data (MonoType *type, stackval *val, char *data, gboolean pinvoke)
465 gpointer *p = (gpointer*)data;
469 /* printf ("TODAT0 %p\n", data); */
470 switch (type->type) {
473 guint8 *p = (guint8*)data;
477 case MONO_TYPE_BOOLEAN: {
478 guint8 *p = (guint8*)data;
479 *p = (val->data.i != 0);
484 case MONO_TYPE_CHAR: {
485 guint16 *p = (guint16*)data;
490 mono_i *p = (mono_i*)data;
491 /* In theory the value used by stloc should match the local var type
492 but in practice it sometimes doesn't (a int32 gets dup'd and stloc'd into
493 a native int - both by csc and mcs). Not sure what to do about sign extension
494 as it is outside the spec... doing the obvious */
495 *p = (mono_i)val->data.nati;
499 mono_u *p = (mono_u*)data;
501 *p = (mono_u)val->data.nati;
506 gint32 *p = (gint32*)data;
512 gint64 *p = (gint64*)data;
517 float *p = (float*)data;
522 double *p = (double*)data;
526 case MONO_TYPE_STRING:
527 case MONO_TYPE_SZARRAY:
528 case MONO_TYPE_CLASS:
529 case MONO_TYPE_OBJECT:
530 case MONO_TYPE_ARRAY:
531 case MONO_TYPE_PTR: {
532 gpointer *p = (gpointer*)data;
536 case MONO_TYPE_VALUETYPE:
537 if (type->data.klass->enumtype) {
538 stackval_to_data (mono_class_enum_basetype (type->data.klass), val, data, pinvoke);
544 size = mono_class_native_size (type->data.klass, NULL);
546 size = mono_class_value_size (type->data.klass, NULL);
548 memcpy (data, val->data.p, size);
552 g_warning ("got type %x", type->type);
553 g_assert_not_reached ();
558 fill_in_trace (MonoException *exception, MonoInvocation *frame)
560 char *stack_trace = dump_frame (frame);
561 MonoDomain *domain = mono_domain_get();
562 (exception)->stack_trace = mono_string_new (domain, stack_trace);
563 (exception)->trace_ips = get_trace_ips (domain, frame);
564 g_free (stack_trace);
567 #define FILL_IN_TRACE(exception, frame) fill_in_trace(exception, frame)
569 #define THROW_EX(exception,ex_ip) \
571 frame->ip = (ex_ip); \
572 frame->ex = (MonoException*)(exception); \
573 FILL_IN_TRACE(frame->ex, frame); \
574 goto handle_exception; \
578 ves_array_create (MonoDomain *domain, MonoClass *klass, MonoMethodSignature *sig, stackval *values)
581 intptr_t *lower_bounds;
586 lengths = alloca (sizeof (uintptr_t) * klass->rank * 2);
587 for (i = 0; i < sig->param_count; ++i) {
588 lengths [i] = values->data.i;
591 if (klass->rank == sig->param_count) {
592 /* Only lengths provided. */
595 /* lower bounds are first. */
596 lower_bounds = (intptr_t *) lengths;
597 lengths += klass->rank;
599 obj = (MonoObject*) mono_array_new_full_checked (domain, klass, lengths, lower_bounds, &error);
600 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
605 ves_array_set (MonoInvocation *frame)
607 stackval *sp = frame->stack_args;
611 gint32 i, t, pos, esize;
617 ac = o->vtable->klass;
619 g_assert (ac->rank >= 1);
622 if (ao->bounds != NULL) {
623 pos -= ao->bounds [0].lower_bound;
624 for (i = 1; i < ac->rank; i++) {
625 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
626 ao->bounds [i].length) {
627 frame->ex = mono_get_exception_index_out_of_range ();
628 FILL_IN_TRACE(frame->ex, frame);
631 pos = pos*ao->bounds [i].length + sp [i].data.i -
632 ao->bounds [i].lower_bound;
634 } else if (pos >= ao->max_length) {
635 frame->ex = mono_get_exception_index_out_of_range ();
636 FILL_IN_TRACE(frame->ex, frame);
640 if (sp [ac->rank].data.p && !mono_object_class (o)->element_class->valuetype) {
642 MonoObject *isinst = mono_object_isinst_checked (sp [ac->rank].data.p, mono_object_class (o)->element_class, &error);
643 mono_error_cleanup (&error);
645 frame->ex = mono_get_exception_array_type_mismatch ();
646 FILL_IN_TRACE (frame->ex, frame);
651 esize = mono_array_element_size (ac);
652 ea = mono_array_addr_with_size (ao, esize, pos);
654 mt = mono_method_signature (frame->runtime_method->method)->params [ac->rank];
655 stackval_to_data (mt, &sp [ac->rank], ea, FALSE);
659 ves_array_get (MonoInvocation *frame)
661 stackval *sp = frame->stack_args;
665 gint32 i, t, pos, esize;
671 ac = o->vtable->klass;
673 g_assert (ac->rank >= 1);
676 if (ao->bounds != NULL) {
677 pos -= ao->bounds [0].lower_bound;
678 for (i = 1; i < ac->rank; i++) {
679 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
680 ao->bounds [i].length) {
681 frame->ex = mono_get_exception_index_out_of_range ();
682 FILL_IN_TRACE(frame->ex, frame);
686 pos = pos*ao->bounds [i].length + sp [i].data.i -
687 ao->bounds [i].lower_bound;
689 } else if (pos >= ao->max_length) {
690 frame->ex = mono_get_exception_index_out_of_range ();
691 FILL_IN_TRACE(frame->ex, frame);
695 esize = mono_array_element_size (ac);
696 ea = mono_array_addr_with_size (ao, esize, pos);
698 mt = mono_method_signature (frame->runtime_method->method)->ret;
699 stackval_from_data (mt, frame->retval, ea, FALSE);
703 ves_array_element_address (MonoInvocation *frame)
705 stackval *sp = frame->stack_args;
709 gint32 i, t, pos, esize;
714 ac = o->vtable->klass;
716 g_assert (ac->rank >= 1);
719 if (ao->bounds != NULL) {
720 pos -= ao->bounds [0].lower_bound;
721 for (i = 1; i < ac->rank; i++) {
722 if ((t = sp [i].data.i - ao->bounds [i].lower_bound) >=
723 ao->bounds [i].length) {
724 frame->ex = mono_get_exception_index_out_of_range ();
725 FILL_IN_TRACE(frame->ex, frame);
728 pos = pos*ao->bounds [i].length + sp [i].data.i -
729 ao->bounds [i].lower_bound;
731 } else if (pos >= ao->max_length) {
732 frame->ex = mono_get_exception_index_out_of_range ();
733 FILL_IN_TRACE(frame->ex, frame);
737 esize = mono_array_element_size (ac);
738 ea = mono_array_addr_with_size (ao, esize, pos);
740 frame->retval->data.p = ea;
744 interp_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
746 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
747 MonoInvocation *frame;
749 MonoMethodHeader *hd;
752 if (!context) return;
754 frame = context->current_frame;
757 gboolean managed = FALSE;
758 MonoMethod *method = frame->runtime_method->method;
759 if (!method || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)))
762 hd = mono_method_get_header_checked (method, &error);
763 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
764 il_offset = frame->ip - (const unsigned short *)hd->code;
765 if (!method->wrapper_type)
768 if (func (method, -1, il_offset, managed, user_data))
770 frame = frame->parent;
774 static MonoPIFunc mono_interp_enter_icall_trampoline = NULL;
776 struct _MethodArguments {
784 typedef struct _MethodArguments MethodArguments;
786 // TODO: this function is also arch dependent (register width).
787 static MethodArguments* build_args_from_sig (MonoMethodSignature *sig, MonoInvocation *frame)
789 // TODO: don't malloc this data structure.
790 MethodArguments *margs = g_malloc0 (sizeof (MethodArguments));
795 for (int i = 0; i < sig->param_count; i++) {
796 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
798 case MONO_TYPE_BOOLEAN:
809 case MONO_TYPE_SZARRAY:
810 case MONO_TYPE_CLASS:
811 case MONO_TYPE_OBJECT:
812 case MONO_TYPE_STRING:
817 g_error ("build_args_from_sig: not implemented yet (1): 0x%x\n", ptype);
822 margs->iargs = g_malloc0 (sizeof (gpointer) * margs->ilen);
825 g_error ("build_args_from_sig: TODO, more than two iregs: %d\n", margs->ilen);
828 g_error ("build_args_from_sig: TODO, allocate floats: %d\n", margs->flen);
835 margs->iargs [0] = frame->obj;
839 for (int i = 0; i < sig->param_count; i++) {
840 guint32 ptype = sig->params [i]->byref ? MONO_TYPE_PTR : sig->params [i]->type;
842 case MONO_TYPE_BOOLEAN:
853 case MONO_TYPE_SZARRAY:
854 case MONO_TYPE_CLASS:
855 case MONO_TYPE_OBJECT:
856 case MONO_TYPE_STRING:
858 margs->iargs [int_i] = frame->stack_args [i].data.l;
859 g_printerr ("build_args_from_sig: margs->iargs[%d]: %p (frame @ %d)\n", int_i, margs->iargs[int_i], i);
863 g_error ("build_args_from_sig: not implemented yet (2): 0x%x\n", ptype);
867 if (sig->ret->type != MONO_TYPE_VOID) {
868 margs->retval = &(frame->retval->data.p);
870 margs->retval = NULL;
877 ves_pinvoke_method (MonoInvocation *frame, MonoMethodSignature *sig, MonoFuncV addr, gboolean string_ctor, ThreadContext *context)
881 MonoInvocation *old_frame = context->current_frame;
882 MonoInvocation *old_env_frame = context->env_frame;
883 jmp_buf *old_env = context->current_env;
886 context->current_frame = old_frame;
887 context->env_frame = old_env_frame;
888 context->current_env = old_env;
889 context->managed_code = 1;
894 context->env_frame = frame;
895 context->current_env = &env;
897 g_assert (frame->runtime_method);
898 func = frame->runtime_method->func;
900 g_printerr ("ICALL(1): func = NULL, addr = %p\n", addr);
901 if (!mono_interp_enter_icall_trampoline) {
903 mono_interp_enter_icall_trampoline = mono_arch_get_enter_icall_trampoline (&info);
904 // mono_tramp_info_register (info, NULL);
906 func = mono_interp_enter_icall_trampoline;
907 g_printerr ("ICALL(2): func = %p, addr = %p\n", func, addr);
910 MethodArguments *margs = build_args_from_sig (sig, frame);
912 g_printerr ("margs(out): ilen=%d, flen=%d\n", margs->ilen, margs->flen);
914 context->current_frame = frame;
915 context->managed_code = 0;
920 context->managed_code = 1;
921 /* domain can only be changed by native code */
922 context->domain = mono_domain_get ();
924 if (*abort_requested)
925 mono_thread_interruption_checkpoint ();
927 if (!MONO_TYPE_ISSTRUCT (sig->ret))
928 stackval_from_data (sig->ret, frame->retval, (char*)&frame->retval->data.p, sig->pinvoke);
930 context->current_frame = old_frame;
931 context->env_frame = old_env_frame;
932 context->current_env = old_env;
934 g_free (margs->iargs);
935 g_free (margs->fargs);
940 interp_delegate_ctor (MonoDomain *domain, MonoObject *this, MonoObject *target, RuntimeMethod *runtime_method)
942 MonoDelegate *delegate = (MonoDelegate *)this;
945 delegate->method_info = mono_method_get_object_checked (domain, runtime_method->method, NULL, &error);
946 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
947 delegate->target = target;
949 if (target && target->vtable->klass == mono_defaults.transparent_proxy_class) {
950 MonoMethod *method = mono_marshal_get_remoting_invoke (runtime_method->method);
951 delegate->method_ptr = mono_interp_get_runtime_method (domain, method, &error);
952 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
954 delegate->method_ptr = runtime_method;
959 mono_interp_ftnptr_to_delegate (MonoClass *klass, gpointer ftn)
963 MonoDomain *domain = mono_domain_get ();
966 d = (MonoDelegate*)mono_object_new_checked (domain, klass, &error);
967 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
969 ji = mono_jit_info_table_find (domain, ftn);
971 mono_raise_exception (mono_get_exception_argument ("", "Function pointer was not created by a Delegate."));
973 /* FIXME: discard the wrapper and call the original method */
974 interp_delegate_ctor (domain, (MonoObject*)d, NULL, mono_interp_get_runtime_method (domain, ji->d.method, &error));
975 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
982 * runtime specifies that the implementation of the method is automatically
983 * provided by the runtime and is primarily used for the methods of delegates.
986 ves_runtime_method (MonoInvocation *frame, ThreadContext *context)
988 MonoMethod *method = frame->runtime_method->method;
989 const char *name = method->name;
990 MonoObject *obj = (MonoObject*)frame->obj;
991 MonoObject *isinst_obj;
994 mono_class_init (method->klass);
996 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.multicastdelegate_class, &error);
997 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
998 if (obj && isinst_obj) {
999 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
1000 interp_delegate_ctor (context->domain, obj, frame->stack_args[0].data.p, frame->stack_args[1].data.p);
1005 isinst_obj = mono_object_isinst_checked (obj, mono_defaults.array_class, &error);
1006 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1007 if (obj && isinst_obj) {
1008 if (*name == 'S' && (strcmp (name, "Set") == 0)) {
1009 ves_array_set (frame);
1012 if (*name == 'G' && (strcmp (name, "Get") == 0)) {
1013 ves_array_get (frame);
1016 if (*name == 'A' && (strcmp (name, "Address") == 0)) {
1017 ves_array_element_address (frame);
1022 g_error ("Don't know how to exec runtime method %s.%s::%s",
1023 method->klass->name_space, method->klass->name,
1028 dump_stack (stackval *stack, stackval *sp)
1030 stackval *s = stack;
1031 GString *str = g_string_new ("");
1034 return g_string_free (str, FALSE);
1037 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
1040 return g_string_free (str, FALSE);
1044 dump_stackval (GString *str, stackval *s, MonoType *type)
1046 switch (type->type) {
1053 case MONO_TYPE_CHAR:
1054 case MONO_TYPE_BOOLEAN:
1055 g_string_append_printf (str, "[%d] ", s->data.i);
1057 case MONO_TYPE_STRING:
1058 case MONO_TYPE_SZARRAY:
1059 case MONO_TYPE_CLASS:
1060 case MONO_TYPE_OBJECT:
1061 case MONO_TYPE_ARRAY:
1065 g_string_append_printf (str, "[%p] ", s->data.p);
1067 case MONO_TYPE_VALUETYPE:
1068 if (type->data.klass->enumtype)
1069 g_string_append_printf (str, "[%d] ", s->data.i);
1071 g_string_append_printf (str, "[vt:%p] ", s->data.p);
1075 g_string_append_printf (str, "[%g] ", s->data.f);
1080 g_string_append_printf (str, "[%lld/0x%0llx] ", s->data.l, s->data.l);
1086 dump_args (MonoInvocation *inv)
1088 GString *str = g_string_new ("");
1090 MonoMethodSignature *signature = mono_method_signature (inv->runtime_method->method);
1092 if (signature->param_count == 0)
1093 return g_string_free (str, FALSE);
1095 if (signature->hasthis)
1096 g_string_append_printf (str, "%p ", inv->obj);
1098 for (i = 0; i < signature->param_count; ++i)
1099 dump_stackval (str, inv->stack_args + i, signature->params [i]);
1101 return g_string_free (str, FALSE);
1105 dump_retval (MonoInvocation *inv)
1107 GString *str = g_string_new ("");
1108 MonoType *ret = mono_method_signature (inv->runtime_method->method)->ret;
1110 if (ret->type != MONO_TYPE_VOID)
1111 dump_stackval (str, inv->retval, ret);
1113 return g_string_free (str, FALSE);
1117 dump_frame (MonoInvocation *inv)
1119 GString *str = g_string_new ("");
1124 for (i = 0; inv; inv = inv->parent) {
1125 if (inv->runtime_method != NULL) {
1126 MonoMethod *method = inv->runtime_method->method;
1130 const char * opname = "";
1132 gchar *source = NULL;
1136 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
1137 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) == 0) {
1138 MonoMethodHeader *hd = mono_method_get_header_checked (method, &error);
1139 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1143 opname = mono_interp_opname [*inv->ip];
1144 codep = inv->ip - inv->runtime_method->code;
1145 source = g_strdup_printf ("%s:%d // (TODO: proper stacktrace)", method->name, codep);
1150 MonoDebugSourceLocation *minfo = mono_debug_lookup_method (method);
1151 source = mono_debug_method_lookup_location (minfo, codep);
1155 args = dump_args (inv);
1156 name = mono_method_full_name (method, TRUE);
1158 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s) at %s\n", i, codep, opname, name, args, source);
1160 g_string_append_printf (str, "#%d: 0x%05x %-10s in %s (%s)\n", i, codep, opname, name, args);
1167 return g_string_free (str, FALSE);
1171 get_trace_ips (MonoDomain *domain, MonoInvocation *top)
1175 MonoInvocation *inv;
1178 for (i = 0, inv = top; inv; inv = inv->parent)
1179 if (inv->runtime_method != NULL)
1182 res = mono_array_new_checked (domain, mono_defaults.int_class, 2 * i, &error);
1183 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1185 for (i = 0, inv = top; inv; inv = inv->parent)
1186 if (inv->runtime_method != NULL) {
1187 mono_array_set (res, gpointer, i, inv->runtime_method);
1189 mono_array_set (res, gpointer, i, (gpointer)inv->ip);
1197 #define MYGUINT64_MAX 18446744073709551615ULL
1198 #define MYGINT64_MAX 9223372036854775807LL
1199 #define MYGINT64_MIN (-MYGINT64_MAX -1LL)
1201 #define MYGUINT32_MAX 4294967295U
1202 #define MYGINT32_MAX 2147483647
1203 #define MYGINT32_MIN (-MYGINT32_MAX -1)
1205 #define CHECK_ADD_OVERFLOW(a,b) \
1206 (gint32)(b) >= 0 ? (gint32)(MYGINT32_MAX) - (gint32)(b) < (gint32)(a) ? -1 : 0 \
1207 : (gint32)(MYGINT32_MIN) - (gint32)(b) > (gint32)(a) ? +1 : 0
1209 #define CHECK_SUB_OVERFLOW(a,b) \
1210 (gint32)(b) < 0 ? (gint32)(MYGINT32_MAX) + (gint32)(b) < (gint32)(a) ? -1 : 0 \
1211 : (gint32)(MYGINT32_MIN) + (gint32)(b) > (gint32)(a) ? +1 : 0
1213 #define CHECK_ADD_OVERFLOW_UN(a,b) \
1214 (guint32)(MYGUINT32_MAX) - (guint32)(b) < (guint32)(a) ? -1 : 0
1216 #define CHECK_SUB_OVERFLOW_UN(a,b) \
1217 (guint32)(a) < (guint32)(b) ? -1 : 0
1219 #define CHECK_ADD_OVERFLOW64(a,b) \
1220 (gint64)(b) >= 0 ? (gint64)(MYGINT64_MAX) - (gint64)(b) < (gint64)(a) ? -1 : 0 \
1221 : (gint64)(MYGINT64_MIN) - (gint64)(b) > (gint64)(a) ? +1 : 0
1223 #define CHECK_SUB_OVERFLOW64(a,b) \
1224 (gint64)(b) < 0 ? (gint64)(MYGINT64_MAX) + (gint64)(b) < (gint64)(a) ? -1 : 0 \
1225 : (gint64)(MYGINT64_MIN) + (gint64)(b) > (gint64)(a) ? +1 : 0
1227 #define CHECK_ADD_OVERFLOW64_UN(a,b) \
1228 (guint64)(MYGUINT64_MAX) - (guint64)(b) < (guint64)(a) ? -1 : 0
1230 #define CHECK_SUB_OVERFLOW64_UN(a,b) \
1231 (guint64)(a) < (guint64)(b) ? -1 : 0
1233 #if SIZEOF_VOID_P == 4
1234 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW(a,b)
1235 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW_UN(a,b)
1237 #define CHECK_ADD_OVERFLOW_NAT(a,b) CHECK_ADD_OVERFLOW64(a,b)
1238 #define CHECK_ADD_OVERFLOW_NAT_UN(a,b) CHECK_ADD_OVERFLOW64_UN(a,b)
1241 /* Resolves to TRUE if the operands would overflow */
1242 #define CHECK_MUL_OVERFLOW(a,b) \
1243 ((gint32)(a) == 0) || ((gint32)(b) == 0) ? 0 : \
1244 (((gint32)(a) > 0) && ((gint32)(b) == -1)) ? FALSE : \
1245 (((gint32)(a) < 0) && ((gint32)(b) == -1)) ? (a == - MYGINT32_MAX) : \
1246 (((gint32)(a) > 0) && ((gint32)(b) > 0)) ? (gint32)(a) > ((MYGINT32_MAX) / (gint32)(b)) : \
1247 (((gint32)(a) > 0) && ((gint32)(b) < 0)) ? (gint32)(a) > ((MYGINT32_MIN) / (gint32)(b)) : \
1248 (((gint32)(a) < 0) && ((gint32)(b) > 0)) ? (gint32)(a) < ((MYGINT32_MIN) / (gint32)(b)) : \
1249 (gint32)(a) < ((MYGINT32_MAX) / (gint32)(b))
1251 #define CHECK_MUL_OVERFLOW_UN(a,b) \
1252 ((guint32)(a) == 0) || ((guint32)(b) == 0) ? 0 : \
1253 (guint32)(b) > ((MYGUINT32_MAX) / (guint32)(a))
1255 #define CHECK_MUL_OVERFLOW64(a,b) \
1256 ((gint64)(a) == 0) || ((gint64)(b) == 0) ? 0 : \
1257 (((gint64)(a) > 0) && ((gint64)(b) == -1)) ? FALSE : \
1258 (((gint64)(a) < 0) && ((gint64)(b) == -1)) ? (a == - MYGINT64_MAX) : \
1259 (((gint64)(a) > 0) && ((gint64)(b) > 0)) ? (gint64)(a) > ((MYGINT64_MAX) / (gint64)(b)) : \
1260 (((gint64)(a) > 0) && ((gint64)(b) < 0)) ? (gint64)(a) > ((MYGINT64_MIN) / (gint64)(b)) : \
1261 (((gint64)(a) < 0) && ((gint64)(b) > 0)) ? (gint64)(a) < ((MYGINT64_MIN) / (gint64)(b)) : \
1262 (gint64)(a) < ((MYGINT64_MAX) / (gint64)(b))
1264 #define CHECK_MUL_OVERFLOW64_UN(a,b) \
1265 ((guint64)(a) == 0) || ((guint64)(b) == 0) ? 0 : \
1266 (guint64)(b) > ((MYGUINT64_MAX) / (guint64)(a))
1268 #if SIZEOF_VOID_P == 4
1269 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW(a,b)
1270 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW_UN(a,b)
1272 #define CHECK_MUL_OVERFLOW_NAT(a,b) CHECK_MUL_OVERFLOW64(a,b)
1273 #define CHECK_MUL_OVERFLOW_NAT_UN(a,b) CHECK_MUL_OVERFLOW64_UN(a,b)
1277 interp_mono_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc, MonoError *error)
1279 MonoInvocation frame;
1280 ThreadContext * volatile context = mono_native_tls_get_value (thread_context_id);
1281 MonoObject *retval = NULL;
1282 MonoMethodSignature *sig = mono_method_signature (method);
1283 MonoClass *klass = mono_class_from_mono_type (sig->ret);
1284 int i, type, isobject = 0;
1287 stackval *args = alloca (sizeof (stackval) * sig->param_count);
1288 ThreadContext context_struct;
1289 MonoInvocation *old_frame = NULL;
1292 mono_error_init (error);
1297 if (context != &context_struct) {
1298 context->domain = mono_domain_get ();
1299 context->current_frame = old_frame;
1300 context->managed_code = 0;
1302 mono_native_tls_set_value (thread_context_id, NULL);
1304 *exc = (MonoObject *)frame.ex;
1308 if (context == NULL) {
1309 context = &context_struct;
1310 context_struct.base_frame = &frame;
1311 context_struct.current_frame = NULL;
1312 context_struct.env_frame = &frame;
1313 context_struct.current_env = &env;
1314 context_struct.search_for_handler = 0;
1315 context_struct.managed_code = 0;
1316 mono_native_tls_set_value (thread_context_id, context);
1319 old_frame = context->current_frame;
1321 context->domain = mono_domain_get ();
1323 switch (sig->ret->type) {
1324 case MONO_TYPE_VOID:
1326 case MONO_TYPE_STRING:
1327 case MONO_TYPE_OBJECT:
1328 case MONO_TYPE_CLASS:
1329 case MONO_TYPE_ARRAY:
1330 case MONO_TYPE_SZARRAY:
1333 case MONO_TYPE_VALUETYPE:
1334 retval = mono_object_new_checked (context->domain, klass, error);
1335 ret = ((char*)retval) + sizeof (MonoObject);
1336 if (!sig->ret->data.klass->enumtype)
1337 result.data.vt = ret;
1340 retval = mono_object_new_checked (context->domain, klass, error);
1341 ret = ((char*)retval) + sizeof (MonoObject);
1345 for (i = 0; i < sig->param_count; ++i) {
1346 if (sig->params [i]->byref) {
1347 args [i].data.p = params [i];
1350 type = sig->params [i]->type;
1355 case MONO_TYPE_BOOLEAN:
1356 args [i].data.i = *(MonoBoolean*)params [i];
1360 case MONO_TYPE_CHAR:
1361 args [i].data.i = *(gint16*)params [i];
1363 #if SIZEOF_VOID_P == 4
1364 case MONO_TYPE_U: /* use VAL_POINTER? */
1369 args [i].data.i = *(gint32*)params [i];
1371 #if SIZEOF_VOID_P == 8
1377 args [i].data.l = *(gint64*)params [i];
1379 case MONO_TYPE_VALUETYPE:
1380 if (sig->params [i]->data.klass->enumtype) {
1381 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
1384 args [i].data.p = params [i];
1387 case MONO_TYPE_STRING:
1388 case MONO_TYPE_CLASS:
1389 case MONO_TYPE_ARRAY:
1390 case MONO_TYPE_SZARRAY:
1391 case MONO_TYPE_OBJECT:
1392 args [i].data.p = params [i];
1395 g_error ("type 0x%x not handled in runtime invoke", sig->params [i]->type);
1399 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
1400 method = mono_marshal_get_native_wrapper (method, FALSE, FALSE);
1401 INIT_FRAME(&frame,context->current_frame,obj,args,&result,mono_get_root_domain (),method,error);
1403 frame.invoke_trap = 1;
1404 context->managed_code = 1;
1405 ves_exec_method_with_context (&frame, context);
1406 context->managed_code = 0;
1407 if (context == &context_struct)
1408 mono_native_tls_set_value (thread_context_id, NULL);
1410 context->current_frame = old_frame;
1411 if (frame.ex != NULL) {
1413 *exc = (MonoObject*) frame.ex;
1416 if (context->current_env != NULL) {
1417 context->env_frame->ex = frame.ex;
1418 longjmp(*context->current_env, 1);
1421 printf("dropped exception...\n");
1423 if (sig->ret->type == MONO_TYPE_VOID && !method->string_ctor)
1425 if (isobject || method->string_ctor)
1426 return result.data.p;
1427 stackval_to_data (sig->ret, &result, ret, sig->pinvoke);
1432 do_icall (ThreadContext *context, int op, stackval *sp, gpointer ptr)
1434 MonoInvocation *old_frame = context->current_frame;
1435 MonoInvocation *old_env_frame = context->env_frame;
1436 jmp_buf *old_env = context->current_env;
1440 context->current_frame = old_frame;
1441 context->env_frame = old_env_frame;
1442 context->current_env = old_env;
1443 context->managed_code = 1;
1447 context->env_frame = context->current_frame;
1448 context->current_env = &env;
1449 context->managed_code = 0;
1452 case MINT_ICALL_V_V: {
1453 void (*func)() = ptr;
1457 case MINT_ICALL_P_V: {
1458 void (*func)(gpointer) = ptr;
1459 func (sp [-1].data.p);
1463 case MINT_ICALL_P_P: {
1464 gpointer (*func)(gpointer) = ptr;
1465 sp [-1].data.p = func (sp [-1].data.p);
1468 case MINT_ICALL_PP_V: {
1469 void (*func)(gpointer,gpointer) = ptr;
1471 func (sp [0].data.p, sp [1].data.p);
1474 case MINT_ICALL_PI_V: {
1475 void (*func)(gpointer,int) = ptr;
1477 func (sp [0].data.p, sp [1].data.i);
1480 case MINT_ICALL_PP_P: {
1481 gpointer (*func)(gpointer,gpointer) = ptr;
1483 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.p);
1486 case MINT_ICALL_PI_P: {
1487 gpointer (*func)(gpointer,int) = ptr;
1489 sp [-1].data.p = func (sp [-1].data.p, sp [0].data.i);
1492 case MINT_ICALL_PPP_V: {
1493 void (*func)(gpointer,gpointer,gpointer) = ptr;
1495 func (sp [0].data.p, sp [1].data.p, sp [2].data.p);
1498 case MINT_ICALL_PPI_V: {
1499 void (*func)(gpointer,gpointer,int) = ptr;
1501 func (sp [0].data.p, sp [1].data.p, sp [2].data.i);
1505 g_assert_not_reached ();
1508 context->env_frame = old_env_frame;
1509 context->current_env = old_env;
1514 static mono_mutex_t create_method_pointer_mutex;
1516 static GHashTable *method_pointer_hash = NULL;
1519 mono_create_method_pointer (MonoMethod *method, MonoError *error)
1523 mono_error_init (error);
1525 mono_os_mutex_lock (&create_method_pointer_mutex);
1526 if (!method_pointer_hash) {
1527 // FIXME: is registering method table as GC root really necessary?
1528 // MONO_GC_REGISTER_ROOT_FIXED (method_pointer_hash);
1529 method_pointer_hash = g_hash_table_new (NULL, NULL);
1531 addr = g_hash_table_lookup (method_pointer_hash, method);
1533 mono_os_mutex_unlock (&create_method_pointer_mutex);
1538 * If it is a static P/Invoke method, we can just return the pointer
1539 * to the method implementation.
1541 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL && ((MonoMethodPInvoke*) method)->addr) {
1542 ji = g_new0 (MonoJitInfo, 1);
1543 ji->d.method = method;
1545 ji->code_start = addr = ((MonoMethodPInvoke*) method)->addr;
1547 mono_jit_info_table_add (mono_get_root_domain (), ji);
1550 g_error ("FIXME: not available? figure out new API");
1552 addr = mono_arch_create_method_pointer (method);
1556 g_hash_table_insert (method_pointer_hash, method, addr);
1557 mono_os_mutex_unlock (&create_method_pointer_mutex);
1563 static int opcode_counts[512];
1565 #define COUNT_OP(op) opcode_counts[op]++
1567 #define COUNT_OP(op)
1571 #define DUMP_INSTR() \
1572 if (tracing > 1) { \
1574 if (sp > frame->stack) { \
1575 ins = dump_stack (frame->stack, sp); \
1577 ins = g_strdup (""); \
1581 g_print ("(%u) ", mono_thread_internal_current ()); \
1582 mono_interp_dis_mintop(rtm->code, ip); \
1583 g_print ("\t%d:%s\n", vt_sp - vtalloc, ins); \
1587 #define DUMP_INSTR()
1591 #define USE_COMPUTED_GOTO 1
1593 #if USE_COMPUTED_GOTO
1594 #define MINT_IN_SWITCH(op) COUNT_OP(op); goto *in_labels[op];
1595 #define MINT_IN_CASE(x) LAB_ ## x:
1597 #define MINT_IN_BREAK if (tracing > 1) goto main_loop; else { COUNT_OP(*ip); goto *in_labels[*ip]; }
1599 #define MINT_IN_BREAK { COUNT_OP(*ip); goto *in_labels[*ip]; }
1601 #define MINT_IN_DEFAULT mint_default: if (0) goto mint_default; /* make gcc shut up */
1603 #define MINT_IN_SWITCH(op) switch (op)
1604 #define MINT_IN_CASE(x) case x:
1605 #define MINT_IN_BREAK break
1606 #define MINT_IN_DEFAULT default:
1610 * Defining this causes register allocation errors in some versions of gcc:
1611 * error: unable to find a register to spill in class `SIREG'
1613 /* #define MINT_USE_DEDICATED_IP_REG */
1616 ves_exec_method_with_context (MonoInvocation *frame, ThreadContext *context)
1618 MonoInvocation child_frame;
1619 GSList *finally_ips = NULL;
1620 const unsigned short *endfinally_ip = NULL;
1621 #if defined(__GNUC__) && defined (i386) && defined (MINT_USE_DEDICATED_IP_REG)
1622 register const unsigned short *ip asm ("%esi");
1624 register const unsigned short *ip;
1626 register stackval *sp;
1629 gint tracing = global_tracing;
1630 unsigned char *vtalloc;
1633 unsigned char *vt_sp;
1634 unsigned char *locals;
1636 MonoObject *o = NULL;
1638 #if USE_COMPUTED_GOTO
1639 static void *in_labels[] = {
1640 #define OPDEF(a,b,c,d) \
1642 #include "mintops.def"
1647 frame->ex_handler = NULL;
1649 context->current_frame = frame;
1652 debug_enter (frame, &tracing);
1655 if (!frame->runtime_method->transformed) {
1656 context->managed_code = 0;
1658 char *mn = mono_method_full_name (frame->runtime_method->method, FALSE);
1659 g_printerr ("(0x%08x) Transforming %s\n", mono_thread_internal_current (), mn);
1662 frame->ex = mono_interp_transform_method (frame->runtime_method, context);
1663 context->managed_code = 1;
1671 rtm = frame->runtime_method;
1672 frame->args = alloca (rtm->alloca_size);
1673 sp = frame->stack = (stackval *)((char *)frame->args + rtm->args_size);
1676 memset(sp, 0, rtm->stack_size);
1678 vt_sp = (unsigned char *) sp + rtm->stack_size;
1682 locals = (unsigned char *) vt_sp + rtm->vt_stack_size;
1684 child_frame.parent = frame;
1690 * using while (ip < end) may result in a 15% performance drop,
1691 * but it may be useful for debug
1695 /* g_assert (sp >= frame->stack); */
1696 /* g_assert(vt_sp - vtalloc <= rtm->vt_stack_size); */
1698 MINT_IN_SWITCH (*ip) {
1699 MINT_IN_CASE(MINT_INITLOCALS)
1700 memset (locals, 0, rtm->locals_size);
1703 MINT_IN_CASE(MINT_NOP)
1706 MINT_IN_CASE(MINT_BREAK)
1708 G_BREAKPOINT (); /* this is not portable... */
1710 MINT_IN_CASE(MINT_LDNULL)
1715 MINT_IN_CASE(MINT_VTRESULT) {
1716 int ret_size = * (guint16 *)(ip + 1);
1717 unsigned char *ret_vt_sp = vt_sp;
1718 vt_sp -= READ32(ip + 2);
1720 memmove (vt_sp, ret_vt_sp, ret_size);
1721 vt_sp += (ret_size + 7) & ~7;
1726 #define LDC(n) do { sp->data.i = (n); ++ip; ++sp; } while (0)
1727 MINT_IN_CASE(MINT_LDC_I4_M1)
1730 MINT_IN_CASE(MINT_LDC_I4_0)
1733 MINT_IN_CASE(MINT_LDC_I4_1)
1736 MINT_IN_CASE(MINT_LDC_I4_2)
1739 MINT_IN_CASE(MINT_LDC_I4_3)
1742 MINT_IN_CASE(MINT_LDC_I4_4)
1745 MINT_IN_CASE(MINT_LDC_I4_5)
1748 MINT_IN_CASE(MINT_LDC_I4_6)
1751 MINT_IN_CASE(MINT_LDC_I4_7)
1754 MINT_IN_CASE(MINT_LDC_I4_8)
1757 MINT_IN_CASE(MINT_LDC_I4_S)
1758 sp->data.i = *(const short *)(ip + 1);
1762 MINT_IN_CASE(MINT_LDC_I4)
1764 sp->data.i = READ32 (ip);
1768 MINT_IN_CASE(MINT_LDC_I8)
1770 sp->data.l = READ64 (ip);
1774 MINT_IN_CASE(MINT_LDC_R4) {
1778 sp->data.f = * (float *)&val;
1783 MINT_IN_CASE(MINT_LDC_R8)
1784 sp->data.l = READ64 (ip + 1); /* note union usage */
1788 MINT_IN_CASE(MINT_DUP)
1793 MINT_IN_CASE(MINT_DUP_VT)
1794 i32 = READ32 (ip + 1);
1796 memcpy(sp->data.p, sp [-1].data.p, i32);
1797 vt_sp += (i32 + 7) & ~7;
1801 MINT_IN_CASE(MINT_POP)
1805 MINT_IN_CASE(MINT_JMP) {
1806 RuntimeMethod *new_method = rtm->data_items [* (guint16 *)(ip + 1)];
1807 if (!new_method->transformed) {
1809 frame->ex = mono_interp_transform_method (new_method, context);
1814 if (new_method->alloca_size > rtm->alloca_size)
1815 g_error ("MINT_JMP to method which needs more stack space (%d > %d)", new_method->alloca_size, rtm->alloca_size);
1816 rtm = frame->runtime_method = new_method;
1817 vt_sp = (unsigned char *) sp + rtm->stack_size;
1821 locals = vt_sp + rtm->vt_stack_size;
1822 ip = rtm->new_body_start; /* bypass storing input args from callers frame */
1825 MINT_IN_CASE(MINT_CALLI) {
1826 MonoMethodSignature *csignature;
1827 stackval *endsp = sp;
1831 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1835 child_frame.runtime_method = sp->data.p;
1838 child_frame.retval = sp;
1839 /* decrement by the actual number of args */
1840 sp -= csignature->param_count;
1841 child_frame.stack_args = sp;
1842 if (csignature->hasthis) {
1844 child_frame.obj = sp->data.p;
1846 child_frame.obj = NULL;
1848 if (csignature->hasthis && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1849 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1850 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1851 } else if (child_frame.runtime_method->method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
1852 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_native_wrapper (child_frame.runtime_method->method, FALSE, FALSE), &error);
1853 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1856 ves_exec_method_with_context (&child_frame, context);
1858 context->current_frame = frame;
1860 if (child_frame.ex) {
1862 * An exception occurred, need to run finally, fault and catch handlers..
1864 frame->ex = child_frame.ex;
1865 goto handle_finally;
1868 /* need to handle typedbyref ... */
1869 if (csignature->ret->type != MONO_TYPE_VOID) {
1875 MINT_IN_CASE(MINT_CALLI_NAT) {
1876 MonoMethodSignature *csignature;
1877 stackval *endsp = sp;
1878 unsigned char *code = NULL;
1882 csignature = rtm->data_items [* (guint16 *)(ip + 1)];
1887 child_frame.runtime_method = NULL;
1890 child_frame.retval = sp;
1891 /* decrement by the actual number of args */
1892 sp -= csignature->param_count;
1893 child_frame.stack_args = sp;
1894 if (csignature->hasthis) {
1896 child_frame.obj = sp->data.p;
1898 child_frame.obj = NULL;
1900 ves_pinvoke_method (&child_frame, csignature, (MonoFuncV) code, FALSE, 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 if (context->search_for_handler) {
1910 context->search_for_handler = 0;
1911 goto handle_exception;
1913 goto handle_finally;
1916 /* need to handle typedbyref ... */
1917 if (csignature->ret->type != MONO_TYPE_VOID) {
1923 MINT_IN_CASE(MINT_CALL) {
1924 stackval *endsp = sp;
1928 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1931 child_frame.retval = sp;
1932 /* decrement by the actual number of args */
1933 sp -= child_frame.runtime_method->param_count;
1934 child_frame.stack_args = sp;
1935 if (child_frame.runtime_method->hasthis) {
1937 child_frame.obj = sp->data.p;
1939 child_frame.obj = NULL;
1941 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1942 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1943 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1945 ves_exec_method_with_context (&child_frame, context);
1947 context->current_frame = frame;
1949 if (child_frame.ex) {
1951 * An exception occurred, need to run finally, fault and catch handlers..
1953 frame->ex = child_frame.ex;
1954 goto handle_finally;
1957 /* need to handle typedbyref ... */
1962 MINT_IN_CASE(MINT_VCALL) {
1965 child_frame.runtime_method = rtm->data_items [* (guint16 *)(ip + 1)];
1969 child_frame.retval = sp;
1970 /* decrement by the actual number of args */
1971 sp -= child_frame.runtime_method->param_count;
1972 child_frame.stack_args = sp;
1973 if (child_frame.runtime_method->hasthis) {
1975 child_frame.obj = sp->data.p;
1977 child_frame.obj = NULL;
1980 if (child_frame.runtime_method->hasthis && !child_frame.runtime_method->valuetype && ((MonoObject *)child_frame.obj)->vtable->klass == mono_defaults.transparent_proxy_class) {
1981 child_frame.runtime_method = mono_interp_get_runtime_method (context->domain, mono_marshal_get_remoting_invoke (child_frame.runtime_method->method), &error);
1982 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1985 ves_exec_method_with_context (&child_frame, context);
1987 context->current_frame = frame;
1989 if (child_frame.ex) {
1991 * An exception occurred, need to run finally, fault and catch handlers..
1993 frame->ex = child_frame.ex;
1994 goto handle_finally;
1998 MINT_IN_CASE(MINT_CALLVIRT) {
1999 stackval *endsp = sp;
2000 MonoObject *this_arg;
2005 token = * (unsigned short *)(ip + 1);
2007 child_frame.runtime_method = rtm->data_items [token];
2009 child_frame.retval = sp;
2011 /* decrement by the actual number of args */
2012 sp -= child_frame.runtime_method->param_count;
2013 child_frame.stack_args = sp;
2015 child_frame.obj = this_arg = sp->data.p;
2017 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2018 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2020 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
2021 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
2024 ves_exec_method_with_context (&child_frame, context);
2026 context->current_frame = frame;
2028 if (child_frame.ex) {
2030 * An exception occurred, need to run finally, fault and catch handlers..
2032 frame->ex = child_frame.ex;
2033 if (context->search_for_handler) {
2034 context->search_for_handler = 0;
2035 goto handle_exception;
2037 goto handle_finally;
2040 /* need to handle typedbyref ... */
2045 MINT_IN_CASE(MINT_VCALLVIRT) {
2046 MonoObject *this_arg;
2051 token = * (unsigned short *)(ip + 1);
2053 child_frame.runtime_method = rtm->data_items [token];
2055 child_frame.retval = sp;
2057 /* decrement by the actual number of args */
2058 sp -= child_frame.runtime_method->param_count;
2059 child_frame.stack_args = sp;
2061 child_frame.obj = this_arg = sp->data.p;
2063 THROW_EX (mono_get_exception_null_reference(), ip - 2);
2064 child_frame.runtime_method = get_virtual_method (context->domain, child_frame.runtime_method, this_arg);
2066 if (this_arg->vtable->klass->valuetype && child_frame.runtime_method->valuetype) {
2067 child_frame.obj = (char *)this_arg + sizeof(MonoObject);
2070 ves_exec_method_with_context (&child_frame, context);
2072 context->current_frame = frame;
2074 if (child_frame.ex) {
2076 * An exception occurred, need to run finally, fault and catch handlers..
2078 frame->ex = child_frame.ex;
2079 if (context->search_for_handler) {
2080 context->search_for_handler = 0;
2081 goto handle_exception;
2083 goto handle_finally;
2087 MINT_IN_CASE(MINT_CALLINT)
2088 g_printerr ("doing MINT_CALLINT: %p\n", ((MonoMethodPInvoke*) frame->runtime_method->method)->addr);
2089 ves_pinvoke_method (frame, mono_method_signature (frame->runtime_method->method), ((MonoMethodPInvoke*) frame->runtime_method->method)->addr,
2090 frame->runtime_method->method->string_ctor, context);
2093 goto handle_exception;
2096 MINT_IN_CASE(MINT_CALLRUN)
2097 ves_runtime_method (frame, context);
2100 goto handle_exception;
2103 MINT_IN_CASE(MINT_RET)
2105 *frame->retval = *sp;
2106 if (sp > frame->stack)
2107 g_warning ("ret: more values on stack: %d", sp-frame->stack);
2109 MINT_IN_CASE(MINT_RET_VOID)
2110 if (sp > frame->stack)
2111 g_warning ("ret.void: more values on stack: %d", sp-frame->stack);
2113 MINT_IN_CASE(MINT_RET_VT)
2114 i32 = READ32(ip + 1);
2116 memcpy(frame->retval->data.p, sp->data.p, i32);
2117 if (sp > frame->stack)
2118 g_warning ("ret.vt: more values on stack: %d", sp-frame->stack);
2120 MINT_IN_CASE(MINT_BR_S)
2121 ip += (short) *(ip + 1);
2123 MINT_IN_CASE(MINT_BR)
2124 ip += (gint32) READ32(ip + 1);
2126 #define ZEROP_S(datamem, op) \
2128 if (sp->data.datamem op 0) \
2129 ip += * (gint16 *)(ip + 1); \
2133 #define ZEROP(datamem, op) \
2135 if (sp->data.datamem op 0) \
2136 ip += READ32(ip + 1); \
2140 MINT_IN_CASE(MINT_BRFALSE_I4_S)
2143 MINT_IN_CASE(MINT_BRFALSE_I8_S)
2146 MINT_IN_CASE(MINT_BRFALSE_R8_S)
2149 MINT_IN_CASE(MINT_BRFALSE_I4)
2152 MINT_IN_CASE(MINT_BRFALSE_I8)
2155 MINT_IN_CASE(MINT_BRFALSE_R8)
2158 MINT_IN_CASE(MINT_BRTRUE_I4_S)
2161 MINT_IN_CASE(MINT_BRTRUE_I8_S)
2164 MINT_IN_CASE(MINT_BRTRUE_R8_S)
2167 MINT_IN_CASE(MINT_BRTRUE_I4)
2170 MINT_IN_CASE(MINT_BRTRUE_I8)
2173 MINT_IN_CASE(MINT_BRTRUE_R8)
2176 #define CONDBR_S(cond) \
2179 ip += * (gint16 *)(ip + 1); \
2182 #define BRELOP_S(datamem, op) \
2183 CONDBR_S(sp[0].data.datamem op sp[1].data.datamem)
2185 #define CONDBR(cond) \
2188 ip += READ32(ip + 1); \
2192 #define BRELOP(datamem, op) \
2193 CONDBR(sp[0].data.datamem op sp[1].data.datamem)
2195 MINT_IN_CASE(MINT_BEQ_I4_S)
2198 MINT_IN_CASE(MINT_BEQ_I8_S)
2201 MINT_IN_CASE(MINT_BEQ_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_BEQ_I4)
2207 MINT_IN_CASE(MINT_BEQ_I8)
2210 MINT_IN_CASE(MINT_BEQ_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_BGE_I4_S)
2216 MINT_IN_CASE(MINT_BGE_I8_S)
2219 MINT_IN_CASE(MINT_BGE_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_BGE_I4)
2225 MINT_IN_CASE(MINT_BGE_I8)
2228 MINT_IN_CASE(MINT_BGE_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_BGT_I4_S)
2234 MINT_IN_CASE(MINT_BGT_I8_S)
2237 MINT_IN_CASE(MINT_BGT_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_BGT_I4)
2243 MINT_IN_CASE(MINT_BGT_I8)
2246 MINT_IN_CASE(MINT_BGT_R8)
2247 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f > sp[1].data.f)
2249 MINT_IN_CASE(MINT_BLT_I4_S)
2252 MINT_IN_CASE(MINT_BLT_I8_S)
2255 MINT_IN_CASE(MINT_BLT_R8_S)
2256 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2258 MINT_IN_CASE(MINT_BLT_I4)
2261 MINT_IN_CASE(MINT_BLT_I8)
2264 MINT_IN_CASE(MINT_BLT_R8)
2265 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f < sp[1].data.f)
2267 MINT_IN_CASE(MINT_BLE_I4_S)
2270 MINT_IN_CASE(MINT_BLE_I8_S)
2273 MINT_IN_CASE(MINT_BLE_R8_S)
2274 CONDBR_S(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2276 MINT_IN_CASE(MINT_BLE_I4)
2279 MINT_IN_CASE(MINT_BLE_I8)
2282 MINT_IN_CASE(MINT_BLE_R8)
2283 CONDBR(!isunordered (sp [0].data.f, sp [1].data.f) && sp[0].data.f <= sp[1].data.f)
2285 MINT_IN_CASE(MINT_BNE_UN_I4_S)
2288 MINT_IN_CASE(MINT_BNE_UN_I8_S)
2291 MINT_IN_CASE(MINT_BNE_UN_R8_S)
2292 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2294 MINT_IN_CASE(MINT_BNE_UN_I4)
2297 MINT_IN_CASE(MINT_BNE_UN_I8)
2300 MINT_IN_CASE(MINT_BNE_UN_R8)
2301 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f != sp[1].data.f)
2304 #define BRELOP_S_CAST(datamem, op, type) \
2306 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2307 ip += * (gint16 *)(ip + 1); \
2311 #define BRELOP_CAST(datamem, op, type) \
2313 if ((type) sp[0].data.datamem op (type) sp[1].data.datamem) \
2314 ip += READ32(ip + 1); \
2318 MINT_IN_CASE(MINT_BGE_UN_I4_S)
2319 BRELOP_S_CAST(i, >=, guint32);
2321 MINT_IN_CASE(MINT_BGE_UN_I8_S)
2322 BRELOP_S_CAST(l, >=, guint64);
2324 MINT_IN_CASE(MINT_BGE_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_BGE_UN_I4)
2328 BRELOP_CAST(i, >=, guint32);
2330 MINT_IN_CASE(MINT_BGE_UN_I8)
2331 BRELOP_CAST(l, >=, guint64);
2333 MINT_IN_CASE(MINT_BGE_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_BGT_UN_I4_S)
2337 BRELOP_S_CAST(i, >, guint32);
2339 MINT_IN_CASE(MINT_BGT_UN_I8_S)
2340 BRELOP_S_CAST(l, >, guint64);
2342 MINT_IN_CASE(MINT_BGT_UN_R8_S)
2343 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2345 MINT_IN_CASE(MINT_BGT_UN_I4)
2346 BRELOP_CAST(i, >, guint32);
2348 MINT_IN_CASE(MINT_BGT_UN_I8)
2349 BRELOP_CAST(l, >, guint64);
2351 MINT_IN_CASE(MINT_BGT_UN_R8)
2352 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f > sp[1].data.f)
2354 MINT_IN_CASE(MINT_BLE_UN_I4_S)
2355 BRELOP_S_CAST(i, <=, guint32);
2357 MINT_IN_CASE(MINT_BLE_UN_I8_S)
2358 BRELOP_S_CAST(l, <=, guint64);
2360 MINT_IN_CASE(MINT_BLE_UN_R8_S)
2361 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2363 MINT_IN_CASE(MINT_BLE_UN_I4)
2364 BRELOP_CAST(i, <=, guint32);
2366 MINT_IN_CASE(MINT_BLE_UN_I8)
2367 BRELOP_CAST(l, <=, guint64);
2369 MINT_IN_CASE(MINT_BLE_UN_R8)
2370 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f <= sp[1].data.f)
2372 MINT_IN_CASE(MINT_BLT_UN_I4_S)
2373 BRELOP_S_CAST(i, <, guint32);
2375 MINT_IN_CASE(MINT_BLT_UN_I8_S)
2376 BRELOP_S_CAST(l, <, guint64);
2378 MINT_IN_CASE(MINT_BLT_UN_R8_S)
2379 CONDBR_S(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2381 MINT_IN_CASE(MINT_BLT_UN_I4)
2382 BRELOP_CAST(i, <, guint32);
2384 MINT_IN_CASE(MINT_BLT_UN_I8)
2385 BRELOP_CAST(l, <, guint64);
2387 MINT_IN_CASE(MINT_BLT_UN_R8)
2388 CONDBR(isunordered (sp [0].data.f, sp [1].data.f) || sp[0].data.f < sp[1].data.f)
2390 MINT_IN_CASE(MINT_SWITCH) {
2392 const unsigned short *st;
2398 if ((guint32)sp->data.i < n) {
2400 ip += 2 * (guint32)sp->data.i;
2401 offset = READ32 (ip);
2408 MINT_IN_CASE(MINT_LDIND_I1)
2410 sp[-1].data.i = *(gint8*)sp[-1].data.p;
2412 MINT_IN_CASE(MINT_LDIND_U1)
2414 sp[-1].data.i = *(guint8*)sp[-1].data.p;
2416 MINT_IN_CASE(MINT_LDIND_I2)
2418 sp[-1].data.i = *(gint16*)sp[-1].data.p;
2420 MINT_IN_CASE(MINT_LDIND_U2)
2422 sp[-1].data.i = *(guint16*)sp[-1].data.p;
2424 MINT_IN_CASE(MINT_LDIND_I4) /* Fall through */
2425 MINT_IN_CASE(MINT_LDIND_U4)
2427 sp[-1].data.i = *(gint32*)sp[-1].data.p;
2429 MINT_IN_CASE(MINT_LDIND_I8)
2431 sp[-1].data.l = *(gint64*)sp[-1].data.p;
2433 MINT_IN_CASE(MINT_LDIND_I)
2435 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2437 MINT_IN_CASE(MINT_LDIND_R4)
2439 sp[-1].data.f = *(gfloat*)sp[-1].data.p;
2441 MINT_IN_CASE(MINT_LDIND_R8)
2443 sp[-1].data.f = *(gdouble*)sp[-1].data.p;
2445 MINT_IN_CASE(MINT_LDIND_REF)
2447 sp[-1].data.p = *(gpointer*)sp[-1].data.p;
2449 MINT_IN_CASE(MINT_STIND_REF)
2452 * (gpointer *) sp->data.p = sp[1].data.p;
2454 MINT_IN_CASE(MINT_STIND_I1)
2457 * (gint8 *) sp->data.p = (gint8)sp[1].data.i;
2459 MINT_IN_CASE(MINT_STIND_I2)
2462 * (gint16 *) sp->data.p = (gint16)sp[1].data.i;
2464 MINT_IN_CASE(MINT_STIND_I4)
2467 * (gint32 *) sp->data.p = sp[1].data.i;
2469 MINT_IN_CASE(MINT_STIND_I)
2472 * (mono_i *) sp->data.p = (mono_i)sp[1].data.p;
2474 MINT_IN_CASE(MINT_STIND_I8)
2477 * (gint64 *) sp->data.p = sp[1].data.l;
2479 MINT_IN_CASE(MINT_STIND_R4)
2482 * (float *) sp->data.p = (gfloat)sp[1].data.f;
2484 MINT_IN_CASE(MINT_STIND_R8)
2487 * (double *) sp->data.p = sp[1].data.f;
2489 #define BINOP(datamem, op) \
2491 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.datamem; \
2493 MINT_IN_CASE(MINT_ADD_I4)
2496 MINT_IN_CASE(MINT_ADD_I8)
2499 MINT_IN_CASE(MINT_ADD_R8)
2502 MINT_IN_CASE(MINT_ADD1_I4)
2506 MINT_IN_CASE(MINT_SUB_I4)
2509 MINT_IN_CASE(MINT_SUB_I8)
2512 MINT_IN_CASE(MINT_SUB_R8)
2515 MINT_IN_CASE(MINT_SUB1_I4)
2519 MINT_IN_CASE(MINT_MUL_I4)
2522 MINT_IN_CASE(MINT_MUL_I8)
2525 MINT_IN_CASE(MINT_MUL_R8)
2528 MINT_IN_CASE(MINT_DIV_I4)
2529 if (sp [-1].data.i == 0)
2530 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2531 if (sp [-1].data.i == (-1))
2532 THROW_EX (mono_get_exception_overflow (), ip);
2535 MINT_IN_CASE(MINT_DIV_I8)
2536 if (sp [-1].data.l == 0)
2537 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2538 if (sp [-1].data.l == (-1))
2539 THROW_EX (mono_get_exception_overflow (), ip);
2542 MINT_IN_CASE(MINT_DIV_R8)
2546 #define BINOP_CAST(datamem, op, type) \
2548 sp [-1].data.datamem = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
2550 MINT_IN_CASE(MINT_DIV_UN_I4)
2551 if (sp [-1].data.i == 0)
2552 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2553 BINOP_CAST(i, /, guint32);
2555 MINT_IN_CASE(MINT_DIV_UN_I8)
2556 if (sp [-1].data.l == 0)
2557 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2558 BINOP_CAST(l, /, guint64);
2560 MINT_IN_CASE(MINT_REM_I4)
2561 if (sp [-1].data.i == 0)
2562 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2565 MINT_IN_CASE(MINT_REM_I8)
2566 if (sp [-1].data.l == 0)
2567 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2570 MINT_IN_CASE(MINT_REM_R8)
2571 /* FIXME: what do we actually do here? */
2573 sp [-1].data.f = fmod (sp [-1].data.f, sp [0].data.f);
2576 MINT_IN_CASE(MINT_REM_UN_I4)
2577 if (sp [-1].data.i == 0)
2578 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2579 BINOP_CAST(i, %, guint32);
2581 MINT_IN_CASE(MINT_REM_UN_I8)
2582 if (sp [-1].data.l == 0)
2583 THROW_EX (mono_get_exception_divide_by_zero (), ip);
2584 BINOP_CAST(l, %, guint64);
2586 MINT_IN_CASE(MINT_AND_I4)
2589 MINT_IN_CASE(MINT_AND_I8)
2592 MINT_IN_CASE(MINT_OR_I4)
2595 MINT_IN_CASE(MINT_OR_I8)
2598 MINT_IN_CASE(MINT_XOR_I4)
2601 MINT_IN_CASE(MINT_XOR_I8)
2605 #define SHIFTOP(datamem, op) \
2607 sp [-1].data.datamem = sp [-1].data.datamem op sp [0].data.i; \
2610 MINT_IN_CASE(MINT_SHL_I4)
2613 MINT_IN_CASE(MINT_SHL_I8)
2616 MINT_IN_CASE(MINT_SHR_I4)
2619 MINT_IN_CASE(MINT_SHR_I8)
2622 MINT_IN_CASE(MINT_SHR_UN_I4)
2624 sp [-1].data.i = (guint32)sp [-1].data.i >> sp [0].data.i;
2627 MINT_IN_CASE(MINT_SHR_UN_I8)
2629 sp [-1].data.l = (guint64)sp [-1].data.l >> sp [0].data.i;
2632 MINT_IN_CASE(MINT_NEG_I4)
2633 sp [-1].data.i = - sp [-1].data.i;
2636 MINT_IN_CASE(MINT_NEG_I8)
2637 sp [-1].data.l = - sp [-1].data.l;
2640 MINT_IN_CASE(MINT_NEG_R8)
2641 sp [-1].data.f = - sp [-1].data.f;
2644 MINT_IN_CASE(MINT_NOT_I4)
2645 sp [-1].data.i = ~ sp [-1].data.i;
2648 MINT_IN_CASE(MINT_NOT_I8)
2649 sp [-1].data.l = ~ sp [-1].data.l;
2652 MINT_IN_CASE(MINT_CONV_I1_I4)
2653 sp [-1].data.i = (gint8)sp [-1].data.i;
2656 MINT_IN_CASE(MINT_CONV_I1_I8)
2657 sp [-1].data.i = (gint8)sp [-1].data.l;
2660 MINT_IN_CASE(MINT_CONV_I1_R8)
2661 sp [-1].data.i = (gint8)sp [-1].data.f;
2664 MINT_IN_CASE(MINT_CONV_U1_I4)
2665 sp [-1].data.i = (guint8)sp [-1].data.i;
2668 MINT_IN_CASE(MINT_CONV_U1_I8)
2669 sp [-1].data.i = (guint8)sp [-1].data.l;
2672 MINT_IN_CASE(MINT_CONV_U1_R8)
2673 sp [-1].data.i = (guint8)sp [-1].data.f;
2676 MINT_IN_CASE(MINT_CONV_I2_I4)
2677 sp [-1].data.i = (gint16)sp [-1].data.i;
2680 MINT_IN_CASE(MINT_CONV_I2_I8)
2681 sp [-1].data.i = (gint16)sp [-1].data.l;
2684 MINT_IN_CASE(MINT_CONV_I2_R8)
2685 sp [-1].data.i = (gint16)sp [-1].data.f;
2688 MINT_IN_CASE(MINT_CONV_U2_I4)
2689 sp [-1].data.i = (guint16)sp [-1].data.i;
2692 MINT_IN_CASE(MINT_CONV_U2_I8)
2693 sp [-1].data.i = (guint16)sp [-1].data.l;
2696 MINT_IN_CASE(MINT_CONV_U2_R8)
2697 sp [-1].data.i = (guint16)sp [-1].data.f;
2700 MINT_IN_CASE(MINT_CONV_I4_R8)
2701 sp [-1].data.i = (gint32)sp [-1].data.f;
2704 MINT_IN_CASE(MINT_CONV_U4_I8)
2705 MINT_IN_CASE(MINT_CONV_I4_I8)
2706 sp [-1].data.i = (gint32)sp [-1].data.l;
2709 MINT_IN_CASE(MINT_CONV_I4_I8_SP)
2710 sp [-2].data.i = (gint32)sp [-2].data.l;
2713 MINT_IN_CASE(MINT_CONV_U4_R8)
2714 sp [-1].data.i = (guint32)sp [-1].data.f;
2717 MINT_IN_CASE(MINT_CONV_I8_I4)
2718 sp [-1].data.l = sp [-1].data.i;
2721 MINT_IN_CASE(MINT_CONV_I8_I4_SP)
2722 sp [-2].data.l = sp [-2].data.i;
2725 MINT_IN_CASE(MINT_CONV_I8_U4)
2726 sp [-1].data.l = (guint32)sp [-1].data.i;
2729 MINT_IN_CASE(MINT_CONV_I8_R8)
2730 sp [-1].data.l = (gint64)sp [-1].data.f;
2733 MINT_IN_CASE(MINT_CONV_R4_I4)
2734 sp [-1].data.f = (float)sp [-1].data.i;
2737 MINT_IN_CASE(MINT_CONV_R4_I8)
2738 sp [-1].data.f = (float)sp [-1].data.l;
2741 MINT_IN_CASE(MINT_CONV_R4_R8)
2742 sp [-1].data.f = (float)sp [-1].data.f;
2745 MINT_IN_CASE(MINT_CONV_R8_I4)
2746 sp [-1].data.f = (double)sp [-1].data.i;
2749 MINT_IN_CASE(MINT_CONV_R8_I8)
2750 sp [-1].data.f = (double)sp [-1].data.l;
2753 MINT_IN_CASE(MINT_CONV_U8_I4)
2754 sp [-1].data.l = sp [-1].data.i & 0xffffffff;
2757 MINT_IN_CASE(MINT_CONV_U8_R8)
2758 sp [-1].data.l = (guint64)sp [-1].data.f;
2762 MINT_IN_CASE(MINT_CPOBJ) {
2765 vtklass = rtm->data_items[READ32 (ip)];
2768 memcpy (sp [0].data.p, sp [1].data.p, mono_class_value_size (vtklass, NULL));
2772 MINT_IN_CASE(MINT_LDOBJ) {
2775 c = rtm->data_items[* (guint16 *)(ip + 1)];
2777 if (c->byval_arg.type != MONO_TYPE_VALUETYPE || c->byval_arg.data.klass->enumtype) {
2779 stackval_from_data (&c->byval_arg, &sp [-1], p, FALSE);
2781 size = mono_class_value_size (c, NULL);
2783 sp [-1].data.p = vt_sp;
2784 memcpy(vt_sp, p, size);
2785 vt_sp += (size + 7) & ~7;
2789 MINT_IN_CASE(MINT_LDSTR)
2790 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
2794 MINT_IN_CASE(MINT_NEWOBJ) {
2795 MonoClass *newobj_class;
2796 MonoMethodSignature *csig;
2797 stackval valuetype_this;
2803 token = * (guint16 *)(ip + 1);
2806 child_frame.runtime_method = rtm->data_items [token];
2807 csig = mono_method_signature (child_frame.runtime_method->method);
2808 newobj_class = child_frame.runtime_method->method->klass;
2809 /*if (profiling_classes) {
2810 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, newobj_class));
2812 g_hash_table_insert (profiling_classes, newobj_class, GUINT_TO_POINTER (count));
2816 if (newobj_class->parent == mono_defaults.array_class) {
2817 sp -= csig->param_count;
2818 o = ves_array_create (context->domain, newobj_class, csig, sp);
2819 goto array_constructed;
2823 * First arg is the object.
2825 if (newobj_class->valuetype) {
2826 if (!newobj_class->enumtype && (newobj_class->byval_arg.type == MONO_TYPE_VALUETYPE)) {
2827 child_frame.obj = vt_sp;
2828 valuetype_this.data.p = vt_sp;
2830 memset (&valuetype_this, 0, sizeof (stackval));
2831 child_frame.obj = &valuetype_this;
2834 if (newobj_class != mono_defaults.string_class) {
2835 context->managed_code = 0;
2836 o = mono_object_new_checked (context->domain, newobj_class, &error);
2837 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2838 context->managed_code = 1;
2839 if (*abort_requested)
2840 mono_thread_interruption_checkpoint ();
2841 child_frame.obj = o;
2843 child_frame.retval = &retval;
2847 if (csig->param_count) {
2848 sp -= csig->param_count;
2849 child_frame.stack_args = sp;
2851 child_frame.stack_args = NULL;
2854 g_assert (csig->call_convention == MONO_CALL_DEFAULT);
2856 child_frame.ip = NULL;
2857 child_frame.ex = NULL;
2859 ves_exec_method_with_context (&child_frame, context);
2861 context->current_frame = frame;
2863 if (child_frame.ex) {
2865 * An exception occurred, need to run finally, fault and catch handlers..
2867 frame->ex = child_frame.ex;
2868 goto handle_finally;
2871 * a constructor returns void, but we need to return the object we created
2874 if (newobj_class->valuetype && !newobj_class->enumtype) {
2875 *sp = valuetype_this;
2876 } else if (newobj_class == mono_defaults.string_class) {
2884 MINT_IN_CASE(MINT_CASTCLASS)
2885 c = rtm->data_items [*(guint16 *)(ip + 1)];
2886 if ((o = sp [-1].data.p)) {
2887 if (c->marshalbyref) {
2888 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2889 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2891 THROW_EX (mono_get_exception_invalid_cast (), ip);
2893 MonoVTable *vt = o->vtable;
2894 MonoClass *oklass = vt->klass;
2895 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2896 g_error ("FIXME: interface method lookup");
2897 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2898 THROW_EX (mono_get_exception_invalid_cast (), ip);
2900 } else if (c->rank) {
2901 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2902 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2904 THROW_EX (mono_get_exception_invalid_cast (), ip);
2905 } else if (!mono_class_has_parent (oklass, c)) {
2906 THROW_EX (mono_get_exception_invalid_cast (), ip);
2912 MINT_IN_CASE(MINT_ISINST)
2913 c = rtm->data_items [*(guint16 *)(ip + 1)];
2914 if ((o = sp [-1].data.p)) {
2915 if (c->marshalbyref) {
2916 MonoObject *isinst_obj = mono_object_isinst_mbyref_checked (o, c, &error);
2917 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2919 sp [-1].data.p = NULL;
2921 MonoVTable *vt = o->vtable;
2922 MonoClass *oklass = vt->klass;
2923 if (c->flags & TYPE_ATTRIBUTE_INTERFACE) {
2924 g_error ("FIXME: interface method lookup");
2925 if (c->interface_id > vt->max_interface_id /* || vt->interface_offsets [c->interface_id] == 0 */) {
2926 sp [-1].data.p = NULL;
2928 } else if (c->rank) {
2929 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2930 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2932 sp [-1].data.p = NULL;
2933 } else if (!mono_class_has_parent (oklass, c)) {
2934 sp [-1].data.p = NULL;
2940 MINT_IN_CASE(MINT_CONV_R_UN_I4)
2941 sp [-1].data.f = (double)(guint32)sp [-1].data.i;
2944 MINT_IN_CASE(MINT_CONV_R_UN_I8)
2945 sp [-1].data.f = (double)(guint64)sp [-1].data.l;
2948 MINT_IN_CASE(MINT_UNBOX)
2949 c = rtm->data_items[*(guint16 *)(ip + 1)];
2953 THROW_EX (mono_get_exception_null_reference (), ip);
2955 MonoObject *isinst_obj = mono_object_isinst_checked (o, c, &error);
2956 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2957 if (!(isinst_obj || ((o->vtable->klass->rank == 0) && (o->vtable->klass->element_class == c->element_class))))
2958 THROW_EX (mono_get_exception_invalid_cast (), ip);
2960 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
2961 g_error ("unbox: implement vt");
2963 stackval_from_data (&c->byval_arg, &sp [-1], mono_object_unbox (o), FALSE);
2967 MINT_IN_CASE(MINT_THROW)
2969 frame->ex_handler = NULL;
2971 sp->data.p = mono_get_exception_null_reference ();
2972 THROW_EX ((MonoException *)sp->data.p, ip);
2974 MINT_IN_CASE(MINT_LDFLDA)
2977 THROW_EX (mono_get_exception_null_reference (), ip);
2978 sp[-1].data.p = (char *)o + * (guint16 *)(ip + 1);
2981 MINT_IN_CASE(MINT_CKNULL)
2984 THROW_EX (mono_get_exception_null_reference (), ip);
2988 #define LDFLD(datamem, fieldtype) \
2989 o = sp [-1].data.p; \
2991 THROW_EX (mono_get_exception_null_reference (), ip); \
2992 sp[-1].data.datamem = * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) ; \
2995 MINT_IN_CASE(MINT_LDFLD_I1) LDFLD(i, gint8); MINT_IN_BREAK;
2996 MINT_IN_CASE(MINT_LDFLD_U1) LDFLD(i, guint8); MINT_IN_BREAK;
2997 MINT_IN_CASE(MINT_LDFLD_I2) LDFLD(i, gint16); MINT_IN_BREAK;
2998 MINT_IN_CASE(MINT_LDFLD_U2) LDFLD(i, guint16); MINT_IN_BREAK;
2999 MINT_IN_CASE(MINT_LDFLD_I4) LDFLD(i, gint32); MINT_IN_BREAK;
3000 MINT_IN_CASE(MINT_LDFLD_I8) LDFLD(l, gint64); MINT_IN_BREAK;
3001 MINT_IN_CASE(MINT_LDFLD_R4) LDFLD(f, float); MINT_IN_BREAK;
3002 MINT_IN_CASE(MINT_LDFLD_R8) LDFLD(f, double); MINT_IN_BREAK;
3003 MINT_IN_CASE(MINT_LDFLD_O) LDFLD(p, gpointer); MINT_IN_BREAK;
3004 MINT_IN_CASE(MINT_LDFLD_P) LDFLD(p, gpointer); MINT_IN_BREAK;
3006 MINT_IN_CASE(MINT_LDFLD_VT)
3009 THROW_EX (mono_get_exception_null_reference (), ip);
3010 i32 = READ32(ip + 2);
3011 sp [-1].data.p = vt_sp;
3012 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3013 vt_sp += (i32 + 7) & ~7;
3017 MINT_IN_CASE(MINT_LDRMFLD) {
3019 MonoClassField *field;
3024 THROW_EX (mono_get_exception_null_reference (), ip);
3025 field = rtm->data_items[* (guint16 *)(ip + 1)];
3027 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
3028 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3030 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3031 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3033 addr = (char*)o + field->offset;
3036 stackval_from_data (field->type, &sp [-1], addr, FALSE);
3040 MINT_IN_CASE(MINT_LDRMFLD_VT) {
3041 MonoClassField *field;
3047 THROW_EX (mono_get_exception_null_reference (), ip);
3048 field = rtm->data_items[* (guint16 *)(ip + 1)];
3049 i32 = READ32(ip + 2);
3051 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
3052 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3053 addr = mono_load_remote_field_checked (o, klass, field, &tmp, &error);
3054 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3056 addr = (char*)o + field->offset;
3059 sp [-1].data.p = vt_sp;
3060 memcpy(sp [-1].data.p, (char *)o + * (guint16 *)(ip + 1), i32);
3061 vt_sp += (i32 + 7) & ~7;
3062 memcpy(sp [-1].data.p, addr, i32);
3066 #define STFLD(datamem, fieldtype) \
3067 o = sp [-2].data.p; \
3069 THROW_EX (mono_get_exception_null_reference (), ip); \
3071 * (fieldtype *)((char *)o + * (guint16 *)(ip + 1)) = sp[1].data.datamem; \
3074 MINT_IN_CASE(MINT_STFLD_I1) STFLD(i, gint8); MINT_IN_BREAK;
3075 MINT_IN_CASE(MINT_STFLD_U1) STFLD(i, guint8); MINT_IN_BREAK;
3076 MINT_IN_CASE(MINT_STFLD_I2) STFLD(i, gint16); MINT_IN_BREAK;
3077 MINT_IN_CASE(MINT_STFLD_U2) STFLD(i, guint16); MINT_IN_BREAK;
3078 MINT_IN_CASE(MINT_STFLD_I4) STFLD(i, gint32); MINT_IN_BREAK;
3079 MINT_IN_CASE(MINT_STFLD_I8) STFLD(l, gint64); MINT_IN_BREAK;
3080 MINT_IN_CASE(MINT_STFLD_R4) STFLD(f, float); MINT_IN_BREAK;
3081 MINT_IN_CASE(MINT_STFLD_R8) STFLD(f, double); MINT_IN_BREAK;
3082 MINT_IN_CASE(MINT_STFLD_O) STFLD(p, gpointer); MINT_IN_BREAK;
3083 MINT_IN_CASE(MINT_STFLD_P) STFLD(p, gpointer); MINT_IN_BREAK;
3085 MINT_IN_CASE(MINT_STFLD_VT)
3088 THROW_EX (mono_get_exception_null_reference (), ip);
3089 i32 = READ32(ip + 2);
3091 memcpy((char *)o + * (guint16 *)(ip + 1), sp [1].data.p, i32);
3092 vt_sp -= (i32 + 7) & ~7;
3096 MINT_IN_CASE(MINT_STRMFLD) {
3097 MonoClassField *field;
3101 THROW_EX (mono_get_exception_null_reference (), ip);
3103 field = rtm->data_items[* (guint16 *)(ip + 1)];
3106 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
3107 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3108 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3109 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3111 stackval_to_data (field->type, &sp [-1], (char*)o + field->offset, FALSE);
3116 MINT_IN_CASE(MINT_STRMFLD_VT) {
3117 MonoClassField *field;
3121 THROW_EX (mono_get_exception_null_reference (), ip);
3122 field = rtm->data_items[* (guint16 *)(ip + 1)];
3123 i32 = READ32(ip + 2);
3126 if (o->vtable->klass == mono_defaults.transparent_proxy_class) {
3127 MonoClass *klass = ((MonoTransparentProxy*)o)->remote_class->proxy_class;
3128 mono_store_remote_field_checked (o, klass, field, &sp [-1].data, &error);
3129 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3131 memcpy((char*)o + field->offset, sp [-1].data.p, i32);
3134 vt_sp -= (i32 + 7) & ~7;
3137 MINT_IN_CASE(MINT_LDSFLDA) {
3138 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3139 MonoVTable *vt = mono_class_vtable (context->domain, field->parent);
3142 if (!vt->initialized) {
3144 mono_runtime_class_init_full (vt, &error);
3145 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3149 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3150 sp->data.p = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3152 sp->data.p = (char*)(vt->vtable) + field->offset;
3156 MINT_IN_CASE(MINT_LDSFLD) {
3158 MonoClassField *field;
3161 field = rtm->data_items[*(guint16 *)(ip + 1)];
3162 vt = rtm->data_items [*(guint16 *)(ip + 2)];
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;
3174 stackval_from_data (field->type, sp, addr, FALSE);
3178 MINT_IN_CASE(MINT_LDSFLD_I4) {
3179 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3180 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3181 if (!vt->initialized) {
3183 mono_runtime_class_init_full (vt, &error);
3184 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3187 sp->data.i = * (gint32 *)((char*)(vt->vtable) + field->offset);
3191 MINT_IN_CASE(MINT_LDSFLD_O) {
3192 MonoClassField *field = rtm->data_items[*(guint16 *)(ip + 1)];
3193 MonoVTable *vt = rtm->data_items [*(guint16 *)(ip + 2)];
3194 if (!vt->initialized) {
3196 mono_runtime_class_init_full (vt, &error);
3197 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3200 sp->data.p = * (gpointer *)((char*)(vt->vtable) + field->offset);
3204 MINT_IN_CASE(MINT_LDSFLD_VT) {
3206 MonoClassField *field;
3211 token = * (guint16 *)(ip + 1);
3212 size = READ32(ip + 2);
3213 field = rtm->data_items[token];
3216 vt = mono_class_vtable (context->domain, field->parent);
3217 if (!vt->initialized) {
3219 mono_runtime_class_init_full (vt, &error);
3220 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3223 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3224 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3226 addr = (char*)(vt->vtable) + field->offset;
3229 vt_sp += (size + 7) & ~7;
3230 stackval_from_data (field->type, sp, addr, FALSE);
3234 MINT_IN_CASE(MINT_STSFLD) {
3236 MonoClassField *field;
3240 token = * (guint16 *)(ip + 1);
3241 field = rtm->data_items[token];
3245 vt = mono_class_vtable (context->domain, field->parent);
3246 if (!vt->initialized) {
3248 mono_runtime_class_init_full (vt, &error);
3249 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3252 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3253 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3255 addr = (char*)(vt->vtable) + field->offset;
3257 stackval_to_data (field->type, sp, addr, FALSE);
3260 MINT_IN_CASE(MINT_STSFLD_VT) {
3262 MonoClassField *field;
3267 token = * (guint16 *)(ip + 1);
3268 size = READ32(ip + 2);
3269 field = rtm->data_items[token];
3272 vt = mono_class_vtable (context->domain, field->parent);
3273 if (!vt->initialized) {
3275 mono_runtime_class_init_full (vt, &error);
3276 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3279 if (context->domain->special_static_fields && (addr = g_hash_table_lookup (context->domain->special_static_fields, field)))
3280 addr = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
3282 addr = (char*)(vt->vtable) + field->offset;
3284 stackval_to_data (field->type, sp, addr, FALSE);
3285 vt_sp -= (size + 7) & ~7;
3288 MINT_IN_CASE(MINT_STOBJ_VT) {
3290 c = rtm->data_items[* (guint16 *)(ip + 1)];
3292 size = mono_class_value_size (c, NULL);
3293 memcpy(sp [-2].data.p, sp [-1].data.p, size);
3294 vt_sp -= (size + 7) & ~7;
3298 MINT_IN_CASE(MINT_STOBJ) {
3300 c = rtm->data_items[* (guint16 *)(ip + 1)];
3302 size = mono_class_value_size (c, NULL);
3303 memcpy(sp [-2].data.p, &sp [-1].data, size);
3307 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_R8)
3308 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3309 THROW_EX (mono_get_exception_overflow (), ip);
3310 sp [-1].data.i = (guint32)sp [-1].data.f;
3313 MINT_IN_CASE(MINT_CONV_OVF_U8_I4)
3314 if (sp [-1].data.i < 0)
3315 THROW_EX (mono_get_exception_overflow (), ip);
3316 sp [-1].data.l = sp [-1].data.i;
3319 MINT_IN_CASE(MINT_CONV_OVF_U8_R8)
3320 MINT_IN_CASE(MINT_CONV_OVF_I8_UN_R8)
3321 if (sp [-1].data.f < 0 || sp [-1].data.f > 9223372036854775807LL)
3322 THROW_EX (mono_get_exception_overflow (), ip);
3323 sp [-1].data.l = (guint64)sp [-1].data.f;
3326 MINT_IN_CASE(MINT_CONV_OVF_I8_R8)
3327 if (sp [-1].data.f < MYGINT64_MIN || sp [-1].data.f > MYGINT64_MAX)
3328 THROW_EX (mono_get_exception_overflow (), ip);
3329 sp [-1].data.l = (gint64)sp [-1].data.f;
3332 MINT_IN_CASE(MINT_CONV_OVF_I4_UN_I8)
3333 if ((mono_u)sp [-1].data.l > MYGUINT32_MAX)
3334 THROW_EX (mono_get_exception_overflow (), ip);
3335 sp [-1].data.i = (mono_u)sp [-1].data.l;
3338 MINT_IN_CASE(MINT_BOX)
3339 c = rtm->data_items [* (guint16 *)(ip + 1)];
3341 if (c->byval_arg.type == MONO_TYPE_VALUETYPE && !c->enumtype) {
3342 int size = mono_class_value_size (c, NULL);
3343 sp [-1].data.p = mono_value_box_checked (context->domain, c, sp [-1].data.p, &error);
3344 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3345 size = (size + 7) & ~7;
3349 stackval_to_data (&c->byval_arg, &sp [-1], (char*)&sp [-1], FALSE);
3350 sp [-1].data.p = mono_value_box_checked (context->domain, c, &sp [-1], &error);
3351 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3355 MINT_IN_CASE(MINT_NEWARR)
3356 sp [-1].data.p = (MonoObject*) mono_array_new_checked (context->domain, rtm->data_items[*(guint16 *)(ip + 1)], sp [-1].data.i, &error);
3357 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3359 /*if (profiling_classes) {
3360 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (profiling_classes, o->vtable->klass));
3362 g_hash_table_insert (profiling_classes, o->vtable->klass, GUINT_TO_POINTER (count));
3366 MINT_IN_CASE(MINT_LDLEN)
3369 THROW_EX (mono_get_exception_null_reference (), ip);
3370 sp [-1].data.nati = mono_array_length ((MonoArray *)o);
3373 MINT_IN_CASE(MINT_GETCHR) {
3377 THROW_EX (mono_get_exception_null_reference (), ip);
3378 i32 = sp [-1].data.i;
3379 if (i32 < 0 || i32 >= mono_string_length (s))
3380 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3382 sp [-1].data.i = mono_string_chars(s)[i32];
3386 MINT_IN_CASE(MINT_STRLEN)
3388 sp [-1].data.i = mono_string_length ((MonoString*)sp [-1].data.p);
3390 MINT_IN_CASE(MINT_ARRAY_RANK)
3393 THROW_EX (mono_get_exception_null_reference (), ip);
3394 sp [-1].data.i = mono_object_class (sp [-1].data.p)->rank;
3397 MINT_IN_CASE(MINT_LDELEMA) {
3401 /*token = READ32 (ip)*/;
3407 aindex = sp [1].data.i;
3408 if (aindex >= mono_array_length ((MonoArray *) o))
3409 THROW_EX (mono_get_exception_index_out_of_range (), ip - 2);
3411 /* check the array element corresponds to token */
3412 esize = mono_array_element_size (((MonoArray *) o)->obj.vtable->klass);
3414 sp->data.p = mono_array_addr_with_size ((MonoArray *) o, esize, aindex);
3418 MINT_IN_CASE(MINT_LDELEM_I1) /* fall through */
3419 MINT_IN_CASE(MINT_LDELEM_U1) /* fall through */
3420 MINT_IN_CASE(MINT_LDELEM_I2) /* fall through */
3421 MINT_IN_CASE(MINT_LDELEM_U2) /* fall through */
3422 MINT_IN_CASE(MINT_LDELEM_I4) /* fall through */
3423 MINT_IN_CASE(MINT_LDELEM_U4) /* fall through */
3424 MINT_IN_CASE(MINT_LDELEM_I8) /* fall through */
3425 MINT_IN_CASE(MINT_LDELEM_I) /* fall through */
3426 MINT_IN_CASE(MINT_LDELEM_R4) /* fall through */
3427 MINT_IN_CASE(MINT_LDELEM_R8) /* fall through */
3428 MINT_IN_CASE(MINT_LDELEM_REF) {
3436 THROW_EX (mono_get_exception_null_reference (), ip);
3438 aindex = sp [1].data.i;
3439 if (aindex >= mono_array_length (o))
3440 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3443 * FIXME: throw mono_get_exception_array_type_mismatch () if needed
3446 case MINT_LDELEM_I1:
3447 sp [0].data.i = mono_array_get (o, gint8, aindex);
3449 case MINT_LDELEM_U1:
3450 sp [0].data.i = mono_array_get (o, guint8, aindex);
3452 case MINT_LDELEM_I2:
3453 sp [0].data.i = mono_array_get (o, gint16, aindex);
3455 case MINT_LDELEM_U2:
3456 sp [0].data.i = mono_array_get (o, guint16, aindex);
3459 sp [0].data.nati = mono_array_get (o, mono_i, aindex);
3461 case MINT_LDELEM_I4:
3462 sp [0].data.i = mono_array_get (o, gint32, aindex);
3464 case MINT_LDELEM_U4:
3465 sp [0].data.i = mono_array_get (o, guint32, aindex);
3467 case MINT_LDELEM_I8:
3468 sp [0].data.l = mono_array_get (o, guint64, aindex);
3470 case MINT_LDELEM_R4:
3471 sp [0].data.f = mono_array_get (o, float, aindex);
3473 case MINT_LDELEM_R8:
3474 sp [0].data.f = mono_array_get (o, double, aindex);
3476 case MINT_LDELEM_REF:
3477 sp [0].data.p = mono_array_get (o, gpointer, aindex);
3487 MINT_IN_CASE(MINT_STELEM_I) /* fall through */
3488 MINT_IN_CASE(MINT_STELEM_I1) /* fall through */
3489 MINT_IN_CASE(MINT_STELEM_I2) /* fall through */
3490 MINT_IN_CASE(MINT_STELEM_I4) /* fall through */
3491 MINT_IN_CASE(MINT_STELEM_I8) /* fall through */
3492 MINT_IN_CASE(MINT_STELEM_R4) /* fall through */
3493 MINT_IN_CASE(MINT_STELEM_R8) /* fall through */
3494 MINT_IN_CASE(MINT_STELEM_REF) {
3501 THROW_EX (mono_get_exception_null_reference (), ip);
3503 aindex = sp [1].data.i;
3504 if (aindex >= mono_array_length ((MonoArray *)o))
3505 THROW_EX (mono_get_exception_index_out_of_range (), ip);
3509 mono_array_set ((MonoArray *)o, mono_i, aindex, sp [2].data.nati);
3511 case MINT_STELEM_I1:
3512 mono_array_set ((MonoArray *)o, gint8, aindex, sp [2].data.i);
3514 case MINT_STELEM_I2:
3515 mono_array_set ((MonoArray *)o, gint16, aindex, sp [2].data.i);
3517 case MINT_STELEM_I4:
3518 mono_array_set ((MonoArray *)o, gint32, aindex, sp [2].data.i);
3520 case MINT_STELEM_I8:
3521 mono_array_set ((MonoArray *)o, gint64, aindex, sp [2].data.l);
3523 case MINT_STELEM_R4:
3524 mono_array_set ((MonoArray *)o, float, aindex, sp [2].data.f);
3526 case MINT_STELEM_R8:
3527 mono_array_set ((MonoArray *)o, double, aindex, sp [2].data.f);
3529 case MINT_STELEM_REF: {
3530 MonoObject *isinst_obj = mono_object_isinst_checked (sp [2].data.p, mono_object_class (o)->element_class, &error);
3531 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3532 if (sp [2].data.p && !isinst_obj)
3533 THROW_EX (mono_get_exception_array_type_mismatch (), ip);
3534 mono_array_set ((MonoArray *)o, gpointer, aindex, sp [2].data.p);
3543 MINT_IN_CASE(MINT_CONV_OVF_I4_U4)
3544 if (sp [-1].data.i < 0)
3545 THROW_EX (mono_get_exception_overflow (), ip);
3548 MINT_IN_CASE(MINT_CONV_OVF_I4_I8)
3549 if (sp [-1].data.l < MYGINT32_MIN || sp [-1].data.l > MYGINT32_MAX)
3550 THROW_EX (mono_get_exception_overflow (), ip);
3551 sp [-1].data.i = (gint32) sp [-1].data.l;
3554 MINT_IN_CASE(MINT_CONV_OVF_I4_R8)
3555 if (sp [-1].data.f < MYGINT32_MIN || sp [-1].data.f > MYGINT32_MAX)
3556 THROW_EX (mono_get_exception_overflow (), ip);
3557 sp [-1].data.i = (gint32) sp [-1].data.f;
3560 MINT_IN_CASE(MINT_CONV_OVF_U4_I4)
3561 if (sp [-1].data.i < 0)
3562 THROW_EX (mono_get_exception_overflow (), ip);
3565 MINT_IN_CASE(MINT_CONV_OVF_U4_I8)
3566 if (sp [-1].data.l < 0 || sp [-1].data.l > MYGUINT32_MAX)
3567 THROW_EX (mono_get_exception_overflow (), ip);
3568 sp [-1].data.i = (guint32) sp [-1].data.l;
3571 MINT_IN_CASE(MINT_CONV_OVF_U4_R8)
3572 if (sp [-1].data.f < 0 || sp [-1].data.f > MYGUINT32_MAX)
3573 THROW_EX (mono_get_exception_overflow (), ip);
3574 sp [-1].data.i = (guint32) sp [-1].data.f;
3577 MINT_IN_CASE(MINT_CONV_OVF_I2_I4)
3578 if (sp [-1].data.i < -32768 || sp [-1].data.i > 32767)
3579 THROW_EX (mono_get_exception_overflow (), ip);
3582 MINT_IN_CASE(MINT_CONV_OVF_I2_I8)
3583 if (sp [-1].data.l < -32768 || sp [-1].data.l > 32767)
3584 THROW_EX (mono_get_exception_overflow (), ip);
3585 sp [-1].data.i = (gint16) sp [-1].data.l;
3588 MINT_IN_CASE(MINT_CONV_OVF_I2_R8)
3589 if (sp [-1].data.f < -32768 || sp [-1].data.f > 32767)
3590 THROW_EX (mono_get_exception_overflow (), ip);
3591 sp [-1].data.i = (gint16) sp [-1].data.f;
3594 MINT_IN_CASE(MINT_CONV_OVF_U2_I4)
3595 if (sp [-1].data.i < 0 || sp [-1].data.i > 65535)
3596 THROW_EX (mono_get_exception_overflow (), ip);
3599 MINT_IN_CASE(MINT_CONV_OVF_U2_I8)
3600 if (sp [-1].data.l < 0 || sp [-1].data.l > 65535)
3601 THROW_EX (mono_get_exception_overflow (), ip);
3602 sp [-1].data.i = (guint16) sp [-1].data.l;
3605 MINT_IN_CASE(MINT_CONV_OVF_U2_R8)
3606 if (sp [-1].data.f < 0 || sp [-1].data.f > 65535)
3607 THROW_EX (mono_get_exception_overflow (), ip);
3608 sp [-1].data.i = (guint16) sp [-1].data.f;
3611 MINT_IN_CASE(MINT_CONV_OVF_I1_I4)
3612 if (sp [-1].data.i < -128 || sp [-1].data.i > 127)
3613 THROW_EX (mono_get_exception_overflow (), ip);
3616 MINT_IN_CASE(MINT_CONV_OVF_I1_I8)
3617 if (sp [-1].data.l < -128 || sp [-1].data.l > 127)
3618 THROW_EX (mono_get_exception_overflow (), ip);
3619 sp [-1].data.i = (gint8) sp [-1].data.l;
3622 MINT_IN_CASE(MINT_CONV_OVF_I1_R8)
3623 if (sp [-1].data.f < -128 || sp [-1].data.f > 127)
3624 THROW_EX (mono_get_exception_overflow (), ip);
3625 sp [-1].data.i = (gint8) sp [-1].data.f;
3628 MINT_IN_CASE(MINT_CONV_OVF_U1_I4)
3629 if (sp [-1].data.i < 0 || sp [-1].data.i > 255)
3630 THROW_EX (mono_get_exception_overflow (), ip);
3633 MINT_IN_CASE(MINT_CONV_OVF_U1_I8)
3634 if (sp [-1].data.l < 0 || sp [-1].data.l > 255)
3635 THROW_EX (mono_get_exception_overflow (), ip);
3636 sp [-1].data.i = (guint8) sp [-1].data.l;
3639 MINT_IN_CASE(MINT_CONV_OVF_U1_R8)
3640 if (sp [-1].data.f < 0 || sp [-1].data.f > 255)
3641 THROW_EX (mono_get_exception_overflow (), ip);
3642 sp [-1].data.i = (guint8) sp [-1].data.f;
3646 MINT_IN_CASE(MINT_LDELEM)
3647 MINT_IN_CASE(MINT_STELEM)
3648 MINT_IN_CASE(MINT_UNBOX_ANY)
3650 MINT_IN_CASE(MINT_REFANYVAL) ves_abort(); MINT_IN_BREAK;
3652 MINT_IN_CASE(MINT_CKFINITE)
3653 if (!isfinite(sp [-1].data.f))
3654 THROW_EX (mono_get_exception_arithmetic (), ip);
3658 MINT_IN_CASE(MINT_MKREFANY) ves_abort(); MINT_IN_BREAK;
3660 MINT_IN_CASE(MINT_LDTOKEN)
3663 * (gpointer *)sp->data.p = rtm->data_items[*(guint16 *)(ip + 1)];
3667 MINT_IN_CASE(MINT_ADD_OVF_I4)
3668 if (CHECK_ADD_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3669 THROW_EX (mono_get_exception_overflow (), ip);
3672 MINT_IN_CASE(MINT_ADD_OVF_I8)
3673 if (CHECK_ADD_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3674 THROW_EX (mono_get_exception_overflow (), ip);
3677 MINT_IN_CASE(MINT_ADD_OVF_UN_I4)
3678 if (CHECK_ADD_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3679 THROW_EX (mono_get_exception_overflow (), ip);
3680 BINOP_CAST(i, +, guint32);
3682 MINT_IN_CASE(MINT_ADD_OVF_UN_I8)
3683 if (CHECK_ADD_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3684 THROW_EX (mono_get_exception_overflow (), ip);
3685 BINOP_CAST(l, +, guint64);
3687 MINT_IN_CASE(MINT_MUL_OVF_I4)
3688 if (CHECK_MUL_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3689 THROW_EX (mono_get_exception_overflow (), ip);
3692 MINT_IN_CASE(MINT_MUL_OVF_I8)
3693 if (CHECK_MUL_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3694 THROW_EX (mono_get_exception_overflow (), ip);
3697 MINT_IN_CASE(MINT_MUL_OVF_UN_I4)
3698 if (CHECK_MUL_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3699 THROW_EX (mono_get_exception_overflow (), ip);
3700 BINOP_CAST(i, *, guint32);
3702 MINT_IN_CASE(MINT_MUL_OVF_UN_I8)
3703 if (CHECK_MUL_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3704 THROW_EX (mono_get_exception_overflow (), ip);
3705 BINOP_CAST(l, *, guint64);
3707 MINT_IN_CASE(MINT_SUB_OVF_I4)
3708 if (CHECK_SUB_OVERFLOW (sp [-2].data.i, sp [-1].data.i))
3709 THROW_EX (mono_get_exception_overflow (), ip);
3712 MINT_IN_CASE(MINT_SUB_OVF_I8)
3713 if (CHECK_SUB_OVERFLOW64 (sp [-2].data.l, sp [-1].data.l))
3714 THROW_EX (mono_get_exception_overflow (), ip);
3717 MINT_IN_CASE(MINT_SUB_OVF_UN_I4)
3718 if (CHECK_SUB_OVERFLOW_UN (sp [-2].data.i, sp [-1].data.i))
3719 THROW_EX (mono_get_exception_overflow (), ip);
3720 BINOP_CAST(i, -, guint32);
3722 MINT_IN_CASE(MINT_SUB_OVF_UN_I8)
3723 if (CHECK_SUB_OVERFLOW64_UN (sp [-2].data.l, sp [-1].data.l))
3724 THROW_EX (mono_get_exception_overflow (), ip);
3725 BINOP_CAST(l, -, guint64);
3727 MINT_IN_CASE(MINT_ENDFINALLY)
3729 ip = finally_ips->data;
3730 finally_ips = g_slist_remove (finally_ips, ip);
3737 MINT_IN_CASE(MINT_LEAVE) /* Fall through */
3738 MINT_IN_CASE(MINT_LEAVE_S)
3739 while (sp > frame->stack) {
3743 if (*ip == MINT_LEAVE_S) {
3744 ip += (short) *(ip + 1);
3746 ip += (gint32) READ32 (ip + 1);
3749 if (frame->ex_handler != NULL && MONO_OFFSET_IN_HANDLER(frame->ex_handler, frame->ip - rtm->code)) {
3750 frame->ex_handler = NULL;
3753 goto handle_finally;
3755 MINT_IN_CASE(MINT_ICALL_V_V)
3756 MINT_IN_CASE(MINT_ICALL_P_V)
3757 MINT_IN_CASE(MINT_ICALL_P_P)
3758 MINT_IN_CASE(MINT_ICALL_PP_V)
3759 MINT_IN_CASE(MINT_ICALL_PI_V)
3760 MINT_IN_CASE(MINT_ICALL_PP_P)
3761 MINT_IN_CASE(MINT_ICALL_PI_P)
3762 MINT_IN_CASE(MINT_ICALL_PPP_V)
3763 MINT_IN_CASE(MINT_ICALL_PPI_V)
3764 sp = do_icall (context, *ip, sp, rtm->data_items [*(guint16 *)(ip + 1)]);
3765 if (frame->ex != NULL)
3766 goto handle_exception;
3769 MINT_IN_CASE(MINT_MONO_LDPTR)
3770 sp->data.p = rtm->data_items [*(guint16 *)(ip + 1)];
3774 MINT_IN_CASE(MINT_MONO_NEWOBJ)
3775 sp->data.p = mono_object_new_checked (context->domain, rtm->data_items [*(guint16 *)(ip + 1)], &error);
3776 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3780 MINT_IN_CASE(MINT_MONO_FREE)
3783 g_error ("that doesn't seem right");
3784 g_free (sp->data.p);
3786 MINT_IN_CASE(MINT_MONO_RETOBJ)
3789 stackval_from_data (mono_method_signature (frame->runtime_method->method)->ret, frame->retval, sp->data.p,
3790 mono_method_signature (frame->runtime_method->method)->pinvoke);
3791 if (sp > frame->stack)
3792 g_warning ("retobj: more values on stack: %d", sp-frame->stack);
3795 #define RELOP(datamem, op) \
3797 sp [-1].data.i = sp [-1].data.datamem op sp [0].data.datamem; \
3799 MINT_IN_CASE(MINT_CEQ_I4)
3802 MINT_IN_CASE(MINT_CEQ0_I4)
3803 sp [-1].data.i = (sp [-1].data.i == 0);
3806 MINT_IN_CASE(MINT_CEQ_I8)
3809 MINT_IN_CASE(MINT_CEQ_R8)
3811 if (isunordered (sp [-1].data.f, sp [0].data.f))
3814 sp [-1].data.i = sp [-1].data.f == sp [0].data.f;
3817 MINT_IN_CASE(MINT_CGT_I4)
3820 MINT_IN_CASE(MINT_CGT_I8)
3823 MINT_IN_CASE(MINT_CGT_R8)
3825 if (isunordered (sp [-1].data.f, sp [0].data.f))
3828 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3832 #define RELOP_CAST(datamem, op, type) \
3834 sp [-1].data.i = (type)sp [-1].data.datamem op (type)sp [0].data.datamem; \
3837 MINT_IN_CASE(MINT_CGT_UN_I4)
3838 RELOP_CAST(i, >, guint32);
3840 MINT_IN_CASE(MINT_CGT_UN_I8)
3841 RELOP_CAST(l, >, guint64);
3843 MINT_IN_CASE(MINT_CGT_UN_R8)
3845 if (isunordered (sp [-1].data.f, sp [0].data.f))
3848 sp [-1].data.i = sp [-1].data.f > sp [0].data.f;
3851 MINT_IN_CASE(MINT_CLT_I4)
3854 MINT_IN_CASE(MINT_CLT_I8)
3857 MINT_IN_CASE(MINT_CLT_R8)
3859 if (isunordered (sp [-1].data.f, sp [0].data.f))
3862 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3865 MINT_IN_CASE(MINT_CLT_UN_I4)
3866 RELOP_CAST(i, <, guint32);
3868 MINT_IN_CASE(MINT_CLT_UN_I8)
3869 RELOP_CAST(l, <, guint64);
3871 MINT_IN_CASE(MINT_CLT_UN_R8)
3873 if (isunordered (sp [-1].data.f, sp [0].data.f))
3876 sp [-1].data.i = sp [-1].data.f < sp [0].data.f;
3879 MINT_IN_CASE(MINT_LDFTN) {
3880 sp->data.p = rtm->data_items [* (guint16 *)(ip + 1)];
3885 MINT_IN_CASE(MINT_LDVIRTFTN) {
3886 RuntimeMethod *m = rtm->data_items [* (guint16 *)(ip + 1)];
3890 THROW_EX (mono_get_exception_null_reference (), ip - 2);
3892 sp->data.p = get_virtual_method (context->domain, m, sp->data.p);
3897 MINT_IN_CASE(MINT_LDTHIS)
3898 sp->data.p = frame->obj;
3902 MINT_IN_CASE(MINT_STTHIS)
3904 frame->obj = sp->data.p;
3907 MINT_IN_CASE(MINT_LDTHISA)
3908 sp->data.p = &frame->obj;
3913 #define LDARG(datamem, argtype) \
3914 sp->data.datamem = * (argtype *)(frame->args + * (guint16 *)(ip + 1)); \
3918 MINT_IN_CASE(MINT_LDARG_I1) LDARG(i, gint8); MINT_IN_BREAK;
3919 MINT_IN_CASE(MINT_LDARG_U1) LDARG(i, guint8); MINT_IN_BREAK;
3920 MINT_IN_CASE(MINT_LDARG_I2) LDARG(i, gint16); MINT_IN_BREAK;
3921 MINT_IN_CASE(MINT_LDARG_U2) LDARG(i, guint16); MINT_IN_BREAK;
3922 MINT_IN_CASE(MINT_LDARG_I4) LDARG(i, gint32); MINT_IN_BREAK;
3923 MINT_IN_CASE(MINT_LDARG_I8) LDARG(l, gint64); MINT_IN_BREAK;
3924 MINT_IN_CASE(MINT_LDARG_R4) LDARG(f, float); MINT_IN_BREAK;
3925 MINT_IN_CASE(MINT_LDARG_R8) LDARG(f, double); MINT_IN_BREAK;
3926 MINT_IN_CASE(MINT_LDARG_O) LDARG(p, gpointer); MINT_IN_BREAK;
3927 MINT_IN_CASE(MINT_LDARG_P) LDARG(p, gpointer); MINT_IN_BREAK;
3929 MINT_IN_CASE(MINT_LDARG_VT)
3931 i32 = READ32(ip + 2);
3932 memcpy(sp->data.p, frame->args + * (guint16 *)(ip + 1), i32);
3933 vt_sp += (i32 + 7) & ~7;
3938 #define STARG(datamem, argtype) \
3940 * (argtype *)(frame->args + * (guint16 *)(ip + 1)) = sp->data.datamem; \
3943 MINT_IN_CASE(MINT_STARG_I1) STARG(i, gint8); MINT_IN_BREAK;
3944 MINT_IN_CASE(MINT_STARG_U1) STARG(i, guint8); MINT_IN_BREAK;
3945 MINT_IN_CASE(MINT_STARG_I2) STARG(i, gint16); MINT_IN_BREAK;
3946 MINT_IN_CASE(MINT_STARG_U2) STARG(i, guint16); MINT_IN_BREAK;
3947 MINT_IN_CASE(MINT_STARG_I4) STARG(i, gint32); MINT_IN_BREAK;
3948 MINT_IN_CASE(MINT_STARG_I8) STARG(l, gint64); MINT_IN_BREAK;
3949 MINT_IN_CASE(MINT_STARG_R4) STARG(f, float); MINT_IN_BREAK;
3950 MINT_IN_CASE(MINT_STARG_R8) STARG(f, double); MINT_IN_BREAK;
3951 MINT_IN_CASE(MINT_STARG_O) STARG(p, gpointer); MINT_IN_BREAK;
3952 MINT_IN_CASE(MINT_STARG_P) STARG(p, gpointer); MINT_IN_BREAK;
3954 MINT_IN_CASE(MINT_STARG_VT)
3955 i32 = READ32(ip + 2);
3957 memcpy(frame->args + * (guint16 *)(ip + 1), sp->data.p, i32);
3958 vt_sp -= (i32 + 7) & ~7;
3962 #define STINARG(datamem, argtype) \
3964 int n = * (guint16 *)(ip + 1); \
3965 * (argtype *)(frame->args + rtm->arg_offsets [n]) = frame->stack_args [n].data.datamem; \
3969 MINT_IN_CASE(MINT_STINARG_I1) STINARG(i, gint8); MINT_IN_BREAK;
3970 MINT_IN_CASE(MINT_STINARG_U1) STINARG(i, guint8); MINT_IN_BREAK;
3971 MINT_IN_CASE(MINT_STINARG_I2) STINARG(i, gint16); MINT_IN_BREAK;
3972 MINT_IN_CASE(MINT_STINARG_U2) STINARG(i, guint16); MINT_IN_BREAK;
3973 MINT_IN_CASE(MINT_STINARG_I4) STINARG(i, gint32); MINT_IN_BREAK;
3974 MINT_IN_CASE(MINT_STINARG_I8) STINARG(l, gint64); MINT_IN_BREAK;
3975 MINT_IN_CASE(MINT_STINARG_R4) STINARG(f, float); MINT_IN_BREAK;
3976 MINT_IN_CASE(MINT_STINARG_R8) STINARG(f, double); MINT_IN_BREAK;
3977 MINT_IN_CASE(MINT_STINARG_O) STINARG(p, gpointer); MINT_IN_BREAK;
3978 MINT_IN_CASE(MINT_STINARG_P) STINARG(p, gpointer); MINT_IN_BREAK;
3980 MINT_IN_CASE(MINT_STINARG_VT) {
3981 int n = * (guint16 *)(ip + 1);
3982 i32 = READ32(ip + 2);
3983 memcpy (frame->args + rtm->arg_offsets [n], frame->stack_args [n].data.p, i32);
3988 MINT_IN_CASE(MINT_LDARGA)
3989 sp->data.p = frame->args + * (guint16 *)(ip + 1);
3994 #define LDLOC(datamem, argtype) \
3995 sp->data.datamem = * (argtype *)(locals + * (guint16 *)(ip + 1)); \
3999 MINT_IN_CASE(MINT_LDLOC_I1) LDLOC(i, gint8); MINT_IN_BREAK;
4000 MINT_IN_CASE(MINT_LDLOC_U1) LDLOC(i, guint8); MINT_IN_BREAK;
4001 MINT_IN_CASE(MINT_LDLOC_I2) LDLOC(i, gint16); MINT_IN_BREAK;
4002 MINT_IN_CASE(MINT_LDLOC_U2) LDLOC(i, guint16); MINT_IN_BREAK;
4003 MINT_IN_CASE(MINT_LDLOC_I4) LDLOC(i, gint32); MINT_IN_BREAK;
4004 MINT_IN_CASE(MINT_LDLOC_I8) LDLOC(l, gint64); MINT_IN_BREAK;
4005 MINT_IN_CASE(MINT_LDLOC_R4) LDLOC(f, float); MINT_IN_BREAK;
4006 MINT_IN_CASE(MINT_LDLOC_R8) LDLOC(f, double); MINT_IN_BREAK;
4007 MINT_IN_CASE(MINT_LDLOC_O) LDLOC(p, gpointer); MINT_IN_BREAK;
4008 MINT_IN_CASE(MINT_LDLOC_P) LDLOC(p, gpointer); MINT_IN_BREAK;
4010 MINT_IN_CASE(MINT_LDLOC_VT)
4012 i32 = READ32(ip + 2);
4013 memcpy(sp->data.p, locals + * (guint16 *)(ip + 1), i32);
4014 vt_sp += (i32 + 7) & ~7;
4019 MINT_IN_CASE(MINT_LDLOCA_S)
4020 sp->data.p = locals + * (guint16 *)(ip + 1);
4025 #define STLOC(datamem, argtype) \
4027 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp->data.datamem; \
4030 MINT_IN_CASE(MINT_STLOC_I1) STLOC(i, gint8); MINT_IN_BREAK;
4031 MINT_IN_CASE(MINT_STLOC_U1) STLOC(i, guint8); MINT_IN_BREAK;
4032 MINT_IN_CASE(MINT_STLOC_I2) STLOC(i, gint16); MINT_IN_BREAK;
4033 MINT_IN_CASE(MINT_STLOC_U2) STLOC(i, guint16); MINT_IN_BREAK;
4034 MINT_IN_CASE(MINT_STLOC_I4) STLOC(i, gint32); MINT_IN_BREAK;
4035 MINT_IN_CASE(MINT_STLOC_I8) STLOC(l, gint64); MINT_IN_BREAK;
4036 MINT_IN_CASE(MINT_STLOC_R4) STLOC(f, float); MINT_IN_BREAK;
4037 MINT_IN_CASE(MINT_STLOC_R8) STLOC(f, double); MINT_IN_BREAK;
4038 MINT_IN_CASE(MINT_STLOC_O) STLOC(p, gpointer); MINT_IN_BREAK;
4039 MINT_IN_CASE(MINT_STLOC_P) STLOC(p, gpointer); MINT_IN_BREAK;
4041 #define STLOC_NP(datamem, argtype) \
4042 * (argtype *)(locals + * (guint16 *)(ip + 1)) = sp [-1].data.datamem; \
4045 MINT_IN_CASE(MINT_STLOC_NP_I4) STLOC_NP(i, gint32); MINT_IN_BREAK;
4046 MINT_IN_CASE(MINT_STLOC_NP_O) STLOC_NP(p, gpointer); MINT_IN_BREAK;
4048 MINT_IN_CASE(MINT_STLOC_VT)
4049 i32 = READ32(ip + 2);
4051 memcpy(locals + * (guint16 *)(ip + 1), sp->data.p, i32);
4052 vt_sp -= (i32 + 7) & ~7;
4056 MINT_IN_CASE(MINT_LOCALLOC)
4057 if (sp != frame->stack + 1) /*FIX?*/
4058 THROW_EX (mono_get_exception_execution_engine (NULL), ip);
4059 sp [-1].data.p = alloca (sp [-1].data.i);
4063 MINT_IN_CASE(MINT_ENDFILTER) ves_abort(); MINT_IN_BREAK;
4065 MINT_IN_CASE(MINT_INITOBJ)
4067 memset (sp->data.vt, 0, READ32(ip + 1));
4070 MINT_IN_CASE(MINT_CPBLK)
4072 if (!sp [0].data.p || !sp [1].data.p)
4073 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4075 /* FIXME: value and size may be int64... */
4076 memcpy (sp [0].data.p, sp [1].data.p, sp [2].data.i);
4079 MINT_IN_CASE(MINT_CONSTRAINED_) {
4081 /* FIXME: implement */
4083 token = READ32 (ip);
4088 MINT_IN_CASE(MINT_INITBLK)
4091 THROW_EX (mono_get_exception_null_reference(), ip - 1);
4093 /* FIXME: value and size may be int64... */
4094 memset (sp [0].data.p, sp [1].data.i, sp [2].data.i);
4097 MINT_IN_CASE(MINT_NO_)
4098 /* FIXME: implement */
4102 MINT_IN_CASE(MINT_RETHROW)
4104 * need to clarify what this should actually do:
4105 * start the search from the last found handler in
4106 * this method or continue in the caller or what.
4107 * Also, do we need to run finally/fault handlers after a retrow?
4108 * Well, this implementation will follow the usual search
4109 * for an handler, considering the current ip as throw spot.
4110 * We need to NULL frame->ex_handler for the later code to
4111 * actually run the new found handler.
4113 frame->ex_handler = NULL;
4114 THROW_EX (frame->ex, ip - 1);
4117 g_print ("Unimplemented opcode: %04x %s at 0x%x\n", *ip, mono_interp_opname[*ip], ip-rtm->code);
4118 THROW_EX (mono_get_exception_execution_engine ("Unimplemented opcode"), ip);
4122 g_assert_not_reached ();
4124 * Exception handling code.
4125 * The exception object is stored in frame->ex.
4132 MonoInvocation *inv;
4133 MonoExceptionClause *clause;
4139 g_print ("* Handling exception '%s' at IL_%04x\n",
4140 frame->ex == NULL ? "** Unknown **" : mono_object_class (frame->ex)->name,
4141 rtm == NULL ? 0 : frame->ip - rtm->code);
4143 if (die_on_exception)
4146 for (inv = frame; inv; inv = inv->parent) {
4148 if (inv->runtime_method == NULL)
4150 method = inv->runtime_method->method;
4151 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4153 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))
4155 if (inv->ip == NULL)
4157 ip_offset = inv->ip - inv->runtime_method->code;
4158 inv->ex_handler = NULL; /* clear this in case we are trhowing an exception while handling one - this one wins */
4159 for (i = 0; i < inv->runtime_method->num_clauses; ++i) {
4160 clause = &inv->runtime_method->clauses [i];
4161 if (clause->flags <= 1 && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4162 if (!clause->flags) {
4163 MonoObject *isinst_obj = mono_object_isinst_checked ((MonoObject*)frame->ex, clause->data.catch_class, &error);
4164 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4167 * OK, we found an handler, now we need to execute the finally
4168 * and fault blocks before branching to the handler code.
4170 inv->ex_handler = clause;
4173 g_print ("* Found handler at '%s'\n", method->name);
4175 goto handle_finally;
4178 /* FIXME: handle filter clauses */
4185 * If we get here, no handler was found: print a stack trace.
4187 for (inv = frame; inv; inv = inv->parent) {
4188 if (inv->invoke_trap)
4189 goto handle_finally;
4192 ex_obj = (MonoObject*)frame->ex;
4193 mono_unhandled_exception (ex_obj);
4200 MonoExceptionClause *clause;
4201 GSList *old_list = finally_ips;
4202 MonoMethod *method = frame->runtime_method->method;
4203 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4204 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4208 g_print ("* Handle finally IL_%04x\n", endfinally_ip == NULL ? 0 : endfinally_ip - rtm->code);
4210 if (rtm == NULL || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
4211 || (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME))) {
4214 ip_offset = frame->ip - rtm->code;
4216 if (endfinally_ip != NULL)
4217 finally_ips = g_slist_prepend(finally_ips, (void *)endfinally_ip);
4218 for (i = 0; i < header->num_clauses; ++i)
4219 if (frame->ex_handler == &rtm->clauses [i])
4223 clause = &rtm->clauses [i];
4224 if (MONO_OFFSET_IN_CLAUSE (clause, ip_offset) && (endfinally_ip == NULL || !(MONO_OFFSET_IN_CLAUSE (clause, endfinally_ip - rtm->code)))) {
4225 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY) {
4226 ip = rtm->code + clause->handler_offset;
4227 finally_ips = g_slist_prepend (finally_ips, (gpointer) ip);
4230 g_print ("* Found finally at IL_%04x with exception: %s\n", clause->handler_offset, frame->ex? "yes": "no");
4236 endfinally_ip = NULL;
4238 if (old_list != finally_ips && finally_ips) {
4239 ip = finally_ips->data;
4240 finally_ips = g_slist_remove (finally_ips, ip);
4241 sp = frame->stack; /* spec says stack should be empty at endfinally so it should be at the start too */
4246 * If an exception is set, we need to execute the fault handler, too,
4247 * otherwise, we continue normally.
4257 MonoExceptionClause *clause;
4258 MonoMethod *method = frame->runtime_method->method;
4259 MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
4260 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4264 g_print ("* Handle fault\n");
4266 ip_offset = frame->ip - rtm->code;
4267 for (i = 0; i < header->num_clauses; ++i) {
4268 clause = &rtm->clauses [i];
4269 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT && MONO_OFFSET_IN_CLAUSE (clause, ip_offset)) {
4270 ip = rtm->code + clause->handler_offset;
4273 g_print ("* Executing handler at IL_%04x\n", clause->handler_offset);
4279 * If the handler for the exception was found in this method, we jump
4280 * to it right away, otherwise we return and let the caller run
4281 * the finally, fault and catch blocks.
4282 * This same code should be present in the endfault opcode, but it
4283 * is corrently not assigned in the ECMA specs: LAMESPEC.
4285 if (frame->ex_handler) {
4288 g_print ("* Executing handler at IL_%04x\n", frame->ex_handler->handler_offset);
4290 ip = rtm->code + frame->ex_handler->handler_offset;
4292 vt_sp = (unsigned char *) sp + rtm->stack_size;
4293 sp->data.p = frame->ex;
4304 ves_exec_method (MonoInvocation *frame)
4306 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4307 ThreadContext context_struct;
4314 mono_unhandled_exception ((MonoObject*)frame->ex);
4317 if (context == NULL) {
4318 context = &context_struct;
4319 context_struct.domain = mono_domain_get ();
4320 context_struct.base_frame = frame;
4321 context_struct.current_frame = NULL;
4322 context_struct.env_frame = frame;
4323 context_struct.current_env = &env;
4324 context_struct.search_for_handler = 0;
4325 context_struct.managed_code = 0;
4326 mono_native_tls_set_value (thread_context_id, context);
4329 frame->parent = context->current_frame;
4330 frame->runtime_method = mono_interp_get_runtime_method (context->domain, frame->method, &error);
4331 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4332 context->managed_code = 1;
4333 ves_exec_method_with_context (frame, context);
4334 context->managed_code = 0;
4336 if (context != &context_struct && context->current_env) {
4337 context->env_frame->ex = frame->ex;
4338 longjmp (*context->current_env, 1);
4341 mono_unhandled_exception ((MonoObject*)frame->ex);
4343 if (context->base_frame == frame)
4344 mono_native_tls_set_value (thread_context_id, NULL);
4346 context->current_frame = frame->parent;
4350 ves_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4352 MonoImage *image = mono_assembly_get_image (assembly);
4357 method = mono_get_method_checked (image, mono_image_get_entry_point (image), NULL, NULL, &error);
4358 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4361 g_error ("No entry point method found in %s", mono_image_get_filename (image));
4363 rval = mono_runtime_run_main_checked (method, argc, argv, &error);
4364 return_val_and_set_pending_if_nok (&error, rval);
4371 "mint %s, the Mono ECMA CLI interpreter, (C) 2001, 2002 Ximian, Inc.\n\n"
4372 "Usage is: mint [options] executable args...\n\n", VERSION);
4374 "Runtime Debugging:\n"
4379 " --noptr\t\t\tdon't print pointer addresses in trace output\n"
4382 " --traceclassinit\n"
4385 " --debug method_name\n"
4392 " --config filename load the specified config file instead of the default\n"
4393 " --workers n maximum number of worker threads\n"
4399 add_signal_handler (int signo, void (*handler)(int))
4402 signal (signo, handler);
4404 struct sigaction sa;
4406 sa.sa_handler = handler;
4407 sigemptyset (&sa.sa_mask);
4410 g_assert (sigaction (signo, &sa, NULL) != -1);
4415 segv_handler (int signum)
4417 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4418 MonoException *segv_exception;
4420 if (context == NULL)
4422 segv_exception = mono_get_exception_null_reference ();
4423 segv_exception->message = mono_string_new (mono_domain_get (), "Null Reference (SIGSEGV)");
4424 mono_raise_exception (segv_exception);
4429 quit_handler (int signum)
4431 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4432 MonoException *quit_exception;
4434 if (context == NULL)
4436 quit_exception = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
4437 mono_raise_exception (quit_exception);
4441 abrt_handler (int signum)
4443 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4444 MonoException *abrt_exception;
4446 if (context == NULL)
4448 abrt_exception = mono_get_exception_execution_engine ("Abort (SIGABRT).");
4449 mono_raise_exception (abrt_exception);
4454 thread_abort_handler (int signum)
4456 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4459 if (context == NULL)
4462 exc = mono_thread_request_interruption (context->managed_code);
4463 if (exc) mono_raise_exception (exc);
4468 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
4469 MonoReflectionMethod **method,
4470 gint32 *iloffset, gint32 *native_offset,
4471 MonoString **file, gint32 *line, gint32 *column)
4473 ThreadContext *context = mono_native_tls_get_value (thread_context_id);
4474 MonoInvocation *inv = context->current_frame;
4478 for (i = 0; inv && i < skip; inv = inv->parent)
4479 if (inv->runtime_method != NULL)
4490 *method = mono_method_get_object_checked (context->domain, inv->runtime_method->method, NULL, &error);
4491 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4496 if (need_file_info) {
4500 *file = mono_string_new (mono_domain_get (), "unknown");
4507 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
4509 MonoDomain *domain = mono_domain_get ();
4511 MonoArray *ta = exc->trace_ips;
4516 /* Exception is not thrown yet */
4517 MonoArray *array = mono_array_new_checked (domain, mono_defaults.stack_frame_class, 0, &error);
4518 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4522 len = mono_array_length (ta);
4524 res = mono_array_new_checked (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0, &error);
4525 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4527 for (i = skip; i < len / 2; i++) {
4528 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new_checked (domain, mono_defaults.stack_frame_class, &error);
4529 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4530 gushort *ip = mono_array_get (ta, gpointer, 2 * i + 1);
4531 RuntimeMethod *rtm = mono_array_get (ta, gpointer, 2 * i);
4534 sf->method = mono_method_get_object_checked (domain, rtm->method, NULL, &error);
4535 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4536 sf->native_offset = ip - rtm->code;
4540 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
4542 if (need_file_info) {
4545 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
4547 sf->filename = filename? mono_string_new (domain, filename): NULL;
4554 mono_array_set (res, gpointer, i, sf);
4561 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target, MonoReflectionMethod *info)
4563 MonoClass *delegate_class = mono_class_from_mono_type (type->type);
4564 MonoObject *delegate;
4567 g_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
4569 delegate = mono_object_new_checked (mono_object_domain (type), delegate_class, &error);
4570 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4572 interp_delegate_ctor (mono_object_domain (type), delegate, target, mono_interp_get_runtime_method (mono_get_root_domain (), info->method, &error));
4573 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4582 int enable_debugging;
4588 static void main_thread_handler (gpointer user_data)
4590 MainThreadArgs *main_args=(MainThreadArgs *)user_data;
4591 MonoAssembly *assembly;
4593 if (main_args->enable_debugging) {
4594 mono_debug_init (MONO_DEBUG_FORMAT_MONO);
4597 assembly = mono_domain_assembly_open (main_args->domain, main_args->file);
4600 fprintf (stderr, "Can not open image %s\n", main_args->file);
4604 ves_exec (main_args->domain, assembly, main_args->argc, main_args->argv);
4608 mono_runtime_install_handlers (void)
4610 add_signal_handler (SIGSEGV, segv_handler);
4611 add_signal_handler (SIGINT, quit_handler);
4612 add_signal_handler (SIGABRT, abrt_handler);
4614 add_signal_handler (mono_thread_get_abort_signal (), thread_abort_handler);
4619 quit_function (MonoDomain *domain, gpointer user_data)
4621 mono_profiler_shutdown ();
4623 mono_runtime_cleanup (domain);
4624 mono_domain_free (domain, TRUE);
4629 mono_interp_cleanup(MonoDomain *domain)
4631 quit_function (domain, NULL);
4635 mono_interp_exec(MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[])
4637 return ves_exec (domain, assembly, argc, argv);
4641 interp_get_imt_trampoline (MonoVTable *vtable, int imt_slot_index)
4643 // FIXME: implement me
4648 interp_create_ftnptr (MonoDomain *domain, gpointer addr)
4650 // FIXME: true on all arch?
4655 mono_current_thread_has_handle_block_guard (void)
4661 mono_interp_init(const char *file)
4664 MonoRuntimeCallbacks callbacks;
4665 MonoRuntimeExceptionHandlingCallbacks ecallbacks;
4668 g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
4669 g_log_set_fatal_mask (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR);
4671 mono_native_tls_alloc (&thread_context_id, NULL);
4672 mono_native_tls_set_value (thread_context_id, NULL);
4673 mono_os_mutex_init_recursive (&runtime_method_lookup_section);
4674 mono_os_mutex_init_recursive (&create_method_pointer_mutex);
4676 // TODO: use callbacks?
4677 mono_runtime_install_handlers ();
4678 mono_interp_transform_init ();
4680 memset (&callbacks, 0, sizeof (callbacks));
4681 // TODO: replace with `mono_install_callbacks`
4682 callbacks.compile_method = mono_create_method_pointer;
4683 callbacks.runtime_invoke = interp_mono_runtime_invoke;
4684 callbacks.get_imt_trampoline = interp_get_imt_trampoline;
4685 callbacks.create_ftnptr = interp_create_ftnptr;
4686 #ifndef DISABLE_REMOTING
4687 mono_install_remoting_trampoline (interp_create_remoting_trampoline);
4689 callbacks.create_jit_trampoline = interp_create_trampoline;
4690 mono_install_callbacks (&callbacks);
4693 memset (&ecallbacks, 0, sizeof (ecallbacks));
4694 ecallbacks.mono_raise_exception = interp_ex_handler;
4695 ecallbacks.mono_current_thread_has_handle_block_guard = mono_current_thread_has_handle_block_guard;
4698 mono_install_stack_walk (interp_walk_stack);
4700 mono_install_eh_callbacks (&ecallbacks);
4702 mono_install_runtime_cleanup (quit_function);
4704 abort_requested = mono_thread_interruption_request_flag ();
4706 domain = mono_init_from_assembly (file, file);
4707 #ifdef __hpux /* generates very big stack frames */
4708 mono_threads_set_default_stacksize(32*1024*1024);
4711 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", ves_icall_get_frame_info);
4712 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", ves_icall_get_trace);
4713 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", mono_runtime_install_handlers);
4714 mono_add_internal_call ("System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal);
4716 mono_register_jit_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", mono_create_icall_signature ("void"), FALSE);
4718 mono_runtime_init_checked (domain, NULL, NULL, &error);
4719 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
4721 mono_thread_attach (domain);
4725 typedef int (*TestMethod) (void);
4728 interp_regression_step (MonoImage *image, int verbose, int *total_run, int *total, GTimer *timer, MonoDomain *domain)
4730 int result, expected, failed, cfailed, run;
4732 double elapsed, transform_time, start_time;
4734 MonoObject *result_obj;
4735 static gboolean filter_method_init = FALSE;
4736 static gchar *filter_method = NULL;
4738 g_print ("Test run: image=%s\n", mono_image_get_filename (image));
4739 cfailed = failed = run = 0;
4740 transform_time = elapsed = 0.0;
4743 /* fixme: ugly hack - delete all previously compiled methods */
4744 if (domain_jit_info (domain)) {
4745 g_hash_table_destroy (domain_jit_info (domain)->jit_trampoline_hash);
4746 domain_jit_info (domain)->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4747 mono_internal_hash_table_destroy (&(domain->jit_code_hash));
4748 mono_jit_code_hash_init (&(domain->jit_code_hash));
4752 g_timer_start (timer);
4753 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4755 MonoMethod *method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4757 mono_error_cleanup (&error); /* FIXME don't swallow the error */
4761 if (!filter_method_init) {
4762 filter_method = g_getenv ("INTERP_FILTER_METHOD");
4763 filter_method_init = TRUE;
4765 gboolean filter = FALSE;
4766 if (filter_method) {
4767 const char *name = filter_method;
4769 if ((strchr (name, '.') > name) || strchr (name, ':')) {
4770 MonoMethodDesc *desc = mono_method_desc_new (name, TRUE);
4771 filter = mono_method_desc_full_match (desc, method);
4772 mono_method_desc_free (desc);
4774 filter = strcmp (method->name, name) == 0;
4776 } else { // no filter
4779 if (strncmp (method->name, "test_", 5) == 0 && filter) {
4780 MonoError interp_error;
4781 RuntimeMethod *runtime_method;
4782 MonoInvocation frame;
4783 ThreadContext context;
4784 stackval frame_result;
4785 MonoObject *exc = NULL;
4787 result_obj = interp_mono_runtime_invoke (method, NULL, NULL, &exc, &interp_error);
4788 if (!mono_error_ok (&interp_error)) {
4790 g_print ("Test '%s' execution failed.\n", method->name);
4791 } else if (exc != NULL) {
4792 g_print ("Exception in Test '%s' occured:\n", method->name);
4793 mono_object_describe (exc);
4797 result = *(gint32 *) mono_object_unbox (result_obj);
4798 expected = atoi (method->name + 5); // FIXME: oh no.
4801 if (result != expected) {
4803 g_print ("Test '%s' failed result (got %d, expected %d).\n", method->name, result, expected);
4808 g_timer_stop (timer);
4809 elapsed = g_timer_elapsed (timer, NULL);
4810 if (failed > 0 || cfailed > 0){
4811 g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n",
4812 run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
4814 g_print ("Results: total tests: %d, all pass \n", run);
4817 g_print ("Elapsed time: %f secs (%f, %f)\n\n", elapsed,
4818 elapsed - transform_time, transform_time);
4819 *total += failed + cfailed;
4823 interp_regression (MonoImage *image, int verbose, int *total_run)
4827 GTimer *timer = g_timer_new ();
4828 MonoDomain *domain = mono_domain_get ();
4829 guint32 i, exclude = 0;
4832 /* load the metadata */
4833 for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
4835 method = mono_get_method_checked (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL, NULL, &error);
4837 mono_error_cleanup (&error);
4840 mono_class_init (method->klass);
4845 interp_regression_step (image, verbose, total_run, &total, timer, domain);
4847 g_timer_destroy (timer);
4852 interp_regression_list (int verbose, int count, char *images [])
4854 int i, total, total_run, run;
4856 total_run = total = 0;
4857 for (i = 0; i < count; ++i) {
4858 MonoAssembly *ass = mono_assembly_open (images [i], NULL);
4860 g_warning ("failed to load assembly: %s", images [i]);
4863 total += interp_regression (mono_assembly_get_image (ass), verbose, &run);
4867 g_print ("Overall results: tests: %d, failed: %d (pass: %.2f%%)\n", total_run, total, 100.0*(total_run-total)/total_run);
4869 g_print ("Overall results: tests: %d, 100%% pass\n", total_run);