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);
814 for (i = csignature->param_count - 1; i >= 0; --i)
815 store_arg (td, i + csignature->hasthis);
817 ADD_CODE(td, MINT_BR_S);
818 offset = body_start_offset - ((td->new_ip - 1) - td->new_code);
819 ADD_CODE(td, offset);
820 if (!is_bb_start [td->ip + 5 - td->il_code])
821 ++td->ip; /* gobble the CEE_RET if it isn't branched to */
825 /* mheader might not exist if this is a delegate invoc, etc */
826 if (mheader && *mheader->code == CEE_RET && called_inited) {
827 if (mono_interp_traceopt)
828 g_print ("Inline (empty) call of %s.%s\n", target_method->klass->name, target_method->name);
829 for (i = 0; i < csignature->param_count; i++)
830 ADD_CODE (td, MINT_POP); /*FIX: vt */
832 if (csignature->hasthis) {
834 ADD_CODE(td, MINT_CKNULL);
835 ADD_CODE (td, MINT_POP);
838 td->sp -= csignature->param_count + csignature->hasthis;
844 if (method->wrapper_type == MONO_WRAPPER_NONE && target_method != NULL) {
845 if (target_method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
846 target_method = mono_marshal_get_native_wrapper (target_method, FALSE, FALSE);
847 if (!virtual && target_method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
848 target_method = mono_marshal_get_synchronized_wrapper (target_method);
850 g_assert (csignature->call_convention == MONO_CALL_DEFAULT || csignature->call_convention == MONO_CALL_C);
851 td->sp -= csignature->param_count + !!csignature->hasthis;
852 for (i = 0; i < csignature->param_count; ++i) {
853 if (td->sp [i + !!csignature->hasthis].type == STACK_TYPE_VT) {
855 MonoClass *klass = mono_class_from_mono_type (csignature->params [i]);
856 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
857 size = mono_class_native_size (klass, NULL);
859 size = mono_class_value_size (klass, NULL);
860 size = (size + 7) & ~7;
861 vt_stack_used += size;
865 /* need to handle typedbyref ... */
866 if (csignature->ret->type != MONO_TYPE_VOID) {
867 int mt = mint_type(csignature->ret);
868 MonoClass *klass = mono_class_from_mono_type (csignature->ret);
869 if (mt == MINT_TYPE_VT) {
870 if (csignature->pinvoke && method->wrapper_type != MONO_WRAPPER_NONE)
871 vt_res_size = mono_class_native_size (klass, NULL);
873 vt_res_size = mono_class_value_size (klass, NULL);
874 PUSH_VT(td, vt_res_size);
876 PUSH_TYPE(td, stack_type[mt], klass);
882 #if SIZEOF_VOID_P == 8
883 if (op == MINT_LDLEN)
884 ADD_CODE (td, MINT_CONV_I4_I8);
886 if (op == MINT_LDELEMA || op == MINT_LDELEMA_TC) {
887 ADD_CODE (td, get_data_item_index (td, target_method->klass));
888 ADD_CODE (td, 1 + target_method->klass->rank);
890 } else if (!calli && !virtual && jit_call_supported (target_method, csignature)) {
891 ADD_CODE(td, MINT_JIT_CALL);
892 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
893 mono_error_assert_ok (&error);
896 ADD_CODE(td, native ? MINT_CALLI_NAT : MINT_CALLI);
898 ADD_CODE(td, is_void ? MINT_VCALLVIRT : MINT_CALLVIRT);
900 ADD_CODE(td, is_void ? MINT_VCALL : MINT_CALL);
903 ADD_CODE(td, get_data_item_index (td, (void *)csignature));
905 ADD_CODE(td, get_data_item_index (td, (void *)mono_interp_get_runtime_method (domain, target_method, &error)));
906 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
910 if (vt_stack_used != 0 || vt_res_size != 0) {
911 ADD_CODE(td, MINT_VTRESULT);
912 ADD_CODE(td, vt_res_size);
913 WRITE32(td, &vt_stack_used);
914 td->vt_sp -= vt_stack_used;
919 generate (MonoMethod *method, RuntimeMethod *rtm, unsigned char *is_bb_start, MonoGenericContext *generic_context)
921 MonoMethodHeader *header = mono_method_get_header (method);
922 MonoMethodSignature *signature = mono_method_signature (method);
923 MonoImage *image = method->klass->image;
924 MonoDomain *domain = mono_domain_get ();
925 MonoClass *constrained_class = NULL;
927 int offset, mt, i, i32;
928 gboolean readonly = FALSE;
930 MonoClassField *field;
931 const unsigned char *end;
932 int new_in_start_offset;
933 int body_start_offset;
937 int generating_code = 1;
939 memset(&td, 0, sizeof(td));
942 td.is_bb_start = is_bb_start;
943 td.il_code = header->code;
944 td.code_size = header->code_size;
946 td.max_code_size = td.code_size;
947 td.new_code = (unsigned short *)g_malloc(td.max_code_size * sizeof(gushort));
948 td.new_code_end = td.new_code + td.max_code_size;
949 td.in_offsets = g_malloc0(header->code_size * sizeof(int));
950 td.forward_refs = g_malloc(header->code_size * sizeof(int));
951 td.stack_state = g_malloc0(header->code_size * sizeof(StackInfo *));
952 td.stack_height = g_malloc(header->code_size * sizeof(int));
953 td.vt_stack_size = g_malloc(header->code_size * sizeof(int));
955 td.max_data_items = 0;
956 td.data_items = NULL;
957 td.data_hash = g_hash_table_new (NULL, NULL);
958 rtm->data_items = td.data_items;
959 for (i = 0; i < header->code_size; i++) {
960 td.forward_refs [i] = -1;
961 td.stack_height [i] = -1;
963 td.new_ip = td.new_code;
964 td.last_new_ip = NULL;
966 td.stack = g_malloc0 ((header->max_stack + 1) * sizeof (td.stack [0]));
968 td.max_stack_height = 0;
970 for (i = 0; i < header->num_clauses; i++) {
971 MonoExceptionClause *c = header->clauses + i;
972 td.stack_height [c->handler_offset] = 0;
973 td.vt_stack_size [c->handler_offset] = 0;
974 td.is_bb_start [c->handler_offset] = 1;
976 td.stack_height [c->handler_offset] = 1;
977 td.stack_state [c->handler_offset] = g_malloc0(sizeof(StackInfo));
978 td.stack_state [c->handler_offset][0].type = STACK_TYPE_O;
979 td.stack_state [c->handler_offset][0].klass = NULL; /*FIX*/
981 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER) {
982 td.stack_height [c->data.filter_offset] = 0;
983 td.vt_stack_size [c->data.filter_offset] = 0;
984 td.is_bb_start [c->data.filter_offset] = 1;
986 td.stack_height [c->data.filter_offset] = 1;
987 td.stack_state [c->data.filter_offset] = g_malloc0(sizeof(StackInfo));
988 td.stack_state [c->data.filter_offset][0].type = STACK_TYPE_O;
989 td.stack_state [c->data.filter_offset][0].klass = NULL; /*FIX*/
993 td.ip = header->code;
994 end = td.ip + header->code_size;
996 if (mono_interp_traceopt) {
997 char *tmp = mono_disasm_code (NULL, method, td.ip, end);
998 char *name = mono_method_full_name (method, TRUE);
999 g_print ("Method %s, original code:\n", name);
1000 g_print ("%s\n", tmp);
1005 if (signature->hasthis)
1006 store_inarg (&td, 0);
1007 for (i = 0; i < signature->param_count; i++)
1008 store_inarg (&td, i + !!signature->hasthis);
1010 body_start_offset = td.new_ip - td.new_code;
1012 for (i = 0; i < header->num_locals; i++) {
1013 int mt = mint_type(header->locals [i]);
1014 if (mt == MINT_TYPE_VT || mt == MINT_TYPE_O || mt == MINT_TYPE_P) {
1015 ADD_CODE(&td, MINT_INITLOCALS);
1020 while (td.ip < end) {
1023 g_assert (td.sp >= td.stack);
1024 g_assert (td.vt_sp < 0x10000000);
1025 in_offset = td.ip - header->code;
1026 td.in_offsets [in_offset] = td.new_ip - td.new_code;
1027 new_in_start_offset = td.new_ip - td.new_code;
1028 td.in_start = td.ip;
1029 while (td.forward_refs [in_offset] >= 0) {
1030 int j = td.forward_refs [in_offset];
1032 td.forward_refs [in_offset] = td.forward_refs [j];
1033 if (td.in_offsets [j] < 0) {
1034 int old_switch_offset = -td.in_offsets [j];
1035 int new_switch_offset = td.in_offsets [old_switch_offset];
1036 int switch_case = (j - old_switch_offset - 5) / 4;
1037 int n_cases = read32 (header->code + old_switch_offset + 1);
1038 offset = (td.new_ip - td.new_code) - (new_switch_offset + 2 * n_cases + 3);
1039 slot = new_switch_offset + 3 + 2 * switch_case;
1040 td.new_code [slot] = * (unsigned short *)(&offset);
1041 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1043 int op = td.new_code [td.in_offsets [j]];
1044 if (mono_interp_opargtype [op] == MintOpShortBranch) {
1045 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1046 g_assert (offset <= 32767);
1047 slot = td.in_offsets [j] + 1;
1048 td.new_code [slot] = offset;
1050 offset = (td.new_ip - td.new_code) - td.in_offsets [j];
1051 slot = td.in_offsets [j] + 1;
1052 td.new_code [slot] = * (unsigned short *)(&offset);
1053 td.new_code [slot + 1] = * ((unsigned short *)&offset + 1);
1057 if (td.stack_height [in_offset] >= 0) {
1058 g_assert (is_bb_start [in_offset]);
1059 if (td.stack_height [in_offset] > 0)
1060 memcpy (td.stack, td.stack_state [in_offset], td.stack_height [in_offset] * sizeof(td.stack [0]));
1061 td.sp = td.stack + td.stack_height [in_offset];
1062 td.vt_sp = td.vt_stack_size [in_offset];
1064 if (is_bb_start [in_offset]) {
1065 generating_code = 1;
1067 if (!generating_code) {
1068 while (td.ip < end && !is_bb_start [td.ip - td.il_code])
1072 if (mono_interp_traceopt > 1) {
1073 printf("IL_%04lx %s %-10s -> IL_%04lx, sp %ld, %s %-12s vt_sp %u (max %u)\n",
1075 td.is_bb_start [td.ip - td.il_code] == 3 ? "<>" :
1076 td.is_bb_start [td.ip - td.il_code] == 2 ? "< " :
1077 td.is_bb_start [td.ip - td.il_code] == 1 ? " >" : " ",
1078 mono_opcode_name (*td.ip), td.new_ip - td.new_code, td.sp - td.stack,
1079 td.sp > td.stack ? stack_type_string [td.sp [-1].type] : " ",
1080 (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) : "",
1081 td.vt_sp, td.max_vt_sp);
1089 SIMPLE_OP(td, MINT_BREAK);
1095 load_arg (&td, *td.ip - CEE_LDARG_0);
1102 load_local (&td, *td.ip - CEE_LDLOC_0);
1109 store_local (&td, *td.ip - CEE_STLOC_0);
1113 load_arg (&td, ((guint8 *)td.ip)[1]);
1116 case CEE_LDARGA_S: {
1117 /* NOTE: n includes this */
1118 int n = ((guint8 *) td.ip) [1];
1119 ADD_CODE (&td, MINT_LDARGA);
1120 ADD_CODE (&td, td.rtm->arg_offsets [n]);
1121 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1126 store_arg (&td, ((guint8 *)td.ip)[1]);
1130 load_local (&td, ((guint8 *)td.ip)[1]);
1134 ADD_CODE(&td, MINT_LDLOCA_S);
1135 ADD_CODE(&td, td.rtm->local_offsets [((guint8 *)td.ip)[1]]);
1136 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
1140 store_local (&td, ((guint8 *)td.ip)[1]);
1144 SIMPLE_OP(td, MINT_LDNULL);
1145 PUSH_TYPE(&td, STACK_TYPE_O, NULL);
1148 SIMPLE_OP(td, MINT_LDC_I4_M1);
1149 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1152 if (!td.is_bb_start[td.ip + 1 - td.il_code] && td.ip [1] == 0xfe && td.ip [2] == CEE_CEQ &&
1153 td.sp > td.stack && td.sp [-1].type == STACK_TYPE_I4) {
1154 SIMPLE_OP(td, MINT_CEQ0_I4);
1157 SIMPLE_OP(td, MINT_LDC_I4_0);
1158 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1162 if (!td.is_bb_start[td.ip + 1 - td.il_code] &&
1163 (td.ip [1] == CEE_ADD || td.ip [1] == CEE_SUB) && td.sp [-1].type == STACK_TYPE_I4) {
1164 ADD_CODE(&td, td.ip [1] == CEE_ADD ? MINT_ADD1_I4 : MINT_SUB1_I4);
1167 SIMPLE_OP(td, MINT_LDC_I4_1);
1168 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1178 SIMPLE_OP(td, (*td.ip - CEE_LDC_I4_0) + MINT_LDC_I4_0);
1179 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1182 ADD_CODE(&td, MINT_LDC_I4_S);
1183 ADD_CODE(&td, ((gint8 *) td.ip) [1]);
1185 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1188 i32 = read32 (td.ip + 1);
1189 ADD_CODE(&td, MINT_LDC_I4);
1192 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
1195 gint64 val = read64 (td.ip + 1);
1196 ADD_CODE(&td, MINT_LDC_I8);
1199 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I8);
1204 readr4 (td.ip + 1, &val);
1205 ADD_CODE(&td, MINT_LDC_R4);
1208 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1213 readr8 (td.ip + 1, &val);
1214 ADD_CODE(&td, MINT_LDC_R8);
1217 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_R8);
1221 int type = td.sp [-1].type;
1222 MonoClass *klass = td.sp [-1].klass;
1223 if (td.sp [-1].type == STACK_TYPE_VT) {
1224 gint32 size = mono_class_value_size (klass, NULL);
1226 ADD_CODE(&td, MINT_DUP_VT);
1227 WRITE32(&td, &size);
1230 SIMPLE_OP(td, MINT_DUP);
1231 PUSH_TYPE(&td, type, klass);
1235 CHECK_STACK(&td, 1);
1236 SIMPLE_OP(td, MINT_POP);
1238 if (td.sp [-1].type == STACK_TYPE_VT) {
1239 int size = mono_class_value_size (td.sp [-1].klass, NULL);
1240 size = (size + 7) & ~7;
1241 ADD_CODE(&td, MINT_VTRESULT);
1243 WRITE32(&td, &size);
1250 if (td.sp > td.stack)
1251 g_warning ("CEE_JMP: stack must be empty");
1252 token = read32 (td.ip + 1);
1253 m = mono_get_method_full (image, token, NULL, generic_context);
1254 ADD_CODE (&td, MINT_JMP);
1255 ADD_CODE (&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1256 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1260 case CEE_CALLVIRT: /* Fall through */
1261 case CEE_CALLI: /* Fall through */
1263 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, constrained_class, readonly);
1264 constrained_class = NULL;
1270 if (signature->ret->type != MONO_TYPE_VOID) {
1272 MonoClass *klass = mono_class_from_mono_type (signature->ret);
1273 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1274 vt_size = mono_class_value_size (klass, NULL);
1275 vt_size = (vt_size + 7) & ~7;
1278 if (td.sp > td.stack)
1279 g_warning ("%s.%s: CEE_RET: more values on stack: %d", td.method->klass->name, td.method->name, td.sp - td.stack);
1280 if (td.vt_sp != vt_size)
1281 g_error ("%s.%s: CEE_RET: value type stack: %d vs. %d", td.method->klass->name, td.method->name, td.vt_sp, vt_size);
1283 SIMPLE_OP(td, signature->ret->type == MONO_TYPE_VOID ? MINT_RET_VOID : MINT_RET);
1285 ADD_CODE(&td, MINT_RET_VT);
1286 WRITE32(&td, &vt_size);
1289 generating_code = 0;
1293 handle_branch (&td, MINT_BR_S, MINT_BR, 5 + read32 (td.ip + 1));
1295 generating_code = 0;
1298 handle_branch (&td, MINT_BR_S, MINT_BR, 2 + (gint8)td.ip [1]);
1300 generating_code = 0;
1303 one_arg_branch (&td, MINT_BRFALSE_I4, 5 + read32 (td.ip + 1));
1307 one_arg_branch (&td, MINT_BRFALSE_I4, 2 + (gint8)td.ip [1]);
1311 one_arg_branch (&td, MINT_BRTRUE_I4, 5 + read32 (td.ip + 1));
1315 one_arg_branch (&td, MINT_BRTRUE_I4, 2 + (gint8)td.ip [1]);
1319 two_arg_branch (&td, MINT_BEQ_I4, 5 + read32 (td.ip + 1));
1323 two_arg_branch (&td, MINT_BEQ_I4, 2 + (gint8) td.ip [1]);
1327 two_arg_branch (&td, MINT_BGE_I4, 5 + read32 (td.ip + 1));
1331 two_arg_branch (&td, MINT_BGE_I4, 2 + (gint8) td.ip [1]);
1335 two_arg_branch (&td, MINT_BGT_I4, 5 + read32 (td.ip + 1));
1339 two_arg_branch (&td, MINT_BGT_I4, 2 + (gint8) td.ip [1]);
1343 two_arg_branch (&td, MINT_BLT_I4, 5 + read32 (td.ip + 1));
1347 two_arg_branch (&td, MINT_BLT_I4, 2 + (gint8) td.ip [1]);
1351 two_arg_branch (&td, MINT_BLE_I4, 5 + read32 (td.ip + 1));
1355 two_arg_branch (&td, MINT_BLE_I4, 2 + (gint8) td.ip [1]);
1359 two_arg_branch (&td, MINT_BNE_UN_I4, 5 + read32 (td.ip + 1));
1363 two_arg_branch (&td, MINT_BNE_UN_I4, 2 + (gint8) td.ip [1]);
1367 two_arg_branch (&td, MINT_BGE_UN_I4, 5 + read32 (td.ip + 1));
1371 two_arg_branch (&td, MINT_BGE_UN_I4, 2 + (gint8) td.ip [1]);
1375 two_arg_branch (&td, MINT_BGT_UN_I4, 5 + read32 (td.ip + 1));
1379 two_arg_branch (&td, MINT_BGT_UN_I4, 2 + (gint8) td.ip [1]);
1383 two_arg_branch (&td, MINT_BLE_UN_I4, 5 + read32 (td.ip + 1));
1387 two_arg_branch (&td, MINT_BLE_UN_I4, 2 + (gint8) td.ip [1]);
1391 two_arg_branch (&td, MINT_BLT_UN_I4, 5 + read32 (td.ip + 1));
1395 two_arg_branch (&td, MINT_BLT_UN_I4, 2 + (gint8) td.ip [1]);
1400 const unsigned char *next_ip;
1401 const unsigned char *base_ip = td.ip;
1402 unsigned short *next_new_ip;
1405 ADD_CODE (&td, MINT_SWITCH);
1408 next_ip = td.ip + n * 4;
1409 next_new_ip = td.new_ip + n * 2;
1411 int stack_height = td.sp - td.stack;
1412 for (i = 0; i < n; i++) {
1413 offset = read32 (td.ip);
1414 target = next_ip - td.il_code + offset;
1417 if (stack_height > 0 && stack_height != td.stack_height [target])
1418 g_warning ("SWITCH with back branch and non-empty stack");
1420 target = td.in_offsets [target] - (next_new_ip - td.new_code);
1422 td.stack_height [target] = stack_height;
1423 td.vt_stack_size [target] = td.vt_sp;
1424 if (stack_height > 0)
1425 td.stack_state [target] = g_memdup (td.stack, stack_height * sizeof (td.stack [0]));
1426 int prev = td.forward_refs [target];
1427 td.forward_refs [td.ip - td.il_code] = prev;
1428 td.forward_refs [target] = td.ip - td.il_code;
1429 td.in_offsets [td.ip - td.il_code] = - (base_ip - td.il_code);
1431 WRITE32 (&td, &target);
1437 CHECK_STACK (&td, 1);
1438 SIMPLE_OP (td, MINT_LDIND_I1);
1439 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1442 CHECK_STACK (&td, 1);
1443 SIMPLE_OP (td, MINT_LDIND_U1);
1444 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1447 CHECK_STACK (&td, 1);
1448 SIMPLE_OP (td, MINT_LDIND_I2);
1449 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1452 CHECK_STACK (&td, 1);
1453 SIMPLE_OP (td, MINT_LDIND_U2);
1454 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1457 CHECK_STACK (&td, 1);
1458 SIMPLE_OP (td, MINT_LDIND_I4);
1459 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1462 CHECK_STACK (&td, 1);
1463 SIMPLE_OP (td, MINT_LDIND_U4);
1464 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1467 CHECK_STACK (&td, 1);
1468 SIMPLE_OP (td, MINT_LDIND_I8);
1469 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1472 CHECK_STACK (&td, 1);
1473 SIMPLE_OP (td, MINT_LDIND_I);
1475 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1478 CHECK_STACK (&td, 1);
1479 SIMPLE_OP (td, MINT_LDIND_R4);
1480 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1483 CHECK_STACK (&td, 1);
1484 SIMPLE_OP (td, MINT_LDIND_R8);
1485 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1488 CHECK_STACK (&td, 1);
1489 SIMPLE_OP (td, MINT_LDIND_REF);
1490 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
1493 CHECK_STACK (&td, 2);
1494 SIMPLE_OP (td, MINT_STIND_REF);
1498 CHECK_STACK (&td, 2);
1499 SIMPLE_OP (td, MINT_STIND_I1);
1503 CHECK_STACK (&td, 2);
1504 SIMPLE_OP (td, MINT_STIND_I2);
1508 CHECK_STACK (&td, 2);
1509 SIMPLE_OP (td, MINT_STIND_I4);
1513 CHECK_STACK (&td, 2);
1514 SIMPLE_OP (td, MINT_STIND_I);
1518 CHECK_STACK (&td, 2);
1519 SIMPLE_OP (td, MINT_STIND_I8);
1523 CHECK_STACK (&td, 2);
1524 SIMPLE_OP (td, MINT_STIND_R4);
1528 CHECK_STACK (&td, 2);
1529 SIMPLE_OP (td, MINT_STIND_R8);
1533 binary_arith_op(&td, MINT_ADD_I4);
1537 binary_arith_op(&td, MINT_SUB_I4);
1541 binary_arith_op(&td, MINT_MUL_I4);
1545 binary_arith_op(&td, MINT_DIV_I4);
1549 binary_arith_op(&td, MINT_DIV_UN_I4);
1553 binary_int_op (&td, MINT_REM_I4);
1557 binary_int_op (&td, MINT_REM_UN_I4);
1561 binary_int_op (&td, MINT_AND_I4);
1565 binary_int_op (&td, MINT_OR_I4);
1569 binary_int_op (&td, MINT_XOR_I4);
1573 shift_op (&td, MINT_SHL_I4);
1577 shift_op (&td, MINT_SHR_I4);
1581 shift_op (&td, MINT_SHR_UN_I4);
1585 unary_arith_op (&td, MINT_NEG_I4);
1589 unary_arith_op (&td, MINT_NOT_I4);
1593 CHECK_STACK (&td, 1);
1594 switch (td.sp [-1].type) {
1596 ADD_CODE(&td, MINT_CONV_U1_R8);
1599 ADD_CODE(&td, MINT_CONV_U1_I4);
1602 ADD_CODE(&td, MINT_CONV_U1_I8);
1605 g_assert_not_reached ();
1608 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1611 CHECK_STACK (&td, 1);
1612 switch (td.sp [-1].type) {
1614 ADD_CODE(&td, MINT_CONV_I1_R8);
1617 ADD_CODE(&td, MINT_CONV_I1_I4);
1620 ADD_CODE(&td, MINT_CONV_I1_I8);
1623 g_assert_not_reached ();
1626 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1629 CHECK_STACK (&td, 1);
1630 switch (td.sp [-1].type) {
1632 ADD_CODE(&td, MINT_CONV_U2_R8);
1635 ADD_CODE(&td, MINT_CONV_U2_I4);
1638 ADD_CODE(&td, MINT_CONV_U2_I8);
1641 g_assert_not_reached ();
1644 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1647 CHECK_STACK (&td, 1);
1648 switch (td.sp [-1].type) {
1650 ADD_CODE(&td, MINT_CONV_I2_R8);
1653 ADD_CODE(&td, MINT_CONV_I2_I4);
1656 ADD_CODE(&td, MINT_CONV_I2_I8);
1659 g_assert_not_reached ();
1662 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1665 CHECK_STACK (&td, 1);
1666 switch (td.sp [-1].type) {
1668 #if SIZEOF_VOID_P == 4
1669 ADD_CODE(&td, MINT_CONV_U4_R8);
1671 ADD_CODE(&td, MINT_CONV_U8_R8);
1675 #if SIZEOF_VOID_P == 8
1676 ADD_CODE(&td, MINT_CONV_U8_I4);
1680 #if SIZEOF_VOID_P == 4
1681 ADD_CODE(&td, MINT_CONV_U4_I8);
1687 g_assert_not_reached ();
1690 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1693 CHECK_STACK (&td, 1);
1694 switch (td.sp [-1].type) {
1696 #if SIZEOF_VOID_P == 8
1697 ADD_CODE(&td, MINT_CONV_I8_R8);
1699 ADD_CODE(&td, MINT_CONV_I4_R8);
1703 #if SIZEOF_VOID_P == 8
1704 ADD_CODE(&td, MINT_CONV_I8_I4);
1712 #if SIZEOF_VOID_P == 4
1713 ADD_CODE(&td, MINT_CONV_I4_I8);
1717 g_assert_not_reached ();
1720 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
1723 CHECK_STACK (&td, 1);
1724 switch (td.sp [-1].type) {
1726 ADD_CODE(&td, MINT_CONV_U4_R8);
1731 ADD_CODE(&td, MINT_CONV_U4_I8);
1734 #if SIZEOF_VOID_P == 8
1735 ADD_CODE(&td, MINT_CONV_U4_I8);
1739 g_assert_not_reached ();
1742 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1745 CHECK_STACK (&td, 1);
1746 switch (td.sp [-1].type) {
1748 ADD_CODE(&td, MINT_CONV_I4_R8);
1753 ADD_CODE(&td, MINT_CONV_I4_I8);
1756 #if SIZEOF_VOID_P == 8
1757 ADD_CODE(&td, MINT_CONV_I4_I8);
1761 g_assert_not_reached ();
1764 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
1767 CHECK_STACK (&td, 1);
1768 switch (td.sp [-1].type) {
1770 ADD_CODE(&td, MINT_CONV_I8_R8);
1773 ADD_CODE(&td, MINT_CONV_I8_I4);
1778 #if SIZEOF_VOID_P == 4
1779 ADD_CODE(&td, MINT_CONV_I8_I4);
1783 g_assert_not_reached ();
1786 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1789 CHECK_STACK (&td, 1);
1790 switch (td.sp [-1].type) {
1792 ADD_CODE(&td, MINT_CONV_R4_R8);
1795 ADD_CODE(&td, MINT_CONV_R4_I8);
1798 ADD_CODE(&td, MINT_CONV_R4_I4);
1801 g_assert_not_reached ();
1804 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1807 CHECK_STACK (&td, 1);
1808 switch (td.sp [-1].type) {
1810 ADD_CODE(&td, MINT_CONV_R8_I4);
1813 ADD_CODE(&td, MINT_CONV_R8_I8);
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_U8_I4);
1832 ADD_CODE(&td, MINT_CONV_U8_R8);
1835 #if SIZEOF_VOID_P == 4
1836 ADD_CODE(&td, MINT_CONV_U8_I4);
1840 g_assert_not_reached ();
1843 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
1846 CHECK_STACK (&td, 2);
1848 token = read32 (td.ip + 1);
1849 klass = mono_class_get_full (image, token, generic_context);
1851 if (klass->valuetype) {
1852 ADD_CODE (&td, MINT_CPOBJ);
1853 ADD_CODE (&td, get_data_item_index(&td, klass));
1855 ADD_CODE (&td, MINT_LDIND_REF);
1856 ADD_CODE (&td, MINT_STIND_REF);
1864 CHECK_STACK (&td, 1);
1866 token = read32 (td.ip + 1);
1868 if (method->wrapper_type != MONO_WRAPPER_NONE)
1869 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1871 klass = mono_class_get_full (image, token, generic_context);
1873 ADD_CODE(&td, MINT_LDOBJ);
1874 ADD_CODE(&td, get_data_item_index(&td, klass));
1875 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1876 size = mono_class_value_size (klass, NULL);
1880 SET_TYPE(td.sp - 1, stack_type[mint_type(&klass->byval_arg)], klass);
1885 token = mono_metadata_token_index (read32 (td.ip + 1));
1887 if (method->wrapper_type != MONO_WRAPPER_NONE) {
1888 s = mono_string_new_wrapper(
1889 mono_method_get_wrapper_data (method, token));
1892 s = mono_ldstr (domain, image, token);
1893 ADD_CODE(&td, MINT_LDSTR);
1894 ADD_CODE(&td, get_data_item_index (&td, s));
1895 PUSH_TYPE(&td, STACK_TYPE_O, mono_defaults.string_class);
1900 MonoMethodSignature *csignature;
1901 guint32 vt_stack_used = 0;
1902 guint32 vt_res_size = 0;
1905 token = read32 (td.ip);
1908 if (method->wrapper_type != MONO_WRAPPER_NONE)
1909 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
1911 m = mono_get_method_full (image, token, NULL, generic_context);
1913 csignature = mono_method_signature (m);
1915 td.sp -= csignature->param_count;
1916 ADD_CODE(&td, MINT_NEWOBJ);
1917 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
1918 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
1920 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT) {
1921 vt_res_size = mono_class_value_size (klass, NULL);
1922 PUSH_VT (&td, vt_res_size);
1924 for (i = 0; i < csignature->param_count; ++i) {
1925 int mt = mint_type(csignature->params [i]);
1926 if (mt == MINT_TYPE_VT) {
1927 MonoClass *k = mono_class_from_mono_type (csignature->params [i]);
1928 gint32 size = mono_class_value_size (k, NULL);
1929 size = (size + 7) & ~7;
1930 vt_stack_used += size;
1933 if (vt_stack_used != 0 || vt_res_size != 0) {
1934 ADD_CODE(&td, MINT_VTRESULT);
1935 ADD_CODE(&td, vt_res_size);
1936 WRITE32(&td, &vt_stack_used);
1937 td.vt_sp -= vt_stack_used;
1939 PUSH_TYPE (&td, stack_type [mint_type (&klass->byval_arg)], klass);
1943 CHECK_STACK (&td, 1);
1944 token = read32 (td.ip + 1);
1945 klass = mono_class_get_full (image, token, generic_context);
1946 ADD_CODE(&td, MINT_CASTCLASS);
1947 ADD_CODE(&td, get_data_item_index (&td, klass));
1948 td.sp [-1].klass = klass;
1952 CHECK_STACK (&td, 1);
1953 token = read32 (td.ip + 1);
1954 klass = mono_class_get_full (image, token, generic_context);
1955 ADD_CODE(&td, MINT_ISINST);
1956 ADD_CODE(&td, get_data_item_index (&td, klass));
1960 switch (td.sp [-1].type) {
1964 ADD_CODE(&td, MINT_CONV_R_UN_I8);
1967 ADD_CODE(&td, MINT_CONV_R_UN_I4);
1970 g_assert_not_reached ();
1972 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
1976 CHECK_STACK (&td, 1);
1977 token = read32 (td.ip + 1);
1979 if (method->wrapper_type != MONO_WRAPPER_NONE)
1980 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
1982 klass = mono_class_get_full (image, token, generic_context);
1984 if (mono_class_is_nullable (klass)) {
1985 g_error ("cee_unbox: implement Nullable");
1988 ADD_CODE(&td, MINT_UNBOX);
1989 ADD_CODE(&td, get_data_item_index (&td, klass));
1990 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
1994 CHECK_STACK (&td, 1);
1995 token = read32 (td.ip + 1);
1997 g_assert (method->wrapper_type == MONO_WRAPPER_NONE);
1998 klass = mono_class_get_full (image, token, generic_context);
2000 if (mini_type_is_reference (&klass->byval_arg)) {
2001 ADD_CODE (&td, MINT_CASTCLASS);
2002 ADD_CODE (&td, get_data_item_index (&td, klass));
2003 SET_TYPE (td.sp - 1, stack_type [mt], klass);
2005 } else if (mono_class_is_nullable (klass)) {
2006 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Unbox", 1);
2007 /* td.ip is incremented by interp_transform_call */
2008 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2010 int mt = mint_type (&klass->byval_arg);
2011 ADD_CODE (&td, MINT_UNBOX);
2012 ADD_CODE (&td, get_data_item_index (&td, klass));
2014 ADD_CODE (&td, MINT_LDOBJ);
2015 ADD_CODE (&td, get_data_item_index(&td, klass));
2016 SET_TYPE (td.sp - 1, stack_type [mt], klass);
2018 if (mt == MINT_TYPE_VT) {
2019 int size = mono_class_value_size (klass, NULL);
2020 PUSH_VT (&td, size);
2027 CHECK_STACK (&td, 1);
2028 SIMPLE_OP (td, MINT_THROW);
2030 generating_code = 0;
2033 CHECK_STACK (&td, 1);
2034 token = read32 (td.ip + 1);
2035 field = mono_field_from_token (image, token, &klass, generic_context);
2036 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2037 mono_class_init (klass);
2039 ADD_CODE (&td, MINT_POP);
2041 ADD_CODE (&td, MINT_LDSFLDA);
2042 ADD_CODE (&td, get_data_item_index (&td, field));
2044 if ((td.sp - 1)->type == STACK_TYPE_O) {
2045 ADD_CODE (&td, MINT_LDFLDA);
2047 g_assert ((td.sp -1)->type == STACK_TYPE_MP);
2048 ADD_CODE (&td, MINT_LDFLDA_UNSAFE);
2050 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
2053 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2056 CHECK_STACK (&td, 1);
2057 token = read32 (td.ip + 1);
2058 field = mono_field_from_token (image, token, &klass, generic_context);
2059 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2060 mono_class_init (klass);
2062 MonoClass *field_klass = mono_class_from_mono_type (field->type);
2063 mt = mint_type (&field_klass->byval_arg);
2064 if (klass->marshalbyref) {
2065 g_assert (!is_static);
2066 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDRMFLD_VT : MINT_LDRMFLD);
2067 ADD_CODE(&td, get_data_item_index (&td, field));
2070 ADD_CODE (&td, MINT_POP);
2072 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2073 ADD_CODE (&td, get_data_item_index (&td, field));
2075 ADD_CODE (&td, MINT_LDFLD_I1 + mt - MINT_TYPE_I1);
2076 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2079 if (mt == MINT_TYPE_VT) {
2080 int size = mono_class_value_size (field_klass, NULL);
2082 WRITE32(&td, &size);
2084 if (td.sp [-1].type == STACK_TYPE_VT) {
2085 int size = mono_class_value_size (klass, NULL);
2086 size = (size + 7) & ~7;
2088 ADD_CODE (&td, MINT_VTRESULT);
2090 WRITE32 (&td, &size);
2093 SET_TYPE(td.sp - 1, stack_type [mt], field_klass);
2097 CHECK_STACK (&td, 2);
2098 token = read32 (td.ip + 1);
2099 field = mono_field_from_token (image, token, &klass, generic_context);
2100 gboolean is_static = !!(field->type->attrs & FIELD_ATTRIBUTE_STATIC);
2101 mono_class_init (klass);
2102 mt = mint_type(field->type);
2104 if (klass->marshalbyref) {
2105 g_assert (!is_static);
2106 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STRMFLD_VT : MINT_STRMFLD);
2107 ADD_CODE(&td, get_data_item_index (&td, field));
2110 ADD_CODE (&td, MINT_POP);
2112 ADD_CODE (&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2113 ADD_CODE (&td, get_data_item_index (&td, field));
2115 ADD_CODE (&td, MINT_STFLD_I1 + mt - MINT_TYPE_I1);
2116 ADD_CODE (&td, klass->valuetype ? field->offset - sizeof(MonoObject) : field->offset);
2119 if (mt == MINT_TYPE_VT) {
2120 MonoClass *klass = mono_class_from_mono_type (field->type);
2121 int size = mono_class_value_size (klass, NULL);
2123 WRITE32(&td, &size);
2130 token = read32 (td.ip + 1);
2131 field = mono_field_from_token (image, token, &klass, generic_context);
2132 ADD_CODE(&td, MINT_LDSFLDA);
2133 ADD_CODE(&td, get_data_item_index (&td, field));
2135 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
2138 token = read32 (td.ip + 1);
2139 field = mono_field_from_token (image, token, &klass, generic_context);
2140 mt = mint_type(field->type);
2141 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_LDSFLD_VT : MINT_LDSFLD);
2142 ADD_CODE(&td, get_data_item_index (&td, field));
2144 if (mt == MINT_TYPE_VT) {
2145 MonoClass *klass = mono_class_from_mono_type (field->type);
2146 int size = mono_class_value_size (klass, NULL);
2148 WRITE32(&td, &size);
2149 klass = field->type->data.klass;
2151 if (mt == MINT_TYPE_O)
2152 klass = mono_class_from_mono_type (field->type);
2155 PUSH_TYPE(&td, stack_type [mt], klass);
2158 CHECK_STACK (&td, 1);
2159 token = read32 (td.ip + 1);
2160 field = mono_field_from_token (image, token, &klass, generic_context);
2161 mt = mint_type(field->type);
2162 ADD_CODE(&td, mt == MINT_TYPE_VT ? MINT_STSFLD_VT : MINT_STSFLD);
2163 ADD_CODE(&td, get_data_item_index (&td, field));
2164 if (mt == MINT_TYPE_VT) {
2165 MonoClass *klass = mono_class_from_mono_type (field->type);
2166 int size = mono_class_value_size (klass, NULL);
2168 WRITE32 (&td, &size);
2175 token = read32 (td.ip + 1);
2177 if (method->wrapper_type != MONO_WRAPPER_NONE)
2178 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2180 klass = mono_class_get_full (image, token, generic_context);
2182 ADD_CODE(&td, td.sp [-1].type == STACK_TYPE_VT ? MINT_STOBJ_VT : MINT_STOBJ);
2183 ADD_CODE(&td, get_data_item_index (&td, klass));
2184 if (td.sp [-1].type == STACK_TYPE_VT) {
2185 size = mono_class_value_size (klass, NULL);
2186 size = (size + 7) & ~7;
2193 case CEE_CONV_OVF_I_UN:
2194 case CEE_CONV_OVF_U_UN:
2195 CHECK_STACK (&td, 1);
2196 switch (td.sp [-1].type) {
2198 #if SIZEOF_VOID_P == 8
2199 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2201 ADD_CODE(&td, MINT_CONV_OVF_I4_UN_R8);
2205 #if SIZEOF_VOID_P == 4
2206 ADD_CODE (&td, MINT_CONV_OVF_I4_UN_I8);
2210 #if SIZEOF_VOID_P == 8
2211 ADD_CODE(&td, MINT_CONV_I8_U4);
2215 g_assert_not_reached ();
2218 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2221 case CEE_CONV_OVF_I8_UN:
2222 case CEE_CONV_OVF_U8_UN:
2223 CHECK_STACK (&td, 1);
2224 switch (td.sp [-1].type) {
2226 ADD_CODE(&td, MINT_CONV_OVF_I8_UN_R8);
2229 if (*td.ip == CEE_CONV_OVF_I8_UN)
2230 ADD_CODE (&td, MINT_CONV_OVF_I8_U8);
2233 ADD_CODE(&td, MINT_CONV_I8_U4);
2236 g_assert_not_reached ();
2239 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2244 CHECK_STACK (&td, 1);
2245 token = read32 (td.ip + 1);
2246 if (method->wrapper_type != MONO_WRAPPER_NONE)
2247 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2249 klass = mono_class_get_full (image, token, generic_context);
2251 if (mono_class_is_nullable (klass)) {
2252 MonoMethod *target_method = mono_class_get_method_from_name (klass, "Box", 1);
2253 /* td.ip is incremented by interp_transform_call */
2254 interp_transform_call (&td, method, target_method, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2255 } else if (!klass->valuetype) {
2256 /* already boxed, do nothing. */
2259 if (mint_type (&klass->byval_arg) == MINT_TYPE_VT && !klass->enumtype) {
2260 size = mono_class_value_size (klass, NULL);
2261 size = (size + 7) & ~7;
2264 ADD_CODE(&td, MINT_BOX);
2265 ADD_CODE(&td, get_data_item_index (&td, klass));
2267 SET_TYPE(td.sp - 1, STACK_TYPE_O, klass);
2274 CHECK_STACK (&td, 1);
2275 token = read32 (td.ip + 1);
2277 if (method->wrapper_type != MONO_WRAPPER_NONE)
2278 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2280 klass = mono_class_get_full (image, token, generic_context);
2282 unsigned char lentype = (td.sp - 1)->type;
2283 if (lentype == STACK_TYPE_I8) {
2284 /* mimic mini behaviour */
2285 ADD_CODE (&td, MINT_CONV_OVF_U4_I8);
2287 g_assert (lentype == STACK_TYPE_I4);
2288 ADD_CODE (&td, MINT_CONV_OVF_U4_I4);
2290 SET_SIMPLE_TYPE (td.sp - 1, STACK_TYPE_I4);
2291 ADD_CODE (&td, MINT_NEWARR);
2292 ADD_CODE (&td, get_data_item_index (&td, klass));
2293 SET_TYPE (td.sp - 1, STACK_TYPE_O, klass);
2298 CHECK_STACK (&td, 1);
2299 SIMPLE_OP (td, MINT_LDLEN);
2300 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2303 CHECK_STACK (&td, 2);
2305 token = read32 (td.ip + 1);
2307 if (method->wrapper_type != MONO_WRAPPER_NONE)
2308 klass = (MonoClass *) mono_method_get_wrapper_data (method, token);
2310 klass = mono_class_get_full (image, token, generic_context);
2312 if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
2313 ADD_CODE (&td, MINT_LDELEMA_TC);
2315 ADD_CODE (&td, MINT_LDELEMA);
2317 ADD_CODE (&td, get_data_item_index (&td, klass));
2318 /* according to spec, ldelema bytecode is only used for 1-dim arrays */
2324 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2327 CHECK_STACK (&td, 2);
2329 SIMPLE_OP (td, MINT_LDELEM_I1);
2331 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2334 CHECK_STACK (&td, 2);
2336 SIMPLE_OP (td, MINT_LDELEM_U1);
2338 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2341 CHECK_STACK (&td, 2);
2343 SIMPLE_OP (td, MINT_LDELEM_I2);
2345 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2348 CHECK_STACK (&td, 2);
2350 SIMPLE_OP (td, MINT_LDELEM_U2);
2352 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2355 CHECK_STACK (&td, 2);
2357 SIMPLE_OP (td, MINT_LDELEM_I4);
2359 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2362 CHECK_STACK (&td, 2);
2364 SIMPLE_OP (td, MINT_LDELEM_U4);
2366 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2369 CHECK_STACK (&td, 2);
2371 SIMPLE_OP (td, MINT_LDELEM_I8);
2373 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2376 CHECK_STACK (&td, 2);
2378 SIMPLE_OP (td, MINT_LDELEM_I);
2380 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2383 CHECK_STACK (&td, 2);
2385 SIMPLE_OP (td, MINT_LDELEM_R4);
2387 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2390 CHECK_STACK (&td, 2);
2392 SIMPLE_OP (td, MINT_LDELEM_R8);
2394 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2396 case CEE_LDELEM_REF:
2397 CHECK_STACK (&td, 2);
2399 SIMPLE_OP (td, MINT_LDELEM_REF);
2401 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2404 CHECK_STACK (&td, 2);
2405 token = read32 (td.ip + 1);
2406 klass = mono_class_get_full (image, token, generic_context);
2407 switch (mint_type (&klass->byval_arg)) {
2410 SIMPLE_OP (td, MINT_LDELEM_I1);
2412 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2416 SIMPLE_OP (td, MINT_LDELEM_U1);
2418 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2422 SIMPLE_OP (td, MINT_LDELEM_U2);
2424 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2428 SIMPLE_OP (td, MINT_LDELEM_I2);
2430 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2434 SIMPLE_OP (td, MINT_LDELEM_I4);
2436 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2440 SIMPLE_OP (td, MINT_LDELEM_I8);
2442 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2446 SIMPLE_OP (td, MINT_LDELEM_R4);
2448 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2452 SIMPLE_OP (td, MINT_LDELEM_R8);
2454 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_R8);
2458 SIMPLE_OP (td, MINT_LDELEM_REF);
2460 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_O);
2462 case MINT_TYPE_VT: {
2463 int size = mono_class_value_size (klass, NULL);
2465 SIMPLE_OP (td, MINT_LDELEM_VT);
2466 ADD_CODE (&td, get_data_item_index (&td, klass));
2467 WRITE32 (&td, &size);
2469 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_VT);
2470 PUSH_VT (&td, size);
2474 GString *res = g_string_new ("");
2475 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2476 g_print ("LDELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2477 g_string_free (res, TRUE);
2485 CHECK_STACK (&td, 3);
2487 SIMPLE_OP (td, MINT_STELEM_I);
2491 CHECK_STACK (&td, 3);
2493 SIMPLE_OP (td, MINT_STELEM_I1);
2497 CHECK_STACK (&td, 3);
2499 SIMPLE_OP (td, MINT_STELEM_I2);
2503 CHECK_STACK (&td, 3);
2505 SIMPLE_OP (td, MINT_STELEM_I4);
2509 CHECK_STACK (&td, 3);
2511 SIMPLE_OP (td, MINT_STELEM_I8);
2515 CHECK_STACK (&td, 3);
2517 SIMPLE_OP (td, MINT_STELEM_R4);
2521 CHECK_STACK (&td, 3);
2523 SIMPLE_OP (td, MINT_STELEM_R8);
2526 case CEE_STELEM_REF:
2527 CHECK_STACK (&td, 3);
2529 SIMPLE_OP (td, MINT_STELEM_REF);
2533 CHECK_STACK (&td, 3);
2535 token = read32 (td.ip + 1);
2536 klass = mono_class_get_full (image, token, generic_context);
2537 switch (mint_type (&klass->byval_arg)) {
2539 SIMPLE_OP (td, MINT_STELEM_U1);
2542 SIMPLE_OP (td, MINT_STELEM_I4);
2545 SIMPLE_OP (td, MINT_STELEM_I8);
2548 SIMPLE_OP (td, MINT_STELEM_REF);
2550 case MINT_TYPE_VT: {
2551 int size = mono_class_value_size (klass, NULL);
2552 SIMPLE_OP (td, MINT_STELEM_VT);
2553 ADD_CODE (&td, get_data_item_index (&td, klass));
2554 WRITE32 (&td, &size);
2559 GString *res = g_string_new ("");
2560 mono_type_get_desc (res, &klass->byval_arg, TRUE);
2561 g_print ("STELEM: %s -> %d (%s)\n", klass->name, mint_type (&klass->byval_arg), res->str);
2562 g_string_free (res, TRUE);
2571 case CEE_CONV_OVF_U1:
2573 case CEE_CONV_OVF_I8:
2575 #if SIZEOF_VOID_P == 8
2576 case CEE_CONV_OVF_U:
2578 case CEE_REFANYVAL: ves_abort(); break;
2581 CHECK_STACK (&td, 1);
2582 SIMPLE_OP (td, MINT_CKFINITE);
2584 case CEE_CONV_OVF_I1:
2585 case CEE_CONV_OVF_I1_UN:
2586 CHECK_STACK (&td, 1);
2587 switch (td.sp [-1].type) {
2589 ADD_CODE(&td, MINT_CONV_OVF_I1_R8);
2592 ADD_CODE(&td, MINT_CONV_OVF_I1_I4);
2595 ADD_CODE(&td, MINT_CONV_OVF_I1_I8);
2598 g_assert_not_reached ();
2601 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2603 case CEE_CONV_OVF_U1:
2604 case CEE_CONV_OVF_U1_UN:
2605 CHECK_STACK (&td, 1);
2606 switch (td.sp [-1].type) {
2608 ADD_CODE(&td, MINT_CONV_OVF_U1_R8);
2611 ADD_CODE(&td, MINT_CONV_OVF_U1_I4);
2614 ADD_CODE(&td, MINT_CONV_OVF_U1_I8);
2617 g_assert_not_reached ();
2620 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2622 case CEE_CONV_OVF_I2:
2623 case CEE_CONV_OVF_I2_UN:
2624 CHECK_STACK (&td, 1);
2625 switch (td.sp [-1].type) {
2627 ADD_CODE(&td, MINT_CONV_OVF_I2_R8);
2630 ADD_CODE(&td, MINT_CONV_OVF_I2_I4);
2633 ADD_CODE(&td, MINT_CONV_OVF_I2_I8);
2636 g_assert_not_reached ();
2639 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2641 case CEE_CONV_OVF_U2_UN:
2642 case CEE_CONV_OVF_U2:
2643 CHECK_STACK (&td, 1);
2644 switch (td.sp [-1].type) {
2646 ADD_CODE(&td, MINT_CONV_OVF_U2_R8);
2649 ADD_CODE(&td, MINT_CONV_OVF_U2_I4);
2652 ADD_CODE(&td, MINT_CONV_OVF_U2_I8);
2655 g_assert_not_reached ();
2658 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2660 #if SIZEOF_VOID_P == 4
2661 case CEE_CONV_OVF_I:
2663 case CEE_CONV_OVF_I4:
2664 case CEE_CONV_OVF_I4_UN:
2665 CHECK_STACK (&td, 1);
2666 switch (td.sp [-1].type) {
2668 ADD_CODE(&td, MINT_CONV_OVF_I4_R8);
2671 if (*td.ip == CEE_CONV_OVF_I4_UN)
2672 ADD_CODE(&td, MINT_CONV_OVF_I4_U4);
2675 if (*td.ip == CEE_CONV_OVF_I4_UN)
2676 ADD_CODE (&td, MINT_CONV_OVF_I4_U8);
2678 ADD_CODE (&td, MINT_CONV_OVF_I4_I8);
2681 g_assert_not_reached ();
2684 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2686 #if SIZEOF_VOID_P == 4
2687 case CEE_CONV_OVF_U:
2689 case CEE_CONV_OVF_U4:
2690 case CEE_CONV_OVF_U4_UN:
2691 CHECK_STACK (&td, 1);
2692 switch (td.sp [-1].type) {
2694 ADD_CODE(&td, MINT_CONV_OVF_U4_R8);
2697 if (*td.ip != CEE_CONV_OVF_U4_UN)
2698 ADD_CODE(&td, MINT_CONV_OVF_U4_I4);
2701 ADD_CODE(&td, MINT_CONV_OVF_U4_I8);
2704 g_assert_not_reached ();
2707 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
2709 #if SIZEOF_VOID_P == 8
2710 case CEE_CONV_OVF_I:
2712 case CEE_CONV_OVF_I8:
2713 CHECK_STACK (&td, 1);
2714 switch (td.sp [-1].type) {
2716 ADD_CODE(&td, MINT_CONV_OVF_I8_R8);
2719 ADD_CODE(&td, MINT_CONV_I8_I4);
2724 g_assert_not_reached ();
2727 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2729 #if SIZEOF_VOID_P == 8
2730 case CEE_CONV_OVF_U:
2732 case CEE_CONV_OVF_U8:
2733 CHECK_STACK (&td, 1);
2734 switch (td.sp [-1].type) {
2736 ADD_CODE(&td, MINT_CONV_OVF_U8_R8);
2739 ADD_CODE(&td, MINT_CONV_OVF_U8_I4);
2742 ADD_CODE (&td, MINT_CONV_OVF_U8_I8);
2745 g_assert_not_reached ();
2748 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I8);
2753 token = read32 (td.ip + 1);
2754 if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD || method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2755 handle = mono_method_get_wrapper_data (method, token);
2756 klass = (MonoClass *) mono_method_get_wrapper_data (method, token + 1);
2757 if (klass == mono_defaults.typehandle_class)
2758 handle = &((MonoClass *) handle)->byval_arg;
2760 if (generic_context) {
2761 handle = mono_class_inflate_generic_type_checked (handle, generic_context, &error);
2762 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
2765 handle = mono_ldtoken (image, token, &klass, generic_context);
2767 mono_class_init (klass);
2768 mt = mint_type (&klass->byval_arg);
2769 g_assert (mt == MINT_TYPE_VT);
2770 size = mono_class_value_size (klass, NULL);
2771 g_assert (size == sizeof(gpointer));
2772 PUSH_VT (&td, sizeof(gpointer));
2773 ADD_CODE (&td, MINT_LDTOKEN);
2774 ADD_CODE (&td, get_data_item_index (&td, handle));
2776 SET_TYPE (td.sp, stack_type [mt], klass);
2782 binary_arith_op(&td, MINT_ADD_OVF_I4);
2785 case CEE_ADD_OVF_UN:
2786 binary_arith_op(&td, MINT_ADD_OVF_UN_I4);
2790 binary_arith_op(&td, MINT_MUL_OVF_I4);
2793 case CEE_MUL_OVF_UN:
2794 binary_arith_op(&td, MINT_MUL_OVF_UN_I4);
2798 binary_arith_op(&td, MINT_SUB_OVF_I4);
2801 case CEE_SUB_OVF_UN:
2802 binary_arith_op(&td, MINT_SUB_OVF_UN_I4);
2805 case CEE_ENDFINALLY:
2807 SIMPLE_OP (td, MINT_ENDFINALLY);
2808 generating_code = 0;
2812 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 5 + read32 (td.ip + 1));
2814 generating_code = 0;
2818 handle_branch (&td, MINT_LEAVE_S, MINT_LEAVE, 2 + (gint8)td.ip [1]);
2820 generating_code = 0;
2825 case CEE_MONO_CALLI_EXTRA_ARG:
2826 /* Same as CEE_CALLI, except that we drop the extra arg required for llvm specific behaviour */
2827 ADD_CODE (&td, MINT_POP);
2830 interp_transform_call (&td, method, NULL, domain, generic_context, is_bb_start, body_start_offset, NULL, FALSE);
2832 case CEE_MONO_JIT_ICALL_ADDR: {
2835 MonoJitICallInfo *info;
2837 token = read32 (td.ip + 1);
2839 func = mono_method_get_wrapper_data (method, token);
2840 info = mono_find_jit_icall_by_addr (func);
2842 ADD_CODE (&td, MINT_LDFTN);
2843 ADD_CODE (&td, get_data_item_index (&td, func));
2844 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_I);
2847 case CEE_MONO_ICALL: {
2850 MonoJitICallInfo *info;
2852 token = read32 (td.ip + 1);
2854 func = mono_method_get_wrapper_data (method, token);
2855 info = mono_find_jit_icall_by_addr (func);
2858 CHECK_STACK (&td, info->sig->param_count);
2859 switch (info->sig->param_count) {
2861 if (MONO_TYPE_IS_VOID (info->sig->ret))
2862 ADD_CODE (&td,MINT_ICALL_V_V);
2864 ADD_CODE (&td, MINT_ICALL_V_P);
2867 if (MONO_TYPE_IS_VOID (info->sig->ret))
2868 ADD_CODE (&td,MINT_ICALL_P_V);
2870 ADD_CODE (&td,MINT_ICALL_P_P);
2873 if (MONO_TYPE_IS_VOID (info->sig->ret)) {
2874 if (info->sig->params [1]->type == MONO_TYPE_I4)
2875 ADD_CODE (&td,MINT_ICALL_PI_V);
2877 ADD_CODE (&td,MINT_ICALL_PP_V);
2879 if (info->sig->params [1]->type == MONO_TYPE_I4)
2880 ADD_CODE (&td,MINT_ICALL_PI_P);
2882 ADD_CODE (&td,MINT_ICALL_PP_P);
2886 g_assert (MONO_TYPE_IS_VOID (info->sig->ret));
2887 if (info->sig->params [2]->type == MONO_TYPE_I4)
2888 ADD_CODE (&td,MINT_ICALL_PPI_V);
2890 ADD_CODE (&td,MINT_ICALL_PPP_V);
2893 g_assert_not_reached ();
2896 if (func == mono_ftnptr_to_delegate) {
2897 g_error ("TODO: ?");
2899 ADD_CODE(&td, get_data_item_index (&td, func));
2900 td.sp -= info->sig->param_count;
2902 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2904 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I);
2908 case CEE_MONO_VTADDR: {
2910 CHECK_STACK (&td, 1);
2911 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
2912 size = mono_class_native_size(td.sp [-1].klass, NULL);
2914 size = mono_class_value_size(td.sp [-1].klass, NULL);
2915 size = (size + 7) & ~7;
2916 ADD_CODE(&td, MINT_VTRESULT);
2918 WRITE32(&td, &size);
2921 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2924 case CEE_MONO_LDPTR:
2925 case CEE_MONO_CLASSCONST:
2926 token = read32 (td.ip + 1);
2928 ADD_CODE(&td, MINT_MONO_LDPTR);
2929 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2930 td.sp [0].type = STACK_TYPE_I;
2933 case CEE_MONO_OBJADDR:
2934 CHECK_STACK (&td, 1);
2936 td.sp[-1].type = STACK_TYPE_MP;
2939 case CEE_MONO_NEWOBJ:
2940 token = read32 (td.ip + 1);
2942 ADD_CODE(&td, MINT_MONO_NEWOBJ);
2943 ADD_CODE(&td, get_data_item_index (&td, mono_method_get_wrapper_data (method, token)));
2944 td.sp [0].type = STACK_TYPE_O;
2947 case CEE_MONO_RETOBJ:
2948 CHECK_STACK (&td, 1);
2949 token = read32 (td.ip + 1);
2951 ADD_CODE(&td, MINT_MONO_RETOBJ);
2954 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2956 /*stackval_from_data (signature->ret, frame->retval, sp->data.vt, signature->pinvoke);*/
2958 if (td.sp > td.stack)
2959 g_warning ("CEE_MONO_RETOBJ: more values on stack: %d", td.sp-td.stack);
2961 case CEE_MONO_LDNATIVEOBJ:
2962 token = read32 (td.ip + 1);
2964 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
2965 g_assert(klass->valuetype);
2966 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
2968 case CEE_MONO_SAVE_LMF:
2969 case CEE_MONO_RESTORE_LMF:
2970 case CEE_MONO_NOT_TAKEN:
2973 case CEE_MONO_LDPTR_INT_REQ_FLAG:
2974 ADD_CODE (&td, MINT_MONO_LDPTR);
2975 ADD_CODE (&td, get_data_item_index (&td, mono_thread_interruption_request_flag ()));
2976 PUSH_TYPE (&td, STACK_TYPE_MP, NULL);
2980 g_error ("transform.c: Unimplemented opcode: 0xF0 %02x at 0x%x\n", *td.ip, td.ip-header->code);
2990 case CEE_PREFIXREF: ves_abort(); break;
2993 * Note: Exceptions thrown when executing a prefixed opcode need
2994 * to take into account the number of prefix bytes (usually the
2995 * throw point is just (ip - n_prefix_bytes).
3001 case CEE_ARGLIST: ves_abort(); break;
3004 CHECK_STACK(&td, 2);
3005 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3006 ADD_CODE(&td, MINT_CEQ_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3008 ADD_CODE(&td, MINT_CEQ_I4 + td.sp [-1].type - STACK_TYPE_I4);
3010 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3014 CHECK_STACK(&td, 2);
3015 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3016 ADD_CODE(&td, MINT_CGT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3018 ADD_CODE(&td, MINT_CGT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3020 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3024 CHECK_STACK(&td, 2);
3025 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3026 ADD_CODE(&td, MINT_CGT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3028 ADD_CODE(&td, MINT_CGT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3030 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3034 CHECK_STACK(&td, 2);
3035 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3036 ADD_CODE(&td, MINT_CLT_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3038 ADD_CODE(&td, MINT_CLT_I4 + td.sp [-1].type - STACK_TYPE_I4);
3040 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3044 CHECK_STACK(&td, 2);
3045 if (td.sp [-1].type == STACK_TYPE_O || td.sp [-1].type == STACK_TYPE_MP)
3046 ADD_CODE(&td, MINT_CLT_UN_I4 + STACK_TYPE_I - STACK_TYPE_I4);
3048 ADD_CODE(&td, MINT_CLT_UN_I4 + td.sp [-1].type - STACK_TYPE_I4);
3050 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_I4);
3053 case CEE_LDVIRTFTN: /* fallthrough */
3056 if (*td.ip == CEE_LDVIRTFTN) {
3057 CHECK_STACK (&td, 1);
3060 token = read32 (td.ip + 1);
3061 if (method->wrapper_type != MONO_WRAPPER_NONE)
3062 m = (MonoMethod *)mono_method_get_wrapper_data (method, token);
3064 m = mono_get_method_full (image, token, NULL, generic_context);
3066 if (method->wrapper_type == MONO_WRAPPER_NONE && m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3067 m = mono_marshal_get_synchronized_wrapper (m);
3069 ADD_CODE(&td, *td.ip == CEE_LDFTN ? MINT_LDFTN : MINT_LDVIRTFTN);
3070 ADD_CODE(&td, get_data_item_index (&td, mono_interp_get_runtime_method (domain, m, &error)));
3071 mono_error_cleanup (&error); /* FIXME: don't swallow the error */
3073 PUSH_SIMPLE_TYPE (&td, STACK_TYPE_F);
3077 load_arg (&td, read16 (td.ip + 1));
3081 int n = read16 (td.ip + 1);
3082 ADD_CODE (&td, MINT_LDARGA);
3083 ADD_CODE (&td, td.rtm->arg_offsets [n]); /* FIX for large offsets */
3084 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3089 store_arg (&td, read16 (td.ip + 1));
3093 load_local (&td, read16 (td.ip + 1));
3097 ADD_CODE(&td, MINT_LDLOCA_S);
3098 ADD_CODE(&td, td.rtm->local_offsets [read16 (td.ip + 1)]);
3099 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_MP);
3103 store_local (&td, read16 (td.ip + 1));
3107 CHECK_STACK (&td, 1);
3108 #if SIZEOF_VOID_P == 8
3109 if (td.sp [-1].type == STACK_TYPE_I8)
3110 ADD_CODE(&td, MINT_CONV_I4_I8);
3112 ADD_CODE(&td, MINT_LOCALLOC);
3113 if (td.sp != td.stack + 1)
3114 g_warning("CEE_LOCALLOC: stack not empty");
3116 SET_SIMPLE_TYPE(td.sp - 1, STACK_TYPE_MP);
3119 case CEE_UNUSED57: ves_abort(); break;
3122 ADD_CODE (&td, MINT_ENDFILTER);
3125 case CEE_UNALIGNED_:
3127 /* FIX: should do something? */;
3131 /* FIX: should do something? */;
3135 /* FIX: should do something? */;
3138 CHECK_STACK(&td, 1);
3139 token = read32 (td.ip + 1);
3140 klass = mono_class_get_full (image, token, generic_context);
3141 if (klass->valuetype) {
3142 ADD_CODE (&td, MINT_INITOBJ);
3143 i32 = mono_class_value_size (klass, NULL);
3144 WRITE32 (&td, &i32);
3146 ADD_CODE (&td, MINT_LDNULL);
3147 ADD_CODE (&td, MINT_STIND_REF);
3153 CHECK_STACK(&td, 3);
3154 /* FIX? convert length to I8? */
3155 ADD_CODE(&td, MINT_CPBLK);
3163 case CEE_CONSTRAINED_:
3164 token = read32 (td.ip + 1);
3165 constrained_class = mono_class_get_full (image, token, generic_context);
3166 mono_class_init (constrained_class);
3170 CHECK_STACK(&td, 3);
3171 ADD_CODE(&td, MINT_INITBLK);
3177 /* FIXME: implement */
3182 SIMPLE_OP (td, MINT_RETHROW);
3183 generating_code = 0;
3187 token = read32 (td.ip + 1);
3189 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC && !image_is_dynamic (method->klass->image) && !generic_context) {
3191 MonoType *type = mono_type_create_from_typespec (image, token);
3192 size = mono_type_size (type, &align);
3195 MonoClass *szclass = mono_class_get_full (image, token, generic_context);
3196 mono_class_init (szclass);
3198 if (!szclass->valuetype)
3199 THROW_EX (mono_exception_from_name (mono_defaults.corlib, "System", "InvalidProgramException"), ip - 5);
3201 size = mono_type_size (&szclass->byval_arg, &align);
3203 ADD_CODE(&td, MINT_LDC_I4);
3204 WRITE32(&td, &size);
3205 PUSH_SIMPLE_TYPE(&td, STACK_TYPE_I4);
3209 case CEE_REFANYTYPE: ves_abort(); break;
3212 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);
3216 g_error ("transform.c: Unimplemented opcode: %02x at 0x%x\n", *td.ip, td.ip-header->code);
3219 if (td.new_ip - td.new_code != new_in_start_offset)
3220 td.last_new_ip = td.new_code + new_in_start_offset;
3221 else if (td.is_bb_start [td.in_start - td.il_code])
3222 td.is_bb_start [td.ip - td.il_code] = 1;
3224 td.last_ip = td.in_start;
3227 if (mono_interp_traceopt) {
3228 const guint16 *p = td.new_code;
3229 printf("Runtime method: %p, VT stack size: %d\n", rtm, td.max_vt_sp);
3230 printf("Calculated stack size: %d, stated size: %d\n", td.max_stack_height, header->max_stack);
3231 while (p < td.new_ip) {
3232 p = mono_interp_dis_mintop(td.new_code, p);
3236 g_assert (td.max_stack_height <= (header->max_stack + 1));
3238 rtm->clauses = mono_domain_alloc0 (domain, header->num_clauses * sizeof (MonoExceptionClause));
3239 memcpy (rtm->clauses, header->clauses, header->num_clauses * sizeof(MonoExceptionClause));
3240 rtm->code = mono_domain_alloc0 (domain, (td.new_ip - td.new_code) * sizeof (gushort));
3241 memcpy (rtm->code, td.new_code, (td.new_ip - td.new_code) * sizeof(gushort));
3242 g_free (td.new_code);
3243 rtm->new_body_start = rtm->code + body_start_offset;
3244 rtm->num_clauses = header->num_clauses;
3245 for (i = 0; i < header->num_clauses; i++) {
3246 MonoExceptionClause *c = rtm->clauses + i;
3247 int end_off = c->try_offset + c->try_len;
3248 c->try_offset = td.in_offsets [c->try_offset];
3249 c->try_len = td.in_offsets [end_off] - c->try_offset;
3250 end_off = c->handler_offset + c->handler_len;
3251 c->handler_offset = td.in_offsets [c->handler_offset];
3252 c->handler_len = td.in_offsets [end_off] - c->handler_offset;
3253 if (c->flags & MONO_EXCEPTION_CLAUSE_FILTER)
3254 c->data.filter_offset = td.in_offsets [c->data.filter_offset];
3256 rtm->vt_stack_size = td.max_vt_sp;
3257 rtm->alloca_size = rtm->locals_size + rtm->args_size + rtm->vt_stack_size + rtm->stack_size;
3258 rtm->data_items = mono_domain_alloc0 (domain, td.n_data_items * sizeof (td.data_items [0]));
3259 memcpy (rtm->data_items, td.data_items, td.n_data_items * sizeof (td.data_items [0]));
3260 g_free (td.in_offsets);
3261 g_free (td.forward_refs);
3262 for (i = 0; i < header->code_size; ++i)
3263 g_free (td.stack_state [i]);
3264 g_free (td.stack_state);
3265 g_free (td.stack_height);
3266 g_free (td.vt_stack_size);
3267 g_free (td.data_items);
3269 g_hash_table_destroy (td.data_hash);
3272 static mono_mutex_t calc_section;
3275 mono_interp_transform_init (void)
3277 mono_os_mutex_init_recursive(&calc_section);
3281 mono_interp_transform_method (RuntimeMethod *runtime_method, ThreadContext *context)
3283 int i, align, size, offset;
3284 MonoMethod *method = runtime_method->method;
3285 MonoImage *image = method->klass->image;
3286 MonoMethodHeader *header = mono_method_get_header (method);
3287 MonoMethodSignature *signature = mono_method_signature (method);
3288 register const unsigned char *ip, *end;
3289 const MonoOpcode *opcode;
3292 MonoDomain *domain = mono_domain_get ();
3293 unsigned char *is_bb_start;
3295 MonoVTable *method_class_vt;
3297 MonoGenericContext *generic_context = NULL;
3299 // g_printerr ("TRANSFORM(0x%016lx): begin %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3300 method_class_vt = mono_class_vtable (domain, runtime_method->method->klass);
3301 if (!method_class_vt->initialized) {
3304 MonoInvocation *last_env_frame = context->env_frame;
3305 jmp_buf *old_env = context->current_env;
3306 error_init (&error);
3309 MonoException *failed = context->env_frame->ex;
3310 context->env_frame->ex = NULL;
3311 context->env_frame = last_env_frame;
3312 context->current_env = old_env;
3315 context->env_frame = context->current_frame;
3316 context->current_env = &env;
3317 mono_runtime_class_init_full (method_class_vt, &error);
3318 if (!mono_error_ok (&error)) {
3319 return mono_error_convert_to_exception (&error);
3321 context->env_frame = last_env_frame;
3322 context->current_env = old_env;
3325 mono_profiler_method_jit (method); /* sort of... */
3327 if (mono_method_signature (method)->is_inflated)
3328 generic_context = mono_method_get_context (method);
3330 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
3331 if (generic_container)
3332 generic_context = &generic_container->context;
3335 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
3336 MonoMethod *nm = NULL;
3337 mono_os_mutex_lock(&calc_section);
3338 if (runtime_method->transformed) {
3339 mono_os_mutex_unlock(&calc_section);
3340 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3344 /* assumes all internal calls with an array this are built in... */
3345 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL && (! mono_method_signature (method)->hasthis || method->klass->rank == 0)) {
3346 nm = mono_marshal_get_native_wrapper (method, TRUE, FALSE);
3347 signature = mono_method_signature (nm);
3349 const char *name = method->name;
3350 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
3351 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
3352 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("ves_icall_mono_delegate_ctor");
3354 char *wrapper_name = g_strdup_printf ("__icall_wrapper_%s", mi->name);
3355 nm = mono_marshal_get_icall_wrapper (mi->sig, wrapper_name, mi->func, TRUE);
3356 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
3357 nm = mono_marshal_get_delegate_invoke (method, NULL);
3358 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
3359 nm = mono_marshal_get_delegate_begin_invoke (method);
3360 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
3361 nm = mono_marshal_get_delegate_end_invoke (method);
3365 runtime_method->code = g_malloc(sizeof(short));
3366 runtime_method->code[0] = MINT_CALLRUN;
3370 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3371 runtime_method->alloca_size = runtime_method->stack_size;
3372 runtime_method->transformed = TRUE;
3373 mono_os_mutex_unlock(&calc_section);
3374 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3378 header = mono_method_get_header (nm);
3379 mono_os_mutex_unlock(&calc_section);
3380 } else if (method->klass == mono_defaults.array_class) {
3381 if (!strcmp (method->name, "UnsafeMov")) {
3382 mono_os_mutex_lock (&calc_section);
3383 if (!runtime_method->transformed) {
3384 runtime_method->code = g_malloc (sizeof (short));
3385 runtime_method->code[0] = MINT_CALLRUN;
3386 runtime_method->stack_size = sizeof (stackval); /* for tracing */
3387 runtime_method->alloca_size = runtime_method->stack_size;
3388 runtime_method->transformed = TRUE;
3390 mono_os_mutex_unlock(&calc_section);
3391 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3393 } else if (!strcmp (method->name, "UnsafeStore)")) {
3395 } else if (!strcmp (method->name, "UnsafeLoad)")) {
3399 g_assert ((signature->param_count + signature->hasthis) < 1000);
3400 g_assert (header->max_stack < 10000);
3401 /* intern the strings in the method. */
3403 end = ip + header->code_size;
3405 is_bb_start = g_malloc0(header->code_size);
3406 is_bb_start [0] = 1;
3413 else if (in == 0xf0) {
3415 in = *ip + MONO_CEE_MONO_ICALL;
3417 opcode = &mono_opcodes [in];
3418 switch (opcode->argument) {
3419 case MonoInlineNone:
3422 case MonoInlineString:
3423 if (method->wrapper_type == MONO_WRAPPER_NONE)
3424 mono_ldstr (domain, image, mono_metadata_token_index (read32 (ip + 1)));
3427 case MonoInlineType:
3428 if (method->wrapper_type == MONO_WRAPPER_NONE) {
3429 class = mono_class_get_full (image, read32 (ip + 1), generic_context);
3430 mono_class_init (class);
3431 /* quick fix to not do this for the fake ptr classes - probably should not be getting the vtable at all here */
3433 g_error ("FIXME: interface method lookup: %s (in method %s)", class->name, method->name);
3434 if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE) && class->interface_offsets != NULL)
3435 mono_class_vtable (domain, class);
3440 case MonoInlineMethod:
3441 if (method->wrapper_type == MONO_WRAPPER_NONE && *ip != CEE_CALLI) {
3442 m = mono_get_method_full (image, read32 (ip + 1), NULL, generic_context);
3444 g_free (is_bb_start);
3445 g_error ("FIXME: where to get method and class string?");
3447 // return mono_get_exception_missing_method ();
3449 mono_class_init (m->klass);
3450 if (!mono_class_is_interface (m->klass))
3451 mono_class_vtable (domain, m->klass);
3455 case MonoInlineField:
3459 case MonoShortInlineR:
3462 case MonoInlineBrTarget:
3463 offset = read32 (ip + 1);
3465 backwards = offset < 0;
3466 offset += ip - header->code;
3467 g_assert (offset >= 0 && offset < header->code_size);
3468 is_bb_start [offset] |= backwards ? 2 : 1;
3470 case MonoShortInlineBrTarget:
3471 offset = ((gint8 *)ip) [1];
3473 backwards = offset < 0;
3474 offset += ip - header->code;
3475 g_assert (offset >= 0 && offset < header->code_size);
3476 is_bb_start [offset] |= backwards ? 2 : 1;
3481 case MonoShortInlineVar:
3482 case MonoShortInlineI:
3485 case MonoInlineSwitch: {
3487 const unsigned char *next_ip;
3491 next_ip = ip + 4 * n;
3492 for (i = 0; i < n; i++) {
3493 offset = read32 (ip);
3494 backwards = offset < 0;
3495 offset += next_ip - header->code;
3496 g_assert (offset >= 0 && offset < header->code_size);
3497 is_bb_start [offset] |= backwards ? 2 : 1;
3507 g_assert_not_reached ();
3510 // g_printerr ("TRANSFORM(0x%016lx): end %s::%s\n", mono_thread_current (), method->klass->name, method->name);
3512 /* the rest needs to be locked so it is only done once */
3513 mono_os_mutex_lock(&calc_section);
3514 if (runtime_method->transformed) {
3515 mono_os_mutex_unlock(&calc_section);
3516 g_free (is_bb_start);
3517 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3521 runtime_method->local_offsets = g_malloc (header->num_locals * sizeof(guint32));
3522 runtime_method->stack_size = (sizeof (stackval) + 2) * header->max_stack; /* + 1 for returns of called functions + 1 for 0-ing in trace*/
3523 runtime_method->stack_size = (runtime_method->stack_size + 7) & ~7;
3525 for (i = 0; i < header->num_locals; ++i) {
3526 size = mono_type_size (header->locals [i], &align);
3527 offset += align - 1;
3528 offset &= ~(align - 1);
3529 runtime_method->local_offsets [i] = offset;
3532 offset = (offset + 7) & ~7;
3533 runtime_method->locals_size = offset;
3534 g_assert (runtime_method->locals_size < 65536);
3536 runtime_method->arg_offsets = g_malloc ((!!signature->hasthis + signature->param_count) * sizeof(guint32));
3538 if (signature->hasthis) {
3539 g_assert (!signature->pinvoke);
3540 size = mono_type_stack_size (&method->klass->byval_arg, &align);
3541 offset += align - 1;
3542 offset &= ~(align - 1);
3543 runtime_method->arg_offsets [0] = offset;
3547 for (i = 0; i < signature->param_count; ++i) {
3548 if (signature->pinvoke) {
3550 size = mono_type_native_stack_size (signature->params [i], &dummy);
3554 size = mono_type_stack_size (signature->params [i], &align);
3555 offset += align - 1;
3556 offset &= ~(align - 1);
3557 runtime_method->arg_offsets [i + !!signature->hasthis] = offset;
3560 offset = (offset + 7) & ~7;
3561 runtime_method->args_size = offset;
3562 g_assert (runtime_method->args_size < 10000);
3564 generate (method, runtime_method, is_bb_start, generic_context);
3566 g_free (is_bb_start);
3568 mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_OK);
3569 runtime_method->transformed = TRUE;
3570 mono_os_mutex_unlock(&calc_section);