3 * transform CIL into different opcodes for more
4 * efficient interpretation
6 * Written by Bernie Solomon (bernard@ugsolutions.com)
11 #include <mono/metadata/appdomain.h>
12 #include <mono/metadata/debug-helpers.h>
13 #include <mono/metadata/exception.h>
14 #include <mono/metadata/mono-endian.h>
15 #include <mono/metadata/marshal.h>
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/tabledefs.h>
19 #include <mono/mini/mini.h>
22 #include "interp-internals.h"
25 // TODO: export from marshal.c
26 MonoDelegate* mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
40 MonoMethodHeader *header;
42 const unsigned char *il_code;
43 const unsigned char *ip;
44 const unsigned char *last_ip;
45 const unsigned char *in_start;
49 StackInfo **stack_state;
52 unsigned char *is_bb_start;
53 unsigned short *new_code;
54 unsigned short *new_code_end;
55 unsigned short *new_ip;
56 unsigned short *last_new_ip;
57 unsigned int max_code_size;
60 unsigned int max_stack_height;
62 unsigned int max_vt_sp;
66 GHashTable *data_hash;
69 #define MINT_TYPE_I1 0
70 #define MINT_TYPE_U1 1
71 #define MINT_TYPE_I2 2
72 #define MINT_TYPE_U2 3
73 #define MINT_TYPE_I4 4
74 #define MINT_TYPE_I8 5
75 #define MINT_TYPE_R4 6
76 #define MINT_TYPE_R8 7
79 #define MINT_TYPE_VT 10
81 #define STACK_TYPE_I4 0
82 #define STACK_TYPE_I8 1
83 #define STACK_TYPE_R8 2
84 #define STACK_TYPE_O 3
85 #define STACK_TYPE_VT 4
86 #define STACK_TYPE_MP 5
87 #define STACK_TYPE_F 6
89 static const char *stack_type_string [] = { "I4", "I8", "R8", "O ", "VT", "MP", "F " };
91 #if SIZEOF_VOID_P == 8
92 #define STACK_TYPE_I STACK_TYPE_I8
94 #define STACK_TYPE_I STACK_TYPE_I4
97 static int stack_type [] = {
100 STACK_TYPE_I4, /*I2*/
101 STACK_TYPE_I4, /*U2*/
102 STACK_TYPE_I4, /*I4*/
103 STACK_TYPE_I8, /*I8*/
104 STACK_TYPE_R8, /*R4*/
105 STACK_TYPE_R8, /*R8*/
112 grow_code (TransformData *td)
114 unsigned int old_ip_offset = td->new_ip - td->new_code;
115 unsigned int old_last_ip_offset = td->last_new_ip - td->new_code;
116 g_assert (old_ip_offset <= td->max_code_size);
117 td->new_code = g_realloc (td->new_code, (td->max_code_size *= 2) * sizeof (td->new_code [0]));
118 td->new_code_end = td->new_code + td->max_code_size;
119 td->new_ip = td->new_code + old_ip_offset;
120 td->last_new_ip = td->new_code + old_last_ip_offset;
123 #define ENSURE_CODE(td, n) \
125 if ((td)->new_ip + (n) > (td)->new_code_end) \
129 #define ADD_CODE(td, n) \
131 if ((td)->new_ip == (td)->new_code_end) \
133 *(td)->new_ip++ = (n); \
136 #define CHECK_STACK(td, n) \
138 int stack_size = (td)->sp - (td)->stack; \
139 if (stack_size < (n)) \
140 g_warning ("%s.%s: not enough values (%d < %d) on stack at %04x", \
141 (td)->method->klass->name, (td)->method->name, \
142 stack_size, n, (td)->ip - (td)->il_code); \
145 #define ENSURE_I4(td, sp_off) \
147 if ((td)->sp [-sp_off].type == STACK_TYPE_I8) \
148 ADD_CODE(td, sp_off == 1 ? MINT_CONV_I4_I8 : MINT_CONV_I4_I8_SP); \
152 handle_branch(TransformData *td, int short_op, int long_op, int offset)
154 int shorten_branch = 0;
155 int target = td->ip + offset - td->il_code;
156 if (target < 0 || target >= td->code_size)
157 g_assert_not_reached ();
158 if (offset > 0 && td->stack_height [target] < 0) {
159 td->stack_height [target] = td->sp - td->stack;
160 if (td->stack_height [target] > 0)
161 td->stack_state [target] = g_memdup (td->stack, td->stack_height [target] * sizeof (td->stack [0]));
162 td->vt_stack_size [target] = td->vt_sp;
165 offset = td->in_offsets [target] - (td->new_ip - td->new_code);
166 if (offset >= -32768) {
170 int prev = td->forward_refs [target];
171 td->forward_refs [td->ip - td->il_code] = prev;
172 td->forward_refs [target] = td->ip - td->il_code;
174 if (td->header->code_size <= 25000) /* FIX to be precise somehow? */
177 if (shorten_branch) {
178 ADD_CODE(td, short_op);
179 ADD_CODE(td, offset);
181 ADD_CODE(td, long_op);
182 ADD_CODE(td, * (unsigned short *)(&offset));
183 ADD_CODE(td, * ((unsigned short *)&offset + 1));
188 one_arg_branch(TransformData *td, int mint_op, int offset)
190 int type = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
191 int long_op = mint_op + type - STACK_TYPE_I4;
192 int short_op = long_op + MINT_BRFALSE_I4_S - MINT_BRFALSE_I4;
195 handle_branch (td, short_op, long_op, offset);
199 two_arg_branch(TransformData *td, int mint_op, int offset)
201 int type1 = td->sp [-1].type == STACK_TYPE_O || td->sp [-1].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-1].type;
202 int type2 = td->sp [-2].type == STACK_TYPE_O || td->sp [-2].type == STACK_TYPE_MP ? STACK_TYPE_I : td->sp [-2].type;
203 int long_op = mint_op + type1 - STACK_TYPE_I4;
204 int short_op = long_op + MINT_BEQ_I4_S - MINT_BEQ_I4;
206 if (type1 == STACK_TYPE_I4 && type2 == STACK_TYPE_I8) {
207 ADD_CODE(td, MINT_CONV_I8_I4);
208 td->in_offsets [td->ip - td->il_code]++;
209 } else if (type1 == STACK_TYPE_I8 && type2 == STACK_TYPE_I4) {
210 ADD_CODE(td, MINT_CONV_I8_I4_SP);
211 td->in_offsets [td->ip - td->il_code]++;
212 } else if (type1 != type2) {
213 g_warning("%s.%s: branch type mismatch %d %d",
214 td->method->klass->name, td->method->name,
215 td->sp [-1].type, td->sp [-2].type);
218 handle_branch (td, short_op, long_op, offset);
222 unary_arith_op(TransformData *td, int mint_op)
224 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
230 binary_arith_op(TransformData *td, int mint_op)
232 int type1 = td->sp [-2].type;
233 int type2 = td->sp [-1].type;
235 #if SIZEOF_VOID_P == 8
236 if ((type1 == STACK_TYPE_MP || type1 == STACK_TYPE_I8) && type2 == STACK_TYPE_I4) {
237 ADD_CODE(td, MINT_CONV_I8_I4);
238 type2 = STACK_TYPE_I8;
240 if (type1 == STACK_TYPE_I4 && (type2 == STACK_TYPE_MP || type2 == STACK_TYPE_I8)) {
241 ADD_CODE(td, MINT_CONV_I8_I4_SP);
242 type1 = STACK_TYPE_I8;
243 td->sp [-2].type = STACK_TYPE_I8;
246 if (type1 == STACK_TYPE_MP)
247 type1 = STACK_TYPE_I;
248 if (type2 == STACK_TYPE_MP)
249 type2 = STACK_TYPE_I;
250 if (type1 != type2) {
251 g_warning("%s.%s: %04x arith type mismatch %s %d %d",
252 td->method->klass->name, td->method->name,
253 td->ip - td->il_code, mono_interp_opname[mint_op], type1, type2);
255 op = mint_op + type1 - STACK_TYPE_I4;
262 binary_int_op(TransformData *td, int mint_op)
264 int op = mint_op + td->sp [-1].type - STACK_TYPE_I4;
266 if (td->sp [-1].type != td->sp [-2].type)
267 g_warning("%s.%s: int type mismatch", td->method->klass->name, td->method->name);
273 shift_op(TransformData *td, int mint_op)
275 int op = mint_op + td->sp [-2].type - STACK_TYPE_I4;
277 if (td->sp [-1].type != STACK_TYPE_I4) {
278 g_warning("%s.%s: shift type mismatch %d",
279 td->method->klass->name, td->method->name,
287 mint_type(MonoType *type)
292 switch (type->type) {
296 case MONO_TYPE_BOOLEAN:
308 #if SIZEOF_VOID_P == 4
322 case MONO_TYPE_STRING:
323 case MONO_TYPE_SZARRAY:
324 case MONO_TYPE_CLASS:
325 case MONO_TYPE_OBJECT:
326 case MONO_TYPE_ARRAY:
328 case MONO_TYPE_VALUETYPE:
329 if (type->data.klass->enumtype) {
330 type = mono_class_enum_basetype (type->data.klass);
334 case MONO_TYPE_GENERICINST:
335 type = &type->data.generic_class->container_class->byval_arg;
338 g_warning ("got type 0x%02x", type->type);
339 g_assert_not_reached ();
345 can_store (int stack_type, int var_type)
347 if (stack_type == STACK_TYPE_O || stack_type == STACK_TYPE_MP)
348 stack_type = STACK_TYPE_I;
349 if (var_type == STACK_TYPE_O || var_type == STACK_TYPE_MP)
350 var_type = STACK_TYPE_I;
351 return stack_type == var_type;
354 #define SET_SIMPLE_TYPE(s, ty) \
361 #define SET_TYPE(s, ty, k) \
368 #define PUSH_SIMPLE_TYPE(td, ty) \
372 sp_height = (td)->sp - (td)->stack; \
373 if (sp_height > (td)->max_stack_height) \
374 (td)->max_stack_height = sp_height; \
375 SET_SIMPLE_TYPE((td)->sp - 1, ty); \
378 #define PUSH_TYPE(td, ty, k) \
382 sp_height = (td)->sp - (td)->stack; \
383 if (sp_height > (td)->max_stack_height) \
384 (td)->max_stack_height = sp_height; \
385 SET_TYPE((td)->sp - 1, ty, k); \
388 #define PUSH_VT(td, size) \
390 (td)->vt_sp += ((size) + 7) & ~7; \
391 if ((td)->vt_sp > (td)->max_vt_sp) \
392 (td)->max_vt_sp = (td)->vt_sp; \
395 #define POP_VT(td, size) \
397 (td)->vt_sp -= ((size) + 7) & ~7; \
400 #if NO_UNALIGNED_ACCESS
401 #define WRITE32(td, v) \
403 ENSURE_CODE(td, 2); \
404 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
405 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
409 #define WRITE64(td, v) \
411 ENSURE_CODE(td, 4); \
412 * (guint16 *)((td)->new_ip) = * (guint16 *)(v); \
413 * ((guint16 *)((td)->new_ip) + 1) = * ((guint16 *)(v) + 1); \
414 * ((guint16 *)((td)->new_ip) + 2) = * ((guint16 *)(v) + 2); \
415 * ((guint16 *)((td)->new_ip) + 3) = * ((guint16 *)(v) + 3); \
419 #define WRITE32(td, v) \
421 ENSURE_CODE(td, 2); \
422 * (guint32 *)((td)->new_ip) = * (guint32 *)(v); \
426 #define WRITE64(td, v) \
428 ENSURE_CODE(td, 4); \
429 * (guint64 *)((td)->new_ip) = * (guint64 *)(v); \
436 load_arg(TransformData *td, int n)
439 MonoClass *klass = NULL;
442 gboolean hasthis = mono_method_signature (td->method)->hasthis;
443 if (hasthis && n == 0)
444 type = &td->method->klass->byval_arg;
446 type = mono_method_signature (td->method)->params [hasthis ? n - 1 : n];
448 mt = mint_type (type);
449 if (mt == MINT_TYPE_VT) {
451 klass = mono_class_from_mono_type (type);
452 if (mono_method_signature (td->method)->pinvoke)
453 size = mono_class_native_size (klass, NULL);
455 size = mono_class_value_size (klass, NULL);
457 if (hasthis && n == 0) {
459 ADD_CODE (td, MINT_LDARG_P);
460 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
464 ADD_CODE (td, MINT_LDARG_VT);
465 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
469 if (hasthis && n == 0) {
471 ADD_CODE (td, MINT_LDARG_P);
472 ADD_CODE (td, td->rtm->arg_offsets [n]); /* FIX for large offset */
475 ADD_CODE(td, MINT_LDARG_I1 + (mt - MINT_TYPE_I1));
476 ADD_CODE(td, td->rtm->arg_offsets [n]); /* FIX for large offset */
477 if (mt == MINT_TYPE_O)
478 klass = mono_class_from_mono_type (type);
481 PUSH_TYPE(td, stack_type[mt], klass);
485 store_arg(TransformData *td, int n)
491 gboolean hasthis = mono_method_signature (td->method)->hasthis;
492 if (hasthis && n == 0)
493 type = &td->method->klass->byval_arg;
495 type = mono_method_signature (td->method)->params [n - !!hasthis];
497 mt = mint_type (type);
498 if (mt == MINT_TYPE_VT) {
500 MonoClass *klass = mono_class_from_mono_type (type);
501 if (mono_method_signature (td->method)->pinvoke)
502 size = mono_class_native_size (klass, NULL);
504 size = mono_class_value_size (klass, NULL);
505 ADD_CODE(td, MINT_STARG_VT);
506 ADD_CODE(td, td->rtm->arg_offsets [n]);
508 if (td->sp [-1].type == STACK_TYPE_VT)
511 ADD_CODE(td, MINT_STARG_I1 + (mt - MINT_TYPE_I1));
512 ADD_CODE(td, td->rtm->arg_offsets [n]);
518 store_inarg(TransformData *td, int n)
521 gboolean hasthis = mono_method_signature (td->method)->hasthis;
522 if (hasthis && n == 0)
523 type = &td->method->klass->byval_arg;
525 type = mono_method_signature (td->method)->params [n - !!hasthis];
527 int mt = mint_type (type);
528 if (hasthis && n == 0) {
529 ADD_CODE (td, MINT_STINARG_P);
533 if (mt == MINT_TYPE_VT) {
534 MonoClass *klass = mono_class_from_mono_type (type);
536 if (mono_method_signature (td->method)->pinvoke)
537 size = mono_class_native_size (klass, NULL);
539 size = mono_class_value_size (klass, NULL);
540 ADD_CODE(td, MINT_STINARG_VT);
544 ADD_CODE(td, MINT_STINARG_I1 + (mt - MINT_TYPE_I1));
550 load_local(TransformData *td, int n)
552 MonoType *type = td->header->locals [n];
553 int mt = mint_type (type);
554 int offset = td->rtm->local_offsets [n];
555 MonoClass *klass = NULL;
556 if (mt == MINT_TYPE_VT) {
557 klass = mono_class_from_mono_type (type);
558 gint32 size = mono_class_value_size (klass, NULL);
560 ADD_CODE(td, MINT_LDLOC_VT);
561 ADD_CODE(td, offset); /*FIX for large offset */
564 g_assert (mt < MINT_TYPE_VT);
565 if (mt == MINT_TYPE_I4 && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
566 td->last_new_ip [0] == MINT_STLOC_I4 && td->last_new_ip [1] == offset) {
567 td->last_new_ip [0] = MINT_STLOC_NP_I4;
568 } else if (mt == MINT_TYPE_O && !td->is_bb_start [td->in_start - td->il_code] && td->last_new_ip != NULL &&
569 td->last_new_ip [0] == MINT_STLOC_O && td->last_new_ip [1] == offset) {
570 td->last_new_ip [0] = MINT_STLOC_NP_O;
572 ADD_CODE(td, MINT_LDLOC_I1 + (mt - MINT_TYPE_I1));
573 ADD_CODE(td, offset); /*FIX for large offset */
575 if (mt == MINT_TYPE_O)
576 klass = mono_class_from_mono_type (type);
578 PUSH_TYPE(td, stack_type[mt], klass);
582 store_local(TransformData *td, int n)
584 MonoType *type = td->header->locals [n];
585 int mt = mint_type (type);
586 int offset = td->rtm->local_offsets [n];
588 #if SIZEOF_VOID_P == 8
589 if (td->sp [-1].type == STACK_TYPE_I4 && stack_type [mt] == STACK_TYPE_I8) {
590 ADD_CODE(td, MINT_CONV_I8_I4);
591 td->sp [-1].type = STACK_TYPE_I8;
594 if (!can_store(td->sp [-1].type, stack_type [mt])) {
595 g_warning("%s.%s: Store local stack type mismatch %d %d",
596 td->method->klass->name, td->method->name,
597 stack_type [mt], td->sp [-1].type);
599 if (mt == MINT_TYPE_VT) {
600 MonoClass *klass = mono_class_from_mono_type (type);
601 gint32 size = mono_class_value_size (klass, NULL);
602 ADD_CODE(td, MINT_STLOC_VT);
603 ADD_CODE(td, offset); /*FIX for large offset */
605 if (td->sp [-1].type == STACK_TYPE_VT)
608 g_assert (mt < MINT_TYPE_VT);
609 ADD_CODE(td, MINT_STLOC_I1 + (mt - MINT_TYPE_I1));
610 ADD_CODE(td, offset); /*FIX for large offset */
615 #define SIMPLE_OP(td, op) \
622 get_data_item_index (TransformData *td, void *ptr)
624 gpointer p = g_hash_table_lookup (td->data_hash, ptr);
627 return GPOINTER_TO_UINT (p) - 1;
628 if (td->max_data_items == td->n_data_items) {
629 td->max_data_items = td->n_data_items == 0 ? 16 : 2 * td->max_data_items;
630 td->data_items = g_realloc (td->data_items, td->max_data_items * sizeof(td->data_items [0]));
632 index = td->n_data_items;
633 td->data_items [index] = ptr;
635 g_hash_table_insert (td->data_hash, ptr, GUINT_TO_POINTER (index + 1));
640 jit_call_supported (MonoMethod *method, MonoMethodSignature *sig)
644 if (sig->param_count > 6)
648 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
650 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
652 if (method->is_inflated)
654 if (method->string_ctor)
657 for (l = jit_classes; l; l = l->next) {
658 char *class_name = l->data;
660 if (!strcmp (method->klass->name, class_name))
669 interp_transform_call (TransformData *td, MonoMethod *method, MonoMethod *target_method, MonoDomain *domain, MonoGenericContext *generic_context, unsigned char *is_bb_start, int body_start_offset, MonoClass *constrained_class, gboolean readonly)
671 MonoImage *image = method->klass->image;
672 MonoMethodSignature *csignature;
674 int virtual = *td->ip == CEE_CALLVIRT;
675 int calli = *td->ip == CEE_CALLI || *td->ip == CEE_MONO_CALLI_EXTRA_ARG;
677 guint32 vt_stack_used = 0;
678 guint32 vt_res_size = 0;
683 guint32 token = read32 (td->ip + 1);
685 if (target_method == NULL) {
688 native = (method->wrapper_type != MONO_WRAPPER_DELEGATE_INVOKE && td->sp [-1].type == STACK_TYPE_I);
690 if (method->wrapper_type != MONO_WRAPPER_NONE)
691 csignature = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
693 csignature = mono_metadata_parse_signature (image, token);
695 if (generic_context) {
696 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
697 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
700 target_method = NULL;
702 if (method->wrapper_type == MONO_WRAPPER_NONE)
703 target_method = mono_get_method_full (image, token, NULL, generic_context);
705 target_method = (MonoMethod *)mono_method_get_wrapper_data (method, token);
706 csignature = mono_method_signature (target_method);
707 if (target_method->klass == mono_defaults.string_class) {
708 if (target_method->name [0] == 'g') {
709 if (strcmp (target_method->name, "get_Chars") == 0)
711 else if (strcmp (target_method->name, "get_Length") == 0)
714 } else if (mono_class_is_subclass_of (target_method->klass, mono_defaults.array_class, FALSE)) {
715 if (!strcmp (target_method->name, "get_Rank")) {
716 op = MINT_ARRAY_RANK;
717 } else if (!strcmp (target_method->name, "get_Length")) {
719 } else if (!strcmp (target_method->name, "Address")) {
720 op = readonly ? MINT_LDELEMA : MINT_LDELEMA_TC;
722 } else if (target_method && generic_context) {
723 csignature = mono_inflate_generic_signature (csignature, generic_context, &error);
724 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
725 target_method = mono_class_inflate_generic_method_checked (target_method, generic_context, &error);
726 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
730 csignature = mono_method_signature (target_method);
733 if (constrained_class) {
734 if (constrained_class->enumtype && !strcmp (target_method->name, "GetHashCode")) {
735 /* Use the corresponding method from the base type to avoid boxing */
736 MonoType *base_type = mono_class_enum_basetype (constrained_class);
737 g_assert (base_type);
738 constrained_class = mono_class_from_mono_type (base_type);
739 target_method = mono_class_get_method_from_name (constrained_class, target_method->name, 0);
740 g_assert (target_method);
744 if (constrained_class) {
745 mono_class_setup_vtable (constrained_class);
747 g_print ("CONSTRAINED.CALLVIRT: %s::%s. %s (%p) ->\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
749 target_method = mono_get_method_constrained_with_method (image, target_method, constrained_class, generic_context, &error);
751 g_print (" : %s::%s. %s (%p)\n", target_method->klass->name, target_method->name, mono_signature_full_name (target_method->signature), target_method);
753 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
754 mono_class_setup_vtable (target_method->klass);
756 if (constrained_class->valuetype && (target_method->klass == mono_defaults.object_class || target_method->klass == mono_defaults.enum_class->parent || target_method->klass == mono_defaults.enum_class)) {
757 if (target_method->klass == mono_defaults.enum_class && (td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
758 /* managed pointer on the stack, we need to deref that puppy */
759 ADD_CODE (td, MINT_LDIND_I);
760 ADD_CODE (td, csignature->param_count);
762 ADD_CODE (td, MINT_BOX);
763 ADD_CODE (td, get_data_item_index (td, constrained_class));
764 ADD_CODE (td, csignature->param_count);
765 } else if (!constrained_class->valuetype) {
766 /* managed pointer on the stack, we need to deref that puppy */
767 ADD_CODE (td, MINT_LDIND_I);
768 ADD_CODE (td, csignature->param_count);
770 if (target_method->klass->valuetype) {
773 /* Interface method */
776 mono_class_setup_vtable (constrained_class);
777 ioffset = mono_class_interface_offset (constrained_class, target_method->klass);
779 g_error ("type load error: constrained_class");
780 slot = mono_method_get_vtable_slot (target_method);
782 g_error ("type load error: target_method->klass");
783 target_method = constrained_class->vtable [ioffset + slot];
785 if (target_method->klass == mono_defaults.enum_class) {
786 if ((td->sp - csignature->param_count - 1)->type == STACK_TYPE_MP) {
787 /* managed pointer on the stack, we need to deref that puppy */
788 ADD_CODE (td, MINT_LDIND_I);
789 ADD_CODE (td, csignature->param_count);
791 ADD_CODE (td, MINT_BOX);
792 ADD_CODE (td, get_data_item_index (td, constrained_class));
793 ADD_CODE (td, csignature->param_count);
801 mono_class_init (target_method->klass);
803 CHECK_STACK (td, csignature->param_count + csignature->hasthis);
804 if (!calli && (!virtual || (target_method->flags & METHOD_ATTRIBUTE_VIRTUAL) == 0) &&
805 (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) == 0 &&
806 (target_method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) == 0) {
807 int called_inited = mono_class_vtable (domain, target_method->klass)->initialized;
808 MonoMethodHeader *mheader = mono_method_get_header (target_method);
810 if (/*mono_metadata_signature_equal (method->signature, target_method->signature) */ method == target_method && *(td->ip + 5) == CEE_RET) {
812 if (mono_interp_traceopt)
813 g_print ("Optimize tail call of %s.%s\n", target_method->klass->name, target_method->name);
815 for (i = csignature->param_count - 1 + !!csignature->hasthis; i >= 0; --i)
818 ADD_CODE(td, MINT_BR_S);
819 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
820 ADD_CODE(td, offset);
821 if (!is_bb_start [td->ip + 5 - td->il_code])
822 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
826 /* mheader might not exist if this is a delegate invoc, etc */
827 if (mheader && *mheader->code == CEE_RET && called_inited) {
828 if (mono_interp_traceopt)
829 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
830 for (i = 0; i < csignature->param_count; i++)
831 ADD_CODE (td, MINT_POP); /*FIX: vt */
833 if (csignature->hasthis) {
835 ADD_CODE(td, MINT_CKNULL);
836 ADD_CODE (td, MINT_POP);
839 td->sp -= csignature->param_count + csignature->hasthis;
845 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
846 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
847 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
848 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
849 target_method = mono_marshal_get_synchronized_wrapper (target_method);
851 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
852 td->sp -= csignature->param_count + !!csignature->hasthis;
853 for (i = 0; i < csignature->param_count; ++i) {
854 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
856 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
857 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
858 size = mono_class_native_size (klass, NULL);
860 size = mono_class_value_size (klass, NULL);
861 size = (size + 7) & ~7;
862 vt_stack_used += size;
866 /* need to handle typedbyref ... */
867 if (csignature->ret->type != MONO_TYPE_VOID) {
868 int mt = mint_type(csignature->ret);
869 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
870 if (mt == MINT_TYPE_VT) {
871 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
872 vt_res_size = mono_class_native_size (klass, NULL);
874 vt_res_size = mono_class_value_size (klass, NULL);
875 PUSH_VT(td, vt_res_size);
877 PUSH_TYPE(td, stack_type[mt], klass);
883 #if SIZEOF_VOID_P == 8
884 if (op == MINT_LDLEN)
885 ADD_CODE (td, MINT_CONV_I4_I8);
887 if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
888 ADD_CODE (td, get_data_item_index (td, target_method->klass));
889 ADD_CODE (td, 1 + target_method->klass->rank);
891 } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
892 ADD_CODE(td, MINT_JIT_CALL);
893 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
894 mono_error_assert_ok (&error);
897 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
899 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
901 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
904 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
906 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
907 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
911 if (vt_stack_used != 0 || vt_res_size != 0) {
912 ADD_CODE(td, MINT_VTRESULT);
913 ADD_CODE(td, vt_res_size);
914 WRITE32(td, &vt_stack_used);
915 td->vt_sp -= vt_stack_used;
919 static MonoClassField *
920 interp_field_from_token (MonoMethod *method, guint32 token, MonoClass **klass, MonoGenericContext *generic_context)
922 MonoClassField *field = NULL;
923 if (method->wrapper_type != MONO_WRAPPER_NONE) {
924 field = (MonoClassField *) mono_method_get_wrapper_data (method, token);
925 *klass = field->parent;
929 field = mono_field_from_token_checked (method->klass->image, token, klass, generic_context, &error);
930 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
936 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
938 MonoMethodHeader *header = mono_method_get_header (method);
939 MonoMethodSignature *signature = mono_method_signature (method);
940 MonoImage *image = method->klass->image;
941 MonoDomain *domain = mono_domain_get ();
942 MonoClass *constrained_class = NULL;
944 int offset, mt, i, i32;
945 gboolean readonly = FALSE;
947 MonoClassField *field;
948 const unsigned char *end;
949 int new_in_start_offset;
950 int body_start_offset;
954 int generating_code = 1;
956 memset(&td, 0, sizeof(td));
959 td.is_bb_start = is_bb_start;
960 td.il_code = header->code;
961 td.code_size = header->code_size;
963 td.max_code_size = td.code_size;
964 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
965 td.new_code_end = td.new_code + td.max_code_size;
966 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
967 td.forward_refs = g_malloc(header->code_size * sizeof(int));
968 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
969 td.stack_height = g_malloc(header->code_size * sizeof(int));
970 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
972 td.max_data_items = 0;
973 td.data_items = NULL;
974 td.data_hash = g_hash_table_new (NULL, NULL);
975 rtm->data_items = td.data_items;
976 for (i = 0; i < header->code_size; i++) {
977 td.forward_refs [i] = -1;
978 td.stack_height [i] = -1;
980 td.new_ip = td.new_code;
981 td.last_new_ip = NULL;
983 td.stack = g_malloc0 ((header->max_stack + 1) * sizeof (td.stack [0]));
985 td.max_stack_height = 0;
987 for (i = 0; i < header->num_clauses; i++) {
988 MonoExceptionClause *c = header->clauses + i;
989 td.stack_height [c->handler_offset] = 0;
990 td.vt_stack_size [c->handler_offset] = 0;
991 td.is_bb_start [c->handler_offset] = 1;
993 td.stack_height [c->handler_offset] = 1;
994 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
995 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
996 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
998 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
999 td.stack_height [c->data.filter_offset] = 0;
1000 td.vt_stack_size [c->data.filter_offset] = 0;
1001 td.is_bb_start [c->data.filter_offset] = 1;
1003 td.stack_height [c->data.filter_offset] = 1;
1004 td.stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
1005 td.stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
1006 td.stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
1010 td.ip = header->code;
1011 end = td.ip + header->code_size;
1013 if (mono_interp_traceopt) {
1014 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
1015 char *name = mono_method_full_name (method, TRUE);
1016 g_print ("Method %s, original code:\n", name);
1017 g_print ("%s\n", tmp);
1022 if (signature->hasthis)
1023 store_inarg (&td, 0);
1024 for (i = 0; i < signature->param_count; i++)
1025 store_inarg (&td, i + !!signature->hasthis);
1027 body_start_offset = td.new_ip - td.new_code;
1029 for (i = 0; i < header->num_locals; i++) {
1030 int mt = mint_type(header->locals [i]);
1031 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
1032 ADD_CODE(&td, MINT_INITLOCALS);
1037 while (td.ip < end) {
1040 g_assert (td.sp >= td.stack);
1041 g_assert (td.vt_sp < 0x10000000);
1042 in_offset = td.ip - header->code;
1043 td.in_offsets [in_offset] = td.new_ip - td.new_code;
1044 new_in_start_offset = td.new_ip - td.new_code;
1045 td.in_start = td.ip;
1046 while (td.forward_refs [in_offset] >= 0) {
1047 int j = td.forward_refs [in_offset];
1049 td.forward_refs [in_offset] = td.forward_refs [j];
1050 if (td.in_offsets [j] < 0) {
1051 int old_switch_offset = -td.in_offsets [j];
1052 int new_switch_offset = td.in_offsets [old_switch_offset];
1053 int switch_case = (j - old_switch_offset - 5) / 4;
1054 int n_cases = read32 (header->code + old_switch_offset + 1);
1055 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
1056 slot = new_switch_offset + 3 + 2 * switch_case;
1057 td.new_code [slot] = * (unsigned short *)(&offset);
1058 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1060 int op = td.new_code [td.in_offsets [j]];
1061 if (mono_interp_opargtype [op] == MintOpShortBranch) {
1062 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1063 g_assert (offset <= 32767);
1064 slot = td.in_offsets [j] + 1;
1065 td.new_code [slot] = offset;
1067 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1068 slot = td.in_offsets [j] + 1;
1069 td.new_code [slot] = * (unsigned short *)(&offset);
1070 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1074 if (td.stack_height [in_offset] >= 0) {
1075 g_assert (is_bb_start [in_offset]);
1076 if (td.stack_height [in_offset] > 0)
1077 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
1078 td.sp = td.stack + td.stack_height [in_offset];
1079 td.vt_sp = td.vt_stack_size [in_offset];
1081 if (is_bb_start [in_offset]) {
1082 generating_code = 1;
1084 if (!generating_code) {
1085 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1089 if (mono_interp_traceopt > 1) {
1090 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1092 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1093 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1094 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1095 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1096 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1097 (td.sp > td.stack && (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_VT)) ? (td.sp [-1].klass == NULL ? "?" : td.sp [-1].klass->name) : "",
1098 td.vt_sp, td.max_vt_sp);
1106 SIMPLE_OP(td, MINT_BREAK);
1112 load_arg (&td, *td.ip - CEE_LDARG_0);
1119 load_local (&td, *td.ip - CEE_LDLOC_0);
1126 store_local (&td, *td.ip - CEE_STLOC_0);
1130 load_arg (&td, ((guint8 *)td.ip)[1]);
1133 case CEE_LDARGA_S: {
1134 /* NOTE: n includes this */
1135 int n = ((guint8 *) td.ip) [1];
1136 ADD_CODE (&td, MINT_LDARGA);
1137 ADD_CODE (&td, td.rtm->arg_offsets [n]);
1138 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1143 store_arg (&td, ((guint8 *)td.ip)[1]);
1147 load_local (&td, ((guint8 *)td.ip)[1]);
1151 ADD_CODE(&td, MINT_LDLOCA_S);
1152 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1153 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1157 store_local (&td, ((guint8 *)td.ip)[1]);
1161 SIMPLE_OP(td, MINT_LDNULL);
1162 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1165 SIMPLE_OP(td, MINT_LDC_I4_M1);
1166 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1169 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1170 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1171 SIMPLE_OP(td, MINT_CEQ0_I4);
1174 SIMPLE_OP(td, MINT_LDC_I4_0);
1175 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1179 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1180 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1181 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1184 SIMPLE_OP(td, MINT_LDC_I4_1);
1185 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1195 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1196 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1199 ADD_CODE(&td, MINT_LDC_I4_S);
1200 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1202 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1205 i32 = read32 (td.ip + 1);
1206 ADD_CODE(&td, MINT_LDC_I4);
1209 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1212 gint64 val = read64 (td.ip + 1);
1213 ADD_CODE(&td, MINT_LDC_I8);
1216 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1221 readr4 (td.ip + 1, &val);
1222 ADD_CODE(&td, MINT_LDC_R4);
1225 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1230 readr8 (td.ip + 1, &val);
1231 ADD_CODE(&td, MINT_LDC_R8);
1234 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1238 int type = td.sp [-1].type;
1239 MonoClass *klass = td.sp [-1].klass;
1240 if (td.sp [-1].type == STACK_TYPE_VT) {
1241 gint32 size = mono_class_value_size (klass, NULL);
1243 ADD_CODE(&td, MINT_DUP_VT);
1244 WRITE32(&td, &size);
1247 SIMPLE_OP(td, MINT_DUP);
1248 PUSH_TYPE(&td, type, klass);
1252 CHECK_STACK(&td, 1);
1253 SIMPLE_OP(td, MINT_POP);
1255 if (td.sp [-1].type == STACK_TYPE_VT) {
1256 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1257 size = (size + 7) & ~7;
1258 ADD_CODE(&td, MINT_VTRESULT);
1260 WRITE32(&td, &size);
1267 if (td.sp > td.stack)
1268 g_warning ("CEE_JMP: stack must be empty");
1269 token = read32 (td.ip + 1);
1270 m = mono_get_method_full (image, token, NULL, generic_context);
1271 ADD_CODE (&td, MINT_JMP);
1272 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1273 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1277 case CEE_CALLVIRT: /* Fall through */
1278 case CEE_CALLI: /* Fall through */
1280 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
1281 constrained_class = NULL;
1287 if (signature->ret->type != MONO_TYPE_VOID) {
1289 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1290 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1291 vt_size = mono_class_value_size (klass, NULL);
1292 vt_size = (vt_size + 7) & ~7;
1295 if (td.sp > td.stack)
1296 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1297 if (td.vt_sp != vt_size)
1298 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1300 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1302 ADD_CODE(&td, MINT_RET_VT);
1303 WRITE32(&td, &vt_size);
1306 generating_code = 0;
1310 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1312 generating_code = 0;
1315 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1317 generating_code = 0;
1320 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1324 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1328 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1332 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1336 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1340 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1344 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1348 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1352 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1356 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1360 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1364 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1368 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1372 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1376 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1380 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1384 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1388 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1392 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1396 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1400 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1404 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1408 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1412 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1417 const unsigned char *next_ip;
1418 const unsigned char *base_ip = td.ip;
1419 unsigned short *next_new_ip;
1422 ADD_CODE (&td, MINT_SWITCH);
1425 next_ip = td.ip + n * 4;
1426 next_new_ip = td.new_ip + n * 2;
1428 int stack_height = td.sp - td.stack;
1429 for (i = 0; i < n; i++) {
1430 offset = read32 (td.ip);
1431 target = next_ip - td.il_code + offset;
1434 if (stack_height > 0 && stack_height != td.stack_height [target])
1435 g_warning ("SWITCH with back branch and non-empty stack");
1437 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1439 td.stack_height [target] = stack_height;
1440 td.vt_stack_size [target] = td.vt_sp;
1441 if (stack_height > 0)
1442 td.stack_state [target] = g_memdup (td.stack, stack_height * sizeof (td.stack [0]));
1443 int prev = td.forward_refs [target];
1444 td.forward_refs [td.ip - td.il_code] = prev;
1445 td.forward_refs [target] = td.ip - td.il_code;
1446 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1448 WRITE32 (&td, &target);
1454 CHECK_STACK (&td, 1);
1455 SIMPLE_OP (td, MINT_LDIND_I1);
1456 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1459 CHECK_STACK (&td, 1);
1460 SIMPLE_OP (td, MINT_LDIND_U1);
1461 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1464 CHECK_STACK (&td, 1);
1465 SIMPLE_OP (td, MINT_LDIND_I2);
1466 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1469 CHECK_STACK (&td, 1);
1470 SIMPLE_OP (td, MINT_LDIND_U2);
1471 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1474 CHECK_STACK (&td, 1);
1475 SIMPLE_OP (td, MINT_LDIND_I4);
1476 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1479 CHECK_STACK (&td, 1);
1480 SIMPLE_OP (td, MINT_LDIND_U4);
1481 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1484 CHECK_STACK (&td, 1);
1485 SIMPLE_OP (td, MINT_LDIND_I8);
1486 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1489 CHECK_STACK (&td, 1);
1490 SIMPLE_OP (td, MINT_LDIND_I);
1492 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1495 CHECK_STACK (&td, 1);
1496 SIMPLE_OP (td, MINT_LDIND_R4);
1497 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1500 CHECK_STACK (&td, 1);
1501 SIMPLE_OP (td, MINT_LDIND_R8);
1502 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1505 CHECK_STACK (&td, 1);
1506 SIMPLE_OP (td, MINT_LDIND_REF);
1507 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1510 CHECK_STACK (&td, 2);
1511 SIMPLE_OP (td, MINT_STIND_REF);
1515 CHECK_STACK (&td, 2);
1516 SIMPLE_OP (td, MINT_STIND_I1);
1520 CHECK_STACK (&td, 2);
1521 SIMPLE_OP (td, MINT_STIND_I2);
1525 CHECK_STACK (&td, 2);
1526 SIMPLE_OP (td, MINT_STIND_I4);
1530 CHECK_STACK (&td, 2);
1531 SIMPLE_OP (td, MINT_STIND_I);
1535 CHECK_STACK (&td, 2);
1536 SIMPLE_OP (td, MINT_STIND_I8);
1540 CHECK_STACK (&td, 2);
1541 SIMPLE_OP (td, MINT_STIND_R4);
1545 CHECK_STACK (&td, 2);
1546 SIMPLE_OP (td, MINT_STIND_R8);
1550 binary_arith_op(&td, MINT_ADD_I4);
1554 binary_arith_op(&td, MINT_SUB_I4);
1558 binary_arith_op(&td, MINT_MUL_I4);
1562 binary_arith_op(&td, MINT_DIV_I4);
1566 binary_arith_op(&td, MINT_DIV_UN_I4);
1570 binary_int_op (&td, MINT_REM_I4);
1574 binary_int_op (&td, MINT_REM_UN_I4);
1578 binary_int_op (&td, MINT_AND_I4);
1582 binary_int_op (&td, MINT_OR_I4);
1586 binary_int_op (&td, MINT_XOR_I4);
1590 shift_op (&td, MINT_SHL_I4);
1594 shift_op (&td, MINT_SHR_I4);
1598 shift_op (&td, MINT_SHR_UN_I4);
1602 unary_arith_op (&td, MINT_NEG_I4);
1606 unary_arith_op (&td, MINT_NOT_I4);
1610 CHECK_STACK (&td, 1);
1611 switch (td.sp [-1].type) {
1613 ADD_CODE(&td, MINT_CONV_U1_R8);
1616 ADD_CODE(&td, MINT_CONV_U1_I4);
1619 ADD_CODE(&td, MINT_CONV_U1_I8);
1622 g_assert_not_reached ();
1625 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1628 CHECK_STACK (&td, 1);
1629 switch (td.sp [-1].type) {
1631 ADD_CODE(&td, MINT_CONV_I1_R8);
1634 ADD_CODE(&td, MINT_CONV_I1_I4);
1637 ADD_CODE(&td, MINT_CONV_I1_I8);
1640 g_assert_not_reached ();
1643 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1646 CHECK_STACK (&td, 1);
1647 switch (td.sp [-1].type) {
1649 ADD_CODE(&td, MINT_CONV_U2_R8);
1652 ADD_CODE(&td, MINT_CONV_U2_I4);
1655 ADD_CODE(&td, MINT_CONV_U2_I8);
1658 g_assert_not_reached ();
1661 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1664 CHECK_STACK (&td, 1);
1665 switch (td.sp [-1].type) {
1667 ADD_CODE(&td, MINT_CONV_I2_R8);
1670 ADD_CODE(&td, MINT_CONV_I2_I4);
1673 ADD_CODE(&td, MINT_CONV_I2_I8);
1676 g_assert_not_reached ();
1679 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1682 CHECK_STACK (&td, 1);
1683 switch (td.sp [-1].type) {
1685 #if SIZEOF_VOID_P == 4
1686 ADD_CODE(&td, MINT_CONV_U4_R8);
1688 ADD_CODE(&td, MINT_CONV_U8_R8);
1692 #if SIZEOF_VOID_P == 8
1693 ADD_CODE(&td, MINT_CONV_U8_I4);
1697 #if SIZEOF_VOID_P == 4
1698 ADD_CODE(&td, MINT_CONV_U4_I8);
1704 g_assert_not_reached ();
1707 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1710 CHECK_STACK (&td, 1);
1711 switch (td.sp [-1].type) {
1713 #if SIZEOF_VOID_P == 8
1714 ADD_CODE(&td, MINT_CONV_I8_R8);
1716 ADD_CODE(&td, MINT_CONV_I4_R8);
1720 #if SIZEOF_VOID_P == 8
1721 ADD_CODE(&td, MINT_CONV_I8_I4);
1729 #if SIZEOF_VOID_P == 4
1730 ADD_CODE(&td, MINT_CONV_I4_I8);
1734 g_assert_not_reached ();
1737 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1740 CHECK_STACK (&td, 1);
1741 switch (td.sp [-1].type) {
1743 ADD_CODE(&td, MINT_CONV_U4_R8);
1748 ADD_CODE(&td, MINT_CONV_U4_I8);
1751 #if SIZEOF_VOID_P == 8
1752 ADD_CODE(&td, MINT_CONV_U4_I8);
1756 g_assert_not_reached ();
1759 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1762 CHECK_STACK (&td, 1);
1763 switch (td.sp [-1].type) {
1765 ADD_CODE(&td, MINT_CONV_I4_R8);
1770 ADD_CODE(&td, MINT_CONV_I4_I8);
1773 #if SIZEOF_VOID_P == 8
1774 ADD_CODE(&td, MINT_CONV_I4_I8);
1778 g_assert_not_reached ();
1781 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1784 CHECK_STACK (&td, 1);
1785 switch (td.sp [-1].type) {
1787 ADD_CODE(&td, MINT_CONV_I8_R8);
1790 ADD_CODE(&td, MINT_CONV_I8_I4);
1795 #if SIZEOF_VOID_P == 4
1796 ADD_CODE(&td, MINT_CONV_I8_I4);
1800 g_assert_not_reached ();
1803 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1806 CHECK_STACK (&td, 1);
1807 switch (td.sp [-1].type) {
1809 ADD_CODE(&td, MINT_CONV_R4_R8);
1812 ADD_CODE(&td, MINT_CONV_R4_I8);
1815 ADD_CODE(&td, MINT_CONV_R4_I4);
1818 g_assert_not_reached ();
1821 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1824 CHECK_STACK (&td, 1);
1825 switch (td.sp [-1].type) {
1827 ADD_CODE(&td, MINT_CONV_R8_I4);
1830 ADD_CODE(&td, MINT_CONV_R8_I8);
1835 g_assert_not_reached ();
1838 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1841 CHECK_STACK (&td, 1);
1842 switch (td.sp [-1].type) {
1844 ADD_CODE(&td, MINT_CONV_U8_I4);
1849 ADD_CODE(&td, MINT_CONV_U8_R8);
1852 #if SIZEOF_VOID_P == 4
1853 ADD_CODE(&td, MINT_CONV_U8_I4);
1857 g_assert_not_reached ();
1860 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1863 CHECK_STACK (&td, 2);
1865 token = read32 (td.ip + 1);
1866 klass = mono_class_get_full (image, token, generic_context);
1868 if (klass->valuetype) {
1869 ADD_CODE (&td, MINT_CPOBJ);
1870 ADD_CODE (&td, get_data_item_index(&td, klass));
1872 ADD_CODE (&td, MINT_LDIND_REF);
1873 ADD_CODE (&td, MINT_STIND_REF);
1881 CHECK_STACK (&td, 1);
1883 token = read32 (td.ip + 1);
1885 if (method->wrapper_type != MONO_WRAPPER_NONE)
1886 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1888 klass = mono_class_get_full (image, token, generic_context);
1890 ADD_CODE(&td, MINT_LDOBJ);
1891 ADD_CODE(&td, get_data_item_index(&td, klass));
1892 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1893 size = mono_class_value_size (klass, NULL);
1897 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1902 token = mono_metadata_token_index (read32 (td.ip + 1));
1904 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1905 s = mono_string_new_wrapper(
1906 mono_method_get_wrapper_data (method, token));
1909 s = mono_ldstr (domain, image, token);
1910 ADD_CODE(&td, MINT_LDSTR);
1911 ADD_CODE(&td, get_data_item_index (&td, s));
1912 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1917 MonoMethodSignature *csignature;
1918 guint32 vt_stack_used = 0;
1919 guint32 vt_res_size = 0;
1922 token = read32 (td.ip);
1925 if (method->wrapper_type != MONO_WRAPPER_NONE)
1926 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1928 m = mono_get_method_full (image, token, NULL, generic_context);
1930 csignature = mono_method_signature (m);
1932 td.sp -= csignature->param_count;
1933 ADD_CODE(&td, MINT_NEWOBJ);
1934 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1935 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1937 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1938 vt_res_size = mono_class_value_size (klass, NULL);
1939 PUSH_VT (&td, vt_res_size);
1941 for (i = 0; i < csignature->param_count; ++i) {
1942 int mt = mint_type(csignature->params [i]);
1943 if (mt == MINT_TYPE_VT) {
1944 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1945 gint32 size = mono_class_value_size (k, NULL);
1946 size = (size + 7) & ~7;
1947 vt_stack_used += size;
1950 if (vt_stack_used != 0 || vt_res_size != 0) {
1951 ADD_CODE(&td, MINT_VTRESULT);
1952 ADD_CODE(&td, vt_res_size);
1953 WRITE32(&td, &vt_stack_used);
1954 td.vt_sp -= vt_stack_used;
1956 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1960 CHECK_STACK (&td, 1);
1961 token = read32 (td.ip + 1);
1962 klass = mini_get_class (method, token, generic_context);
1963 ADD_CODE(&td, MINT_CASTCLASS);
1964 ADD_CODE(&td, get_data_item_index (&td, klass));
1965 td.sp [-1].klass = klass;
1969 CHECK_STACK (&td, 1);
1970 token = read32 (td.ip + 1);
1971 klass = mini_get_class (method, token, generic_context);
1972 ADD_CODE(&td, MINT_ISINST);
1973 ADD_CODE(&td, get_data_item_index (&td, klass));
1977 switch (td.sp [-1].type) {
1981 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1984 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1987 g_assert_not_reached ();
1989 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1993 CHECK_STACK (&td, 1);
1994 token = read32 (td.ip + 1);
1996 if (method->wrapper_type != MONO_WRAPPER_NONE)
1997 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1999 klass = mono_class_get_full (image, token, generic_context);
2001 if (mono_class_is_nullable (klass)) {
2002 g_error ("cee_unbox: implement Nullable");
2005 ADD_CODE(&td, MINT_UNBOX);
2006 ADD_CODE(&td, get_data_item_index (&td, klass));
2007 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2011 CHECK_STACK (&td, 1);
2012 token = read32 (td.ip + 1);
2014 klass = mini_get_class (method, token, generic_context);
2016 if (mini_type_is_reference (&klass->byval_arg)) {
2017 int mt = mint_type (&klass->byval_arg);
2018 ADD_CODE (&td, MINT_CASTCLASS);
2019 ADD_CODE (&td, get_data_item_index (&td, klass));
2020 SET_TYPE (td.sp - 1, stack_type [mt], klass);
2022 } else if (mono_class_is_nullable (klass)) {
2023 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
2024 /* td.ip is incremented by interp_transform_call */
2025 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2027 int mt = mint_type (&klass->byval_arg);
2028 ADD_CODE (&td, MINT_UNBOX);
2029 ADD_CODE (&td, get_data_item_index (&td, klass));
2031 ADD_CODE (&td, MINT_LDOBJ);
2032 ADD_CODE (&td, get_data_item_index(&td, klass));
2033 SET_TYPE (td.sp - 1, stack_type [mt], klass);
2035 if (mt == MINT_TYPE_VT) {
2036 int size = mono_class_value_size (klass, NULL);
2037 PUSH_VT (&td, size);
2044 CHECK_STACK (&td, 1);
2045 SIMPLE_OP (td, MINT_THROW);
2047 generating_code = 0;
2050 CHECK_STACK (&td, 1);
2051 token = read32 (td.ip + 1);
2052 field = interp_field_from_token (method, token, &klass, generic_context);
2053 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2054 mono_class_init (klass);
2056 ADD_CODE (&td, MINT_POP);
2058 ADD_CODE (&td, MINT_LDSFLDA);
2059 ADD_CODE (&td, get_data_item_index (&td, field));
2061 if ((td.sp - 1)->type == STACK_TYPE_O) {
2062 ADD_CODE (&td, MINT_LDFLDA);
2064 g_assert ((td.sp -1)->type == STACK_TYPE_MP);
2065 ADD_CODE (&td, MINT_LDFLDA_UNSAFE);
2067 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2070 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2073 CHECK_STACK (&td, 1);
2074 token = read32 (td.ip + 1);
2075 field = interp_field_from_token (method, token, &klass, generic_context);
2076 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2077 mono_class_init (klass);
2079 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2080 mt = mint_type (&field_klass->byval_arg);
2081 if (klass->marshalbyref) {
2082 g_assert (!is_static);
2083 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2084 ADD_CODE(&td, get_data_item_index (&td, field));
2087 ADD_CODE (&td, MINT_POP);
2089 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2090 ADD_CODE (&td, get_data_item_index (&td, field));
2092 ADD_CODE (&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2093 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2096 if (mt == MINT_TYPE_VT) {
2097 int size = mono_class_value_size (field_klass, NULL);
2099 WRITE32(&td, &size);
2101 if (td.sp [-1].type == STACK_TYPE_VT) {
2102 int size = mono_class_value_size (klass, NULL);
2103 size = (size + 7) & ~7;
2105 ADD_CODE (&td, MINT_VTRESULT);
2107 WRITE32 (&td, &size);
2110 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
2114 CHECK_STACK (&td, 2);
2115 token = read32 (td.ip + 1);
2116 field = interp_field_from_token (method, token, &klass, generic_context);
2117 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2118 mono_class_init (klass);
2119 mt = mint_type(field->type);
2121 if (klass->marshalbyref) {
2122 g_assert (!is_static);
2123 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2124 ADD_CODE(&td, get_data_item_index (&td, field));
2127 ADD_CODE (&td, MINT_POP);
2129 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2130 ADD_CODE (&td, get_data_item_index (&td, field));
2132 ADD_CODE (&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2133 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2136 if (mt == MINT_TYPE_VT) {
2137 MonoClass *klass = mono_class_from_mono_type (field->type);
2138 int size = mono_class_value_size (klass, NULL);
2140 WRITE32(&td, &size);
2147 token = read32 (td.ip + 1);
2148 field = interp_field_from_token (method, token, &klass, generic_context);
2149 ADD_CODE(&td, MINT_LDSFLDA);
2150 ADD_CODE(&td, get_data_item_index (&td, field));
2152 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2155 token = read32 (td.ip + 1);
2156 field = interp_field_from_token (method, token, &klass, generic_context);
2157 mt = mint_type(field->type);
2158 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2159 ADD_CODE(&td, get_data_item_index (&td, field));
2161 if (mt == MINT_TYPE_VT) {
2162 MonoClass *klass = mono_class_from_mono_type (field->type);
2163 int size = mono_class_value_size (klass, NULL);
2165 WRITE32(&td, &size);
2166 klass = field->type->data.klass;
2168 if (mt == MINT_TYPE_O)
2169 klass = mono_class_from_mono_type (field->type);
2172 PUSH_TYPE(&td, stack_type [mt], klass);
2175 CHECK_STACK (&td, 1);
2176 token = read32 (td.ip + 1);
2177 field = interp_field_from_token (method, token, &klass, generic_context);
2178 mt = mint_type(field->type);
2179 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2180 ADD_CODE(&td, get_data_item_index (&td, field));
2181 if (mt == MINT_TYPE_VT) {
2182 MonoClass *klass = mono_class_from_mono_type (field->type);
2183 int size = mono_class_value_size (klass, NULL);
2185 WRITE32 (&td, &size);
2192 token = read32 (td.ip + 1);
2194 if (method->wrapper_type != MONO_WRAPPER_NONE)
2195 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2197 klass = mini_get_class (method, token, generic_context);
2199 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2200 ADD_CODE(&td, get_data_item_index (&td, klass));
2201 if (td.sp [-1].type == STACK_TYPE_VT) {
2202 size = mono_class_value_size (klass, NULL);
2203 size = (size + 7) & ~7;
2210 case CEE_CONV_OVF_I_UN:
2211 case CEE_CONV_OVF_U_UN:
2212 CHECK_STACK (&td, 1);
2213 switch (td.sp [-1].type) {
2215 #if SIZEOF_VOID_P == 8
2216 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2218 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2222 #if SIZEOF_VOID_P == 4
2223 ADD_CODE (&td, MINT_CONV_OVF_I4_UN_I8);
2227 #if SIZEOF_VOID_P == 8
2228 ADD_CODE(&td, MINT_CONV_I8_U4);
2232 g_assert_not_reached ();
2235 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2238 case CEE_CONV_OVF_I8_UN:
2239 case CEE_CONV_OVF_U8_UN:
2240 CHECK_STACK (&td, 1);
2241 switch (td.sp [-1].type) {
2243 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2246 if (*td.ip == CEE_CONV_OVF_I8_UN)
2247 ADD_CODE (&td, MINT_CONV_OVF_I8_U8);
2250 ADD_CODE(&td, MINT_CONV_I8_U4);
2253 g_assert_not_reached ();
2256 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2261 CHECK_STACK (&td, 1);
2262 token = read32 (td.ip + 1);
2263 if (method->wrapper_type != MONO_WRAPPER_NONE)
2264 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2266 klass = mini_get_class (method, token, generic_context);
2268 if (mono_class_is_nullable (klass)) {
2269 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2270 /* td.ip is incremented by interp_transform_call */
2271 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2272 } else if (!klass->valuetype) {
2273 /* already boxed, do nothing. */
2276 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2277 size = mono_class_value_size (klass, NULL);
2278 size = (size + 7) & ~7;
2281 ADD_CODE(&td, MINT_BOX);
2282 ADD_CODE(&td, get_data_item_index (&td, klass));
2284 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2291 CHECK_STACK (&td, 1);
2292 token = read32 (td.ip + 1);
2294 if (method->wrapper_type != MONO_WRAPPER_NONE)
2295 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2297 klass = mini_get_class (method, token, generic_context);
2299 unsigned char lentype = (td.sp - 1)->type;
2300 if (lentype == STACK_TYPE_I8) {
2301 /* mimic mini behaviour */
2302 ADD_CODE (&td, MINT_CONV_OVF_U4_I8);
2304 g_assert (lentype == STACK_TYPE_I4);
2305 ADD_CODE (&td, MINT_CONV_OVF_U4_I4);
2307 SET_SIMPLE_TYPE (td.sp - 1, STACK_TYPE_I4);
2308 ADD_CODE (&td, MINT_NEWARR);
2309 ADD_CODE (&td, get_data_item_index (&td, klass));
2310 SET_TYPE (td.sp - 1, STACK_TYPE_O, klass);
2315 CHECK_STACK (&td, 1);
2316 SIMPLE_OP (td, MINT_LDLEN);
2317 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2320 CHECK_STACK (&td, 2);
2322 token = read32 (td.ip + 1);
2324 if (method->wrapper_type != MONO_WRAPPER_NONE)
2325 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2327 klass = mini_get_class (method, token, generic_context);
2329 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2330 ADD_CODE (&td, MINT_LDELEMA_TC);
2332 ADD_CODE (&td, MINT_LDELEMA);
2334 ADD_CODE (&td, get_data_item_index (&td, klass));
2335 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
2341 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2344 CHECK_STACK (&td, 2);
2346 SIMPLE_OP (td, MINT_LDELEM_I1);
2348 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2351 CHECK_STACK (&td, 2);
2353 SIMPLE_OP (td, MINT_LDELEM_U1);
2355 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2358 CHECK_STACK (&td, 2);
2360 SIMPLE_OP (td, MINT_LDELEM_I2);
2362 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2365 CHECK_STACK (&td, 2);
2367 SIMPLE_OP (td, MINT_LDELEM_U2);
2369 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2372 CHECK_STACK (&td, 2);
2374 SIMPLE_OP (td, MINT_LDELEM_I4);
2376 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2379 CHECK_STACK (&td, 2);
2381 SIMPLE_OP (td, MINT_LDELEM_U4);
2383 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2386 CHECK_STACK (&td, 2);
2388 SIMPLE_OP (td, MINT_LDELEM_I8);
2390 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2393 CHECK_STACK (&td, 2);
2395 SIMPLE_OP (td, MINT_LDELEM_I);
2397 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2400 CHECK_STACK (&td, 2);
2402 SIMPLE_OP (td, MINT_LDELEM_R4);
2404 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2407 CHECK_STACK (&td, 2);
2409 SIMPLE_OP (td, MINT_LDELEM_R8);
2411 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2413 case CEE_LDELEM_REF:
2414 CHECK_STACK (&td, 2);
2416 SIMPLE_OP (td, MINT_LDELEM_REF);
2418 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2421 CHECK_STACK (&td, 2);
2422 token = read32 (td.ip + 1);
2423 klass = mini_get_class (method, token, generic_context);
2424 switch (mint_type (&klass->byval_arg)) {
2427 SIMPLE_OP (td, MINT_LDELEM_I1);
2429 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2433 SIMPLE_OP (td, MINT_LDELEM_U1);
2435 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2439 SIMPLE_OP (td, MINT_LDELEM_U2);
2441 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2445 SIMPLE_OP (td, MINT_LDELEM_I2);
2447 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2451 SIMPLE_OP (td, MINT_LDELEM_I4);
2453 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2457 SIMPLE_OP (td, MINT_LDELEM_I8);
2459 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2463 SIMPLE_OP (td, MINT_LDELEM_R4);
2465 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2469 SIMPLE_OP (td, MINT_LDELEM_R8);
2471 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2475 SIMPLE_OP (td, MINT_LDELEM_REF);
2477 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2479 case MINT_TYPE_VT: {
2480 int size = mono_class_value_size (klass, NULL);
2482 SIMPLE_OP (td, MINT_LDELEM_VT);
2483 ADD_CODE (&td, get_data_item_index (&td, klass));
2484 WRITE32 (&td, &size);
2486 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2487 PUSH_VT (&td, size);
2491 GString *res = g_string_new ("");
2492 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2493 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2494 g_string_free (res, TRUE);
2502 CHECK_STACK (&td, 3);
2504 SIMPLE_OP (td, MINT_STELEM_I);
2508 CHECK_STACK (&td, 3);
2510 SIMPLE_OP (td, MINT_STELEM_I1);
2514 CHECK_STACK (&td, 3);
2516 SIMPLE_OP (td, MINT_STELEM_I2);
2520 CHECK_STACK (&td, 3);
2522 SIMPLE_OP (td, MINT_STELEM_I4);
2526 CHECK_STACK (&td, 3);
2528 SIMPLE_OP (td, MINT_STELEM_I8);
2532 CHECK_STACK (&td, 3);
2534 SIMPLE_OP (td, MINT_STELEM_R4);
2538 CHECK_STACK (&td, 3);
2540 SIMPLE_OP (td, MINT_STELEM_R8);
2543 case CEE_STELEM_REF:
2544 CHECK_STACK (&td, 3);
2546 SIMPLE_OP (td, MINT_STELEM_REF);
2550 CHECK_STACK (&td, 3);
2552 token = read32 (td.ip + 1);
2553 klass = mini_get_class (method, token, generic_context);
2554 switch (mint_type (&klass->byval_arg)) {
2556 SIMPLE_OP (td, MINT_STELEM_U1);
2559 SIMPLE_OP (td, MINT_STELEM_U2);
2562 SIMPLE_OP (td, MINT_STELEM_I4);
2565 SIMPLE_OP (td, MINT_STELEM_I8);
2568 SIMPLE_OP (td, MINT_STELEM_REF);
2570 case MINT_TYPE_VT: {
2571 int size = mono_class_value_size (klass, NULL);
2572 SIMPLE_OP (td, MINT_STELEM_VT);
2573 ADD_CODE (&td, get_data_item_index (&td, klass));
2574 WRITE32 (&td, &size);
2579 GString *res = g_string_new ("");
2580 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2581 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2582 g_string_free (res, TRUE);
2591 case CEE_CONV_OVF_U1:
2593 case CEE_CONV_OVF_I8:
2595 #if SIZEOF_VOID_P == 8
2596 case CEE_CONV_OVF_U:
2598 case CEE_REFANYVAL: ves_abort(); break;
2601 CHECK_STACK (&td, 1);
2602 SIMPLE_OP (td, MINT_CKFINITE);
2604 case CEE_CONV_OVF_I1:
2605 case CEE_CONV_OVF_I1_UN:
2606 CHECK_STACK (&td, 1);
2607 switch (td.sp [-1].type) {
2609 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2612 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2615 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2618 g_assert_not_reached ();
2621 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2623 case CEE_CONV_OVF_U1:
2624 case CEE_CONV_OVF_U1_UN:
2625 CHECK_STACK (&td, 1);
2626 switch (td.sp [-1].type) {
2628 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2631 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2634 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2637 g_assert_not_reached ();
2640 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2642 case CEE_CONV_OVF_I2:
2643 case CEE_CONV_OVF_I2_UN:
2644 CHECK_STACK (&td, 1);
2645 switch (td.sp [-1].type) {
2647 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2650 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2653 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2656 g_assert_not_reached ();
2659 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2661 case CEE_CONV_OVF_U2_UN:
2662 case CEE_CONV_OVF_U2:
2663 CHECK_STACK (&td, 1);
2664 switch (td.sp [-1].type) {
2666 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2669 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2672 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2675 g_assert_not_reached ();
2678 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2680 #if SIZEOF_VOID_P == 4
2681 case CEE_CONV_OVF_I:
2683 case CEE_CONV_OVF_I4:
2684 case CEE_CONV_OVF_I4_UN:
2685 CHECK_STACK (&td, 1);
2686 switch (td.sp [-1].type) {
2688 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2691 if (*td.ip == CEE_CONV_OVF_I4_UN)
2692 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2695 if (*td.ip == CEE_CONV_OVF_I4_UN)
2696 ADD_CODE (&td, MINT_CONV_OVF_I4_U8);
2698 ADD_CODE (&td, MINT_CONV_OVF_I4_I8);
2701 g_assert_not_reached ();
2704 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2706 #if SIZEOF_VOID_P == 4
2707 case CEE_CONV_OVF_U:
2709 case CEE_CONV_OVF_U4:
2710 case CEE_CONV_OVF_U4_UN:
2711 CHECK_STACK (&td, 1);
2712 switch (td.sp [-1].type) {
2714 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2717 if (*td.ip != CEE_CONV_OVF_U4_UN)
2718 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2721 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2724 g_assert_not_reached ();
2727 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2729 #if SIZEOF_VOID_P == 8
2730 case CEE_CONV_OVF_I:
2732 case CEE_CONV_OVF_I8:
2733 CHECK_STACK (&td, 1);
2734 switch (td.sp [-1].type) {
2736 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2739 ADD_CODE(&td, MINT_CONV_I8_I4);
2744 g_assert_not_reached ();
2747 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2749 #if SIZEOF_VOID_P == 8
2750 case CEE_CONV_OVF_U:
2752 case CEE_CONV_OVF_U8:
2753 CHECK_STACK (&td, 1);
2754 switch (td.sp [-1].type) {
2756 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2759 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2762 ADD_CODE (&td, MINT_CONV_OVF_U8_I8);
2765 g_assert_not_reached ();
2768 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2773 token = read32 (td.ip + 1);
2774 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2775 handle = mono_method_get_wrapper_data (method, token);
2776 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
2777 if (klass == mono_defaults.typehandle_class)
2778 handle = &((MonoClass *) handle)->byval_arg;
2780 if (generic_context) {
2781 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
2782 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2785 handle = mono_ldtoken (image, token, &klass, generic_context);
2787 mono_class_init (klass);
2788 mt = mint_type (&klass->byval_arg);
2789 g_assert (mt == MINT_TYPE_VT);
2790 size = mono_class_value_size (klass, NULL);
2791 g_assert (size == sizeof(gpointer));
2792 PUSH_VT (&td, sizeof(gpointer));
2793 ADD_CODE (&td, MINT_LDTOKEN);
2794 ADD_CODE (&td, get_data_item_index (&td, handle));
2796 SET_TYPE (td.sp, stack_type [mt], klass);
2802 binary_arith_op(&td, MINT_ADD_OVF_I4);
2805 case CEE_ADD_OVF_UN:
2806 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2810 binary_arith_op(&td, MINT_MUL_OVF_I4);
2813 case CEE_MUL_OVF_UN:
2814 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2818 binary_arith_op(&td, MINT_SUB_OVF_I4);
2821 case CEE_SUB_OVF_UN:
2822 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2825 case CEE_ENDFINALLY:
2827 SIMPLE_OP (td, MINT_ENDFINALLY);
2828 generating_code = 0;
2832 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2834 generating_code = 0;
2838 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2840 generating_code = 0;
2845 case CEE_MONO_CALLI_EXTRA_ARG:
2846 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
2847 ADD_CODE (&td, MINT_POP);
2850 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2852 case CEE_MONO_JIT_ICALL_ADDR: {
2855 MonoJitICallInfo *info;
2857 token = read32 (td.ip + 1);
2859 func = mono_method_get_wrapper_data (method, token);
2860 info = mono_find_jit_icall_by_addr (func);
2862 ADD_CODE (&td, MINT_LDFTN);
2863 ADD_CODE (&td, get_data_item_index (&td, func));
2864 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
2867 case CEE_MONO_ICALL: {
2870 MonoJitICallInfo *info;
2872 token = read32 (td.ip + 1);
2874 func = mono_method_get_wrapper_data (method, token);
2875 info = mono_find_jit_icall_by_addr (func);
2878 CHECK_STACK (&td, info->sig->param_count);
2879 switch (info->sig->param_count) {
2881 if (MONO_TYPE_IS_VOID (info->sig->ret))
2882 ADD_CODE (&td,MINT_ICALL_V_V);
2884 ADD_CODE (&td, MINT_ICALL_V_P);
2887 if (MONO_TYPE_IS_VOID (info->sig->ret))
2888 ADD_CODE (&td,MINT_ICALL_P_V);
2890 ADD_CODE (&td,MINT_ICALL_P_P);
2893 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2894 if (info->sig->params [1]->type == MONO_TYPE_I4)
2895 ADD_CODE (&td,MINT_ICALL_PI_V);
2897 ADD_CODE (&td,MINT_ICALL_PP_V);
2899 if (info->sig->params [1]->type == MONO_TYPE_I4)
2900 ADD_CODE (&td,MINT_ICALL_PI_P);
2902 ADD_CODE (&td,MINT_ICALL_PP_P);
2906 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2907 if (info->sig->params [2]->type == MONO_TYPE_I4)
2908 ADD_CODE (&td,MINT_ICALL_PPI_V);
2910 ADD_CODE (&td,MINT_ICALL_PPP_V);
2913 g_assert_not_reached ();
2916 if (func == mono_ftnptr_to_delegate) {
2917 g_error ("TODO: ?");
2919 ADD_CODE(&td, get_data_item_index (&td, func));
2920 td.sp -= info->sig->param_count;
2922 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2924 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2928 case CEE_MONO_VTADDR: {
2930 CHECK_STACK (&td, 1);
2931 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2932 size = mono_class_native_size(td.sp [-1].klass, NULL);
2934 size = mono_class_value_size(td.sp [-1].klass, NULL);
2935 size = (size + 7) & ~7;
2936 ADD_CODE(&td, MINT_VTRESULT);
2938 WRITE32(&td, &size);
2941 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2944 case CEE_MONO_LDPTR:
2945 case CEE_MONO_CLASSCONST:
2946 token = read32 (td.ip + 1);
2948 ADD_CODE(&td, MINT_MONO_LDPTR);
2949 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2950 td.sp [0].type = STACK_TYPE_I;
2953 case CEE_MONO_OBJADDR:
2954 CHECK_STACK (&td, 1);
2956 td.sp[-1].type = STACK_TYPE_MP;
2959 case CEE_MONO_NEWOBJ:
2960 token = read32 (td.ip + 1);
2962 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2963 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2964 td.sp [0].type = STACK_TYPE_O;
2967 case CEE_MONO_RETOBJ:
2968 CHECK_STACK (&td, 1);
2969 token = read32 (td.ip + 1);
2971 ADD_CODE(&td, MINT_MONO_RETOBJ);
2974 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2976 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2978 if (td.sp > td.stack)
2979 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2981 case CEE_MONO_LDNATIVEOBJ:
2982 token = read32 (td.ip + 1);
2984 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2985 g_assert(klass->valuetype);
2986 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2988 case CEE_MONO_TLS: {
2989 gint32 key = read32 (td.ip + 1);
2991 g_assert (key < TLS_KEY_NUM);
2992 ADD_CODE (&td, MINT_MONO_TLS);
2993 WRITE32 (&td, &key);
2994 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_MP);
2997 case CEE_MONO_ATOMIC_STORE_I4:
2998 CHECK_STACK (&td, 2);
2999 SIMPLE_OP (td, MINT_MONO_ATOMIC_STORE_I4);
3003 case CEE_MONO_SAVE_LMF:
3004 case CEE_MONO_RESTORE_LMF:
3005 case CEE_MONO_NOT_TAKEN:
3008 case CEE_MONO_LDPTR_INT_REQ_FLAG:
3009 ADD_CODE (&td, MINT_MONO_LDPTR);
3010 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
3011 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
3014 case CEE_MONO_JIT_ATTACH:
3015 ADD_CODE (&td, MINT_MONO_JIT_ATTACH);
3018 case CEE_MONO_JIT_DETACH:
3019 ADD_CODE (&td, MINT_MONO_JIT_DETACH);
3023 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
3033 case CEE_PREFIXREF: ves_abort(); break;
3036 * Note: Exceptions thrown when executing a prefixed opcode need
3037 * to take into account the number of prefix bytes (usually the
3038 * throw point is just (ip - n_prefix_bytes).
3044 case CEE_ARGLIST: ves_abort(); break;
3047 CHECK_STACK(&td, 2);
3048 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3049 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3051 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
3053 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3057 CHECK_STACK(&td, 2);
3058 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3059 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3061 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3063 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3067 CHECK_STACK(&td, 2);
3068 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3069 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3071 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3073 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3077 CHECK_STACK(&td, 2);
3078 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3079 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3081 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3083 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3087 CHECK_STACK(&td, 2);
3088 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3089 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3091 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3093 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3096 case CEE_LDVIRTFTN: /* fallthrough */
3099 if (*td.ip == CEE_LDVIRTFTN) {
3100 CHECK_STACK (&td, 1);
3103 token = read32 (td.ip + 1);
3104 if (method->wrapper_type != MONO_WRAPPER_NONE)
3105 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3107 m = mono_get_method_full (image, token, NULL, generic_context);
3109 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3110 m = mono_marshal_get_synchronized_wrapper (m);
3112 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3113 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
3114 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3116 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
3120 load_arg (&td, read16 (td.ip + 1));
3124 int n = read16 (td.ip + 1);
3125 ADD_CODE (&td, MINT_LDARGA);
3126 ADD_CODE (&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
3127 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3132 store_arg (&td, read16 (td.ip + 1));
3136 load_local (&td, read16 (td.ip + 1));
3140 ADD_CODE(&td, MINT_LDLOCA_S);
3141 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
3142 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3146 store_local (&td, read16 (td.ip + 1));
3150 CHECK_STACK (&td, 1);
3151 #if SIZEOF_VOID_P == 8
3152 if (td.sp [-1].type == STACK_TYPE_I8)
3153 ADD_CODE(&td, MINT_CONV_I4_I8);
3155 ADD_CODE(&td, MINT_LOCALLOC);
3156 if (td.sp != td.stack + 1)
3157 g_warning("CEE_LOCALLOC: stack not empty");
3159 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3162 case CEE_UNUSED57: ves_abort(); break;
3165 ADD_CODE (&td, MINT_ENDFILTER);
3168 case CEE_UNALIGNED_:
3170 /* FIX: should do something? */;
3174 /* FIX: should do something? */;
3178 /* FIX: should do something? */;
3181 CHECK_STACK(&td, 1);
3182 token = read32 (td.ip + 1);
3183 klass = mini_get_class (method, token, generic_context);
3184 if (klass->valuetype) {
3185 ADD_CODE (&td, MINT_INITOBJ);
3186 i32 = mono_class_value_size (klass, NULL);
3187 WRITE32 (&td, &i32);
3189 ADD_CODE (&td, MINT_LDNULL);
3190 ADD_CODE (&td, MINT_STIND_REF);
3196 CHECK_STACK(&td, 3);
3197 /* FIX? convert length to I8? */
3198 ADD_CODE(&td, MINT_CPBLK);
3206 case CEE_CONSTRAINED_:
3207 token = read32 (td.ip + 1);
3208 constrained_class = mini_get_class (method, token, generic_context);
3209 mono_class_init (constrained_class);
3213 CHECK_STACK(&td, 3);
3214 ADD_CODE(&td, MINT_INITBLK);
3220 /* FIXME: implement */
3225 SIMPLE_OP (td, MINT_RETHROW);
3226 generating_code = 0;
3230 token = read32 (td.ip + 1);
3232 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
3234 MonoType *type = mono_type_create_from_typespec (image, token);
3235 size = mono_type_size (type, &align);
3238 MonoClass *szclass = mini_get_class (method, token, generic_context);
3239 mono_class_init (szclass);
3241 if (!szclass->valuetype)
3242 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3244 size = mono_type_size (&szclass->byval_arg, &align);
3246 ADD_CODE(&td, MINT_LDC_I4);
3247 WRITE32(&td, &size);
3248 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
3252 case CEE_REFANYTYPE: ves_abort(); break;
3255 g_error ("transform.c: Unimplemented opcode: 0xFE %02x (%s) at 0x%x\n", *td.ip, mono_opcode_name (256 + *td.ip), td.ip-header->code);
3259 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3262 if (td.new_ip - td.new_code != new_in_start_offset)
3263 td.last_new_ip = td.new_code + new_in_start_offset;
3264 else if (td.is_bb_start [td.in_start - td.il_code])
3265 td.is_bb_start [td.ip - td.il_code] = 1;
3267 td.last_ip = td.in_start;
3270 if (mono_interp_traceopt) {
3271 const guint16 *p = td.new_code;
3272 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
3273 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3274 while (p < td.new_ip) {
3275 p = mono_interp_dis_mintop(td.new_code, p);
3279 g_assert (td.max_stack_height <= (header->max_stack + 1));
3281 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
3282 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3283 rtm->code = mono_domain_alloc0 (domain, (td.new_ip - td.new_code) * sizeof (gushort));
3284 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3285 g_free (td.new_code);
3286 rtm->new_body_start = rtm->code + body_start_offset;
3287 rtm->num_clauses = header->num_clauses;
3288 for (i = 0; i < header->num_clauses; i++) {
3289 MonoExceptionClause *c = rtm->clauses + i;
3290 int end_off = c->try_offset + c->try_len;
3291 c->try_offset = td.in_offsets [c->try_offset];
3292 c->try_len = td.in_offsets [end_off] - c->try_offset;
3293 end_off = c->handler_offset + c->handler_len;
3294 c->handler_offset = td.in_offsets [c->handler_offset];
3295 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3296 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
3297 c->data.filter_offset = td.in_offsets [c->data.filter_offset];
3299 rtm->vt_stack_size = td.max_vt_sp;
3300 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3301 rtm->data_items = mono_domain_alloc0 (domain, td.n_data_items * sizeof (td.data_items [0]));
3302 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3303 g_free (td.in_offsets);
3304 g_free (td.forward_refs);
3305 for (i = 0; i < header->code_size; ++i)
3306 g_free (td.stack_state [i]);
3307 g_free (td.stack_state);
3308 g_free (td.stack_height);
3309 g_free (td.vt_stack_size);
3310 g_free (td.data_items);
3312 g_hash_table_destroy (td.data_hash);
3315 static mono_mutex_t calc_section;
3318 mono_interp_transform_init (void)
3320 mono_os_mutex_init_recursive(&calc_section);
3324 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3326 int i, align, size, offset;
3327 MonoMethod *method = runtime_method->method;
3328 MonoImage *image = method->klass->image;
3329 MonoMethodHeader *header = mono_method_get_header (method);
3330 MonoMethodSignature *signature = mono_method_signature (method);
3331 register const unsigned char *ip, *end;
3332 const MonoOpcode *opcode;
3335 MonoDomain *domain = mono_domain_get ();
3336 unsigned char *is_bb_start;
3338 MonoVTable *method_class_vt;
3340 MonoGenericContext *generic_context = NULL;
3342 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3343 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3344 if (!method_class_vt->initialized) {
3347 MonoInvocation *last_env_frame = context->env_frame;
3348 jmp_buf *old_env = context->current_env;
3349 error_init (&error);
3352 MonoException *failed = context->env_frame->ex;
3353 context->env_frame->ex = NULL;
3354 context->env_frame = last_env_frame;
3355 context->current_env = old_env;
3358 context->env_frame = context->current_frame;
3359 context->current_env = &env;
3360 mono_runtime_class_init_full (method_class_vt, &error);
3361 if (!mono_error_ok (&error)) {
3362 return mono_error_convert_to_exception (&error);
3364 context->env_frame = last_env_frame;
3365 context->current_env = old_env;
3368 mono_profiler_method_jit (method); /* sort of... */
3370 if (mono_method_signature (method)->is_inflated)
3371 generic_context = mono_method_get_context (method);
3373 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
3374 if (generic_container)
3375 generic_context = &generic_container->context;
3378 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3379 MonoMethod *nm = NULL;
3380 mono_os_mutex_lock(&calc_section);
3381 if (runtime_method->transformed) {
3382 mono_os_mutex_unlock(&calc_section);
3383 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3387 /* assumes all internal calls with an array this are built in... */
3388 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3389 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3390 signature = mono_method_signature (nm);
3392 const char *name = method->name;
3393 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3394 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
3395 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
3397 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
3398 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
3399 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3400 nm = mono_marshal_get_delegate_invoke (method, NULL);
3401 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3402 nm = mono_marshal_get_delegate_begin_invoke (method);
3403 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3404 nm = mono_marshal_get_delegate_end_invoke (method);
3408 runtime_method->code = g_malloc(sizeof(short));
3409 runtime_method->code[0] = MINT_CALLRUN;
3413 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3414 runtime_method->alloca_size = runtime_method->stack_size;
3415 runtime_method->transformed = TRUE;
3416 mono_os_mutex_unlock(&calc_section);
3417 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3421 header = mono_method_get_header (nm);
3422 mono_os_mutex_unlock(&calc_section);
3423 } else if (method->klass == mono_defaults.array_class) {
3424 if (!strcmp (method->name, "UnsafeMov")) {
3425 mono_os_mutex_lock (&calc_section);
3426 if (!runtime_method->transformed) {
3427 runtime_method->code = g_malloc (sizeof (short));
3428 runtime_method->code[0] = MINT_CALLRUN;
3429 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3430 runtime_method->alloca_size = runtime_method->stack_size;
3431 runtime_method->transformed = TRUE;
3433 mono_os_mutex_unlock(&calc_section);
3434 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3436 } else if (!strcmp (method->name, "UnsafeStore)")) {
3438 } else if (!strcmp (method->name, "UnsafeLoad)")) {
3442 g_assert ((signature->param_count + signature->hasthis) < 1000);
3443 g_assert (header->max_stack < 10000);
3444 /* intern the strings in the method. */
3446 end = ip + header->code_size;
3448 is_bb_start = g_malloc0(header->code_size);
3449 is_bb_start [0] = 1;
3456 else if (in == 0xf0) {
3458 in = *ip + MONO_CEE_MONO_ICALL;
3460 opcode = &mono_opcodes [in];
3461 switch (opcode->argument) {
3462 case MonoInlineNone:
3465 case MonoInlineString:
3466 if (method->wrapper_type == MONO_WRAPPER_NONE)
3467 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3470 case MonoInlineType:
3471 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3472 class = mini_get_class (method, read32 (ip + 1), generic_context);
3473 mono_class_init (class);
3474 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3476 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3477 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3478 mono_class_vtable (domain, class);
3483 case MonoInlineMethod:
3484 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3485 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3487 g_free (is_bb_start);
3488 g_error ("FIXME: where to get method and class string?");
3490 // return mono_get_exception_missing_method ();
3492 mono_class_init (m->klass);
3493 if (!mono_class_is_interface (m->klass))
3494 mono_class_vtable (domain, m->klass);
3498 case MonoInlineField:
3502 case MonoShortInlineR:
3505 case MonoInlineBrTarget:
3506 offset = read32 (ip + 1);
3508 backwards = offset < 0;
3509 offset += ip - header->code;
3510 g_assert (offset >= 0 && offset < header->code_size);
3511 is_bb_start [offset] |= backwards ? 2 : 1;
3513 case MonoShortInlineBrTarget:
3514 offset = ((gint8 *)ip) [1];
3516 backwards = offset < 0;
3517 offset += ip - header->code;
3518 g_assert (offset >= 0 && offset < header->code_size);
3519 is_bb_start [offset] |= backwards ? 2 : 1;
3524 case MonoShortInlineVar:
3525 case MonoShortInlineI:
3528 case MonoInlineSwitch: {
3530 const unsigned char *next_ip;
3534 next_ip = ip + 4 * n;
3535 for (i = 0; i < n; i++) {
3536 offset = read32 (ip);
3537 backwards = offset < 0;
3538 offset += next_ip - header->code;
3539 g_assert (offset >= 0 && offset < header->code_size);
3540 is_bb_start [offset] |= backwards ? 2 : 1;
3550 g_assert_not_reached ();
3553 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3555 /* the rest needs to be locked so it is only done once */
3556 mono_os_mutex_lock(&calc_section);
3557 if (runtime_method->transformed) {
3558 mono_os_mutex_unlock(&calc_section);
3559 g_free (is_bb_start);
3560 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3564 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3565 runtime_method->stack_size = (sizeof (stackval)) * (header->max_stack + 2); /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3566 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3568 for (i = 0; i < header->num_locals; ++i) {
3569 size = mono_type_size (header->locals [i], &align);
3570 offset += align - 1;
3571 offset &= ~(align - 1);
3572 runtime_method->local_offsets [i] = offset;
3575 offset = (offset + 7) & ~7;
3576 runtime_method->locals_size = offset;
3577 g_assert (runtime_method->locals_size < 65536);
3579 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3581 if (signature->hasthis) {
3582 g_assert (!signature->pinvoke);
3583 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3584 offset += align - 1;
3585 offset &= ~(align - 1);
3586 runtime_method->arg_offsets [0] = offset;
3590 for (i = 0; i < signature->param_count; ++i) {
3591 if (signature->pinvoke) {
3593 size = mono_type_native_stack_size (signature->params [i], &dummy);
3597 size = mono_type_stack_size (signature->params [i], &align);
3598 offset += align - 1;
3599 offset &= ~(align - 1);
3600 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3603 offset = (offset + 7) & ~7;
3604 runtime_method->args_size = offset;
3605 g_assert (runtime_method->args_size < 10000);
3607 generate (method, runtime_method, is_bb_start, generic_context);
3609 g_free (is_bb_start);
3611 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3612 runtime_method->transformed = TRUE;
3613 mono_os_mutex_unlock(&calc_section);